summaryrefslogtreecommitdiff
path: root/capplet/gsp-app-manager.c
blob: 7e245db1d788360f327e7436bfc5654f866f4c88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 1999 Free Software Foundation, Inc.
 * Copyright (C) 2007, 2009 Vincent Untz.
 * Copyright (C) 2008 Lucas Rocha.
 * Copyright (C) 2008 William Jon McCann <jmccann@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include <string.h>

#include "gsm-util.h"
#include "gsp-app.h"

#include "gsp-app-manager.h"

static GspAppManager *manager = NULL;

typedef struct {
        char         *dir;
        int           index;
        GFileMonitor *monitor;
} GspXdgDir;

typedef struct {
        GSList *apps;
        GSList *dirs;
} GspAppManagerPrivate;

enum {
        ADDED,
        REMOVED,
        LAST_SIGNAL
};

static guint gsp_app_manager_signals[LAST_SIGNAL] = { 0 };


G_DEFINE_TYPE_WITH_PRIVATE (GspAppManager, gsp_app_manager, G_TYPE_OBJECT)

static void     gsp_app_manager_dispose      (GObject       *object);
static void     gsp_app_manager_finalize     (GObject       *object);
static void     _gsp_app_manager_app_unref   (GspApp        *app,
                                              GspAppManager *manager);
static void     _gsp_app_manager_app_removed (GspAppManager *manager,
                                              GspApp        *app);

static GspXdgDir *
_gsp_xdg_dir_new (const char *dir,
                  int         index)
{
        GspXdgDir *xdgdir;

        xdgdir = g_slice_new (GspXdgDir);

        xdgdir->dir = g_strdup (dir);
        xdgdir->index = index;
        xdgdir->monitor = NULL;

        return xdgdir;
}

static void
_gsp_xdg_dir_free (GspXdgDir *xdgdir)
{
        if (xdgdir->dir) {
                g_free (xdgdir->dir);
                xdgdir->dir = NULL;
        }

        if (xdgdir->monitor) {
                g_file_monitor_cancel (xdgdir->monitor);
                g_object_unref (xdgdir->monitor);
                xdgdir->monitor = NULL;
        }

        g_slice_free (GspXdgDir, xdgdir);
}

static void
gsp_app_manager_class_init (GspAppManagerClass *class)
{
        GObjectClass *gobject_class = G_OBJECT_CLASS (class);

        gobject_class->dispose  = gsp_app_manager_dispose;
        gobject_class->finalize = gsp_app_manager_finalize;

        gsp_app_manager_signals[ADDED] =
                g_signal_new ("added",
                              G_TYPE_FROM_CLASS (gobject_class),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (GspAppManagerClass,
                                               added),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__OBJECT,
                              G_TYPE_NONE, 1, G_TYPE_OBJECT);

        gsp_app_manager_signals[REMOVED] =
                g_signal_new ("removed",
                              G_TYPE_FROM_CLASS (gobject_class),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (GspAppManagerClass,
                                               removed),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__OBJECT,
                              G_TYPE_NONE, 1, G_TYPE_OBJECT);

}

static void
gsp_app_manager_init (GspAppManager *manager)
{
        GspAppManagerPrivate *priv;
        priv = gsp_app_manager_get_instance_private (manager);

        // is needed?
        memset (priv, 0, sizeof (GspAppManagerPrivate));
}

static void
gsp_app_manager_dispose (GObject *object)
{
        GspAppManager *manager;
        GspAppManagerPrivate *priv;

        g_return_if_fail (object != NULL);
        g_return_if_fail (GSP_IS_APP_MANAGER (object));

        manager = GSP_APP_MANAGER (object);
        priv = gsp_app_manager_get_instance_private (manager);

        /* we unref GspApp objects in dispose since they might need to
         * reference us during their dispose/finalize */
        g_slist_foreach (priv->apps,
                         (GFunc) _gsp_app_manager_app_unref, manager);
        g_slist_free (priv->apps);
        priv->apps = NULL;

        G_OBJECT_CLASS (gsp_app_manager_parent_class)->dispose (object);
}

