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-2009 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>
34 #include <libmissioncontrol/mission-control.h>
35 #include <libmissioncontrol/mc-account.h>
37 #include <libempathy/empathy-tp-contact-factory.h>
38 #include <libempathy/empathy-account-manager.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 EmpathyTpContactFactory *factory;
57 EmpathyAccountManager *account_manager;
62 EmpathyContact *contact;
65 } MegaphoneAppletPriv;
67 G_DEFINE_TYPE(MegaphoneApplet, megaphone_applet, PANEL_TYPE_APPLET)
70 megaphone_applet_update_icon (MegaphoneApplet *applet)
72 MegaphoneAppletPriv *priv = GET_PRIV (applet);
73 EmpathyAvatar *avatar = NULL;
74 GdkPixbuf *avatar_pixbuf;
77 avatar = empathy_contact_get_avatar (priv->contact);
79 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
80 GTK_STOCK_PREFERENCES,
88 /* Try to take avatar from cache */
89 avatar_token = panel_applet_gconf_get_string (PANEL_APPLET (applet),
92 if (!EMP_STR_EMPTY (avatar_token)) {
93 empathy_contact_load_avatar_cache (priv->contact, avatar_token);
94 avatar = empathy_contact_get_avatar (priv->contact);
96 g_free (avatar_token);
100 avatar_pixbuf = empathy_pixbuf_from_avatar_scaled (avatar,
101 priv->image_size - 2,
102 priv->image_size - 2);
104 GtkIconTheme *icon_theme;
106 /* Load the default icon when no avatar is found */
107 icon_theme = gtk_icon_theme_get_default ();
108 avatar_pixbuf = gtk_icon_theme_load_icon (icon_theme,
110 priv->image_size - 2,
114 /* Now some desaturation if the contact is offline */
115 if (!empathy_contact_is_online (priv->contact)) {
116 GdkPixbuf *offline_avatar;
118 offline_avatar = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE,
120 gdk_pixbuf_get_height (avatar_pixbuf),
121 gdk_pixbuf_get_width (avatar_pixbuf));
122 gdk_pixbuf_saturate_and_pixelate (avatar_pixbuf,
126 g_object_unref (avatar_pixbuf);
127 avatar_pixbuf = offline_avatar;
130 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), avatar_pixbuf);
131 g_object_unref (avatar_pixbuf);
135 megaphone_applet_update_contact (MegaphoneApplet *applet)
137 MegaphoneAppletPriv *priv = GET_PRIV (applet);
141 const gchar *avatar_token = NULL;
144 EmpathyAvatar *avatar;
146 avatar = empathy_contact_get_avatar (priv->contact);
148 avatar_token = avatar->token;
153 panel_applet_gconf_set_string (PANEL_APPLET (applet),
154 "avatar_token", avatar_token,
158 megaphone_applet_update_icon (applet);
160 if (priv->contact ) {
161 name = empathy_contact_get_name (priv->contact);
162 status = empathy_contact_get_status (priv->contact);
163 tip = g_strdup_printf ("<b>%s</b>: %s", name, status);
164 gtk_widget_set_tooltip_markup (GTK_WIDGET (applet), tip);
167 gtk_widget_set_tooltip_markup (GTK_WIDGET (applet),
168 _("Please configure a contact."));
174 megaphone_applet_got_contact_cb (EmpathyTpContactFactory *factory,
175 EmpathyContact *contact,
180 MegaphoneAppletPriv *priv = GET_PRIV (applet);
183 DEBUG ("Error: %s", error->message);
187 priv->contact = g_object_ref (contact);
188 g_signal_connect_swapped (priv->contact, "notify",
189 G_CALLBACK (megaphone_applet_update_contact),
191 megaphone_applet_update_contact (MEGAPHONE_APPLET (applet));
195 megaphone_applet_new_connection_cb (EmpathyAccountManager *manager,
196 TpConnection *connection,
198 MegaphoneApplet *applet)
200 MegaphoneAppletPriv *priv = GET_PRIV (applet);
202 if (priv->contact || !empathy_account_equal (account, priv->account)) {
206 priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
207 empathy_tp_contact_factory_get_from_id (priv->factory, priv->id,
208 megaphone_applet_got_contact_cb,
209 NULL, NULL, G_OBJECT (applet));
213 megaphone_applet_preferences_response_cb (GtkWidget *dialog,
215 MegaphoneApplet *applet)
217 if (response == GTK_RESPONSE_ACCEPT) {
218 EmpathyContactListView *contact_list;
219 EmpathyContact *contact;
221 /* Retrieve the selected contact, if any and set it up in gconf.
222 * GConf will notify us from the change and we will adjust ourselves */
223 contact_list = g_object_get_data (G_OBJECT (dialog), "contact-list");
224 contact = empathy_contact_list_view_dup_selected (contact_list);
227 const gchar *account_id;
228 const gchar *contact_id;
231 account = empathy_contact_get_account (contact);
232 account_id = mc_account_get_unique_name (account);
233 contact_id = empathy_contact_get_id (contact);
235 str = g_strconcat (account_id, "/", contact_id, NULL);
236 panel_applet_gconf_set_string (PANEL_APPLET (applet),
239 panel_applet_gconf_set_string (PANEL_APPLET (applet),
243 g_object_unref (contact);
246 gtk_widget_destroy (dialog);
250 megaphone_applet_show_preferences (MegaphoneApplet *applet)
254 EmpathyContactListView *contact_list;
255 EmpathyContactListStore *contact_store;
256 EmpathyContactManager *contact_manager;
258 dialog = gtk_dialog_new_with_buttons (_("Select contact..."),
266 /* Show all contacts, even offline and sort alphabetically */
267 contact_manager = empathy_contact_manager_dup_singleton ();
268 contact_store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (contact_manager));
269 g_object_set (contact_store,
271 "show-avatars", TRUE,
272 "show-offline", TRUE,
273 "sort-criterium", EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
275 contact_list = empathy_contact_list_view_new (contact_store,
276 EMPATHY_CONTACT_LIST_FEATURE_NONE,
277 EMPATHY_CONTACT_FEATURE_NONE);
278 g_object_unref (contact_manager);
279 g_object_unref (contact_store);
280 gtk_widget_show (GTK_WIDGET (contact_list));
282 gtk_window_set_default_size (GTK_WINDOW (dialog), 300, 500);
283 scroll = gtk_scrolled_window_new (NULL, NULL);
284 gtk_container_add (GTK_CONTAINER (scroll), GTK_WIDGET (contact_list));
285 gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), scroll);
286 gtk_widget_show (scroll);
288 g_object_set_data (G_OBJECT (dialog), "contact-list", contact_list);
290 g_signal_connect (dialog, "response",
291 G_CALLBACK (megaphone_applet_preferences_response_cb),
294 gtk_widget_show (dialog);
298 megaphone_applet_button_press_event_cb (GtkWidget *widget,
299 GdkEventButton *event,
300 MegaphoneApplet *applet)
302 MegaphoneAppletPriv *priv = GET_PRIV (applet);
305 /* Only react on left-clicks */
306 if (event->button != 1 || event->type != GDK_BUTTON_PRESS) {
310 /* If the contact is unavailable we display the preferences dialog */
311 if (priv->contact == NULL) {
312 megaphone_applet_show_preferences (applet);
316 DEBUG ("Requesting text channel for contact %s (%d)",
317 empathy_contact_get_id (priv->contact),
318 empathy_contact_get_handle (priv->contact));
320 mc = empathy_mission_control_dup_singleton ();
321 mission_control_request_channel (mc,
322 empathy_contact_get_account (priv->contact),
323 TP_IFACE_CHANNEL_TYPE_TEXT,
324 empathy_contact_get_handle (priv->contact),
325 TP_HANDLE_TYPE_CONTACT,
333 megaphone_applet_size_allocate_cb (GtkWidget *widget,
334 GtkAllocation *allocation,
335 MegaphoneApplet *applet)
337 MegaphoneAppletPriv *priv = GET_PRIV (applet);
339 PanelAppletOrient orient;
341 orient = panel_applet_get_orient (PANEL_APPLET (widget));
342 if (orient == PANEL_APPLET_ORIENT_LEFT ||
343 orient == PANEL_APPLET_ORIENT_RIGHT) {
344 size = allocation->width;
346 size = allocation->height;
349 if (size != priv->image_size) {
350 priv->image_size = size;
351 megaphone_applet_update_icon (applet);
356 megaphone_applet_finalize (GObject *object)
358 MegaphoneAppletPriv *priv = GET_PRIV (object);
361 g_object_unref (priv->contact);
364 g_object_unref (priv->factory);
366 if (priv->account_manager) {
367 g_signal_handlers_disconnect_by_func (priv->account_manager,
368 megaphone_applet_new_connection_cb, object);
369 g_object_unref (priv->account_manager);
372 if (priv->gconf_cnxn != 0) {
373 gconf_client_notify_remove (priv->gconf, priv->gconf_cnxn);
375 g_object_unref (priv->gconf);
378 G_OBJECT_CLASS (megaphone_applet_parent_class)->finalize (object);
382 megaphone_applet_class_init (MegaphoneAppletClass *class)
384 GObjectClass *object_class;
386 object_class = G_OBJECT_CLASS (class);
388 object_class->finalize = megaphone_applet_finalize;
390 g_type_class_add_private (object_class, sizeof (MegaphoneAppletPriv));
395 megaphone_applet_init (MegaphoneApplet *applet)
397 MegaphoneAppletPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (applet,
398 MEGAPHONE_TYPE_APPLET, MegaphoneAppletPriv);
401 priv->gconf = gconf_client_get_default ();
402 priv->account_manager = empathy_account_manager_dup_singleton ();
403 g_signal_connect (priv->account_manager, "new-connection",
404 G_CALLBACK (megaphone_applet_new_connection_cb), applet);
406 /* Image holds the contact avatar */
407 priv->image = gtk_image_new ();
408 gtk_widget_show (priv->image);
409 gtk_container_add (GTK_CONTAINER (applet), priv->image);
411 /* We want transparency */
412 panel_applet_set_flags (PANEL_APPLET (applet), PANEL_APPLET_EXPAND_MINOR);
413 panel_applet_set_background_widget (PANEL_APPLET (applet), GTK_WIDGET (applet));
415 /* Listen for clicks on the applet to dispatch a channel */
416 g_signal_connect (applet, "button-press-event",
417 G_CALLBACK (megaphone_applet_button_press_event_cb),
420 /* Allow to resize our avatar when needed */
421 g_signal_connect (applet, "size-allocate",
422 G_CALLBACK (megaphone_applet_size_allocate_cb),
427 megaphone_applet_set_contact (MegaphoneApplet *applet,
430 MegaphoneAppletPriv *priv = GET_PRIV (applet);
431 TpConnection *connection;
434 DEBUG ("Setting new contact %s", str);
436 /* Release old contact, if any */
438 g_signal_handlers_disconnect_by_func (priv->contact,
439 megaphone_applet_update_contact,
441 g_object_unref (priv->contact),
442 priv->contact = NULL;
445 g_object_unref (priv->account),
446 priv->account = NULL;
449 g_object_unref (priv->factory),
450 priv->factory = NULL;
453 /* Lookup the new contact */
455 strv = g_strsplit (str, "/", 2);
456 priv->account = mc_account_lookup (strv[0]);
463 connection = empathy_account_manager_get_connection (
464 priv->account_manager, priv->account);
466 megaphone_applet_new_connection_cb (priv->account_manager,
467 connection, priv->account, applet);
473 megaphone_applet_information_cb (BonoboUIComponent *uic,
474 MegaphoneApplet *applet,
475 const gchar *verb_name)
477 MegaphoneAppletPriv *priv = GET_PRIV (applet);
479 /* FIXME: We should grey out the menu item if there are no available contact */
481 empathy_contact_information_dialog_show (priv->contact, NULL);
486 megaphone_applet_preferences_cb (BonoboUIComponent *uic,
487 MegaphoneApplet *applet,
488 const gchar *verb_name)
490 megaphone_applet_show_preferences (applet);
494 megaphone_applet_gconf_notify_cb (GConfClient *client,
497 MegaphoneApplet *applet)
502 key = gconf_entry_get_key (entry);
503 value = gconf_entry_get_value (entry);
504 DEBUG ("GConf notification for key '%s'", key);
506 if (value && g_str_has_suffix (key, "/contact_id")) {
507 megaphone_applet_set_contact (applet,
508 gconf_value_get_string (value));
513 megaphone_applet_about_cb (BonoboUIComponent *uic,
514 MegaphoneApplet *applet,
515 const gchar *verb_name)
517 const char* authors[] = {
518 "Raphaël Slinckx <raphael@slinckx.net>",
519 "Xavier Claessens <xclaesse@gmail.com>",
523 gtk_show_about_dialog (NULL,
525 "version", PACKAGE_VERSION,
526 "copyright", "Raphaël Slinckx 2007\nCollabora Ltd 2007",
527 "comments", _("Talk!"),
529 "logo-icon-name", "stock_people",
533 static const BonoboUIVerb megaphone_applet_menu_verbs [] = {
534 BONOBO_UI_UNSAFE_VERB ("information", megaphone_applet_information_cb),
535 BONOBO_UI_UNSAFE_VERB ("preferences", megaphone_applet_preferences_cb),
536 BONOBO_UI_UNSAFE_VERB ("about", megaphone_applet_about_cb),
541 megaphone_applet_factory (PanelApplet *applet,
545 MegaphoneAppletPriv *priv = GET_PRIV (applet);
549 /* Ensure it's us! */
550 if (strcmp (iid, "OAFIID:GNOME_Megaphone_Applet") != 0) {
554 DEBUG ("Starting up new instance!");
556 /* Set up the right-click menu */
557 panel_applet_setup_menu_from_file (applet,
559 "GNOME_Megaphone_Applet.xml",
561 megaphone_applet_menu_verbs,
564 /* Define the schema to be used for each applet instance's preferences */
565 panel_applet_add_preferences (applet,
566 "/schemas/apps/megaphone-applet/prefs",
569 /* We watch the preferences directory */
570 pref_dir = panel_applet_gconf_get_full_key (applet, "");
571 pref_dir[strlen (pref_dir)-1] = '\0';
572 gconf_client_add_dir (priv->gconf, pref_dir,
573 GCONF_CLIENT_PRELOAD_ONELEVEL,
575 gconf_client_notify_add (priv->gconf, pref_dir,
576 (GConfClientNotifyFunc) megaphone_applet_gconf_notify_cb,
581 /* Initial setup with the pre-existing gconf key, or contact_id=NULL if no previous pref */
582 contact_id = panel_applet_gconf_get_string (PANEL_APPLET (applet), "contact_id", NULL);
583 megaphone_applet_set_contact (MEGAPHONE_APPLET (applet), contact_id);
587 gtk_widget_show (GTK_WIDGET (applet));
592 PANEL_APPLET_BONOBO_FACTORY ("OAFIID:GNOME_Megaphone_Applet_Factory",
593 MEGAPHONE_TYPE_APPLET,
594 "Megaphone", PACKAGE_VERSION,
595 megaphone_applet_factory,