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