aboutsummaryrefslogtreecommitdiff
path: root/src/app-section.c
blob: b7b557ae9be72f771ef7c77280e3a2318bfc146b (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
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
/*
An indicator to show information that is in messaging applications
that the user is using.

Copyright 2012 Canonical Ltd.

Authors:
    Lars Uebernickel <lars.uebernickel@canonical.com>
    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

#include <gdk/gdk.h>
#include <glib/gi18n.h>
#include <gio/gdesktopappinfo.h>
#include <gio/gio.h>
#include <libindicator/indicator-desktop-shortcuts.h>
#include "app-section.h"
#include "dbus-data.h"
#include "gmenuutils.h"
#include "gactionmuxer.h"
#include "indicator-messages-application.h"

struct _AppSectionPrivate
{
	GDesktopAppInfo * appinfo;
	GFileMonitor *desktop_file_monitor;

	IndicatorDesktopShortcuts * ids;

	GCancellable *app_proxy_cancellable;
	IndicatorMessagesApplication *app_proxy;

	GMenu *menu;
	GMenu *source_menu;

	GSimpleActionGroup *static_shortcuts;
	GSimpleActionGroup *source_actions;
	GActionMuxer *muxer;

	gboolean draws_attention;
	gboolean uses_chat_status;
	gchar *chat_status;

	guint name_watch_id;
};

enum {
	PROP_0,
	PROP_APPINFO,
	PROP_ACTIONS,
	PROP_DRAWS_ATTENTION,
	PROP_USES_CHAT_STATUS,
	PROP_CHAT_STATUS,
	NUM_PROPERTIES
};

static GParamSpec *properties[NUM_PROPERTIES];
static guint destroy_signal;

/* Prototypes */
static void app_section_class_init   (AppSectionClass *klass);
static void app_section_init         (AppSection *self);
static void app_section_get_property (GObject    *object,
				      guint       property_id,
				      GValue     *value,
				      GParamSpec *pspec);
static void app_section_set_property (GObject      *object,
				      guint         property_id,
				      const GValue *value,
				      GParamSpec   *pspec);
static void app_section_dispose      (GObject *object);
static void app_section_finalize     (GObject *object);
static void activate_cb              (GSimpleAction *action,
				      GVariant *param,
				      gpointer userdata);
static void launch_action_change_state (GSimpleAction *action,
					GVariant      *value,
					gpointer       user_data);
static void app_section_set_app_info (AppSection *self,
				      GDesktopAppInfo *appinfo);
static void	desktop_file_changed_cb		(GFileMonitor      *monitor,
						 GFile             *file,
						 GFile             *other_file,
						 GFileMonitorEvent  event,
						 gpointer           user_data);

/* GObject Boilerplate */
G_DEFINE_TYPE (AppSection, app_section, G_TYPE_OBJECT);

static void
app_section_class_init (AppSectionClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	g_type_class_add_private (klass, sizeof (AppSectionPrivate));

	object_class->get_property = app_section_get_property;
	object_class->set_property = app_section_set_property;
	object_class->dispose = app_section_dispose;
	object_class->finalize = app_section_finalize;

	properties[PROP_APPINFO] = g_param_spec_object ("app-info",
							"AppInfo",
							"The GAppInfo for the app that this menu represents",
							G_TYPE_APP_INFO,
							G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);

	properties[PROP_ACTIONS] = g_param_spec_object ("actions",
							"Actions",
							"The actions exported by this application",
							G_TYPE_ACTION_GROUP,
							G_PARAM_READABLE);

	properties[PROP_DRAWS_ATTENTION] = g_param_spec_boolean ("draws-attention",
								 "Draws attention",
								 "Whether the section currently draws attention",
								 FALSE,
								 G_PARAM_READABLE);

	properties[PROP_USES_CHAT_STATUS] = g_param_spec_boolean ("uses-chat-status",
								  "Uses chat status",
								  "Whether the section uses the global chat status",
								  FALSE,
								  G_PARAM_READABLE);

	properties[PROP_CHAT_STATUS] = g_param_spec_string ("chat-status",
							    "Chat status",
							    "Current chat status of the application",
							    NULL,
							    G_PARAM_READWRITE |
							    G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);

	destroy_signal = g_signal_new ("destroy",
				       APP_SECTION_TYPE,
				       G_SIGNAL_RUN_FIRST,
				       0,
				       NULL, NULL,
				       g_cclosure_marshal_VOID__VOID,
				       G_TYPE_NONE, 0);
}

static void
app_section_init (AppSection *self)
{
	AppSectionPrivate *priv;
	GMenuItem *item;

	self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
						  APP_SECTION_TYPE,
						  AppSectionPrivate);
	priv = self->priv;

	priv->appinfo = NULL;

	priv->menu = g_menu_new ();

	priv->source_menu = g_menu_new ();
	item = g_menu_item_new_section (NULL, G_MENU_MODEL (priv->source_menu));
	g_menu_item_set_attribute (item, "action-namespace", "s", "source");
	g_menu_append_item (priv->menu, item);
	g_object_unref (item);

	priv->static_shortcuts = g_simple_action_group_new ();
	priv->source_actions = g_simple_action_group_new ();

	priv->muxer = g_action_muxer_new ();
	g_action_muxer_insert (priv->muxer, NULL, G_ACTION_GROUP (priv->static_shortcuts));
	g_action_muxer_insert (priv->muxer, "source", G_ACTION_GROUP (priv->source_actions));

	priv->draws_attention = FALSE;

	return;
}

