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_message_received_cb (EmpathyTpChat *tp_chat,
373 EmpathyMessage *message,
374 EmpathyStatusIcon *icon)
376 EmpathyContact *sender;
380 sender = empathy_message_get_sender (message);
381 msg = g_strdup_printf (_("New message from %s:\n%s"),
382 empathy_contact_get_name (sender),
383 empathy_message_get_body (message));
385 channel = empathy_tp_chat_get_channel (tp_chat);
386 status_icon_event_add (icon, EMPATHY_IMAGE_NEW_MESSAGE, msg,
387 status_icon_channel_process,
388 g_object_ref (channel));
391 g_object_unref (tp_chat);
395 status_icon_filter_channel_cb (EmpathyDispatcher *dispatcher,
397 EmpathyStatusIcon *icon)
401 g_object_get (channel, "channel-type", &channel_type, NULL);
402 if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)) {
403 EmpathyTpChat *tp_chat;
405 tp_chat = empathy_tp_chat_new (channel);
406 g_signal_connect (tp_chat, "message-received",
407 G_CALLBACK (status_icon_chat_message_received_cb),
410 else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA)) {
411 EmpathyTpGroup *tp_group;
412 EmpathyContact *contact;
415 tp_group = empathy_tp_group_new (channel);
416 empathy_run_until_ready (tp_group);
417 empathy_tp_group_get_invitation (tp_group, &contact);
418 empathy_contact_run_until_ready (contact,
419 EMPATHY_CONTACT_READY_NAME,
422 msg = g_strdup_printf (_("Incoming call from %s"),
423 empathy_contact_get_name (contact));
425 status_icon_event_add (icon, EMPATHY_IMAGE_VOIP, msg,
426 status_icon_channel_process,
427 g_object_ref (channel));
430 g_object_unref (contact);
431 g_object_unref (tp_group);
434 g_free (channel_type);
438 status_icon_tube_process (EmpathyStatusIcon *icon,
441 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
442 EmpathyDispatcherTube *tube = (EmpathyDispatcherTube*) user_data;
444 if (tube->activatable) {
445 empathy_dispatcher_tube_process (priv->dispatcher, tube);
450 /* Tell the user that the tube can't be handled */
451 str = g_strdup_printf (_("%s offered you an invitation, but "
452 "you don't have the needed external "
453 "application to handle it."),
454 empathy_contact_get_name (tube->initiator));
456 dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
458 GTK_BUTTONS_OK, str);
459 gtk_window_set_title (GTK_WINDOW (dialog),
460 _("Invitation Error"));
463 gtk_widget_show (dialog);
464 g_signal_connect (dialog, "response",
465 G_CALLBACK (gtk_widget_destroy),
469 empathy_dispatcher_tube_unref (tube);
473 status_icon_filter_tube_cb (EmpathyDispatcher *dispatcher,
474 EmpathyDispatcherTube *tube,
475 EmpathyStatusIcon *icon)
477 const gchar *icon_name;
480 empathy_contact_run_until_ready (tube->initiator,
481 EMPATHY_CONTACT_READY_NAME, NULL);
483 if (tube->activatable) {
484 icon_name = GTK_STOCK_EXECUTE;
485 msg = g_strdup_printf (_("%s is offering you an invitation. An external "
486 "application will be started to handle it."),
487 empathy_contact_get_name (tube->initiator));
489 icon_name = GTK_STOCK_DIALOG_ERROR;
490 msg = g_strdup_printf (_("%s is offering you an invitation, but "
491 "you don't have the needed external "
492 "application to handle it."),
493 empathy_contact_get_name (tube->initiator));
496 status_icon_event_add (icon, icon_name, msg, status_icon_tube_process,
497 empathy_dispatcher_tube_ref (tube));
503 status_icon_pending_subscribe (EmpathyStatusIcon *icon,
506 EmpathyContact *contact = EMPATHY_CONTACT (user_data);
508 empathy_subscription_dialog_show (contact, NULL);
509 g_object_unref (contact);
513 status_icon_pendings_changed_cb (EmpathyContactList *list,
514 EmpathyContact *contact,
515 EmpathyContact *actor,
519 EmpathyStatusIcon *icon)
524 /* FIXME: remove event if any */
528 DEBUG ("New local pending contact");
530 empathy_contact_run_until_ready (contact,
531 EMPATHY_CONTACT_READY_NAME,
534 str = g_string_new (NULL);
535 g_string_printf (str, _("Subscription requested by %s"),
536 empathy_contact_get_name (contact));
537 if (!G_STR_EMPTY (message)) {
538 g_string_append_printf (str, _("\nMessage: %s"), message);
541 status_icon_event_add (icon, GTK_STOCK_DIALOG_QUESTION, str->str,
542 status_icon_pending_subscribe,
543 g_object_ref (contact));
545 g_string_free (str, TRUE);
549 status_icon_finalize (GObject *object)
551 EmpathyStatusIconPriv *priv = GET_PRIV (object);
553 if (priv->blink_timeout) {
554 g_source_remove (priv->blink_timeout);
557 empathy_disconnect_account_status_changed (priv->token);
558 g_slist_foreach (priv->events, (GFunc) status_icon_event_free, NULL);
559 g_slist_free (priv->events);
561 g_object_unref (priv->icon);
562 g_object_unref (priv->idle);
563 g_object_unref (priv->mc);
564 g_object_unref (priv->contact_manager);
568 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
570 GObjectClass *object_class = G_OBJECT_CLASS (klass);
572 object_class->finalize = status_icon_finalize;
574 g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
578 status_icon_status_changed_cb (MissionControl *mc,
579 TpConnectionStatus status,
581 TpConnectionStatusReason reason,
582 const gchar *unique_name,
583 EmpathyStatusIcon *icon)
585 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
587 guint connection_status = 1;
589 /* Check for a connected account */
590 accounts = mc_accounts_list_by_enabled (TRUE);
591 for (l = accounts; l; l = l->next) {
592 connection_status = mission_control_get_connection_status (priv->mc,
595 if (connection_status == 0) {
599 mc_accounts_list_free (accounts);
601 gtk_widget_set_sensitive (priv->message_item, connection_status == 0);
605 empathy_status_icon_init (EmpathyStatusIcon *icon)
607 EmpathyStatusIconPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (icon,
608 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv);
611 priv->icon = gtk_status_icon_new ();
612 priv->mc = empathy_mission_control_new ();
613 priv->idle = empathy_idle_new ();
614 priv->dispatcher = empathy_dispatcher_new ();
615 priv->contact_manager = empathy_contact_manager_new ();
616 priv->token = empathy_connect_to_account_status_changed (priv->mc,
617 G_CALLBACK (status_icon_status_changed_cb),
620 /* make icon listen and respond to MAIN_WINDOW_HIDDEN changes */
621 empathy_conf_notify_add (empathy_conf_get (),
622 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
623 status_icon_notify_visibility_cb,
626 status_icon_create_menu (icon);
627 status_icon_idle_notify_cb (icon);
629 g_signal_connect_swapped (priv->idle, "notify",
630 G_CALLBACK (status_icon_idle_notify_cb),
632 g_signal_connect (priv->dispatcher, "filter-channel",
633 G_CALLBACK (status_icon_filter_channel_cb),
635 g_signal_connect (priv->dispatcher, "filter-tube",
636 G_CALLBACK (status_icon_filter_tube_cb),
638 g_signal_connect (priv->contact_manager, "pendings-changed",
639 G_CALLBACK (status_icon_pendings_changed_cb),
641 g_signal_connect (priv->icon, "activate",
642 G_CALLBACK (status_icon_activate_cb),
644 g_signal_connect (priv->icon, "popup-menu",
645 G_CALLBACK (status_icon_popup_menu_cb),
650 empathy_status_icon_new (GtkWindow *window)
652 EmpathyStatusIconPriv *priv;
653 EmpathyStatusIcon *icon;
654 gboolean should_hide;
656 g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
658 icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
659 priv = GET_PRIV (icon);
661 priv->window = g_object_ref (window);
663 g_signal_connect (priv->window, "delete-event",
664 G_CALLBACK (status_icon_delete_event_cb),
667 empathy_conf_get_bool (empathy_conf_get (),
668 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
671 if (gtk_window_is_active (priv->window) == should_hide) {
672 status_icon_set_visibility (icon, !should_hide, FALSE);