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>
23 #include "empathy-status-icon.h"
25 #include "empathy-event-manager.h"
26 #include "empathy-gsettings.h"
27 #include "empathy-new-call-dialog.h"
28 #include "empathy-new-message-dialog.h"
29 #include "empathy-presence-chooser.h"
30 #include "empathy-ui-utils.h"
31 #include "empathy-utils.h"
33 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
34 #include "empathy-debug.h"
36 /* Number of ms to wait when blinking */
37 #define BLINK_TIMEOUT 500
39 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyStatusIcon)
42 TpAccountManager *account_manager;
43 gboolean showing_event_icon;
45 EmpathyEventManager *event_manager;
47 GSettings *gsettings_ui;
50 GtkUIManager *ui_manager;
51 GtkWidget *popup_menu;
52 GtkAction *show_window_item;
53 GtkAction *new_message_item;
54 GtkAction *status_item;
55 } EmpathyStatusIconPriv;
57 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
60 status_icon_update_tooltip (EmpathyStatusIcon *icon)
62 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
65 gchar *tooltip = NULL;
67 if (priv->event->message != NULL)
68 tooltip = g_markup_printf_escaped ("<i>%s</i>\n%s",
70 priv->event->message);
72 tooltip = g_markup_printf_escaped ("<i>%s</i>",
74 gtk_status_icon_set_tooltip_markup (priv->icon, tooltip);
77 TpConnectionPresenceType type;
80 type = tp_account_manager_get_most_available_presence (
81 priv->account_manager, NULL, &msg);
83 if (!EMP_STR_EMPTY (msg)) {
84 gtk_status_icon_set_tooltip_text (priv->icon, msg);
87 gtk_status_icon_set_tooltip_text (priv->icon,
88 empathy_presence_get_default_message (type));
96 status_icon_update_icon (EmpathyStatusIcon *icon)
98 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
99 const gchar *icon_name;
101 if (priv->event && priv->showing_event_icon) {
102 icon_name = priv->event->icon_name;
104 TpConnectionPresenceType state;
106 state = tp_account_manager_get_most_available_presence (
107 priv->account_manager, NULL, NULL);
109 /* An unset presence type here doesn't make sense. Force it
111 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
112 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
115 icon_name = empathy_icon_name_for_presence (state);
118 if (icon_name != NULL)
119 gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
123 status_icon_blink_timeout_cb (EmpathyStatusIcon *icon)
125 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
127 priv->showing_event_icon = !priv->showing_event_icon;
128 status_icon_update_icon (icon);
133 status_icon_event_added_cb (EmpathyEventManager *manager,
135 EmpathyStatusIcon *icon)
137 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
143 DEBUG ("New event %p", event);
146 if (event->must_ack || event->type == EMPATHY_EVENT_TYPE_AUTH) {
147 priv->showing_event_icon = TRUE;
148 status_icon_update_icon (icon);
149 status_icon_update_tooltip (icon);
152 if (!priv->blink_timeout && priv->showing_event_icon) {
153 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
154 (GSourceFunc) status_icon_blink_timeout_cb,
160 status_icon_event_removed_cb (EmpathyEventManager *manager,
162 EmpathyStatusIcon *icon)
164 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
166 if (event != priv->event) {
170 priv->event = empathy_event_manager_get_top_event (priv->event_manager);
172 status_icon_update_tooltip (icon);
173 status_icon_update_icon (icon);
175 if (!priv->event && priv->blink_timeout) {
176 g_source_remove (priv->blink_timeout);
177 priv->blink_timeout = 0;
182 status_icon_event_updated_cb (EmpathyEventManager *manager,
184 EmpathyStatusIcon *icon)
186 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
188 if (event != priv->event) {
192 status_icon_update_tooltip (icon);
196 status_icon_set_visibility (EmpathyStatusIcon *icon,
200 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
203 g_settings_set_boolean (priv->gsettings_ui,
204 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
209 gtk_widget_hide (priv->window);
211 empathy_window_present (GTK_WINDOW (priv->window));
216 status_icon_notify_visibility_cb (GSettings *gsettings,
220 EmpathyStatusIcon *icon = user_data;
221 gboolean hidden = FALSE;
223 hidden = g_settings_get_boolean (gsettings, key);
224 status_icon_set_visibility (icon, !hidden, FALSE);
228 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
230 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
233 visible = gtk_window_is_active (GTK_WINDOW (priv->window));
234 status_icon_set_visibility (icon, !visible, TRUE);
238 status_icon_presence_changed_cb (EmpathyStatusIcon *icon)
240 status_icon_update_icon (icon);
241 status_icon_update_tooltip (icon);
245 status_icon_delete_event_cb (GtkWidget *widget,
247 EmpathyStatusIcon *icon)
249 status_icon_set_visibility (icon, FALSE, TRUE);
254 status_icon_key_press_event_cb (GtkWidget *window,
256 EmpathyStatusIcon *icon)
258 if (event->keyval == GDK_KEY_Escape) {
259 status_icon_set_visibility (icon, FALSE, TRUE);
265 status_icon_activate_cb (GtkStatusIcon *status_icon,
266 EmpathyStatusIcon *icon)
268 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
270 DEBUG ("%s", priv->event ? "event" : "toggle");
273 empathy_event_activate (priv->event);
275 status_icon_toggle_visibility (icon);
280 status_icon_show_hide_window_cb (GtkToggleAction *action,
281 EmpathyStatusIcon *icon)
285 visible = gtk_toggle_action_get_active (action);
286 status_icon_set_visibility (icon, visible, TRUE);
290 status_icon_new_message_cb (GtkAction *action,
291 EmpathyStatusIcon *icon)
293 empathy_new_message_dialog_show (NULL);
297 status_icon_new_call_cb (GtkAction *action,
298 EmpathyStatusIcon *icon)
300 empathy_new_call_dialog_show (NULL);
304 status_icon_quit_cb (GtkAction *action,
305 EmpathyStatusIcon *icon)
307 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
309 gtk_widget_destroy (priv->window);
313 status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
316 EmpathyStatusIcon *icon)
318 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
319 GtkWidget *menu_item;
323 show = gtk_widget_get_visible (priv->window);
325 g_signal_handlers_block_by_func (priv->show_window_item,
326 status_icon_show_hide_window_cb,
328 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (priv->show_window_item),
330 g_signal_handlers_unblock_by_func (priv->show_window_item,
331 status_icon_show_hide_window_cb,
334 menu_item = gtk_ui_manager_get_widget (priv->ui_manager, "/menu/status");
335 submenu = empathy_presence_chooser_create_menu ();
336 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), submenu);
338 gtk_menu_popup (GTK_MENU (priv->popup_menu),
340 gtk_status_icon_position_menu,
347 status_icon_create_menu (EmpathyStatusIcon *icon)
349 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
353 filename = empathy_file_lookup ("empathy-status-icon.ui", "src");
354 gui = empathy_builder_get_file (filename,
355 "ui_manager", &priv->ui_manager,
356 "menu", &priv->popup_menu,
357 "show_list", &priv->show_window_item,
358 "new_message", &priv->new_message_item,
359 "status", &priv->status_item,
363 empathy_builder_connect (gui, icon,
364 "show_list", "toggled", status_icon_show_hide_window_cb,
365 "new_message", "activate", status_icon_new_message_cb,
366 "new_call", "activate", status_icon_new_call_cb,
367 "quit", "activate", status_icon_quit_cb,
370 g_object_ref (priv->ui_manager);
371 g_object_unref (gui);
375 status_icon_status_changed_cb (TpAccount *account,
376 TpConnectionStatus current,
377 TpConnectionStatus previous,
378 TpConnectionStatusReason reason,
379 gchar *dbus_error_name,
381 EmpathyStatusIcon *icon)
383 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
385 gtk_action_set_sensitive (priv->new_message_item,
386 empathy_account_manager_get_accounts_connected (NULL));
390 status_icon_finalize (GObject *object)
392 EmpathyStatusIconPriv *priv = GET_PRIV (object);
394 if (priv->blink_timeout) {
395 g_source_remove (priv->blink_timeout);
398 g_object_unref (priv->icon);
399 g_object_unref (priv->account_manager);
400 g_object_unref (priv->event_manager);
401 g_object_unref (priv->ui_manager);
402 g_object_unref (priv->gsettings_ui);
403 g_object_unref (priv->window);
407 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
409 GObjectClass *object_class = G_OBJECT_CLASS (klass);
411 object_class->finalize = status_icon_finalize;
413 g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
417 account_manager_prepared_cb (GObject *source_object,
418 GAsyncResult *result,
422 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
423 EmpathyStatusIcon *icon = user_data;
424 GError *error = NULL;
426 if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
427 DEBUG ("Failed to prepare account manager: %s", error->message);
428 g_error_free (error);
432 list = tp_account_manager_dup_valid_accounts (account_manager);
433 for (l = list; l != NULL; l = l->next) {
434 tp_g_signal_connect_object (l->data, "status-changed",
435 G_CALLBACK (status_icon_status_changed_cb),
438 g_list_free_full (list, g_object_unref);
440 status_icon_presence_changed_cb (icon);
444 empathy_status_icon_init (EmpathyStatusIcon *icon)
446 EmpathyStatusIconPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (icon,
447 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv);
450 priv->icon = gtk_status_icon_new ();
451 priv->account_manager = tp_account_manager_dup ();
452 priv->event_manager = empathy_event_manager_dup_singleton ();
454 tp_proxy_prepare_async (priv->account_manager, NULL,
455 account_manager_prepared_cb, icon);
457 /* make icon listen and respond to MAIN_WINDOW_HIDDEN changes */
458 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
459 g_signal_connect (priv->gsettings_ui,
460 "changed::" EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
461 G_CALLBACK (status_icon_notify_visibility_cb),
464 status_icon_create_menu (icon);
466 g_signal_connect_swapped (priv->account_manager,
467 "most-available-presence-changed",
468 G_CALLBACK (status_icon_presence_changed_cb),
470 g_signal_connect (priv->event_manager, "event-added",
471 G_CALLBACK (status_icon_event_added_cb),
473 g_signal_connect (priv->event_manager, "event-removed",
474 G_CALLBACK (status_icon_event_removed_cb),
476 g_signal_connect (priv->event_manager, "event-updated",
477 G_CALLBACK (status_icon_event_updated_cb),
479 g_signal_connect (priv->icon, "activate",
480 G_CALLBACK (status_icon_activate_cb),
482 g_signal_connect (priv->icon, "popup-menu",
483 G_CALLBACK (status_icon_popup_menu_cb),
488 empathy_status_icon_new (GtkWindow *window, gboolean hide_contact_list)
490 EmpathyStatusIconPriv *priv;
491 EmpathyStatusIcon *icon;
492 gboolean should_hide;
494 g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
496 icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
497 priv = GET_PRIV (icon);
499 priv->window = g_object_ref (window);
501 g_signal_connect_after (priv->window, "key-press-event",
502 G_CALLBACK (status_icon_key_press_event_cb),
505 g_signal_connect (priv->window, "delete-event",
506 G_CALLBACK (status_icon_delete_event_cb),
509 if (!hide_contact_list) {
510 should_hide = g_settings_get_boolean (priv->gsettings_ui,
511 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN);
516 status_icon_set_visibility (icon, !should_hide, FALSE);