static void
gsp_app_manager_finalize (GObject *object)
{
        GspAppManager *manager;
        GspAppManagerPrivate *priv;

        g_return_if_fail (object != NULL);
        g_return_if_fail (GSP_IS_APP_MANAGER (object));

        manager = GSP_APP_MANAGER (object);
        priv = gsp_app_manager_get_instance_private (manager);

        g_slist_foreach (priv->dirs,
                         (GFunc) _gsp_xdg_dir_free, NULL);
        g_slist_free (priv->dirs);
        priv->dirs = NULL;

        G_OBJECT_CLASS (gsp_app_manager_parent_class)->finalize (object);

        manager = NULL;
}

static void
_gsp_app_manager_emit_added (GspAppManager *manager,
                             GspApp        *app)
{
        g_signal_emit (G_OBJECT (manager), gsp_app_manager_signals[ADDED],
                       0, app);
}

static void
_gsp_app_manager_emit_removed (GspAppManager *manager,
                               GspApp        *app)
{
        g_signal_emit (G_OBJECT (manager), gsp_app_manager_signals[REMOVED],
                       0, app);
}

/*
 * Directories
 */

static int
gsp_app_manager_get_dir_index (GspAppManager *manager,
                               const char    *dir)
{
        GSList    *l;
        GspXdgDir *xdgdir;
        GspAppManagerPrivate *priv;

        g_return_val_if_fail (GSP_IS_APP_MANAGER (manager), -1);
        g_return_val_if_fail (dir != NULL, -1);

        priv = gsp_app_manager_get_instance_private (manager);

        for (l = priv->dirs; l != NULL; l = l->next) {
                xdgdir = l->data;
                if (strcmp (dir, xdgdir->dir) == 0) {
                        return xdgdir->index;
                }
        }

        return -1;
}

const char *
gsp_app_manager_get_dir (GspAppManager *manager,
                         unsigned int   index)
{
        GSList    *l;
        GspXdgDir *xdgdir;
        GspAppManagerPrivate *priv;

        g_return_val_if_fail (GSP_IS_APP_MANAGER (manager), NULL);

        priv = gsp_app_manager_get_instance_private (manager);

        for (l = priv->dirs; l != NULL; l = l->next) {
                xdgdir = l->data;
                if (index == xdgdir->index) {
                        return xdgdir->dir;
                }
        }

        return NULL;
}

static int
_gsp_app_manager_find_dir_with_basename (GspAppManager *manager,
                                         const char    *basename,
                                         int            minimum_index)
{
        GSList    *l;
        GspXdgDir *xdgdir;
        char      *path;
        GKeyFile  *keyfile;
        int        result = -1;
        GspAppManagerPrivate *priv;

        path = NULL;
        keyfile = g_key_file_new ();
        priv = gsp_app_manager_get_instance_private (manager);

        for (l = priv->dirs; l != NULL; l = l->next) {
                xdgdir = l->data;

                if (xdgdir->index <= minimum_index) {
                        continue;
                }

                g_free (path);
                path = g_build_filename (xdgdir->dir, basename, NULL);
                if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
                        continue;
                }

                if (!g_key_file_load_from_file (keyfile, path,
                                                G_KEY_FILE_NONE, NULL)) {
                        continue;
                }

                /* the file exists and is readable */
                if (result == -1) {
                        result = xdgdir->index;
                } else {
                        result = MIN (result, xdgdir->index);
                }
        }

        g_key_file_free (keyfile);
        g_free (path);

        return result;
}

