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