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