aboutsummaryrefslogtreecommitdiff
path: root/src/application-service-appstore.c
blob: 2f53ecf6fe8c92839863d229cab92b6eaf3e51a0 (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
/*
An object that stores the registration of all the application
indicators.  It also communicates this to the indicator visualization.

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

#include <dbus/dbus-glib.h>
#include "app-indicator.h"
#include "app-indicator-enum-types.h"
#include "application-service-appstore.h"
#include "application-service-marshal.h"
#include "dbus-properties-client.h"
#include "dbus-shared.h"
#include "notification-approver-client.h"

/* DBus Prototypes */
static gboolean _application_service_server_get_applications (ApplicationServiceAppstore * appstore, GPtrArray ** apps, GError ** error);

#include "application-service-server.h"

#define NOTIFICATION_ITEM_PROP_ID         "Id"
#define NOTIFICATION_ITEM_PROP_CATEGORY   "Category"
#define NOTIFICATION_ITEM_PROP_STATUS     "Status"
#define NOTIFICATION_ITEM_PROP_ICON_NAME  "IconName"
#define NOTIFICATION_ITEM_PROP_AICON_NAME "AttentionIconName"
#define NOTIFICATION_ITEM_PROP_ICON_PATH  "IconThemePath"
#define NOTIFICATION_ITEM_PROP_MENU       "Menu"
#define NOTIFICATION_ITEM_PROP_LABEL      "Label"
#define NOTIFICATION_ITEM_PROP_LABEL_GUIDE "LabelGuide"

#define NOTIFICATION_ITEM_SIG_NEW_ICON    "NewIcon"
#define NOTIFICATION_ITEM_SIG_NEW_AICON   "NewAttentionIcon"
#define NOTIFICATION_ITEM_SIG_NEW_STATUS  "NewStatus"
#define NOTIFICATION_ITEM_SIG_NEW_LABEL   "NewLabel"

/* Private Stuff */
struct _ApplicationServiceAppstorePrivate {
	DBusGConnection * bus;
	GList * applications;
	GList * approvers;
	AppLruFile * lrufile;
};

typedef struct _Approver Approver;
struct _Approver {
	DBusGProxy * proxy;
};

typedef struct _Application Application;
struct _Application {
	gchar * id;
	gchar * category;
	gchar * dbus_name;
	gchar * dbus_object;
	ApplicationServiceAppstore * appstore; /* not ref'd */
	DBusGProxy * dbus_proxy;
	DBusGProxy * prop_proxy;
	gboolean validated; /* Whether we've gotten all the parameters and they look good. */
	AppIndicatorStatus status;
	gchar * icon;
	gchar * aicon;
	gchar * menu;
	gchar * icon_path;
	gboolean currently_free;
};

#define APPLICATION_SERVICE_APPSTORE_GET_PRIVATE(o) \
			(G_TYPE_INSTANCE_GET_PRIVATE ((o), APPLICATION_SERVICE_APPSTORE_TYPE, ApplicationServiceAppstorePrivate))

/* Signals Stuff */
enum {
	APPLICATION_ADDED,
	APPLICATION_REMOVED,
	APPLICATION_ICON_CHANGED,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

/* GObject stuff */
static void application_service_appstore_class_init (ApplicationServiceAppstoreClass *klass);
static void application_service_appstore_init       (ApplicationServiceAppstore *self);
static void application_service_appstore_dispose    (GObject *object);
static void application_service_appstore_finalize   (GObject *object);
static AppIndicatorStatus string_to_status(const gchar * status_string);
static void apply_status (Application * app, AppIndicatorStatus status);
static void approver_free (gpointer papprover, gpointer user_data);
static void check_with_new_approver (gpointer papp, gpointer papprove);
static void check_with_old_approver (gpointer papprove, gpointer papp);

G_DEFINE_TYPE (ApplicationServiceAppstore, application_service_appstore, G_TYPE_OBJECT);

static void
application_service_appstore_class_init (ApplicationServiceAppstoreClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	g_type_class_add_private (klass, sizeof (ApplicationServiceAppstorePrivate));

	object_class->dispose = application_service_appstore_dispose;
	object_class->finalize = application_service_appstore_finalize;

	signals[APPLICATION_ADDED] = g_signal_new ("application-added",
	                                           G_TYPE_FROM_CLASS(klass),
	                                           G_SIGNAL_RUN_LAST,
	                                           G_STRUCT_OFFSET (ApplicationServiceAppstoreClass, application_added),
	                                           NULL, NULL,
	                                           _application_service_marshal_VOID__STRING_INT_STRING_STRING_STRING_STRING_STRING,
	                                           G_TYPE_NONE, 7, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_NONE);
	signals[APPLICATION_REMOVED] = g_signal_new ("application-removed",
	                                           G_TYPE_FROM_CLASS(klass),
	                                           G_SIGNAL_RUN_LAST,
	                                           G_STRUCT_OFFSET (ApplicationServiceAppstoreClass, application_removed),
	                                           NULL, NULL,
	                                           g_cclosure_marshal_VOID__INT,
	                                           G_TYPE_NONE, 1, G_TYPE_INT, G_TYPE_NONE);
	signals[APPLICATION_ICON_CHANGED] = g_signal_new ("application-icon-changed",
	                                           G_TYPE_FROM_CLASS(klass),
	                                           G_SIGNAL_RUN_LAST,
	                                           G_STRUCT_OFFSET (ApplicationServiceAppstoreClass, application_icon_changed),
	                                           NULL, NULL,
	                                           _application_service_marshal_VOID__INT_STRING,
	                                           G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_STRING, G_TYPE_NONE);

	dbus_g_object_type_install_info(APPLICATION_SERVICE_APPSTORE_TYPE,
	                                &dbus_glib__application_service_server_object_info);

	return;
}

static void
application_service_appstore_init (ApplicationServiceAppstore *self)
{
    
	ApplicationServiceAppstorePrivate * priv = APPLICATION_SERVICE_APPSTORE_GET_PRIVATE (self);

	priv->applications = NULL;
	priv->approvers = NULL;
	priv->lrufile = NULL;
	
	GError * error = NULL;
	priv->bus = dbus_g_bus_get(DBUS_BUS_STARTER, &error);
	if (error != NULL) {
		g_error("Unable to get session bus: %s", error->message);
		g_error_free(error);
		return;
	}

	dbus_g_connection_register_g_object(priv->bus,
	                                    INDICATOR_APPLICATION_DBUS_OBJ,
	                                    G_OBJECT(self));
	                                    
    self->priv = priv;

	return;
}

static void
application_service_appstore_dispose (GObject *object)
{
	ApplicationServiceAppstorePrivate * priv = APPLICATION_SERVICE_APPSTORE(object)->priv;

	while (priv->applications != NULL) {
		application_service_appstore_application_remove(APPLICATION_SERVICE_APPSTORE(object),
		                                           ((Application *)priv->applications->data)->dbus_name,
		                                           ((Application *)priv->applications->data)->dbus_object);
	}

	if (priv->approvers != NULL) {
		g_list_foreach(priv->approvers, approver_free, NULL);
		g_list_free(priv->approvers);
		priv->approvers = NULL;
	}

	G_OBJECT_CLASS (application_service_appstore_parent_class)->dispose (object);
	return;
}

static void
application_service_appstore_finalize (GObject *object)
{

	G_OBJECT_CLASS (application_service_appstore_parent_class)->finalize (object);
	return;
}

/* Return from getting the properties from the item.  We're looking at those
   and making sure we have everythign that we need.  If we do, then we'll
   move on up to sending this onto the indicator. */
static void
get_all_properties_cb (DBusGProxy * proxy, GHashTable * properties, GError * error, gpointer data)
{
	if (error != NULL) {
		g_warning("Unable to get properties: %s", error->message);
		return;
	}

	Application * app = (Application *)data;

	if (g_hash_table_lookup(properties, NOTIFICATION_ITEM_PROP_MENU) == NULL ||
			g_hash_table_lookup(properties, NOTIFICATION_ITEM_PROP_ID) == NULL ||
			g_hash_table_lookup(properties, NOTIFICATION_ITEM_PROP_CATEGORY) == NULL ||
			g_hash_table_lookup(properties, NOTIFICATION_ITEM_PROP_STATUS) == NULL ||
			g_hash_table_lookup(properties, NOTIFICATION_ITEM_PROP_ICON_NAME) == NULL) {
		g_warning("Notification Item on object %s of %s doesn't have enough properties.", app->dbus_object, app->dbus_name);
		g_free(app); // Need to do more than this, but it gives the idea of the flow we're going for.
		return;
	}

	app->validated = TRUE;

	app->id = g_value_dup_string(g_hash_table_lookup(properties, NOTIFICATION_ITEM_PROP_ID));
	app->category = g_value_dup_string(g_hash_table_lookup(properties, NOTIFICATION_ITEM_PROP_CATEGORY));
	ApplicationServiceAppstorePrivate * priv = app->appstore->priv;
	app_lru_file_touch(priv->lrufile, app->id, app->category);

	app->icon = g_value_dup_string(g_hash_table_lookup(properties, NOTIFICATION_ITEM_PROP_ICON_NAME));

	GValue * menuval = (GValue *)g_hash_table_lookup(properties, NOTIFICATION_ITEM_PROP_MENU);
	if (G_VALUE_TYPE(menuval) == G_TYPE_STRING) {
		/* This is here to support an older version where we
		   were using strings instea of object paths. */
		app->menu = g_value_dup_string(menuval);
	} else {
		app->menu = g_strdup((gchar *)g_value_get_boxed(menuval));
	}

	if (g_hash_table_lookup(properties, NOTIFICATION_ITEM_PROP_AICON_NAME) != NULL) {
		app->aicon = g_value_dup_string(g_hash_table_lookup(properties, NOTIFICATION_ITEM_PROP_AICON_NAME));
	}

	gpointer icon_path_data = g_hash_table_lookup(properties, NOTIFICATION_ITEM_PROP_ICON_PATH);
	if (icon_path_data != NULL) {
		app->icon_path = g_value_dup_string((GValue *)icon_path_data);
	} else {
		app->icon_path = g_strdup("");
	}

	/* TODO: Calling approvers, but we're ignoring the results.  So, eh. */
	g_list_foreach(priv->approvers, check_with_old_approver, app);

	apply_status(app, string_to_status(g_value_get_string(g_hash_table_lookup(properties, NOTIFICATION_ITEM_PROP_STATUS))));

	return;
}

/* Check the application against an approver */
static void
check_with_old_approver (gpointer papprove, gpointer papp)
{
	/* Funny the parallels, eh? */
	check_with_new_approver(papp, papprove);
	return;
}

/* Simple translation function -- could be optimized */
static AppIndicatorStatus
string_to_status(const gchar * status_string)
{
	GEnumClass * klass = G_ENUM_CLASS(g_type_class_ref(APP_INDICATOR_TYPE_INDICATOR_STATUS));
	g_return_val_if_fail(klass != NULL, APP_INDICATOR_STATUS_PASSIVE);

	AppIndicatorStatus retval = APP_INDICATOR_STATUS_PASSIVE;

	GEnumValue * val = g_enum_get_value_by_nick(klass, status_string);
	if (val == NULL) {
		g_warning("Unrecognized status '%s' assuming passive.", status_string);
	} else {
		retval = (AppIndicatorStatus)val->value;
	}

	g_type_class_unref(klass);

	return retval;
}

/* A small helper function to get the position of an application
   in the app list. */
static gint 
get_position (Application * app) {
	ApplicationServiceAppstore * appstore = app->appstore;
	ApplicationServiceAppstorePrivate * priv = appstore->priv;

	GList * applistitem = g_list_find(priv->applications, app);
	if (applistitem == NULL) {
		return -1;
	}

	return g_list_position(priv->applications, applistitem);
}

/* A simple global function for dealing with freeing the information
   in an Application structure */
static void
application_free (Application * app)
{
	if (app == NULL) return;
	
	/* Handle the case where this could be called by unref'ing one of
	   the proxy objects. */
	if (app->currently_free) return;
	app->currently_free = TRUE;
	
	if (app->dbus_proxy) {
		g_object_unref(app->dbus_proxy);
	}
	if (app->prop_proxy) {
		g_object_unref(app->prop_proxy);
	}

	if (app->id != NULL) {
		g_free(app->id);
	}
	if (app->category != NULL) {
		g_free(app->category);
	}
	if (app->dbus_name != NULL) {
		g_free(app->dbus_name);
	}
	if (app->dbus_object != NULL) {
		g_free(app->dbus_object);
	}
	if (app->icon != NULL) {
		g_free(app->icon);
	}
	if (app->aicon != NULL) {
		g_free(app->aicon);
	}
	if (app->menu != NULL) {
		g_free(app->menu);
	}
	if (app->icon_path != NULL) {
		g_free(app->icon_path);
	}

	g_free(app);
	return;
}

/* Gets called when the proxy is destroyed, which is usually when it
   drops off of the bus. */
static void
application_removed_cb (DBusGProxy * proxy, gpointer userdata)
{
	Application * app = (Application *)userdata;

	/* Remove from the panel */
	apply_status(app, APP_INDICATOR_STATUS_PASSIVE);

	/* Destroy the data */
	application_free(app);
	return;
}

static gboolean
can_add_application (GList *applications, Application *app)
{
  if (applications)
    {
      GList *l = NULL;

      for (l = applications; l != NULL; l = g_list_next (l))
        {
          Application *tmp_app = (Application *)l->data;

          if (g_strcmp0 (tmp_app->dbus_name, app->dbus_name) == 0 &&
              g_strcmp0 (tmp_app->dbus_object, app->dbus_object) == 0)
            {
              return FALSE;
            }
        }
    }

  return TRUE;
}

/* This function takes two Application structure
   pointers and uses the lrufile to compare them. */
static gint
app_sort_func (gconstpointer a, gconstpointer b, gpointer userdata)
{
	Application * appa = (Application *)a;
	Application * appb = (Application *)b;
	AppLruFile * lrufile = (AppLruFile *)userdata;

	return app_lru_file_sort(lrufile, appa->id, appb->id);
}

/* Change the status of the application.  If we're going passive
   it removes it from the panel.  If we're coming online, then
   it add it to the panel.  Otherwise it changes the icon. */
static void
apply_status (Application * app, AppIndicatorStatus status)
{
	if (app->status == status) {
		return;
	}
	g_debug("Changing app status to: %d", status);

	ApplicationServiceAppstore * appstore = app->appstore;
	ApplicationServiceAppstorePrivate * priv = appstore->priv;

	/* This means we're going off line */
	if (status == APP_INDICATOR_STATUS_PASSIVE) {
		gint position = get_position(app);
		if (position == -1) return;

		g_signal_emit(G_OBJECT(appstore),
					  signals[APPLICATION_REMOVED], 0, 
					  position, TRUE);
                priv->applications = g_list_remove(priv->applications, app);
	} else {
		/* Figure out which icon we should be using */
		gchar * newicon = app->icon;
		if (status == APP_INDICATOR_STATUS_ATTENTION && app->aicon != NULL && app->aicon[0] != '\0') {
			newicon = app->aicon;
		}

		/* Determine whether we're already shown or not */
		if (app->status == APP_INDICATOR_STATUS_PASSIVE) {
                        if (can_add_application (priv->applications, app)) {
                                /* Put on panel */
                                priv->applications = g_list_insert_sorted_with_data (priv->applications, app, app_sort_func, priv->lrufile);

                                g_signal_emit(G_OBJECT(app->appstore),
                                              signals[APPLICATION_ADDED], 0,
                                              newicon,
                                              g_list_index(priv->applications, app), /* Position */
                                              app->dbus_name,
                                              app->menu,
                                              app->icon_path,
                                              "",
                                              "",
                                              TRUE);
                        }
		} else {
			/* Icon update */
			gint position = get_position(app);
			if (position == -1) return;

			g_signal_emit(G_OBJECT(appstore),
			              signals[APPLICATION_ICON_CHANGED], 0, 
			              position, newicon, TRUE);
		}
	}

	app->status = status;

	return;
}

/* Gets the data back on an updated icon signal.  Hopefully
   a new fun icon. */
static void
new_icon_cb (DBusGProxy * proxy, GValue value, GError * error, gpointer userdata)
{
	/* Check for errors */
	if (error != NULL) {
		g_warning("Unable to get updated icon name: %s", error->message);
		return;
	}

	/* Grab the icon and make sure we have one */
	const gchar * newicon = g_value_get_string(&value);
	if (newicon == NULL) {
		g_warning("Bad new icon :(");
		return;
	}

	Application * app = (Application *) userdata;

	if (g_strcmp0(newicon, app->icon)) {
		/* If the new icon is actually a new icon */
		if (app->icon != NULL) g_free(app->icon);
		app->icon = g_strdup(newicon);

		if (app->status == APP_INDICATOR_STATUS_ACTIVE) {
			gint position = get_position(app);
			if (position == -1) return;

			g_signal_emit(G_OBJECT(app->appstore),
			              signals[APPLICATION_ICON_CHANGED], 0, 
			              position, newicon, TRUE);
		}
	}

	return;
}

/* Gets the data back on an updated aicon signal.  Hopefully
   a new fun icon. */
static void
new_aicon_cb (DBusGProxy * proxy, GValue value, GError * error, gpointer userdata)
{
	/* Check for errors */
	if (error != NULL) {
		g_warning("Unable to get updated icon name: %s", error->message);
		return;
	}

	/* Grab the icon and make sure we have one */
	const gchar * newicon = g_value_get_string(&value);
	if (newicon == NULL) {
		g_warning("Bad new icon :(");
		return;
	}

	Application * app = (Application *) userdata;

	if (g_strcmp0(newicon, app->aicon)) {
		/* If the new icon is actually a new icon */
		if (app->aicon != NULL) g_free(app->aicon);
		app->aicon = g_strdup(newicon);

		if (app->status == APP_INDICATOR_STATUS_ATTENTION) {
			gint position = get_position(app);
			if (position == -1) return;

			g_signal_emit(G_OBJECT(app->appstore),
			              signals[APPLICATION_ICON_CHANGED], 0, 
			              position, newicon, TRUE);
		}
	}

	return;
}

/* Called when the Notification Item signals that it
   has a new icon. */
static void
new_icon (DBusGProxy * proxy, gpointer data)
{
	Application * app = (Application *)data;
	if (!app->validated) return;

	org_freedesktop_DBus_Properties_get_async(app->prop_proxy,
	                                          NOTIFICATION_ITEM_DBUS_IFACE,
	                                          NOTIFICATION_ITEM_PROP_ICON_NAME,
	                                          new_icon_cb,
	                                          app);
	return;
}

/* Called when the Notification Item signals that it
   has a new attention icon. */
static void
new_aicon (DBusGProxy * proxy, gpointer data)
{
	Application * app = (Application *)data;
	if (!app->validated) return;

	org_freedesktop_DBus_Properties_get_async(app->prop_proxy,
	                                          NOTIFICATION_ITEM_DBUS_IFACE,
	                                          NOTIFICATION_ITEM_PROP_AICON_NAME,
	                                          new_aicon_cb,
	                                          app);

	return;
}

/* Called when the Notification Item signals that it
   has a new status. */
static void
new_status (DBusGProxy * proxy, const gchar * status, gpointer data)
{
	Application * app = (Application *)data;
	if (!app->validated) return;

	apply_status(app, string_to_status(status));

	return;
}

/* Adding a new NotificationItem object from DBus in to the
   appstore.  First, we need to get the information on it
   though. */
void
application_service_appstore_application_add (ApplicationServiceAppstore * appstore, const gchar * dbus_name, const gchar * dbus_object)
{
	g_debug("Adding new application: %s:%s", dbus_name, dbus_object);

	/* Make sure we got a sensible request */
	g_return_if_fail(IS_APPLICATION_SERVICE_APPSTORE(appstore));
	g_return_if_fail(dbus_name != NULL && dbus_name[0] != '\0');
	g_return_if_fail(dbus_object != NULL && dbus_object[0] != '\0');
	ApplicationServiceAppstorePrivate * priv = appstore->priv;

	/* Build the application entry.  This will be carried
	   along until we're sure we've got everything. */
	Application * app = g_new0(Application, 1);

	app->validated = FALSE;
	app->dbus_name = g_strdup(dbus_name);
	app->dbus_object = g_strdup(dbus_object);
	app->appstore = appstore;
	app->status = APP_INDICATOR_STATUS_PASSIVE;
	app->icon = NULL;
	app->aicon = NULL;
	app->menu = NULL;
	app->icon_path = NULL;
	app->currently_free = FALSE;

	/* Get the DBus proxy for the NotificationItem interface */
	GError * error = NULL;
	app->dbus_proxy = dbus_g_proxy_new_for_name_owner(priv->bus,
	                                                  app->dbus_name,
	                                                  app->dbus_object,
	                                                  NOTIFICATION_ITEM_DBUS_IFACE,
	                                                  &error);

	if (error != NULL) {
		g_warning("Unable to get notification item proxy for object '%s' on host '%s': %s", dbus_object, dbus_name, error->message);
		g_error_free(error);
		g_free(app);
		return;
	}
	
	/* We've got it, let's watch it for destruction */
	g_signal_connect(G_OBJECT(app->dbus_proxy), "destroy", G_CALLBACK(application_removed_cb), app);

	/* Grab the property proxy interface */
	app->prop_proxy = dbus_g_proxy_new_for_name_owner(priv->bus,
	                                                  app->dbus_name,
	                                                  app->dbus_object,
	                                                  DBUS_INTERFACE_PROPERTIES,
	                                                  &error);

	if (error != NULL) {
		g_warning("Unable to get property proxy for object '%s' on host '%s': %s", dbus_object, dbus_name, error->message);
		g_error_free(error);
		g_object_unref(app->dbus_proxy);
		g_free(app);
		return;
	}

	/* Connect to signals */
	dbus_g_proxy_add_signal(app->dbus_proxy,
	                        NOTIFICATION_ITEM_SIG_NEW_ICON,
	                        G_TYPE_INVALID);
	dbus_g_proxy_add_signal(app->dbus_proxy,
	                        NOTIFICATION_ITEM_SIG_NEW_AICON,
	                        G_TYPE_INVALID);
	dbus_g_proxy_add_signal(app->dbus_proxy,
	                        NOTIFICATION_ITEM_SIG_NEW_STATUS,
	                        G_TYPE_STRING,
	                        G_TYPE_INVALID);

	dbus_g_proxy_connect_signal(app->dbus_proxy,
	                            NOTIFICATION_ITEM_SIG_NEW_ICON,
	                            G_CALLBACK(new_icon),
	                            app,
	                            NULL);
	dbus_g_proxy_connect_signal(app->dbus_proxy,
	                            NOTIFICATION_ITEM_SIG_NEW_AICON,
	                            G_CALLBACK(new_aicon),
	                            app,
	                            NULL);
	dbus_g_proxy_connect_signal(app->dbus_proxy,
	                            NOTIFICATION_ITEM_SIG_NEW_STATUS,
	                            G_CALLBACK(new_status),
	                            app,
	                            NULL);

	/* Get all the propertiees */
	org_freedesktop_DBus_Properties_get_all_async(app->prop_proxy,
	                                              NOTIFICATION_ITEM_DBUS_IFACE,
	                                              get_all_properties_cb,
	                                              app);

	/* We're returning, nothing is yet added until the properties
	   come back and give us more info. */
	return;
}

/* Removes an application.  Currently only works for the apps
   that are shown.  /TODO Need to fix that. */
void
application_service_appstore_application_remove (ApplicationServiceAppstore * appstore, const gchar * dbus_name, const gchar * dbus_object)
{
	g_return_if_fail(IS_APPLICATION_SERVICE_APPSTORE(appstore));
	g_return_if_fail(dbus_name != NULL && dbus_name[0] != '\0');
	g_return_if_fail(dbus_object != NULL && dbus_object[0] != '\0');

	ApplicationServiceAppstorePrivate * priv = appstore->priv;
	GList * listpntr;

	for (listpntr = priv->applications; listpntr != NULL; listpntr = g_list_next(listpntr)) {
		Application * app = (Application *)listpntr->data;

		if (!g_strcmp0(app->dbus_name, dbus_name) && !g_strcmp0(app->dbus_object, dbus_object)) {
			application_removed_cb(NULL, app);
			break; /* NOTE: Must break as the list will become inconsistent */
		}
	}

	return;
}

/* Creates a basic appstore object and attaches the
   LRU file object to it. */
ApplicationServiceAppstore *
application_service_appstore_new (AppLruFile * lrufile)
{
	g_return_val_if_fail(IS_APP_LRU_FILE(lrufile), NULL);
	ApplicationServiceAppstore * appstore = APPLICATION_SERVICE_APPSTORE(g_object_new(APPLICATION_SERVICE_APPSTORE_TYPE, NULL));
	ApplicationServiceAppstorePrivate * priv = appstore->priv;
	priv->lrufile = lrufile;
	return appstore;
}

/* DBus Interface */
static gboolean
_application_service_server_get_applications (ApplicationServiceAppstore * appstore, GPtrArray ** apps, GError ** error)
{
	ApplicationServiceAppstorePrivate * priv = appstore->priv;

	*apps = g_ptr_array_new();
	GList * listpntr;
	gint position = 0;

	for (listpntr = priv->applications; listpntr != NULL; listpntr = g_list_next(listpntr)) {
		GValueArray * values = g_value_array_new(5);

		GValue value = {0};

		/* Icon name */
		g_value_init(&value, G_TYPE_STRING);
		g_value_set_string(&value, ((Application *)listpntr->data)->icon);
		g_value_array_append(values, &value);
		g_value_unset(&value);

		/* Position */
		g_value_init(&value, G_TYPE_INT);
		g_value_set_int(&value, position++);
		g_value_array_append(values, &value);
		g_value_unset(&value);

		/* DBus Address */
		g_value_init(&value, G_TYPE_STRING);
		g_value_set_string(&value, ((Application *)listpntr->data)->dbus_name);
		g_value_array_append(values, &value);
		g_value_unset(&value);

		/* DBus Object */
		g_value_init(&value, DBUS_TYPE_G_OBJECT_PATH);
		g_value_set_static_boxed(&value, ((Application *)listpntr->data)->menu);
		g_value_array_append(values, &value);
		g_value_unset(&value);

		/* Icon path */
		g_value_init(&value, G_TYPE_STRING);
		g_value_set_string(&value, ((Application *)listpntr->data)->icon_path);
		g_value_array_append(values, &value);
		g_value_unset(&value);

		/* Label */
		g_value_init(&value, G_TYPE_STRING);
		g_value_set_string(&value, ""); 
		g_value_array_append(values, &value);
		g_value_unset(&value);

		/* Guide */
		g_value_init(&value, G_TYPE_STRING);
		g_value_set_string(&value, ""); 
		g_value_array_append(values, &value);
		g_value_unset(&value);

		g_ptr_array_add(*apps, values);
	}

	return TRUE;
}

/* Frees the data associated with an approver */
static void
approver_free (gpointer papprover, gpointer user_data)
{
	Approver * approver = (Approver *)papprover;
	g_return_if_fail(approver != NULL);
	
	if (approver->proxy != NULL) {
		g_object_unref(approver->proxy);
		approver->proxy = NULL;
	}

	g_free(approver);
	return;
}

/* What did the approver tell us? */
static void
approver_request_cb (DBusGProxy *proxy, gboolean OUT_approved, GError *error, gpointer userdata)
{
	g_debug("Approver responded: %s", OUT_approved ? "approve" : "rejected");
	return;
}

/* Run the applications through the new approver */
static void
check_with_new_approver (gpointer papp, gpointer papprove)
{
	Application * app = (Application *)papp;
	Approver * approver = (Approver *)papprove;

	org_ayatana_StatusNotifierApprover_approve_item_async(approver->proxy,
	                                                      app->id,
	                                                      app->category,
	                                                      0,
	                                                      app->dbus_name,
	                                                      app->dbus_object,
	                                                      approver_request_cb,
	                                                      app);

	return;
}

/* Adds a new approver to the app store */
void
application_service_appstore_approver_add (ApplicationServiceAppstore * appstore, const gchar * dbus_name, const gchar * dbus_object)
{
	g_return_if_fail(IS_APPLICATION_SERVICE_APPSTORE(appstore));
	g_return_if_fail(dbus_name != NULL);
	g_return_if_fail(dbus_object != NULL);
	ApplicationServiceAppstorePrivate * priv = APPLICATION_SERVICE_APPSTORE_GET_PRIVATE (appstore);

	Approver * approver = g_new0(Approver, 1);

	GError * error = NULL;
	approver->proxy = dbus_g_proxy_new_for_name_owner(priv->bus,
	                                                  dbus_name,
	                                                  dbus_object,
	                                                  NOTIFICATION_APPROVER_DBUS_IFACE,
	                                                  &error);
	if (error != NULL) {
		g_warning("Unable to get approver interface on '%s:%s' : %s", dbus_name, dbus_object, error->message);
		g_error_free(error);
		g_free(approver);
		return;
	}

	priv->approvers = g_list_prepend(priv->approvers, approver);

	g_list_foreach(priv->applications, check_with_new_approver, approver);

	return;
}