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