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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
|
/*
The indicator application visualization object. It takes the information
given by the service and turns it into real-world pixels that users can
actually use. Well, GTK does that, but this asks nicely.
Copyright 2009 Canonical Ltd.
Authors:
Ted Gould <ted@canonical.com>
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3, as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/* G Stuff */
#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>
#include <gtk/gtk.h>
/* DBus Stuff */
#ifdef HAVE_GTK3
#include <libdbusmenu-gtk3/menu.h>
#else
#include <libdbusmenu-gtk/menu.h>
#endif
/* Indicator Stuff */
#include <libindicator/indicator.h>
#include <libindicator/indicator-object.h>
#include <libindicator/indicator-service-manager.h>
#include <libindicator/indicator-image-helper.h>
/* Local Stuff */
#include "dbus-shared.h"
#include "gen-application-service.xml.h"
#include "application-service-marshal.h"
#define PANEL_ICON_SUFFIX "panel"
#define INDICATOR_APPLICATION_TYPE (indicator_application_get_type ())
#define INDICATOR_APPLICATION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), INDICATOR_APPLICATION_TYPE, IndicatorApplication))
#define INDICATOR_APPLICATION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), INDICATOR_APPLICATION_TYPE, IndicatorApplicationClass))
#define IS_INDICATOR_APPLICATION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), INDICATOR_APPLICATION_TYPE))
#define IS_INDICATOR_APPLICATION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), INDICATOR_APPLICATION_TYPE))
#define INDICATOR_APPLICATION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), INDICATOR_APPLICATION_TYPE, IndicatorApplicationClass))
typedef struct _IndicatorApplication IndicatorApplication;
typedef struct _IndicatorApplicationClass IndicatorApplicationClass;
struct _IndicatorApplicationClass {
IndicatorObjectClass parent_class;
};
struct _IndicatorApplication {
IndicatorObject parent;
};
GType indicator_application_get_type (void);
INDICATOR_SET_VERSION
INDICATOR_SET_TYPE(INDICATOR_APPLICATION_TYPE)
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
typedef struct _IndicatorApplicationPrivate IndicatorApplicationPrivate;
struct _IndicatorApplicationPrivate {
IndicatorServiceManager * sm;
GCancellable * service_proxy_cancel;
GDBusProxy * service_proxy;
GList * applications;
GHashTable * theme_dirs;
guint disconnect_kill;
GCancellable * get_apps_cancel;
};
typedef struct _ApplicationEntry ApplicationEntry;
struct _ApplicationEntry {
IndicatorObjectEntry entry;
gchar * icon_theme_path;
gboolean old_service;
gchar * dbusobject;
gchar * dbusaddress;
gchar * guide;
gchar * longname;
};
#define INDICATOR_APPLICATION_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), INDICATOR_APPLICATION_TYPE, IndicatorApplicationPrivate))
static void indicator_application_class_init (IndicatorApplicationClass *klass);
static void indicator_application_init (IndicatorApplication *self);
static void indicator_application_dispose (GObject *object);
static void indicator_application_finalize (GObject *object);
static GList * get_entries (IndicatorObject * io);
static guint get_location (IndicatorObject * io, IndicatorObjectEntry * entry);
static void entry_scrolled (IndicatorObject * io, IndicatorObjectEntry * entry, gint delta, IndicatorScrollDirection direction);
void connection_changed (IndicatorServiceManager * sm, gboolean connected, IndicatorApplication * application);
static void connected (IndicatorApplication * application);
static void disconnected (IndicatorApplication * application);
static void disconnected_helper (gpointer data, gpointer user_data);
static gboolean disconnected_kill (gpointer user_data);
static void disconnected_kill_helper (gpointer data, gpointer user_data);
static void application_added (IndicatorApplication * application, const gchar * iconname, gint position, const gchar * dbusaddress, const gchar * dbusobject, const gchar * icon_theme_path, const gchar * label, const gchar * guide, const gchar * accessible_desc, const gchar * hint);
static void application_removed (IndicatorApplication * application, gint position);
static void application_label_changed (IndicatorApplication * application, gint position, const gchar * label, const gchar * guide);
static void application_icon_changed (IndicatorApplication * application, gint position, const gchar * iconname, const gchar * icondesc);
static void application_icon_theme_path_changed (IndicatorApplication * application, gint position, const gchar * icon_theme_path);
static void get_applications (GObject * obj, GAsyncResult * res, gpointer user_data);
static void get_applications_helper (IndicatorApplication * self, GVariant * variant);
static void theme_dir_unref(IndicatorApplication * ia, const gchar * dir);
static void theme_dir_ref(IndicatorApplication * ia, const gchar * dir);
static void service_proxy_cb (GObject * object, GAsyncResult * res, gpointer user_data);
static void receive_signal (GDBusProxy * proxy, gchar * sender_name, gchar * signal_name, GVariant * parameters, gpointer user_data);
G_DEFINE_TYPE (IndicatorApplication, indicator_application, INDICATOR_OBJECT_TYPE);
static void
indicator_application_class_init (IndicatorApplicationClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (IndicatorApplicationPrivate));
object_class->dispose = indicator_application_dispose;
object_class->finalize = indicator_application_finalize;
IndicatorObjectClass * io_class = INDICATOR_OBJECT_CLASS(klass);
io_class->get_entries = get_entries;
io_class->get_location = get_location;
io_class->entry_scrolled = entry_scrolled;
return;
}
static void
indicator_application_init (IndicatorApplication *self)
{
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(self);
/* These are built in the connection phase */
priv->service_proxy_cancel = NULL;
priv->service_proxy = NULL;
priv->theme_dirs = NULL;
priv->disconnect_kill = 0;
priv->sm = indicator_service_manager_new(INDICATOR_APPLICATION_DBUS_ADDR);
g_signal_connect(G_OBJECT(priv->sm), INDICATOR_SERVICE_MANAGER_SIGNAL_CONNECTION_CHANGE, G_CALLBACK(connection_changed), self);
priv->applications = NULL;
priv->theme_dirs = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
priv->get_apps_cancel = NULL;
return;
}
static void
indicator_application_dispose (GObject *object)
{
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(object);
if (priv->disconnect_kill != 0) {
g_source_remove(priv->disconnect_kill);
}
if (priv->get_apps_cancel != NULL) {
g_cancellable_cancel(priv->get_apps_cancel);
g_object_unref(priv->get_apps_cancel);
priv->get_apps_cancel = NULL;
}
while (priv->applications != NULL) {
application_removed(INDICATOR_APPLICATION(object),
0);
}
if (priv->sm != NULL) {
g_object_unref(priv->sm);
priv->sm = NULL;
}
if (priv->service_proxy != NULL) {
g_object_unref(G_OBJECT(priv->service_proxy));
priv->service_proxy = NULL;
}
if (priv->service_proxy_cancel != NULL) {
g_cancellable_cancel(priv->service_proxy_cancel);
g_object_unref(priv->service_proxy_cancel);
priv->service_proxy_cancel = NULL;
}
if (priv->theme_dirs != NULL) {
while (g_hash_table_size(priv->theme_dirs)) {
GList * keys = g_hash_table_get_keys(priv->theme_dirs);
theme_dir_unref(INDICATOR_APPLICATION(object), (gchar *)keys->data);
}
g_hash_table_destroy(priv->theme_dirs);
priv->theme_dirs = NULL;
}
G_OBJECT_CLASS (indicator_application_parent_class)->dispose (object);
return;
}
static void
indicator_application_finalize (GObject *object)
{
G_OBJECT_CLASS (indicator_application_parent_class)->finalize (object);
return;
}
/* Responds to connection change event from the service manager and
splits it into two. */
void
connection_changed (IndicatorServiceManager * sm, gboolean connect, IndicatorApplication * application)
{
g_return_if_fail(IS_INDICATOR_APPLICATION(application));
if (connect) {
connected(application);
} else {
disconnected(application);
}
return;
}
/* Brings up the connection to a service that has just come onto the
bus, or is atleast new to us. */
void
connected (IndicatorApplication * application)
{
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(application);
g_debug("Connected to Application Indicator Service.");
if (priv->service_proxy_cancel == NULL && priv->service_proxy == NULL) {
/* Build the service proxy */
priv->service_proxy_cancel = g_cancellable_new();
g_dbus_proxy_new_for_bus(G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
INDICATOR_APPLICATION_DBUS_ADDR,
INDICATOR_APPLICATION_DBUS_OBJ,
INDICATOR_APPLICATION_DBUS_IFACE,
priv->service_proxy_cancel,
service_proxy_cb,
application);
}
return;
}
/* Callback from trying to create the proxy for the service, this
could include starting the service. */
static void
service_proxy_cb (GObject * object, GAsyncResult * res, gpointer user_data)
{
GError * error = NULL;
IndicatorApplication * self = INDICATOR_APPLICATION(user_data);
g_return_if_fail(self != NULL);
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(self);
GDBusProxy * proxy = g_dbus_proxy_new_for_bus_finish(res, &error);
if (priv->service_proxy_cancel != NULL) {
g_object_unref(priv->service_proxy_cancel);
priv->service_proxy_cancel = NULL;
}
if (error != NULL) {
g_critical("Could not grab DBus proxy for %s: %s", INDICATOR_APPLICATION_DBUS_ADDR, error->message);
g_error_free(error);
return;
}
/* Okay, we're good to grab the proxy at this point, we're
sure that it's ours. */
priv->service_proxy = proxy;
g_signal_connect(proxy, "g-signal", G_CALLBACK(receive_signal), self);
/* We shouldn't be in a situation where we've already
called this function. It doesn't *hurt* anything, but
man we should look into it more. */
if (priv->get_apps_cancel != NULL) {
g_warning("Already getting applications? Odd.");
return;
}
priv->get_apps_cancel = g_cancellable_new();
/* Query it for existing applications */
g_debug("Request current apps");
g_dbus_proxy_call(priv->service_proxy, "GetApplications", NULL,
G_DBUS_CALL_FLAGS_NONE, -1, priv->get_apps_cancel,
get_applications, self);
return;
}
/* Marks every current application as belonging to the old
service so that we can delete it if it doesn't come back.
Also, sets up a timeout on comming back. */
static void
disconnected (IndicatorApplication * application)
{
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(application);
g_list_foreach(priv->applications, disconnected_helper, application);
/* I'll like this to be a little shorter, but it's a bit
inpractical to make it so. This means that the user will
probably notice a visible glitch. Though, if applications
are disappearing there isn't much we can do. */
priv->disconnect_kill = g_timeout_add(250, disconnected_kill, application);
return;
}
/* Marks an entry as being from the old service */
static void
disconnected_helper (gpointer data, gpointer user_data)
{
ApplicationEntry * entry = (ApplicationEntry *)data;
entry->old_service = TRUE;
return;
}
/* Makes sure the old applications that don't come back
get dropped. */
static gboolean
disconnected_kill (gpointer user_data)
{
g_return_val_if_fail(IS_INDICATOR_APPLICATION(user_data), FALSE);
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(user_data);
priv->disconnect_kill = 0;
g_list_foreach(priv->applications, disconnected_kill_helper, user_data);
return FALSE;
}
/* Looks for entries that are still associated with the
old service and removes them. */
static void
disconnected_kill_helper (gpointer data, gpointer user_data)
{
g_return_if_fail(IS_INDICATOR_APPLICATION(user_data));
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(user_data);
ApplicationEntry * entry = (ApplicationEntry *)data;
if (entry->old_service) {
application_removed(INDICATOR_APPLICATION(user_data), g_list_index(priv->applications, data));
}
return;
}
/* Goes through the list of applications that we're maintaining and
pulls out the IndicatorObjectEntry and returns that in a list
for the caller. */
static GList *
get_entries (IndicatorObject * io)
{
g_return_val_if_fail(IS_INDICATOR_APPLICATION(io), NULL);
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(io);
GList * retval = NULL;
GList * apppointer = NULL;
for (apppointer = priv->applications; apppointer != NULL; apppointer = g_list_next(apppointer)) {
IndicatorObjectEntry * entry = &(((ApplicationEntry *)apppointer->data)->entry);
retval = g_list_prepend(retval, entry);
}
if (retval != NULL) {
retval = g_list_reverse(retval);
}
return retval;
}
/* Finds the location of a specific entry */
static guint
get_location (IndicatorObject * io, IndicatorObjectEntry * entry)
{
g_return_val_if_fail(IS_INDICATOR_APPLICATION(io), 0);
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(io);
return g_list_index(priv->applications, entry);
}
/* Redirect the scroll event to the Application Item */
static void entry_scrolled (IndicatorObject * io, IndicatorObjectEntry * entry, gint delta, IndicatorScrollDirection direction) {
g_return_if_fail(IS_INDICATOR_APPLICATION(io));
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(io);
g_return_if_fail(priv->service_proxy);
GList *l = g_list_find(priv->applications, entry);
if (l == NULL)
return;
ApplicationEntry *app = l->data;
if (app && app->dbusaddress && app->dbusobject && priv->service_proxy) {
g_dbus_proxy_call(priv->service_proxy, "ApplicationScrollEvent",
g_variant_new("(ssiu)", app->dbusaddress,
app->dbusobject,
delta, direction),
G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
}
}
/* Does a quick meausre of how big the string is in
pixels with a Pango layout */
static gint
measure_string (GtkStyle * style, PangoContext * context, const gchar * string)
{
PangoLayout * layout = pango_layout_new(context);
pango_layout_set_text(layout, string, -1);
pango_layout_set_font_description(layout, style->font_desc);
gint width;
pango_layout_get_pixel_size(layout, &width, NULL);
g_object_unref(layout);
return width;
}
/* Try to get a good guess at what a maximum width of the entire
string would be. */
static void
guess_label_size (ApplicationEntry * app)
{
/* This is during startup. */
if (app->entry.label == NULL) return;
GtkStyle * style = gtk_widget_get_style(GTK_WIDGET(app->entry.label));
PangoContext * context = gtk_widget_get_pango_context(GTK_WIDGET(app->entry.label));
gint length = measure_string(style, context, gtk_label_get_text(app->entry.label));
if (app->guide != NULL) {
gint guidelen = measure_string(style, context, app->guide);
if (guidelen > length) {
length = guidelen;
}
}
gtk_widget_set_size_request(GTK_WIDGET(app->entry.label), length, -1);
return;
}
/* Here we respond to new applications by building up the
ApplicationEntry and signaling the indicator host that
we've got a new indicator. */
static void
application_added (IndicatorApplication * application, const gchar * iconname, gint position, const gchar * dbusaddress, const gchar * dbusobject, const gchar * icon_theme_path, const gchar * label, const gchar * guide, const gchar * accessible_desc, const gchar * hint)
{
g_return_if_fail(IS_INDICATOR_APPLICATION(application));
g_debug("Building new application entry: %s with icon: %s at position %i", dbusaddress, iconname, position);
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(application);
ApplicationEntry * app = g_new0(ApplicationEntry, 1);
app->old_service = FALSE;
app->icon_theme_path = NULL;
if (icon_theme_path != NULL && icon_theme_path[0] != '\0') {
app->icon_theme_path = g_strdup(icon_theme_path);
theme_dir_ref(application, icon_theme_path);
}
app->dbusaddress = g_strdup(dbusaddress);
app->dbusobject = g_strdup(dbusobject);
app->guide = NULL;
/* We make a long name using the suffix, and if that
icon is available we want to use it. Otherwise we'll
just use the name we were given. */
app->longname = NULL;
if (!g_str_has_suffix(iconname, PANEL_ICON_SUFFIX)) {
app->longname = g_strdup_printf("%s-%s", iconname, PANEL_ICON_SUFFIX);
} else {
app->longname = g_strdup(iconname);
}
app->entry.image = indicator_image_helper(app->longname);
if (label == NULL || label[0] == '\0') {
app->entry.label = NULL;
} else {
app->entry.label = GTK_LABEL(gtk_label_new(label));
g_object_ref(G_OBJECT(app->entry.label));
gtk_widget_show(GTK_WIDGET(app->entry.label));
if (app->guide != NULL) {
g_free(app->guide);
app->guide = NULL;
}
if (guide != NULL) {
app->guide = g_strdup(guide);
}
guess_label_size(app);
}
if (accessible_desc == NULL || accessible_desc[0] == '\0') {
app->entry.accessible_desc = NULL;
} else {
app->entry.accessible_desc = g_strdup(accessible_desc);
}
if (hint == NULL || hint[0] == '\0') {
app->entry.name_hint = NULL;
} else {
app->entry.name_hint = g_strdup(hint);
}
app->entry.menu = GTK_MENU(dbusmenu_gtkmenu_new((gchar *)dbusaddress, (gchar *)dbusobject));
/* Keep copies of these for ourself, just in case. */
g_object_ref(app->entry.image);
g_object_ref(app->entry.menu);
gtk_widget_show(GTK_WIDGET(app->entry.image));
priv->applications = g_list_insert(priv->applications, app, position);
g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED_ID, 0, &(app->entry), TRUE);
return;
}
/* This removes the application from the list and free's all
of the memory associated with it. */
static void
application_removed (IndicatorApplication * application, gint position)
{
g_return_if_fail(IS_INDICATOR_APPLICATION(application));
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(application);
ApplicationEntry * app = (ApplicationEntry *)g_list_nth_data(priv->applications, position);
if (app == NULL) {
g_warning("Unable to find application at position: %d", position);
return;
}
priv->applications = g_list_remove(priv->applications, app);
g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_REMOVED_ID, 0, &(app->entry), TRUE);
if (app->icon_theme_path != NULL) {
theme_dir_unref(application, app->icon_theme_path);
g_free(app->icon_theme_path);
}
if (app->dbusaddress != NULL) {
g_free(app->dbusaddress);
}
if (app->dbusobject != NULL) {
g_free(app->dbusobject);
}
if (app->guide != NULL) {
g_free(app->guide);
}
if (app->longname != NULL) {
g_free(app->longname);
}
if (app->entry.image != NULL) {
g_object_unref(G_OBJECT(app->entry.image));
}
if (app->entry.label != NULL) {
g_object_unref(G_OBJECT(app->entry.label));
}
if (app->entry.menu != NULL) {
g_object_unref(G_OBJECT(app->entry.menu));
}
if (app->entry.accessible_desc != NULL) {
g_free((gchar *)app->entry.accessible_desc);
}
if (app->entry.name_hint != NULL) {
g_free((gchar *)app->entry.name_hint);
}
g_free(app);
return;
}
/* The callback for the signal that the label for an application
has changed. */
static void
application_label_changed (IndicatorApplication * application, gint position, const gchar * label, const gchar * guide)
{
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(application);
ApplicationEntry * app = (ApplicationEntry *)g_list_nth_data(priv->applications, position);
gboolean signal_reload = FALSE;
if (app == NULL) {
g_warning("Unable to find application at position: %d", position);
return;
}
if (label == NULL || label[0] == '\0') {
/* No label, let's see if we need to delete the old one */
if (app->entry.label != NULL) {
g_object_unref(G_OBJECT(app->entry.label));
app->entry.label = NULL;
signal_reload = TRUE;
}
} else {
/* We've got a label, is this just an update or is
it a new thing. */
if (app->entry.label != NULL) {
gtk_label_set_text(app->entry.label, label);
} else {
app->entry.label = GTK_LABEL(gtk_label_new(label));
g_object_ref(G_OBJECT(app->entry.label));
gtk_widget_show(GTK_WIDGET(app->entry.label));
signal_reload = TRUE;
}
}
/* Copy the guide if we have one */
if (app->guide != NULL) {
g_free(app->guide);
app->guide = NULL;
}
if (guide != NULL && guide[0] != '\0') {
app->guide = g_strdup(guide);
}
/* Protected against not having a label */
guess_label_size(app);
if (signal_reload) {
/* Telling the listener that this has been removed, and then
readded to make it reparse the entry. */
if (app->entry.label != NULL) {
gtk_widget_hide(GTK_WIDGET(app->entry.label));
}
if (app->entry.image != NULL) {
gtk_widget_hide(GTK_WIDGET(app->entry.image));
}
if (app->entry.menu != NULL) {
gtk_menu_detach(app->entry.menu);
}
g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_REMOVED_ID, 0, &(app->entry), TRUE);
if (app->entry.label != NULL) {
gtk_widget_show(GTK_WIDGET(app->entry.label));
}
if (app->entry.image != NULL) {
indicator_image_helper_update(app->entry.image, app->longname);
gtk_widget_show(GTK_WIDGET(app->entry.image));
}
g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED_ID, 0, &(app->entry), TRUE);
}
return;
}
/* The callback for the signal that the icon for an application
has changed. */
static void
application_icon_changed (IndicatorApplication * application, gint position, const gchar * iconname, const gchar * icondesc)
{
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(application);
ApplicationEntry * app = (ApplicationEntry *)g_list_nth_data(priv->applications, position);
if (app == NULL) {
g_warning("Unable to find application at position: %d", position);
return;
}
if (iconname == NULL) {
g_warning("We can't have a NULL icon name on %d", position);
return;
}
/* We make a long name using the suffix, and if that
icon is available we want to use it. Otherwise we'll
just use the name we were given. */
if (app->longname != NULL) {
g_free(app->longname);
app->longname = NULL;
}
if (!g_str_has_suffix(iconname, PANEL_ICON_SUFFIX)) {
app->longname = g_strdup_printf("%s-%s", iconname, PANEL_ICON_SUFFIX);
} else {
app->longname = g_strdup(iconname);
}
indicator_image_helper_update(app->entry.image, app->longname);
if (g_strcmp0(app->entry.accessible_desc, icondesc) != 0) {
if (app->entry.accessible_desc != NULL) {
g_free((gchar *)app->entry.accessible_desc);
app->entry.accessible_desc = NULL;
}
if (icondesc != NULL && icondesc[0] != '\0') {
app->entry.accessible_desc = g_strdup(icondesc);
}
g_signal_emit(G_OBJECT(application), INDICATOR_OBJECT_SIGNAL_ACCESSIBLE_DESC_UPDATE_ID, 0, &(app->entry), TRUE);
}
return;
}
/* The callback for the signal that the icon theme path for an application
has changed. */
static void
application_icon_theme_path_changed (IndicatorApplication * application, gint position, const gchar * icon_theme_path)
{
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(application);
ApplicationEntry * app = (ApplicationEntry *)g_list_nth_data(priv->applications, position);
if (app == NULL) {
g_warning("Unable to find application at position: %d", position);
return;
}
if (g_strcmp0(icon_theme_path, app->icon_theme_path) != 0) {
if(app->icon_theme_path != NULL) {
theme_dir_unref(application, app->icon_theme_path);
g_free(app->icon_theme_path);
app->icon_theme_path = NULL;
}
if (icon_theme_path != NULL && icon_theme_path[0] != '\0') {
app->icon_theme_path = g_strdup(icon_theme_path);
theme_dir_ref(application, app->icon_theme_path);
}
indicator_image_helper_update(app->entry.image, app->longname);
}
return;
}
/* Receives all signals from the service, routed to the appropriate functions */
static void
receive_signal (GDBusProxy * proxy, gchar * sender_name, gchar * signal_name,
GVariant * parameters, gpointer user_data)
{
IndicatorApplication * self = INDICATOR_APPLICATION(user_data);
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(self);
/* If we're in the middle of a GetApplications call and we get
any of these our state is probably going to just be confused. Let's
cancel the call we had and try again to try and get a clear answer */
if (priv->get_apps_cancel != NULL) {
g_cancellable_cancel(priv->get_apps_cancel);
g_object_unref(priv->get_apps_cancel);
priv->get_apps_cancel = g_cancellable_new();
g_dbus_proxy_call(priv->service_proxy, "GetApplications", NULL,
G_DBUS_CALL_FLAGS_NONE, -1, priv->get_apps_cancel,
get_applications, self);
return;
}
if (g_strcmp0(signal_name, "ApplicationAdded") == 0) {
const gchar * iconname;
gint position;
const gchar * dbusaddress;
const gchar * dbusobject;
const gchar * icon_theme_path;
const gchar * label;
const gchar * guide;
const gchar * accessible_desc;
const gchar * hint;
g_variant_get (parameters, "(&si&s&o&s&s&s&s&s)", &iconname,
&position, &dbusaddress, &dbusobject,
&icon_theme_path, &label, &guide,
&accessible_desc, &hint);
application_added(self, iconname, position, dbusaddress,
dbusobject, icon_theme_path, label, guide,
accessible_desc, hint);
}
else if (g_strcmp0(signal_name, "ApplicationRemoved") == 0) {
gint position;
g_variant_get (parameters, "(i)", &position);
application_removed(self, position);
}
else if (g_strcmp0(signal_name, "ApplicationIconChanged") == 0) {
gint position;
const gchar * iconname;
const gchar * icondesc;
g_variant_get (parameters, "(i&s&s)", &position, &iconname, &icondesc);
application_icon_changed(self, position, iconname, icondesc);
}
else if (g_strcmp0(signal_name, "ApplicationIconThemePathChanged") == 0) {
gint position;
const gchar * icon_theme_path;
g_variant_get (parameters, "(i&s)", &position, &icon_theme_path);
application_icon_theme_path_changed(self, position, icon_theme_path);
}
else if (g_strcmp0(signal_name, "ApplicationLabelChanged") == 0) {
gint position;
const gchar * label;
const gchar * guide;
g_variant_get (parameters, "(i&s&s)", &position, &label, &guide);
application_label_changed(self, position, label, guide);
}
return;
}
/* This responds to the list of applications that the service
has and calls application_added on each one of them. */
static void
get_applications (GObject * obj, GAsyncResult * res, gpointer user_data)
{
IndicatorApplication * self = INDICATOR_APPLICATION(user_data);
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(self);
GError * error = NULL;
GVariant * result;
GVariant * child;
GVariantIter * iter;
result = g_dbus_proxy_call_finish(priv->service_proxy, res, &error);
/* No one can cancel us anymore, we've completed! */
if (priv->get_apps_cancel != NULL) {
if (error == NULL || error->domain != G_IO_ERROR || error->code != G_IO_ERROR_CANCELLED) {
g_object_unref(priv->get_apps_cancel);
priv->get_apps_cancel = NULL;
}
}
/* If we got an error, print it and exit out */
if (error != NULL) {
g_warning("Unable to get application list: %s", error->message);
g_error_free(error);
return;
}
/* Remove all applications that we previously had
as we're going to repopulate the list. */
while (priv->applications != NULL) {
application_removed(self, 0);
}
/* Get our new applications that we got in the request */
g_variant_get(result, "(a(sisosssss))", &iter);
while ((child = g_variant_iter_next_value (iter))) {
get_applications_helper(self, child);
g_variant_unref(child);
}
g_variant_iter_free (iter);
g_variant_unref(result);
return;
}
/* A little helper that takes apart the DBus structure and calls
application_added on every entry in the list. */
static void
get_applications_helper (IndicatorApplication * self, GVariant * variant)
{
const gchar * icon_name;
gint position;
const gchar * dbus_address;
const gchar * dbus_object;
const gchar * icon_theme_path;
const gchar * label;
const gchar * guide;
const gchar * accessible_desc;
const gchar * hint;
g_variant_get(variant, "(sisosssss)", &icon_name, &position,
&dbus_address, &dbus_object, &icon_theme_path, &label,
&guide, &accessible_desc, &hint);
return application_added(self, icon_name, position, dbus_address, dbus_object, icon_theme_path, label, guide, accessible_desc, hint);
}
/* Unrefs a theme directory. This may involve removing it from
the search path. */
static void
theme_dir_unref(IndicatorApplication * ia, const gchar * dir)
{
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(ia);
/* Grab the count for this dir */
int count = GPOINTER_TO_INT(g_hash_table_lookup(priv->theme_dirs, dir));
/* Is this a simple deprecation, if so, we can just lower the
number and move on. */
if (count > 1) {
count--;
g_hash_table_insert(priv->theme_dirs, g_strdup(dir), GINT_TO_POINTER(count));
return;
}
/* Try to remove it from the hash table, this makes sure
that it existed */
if (!g_hash_table_remove(priv->theme_dirs, dir)) {
g_warning("Unref'd a directory that wasn't in the theme dir hash table.");
return;
}
GtkIconTheme * icon_theme = gtk_icon_theme_get_default();
gchar ** paths;
gint path_count;
gtk_icon_theme_get_search_path(icon_theme, &paths, &path_count);
gint i;
gboolean found = FALSE;
for (i = 0; i < path_count; i++) {
if (found) {
/* If we've already found the right entry */
paths[i - 1] = paths[i];
} else {
/* We're still looking, is this the one? */
if (!g_strcmp0(paths[i], dir)) {
found = TRUE;
/* We're freeing this here as it won't be captured by the
g_strfreev() below as it's out of the array. */
g_free(paths[i]);
}
}
}
/* If we found one we need to reset the path to
accomidate the changes */
if (found) {
paths[path_count - 1] = NULL; /* Clear the last one */
gtk_icon_theme_set_search_path(icon_theme, (const gchar **)paths, path_count - 1);
}
g_strfreev(paths);
return;
}
/* Refs a theme directory, and it may add it to the search
path */
static void
theme_dir_ref(IndicatorApplication * ia, const gchar * dir)
{
IndicatorApplicationPrivate * priv = INDICATOR_APPLICATION_GET_PRIVATE(ia);
int count = 0;
if ((count = GPOINTER_TO_INT(g_hash_table_lookup(priv->theme_dirs, dir))) != 0) {
/* It exists so what we need to do is increase the ref
count of this dir. */
count++;
} else {
/* It doesn't exist, so we need to add it to the table
and to the search path. */
gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(), dir);
g_debug("\tAppending search path: %s", dir);
count = 1;
}
g_hash_table_insert(priv->theme_dirs, g_strdup(dir), GINT_TO_POINTER(count));
return;
}
|