]> git.0d.be Git - empathy.git/blob - src/empathy-status-icon.c
Fix some warnings
[empathy.git] / src / empathy-status-icon.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
18  * 
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <gtk/gtk.h>
27 #include <glade/glade.h>
28 #include <glib/gi18n.h>
29
30 #include <libmissioncontrol/mission-control.h>
31
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-idle.h>
39 #include <libempathy/empathy-filter.h>
40
41 #include <libempathy-gtk/empathy-contact-dialogs.h>
42 #include <libempathy-gtk/empathy-presence-chooser.h>
43 #include <libempathy-gtk/empathy-conf.h>
44 #include <libempathy-gtk/empathy-ui-utils.h>
45 #include <libempathy-gtk/empathy-accounts-dialog.h>
46 #include <libempathy-gtk/empathy-images.h>
47 #include <libempathy-gtk/empathy-new-message-dialog.h>
48
49 #include "empathy-status-icon.h"
50 #include "empathy-preferences.h"
51
52
53 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
54                        EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv))
55
56 #define DEBUG_DOMAIN "StatusIcon"
57
58 /* Number of ms to wait when blinking */
59 #define BLINK_TIMEOUT 500
60
61 typedef struct _StatusIconEvent StatusIconEvent;
62
63 struct _EmpathyStatusIconPriv {
64         GtkStatusIcon         *icon;
65         EmpathyContactManager *manager;
66         EmpathyFilter         *text_filter;
67         EmpathyFilter         *call_filter;
68         EmpathyIdle           *idle;
69         MissionControl        *mc;
70         GList                 *events;
71         gboolean               showing_event_icon;
72         StatusIconEvent       *flash_state_event;
73         guint                  blink_timeout;
74
75         GtkWindow             *window;
76         GtkWidget             *popup_menu;
77         GtkWidget             *show_window_item;
78         GtkWidget             *message_item;
79         GtkWidget             *status_item;
80 };
81
82 typedef void (*EventActivatedFunc) (StatusIconEvent *event);
83
84 struct _StatusIconEvent {
85         gchar              *icon_name;
86         gchar              *message;
87         EventActivatedFunc  func;
88         gpointer            user_data;
89 };
90
91
92 static void       empathy_status_icon_class_init  (EmpathyStatusIconClass *klass);
93 static void       empathy_status_icon_init        (EmpathyStatusIcon      *icon);
94 static void       status_icon_finalize            (GObject                *object);
95 static void       status_icon_text_filter_new_channel (EmpathyFilter      *filter,
96                                                    TpChannel              *channel,
97                                                    EmpathyStatusIcon      *icon);
98 static void       status_icon_call_filter_new_channel (EmpathyFilter      *filter,
99                                                    TpChannel              *channel,
100                                                    EmpathyStatusIcon      *icon);
101 static void       status_icon_message_received_cb (EmpathyTpChat          *tp_chat,
102                                                    EmpathyMessage         *message,
103                                                    EmpathyStatusIcon      *icon);
104 static void       status_icon_idle_notify_cb      (EmpathyStatusIcon      *icon);
105 static void       status_icon_update_tooltip      (EmpathyStatusIcon      *icon);
106 static void       status_icon_set_from_state      (EmpathyStatusIcon      *icon);
107 static void       status_icon_set_visibility      (EmpathyStatusIcon      *icon,
108                                                    gboolean                visible,
109                                                    gboolean                store);
110 static void       status_icon_toggle_visibility   (EmpathyStatusIcon      *icon);
111 static void       status_icon_activate_cb         (GtkStatusIcon          *status_icon,
112                                                    EmpathyStatusIcon      *icon);
113 static gboolean   status_icon_delete_event_cb     (GtkWidget              *widget,
114                                                    GdkEvent               *event,
115                                                    EmpathyStatusIcon      *icon);
116 static void       status_icon_popup_menu_cb       (GtkStatusIcon          *status_icon,
117                                                    guint                   button,
118                                                    guint                   activate_time,
119                                                    EmpathyStatusIcon      *icon);
120 static void       status_icon_create_menu         (EmpathyStatusIcon      *icon);
121 static void       status_icon_new_message_cb      (GtkWidget              *widget,
122                                                    EmpathyStatusIcon      *icon);
123 static void       status_icon_quit_cb             (GtkWidget              *window,
124                                                    EmpathyStatusIcon      *icon);
125 static void       status_icon_show_hide_window_cb (GtkWidget              *widget,
126                                                    EmpathyStatusIcon      *icon);
127 static void       status_icon_pendings_changed_cb (EmpathyContactManager  *manager,
128                                                    EmpathyContact         *contact,
129                                                    EmpathyContact         *actor,
130                                                    guint                   reason,
131                                                    gchar                  *message,
132                                                    gboolean                is_pending,
133                                                    EmpathyStatusIcon      *icon);
134 static void       status_icon_event_subscribe_cb  (StatusIconEvent        *event);
135 static void       status_icon_event_flash_state_cb (StatusIconEvent       *event);
136 static void       status_icon_event_msg_cb        (StatusIconEvent        *event);
137 static StatusIconEvent * status_icon_event_new    (EmpathyStatusIcon      *icon,
138                                                    const gchar            *icon_name,
139                                                    const gchar            *message);
140 static void       status_icon_event_remove        (EmpathyStatusIcon      *icon,
141                                                    StatusIconEvent        *event);
142 static gboolean   status_icon_event_timeout_cb    (EmpathyStatusIcon      *icon);
143 static void       status_icon_event_free          (StatusIconEvent        *event);
144
145 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
146
147 static void
148 status_icon_notify_visibility_cb (EmpathyConf *conf,
149                                   const gchar *key,
150                                   gpointer     user_data)
151 {
152         EmpathyStatusIcon *icon = user_data;
153         gboolean           hidden = FALSE;
154
155         if (empathy_conf_get_bool (conf, key, &hidden)) {
156                 status_icon_set_visibility (icon, !hidden, FALSE);
157         }
158 }
159
160 static void
161 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
162 {
163         GObjectClass *object_class = G_OBJECT_CLASS (klass);
164
165         object_class->finalize = status_icon_finalize;
166
167         g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
168 }
169
170 static void
171 empathy_status_icon_init (EmpathyStatusIcon *icon)
172 {
173         EmpathyStatusIconPriv *priv;
174         GList                 *pendings, *l;
175
176         priv = GET_PRIV (icon);
177
178         priv->icon = gtk_status_icon_new ();
179         priv->manager = empathy_contact_manager_new ();
180         priv->mc = empathy_mission_control_new ();
181         priv->text_filter = empathy_filter_new ("org.gnome.Empathy.ChatFilter",
182                                                 "/org/gnome/Empathy/ChatFilter",
183                                                 TP_IFACE_CHANNEL_TYPE_TEXT,
184                                                 MC_FILTER_PRIORITY_DIALOG,
185                                                 MC_FILTER_FLAG_INCOMING);
186         priv->call_filter = empathy_filter_new ("org.gnome.Empathy.CallFilter",
187                                                 "/org/gnome/Empathy/CallFilter",
188                                                 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
189                                                 MC_FILTER_PRIORITY_DIALOG,
190                                                 MC_FILTER_FLAG_INCOMING);
191
192         priv->idle = empathy_idle_new ();
193
194         /* make icon listen and respond to MAIN_WINDOW_HIDDEN changes */
195         empathy_conf_notify_add (empathy_conf_get (),
196                                  EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
197                                  status_icon_notify_visibility_cb,
198                                  icon);
199
200         status_icon_create_menu (icon);
201         status_icon_idle_notify_cb (icon);
202
203         g_signal_connect (priv->text_filter, "new-channel",
204                           G_CALLBACK (status_icon_text_filter_new_channel),
205                           icon);
206         g_signal_connect (priv->call_filter, "new-channel",
207                           G_CALLBACK (status_icon_call_filter_new_channel),
208                           icon);
209         g_signal_connect_swapped (priv->idle, "notify",
210                                   G_CALLBACK (status_icon_idle_notify_cb),
211                                   icon);
212         g_signal_connect (priv->icon, "activate",
213                           G_CALLBACK (status_icon_activate_cb),
214                           icon);
215         g_signal_connect (priv->icon, "popup-menu",
216                           G_CALLBACK (status_icon_popup_menu_cb),
217                           icon);
218         g_signal_connect (priv->manager, "pendings-changed",
219                           G_CALLBACK (status_icon_pendings_changed_cb),
220                           icon);
221
222         pendings = empathy_contact_list_get_pendings (EMPATHY_CONTACT_LIST (priv->manager));
223         for (l = pendings; l; l = l->next) {
224                 EmpathyPendingInfo *info;
225
226                 info = l->data;
227                 status_icon_pendings_changed_cb (priv->manager,
228                                                  info->member,
229                                                  info->actor,
230                                                  0,
231                                                  info->message,
232                                                  TRUE,
233                                                  icon);
234                 empathy_pending_info_free (info);
235         }
236         g_list_free (pendings);
237 }
238
239 static void
240 status_icon_finalize (GObject *object)
241 {
242         EmpathyStatusIconPriv *priv;
243
244         priv = GET_PRIV (object);
245
246         g_list_foreach (priv->events, (GFunc) status_icon_event_free, NULL);
247         g_list_free (priv->events);
248
249         if (priv->blink_timeout) {
250                 g_source_remove (priv->blink_timeout);
251         }
252
253         g_object_unref (priv->icon);
254         g_object_unref (priv->window);
255         g_object_unref (priv->idle);
256         g_object_unref (priv->manager);
257         g_object_unref (priv->mc);
258         g_object_unref (priv->text_filter);
259         g_object_unref (priv->call_filter);
260 }
261
262 EmpathyStatusIcon *
263 empathy_status_icon_new (GtkWindow *window)
264 {
265         EmpathyStatusIconPriv *priv;
266         EmpathyStatusIcon     *icon;
267         gboolean               should_hide;
268
269         g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
270
271         icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
272         priv = GET_PRIV (icon);
273
274         priv->window = g_object_ref (window);
275
276         g_signal_connect (priv->window, "delete-event",
277                           G_CALLBACK (status_icon_delete_event_cb),
278                           icon);
279
280         empathy_conf_get_bool (empathy_conf_get (),
281                               EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
282                               &should_hide);
283
284         if (gtk_window_is_active (priv->window) == should_hide) {
285                 status_icon_set_visibility (icon, !should_hide, FALSE);
286         }
287
288         return icon;
289 }
290
291 static void
292 status_icon_text_filter_new_channel (EmpathyFilter     *filter,
293                                      TpChannel         *channel,
294                                      EmpathyStatusIcon *icon)
295 {
296         EmpathyTpChat *tp_chat;
297
298         empathy_debug (DEBUG_DOMAIN, "New text channel to be filtered for contact %p",
299                        channel);
300
301         tp_chat = empathy_tp_chat_new (channel, FALSE);
302         g_object_set_data (G_OBJECT (tp_chat), "filter", filter);
303         g_object_set_data (G_OBJECT (tp_chat), "channel", channel);
304
305         g_signal_connect (tp_chat, "message-received",
306                           G_CALLBACK (status_icon_message_received_cb),
307                           icon);
308 }
309
310 static void
311 status_icon_message_received_cb (EmpathyTpChat     *tp_chat,
312                                  EmpathyMessage    *message,
313                                  EmpathyStatusIcon *icon)
314 {
315         EmpathyContact  *sender;
316         gchar           *msg;
317         StatusIconEvent *event;
318
319         empathy_debug (DEBUG_DOMAIN, "Message received, add event");
320
321         g_signal_handlers_disconnect_by_func (tp_chat,
322                                               status_icon_message_received_cb,
323                                               icon);
324
325         sender = empathy_message_get_sender (message);
326         msg = g_strdup_printf (_("New message from %s:\n%s"),
327                                empathy_contact_get_name (sender),
328                                empathy_message_get_body (message));
329
330         event = status_icon_event_new (icon, EMPATHY_IMAGE_NEW_MESSAGE, msg);
331         event->func = status_icon_event_msg_cb;
332         event->user_data = tp_chat;
333         g_free (msg);
334 }
335
336 static void
337 status_icon_event_call_cb (StatusIconEvent *event)
338 {
339         EmpathyTpGroup *group;
340         EmpathyFilter  *filter;
341         TpChannel      *channel;
342
343         empathy_debug (DEBUG_DOMAIN, "Dispatching call channel");
344
345         group = event->user_data;
346         filter = g_object_get_data (G_OBJECT (group), "filter");
347         channel = g_object_get_data (G_OBJECT (group), "channel");
348         empathy_filter_process (filter, channel, TRUE);
349         g_object_unref (group);
350 }
351
352 static void
353 status_icon_call_filter_new_channel (EmpathyFilter     *filter,
354                                      TpChannel         *channel,
355                                      EmpathyStatusIcon *icon)
356 {
357         EmpathyTpGroup     *group;
358         EmpathyPendingInfo *invitation;
359         EmpathyContact     *contact = NULL;
360
361         empathy_debug (DEBUG_DOMAIN, "New media channel to be filtered");
362
363         /* FIXME: We have to check if the user is member or local-pending to
364          * know if it's an incoming or outgoing call because of the way we
365          * request media channels MC can't know if it's incoming or outgoing */
366         group = empathy_tp_group_new (channel);
367         empathy_run_until_ready (group);
368
369         invitation = empathy_tp_group_get_invitation (group, &contact); 
370         if (!invitation) {
371                 empathy_debug (DEBUG_DOMAIN, "Process OUTGOING call channel");
372                 empathy_filter_process (filter, channel, TRUE);
373         } else {
374                 StatusIconEvent *event;
375                 gchar           *msg;
376
377                 /* We are local pending, it's an incoming call, we need to ask
378                  * the user if he wants to accept the call. */
379                 empathy_contact_run_until_ready (contact,
380                                                  EMPATHY_CONTACT_READY_NAME,
381                                                  NULL);
382
383                 empathy_debug (DEBUG_DOMAIN, "INCOMING call, add event");
384
385                 msg = g_strdup_printf (_("Incoming call from %s:\n%s"),
386                                        empathy_contact_get_name (contact),
387                                        invitation->message);
388
389                 g_object_set_data (G_OBJECT (group), "filter", filter);
390                 g_object_set_data (G_OBJECT (group), "channel", channel);
391                 event = status_icon_event_new (icon, EMPATHY_IMAGE_VOIP, msg);
392                 event->func = status_icon_event_call_cb;
393                 event->user_data = group;
394                 g_free (msg);
395         }
396
397         if (contact) {
398                 g_object_unref (contact);
399         }
400 }
401
402 static void
403 status_icon_idle_notify_cb (EmpathyStatusIcon *icon)
404 {
405         EmpathyStatusIconPriv *priv;
406         McPresence             flash_state;
407
408         priv = GET_PRIV (icon);
409
410         flash_state = empathy_idle_get_flash_state (priv->idle);
411         if (flash_state != MC_PRESENCE_UNSET) {
412                 const gchar *icon_name;
413
414                 icon_name = empathy_icon_name_for_presence (flash_state);
415                 if (!priv->flash_state_event) {
416                         /* We are now flashing */
417                         priv->flash_state_event = status_icon_event_new (icon, icon_name, NULL);
418                         priv->flash_state_event->user_data = icon;
419                         priv->flash_state_event->func = status_icon_event_flash_state_cb;
420                 } else {
421                         /* We are still flashing but with another state */
422                         g_free (priv->flash_state_event->icon_name);
423                         priv->flash_state_event->icon_name = g_strdup (icon_name);
424                 }
425         }
426         else if (priv->flash_state_event) {
427                 /* We are no more flashing */
428                 status_icon_event_remove (icon, priv->flash_state_event);
429                 priv->flash_state_event = NULL;
430         }
431
432         if (!priv->showing_event_icon) {
433                 status_icon_set_from_state (icon);
434         }
435
436         status_icon_update_tooltip (icon);
437 }
438
439 static void
440 status_icon_update_tooltip (EmpathyStatusIcon *icon)
441 {
442         EmpathyStatusIconPriv *priv;
443         const gchar           *tooltip = NULL;
444
445         priv = GET_PRIV (icon);
446
447         if (priv->events) {
448                 StatusIconEvent *event;
449
450                 event = priv->events->data;
451                 tooltip = event->message;
452         }
453
454         if (!tooltip) {
455                 tooltip = empathy_idle_get_status (priv->idle);
456         }
457
458         gtk_status_icon_set_tooltip (priv->icon, tooltip);      
459 }
460
461 static void
462 status_icon_set_from_state (EmpathyStatusIcon *icon)
463 {
464         EmpathyStatusIconPriv *priv;
465         McPresence             state;
466         const gchar           *icon_name;
467
468         priv = GET_PRIV (icon);
469
470         state = empathy_idle_get_state (priv->idle);
471         icon_name = empathy_icon_name_for_presence (state);
472         gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
473 }
474
475 static void
476 status_icon_set_visibility (EmpathyStatusIcon *icon,
477                             gboolean           visible,
478                             gboolean           store)
479 {
480         EmpathyStatusIconPriv *priv;
481
482         priv = GET_PRIV (icon);
483
484         if (store) {
485                 empathy_conf_set_bool (empathy_conf_get (),
486                                        EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, !visible);
487         }
488
489         if (!visible) {
490                 empathy_window_iconify (priv->window, priv->icon);
491         } else {
492                 GList *accounts;
493
494                 empathy_window_present (GTK_WINDOW (priv->window), TRUE);
495         
496                 /* Show the accounts dialog if there is no enabled accounts */
497                 accounts = mc_accounts_list_by_enabled (TRUE);
498                 if (accounts) {
499                         mc_accounts_list_free (accounts);
500                 } else {
501                         empathy_debug (DEBUG_DOMAIN,
502                                       "No enabled account, Showing account dialog");
503                         empathy_accounts_dialog_show (GTK_WINDOW (priv->window));
504                 }
505         }
506 }
507
508 static void
509 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
510 {
511         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
512         gboolean               visible;
513
514         visible = gtk_window_is_active (priv->window);
515         status_icon_set_visibility (icon, !visible, TRUE);
516 }
517
518 static void
519 status_icon_activate_cb (GtkStatusIcon     *status_icon,
520                          EmpathyStatusIcon *icon)
521 {
522         EmpathyStatusIconPriv *priv;
523
524         priv = GET_PRIV (icon);
525
526         empathy_debug (DEBUG_DOMAIN, "Activated: %s",
527                        priv->events ? "event" : "toggle");
528
529         if (priv->events) {
530                 status_icon_event_remove (icon, priv->events->data);
531         } else {
532                 status_icon_toggle_visibility (icon);
533         }
534 }
535
536 static gboolean
537 status_icon_delete_event_cb (GtkWidget         *widget,
538                              GdkEvent          *event,
539                              EmpathyStatusIcon *icon)
540 {
541         status_icon_set_visibility (icon, FALSE, TRUE);
542
543         return TRUE;
544 }
545
546 static void
547 status_icon_popup_menu_cb (GtkStatusIcon     *status_icon,
548                            guint              button,
549                            guint              activate_time,
550                            EmpathyStatusIcon *icon)
551 {
552         EmpathyStatusIconPriv *priv;
553         GtkWidget             *submenu;
554         gboolean               show;
555
556         priv = GET_PRIV (icon);
557
558         show = empathy_window_get_is_visible (GTK_WINDOW (priv->window));
559
560         g_signal_handlers_block_by_func (priv->show_window_item,
561                                          status_icon_show_hide_window_cb,
562                                          icon);
563         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (priv->show_window_item),
564                                         show);
565         g_signal_handlers_unblock_by_func (priv->show_window_item,
566                                            status_icon_show_hide_window_cb,
567                                            icon);
568
569         submenu = empathy_presence_chooser_create_menu ();
570         gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->status_item),
571                                    submenu);
572
573         gtk_menu_popup (GTK_MENU (priv->popup_menu),
574                         NULL, NULL,
575                         gtk_status_icon_position_menu,
576                         priv->icon,
577                         button,
578                         activate_time);
579 }
580
581 static void
582 status_icon_create_menu (EmpathyStatusIcon *icon)
583 {
584         EmpathyStatusIconPriv *priv;
585         GladeXML              *glade;
586         gchar                 *filename;
587
588         priv = GET_PRIV (icon);
589
590         filename = empathy_file_lookup ("empathy-status-icon.glade", "src");
591         glade = empathy_glade_get_file (filename,
592                                        "tray_menu",
593                                        NULL,
594                                        "tray_menu", &priv->popup_menu,
595                                        "tray_show_list", &priv->show_window_item,
596                                        "tray_new_message", &priv->message_item,
597                                        "tray_status", &priv->status_item,
598                                        NULL);
599         g_free (filename);
600
601         empathy_glade_connect (glade,
602                               icon,
603                               "tray_show_list", "toggled", status_icon_show_hide_window_cb,
604                               "tray_new_message", "activate", status_icon_new_message_cb,
605                               "tray_quit", "activate", status_icon_quit_cb,
606                               NULL);
607
608         g_object_unref (glade);
609 }
610
611 static void
612 status_icon_new_message_cb (GtkWidget         *widget,
613                             EmpathyStatusIcon *icon)
614 {
615         EmpathyStatusIconPriv *priv;
616
617         priv = GET_PRIV (icon);
618
619         empathy_new_message_dialog_show (NULL);
620 }
621
622 static void
623 status_icon_quit_cb (GtkWidget         *window,
624                      EmpathyStatusIcon *icon)
625 {
626         gtk_main_quit ();
627 }
628
629 static void
630 status_icon_show_hide_window_cb (GtkWidget         *widget,
631                                  EmpathyStatusIcon *icon)
632 {
633         gboolean visible;
634
635         visible = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget));
636         status_icon_set_visibility (icon, visible, TRUE);
637 }
638
639 static void
640 status_icon_pendings_changed_cb (EmpathyContactManager *manager,
641                                  EmpathyContact        *contact,
642                                  EmpathyContact        *actor,
643                                  guint                  reason,
644                                  gchar                 *message,
645                                  gboolean               is_pending,
646                                  EmpathyStatusIcon     *icon)
647 {
648         EmpathyStatusIconPriv *priv;
649         StatusIconEvent       *event;
650         GString               *str;
651
652         priv = GET_PRIV (icon);
653
654         if (!is_pending) {
655                 /* FIXME: We should remove the event */
656                 return;
657         }
658
659         empathy_contact_run_until_ready (contact,
660                                          EMPATHY_CONTACT_READY_NAME,
661                                          NULL);
662
663         str = g_string_new (NULL);
664         g_string_printf (str, _("Subscription requested by %s"),
665                          empathy_contact_get_name (contact));   
666         if (!G_STR_EMPTY (message)) {
667                 g_string_append_printf (str, _("\nMessage: %s"), message);
668         }
669
670         event = status_icon_event_new (icon, GTK_STOCK_DIALOG_QUESTION, str->str);
671         event->user_data = g_object_ref (contact);
672         event->func = status_icon_event_subscribe_cb;
673
674         g_string_free (str, TRUE);
675 }
676
677 static void
678 status_icon_event_subscribe_cb (StatusIconEvent *event)
679 {
680         EmpathyContact *contact;
681
682         contact = EMPATHY_CONTACT (event->user_data);
683
684         empathy_subscription_dialog_show (contact, NULL);
685
686         g_object_unref (contact);
687 }
688
689 static void
690 status_icon_event_flash_state_cb (StatusIconEvent *event)
691 {
692         EmpathyStatusIconPriv *priv;
693
694         priv = GET_PRIV (event->user_data);
695
696         empathy_idle_set_flash_state (priv->idle, MC_PRESENCE_UNSET);
697 }
698
699 static void
700 status_icon_event_msg_cb (StatusIconEvent *event)
701 {
702         EmpathyFilter *filter;
703         EmpathyTpChat *tp_chat;
704         TpChannel     *channel;
705
706         empathy_debug (DEBUG_DOMAIN, "Dispatching text channel");
707
708         tp_chat = event->user_data;
709         filter = g_object_get_data (G_OBJECT (tp_chat), "filter");
710         channel = g_object_get_data (G_OBJECT (tp_chat), "channel");
711         empathy_filter_process (filter, channel, TRUE);
712
713         g_object_unref (tp_chat);
714 }
715
716 static StatusIconEvent *
717 status_icon_event_new (EmpathyStatusIcon *icon,
718                        const gchar       *icon_name,
719                        const gchar       *message)
720 {
721         EmpathyStatusIconPriv *priv;
722         StatusIconEvent       *event;
723
724         priv = GET_PRIV (icon);
725
726         event = g_slice_new0 (StatusIconEvent);
727         event->icon_name = g_strdup (icon_name);        
728         event->message = g_strdup (message);
729
730         priv->events = g_list_append (priv->events, event);
731         if (!priv->blink_timeout) {
732                 priv->showing_event_icon = FALSE;
733                 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
734                                                      (GSourceFunc) status_icon_event_timeout_cb,
735                                                      icon);
736                 status_icon_event_timeout_cb (icon);
737                 status_icon_update_tooltip (icon);
738         }
739
740         return event;
741 }
742
743 static void
744 status_icon_event_remove (EmpathyStatusIcon *icon,
745                           StatusIconEvent   *event)
746 {
747         EmpathyStatusIconPriv *priv;
748
749         priv = GET_PRIV (icon);
750
751         if (event->func) {
752                 event->func (event);
753         }
754         priv->events = g_list_remove (priv->events, event);
755         status_icon_event_free (event);
756         priv->showing_event_icon = FALSE;
757         status_icon_update_tooltip (icon);
758         status_icon_set_from_state (icon);
759
760         if (priv->events) {
761                 return;
762         }
763
764         if (priv->blink_timeout) {
765                 g_source_remove (priv->blink_timeout);
766                 priv->blink_timeout = 0;
767         }
768 }
769
770 static gboolean
771 status_icon_event_timeout_cb (EmpathyStatusIcon *icon)
772 {
773         EmpathyStatusIconPriv *priv;
774
775         priv = GET_PRIV (icon);
776
777         priv->showing_event_icon = !priv->showing_event_icon;
778
779         if (!priv->showing_event_icon) {
780                 status_icon_set_from_state (icon);
781         } else {
782                 StatusIconEvent *event;
783
784                 event = priv->events->data;
785                 gtk_status_icon_set_from_icon_name (priv->icon, event->icon_name);
786         }
787
788         return TRUE;
789 }
790
791 static void
792 status_icon_event_free (StatusIconEvent *event)
793 {
794         g_free (event->icon_name);
795         g_free (event->message);
796         g_slice_free (StatusIconEvent, event);
797 }
798