static void
app_section_get_property (GObject    *object,
			  guint       property_id,
			  GValue     *value,
			  GParamSpec *pspec)
{
	AppSection *self = APP_SECTION (object);

	switch (property_id)
	{
	case PROP_APPINFO:
		g_value_set_object (value, app_section_get_app_info (self));
		break;

	case PROP_DRAWS_ATTENTION:
		g_value_set_boolean (value, app_section_get_draws_attention (self));
		break;

	case PROP_USES_CHAT_STATUS:
		g_value_set_boolean (value, app_section_get_uses_chat_status (self));
		break;

	case PROP_CHAT_STATUS:
		g_value_set_string (value, app_section_get_status (self));
		break;

	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
app_section_set_property (GObject      *object,
			  guint         property_id,
			  const GValue *value,
			  GParamSpec   *pspec)
{
	AppSection *self = APP_SECTION (object);

	switch (property_id)
	{
	case PROP_APPINFO:
		app_section_set_app_info (self, g_value_get_object (value));
		break;

	case PROP_CHAT_STATUS:
		app_section_set_status (self, g_value_get_string (value));
		break;

	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
app_section_dispose (GObject *object)
{
	AppSection * self = APP_SECTION(object);
	AppSectionPrivate * priv = self->priv;

	if (priv->app_proxy_cancellable) {
		g_cancellable_cancel (priv->app_proxy_cancellable);
		g_clear_object (&priv->app_proxy_cancellable);
	}

	if (priv->desktop_file_monitor) {
		g_signal_handlers_disconnect_by_func (priv->desktop_file_monitor, desktop_file_changed_cb, self);
		g_clear_object (&priv->desktop_file_monitor);
	}

	g_clear_object (&priv->app_proxy);

	g_clear_object (&priv->menu);
	g_clear_object (&priv->source_menu);
	g_clear_object (&priv->static_shortcuts);
	g_clear_object (&priv->source_actions);

	if (priv->name_watch_id) {
		g_bus_unwatch_name (priv->name_watch_id);
		priv->name_watch_id = 0;
	}

	g_clear_object (&priv->muxer);

	g_clear_object (&priv->ids);
	g_clear_object (&priv->appinfo);

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

static void
app_section_finalize (GObject *object)
{
	AppSection * self = APP_SECTION(object);

	g_free (self->priv->chat_status);

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

static GAppLaunchContext *
get_launch_context (guint32 timestamp)
{
	GdkDisplay *display = gdk_display_get_default();
	GdkAppLaunchContext *launch_context = gdk_display_get_app_launch_context (display);

	gdk_app_launch_context_set_timestamp (launch_context, timestamp);

	return G_APP_LAUNCH_CONTEXT (launch_context);
}

/* Respond to one of the shortcuts getting clicked on. */
static void
nick_activate_cb (GSimpleAction *action,
		  GVariant *param,
		  gpointer userdata)
{
	const gchar * nick = g_action_get_name (G_ACTION (action));
	AppSection * mi = APP_SECTION (userdata);
	AppSectionPrivate * priv = mi->priv;

	g_return_if_fail(priv->ids != NULL);

	if (!indicator_desktop_shortcuts_nick_exec_with_context(priv->ids, nick, NULL)) {
		g_warning("Unable to execute nick '%s' for desktop file '%s'",
			  nick, g_desktop_app_info_get_filename (priv->appinfo));
	}

	return;
}

static void
keyfile_loaded (GObject *source_object,
		GAsyncResult *result,
		gpointer user_data)
{
	AppSection *self = user_data;
	gchar *contents;
	gsize length;
	GKeyFile *keyfile;
	GError *error = NULL;

	if (!g_file_load_contents_finish (G_FILE (source_object), result,
					 &contents, &length, NULL, &error)) {
		g_warning ("could not read key file: %s", error->message);
		g_error_free (error);
		return;
	}

	keyfile = g_key_file_new ();
	if (!g_key_file_load_from_data (keyfile, contents, length, 0, &error)) {
		g_warning ("could not read key file: %s", error->message);
		g_error_free (error);
		goto out;
	}

	self->priv->uses_chat_status = g_key_file_get_boolean (keyfile,
							       G_KEY_FILE_DESKTOP_GROUP,
							       "X-MessagingMenu-UsesChatSection",
							       &error);

	g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_USES_CHAT_STATUS]);

	if (error) {
		if (error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) {
			g_warning ("could not read X-MessagingMenu-UsesChatSection: %s",
				   error->message);
		}
		g_error_free (error);
		goto out;
	}

out:
	g_key_file_free (keyfile);
	g_free (contents);
}

static void
g_menu_clear (GMenu *menu)
{
	gint n_items = g_menu_model_get_n_items (G_MENU_MODEL (menu));

	while (n_items--)
		g_menu_remove (menu, 0);
}

static void
g_simple_action_group_clear (GSimpleActionGroup *group)
{
	gchar **actions;
	gchar **it;

	actions = g_action_group_list_actions (G_ACTION_GROUP (group));
	for (it = actions; *it; it++)
		g_simple_action_group_remove (group, *it);

	g_strfreev (actions);
}

static void
app_section_update_menu (AppSection *self)
{
	AppSectionPrivate *priv = self->priv;
	GSimpleAction *launch;
	GFile *keyfile;
	GMenuItem *item;
	gchar *iconstr;
	gboolean is_running;

	g_menu_clear (priv->menu);
	g_simple_action_group_clear (priv->static_shortcuts);

	is_running = priv->name_watch_id > 0;
	launch = g_simple_action_new_stateful ("launch", G_VARIANT_TYPE_UINT32, g_variant_new_boolean (is_running));
	g_signal_connect (launch, "activate", G_CALLBACK (activate_cb), self);
	g_signal_connect (launch, "change-state", G_CALLBACK (launch_action_change_state), self);
	g_simple_action_group_insert (priv->static_shortcuts, G_ACTION (launch));

	item = g_menu_item_new (g_app_info_get_name (G_APP_INFO (priv->appinfo)), "launch");
	g_menu_item_set_attribute (item, "x-canonical-type", "s", "ImAppMenuItem");
	iconstr = g_icon_to_string (g_app_info_get_icon (G_APP_INFO (priv->appinfo)));
	g_menu_item_set_attribute (item, "x-canonical-icon", "s", iconstr);
	g_free (iconstr);

	g_menu_append_item (priv->menu, item);
	g_object_unref (item);

	/* Start to build static shortcuts */
	priv->ids = indicator_desktop_shortcuts_new(g_desktop_app_info_get_filename (priv->appinfo), "Messaging Menu");
	const gchar ** nicks = indicator_desktop_shortcuts_get_nicks(priv->ids);
	gint i;
	for (i = 0; nicks[i] != NULL; i++) {
		gchar *name;
		GSimpleAction *action;
		GMenuItem *item;

		name = indicator_desktop_shortcuts_nick_get_name(priv->ids, nicks[i]);

		action = g_simple_action_new (nicks[i], G_VARIANT_TYPE_UINT32);
		g_signal_connect(action, "activate", G_CALLBACK (nick_activate_cb), self);
		g_simple_action_group_insert (priv->static_shortcuts, G_ACTION (action));
		g_object_unref (action);

		item = g_menu_item_new (name, nicks[i]);
		g_menu_item_set_attribute (item, "x-canonical-type", "s", "IdoMenuItem");
		g_menu_item_set_attribute (item, "x-canonical-icon", "s", ""); /* empty to get indentation */
		g_menu_append_item (priv->menu, item);

		g_object_unref (item);
		g_free(name);
	}

	item = g_menu_item_new_section (NULL, G_MENU_MODEL (priv->source_menu));
	g_menu_item_set_attribute (item, "action-namespace", "s", "source");
	g_menu_append_item (priv->menu, item);
	g_object_unref (item);

	keyfile = g_file_new_for_path (g_desktop_app_info_get_filename (priv->appinfo));
	g_file_load_contents_async (keyfile, NULL, keyfile_loaded, self);

	g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ACTIONS]);

	g_object_unref (keyfile);
	g_object_unref (launch);
}

static void
desktop_file_changed_cb (GFileMonitor      *monitor,
			 GFile             *file,
			 GFile             *other_file,
			 GFileMonitorEvent  event,
			 gpointer           user_data)
{
	AppSection *self = user_data;

	if (event == G_FILE_MONITOR_EVENT_CHANGED) {
		app_section_update_menu (self);
	}
	else if (event == G_FILE_MONITOR_EVENT_DELETED ||
		 event == G_FILE_MONITOR_EVENT_UNMOUNTED) {
		g_signal_emit (self, destroy_signal, 0);
	}
}

static void
app_section_set_app_info (AppSection *self,
			  GDesktopAppInfo *appinfo)
{
	AppSectionPrivate *priv = self->priv;
	GFile *desktop_file;
	GError *error = NULL;

	g_return_if_fail (priv->appinfo == NULL);
	g_return_if_fail (priv->desktop_file_monitor == NULL);

	if (appinfo == NULL) {
		g_warning ("appinfo must not be NULL");
		return;
	}

	priv->appinfo = g_object_ref (appinfo);

	desktop_file = g_file_new_for_path (g_desktop_app_info_get_filename (appinfo));
	priv->desktop_file_monitor = g_file_monitor (desktop_file, G_FILE_MONITOR_SEND_MOVED, NULL, &error);
	if (priv->desktop_file_monitor == NULL) {
		g_warning ("unable to watch desktop file: %s", error->message);
		g_error_free (error);
	}
	g_signal_connect (priv->desktop_file_monitor, "changed",
			  G_CALLBACK (desktop_file_changed_cb), self);

	g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_APPINFO]);

	app_section_update_menu (self);

	g_object_unref (desktop_file);
}

