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