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