AppSection *
app_section_new (GDesktopAppInfo *appinfo)
{
	return g_object_new (APP_SECTION_TYPE,
			     "app-info", appinfo,
			     NULL);
}

static void
activate_cb (GSimpleAction *action,
	     GVariant *param,
	     gpointer userdata)
{
	AppSection * mi = APP_SECTION (userdata);
	AppSectionPrivate * priv = mi->priv;
	GError *error = NULL;
	GAppLaunchContext *launch_context = get_launch_context (g_variant_get_uint32 (param));

	if (!g_app_info_launch (G_APP_INFO (priv->appinfo), NULL, launch_context, &error)) {
		g_warning("Unable to execute application for desktop file '%s': %s",
			  g_desktop_app_info_get_filename (priv->appinfo),
			  error->message);
		g_error_free (error);
	}

	g_object_unref (launch_context);
}

static void
launch_action_change_state (GSimpleAction *action,
			    GVariant      *value,
			    gpointer       user_data)
{
	g_simple_action_set_state (action, value);
}

const gchar *
app_section_get_desktop (AppSection * self)
{
	AppSectionPrivate * priv = self->priv;
	if (priv->appinfo)
		return g_desktop_app_info_get_filename (priv->appinfo);
	else
		return NULL;
}

GActionGroup *
app_section_get_actions (AppSection *self)
{
	AppSectionPrivate * priv = self->priv;
	return G_ACTION_GROUP (priv->muxer);
}

