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