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 <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-idle.h>
39 #include <libempathy/empathy-filter.h>
41 #include <libempathy-gtk/empathy-contact-dialogs.h>
42 #include <libempathy-gtk/empathy-presence-chooser.h>
43 #include <libempathy-gtk/empathy-conf.h>
44 #include <libempathy-gtk/empathy-ui-utils.h>
45 #include <libempathy-gtk/empathy-accounts-dialog.h>
46 #include <libempathy-gtk/empathy-images.h>
47 #include <libempathy-gtk/empathy-new-message-dialog.h>
49 #include "empathy-status-icon.h"
50 #include "empathy-preferences.h"
53 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
54 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv))
56 #define DEBUG_DOMAIN "StatusIcon"
58 /* Number of ms to wait when blinking */
59 #define BLINK_TIMEOUT 500
61 typedef struct _StatusIconEvent StatusIconEvent;
63 struct _EmpathyStatusIconPriv {
65 EmpathyContactManager *manager;
66 EmpathyFilter *text_filter;
67 EmpathyFilter *call_filter;
71 gboolean showing_event_icon;
72 StatusIconEvent *flash_state_event;
76 GtkWidget *popup_menu;
77 GtkWidget *show_window_item;
78 GtkWidget *message_item;
79 GtkWidget *status_item;
82 typedef void (*EventActivatedFunc) (StatusIconEvent *event);
84 struct _StatusIconEvent {
87 EventActivatedFunc func;
92 static void empathy_status_icon_class_init (EmpathyStatusIconClass *klass);
93 static void empathy_status_icon_init (EmpathyStatusIcon *icon);
94 static void status_icon_finalize (GObject *object);
95 static void status_icon_text_filter_new_channel (EmpathyFilter *filter,
98 EmpathyStatusIcon *icon);
99 static void status_icon_call_filter_new_channel (EmpathyFilter *filter,
102 EmpathyStatusIcon *icon);
103 static void status_icon_message_received_cb (EmpathyTpChat *tp_chat,
104 EmpathyMessage *message,
105 EmpathyStatusIcon *icon);
106 static void status_icon_idle_notify_cb (EmpathyStatusIcon *icon);
107 static void status_icon_update_tooltip (EmpathyStatusIcon *icon);
108 static void status_icon_set_from_state (EmpathyStatusIcon *icon);
109 static void status_icon_set_visibility (EmpathyStatusIcon *icon,
112 static void status_icon_toggle_visibility (EmpathyStatusIcon *icon);
113 static void status_icon_activate_cb (GtkStatusIcon *status_icon,
114 EmpathyStatusIcon *icon);
115 static gboolean status_icon_delete_event_cb (GtkWidget *widget,
117 EmpathyStatusIcon *icon);
118 static void status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
121 EmpathyStatusIcon *icon);
122 static void status_icon_create_menu (EmpathyStatusIcon *icon);
123 static void status_icon_new_message_cb (GtkWidget *widget,
124 EmpathyStatusIcon *icon);
125 static void status_icon_quit_cb (GtkWidget *window,
126 EmpathyStatusIcon *icon);
127 static void status_icon_show_hide_window_cb (GtkWidget *widget,
128 EmpathyStatusIcon *icon);
129 static void status_icon_pendings_changed_cb (EmpathyContactManager *manager,
130 EmpathyContact *contact,
131 EmpathyContact *actor,
135 EmpathyStatusIcon *icon);
136 static void status_icon_event_subscribe_cb (StatusIconEvent *event);
137 static void status_icon_event_flash_state_cb (StatusIconEvent *event);
138 static void status_icon_event_msg_cb (StatusIconEvent *event);
139 static StatusIconEvent * status_icon_event_new (EmpathyStatusIcon *icon,
140 const gchar *icon_name,
141 const gchar *message);
142 static void status_icon_event_remove (EmpathyStatusIcon *icon,
143 StatusIconEvent *event);
144 static gboolean status_icon_event_timeout_cb (EmpathyStatusIcon *icon);
145 static void status_icon_event_free (StatusIconEvent *event);
147 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
150 status_icon_notify_visibility_cb (EmpathyConf *conf,
154 EmpathyStatusIcon *icon = user_data;
155 gboolean hidden = FALSE;
157 if (empathy_conf_get_bool (conf, key, &hidden)) {
158 status_icon_set_visibility (icon, !hidden, FALSE);
163 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
165 GObjectClass *object_class = G_OBJECT_CLASS (klass);
167 object_class->finalize = status_icon_finalize;
169 g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
173 empathy_status_icon_init (EmpathyStatusIcon *icon)
175 EmpathyStatusIconPriv *priv;
178 priv = GET_PRIV (icon);
180 priv->icon = gtk_status_icon_new ();
181 priv->manager = empathy_contact_manager_new ();
182 priv->mc = empathy_mission_control_new ();
183 priv->text_filter = empathy_filter_new ("org.gnome.Empathy.ChatFilter",
184 "/org/gnome/Empathy/ChatFilter",
185 TP_IFACE_CHANNEL_TYPE_TEXT,
186 MC_FILTER_PRIORITY_DIALOG,
187 MC_FILTER_FLAG_INCOMING);
188 priv->call_filter = empathy_filter_new ("org.gnome.Empathy.CallFilter",
189 "/org/gnome/Empathy/CallFilter",
190 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
191 MC_FILTER_PRIORITY_DIALOG,
192 MC_FILTER_FLAG_INCOMING);
194 priv->idle = empathy_idle_new ();
196 /* make icon listen and respond to MAIN_WINDOW_HIDDEN changes */
197 empathy_conf_notify_add (empathy_conf_get (),
198 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
199 status_icon_notify_visibility_cb,
202 status_icon_create_menu (icon);
203 status_icon_idle_notify_cb (icon);
205 g_signal_connect (priv->text_filter, "new-channel",
206 G_CALLBACK (status_icon_text_filter_new_channel),
208 g_signal_connect (priv->call_filter, "new-channel",
209 G_CALLBACK (status_icon_call_filter_new_channel),
211 g_signal_connect_swapped (priv->idle, "notify",
212 G_CALLBACK (status_icon_idle_notify_cb),
214 g_signal_connect (priv->icon, "activate",
215 G_CALLBACK (status_icon_activate_cb),
217 g_signal_connect (priv->icon, "popup-menu",
218 G_CALLBACK (status_icon_popup_menu_cb),
220 g_signal_connect (priv->manager, "pendings-changed",
221 G_CALLBACK (status_icon_pendings_changed_cb),
224 pendings = empathy_contact_list_get_pendings (EMPATHY_CONTACT_LIST (priv->manager));
225 for (l = pendings; l; l = l->next) {
226 EmpathyPendingInfo *info;
229 status_icon_pendings_changed_cb (priv->manager,
236 empathy_pending_info_free (info);
238 g_list_free (pendings);
242 status_icon_finalize (GObject *object)
244 EmpathyStatusIconPriv *priv;
246 priv = GET_PRIV (object);
248 g_list_foreach (priv->events, (GFunc) status_icon_event_free, NULL);
249 g_list_free (priv->events);
251 if (priv->blink_timeout) {
252 g_source_remove (priv->blink_timeout);
255 g_object_unref (priv->icon);
256 g_object_unref (priv->window);
257 g_object_unref (priv->idle);
258 g_object_unref (priv->manager);
259 g_object_unref (priv->mc);
260 g_object_unref (priv->text_filter);
261 g_object_unref (priv->call_filter);
265 empathy_status_icon_new (GtkWindow *window)
267 EmpathyStatusIconPriv *priv;
268 EmpathyStatusIcon *icon;
269 gboolean should_hide;
271 g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
273 icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
274 priv = GET_PRIV (icon);
276 priv->window = g_object_ref (window);
278 g_signal_connect (priv->window, "delete-event",
279 G_CALLBACK (status_icon_delete_event_cb),
282 empathy_conf_get_bool (empathy_conf_get (),
283 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
286 if (gtk_window_is_active (priv->window) == should_hide) {
287 status_icon_set_visibility (icon, !should_hide, FALSE);
294 status_icon_text_filter_new_channel (EmpathyFilter *filter,
297 EmpathyStatusIcon *icon)
299 EmpathyStatusIconPriv *priv;
301 EmpathyTpChat *tp_chat;
303 priv = GET_PRIV (icon);
305 account = mission_control_get_account_for_connection (priv->mc, tp_conn, NULL);
307 empathy_debug (DEBUG_DOMAIN, "New text channel to be filtered for contact %s",
308 empathy_inspect_channel (account, tp_chan));
310 tp_chat = empathy_tp_chat_new (account, tp_chan);
311 g_object_set_data (G_OBJECT (tp_chat), "filter", filter);
312 g_object_unref (account);
314 g_signal_connect (tp_chat, "message-received",
315 G_CALLBACK (status_icon_message_received_cb),
320 status_icon_message_received_cb (EmpathyTpChat *tp_chat,
321 EmpathyMessage *message,
322 EmpathyStatusIcon *icon)
324 EmpathyContact *sender;
326 StatusIconEvent *event;
328 empathy_debug (DEBUG_DOMAIN, "Message received, add event");
330 g_signal_handlers_disconnect_by_func (tp_chat,
331 status_icon_message_received_cb,
334 sender = empathy_message_get_sender (message);
335 msg = g_strdup_printf (_("New message from %s:\n%s"),
336 empathy_contact_get_name (sender),
337 empathy_message_get_body (message));
339 event = status_icon_event_new (icon, EMPATHY_IMAGE_NEW_MESSAGE, msg);
340 event->func = status_icon_event_msg_cb;
341 event->user_data = tp_chat;
346 status_icon_call_member_added_cb (EmpathyTpGroup *group,
347 EmpathyContact *member,
348 EmpathyContact *actor,
350 const gchar *message,
351 EmpathyStatusIcon *icon)
353 EmpathyFilter *filter;
355 if (empathy_contact_is_user (member)) {
356 /* We are member, it's an outgoing call, we can dispatch
357 * the channel without asking the user */
358 empathy_debug (DEBUG_DOMAIN, "Process OUTGOING call channel");
359 filter = g_object_get_data (G_OBJECT (group), "filter");
360 empathy_filter_process (filter,
361 empathy_tp_group_get_channel (group),
363 g_object_unref (group);
368 status_icon_event_call_cb (StatusIconEvent *event)
370 EmpathyFilter *filter;
371 EmpathyTpGroup *group;
373 empathy_debug (DEBUG_DOMAIN, "Dispatching call channel");
375 group = event->user_data;
376 filter = g_object_get_data (G_OBJECT (group), "filter");
377 empathy_filter_process (filter,
378 empathy_tp_group_get_channel (group),
380 g_object_unref (group);
384 status_icon_call_local_pending_cb (EmpathyTpGroup *group,
385 EmpathyContact *member,
386 EmpathyContact *actor,
388 const gchar *message,
389 EmpathyStatusIcon *icon)
391 StatusIconEvent *event;
393 if (empathy_contact_is_user (member)) {
396 /* We are local pending, it's an incoming call, we need to ask
397 * the user if he wants to accept the call. */
398 empathy_contact_run_until_ready (member,
399 EMPATHY_CONTACT_READY_NAME,
402 empathy_debug (DEBUG_DOMAIN, "INCOMING call, add event");
404 msg = g_strdup_printf (_("Incoming call from %s:\n%s"),
405 empathy_contact_get_name (member),
408 event = status_icon_event_new (icon, EMPATHY_IMAGE_VOIP, msg);
409 event->func = status_icon_event_call_cb;
410 event->user_data = group;
416 status_icon_call_filter_new_channel (EmpathyFilter *filter,
419 EmpathyStatusIcon *icon)
421 EmpathyStatusIconPriv *priv;
423 EmpathyTpGroup *group;
425 priv = GET_PRIV (icon);
427 account = mission_control_get_account_for_connection (priv->mc, tp_conn, NULL);
429 empathy_debug (DEBUG_DOMAIN, "New media channel to be filtered");
431 /* FIXME: We have to check if the user is member or local-pending to
432 * know if it's an incoming or outgoing call because of the way we
433 * request media channels MC can't know if it's incoming or outgoing */
434 group = empathy_tp_group_new (account, tp_chan);
435 g_object_set_data (G_OBJECT (group), "filter", filter);
436 g_object_unref (account);
438 g_signal_connect (group, "member-added",
439 G_CALLBACK (status_icon_call_member_added_cb),
441 g_signal_connect (group, "local-pending",
442 G_CALLBACK (status_icon_call_local_pending_cb),
447 status_icon_idle_notify_cb (EmpathyStatusIcon *icon)
449 EmpathyStatusIconPriv *priv;
450 McPresence flash_state;
452 priv = GET_PRIV (icon);
454 flash_state = empathy_idle_get_flash_state (priv->idle);
455 if (flash_state != MC_PRESENCE_UNSET) {
456 const gchar *icon_name;
458 icon_name = empathy_icon_name_for_presence (flash_state);
459 if (!priv->flash_state_event) {
460 /* We are now flashing */
461 priv->flash_state_event = status_icon_event_new (icon, icon_name, NULL);
462 priv->flash_state_event->user_data = icon;
463 priv->flash_state_event->func = status_icon_event_flash_state_cb;
465 /* We are still flashing but with another state */
466 g_free (priv->flash_state_event->icon_name);
467 priv->flash_state_event->icon_name = g_strdup (icon_name);
470 else if (priv->flash_state_event) {
471 /* We are no more flashing */
472 status_icon_event_remove (icon, priv->flash_state_event);
473 priv->flash_state_event = NULL;
476 if (!priv->showing_event_icon) {
477 status_icon_set_from_state (icon);
480 status_icon_update_tooltip (icon);
484 status_icon_update_tooltip (EmpathyStatusIcon *icon)
486 EmpathyStatusIconPriv *priv;
487 const gchar *tooltip = NULL;
489 priv = GET_PRIV (icon);
492 StatusIconEvent *event;
494 event = priv->events->data;
495 tooltip = event->message;
499 tooltip = empathy_idle_get_status (priv->idle);
502 gtk_status_icon_set_tooltip (priv->icon, tooltip);
506 status_icon_set_from_state (EmpathyStatusIcon *icon)
508 EmpathyStatusIconPriv *priv;
510 const gchar *icon_name;
512 priv = GET_PRIV (icon);
514 state = empathy_idle_get_state (priv->idle);
515 icon_name = empathy_icon_name_for_presence (state);
516 gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
520 status_icon_set_visibility (EmpathyStatusIcon *icon,
524 EmpathyStatusIconPriv *priv;
526 priv = GET_PRIV (icon);
529 empathy_conf_set_bool (empathy_conf_get (),
530 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, !visible);
534 empathy_window_iconify (priv->window, priv->icon);
538 empathy_window_present (GTK_WINDOW (priv->window), TRUE);
540 /* Show the accounts dialog if there is no enabled accounts */
541 accounts = mc_accounts_list_by_enabled (TRUE);
543 mc_accounts_list_free (accounts);
545 empathy_debug (DEBUG_DOMAIN,
546 "No enabled account, Showing account dialog");
547 empathy_accounts_dialog_show (GTK_WINDOW (priv->window));
553 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
555 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
558 visible = gtk_window_is_active (priv->window);
559 status_icon_set_visibility (icon, !visible, TRUE);
563 status_icon_activate_cb (GtkStatusIcon *status_icon,
564 EmpathyStatusIcon *icon)
566 EmpathyStatusIconPriv *priv;
568 priv = GET_PRIV (icon);
570 empathy_debug (DEBUG_DOMAIN, "Activated: %s",
571 priv->events ? "event" : "toggle");
574 status_icon_event_remove (icon, priv->events->data);
576 status_icon_toggle_visibility (icon);
581 status_icon_delete_event_cb (GtkWidget *widget,
583 EmpathyStatusIcon *icon)
585 status_icon_set_visibility (icon, FALSE, TRUE);
591 status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
594 EmpathyStatusIcon *icon)
596 EmpathyStatusIconPriv *priv;
600 priv = GET_PRIV (icon);
602 show = empathy_window_get_is_visible (GTK_WINDOW (priv->window));
604 g_signal_handlers_block_by_func (priv->show_window_item,
605 status_icon_show_hide_window_cb,
607 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (priv->show_window_item),
609 g_signal_handlers_unblock_by_func (priv->show_window_item,
610 status_icon_show_hide_window_cb,
613 submenu = empathy_presence_chooser_create_menu ();
614 gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->status_item),
617 gtk_menu_popup (GTK_MENU (priv->popup_menu),
619 gtk_status_icon_position_menu,
626 status_icon_create_menu (EmpathyStatusIcon *icon)
628 EmpathyStatusIconPriv *priv;
632 priv = GET_PRIV (icon);
634 filename = empathy_file_lookup ("empathy-status-icon.glade", "src");
635 glade = empathy_glade_get_file (filename,
638 "tray_menu", &priv->popup_menu,
639 "tray_show_list", &priv->show_window_item,
640 "tray_new_message", &priv->message_item,
641 "tray_status", &priv->status_item,
645 empathy_glade_connect (glade,
647 "tray_show_list", "toggled", status_icon_show_hide_window_cb,
648 "tray_new_message", "activate", status_icon_new_message_cb,
649 "tray_quit", "activate", status_icon_quit_cb,
652 g_object_unref (glade);
656 status_icon_new_message_cb (GtkWidget *widget,
657 EmpathyStatusIcon *icon)
659 EmpathyStatusIconPriv *priv;
661 priv = GET_PRIV (icon);
663 empathy_new_message_dialog_show (NULL);
667 status_icon_quit_cb (GtkWidget *window,
668 EmpathyStatusIcon *icon)
674 status_icon_show_hide_window_cb (GtkWidget *widget,
675 EmpathyStatusIcon *icon)
679 visible = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget));
680 status_icon_set_visibility (icon, visible, TRUE);
684 status_icon_pendings_changed_cb (EmpathyContactManager *manager,
685 EmpathyContact *contact,
686 EmpathyContact *actor,
690 EmpathyStatusIcon *icon)
692 EmpathyStatusIconPriv *priv;
693 StatusIconEvent *event;
696 priv = GET_PRIV (icon);
699 /* FIXME: We should remove the event */
703 empathy_contact_run_until_ready (contact,
704 EMPATHY_CONTACT_READY_NAME,
707 str = g_string_new (NULL);
708 g_string_printf (str, _("Subscription requested by %s"),
709 empathy_contact_get_name (contact));
710 if (!G_STR_EMPTY (message)) {
711 g_string_append_printf (str, _("\nMessage: %s"), message);
714 event = status_icon_event_new (icon, GTK_STOCK_DIALOG_QUESTION, str->str);
715 event->user_data = g_object_ref (contact);
716 event->func = status_icon_event_subscribe_cb;
718 g_string_free (str, TRUE);
722 status_icon_event_subscribe_cb (StatusIconEvent *event)
724 EmpathyContact *contact;
726 contact = EMPATHY_CONTACT (event->user_data);
728 empathy_subscription_dialog_show (contact, NULL);
730 g_object_unref (contact);
734 status_icon_event_flash_state_cb (StatusIconEvent *event)
736 EmpathyStatusIconPriv *priv;
738 priv = GET_PRIV (event->user_data);
740 empathy_idle_set_flash_state (priv->idle, MC_PRESENCE_UNSET);
744 status_icon_event_msg_cb (StatusIconEvent *event)
746 EmpathyFilter *filter;
747 EmpathyTpChat *tp_chat;
749 empathy_debug (DEBUG_DOMAIN, "Dispatching text channel");
751 tp_chat = event->user_data;
752 filter = g_object_get_data (G_OBJECT (tp_chat), "filter");
753 empathy_filter_process (filter,
754 empathy_tp_chat_get_channel (tp_chat),
757 g_object_unref (tp_chat);
760 static StatusIconEvent *
761 status_icon_event_new (EmpathyStatusIcon *icon,
762 const gchar *icon_name,
763 const gchar *message)
765 EmpathyStatusIconPriv *priv;
766 StatusIconEvent *event;
768 priv = GET_PRIV (icon);
770 event = g_slice_new0 (StatusIconEvent);
771 event->icon_name = g_strdup (icon_name);
772 event->message = g_strdup (message);
774 priv->events = g_list_append (priv->events, event);
775 if (!priv->blink_timeout) {
776 priv->showing_event_icon = FALSE;
777 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
778 (GSourceFunc) status_icon_event_timeout_cb,
780 status_icon_event_timeout_cb (icon);
781 status_icon_update_tooltip (icon);
788 status_icon_event_remove (EmpathyStatusIcon *icon,
789 StatusIconEvent *event)
791 EmpathyStatusIconPriv *priv;
793 priv = GET_PRIV (icon);
798 priv->events = g_list_remove (priv->events, event);
799 status_icon_event_free (event);
800 priv->showing_event_icon = FALSE;
801 status_icon_update_tooltip (icon);
802 status_icon_set_from_state (icon);
808 if (priv->blink_timeout) {
809 g_source_remove (priv->blink_timeout);
810 priv->blink_timeout = 0;
815 status_icon_event_timeout_cb (EmpathyStatusIcon *icon)
817 EmpathyStatusIconPriv *priv;
819 priv = GET_PRIV (icon);
821 priv->showing_event_icon = !priv->showing_event_icon;
823 if (!priv->showing_event_icon) {
824 status_icon_set_from_state (icon);
826 StatusIconEvent *event;
828 event = priv->events->data;
829 gtk_status_icon_set_from_icon_name (priv->icon, event->icon_name);
836 status_icon_event_free (StatusIconEvent *event)
838 g_free (event->icon_name);
839 g_free (event->message);
840 g_slice_free (StatusIconEvent, event);