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 <telepathy-glib/account-manager.h>
33 #include <telepathy-glib/util.h>
35 #include <libempathy/empathy-gsettings.h>
36 #include <libempathy/empathy-utils.h>
38 #include <libempathy-gtk/empathy-presence-chooser.h>
39 #include <libempathy-gtk/empathy-ui-utils.h>
40 #include <libempathy-gtk/empathy-images.h>
41 #include <libempathy-gtk/empathy-new-message-dialog.h>
42 #include <libempathy-gtk/empathy-new-call-dialog.h>
44 #include "empathy-accounts-dialog.h"
45 #include "empathy-status-icon.h"
46 #include "empathy-preferences.h"
47 #include "empathy-event-manager.h"
49 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
50 #include <libempathy/empathy-debug.h>
52 /* Number of ms to wait when blinking */
53 #define BLINK_TIMEOUT 500
55 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyStatusIcon)
58 TpAccountManager *account_manager;
59 gboolean showing_event_icon;
61 EmpathyEventManager *event_manager;
63 GSettings *gsettings_ui;
66 GtkUIManager *ui_manager;
67 GtkWidget *popup_menu;
68 GtkAction *show_window_item;
69 GtkAction *new_message_item;
70 GtkAction *status_item;
71 } EmpathyStatusIconPriv;
73 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
76 status_icon_update_tooltip (EmpathyStatusIcon *icon)
78 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
81 gchar *tooltip = NULL;
83 if (priv->event->message != NULL)
84 tooltip = g_markup_printf_escaped ("<i>%s</i>\n%s",
86 priv->event->message);
88 tooltip = g_markup_printf_escaped ("<i>%s</i>",
90 gtk_status_icon_set_tooltip_markup (priv->icon, tooltip);
93 TpConnectionPresenceType type;
96 type = tp_account_manager_get_most_available_presence (
97 priv->account_manager, NULL, &msg);
99 if (!EMP_STR_EMPTY (msg)) {
100 gtk_status_icon_set_tooltip_text (priv->icon, msg);
103 gtk_status_icon_set_tooltip_text (priv->icon,
104 empathy_presence_get_default_message (type));
112 status_icon_update_icon (EmpathyStatusIcon *icon)
114 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
115 const gchar *icon_name;
117 if (priv->event && priv->showing_event_icon) {
118 icon_name = priv->event->icon_name;
120 TpConnectionPresenceType state;
122 state = tp_account_manager_get_most_available_presence (
123 priv->account_manager, NULL, NULL);
125 /* An unset presence type here doesn't make sense. Force it
127 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
128 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
131 icon_name = empathy_icon_name_for_presence (state);
134 if (icon_name != NULL)
135 gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
139 status_icon_blink_timeout_cb (EmpathyStatusIcon *icon)
141 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
143 priv->showing_event_icon = !priv->showing_event_icon;
144 status_icon_update_icon (icon);
149 status_icon_event_added_cb (EmpathyEventManager *manager,
151 EmpathyStatusIcon *icon)
153 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
159 DEBUG ("New event %p", event);
162 if (event->must_ack || event->type == EMPATHY_EVENT_TYPE_AUTH) {
163 priv->showing_event_icon = TRUE;
164 status_icon_update_icon (icon);
165 status_icon_update_tooltip (icon);
168 if (!priv->blink_timeout && priv->showing_event_icon) {
169 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
170 (GSourceFunc) status_icon_blink_timeout_cb,
176 status_icon_event_removed_cb (EmpathyEventManager *manager,
178 EmpathyStatusIcon *icon)
180 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
182 if (event != priv->event) {
186 priv->event = empathy_event_manager_get_top_event (priv->event_manager);
188 status_icon_update_tooltip (icon);
189 status_icon_update_icon (icon);
191 if (!priv->event && priv->blink_timeout) {
192 g_source_remove (priv->blink_timeout);
193 priv->blink_timeout = 0;
198 status_icon_event_updated_cb (EmpathyEventManager *manager,
200 EmpathyStatusIcon *icon)
202 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
204 if (event != priv->event) {
208 status_icon_update_tooltip (icon);
212 status_icon_set_visibility (EmpathyStatusIcon *icon,
216 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
219 g_settings_set_boolean (priv->gsettings_ui,
220 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
225 gtk_widget_hide (priv->window);
227 empathy_window_present (GTK_WINDOW (priv->window));
232 status_icon_notify_visibility_cb (GSettings *gsettings,
236 EmpathyStatusIcon *icon = user_data;
237 gboolean hidden = FALSE;
239 hidden = g_settings_get_boolean (gsettings, key);
240 status_icon_set_visibility (icon, !hidden, FALSE);
244 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
246 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
249 visible = gtk_window_is_active (GTK_WINDOW (priv->window));
250 status_icon_set_visibility (icon, !visible, TRUE);
254 status_icon_presence_changed_cb (EmpathyStatusIcon *icon)
256 status_icon_update_icon (icon);
257 status_icon_update_tooltip (icon);
261 status_icon_delete_event_cb (GtkWidget *widget,
263 EmpathyStatusIcon *icon)
265 status_icon_set_visibility (icon, FALSE, TRUE);
270 status_icon_key_press_event_cb (GtkWidget *window,
272 EmpathyStatusIcon *icon)
274 if (event->keyval == GDK_KEY_Escape) {
275 status_icon_set_visibility (icon, FALSE, TRUE);
281 status_icon_activate_cb (GtkStatusIcon *status_icon,
282 EmpathyStatusIcon *icon)
284 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
286 DEBUG ("%s", priv->event ? "event" : "toggle");
289 empathy_event_activate (priv->event);
291 status_icon_toggle_visibility (icon);
296 status_icon_show_hide_window_cb (GtkToggleAction *action,
297 EmpathyStatusIcon *icon)
301 visible = gtk_toggle_action_get_active (action);
302 status_icon_set_visibility (icon, visible, TRUE);
306 status_icon_new_message_cb (GtkAction *action,
307 EmpathyStatusIcon *icon)
309 empathy_new_message_dialog_show (NULL);
313 status_icon_new_call_cb (GtkAction *action,
314 EmpathyStatusIcon *icon)
316 empathy_new_call_dialog_show (NULL);
320 status_icon_quit_cb (GtkAction *action,
321 EmpathyStatusIcon *icon)
323 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
325 gtk_widget_destroy (priv->window);
329 status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
332 EmpathyStatusIcon *icon)
334 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
335 GtkWidget *menu_item;
339 show = gtk_widget_get_visible (priv->window);
341 g_signal_handlers_block_by_func (priv->show_window_item,
342 status_icon_show_hide_window_cb,
344 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (priv->show_window_item),
346 g_signal_handlers_unblock_by_func (priv->show_window_item,
347 status_icon_show_hide_window_cb,
350 menu_item = gtk_ui_manager_get_widget (priv->ui_manager, "/menu/status");
351 submenu = empathy_presence_chooser_create_menu ();
352 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), submenu);
354 gtk_menu_popup (GTK_MENU (priv->popup_menu),
356 gtk_status_icon_position_menu,
363 status_icon_create_menu (EmpathyStatusIcon *icon)
365 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
369 filename = empathy_file_lookup ("empathy-status-icon.ui", "src");
370 gui = empathy_builder_get_file (filename,
371 "ui_manager", &priv->ui_manager,
372 "menu", &priv->popup_menu,
373 "show_list", &priv->show_window_item,
374 "new_message", &priv->new_message_item,
375 "status", &priv->status_item,
379 empathy_builder_connect (gui, icon,
380 "show_list", "toggled", status_icon_show_hide_window_cb,
381 "new_message", "activate", status_icon_new_message_cb,
382 "new_call", "activate", status_icon_new_call_cb,
383 "quit", "activate", status_icon_quit_cb,
386 g_object_ref (priv->ui_manager);
387 g_object_unref (gui);
391 status_icon_status_changed_cb (TpAccount *account,
392 TpConnectionStatus current,
393 TpConnectionStatus previous,
394 TpConnectionStatusReason reason,
395 gchar *dbus_error_name,
397 EmpathyStatusIcon *icon)
399 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
401 gtk_action_set_sensitive (priv->new_message_item,
402 empathy_account_manager_get_accounts_connected (NULL));
406 status_icon_finalize (GObject *object)
408 EmpathyStatusIconPriv *priv = GET_PRIV (object);
410 if (priv->blink_timeout) {
411 g_source_remove (priv->blink_timeout);
414 g_object_unref (priv->icon);
415 g_object_unref (priv->account_manager);
416 g_object_unref (priv->event_manager);
417 g_object_unref (priv->ui_manager);
418 g_object_unref (priv->gsettings_ui);
419 g_object_unref (priv->window);
423 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
425 GObjectClass *object_class = G_OBJECT_CLASS (klass);
427 object_class->finalize = status_icon_finalize;
429 g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
433 account_manager_prepared_cb (GObject *source_object,
434 GAsyncResult *result,
438 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
439 EmpathyStatusIcon *icon = user_data;
440 GError *error = NULL;
442 if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
443 DEBUG ("Failed to prepare account manager: %s", error->message);
444 g_error_free (error);
448 list = tp_account_manager_get_valid_accounts (account_manager);
449 for (l = list; l != NULL; l = l->next) {
450 tp_g_signal_connect_object (l->data, "status-changed",
451 G_CALLBACK (status_icon_status_changed_cb),
456 status_icon_presence_changed_cb (icon);
460 empathy_status_icon_init (EmpathyStatusIcon *icon)
462 EmpathyStatusIconPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (icon,
463 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv);
466 priv->icon = gtk_status_icon_new ();
467 priv->account_manager = tp_account_manager_dup ();
468 priv->event_manager = empathy_event_manager_dup_singleton ();
470 tp_proxy_prepare_async (priv->account_manager, NULL,
471 account_manager_prepared_cb, icon);
473 /* make icon listen and respond to MAIN_WINDOW_HIDDEN changes */
474 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
475 g_signal_connect (priv->gsettings_ui,
476 "changed::" EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
477 G_CALLBACK (status_icon_notify_visibility_cb),
480 status_icon_create_menu (icon);
482 g_signal_connect_swapped (priv->account_manager,
483 "most-available-presence-changed",
484 G_CALLBACK (status_icon_presence_changed_cb),
486 g_signal_connect (priv->event_manager, "event-added",
487 G_CALLBACK (status_icon_event_added_cb),
489 g_signal_connect (priv->event_manager, "event-removed",
490 G_CALLBACK (status_icon_event_removed_cb),
492 g_signal_connect (priv->event_manager, "event-updated",
493 G_CALLBACK (status_icon_event_updated_cb),
495 g_signal_connect (priv->icon, "activate",
496 G_CALLBACK (status_icon_activate_cb),
498 g_signal_connect (priv->icon, "popup-menu",
499 G_CALLBACK (status_icon_popup_menu_cb),
504 empathy_status_icon_new (GtkWindow *window, gboolean hide_contact_list)
506 EmpathyStatusIconPriv *priv;
507 EmpathyStatusIcon *icon;
508 gboolean should_hide;
510 g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
512 icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
513 priv = GET_PRIV (icon);
515 priv->window = g_object_ref (window);
517 g_signal_connect_after (priv->window, "key-press-event",
518 G_CALLBACK (status_icon_key_press_event_cb),
521 g_signal_connect (priv->window, "delete-event",
522 G_CALLBACK (status_icon_delete_event_cb),
525 if (!hide_contact_list) {
526 should_hide = g_settings_get_boolean (priv->gsettings_ui,
527 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN);
532 status_icon_set_visibility (icon, !should_hide, FALSE);