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;
87 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
90 status_icon_event_free (StatusIconEvent *event)
92 g_free (event->icon_name);
93 g_free (event->message);
94 g_slice_free (StatusIconEvent, event);
98 status_icon_set_visibility (EmpathyStatusIcon *icon,
102 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
105 empathy_conf_set_bool (empathy_conf_get (),
106 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, !visible);
110 empathy_window_iconify (priv->window, priv->icon);
114 empathy_window_present (GTK_WINDOW (priv->window), TRUE);
116 /* Show the accounts dialog if there is no enabled accounts */
117 accounts = mc_accounts_list_by_enabled (TRUE);
119 mc_accounts_list_free (accounts);
121 DEBUG ("No enabled account, Showing account dialog");
122 empathy_accounts_dialog_show (GTK_WINDOW (priv->window));
128 status_icon_notify_visibility_cb (EmpathyConf *conf,
132 EmpathyStatusIcon *icon = user_data;
133 gboolean hidden = FALSE;
135 if (empathy_conf_get_bool (conf, key, &hidden)) {
136 status_icon_set_visibility (icon, !hidden, FALSE);
141 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
143 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
146 visible = gtk_window_is_active (priv->window);
147 status_icon_set_visibility (icon, !visible, TRUE);
151 status_icon_update_tooltip (EmpathyStatusIcon *icon)
153 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
154 const gchar *tooltip = NULL;
157 tooltip = ((StatusIconEvent*)priv->events->data)->message;
161 tooltip = empathy_idle_get_status (priv->idle);
164 gtk_status_icon_set_tooltip (priv->icon, tooltip);
168 status_icon_update_icon (EmpathyStatusIcon *icon)
170 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
171 const gchar *icon_name;
173 if (priv->events && priv->showing_event_icon) {
174 icon_name = ((StatusIconEvent*)priv->events->data)->icon_name;
178 state = empathy_idle_get_state (priv->idle);
179 icon_name = empathy_icon_name_for_presence (state);
182 gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
186 status_icon_idle_notify_cb (EmpathyStatusIcon *icon)
188 status_icon_update_icon (icon);
189 status_icon_update_tooltip (icon);
193 status_icon_delete_event_cb (GtkWidget *widget,
195 EmpathyStatusIcon *icon)
197 status_icon_set_visibility (icon, FALSE, TRUE);
202 status_icon_activate_cb (GtkStatusIcon *status_icon,
203 EmpathyStatusIcon *icon)
205 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
207 DEBUG ("Activated: %s", priv->events ? "event" : "toggle");
210 StatusIconEvent *event;
212 event = priv->events->data;
214 event->func (icon, event->user_data);
216 status_icon_event_free (event);
217 priv->events = g_slist_remove (priv->events, event);
218 status_icon_update_tooltip (icon);
219 status_icon_update_icon (icon);
221 if (!priv->events && priv->blink_timeout) {
222 g_source_remove (priv->blink_timeout);
223 priv->blink_timeout = 0;
226 status_icon_toggle_visibility (icon);
231 status_icon_show_hide_window_cb (GtkWidget *widget,
232 EmpathyStatusIcon *icon)
236 visible = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget));
237 status_icon_set_visibility (icon, visible, TRUE);
241 status_icon_new_message_cb (GtkWidget *widget,
242 EmpathyStatusIcon *icon)
244 empathy_new_message_dialog_show (NULL);
248 status_icon_quit_cb (GtkWidget *window,
249 EmpathyStatusIcon *icon)
255 status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
258 EmpathyStatusIcon *icon)
260 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
264 show = empathy_window_get_is_visible (GTK_WINDOW (priv->window));
266 g_signal_handlers_block_by_func (priv->show_window_item,
267 status_icon_show_hide_window_cb,
269 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (priv->show_window_item),
271 g_signal_handlers_unblock_by_func (priv->show_window_item,
272 status_icon_show_hide_window_cb,
275 submenu = empathy_presence_chooser_create_menu ();
276 gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->status_item),
279 gtk_menu_popup (GTK_MENU (priv->popup_menu),
281 gtk_status_icon_position_menu,
288 status_icon_create_menu (EmpathyStatusIcon *icon)
290 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
294 filename = empathy_file_lookup ("empathy-status-icon.glade", "src");
295 glade = empathy_glade_get_file (filename,
298 "tray_menu", &priv->popup_menu,
299 "tray_show_list", &priv->show_window_item,
300 "tray_new_message", &priv->message_item,
301 "tray_status", &priv->status_item,
305 empathy_glade_connect (glade,
307 "tray_show_list", "toggled", status_icon_show_hide_window_cb,
308 "tray_new_message", "activate", status_icon_new_message_cb,
309 "tray_quit", "activate", status_icon_quit_cb,
312 g_object_unref (glade);
316 status_icon_blink_timeout_cb (EmpathyStatusIcon *icon)
318 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
320 priv->showing_event_icon = !priv->showing_event_icon;
321 status_icon_update_icon (icon);
327 status_icon_event_add (EmpathyStatusIcon *icon,
328 const gchar *icon_name,
329 const gchar *message,
330 StatusIconEventFunc func,
333 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
334 StatusIconEvent *event;
337 DEBUG ("Adding event: %s", message);
339 event = g_slice_new (StatusIconEvent);
340 event->icon_name = g_strdup (icon_name);
341 event->message = g_strdup (message);
343 event->user_data = user_data;
345 had_events = (priv->events != NULL);
346 priv->events = g_slist_append (priv->events, event);
348 priv->showing_event_icon = TRUE;
349 status_icon_update_icon (icon);
350 status_icon_update_tooltip (icon);
352 if (!priv->blink_timeout) {
353 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
354 (GSourceFunc) status_icon_blink_timeout_cb,
361 status_icon_channel_process (EmpathyStatusIcon *icon,
364 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
365 TpChannel *channel = TP_CHANNEL (user_data);
367 empathy_dispatcher_channel_process (priv->dispatcher, channel);
368 g_object_unref (channel);
372 status_icon_chat_unref_idle (gpointer user_data)
374 g_object_unref (user_data);
379 status_icon_chat_message_received_cb (EmpathyTpChat *tp_chat,
380 EmpathyMessage *message,
381 EmpathyStatusIcon *icon)
383 EmpathyContact *sender;
387 g_idle_add (status_icon_chat_unref_idle, tp_chat);
388 g_signal_handlers_disconnect_by_func (tp_chat,
389 status_icon_chat_message_received_cb,
392 sender = empathy_message_get_sender (message);
393 msg = g_strdup_printf (_("New message from %s:\n%s"),
394 empathy_contact_get_name (sender),
395 empathy_message_get_body (message));
397 channel = empathy_tp_chat_get_channel (tp_chat);
398 status_icon_event_add (icon, EMPATHY_IMAGE_NEW_MESSAGE, msg,
399 status_icon_channel_process,
400 g_object_ref (channel));
406 status_icon_filter_channel_cb (EmpathyDispatcher *dispatcher,
408 EmpathyStatusIcon *icon)
412 g_object_get (channel, "channel-type", &channel_type, NULL);
413 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)) {
414 EmpathyTpChat *tp_chat;
416 tp_chat = empathy_tp_chat_new (channel);
417 g_signal_connect (tp_chat, "message-received",
418 G_CALLBACK (status_icon_chat_message_received_cb),
421 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA)) {
422 EmpathyTpGroup *tp_group;
423 EmpathyContact *contact;
426 tp_group = empathy_tp_group_new (channel);
427 empathy_run_until_ready (tp_group);
428 empathy_tp_group_get_invitation (tp_group, &contact);
429 empathy_contact_run_until_ready (contact,
430 EMPATHY_CONTACT_READY_NAME,
433 msg = g_strdup_printf (_("Incoming call from %s"),
434 empathy_contact_get_name (contact));
436 status_icon_event_add (icon, EMPATHY_IMAGE_VOIP, msg,
437 status_icon_channel_process,
438 g_object_ref (channel));
441 g_object_unref (contact);
442 g_object_unref (tp_group);
445 g_free (channel_type);
449 status_icon_tube_process (EmpathyStatusIcon *icon,
452 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
453 EmpathyDispatcherTube *tube = (EmpathyDispatcherTube*) user_data;
455 if (tube->activatable) {
456 empathy_dispatcher_tube_process (priv->dispatcher, tube);
461 /* Tell the user that the tube can't be handled */
462 str = g_strdup_printf (_("%s offered you an invitation, but "
463 "you don't have the needed external "
464 "application to handle it."),
465 empathy_contact_get_name (tube->initiator));
467 dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
469 GTK_BUTTONS_OK, str);
470 gtk_window_set_title (GTK_WINDOW (dialog),
471 _("Invitation Error"));
474 gtk_widget_show (dialog);
475 g_signal_connect (dialog, "response",
476 G_CALLBACK (gtk_widget_destroy),
480 empathy_dispatcher_tube_unref (tube);
484 status_icon_filter_tube_cb (EmpathyDispatcher *dispatcher,
485 EmpathyDispatcherTube *tube,
486 EmpathyStatusIcon *icon)
488 const gchar *icon_name;
491 empathy_contact_run_until_ready (tube->initiator,
492 EMPATHY_CONTACT_READY_NAME, NULL);
494 if (tube->activatable) {
495 icon_name = GTK_STOCK_EXECUTE;
496 msg = g_strdup_printf (_("%s is offering you an invitation. An external "
497 "application will be started to handle it."),
498 empathy_contact_get_name (tube->initiator));
500 icon_name = GTK_STOCK_DIALOG_ERROR;
501 msg = g_strdup_printf (_("%s is offering you an invitation, but "
502 "you don't have the needed external "
503 "application to handle it."),
504 empathy_contact_get_name (tube->initiator));
507 status_icon_event_add (icon, icon_name, msg, status_icon_tube_process,
508 empathy_dispatcher_tube_ref (tube));
514 status_icon_pending_subscribe (EmpathyStatusIcon *icon,
517 EmpathyContact *contact = EMPATHY_CONTACT (user_data);
519 empathy_subscription_dialog_show (contact, NULL);
520 g_object_unref (contact);
524 status_icon_pendings_changed_cb (EmpathyContactList *list,
525 EmpathyContact *contact,
526 EmpathyContact *actor,
530 EmpathyStatusIcon *icon)
535 /* FIXME: remove event if any */
539 DEBUG ("New local pending contact");
541 empathy_contact_run_until_ready (contact,
542 EMPATHY_CONTACT_READY_NAME,
545 str = g_string_new (NULL);
546 g_string_printf (str, _("Subscription requested by %s"),
547 empathy_contact_get_name (contact));
548 if (!G_STR_EMPTY (message)) {
549 g_string_append_printf (str, _("\nMessage: %s"), message);
552 status_icon_event_add (icon, GTK_STOCK_DIALOG_QUESTION, str->str,
553 status_icon_pending_subscribe,
554 g_object_ref (contact));
556 g_string_free (str, TRUE);
560 status_icon_finalize (GObject *object)
562 EmpathyStatusIconPriv *priv = GET_PRIV (object);
564 if (priv->blink_timeout) {
565 g_source_remove (priv->blink_timeout);
568 empathy_disconnect_account_status_changed (priv->token);
569 g_slist_foreach (priv->events, (GFunc) status_icon_event_free, NULL);
570 g_slist_free (priv->events);
572 g_object_unref (priv->icon);
573 g_object_unref (priv->idle);
574 g_object_unref (priv->mc);
575 g_object_unref (priv->contact_manager);
579 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
581 GObjectClass *object_class = G_OBJECT_CLASS (klass);
583 object_class->finalize = status_icon_finalize;
585 g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
589 status_icon_status_changed_cb (MissionControl *mc,
590 TpConnectionStatus status,
592 TpConnectionStatusReason reason,
593 const gchar *unique_name,
594 EmpathyStatusIcon *icon)
596 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
598 guint connection_status = 1;
600 /* Check for a connected account */
601 accounts = mc_accounts_list_by_enabled (TRUE);
602 for (l = accounts; l; l = l->next) {
603 connection_status = mission_control_get_connection_status (priv->mc,
606 if (connection_status == 0) {
610 mc_accounts_list_free (accounts);
612 gtk_widget_set_sensitive (priv->message_item, connection_status == 0);
616 empathy_status_icon_init (EmpathyStatusIcon *icon)
618 EmpathyStatusIconPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (icon,
619 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv);
622 priv->icon = gtk_status_icon_new ();
623 priv->mc = empathy_mission_control_new ();
624 priv->idle = empathy_idle_new ();
625 priv->dispatcher = empathy_dispatcher_new ();
626 priv->contact_manager = empathy_contact_manager_new ();
627 priv->token = empathy_connect_to_account_status_changed (priv->mc,
628 G_CALLBACK (status_icon_status_changed_cb),
631 /* make icon listen and respond to MAIN_WINDOW_HIDDEN changes */
632 empathy_conf_notify_add (empathy_conf_get (),
633 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
634 status_icon_notify_visibility_cb,
637 status_icon_create_menu (icon);
638 status_icon_idle_notify_cb (icon);
640 g_signal_connect_swapped (priv->idle, "notify",
641 G_CALLBACK (status_icon_idle_notify_cb),
643 g_signal_connect (priv->dispatcher, "filter-channel",
644 G_CALLBACK (status_icon_filter_channel_cb),
646 g_signal_connect (priv->dispatcher, "filter-tube",
647 G_CALLBACK (status_icon_filter_tube_cb),
649 g_signal_connect (priv->contact_manager, "pendings-changed",
650 G_CALLBACK (status_icon_pendings_changed_cb),
652 g_signal_connect (priv->icon, "activate",
653 G_CALLBACK (status_icon_activate_cb),
655 g_signal_connect (priv->icon, "popup-menu",
656 G_CALLBACK (status_icon_popup_menu_cb),
661 empathy_status_icon_new (GtkWindow *window)
663 EmpathyStatusIconPriv *priv;
664 EmpathyStatusIcon *icon;
665 gboolean should_hide;
667 g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
669 icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
670 priv = GET_PRIV (icon);
672 priv->window = g_object_ref (window);
674 g_signal_connect (priv->window, "delete-event",
675 G_CALLBACK (status_icon_delete_event_cb),
678 empathy_conf_get_bool (empathy_conf_get (),
679 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
682 if (gtk_window_is_active (priv->window) == should_hide) {
683 status_icon_set_visibility (icon, !should_hide, FALSE);