Updated German help translation
[empathy/ppotvin.git] / src / empathy-status-icon.c
blobb137daf721181d21321165031c66c7dd9a2aeebf
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2007-2008 Collabora Ltd.
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.
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.
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
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
22 #include <config.h>
24 #include <string.h>
26 #include <glib.h>
28 #include <gtk/gtk.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <glib/gi18n.h>
32 #include <libnotify/notification.h>
33 #include <libnotify/notify.h>
35 #include <telepathy-glib/account-manager.h>
36 #include <telepathy-glib/util.h>
38 #include <libempathy/empathy-utils.h>
40 #include <libempathy-gtk/empathy-presence-chooser.h>
41 #include <libempathy-gtk/empathy-conf.h>
42 #include <libempathy-gtk/empathy-ui-utils.h>
43 #include <libempathy-gtk/empathy-images.h>
44 #include <libempathy-gtk/empathy-new-message-dialog.h>
45 #include <libempathy-gtk/empathy-new-call-dialog.h>
46 #include <libempathy-gtk/empathy-notify-manager.h>
48 #include "empathy-accounts-dialog.h"
49 #include "empathy-status-icon.h"
50 #include "empathy-preferences.h"
51 #include "empathy-event-manager.h"
53 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
54 #include <libempathy/empathy-debug.h>
56 /* Number of ms to wait when blinking */
57 #define BLINK_TIMEOUT 500
59 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyStatusIcon)
60 typedef struct {
61 GtkStatusIcon *icon;
62 TpAccountManager *account_manager;
63 EmpathyNotifyManager *notify_mgr;
64 gboolean showing_event_icon;
65 guint blink_timeout;
66 EmpathyEventManager *event_manager;
67 EmpathyEvent *event;
68 NotifyNotification *notification;
70 GtkWindow *window;
71 GtkUIManager *ui_manager;
72 GtkWidget *popup_menu;
73 GtkAction *show_window_item;
74 GtkAction *new_message_item;
75 GtkAction *status_item;
76 } EmpathyStatusIconPriv;
78 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
80 static gboolean
81 activate_event (EmpathyEvent *event)
83 empathy_event_activate (event);
85 return FALSE;
88 static void
89 status_icon_notification_closed_cb (NotifyNotification *notification,
90 EmpathyStatusIcon *icon)
92 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
93 EmpathyNotificationClosedReason reason = 0;
95 #ifdef notify_notification_get_closed_reason
96 reason = notify_notification_get_closed_reason (notification);
97 #endif
98 if (priv->notification) {
99 g_object_unref (priv->notification);
100 priv->notification = NULL;
103 if (!priv->event) {
104 return;
107 /* the notification has been closed by the user, see the
108 * DesktopNotification spec.
110 if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
111 /* use an idle here, as this callback is called from a
112 * DBus signal handler inside libnotify, and we might call
113 * a *_run_* method when activating the event.
115 g_idle_add ((GSourceFunc) activate_event, priv->event);
116 } else {
117 /* inhibit other updates for this event */
118 empathy_event_inhibit_updates (priv->event);
122 static void
123 notification_close_helper (EmpathyStatusIconPriv *priv)
125 if (priv->notification) {
126 notify_notification_close (priv->notification, NULL);
127 g_object_unref (priv->notification);
128 priv->notification = NULL;
132 static void
133 notification_action_cb (NotifyNotification *notification,
134 gchar *action,
135 EmpathyStatusIcon *icon)
137 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
139 if (priv->event)
140 empathy_event_activate (priv->event);
143 static void
144 status_icon_update_notification (EmpathyStatusIcon *icon)
146 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
147 GdkPixbuf *pixbuf = NULL;
149 if (!empathy_notify_manager_notification_is_enabled (priv->notify_mgr)) {
150 /* always close the notification if this happens */
151 notification_close_helper (priv);
152 return;
155 if (priv->event) {
156 gchar *message_esc = NULL;
157 gchar *header_esc = NULL;
159 if (priv->event->message != NULL)
160 message_esc = g_markup_escape_text (priv->event->message, -1);
162 if (priv->event->header != NULL)
163 header_esc = g_markup_escape_text (priv->event->header, -1);
165 if (priv->notification) {
166 notify_notification_update (priv->notification,
167 header_esc, message_esc,
168 NULL);
169 } else {
170 priv->notification = notify_notification_new_with_status_icon
171 (header_esc, message_esc, NULL, priv->icon);
172 notify_notification_set_timeout (priv->notification,
173 NOTIFY_EXPIRES_DEFAULT);
175 if (empathy_notify_manager_has_capability (priv->notify_mgr,
176 EMPATHY_NOTIFY_MANAGER_CAP_ACTIONS)) {
177 notify_notification_add_action (priv->notification,
178 "respond",
179 _("Respond"),
180 (NotifyActionCallback) notification_action_cb,
181 icon,
182 NULL);
185 g_signal_connect (priv->notification, "closed",
186 G_CALLBACK (status_icon_notification_closed_cb), icon);
189 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
190 priv->notify_mgr, priv->event->contact,
191 priv->event->icon_name);
193 if (pixbuf != NULL) {
194 notify_notification_set_icon_from_pixbuf (priv->notification,
195 pixbuf);
196 g_object_unref (pixbuf);
199 notify_notification_show (priv->notification, NULL);
201 g_free (message_esc);
202 g_free (header_esc);
203 } else {
204 notification_close_helper (priv);
208 static void
209 status_icon_update_tooltip (EmpathyStatusIcon *icon)
211 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
212 gchar *tooltip = NULL;
214 if (priv->event) {
215 if (priv->event->message != NULL)
216 tooltip = g_markup_printf_escaped ("<i>%s</i>\n%s",
217 priv->event->header,
218 priv->event->message);
219 else
220 tooltip = g_markup_printf_escaped ("<i>%s</i>",
221 priv->event->header);
222 gtk_status_icon_set_tooltip_markup (priv->icon, tooltip);
223 } else {
224 tp_account_manager_get_most_available_presence (
225 priv->account_manager, &tooltip, NULL);
226 gtk_status_icon_set_tooltip_text (priv->icon, tooltip);
229 g_free (tooltip);
232 static void
233 status_icon_update_icon (EmpathyStatusIcon *icon)
235 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
236 const gchar *icon_name;
238 if (priv->event && priv->showing_event_icon) {
239 icon_name = priv->event->icon_name;
240 } else {
241 TpConnectionPresenceType state;
243 state = tp_account_manager_get_most_available_presence (
244 priv->account_manager, NULL, NULL);
246 /* An unset presence type here doesn't make sense. Force it
247 * to be offline. */
248 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
249 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
252 icon_name = empathy_icon_name_for_presence (state);
255 if (icon_name != NULL)
256 gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
259 static gboolean
260 status_icon_blink_timeout_cb (EmpathyStatusIcon *icon)
262 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
264 priv->showing_event_icon = !priv->showing_event_icon;
265 status_icon_update_icon (icon);
267 return TRUE;
269 static void
270 status_icon_event_added_cb (EmpathyEventManager *manager,
271 EmpathyEvent *event,
272 EmpathyStatusIcon *icon)
274 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
276 if (priv->event) {
277 return;
280 DEBUG ("New event %p", event);
282 priv->event = event;
283 if (event->must_ack) {
284 priv->showing_event_icon = TRUE;
285 status_icon_update_icon (icon);
286 status_icon_update_tooltip (icon);
288 status_icon_update_notification (icon);
290 if (!priv->blink_timeout && priv->showing_event_icon) {
291 priv->blink_timeout = g_timeout_add (BLINK_TIMEOUT,
292 (GSourceFunc) status_icon_blink_timeout_cb,
293 icon);
297 static void
298 status_icon_event_removed_cb (EmpathyEventManager *manager,
299 EmpathyEvent *event,
300 EmpathyStatusIcon *icon)
302 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
304 if (event != priv->event) {
305 return;
308 priv->event = empathy_event_manager_get_top_event (priv->event_manager);
310 status_icon_update_tooltip (icon);
311 status_icon_update_icon (icon);
313 /* update notification anyway, as it's safe and we might have been
314 * changed presence in the meanwhile
316 status_icon_update_notification (icon);
318 if (!priv->event && priv->blink_timeout) {
319 g_source_remove (priv->blink_timeout);
320 priv->blink_timeout = 0;
324 static void
325 status_icon_event_updated_cb (EmpathyEventManager *manager,
326 EmpathyEvent *event,
327 EmpathyStatusIcon *icon)
329 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
331 if (event != priv->event) {
332 return;
335 if (empathy_notify_manager_notification_is_enabled (priv->notify_mgr)) {
336 status_icon_update_notification (icon);
339 status_icon_update_tooltip (icon);
342 static void
343 status_icon_set_visibility (EmpathyStatusIcon *icon,
344 gboolean visible,
345 gboolean store)
347 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
349 if (store) {
350 empathy_conf_set_bool (empathy_conf_get (),
351 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN, !visible);
354 if (!visible) {
355 empathy_window_iconify (priv->window, priv->icon);
356 } else {
357 empathy_window_present (GTK_WINDOW (priv->window), TRUE);
361 static void
362 status_icon_notify_visibility_cb (EmpathyConf *conf,
363 const gchar *key,
364 gpointer user_data)
366 EmpathyStatusIcon *icon = user_data;
367 gboolean hidden = FALSE;
369 if (empathy_conf_get_bool (conf, key, &hidden)) {
370 status_icon_set_visibility (icon, !hidden, FALSE);
374 static void
375 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
377 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
378 gboolean visible;
380 visible = gtk_window_is_active (priv->window);
381 status_icon_set_visibility (icon, !visible, TRUE);
384 static void
385 status_icon_presence_changed_cb (EmpathyStatusIcon *icon)
387 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
389 status_icon_update_icon (icon);
390 status_icon_update_tooltip (icon);
392 if (!empathy_notify_manager_notification_is_enabled (priv->notify_mgr)) {
393 /* dismiss the outstanding notification if present */
395 if (priv->notification) {
396 notify_notification_close (priv->notification, NULL);
397 g_object_unref (priv->notification);
398 priv->notification = NULL;
403 static gboolean
404 status_icon_delete_event_cb (GtkWidget *widget,
405 GdkEvent *event,
406 EmpathyStatusIcon *icon)
408 status_icon_set_visibility (icon, FALSE, TRUE);
409 return TRUE;
412 static gboolean
413 status_icon_key_press_event_cb (GtkWidget *window,
414 GdkEventKey *event,
415 EmpathyStatusIcon *icon)
417 if (event->keyval == GDK_Escape) {
418 status_icon_set_visibility (icon, FALSE, TRUE);
420 return FALSE;
423 static void
424 status_icon_activate_cb (GtkStatusIcon *status_icon,
425 EmpathyStatusIcon *icon)
427 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
429 DEBUG ("%s", priv->event ? "event" : "toggle");
431 if (priv->event) {
432 empathy_event_activate (priv->event);
433 } else {
434 status_icon_toggle_visibility (icon);
438 static void
439 status_icon_show_hide_window_cb (GtkToggleAction *action,
440 EmpathyStatusIcon *icon)
442 gboolean visible;
444 visible = gtk_toggle_action_get_active (action);
445 status_icon_set_visibility (icon, visible, TRUE);
448 static void
449 status_icon_new_message_cb (GtkAction *action,
450 EmpathyStatusIcon *icon)
452 empathy_new_message_dialog_show (NULL);
455 static void
456 status_icon_new_call_cb (GtkAction *action,
457 EmpathyStatusIcon *icon)
459 empathy_new_call_dialog_show (NULL);
462 static void
463 status_icon_quit_cb (GtkAction *action,
464 EmpathyStatusIcon *icon)
466 gtk_main_quit ();
469 static void
470 status_icon_popup_menu_cb (GtkStatusIcon *status_icon,
471 guint button,
472 guint activate_time,
473 EmpathyStatusIcon *icon)
475 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
476 GtkWidget *menu_item;
477 GtkWidget *submenu;
478 gboolean show;
480 show = empathy_window_get_is_visible (GTK_WINDOW (priv->window));
482 g_signal_handlers_block_by_func (priv->show_window_item,
483 status_icon_show_hide_window_cb,
484 icon);
485 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (priv->show_window_item),
486 show);
487 g_signal_handlers_unblock_by_func (priv->show_window_item,
488 status_icon_show_hide_window_cb,
489 icon);
491 menu_item = gtk_ui_manager_get_widget (priv->ui_manager, "/menu/status");
492 submenu = empathy_presence_chooser_create_menu ();
493 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), submenu);
495 gtk_menu_popup (GTK_MENU (priv->popup_menu),
496 NULL, NULL,
497 gtk_status_icon_position_menu,
498 priv->icon,
499 button,
500 activate_time);
503 static void
504 status_icon_create_menu (EmpathyStatusIcon *icon)
506 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
507 GtkBuilder *gui;
508 gchar *filename;
510 filename = empathy_file_lookup ("empathy-status-icon.ui", "src");
511 gui = empathy_builder_get_file (filename,
512 "ui_manager", &priv->ui_manager,
513 "menu", &priv->popup_menu,
514 "show_list", &priv->show_window_item,
515 "new_message", &priv->new_message_item,
516 "status", &priv->status_item,
517 NULL);
518 g_free (filename);
520 empathy_builder_connect (gui, icon,
521 "show_list", "toggled", status_icon_show_hide_window_cb,
522 "new_message", "activate", status_icon_new_message_cb,
523 "new_call", "activate", status_icon_new_call_cb,
524 "quit", "activate", status_icon_quit_cb,
525 NULL);
527 g_object_ref (priv->ui_manager);
528 g_object_unref (gui);
531 static void
532 status_icon_status_changed_cb (TpAccount *account,
533 TpConnectionStatus current,
534 TpConnectionStatus previous,
535 TpConnectionStatusReason reason,
536 gchar *dbus_error_name,
537 GHashTable *details,
538 EmpathyStatusIcon *icon)
540 EmpathyStatusIconPriv *priv = GET_PRIV (icon);
542 gtk_action_set_sensitive (priv->new_message_item,
543 empathy_account_manager_get_accounts_connected (NULL));
546 static void
547 status_icon_finalize (GObject *object)
549 EmpathyStatusIconPriv *priv = GET_PRIV (object);
551 if (priv->blink_timeout) {
552 g_source_remove (priv->blink_timeout);
555 if (priv->notification) {
556 notify_notification_close (priv->notification, NULL);
557 g_object_unref (priv->notification);
558 priv->notification = NULL;
561 g_object_unref (priv->icon);
562 g_object_unref (priv->account_manager);
563 g_object_unref (priv->event_manager);
564 g_object_unref (priv->ui_manager);
565 g_object_unref (priv->notify_mgr);
568 static void
569 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
571 GObjectClass *object_class = G_OBJECT_CLASS (klass);
573 object_class->finalize = status_icon_finalize;
575 g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
578 static void
579 account_manager_prepared_cb (GObject *source_object,
580 GAsyncResult *result,
581 gpointer user_data)
583 GList *list, *l;
584 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
585 EmpathyStatusIcon *icon = user_data;
586 GError *error = NULL;
588 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
589 DEBUG ("Failed to prepare account manager: %s", error->message);
590 g_error_free (error);
591 return;
594 list = tp_account_manager_get_valid_accounts (account_manager);
595 for (l = list; l != NULL; l = l->next) {
596 empathy_signal_connect_weak (l->data, "status-changed",
597 G_CALLBACK (status_icon_status_changed_cb),
598 G_OBJECT (icon));
600 g_list_free (list);
602 status_icon_presence_changed_cb (icon);
605 static void
606 empathy_status_icon_init (EmpathyStatusIcon *icon)
608 EmpathyStatusIconPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (icon,
609 EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv);
611 icon->priv = priv;
612 priv->icon = gtk_status_icon_new ();
613 priv->account_manager = tp_account_manager_dup ();
614 priv->event_manager = empathy_event_manager_dup_singleton ();
616 tp_account_manager_prepare_async (priv->account_manager, NULL,
617 account_manager_prepared_cb, icon);
619 /* make icon listen and respond to MAIN_WINDOW_HIDDEN changes */
620 empathy_conf_notify_add (empathy_conf_get (),
621 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
622 status_icon_notify_visibility_cb,
623 icon);
625 status_icon_create_menu (icon);
627 g_signal_connect_swapped (priv->account_manager,
628 "most-available-presence-changed",
629 G_CALLBACK (status_icon_presence_changed_cb),
630 icon);
631 g_signal_connect (priv->event_manager, "event-added",
632 G_CALLBACK (status_icon_event_added_cb),
633 icon);
634 g_signal_connect (priv->event_manager, "event-removed",
635 G_CALLBACK (status_icon_event_removed_cb),
636 icon);
637 g_signal_connect (priv->event_manager, "event-updated",
638 G_CALLBACK (status_icon_event_updated_cb),
639 icon);
640 g_signal_connect (priv->icon, "activate",
641 G_CALLBACK (status_icon_activate_cb),
642 icon);
643 g_signal_connect (priv->icon, "popup-menu",
644 G_CALLBACK (status_icon_popup_menu_cb),
645 icon);
647 priv->notify_mgr = empathy_notify_manager_dup_singleton ();
650 EmpathyStatusIcon *
651 empathy_status_icon_new (GtkWindow *window, gboolean hide_contact_list)
653 EmpathyStatusIconPriv *priv;
654 EmpathyStatusIcon *icon;
655 gboolean should_hide;
657 g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
659 icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
660 priv = GET_PRIV (icon);
662 priv->window = g_object_ref (window);
664 g_signal_connect_after (priv->window, "key-press-event",
665 G_CALLBACK (status_icon_key_press_event_cb),
666 icon);
668 g_signal_connect (priv->window, "delete-event",
669 G_CALLBACK (status_icon_delete_event_cb),
670 icon);
672 if (!hide_contact_list) {
673 empathy_conf_get_bool (empathy_conf_get (),
674 EMPATHY_PREFS_UI_MAIN_WINDOW_HIDDEN,
675 &should_hide);
676 } else {
677 should_hide = TRUE;
680 if (gtk_window_is_active (priv->window) == should_hide) {
681 status_icon_set_visibility (icon, !should_hide, FALSE);
684 return icon;