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"
51 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
52 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv))
54 #define DEBUG_DOMAIN "StatusIcon"
56 /* Number of ms to wait when blinking */
57 #define BLINK_TIMEOUT 500
59 typedef struct _StatusIconEvent StatusIconEvent;
61 struct _EmpathyStatusIconPriv {
63 EmpathyContactManager *manager;
64 EmpathyFilter *text_filter;
68 gboolean showing_event_icon;
69 StatusIconEvent *flash_state_event;
73 GtkWidget *popup_menu;
74 GtkWidget *show_window_item;
75 GtkWidget *message_item;
76 GtkWidget *status_item;
79 typedef void (*EventActivatedFunc) (StatusIconEvent *event);
81 struct _StatusIconEvent {
84 EventActivatedFunc func;
89 static void empathy_status_icon_class_init (EmpathyStatusIconClass *klass);
90 static void empathy_status_icon_init (EmpathyStatusIcon *icon);
91 static void status_icon_finalize (GObject *object);
92 static void status_icon_filter_new_channel (EmpathyFilter *filter,
95 EmpathyStatusIcon *icon);
96 static void status_icon_message_received_cb (EmpathyTpChat *tp_chat,
97 EmpathyMessage *message,
98 EmpathyStatusIcon *icon);
99 static void status_icon_idle_notify_cb (EmpathyStatusIcon *icon);
100 static void status_icon_update_tooltip (EmpathyStatusIcon *icon);
101 static void status_icon_set_from_state (EmpathyStatusIcon *icon);
102 static void status_icon_set_visibility (EmpathyStatusIcon *icon,
104 static void status_icon_toggle_visibility (EmpathyStatusIcon *icon);
105 static void status_icon_activate_cb (GtkStatusIcon *status_icon,
106 EmpathyStatusIcon *icon);
107 static gboolean status_icon_delete_event_cb (GtkWidget *widget,
109 EmpathyStatusIcon *icon);
110 static void status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
113 EmpathyStatusIcon *icon);
114 static void status_icon_create_menu (EmpathyStatusIcon *icon);
115 static void status_icon_new_message_cb (GtkWidget *widget,
116 EmpathyStatusIcon *icon);
117 static void status_icon_quit_cb (GtkWidget *window,
118 EmpathyStatusIcon *icon);
119 static void status_icon_show_hide_window_cb (GtkWidget *widget,
120 EmpathyStatusIcon *icon);
121 static void status_icon_pendings_changed_cb (EmpathyContactManager *manager,
122 EmpathyContact *contact,
123 EmpathyContact *actor,
127 EmpathyStatusIcon *icon);
128 static void status_icon_event_subscribe_cb (StatusIconEvent *event);
129 static void status_icon_event_flash_state_cb (StatusIconEvent *event);
130 static void status_icon_event_msg_cb (StatusIconEvent *event);
131 static StatusIconEvent * status_icon_event_new (EmpathyStatusIcon *icon,
132 const gchar *icon_name,
133 const gchar *message);
134 static void status_icon_event_remove (EmpathyStatusIcon *icon,
135 StatusIconEvent *event);
136 static gboolean status_icon_event_timeout_cb (EmpathyStatusIcon *icon);
137 static void status_icon_event_free (StatusIconEvent *event);
139 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
142 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
144 GObjectClass *object_class = G_OBJECT_CLASS (klass);
146 object_class->finalize = status_icon_finalize;
148 g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
152 empathy_status_icon_init (EmpathyStatusIcon *icon)
154 EmpathyStatusIconPriv *priv;
157 priv = GET_PRIV (icon);
159 priv->icon = gtk_status_icon_new ();
160 priv->idle = empathy_idle_new ();
161 empathy_idle_set_auto_away (priv->idle, TRUE);
162 empathy_idle_set_auto_disconnect (priv->idle, TRUE);
163 priv->manager = empathy_contact_manager_new ();
164 priv->mc = empathy_mission_control_new ();
165 priv->text_filter = empathy_filter_new ("org.gnome.Empathy.ChatFilter",
166 "/org/gnome/Empathy/ChatFilter",
167 TP_IFACE_CHANNEL_TYPE_TEXT,
168 MC_FILTER_PRIORITY_DIALOG,
169 MC_FILTER_FLAG_INCOMING);
171 status_icon_create_menu (icon);
172 status_icon_idle_notify_cb (icon);
174 g_signal_connect (priv->text_filter, "new-channel",
175 G_CALLBACK (status_icon_filter_new_channel),
177 g_signal_connect_swapped (priv->idle, "notify",
178 G_CALLBACK (status_icon_idle_notify_cb),
180 g_signal_connect (priv->icon, "activate",
181 G_CALLBACK (status_icon_activate_cb),
183 g_signal_connect (priv->icon, "popup-menu",
184 G_CALLBACK (status_icon_popup_menu_cb),
186 g_signal_connect (priv->manager, "pendings-changed",
187 G_CALLBACK (status_icon_pendings_changed_cb),
190 pendings = empathy_contact_list_get_pendings (EMPATHY_CONTACT_LIST (priv->manager));
191 for (l = pendings; l; l = l->next) {
192 EmpathyPendingInfo *info;
195 status_icon_pendings_changed_cb (priv->manager,
202 empathy_pending_info_free (info);
204 g_list_free (pendings);
208 status_icon_finalize (GObject *object)
210 EmpathyStatusIconPriv *priv;
212 priv = GET_PRIV (object);
214 g_list_foreach (priv->events, (GFunc) status_icon_event_free, NULL);
215 g_list_free (priv->events);
217 if (priv->blink_timeout) {
218 g_source_remove (priv->blink_timeout);
221 g_object_unref (priv->icon);
222 g_object_unref (priv->window);
223 g_object_unref (priv->idle);
224 g_object_unref (priv->manager);
225 g_object_unref (priv->mc);
229 empathy_status_icon_new (GtkWindow *window)
231 EmpathyStatusIconPriv *priv;
232 EmpathyStatusIcon *icon;
233 gboolean should_hide;
235 g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
237 icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
238 priv = GET_PRIV (icon);
240 priv->window = g_object_ref (window);
242 g_signal_connect (priv->window, "delete-event",
243 G_CALLBACK (status_icon_delete_event_cb),
246 empathy_conf_get_bool (empathy_conf_get (),
247 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
250 if (gtk_window_is_active (priv->window) == should_hide) {
251 status_icon_set_visibility (icon, !should_hide);
258 status_icon_filter_new_channel (EmpathyFilter *filter,
261 EmpathyStatusIcon *icon)
263 EmpathyStatusIconPriv *priv;
265 EmpathyTpChat *tp_chat;
268 priv = GET_PRIV (icon);
270 account = mission_control_get_account_for_connection (priv->mc, tp_conn, NULL);
272 empathy_debug (DEBUG_DOMAIN, "New text channel to be filtered for contact %s",
273 empathy_inspect_channel (account, tp_chan));
275 tp_chat = empathy_tp_chat_new (account, tp_chan);
276 g_object_set_data (G_OBJECT (tp_chat), "filter", filter);
277 g_object_unref (account);
279 messages = empathy_tp_chat_get_pendings (tp_chat);
281 empathy_debug (DEBUG_DOMAIN, "No pending msg, waiting...");
282 g_signal_connect (tp_chat, "message-received",
283 G_CALLBACK (status_icon_message_received_cb),
288 status_icon_message_received_cb (tp_chat, messages->data, icon);
290 g_list_foreach (messages, (GFunc) g_object_unref, NULL);
291 g_list_free (messages);
295 status_icon_message_received_cb (EmpathyTpChat *tp_chat,
296 EmpathyMessage *message,
297 EmpathyStatusIcon *icon)
299 EmpathyContact *sender;
301 StatusIconEvent *event;
303 empathy_debug (DEBUG_DOMAIN, "Message received, add event");
305 g_signal_handlers_disconnect_by_func (tp_chat,
306 status_icon_message_received_cb,
309 sender = empathy_message_get_sender (message);
310 msg = g_strdup_printf (_("New message from %s:\n%s"),
311 empathy_contact_get_name (sender),
312 empathy_message_get_body (message));
314 event = status_icon_event_new (icon, EMPATHY_IMAGE_NEW_MESSAGE, msg);
315 event->func = status_icon_event_msg_cb;
316 event->user_data = tp_chat;
320 status_icon_idle_notify_cb (EmpathyStatusIcon *icon)
322 EmpathyStatusIconPriv *priv;
323 McPresence flash_state;
325 priv = GET_PRIV (icon);
327 flash_state = empathy_idle_get_flash_state (priv->idle);
328 if (flash_state != MC_PRESENCE_UNSET) {
329 const gchar *icon_name;
331 icon_name = empathy_icon_name_for_presence_state (flash_state);
332 if (!priv->flash_state_event) {
333 /* We are now flashing */
334 priv->flash_state_event = status_icon_event_new (icon, icon_name, NULL);
335 priv->flash_state_event->user_data = icon;
336 priv->flash_state_event->func = status_icon_event_flash_state_cb;
338 /* We are still flashing but with another state */
339 g_free (priv->flash_state_event->icon_name);
340 priv->flash_state_event->icon_name = g_strdup (icon_name);
343 else if (priv->flash_state_event) {
344 /* We are no more flashing */
345 status_icon_event_remove (icon, priv->flash_state_event);
346 priv->flash_state_event = NULL;
349 if (!priv->showing_event_icon) {
350 status_icon_set_from_state (icon);
353 status_icon_update_tooltip (icon);
357 status_icon_update_tooltip (EmpathyStatusIcon *icon)
359 EmpathyStatusIconPriv *priv;
360 const gchar *tooltip = NULL;
362 priv = GET_PRIV (icon);
365 StatusIconEvent *event;
367 event = priv->events->data;
368 tooltip = event->message;
372 tooltip = empathy_idle_get_status (priv->idle);
375 gtk_status_icon_set_tooltip (priv->icon, tooltip);
379 status_icon_set_from_state (EmpathyStatusIcon *icon)
381 EmpathyStatusIconPriv *priv;
383 const gchar *icon_name;
385 priv = GET_PRIV (icon);
387 state = empathy_idle_get_state (priv->idle);
388 icon_name = empathy_icon_name_for_presence_state (state);
389 gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
393 status_icon_set_visibility (EmpathyStatusIcon *icon,
396 EmpathyStatusIconPriv *priv;
398 priv = GET_PRIV (icon);
401 empathy_window_iconify (priv->window, priv->icon);
402 empathy_conf_set_bool (empathy_conf_get (),
403 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, TRUE);
407 empathy_window_present (GTK_WINDOW (priv->window), TRUE);
408 empathy_conf_set_bool (empathy_conf_get (),
409 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, FALSE);
411 /* Show the accounts dialog if there is no enabled accounts */
412 accounts = mc_accounts_list_by_enabled (TRUE);
414 mc_accounts_list_free (accounts);
416 empathy_debug (DEBUG_DOMAIN,
417 "No enabled account, Showing account dialog");
418 empathy_accounts_dialog_show (GTK_WINDOW (priv->window));
424 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
426 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
429 visible = gtk_window_is_active (priv->window);
430 status_icon_set_visibility (icon, !visible);
434 status_icon_activate_cb (GtkStatusIcon *status_icon,
435 EmpathyStatusIcon *icon)
437 EmpathyStatusIconPriv *priv;
439 priv = GET_PRIV (icon);
441 empathy_debug (DEBUG_DOMAIN, "Activated: %s",
442 priv->events ? "event" : "toggle");
445 status_icon_event_remove (icon, priv->events->data);
447 status_icon_toggle_visibility (icon);
452 status_icon_delete_event_cb (GtkWidget *widget,
454 EmpathyStatusIcon *icon)
456 status_icon_set_visibility (icon, FALSE);
462 status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
465 EmpathyStatusIcon *icon)
467 EmpathyStatusIconPriv *priv;
471 priv = GET_PRIV (icon);
473 show = empathy_window_get_is_visible (GTK_WINDOW (priv->window));
475 g_signal_handlers_block_by_func (priv->show_window_item,
476 status_icon_show_hide_window_cb,
478 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (priv->show_window_item),
480 g_signal_handlers_unblock_by_func (priv->show_window_item,
481 status_icon_show_hide_window_cb,
484 submenu = empathy_presence_chooser_create_menu ();
485 gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->status_item),
488 gtk_menu_popup (GTK_MENU (priv->popup_menu),
490 gtk_status_icon_position_menu,
497 status_icon_create_menu (EmpathyStatusIcon *icon)
499 EmpathyStatusIconPriv *priv;
502 priv = GET_PRIV (icon);
504 glade = empathy_glade_get_file ("empathy-status-icon.glade",
507 "tray_menu", &priv->popup_menu,
508 "tray_show_list", &priv->show_window_item,
509 "tray_new_message", &priv->message_item,
510 "tray_status", &priv->status_item,
513 empathy_glade_connect (glade,
515 "tray_show_list", "toggled", status_icon_show_hide_window_cb,
516 "tray_new_message", "activate", status_icon_new_message_cb,
517 "tray_quit", "activate", status_icon_quit_cb,
520 g_object_unref (glade);
524 status_icon_new_message_cb (GtkWidget *widget,
525 EmpathyStatusIcon *icon)
527 EmpathyStatusIconPriv *priv;
529 priv = GET_PRIV (icon);
531 //empathy_new_message_dialog_show (GTK_WINDOW (priv->window));
535 status_icon_quit_cb (GtkWidget *window,
536 EmpathyStatusIcon *icon)
542 status_icon_show_hide_window_cb (GtkWidget *widget,
543 EmpathyStatusIcon *icon)
547 visible = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget));
548 status_icon_set_visibility (icon, visible);
552 status_icon_pendings_changed_cb (EmpathyContactManager *manager,
553 EmpathyContact *contact,
554 EmpathyContact *actor,
558 EmpathyStatusIcon *icon)
560 EmpathyStatusIconPriv *priv;
561 StatusIconEvent *event;
565 priv = GET_PRIV (icon);
568 /* FIXME: We should remove the event */
572 for (l = priv->events; l; l = l->next) {
573 if (empathy_contact_equal (contact, ((StatusIconEvent*)l->data)->user_data)) {
578 str = g_string_new (NULL);
579 g_string_printf (str, _("Subscription requested by %s"),
580 empathy_contact_get_name (contact));
581 if (!G_STR_EMPTY (message)) {
582 g_string_append_printf (str, _("\nMessage: %s"), message);
585 event = status_icon_event_new (icon, GTK_STOCK_DIALOG_QUESTION, str->str);
586 event->user_data = g_object_ref (contact);
587 event->func = status_icon_event_subscribe_cb;
589 g_string_free (str, TRUE);
593 status_icon_event_subscribe_cb (StatusIconEvent *event)
595 EmpathyContact *contact;
597 contact = EMPATHY_CONTACT (event->user_data);
599 empathy_subscription_dialog_show (contact, NULL);
601 g_object_unref (contact);
605 status_icon_event_flash_state_cb (StatusIconEvent *event)
607 EmpathyStatusIconPriv *priv;
609 priv = GET_PRIV (event->user_data);
611 empathy_idle_set_flash_state (priv->idle, MC_PRESENCE_UNSET);
615 status_icon_event_msg_cb (StatusIconEvent *event)
617 EmpathyFilter *filter;
618 EmpathyTpChat *tp_chat;
620 empathy_debug (DEBUG_DOMAIN, "Dispatching text channel");
622 tp_chat = event->user_data;
623 filter = g_object_get_data (G_OBJECT (tp_chat), "filter");
624 empathy_filter_process (filter,
625 empathy_tp_chat_get_channel (tp_chat),
627 g_object_unref (tp_chat);
630 static StatusIconEvent *
631 status_icon_event_new (EmpathyStatusIcon *icon,
632 const gchar *icon_name,
633 const gchar *message)
635 EmpathyStatusIconPriv *priv;
636 StatusIconEvent *event;
638 priv = GET_PRIV (icon);
640 event = g_slice_new0 (StatusIconEvent);
641 event->icon_name = g_strdup (icon_name);
642 event->message = g_strdup (message);
644 priv->events = g_list_append (priv->events, event);
645 if (!priv->blink_timeout) {
646 priv->showing_event_icon = FALSE;
647 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
648 (GSourceFunc) status_icon_event_timeout_cb,
650 status_icon_event_timeout_cb (icon);
651 status_icon_update_tooltip (icon);
658 status_icon_event_remove (EmpathyStatusIcon *icon,
659 StatusIconEvent *event)
661 EmpathyStatusIconPriv *priv;
663 priv = GET_PRIV (icon);
668 priv->events = g_list_remove (priv->events, event);
669 status_icon_event_free (event);
670 priv->showing_event_icon = FALSE;
671 status_icon_update_tooltip (icon);
672 status_icon_set_from_state (icon);
678 if (priv->blink_timeout) {
679 g_source_remove (priv->blink_timeout);
680 priv->blink_timeout = 0;
685 status_icon_event_timeout_cb (EmpathyStatusIcon *icon)
687 EmpathyStatusIconPriv *priv;
689 priv = GET_PRIV (icon);
691 priv->showing_event_icon = !priv->showing_event_icon;
693 if (!priv->showing_event_icon) {
694 status_icon_set_from_state (icon);
696 StatusIconEvent *event;
698 event = priv->events->data;
699 gtk_status_icon_set_from_icon_name (priv->icon, event->icon_name);
706 status_icon_event_free (StatusIconEvent *event)
708 g_free (event->icon_name);
709 g_free (event->message);
710 g_slice_free (StatusIconEvent, event);