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

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/>.
*/

#include <string.h>
#include <locale.h>
#include <libintl.h>
#include <config.h>
#include <pango/pango-utils.h>
#include <libindicator/indicator-service.h>
#include <gio/gio.h>
#include <gio/gdesktopappinfo.h>
#include <glib/gi18n.h>

#include "app-menu-item.h"
#include "dbus-data.h"
#include "messages-service-dbus.h"
#include "status-items.h"

static IndicatorService * service = NULL;
static GList * serverList = NULL;

static GDBusConnection *bus;
static GSimpleActionGroup *actions;
static GMenu *menu;
static GSettings *settings;
static GMainLoop * mainloop = NULL;

static MessageServiceDbus * dbus_interface = NULL;


/*
 * Server List
 */

typedef struct _serverList_t serverList_t;
struct _serverList_t {
	AppMenuItem * menuitem;
	gboolean attention;
	guint count;
	GList * imList;
};

static gint
serverList_sort (gconstpointer a, gconstpointer b)
{
	serverList_t * pa, * pb;

	pa = (serverList_t *)a;
	pb = (serverList_t *)b;

	const gchar * pan = app_menu_item_get_name(pa->menuitem);
	const gchar * pbn = app_menu_item_get_name(pb->menuitem);

	return g_strcmp0(pan, pbn);
}

/* This function turns a specific desktop id into a menu
   item and registers it appropriately with everyone */
static gboolean
build_launcher (gpointer data)
{
	gchar *desktop_id = data;
	GDesktopAppInfo *appinfo;
	GList *listitem;

	appinfo = g_desktop_app_info_new (desktop_id);

	/* Check to see if we already have a launcher */
	for (listitem = serverList; listitem != NULL; listitem = listitem->next) {
		serverList_t * slt = listitem->data;
		if (!g_strcmp0(app_menu_item_get_desktop(slt->menuitem),
			       g_desktop_app_info_get_filename (appinfo))) {
			break;
		}
	}

	if (listitem == NULL) {
		/* If not */
		/* Build the item */
		serverList_t * sl_item = g_new0(serverList_t, 1);
		sl_item->menuitem = app_menu_item_new(appinfo);

		serverList = g_list_insert_sorted (serverList, sl_item, serverList_sort);

		/* TODO insert it at the right position (alphabetically by application name) */
		g_menu_insert_section (menu, 2,
				       app_menu_item_get_name (sl_item->menuitem),
				       app_menu_item_get_menu (sl_item->menuitem));
	}

	g_object_unref (appinfo);
	g_free (desktop_id);
	return FALSE;
}

/* This function goes through all the launchers that we're
   supposed to be grabbing and decides to show turn them
   into menu items or not.  It doens't do the work, but it
   makes the decision. */
static gboolean
build_launchers (gpointer data)
{
	gchar **applications = g_settings_get_strv (settings, "applications");
	gchar **app;

	g_return_val_if_fail (applications != NULL, FALSE);

	for (app = applications; *app; app++)
	{
		g_idle_add(build_launcher, g_strdup (*app));
	}

	serverList = g_list_sort(serverList, serverList_sort);

	g_strfreev (applications);
	return FALSE;
}

static void 
service_shutdown (IndicatorService * service, gpointer user_data)
{
	g_warning("Shutting down service!");
	g_main_loop_quit(mainloop);
	return;
}

static void
clear_action_activate (GSimpleAction *simple,
		       GVariant *param,
		       gpointer user_data)
{
	MessageServiceDbus *msg_service = user_data;
	message_service_dbus_set_attention(msg_service, FALSE);
}

static void
clear_action_handler (MessageServiceDbus *msd,
		      gboolean attention,
		      gpointer user_data)
{
	GSimpleAction *action = user_data;
	g_simple_action_set_enabled (action, attention);
}

int
main (int argc, char ** argv)
{
	GError *error = NULL;
	GActionEntry entries[] = {
		{ "status", NULL, "s", "'offline'", NULL },
		{ "clear", clear_action_activate }
	};
	GMenuModel *status_items;

	/* Glib init */
	g_type_init();

	/* Create the Indicator Service interface */
	service = indicator_service_new_version(INDICATOR_MESSAGES_DBUS_NAME, 1);
	g_signal_connect(service, INDICATOR_SERVICE_SIGNAL_SHUTDOWN, G_CALLBACK(service_shutdown), NULL);

	/* 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 */
	dbus_interface = message_service_dbus_new();

	bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
	if (!bus) {
		g_warning ("unable to connect to the session bus: %s", error->message);
		g_error_free (error);
		return 1;
	}

	actions = g_simple_action_group_new ();
	g_simple_action_group_add_entries (actions, entries, G_N_ELEMENTS (entries), NULL);
	g_dbus_connection_export_action_group (bus, INDICATOR_MESSAGES_DBUS_OBJECT,
                                               G_ACTION_GROUP (actions), &error);
	if (error) {
		g_warning ("unable to export action group on dbus: %s", error->message);
		g_error_free (error);
		return 1;
	}

	g_signal_connect (dbus_interface, MESSAGE_SERVICE_DBUS_SIGNAL_ATTENTION_CHANGED,
			  G_CALLBACK(clear_action_handler),
			  g_action_map_lookup_action (G_ACTION_MAP (actions), "clear"));

	status_items = status_items_build (g_action_map_lookup_action (G_ACTION_MAP (actions), "status"));

	menu = g_menu_new ();
	g_menu_append_section (menu, _("Status"), status_items);
	g_menu_append (menu, _("Clear"), "clear");

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

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

	g_idle_add(build_launchers, NULL);

	/* Let's run a mainloop */
	mainloop = g_main_loop_new(NULL, FALSE);
	g_main_loop_run(mainloop);

	/* Clean up */
	status_items_cleanup();
	g_object_unref (settings);

	return 0;
}