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