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