1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Collabora Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program 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 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
28 #include <glade/glade.h>
29 #include <glib/gi18n.h>
31 #include <libmissioncontrol/mission-control.h>
33 #include <libempathy/empathy-contact-list.h>
34 #include <libempathy/empathy-contact-manager.h>
35 #include <libempathy/empathy-contact.h>
36 #include <libempathy/empathy-tp-chat.h>
37 #include <libempathy/empathy-debug.h>
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy/empathy-conf.h>
40 #include <libempathy/empathy-idle.h>
41 #include <libempathy/empathy-filter.h>
43 #include "empathy-status-icon.h"
44 #include "empathy-contact-dialogs.h"
45 #include "empathy-presence-chooser.h"
46 #include "empathy-preferences.h"
47 #include "empathy-ui-utils.h"
48 #include "empathy-accounts-dialog.h"
49 #include "empathy-images.h"
52 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
53 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv))
55 #define DEBUG_DOMAIN "StatusIcon"
57 /* Number of ms to wait when blinking */
58 #define BLINK_TIMEOUT 500
60 typedef struct _StatusIconEvent StatusIconEvent;
62 struct _EmpathyStatusIconPriv {
64 EmpathyContactManager *manager;
65 EmpathyFilter *text_filter;
69 gboolean showing_event_icon;
70 StatusIconEvent *flash_state_event;
74 GtkWidget *popup_menu;
75 GtkWidget *show_window_item;
76 GtkWidget *message_item;
77 GtkWidget *status_item;
80 typedef void (*EventActivatedFunc) (StatusIconEvent *event);
82 struct _StatusIconEvent {
85 EventActivatedFunc func;
90 static void empathy_status_icon_class_init (EmpathyStatusIconClass *klass);
91 static void empathy_status_icon_init (EmpathyStatusIcon *icon);
92 static void status_icon_finalize (GObject *object);
93 static void status_icon_filter_new_channel (EmpathyFilter *filter,
96 EmpathyStatusIcon *icon);
97 static void status_icon_message_received_cb (EmpathyTpChat *tp_chat,
98 EmpathyMessage *message,
99 EmpathyStatusIcon *icon);
100 static void status_icon_idle_notify_cb (EmpathyStatusIcon *icon);
101 static void status_icon_update_tooltip (EmpathyStatusIcon *icon);
102 static void status_icon_set_from_state (EmpathyStatusIcon *icon);
103 static void status_icon_set_visibility (EmpathyStatusIcon *icon,
105 static void status_icon_toggle_visibility (EmpathyStatusIcon *icon);
106 static void status_icon_activate_cb (GtkStatusIcon *status_icon,
107 EmpathyStatusIcon *icon);
108 static gboolean status_icon_delete_event_cb (GtkWidget *widget,
110 EmpathyStatusIcon *icon);
111 static void status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
114 EmpathyStatusIcon *icon);
115 static void status_icon_create_menu (EmpathyStatusIcon *icon);
116 static void status_icon_new_message_cb (GtkWidget *widget,
117 EmpathyStatusIcon *icon);
118 static void status_icon_quit_cb (GtkWidget *window,
119 EmpathyStatusIcon *icon);
120 static void status_icon_show_hide_window_cb (GtkWidget *widget,
121 EmpathyStatusIcon *icon);
122 static void status_icon_pendings_changed_cb (EmpathyContactManager *manager,
123 EmpathyContact *contact,
124 EmpathyContact *actor,
128 EmpathyStatusIcon *icon);
129 static void status_icon_event_subscribe_cb (StatusIconEvent *event);
130 static void status_icon_event_flash_state_cb (StatusIconEvent *event);
131 static void status_icon_event_msg_cb (StatusIconEvent *event);
132 static StatusIconEvent * status_icon_event_new (EmpathyStatusIcon *icon,
133 const gchar *icon_name,
134 const gchar *message);
135 static void status_icon_event_remove (EmpathyStatusIcon *icon,
136 StatusIconEvent *event);
137 static gboolean status_icon_event_timeout_cb (EmpathyStatusIcon *icon);
138 static void status_icon_event_free (StatusIconEvent *event);
140 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
143 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
145 GObjectClass *object_class = G_OBJECT_CLASS (klass);
147 object_class->finalize = status_icon_finalize;
149 g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
153 empathy_status_icon_init (EmpathyStatusIcon *icon)
155 EmpathyStatusIconPriv *priv;
158 priv = GET_PRIV (icon);
160 priv->icon = gtk_status_icon_new ();
161 priv->idle = empathy_idle_new ();
162 empathy_idle_set_auto_away (priv->idle, TRUE);
163 empathy_idle_set_auto_disconnect (priv->idle, TRUE);
164 priv->manager = empathy_contact_manager_new ();
165 priv->mc = empathy_mission_control_new ();
166 priv->text_filter = empathy_filter_new ("org.gnome.Empathy.Chat",
167 "/org/freedesktop/Telepathy/Filter",
168 TP_IFACE_CHANNEL_TYPE_TEXT,
169 MC_FILTER_PRIORITY_DIALOG,
170 MC_FILTER_FLAG_INCOMING);
172 status_icon_create_menu (icon);
173 status_icon_idle_notify_cb (icon);
175 g_signal_connect (priv->text_filter, "new-channel",
176 G_CALLBACK (status_icon_filter_new_channel),
178 g_signal_connect_swapped (priv->idle, "notify",
179 G_CALLBACK (status_icon_idle_notify_cb),
181 g_signal_connect (priv->icon, "activate",
182 G_CALLBACK (status_icon_activate_cb),
184 g_signal_connect (priv->icon, "popup-menu",
185 G_CALLBACK (status_icon_popup_menu_cb),
187 g_signal_connect (priv->manager, "pendings-changed",
188 G_CALLBACK (status_icon_pendings_changed_cb),
191 pendings = empathy_contact_list_get_pendings (EMPATHY_CONTACT_LIST (priv->manager));
192 for (l = pendings; l; l = l->next) {
193 EmpathyPendingInfo *info;
196 status_icon_pendings_changed_cb (priv->manager,
203 empathy_pending_info_free (info);
205 g_list_free (pendings);
209 status_icon_finalize (GObject *object)
211 EmpathyStatusIconPriv *priv;
213 priv = GET_PRIV (object);
215 g_list_foreach (priv->events, (GFunc) status_icon_event_free, NULL);
216 g_list_free (priv->events);
218 if (priv->blink_timeout) {
219 g_source_remove (priv->blink_timeout);
222 g_object_unref (priv->icon);
223 g_object_unref (priv->window);
224 g_object_unref (priv->idle);
225 g_object_unref (priv->manager);
226 g_object_unref (priv->mc);
230 empathy_status_icon_new (GtkWindow *window)
232 EmpathyStatusIconPriv *priv;
233 EmpathyStatusIcon *icon;
234 gboolean should_hide;
236 g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
238 icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
239 priv = GET_PRIV (icon);
241 priv->window = g_object_ref (window);
243 g_signal_connect (priv->window, "delete-event",
244 G_CALLBACK (status_icon_delete_event_cb),
247 empathy_conf_get_bool (empathy_conf_get (),
248 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
251 status_icon_set_visibility (icon, !should_hide);
257 status_icon_filter_new_channel (EmpathyFilter *filter,
260 EmpathyStatusIcon *icon)
262 EmpathyStatusIconPriv *priv;
264 EmpathyTpChat *tp_chat;
267 priv = GET_PRIV (icon);
269 account = mission_control_get_account_for_connection (priv->mc, tp_conn, NULL);
271 empathy_debug (DEBUG_DOMAIN, "New text channel to be filtered for contact %s",
272 empathy_inspect_channel (account, tp_chan));
274 tp_chat = empathy_tp_chat_new (account, tp_chan);
275 g_object_set_data (G_OBJECT (tp_chat), "filter", filter);
276 g_object_unref (account);
278 messages = empathy_tp_chat_get_pendings (tp_chat);
280 empathy_debug (DEBUG_DOMAIN, "No pending msg, waiting...");
281 g_signal_connect (tp_chat, "message-received",
282 G_CALLBACK (status_icon_message_received_cb),
287 status_icon_message_received_cb (tp_chat, messages->data, icon);
289 g_list_foreach (messages, (GFunc) g_object_unref, NULL);
290 g_list_free (messages);
294 status_icon_message_received_cb (EmpathyTpChat *tp_chat,
295 EmpathyMessage *message,
296 EmpathyStatusIcon *icon)
298 EmpathyContact *sender;
300 StatusIconEvent *event;
302 empathy_debug (DEBUG_DOMAIN, "Message received, add event");
304 g_signal_handlers_disconnect_by_func (tp_chat,
305 status_icon_message_received_cb,
308 sender = empathy_message_get_sender (message);
309 msg = g_strdup_printf (_("New message from %s:\n%s"),
310 empathy_contact_get_name (sender),
311 empathy_message_get_body (message));
313 event = status_icon_event_new (icon, EMPATHY_IMAGE_NEW_MESSAGE, msg);
314 event->func = status_icon_event_msg_cb;
315 event->user_data = tp_chat;
319 status_icon_idle_notify_cb (EmpathyStatusIcon *icon)
321 EmpathyStatusIconPriv *priv;
322 McPresence flash_state;
324 priv = GET_PRIV (icon);
326 flash_state = empathy_idle_get_flash_state (priv->idle);
327 if (flash_state != MC_PRESENCE_UNSET) {
328 const gchar *icon_name;
330 icon_name = empathy_icon_name_for_presence_state (flash_state);
331 if (!priv->flash_state_event) {
332 /* We are now flashing */
333 priv->flash_state_event = status_icon_event_new (icon, icon_name, NULL);
334 priv->flash_state_event->user_data = icon;
335 priv->flash_state_event->func = status_icon_event_flash_state_cb;
337 /* We are still flashing but with another state */
338 g_free (priv->flash_state_event->icon_name);
339 priv->flash_state_event->icon_name = g_strdup (icon_name);
342 else if (priv->flash_state_event) {
343 /* We are no more flashing */
344 status_icon_event_remove (icon, priv->flash_state_event);
345 priv->flash_state_event = NULL;
348 if (!priv->showing_event_icon) {
349 status_icon_set_from_state (icon);
352 status_icon_update_tooltip (icon);
356 status_icon_update_tooltip (EmpathyStatusIcon *icon)
358 EmpathyStatusIconPriv *priv;
359 const gchar *tooltip = NULL;
361 priv = GET_PRIV (icon);
364 StatusIconEvent *event;
366 event = priv->events->data;
367 tooltip = event->message;
371 tooltip = empathy_idle_get_status (priv->idle);
374 gtk_status_icon_set_tooltip (priv->icon, tooltip);
378 status_icon_set_from_state (EmpathyStatusIcon *icon)
380 EmpathyStatusIconPriv *priv;
382 const gchar *icon_name;
384 priv = GET_PRIV (icon);
386 state = empathy_idle_get_state (priv->idle);
387 icon_name = empathy_icon_name_for_presence_state (state);
388 gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
392 status_icon_set_visibility (EmpathyStatusIcon *icon,
395 EmpathyStatusIconPriv *priv;
397 priv = GET_PRIV (icon);
400 empathy_window_iconify (priv->window, priv->icon);
401 empathy_conf_set_bool (empathy_conf_get (),
402 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, TRUE);
406 empathy_window_present (GTK_WINDOW (priv->window), TRUE);
407 empathy_conf_set_bool (empathy_conf_get (),
408 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, FALSE);
410 /* Show the accounts dialog if there is no enabled accounts */
411 accounts = mc_accounts_list_by_enabled (TRUE);
413 mc_accounts_list_free (accounts);
415 empathy_debug (DEBUG_DOMAIN,
416 "No enabled account, Showing account dialog");
417 empathy_accounts_dialog_show (GTK_WINDOW (priv->window));
423 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
425 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
428 visible = gtk_window_is_active (priv->window);
429 status_icon_set_visibility (icon, !visible);
433 status_icon_activate_cb (GtkStatusIcon *status_icon,
434 EmpathyStatusIcon *icon)
436 EmpathyStatusIconPriv *priv;
438 priv = GET_PRIV (icon);
440 empathy_debug (DEBUG_DOMAIN, "Activated: %s",
441 priv->events ? "event" : "toggle");
444 status_icon_event_remove (icon, priv->events->data);
446 status_icon_toggle_visibility (icon);
451 status_icon_delete_event_cb (GtkWidget *widget,
453 EmpathyStatusIcon *icon)
455 status_icon_set_visibility (icon, FALSE);
461 status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
464 EmpathyStatusIcon *icon)
466 EmpathyStatusIconPriv *priv;
470 priv = GET_PRIV (icon);
472 show = empathy_window_get_is_visible (GTK_WINDOW (priv->window));
474 g_signal_handlers_block_by_func (priv->show_window_item,
475 status_icon_show_hide_window_cb,
477 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (priv->show_window_item),
479 g_signal_handlers_unblock_by_func (priv->show_window_item,
480 status_icon_show_hide_window_cb,
483 submenu = empathy_presence_chooser_create_menu ();
484 gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->status_item),
487 gtk_menu_popup (GTK_MENU (priv->popup_menu),
489 gtk_status_icon_position_menu,
496 status_icon_create_menu (EmpathyStatusIcon *icon)
498 EmpathyStatusIconPriv *priv;
501 priv = GET_PRIV (icon);
503 glade = empathy_glade_get_file ("empathy-status-icon.glade",
506 "tray_menu", &priv->popup_menu,
507 "tray_show_list", &priv->show_window_item,
508 "tray_new_message", &priv->message_item,
509 "tray_status", &priv->status_item,
512 empathy_glade_connect (glade,
514 "tray_show_list", "toggled", status_icon_show_hide_window_cb,
515 "tray_new_message", "activate", status_icon_new_message_cb,
516 "tray_quit", "activate", status_icon_quit_cb,
519 g_object_unref (glade);
523 status_icon_new_message_cb (GtkWidget *widget,
524 EmpathyStatusIcon *icon)
526 EmpathyStatusIconPriv *priv;
528 priv = GET_PRIV (icon);
530 //empathy_new_message_dialog_show (GTK_WINDOW (priv->window));
534 status_icon_quit_cb (GtkWidget *window,
535 EmpathyStatusIcon *icon)
541 status_icon_show_hide_window_cb (GtkWidget *widget,
542 EmpathyStatusIcon *icon)
546 visible = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget));
547 status_icon_set_visibility (icon, visible);
551 status_icon_pendings_changed_cb (EmpathyContactManager *manager,
552 EmpathyContact *contact,
553 EmpathyContact *actor,
557 EmpathyStatusIcon *icon)
559 EmpathyStatusIconPriv *priv;
560 StatusIconEvent *event;
564 priv = GET_PRIV (icon);
567 /* FIXME: We should remove the event */
571 for (l = priv->events; l; l = l->next) {
572 if (empathy_contact_equal (contact, ((StatusIconEvent*)l->data)->user_data)) {
577 str = g_string_new (NULL);
578 g_string_printf (str, _("Subscription requested by %s"),
579 empathy_contact_get_name (contact));
580 if (!G_STR_EMPTY (message)) {
581 g_string_append_printf (str, _("\nMessage: %s"), message);
584 event = status_icon_event_new (icon, GTK_STOCK_DIALOG_QUESTION, str->str);
585 event->user_data = g_object_ref (contact);
586 event->func = status_icon_event_subscribe_cb;
588 g_string_free (str, TRUE);
592 status_icon_event_subscribe_cb (StatusIconEvent *event)
594 EmpathyContact *contact;
596 contact = EMPATHY_CONTACT (event->user_data);
598 empathy_subscription_dialog_show (contact, NULL);
600 g_object_unref (contact);
604 status_icon_event_flash_state_cb (StatusIconEvent *event)
606 EmpathyStatusIconPriv *priv;
608 priv = GET_PRIV (event->user_data);
610 empathy_idle_set_flash_state (priv->idle, MC_PRESENCE_UNSET);
614 status_icon_event_msg_cb (StatusIconEvent *event)
616 EmpathyFilter *filter;
617 EmpathyTpChat *tp_chat;
619 empathy_debug (DEBUG_DOMAIN, "Dispatching text channel");
621 tp_chat = event->user_data;
622 filter = g_object_get_data (G_OBJECT (tp_chat), "filter");
623 empathy_filter_process (filter,
624 empathy_tp_chat_get_channel (tp_chat),
626 g_object_unref (tp_chat);
629 static StatusIconEvent *
630 status_icon_event_new (EmpathyStatusIcon *icon,
631 const gchar *icon_name,
632 const gchar *message)
634 EmpathyStatusIconPriv *priv;
635 StatusIconEvent *event;
637 priv = GET_PRIV (icon);
639 event = g_slice_new0 (StatusIconEvent);
640 event->icon_name = g_strdup (icon_name);
641 event->message = g_strdup (message);
643 priv->events = g_list_append (priv->events, event);
644 if (!priv->blink_timeout) {
645 priv->showing_event_icon = FALSE;
646 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
647 (GSourceFunc) status_icon_event_timeout_cb,
649 status_icon_event_timeout_cb (icon);
650 status_icon_update_tooltip (icon);
657 status_icon_event_remove (EmpathyStatusIcon *icon,
658 StatusIconEvent *event)
660 EmpathyStatusIconPriv *priv;
662 priv = GET_PRIV (icon);
667 priv->events = g_list_remove (priv->events, event);
668 status_icon_event_free (event);
669 priv->showing_event_icon = FALSE;
670 status_icon_update_tooltip (icon);
671 status_icon_set_from_state (icon);
677 if (priv->blink_timeout) {
678 g_source_remove (priv->blink_timeout);
679 priv->blink_timeout = 0;
684 status_icon_event_timeout_cb (EmpathyStatusIcon *icon)
686 EmpathyStatusIconPriv *priv;
688 priv = GET_PRIV (icon);
690 priv->showing_event_icon = !priv->showing_event_icon;
692 if (!priv->showing_event_icon) {
693 status_icon_set_from_state (icon);
695 StatusIconEvent *event;
697 event = priv->events->data;
698 gtk_status_icon_set_from_icon_name (priv->icon, event->icon_name);
705 status_icon_event_free (StatusIconEvent *event)
707 g_free (event->icon_name);
708 g_free (event->message);
709 g_slice_free (StatusIconEvent, event);