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