summaryrefslogtreecommitdiff
path: root/megaphone/src/megaphone-applet.c
blob: 5deeb6c9809e799d0903f8d713a7a7c6764b8e84 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * Copyright (C) 2007 Raphaël Slinckx <raphael@slinckx.net>
 * Copyright (C) 2007 Collabora Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Authors: Raphaël Slinckx <raphael@slinckx.net>
 *          Xavier Claessens <xclaesse@gmail.com>
 */

#include "config.h"

#include <string.h>

#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <bonobo/bonobo-ui-component.h>
#include <panel-2.0/panel-applet-gconf.h>
#include <gconf/gconf-client.h>
#include <libgnomevfs/gnome-vfs-utils.h>

#include <libmissioncontrol/mission-control.h>
#include <libmissioncontrol/mc-account.h>

#include <libempathy/empathy-contact-factory.h>
#include <libempathy/empathy-contact.h>
#include <libempathy/empathy-contact-list.h>
#include <libempathy/empathy-contact-manager.h>
#include <libempathy/empathy-utils.h>

#include <libempathy-gtk/empathy-contact-list-view.h>
#include <libempathy-gtk/empathy-contact-list-store.h>
#include <libempathy-gtk/empathy-contact-dialogs.h>
#include <libempathy-gtk/empathy-ui-utils.h>

#include "megaphone-applet.h"

#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
#include <libempathy/empathy-debug.h>

#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, MegaphoneApplet)
typedef struct {
	EmpathyContactFactory *factory;
	GtkWidget             *image;
	gint                   image_size;
	EmpathyContact        *contact;
	GConfClient           *gconf;
	guint                  gconf_cnxn;
} MegaphoneAppletPriv;

static void megaphone_applet_finalize                  (GObject            *object);
static void megaphone_applet_size_allocate_cb          (GtkWidget          *widget,
							GtkAllocation      *allocation,
							MegaphoneApplet    *applet);
static gboolean megaphone_applet_button_press_event_cb (GtkWidget          *widget,
							GdkEventButton     *event, 
							MegaphoneApplet    *applet);
static void megaphone_applet_information_cb            (BonoboUIComponent  *uic,
							MegaphoneApplet    *applet, 
							const gchar        *verb_name);
static void megaphone_applet_preferences_cb            (BonoboUIComponent  *uic,
							MegaphoneApplet    *applet, 
							const gchar        *verb_name);
static void megaphone_applet_about_cb                  (BonoboUIComponent  *uic,
							MegaphoneApplet    *applet, 
							const gchar        *verb_name);

G_DEFINE_TYPE(MegaphoneApplet, megaphone_applet, PANEL_TYPE_APPLET)

static const BonoboUIVerb megaphone_applet_menu_verbs [] = {
	BONOBO_UI_UNSAFE_VERB ("information", megaphone_applet_information_cb),
	BONOBO_UI_UNSAFE_VERB ("preferences", megaphone_applet_preferences_cb),
	BONOBO_UI_UNSAFE_VERB ("about",       megaphone_applet_about_cb),
	BONOBO_UI_VERB_END
};

static const char* authors[] = {
	"Raphaël Slinckx <raphael@slinckx.net>", 
	"Xavier Claessens <xclaesse@gmail.com>", 
	NULL
};

static void
megaphone_applet_class_init (MegaphoneAppletClass *class)
{
	GObjectClass *object_class;

	object_class = G_OBJECT_CLASS (class);

	object_class->finalize = megaphone_applet_finalize;

	g_type_class_add_private (object_class, sizeof (MegaphoneAppletPriv));
}

static void
megaphone_applet_init (MegaphoneApplet *applet)
{
	MegaphoneAppletPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (applet,
		MEGAPHONE_TYPE_APPLET, MegaphoneAppletPriv);

	applet->priv = priv;
	priv->factory = empathy_contact_factory_new ();
	priv->gconf = gconf_client_get_default ();

	/* Image holds the contact avatar */
	priv->image = gtk_image_new ();
	gtk_widget_show (priv->image);
	gtk_container_add (GTK_CONTAINER (applet), priv->image);

	/* We want transparency */
	panel_applet_set_flags (PANEL_APPLET (applet), PANEL_APPLET_EXPAND_MINOR);
	panel_applet_set_background_widget (PANEL_APPLET (applet), GTK_WIDGET (applet));

	/* Listen for clicks on the applet to dispatch a channel */
	g_signal_connect (applet, "button-press-event",
			  G_CALLBACK (megaphone_applet_button_press_event_cb),
			  applet);

	/* Allow to resize our avatar when needed */
	g_signal_connect (applet, "size-allocate",
			  G_CALLBACK (megaphone_applet_size_allocate_cb),
			  applet);
}

