]> git.0d.be Git - empathy.git/blob - megaphone/src/megaphone-applet.c
Fix ref count error for account objects.
[empathy.git] / megaphone / src / megaphone-applet.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* 
3  * Copyright (C) 2007 Raphaël Slinckx <raphael@slinckx.net>
4  * Copyright (C) 2007 Collabora Ltd.
5  *
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.
10  *
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.
15  *
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.
19  *
20  * Authors: Raphaël Slinckx <raphael@slinckx.net>
21  *          Xavier Claessens <xclaesse@gmail.com>
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.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>
34
35 #include <libmissioncontrol/mission-control.h>
36 #include <libmissioncontrol/mc-account.h>
37
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-debug.h>
43 #include <libempathy/empathy-utils.h>
44
45 #include <libempathy-gtk/empathy-contact-list-view.h>
46 #include <libempathy-gtk/empathy-contact-list-store.h>
47 #include <libempathy-gtk/empathy-contact-dialogs.h>
48 #include <libempathy-gtk/empathy-ui-utils.h>
49
50 #include "megaphone-applet.h"
51
52 #define DEBUG_DOMAIN "MegaphoneApplet"
53
54 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), MEGAPHONE_TYPE_APPLET, MegaphoneAppletPriv))
55
56 typedef struct _MegaphoneAppletPriv MegaphoneAppletPriv;
57
58 struct _MegaphoneAppletPriv {
59         EmpathyContactFactory *factory;
60         GtkWidget             *image;
61         gint                   image_size;
62         EmpathyContact        *contact;
63         GConfClient           *gconf;
64         guint                  gconf_cnxn;
65 };
66
67 static void megaphone_applet_finalize                  (GObject            *object);
68 static void megaphone_applet_size_allocate_cb          (GtkWidget          *widget,
69                                                         GtkAllocation      *allocation,
70                                                         MegaphoneApplet    *applet);
71 static gboolean megaphone_applet_button_press_event_cb (GtkWidget          *widget,
72                                                         GdkEventButton     *event, 
73                                                         MegaphoneApplet    *applet);
74 static void megaphone_applet_information_cb            (BonoboUIComponent  *uic,
75                                                         MegaphoneApplet    *applet, 
76                                                         const gchar        *verb_name);
77 static void megaphone_applet_preferences_cb            (BonoboUIComponent  *uic,
78                                                         MegaphoneApplet    *applet, 
79                                                         const gchar        *verb_name);
80 static void megaphone_applet_about_cb                  (BonoboUIComponent  *uic,
81                                                         MegaphoneApplet    *applet, 
82                                                         const gchar        *verb_name);
83
84 G_DEFINE_TYPE(MegaphoneApplet, megaphone_applet, PANEL_TYPE_APPLET)
85
86 static const BonoboUIVerb megaphone_applet_menu_verbs [] = {
87         BONOBO_UI_UNSAFE_VERB ("information", megaphone_applet_information_cb),
88         BONOBO_UI_UNSAFE_VERB ("preferences", megaphone_applet_preferences_cb),
89         BONOBO_UI_UNSAFE_VERB ("about",       megaphone_applet_about_cb),
90         BONOBO_UI_VERB_END
91 };
92
93 static const char* authors[] = {
94         "Raphaël Slinckx <raphael@slinckx.net>", 
95         "Xavier Claessens <xclaesse@gmail.com>", 
96         NULL
97 };
98
99 static void
100 megaphone_applet_class_init (MegaphoneAppletClass *class)
101 {
102         GObjectClass *object_class;
103
104         object_class = G_OBJECT_CLASS (class);
105
106         object_class->finalize = megaphone_applet_finalize;
107
108         g_type_class_add_private (object_class, sizeof (MegaphoneAppletPriv));
109 }
110
111 static void
112 megaphone_applet_init (MegaphoneApplet *applet)
113 {
114         MegaphoneAppletPriv *priv = GET_PRIV (applet);
115
116         priv->factory = empathy_contact_factory_new ();
117         priv->gconf = gconf_client_get_default ();
118
119         /* Image holds the contact avatar */
120         priv->image = gtk_image_new ();
121         gtk_widget_show (priv->image);
122         gtk_container_add (GTK_CONTAINER (applet), priv->image);
123
124         /* We want transparency */
125         panel_applet_set_flags (PANEL_APPLET (applet), PANEL_APPLET_EXPAND_MINOR);
126         panel_applet_set_background_widget (PANEL_APPLET (applet), GTK_WIDGET (applet));
127
128         /* Listen for clicks on the applet to dispatch a channel */
129         g_signal_connect (applet, "button-press-event",
130                           G_CALLBACK (megaphone_applet_button_press_event_cb),
131                           applet);
132
133         /* Allow to resize our avatar when needed */
134         g_signal_connect (applet, "size-allocate",
135                           G_CALLBACK (megaphone_applet_size_allocate_cb),
136                           applet);
137 }
138
139 static void
140 megaphone_applet_finalize (GObject *object)
141 {
142         MegaphoneAppletPriv *priv = GET_PRIV (object);
143         
144         if (priv->contact) {
145                 g_object_unref (priv->contact);
146         }
147         g_object_unref (priv->factory);
148
149         if (priv->gconf_cnxn != 0) {
150                 gconf_client_notify_remove (priv->gconf, priv->gconf_cnxn);
151         }
152         g_object_unref (priv->gconf);
153
154         G_OBJECT_CLASS (megaphone_applet_parent_class)->finalize (object);
155 }
156
157 static gchar *
158 megaphone_applet_get_avatar_file (MegaphoneApplet *applet)
159 {
160         MegaphoneAppletPriv *priv = GET_PRIV (applet);
161         const gchar         *id;
162         gchar               *escaped_avatar;
163         gchar               *avatar_path;
164         gchar               *avatar_file;
165
166         id = empathy_contact_get_id (priv->contact);
167         escaped_avatar = gnome_vfs_escape_slashes (id);
168
169         avatar_path = g_build_filename (g_get_home_dir (),
170                                         ".gnome2",
171                                         PACKAGE_NAME,
172                                         "megaphone",
173                                         NULL);
174         g_mkdir_with_parents (avatar_path, 0700);
175         
176         avatar_file = g_build_filename (avatar_path, escaped_avatar, NULL);
177
178         g_free (avatar_path);
179         g_free (escaped_avatar);
180
181         return avatar_file;
182 }
183
184 static void
185 megaphone_applet_update_avatar (MegaphoneApplet *applet)
186 {
187         MegaphoneAppletPriv *priv = GET_PRIV (applet);
188         GdkPixbuf           *avatar;
189         gchar               *avatar_file;
190
191         if (priv->contact == NULL) {
192                 empathy_debug (DEBUG_DOMAIN, "Update image size: %i",
193                                priv->image_size - 2);
194                 gtk_image_set_pixel_size (GTK_IMAGE (priv->image),
195                                           priv->image_size - 2);
196                 return;
197         }
198
199         empathy_debug (DEBUG_DOMAIN, "Update avatar: %s %i",
200                        empathy_contact_get_id (priv->contact),
201                        priv->image_size - 2);
202
203         /* Compute the avatar cache file name */
204         avatar_file = megaphone_applet_get_avatar_file (applet);
205
206         /* Fetch existing avatar */
207         avatar = empathy_pixbuf_avatar_from_contact_scaled (priv->contact,
208                                                             priv->image_size - 2,
209                                                             priv->image_size - 2);
210         if (avatar == NULL) {
211                 /* Try to load a cached avatar */
212                 avatar = gdk_pixbuf_new_from_file (avatar_file, NULL);
213                 if (avatar == NULL) {
214                         GtkIconTheme *icon_theme;
215
216                         /* Load the default icon when no avatar is found */
217                         icon_theme = gtk_icon_theme_get_default ();
218                         avatar = gtk_icon_theme_load_icon (icon_theme,
219                                                            "stock_contact",
220                                                            priv->image_size - 2,
221                                                            0, NULL);
222                 }
223         } else {
224                 /* Cache avatar */
225                 gdk_pixbuf_save (avatar, avatar_file, "png", NULL, NULL);
226         }
227         g_free (avatar_file);
228
229         /* Now some desaturation if the contact is offline */
230         if (!empathy_contact_is_online (priv->contact)) {
231                 GdkPixbuf *offline_avatar;
232
233                 offline_avatar = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE,
234                                                  8,
235                                                  gdk_pixbuf_get_height (avatar),
236                                                  gdk_pixbuf_get_width (avatar));
237                 gdk_pixbuf_saturate_and_pixelate (avatar,
238                                                   offline_avatar,
239                                                   0.0,
240                                                   TRUE);
241                 g_object_unref (avatar);
242                 avatar = offline_avatar;
243         }
244
245         gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), avatar);
246         g_object_unref (avatar);
247 }
248
249 static void
250 megaphone_applet_update_contact (MegaphoneApplet *applet)
251 {
252         MegaphoneAppletPriv *priv = GET_PRIV (applet);
253         const gchar         *name;
254         const gchar         *status;
255         gchar               *tip;
256
257         if (priv->contact == NULL) {
258                 return;
259         }
260
261         empathy_debug (DEBUG_DOMAIN, "Update contact: %s",
262                        empathy_contact_get_id (priv->contact));
263
264         megaphone_applet_update_avatar (applet);
265
266         name = empathy_contact_get_name (priv->contact);
267         status = empathy_contact_get_status (priv->contact);
268         tip = g_strdup_printf ("<b>%s</b>: %s", name, status);
269
270         gtk_widget_set_tooltip_markup (GTK_WIDGET (applet), tip);
271         g_free (tip);
272 }
273
274 static void
275 megaphone_applet_set_contact (MegaphoneApplet *applet,
276                               const gchar     *str)
277 {
278         MegaphoneAppletPriv *priv = GET_PRIV (applet);
279         McAccount           *account = NULL;
280         gchar              **strv = NULL;
281
282         empathy_debug (DEBUG_DOMAIN, "Setting new contact %s", str);
283
284         /* Release old contact, if any */
285         if (priv->contact) {
286                 g_signal_handlers_disconnect_by_func (priv->contact,
287                                                       megaphone_applet_update_contact,
288                                                       applet);
289                 g_object_unref (priv->contact),
290                 priv->contact = NULL;
291         }
292
293         /* Lookup the new contact */
294         if (str) {
295                 strv = g_strsplit (str, "/", 2);
296                 account = mc_account_lookup (strv[0]);
297         }
298         if (account) {
299                 priv->contact = empathy_contact_factory_get_from_id (priv->factory,
300                                                                      account,
301                                                                      strv[1]);
302                 g_object_unref (account);
303         }
304         g_strfreev (strv);
305
306         /* Take hold of the new contact if any */
307         if (priv->contact) {
308                 /* Listen for updates on the contact, and force a first update */
309                 g_signal_connect_swapped (priv->contact, "notify",
310                                           G_CALLBACK (megaphone_applet_update_contact),
311                                           applet);
312                 megaphone_applet_update_contact (applet);
313         } else {
314                 gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
315                                               GTK_STOCK_PREFERENCES,
316                                               GTK_ICON_SIZE_MENU);
317                 gtk_widget_set_tooltip_markup (GTK_WIDGET (applet),
318                                                "Please configure a contact.");
319         }
320 }
321
322 static void
323 megaphone_applet_preferences_response_cb (GtkWidget       *dialog,
324                                           gint             response,
325                                           MegaphoneApplet *applet) 
326 {
327         if (response == GTK_RESPONSE_ACCEPT) {
328                 EmpathyContactListView *contact_list;
329                 EmpathyContact         *contact;
330
331                 /* Retrieve the selected contact, if any and set it up in gconf.
332                  * GConf will notify us from the change and we will adjust ourselves */
333                 contact_list = g_object_get_data (G_OBJECT (dialog), "contact-list");
334                 contact = empathy_contact_list_view_get_selected (contact_list);
335                 if (contact) {
336                         McAccount   *account;
337                         const gchar *account_id;
338                         const gchar *contact_id;
339                         gchar       *str;
340
341                         account = empathy_contact_get_account (contact);
342                         account_id = mc_account_get_unique_name (account);
343                         contact_id = empathy_contact_get_id (contact);
344
345                         str = g_strconcat (account_id, "/", contact_id, NULL);
346                         panel_applet_gconf_set_string (PANEL_APPLET (applet),
347                                                        "contact_id", str,
348                                                        NULL);
349                         g_free (str);
350                 }
351         }
352         gtk_widget_destroy (dialog);
353 }
354
355 static void
356 megaphone_applet_show_preferences (MegaphoneApplet *applet)
357 {
358         GtkWidget               *dialog;
359         GtkWidget               *scroll;
360         EmpathyContactListView  *contact_list;
361         EmpathyContactListStore *contact_store;
362         EmpathyContactManager   *contact_manager;
363
364         dialog = gtk_dialog_new_with_buttons ("Select contact...",
365                                               NULL, 0,
366                                               GTK_STOCK_CANCEL,
367                                               GTK_RESPONSE_REJECT,
368                                               GTK_STOCK_OK,
369                                               GTK_RESPONSE_ACCEPT,
370                                               NULL);
371
372         /* Show all contacts, even offline and sort alphabetically */
373         contact_manager = empathy_contact_manager_new ();
374         contact_store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (contact_manager));
375         g_object_set (contact_store,
376                       "is-compact", TRUE,
377                       "show-avatars", TRUE,
378                       "show-offline", TRUE,
379                       "sort-criterium", EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
380                       NULL);
381         contact_list = empathy_contact_list_view_new (contact_store);
382         gtk_tree_view_expand_all (GTK_TREE_VIEW (contact_list));
383         g_object_unref (contact_manager);
384         g_object_unref (contact_store);
385         gtk_widget_show (GTK_WIDGET (contact_list));
386
387         gtk_window_set_default_size (GTK_WINDOW (dialog), 300, 500);
388         scroll = gtk_scrolled_window_new (NULL, NULL);
389         gtk_container_add (GTK_CONTAINER (scroll), GTK_WIDGET (contact_list));
390         gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), scroll);
391         gtk_widget_show (scroll);
392         
393         g_object_set_data (G_OBJECT (dialog), "contact-list", contact_list);
394
395         g_signal_connect (dialog, "response",
396                           G_CALLBACK (megaphone_applet_preferences_response_cb),
397                           applet);
398
399         gtk_widget_show (dialog);
400 }
401
402 static void
403 megaphone_applet_information_cb (BonoboUIComponent *uic,
404                                  MegaphoneApplet   *applet,
405                                  const gchar       *verb_name)
406 {
407         MegaphoneAppletPriv *priv = GET_PRIV (applet);
408
409         /* FIXME: We should grey out the menu item if there are no available contact */
410         if (priv->contact) {
411                 empathy_contact_information_dialog_show (priv->contact, NULL, FALSE);
412         }
413 }
414
415 static void
416 megaphone_applet_preferences_cb (BonoboUIComponent *uic, 
417                                  MegaphoneApplet   *applet, 
418                                  const gchar       *verb_name)
419 {
420         megaphone_applet_show_preferences (applet);
421 }
422
423 static void
424 megaphone_applet_about_cb (BonoboUIComponent *uic, 
425                            MegaphoneApplet   *applet, 
426                            const gchar       *verb_name)
427 {
428         gtk_show_about_dialog (NULL,
429                                "name", "Megaphone", 
430                                "version", PACKAGE_VERSION,
431                                "copyright", "Raphaël Slinckx 2007\nCollabora Ltd 2007",
432                                "comments", _("Talk!"),
433                                "authors", authors,
434                                "logo-icon-name", "stock_people",
435                                NULL);
436 }
437
438 static gboolean
439 megaphone_applet_button_press_event_cb (GtkWidget       *widget,
440                                         GdkEventButton  *event, 
441                                         MegaphoneApplet *applet)
442 {
443         MegaphoneAppletPriv *priv = GET_PRIV (applet);
444         MissionControl      *mc;
445
446         /* Only react on left-clicks */
447         if (event->button != 1 || event->type != GDK_BUTTON_PRESS) {
448                 return FALSE;
449         }
450
451         /* If the contact is unavailable we display the preferences dialog */
452         if (priv->contact == NULL) {
453                 megaphone_applet_show_preferences (applet);
454                 return TRUE;
455         }
456         
457         empathy_debug (DEBUG_DOMAIN, "Requesting text channel for contact %s (%d)",
458                        empathy_contact_get_id (priv->contact),
459                        empathy_contact_get_handle (priv->contact));
460
461         mc = empathy_mission_control_new ();
462         mission_control_request_channel (mc,
463                                          empathy_contact_get_account (priv->contact),
464                                          TP_IFACE_CHANNEL_TYPE_TEXT,
465                                          empathy_contact_get_handle (priv->contact),
466                                          TP_HANDLE_TYPE_CONTACT,
467                                          NULL, NULL);
468         g_object_unref (mc);
469         
470         return TRUE;
471 }
472
473 static void
474 megaphone_applet_size_allocate_cb (GtkWidget       *widget,
475                                    GtkAllocation   *allocation,
476                                    MegaphoneApplet *applet)
477 {
478         MegaphoneAppletPriv *priv = GET_PRIV (applet);
479         gint                 size;
480         PanelAppletOrient    orient;
481
482         orient = panel_applet_get_orient (PANEL_APPLET (widget));
483         if (orient == PANEL_APPLET_ORIENT_LEFT ||
484             orient == PANEL_APPLET_ORIENT_RIGHT) {
485                 size = allocation->width;
486         } else {
487                 size = allocation->height;
488         }
489
490         if (size != priv->image_size) {
491                 priv->image_size = size;
492                 megaphone_applet_update_avatar (applet);
493         }
494 }
495
496 static void
497 megaphone_applet_gconf_notify_cb (GConfClient     *client,
498                                   guint            cnxn_id,
499                                   GConfEntry      *entry,
500                                   MegaphoneApplet *applet)
501 {
502         const gchar *key;
503         GConfValue  *value;
504
505         key = gconf_entry_get_key (entry);
506         value = gconf_entry_get_value (entry);
507         empathy_debug (DEBUG_DOMAIN, "GConf notification for key '%s'", key);
508
509         if (value && g_str_has_suffix (key, "/contact_id")) {
510                 megaphone_applet_set_contact (applet,
511                                               gconf_value_get_string (value));
512         }
513 }
514
515 static gboolean
516 megaphone_applet_factory (PanelApplet *applet, 
517                           const gchar *iid, 
518                           gpointer     data)
519 {
520         MegaphoneAppletPriv *priv = GET_PRIV (applet);
521         gchar               *pref_dir;
522         gchar               *contact_id;
523
524         /* Ensure it's us! */
525         if (strcmp (iid, "OAFIID:GNOME_Megaphone_Applet") != 0) {
526                 return FALSE;
527         }
528         
529         empathy_debug (DEBUG_DOMAIN, "Starting up new instance!");
530
531         /* Set up the right-click menu */
532         panel_applet_setup_menu_from_file (applet,
533                                            PKGDATADIR,
534                                            "GNOME_Megaphone_Applet.xml",
535                                            NULL,
536                                            megaphone_applet_menu_verbs,
537                                            applet);
538
539         /* Define the schema to be used for each applet instance's preferences */
540         panel_applet_add_preferences (applet,
541                                       "/schemas/apps/megaphone-applet/prefs",
542                                       NULL);
543         
544         /* We watch the preferences directory */
545         pref_dir = panel_applet_gconf_get_full_key (applet, "");
546         pref_dir[strlen (pref_dir)-1] = '\0';
547         gconf_client_add_dir (priv->gconf, pref_dir,
548                               GCONF_CLIENT_PRELOAD_ONELEVEL,
549                               NULL);
550         gconf_client_notify_add (priv->gconf, pref_dir,
551                                  (GConfClientNotifyFunc) megaphone_applet_gconf_notify_cb,
552                                  applet,
553                                  NULL, NULL);
554         g_free (pref_dir);
555
556         /* Initial setup with the pre-existing gconf key, or contact_id=NULL if no previous pref */
557         contact_id = panel_applet_gconf_get_string (PANEL_APPLET (applet), "contact_id", NULL);
558         megaphone_applet_set_contact (MEGAPHONE_APPLET (applet), contact_id);
559         g_free (contact_id);
560
561         /* Let's go! */
562         gtk_widget_show (GTK_WIDGET (applet));
563
564         return TRUE;
565 }
566
567 PANEL_APPLET_BONOBO_FACTORY ("OAFIID:GNOME_Megaphone_Applet_Factory",
568                              MEGAPHONE_TYPE_APPLET,
569                              "Megaphone", PACKAGE_VERSION,
570                              megaphone_applet_factory,
571                              NULL);
572