1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Raphaël Slinckx <raphael@slinckx.net>
4 * Copyright (C) 2007 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * Authors: Raphaël Slinckx <raphael@slinckx.net>
21 * Xavier Claessens <xclaesse@gmail.com>
28 #include <glib/gi18n.h>
30 #include <bonobo/bonobo-ui-component.h>
31 #include <panel-2.0/panel-applet-gconf.h>
32 #include <gconf/gconf-client.h>
33 #include <libgnomevfs/gnome-vfs-utils.h>
35 #include <libmissioncontrol/mission-control.h>
36 #include <libmissioncontrol/mc-account.h>
38 #include <libempathy/empathy-contact-factory.h>
39 #include <libempathy/empathy-contact.h>
40 #include <libempathy/empathy-contact-list.h>
41 #include <libempathy/empathy-contact-manager.h>
42 #include <libempathy/empathy-utils.h>
44 #include <libempathy-gtk/empathy-contact-list-view.h>
45 #include <libempathy-gtk/empathy-contact-list-store.h>
46 #include <libempathy-gtk/empathy-contact-dialogs.h>
47 #include <libempathy-gtk/empathy-ui-utils.h>
49 #include "megaphone-applet.h"
51 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
52 #include <libempathy/empathy-debug.h>
54 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, MegaphoneApplet)
56 EmpathyContactFactory *factory;
59 EmpathyContact *contact;
62 } MegaphoneAppletPriv;
64 static void megaphone_applet_finalize (GObject *object);
65 static void megaphone_applet_size_allocate_cb (GtkWidget *widget,
66 GtkAllocation *allocation,
67 MegaphoneApplet *applet);
68 static gboolean megaphone_applet_button_press_event_cb (GtkWidget *widget,
69 GdkEventButton *event,
70 MegaphoneApplet *applet);
71 static void megaphone_applet_information_cb (BonoboUIComponent *uic,
72 MegaphoneApplet *applet,
73 const gchar *verb_name);
74 static void megaphone_applet_preferences_cb (BonoboUIComponent *uic,
75 MegaphoneApplet *applet,
76 const gchar *verb_name);
77 static void megaphone_applet_about_cb (BonoboUIComponent *uic,
78 MegaphoneApplet *applet,
79 const gchar *verb_name);
81 G_DEFINE_TYPE(MegaphoneApplet, megaphone_applet, PANEL_TYPE_APPLET)
83 static const BonoboUIVerb megaphone_applet_menu_verbs [] = {
84 BONOBO_UI_UNSAFE_VERB ("information", megaphone_applet_information_cb),
85 BONOBO_UI_UNSAFE_VERB ("preferences", megaphone_applet_preferences_cb),
86 BONOBO_UI_UNSAFE_VERB ("about", megaphone_applet_about_cb),
90 static const char* authors[] = {
91 "Raphaël Slinckx <raphael@slinckx.net>",
92 "Xavier Claessens <xclaesse@gmail.com>",
97 megaphone_applet_class_init (MegaphoneAppletClass *class)
99 GObjectClass *object_class;
101 object_class = G_OBJECT_CLASS (class);
103 object_class->finalize = megaphone_applet_finalize;
105 g_type_class_add_private (object_class, sizeof (MegaphoneAppletPriv));
109 megaphone_applet_init (MegaphoneApplet *applet)
111 MegaphoneAppletPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (applet,
112 MEGAPHONE_TYPE_APPLET, MegaphoneAppletPriv);
115 priv->factory = empathy_contact_factory_new ();
116 priv->gconf = gconf_client_get_default ();
118 /* Image holds the contact avatar */
119 priv->image = gtk_image_new ();
120 gtk_widget_show (priv->image);
121 gtk_container_add (GTK_CONTAINER (applet), priv->image);
123 /* We want transparency */
124 panel_applet_set_flags (PANEL_APPLET (applet), PANEL_APPLET_EXPAND_MINOR);
125 panel_applet_set_background_widget (PANEL_APPLET (applet), GTK_WIDGET (applet));
127 /* Listen for clicks on the applet to dispatch a channel */
128 g_signal_connect (applet, "button-press-event",
129 G_CALLBACK (megaphone_applet_button_press_event_cb),
132 /* Allow to resize our avatar when needed */
133 g_signal_connect (applet, "size-allocate",
134 G_CALLBACK (megaphone_applet_size_allocate_cb),
139 megaphone_applet_finalize (GObject *object)
141 MegaphoneAppletPriv *priv = GET_PRIV (object);
144 g_object_unref (priv->contact);
146 g_object_unref (priv->factory);
148 if (priv->gconf_cnxn != 0) {
149 gconf_client_notify_remove (priv->gconf, priv->gconf_cnxn);
151 g_object_unref (priv->gconf);
153 G_OBJECT_CLASS (megaphone_applet_parent_class)->finalize (object);
157 megaphone_applet_update_icon (MegaphoneApplet *applet)
159 MegaphoneAppletPriv *priv = GET_PRIV (applet);
160 EmpathyAvatar *avatar = NULL;
161 GdkPixbuf *avatar_pixbuf;
164 avatar = empathy_contact_get_avatar (priv->contact);
166 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
167 GTK_STOCK_PREFERENCES,
175 /* Try to take avatar from cache */
176 avatar_token = panel_applet_gconf_get_string (PANEL_APPLET (applet),
179 if (!G_STR_EMPTY (avatar_token)) {
180 avatar = empathy_avatar_new_from_cache (avatar_token);
182 g_free (avatar_token);
184 empathy_avatar_ref (avatar);
188 avatar_pixbuf = empathy_pixbuf_from_avatar_scaled (avatar,
189 priv->image_size - 2,
190 priv->image_size - 2);
191 empathy_avatar_unref (avatar);
193 GtkIconTheme *icon_theme;
195 /* Load the default icon when no avatar is found */
196 icon_theme = gtk_icon_theme_get_default ();
197 avatar_pixbuf = gtk_icon_theme_load_icon (icon_theme,
199 priv->image_size - 2,
203 /* Now some desaturation if the contact is offline */
204 if (!empathy_contact_is_online (priv->contact)) {
205 GdkPixbuf *offline_avatar;
207 offline_avatar = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE,
209 gdk_pixbuf_get_height (avatar_pixbuf),
210 gdk_pixbuf_get_width (avatar_pixbuf));
211 gdk_pixbuf_saturate_and_pixelate (avatar_pixbuf,
215 g_object_unref (avatar_pixbuf);
216 avatar_pixbuf = offline_avatar;
219 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), avatar_pixbuf);
220 g_object_unref (avatar_pixbuf);
224 megaphone_applet_update_contact (MegaphoneApplet *applet)
226 MegaphoneAppletPriv *priv = GET_PRIV (applet);
230 const gchar *avatar_token = NULL;
233 EmpathyAvatar *avatar;
235 avatar = empathy_contact_get_avatar (priv->contact);
237 avatar_token = avatar->token;
242 panel_applet_gconf_set_string (PANEL_APPLET (applet),
243 "avatar_token", avatar_token,
247 megaphone_applet_update_icon (applet);
249 if (priv->contact ) {
250 name = empathy_contact_get_name (priv->contact);
251 status = empathy_contact_get_status (priv->contact);
252 tip = g_strdup_printf ("<b>%s</b>: %s", name, status);
253 gtk_widget_set_tooltip_markup (GTK_WIDGET (applet), tip);
256 gtk_widget_set_tooltip_markup (GTK_WIDGET (applet),
257 "Please configure a contact.");
263 megaphone_applet_set_contact (MegaphoneApplet *applet,
266 MegaphoneAppletPriv *priv = GET_PRIV (applet);
267 McAccount *account = NULL;
270 DEBUG ("Setting new contact %s", str);
272 /* Release old contact, if any */
274 g_signal_handlers_disconnect_by_func (priv->contact,
275 megaphone_applet_update_contact,
277 g_object_unref (priv->contact),
278 priv->contact = NULL;
281 /* Lookup the new contact */
283 strv = g_strsplit (str, "/", 2);
284 account = mc_account_lookup (strv[0]);
287 priv->contact = empathy_contact_factory_get_from_id (priv->factory,
290 g_object_unref (account);
294 /* Take hold of the new contact if any */
296 /* Listen for updates on the contact, and force a first update */
297 g_signal_connect_swapped (priv->contact, "notify",
298 G_CALLBACK (megaphone_applet_update_contact),
302 megaphone_applet_update_contact (applet);
306 megaphone_applet_preferences_response_cb (GtkWidget *dialog,
308 MegaphoneApplet *applet)
310 if (response == GTK_RESPONSE_ACCEPT) {
311 EmpathyContactListView *contact_list;
312 EmpathyContact *contact;
314 /* Retrieve the selected contact, if any and set it up in gconf.
315 * GConf will notify us from the change and we will adjust ourselves */
316 contact_list = g_object_get_data (G_OBJECT (dialog), "contact-list");
317 contact = empathy_contact_list_view_get_selected (contact_list);
320 const gchar *account_id;
321 const gchar *contact_id;
324 account = empathy_contact_get_account (contact);
325 account_id = mc_account_get_unique_name (account);
326 contact_id = empathy_contact_get_id (contact);
328 str = g_strconcat (account_id, "/", contact_id, NULL);
329 panel_applet_gconf_set_string (PANEL_APPLET (applet),
332 panel_applet_gconf_set_string (PANEL_APPLET (applet),
338 gtk_widget_destroy (dialog);
342 megaphone_applet_show_preferences (MegaphoneApplet *applet)
346 EmpathyContactListView *contact_list;
347 EmpathyContactListStore *contact_store;
348 EmpathyContactManager *contact_manager;
350 dialog = gtk_dialog_new_with_buttons ("Select contact...",
358 /* Show all contacts, even offline and sort alphabetically */
359 contact_manager = empathy_contact_manager_new ();
360 contact_store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (contact_manager));
361 g_object_set (contact_store,
363 "show-avatars", TRUE,
364 "show-offline", TRUE,
365 "sort-criterium", EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
367 contact_list = empathy_contact_list_view_new (contact_store,
368 EMPATHY_CONTACT_LIST_FEATURE_NONE,
369 EMPATHY_CONTACT_FEATURE_NONE);
370 g_object_unref (contact_manager);
371 g_object_unref (contact_store);
372 gtk_widget_show (GTK_WIDGET (contact_list));
374 gtk_window_set_default_size (GTK_WINDOW (dialog), 300, 500);
375 scroll = gtk_scrolled_window_new (NULL, NULL);
376 gtk_container_add (GTK_CONTAINER (scroll), GTK_WIDGET (contact_list));
377 gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), scroll);
378 gtk_widget_show (scroll);
380 g_object_set_data (G_OBJECT (dialog), "contact-list", contact_list);
382 g_signal_connect (dialog, "response",
383 G_CALLBACK (megaphone_applet_preferences_response_cb),
386 gtk_widget_show (dialog);
390 megaphone_applet_information_cb (BonoboUIComponent *uic,
391 MegaphoneApplet *applet,
392 const gchar *verb_name)
394 MegaphoneAppletPriv *priv = GET_PRIV (applet);
396 /* FIXME: We should grey out the menu item if there are no available contact */
398 empathy_contact_information_dialog_show (priv->contact, NULL, FALSE, FALSE);
403 megaphone_applet_preferences_cb (BonoboUIComponent *uic,
404 MegaphoneApplet *applet,
405 const gchar *verb_name)
407 megaphone_applet_show_preferences (applet);
411 megaphone_applet_about_cb (BonoboUIComponent *uic,
412 MegaphoneApplet *applet,
413 const gchar *verb_name)
415 gtk_show_about_dialog (NULL,
417 "version", PACKAGE_VERSION,
418 "copyright", "Raphaël Slinckx 2007\nCollabora Ltd 2007",
419 "comments", _("Talk!"),
421 "logo-icon-name", "stock_people",
426 megaphone_applet_button_press_event_cb (GtkWidget *widget,
427 GdkEventButton *event,
428 MegaphoneApplet *applet)
430 MegaphoneAppletPriv *priv = GET_PRIV (applet);
433 /* Only react on left-clicks */
434 if (event->button != 1 || event->type != GDK_BUTTON_PRESS) {
438 /* If the contact is unavailable we display the preferences dialog */
439 if (priv->contact == NULL) {
440 megaphone_applet_show_preferences (applet);
444 DEBUG ("Requesting text channel for contact %s (%d)",
445 empathy_contact_get_id (priv->contact),
446 empathy_contact_get_handle (priv->contact));
448 mc = empathy_mission_control_new ();
449 mission_control_request_channel (mc,
450 empathy_contact_get_account (priv->contact),
451 TP_IFACE_CHANNEL_TYPE_TEXT,
452 empathy_contact_get_handle (priv->contact),
453 TP_HANDLE_TYPE_CONTACT,
461 megaphone_applet_size_allocate_cb (GtkWidget *widget,
462 GtkAllocation *allocation,
463 MegaphoneApplet *applet)
465 MegaphoneAppletPriv *priv = GET_PRIV (applet);
467 PanelAppletOrient orient;
469 orient = panel_applet_get_orient (PANEL_APPLET (widget));
470 if (orient == PANEL_APPLET_ORIENT_LEFT ||
471 orient == PANEL_APPLET_ORIENT_RIGHT) {
472 size = allocation->width;
474 size = allocation->height;
477 if (size != priv->image_size) {
478 priv->image_size = size;
479 megaphone_applet_update_icon (applet);
484 megaphone_applet_gconf_notify_cb (GConfClient *client,
487 MegaphoneApplet *applet)
492 key = gconf_entry_get_key (entry);
493 value = gconf_entry_get_value (entry);
494 DEBUG ("GConf notification for key '%s'", key);
496 if (value && g_str_has_suffix (key, "/contact_id")) {
497 megaphone_applet_set_contact (applet,
498 gconf_value_get_string (value));
503 megaphone_applet_factory (PanelApplet *applet,
507 MegaphoneAppletPriv *priv = GET_PRIV (applet);
511 /* Ensure it's us! */
512 if (strcmp (iid, "OAFIID:GNOME_Megaphone_Applet") != 0) {
516 DEBUG ("Starting up new instance!");
518 /* Set up the right-click menu */
519 panel_applet_setup_menu_from_file (applet,
521 "GNOME_Megaphone_Applet.xml",
523 megaphone_applet_menu_verbs,
526 /* Define the schema to be used for each applet instance's preferences */
527 panel_applet_add_preferences (applet,
528 "/schemas/apps/megaphone-applet/prefs",
531 /* We watch the preferences directory */
532 pref_dir = panel_applet_gconf_get_full_key (applet, "");
533 pref_dir[strlen (pref_dir)-1] = '\0';
534 gconf_client_add_dir (priv->gconf, pref_dir,
535 GCONF_CLIENT_PRELOAD_ONELEVEL,
537 gconf_client_notify_add (priv->gconf, pref_dir,
538 (GConfClientNotifyFunc) megaphone_applet_gconf_notify_cb,
543 /* Initial setup with the pre-existing gconf key, or contact_id=NULL if no previous pref */
544 contact_id = panel_applet_gconf_get_string (PANEL_APPLET (applet), "contact_id", NULL);
545 megaphone_applet_set_contact (MEGAPHONE_APPLET (applet), contact_id);
549 gtk_widget_show (GTK_WIDGET (applet));
554 PANEL_APPLET_BONOBO_FACTORY ("OAFIID:GNOME_Megaphone_Applet_Factory",
555 MEGAPHONE_TYPE_APPLET,
556 "Megaphone", PACKAGE_VERSION,
557 megaphone_applet_factory,