static void
megaphone_applet_finalize (GObject *object)
{
	MegaphoneAppletPriv *priv = GET_PRIV (object);
	
	if (priv->contact) {
		g_object_unref (priv->contact);
	}
	g_object_unref (priv->factory);

	if (priv->gconf_cnxn != 0) {
		gconf_client_notify_remove (priv->gconf, priv->gconf_cnxn);
	}
	g_object_unref (priv->gconf);

	G_OBJECT_CLASS (megaphone_applet_parent_class)->finalize (object);
}

static void
megaphone_applet_update_icon (MegaphoneApplet *applet)
{
	MegaphoneAppletPriv *priv = GET_PRIV (applet);
	EmpathyAvatar       *avatar = NULL;
	GdkPixbuf           *avatar_pixbuf;

	if (priv->contact) {
		avatar = empathy_contact_get_avatar (priv->contact);
	} else {
		gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
					      GTK_STOCK_PREFERENCES,
					      GTK_ICON_SIZE_MENU);
		return;
	}

	if (!avatar) {
		gchar *avatar_token;

		/* Try to take avatar from cache */
		avatar_token = panel_applet_gconf_get_string (PANEL_APPLET (applet),
							      "avatar_token",
							      NULL);
		if (!G_STR_EMPTY (avatar_token)) {
			avatar = empathy_avatar_new_from_cache (avatar_token);
		}
		g_free (avatar_token);
	} else {
		empathy_avatar_ref (avatar);
	}

	if (avatar) {
		avatar_pixbuf = empathy_pixbuf_from_avatar_scaled (avatar,
								   priv->image_size - 2,
								   priv->image_size - 2);
		empathy_avatar_unref (avatar);
	} else {
		GtkIconTheme *icon_theme;

		/* Load the default icon when no avatar is found */
		icon_theme = gtk_icon_theme_get_default ();
		avatar_pixbuf = gtk_icon_theme_load_icon (icon_theme,
							  "stock_contact",
							  priv->image_size - 2,
							  0, NULL);
	}

	/* Now some desaturation if the contact is offline */
	if (!empathy_contact_is_online (priv->contact)) {
		GdkPixbuf *offline_avatar;

		offline_avatar = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE,
						 8,
						 gdk_pixbuf_get_height (avatar_pixbuf),
						 gdk_pixbuf_get_width (avatar_pixbuf));
		gdk_pixbuf_saturate_and_pixelate (avatar_pixbuf,
						  offline_avatar,
						  0.0,
						  TRUE);
		g_object_unref (avatar_pixbuf);
		avatar_pixbuf = offline_avatar;
	}

	gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), avatar_pixbuf);
	g_object_unref (avatar_pixbuf);
}

static void
megaphone_applet_update_contact (MegaphoneApplet *applet)
{
	MegaphoneAppletPriv *priv = GET_PRIV (applet);
	const gchar         *name;
	const gchar         *status;
	gchar               *tip;
	const gchar         *avatar_token = NULL;

	if (priv->contact) {
		EmpathyAvatar *avatar;

		avatar = empathy_contact_get_avatar (priv->contact);
		if (avatar) {
			avatar_token = avatar->token;
		}
	}

	if (avatar_token) {
		panel_applet_gconf_set_string (PANEL_APPLET (applet),
					       "avatar_token", avatar_token,
					       NULL);
	}

	megaphone_applet_update_icon (applet);

	if (priv->contact ) {
		name = empathy_contact_get_name (priv->contact);
		status = empathy_contact_get_status (priv->contact);
		tip = g_strdup_printf ("<b>%s</b>: %s", name, status);
		gtk_widget_set_tooltip_markup (GTK_WIDGET (applet), tip);
		g_free (tip);
	} else {
		gtk_widget_set_tooltip_markup (GTK_WIDGET (applet),
					       "Please configure a contact.");
	}

}

static void
megaphone_applet_set_contact (MegaphoneApplet *applet,
			      const gchar     *str)
{
	MegaphoneAppletPriv *priv = GET_PRIV (applet);
	McAccount           *account = NULL;
	gchar              **strv = NULL;

	DEBUG ("Setting new contact %s", str);

	/* Release old contact, if any */
	if (priv->contact) {
		g_signal_handlers_disconnect_by_func (priv->contact,
						      megaphone_applet_update_contact,
						      applet);
		g_object_unref (priv->contact),
		priv->contact = NULL;
	}

	/* Lookup the new contact */
	if (str) {
		strv = g_strsplit (str, "/", 2);
		account = mc_account_lookup (strv[0]);
	}
	if (account) {
		priv->contact = empathy_contact_factory_get_from_id (priv->factory,
								     account,
								     strv[1]);
		g_object_unref (account);
	}
	g_strfreev (strv);

	/* Take hold of the new contact if any */
	if (priv->contact) {
		/* Listen for updates on the contact, and force a first update */
		g_signal_connect_swapped (priv->contact, "notify",
					  G_CALLBACK (megaphone_applet_update_contact),
					  applet);
	}

	megaphone_applet_update_contact (applet);
}

