]> git.0d.be Git - empathy.git/blob - src/empathy-status-icon.c
UOA: Do not segfault when "Done" or "Cancel" button clicked but widget is not ready yet
[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 <libempathy/empathy-gsettings.h>
25 #include <libempathy/empathy-utils.h>
26
27 #include <libempathy-gtk/empathy-presence-chooser.h>
28 #include <libempathy-gtk/empathy-ui-utils.h>
29 #include <libempathy-gtk/empathy-new-message-dialog.h>
30 #include <libempathy-gtk/empathy-new-call-dialog.h>
31
32 #include "empathy-status-icon.h"
33 #include "empathy-event-manager.h"
34
35 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
36 #include <libempathy/empathy-debug.h>
37
38 /* Number of ms to wait when blinking */
39 #define BLINK_TIMEOUT 500
40
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyStatusIcon)
42 typedef struct {
43         GtkStatusIcon       *icon;
44         TpAccountManager    *account_manager;
45         gboolean             showing_event_icon;
46         guint                blink_timeout;
47         EmpathyEventManager *event_manager;
48         EmpathyEvent        *event;
49         GSettings           *gsettings_ui;
50
51         GtkWidget           *window;
52         GtkUIManager        *ui_manager;
53         GtkWidget           *popup_menu;
54         GtkAction           *show_window_item;
55         GtkAction           *new_message_item;
56         GtkAction           *status_item;
57 } EmpathyStatusIconPriv;
58
59 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
60
61 static void
62 status_icon_update_tooltip (EmpathyStatusIcon *icon)
63 {
64         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
65
66         if (priv->event) {
67                 gchar *tooltip = NULL;
68
69                 if (priv->event->message != NULL)
70                                 tooltip = g_markup_printf_escaped ("<i>%s</i>\n%s",
71                                                                    priv->event->header,
72                                                                    priv->event->message);
73                 else
74                                 tooltip = g_markup_printf_escaped ("<i>%s</i>",
75                                                                    priv->event->header);
76                 gtk_status_icon_set_tooltip_markup (priv->icon, tooltip);
77                 g_free (tooltip);
78         } else {
79                 TpConnectionPresenceType type;
80                 gchar *msg;
81
82                 type = tp_account_manager_get_most_available_presence (
83                         priv->account_manager, NULL, &msg);
84
85                 if (!EMP_STR_EMPTY (msg)) {
86                         gtk_status_icon_set_tooltip_text (priv->icon, msg);
87                 }
88                 else {
89                         gtk_status_icon_set_tooltip_text (priv->icon,
90                                                 empathy_presence_get_default_message (type));
91                 }
92
93                 g_free (msg);
94         }
95 }
96
97 static void
98 status_icon_update_icon (EmpathyStatusIcon *icon)
99 {
100         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
101         const gchar           *icon_name;
102
103         if (priv->event && priv->showing_event_icon) {
104                 icon_name = priv->event->icon_name;
105         } else {
106                 TpConnectionPresenceType state;
107
108                 state = tp_account_manager_get_most_available_presence (
109                         priv->account_manager, NULL, NULL);
110
111                 /* An unset presence type here doesn't make sense. Force it
112                  * to be offline. */
113                 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
114                         state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
115                 }
116
117                 icon_name = empathy_icon_name_for_presence (state);
118         }
119
120         if (icon_name != NULL)
121                 gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
122 }
123
124 static gboolean
125 status_icon_blink_timeout_cb (EmpathyStatusIcon *icon)
126 {
127         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
128
129         priv->showing_event_icon = !priv->showing_event_icon;
130         status_icon_update_icon (icon);
131
132         return TRUE;
133 }
134 static void
135 status_icon_event_added_cb (EmpathyEventManager *manager,
136                             EmpathyEvent        *event,
137                             EmpathyStatusIcon   *icon)
138 {
139         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
140
141         if (priv->event) {
142                 return;
143         }
144
145         DEBUG ("New event %p", event);
146
147         priv->event = event;
148         if (event->must_ack || event->type == EMPATHY_EVENT_TYPE_AUTH) {
149                 priv->showing_event_icon = TRUE;
150                 status_icon_update_icon (icon);
151                 status_icon_update_tooltip (icon);
152         }
153
154         if (!priv->blink_timeout && priv->showing_event_icon) {
155                 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
156                                                      (GSourceFunc) status_icon_blink_timeout_cb,
157                                                      icon);
158         }
159 }
160
161 static void
162 status_icon_event_removed_cb (EmpathyEventManager *manager,
163                               EmpathyEvent        *event,
164                               EmpathyStatusIcon   *icon)
165 {
166         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
167
168         if (event != priv->event) {
169                 return;
170         }
171
172         priv->event = empathy_event_manager_get_top_event (priv->event_manager);
173
174         status_icon_update_tooltip (icon);
175         status_icon_update_icon (icon);
176
177         if (!priv->event && priv->blink_timeout) {
178                 g_source_remove (priv->blink_timeout);
179                 priv->blink_timeout = 0;
180         }
181 }
182
183 static void
184 status_icon_event_updated_cb (EmpathyEventManager *manager,
185                               EmpathyEvent        *event,
186                               EmpathyStatusIcon   *icon)
187 {
188         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
189
190         if (event != priv->event) {
191                 return;
192         }
193
194         status_icon_update_tooltip (icon);
195 }
196
197 static void
198 status_icon_set_visibility (EmpathyStatusIcon *icon,
199                             gboolean           visible,
200                             gboolean           store)
201 {
202         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
203
204         if (store) {
205                 g_settings_set_boolean (priv->gsettings_ui,
206                                         EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
207                                         !visible);
208         }
209
210         if (!visible) {
211                 gtk_widget_hide (priv->window);
212         } else {
213                 empathy_window_present (GTK_WINDOW (priv->window));
214         }
215 }
216
217 static void
218 status_icon_notify_visibility_cb (GSettings   *gsettings,
219                                   const gchar *key,
220                                   gpointer     user_data)
221 {
222         EmpathyStatusIcon *icon = user_data;
223         gboolean           hidden = FALSE;
224
225         hidden = g_settings_get_boolean (gsettings, key);
226         status_icon_set_visibility (icon, !hidden, FALSE);
227 }
228
229 static void
230 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
231 {
232         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
233         gboolean               visible;
234
235         visible = gtk_window_is_active (GTK_WINDOW (priv->window));
236         status_icon_set_visibility (icon, !visible, TRUE);
237 }
238
239 static void
240 status_icon_presence_changed_cb (EmpathyStatusIcon *icon)
241 {
242         status_icon_update_icon (icon);
243         status_icon_update_tooltip (icon);
244 }
245
246 static gboolean
247 status_icon_delete_event_cb (GtkWidget         *widget,
248                              GdkEvent          *event,
249                              EmpathyStatusIcon *icon)
250 {
251         status_icon_set_visibility (icon, FALSE, TRUE);
252         return TRUE;
253 }
254
255 static gboolean
256 status_icon_key_press_event_cb  (GtkWidget *window,
257                                  GdkEventKey *event,
258                                  EmpathyStatusIcon *icon)
259 {
260         if (event->keyval == GDK_KEY_Escape) {
261                 status_icon_set_visibility (icon, FALSE, TRUE);
262         }
263         return FALSE;
264 }
265
266 static void
267 status_icon_activate_cb (GtkStatusIcon     *status_icon,
268                          EmpathyStatusIcon *icon)
269 {
270         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
271
272         DEBUG ("%s", priv->event ? "event" : "toggle");
273
274         if (priv->event) {
275                 empathy_event_activate (priv->event);
276         } else {
277                 status_icon_toggle_visibility (icon);
278         }
279 }
280
281 static void
282 status_icon_show_hide_window_cb (GtkToggleAction   *action,
283                                  EmpathyStatusIcon *icon)
284 {
285         gboolean visible;
286
287         visible = gtk_toggle_action_get_active (action);
288         status_icon_set_visibility (icon, visible, TRUE);
289 }
290
291 static void
292 status_icon_new_message_cb (GtkAction         *action,
293                             EmpathyStatusIcon *icon)
294 {
295         empathy_new_message_dialog_show (NULL);
296 }
297
298 static void
299 status_icon_new_call_cb (GtkAction         *action,
300                             EmpathyStatusIcon *icon)
301 {
302         empathy_new_call_dialog_show (NULL);
303 }
304
305 static void
306 status_icon_quit_cb (GtkAction         *action,
307                      EmpathyStatusIcon *icon)
308 {
309         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
310
311         gtk_widget_destroy (priv->window);
312 }
313
314 static void
315 status_icon_popup_menu_cb (GtkStatusIcon     *status_icon,
316                            guint              button,
317                            guint              activate_time,
318                            EmpathyStatusIcon *icon)
319 {
320         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
321         GtkWidget             *menu_item;
322         GtkWidget             *submenu;
323         gboolean               show;
324
325         show = gtk_widget_get_visible (priv->window);
326
327         g_signal_handlers_block_by_func (priv->show_window_item,
328                                          status_icon_show_hide_window_cb,
329                                          icon);
330         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (priv->show_window_item),
331                                       show);
332         g_signal_handlers_unblock_by_func (priv->show_window_item,
333                                            status_icon_show_hide_window_cb,
334                                            icon);
335
336         menu_item = gtk_ui_manager_get_widget (priv->ui_manager, "/menu/status");
337         submenu = empathy_presence_chooser_create_menu ();
338         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), submenu);
339
340         gtk_menu_popup (GTK_MENU (priv->popup_menu),
341                         NULL, NULL,
342                         gtk_status_icon_position_menu,
343                         priv->icon,
344                         button,
345                         activate_time);
346 }
347
348 static void
349 status_icon_create_menu (EmpathyStatusIcon *icon)
350 {
351         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
352         GtkBuilder            *gui;
353         gchar                 *filename;
354
355         filename = empathy_file_lookup ("empathy-status-icon.ui", "src");
356         gui = empathy_builder_get_file (filename,
357                                         "ui_manager", &priv->ui_manager,
358                                         "menu", &priv->popup_menu,
359                                         "show_list", &priv->show_window_item,
360                                         "new_message", &priv->new_message_item,
361                                         "status", &priv->status_item,
362                                        NULL);
363         g_free (filename);
364
365         empathy_builder_connect (gui, icon,
366                               "show_list", "toggled", status_icon_show_hide_window_cb,
367                               "new_message", "activate", status_icon_new_message_cb,
368                               "new_call", "activate", status_icon_new_call_cb,
369                               "quit", "activate", status_icon_quit_cb,
370                               NULL);
371
372         g_object_ref (priv->ui_manager);
373         g_object_unref (gui);
374 }
375
376 static void
377 status_icon_status_changed_cb (TpAccount *account,
378                                TpConnectionStatus current,
379                                TpConnectionStatus previous,
380                                TpConnectionStatusReason reason,
381                                gchar *dbus_error_name,
382                                GHashTable *details,
383                                EmpathyStatusIcon *icon)
384 {
385         EmpathyStatusIconPriv *priv = GET_PRIV (icon);
386
387         gtk_action_set_sensitive (priv->new_message_item,
388                                   empathy_account_manager_get_accounts_connected (NULL));
389 }
390
391 static void
392 status_icon_finalize (GObject *object)
393 {
394         EmpathyStatusIconPriv *priv = GET_PRIV (object);
395
396         if (priv->blink_timeout) {
397                 g_source_remove (priv->blink_timeout);
398         }
399
400         g_object_unref (priv->icon);
401         g_object_unref (priv->account_manager);
402         g_object_unref (priv->event_manager);
403         g_object_unref (priv->ui_manager);
404         g_object_unref (priv->gsettings_ui);
405         g_object_unref (priv->window);
406 }
407
408 static void
409 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
410 {
411         GObjectClass *object_class = G_OBJECT_CLASS (klass);
412
413         object_class->finalize = status_icon_finalize;
414
415         g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
416 }
417
418 static void
419 account_manager_prepared_cb (GObject *source_object,
420                              GAsyncResult *result,
421                              gpointer user_data)
422 {
423         GList *list, *l;
424         TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
425         EmpathyStatusIcon *icon = user_data;
426         GError *error = NULL;
427
428         if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
429                 DEBUG ("Failed to prepare account manager: %s", error->message);
430                 g_error_free (error);
431                 return;
432         }
433
434         list = tp_account_manager_dup_valid_accounts (account_manager);
435         for (l = list; l != NULL; l = l->next) {
436                 tp_g_signal_connect_object (l->data, "status-changed",
437                                              G_CALLBACK (status_icon_status_changed_cb),
438                                              icon, 0);
439         }
440         g_list_free_full (list, g_object_unref);
441
442         status_icon_presence_changed_cb (icon);
443 }
444
445 static void
446 empathy_status_icon_init (EmpathyStatusIcon *icon)
447 {
448         EmpathyStatusIconPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (icon,
449                 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv);
450
451         icon->priv = priv;
452         priv->icon = gtk_status_icon_new ();
453         priv->account_manager = tp_account_manager_dup ();
454         priv->event_manager = empathy_event_manager_dup_singleton ();
455
456         tp_proxy_prepare_async (priv->account_manager, NULL,
457             account_manager_prepared_cb, icon);
458
459         /* make icon listen and respond to MAIN_WINDOW_HIDDEN changes */
460         priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
461         g_signal_connect (priv->gsettings_ui,
462                           "changed::" EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
463                           G_CALLBACK (status_icon_notify_visibility_cb),
464                           icon);
465
466         status_icon_create_menu (icon);
467
468         g_signal_connect_swapped (priv->account_manager,
469                                   "most-available-presence-changed",
470                                   G_CALLBACK (status_icon_presence_changed_cb),
471                                   icon);
472         g_signal_connect (priv->event_manager, "event-added",
473                           G_CALLBACK (status_icon_event_added_cb),
474                           icon);
475         g_signal_connect (priv->event_manager, "event-removed",
476                           G_CALLBACK (status_icon_event_removed_cb),
477                           icon);
478         g_signal_connect (priv->event_manager, "event-updated",
479                           G_CALLBACK (status_icon_event_updated_cb),
480                           icon);
481         g_signal_connect (priv->icon, "activate",
482                           G_CALLBACK (status_icon_activate_cb),
483                           icon);
484         g_signal_connect (priv->icon, "popup-menu",
485                           G_CALLBACK (status_icon_popup_menu_cb),
486                           icon);
487 }
488
489 EmpathyStatusIcon *
490 empathy_status_icon_new (GtkWindow *window, gboolean hide_contact_list)
491 {
492         EmpathyStatusIconPriv *priv;
493         EmpathyStatusIcon     *icon;
494         gboolean               should_hide;
495
496         g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
497
498         icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
499         priv = GET_PRIV (icon);
500
501         priv->window = g_object_ref (window);
502
503         g_signal_connect_after (priv->window, "key-press-event",
504                           G_CALLBACK (status_icon_key_press_event_cb),
505                           icon);
506
507         g_signal_connect (priv->window, "delete-event",
508                           G_CALLBACK (status_icon_delete_event_cb),
509                           icon);
510
511         if (!hide_contact_list) {
512                 should_hide = g_settings_get_boolean (priv->gsettings_ui,
513                         EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN);
514         } else {
515                 should_hide = TRUE;
516         }
517
518         status_icon_set_visibility (icon, !should_hide, FALSE);
519
520         return icon;
521 }
522