static void
_gsp_app_manager_handle_delete (GspAppManager *manager,
                                GspApp        *app,
                                const char    *basename,
                                int            index)
{
        unsigned int position;
        unsigned int system_position;

        position = gsp_app_get_xdg_position (app);
        system_position = gsp_app_get_xdg_system_position (app);

        if (system_position < index) {
                /* it got deleted, but we don't even care about it */
                return;
        }

        if (index < position) {
                /* it got deleted, but in a position earlier than the current
                 * one. This happens when the user file was changed and became
                 * identical to the system file; in this case, the user file is
                 * simply removed. */
                 g_assert (index == 0);
                 return;
        }

        if (position == index &&
            (system_position == index || system_position == G_MAXUINT)) {
                /* the file used by the user was deleted, and there's no other
                 * file in system directories. So it really got deleted. */
                _gsp_app_manager_app_removed (manager, app);
                return;
        }

        if (system_position == index) {
                /* then we know that position != index; we just hae to tell
                 * GspApp if there's still a system directory containing this
                 * basename */
                int new_system;

                new_system = _gsp_app_manager_find_dir_with_basename (manager,
                                                                      basename,
                                                                      index);
                if (new_system < 0) {
                        gsp_app_set_xdg_system_position (app, G_MAXUINT);
                } else {
                        gsp_app_set_xdg_system_position (app, new_system);
                }

                return;
        }

        if (position == index) {
                /* then we know that system_position != G_MAXUINT; we need to
                 * tell GspApp to change position to system_position */
                const char *dir;

                dir = gsp_app_manager_get_dir (manager, system_position);
                if (dir) {
                        char *path;

                        path = g_build_filename (dir, basename, NULL);
                        gsp_app_reload_at (app, path,
                                           (unsigned int) system_position);
                        g_free (path);
                } else {
                        _gsp_app_manager_app_removed (manager, app);
                }

                return;
        }

        g_assert_not_reached ();
}

static gboolean
gsp_app_manager_xdg_dir_monitor (GFileMonitor      *monitor,
                                 GFile             *child,
                                 GFile             *other_file,
                                 GFileMonitorEvent  flags,
                                 gpointer           data)
{
        GspAppManager *manager;
        GspApp        *old_app;
        GspApp        *app;
        GFile         *parent;
        char          *basename;
        char          *dir;
        char          *path;
        int            index;

        manager = GSP_APP_MANAGER (data);

        basename = g_file_get_basename (child);
        if (!g_str_has_suffix (basename, ".desktop")) {
                /* not a desktop file, we can ignore */
                g_free (basename);
                return TRUE;
        }
        old_app = gsp_app_manager_find_app_with_basename (manager, basename);

        parent = g_file_get_parent (child);
        dir = g_file_get_path (parent);
        g_object_unref (parent);

        index = gsp_app_manager_get_dir_index (manager, dir);
        if (index < 0) {
                /* not a directory we know; should never happen, though */
                g_free (dir);
                return TRUE;
        }

        path = g_file_get_path (child);

        switch (flags) {
        case G_FILE_MONITOR_EVENT_CHANGED:
        case G_FILE_MONITOR_EVENT_CREATED:
                /* we just do as if it was a new file: GspApp is clever enough
                 * to do the right thing */
                app = gsp_app_new (path, (unsigned int) index);

                /* we didn't have this app before, so add it */
                if (old_app == NULL && app != NULL) {
                        gsp_app_manager_add (manager, app);
                        g_object_unref (app);
                }
                /* else: it was just updated, GspApp took care of
                 * sending the event */
                break;
        case G_FILE_MONITOR_EVENT_DELETED:
                if (!old_app) {
                        /* it got deleted, but we don't know about it, so
                         * nothing to do */
                        break;
                }

                _gsp_app_manager_handle_delete (manager, old_app,
                                                basename, index);
                break;
        default:
                break;
        }

        g_free (path);
        g_free (dir);
        g_free (basename);

        return TRUE;
}

/*
 * Initialization
 */

static void
_gsp_app_manager_fill_from_dir (GspAppManager *manager,
                                GspXdgDir     *xdgdir)
{
        GFile      *file;
        GDir       *dir;
        const char *name;

        file = g_file_new_for_path (xdgdir->dir);
        xdgdir->monitor = g_file_monitor_directory (file, G_FILE_MONITOR_NONE,
                                                    NULL, NULL);
        g_object_unref (file);

        if (xdgdir->monitor) {
                g_signal_connect (xdgdir->monitor, "changed",
                                  G_CALLBACK (gsp_app_manager_xdg_dir_monitor),
                                  manager);
        }

        dir = g_dir_open (xdgdir->dir, 0, NULL);
        if (!dir) {
                return;
        }

        while ((name = g_dir_read_name (dir))) {
                GspApp *app;
                char   *desktop_file_path;

                if (!g_str_has_suffix (name, ".desktop")) {
                        continue;
                }

                desktop_file_path = g_build_filename (xdgdir->dir, name, NULL);
                app = gsp_app_new (desktop_file_path, xdgdir->index);

                if (app != NULL) {
                        gsp_app_manager_add (manager, app);
                        g_object_unref (app);
                }

                g_free (desktop_file_path);
        }

        g_dir_close (dir);
}