static void
megaphone_applet_preferences_response_cb (GtkWidget       *dialog,
					  gint             response,
					  MegaphoneApplet *applet) 
{
	if (response == GTK_RESPONSE_ACCEPT) {
		EmpathyContactListView *contact_list;
		EmpathyContact         *contact;

		/* Retrieve the selected contact, if any and set it up in gconf.
		 * GConf will notify us from the change and we will adjust ourselves */
		contact_list = g_object_get_data (G_OBJECT (dialog), "contact-list");
		contact = empathy_contact_list_view_get_selected (contact_list);
		if (contact) {
			McAccount   *account;
			const gchar *account_id;
			const gchar *contact_id;
			gchar       *str;

			account = empathy_contact_get_account (contact);
			account_id = mc_account_get_unique_name (account);
			contact_id = empathy_contact_get_id (contact);

			str = g_strconcat (account_id, "/", contact_id, NULL);
			panel_applet_gconf_set_string (PANEL_APPLET (applet),
						       "avatar_token", "",
						       NULL);
			panel_applet_gconf_set_string (PANEL_APPLET (applet),
						       "contact_id", str,
						       NULL);
			g_free (str);
		}
	}
	gtk_widget_destroy (dialog);
}

static void
megaphone_applet_show_preferences (MegaphoneApplet *applet)
{
	GtkWidget               *dialog;
	GtkWidget               *scroll;
	EmpathyContactListView  *contact_list;
	EmpathyContactListStore *contact_store;
	EmpathyContactManager   *contact_manager;

	dialog = gtk_dialog_new_with_buttons ("Select contact...",
					      NULL, 0,
					      GTK_STOCK_CANCEL,
					      GTK_RESPONSE_REJECT,
					      GTK_STOCK_OK,
					      GTK_RESPONSE_ACCEPT,
					      NULL);

	/* Show all contacts, even offline and sort alphabetically */
	contact_manager = empathy_contact_manager_new ();
	contact_store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (contact_manager));
	g_object_set (contact_store,
		      "is-compact", TRUE,
		      "show-avatars", TRUE,
		      "show-offline", TRUE,
		      "sort-criterium", EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
		      NULL);
	contact_list = empathy_contact_list_view_new (contact_store,
						      EMPATHY_CONTACT_LIST_FEATURE_NONE,
						      EMPATHY_CONTACT_FEATURE_NONE);
	g_object_unref (contact_manager);
	g_object_unref (contact_store);
	gtk_widget_show (GTK_WIDGET (contact_list));

	gtk_window_set_default_size (GTK_WINDOW (dialog), 300, 500);
	scroll = gtk_scrolled_window_new (NULL, NULL);
	gtk_container_add (GTK_CONTAINER (scroll), GTK_WIDGET (contact_list));
	gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), scroll);
	gtk_widget_show (scroll);
	
	g_object_set_data (G_OBJECT (dialog), "contact-list", contact_list);

	g_signal_connect (dialog, "response",
			  G_CALLBACK (megaphone_applet_preferences_response_cb),
			  applet);

	gtk_widget_show (dialog);
}

static void
megaphone_applet_information_cb (BonoboUIComponent *uic,
				 MegaphoneApplet   *applet,
				 const gchar       *verb_name)
{
	MegaphoneAppletPriv *priv = GET_PRIV (applet);

	/* FIXME: We should grey out the menu item if there are no available contact */
	if (priv->contact) {
		empathy_contact_information_dialog_show (priv->contact, NULL, FALSE, FALSE);
	}
}

static void
megaphone_applet_preferences_cb (BonoboUIComponent *uic, 
				 MegaphoneApplet   *applet, 
				 const gchar       *verb_name)
{
	megaphone_applet_show_preferences (applet);
}

static void
megaphone_applet_about_cb (BonoboUIComponent *uic, 
			   MegaphoneApplet   *applet, 
			   const gchar       *verb_name)
{
	gtk_show_about_dialog (NULL,
			       "name", "Megaphone", 
			       "version", PACKAGE_VERSION,
			       "copyright", "Raphaël Slinckx 2007\nCollabora Ltd 2007",
			       "comments", _("Talk!"),
			       "authors", authors,
			       "logo-icon-name", "stock_people",
			       NULL);
}