GMenuModel *
app_section_get_menu (AppSection *self)
{
	AppSectionPrivate * priv = self->priv;
	return G_MENU_MODEL (priv->menu);
}

GAppInfo *
app_section_get_app_info (AppSection *self)
{
	AppSectionPrivate * priv = self->priv;
	return G_APP_INFO (priv->appinfo);
}

gboolean
app_section_get_draws_attention (AppSection *self)
{
	AppSectionPrivate * priv = self->priv;
	return priv->draws_attention;
}

void
app_section_clear_draws_attention (AppSection *self)
{
	self->priv->draws_attention = FALSE;
	g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DRAWS_ATTENTION]);
}

static void
application_vanished (GDBusConnection *bus,
		      const gchar *name,
		      gpointer user_data)
{
	AppSection *self = user_data;

	app_section_unset_object_path (self);
}

static void
update_draws_attention (AppSection *self)
{
	AppSectionPrivate *priv = self->priv;
	gchar **actions;
	gchar **it;
	gboolean draws_attention = FALSE;

	actions = g_action_group_list_actions (G_ACTION_GROUP (priv->source_actions));

	for (it = actions; *it; it++) {
		GVariant *state;

		state = g_action_group_get_action_state (G_ACTION_GROUP (priv->source_actions), *it);
		if (state) {
			gboolean b;
			g_variant_get (state, "(uxsb)", NULL, NULL, NULL, &b);
			draws_attention = b || draws_attention;
			g_variant_unref (state);
		}

		if (draws_attention)
			break;
	}

	if (draws_attention != priv->draws_attention) {
		priv->draws_attention = draws_attention;
		g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DRAWS_ATTENTION]);
	}

	g_strfreev (actions);
}

