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