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