static gboolean
megaphone_applet_button_press_event_cb (GtkWidget       *widget,
					GdkEventButton  *event, 
					MegaphoneApplet *applet)
{
	MegaphoneAppletPriv *priv = GET_PRIV (applet);
	MissionControl      *mc;

	/* Only react on left-clicks */
	if (event->button != 1 || event->type != GDK_BUTTON_PRESS) {
		return FALSE;
	}

	/* If the contact is unavailable we display the preferences dialog */
	if (priv->contact == NULL) {
		megaphone_applet_show_preferences (applet);
		return TRUE;
	}
	
	DEBUG ("Requesting text channel for contact %s (%d)",
		empathy_contact_get_id (priv->contact),
		empathy_contact_get_handle (priv->contact));

	mc = empathy_mission_control_new ();
	mission_control_request_channel (mc,
					 empathy_contact_get_account (priv->contact),
					 TP_IFACE_CHANNEL_TYPE_TEXT,
					 empathy_contact_get_handle (priv->contact),
					 TP_HANDLE_TYPE_CONTACT,
					 NULL, NULL);
	g_object_unref (mc);
	
	return TRUE;
}

static void
megaphone_applet_size_allocate_cb (GtkWidget       *widget,
				   GtkAllocation   *allocation,
				   MegaphoneApplet *applet)
{
	MegaphoneAppletPriv *priv = GET_PRIV (applet);
	gint                 size;
	PanelAppletOrient    orient;

	orient = panel_applet_get_orient (PANEL_APPLET (widget));
	if (orient == PANEL_APPLET_ORIENT_LEFT ||
	    orient == PANEL_APPLET_ORIENT_RIGHT) {
		size = allocation->width;
	} else {
		size = allocation->height;
	}

	if (size != priv->image_size) {
		priv->image_size = size;
		megaphone_applet_update_icon (applet);
	}
}

static void
megaphone_applet_gconf_notify_cb (GConfClient     *client,
				  guint            cnxn_id,
				  GConfEntry      *entry,
				  MegaphoneApplet *applet)
{
	const gchar *key;
	GConfValue  *value;

	key = gconf_entry_get_key (entry);
	value = gconf_entry_get_value (entry);
	DEBUG ("GConf notification for key '%s'", key);

	if (value && g_str_has_suffix (key, "/contact_id")) {
		megaphone_applet_set_contact (applet,
					      gconf_value_get_string (value));
	}
}

static gboolean
megaphone_applet_factory (PanelApplet *applet, 
			  const gchar *iid, 
			  gpointer     data)
{
	MegaphoneAppletPriv *priv = GET_PRIV (applet);
	gchar               *pref_dir;
	gchar               *contact_id;

	/* Ensure it's us! */
	if (strcmp (iid, "OAFIID:GNOME_Megaphone_Applet") != 0) {
		return FALSE;
	}
	
	DEBUG ("Starting up new instance!");

	/* Set up the right-click menu */
	panel_applet_setup_menu_from_file (applet,
					   PKGDATADIR,
					   "GNOME_Megaphone_Applet.xml",
					   NULL,
					   megaphone_applet_menu_verbs,
					   applet);

	/* Define the schema to be used for each applet instance's preferences */
	panel_applet_add_preferences (applet,
				      "/schemas/apps/megaphone-applet/prefs",
				      NULL);
	
	/* We watch the preferences directory */
	pref_dir = panel_applet_gconf_get_full_key (applet, "");
	pref_dir[strlen (pref_dir)-1] = '\0';
	gconf_client_add_dir (priv->gconf, pref_dir,
			      GCONF_CLIENT_PRELOAD_ONELEVEL,
			      NULL);
	gconf_client_notify_add (priv->gconf, pref_dir,
				 (GConfClientNotifyFunc) megaphone_applet_gconf_notify_cb,
				 applet,
				 NULL, NULL);
	g_free (pref_dir);

	/* Initial setup with the pre-existing gconf key, or contact_id=NULL if no previous pref */
	contact_id = panel_applet_gconf_get_string (PANEL_APPLET (applet), "contact_id", NULL);
	megaphone_applet_set_contact (MEGAPHONE_APPLET (applet), contact_id);
	g_free (contact_id);

	/* Let's go! */
	gtk_widget_show (GTK_WIDGET (applet));

	return TRUE;
}

PANEL_APPLET_BONOBO_FACTORY ("OAFIID:GNOME_Megaphone_Applet_Factory",
			     MEGAPHONE_TYPE_APPLET,
			     "Megaphone", PACKAGE_VERSION,
			     megaphone_applet_factory,
			     NULL);