]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-status-icon.c
Remove the autogen.sh script and use gnome-autogen.sh instead.
[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/empathy-contact.h>
36 #include <libempathy/empathy-tp-chat.h>
37 #include <libempathy/empathy-debug.h>
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy/empathy-conf.h>
40 #include <libempathy/empathy-idle.h>
41 #include <libempathy/empathy-filter.h>
42
43 #include "empathy-status-icon.h"
44 #include "empathy-contact-dialogs.h"
45 #include "empathy-presence-chooser.h"
46 #include "empathy-preferences.h"
47 #include "empathy-ui-utils.h"
48 #include "empathy-accounts-dialog.h"
49 #include "empathy-images.h"
50
51
52 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
53                        EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv))
54
55 #define DEBUG_DOMAIN "StatusIcon"
56
57 /* Number of ms to wait when blinking */
58 #define BLINK_TIMEOUT 500
59
60 typedef struct _StatusIconEvent StatusIconEvent;
61
62 struct _EmpathyStatusIconPriv {
63         GtkStatusIcon         *icon;
64         EmpathyContactManager *manager;
65         EmpathyFilter         *text_filter;
66         EmpathyIdle           *idle;
67         MissionControl        *mc;
68         GList                 *events;
69         gboolean               showing_event_icon;
70         StatusIconEvent       *flash_state_event;
71         guint                  blink_timeout;
72
73         GtkWindow             *window;
74         GtkWidget             *popup_menu;
75         GtkWidget             *show_window_item;
76         GtkWidget             *message_item;
77         GtkWidget             *status_item;
78 };
79
80 typedef void (*EventActivatedFunc) (StatusIconEvent *event);
81
82 struct _StatusIconEvent {
83         gchar              *icon_name;
84         gchar              *message;
85         EventActivatedFunc  func;
86         gpointer            user_data;
87 };
88
89
90 static void       empathy_status_icon_class_init  (EmpathyStatusIconClass *klass);
91 static void       empathy_status_icon_init        (EmpathyStatusIcon      *icon);
92 static void       status_icon_finalize            (GObject                *object);
93 static void       status_icon_filter_new_channel  (EmpathyFilter          *filter,
94                                                    TpConn                 *tp_conn,
95                                                    TpChan                 *tp_chan,
96                                                    EmpathyStatusIcon      *icon);
97 static void       status_icon_message_received_cb (EmpathyTpChat          *tp_chat,
98                                                    EmpathyMessage         *message,
99                                                    EmpathyStatusIcon      *icon);
100 static void       status_icon_idle_notify_cb      (EmpathyStatusIcon      *icon);
101 static void       status_icon_update_tooltip      (EmpathyStatusIcon      *icon);
102 static void       status_icon_set_from_state      (EmpathyStatusIcon      *icon);
103 static void       status_icon_toggle_visibility   (EmpathyStatusIcon      *icon);
104 static void       status_icon_activate_cb         (GtkStatusIcon          *status_icon,
105                                                    EmpathyStatusIcon      *icon);
106 static gboolean   status_icon_delete_event_cb     (GtkWidget              *widget,
107                                                    GdkEvent               *event,
108                                                    EmpathyStatusIcon      *icon);
109 static void       status_icon_popup_menu_cb       (GtkStatusIcon          *status_icon,
110                                                    guint                   button,
111                                                    guint                   activate_time,
112                                                    EmpathyStatusIcon      *icon);
113 static void       status_icon_create_menu         (EmpathyStatusIcon      *icon);
114 static void       status_icon_new_message_cb      (GtkWidget              *widget,
115                                                    EmpathyStatusIcon      *icon);
116 static void       status_icon_quit_cb             (GtkWidget              *window,
117                                                    EmpathyStatusIcon      *icon);
118 static void       status_icon_show_hide_window_cb (GtkWidget              *widget,
119                                                    EmpathyStatusIcon      *icon);
120 static void       status_icon_local_pending_cb    (EmpathyContactManager  *manager,
121                                                    EmpathyContact          *contact,
122                                                    gchar                  *message,
123                                                    EmpathyStatusIcon      *icon);
124 static void       status_icon_event_subscribe_cb  (StatusIconEvent        *event);
125 static void       status_icon_event_flash_state_cb (StatusIconEvent       *event);
126 static void       status_icon_event_msg_cb        (StatusIconEvent        *event);
127 static StatusIconEvent * status_icon_event_new    (EmpathyStatusIcon      *icon,
128                                                    const gchar            *icon_name,
129                                                    const gchar            *message);
130 static void       status_icon_event_remove        (EmpathyStatusIcon      *icon,
131                                                    StatusIconEvent        *event);
132 static gboolean   status_icon_event_timeout_cb    (EmpathyStatusIcon      *icon);
133 static void       status_icon_event_free          (StatusIconEvent        *event);
134
135 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
136
137 static void
138 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
139 {
140         GObjectClass *object_class = G_OBJECT_CLASS (klass);
141
142         object_class->finalize = status_icon_finalize;
143
144         g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
145 }
146
147 static void
148 empathy_status_icon_init (EmpathyStatusIcon *icon)
149 {
150         EmpathyStatusIconPriv *priv;
151         GList                 *pending, *l;
152
153         priv = GET_PRIV (icon);
154
155         priv->icon = gtk_status_icon_new ();
156         priv->idle = empathy_idle_new ();
157         priv->manager = empathy_contact_manager_new ();
158         priv->mc = empathy_mission_control_new ();
159         priv->text_filter = empathy_filter_new ("org.gnome.Empathy.Chat",
160                                                 "/org/freedesktop/Telepathy/Filter",
161                                                 TP_IFACE_CHANNEL_TYPE_TEXT,
162                                                 MC_FILTER_PRIORITY_DIALOG,
163                                                 MC_FILTER_FLAG_INCOMING);
164
165         status_icon_create_menu (icon);
166         status_icon_idle_notify_cb (icon);
167
168         g_signal_connect (priv->text_filter, "new-channel",
169                           G_CALLBACK (status_icon_filter_new_channel),
170                           icon);
171         g_signal_connect_swapped (priv->idle, "notify",
172                                   G_CALLBACK (status_icon_idle_notify_cb),
173                                   icon);
174         g_signal_connect (priv->icon, "activate",
175                           G_CALLBACK (status_icon_activate_cb),
176                           icon);
177         g_signal_connect (priv->icon, "popup-menu",
178                           G_CALLBACK (status_icon_popup_menu_cb),
179                           icon);
180         g_signal_connect (priv->manager, "local-pending",
181                           G_CALLBACK (status_icon_local_pending_cb),
182                           icon);
183
184         pending = empathy_contact_list_get_local_pending (EMPATHY_CONTACT_LIST (priv->manager));
185         for (l = pending; l; l = l->next) {
186                 EmpathyContactListInfo *info;
187
188                 info = l->data;
189                 status_icon_local_pending_cb (priv->manager,
190                                               info->contact,
191                                               info->message,
192                                               icon);
193         }
194         g_list_free (pending);
195 }
196
197 static void
198 status_icon_finalize (GObject *object)
199 {
200         EmpathyStatusIconPriv *priv;
201
202         priv = GET_PRIV (object);
203
204         g_list_foreach (priv->events, (GFunc) status_icon_event_free, NULL);
205         g_list_free (priv->events);
206
207         if (priv->blink_timeout) {
208                 g_source_remove (priv->blink_timeout);
209         }
210
211         g_object_unref (priv->icon);
212         g_object_unref (priv->window);
213         g_object_unref (priv->idle);
214         g_object_unref (priv->manager);
215         g_object_unref (priv->mc);
216 }
217
218 EmpathyStatusIcon *
219 empathy_status_icon_new (GtkWindow *window)
220 {
221         EmpathyStatusIconPriv *priv;
222         EmpathyStatusIcon     *icon;
223         gboolean               should_hide;
224         gboolean               visible;
225
226         g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
227
228         icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
229         priv = GET_PRIV (icon);
230
231         priv->window = g_object_ref (window);
232
233         g_signal_connect (priv->window, "delete-event",
234                           G_CALLBACK (status_icon_delete_event_cb),
235                           icon);
236
237         empathy_conf_get_bool (empathy_conf_get (),
238                               EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
239                               &should_hide);
240         visible = empathy_window_get_is_visible (window);
241
242         if ((!should_hide && !visible) || (should_hide && visible)) {
243                 status_icon_toggle_visibility (icon);
244         }
245
246         return icon;
247 }
248
249 static void
250 status_icon_filter_new_channel (EmpathyFilter     *filter,
251                                 TpConn            *tp_conn,
252                                 TpChan            *tp_chan,
253                                 EmpathyStatusIcon *icon)
254 {
255         EmpathyStatusIconPriv *priv;
256         McAccount             *account;
257         EmpathyTpChat         *tp_chat;
258         GList                 *messages;
259
260         priv = GET_PRIV (icon);
261
262         empathy_debug (DEBUG_DOMAIN, "New text channel to be filtered");
263
264         account = mission_control_get_account_for_connection (priv->mc, tp_conn, NULL);
265         tp_chat = empathy_tp_chat_new (account, tp_chan);
266         g_object_set_data (G_OBJECT (tp_chat), "filter", filter);
267         g_object_unref (account);
268
269         messages = empathy_tp_chat_get_pendings (tp_chat);
270         if (!messages) {
271                 empathy_debug (DEBUG_DOMAIN, "No pending msg, waiting...");
272                 g_signal_connect (tp_chat, "message-received",
273                                   G_CALLBACK (status_icon_message_received_cb),
274                                   icon);
275                 return;
276         }
277
278         status_icon_message_received_cb (tp_chat, messages->data, icon);
279
280         g_list_foreach (messages, (GFunc) g_object_unref, NULL);
281         g_list_free (messages);
282 }
283
284 static void
285 status_icon_message_received_cb (EmpathyTpChat     *tp_chat,
286                                  EmpathyMessage    *message,
287                                  EmpathyStatusIcon *icon)
288 {
289         EmpathyContact  *sender;
290         gchar           *msg;
291         StatusIconEvent *event;
292
293         empathy_debug (DEBUG_DOMAIN, "Message received, add event");
294
295         g_signal_handlers_disconnect_by_func (tp_chat,
296                                               status_icon_message_received_cb,
297                                               icon);
298
299         sender = empathy_message_get_sender (message);
300         msg = g_strdup_printf (_("New message from %s:\n%s"),
301                                empathy_contact_get_name (sender),
302                                empathy_message_get_body (message));
303
304         event = status_icon_event_new (icon, EMPATHY_IMAGE_NEW_MESSAGE, msg);
305         event->func = status_icon_event_msg_cb;
306         event->user_data = tp_chat;
307 }
308
309 static void
310 status_icon_idle_notify_cb (EmpathyStatusIcon *icon)
311 {
312         EmpathyStatusIconPriv *priv;
313         McPresence             flash_state;
314
315         priv = GET_PRIV (icon);
316
317         flash_state = empathy_idle_get_flash_state (priv->idle);
318         if (flash_state != MC_PRESENCE_UNSET) {
319                 const gchar *icon_name;
320
321                 icon_name = empathy_icon_name_for_presence_state (flash_state);
322                 if (!priv->flash_state_event) {
323                         /* We are now flashing */
324                         priv->flash_state_event = status_icon_event_new (icon, icon_name, NULL);
325                         priv->flash_state_event->user_data = icon;
326                         priv->flash_state_event->func = status_icon_event_flash_state_cb;
327                 } else {
328                         /* We are still flashing but with another state */
329                         g_free (priv->flash_state_event->icon_name);
330                         priv->flash_state_event->icon_name = g_strdup (icon_name);
331                 }
332         }
333         else if (priv->flash_state_event) {
334                 /* We are no more flashing */
335                 status_icon_event_remove (icon, priv->flash_state_event);
336                 priv->flash_state_event = NULL;
337         }
338
339         if (!priv->showing_event_icon) {
340                 status_icon_set_from_state (icon);
341         }
342
343         status_icon_update_tooltip (icon);
344 }
345
346 static void
347 status_icon_update_tooltip (EmpathyStatusIcon *icon)
348 {
349         EmpathyStatusIconPriv *priv;
350         const gchar           *tooltip = NULL;
351
352         priv = GET_PRIV (icon);
353
354         if (priv->events) {
355                 StatusIconEvent *event;
356
357                 event = priv->events->data;
358                 tooltip = event->message;
359         }
360
361         if (!tooltip) {
362                 tooltip = empathy_idle_get_status (priv->idle);
363         }
364
365         gtk_status_icon_set_tooltip (priv->icon, tooltip);      
366 }
367
368 static void
369 status_icon_set_from_state (EmpathyStatusIcon *icon)
370 {
371         EmpathyStatusIconPriv *priv;
372         McPresence             state;
373         const gchar           *icon_name;
374
375         priv = GET_PRIV (icon);
376
377         state = empathy_idle_get_state (priv->idle);
378         icon_name = empathy_icon_name_for_presence_state (state);
379         gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
380 }
381
382 static void
383 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
384 {
385         EmpathyStatusIconPriv *priv;
386         gboolean               visible;
387
388         priv = GET_PRIV (icon);
389
390         visible = empathy_window_get_is_visible (GTK_WINDOW (priv->window));
391
392         if (visible) {
393                 empathy_window_iconify (priv->window, priv->icon);
394                 empathy_conf_set_bool (empathy_conf_get (),
395                                       EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, TRUE);
396         } else {
397                 GList *accounts;
398
399                 empathy_window_present (GTK_WINDOW (priv->window), TRUE);
400                 empathy_conf_set_bool (empathy_conf_get (),
401                                       EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, FALSE);
402         
403                 /* Show the accounts dialog if there is no enabled accounts */
404                 accounts = mc_accounts_list_by_enabled (TRUE);
405                 if (accounts) {
406                         mc_accounts_list_free (accounts);
407                 } else {
408                         empathy_debug (DEBUG_DOMAIN,
409                                       "No enabled account, Showing account dialog");
410                         empathy_accounts_dialog_show (GTK_WINDOW (priv->window));
411                 }
412         }
413 }
414
415 static void
416 status_icon_activate_cb (GtkStatusIcon     *status_icon,
417                          EmpathyStatusIcon *icon)
418 {
419         EmpathyStatusIconPriv *priv;
420
421         priv = GET_PRIV (icon);
422
423         empathy_debug (DEBUG_DOMAIN, "Activated: %s",
424                        priv->events ? "event" : "toggle");
425
426         if (priv->events) {
427                 status_icon_event_remove (icon, priv->events->data);
428         } else {
429                 status_icon_toggle_visibility (icon);
430         }
431 }
432
433 static gboolean
434 status_icon_delete_event_cb (GtkWidget         *widget,
435                              GdkEvent          *event,
436                              EmpathyStatusIcon *icon)
437 {
438         status_icon_toggle_visibility (icon);
439
440         return TRUE;
441 }
442
443 static void
444 status_icon_popup_menu_cb (GtkStatusIcon     *status_icon,
445                            guint              button,
446                            guint              activate_time,
447                            EmpathyStatusIcon *icon)
448 {
449         EmpathyStatusIconPriv *priv;
450         GtkWidget             *submenu;
451         gboolean               show;
452
453         priv = GET_PRIV (icon);
454
455         show = empathy_window_get_is_visible (GTK_WINDOW (priv->window));
456
457         g_signal_handlers_block_by_func (priv->show_window_item,
458                                          status_icon_show_hide_window_cb,
459                                          icon);
460         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (priv->show_window_item),
461                                         show);
462         g_signal_handlers_unblock_by_func (priv->show_window_item,
463                                            status_icon_show_hide_window_cb,
464                                            icon);
465
466         submenu = empathy_presence_chooser_create_menu ();
467         gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->status_item),
468                                    submenu);
469
470         gtk_menu_popup (GTK_MENU (priv->popup_menu),
471                         NULL, NULL,
472                         gtk_status_icon_position_menu,
473                         priv->icon,
474                         button,
475                         activate_time);
476 }
477
478 static void
479 status_icon_create_menu (EmpathyStatusIcon *icon)
480 {
481         EmpathyStatusIconPriv *priv;
482         GladeXML              *glade;
483
484         priv = GET_PRIV (icon);
485
486         glade = empathy_glade_get_file ("empathy-status-icon.glade",
487                                        "tray_menu",
488                                        NULL,
489                                        "tray_menu", &priv->popup_menu,
490                                        "tray_show_list", &priv->show_window_item,
491                                        "tray_new_message", &priv->message_item,
492                                        "tray_status", &priv->status_item,
493                                        NULL);
494
495         empathy_glade_connect (glade,
496                               icon,
497                               "tray_new_message", "activate", status_icon_new_message_cb,
498                               "tray_quit", "activate", status_icon_quit_cb,
499                               NULL);
500
501         g_signal_connect (priv->show_window_item, "toggled",
502                           G_CALLBACK (status_icon_show_hide_window_cb),
503                           icon);
504
505         g_object_unref (glade);
506 }
507
508 static void
509 status_icon_new_message_cb (GtkWidget         *widget,
510                             EmpathyStatusIcon *icon)
511 {
512         EmpathyStatusIconPriv *priv;
513
514         priv = GET_PRIV (icon);
515
516         //empathy_new_message_dialog_show (GTK_WINDOW (priv->window));
517 }
518
519 static void
520 status_icon_quit_cb (GtkWidget         *window,
521                      EmpathyStatusIcon *icon)
522 {
523         gtk_main_quit ();
524 }
525
526 static void
527 status_icon_show_hide_window_cb (GtkWidget         *widget,
528                                  EmpathyStatusIcon *icon)
529 {
530         status_icon_toggle_visibility (icon);
531 }
532
533 static void
534 status_icon_local_pending_cb (EmpathyContactManager *manager,
535                               EmpathyContact         *contact,
536                               gchar                 *message,
537                               EmpathyStatusIcon     *icon)
538 {
539         EmpathyStatusIconPriv *priv;
540         StatusIconEvent       *event;
541         gchar                 *str;
542         GList                 *l;
543
544         priv = GET_PRIV (icon);
545
546         for (l = priv->events; l; l = l->next) {
547                 if (empathy_contact_equal (contact, ((StatusIconEvent*)l->data)->user_data)) {
548                         return;
549                 }
550         }
551
552         str = g_strdup_printf (_("Subscription requested for %s\n"
553                                  "Message: %s"),
554                                empathy_contact_get_name (contact),
555                                message);
556
557         event = status_icon_event_new (icon, GTK_STOCK_DIALOG_QUESTION, str);
558         event->user_data = g_object_ref (contact);
559         event->func = status_icon_event_subscribe_cb;
560
561         g_free (str);
562 }
563
564 static void
565 status_icon_event_subscribe_cb (StatusIconEvent *event)
566 {
567         EmpathyContact *contact;
568
569         contact = EMPATHY_CONTACT (event->user_data);
570
571         empathy_subscription_dialog_show (contact, NULL);
572
573         g_object_unref (contact);
574 }
575
576 static void
577 status_icon_event_flash_state_cb (StatusIconEvent *event)
578 {
579         EmpathyStatusIconPriv *priv;
580
581         priv = GET_PRIV (event->user_data);
582
583         empathy_idle_set_flash_state (priv->idle, MC_PRESENCE_UNSET);
584 }
585
586 static void
587 status_icon_event_msg_cb (StatusIconEvent *event)
588 {
589         EmpathyFilter *filter;
590         EmpathyTpChat *tp_chat;
591
592         empathy_debug (DEBUG_DOMAIN, "Dispatching text channel");
593
594         tp_chat = event->user_data;
595         filter = g_object_get_data (G_OBJECT (tp_chat), "filter");
596         empathy_filter_process (filter,
597                                 empathy_tp_chat_get_channel (tp_chat),
598                                 TRUE);
599         g_object_unref (tp_chat);
600 }
601
602 static StatusIconEvent *
603 status_icon_event_new (EmpathyStatusIcon *icon,
604                        const gchar       *icon_name,
605                        const gchar       *message)
606 {
607         EmpathyStatusIconPriv *priv;
608         StatusIconEvent       *event;
609
610         priv = GET_PRIV (icon);
611
612         event = g_slice_new0 (StatusIconEvent);
613         event->icon_name = g_strdup (icon_name);        
614         event->message = g_strdup (message);
615
616         priv->events = g_list_append (priv->events, event);
617         if (!priv->blink_timeout) {
618                 priv->showing_event_icon = FALSE;
619                 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
620                                                      (GSourceFunc) status_icon_event_timeout_cb,
621                                                      icon);
622                 status_icon_event_timeout_cb (icon);
623         }
624
625         return event;
626 }
627
628 static void
629 status_icon_event_remove (EmpathyStatusIcon *icon,
630                           StatusIconEvent   *event)
631 {
632         EmpathyStatusIconPriv *priv;
633
634         priv = GET_PRIV (icon);
635
636         if (event->func) {
637                 event->func (event);
638         }
639         priv->events = g_list_remove (priv->events, event);
640         status_icon_event_free (event);
641         priv->showing_event_icon = FALSE;
642         status_icon_update_tooltip (icon);
643         status_icon_set_from_state (icon);
644
645         if (priv->events) {
646                 return;
647         }
648
649         if (priv->blink_timeout) {
650                 g_source_remove (priv->blink_timeout);
651                 priv->blink_timeout = 0;
652         }
653 }
654
655 static gboolean
656 status_icon_event_timeout_cb (EmpathyStatusIcon *icon)
657 {
658         EmpathyStatusIconPriv *priv;
659
660         priv = GET_PRIV (icon);
661
662         priv->showing_event_icon = !priv->showing_event_icon;
663
664         if (!priv->showing_event_icon) {
665                 status_icon_set_from_state (icon);
666         } else {
667                 StatusIconEvent *event;
668
669                 event = priv->events->data;
670                 gtk_status_icon_set_from_icon_name (priv->icon, event->icon_name);
671         }
672         status_icon_update_tooltip (icon);
673
674         return TRUE;
675 }
676
677 static void
678 status_icon_event_free (StatusIconEvent *event)
679 {
680         g_free (event->icon_name);
681         g_free (event->message);
682         g_slice_free (StatusIconEvent, event);
683 }
684