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>
27 #include <gdk/gdkkeysyms.h>
29 #include <libnotify/notification.h>
31 #include <libempathy/empathy-utils.h>
32 #include <libempathy/empathy-idle.h>
33 #include <libempathy/empathy-account-manager.h>
35 #include <libempathy-gtk/empathy-presence-chooser.h>
36 #include <libempathy-gtk/empathy-conf.h>
37 #include <libempathy-gtk/empathy-ui-utils.h>
38 #include <libempathy-gtk/empathy-images.h>
39 #include <libempathy-gtk/empathy-new-message-dialog.h>
41 #include "empathy-accounts-dialog.h"
42 #include "empathy-status-icon.h"
43 #include "empathy-preferences.h"
44 #include "empathy-event-manager.h"
45 #include "empathy-misc.h"
47 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
48 #include <libempathy/empathy-debug.h>
50 /* Number of ms to wait when blinking */
51 #define BLINK_TIMEOUT 500
53 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyStatusIcon)
57 EmpathyAccountManager *account_manager;
58 gboolean showing_event_icon;
60 EmpathyEventManager *event_manager;
62 NotifyNotification *notification;
65 GtkUIManager *ui_manager;
66 GtkWidget *popup_menu;
67 GtkAction *show_window_item;
68 GtkAction *new_message_item;
69 GtkAction *status_item;
70 } EmpathyStatusIconPriv;
72 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
75 activate_event (EmpathyEvent *event)
77 empathy_event_activate (event);
83 status_icon_notification_closed_cb (NotifyNotification *notification,
84 EmpathyStatusIcon *icon)
86 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
87 EmpathyNotificationClosedReason reason = 0;
89 #ifdef notify_notification_get_closed_reason
90 reason = notify_notification_get_closed_reason (notification);
92 if (priv->notification) {
93 g_object_unref (priv->notification);
94 priv->notification = NULL;
101 /* the notification has been closed by the user, see the
102 * DesktopNotification spec.
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.
109 g_idle_add ((GSourceFunc) activate_event, priv->event);
111 /* inhibit other updates for this event */
112 empathy_event_inhibit_updates (priv->event);
117 notification_close_helper (EmpathyStatusIconPriv *priv)
119 if (priv->notification) {
120 notify_notification_close (priv->notification, NULL);
121 g_object_unref (priv->notification);
122 priv->notification = NULL;
127 status_icon_update_notification (EmpathyStatusIcon *icon)
129 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
130 GdkPixbuf *pixbuf = NULL;
132 if (!empathy_notification_is_enabled ()) {
133 /* always close the notification if this happens */
134 notification_close_helper (priv);
139 pixbuf = empathy_misc_get_pixbuf_for_notification (priv->event->contact,
140 priv->event->icon_name);
142 if (priv->notification) {
143 notify_notification_update (priv->notification,
144 priv->event->header, priv->event->message,
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);
152 g_signal_connect (priv->notification, "closed",
153 G_CALLBACK (status_icon_notification_closed_cb), icon);
156 /* if icon doesn't exist libnotify will crash */
158 notify_notification_set_icon_from_pixbuf (priv->notification,
160 notify_notification_show (priv->notification, NULL);
162 g_object_unref (pixbuf);
164 notification_close_helper (priv);
169 status_icon_update_tooltip (EmpathyStatusIcon *icon)
171 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
172 gchar *tooltip = NULL;
175 if (priv->event->message != NULL)
176 tooltip = g_strdup_printf ("<i>%s</i>\n%s",
178 priv->event->message);
180 tooltip = g_strdup_printf ("<i>%s</i>",
181 priv->event->header);
182 gtk_status_icon_set_tooltip_markup (priv->icon, tooltip);
184 tooltip = g_strdup (empathy_idle_get_status (priv->idle));
185 gtk_status_icon_set_tooltip_text (priv->icon, tooltip);
192 status_icon_update_icon (EmpathyStatusIcon *icon)
194 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
195 const gchar *icon_name;
197 if (priv->event && priv->showing_event_icon) {
198 icon_name = priv->event->icon_name;
200 TpConnectionPresenceType state;
202 state = empathy_idle_get_state (priv->idle);
203 icon_name = empathy_icon_name_for_presence (state);
206 if (icon_name != NULL)
207 gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
211 status_icon_blink_timeout_cb (EmpathyStatusIcon *icon)
213 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
215 priv->showing_event_icon = !priv->showing_event_icon;
216 status_icon_update_icon (icon);
221 status_icon_event_added_cb (EmpathyEventManager *manager,
223 EmpathyStatusIcon *icon)
225 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
231 DEBUG ("New event %p", event);
234 if (event->must_ack) {
235 priv->showing_event_icon = TRUE;
236 status_icon_update_icon (icon);
237 status_icon_update_tooltip (icon);
239 status_icon_update_notification (icon);
241 if (!priv->blink_timeout && priv->showing_event_icon) {
242 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
243 (GSourceFunc) status_icon_blink_timeout_cb,
249 status_icon_event_removed_cb (EmpathyEventManager *manager,
251 EmpathyStatusIcon *icon)
253 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
255 if (event != priv->event) {
259 priv->event = empathy_event_manager_get_top_event (priv->event_manager);
261 status_icon_update_tooltip (icon);
262 status_icon_update_icon (icon);
264 /* update notification anyway, as it's safe and we might have been
265 * changed presence in the meanwhile
267 status_icon_update_notification (icon);
269 if (!priv->event && priv->blink_timeout) {
270 g_source_remove (priv->blink_timeout);
271 priv->blink_timeout = 0;
276 status_icon_event_updated_cb (EmpathyEventManager *manager,
278 EmpathyStatusIcon *icon)
280 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
282 if (event != priv->event) {
286 if (empathy_notification_is_enabled ()) {
287 status_icon_update_notification (icon);
290 status_icon_update_tooltip (icon);
294 status_icon_set_visibility (EmpathyStatusIcon *icon,
298 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
301 empathy_conf_set_bool (empathy_conf_get (),
302 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, !visible);
306 empathy_window_iconify (priv->window, priv->icon);
310 gboolean one_enabled = FALSE;
312 empathy_window_present (GTK_WINDOW (priv->window), TRUE);
314 /* Show the accounts dialog if there is no enabled accounts */
315 accounts = empathy_account_manager_dup_accounts (priv->account_manager);
316 for (l = accounts ; l != NULL ; l = g_list_next (l)) {
317 one_enabled = empathy_account_is_enabled (EMPATHY_ACCOUNT (l->data))
319 g_object_unref (l->data);
321 g_list_free (accounts);
323 DEBUG ("No enabled account, Showing account dialog");
324 empathy_accounts_dialog_show (GTK_WINDOW (priv->window), NULL);
330 status_icon_notify_visibility_cb (EmpathyConf *conf,
334 EmpathyStatusIcon *icon = user_data;
335 gboolean hidden = FALSE;
337 if (empathy_conf_get_bool (conf, key, &hidden)) {
338 status_icon_set_visibility (icon, !hidden, FALSE);
343 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
345 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
348 visible = gtk_window_is_active (priv->window);
349 status_icon_set_visibility (icon, !visible, TRUE);
353 status_icon_idle_notify_cb (EmpathyStatusIcon *icon)
355 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
357 status_icon_update_icon (icon);
358 status_icon_update_tooltip (icon);
360 if (!empathy_notification_is_enabled ()) {
361 /* dismiss the outstanding notification if present */
363 if (priv->notification) {
364 notify_notification_close (priv->notification, NULL);
365 g_object_unref (priv->notification);
366 priv->notification = NULL;
372 status_icon_delete_event_cb (GtkWidget *widget,
374 EmpathyStatusIcon *icon)
376 status_icon_set_visibility (icon, FALSE, TRUE);
381 status_icon_key_press_event_cb (GtkWidget *window,
383 EmpathyStatusIcon *icon)
385 if (event->keyval == GDK_Escape) {
386 status_icon_set_visibility (icon, FALSE, TRUE);
392 status_icon_activate_cb (GtkStatusIcon *status_icon,
393 EmpathyStatusIcon *icon)
395 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
397 DEBUG ("%s", priv->event ? "event" : "toggle");
400 empathy_event_activate (priv->event);
402 status_icon_toggle_visibility (icon);
407 status_icon_show_hide_window_cb (GtkToggleAction *action,
408 EmpathyStatusIcon *icon)
412 visible = gtk_toggle_action_get_active (action);
413 status_icon_set_visibility (icon, visible, TRUE);
417 status_icon_new_message_cb (GtkAction *action,
418 EmpathyStatusIcon *icon)
420 empathy_new_message_dialog_show (NULL);
424 status_icon_quit_cb (GtkAction *action,
425 EmpathyStatusIcon *icon)
431 status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
434 EmpathyStatusIcon *icon)
436 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
437 GtkWidget *menu_item;
441 show = empathy_window_get_is_visible (GTK_WINDOW (priv->window));
443 g_signal_handlers_block_by_func (priv->show_window_item,
444 status_icon_show_hide_window_cb,
446 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (priv->show_window_item),
448 g_signal_handlers_unblock_by_func (priv->show_window_item,
449 status_icon_show_hide_window_cb,
452 menu_item = gtk_ui_manager_get_widget (priv->ui_manager, "/menu/status");
453 submenu = empathy_presence_chooser_create_menu ();
454 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), submenu);
456 gtk_menu_popup (GTK_MENU (priv->popup_menu),
458 gtk_status_icon_position_menu,
465 status_icon_create_menu (EmpathyStatusIcon *icon)
467 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
471 filename = empathy_file_lookup ("empathy-status-icon.ui", "src");
472 gui = empathy_builder_get_file (filename,
473 "ui_manager", &priv->ui_manager,
474 "menu", &priv->popup_menu,
475 "show_list", &priv->show_window_item,
476 "new_message", &priv->new_message_item,
477 "status", &priv->status_item,
481 empathy_builder_connect (gui, icon,
482 "show_list", "toggled", status_icon_show_hide_window_cb,
483 "new_message", "activate", status_icon_new_message_cb,
484 "quit", "activate", status_icon_quit_cb,
487 g_object_ref (priv->ui_manager);
488 g_object_unref (gui);
492 status_icon_connection_changed_cb (EmpathyAccountManager *manager,
493 EmpathyAccount *account,
494 TpConnectionStatusReason reason,
495 TpConnectionStatus current,
496 TpConnectionStatus previous,
497 EmpathyStatusIcon *icon)
499 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
500 int connected_accounts;
502 /* Check for a connected account */
503 connected_accounts = empathy_account_manager_get_connected_accounts (manager);
505 gtk_action_set_sensitive (priv->new_message_item, connected_accounts > 0);
509 status_icon_finalize (GObject *object)
511 EmpathyStatusIconPriv *priv = GET_PRIV (object);
513 if (priv->blink_timeout) {
514 g_source_remove (priv->blink_timeout);
517 g_signal_handlers_disconnect_by_func (priv->account_manager,
518 status_icon_connection_changed_cb,
521 if (priv->notification) {
522 notify_notification_close (priv->notification, NULL);
523 g_object_unref (priv->notification);
524 priv->notification = NULL;
527 g_object_unref (priv->icon);
528 g_object_unref (priv->idle);
529 g_object_unref (priv->account_manager);
530 g_object_unref (priv->event_manager);
531 g_object_unref (priv->ui_manager);
535 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
537 GObjectClass *object_class = G_OBJECT_CLASS (klass);
539 object_class->finalize = status_icon_finalize;
541 g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
545 empathy_status_icon_init (EmpathyStatusIcon *icon)
547 EmpathyStatusIconPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (icon,
548 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv);
551 priv->icon = gtk_status_icon_new ();
552 priv->account_manager = empathy_account_manager_dup_singleton ();
553 priv->idle = empathy_idle_dup_singleton ();
554 priv->event_manager = empathy_event_manager_dup_singleton ();
556 g_signal_connect (priv->account_manager,
557 "account-connection-changed",
558 G_CALLBACK (status_icon_connection_changed_cb), icon);
560 /* make icon listen and respond to MAIN_WINDOW_HIDDEN changes */
561 empathy_conf_notify_add (empathy_conf_get (),
562 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
563 status_icon_notify_visibility_cb,
566 status_icon_create_menu (icon);
567 status_icon_idle_notify_cb (icon);
569 g_signal_connect_swapped (priv->idle, "notify",
570 G_CALLBACK (status_icon_idle_notify_cb),
572 g_signal_connect (priv->event_manager, "event-added",
573 G_CALLBACK (status_icon_event_added_cb),
575 g_signal_connect (priv->event_manager, "event-removed",
576 G_CALLBACK (status_icon_event_removed_cb),
578 g_signal_connect (priv->event_manager, "event-updated",
579 G_CALLBACK (status_icon_event_updated_cb),
581 g_signal_connect (priv->icon, "activate",
582 G_CALLBACK (status_icon_activate_cb),
584 g_signal_connect (priv->icon, "popup-menu",
585 G_CALLBACK (status_icon_popup_menu_cb),
590 empathy_status_icon_new (GtkWindow *window, gboolean hide_contact_list)
592 EmpathyStatusIconPriv *priv;
593 EmpathyStatusIcon *icon;
594 gboolean should_hide;
596 g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
598 icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
599 priv = GET_PRIV (icon);
601 priv->window = g_object_ref (window);
603 g_signal_connect_after (priv->window, "key-press-event",
604 G_CALLBACK (status_icon_key_press_event_cb),
607 g_signal_connect (priv->window, "delete-event",
608 G_CALLBACK (status_icon_delete_event_cb),
611 if (!hide_contact_list) {
612 empathy_conf_get_bool (empathy_conf_get (),
613 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
619 if (gtk_window_is_active (priv->window) == should_hide) {
620 status_icon_set_visibility (icon, !should_hide, FALSE);