static void
remove_source (AppSection  *self,
	       const gchar *id)
{
	AppSectionPrivate *priv = self->priv;
	guint n_items;
	guint i;

	n_items = g_menu_model_get_n_items (G_MENU_MODEL (priv->source_menu));
	for (i = 0; i < n_items; i++) {
		gchar *action;
		gboolean found = FALSE;

		if (g_menu_model_get_item_attribute (G_MENU_MODEL (priv->source_menu), i,
						     G_MENU_ATTRIBUTE_ACTION, "s", &action)) {
			found = g_str_equal (action, id);
			g_free (action);
		}

		if (found) {
			g_menu_remove (priv->source_menu, i);
			break;
		}
	}

	g_simple_action_group_remove (priv->source_actions, id);
	update_draws_attention (self);
}

static void
source_action_activated (GSimpleAction *action,
			 GVariant      *parameter,
			 gpointer       user_data)
{
	AppSection *self = APP_SECTION (user_data);
	AppSectionPrivate *priv = APP_SECTION (user_data)->priv;

	g_return_if_fail (priv->app_proxy != NULL);

	indicator_messages_application_call_activate_source (priv->app_proxy,
							     g_action_get_name (G_ACTION (action)),
							     priv->app_proxy_cancellable,
							     NULL, NULL);

	remove_source (self, g_action_get_name (G_ACTION (action)));
}

