]> git.0d.be Git - empathy.git/blob - src/empathy-status-icon.c
move empathy-misc to empathy-notify-manager
[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 <glib.h>
27
28 #include <gtk/gtk.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <glib/gi18n.h>
31
32 #include <libnotify/notification.h>
33 #include <libnotify/notify.h>
34
35 #include <telepathy-glib/account-manager.h>
36 #include <telepathy-glib/util.h>
37
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy/empathy-idle.h>
40
41 #include <libempathy-gtk/empathy-presence-chooser.h>
42 #include <libempathy-gtk/empathy-conf.h>
43 #include <libempathy-gtk/empathy-ui-utils.h>
44 #include <libempathy-gtk/empathy-images.h>
45 #include <libempathy-gtk/empathy-new-message-dialog.h>
46 #include <libempathy-gtk/empathy-notify-manager.h>
47
48 #include "empathy-accounts-dialog.h"
49 #include "empathy-status-icon.h"
50 #include "empathy-preferences.h"
51 #include "empathy-event-manager.h"
52
53 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
54 #include <libempathy/empathy-debug.h>
55
56 /* Number of ms to wait when blinking */
57 #define BLINK_TIMEOUT 500
58
59 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyStatusIcon)
60 typedef struct {
61         GtkStatusIcon       *icon;
62         EmpathyIdle         *idle;
63         TpAccountManager    *account_manager;
64         EmpathyNotifyManager *notify_mgr;
65         gboolean             showing_event_icon;
66         guint                blink_timeout;
67         EmpathyEventManager *event_manager;
68         EmpathyEvent        *event;
69         NotifyNotification  *notification;
70
71         GtkWindow           *window;
72         GtkUIManager        *ui_manager;
73         GtkWidget           *popup_menu;
74         GtkAction           *show_window_item;
75         GtkAction           *new_message_item;
76         GtkAction           *status_item;
77 } EmpathyStatusIconPriv;
78
79 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
80
81 static gboolean
82 activate_event (EmpathyEvent *event)
83 {
84         empathy_event_activate (event);
85
86         return FALSE;
87 }
88
89 static void
90 status_icon_notification_closed_cb (NotifyNotification *notification,
91                                     EmpathyStatusIcon  *icon)
92 {
93         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
94         EmpathyNotificationClosedReason reason = 0;
95
96 #ifdef notify_notification_get_closed_reason
97         reason = notify_notification_get_closed_reason (notification);
98 #endif
99         if (priv->notification) {
100                 g_object_unref (priv->notification);
101                 priv->notification = NULL;
102         }
103
104         if (!priv->event) {
105                 return;
106         }
107
108         /* the notification has been closed by the user, see the
109          * DesktopNotification spec.
110          */
111         if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
112                 /* use an idle here, as this callback is called from a
113                  * DBus signal handler inside libnotify, and we might call
114                  * a *_run_* method when activating the event.
115                  */
116                 g_idle_add ((GSourceFunc) activate_event, priv->event);
117         } else {
118                 /* inhibit other updates for this event */
119                 empathy_event_inhibit_updates (priv->event);
120         }
121 }
122
123 static void
124 notification_close_helper (EmpathyStatusIconPriv *priv)
125 {
126         if (priv->notification) {
127                 notify_notification_close (priv->notification, NULL);
128                 g_object_unref (priv->notification);
129                 priv->notification = NULL;
130         }
131 }
132
133 static void
134 notification_action_cb (NotifyNotification *notification,
135                         gchar              *action,
136                         EmpathyStatusIcon  *icon)
137 {
138         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
139
140         if (priv->event)
141                 empathy_event_activate (priv->event);
142 }
143
144 static void
145 status_icon_update_notification (EmpathyStatusIcon *icon)
146 {
147         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
148         GdkPixbuf *pixbuf = NULL;
149
150         if (!empathy_notification_is_enabled ()) {
151                 /* always close the notification if this happens */
152                 notification_close_helper (priv);
153                 return;
154         }
155
156         if (priv->event) {
157                 gchar *message_esc = NULL;
158                 gchar *header_esc = NULL;
159
160                 if (priv->event->message != NULL)
161                         message_esc = g_markup_escape_text (priv->event->message, -1);
162
163                 if (priv->event->header != NULL)
164                         header_esc = g_markup_escape_text (priv->event->header, -1);
165
166                 if (priv->notification) {
167                         notify_notification_update (priv->notification,
168                                                     header_esc, message_esc,
169                                                     NULL);
170                 } else {
171                         priv->notification = notify_notification_new_with_status_icon
172                                 (header_esc, message_esc, NULL, priv->icon);
173                         notify_notification_set_timeout (priv->notification,
174                                                          NOTIFY_EXPIRES_DEFAULT);
175
176                         if (empathy_notify_manager_has_capability (priv->notify_mgr,
177                                    EMPATHY_NOTIFY_MANAGER_CAPA_ACTIONS)) {
178                                 notify_notification_add_action (priv->notification,
179                                         "respond",
180                                         _("Respond"),
181                                         (NotifyActionCallback) notification_action_cb,
182                                         icon,
183                                         NULL);
184                         }
185
186                         g_signal_connect (priv->notification, "closed",
187                                           G_CALLBACK (status_icon_notification_closed_cb), icon);
188                 }
189
190                 pixbuf = empathy_misc_get_pixbuf_for_notification (priv->event->contact,
191                                                                    priv->event->icon_name);
192
193                 if (pixbuf != NULL) {
194                         notify_notification_set_icon_from_pixbuf (priv->notification,
195                                                           pixbuf);
196                         g_object_unref (pixbuf);
197                 }
198
199                 notify_notification_show (priv->notification, NULL);
200
201                 g_free (message_esc);
202                 g_free (header_esc);
203         } else {
204                 notification_close_helper (priv);
205         }
206 }
207
208 static void
209 status_icon_update_tooltip (EmpathyStatusIcon *icon)
210 {
211         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
212         gchar                 *tooltip = NULL;
213
214         if (priv->event) {
215                 if (priv->event->message != NULL)
216                                 tooltip = g_markup_printf_escaped ("<i>%s</i>\n%s",
217                                                                    priv->event->header,
218                                                                    priv->event->message);
219                 else
220                                 tooltip = g_markup_printf_escaped ("<i>%s</i>",
221                                                                    priv->event->header);
222                 gtk_status_icon_set_tooltip_markup (priv->icon, tooltip);
223         } else {
224                 tooltip = g_strdup (empathy_idle_get_status (priv->idle));
225                 gtk_status_icon_set_tooltip_text (priv->icon, tooltip);
226         }
227
228         g_free (tooltip);
229 }
230
231 static void
232 status_icon_update_icon (EmpathyStatusIcon *icon)
233 {
234         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
235         const gchar           *icon_name;
236
237         if (priv->event && priv->showing_event_icon) {
238                 icon_name = priv->event->icon_name;
239         } else {
240                 TpConnectionPresenceType state;
241
242                 state = empathy_idle_get_state (priv->idle);
243
244                 /* An unset presence type here doesn't make sense. Force it
245                  * to be offline. */
246                 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
247                         state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
248                 }
249
250                 icon_name = empathy_icon_name_for_presence (state);
251         }
252
253         if (icon_name != NULL)
254                 gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
255 }
256
257 static gboolean
258 status_icon_blink_timeout_cb (EmpathyStatusIcon *icon)
259 {
260         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
261
262         priv->showing_event_icon = !priv->showing_event_icon;
263         status_icon_update_icon (icon);
264
265         return TRUE;
266 }
267 static void
268 status_icon_event_added_cb (EmpathyEventManager *manager,
269                             EmpathyEvent        *event,
270                             EmpathyStatusIcon   *icon)
271 {
272         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
273
274         if (priv->event) {
275                 return;
276         }
277
278         DEBUG ("New event %p", event);
279
280         priv->event = event;
281         if (event->must_ack) {
282                 priv->showing_event_icon = TRUE;
283                 status_icon_update_icon (icon);
284                 status_icon_update_tooltip (icon);
285         }
286         status_icon_update_notification (icon);
287
288         if (!priv->blink_timeout && priv->showing_event_icon) {
289                 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
290                                                      (GSourceFunc) status_icon_blink_timeout_cb,
291                                                      icon);
292         }
293 }
294
295 static void
296 status_icon_event_removed_cb (EmpathyEventManager *manager,
297                               EmpathyEvent        *event,
298                               EmpathyStatusIcon   *icon)
299 {
300         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
301
302         if (event != priv->event) {
303                 return;
304         }
305
306         priv->event = empathy_event_manager_get_top_event (priv->event_manager);
307
308         status_icon_update_tooltip (icon);
309         status_icon_update_icon (icon);
310
311         /* update notification anyway, as it's safe and we might have been
312          * changed presence in the meanwhile
313          */
314         status_icon_update_notification (icon);
315
316         if (!priv->event && priv->blink_timeout) {
317                 g_source_remove (priv->blink_timeout);
318                 priv->blink_timeout = 0;
319         }
320 }
321
322 static void
323 status_icon_event_updated_cb (EmpathyEventManager *manager,
324                               EmpathyEvent        *event,
325                               EmpathyStatusIcon   *icon)
326 {
327         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
328
329         if (event != priv->event) {
330                 return;
331         }
332
333         if (empathy_notification_is_enabled ()) {
334                 status_icon_update_notification (icon);
335         }
336
337         status_icon_update_tooltip (icon);
338 }
339
340 static void
341 status_icon_set_visibility (EmpathyStatusIcon *icon,
342                             gboolean           visible,
343                             gboolean           store)
344 {
345         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
346
347         if (store) {
348                 empathy_conf_set_bool (empathy_conf_get (),
349                                        EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, !visible);
350         }
351
352         if (!visible) {
353                 empathy_window_iconify (priv->window, priv->icon);
354         } else {
355                 empathy_window_present (GTK_WINDOW (priv->window), TRUE);
356         }
357 }
358
359 static void
360 status_icon_notify_visibility_cb (EmpathyConf *conf,
361                                   const gchar *key,
362                                   gpointer     user_data)
363 {
364         EmpathyStatusIcon *icon = user_data;
365         gboolean           hidden = FALSE;
366
367         if (empathy_conf_get_bool (conf, key, &hidden)) {
368                 status_icon_set_visibility (icon, !hidden, FALSE);
369         }
370 }
371
372 static void
373 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
374 {
375         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
376         gboolean               visible;
377
378         visible = gtk_window_is_active (priv->window);
379         status_icon_set_visibility (icon, !visible, TRUE);
380 }
381
382 static void
383 status_icon_idle_notify_cb (EmpathyStatusIcon *icon)
384 {
385         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
386
387         status_icon_update_icon (icon);
388         status_icon_update_tooltip (icon);
389
390         if (!empathy_notification_is_enabled ()) {
391                 /* dismiss the outstanding notification if present */
392
393                 if (priv->notification) {
394                         notify_notification_close (priv->notification, NULL);
395                         g_object_unref (priv->notification);
396                         priv->notification = NULL;
397                 }
398         }
399 }
400
401 static gboolean
402 status_icon_delete_event_cb (GtkWidget         *widget,
403                              GdkEvent          *event,
404                              EmpathyStatusIcon *icon)
405 {
406         status_icon_set_visibility (icon, FALSE, TRUE);
407         return TRUE;
408 }
409
410 static gboolean
411 status_icon_key_press_event_cb  (GtkWidget *window,
412                                  GdkEventKey *event,
413                                  EmpathyStatusIcon *icon)
414 {
415         if (event->keyval == GDK_Escape) {
416                 status_icon_set_visibility (icon, FALSE, TRUE);
417         }
418         return FALSE;
419 }
420
421 static void
422 status_icon_activate_cb (GtkStatusIcon     *status_icon,
423                          EmpathyStatusIcon *icon)
424 {
425         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
426
427         DEBUG ("%s", priv->event ? "event" : "toggle");
428
429         if (priv->event) {
430                 empathy_event_activate (priv->event);
431         } else {
432                 status_icon_toggle_visibility (icon);
433         }
434 }
435
436 static void
437 status_icon_show_hide_window_cb (GtkToggleAction   *action,
438                                  EmpathyStatusIcon *icon)
439 {
440         gboolean visible;
441
442         visible = gtk_toggle_action_get_active (action);
443         status_icon_set_visibility (icon, visible, TRUE);
444 }
445
446 static void
447 status_icon_new_message_cb (GtkAction         *action,
448                             EmpathyStatusIcon *icon)
449 {
450         empathy_new_message_dialog_show (NULL);
451 }
452
453 static void
454 status_icon_quit_cb (GtkAction         *action,
455                      EmpathyStatusIcon *icon)
456 {
457         gtk_main_quit ();
458 }
459
460 static void
461 status_icon_popup_menu_cb (GtkStatusIcon     *status_icon,
462                            guint              button,
463                            guint              activate_time,
464                            EmpathyStatusIcon *icon)
465 {
466         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
467         GtkWidget             *menu_item;
468         GtkWidget             *submenu;
469         gboolean               show;
470
471         show = empathy_window_get_is_visible (GTK_WINDOW (priv->window));
472
473         g_signal_handlers_block_by_func (priv->show_window_item,
474                                          status_icon_show_hide_window_cb,
475                                          icon);
476         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (priv->show_window_item),
477                                       show);
478         g_signal_handlers_unblock_by_func (priv->show_window_item,
479                                            status_icon_show_hide_window_cb,
480                                            icon);
481
482         menu_item = gtk_ui_manager_get_widget (priv->ui_manager, "/menu/status");
483         submenu = empathy_presence_chooser_create_menu ();
484         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), submenu);
485
486         gtk_menu_popup (GTK_MENU (priv->popup_menu),
487                         NULL, NULL,
488                         gtk_status_icon_position_menu,
489                         priv->icon,
490                         button,
491                         activate_time);
492 }
493
494 static void
495 status_icon_create_menu (EmpathyStatusIcon *icon)
496 {
497         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
498         GtkBuilder            *gui;
499         gchar                 *filename;
500
501         filename = empathy_file_lookup ("empathy-status-icon.ui", "src");
502         gui = empathy_builder_get_file (filename,
503                                         "ui_manager", &priv->ui_manager,
504                                         "menu", &priv->popup_menu,
505                                         "show_list", &priv->show_window_item,
506                                         "new_message", &priv->new_message_item,
507                                         "status", &priv->status_item,
508                                        NULL);
509         g_free (filename);
510
511         empathy_builder_connect (gui, icon,
512                               "show_list", "toggled", status_icon_show_hide_window_cb,
513                               "new_message", "activate", status_icon_new_message_cb,
514                               "quit", "activate", status_icon_quit_cb,
515                               NULL);
516
517         g_object_ref (priv->ui_manager);
518         g_object_unref (gui);
519 }
520
521 static void
522 status_icon_status_changed_cb (TpAccount *account,
523                                TpConnectionStatus current,
524                                TpConnectionStatus previous,
525                                TpConnectionStatusReason reason,
526                                gchar *dbus_error_name,
527                                GHashTable *details,
528                                EmpathyStatusIcon *icon)
529 {
530         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
531
532         gtk_action_set_sensitive (priv->new_message_item,
533                                   empathy_account_manager_get_accounts_connected (NULL));
534 }
535
536 static void
537 status_icon_finalize (GObject *object)
538 {
539         EmpathyStatusIconPriv *priv = GET_PRIV (object);
540
541         if (priv->blink_timeout) {
542                 g_source_remove (priv->blink_timeout);
543         }
544
545         if (priv->notification) {
546                 notify_notification_close (priv->notification, NULL);
547                 g_object_unref (priv->notification);
548                 priv->notification = NULL;
549         }
550
551         g_object_unref (priv->icon);
552         g_object_unref (priv->idle);
553         g_object_unref (priv->account_manager);
554         g_object_unref (priv->event_manager);
555         g_object_unref (priv->ui_manager);
556         g_object_unref (priv->notify_mgr);
557 }
558
559 static void
560 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
561 {
562         GObjectClass *object_class = G_OBJECT_CLASS (klass);
563
564         object_class->finalize = status_icon_finalize;
565
566         g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
567 }
568
569 static void
570 account_manager_prepared_cb (GObject *source_object,
571                              GAsyncResult *result,
572                              gpointer user_data)
573 {
574         GList *list, *l;
575         TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
576         EmpathyStatusIcon *icon = user_data;
577         GError *error = NULL;
578
579         if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
580                 DEBUG ("Failed to prepare account manager: %s", error->message);
581                 g_error_free (error);
582                 return;
583         }
584
585         list = tp_account_manager_get_valid_accounts (account_manager);
586         for (l = list; l != NULL; l = l->next) {
587                 empathy_signal_connect_weak (l->data, "status-changed",
588                                              G_CALLBACK (status_icon_status_changed_cb),
589                                              G_OBJECT (icon));
590         }
591         g_list_free (list);
592 }
593
594 static void
595 empathy_status_icon_init (EmpathyStatusIcon *icon)
596 {
597         EmpathyStatusIconPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (icon,
598                 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv);
599
600         icon->priv = priv;
601         priv->icon = gtk_status_icon_new ();
602         priv->account_manager = tp_account_manager_dup ();
603         priv->idle = empathy_idle_dup_singleton ();
604         priv->event_manager = empathy_event_manager_dup_singleton ();
605
606         tp_account_manager_prepare_async (priv->account_manager, NULL,
607             account_manager_prepared_cb, icon);
608
609         /* make icon listen and respond to MAIN_WINDOW_HIDDEN changes */
610         empathy_conf_notify_add (empathy_conf_get (),
611                                  EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
612                                  status_icon_notify_visibility_cb,
613                                  icon);
614
615         status_icon_create_menu (icon);
616         status_icon_idle_notify_cb (icon);
617
618         g_signal_connect_swapped (priv->idle, "notify",
619                                   G_CALLBACK (status_icon_idle_notify_cb),
620                                   icon);
621         g_signal_connect (priv->event_manager, "event-added",
622                           G_CALLBACK (status_icon_event_added_cb),
623                           icon);
624         g_signal_connect (priv->event_manager, "event-removed",
625                           G_CALLBACK (status_icon_event_removed_cb),
626                           icon);
627         g_signal_connect (priv->event_manager, "event-updated",
628                           G_CALLBACK (status_icon_event_updated_cb),
629                           icon);
630         g_signal_connect (priv->icon, "activate",
631                           G_CALLBACK (status_icon_activate_cb),
632                           icon);
633         g_signal_connect (priv->icon, "popup-menu",
634                           G_CALLBACK (status_icon_popup_menu_cb),
635                           icon);
636
637         /* Query the notification daemon to check if it supports actions */
638         priv->notify_mgr = empathy_notify_manager_dup_singleton ();
639 }
640
641 EmpathyStatusIcon *
642 empathy_status_icon_new (GtkWindow *window, gboolean hide_contact_list)
643 {
644         EmpathyStatusIconPriv *priv;
645         EmpathyStatusIcon     *icon;
646         gboolean               should_hide;
647
648         g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
649
650         icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
651         priv = GET_PRIV (icon);
652
653         priv->window = g_object_ref (window);
654
655         g_signal_connect_after (priv->window, "key-press-event",
656                           G_CALLBACK (status_icon_key_press_event_cb),
657                           icon);
658
659         g_signal_connect (priv->window, "delete-event",
660                           G_CALLBACK (status_icon_delete_event_cb),
661                           icon);
662
663         if (!hide_contact_list) {
664                 empathy_conf_get_bool (empathy_conf_get (),
665                                        EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
666                                        &should_hide);
667         } else {
668                 should_hide = TRUE;
669         }
670
671         if (gtk_window_is_active (priv->window) == should_hide) {
672                 status_icon_set_visibility (icon, !should_hide, FALSE);
673         }
674
675         return icon;
676 }
677