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