static void
sources_listed (GObject      *source_object,
		GAsyncResult *result,
		gpointer      user_data)
{
	AppSection *self = user_data;
	AppSectionPrivate *priv = self->priv;
	GVariant *sources = NULL;
	GError *error = NULL;
	GVariantIter iter;
	const gchar *id;
	const gchar *label;
	const gchar *iconstr;
	guint32 count;
	gint64 time;
	const gchar *string;
	gboolean draws_attention;

	if (!indicator_messages_application_call_list_sources_finish (INDICATOR_MESSAGES_APPLICATION (source_object),
								      &sources, result, &error))
	{
		g_warning ("could not fetch the list of sources: %s", error->message);
		g_error_free (error);
		return;
	}

	g_menu_clear (priv->source_menu);
	g_simple_action_group_clear (priv->source_actions);
	priv->draws_attention = FALSE;

	g_variant_iter_init (&iter, sources);
	while (g_variant_iter_next (&iter, "(&s&s&sux&sb)", &id, &label, &iconstr,
				    &count, &time, &string, &draws_attention))
	{
		GVariant *state;
		GSimpleAction *action;
		GMenuItem *item;

		state = g_variant_new ("(uxsb)", count, time, string, draws_attention);
		action = g_simple_action_new_stateful (id, NULL, state);
		g_signal_connect (action, "activate", G_CALLBACK (source_action_activated), self);
		g_simple_action_group_insert (priv->source_actions, G_ACTION (action));

		item = g_menu_item_new (label, id);
		g_menu_item_set_attribute (item, "x-canonical-type", "s", "ImSourceMenuItem");
		g_menu_append_item (priv->source_menu, item);

		priv->draws_attention = priv->draws_attention || draws_attention;

		g_object_unref (item);
		g_object_unref (action);
	}

	g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DRAWS_ATTENTION]);

	g_variant_unref (sources);
}

static void
source_added (IndicatorMessagesApplication  *app,
	      const gchar		    *id,
	      const gchar		    *label,
	      const gchar		    *iconstr,
	      guint			     count,
	      gint64			     time,
	      const gchar		    *string,
	      gboolean			     draws_attention,
	      gpointer			     user_data)
{
	AppSection *self = user_data;
	AppSectionPrivate *priv = self->priv;
	GVariant *state;
	GSimpleAction *action;

	/* TODO put label and icon into the action as well */

	state = g_variant_new ("(uxsb)", count, time, string, draws_attention);
	action = g_simple_action_new_stateful (id, NULL, state);

	g_simple_action_group_insert (priv->source_actions, G_ACTION (action));

	if (draws_attention && !priv->draws_attention) {
		priv->draws_attention = TRUE;
		g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DRAWS_ATTENTION]);
	}

	g_object_unref (action);
}
static void
source_changed (IndicatorMessagesApplication  *app,
		const gchar		       *id,
		const gchar		       *label,
		const gchar		       *iconstr,
		guint				count,
		gint64				time,
		const gchar		       *string,
		gboolean			draws_attention,
		gpointer			user_data)
{
	AppSection *self = user_data;
	AppSectionPrivate *priv = self->priv;
	GVariant *state;

	/* TODO put label and icon into the action as well */

	state = g_variant_new ("(uxsb)", count, time, string, draws_attention);
	g_action_group_change_action_state (G_ACTION_GROUP (priv->source_actions), id, state);

	update_draws_attention (self);
}

static void
source_removed (IndicatorMessagesApplication *app,
		const gchar		     *id,
		gpointer		      user_data)
{
	AppSection *self = user_data;

	remove_source (self, id);
}

