]> git.0d.be Git - empathy.git/blob - src/empathy-status-icon.c
Add en_GB in gitignore
[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 ("%s\n%s",
175                                                            priv->event->header,
176                                                            priv->event->message);
177                 else
178                                 tooltip = g_strdup (priv->event->header);
179         }
180
181         if (!tooltip) {
182                 tooltip = g_strdup (empathy_idle_get_status (priv->idle));
183         }
184
185         /* FIXME: when we will depend on GTK+ 2.16.0, we should use
186          * gtk_status_icon_set_tooltip_markup () and make the header italic.
187          */
188         gtk_status_icon_set_tooltip (priv->icon, tooltip);
189
190         g_free (tooltip);
191 }
192
193 static void
194 status_icon_update_icon (EmpathyStatusIcon *icon)
195 {
196         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
197         const gchar           *icon_name;
198
199         if (priv->event && priv->showing_event_icon) {
200                 icon_name = priv->event->icon_name;
201         } else {
202                 McPresence state;
203
204                 state = empathy_idle_get_state (priv->idle);
205                 icon_name = empathy_icon_name_for_presence (state);
206         }
207
208         gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
209 }
210
211 static gboolean
212 status_icon_blink_timeout_cb (EmpathyStatusIcon *icon)
213 {
214         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
215
216         priv->showing_event_icon = !priv->showing_event_icon;
217         status_icon_update_icon (icon);
218
219         return TRUE;
220 }
221 static void
222 status_icon_event_added_cb (EmpathyEventManager *manager,
223                             EmpathyEvent        *event,
224                             EmpathyStatusIcon   *icon)
225 {
226         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
227
228         if (priv->event) {
229                 return;
230         }
231
232         DEBUG ("New event %p", event);
233
234         priv->event = event;
235         priv->showing_event_icon = TRUE;
236
237         status_icon_update_icon (icon);
238         status_icon_update_tooltip (icon);
239         status_icon_update_notification (icon);
240
241         if (!priv->blink_timeout) {
242                 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
243                                                      (GSourceFunc) status_icon_blink_timeout_cb,
244                                                      icon);
245         }
246 }
247
248 static void
249 status_icon_event_removed_cb (EmpathyEventManager *manager,
250                               EmpathyEvent        *event,
251                               EmpathyStatusIcon   *icon)
252 {
253         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
254
255         if (event != priv->event) {
256                 return;
257         }
258
259         priv->event = empathy_event_manager_get_top_event (priv->event_manager);
260
261         status_icon_update_tooltip (icon);
262         status_icon_update_icon (icon);
263
264         /* update notification anyway, as it's safe and we might have been
265          * changed presence in the meanwhile
266          */     
267         status_icon_update_notification (icon);
268
269         if (!priv->event && priv->blink_timeout) {
270                 g_source_remove (priv->blink_timeout);
271                 priv->blink_timeout = 0;
272         }
273 }
274
275 static void
276 status_icon_event_updated_cb (EmpathyEventManager *manager,
277                               EmpathyEvent        *event,
278                               EmpathyStatusIcon   *icon)
279 {
280         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
281
282         if (event != priv->event) {
283                 return;
284         }
285
286         if (empathy_notification_is_enabled ()) {
287                 status_icon_update_notification (icon);
288         }
289
290         status_icon_update_tooltip (icon);
291 }
292
293 static void
294 status_icon_set_visibility (EmpathyStatusIcon *icon,
295                             gboolean           visible,
296                             gboolean           store)
297 {
298         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
299
300         if (store) {
301                 empathy_conf_set_bool (empathy_conf_get (),
302                                        EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, !visible);
303         }
304
305         if (!visible) {
306                 empathy_window_iconify (priv->window, priv->icon);
307         } else {
308                 GList *accounts;
309
310                 empathy_window_present (GTK_WINDOW (priv->window), TRUE);
311
312                 /* Show the accounts dialog if there is no enabled accounts */
313                 accounts = mc_accounts_list_by_enabled (TRUE);
314                 if (accounts) {
315                         mc_accounts_list_free (accounts);
316                 } else {
317                         DEBUG ("No enabled account, Showing account dialog");
318                         empathy_accounts_dialog_show (GTK_WINDOW (priv->window), NULL);
319                 }
320         }
321 }
322
323 static void
324 status_icon_notify_visibility_cb (EmpathyConf *conf,
325                                   const gchar *key,
326                                   gpointer     user_data)
327 {
328         EmpathyStatusIcon *icon = user_data;
329         gboolean           hidden = FALSE;
330
331         if (empathy_conf_get_bool (conf, key, &hidden)) {
332                 status_icon_set_visibility (icon, !hidden, FALSE);
333         }
334 }
335
336 static void
337 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
338 {
339         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
340         gboolean               visible;
341
342         visible = gtk_window_is_active (priv->window);
343         status_icon_set_visibility (icon, !visible, TRUE);
344 }
345
346 static void
347 status_icon_idle_notify_cb (EmpathyStatusIcon *icon)
348 {
349         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
350
351         status_icon_update_icon (icon);
352         status_icon_update_tooltip (icon);
353
354         if (!empathy_notification_is_enabled ()) {
355                 /* dismiss the outstanding notification if present */
356
357                 if (priv->notification) {
358                         notify_notification_close (priv->notification, NULL);
359                         g_object_unref (priv->notification);
360                         priv->notification = NULL;
361                 }
362         }
363 }
364
365 static gboolean
366 status_icon_delete_event_cb (GtkWidget         *widget,
367                              GdkEvent          *event,
368                              EmpathyStatusIcon *icon)
369 {
370         status_icon_set_visibility (icon, FALSE, TRUE);
371         return TRUE;
372 }
373
374 static gboolean
375 status_icon_key_press_event_cb  (GtkWidget *window,
376                                  GdkEventKey *event,
377                                  EmpathyStatusIcon *icon)
378 {
379         if (event->keyval == GDK_Escape) {
380                 status_icon_set_visibility (icon, FALSE, TRUE);
381         }
382         return FALSE;
383 }
384                                 
385 static void
386 status_icon_activate_cb (GtkStatusIcon     *status_icon,
387                          EmpathyStatusIcon *icon)
388 {
389         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
390
391         DEBUG ("%s", priv->event ? "event" : "toggle");
392
393         if (priv->event) {
394                 empathy_event_activate (priv->event);
395         } else {
396                 status_icon_toggle_visibility (icon);
397         }
398 }
399
400 static void
401 status_icon_show_hide_window_cb (GtkWidget         *widget,
402                                  EmpathyStatusIcon *icon)
403 {
404         gboolean visible;
405
406         visible = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget));
407         status_icon_set_visibility (icon, visible, TRUE);
408 }
409
410 static void
411 status_icon_new_message_cb (GtkWidget         *widget,
412                             EmpathyStatusIcon *icon)
413 {
414         empathy_new_message_dialog_show (NULL);
415 }
416
417 static void
418 status_icon_quit_cb (GtkWidget         *window,
419                      EmpathyStatusIcon *icon)
420 {
421         gtk_main_quit ();
422 }
423
424 static void
425 status_icon_popup_menu_cb (GtkStatusIcon     *status_icon,
426                            guint              button,
427                            guint              activate_time,
428                            EmpathyStatusIcon *icon)
429 {
430         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
431         GtkWidget             *submenu;
432         gboolean               show;
433
434         show = empathy_window_get_is_visible (GTK_WINDOW (priv->window));
435
436         g_signal_handlers_block_by_func (priv->show_window_item,
437                                          status_icon_show_hide_window_cb,
438                                          icon);
439         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (priv->show_window_item),
440                                         show);
441         g_signal_handlers_unblock_by_func (priv->show_window_item,
442                                            status_icon_show_hide_window_cb,
443                                            icon);
444
445         submenu = empathy_presence_chooser_create_menu ();
446         gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->status_item),
447                                    submenu);
448
449         gtk_menu_popup (GTK_MENU (priv->popup_menu),
450                         NULL, NULL,
451                         gtk_status_icon_position_menu,
452                         priv->icon,
453                         button,
454                         activate_time);
455 }
456
457 static void
458 status_icon_create_menu (EmpathyStatusIcon *icon)
459 {
460         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
461         GladeXML              *glade;
462         gchar                 *filename;
463
464         filename = empathy_file_lookup ("empathy-status-icon.glade", "src");
465         glade = empathy_glade_get_file (filename,
466                                        "tray_menu",
467                                        NULL,
468                                        "tray_menu", &priv->popup_menu,
469                                        "tray_show_list", &priv->show_window_item,
470                                        "tray_new_message", &priv->message_item,
471                                        "tray_status", &priv->status_item,
472                                        NULL);
473         g_free (filename);
474
475         empathy_glade_connect (glade,
476                               icon,
477                               "tray_show_list", "toggled", status_icon_show_hide_window_cb,
478                               "tray_new_message", "activate", status_icon_new_message_cb,
479                               "tray_quit", "activate", status_icon_quit_cb,
480                               NULL);
481
482         g_object_unref (glade);
483 }
484
485 static void
486 status_icon_connection_changed_cb (EmpathyAccountManager *manager,
487                                    McAccount *account,
488                                    TpConnectionStatusReason reason,
489                                    TpConnectionStatus current,
490                                    TpConnectionStatus previous,
491                                    EmpathyStatusIcon *icon)
492 {
493         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
494         int connected_accounts;
495
496         /* Check for a connected account */
497         connected_accounts = empathy_account_manager_get_connected_accounts (manager);
498
499         gtk_widget_set_sensitive (priv->message_item, connected_accounts > 0);
500 }
501
502 static void
503 status_icon_finalize (GObject *object)
504 {
505         EmpathyStatusIconPriv *priv = GET_PRIV (object);
506
507         if (priv->blink_timeout) {
508                 g_source_remove (priv->blink_timeout);
509         }
510
511         g_signal_handlers_disconnect_by_func (priv->account_manager,
512                                               status_icon_connection_changed_cb,
513                                               object);
514
515         if (priv->notification) {
516                 notify_notification_close (priv->notification, NULL);
517                 g_object_unref (priv->notification);
518                 priv->notification = NULL;
519         }
520
521         g_object_unref (priv->icon);
522         g_object_unref (priv->idle);
523         g_object_unref (priv->account_manager);
524         g_object_unref (priv->event_manager);
525 }
526
527 static void
528 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
529 {
530         GObjectClass *object_class = G_OBJECT_CLASS (klass);
531
532         object_class->finalize = status_icon_finalize;
533
534         g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
535 }
536
537 static void
538 empathy_status_icon_init (EmpathyStatusIcon *icon)
539 {
540         EmpathyStatusIconPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (icon,
541                 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv);
542
543         icon->priv = priv;
544         priv->icon = gtk_status_icon_new ();
545         priv->account_manager = empathy_account_manager_dup_singleton ();
546         priv->idle = empathy_idle_dup_singleton ();
547         priv->event_manager = empathy_event_manager_dup_singleton ();
548
549         g_signal_connect (priv->account_manager,
550                           "account-connection-changed",
551                           G_CALLBACK (status_icon_connection_changed_cb), icon);
552
553         /* make icon listen and respond to MAIN_WINDOW_HIDDEN changes */
554         empathy_conf_notify_add (empathy_conf_get (),
555                                  EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
556                                  status_icon_notify_visibility_cb,
557                                  icon);
558
559         status_icon_create_menu (icon);
560         status_icon_idle_notify_cb (icon);
561
562         g_signal_connect_swapped (priv->idle, "notify",
563                                   G_CALLBACK (status_icon_idle_notify_cb),
564                                   icon);
565         g_signal_connect (priv->event_manager, "event-added",
566                           G_CALLBACK (status_icon_event_added_cb),
567                           icon);
568         g_signal_connect (priv->event_manager, "event-removed",
569                           G_CALLBACK (status_icon_event_removed_cb),
570                           icon);
571         g_signal_connect (priv->event_manager, "event-updated",
572                           G_CALLBACK (status_icon_event_updated_cb),
573                           icon);
574         g_signal_connect (priv->icon, "activate",
575                           G_CALLBACK (status_icon_activate_cb),
576                           icon);
577         g_signal_connect (priv->icon, "popup-menu",
578                           G_CALLBACK (status_icon_popup_menu_cb),
579                           icon);
580 }
581
582 EmpathyStatusIcon *
583 empathy_status_icon_new (GtkWindow *window, gboolean hide_contact_list)
584 {
585         EmpathyStatusIconPriv *priv;
586         EmpathyStatusIcon     *icon;
587         gboolean               should_hide;
588
589         g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
590
591         icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
592         priv = GET_PRIV (icon);
593
594         priv->window = g_object_ref (window);
595
596         g_signal_connect (priv->window, "key-press-event",
597                           G_CALLBACK (status_icon_key_press_event_cb),
598                           icon);
599
600         g_signal_connect (priv->window, "delete-event",
601                           G_CALLBACK (status_icon_delete_event_cb),
602                           icon);
603
604         if (!hide_contact_list) {
605                 empathy_conf_get_bool (empathy_conf_get (),
606                                        EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
607                                        &should_hide);
608         } else {
609                 should_hide = TRUE;
610         }
611
612         if (gtk_window_is_active (priv->window) == should_hide) {
613                 status_icon_set_visibility (icon, !should_hide, FALSE);
614         }
615
616         return icon;
617 }
618