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>
24 #include "libempathy/empathy-gsettings.h"
25 #include "libempathy/empathy-utils.h"
27 #include "libempathy-gtk/empathy-presence-chooser.h"
28 #include "libempathy-gtk/empathy-ui-utils.h"
29 #include "libempathy-gtk/empathy-new-message-dialog.h"
30 #include "libempathy-gtk/empathy-new-call-dialog.h"
32 #include "empathy-status-icon.h"
33 #include "empathy-event-manager.h"
35 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
36 #include "libempathy/empathy-debug.h"
38 /* Number of ms to wait when blinking */
39 #define BLINK_TIMEOUT 500
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyStatusIcon)
44 TpAccountManager *account_manager;
45 gboolean showing_event_icon;
47 EmpathyEventManager *event_manager;
49 GSettings *gsettings_ui;
52 GtkUIManager *ui_manager;
53 GtkWidget *popup_menu;
54 GtkAction *show_window_item;
55 GtkAction *new_message_item;
56 GtkAction *status_item;
57 } EmpathyStatusIconPriv;
59 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
62 status_icon_update_tooltip (EmpathyStatusIcon *icon)
64 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
67 gchar *tooltip = NULL;
69 if (priv->event->message != NULL)
70 tooltip = g_markup_printf_escaped ("<i>%s</i>\n%s",
72 priv->event->message);
74 tooltip = g_markup_printf_escaped ("<i>%s</i>",
76 gtk_status_icon_set_tooltip_markup (priv->icon, tooltip);
79 TpConnectionPresenceType type;
82 type = tp_account_manager_get_most_available_presence (
83 priv->account_manager, NULL, &msg);
85 if (!EMP_STR_EMPTY (msg)) {
86 gtk_status_icon_set_tooltip_text (priv->icon, msg);
89 gtk_status_icon_set_tooltip_text (priv->icon,
90 empathy_presence_get_default_message (type));
98 status_icon_update_icon (EmpathyStatusIcon *icon)
100 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
101 const gchar *icon_name;
103 if (priv->event && priv->showing_event_icon) {
104 icon_name = priv->event->icon_name;
106 TpConnectionPresenceType state;
108 state = tp_account_manager_get_most_available_presence (
109 priv->account_manager, NULL, NULL);
111 /* An unset presence type here doesn't make sense. Force it
113 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
114 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
117 icon_name = empathy_icon_name_for_presence (state);
120 if (icon_name != NULL)
121 gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
125 status_icon_blink_timeout_cb (EmpathyStatusIcon *icon)
127 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
129 priv->showing_event_icon = !priv->showing_event_icon;
130 status_icon_update_icon (icon);
135 status_icon_event_added_cb (EmpathyEventManager *manager,
137 EmpathyStatusIcon *icon)
139 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
145 DEBUG ("New event %p", event);
148 if (event->must_ack || event->type == EMPATHY_EVENT_TYPE_AUTH) {
149 priv->showing_event_icon = TRUE;
150 status_icon_update_icon (icon);
151 status_icon_update_tooltip (icon);
154 if (!priv->blink_timeout && priv->showing_event_icon) {
155 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
156 (GSourceFunc) status_icon_blink_timeout_cb,
162 status_icon_event_removed_cb (EmpathyEventManager *manager,
164 EmpathyStatusIcon *icon)
166 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
168 if (event != priv->event) {
172 priv->event = empathy_event_manager_get_top_event (priv->event_manager);
174 status_icon_update_tooltip (icon);
175 status_icon_update_icon (icon);
177 if (!priv->event && priv->blink_timeout) {
178 g_source_remove (priv->blink_timeout);
179 priv->blink_timeout = 0;
184 status_icon_event_updated_cb (EmpathyEventManager *manager,
186 EmpathyStatusIcon *icon)
188 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
190 if (event != priv->event) {
194 status_icon_update_tooltip (icon);
198 status_icon_set_visibility (EmpathyStatusIcon *icon,
202 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
205 g_settings_set_boolean (priv->gsettings_ui,
206 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
211 gtk_widget_hide (priv->window);
213 empathy_window_present (GTK_WINDOW (priv->window));
218 status_icon_notify_visibility_cb (GSettings *gsettings,
222 EmpathyStatusIcon *icon = user_data;
223 gboolean hidden = FALSE;
225 hidden = g_settings_get_boolean (gsettings, key);
226 status_icon_set_visibility (icon, !hidden, FALSE);
230 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
232 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
235 visible = gtk_window_is_active (GTK_WINDOW (priv->window));
236 status_icon_set_visibility (icon, !visible, TRUE);
240 status_icon_presence_changed_cb (EmpathyStatusIcon *icon)
242 status_icon_update_icon (icon);
243 status_icon_update_tooltip (icon);
247 status_icon_delete_event_cb (GtkWidget *widget,
249 EmpathyStatusIcon *icon)
251 status_icon_set_visibility (icon, FALSE, TRUE);
256 status_icon_key_press_event_cb (GtkWidget *window,
258 EmpathyStatusIcon *icon)
260 if (event->keyval == GDK_KEY_Escape) {
261 status_icon_set_visibility (icon, FALSE, TRUE);
267 status_icon_activate_cb (GtkStatusIcon *status_icon,
268 EmpathyStatusIcon *icon)
270 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
272 DEBUG ("%s", priv->event ? "event" : "toggle");
275 empathy_event_activate (priv->event);
277 status_icon_toggle_visibility (icon);
282 status_icon_show_hide_window_cb (GtkToggleAction *action,
283 EmpathyStatusIcon *icon)
287 visible = gtk_toggle_action_get_active (action);
288 status_icon_set_visibility (icon, visible, TRUE);
292 status_icon_new_message_cb (GtkAction *action,
293 EmpathyStatusIcon *icon)
295 empathy_new_message_dialog_show (NULL);
299 status_icon_new_call_cb (GtkAction *action,
300 EmpathyStatusIcon *icon)
302 empathy_new_call_dialog_show (NULL);
306 status_icon_quit_cb (GtkAction *action,
307 EmpathyStatusIcon *icon)
309 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
311 gtk_widget_destroy (priv->window);
315 status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
318 EmpathyStatusIcon *icon)
320 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
321 GtkWidget *menu_item;
325 show = gtk_widget_get_visible (priv->window);
327 g_signal_handlers_block_by_func (priv->show_window_item,
328 status_icon_show_hide_window_cb,
330 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (priv->show_window_item),
332 g_signal_handlers_unblock_by_func (priv->show_window_item,
333 status_icon_show_hide_window_cb,
336 menu_item = gtk_ui_manager_get_widget (priv->ui_manager, "/menu/status");
337 submenu = empathy_presence_chooser_create_menu ();
338 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), submenu);
340 gtk_menu_popup (GTK_MENU (priv->popup_menu),
342 gtk_status_icon_position_menu,
349 status_icon_create_menu (EmpathyStatusIcon *icon)
351 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
355 filename = empathy_file_lookup ("empathy-status-icon.ui", "src");
356 gui = empathy_builder_get_file (filename,
357 "ui_manager", &priv->ui_manager,
358 "menu", &priv->popup_menu,
359 "show_list", &priv->show_window_item,
360 "new_message", &priv->new_message_item,
361 "status", &priv->status_item,
365 empathy_builder_connect (gui, icon,
366 "show_list", "toggled", status_icon_show_hide_window_cb,
367 "new_message", "activate", status_icon_new_message_cb,
368 "new_call", "activate", status_icon_new_call_cb,
369 "quit", "activate", status_icon_quit_cb,
372 g_object_ref (priv->ui_manager);
373 g_object_unref (gui);
377 status_icon_status_changed_cb (TpAccount *account,
378 TpConnectionStatus current,
379 TpConnectionStatus previous,
380 TpConnectionStatusReason reason,
381 gchar *dbus_error_name,
383 EmpathyStatusIcon *icon)
385 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
387 gtk_action_set_sensitive (priv->new_message_item,
388 empathy_account_manager_get_accounts_connected (NULL));
392 status_icon_finalize (GObject *object)
394 EmpathyStatusIconPriv *priv = GET_PRIV (object);
396 if (priv->blink_timeout) {
397 g_source_remove (priv->blink_timeout);
400 g_object_unref (priv->icon);
401 g_object_unref (priv->account_manager);
402 g_object_unref (priv->event_manager);
403 g_object_unref (priv->ui_manager);
404 g_object_unref (priv->gsettings_ui);
405 g_object_unref (priv->window);
409 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
411 GObjectClass *object_class = G_OBJECT_CLASS (klass);
413 object_class->finalize = status_icon_finalize;
415 g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
419 account_manager_prepared_cb (GObject *source_object,
420 GAsyncResult *result,
424 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
425 EmpathyStatusIcon *icon = user_data;
426 GError *error = NULL;
428 if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
429 DEBUG ("Failed to prepare account manager: %s", error->message);
430 g_error_free (error);
434 list = tp_account_manager_dup_valid_accounts (account_manager);
435 for (l = list; l != NULL; l = l->next) {
436 tp_g_signal_connect_object (l->data, "status-changed",
437 G_CALLBACK (status_icon_status_changed_cb),
440 g_list_free_full (list, g_object_unref);
442 status_icon_presence_changed_cb (icon);
446 empathy_status_icon_init (EmpathyStatusIcon *icon)
448 EmpathyStatusIconPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (icon,
449 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv);
452 priv->icon = gtk_status_icon_new ();
453 priv->account_manager = tp_account_manager_dup ();
454 priv->event_manager = empathy_event_manager_dup_singleton ();
456 tp_proxy_prepare_async (priv->account_manager, NULL,
457 account_manager_prepared_cb, icon);
459 /* make icon listen and respond to MAIN_WINDOW_HIDDEN changes */
460 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
461 g_signal_connect (priv->gsettings_ui,
462 "changed::" EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
463 G_CALLBACK (status_icon_notify_visibility_cb),
466 status_icon_create_menu (icon);
468 g_signal_connect_swapped (priv->account_manager,
469 "most-available-presence-changed",
470 G_CALLBACK (status_icon_presence_changed_cb),
472 g_signal_connect (priv->event_manager, "event-added",
473 G_CALLBACK (status_icon_event_added_cb),
475 g_signal_connect (priv->event_manager, "event-removed",
476 G_CALLBACK (status_icon_event_removed_cb),
478 g_signal_connect (priv->event_manager, "event-updated",
479 G_CALLBACK (status_icon_event_updated_cb),
481 g_signal_connect (priv->icon, "activate",
482 G_CALLBACK (status_icon_activate_cb),
484 g_signal_connect (priv->icon, "popup-menu",
485 G_CALLBACK (status_icon_popup_menu_cb),
490 empathy_status_icon_new (GtkWindow *window, gboolean hide_contact_list)
492 EmpathyStatusIconPriv *priv;
493 EmpathyStatusIcon *icon;
494 gboolean should_hide;
496 g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
498 icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
499 priv = GET_PRIV (icon);
501 priv->window = g_object_ref (window);
503 g_signal_connect_after (priv->window, "key-press-event",
504 G_CALLBACK (status_icon_key_press_event_cb),
507 g_signal_connect (priv->window, "delete-event",
508 G_CALLBACK (status_icon_delete_event_cb),
511 if (!hide_contact_list) {
512 should_hide = g_settings_get_boolean (priv->gsettings_ui,
513 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN);
518 status_icon_set_visibility (icon, !should_hide, FALSE);