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)
327 status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
330 EmpathyStatusIcon *icon)
332 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
333 GtkWidget *menu_item;
337 show = gtk_widget_get_visible (priv->window);
339 g_signal_handlers_block_by_func (priv->show_window_item,
340 status_icon_show_hide_window_cb,
342 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (priv->show_window_item),
344 g_signal_handlers_unblock_by_func (priv->show_window_item,
345 status_icon_show_hide_window_cb,
348 menu_item = gtk_ui_manager_get_widget (priv->ui_manager, "/menu/status");
349 submenu = empathy_presence_chooser_create_menu ();
350 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), submenu);
352 gtk_menu_popup (GTK_MENU (priv->popup_menu),
354 gtk_status_icon_position_menu,
361 status_icon_create_menu (EmpathyStatusIcon *icon)
363 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
367 filename = empathy_file_lookup ("empathy-status-icon.ui", "src");
368 gui = empathy_builder_get_file (filename,
369 "ui_manager", &priv->ui_manager,
370 "menu", &priv->popup_menu,
371 "show_list", &priv->show_window_item,
372 "new_message", &priv->new_message_item,
373 "status", &priv->status_item,
377 empathy_builder_connect (gui, icon,
378 "show_list", "toggled", status_icon_show_hide_window_cb,
379 "new_message", "activate", status_icon_new_message_cb,
380 "new_call", "activate", status_icon_new_call_cb,
381 "quit", "activate", status_icon_quit_cb,
384 g_object_ref (priv->ui_manager);
385 g_object_unref (gui);
389 status_icon_status_changed_cb (TpAccount *account,
390 TpConnectionStatus current,
391 TpConnectionStatus previous,
392 TpConnectionStatusReason reason,
393 gchar *dbus_error_name,
395 EmpathyStatusIcon *icon)
397 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
399 gtk_action_set_sensitive (priv->new_message_item,
400 empathy_account_manager_get_accounts_connected (NULL));
404 status_icon_finalize (GObject *object)
406 EmpathyStatusIconPriv *priv = GET_PRIV (object);
408 if (priv->blink_timeout) {
409 g_source_remove (priv->blink_timeout);
412 g_object_unref (priv->icon);
413 g_object_unref (priv->account_manager);
414 g_object_unref (priv->event_manager);
415 g_object_unref (priv->ui_manager);
416 g_object_unref (priv->gsettings_ui);
417 g_object_unref (priv->window);
421 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
423 GObjectClass *object_class = G_OBJECT_CLASS (klass);
425 object_class->finalize = status_icon_finalize;
427 g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
431 account_manager_prepared_cb (GObject *source_object,
432 GAsyncResult *result,
436 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
437 EmpathyStatusIcon *icon = user_data;
438 GError *error = NULL;
440 if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
441 DEBUG ("Failed to prepare account manager: %s", error->message);
442 g_error_free (error);
446 list = tp_account_manager_get_valid_accounts (account_manager);
447 for (l = list; l != NULL; l = l->next) {
448 tp_g_signal_connect_object (l->data, "status-changed",
449 G_CALLBACK (status_icon_status_changed_cb),
454 status_icon_presence_changed_cb (icon);
458 empathy_status_icon_init (EmpathyStatusIcon *icon)
460 EmpathyStatusIconPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (icon,
461 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv);
464 priv->icon = gtk_status_icon_new ();
465 priv->account_manager = tp_account_manager_dup ();
466 priv->event_manager = empathy_event_manager_dup_singleton ();
468 tp_proxy_prepare_async (priv->account_manager, NULL,
469 account_manager_prepared_cb, icon);
471 /* make icon listen and respond to MAIN_WINDOW_HIDDEN changes */
472 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
473 g_signal_connect (priv->gsettings_ui,
474 "changed::" EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
475 G_CALLBACK (status_icon_notify_visibility_cb),
478 status_icon_create_menu (icon);
480 g_signal_connect_swapped (priv->account_manager,
481 "most-available-presence-changed",
482 G_CALLBACK (status_icon_presence_changed_cb),
484 g_signal_connect (priv->event_manager, "event-added",
485 G_CALLBACK (status_icon_event_added_cb),
487 g_signal_connect (priv->event_manager, "event-removed",
488 G_CALLBACK (status_icon_event_removed_cb),
490 g_signal_connect (priv->event_manager, "event-updated",
491 G_CALLBACK (status_icon_event_updated_cb),
493 g_signal_connect (priv->icon, "activate",
494 G_CALLBACK (status_icon_activate_cb),
496 g_signal_connect (priv->icon, "popup-menu",
497 G_CALLBACK (status_icon_popup_menu_cb),
502 empathy_status_icon_new (GtkWindow *window, gboolean hide_contact_list)
504 EmpathyStatusIconPriv *priv;
505 EmpathyStatusIcon *icon;
506 gboolean should_hide;
508 g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
510 icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
511 priv = GET_PRIV (icon);
513 priv->window = g_object_ref (window);
515 g_signal_connect_after (priv->window, "key-press-event",
516 G_CALLBACK (status_icon_key_press_event_cb),
519 g_signal_connect (priv->window, "delete-event",
520 G_CALLBACK (status_icon_delete_event_cb),
523 if (!hide_contact_list) {
524 should_hide = g_settings_get_boolean (priv->gsettings_ui,
525 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN);
530 status_icon_set_visibility (icon, !should_hide, FALSE);