1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 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 <glib/gi18n.h>
30 #include <libmissioncontrol/mission-control.h>
32 #include <libempathy/empathy-contact-list.h>
33 #include <libempathy/empathy-contact-manager.h>
34 #include <libempathy/empathy-contact.h>
35 #include <libempathy/empathy-tp-chat.h>
36 #include <libempathy/empathy-debug.h>
37 #include <libempathy/empathy-utils.h>
38 #include <libempathy/empathy-conf.h>
39 #include <libempathy/empathy-idle.h>
40 #include <libempathy/empathy-filter.h>
42 #include "empathy-status-icon.h"
43 #include "empathy-contact-dialogs.h"
44 #include "empathy-presence-chooser.h"
45 #include "empathy-preferences.h"
46 #include "empathy-ui-utils.h"
47 #include "empathy-accounts-dialog.h"
48 #include "empathy-images.h"
49 #include "empathy-new-message-dialog.h"
52 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
53 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv))
55 #define DEBUG_DOMAIN "StatusIcon"
57 /* Number of ms to wait when blinking */
58 #define BLINK_TIMEOUT 500
60 typedef struct _StatusIconEvent StatusIconEvent;
62 struct _EmpathyStatusIconPriv {
64 EmpathyContactManager *manager;
65 EmpathyFilter *text_filter;
69 gboolean showing_event_icon;
70 StatusIconEvent *flash_state_event;
74 GtkWidget *popup_menu;
75 GtkWidget *show_window_item;
76 GtkWidget *message_item;
77 GtkWidget *status_item;
80 typedef void (*EventActivatedFunc) (StatusIconEvent *event);
82 struct _StatusIconEvent {
85 EventActivatedFunc func;
90 static void empathy_status_icon_class_init (EmpathyStatusIconClass *klass);
91 static void empathy_status_icon_init (EmpathyStatusIcon *icon);
92 static void status_icon_finalize (GObject *object);
93 static void status_icon_filter_new_channel (EmpathyFilter *filter,
96 EmpathyStatusIcon *icon);
97 static void status_icon_message_received_cb (EmpathyTpChat *tp_chat,
98 EmpathyMessage *message,
99 EmpathyStatusIcon *icon);
100 static void status_icon_idle_notify_cb (EmpathyStatusIcon *icon);
101 static void status_icon_update_tooltip (EmpathyStatusIcon *icon);
102 static void status_icon_set_from_state (EmpathyStatusIcon *icon);
103 static void status_icon_set_visibility (EmpathyStatusIcon *icon,
106 static void status_icon_toggle_visibility (EmpathyStatusIcon *icon);
107 static void status_icon_activate_cb (GtkStatusIcon *status_icon,
108 EmpathyStatusIcon *icon);
109 static gboolean status_icon_delete_event_cb (GtkWidget *widget,
111 EmpathyStatusIcon *icon);
112 static void status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
115 EmpathyStatusIcon *icon);
116 static void status_icon_create_menu (EmpathyStatusIcon *icon);
117 static void status_icon_new_message_cb (GtkWidget *widget,
118 EmpathyStatusIcon *icon);
119 static void status_icon_quit_cb (GtkWidget *window,
120 EmpathyStatusIcon *icon);
121 static void status_icon_show_hide_window_cb (GtkWidget *widget,
122 EmpathyStatusIcon *icon);
123 static void status_icon_pendings_changed_cb (EmpathyContactManager *manager,
124 EmpathyContact *contact,
125 EmpathyContact *actor,
129 EmpathyStatusIcon *icon);
130 static void status_icon_event_subscribe_cb (StatusIconEvent *event);
131 static void status_icon_event_flash_state_cb (StatusIconEvent *event);
132 static void status_icon_event_msg_cb (StatusIconEvent *event);
133 static StatusIconEvent * status_icon_event_new (EmpathyStatusIcon *icon,
134 const gchar *icon_name,
135 const gchar *message);
136 static void status_icon_event_remove (EmpathyStatusIcon *icon,
137 StatusIconEvent *event);
138 static gboolean status_icon_event_timeout_cb (EmpathyStatusIcon *icon);
139 static void status_icon_event_free (StatusIconEvent *event);
141 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
144 status_icon_notify_use_nm_cb (EmpathyConf *conf,
148 EmpathyStatusIconPriv *priv = GET_PRIV (user_data);
151 if (empathy_conf_get_bool (conf, key, &use_nm)) {
152 empathy_idle_set_use_nm (priv->idle, use_nm);
157 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
159 GObjectClass *object_class = G_OBJECT_CLASS (klass);
161 object_class->finalize = status_icon_finalize;
163 g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
167 empathy_status_icon_init (EmpathyStatusIcon *icon)
169 EmpathyStatusIconPriv *priv;
173 priv = GET_PRIV (icon);
175 priv->icon = gtk_status_icon_new ();
176 priv->manager = empathy_contact_manager_new ();
177 priv->mc = empathy_mission_control_new ();
178 priv->text_filter = empathy_filter_new ("org.gnome.Empathy.ChatFilter",
179 "/org/gnome/Empathy/ChatFilter",
180 TP_IFACE_CHANNEL_TYPE_TEXT,
181 MC_FILTER_PRIORITY_DIALOG,
182 MC_FILTER_FLAG_INCOMING);
184 /* Setup EmpathyIdle */
185 priv->idle = empathy_idle_new ();
186 empathy_conf_get_bool (empathy_conf_get (),
187 EMPATHY_PREFS_USE_NM,
189 empathy_conf_notify_add (empathy_conf_get (),
190 EMPATHY_PREFS_USE_NM,
191 status_icon_notify_use_nm_cb,
193 empathy_idle_set_auto_away (priv->idle, TRUE);
194 empathy_idle_set_use_nm (priv->idle, use_nm);
197 status_icon_create_menu (icon);
198 status_icon_idle_notify_cb (icon);
200 g_signal_connect (priv->text_filter, "new-channel",
201 G_CALLBACK (status_icon_filter_new_channel),
203 g_signal_connect_swapped (priv->idle, "notify",
204 G_CALLBACK (status_icon_idle_notify_cb),
206 g_signal_connect (priv->icon, "activate",
207 G_CALLBACK (status_icon_activate_cb),
209 g_signal_connect (priv->icon, "popup-menu",
210 G_CALLBACK (status_icon_popup_menu_cb),
212 g_signal_connect (priv->manager, "pendings-changed",
213 G_CALLBACK (status_icon_pendings_changed_cb),
216 pendings = empathy_contact_list_get_pendings (EMPATHY_CONTACT_LIST (priv->manager));
217 for (l = pendings; l; l = l->next) {
218 EmpathyPendingInfo *info;
221 status_icon_pendings_changed_cb (priv->manager,
228 empathy_pending_info_free (info);
230 g_list_free (pendings);
234 status_icon_finalize (GObject *object)
236 EmpathyStatusIconPriv *priv;
238 priv = GET_PRIV (object);
240 g_list_foreach (priv->events, (GFunc) status_icon_event_free, NULL);
241 g_list_free (priv->events);
243 if (priv->blink_timeout) {
244 g_source_remove (priv->blink_timeout);
247 g_object_unref (priv->icon);
248 g_object_unref (priv->window);
249 g_object_unref (priv->idle);
250 g_object_unref (priv->manager);
251 g_object_unref (priv->mc);
255 empathy_status_icon_new (GtkWindow *window)
257 EmpathyStatusIconPriv *priv;
258 EmpathyStatusIcon *icon;
259 gboolean should_hide;
261 g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
263 icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
264 priv = GET_PRIV (icon);
266 priv->window = g_object_ref (window);
268 g_signal_connect (priv->window, "delete-event",
269 G_CALLBACK (status_icon_delete_event_cb),
272 empathy_conf_get_bool (empathy_conf_get (),
273 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
276 if (gtk_window_is_active (priv->window) == should_hide) {
277 status_icon_set_visibility (icon, !should_hide, FALSE);
284 status_icon_filter_new_channel (EmpathyFilter *filter,
287 EmpathyStatusIcon *icon)
289 EmpathyStatusIconPriv *priv;
291 EmpathyTpChat *tp_chat;
294 priv = GET_PRIV (icon);
296 account = mission_control_get_account_for_connection (priv->mc, tp_conn, NULL);
298 empathy_debug (DEBUG_DOMAIN, "New text channel to be filtered for contact %s",
299 empathy_inspect_channel (account, tp_chan));
301 tp_chat = empathy_tp_chat_new (account, tp_chan);
302 g_object_set_data (G_OBJECT (tp_chat), "filter", filter);
303 g_object_unref (account);
305 messages = empathy_tp_chat_get_pendings (tp_chat);
307 empathy_debug (DEBUG_DOMAIN, "No pending msg, waiting...");
308 g_signal_connect (tp_chat, "message-received",
309 G_CALLBACK (status_icon_message_received_cb),
314 status_icon_message_received_cb (tp_chat, messages->data, icon);
316 g_list_foreach (messages, (GFunc) g_object_unref, NULL);
317 g_list_free (messages);
321 status_icon_message_received_cb (EmpathyTpChat *tp_chat,
322 EmpathyMessage *message,
323 EmpathyStatusIcon *icon)
325 EmpathyContact *sender;
327 StatusIconEvent *event;
329 empathy_debug (DEBUG_DOMAIN, "Message received, add event");
331 g_signal_handlers_disconnect_by_func (tp_chat,
332 status_icon_message_received_cb,
335 sender = empathy_message_get_sender (message);
336 msg = g_strdup_printf (_("New message from %s:\n%s"),
337 empathy_contact_get_name (sender),
338 empathy_message_get_body (message));
340 event = status_icon_event_new (icon, EMPATHY_IMAGE_NEW_MESSAGE, msg);
341 event->func = status_icon_event_msg_cb;
342 event->user_data = tp_chat;
346 status_icon_idle_notify_cb (EmpathyStatusIcon *icon)
348 EmpathyStatusIconPriv *priv;
349 McPresence flash_state;
351 priv = GET_PRIV (icon);
353 flash_state = empathy_idle_get_flash_state (priv->idle);
354 if (flash_state != MC_PRESENCE_UNSET) {
355 const gchar *icon_name;
357 icon_name = empathy_icon_name_for_presence_state (flash_state);
358 if (!priv->flash_state_event) {
359 /* We are now flashing */
360 priv->flash_state_event = status_icon_event_new (icon, icon_name, NULL);
361 priv->flash_state_event->user_data = icon;
362 priv->flash_state_event->func = status_icon_event_flash_state_cb;
364 /* We are still flashing but with another state */
365 g_free (priv->flash_state_event->icon_name);
366 priv->flash_state_event->icon_name = g_strdup (icon_name);
369 else if (priv->flash_state_event) {
370 /* We are no more flashing */
371 status_icon_event_remove (icon, priv->flash_state_event);
372 priv->flash_state_event = NULL;
375 if (!priv->showing_event_icon) {
376 status_icon_set_from_state (icon);
379 status_icon_update_tooltip (icon);
383 status_icon_update_tooltip (EmpathyStatusIcon *icon)
385 EmpathyStatusIconPriv *priv;
386 const gchar *tooltip = NULL;
388 priv = GET_PRIV (icon);
391 StatusIconEvent *event;
393 event = priv->events->data;
394 tooltip = event->message;
398 tooltip = empathy_idle_get_status (priv->idle);
401 gtk_status_icon_set_tooltip (priv->icon, tooltip);
405 status_icon_set_from_state (EmpathyStatusIcon *icon)
407 EmpathyStatusIconPriv *priv;
409 const gchar *icon_name;
411 priv = GET_PRIV (icon);
413 state = empathy_idle_get_state (priv->idle);
414 icon_name = empathy_icon_name_for_presence_state (state);
415 gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
419 status_icon_set_visibility (EmpathyStatusIcon *icon,
423 EmpathyStatusIconPriv *priv;
425 priv = GET_PRIV (icon);
428 empathy_conf_set_bool (empathy_conf_get (),
429 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, !visible);
433 empathy_window_iconify (priv->window, priv->icon);
437 empathy_window_present (GTK_WINDOW (priv->window), TRUE);
439 /* Show the accounts dialog if there is no enabled accounts */
440 accounts = mc_accounts_list_by_enabled (TRUE);
442 mc_accounts_list_free (accounts);
444 empathy_debug (DEBUG_DOMAIN,
445 "No enabled account, Showing account dialog");
446 empathy_accounts_dialog_show (GTK_WINDOW (priv->window));
452 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
454 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
457 visible = gtk_window_is_active (priv->window);
458 status_icon_set_visibility (icon, !visible, TRUE);
462 status_icon_activate_cb (GtkStatusIcon *status_icon,
463 EmpathyStatusIcon *icon)
465 EmpathyStatusIconPriv *priv;
467 priv = GET_PRIV (icon);
469 empathy_debug (DEBUG_DOMAIN, "Activated: %s",
470 priv->events ? "event" : "toggle");
473 status_icon_event_remove (icon, priv->events->data);
475 status_icon_toggle_visibility (icon);
480 status_icon_delete_event_cb (GtkWidget *widget,
482 EmpathyStatusIcon *icon)
484 status_icon_set_visibility (icon, FALSE, TRUE);
490 status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
493 EmpathyStatusIcon *icon)
495 EmpathyStatusIconPriv *priv;
499 priv = GET_PRIV (icon);
501 show = empathy_window_get_is_visible (GTK_WINDOW (priv->window));
503 g_signal_handlers_block_by_func (priv->show_window_item,
504 status_icon_show_hide_window_cb,
506 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (priv->show_window_item),
508 g_signal_handlers_unblock_by_func (priv->show_window_item,
509 status_icon_show_hide_window_cb,
512 submenu = empathy_presence_chooser_create_menu ();
513 gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->status_item),
516 gtk_menu_popup (GTK_MENU (priv->popup_menu),
518 gtk_status_icon_position_menu,
525 status_icon_create_menu (EmpathyStatusIcon *icon)
527 EmpathyStatusIconPriv *priv;
530 priv = GET_PRIV (icon);
532 glade = empathy_glade_get_file ("empathy-status-icon.glade",
535 "tray_menu", &priv->popup_menu,
536 "tray_show_list", &priv->show_window_item,
537 "tray_new_message", &priv->message_item,
538 "tray_status", &priv->status_item,
541 empathy_glade_connect (glade,
543 "tray_show_list", "toggled", status_icon_show_hide_window_cb,
544 "tray_new_message", "activate", status_icon_new_message_cb,
545 "tray_quit", "activate", status_icon_quit_cb,
548 g_object_unref (glade);
552 status_icon_new_message_cb (GtkWidget *widget,
553 EmpathyStatusIcon *icon)
555 EmpathyStatusIconPriv *priv;
557 priv = GET_PRIV (icon);
559 empathy_new_message_dialog_show (NULL);
563 status_icon_quit_cb (GtkWidget *window,
564 EmpathyStatusIcon *icon)
570 status_icon_show_hide_window_cb (GtkWidget *widget,
571 EmpathyStatusIcon *icon)
575 visible = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget));
576 status_icon_set_visibility (icon, visible, TRUE);
580 status_icon_pendings_changed_cb (EmpathyContactManager *manager,
581 EmpathyContact *contact,
582 EmpathyContact *actor,
586 EmpathyStatusIcon *icon)
588 EmpathyStatusIconPriv *priv;
589 StatusIconEvent *event;
593 priv = GET_PRIV (icon);
596 /* FIXME: We should remove the event */
600 for (l = priv->events; l; l = l->next) {
601 if (empathy_contact_equal (contact, ((StatusIconEvent*)l->data)->user_data)) {
606 str = g_string_new (NULL);
607 g_string_printf (str, _("Subscription requested by %s"),
608 empathy_contact_get_name (contact));
609 if (!G_STR_EMPTY (message)) {
610 g_string_append_printf (str, _("\nMessage: %s"), message);
613 event = status_icon_event_new (icon, GTK_STOCK_DIALOG_QUESTION, str->str);
614 event->user_data = g_object_ref (contact);
615 event->func = status_icon_event_subscribe_cb;
617 g_string_free (str, TRUE);
621 status_icon_event_subscribe_cb (StatusIconEvent *event)
623 EmpathyContact *contact;
625 contact = EMPATHY_CONTACT (event->user_data);
627 empathy_subscription_dialog_show (contact, NULL);
629 g_object_unref (contact);
633 status_icon_event_flash_state_cb (StatusIconEvent *event)
635 EmpathyStatusIconPriv *priv;
637 priv = GET_PRIV (event->user_data);
639 empathy_idle_set_flash_state (priv->idle, MC_PRESENCE_UNSET);
643 status_icon_event_msg_cb (StatusIconEvent *event)
645 EmpathyFilter *filter;
646 EmpathyTpChat *tp_chat;
648 empathy_debug (DEBUG_DOMAIN, "Dispatching text channel");
650 tp_chat = event->user_data;
651 filter = g_object_get_data (G_OBJECT (tp_chat), "filter");
652 empathy_filter_process (filter,
653 empathy_tp_chat_get_channel (tp_chat),
655 g_object_unref (tp_chat);
658 static StatusIconEvent *
659 status_icon_event_new (EmpathyStatusIcon *icon,
660 const gchar *icon_name,
661 const gchar *message)
663 EmpathyStatusIconPriv *priv;
664 StatusIconEvent *event;
666 priv = GET_PRIV (icon);
668 event = g_slice_new0 (StatusIconEvent);
669 event->icon_name = g_strdup (icon_name);
670 event->message = g_strdup (message);
672 priv->events = g_list_append (priv->events, event);
673 if (!priv->blink_timeout) {
674 priv->showing_event_icon = FALSE;
675 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
676 (GSourceFunc) status_icon_event_timeout_cb,
678 status_icon_event_timeout_cb (icon);
679 status_icon_update_tooltip (icon);
686 status_icon_event_remove (EmpathyStatusIcon *icon,
687 StatusIconEvent *event)
689 EmpathyStatusIconPriv *priv;
691 priv = GET_PRIV (icon);
696 priv->events = g_list_remove (priv->events, event);
697 status_icon_event_free (event);
698 priv->showing_event_icon = FALSE;
699 status_icon_update_tooltip (icon);
700 status_icon_set_from_state (icon);
706 if (priv->blink_timeout) {
707 g_source_remove (priv->blink_timeout);
708 priv->blink_timeout = 0;
713 status_icon_event_timeout_cb (EmpathyStatusIcon *icon)
715 EmpathyStatusIconPriv *priv;
717 priv = GET_PRIV (icon);
719 priv->showing_event_icon = !priv->showing_event_icon;
721 if (!priv->showing_event_icon) {
722 status_icon_set_from_state (icon);
724 StatusIconEvent *event;
726 event = priv->events->data;
727 gtk_status_icon_set_from_icon_name (priv->icon, event->icon_name);
734 status_icon_event_free (StatusIconEvent *event)
736 g_free (event->icon_name);
737 g_free (event->message);
738 g_slice_free (StatusIconEvent, event);