]> git.0d.be Git - empathy.git/blob - src/empathy-status-icon.c
214e31e16f4f95db3d53d41975f89023f46c1a42
[empathy.git] / src / empathy-status-icon.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  * 
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <gtk/gtk.h>
27 #include <glade/glade.h>
28 #include <gdk/gdkkeysyms.h>
29
30 #include <libnotify/notification.h>
31
32 #include <libempathy/empathy-utils.h>
33 #include <libempathy/empathy-idle.h>
34 #include <libempathy/empathy-account-manager.h>
35
36 #include <libempathy-gtk/empathy-presence-chooser.h>
37 #include <libempathy-gtk/empathy-conf.h>
38 #include <libempathy-gtk/empathy-ui-utils.h>
39 #include <libempathy-gtk/empathy-images.h>
40 #include <libempathy-gtk/empathy-new-message-dialog.h>
41
42 #include "empathy-accounts-dialog.h"
43 #include "empathy-status-icon.h"
44 #include "empathy-preferences.h"
45 #include "empathy-event-manager.h"
46 #include "empathy-misc.h"
47
48 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
49 #include <libempathy/empathy-debug.h>
50
51 /* Number of ms to wait when blinking */
52 #define BLINK_TIMEOUT 500
53
54 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyStatusIcon)
55 typedef struct {
56         GtkStatusIcon       *icon;
57         EmpathyIdle         *idle;
58         EmpathyAccountManager *account_manager;
59         gboolean             showing_event_icon;
60         guint                blink_timeout;
61         EmpathyEventManager *event_manager;
62         EmpathyEvent        *event;
63         NotifyNotification  *notification;
64
65         GtkWindow           *window;
66         GtkWidget           *popup_menu;
67         GtkWidget           *show_window_item;
68         GtkWidget           *message_item;
69         GtkWidget           *status_item;
70 } EmpathyStatusIconPriv;
71
72 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
73
74 static gboolean
75 activate_event (EmpathyEvent *event)
76 {
77         empathy_event_activate (event);
78
79         return FALSE;
80 }
81
82 static void
83 status_icon_notification_closed_cb (NotifyNotification *notification,
84                                     EmpathyStatusIcon  *icon)
85 {
86         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
87         EmpathyNotificationClosedReason reason = 0;
88
89 #ifdef notify_notification_get_closed_reason
90         reason = notify_notification_get_closed_reason (notification);
91 #endif
92         if (priv->notification) {
93                 g_object_unref (priv->notification);
94                 priv->notification = NULL;
95         }
96
97         if (!priv->event) {
98                 return;
99         }
100
101         /* the notification has been closed by the user, see the
102          * DesktopNotification spec.
103          */
104         if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
105                 /* use an idle here, as this callback is called from a
106                  * DBus signal handler inside libnotify, and we might call
107                  * a *_run_* method when activating the event.
108                  */
109                 g_idle_add ((GSourceFunc) activate_event, priv->event);
110         } else {
111                 /* inhibit other updates for this event */
112                 empathy_event_inhibit_updates (priv->event);
113         }
114 }
115
116 static void
117 notification_close_helper (EmpathyStatusIconPriv *priv)
118 {
119         if (priv->notification) {
120                 notify_notification_close (priv->notification, NULL);
121                 g_object_unref (priv->notification);
122                 priv->notification = NULL;
123         }
124 }
125
126 static void
127 status_icon_update_notification (EmpathyStatusIcon *icon)
128 {
129         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
130         GdkPixbuf *pixbuf = NULL;
131
132         if (!empathy_notification_is_enabled ()) {
133                 /* always close the notification if this happens */
134                 notification_close_helper (priv);
135                 return;
136         }
137
138         if (priv->event) {
139                 pixbuf = empathy_misc_get_pixbuf_for_notification (priv->event->contact,
140                                                                    priv->event->icon_name);
141
142                 if (priv->notification) {
143                         notify_notification_update (priv->notification,
144                                                     priv->event->header, priv->event->message,
145                                                     NULL);
146                 } else {
147                         priv->notification = notify_notification_new_with_status_icon
148                                 (priv->event->header, priv->event->message, NULL, priv->icon);
149                         notify_notification_set_timeout (priv->notification,
150                                                          NOTIFY_EXPIRES_DEFAULT);
151
152                         g_signal_connect (priv->notification, "closed",
153                                           G_CALLBACK (status_icon_notification_closed_cb), icon);
154
155                 }
156                 notify_notification_set_icon_from_pixbuf (priv->notification,
157                                                           pixbuf);
158                 notify_notification_show (priv->notification, NULL);
159
160                 g_object_unref (pixbuf);
161         } else {
162                 notification_close_helper (priv);
163         }
164 }
165
166 static void
167 status_icon_update_tooltip (EmpathyStatusIcon *icon)
168 {
169         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
170         gchar                 *tooltip = NULL;
171
172         if (priv->event) {
173                 if (priv->event->message != NULL)
174                                 tooltip = g_strdup_printf ("<i>%s</i>\n%s",
175                                                            priv->event->header,
176                                                            priv->event->message);
177                 else
178                                 tooltip = g_strdup_printf ("<i>%s</i>",
179                                                            priv->event->header);
180                 gtk_status_icon_set_tooltip_markup (priv->icon, tooltip);
181         } else {
182                 tooltip = g_strdup (empathy_idle_get_status (priv->idle));
183                 gtk_status_icon_set_tooltip_text (priv->icon, tooltip);
184         }
185
186         g_free (tooltip);
187 }
188
189 static void
190 status_icon_update_icon (EmpathyStatusIcon *icon)
191 {
192         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
193         const gchar           *icon_name;
194
195         if (priv->event && priv->showing_event_icon) {
196                 icon_name = priv->event->icon_name;
197         } else {
198                 McPresence state;
199
200                 state = empathy_idle_get_state (priv->idle);
201                 icon_name = empathy_icon_name_for_presence (state);
202         }
203
204         gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
205 }
206
207 static gboolean
208 status_icon_blink_timeout_cb (EmpathyStatusIcon *icon)
209 {
210         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
211
212         priv->showing_event_icon = !priv->showing_event_icon;
213         status_icon_update_icon (icon);
214
215         return TRUE;
216 }
217 static void
218 status_icon_event_added_cb (EmpathyEventManager *manager,
219                             EmpathyEvent        *event,
220                             EmpathyStatusIcon   *icon)
221 {
222         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
223
224         if (priv->event) {
225                 return;
226         }
227
228         DEBUG ("New event %p", event);
229
230         priv->event = event;
231         priv->showing_event_icon = TRUE;
232
233         status_icon_update_icon (icon);
234         status_icon_update_tooltip (icon);
235         status_icon_update_notification (icon);
236
237         if (!priv->blink_timeout) {
238                 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
239                                                      (GSourceFunc) status_icon_blink_timeout_cb,
240                                                      icon);
241         }
242 }
243
244 static void
245 status_icon_event_removed_cb (EmpathyEventManager *manager,
246                               EmpathyEvent        *event,
247                               EmpathyStatusIcon   *icon)
248 {
249         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
250
251         if (event != priv->event) {
252                 return;
253         }
254
255         priv->event = empathy_event_manager_get_top_event (priv->event_manager);
256
257         status_icon_update_tooltip (icon);
258         status_icon_update_icon (icon);
259
260         /* update notification anyway, as it's safe and we might have been
261          * changed presence in the meanwhile
262          */     
263         status_icon_update_notification (icon);
264
265         if (!priv->event && priv->blink_timeout) {
266                 g_source_remove (priv->blink_timeout);
267                 priv->blink_timeout = 0;
268         }
269 }
270
271 static void
272 status_icon_event_updated_cb (EmpathyEventManager *manager,
273                               EmpathyEvent        *event,
274                               EmpathyStatusIcon   *icon)
275 {
276         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
277
278         if (event != priv->event) {
279                 return;
280         }
281
282         if (empathy_notification_is_enabled ()) {
283                 status_icon_update_notification (icon);
284         }
285
286         status_icon_update_tooltip (icon);
287 }
288
289 static void
290 status_icon_set_visibility (EmpathyStatusIcon *icon,
291                             gboolean           visible,
292                             gboolean           store)
293 {
294         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
295
296         if (store) {
297                 empathy_conf_set_bool (empathy_conf_get (),
298                                        EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, !visible);
299         }
300
301         if (!visible) {
302                 empathy_window_iconify (priv->window, priv->icon);
303         } else {
304                 GList *accounts;
305
306                 empathy_window_present (GTK_WINDOW (priv->window), TRUE);
307
308                 /* Show the accounts dialog if there is no enabled accounts */
309                 accounts = mc_accounts_list_by_enabled (TRUE);
310                 if (accounts) {
311                         mc_accounts_list_free (accounts);
312                 } else {
313                         DEBUG ("No enabled account, Showing account dialog");
314                         empathy_accounts_dialog_show (GTK_WINDOW (priv->window), NULL);
315                 }
316         }
317 }
318
319 static void
320 status_icon_notify_visibility_cb (EmpathyConf *conf,
321                                   const gchar *key,
322                                   gpointer     user_data)
323 {
324         EmpathyStatusIcon *icon = user_data;
325         gboolean           hidden = FALSE;
326
327         if (empathy_conf_get_bool (conf, key, &hidden)) {
328                 status_icon_set_visibility (icon, !hidden, FALSE);
329         }
330 }
331
332 static void
333 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
334 {
335         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
336         gboolean               visible;
337
338         visible = gtk_window_is_active (priv->window);
339         status_icon_set_visibility (icon, !visible, TRUE);
340 }
341
342 static void
343 status_icon_idle_notify_cb (EmpathyStatusIcon *icon)
344 {
345         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
346
347         status_icon_update_icon (icon);
348         status_icon_update_tooltip (icon);
349
350         if (!empathy_notification_is_enabled ()) {
351                 /* dismiss the outstanding notification if present */
352
353                 if (priv->notification) {
354                         notify_notification_close (priv->notification, NULL);
355                         g_object_unref (priv->notification);
356                         priv->notification = NULL;
357                 }
358         }
359 }
360
361 static gboolean
362 status_icon_delete_event_cb (GtkWidget         *widget,
363                              GdkEvent          *event,
364                              EmpathyStatusIcon *icon)
365 {
366         status_icon_set_visibility (icon, FALSE, TRUE);
367         return TRUE;
368 }
369
370 static gboolean
371 status_icon_key_press_event_cb  (GtkWidget *window,
372                                  GdkEventKey *event,
373                                  EmpathyStatusIcon *icon)
374 {
375         if (event->keyval == GDK_Escape) {
376                 status_icon_set_visibility (icon, FALSE, TRUE);
377         }
378         return FALSE;
379 }
380                                 
381 static void
382 status_icon_activate_cb (GtkStatusIcon     *status_icon,
383                          EmpathyStatusIcon *icon)
384 {
385         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
386
387         DEBUG ("%s", priv->event ? "event" : "toggle");
388
389         if (priv->event) {
390                 empathy_event_activate (priv->event);
391         } else {
392                 status_icon_toggle_visibility (icon);
393         }
394 }
395
396 static void
397 status_icon_show_hide_window_cb (GtkWidget         *widget,
398                                  EmpathyStatusIcon *icon)
399 {
400         gboolean visible;
401
402         visible = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget));
403         status_icon_set_visibility (icon, visible, TRUE);
404 }
405
406 static void
407 status_icon_new_message_cb (GtkWidget         *widget,
408                             EmpathyStatusIcon *icon)
409 {
410         empathy_new_message_dialog_show (NULL);
411 }
412
413 static void
414 status_icon_quit_cb (GtkWidget         *window,
415                      EmpathyStatusIcon *icon)
416 {
417         gtk_main_quit ();
418 }
419
420 static void
421 status_icon_popup_menu_cb (GtkStatusIcon     *status_icon,
422                            guint              button,
423                            guint              activate_time,
424                            EmpathyStatusIcon *icon)
425 {
426         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
427         GtkWidget             *submenu;
428         gboolean               show;
429
430         show = empathy_window_get_is_visible (GTK_WINDOW (priv->window));
431
432         g_signal_handlers_block_by_func (priv->show_window_item,
433                                          status_icon_show_hide_window_cb,
434                                          icon);
435         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (priv->show_window_item),
436                                         show);
437         g_signal_handlers_unblock_by_func (priv->show_window_item,
438                                            status_icon_show_hide_window_cb,
439                                            icon);
440
441         submenu = empathy_presence_chooser_create_menu ();
442         gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->status_item),
443                                    submenu);
444
445         gtk_menu_popup (GTK_MENU (priv->popup_menu),
446                         NULL, NULL,
447                         gtk_status_icon_position_menu,
448                         priv->icon,
449                         button,
450                         activate_time);
451 }
452
453 static void
454 status_icon_create_menu (EmpathyStatusIcon *icon)
455 {
456         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
457         GladeXML              *glade;
458         gchar                 *filename;
459
460         filename = empathy_file_lookup ("empathy-status-icon.glade", "src");
461         glade = empathy_glade_get_file (filename,
462                                        "tray_menu",
463                                        NULL,
464                                        "tray_menu", &priv->popup_menu,
465                                        "tray_show_list", &priv->show_window_item,
466                                        "tray_new_message", &priv->message_item,
467                                        "tray_status", &priv->status_item,
468                                        NULL);
469         g_free (filename);
470
471         empathy_glade_connect (glade,
472                               icon,
473                               "tray_show_list", "toggled", status_icon_show_hide_window_cb,
474                               "tray_new_message", "activate", status_icon_new_message_cb,
475                               "tray_quit", "activate", status_icon_quit_cb,
476                               NULL);
477
478         g_object_unref (glade);
479 }
480
481 static void
482 status_icon_connection_changed_cb (EmpathyAccountManager *manager,
483                                    McAccount *account,
484                                    TpConnectionStatusReason reason,
485                                    TpConnectionStatus current,
486                                    TpConnectionStatus previous,
487                                    EmpathyStatusIcon *icon)
488 {
489         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
490         int connected_accounts;
491
492         /* Check for a connected account */
493         connected_accounts = empathy_account_manager_get_connected_accounts (manager);
494
495         gtk_widget_set_sensitive (priv->message_item, connected_accounts > 0);
496 }
497
498 static void
499 status_icon_finalize (GObject *object)
500 {
501         EmpathyStatusIconPriv *priv = GET_PRIV (object);
502
503         if (priv->blink_timeout) {
504                 g_source_remove (priv->blink_timeout);
505         }
506
507         g_signal_handlers_disconnect_by_func (priv->account_manager,
508                                               status_icon_connection_changed_cb,
509                                               object);
510
511         if (priv->notification) {
512                 notify_notification_close (priv->notification, NULL);
513                 g_object_unref (priv->notification);
514                 priv->notification = NULL;
515         }
516
517         g_object_unref (priv->icon);
518         g_object_unref (priv->idle);
519         g_object_unref (priv->account_manager);
520         g_object_unref (priv->event_manager);
521 }
522
523 static void
524 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
525 {
526         GObjectClass *object_class = G_OBJECT_CLASS (klass);
527
528         object_class->finalize = status_icon_finalize;
529
530         g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
531 }
532
533 static void
534 empathy_status_icon_init (EmpathyStatusIcon *icon)
535 {
536         EmpathyStatusIconPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (icon,
537                 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv);
538
539         icon->priv = priv;
540         priv->icon = gtk_status_icon_new ();
541         priv->account_manager = empathy_account_manager_dup_singleton ();
542         priv->idle = empathy_idle_dup_singleton ();
543         priv->event_manager = empathy_event_manager_dup_singleton ();
544
545         g_signal_connect (priv->account_manager,
546                           "account-connection-changed",
547                           G_CALLBACK (status_icon_connection_changed_cb), icon);
548
549         /* make icon listen and respond to MAIN_WINDOW_HIDDEN changes */
550         empathy_conf_notify_add (empathy_conf_get (),
551                                  EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
552                                  status_icon_notify_visibility_cb,
553                                  icon);
554
555         status_icon_create_menu (icon);
556         status_icon_idle_notify_cb (icon);
557
558         g_signal_connect_swapped (priv->idle, "notify",
559                                   G_CALLBACK (status_icon_idle_notify_cb),
560                                   icon);
561         g_signal_connect (priv->event_manager, "event-added",
562                           G_CALLBACK (status_icon_event_added_cb),
563                           icon);
564         g_signal_connect (priv->event_manager, "event-removed",
565                           G_CALLBACK (status_icon_event_removed_cb),
566                           icon);
567         g_signal_connect (priv->event_manager, "event-updated",
568                           G_CALLBACK (status_icon_event_updated_cb),
569                           icon);
570         g_signal_connect (priv->icon, "activate",
571                           G_CALLBACK (status_icon_activate_cb),
572                           icon);
573         g_signal_connect (priv->icon, "popup-menu",
574                           G_CALLBACK (status_icon_popup_menu_cb),
575                           icon);
576 }
577
578 EmpathyStatusIcon *
579 empathy_status_icon_new (GtkWindow *window, gboolean hide_contact_list)
580 {
581         EmpathyStatusIconPriv *priv;
582         EmpathyStatusIcon     *icon;
583         gboolean               should_hide;
584
585         g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
586
587         icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
588         priv = GET_PRIV (icon);
589
590         priv->window = g_object_ref (window);
591
592         g_signal_connect (priv->window, "key-press-event",
593                           G_CALLBACK (status_icon_key_press_event_cb),
594                           icon);
595
596         g_signal_connect (priv->window, "delete-event",
597                           G_CALLBACK (status_icon_delete_event_cb),
598                           icon);
599
600         if (!hide_contact_list) {
601                 empathy_conf_get_bool (empathy_conf_get (),
602                                        EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
603                                        &should_hide);
604         } else {
605                 should_hide = TRUE;
606         }
607
608         if (gtk_window_is_active (priv->window) == should_hide) {
609                 status_icon_set_visibility (icon, !should_hide, FALSE);
610         }
611
612         return icon;
613 }
614