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