void
gsp_app_manager_fill (GspAppManager *manager)
{
        char **autostart_dirs;
        int    i;
        GspAppManagerPrivate *priv;

        priv = gsp_app_manager_get_instance_private (manager);

        if (priv->apps != NULL)
                return;

        autostart_dirs = gsm_util_get_autostart_dirs ();
        /* we always assume that the first directory is the user one */
        g_assert (g_str_has_prefix (autostart_dirs[0],
                                    g_get_user_config_dir ()));

        for (i = 0; autostart_dirs[i] != NULL; i++) {
                GspXdgDir *xdgdir;

                if (gsp_app_manager_get_dir_index (manager,
                                                   autostart_dirs[i]) >= 0) {
                        continue;
                }

                xdgdir = _gsp_xdg_dir_new (autostart_dirs[i], i);
                priv->dirs = g_slist_prepend (priv->dirs,
                                                       xdgdir);

                _gsp_app_manager_fill_from_dir (manager, xdgdir);
        }

        g_strfreev (autostart_dirs);
}

/*
 * App handling
 */

static void
_gsp_app_manager_app_unref (GspApp        *app,
                            GspAppManager *manager)
{
        g_signal_handlers_disconnect_by_func (app,
                                              _gsp_app_manager_app_removed,
                                              manager);
        g_object_unref (app);
}

static void
_gsp_app_manager_app_removed (GspAppManager *manager,
                              GspApp        *app)
{
        GspAppManagerPrivate *priv;

        _gsp_app_manager_emit_removed (manager, app);
        priv = gsp_app_manager_get_instance_private (manager);
        priv->apps = g_slist_remove (priv->apps, app);
        _gsp_app_manager_app_unref (app, manager);
}

void
gsp_app_manager_add (GspAppManager *manager,
                     GspApp        *app)
{
        GspAppManagerPrivate *priv;

        g_return_if_fail (GSP_IS_APP_MANAGER (manager));
        g_return_if_fail (GSP_IS_APP (app));

        priv = gsp_app_manager_get_instance_private (manager);

        priv->apps = g_slist_prepend (priv->apps,
                                      g_object_ref (app));
        g_signal_connect_swapped (app, "removed",
                                  G_CALLBACK (_gsp_app_manager_app_removed),
                                  manager);
        _gsp_app_manager_emit_added (manager, app);
}

GspApp *
gsp_app_manager_find_app_with_basename (GspAppManager *manager,
                                        const char    *basename)
{
        GSList *l;
        GspApp *app;
        GspAppManagerPrivate *priv;

        g_return_val_if_fail (GSP_IS_APP_MANAGER (manager), NULL);
        g_return_val_if_fail (basename != NULL, NULL);

        priv = gsp_app_manager_get_instance_private (manager);

        for (l = priv->apps; l != NULL; l = l->next) {
                app = GSP_APP (l->data);
                if (strcmp (basename, gsp_app_get_basename (app)) == 0)
                        return app;
        }

        return NULL;
}

/*
 * Singleton
 */

GspAppManager *
gsp_app_manager_get (void)
{
        if (manager == NULL) {
                manager = g_object_new (GSP_TYPE_APP_MANAGER, NULL);
                return manager;
        } else {
                return g_object_ref (manager);
        }
}

GSList *
gsp_app_manager_get_apps (GspAppManager *manager)
{
        GspAppManagerPrivate *priv;

        g_return_val_if_fail (GSP_IS_APP_MANAGER (manager), NULL);

        priv = gsp_app_manager_get_instance_private (manager);

        return g_slist_copy (priv->apps);
}