]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-status-icon.c
Add support for blinking when there is an event. Make use of EmpathyIdle
[empathy.git] / libempathy-gtk / empathy-status-icon.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007 Collabora Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program 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  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  * 
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26
27 #include <gtk/gtk.h>
28 #include <glade/glade.h>
29 #include <glib/gi18n.h>
30
31 #include <libmissioncontrol/mission-control.h>
32
33 #include <libempathy/empathy-contact-list.h>
34 #include <libempathy/empathy-contact-manager.h>
35 #include <libempathy/gossip-contact.h>
36 #include <libempathy/gossip-debug.h>
37 #include <libempathy/gossip-utils.h>
38 #include <libempathy/gossip-conf.h>
39 #include <libempathy/empathy-idle.h>
40
41 #include "empathy-status-icon.h"
42 #include "gossip-presence-chooser.h"
43 #include "gossip-preferences.h"
44 #include "gossip-ui-utils.h"
45 #include "gossip-accounts-dialog.h"
46
47
48 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
49                        EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv))
50
51 #define DEBUG_DOMAIN "StatusIcon"
52
53 /* Number of ms to wait when blinking */
54 #define BLINK_TIMEOUT 500
55
56 struct _EmpathyStatusIconPriv {
57         GtkStatusIcon         *icon;
58         EmpathyContactManager *manager;
59         EmpathyIdle           *idle;
60         GList                 *events;
61         guint                  blink_timeout;
62         gboolean               showing_state_icon;
63
64         GtkWindow             *window;
65
66         GtkWidget             *popup_menu;
67         GtkWidget             *show_window_item;
68         GtkWidget             *message_item;
69         GtkWidget             *status_item;
70 };
71
72 typedef struct _StatusIconEvent StatusIconEvent;
73
74 typedef void (*EventActivatedFunc) (StatusIconEvent *event);
75
76 struct _StatusIconEvent {
77         gchar              *icon_name;
78         gchar              *message;
79         EventActivatedFunc  func;
80         gpointer            user_data;
81 };
82
83
84 static void       empathy_status_icon_class_init  (EmpathyStatusIconClass *klass);
85 static void       empathy_status_icon_init        (EmpathyStatusIcon      *icon);
86 static void       status_icon_finalize            (GObject                *object);
87 static void       status_icon_idle_notify_cb      (EmpathyIdle            *idle,
88                                                    GParamSpec             *param,
89                                                    EmpathyStatusIcon      *icon);
90 static void       status_icon_update_tooltip      (EmpathyStatusIcon      *icon);
91 static void       status_icon_set_from_state      (EmpathyStatusIcon      *icon);
92 static void       status_icon_toggle_visibility   (EmpathyStatusIcon      *icon);
93 static void       status_icon_activate_cb         (GtkStatusIcon          *status_icon,
94                                                    EmpathyStatusIcon      *icon);
95 static gboolean   status_icon_delete_event_cb     (GtkWidget              *widget,
96                                                    GdkEvent               *event,
97                                                    EmpathyStatusIcon      *icon);
98 static void       status_icon_popup_menu_cb       (GtkStatusIcon          *status_icon,
99                                                    guint                   button,
100                                                    guint                   activate_time,
101                                                    EmpathyStatusIcon      *icon);
102 static void       status_icon_create_menu         (EmpathyStatusIcon      *icon);
103 static void       status_icon_new_message_cb      (GtkWidget              *widget,
104                                                    EmpathyStatusIcon      *icon);
105 static void       status_icon_quit_cb             (GtkWidget              *window,
106                                                    EmpathyStatusIcon      *icon);
107 static void       status_icon_show_hide_window_cb (GtkWidget              *widget,
108                                                    EmpathyStatusIcon      *icon);
109 static void       status_icon_local_pending_cb    (EmpathyContactManager  *manager,
110                                                    GossipContact          *contact,
111                                                    gchar                  *message,
112                                                    EmpathyStatusIcon      *icon);
113 static void       status_icon_event_subscribe_cb  (StatusIconEvent        *event);
114 static StatusIconEvent * status_icon_event_new    (EmpathyStatusIcon      *icon,
115                                                    const gchar            *icon_name,
116                                                    const gchar            *message);
117 static void       status_icon_event_remove        (EmpathyStatusIcon      *icon,
118                                                    StatusIconEvent        *event);
119 static gboolean   status_icon_event_timeout_cb    (EmpathyStatusIcon      *icon);
120 static void       status_icon_event_free          (StatusIconEvent        *event);
121
122 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
123
124 static void
125 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
126 {
127         GObjectClass *object_class = G_OBJECT_CLASS (klass);
128
129         object_class->finalize = status_icon_finalize;
130
131         g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
132 }
133
134 static void
135 empathy_status_icon_init (EmpathyStatusIcon *icon)
136 {
137         EmpathyStatusIconPriv *priv;
138         GList                 *pending, *l;
139
140         priv = GET_PRIV (icon);
141
142         priv->icon = gtk_status_icon_new ();
143         priv->idle = empathy_idle_new ();
144         priv->manager = empathy_contact_manager_new ();
145         priv->showing_state_icon = TRUE;
146
147         status_icon_create_menu (icon);
148         status_icon_set_from_state (icon);
149         status_icon_update_tooltip (icon);
150
151         g_signal_connect (priv->idle, "notify",
152                           G_CALLBACK (status_icon_idle_notify_cb),
153                           icon);
154         g_signal_connect (priv->icon, "activate",
155                           G_CALLBACK (status_icon_activate_cb),
156                           icon);
157         g_signal_connect (priv->icon, "popup-menu",
158                           G_CALLBACK (status_icon_popup_menu_cb),
159                           icon);
160         g_signal_connect (priv->manager, "local-pending",
161                           G_CALLBACK (status_icon_local_pending_cb),
162                           icon);
163
164         pending = empathy_contact_list_get_local_pending (EMPATHY_CONTACT_LIST (priv->manager));
165         for (l = pending; l; l = l->next) {
166                 EmpathyContactListInfo *info;
167
168                 info = l->data;
169                 status_icon_local_pending_cb (priv->manager,
170                                               info->contact,
171                                               info->message,
172                                               icon);
173         }
174         g_list_free (pending);
175 }
176
177 static void
178 status_icon_finalize (GObject *object)
179 {
180         EmpathyStatusIconPriv *priv;
181
182         priv = GET_PRIV (object);
183
184         g_list_foreach (priv->events, (GFunc) status_icon_event_free, NULL);
185         g_list_free (priv->events);
186
187         if (priv->blink_timeout) {
188                 g_source_remove (priv->blink_timeout);
189         }
190
191         g_object_unref (priv->icon);
192         g_object_unref (priv->window);
193         g_object_unref (priv->idle);
194         g_object_unref (priv->manager);
195 }
196
197 EmpathyStatusIcon *
198 empathy_status_icon_new (GtkWindow *window)
199 {
200         EmpathyStatusIconPriv *priv;
201         EmpathyStatusIcon     *icon;
202         gboolean               should_hide;
203         gboolean               visible;
204
205         g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
206
207         icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
208         priv = GET_PRIV (icon);
209
210         priv->window = g_object_ref (window);
211
212         g_signal_connect (priv->window, "delete-event",
213                           G_CALLBACK (status_icon_delete_event_cb),
214                           icon);
215
216         gossip_conf_get_bool (gossip_conf_get (),
217                               GOSSIP_PREFS_UI_MAIN_WINDOW_HIDDEN,
218                               &should_hide);
219         visible = gossip_window_get_is_visible (window);
220
221         if ((!should_hide && !visible) || (should_hide && visible)) {
222                 status_icon_toggle_visibility (icon);
223         }
224
225         return icon;
226 }
227
228 static void
229 status_icon_idle_notify_cb (EmpathyIdle       *idle,
230                             GParamSpec        *param,
231                             EmpathyStatusIcon *icon)
232 {
233         EmpathyStatusIconPriv *priv;
234
235         priv = GET_PRIV (icon);
236
237         if (priv->showing_state_icon) {
238                 status_icon_set_from_state (icon);
239         }
240
241         status_icon_update_tooltip (icon);
242 }
243
244 static void
245 status_icon_update_tooltip (EmpathyStatusIcon *icon)
246 {
247         EmpathyStatusIconPriv *priv;
248         const gchar           *tooltip = NULL;
249
250         priv = GET_PRIV (icon);
251
252         if (priv->events) {
253                 StatusIconEvent *event;
254
255                 event = priv->events->data;
256                 tooltip = event->message;
257         }
258
259         if (!tooltip) {
260                 tooltip = empathy_idle_get_status (priv->idle);
261         }
262
263         gtk_status_icon_set_tooltip (priv->icon, tooltip);      
264 }
265
266 static void
267 status_icon_set_from_state (EmpathyStatusIcon *icon)
268 {
269         EmpathyStatusIconPriv *priv;
270         McPresence             state;
271         const gchar           *icon_name;
272
273         priv = GET_PRIV (icon);
274
275         state = empathy_idle_get_state (priv->idle);
276         icon_name = gossip_icon_name_for_presence_state (state);
277         gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
278 }
279
280 static void
281 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
282 {
283         EmpathyStatusIconPriv *priv;
284         gboolean               visible;
285
286         priv = GET_PRIV (icon);
287
288         visible = gossip_window_get_is_visible (GTK_WINDOW (priv->window));
289
290         if (visible) {
291                 gtk_widget_hide (GTK_WIDGET (priv->window));
292                 gossip_conf_set_bool (gossip_conf_get (),
293                                       GOSSIP_PREFS_UI_MAIN_WINDOW_HIDDEN, TRUE);
294         } else {
295                 GList *accounts;
296
297                 gossip_window_present (GTK_WINDOW (priv->window), TRUE);
298                 gossip_conf_set_bool (gossip_conf_get (),
299                                       GOSSIP_PREFS_UI_MAIN_WINDOW_HIDDEN, FALSE);
300         
301                 /* Show the accounts dialog if there is no enabled accounts */
302                 accounts = mc_accounts_list_by_enabled (TRUE);
303                 if (accounts) {
304                         mc_accounts_list_free (accounts);
305                 } else {
306                         gossip_debug (DEBUG_DOMAIN,
307                                       "No enabled account, Showing account dialog");
308                         gossip_accounts_dialog_show (GTK_WINDOW (priv->window));
309                 }
310         }
311 }
312
313 static void
314 status_icon_activate_cb (GtkStatusIcon     *status_icon,
315                          EmpathyStatusIcon *icon)
316 {
317         EmpathyStatusIconPriv *priv;
318
319         priv = GET_PRIV (icon);
320
321         if (priv->events) {
322                 status_icon_event_remove (icon, priv->events->data);
323         } else {
324                 status_icon_toggle_visibility (icon);
325         }
326 }
327
328 static gboolean
329 status_icon_delete_event_cb (GtkWidget         *widget,
330                              GdkEvent          *event,
331                              EmpathyStatusIcon *icon)
332 {
333         status_icon_toggle_visibility (icon);
334
335         return TRUE;
336 }
337
338 static void
339 status_icon_popup_menu_cb (GtkStatusIcon     *status_icon,
340                            guint              button,
341                            guint              activate_time,
342                            EmpathyStatusIcon *icon)
343 {
344         EmpathyStatusIconPriv *priv;
345         GtkWidget             *submenu;
346         gboolean               show;
347
348         priv = GET_PRIV (icon);
349
350         show = gossip_window_get_is_visible (GTK_WINDOW (priv->window));
351
352         g_signal_handlers_block_by_func (priv->show_window_item,
353                                          status_icon_show_hide_window_cb,
354                                          icon);
355         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (priv->show_window_item),
356                                         show);
357         g_signal_handlers_unblock_by_func (priv->show_window_item,
358                                            status_icon_show_hide_window_cb,
359                                            icon);
360
361         submenu = gossip_presence_chooser_create_menu ();
362         gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->status_item),
363                                    submenu);
364
365         gtk_menu_popup (GTK_MENU (priv->popup_menu),
366                         NULL, NULL,
367                         gtk_status_icon_position_menu,
368                         priv->icon,
369                         button,
370                         activate_time);
371 }
372
373 static void
374 status_icon_create_menu (EmpathyStatusIcon *icon)
375 {
376         EmpathyStatusIconPriv *priv;
377         GladeXML              *glade;
378
379         priv = GET_PRIV (icon);
380
381         glade = gossip_glade_get_file ("empathy-status-icon.glade",
382                                        "tray_menu",
383                                        NULL,
384                                        "tray_menu", &priv->popup_menu,
385                                        "tray_show_list", &priv->show_window_item,
386                                        "tray_new_message", &priv->message_item,
387                                        "tray_status", &priv->status_item,
388                                        NULL);
389
390         gossip_glade_connect (glade,
391                               icon,
392                               "tray_new_message", "activate", status_icon_new_message_cb,
393                               "tray_quit", "activate", status_icon_quit_cb,
394                               NULL);
395
396         g_signal_connect (priv->show_window_item, "toggled",
397                           G_CALLBACK (status_icon_show_hide_window_cb),
398                           icon);
399
400         g_object_unref (glade);
401 }
402
403 static void
404 status_icon_new_message_cb (GtkWidget         *widget,
405                             EmpathyStatusIcon *icon)
406 {
407         EmpathyStatusIconPriv *priv;
408
409         priv = GET_PRIV (icon);
410
411         //gossip_new_message_dialog_show (GTK_WINDOW (priv->window));
412 }
413
414 static void
415 status_icon_quit_cb (GtkWidget         *window,
416                      EmpathyStatusIcon *icon)
417 {
418         gtk_main_quit ();
419 }
420
421 static void
422 status_icon_show_hide_window_cb (GtkWidget         *widget,
423                                  EmpathyStatusIcon *icon)
424 {
425         status_icon_toggle_visibility (icon);
426 }
427
428 static void
429 status_icon_local_pending_cb (EmpathyContactManager *manager,
430                               GossipContact         *contact,
431                               gchar                 *message,
432                               EmpathyStatusIcon     *icon)
433 {
434         EmpathyStatusIconPriv *priv;
435         StatusIconEvent       *event;
436         gchar                 *str;
437         GList                 *l;
438
439         priv = GET_PRIV (icon);
440
441         for (l = priv->events; l; l = l->next) {
442                 if (gossip_contact_equal (contact, ((StatusIconEvent*)l->data)->user_data)) {
443                         return;
444                 }
445         }
446
447         str = g_strdup_printf (_("Subscription requested for %s\n"
448                                  "Message: %s"),
449                                gossip_contact_get_name (contact),
450                                message);
451
452         event = status_icon_event_new (icon, GTK_STOCK_DIALOG_QUESTION, str);
453         event->user_data = g_object_ref (contact);
454         event->func = status_icon_event_subscribe_cb;
455
456         g_free (str);
457 }
458
459 static void
460 status_icon_event_subscribe_cb (StatusIconEvent *event)
461 {
462         GossipContact *contact;
463
464         contact = GOSSIP_CONTACT (event->user_data);
465
466         g_object_unref (contact);
467 }
468
469 static StatusIconEvent *
470 status_icon_event_new (EmpathyStatusIcon *icon,
471                        const gchar       *icon_name,
472                        const gchar       *message)
473 {
474         EmpathyStatusIconPriv *priv;
475         StatusIconEvent       *event;
476
477         priv = GET_PRIV (icon);
478
479         event = g_slice_new0 (StatusIconEvent);
480         event->icon_name = g_strdup (icon_name);        
481         event->message = g_strdup (message);
482
483         priv->events = g_list_append (priv->events, event);
484         if (!priv->blink_timeout) {
485                 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
486                                                      (GSourceFunc) status_icon_event_timeout_cb,
487                                                      icon);
488                 status_icon_event_timeout_cb (icon);
489         }
490
491         return event;
492 }
493
494 static void
495 status_icon_event_remove (EmpathyStatusIcon *icon,
496                           StatusIconEvent   *event)
497 {
498         EmpathyStatusIconPriv *priv;
499
500         priv = GET_PRIV (icon);
501
502         if (event->func) {
503                 event->func (event);
504         }
505         priv->events = g_list_remove (priv->events, event);
506         status_icon_event_free (event);
507         status_icon_update_tooltip (icon);
508
509         if (priv->events) {
510                 return;
511         }
512
513         status_icon_set_from_state (icon);
514         priv->showing_state_icon = TRUE;
515
516         if (priv->blink_timeout) {
517                 g_source_remove (priv->blink_timeout);
518                 priv->blink_timeout = 0;
519
520         }
521 }
522
523 static gboolean
524 status_icon_event_timeout_cb (EmpathyStatusIcon *icon)
525 {
526         EmpathyStatusIconPriv *priv;
527
528         priv = GET_PRIV (icon);
529
530         priv->showing_state_icon = !priv->showing_state_icon;
531
532         if (priv->showing_state_icon) {
533                 status_icon_set_from_state (icon);
534         } else {
535                 StatusIconEvent *event;
536
537                 event = priv->events->data;
538                 gtk_status_icon_set_from_icon_name (priv->icon, event->icon_name);
539         }
540         status_icon_update_tooltip (icon);
541
542         return TRUE;
543 }
544
545 static void
546 status_icon_event_free (StatusIconEvent *event)
547 {
548         g_free (event->icon_name);
549         g_free (event->message);
550         g_slice_free (StatusIconEvent, event);
551 }
552