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