static void
app_proxy_created (GObject      *source_object,
		   GAsyncResult *result,
		   gpointer      user_data)
{
	AppSectionPrivate *priv = APP_SECTION (user_data)->priv;
	GError *error = NULL;

	priv->app_proxy = indicator_messages_application_proxy_new_finish (result, &error);
	if (!priv->app_proxy) {
		g_warning ("could not create application proxy: %s", error->message);
		g_error_free (error);
		return;
	}

	indicator_messages_application_call_list_sources (priv->app_proxy, priv->app_proxy_cancellable,
							  sources_listed, user_data);

	g_signal_connect (priv->app_proxy, "source-added", G_CALLBACK (source_added), user_data);
	g_signal_connect (priv->app_proxy, "source-changed", G_CALLBACK (source_changed), user_data);
	g_signal_connect (priv->app_proxy, "source-removed", G_CALLBACK (source_removed), user_data);
}

/*
 * app_section_set_object_path:
 * @self: an #AppSection
 * @bus: a #GDBusConnection
 * @bus_name: the bus name of the application
 * @object_path: the object path on which the app exports its actions and menus
 *
 * Sets the D-Bus object path exported by an instance of the application
 * associated with @self.  Actions and menus exported on that path will be
 * shown in the section.
 */
void
app_section_set_object_path (AppSection *self,
			     GDBusConnection *bus,
			     const gchar *bus_name,
			     const gchar *object_path)
{
	AppSectionPrivate *priv = self->priv;

	g_object_freeze_notify (G_OBJECT (self));
	app_section_unset_object_path (self);

	priv->app_proxy_cancellable = g_cancellable_new ();
	indicator_messages_application_proxy_new (bus,
						  G_DBUS_PROXY_FLAGS_NONE,
						  bus_name,
						  object_path,
						  priv->app_proxy_cancellable,
						  app_proxy_created,
						  self);

	priv->draws_attention = FALSE;

	priv->name_watch_id = g_bus_watch_name_on_connection (bus, bus_name, 0,
							      NULL, application_vanished,
							      self, NULL);

	g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ACTIONS]);
	g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DRAWS_ATTENTION]);
	g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_USES_CHAT_STATUS]);
	g_object_thaw_notify (G_OBJECT (self));

	g_action_group_change_action_state (G_ACTION_GROUP (priv->static_shortcuts),
					    "launch", g_variant_new_boolean (TRUE));
}

/*
 * app_section_unset_object_path:
 * @self: an #AppSection
 *
 * Unsets the object path set with app_section_set_object_path().  The section
 * will return to only showing application name and static shortcuts in the
 * menu.
 */
void
app_section_unset_object_path (AppSection *self)
{
	AppSectionPrivate *priv = self->priv;

	if (priv->app_proxy_cancellable) {
		g_cancellable_cancel (priv->app_proxy_cancellable);
		g_clear_object (&priv->app_proxy_cancellable);
	}
	g_clear_object (&priv->app_proxy);

	if (priv->name_watch_id) {
		g_bus_unwatch_name (priv->name_watch_id);
		priv->name_watch_id = 0;
	}

	g_simple_action_group_clear (priv->source_actions);
	g_menu_clear (priv->source_menu);

	priv->draws_attention = FALSE;
	g_clear_pointer (&priv->chat_status, g_free);

	g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ACTIONS]);
	g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DRAWS_ATTENTION]);
	g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_USES_CHAT_STATUS]);
	g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_CHAT_STATUS]);

	g_action_group_change_action_state (G_ACTION_GROUP (priv->static_shortcuts),
					    "launch", g_variant_new_boolean (FALSE));
}

gboolean
app_section_get_uses_chat_status (AppSection *self)
{
	AppSectionPrivate * priv = self->priv;

	return priv->uses_chat_status;
}

const gchar *
app_section_get_status (AppSection *self)
{
	AppSectionPrivate * priv = self->priv;

	return priv->chat_status;
}

void
app_section_set_status (AppSection  *self,
			const gchar *status)
{
	AppSectionPrivate * priv = self->priv;

	g_free (priv->chat_status);
	priv->chat_status = g_strdup (status);

	g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_CHAT_STATUS]);
}