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

Copyright 2012 Canonical Ltd.

Authors:
    Ted Gould <ted@canonical.com>
    Lars Uebernickel <lars.uebernickel@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/>.
*/

#include <config.h>
#include <locale.h>
#include <gio/gio.h>
#include <glib/gi18n.h>

#include "dbus-data.h"
#include "gsettingsstrv.h"
#include "gmenuutils.h"
#include "indicator-messages-service.h"
#include "indicator-messages-application.h"
#include "im-phone-menu.h"
#include "im-application-list.h"

#define NUM_STATUSES 5

static ImApplicationList *applications;

static IndicatorMessagesService *messages_service;
static GMenu *toplevel_menu;
static ImPhoneMenu *menu;
static GSettings *settings;

static void
register_application (IndicatorMessagesService *service,
		      GDBusMethodInvocation *invocation,
		      const gchar *desktop_id,
		      const gchar *menu_path,
		      gpointer user_data)
{
	GDBusConnection *bus;
	const gchar *sender;

	im_application_list_add (applications, desktop_id);

	bus = g_dbus_interface_skeleton_get_connection (G_DBUS_INTERFACE_SKELETON (service));
	sender = g_dbus_method_invocation_get_sender (invocation);

	im_application_list_set_remote (applications, desktop_id, bus, sender, menu_path);
	g_settings_strv_append_unique (settings, "applications", desktop_id);

	indicator_messages_service_complete_register_application (service, invocation);
}

static void
unregister_application (IndicatorMessagesService *service,
			GDBusMethodInvocation *invocation,
			const gchar *desktop_id,
			gpointer user_data)
{
	im_application_list_remove (applications, desktop_id);
	g_settings_strv_remove (settings, "applications", desktop_id);

	indicator_messages_service_complete_unregister_application (service, invocation);
}

static void
on_bus_acquired (GDBusConnection *bus,
		 const gchar     *name,
		 gpointer         user_data)
{
	GError *error = NULL;

	g_dbus_connection_export_action_group (bus, INDICATOR_MESSAGES_DBUS_OBJECT,
					       im_application_list_get_action_group (applications),
					       &error);
	if (error) {
		g_warning ("unable to export action group on dbus: %s", error->message);
		g_error_free (error);
		return;
	}

	g_dbus_connection_export_menu_model (bus, INDICATOR_MESSAGES_DBUS_OBJECT "/phone",
					     G_MENU_MODEL (toplevel_menu), &error);
	if (error) {
		g_warning ("unable to export menu on dbus: %s", error->message);
		g_error_free (error);
		return;
	}

	g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (messages_service),
					  bus, INDICATOR_MESSAGES_DBUS_SERVICE_OBJECT,
					  &error);
	if (error) {
		g_warning ("unable to export messages service on dbus: %s", error->message);
		g_error_free (error);
		return;
	}

	g_object_unref (bus);
}

static void
on_name_lost (GDBusConnection *bus,
	      const gchar     *name,
	      gpointer         user_data)
{
	GMainLoop *mainloop = user_data;

	g_main_loop_quit (mainloop);
}

int
main (int argc, char ** argv)
{
	GMainLoop * mainloop = NULL;
	GMenuItem *root;
	GBusNameOwnerFlags flags;

	/* Glib init */
	g_type_init();

	mainloop = g_main_loop_new (NULL, FALSE);

	/* Setting up i18n and gettext.  Apparently, we need
	   all of these. */
	setlocale (LC_ALL, "");
	bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
	textdomain (GETTEXT_PACKAGE);

	/* Bring up the service DBus interface */
	messages_service = indicator_messages_service_skeleton_new ();

	flags = G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT;
	if (argc >= 2 && g_str_equal (argv[1], "--replace"))
		flags |= G_BUS_NAME_OWNER_FLAGS_REPLACE;

	g_bus_own_name (G_BUS_TYPE_SESSION, "com.canonical.indicator.messages", flags,
			on_bus_acquired, NULL, on_name_lost, mainloop, NULL);

	g_signal_connect (messages_service, "handle-register-application",
			  G_CALLBACK (register_application), NULL);
	g_signal_connect (messages_service, "handle-unregister-application",
			  G_CALLBACK (unregister_application), NULL);

	menu = im_phone_menu_new ();

	toplevel_menu = g_menu_new ();
	root = g_menu_item_new (NULL, "messages");
	g_menu_item_set_attribute (root, "x-canonical-type", "s", "com.canonical.indicator.root");
	g_menu_item_set_submenu (root, im_phone_menu_get_model (menu));
	g_menu_append_item (toplevel_menu, root);

	settings = g_settings_new ("com.canonical.indicator.messages");

	applications = im_application_list_new ();
	g_signal_connect_swapped (applications, "message-added",
				  G_CALLBACK (im_phone_menu_add_message), menu);
	g_signal_connect_swapped (applications, "message-removed",
				  G_CALLBACK (im_phone_menu_remove_message), menu);
	g_signal_connect_swapped (applications, "app-stopped",
				  G_CALLBACK (im_phone_menu_remove_application), menu);
	g_signal_connect_swapped (applications, "remove-all",
				  G_CALLBACK (im_phone_menu_remove_all), menu);

	g_main_loop_run(mainloop);

	/* Clean up */
	g_object_unref (root);
	g_object_unref (messages_service);
	g_object_unref (settings);
	g_object_unref (applications);
	return 0;
}