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-new-call-dialog.h>
46 #include <libempathy-gtk/empathy-notify-manager.h>
48 #include "empathy-accounts-dialog.h"
49 #include "empathy-status-icon.h"
50 #include "empathy-preferences.h"
51 #include "empathy-event-manager.h"
53 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
54 #include <libempathy/empathy-debug.h>
56 /* Number of ms to wait when blinking */
57 #define BLINK_TIMEOUT 500
59 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyStatusIcon)
62 TpAccountManager *account_manager;
63 EmpathyNotifyManager *notify_mgr;
64 gboolean showing_event_icon;
66 EmpathyEventManager *event_manager;
68 NotifyNotification *notification;
71 GtkUIManager *ui_manager;
72 GtkWidget *popup_menu;
73 GtkAction *show_window_item;
74 GtkAction *new_message_item;
75 GtkAction *status_item;
76 } EmpathyStatusIconPriv;
78 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
81 activate_event (EmpathyEvent *event)
83 empathy_event_activate (event);
89 status_icon_notification_closed_cb (NotifyNotification *notification,
90 EmpathyStatusIcon *icon)
92 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
93 EmpathyNotificationClosedReason reason = 0;
95 #ifdef notify_notification_get_closed_reason
96 reason = notify_notification_get_closed_reason (notification);
98 if (priv->notification) {
99 g_object_unref (priv->notification);
100 priv->notification = NULL;
107 /* the notification has been closed by the user, see the
108 * DesktopNotification spec.
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.
115 g_idle_add ((GSourceFunc) activate_event, priv->event);
117 /* inhibit other updates for this event */
118 empathy_event_inhibit_updates (priv->event);
123 notification_close_helper (EmpathyStatusIconPriv *priv)
125 if (priv->notification) {
126 notify_notification_close (priv->notification, NULL);
127 g_object_unref (priv->notification);
128 priv->notification = NULL;
133 notification_action_cb (NotifyNotification *notification,
135 EmpathyStatusIcon *icon)
137 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
140 empathy_event_activate (priv->event);
144 status_icon_update_notification (EmpathyStatusIcon *icon)
146 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
147 GdkPixbuf *pixbuf = NULL;
149 if (!empathy_notify_manager_notification_is_enabled (priv->notify_mgr)) {
150 /* always close the notification if this happens */
151 notification_close_helper (priv);
156 gchar *message_esc = NULL;
158 if (priv->event->message != NULL)
159 message_esc = g_markup_escape_text (priv->event->message, -1);
161 if (priv->notification) {
162 notify_notification_update (priv->notification,
163 priv->event->header, message_esc,
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);
171 if (empathy_notify_manager_has_capability (priv->notify_mgr,
172 EMPATHY_NOTIFY_MANAGER_CAP_ACTIONS)) {
173 notify_notification_add_action (priv->notification,
176 (NotifyActionCallback) notification_action_cb,
181 g_signal_connect (priv->notification, "closed",
182 G_CALLBACK (status_icon_notification_closed_cb), icon);
185 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
186 priv->notify_mgr, priv->event->contact,
187 priv->event->icon_name);
189 if (pixbuf != NULL) {
190 notify_notification_set_icon_from_pixbuf (priv->notification,
192 g_object_unref (pixbuf);
195 notify_notification_show (priv->notification, NULL);
197 g_free (message_esc);
199 notification_close_helper (priv);
204 status_icon_update_tooltip (EmpathyStatusIcon *icon)
206 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
209 gchar *tooltip = NULL;
211 if (priv->event->message != NULL)
212 tooltip = g_markup_printf_escaped ("<i>%s</i>\n%s",
214 priv->event->message);
216 tooltip = g_markup_printf_escaped ("<i>%s</i>",
217 priv->event->header);
218 gtk_status_icon_set_tooltip_markup (priv->icon, tooltip);
221 TpConnectionPresenceType type;
224 type = tp_account_manager_get_most_available_presence (
225 priv->account_manager, NULL, &msg);
227 if (!EMP_STR_EMPTY (msg)) {
228 gtk_status_icon_set_tooltip_text (priv->icon, msg);
231 gtk_status_icon_set_tooltip_text (priv->icon,
232 empathy_presence_get_default_message (type));
240 status_icon_update_icon (EmpathyStatusIcon *icon)
242 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
243 const gchar *icon_name;
245 if (priv->event && priv->showing_event_icon) {
246 icon_name = priv->event->icon_name;
248 TpConnectionPresenceType state;
250 state = tp_account_manager_get_most_available_presence (
251 priv->account_manager, NULL, NULL);
253 /* An unset presence type here doesn't make sense. Force it
255 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
256 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
259 icon_name = empathy_icon_name_for_presence (state);
262 if (icon_name != NULL)
263 gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
267 status_icon_blink_timeout_cb (EmpathyStatusIcon *icon)
269 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
271 priv->showing_event_icon = !priv->showing_event_icon;
272 status_icon_update_icon (icon);
277 status_icon_event_added_cb (EmpathyEventManager *manager,
279 EmpathyStatusIcon *icon)
281 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
287 DEBUG ("New event %p", event);
290 if (event->must_ack) {
291 priv->showing_event_icon = TRUE;
292 status_icon_update_icon (icon);
293 status_icon_update_tooltip (icon);
295 status_icon_update_notification (icon);
297 if (!priv->blink_timeout && priv->showing_event_icon) {
298 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
299 (GSourceFunc) status_icon_blink_timeout_cb,
305 status_icon_event_removed_cb (EmpathyEventManager *manager,
307 EmpathyStatusIcon *icon)
309 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
311 if (event != priv->event) {
315 priv->event = empathy_event_manager_get_top_event (priv->event_manager);
317 status_icon_update_tooltip (icon);
318 status_icon_update_icon (icon);
320 /* update notification anyway, as it's safe and we might have been
321 * changed presence in the meanwhile
323 status_icon_update_notification (icon);
325 if (!priv->event && priv->blink_timeout) {
326 g_source_remove (priv->blink_timeout);
327 priv->blink_timeout = 0;
332 status_icon_event_updated_cb (EmpathyEventManager *manager,
334 EmpathyStatusIcon *icon)
336 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
338 if (event != priv->event) {
342 if (empathy_notify_manager_notification_is_enabled (priv->notify_mgr)) {
343 status_icon_update_notification (icon);
346 status_icon_update_tooltip (icon);
350 status_icon_set_visibility (EmpathyStatusIcon *icon,
354 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
357 empathy_conf_set_bool (empathy_conf_get (),
358 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, !visible);
362 empathy_window_iconify (priv->window, priv->icon);
364 empathy_window_present (GTK_WINDOW (priv->window), TRUE);
369 status_icon_notify_visibility_cb (EmpathyConf *conf,
373 EmpathyStatusIcon *icon = user_data;
374 gboolean hidden = FALSE;
376 if (empathy_conf_get_bool (conf, key, &hidden)) {
377 status_icon_set_visibility (icon, !hidden, FALSE);
382 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
384 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
387 visible = gtk_window_is_active (priv->window);
388 status_icon_set_visibility (icon, !visible, TRUE);
392 status_icon_presence_changed_cb (EmpathyStatusIcon *icon)
394 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
396 status_icon_update_icon (icon);
397 status_icon_update_tooltip (icon);
399 if (!empathy_notify_manager_notification_is_enabled (priv->notify_mgr)) {
400 /* dismiss the outstanding notification if present */
402 if (priv->notification) {
403 notify_notification_close (priv->notification, NULL);
404 g_object_unref (priv->notification);
405 priv->notification = NULL;
411 status_icon_delete_event_cb (GtkWidget *widget,
413 EmpathyStatusIcon *icon)
415 status_icon_set_visibility (icon, FALSE, TRUE);
420 status_icon_key_press_event_cb (GtkWidget *window,
422 EmpathyStatusIcon *icon)
424 if (event->keyval == GDK_Escape) {
425 status_icon_set_visibility (icon, FALSE, TRUE);
431 status_icon_activate_cb (GtkStatusIcon *status_icon,
432 EmpathyStatusIcon *icon)
434 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
436 DEBUG ("%s", priv->event ? "event" : "toggle");
439 empathy_event_activate (priv->event);
441 status_icon_toggle_visibility (icon);
446 status_icon_show_hide_window_cb (GtkToggleAction *action,
447 EmpathyStatusIcon *icon)
451 visible = gtk_toggle_action_get_active (action);
452 status_icon_set_visibility (icon, visible, TRUE);
456 status_icon_new_message_cb (GtkAction *action,
457 EmpathyStatusIcon *icon)
459 empathy_new_message_dialog_show (NULL);
463 status_icon_new_call_cb (GtkAction *action,
464 EmpathyStatusIcon *icon)
466 empathy_new_call_dialog_show (NULL);
470 status_icon_quit_cb (GtkAction *action,
471 EmpathyStatusIcon *icon)
477 status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
480 EmpathyStatusIcon *icon)
482 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
483 GtkWidget *menu_item;
487 show = empathy_window_get_is_visible (GTK_WINDOW (priv->window));
489 g_signal_handlers_block_by_func (priv->show_window_item,
490 status_icon_show_hide_window_cb,
492 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (priv->show_window_item),
494 g_signal_handlers_unblock_by_func (priv->show_window_item,
495 status_icon_show_hide_window_cb,
498 menu_item = gtk_ui_manager_get_widget (priv->ui_manager, "/menu/status");
499 submenu = empathy_presence_chooser_create_menu ();
500 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), submenu);
502 gtk_menu_popup (GTK_MENU (priv->popup_menu),
504 gtk_status_icon_position_menu,
511 status_icon_create_menu (EmpathyStatusIcon *icon)
513 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
517 filename = empathy_file_lookup ("empathy-status-icon.ui", "src");
518 gui = empathy_builder_get_file (filename,
519 "ui_manager", &priv->ui_manager,
520 "menu", &priv->popup_menu,
521 "show_list", &priv->show_window_item,
522 "new_message", &priv->new_message_item,
523 "status", &priv->status_item,
527 empathy_builder_connect (gui, icon,
528 "show_list", "toggled", status_icon_show_hide_window_cb,
529 "new_message", "activate", status_icon_new_message_cb,
530 "new_call", "activate", status_icon_new_call_cb,
531 "quit", "activate", status_icon_quit_cb,
534 g_object_ref (priv->ui_manager);
535 g_object_unref (gui);
539 status_icon_status_changed_cb (TpAccount *account,
540 TpConnectionStatus current,
541 TpConnectionStatus previous,
542 TpConnectionStatusReason reason,
543 gchar *dbus_error_name,
545 EmpathyStatusIcon *icon)
547 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
549 gtk_action_set_sensitive (priv->new_message_item,
550 empathy_account_manager_get_accounts_connected (NULL));
554 status_icon_finalize (GObject *object)
556 EmpathyStatusIconPriv *priv = GET_PRIV (object);
558 if (priv->blink_timeout) {
559 g_source_remove (priv->blink_timeout);
562 if (priv->notification) {
563 notify_notification_close (priv->notification, NULL);
564 g_object_unref (priv->notification);
565 priv->notification = NULL;
568 g_object_unref (priv->icon);
569 g_object_unref (priv->account_manager);
570 g_object_unref (priv->event_manager);
571 g_object_unref (priv->ui_manager);
572 g_object_unref (priv->notify_mgr);
576 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
578 GObjectClass *object_class = G_OBJECT_CLASS (klass);
580 object_class->finalize = status_icon_finalize;
582 g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
586 account_manager_prepared_cb (GObject *source_object,
587 GAsyncResult *result,
591 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
592 EmpathyStatusIcon *icon = user_data;
593 GError *error = NULL;
595 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
596 DEBUG ("Failed to prepare account manager: %s", error->message);
597 g_error_free (error);
601 list = tp_account_manager_get_valid_accounts (account_manager);
602 for (l = list; l != NULL; l = l->next) {
603 empathy_signal_connect_weak (l->data, "status-changed",
604 G_CALLBACK (status_icon_status_changed_cb),
609 status_icon_presence_changed_cb (icon);
613 empathy_status_icon_init (EmpathyStatusIcon *icon)
615 EmpathyStatusIconPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (icon,
616 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv);
619 priv->icon = gtk_status_icon_new ();
620 priv->account_manager = tp_account_manager_dup ();
621 priv->event_manager = empathy_event_manager_dup_singleton ();
623 tp_account_manager_prepare_async (priv->account_manager, NULL,
624 account_manager_prepared_cb, icon);
626 /* make icon listen and respond to MAIN_WINDOW_HIDDEN changes */
627 empathy_conf_notify_add (empathy_conf_get (),
628 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
629 status_icon_notify_visibility_cb,
632 status_icon_create_menu (icon);
634 g_signal_connect_swapped (priv->account_manager,
635 "most-available-presence-changed",
636 G_CALLBACK (status_icon_presence_changed_cb),
638 g_signal_connect (priv->event_manager, "event-added",
639 G_CALLBACK (status_icon_event_added_cb),
641 g_signal_connect (priv->event_manager, "event-removed",
642 G_CALLBACK (status_icon_event_removed_cb),
644 g_signal_connect (priv->event_manager, "event-updated",
645 G_CALLBACK (status_icon_event_updated_cb),
647 g_signal_connect (priv->icon, "activate",
648 G_CALLBACK (status_icon_activate_cb),
650 g_signal_connect (priv->icon, "popup-menu",
651 G_CALLBACK (status_icon_popup_menu_cb),
654 priv->notify_mgr = empathy_notify_manager_dup_singleton ();
658 empathy_status_icon_new (GtkWindow *window, gboolean hide_contact_list)
660 EmpathyStatusIconPriv *priv;
661 EmpathyStatusIcon *icon;
662 gboolean should_hide;
664 g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
666 icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
667 priv = GET_PRIV (icon);
669 priv->window = g_object_ref (window);
671 g_signal_connect_after (priv->window, "key-press-event",
672 G_CALLBACK (status_icon_key_press_event_cb),
675 g_signal_connect (priv->window, "delete-event",
676 G_CALLBACK (status_icon_delete_event_cb),
679 if (!hide_contact_list) {
680 empathy_conf_get_bool (empathy_conf_get (),
681 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
687 if (gtk_window_is_active (priv->window) == should_hide) {
688 status_icon_set_visibility (icon, !should_hide, FALSE);