1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
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.
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.
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
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
29 #include <gdk/gdkkeysyms.h>
30 #include <glib/gi18n.h>
32 #include <libnotify/notification.h>
33 #include <libnotify/notify.h>
35 #include <telepathy-glib/account-manager.h>
36 #include <telepathy-glib/util.h>
38 #include <libempathy/empathy-utils.h>
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>
47 #include "empathy-accounts-dialog.h"
48 #include "empathy-status-icon.h"
49 #include "empathy-preferences.h"
50 #include "empathy-event-manager.h"
52 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
53 #include <libempathy/empathy-debug.h>
55 /* Number of ms to wait when blinking */
56 #define BLINK_TIMEOUT 500
58 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyStatusIcon)
61 TpAccountManager *account_manager;
62 EmpathyNotifyManager *notify_mgr;
63 gboolean showing_event_icon;
65 EmpathyEventManager *event_manager;
67 NotifyNotification *notification;
70 GtkUIManager *ui_manager;
71 GtkWidget *popup_menu;
72 GtkAction *show_window_item;
73 GtkAction *new_message_item;
74 GtkAction *status_item;
75 } EmpathyStatusIconPriv;
77 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
80 activate_event (EmpathyEvent *event)
82 empathy_event_activate (event);
88 status_icon_notification_closed_cb (NotifyNotification *notification,
89 EmpathyStatusIcon *icon)
91 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
92 EmpathyNotificationClosedReason reason = 0;
94 #ifdef notify_notification_get_closed_reason
95 reason = notify_notification_get_closed_reason (notification);
97 if (priv->notification) {
98 g_object_unref (priv->notification);
99 priv->notification = NULL;
106 /* the notification has been closed by the user, see the
107 * DesktopNotification spec.
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.
114 g_idle_add ((GSourceFunc) activate_event, priv->event);
116 /* inhibit other updates for this event */
117 empathy_event_inhibit_updates (priv->event);
122 notification_close_helper (EmpathyStatusIconPriv *priv)
124 if (priv->notification) {
125 notify_notification_close (priv->notification, NULL);
126 g_object_unref (priv->notification);
127 priv->notification = NULL;
132 notification_action_cb (NotifyNotification *notification,
134 EmpathyStatusIcon *icon)
136 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
139 empathy_event_activate (priv->event);
143 status_icon_update_notification (EmpathyStatusIcon *icon)
145 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
146 GdkPixbuf *pixbuf = NULL;
148 if (!empathy_notify_manager_notification_is_enabled (priv->notify_mgr)) {
149 /* always close the notification if this happens */
150 notification_close_helper (priv);
155 gchar *message_esc = NULL;
156 gchar *header_esc = NULL;
158 if (priv->event->message != NULL)
159 message_esc = g_markup_escape_text (priv->event->message, -1);
161 if (priv->event->header != NULL)
162 header_esc = g_markup_escape_text (priv->event->header, -1);
164 if (priv->notification) {
165 notify_notification_update (priv->notification,
166 header_esc, message_esc,
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);
174 if (empathy_notify_manager_has_capability (priv->notify_mgr,
175 EMPATHY_NOTIFY_MANAGER_CAP_ACTIONS)) {
176 notify_notification_add_action (priv->notification,
179 (NotifyActionCallback) notification_action_cb,
184 g_signal_connect (priv->notification, "closed",
185 G_CALLBACK (status_icon_notification_closed_cb), icon);
188 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
189 priv->notify_mgr, priv->event->contact,
190 priv->event->icon_name);
192 if (pixbuf != NULL) {
193 notify_notification_set_icon_from_pixbuf (priv->notification,
195 g_object_unref (pixbuf);
198 notify_notification_show (priv->notification, NULL);
200 g_free (message_esc);
203 notification_close_helper (priv);
208 status_icon_update_tooltip (EmpathyStatusIcon *icon)
210 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
211 gchar *tooltip = NULL;
214 if (priv->event->message != NULL)
215 tooltip = g_markup_printf_escaped ("<i>%s</i>\n%s",
217 priv->event->message);
219 tooltip = g_markup_printf_escaped ("<i>%s</i>",
220 priv->event->header);
221 gtk_status_icon_set_tooltip_markup (priv->icon, tooltip);
223 tp_account_manager_get_most_available_presence (
224 priv->account_manager, &tooltip, NULL);
225 gtk_status_icon_set_tooltip_text (priv->icon, tooltip);
232 status_icon_update_icon (EmpathyStatusIcon *icon)
234 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
235 const gchar *icon_name;
237 if (priv->event && priv->showing_event_icon) {
238 icon_name = priv->event->icon_name;
240 TpConnectionPresenceType state;
242 state = tp_account_manager_get_most_available_presence (
243 priv->account_manager, NULL, NULL);
245 /* An unset presence type here doesn't make sense. Force it
247 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
248 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
251 icon_name = empathy_icon_name_for_presence (state);
254 if (icon_name != NULL)
255 gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
259 status_icon_blink_timeout_cb (EmpathyStatusIcon *icon)
261 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
263 priv->showing_event_icon = !priv->showing_event_icon;
264 status_icon_update_icon (icon);
269 status_icon_event_added_cb (EmpathyEventManager *manager,
271 EmpathyStatusIcon *icon)
273 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
279 DEBUG ("New event %p", event);
282 if (event->must_ack) {
283 priv->showing_event_icon = TRUE;
284 status_icon_update_icon (icon);
285 status_icon_update_tooltip (icon);
287 status_icon_update_notification (icon);
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,
297 status_icon_event_removed_cb (EmpathyEventManager *manager,
299 EmpathyStatusIcon *icon)
301 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
303 if (event != priv->event) {
307 priv->event = empathy_event_manager_get_top_event (priv->event_manager);
309 status_icon_update_tooltip (icon);
310 status_icon_update_icon (icon);
312 /* update notification anyway, as it's safe and we might have been
313 * changed presence in the meanwhile
315 status_icon_update_notification (icon);
317 if (!priv->event && priv->blink_timeout) {
318 g_source_remove (priv->blink_timeout);
319 priv->blink_timeout = 0;
324 status_icon_event_updated_cb (EmpathyEventManager *manager,
326 EmpathyStatusIcon *icon)
328 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
330 if (event != priv->event) {
334 if (empathy_notify_manager_notification_is_enabled (priv->notify_mgr)) {
335 status_icon_update_notification (icon);
338 status_icon_update_tooltip (icon);
342 status_icon_set_visibility (EmpathyStatusIcon *icon,
346 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
349 empathy_conf_set_bool (empathy_conf_get (),
350 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, !visible);
354 empathy_window_iconify (priv->window, priv->icon);
356 empathy_window_present (GTK_WINDOW (priv->window), TRUE);
361 status_icon_notify_visibility_cb (EmpathyConf *conf,
365 EmpathyStatusIcon *icon = user_data;
366 gboolean hidden = FALSE;
368 if (empathy_conf_get_bool (conf, key, &hidden)) {
369 status_icon_set_visibility (icon, !hidden, FALSE);
374 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
376 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
379 visible = gtk_window_is_active (priv->window);
380 status_icon_set_visibility (icon, !visible, TRUE);
384 status_icon_presence_changed_cb (EmpathyStatusIcon *icon)
386 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
388 status_icon_update_icon (icon);
389 status_icon_update_tooltip (icon);
391 if (!empathy_notify_manager_notification_is_enabled (priv->notify_mgr)) {
392 /* dismiss the outstanding notification if present */
394 if (priv->notification) {
395 notify_notification_close (priv->notification, NULL);
396 g_object_unref (priv->notification);
397 priv->notification = NULL;
403 status_icon_delete_event_cb (GtkWidget *widget,
405 EmpathyStatusIcon *icon)
407 status_icon_set_visibility (icon, FALSE, TRUE);
412 status_icon_key_press_event_cb (GtkWidget *window,
414 EmpathyStatusIcon *icon)
416 if (event->keyval == GDK_Escape) {
417 status_icon_set_visibility (icon, FALSE, TRUE);
423 status_icon_activate_cb (GtkStatusIcon *status_icon,
424 EmpathyStatusIcon *icon)
426 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
428 DEBUG ("%s", priv->event ? "event" : "toggle");
431 empathy_event_activate (priv->event);
433 status_icon_toggle_visibility (icon);
438 status_icon_show_hide_window_cb (GtkToggleAction *action,
439 EmpathyStatusIcon *icon)
443 visible = gtk_toggle_action_get_active (action);
444 status_icon_set_visibility (icon, visible, TRUE);
448 status_icon_new_message_cb (GtkAction *action,
449 EmpathyStatusIcon *icon)
451 empathy_new_message_dialog_show (NULL);
455 status_icon_quit_cb (GtkAction *action,
456 EmpathyStatusIcon *icon)
462 status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
465 EmpathyStatusIcon *icon)
467 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
468 GtkWidget *menu_item;
472 show = empathy_window_get_is_visible (GTK_WINDOW (priv->window));
474 g_signal_handlers_block_by_func (priv->show_window_item,
475 status_icon_show_hide_window_cb,
477 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (priv->show_window_item),
479 g_signal_handlers_unblock_by_func (priv->show_window_item,
480 status_icon_show_hide_window_cb,
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);
487 gtk_menu_popup (GTK_MENU (priv->popup_menu),
489 gtk_status_icon_position_menu,
496 status_icon_create_menu (EmpathyStatusIcon *icon)
498 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
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,
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,
518 g_object_ref (priv->ui_manager);
519 g_object_unref (gui);
523 status_icon_status_changed_cb (TpAccount *account,
524 TpConnectionStatus current,
525 TpConnectionStatus previous,
526 TpConnectionStatusReason reason,
527 gchar *dbus_error_name,
529 EmpathyStatusIcon *icon)
531 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
533 gtk_action_set_sensitive (priv->new_message_item,
534 empathy_account_manager_get_accounts_connected (NULL));
538 status_icon_finalize (GObject *object)
540 EmpathyStatusIconPriv *priv = GET_PRIV (object);
542 if (priv->blink_timeout) {
543 g_source_remove (priv->blink_timeout);
546 if (priv->notification) {
547 notify_notification_close (priv->notification, NULL);
548 g_object_unref (priv->notification);
549 priv->notification = NULL;
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);
560 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
562 GObjectClass *object_class = G_OBJECT_CLASS (klass);
564 object_class->finalize = status_icon_finalize;
566 g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
570 account_manager_prepared_cb (GObject *source_object,
571 GAsyncResult *result,
575 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
576 EmpathyStatusIcon *icon = user_data;
577 GError *error = NULL;
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);
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),
593 status_icon_presence_changed_cb (icon);
597 empathy_status_icon_init (EmpathyStatusIcon *icon)
599 EmpathyStatusIconPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (icon,
600 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv);
603 priv->icon = gtk_status_icon_new ();
604 priv->account_manager = tp_account_manager_dup ();
605 priv->event_manager = empathy_event_manager_dup_singleton ();
607 tp_account_manager_prepare_async (priv->account_manager, NULL,
608 account_manager_prepared_cb, icon);
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,
616 status_icon_create_menu (icon);
618 g_signal_connect_swapped (priv->account_manager,
619 "most-available-presence-changed",
620 G_CALLBACK (status_icon_presence_changed_cb),
622 g_signal_connect (priv->event_manager, "event-added",
623 G_CALLBACK (status_icon_event_added_cb),
625 g_signal_connect (priv->event_manager, "event-removed",
626 G_CALLBACK (status_icon_event_removed_cb),
628 g_signal_connect (priv->event_manager, "event-updated",
629 G_CALLBACK (status_icon_event_updated_cb),
631 g_signal_connect (priv->icon, "activate",
632 G_CALLBACK (status_icon_activate_cb),
634 g_signal_connect (priv->icon, "popup-menu",
635 G_CALLBACK (status_icon_popup_menu_cb),
638 priv->notify_mgr = empathy_notify_manager_dup_singleton ();
642 empathy_status_icon_new (GtkWindow *window, gboolean hide_contact_list)
644 EmpathyStatusIconPriv *priv;
645 EmpathyStatusIcon *icon;
646 gboolean should_hide;
648 g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
650 icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
651 priv = GET_PRIV (icon);
653 priv->window = g_object_ref (window);
655 g_signal_connect_after (priv->window, "key-press-event",
656 G_CALLBACK (status_icon_key_press_event_cb),
659 g_signal_connect (priv->window, "delete-event",
660 G_CALLBACK (status_icon_delete_event_cb),
663 if (!hide_contact_list) {
664 empathy_conf_get_bool (empathy_conf_get (),
665 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
671 if (gtk_window_is_active (priv->window) == should_hide) {
672 status_icon_set_visibility (icon, !should_hide, FALSE);