]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-status-icon.c
Add help menu in chat window
[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 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-conf.h>
39 #include <libempathy/empathy-idle.h>
40 #include <libempathy/empathy-filter.h>
41
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"
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         EmpathyIdle           *idle;
67         MissionControl        *mc;
68         GList                 *events;
69         gboolean               showing_event_icon;
70         StatusIconEvent       *flash_state_event;
71         guint                  blink_timeout;
72
73         GtkWindow             *window;
74         GtkWidget             *popup_menu;
75         GtkWidget             *show_window_item;
76         GtkWidget             *message_item;
77         GtkWidget             *status_item;
78 };
79
80 typedef void (*EventActivatedFunc) (StatusIconEvent *event);
81
82 struct _StatusIconEvent {
83         gchar              *icon_name;
84         gchar              *message;
85         EventActivatedFunc  func;
86         gpointer            user_data;
87 };
88
89
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,
94                                                    TpConn                 *tp_conn,
95                                                    TpChan                 *tp_chan,
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,
104                                                    gboolean                visible);
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,
109                                                    GdkEvent               *event,
110                                                    EmpathyStatusIcon      *icon);
111 static void       status_icon_popup_menu_cb       (GtkStatusIcon          *status_icon,
112                                                    guint                   button,
113                                                    guint                   activate_time,
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,
125                                                    guint                   reason,
126                                                    gchar                  *message,
127                                                    gboolean                is_pending,
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);
139
140 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
141
142 static void
143 status_icon_notify_use_nm_cb (EmpathyConf  *conf,
144                               const gchar *key,
145                               gpointer     user_data)
146 {
147         EmpathyStatusIconPriv *priv = GET_PRIV (user_data);
148         gboolean               use_nm;
149
150         if (empathy_conf_get_bool (conf, key, &use_nm)) {
151                 empathy_idle_set_use_nm (priv->idle, use_nm);
152         }
153 }
154
155 static void
156 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
157 {
158         GObjectClass *object_class = G_OBJECT_CLASS (klass);
159
160         object_class->finalize = status_icon_finalize;
161
162         g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
163 }
164
165 static void
166 empathy_status_icon_init (EmpathyStatusIcon *icon)
167 {
168         EmpathyStatusIconPriv *priv;
169         GList                 *pendings, *l;
170         gboolean               use_nm;
171
172         priv = GET_PRIV (icon);
173
174         priv->icon = gtk_status_icon_new ();
175         priv->manager = empathy_contact_manager_new ();
176         priv->mc = empathy_mission_control_new ();
177         priv->text_filter = empathy_filter_new ("org.gnome.Empathy.ChatFilter",
178                                                 "/org/gnome/Empathy/ChatFilter",
179                                                 TP_IFACE_CHANNEL_TYPE_TEXT,
180                                                 MC_FILTER_PRIORITY_DIALOG,
181                                                 MC_FILTER_FLAG_INCOMING);
182
183         /* Setup EmpathyIdle */
184         priv->idle = empathy_idle_new ();
185         empathy_conf_get_bool (empathy_conf_get (),
186                                EMPATHY_PREFS_USE_NM,
187                                &use_nm);
188         empathy_conf_notify_add (empathy_conf_get (),
189                                  EMPATHY_PREFS_USE_NM,
190                                  status_icon_notify_use_nm_cb,
191                                  icon);
192         empathy_idle_set_auto_away (priv->idle, TRUE);
193         empathy_idle_set_use_nm (priv->idle, use_nm);
194
195
196         status_icon_create_menu (icon);
197         status_icon_idle_notify_cb (icon);
198
199         g_signal_connect (priv->text_filter, "new-channel",
200                           G_CALLBACK (status_icon_filter_new_channel),
201                           icon);
202         g_signal_connect_swapped (priv->idle, "notify",
203                                   G_CALLBACK (status_icon_idle_notify_cb),
204                                   icon);
205         g_signal_connect (priv->icon, "activate",
206                           G_CALLBACK (status_icon_activate_cb),
207                           icon);
208         g_signal_connect (priv->icon, "popup-menu",
209                           G_CALLBACK (status_icon_popup_menu_cb),
210                           icon);
211         g_signal_connect (priv->manager, "pendings-changed",
212                           G_CALLBACK (status_icon_pendings_changed_cb),
213                           icon);
214
215         pendings = empathy_contact_list_get_pendings (EMPATHY_CONTACT_LIST (priv->manager));
216         for (l = pendings; l; l = l->next) {
217                 EmpathyPendingInfo *info;
218
219                 info = l->data;
220                 status_icon_pendings_changed_cb (priv->manager,
221                                                  info->member,
222                                                  info->actor,
223                                                  0,
224                                                  info->message,
225                                                  TRUE,
226                                                  icon);
227                 empathy_pending_info_free (info);
228         }
229         g_list_free (pendings);
230 }
231
232 static void
233 status_icon_finalize (GObject *object)
234 {
235         EmpathyStatusIconPriv *priv;
236
237         priv = GET_PRIV (object);
238
239         g_list_foreach (priv->events, (GFunc) status_icon_event_free, NULL);
240         g_list_free (priv->events);
241
242         if (priv->blink_timeout) {
243                 g_source_remove (priv->blink_timeout);
244         }
245
246         g_object_unref (priv->icon);
247         g_object_unref (priv->window);
248         g_object_unref (priv->idle);
249         g_object_unref (priv->manager);
250         g_object_unref (priv->mc);
251 }
252
253 EmpathyStatusIcon *
254 empathy_status_icon_new (GtkWindow *window)
255 {
256         EmpathyStatusIconPriv *priv;
257         EmpathyStatusIcon     *icon;
258         gboolean               should_hide;
259
260         g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
261
262         icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
263         priv = GET_PRIV (icon);
264
265         priv->window = g_object_ref (window);
266
267         g_signal_connect (priv->window, "delete-event",
268                           G_CALLBACK (status_icon_delete_event_cb),
269                           icon);
270
271         empathy_conf_get_bool (empathy_conf_get (),
272                               EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
273                               &should_hide);
274
275         if (gtk_window_is_active (priv->window) == should_hide) {
276                 status_icon_set_visibility (icon, !should_hide);
277         }
278
279         return icon;
280 }
281
282 static void
283 status_icon_filter_new_channel (EmpathyFilter     *filter,
284                                 TpConn            *tp_conn,
285                                 TpChan            *tp_chan,
286                                 EmpathyStatusIcon *icon)
287 {
288         EmpathyStatusIconPriv *priv;
289         McAccount             *account;
290         EmpathyTpChat         *tp_chat;
291         GList                 *messages;
292
293         priv = GET_PRIV (icon);
294
295         account = mission_control_get_account_for_connection (priv->mc, tp_conn, NULL);
296
297         empathy_debug (DEBUG_DOMAIN, "New text channel to be filtered for contact %s",
298                        empathy_inspect_channel (account, tp_chan));
299
300         tp_chat = empathy_tp_chat_new (account, tp_chan);
301         g_object_set_data (G_OBJECT (tp_chat), "filter", filter);
302         g_object_unref (account);
303
304         messages = empathy_tp_chat_get_pendings (tp_chat);
305         if (!messages) {
306                 empathy_debug (DEBUG_DOMAIN, "No pending msg, waiting...");
307                 g_signal_connect (tp_chat, "message-received",
308                                   G_CALLBACK (status_icon_message_received_cb),
309                                   icon);
310                 return;
311         }
312
313         status_icon_message_received_cb (tp_chat, messages->data, icon);
314
315         g_list_foreach (messages, (GFunc) g_object_unref, NULL);
316         g_list_free (messages);
317 }
318
319 static void
320 status_icon_message_received_cb (EmpathyTpChat     *tp_chat,
321                                  EmpathyMessage    *message,
322                                  EmpathyStatusIcon *icon)
323 {
324         EmpathyContact  *sender;
325         gchar           *msg;
326         StatusIconEvent *event;
327
328         empathy_debug (DEBUG_DOMAIN, "Message received, add event");
329
330         g_signal_handlers_disconnect_by_func (tp_chat,
331                                               status_icon_message_received_cb,
332                                               icon);
333
334         sender = empathy_message_get_sender (message);
335         msg = g_strdup_printf (_("New message from %s:\n%s"),
336                                empathy_contact_get_name (sender),
337                                empathy_message_get_body (message));
338
339         event = status_icon_event_new (icon, EMPATHY_IMAGE_NEW_MESSAGE, msg);
340         event->func = status_icon_event_msg_cb;
341         event->user_data = tp_chat;
342 }
343
344 static void
345 status_icon_idle_notify_cb (EmpathyStatusIcon *icon)
346 {
347         EmpathyStatusIconPriv *priv;
348         McPresence             flash_state;
349
350         priv = GET_PRIV (icon);
351
352         flash_state = empathy_idle_get_flash_state (priv->idle);
353         if (flash_state != MC_PRESENCE_UNSET) {
354                 const gchar *icon_name;
355
356                 icon_name = empathy_icon_name_for_presence_state (flash_state);
357                 if (!priv->flash_state_event) {
358                         /* We are now flashing */
359                         priv->flash_state_event = status_icon_event_new (icon, icon_name, NULL);
360                         priv->flash_state_event->user_data = icon;
361                         priv->flash_state_event->func = status_icon_event_flash_state_cb;
362                 } else {
363                         /* We are still flashing but with another state */
364                         g_free (priv->flash_state_event->icon_name);
365                         priv->flash_state_event->icon_name = g_strdup (icon_name);
366                 }
367         }
368         else if (priv->flash_state_event) {
369                 /* We are no more flashing */
370                 status_icon_event_remove (icon, priv->flash_state_event);
371                 priv->flash_state_event = NULL;
372         }
373
374         if (!priv->showing_event_icon) {
375                 status_icon_set_from_state (icon);
376         }
377
378         status_icon_update_tooltip (icon);
379 }
380
381 static void
382 status_icon_update_tooltip (EmpathyStatusIcon *icon)
383 {
384         EmpathyStatusIconPriv *priv;
385         const gchar           *tooltip = NULL;
386
387         priv = GET_PRIV (icon);
388
389         if (priv->events) {
390                 StatusIconEvent *event;
391
392                 event = priv->events->data;
393                 tooltip = event->message;
394         }
395
396         if (!tooltip) {
397                 tooltip = empathy_idle_get_status (priv->idle);
398         }
399
400         gtk_status_icon_set_tooltip (priv->icon, tooltip);      
401 }
402
403 static void
404 status_icon_set_from_state (EmpathyStatusIcon *icon)
405 {
406         EmpathyStatusIconPriv *priv;
407         McPresence             state;
408         const gchar           *icon_name;
409
410         priv = GET_PRIV (icon);
411
412         state = empathy_idle_get_state (priv->idle);
413         icon_name = empathy_icon_name_for_presence_state (state);
414         gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
415 }
416
417 static void
418 status_icon_set_visibility (EmpathyStatusIcon *icon,
419                             gboolean           visible)
420 {
421         EmpathyStatusIconPriv *priv;
422
423         priv = GET_PRIV (icon);
424
425         if (!visible) {
426                 empathy_window_iconify (priv->window, priv->icon);
427                 empathy_conf_set_bool (empathy_conf_get (),
428                                       EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, TRUE);
429         } else {
430                 GList *accounts;
431
432                 empathy_window_present (GTK_WINDOW (priv->window), TRUE);
433                 empathy_conf_set_bool (empathy_conf_get (),
434                                       EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, FALSE);
435         
436                 /* Show the accounts dialog if there is no enabled accounts */
437                 accounts = mc_accounts_list_by_enabled (TRUE);
438                 if (accounts) {
439                         mc_accounts_list_free (accounts);
440                 } else {
441                         empathy_debug (DEBUG_DOMAIN,
442                                       "No enabled account, Showing account dialog");
443                         empathy_accounts_dialog_show (GTK_WINDOW (priv->window));
444                 }
445         }
446 }
447
448 static void
449 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
450 {
451         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
452         gboolean               visible;
453
454         visible = gtk_window_is_active (priv->window);
455         status_icon_set_visibility (icon, !visible);
456 }
457
458 static void
459 status_icon_activate_cb (GtkStatusIcon     *status_icon,
460                          EmpathyStatusIcon *icon)
461 {
462         EmpathyStatusIconPriv *priv;
463
464         priv = GET_PRIV (icon);
465
466         empathy_debug (DEBUG_DOMAIN, "Activated: %s",
467                        priv->events ? "event" : "toggle");
468
469         if (priv->events) {
470                 status_icon_event_remove (icon, priv->events->data);
471         } else {
472                 status_icon_toggle_visibility (icon);
473         }
474 }
475
476 static gboolean
477 status_icon_delete_event_cb (GtkWidget         *widget,
478                              GdkEvent          *event,
479                              EmpathyStatusIcon *icon)
480 {
481         status_icon_set_visibility (icon, FALSE);
482
483         return TRUE;
484 }
485
486 static void
487 status_icon_popup_menu_cb (GtkStatusIcon     *status_icon,
488                            guint              button,
489                            guint              activate_time,
490                            EmpathyStatusIcon *icon)
491 {
492         EmpathyStatusIconPriv *priv;
493         GtkWidget             *submenu;
494         gboolean               show;
495
496         priv = GET_PRIV (icon);
497
498         show = empathy_window_get_is_visible (GTK_WINDOW (priv->window));
499
500         g_signal_handlers_block_by_func (priv->show_window_item,
501                                          status_icon_show_hide_window_cb,
502                                          icon);
503         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (priv->show_window_item),
504                                         show);
505         g_signal_handlers_unblock_by_func (priv->show_window_item,
506                                            status_icon_show_hide_window_cb,
507                                            icon);
508
509         submenu = empathy_presence_chooser_create_menu ();
510         gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->status_item),
511                                    submenu);
512
513         gtk_menu_popup (GTK_MENU (priv->popup_menu),
514                         NULL, NULL,
515                         gtk_status_icon_position_menu,
516                         priv->icon,
517                         button,
518                         activate_time);
519 }
520
521 static void
522 status_icon_create_menu (EmpathyStatusIcon *icon)
523 {
524         EmpathyStatusIconPriv *priv;
525         GladeXML              *glade;
526
527         priv = GET_PRIV (icon);
528
529         glade = empathy_glade_get_file ("empathy-status-icon.glade",
530                                        "tray_menu",
531                                        NULL,
532                                        "tray_menu", &priv->popup_menu,
533                                        "tray_show_list", &priv->show_window_item,
534                                        "tray_new_message", &priv->message_item,
535                                        "tray_status", &priv->status_item,
536                                        NULL);
537
538         empathy_glade_connect (glade,
539                               icon,
540                               "tray_show_list", "toggled", status_icon_show_hide_window_cb,
541                               "tray_new_message", "activate", status_icon_new_message_cb,
542                               "tray_quit", "activate", status_icon_quit_cb,
543                               NULL);
544
545         g_object_unref (glade);
546 }
547
548 static void
549 status_icon_new_message_cb (GtkWidget         *widget,
550                             EmpathyStatusIcon *icon)
551 {
552         EmpathyStatusIconPriv *priv;
553
554         priv = GET_PRIV (icon);
555
556         empathy_new_message_dialog_show (GTK_WINDOW (priv->window));
557 }
558
559 static void
560 status_icon_quit_cb (GtkWidget         *window,
561                      EmpathyStatusIcon *icon)
562 {
563         gtk_main_quit ();
564 }
565
566 static void
567 status_icon_show_hide_window_cb (GtkWidget         *widget,
568                                  EmpathyStatusIcon *icon)
569 {
570         gboolean visible;
571
572         visible = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (widget));
573         status_icon_set_visibility (icon, visible);
574 }
575
576 static void
577 status_icon_pendings_changed_cb (EmpathyContactManager *manager,
578                                  EmpathyContact        *contact,
579                                  EmpathyContact        *actor,
580                                  guint                  reason,
581                                  gchar                 *message,
582                                  gboolean               is_pending,
583                                  EmpathyStatusIcon     *icon)
584 {
585         EmpathyStatusIconPriv *priv;
586         StatusIconEvent       *event;
587         GString               *str;
588         GList                 *l;
589
590         priv = GET_PRIV (icon);
591
592         if (!is_pending) {
593                 /* FIXME: We should remove the event */
594                 return;
595         }
596
597         for (l = priv->events; l; l = l->next) {
598                 if (empathy_contact_equal (contact, ((StatusIconEvent*)l->data)->user_data)) {
599                         return;
600                 }
601         }
602
603         str = g_string_new (NULL);
604         g_string_printf (str, _("Subscription requested by %s"),
605                          empathy_contact_get_name (contact));   
606         if (!G_STR_EMPTY (message)) {
607                 g_string_append_printf (str, _("\nMessage: %s"), message);
608         }
609
610         event = status_icon_event_new (icon, GTK_STOCK_DIALOG_QUESTION, str->str);
611         event->user_data = g_object_ref (contact);
612         event->func = status_icon_event_subscribe_cb;
613
614         g_string_free (str, TRUE);
615 }
616
617 static void
618 status_icon_event_subscribe_cb (StatusIconEvent *event)
619 {
620         EmpathyContact *contact;
621
622         contact = EMPATHY_CONTACT (event->user_data);
623
624         empathy_subscription_dialog_show (contact, NULL);
625
626         g_object_unref (contact);
627 }
628
629 static void
630 status_icon_event_flash_state_cb (StatusIconEvent *event)
631 {
632         EmpathyStatusIconPriv *priv;
633
634         priv = GET_PRIV (event->user_data);
635
636         empathy_idle_set_flash_state (priv->idle, MC_PRESENCE_UNSET);
637 }
638
639 static void
640 status_icon_event_msg_cb (StatusIconEvent *event)
641 {
642         EmpathyFilter *filter;
643         EmpathyTpChat *tp_chat;
644
645         empathy_debug (DEBUG_DOMAIN, "Dispatching text channel");
646
647         tp_chat = event->user_data;
648         filter = g_object_get_data (G_OBJECT (tp_chat), "filter");
649         empathy_filter_process (filter,
650                                 empathy_tp_chat_get_channel (tp_chat),
651                                 TRUE);
652         g_object_unref (tp_chat);
653 }
654
655 static StatusIconEvent *
656 status_icon_event_new (EmpathyStatusIcon *icon,
657                        const gchar       *icon_name,
658                        const gchar       *message)
659 {
660         EmpathyStatusIconPriv *priv;
661         StatusIconEvent       *event;
662
663         priv = GET_PRIV (icon);
664
665         event = g_slice_new0 (StatusIconEvent);
666         event->icon_name = g_strdup (icon_name);        
667         event->message = g_strdup (message);
668
669         priv->events = g_list_append (priv->events, event);
670         if (!priv->blink_timeout) {
671                 priv->showing_event_icon = FALSE;
672                 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
673                                                      (GSourceFunc) status_icon_event_timeout_cb,
674                                                      icon);
675                 status_icon_event_timeout_cb (icon);
676                 status_icon_update_tooltip (icon);
677         }
678
679         return event;
680 }
681
682 static void
683 status_icon_event_remove (EmpathyStatusIcon *icon,
684                           StatusIconEvent   *event)
685 {
686         EmpathyStatusIconPriv *priv;
687
688         priv = GET_PRIV (icon);
689
690         if (event->func) {
691                 event->func (event);
692         }
693         priv->events = g_list_remove (priv->events, event);
694         status_icon_event_free (event);
695         priv->showing_event_icon = FALSE;
696         status_icon_update_tooltip (icon);
697         status_icon_set_from_state (icon);
698
699         if (priv->events) {
700                 return;
701         }
702
703         if (priv->blink_timeout) {
704                 g_source_remove (priv->blink_timeout);
705                 priv->blink_timeout = 0;
706         }
707 }
708
709 static gboolean
710 status_icon_event_timeout_cb (EmpathyStatusIcon *icon)
711 {
712         EmpathyStatusIconPriv *priv;
713
714         priv = GET_PRIV (icon);
715
716         priv->showing_event_icon = !priv->showing_event_icon;
717
718         if (!priv->showing_event_icon) {
719                 status_icon_set_from_state (icon);
720         } else {
721                 StatusIconEvent *event;
722
723                 event = priv->events->data;
724                 gtk_status_icon_set_from_icon_name (priv->icon, event->icon_name);
725         }
726
727         return TRUE;
728 }
729
730 static void
731 status_icon_event_free (StatusIconEvent *event)
732 {
733         g_free (event->icon_name);
734         g_free (event->message);
735         g_slice_free (StatusIconEvent, event);
736 }
737