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 <glade/glade.h>
28 #include <gdk/gdkkeysyms.h>
30 #include <libempathy/empathy-utils.h>
31 #include <libempathy/empathy-idle.h>
33 #include <libempathy-gtk/empathy-presence-chooser.h>
34 #include <libempathy-gtk/empathy-conf.h>
35 #include <libempathy-gtk/empathy-ui-utils.h>
36 #include <libempathy-gtk/empathy-images.h>
37 #include <libempathy-gtk/empathy-new-message-dialog.h>
39 #include "empathy-accounts-dialog.h"
40 #include "empathy-status-icon.h"
41 #include "empathy-preferences.h"
42 #include "empathy-event-manager.h"
44 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
45 #include <libempathy/empathy-debug.h>
47 /* Number of ms to wait when blinking */
48 #define BLINK_TIMEOUT 500
50 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyStatusIcon)
55 gboolean showing_event_icon;
58 EmpathyEventManager *event_manager;
62 GtkWidget *popup_menu;
63 GtkWidget *show_window_item;
64 GtkWidget *message_item;
65 GtkWidget *status_item;
66 } EmpathyStatusIconPriv;
68 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
71 status_icon_update_tooltip (EmpathyStatusIcon *icon)
73 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
74 const gchar *tooltip = NULL;
77 tooltip = priv->event->message;
81 tooltip = empathy_idle_get_status (priv->idle);
84 gtk_status_icon_set_tooltip (priv->icon, tooltip);
88 status_icon_update_icon (EmpathyStatusIcon *icon)
90 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
91 const gchar *icon_name;
93 if (priv->event && priv->showing_event_icon) {
94 icon_name = priv->event->icon_name;
98 state = empathy_idle_get_state (priv->idle);
99 icon_name = empathy_icon_name_for_presence (state);
102 gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
106 status_icon_blink_timeout_cb (EmpathyStatusIcon *icon)
108 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
110 priv->showing_event_icon = !priv->showing_event_icon;
111 status_icon_update_icon (icon);
117 status_icon_event_added_cb (EmpathyEventManager *manager,
119 EmpathyStatusIcon *icon)
121 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
127 DEBUG ("New event %p", event);
130 priv->showing_event_icon = TRUE;
132 status_icon_update_icon (icon);
133 status_icon_update_tooltip (icon);
135 if (!priv->blink_timeout) {
136 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
137 (GSourceFunc) status_icon_blink_timeout_cb,
143 status_icon_event_removed_cb (EmpathyEventManager *manager,
145 EmpathyStatusIcon *icon)
147 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
149 if (event != priv->event) {
153 priv->event = empathy_event_manager_get_top_event (priv->event_manager);
155 status_icon_update_tooltip (icon);
156 status_icon_update_icon (icon);
158 if (!priv->event && priv->blink_timeout) {
159 g_source_remove (priv->blink_timeout);
160 priv->blink_timeout = 0;
165 status_icon_set_visibility (EmpathyStatusIcon *icon,
169 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
172 empathy_conf_set_bool (empathy_conf_get (),
173 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, !visible);
177 empathy_window_iconify (priv->window, priv->icon);
181 empathy_window_present (GTK_WINDOW (priv->window), TRUE);
183 /* Show the accounts dialog if there is no enabled accounts */
184 accounts = mc_accounts_list_by_enabled (TRUE);
186 mc_accounts_list_free (accounts);
188 DEBUG ("No enabled account, Showing account dialog");
189 empathy_accounts_dialog_show (GTK_WINDOW (priv->window), NULL);
195 status_icon_notify_visibility_cb (EmpathyConf *conf,
199 EmpathyStatusIcon *icon = user_data;
200 gboolean hidden = FALSE;
202 if (empathy_conf_get_bool (conf, key, &hidden)) {
203 status_icon_set_visibility (icon, !hidden, FALSE);
208 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
210 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
213 visible = gtk_window_is_active (priv->window);
214 status_icon_set_visibility (icon, !visible, TRUE);
218 status_icon_idle_notify_cb (EmpathyStatusIcon *icon)
220 status_icon_update_icon (icon);
221 status_icon_update_tooltip (icon);
225 status_icon_delete_event_cb (GtkWidget *widget,
227 EmpathyStatusIcon *icon)
229 status_icon_set_visibility (icon, FALSE, TRUE);
234 status_icon_key_press_event_cb (GtkWidget *window,
236 EmpathyStatusIcon *icon)
238 if (event->keyval == GDK_Escape) {
239 status_icon_set_visibility (icon, FALSE, TRUE);
245 status_icon_activate_cb (GtkStatusIcon *status_icon,
246 EmpathyStatusIcon *icon)
248 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
250 DEBUG ("%s", priv->event ? "event" : "toggle");
253 empathy_event_activate (priv->event);
255 status_icon_toggle_visibility (icon);
260 status_icon_show_hide_window_cb (GtkWidget *widget,
261 EmpathyStatusIcon *icon)
265 visible = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget));
266 status_icon_set_visibility (icon, visible, TRUE);
270 status_icon_new_message_cb (GtkWidget *widget,
271 EmpathyStatusIcon *icon)
273 empathy_new_message_dialog_show (NULL);
277 status_icon_quit_cb (GtkWidget *window,
278 EmpathyStatusIcon *icon)
284 status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
287 EmpathyStatusIcon *icon)
289 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
293 show = empathy_window_get_is_visible (GTK_WINDOW (priv->window));
295 g_signal_handlers_block_by_func (priv->show_window_item,
296 status_icon_show_hide_window_cb,
298 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (priv->show_window_item),
300 g_signal_handlers_unblock_by_func (priv->show_window_item,
301 status_icon_show_hide_window_cb,
304 submenu = empathy_presence_chooser_create_menu ();
305 gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->status_item),
308 gtk_menu_popup (GTK_MENU (priv->popup_menu),
310 gtk_status_icon_position_menu,
317 status_icon_create_menu (EmpathyStatusIcon *icon)
319 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
323 filename = empathy_file_lookup ("empathy-status-icon.glade", "src");
324 glade = empathy_glade_get_file (filename,
327 "tray_menu", &priv->popup_menu,
328 "tray_show_list", &priv->show_window_item,
329 "tray_new_message", &priv->message_item,
330 "tray_status", &priv->status_item,
334 empathy_glade_connect (glade,
336 "tray_show_list", "toggled", status_icon_show_hide_window_cb,
337 "tray_new_message", "activate", status_icon_new_message_cb,
338 "tray_quit", "activate", status_icon_quit_cb,
341 g_object_unref (glade);
345 status_icon_finalize (GObject *object)
347 EmpathyStatusIconPriv *priv = GET_PRIV (object);
349 if (priv->blink_timeout) {
350 g_source_remove (priv->blink_timeout);
353 empathy_disconnect_account_status_changed (priv->token);
355 g_object_unref (priv->icon);
356 g_object_unref (priv->idle);
357 g_object_unref (priv->mc);
358 g_object_unref (priv->event_manager);
362 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
364 GObjectClass *object_class = G_OBJECT_CLASS (klass);
366 object_class->finalize = status_icon_finalize;
368 g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
372 status_icon_status_changed_cb (MissionControl *mc,
373 TpConnectionStatus status,
375 TpConnectionStatusReason reason,
376 const gchar *unique_name,
377 EmpathyStatusIcon *icon)
379 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
381 guint connection_status = 1;
383 /* Check for a connected account */
384 accounts = mc_accounts_list_by_enabled (TRUE);
385 for (l = accounts; l; l = l->next) {
386 connection_status = mission_control_get_connection_status (priv->mc,
389 if (connection_status == 0) {
393 mc_accounts_list_free (accounts);
395 gtk_widget_set_sensitive (priv->message_item, connection_status == 0);
399 empathy_status_icon_init (EmpathyStatusIcon *icon)
401 EmpathyStatusIconPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (icon,
402 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv);
405 priv->icon = gtk_status_icon_new ();
406 priv->mc = empathy_mission_control_new ();
407 priv->idle = empathy_idle_new ();
408 priv->event_manager = empathy_event_manager_new ();
409 priv->token = empathy_connect_to_account_status_changed (priv->mc,
410 G_CALLBACK (status_icon_status_changed_cb),
413 /* make icon listen and respond to MAIN_WINDOW_HIDDEN changes */
414 empathy_conf_notify_add (empathy_conf_get (),
415 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
416 status_icon_notify_visibility_cb,
419 status_icon_create_menu (icon);
420 status_icon_idle_notify_cb (icon);
422 g_signal_connect_swapped (priv->idle, "notify",
423 G_CALLBACK (status_icon_idle_notify_cb),
425 g_signal_connect (priv->event_manager, "event-added",
426 G_CALLBACK (status_icon_event_added_cb),
428 g_signal_connect (priv->event_manager, "event-removed",
429 G_CALLBACK (status_icon_event_removed_cb),
431 g_signal_connect (priv->icon, "activate",
432 G_CALLBACK (status_icon_activate_cb),
434 g_signal_connect (priv->icon, "popup-menu",
435 G_CALLBACK (status_icon_popup_menu_cb),
440 empathy_status_icon_new (GtkWindow *window, gboolean hide_contact_list)
442 EmpathyStatusIconPriv *priv;
443 EmpathyStatusIcon *icon;
444 gboolean should_hide;
446 g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
448 icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
449 priv = GET_PRIV (icon);
451 priv->window = g_object_ref (window);
453 g_signal_connect (priv->window, "key-press-event",
454 G_CALLBACK (status_icon_key_press_event_cb),
457 g_signal_connect (priv->window, "delete-event",
458 G_CALLBACK (status_icon_delete_event_cb),
461 if (!hide_contact_list) {
462 empathy_conf_get_bool (empathy_conf_get (),
463 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
469 if (gtk_window_is_active (priv->window) == should_hide) {
470 status_icon_set_visibility (icon, !should_hide, FALSE);