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 <telepathy-glib/util.h>
32 #include <libempathy/empathy-utils.h>
33 #include <libempathy/empathy-idle.h>
34 #include <libempathy/empathy-contact-manager.h>
35 #include <libempathy/empathy-dispatcher.h>
36 #include <libempathy/empathy-tp-chat.h>
37 #include <libempathy/empathy-tp-group.h>
39 #include <libempathy-gtk/empathy-presence-chooser.h>
40 #include <libempathy-gtk/empathy-conf.h>
41 #include <libempathy-gtk/empathy-ui-utils.h>
42 #include <libempathy-gtk/empathy-accounts-dialog.h>
43 #include <libempathy-gtk/empathy-images.h>
44 #include <libempathy-gtk/empathy-new-message-dialog.h>
45 #include <libempathy-gtk/empathy-contact-dialogs.h>
47 #include "empathy-status-icon.h"
48 #include "empathy-preferences.h"
50 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
51 #include <libempathy/empathy-debug.h>
53 /* Number of ms to wait when blinking */
54 #define BLINK_TIMEOUT 500
56 typedef struct _StatusIconEvent StatusIconEvent;
58 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyStatusIcon)
63 EmpathyDispatcher *dispatcher;
64 EmpathyContactManager *contact_manager;
66 gboolean showing_event_icon;
71 GtkWidget *popup_menu;
72 GtkWidget *show_window_item;
73 GtkWidget *message_item;
74 GtkWidget *status_item;
75 } EmpathyStatusIconPriv;
77 typedef void (*StatusIconEventFunc) (EmpathyStatusIcon *icon,
80 struct _StatusIconEvent {
83 StatusIconEventFunc func;
86 EmpathyStatusIcon *icon;
89 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
92 status_icon_update_tooltip (EmpathyStatusIcon *icon)
94 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
95 const gchar *tooltip = NULL;
98 tooltip = ((StatusIconEvent*)priv->events->data)->message;
102 tooltip = empathy_idle_get_status (priv->idle);
105 gtk_status_icon_set_tooltip (priv->icon, tooltip);
109 status_icon_update_icon (EmpathyStatusIcon *icon)
111 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
112 const gchar *icon_name;
114 if (priv->events && priv->showing_event_icon) {
115 icon_name = ((StatusIconEvent*)priv->events->data)->icon_name;
119 state = empathy_idle_get_state (priv->idle);
120 icon_name = empathy_icon_name_for_presence (state);
123 gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
127 status_icon_blink_timeout_cb (EmpathyStatusIcon *icon)
129 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
131 priv->showing_event_icon = !priv->showing_event_icon;
132 status_icon_update_icon (icon);
137 static void status_icon_channel_invalidated_cb (TpProxy *channel, guint domain,
138 gint code, gchar *message,
139 EmpathyStatusIcon *icon);
142 status_icon_event_free (StatusIconEvent *event)
144 g_free (event->icon_name);
145 g_free (event->message);
146 if (event->channel) {
147 g_signal_handlers_disconnect_by_func (event->channel,
148 status_icon_channel_invalidated_cb,
150 g_object_unref (event->channel);
152 g_slice_free (StatusIconEvent, event);
156 status_icon_event_remove (EmpathyStatusIcon *icon,
157 StatusIconEvent *event)
159 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
162 if (!(l = g_slist_find (priv->events, event))) {
166 priv->events = g_slist_delete_link (priv->events, l);
167 status_icon_event_free (event);
168 status_icon_update_tooltip (icon);
169 status_icon_update_icon (icon);
171 if (!priv->events && priv->blink_timeout) {
172 g_source_remove (priv->blink_timeout);
173 priv->blink_timeout = 0;
178 status_icon_event_remove_by_channel (EmpathyStatusIcon *icon,
181 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
184 for (l = priv->events; l; l = l->next) {
185 StatusIconEvent *event = l->data;
187 if (event->channel &&
188 empathy_proxy_equal (event->channel, channel)) {
189 DEBUG ("Found event '%s'", event->message);
190 status_icon_event_remove (icon, event);
197 status_icon_event_activate (EmpathyStatusIcon *icon,
198 StatusIconEvent *event)
201 event->func (icon, event->user_data);
203 status_icon_event_remove (icon, event);
207 status_icon_event_add (EmpathyStatusIcon *icon,
208 const gchar *icon_name,
209 const gchar *message,
210 StatusIconEventFunc func,
214 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
215 StatusIconEvent *event;
218 DEBUG ("Adding event: %s", message);
220 event = g_slice_new0 (StatusIconEvent);
221 event->icon_name = g_strdup (icon_name);
222 event->message = g_strdup (message);
224 event->user_data = user_data;
228 event->channel = g_object_ref (channel);
229 g_signal_connect (channel, "invalidated",
230 G_CALLBACK (status_icon_channel_invalidated_cb),
234 had_events = (priv->events != NULL);
235 priv->events = g_slist_append (priv->events, event);
237 priv->showing_event_icon = TRUE;
238 status_icon_update_icon (icon);
239 status_icon_update_tooltip (icon);
241 if (!priv->blink_timeout) {
242 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
243 (GSourceFunc) status_icon_blink_timeout_cb,
250 status_icon_channel_invalidated_cb (TpProxy *channel,
254 EmpathyStatusIcon *icon)
256 DEBUG ("%s", message);
257 status_icon_event_remove_by_channel (icon, TP_CHANNEL (channel));
261 status_icon_set_visibility (EmpathyStatusIcon *icon,
265 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
268 empathy_conf_set_bool (empathy_conf_get (),
269 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, !visible);
273 empathy_window_iconify (priv->window, priv->icon);
277 empathy_window_present (GTK_WINDOW (priv->window), TRUE);
279 /* Show the accounts dialog if there is no enabled accounts */
280 accounts = mc_accounts_list_by_enabled (TRUE);
282 mc_accounts_list_free (accounts);
284 DEBUG ("No enabled account, Showing account dialog");
285 empathy_accounts_dialog_show (GTK_WINDOW (priv->window));
291 status_icon_notify_visibility_cb (EmpathyConf *conf,
295 EmpathyStatusIcon *icon = user_data;
296 gboolean hidden = FALSE;
298 if (empathy_conf_get_bool (conf, key, &hidden)) {
299 status_icon_set_visibility (icon, !hidden, FALSE);
304 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
306 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
309 visible = gtk_window_is_active (priv->window);
310 status_icon_set_visibility (icon, !visible, TRUE);
314 status_icon_idle_notify_cb (EmpathyStatusIcon *icon)
316 status_icon_update_icon (icon);
317 status_icon_update_tooltip (icon);
321 status_icon_delete_event_cb (GtkWidget *widget,
323 EmpathyStatusIcon *icon)
325 status_icon_set_visibility (icon, FALSE, TRUE);
330 status_icon_activate_cb (GtkStatusIcon *status_icon,
331 EmpathyStatusIcon *icon)
333 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
335 DEBUG ("Activated: %s", priv->events ? "event" : "toggle");
338 status_icon_event_activate (icon, priv->events->data);
340 status_icon_toggle_visibility (icon);
345 status_icon_show_hide_window_cb (GtkWidget *widget,
346 EmpathyStatusIcon *icon)
350 visible = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget));
351 status_icon_set_visibility (icon, visible, TRUE);
355 status_icon_new_message_cb (GtkWidget *widget,
356 EmpathyStatusIcon *icon)
358 empathy_new_message_dialog_show (NULL);
362 status_icon_quit_cb (GtkWidget *window,
363 EmpathyStatusIcon *icon)
369 status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
372 EmpathyStatusIcon *icon)
374 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
378 show = empathy_window_get_is_visible (GTK_WINDOW (priv->window));
380 g_signal_handlers_block_by_func (priv->show_window_item,
381 status_icon_show_hide_window_cb,
383 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (priv->show_window_item),
385 g_signal_handlers_unblock_by_func (priv->show_window_item,
386 status_icon_show_hide_window_cb,
389 submenu = empathy_presence_chooser_create_menu ();
390 gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->status_item),
393 gtk_menu_popup (GTK_MENU (priv->popup_menu),
395 gtk_status_icon_position_menu,
402 status_icon_create_menu (EmpathyStatusIcon *icon)
404 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
408 filename = empathy_file_lookup ("empathy-status-icon.glade", "src");
409 glade = empathy_glade_get_file (filename,
412 "tray_menu", &priv->popup_menu,
413 "tray_show_list", &priv->show_window_item,
414 "tray_new_message", &priv->message_item,
415 "tray_status", &priv->status_item,
419 empathy_glade_connect (glade,
421 "tray_show_list", "toggled", status_icon_show_hide_window_cb,
422 "tray_new_message", "activate", status_icon_new_message_cb,
423 "tray_quit", "activate", status_icon_quit_cb,
426 g_object_unref (glade);
430 status_icon_channel_process (EmpathyStatusIcon *icon,
433 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
434 TpChannel *channel = TP_CHANNEL (user_data);
436 empathy_dispatcher_channel_process (priv->dispatcher, channel);
440 status_icon_chat_unref_idle (gpointer user_data)
442 g_object_unref (user_data);
447 status_icon_chat_message_received_cb (EmpathyTpChat *tp_chat,
448 EmpathyMessage *message,
449 EmpathyStatusIcon *icon)
451 EmpathyContact *sender;
455 g_idle_add (status_icon_chat_unref_idle, tp_chat);
456 g_signal_handlers_disconnect_by_func (tp_chat,
457 status_icon_chat_message_received_cb,
460 sender = empathy_message_get_sender (message);
461 msg = g_strdup_printf (_("New message from %s:\n%s"),
462 empathy_contact_get_name (sender),
463 empathy_message_get_body (message));
465 channel = empathy_tp_chat_get_channel (tp_chat);
466 status_icon_event_add (icon, EMPATHY_IMAGE_NEW_MESSAGE, msg,
467 status_icon_channel_process,
474 status_icon_filter_channel_cb (EmpathyDispatcher *dispatcher,
476 EmpathyStatusIcon *icon)
480 g_object_get (channel, "channel-type", &channel_type, NULL);
481 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)) {
482 EmpathyTpChat *tp_chat;
484 tp_chat = empathy_tp_chat_new (channel);
485 g_signal_connect (tp_chat, "message-received",
486 G_CALLBACK (status_icon_chat_message_received_cb),
489 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA)) {
490 EmpathyTpGroup *tp_group;
491 EmpathyContact *contact;
494 tp_group = empathy_tp_group_new (channel);
495 empathy_run_until_ready (tp_group);
496 empathy_tp_group_get_invitation (tp_group, &contact);
497 empathy_contact_run_until_ready (contact,
498 EMPATHY_CONTACT_READY_NAME,
501 msg = g_strdup_printf (_("Incoming call from %s"),
502 empathy_contact_get_name (contact));
504 status_icon_event_add (icon, EMPATHY_IMAGE_VOIP, msg,
505 status_icon_channel_process,
509 g_object_unref (contact);
510 g_object_unref (tp_group);
513 g_free (channel_type);
517 status_icon_dispatch_channel_cb (EmpathyDispatcher *dispatcher,
519 EmpathyStatusIcon *icon)
521 status_icon_event_remove_by_channel (icon, channel);
525 status_icon_tube_process (EmpathyStatusIcon *icon,
528 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
529 EmpathyDispatcherTube *tube = (EmpathyDispatcherTube*) user_data;
531 if (tube->activatable) {
532 empathy_dispatcher_tube_process (priv->dispatcher, tube);
537 /* Tell the user that the tube can't be handled */
538 str = g_strdup_printf (_("%s offered you an invitation, but "
539 "you don't have the needed external "
540 "application to handle it."),
541 empathy_contact_get_name (tube->initiator));
543 dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
545 GTK_BUTTONS_OK, str);
546 gtk_window_set_title (GTK_WINDOW (dialog),
547 _("Invitation Error"));
550 gtk_widget_show (dialog);
551 g_signal_connect (dialog, "response",
552 G_CALLBACK (gtk_widget_destroy),
556 empathy_dispatcher_tube_unref (tube);
560 status_icon_filter_tube_cb (EmpathyDispatcher *dispatcher,
561 EmpathyDispatcherTube *tube,
562 EmpathyStatusIcon *icon)
564 const gchar *icon_name;
567 empathy_contact_run_until_ready (tube->initiator,
568 EMPATHY_CONTACT_READY_NAME, NULL);
570 if (tube->activatable) {
571 icon_name = GTK_STOCK_EXECUTE;
572 msg = g_strdup_printf (_("%s is offering you an invitation. An external "
573 "application will be started to handle it."),
574 empathy_contact_get_name (tube->initiator));
576 icon_name = GTK_STOCK_DIALOG_ERROR;
577 msg = g_strdup_printf (_("%s is offering you an invitation, but "
578 "you don't have the needed external "
579 "application to handle it."),
580 empathy_contact_get_name (tube->initiator));
583 status_icon_event_add (icon, icon_name, msg, status_icon_tube_process,
584 empathy_dispatcher_tube_ref (tube),
591 status_icon_pending_subscribe (EmpathyStatusIcon *icon,
594 EmpathyContact *contact = EMPATHY_CONTACT (user_data);
596 empathy_subscription_dialog_show (contact, NULL);
597 g_object_unref (contact);
601 status_icon_pendings_changed_cb (EmpathyContactList *list,
602 EmpathyContact *contact,
603 EmpathyContact *actor,
607 EmpathyStatusIcon *icon)
612 /* FIXME: remove event if any */
616 DEBUG ("New local pending contact");
618 empathy_contact_run_until_ready (contact,
619 EMPATHY_CONTACT_READY_NAME,
622 str = g_string_new (NULL);
623 g_string_printf (str, _("Subscription requested by %s"),
624 empathy_contact_get_name (contact));
625 if (!G_STR_EMPTY (message)) {
626 g_string_append_printf (str, _("\nMessage: %s"), message);
629 status_icon_event_add (icon, GTK_STOCK_DIALOG_QUESTION, str->str,
630 status_icon_pending_subscribe,
631 g_object_ref (contact),
634 g_string_free (str, TRUE);
638 status_icon_finalize (GObject *object)
640 EmpathyStatusIconPriv *priv = GET_PRIV (object);
642 if (priv->blink_timeout) {
643 g_source_remove (priv->blink_timeout);
646 empathy_disconnect_account_status_changed (priv->token);
647 g_slist_foreach (priv->events, (GFunc) status_icon_event_free, NULL);
648 g_slist_free (priv->events);
650 g_object_unref (priv->icon);
651 g_object_unref (priv->idle);
652 g_object_unref (priv->mc);
653 g_object_unref (priv->contact_manager);
657 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
659 GObjectClass *object_class = G_OBJECT_CLASS (klass);
661 object_class->finalize = status_icon_finalize;
663 g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
667 status_icon_status_changed_cb (MissionControl *mc,
668 TpConnectionStatus status,
670 TpConnectionStatusReason reason,
671 const gchar *unique_name,
672 EmpathyStatusIcon *icon)
674 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
676 guint connection_status = 1;
678 /* Check for a connected account */
679 accounts = mc_accounts_list_by_enabled (TRUE);
680 for (l = accounts; l; l = l->next) {
681 connection_status = mission_control_get_connection_status (priv->mc,
684 if (connection_status == 0) {
688 mc_accounts_list_free (accounts);
690 gtk_widget_set_sensitive (priv->message_item, connection_status == 0);
694 empathy_status_icon_init (EmpathyStatusIcon *icon)
696 EmpathyStatusIconPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (icon,
697 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv);
700 priv->icon = gtk_status_icon_new ();
701 priv->mc = empathy_mission_control_new ();
702 priv->idle = empathy_idle_new ();
703 priv->dispatcher = empathy_dispatcher_new ();
704 priv->contact_manager = empathy_contact_manager_new ();
705 priv->token = empathy_connect_to_account_status_changed (priv->mc,
706 G_CALLBACK (status_icon_status_changed_cb),
709 /* make icon listen and respond to MAIN_WINDOW_HIDDEN changes */
710 empathy_conf_notify_add (empathy_conf_get (),
711 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
712 status_icon_notify_visibility_cb,
715 status_icon_create_menu (icon);
716 status_icon_idle_notify_cb (icon);
718 g_signal_connect_swapped (priv->idle, "notify",
719 G_CALLBACK (status_icon_idle_notify_cb),
721 g_signal_connect (priv->dispatcher, "filter-channel",
722 G_CALLBACK (status_icon_filter_channel_cb),
724 g_signal_connect (priv->dispatcher, "dispatch-channel",
725 G_CALLBACK (status_icon_dispatch_channel_cb),
727 g_signal_connect (priv->dispatcher, "filter-tube",
728 G_CALLBACK (status_icon_filter_tube_cb),
730 g_signal_connect (priv->contact_manager, "pendings-changed",
731 G_CALLBACK (status_icon_pendings_changed_cb),
733 g_signal_connect (priv->icon, "activate",
734 G_CALLBACK (status_icon_activate_cb),
736 g_signal_connect (priv->icon, "popup-menu",
737 G_CALLBACK (status_icon_popup_menu_cb),
742 empathy_status_icon_new (GtkWindow *window)
744 EmpathyStatusIconPriv *priv;
745 EmpathyStatusIcon *icon;
746 gboolean should_hide;
748 g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
750 icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
751 priv = GET_PRIV (icon);
753 priv->window = g_object_ref (window);
755 g_signal_connect (priv->window, "delete-event",
756 G_CALLBACK (status_icon_delete_event_cb),
759 empathy_conf_get_bool (empathy_conf_get (),
760 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
763 if (gtk_window_is_active (priv->window) == should_hide) {
764 status_icon_set_visibility (icon, !should_hide, FALSE);