[darcs-to-svn @ Show/Hide main window on delete event]
[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 <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <glade/glade.h>
30
31 #include <libtelepathy/tp-helpers.h>
32
33 #include <libmissioncontrol/mission-control.h>
34
35 #include <libempathy/gossip-debug.h>
36 #include <libempathy/gossip-utils.h>
37 #include <libempathy/gossip-conf.h>
38
39 #include "empathy-status-icon.h"
40 #include "gossip-presence-chooser.h"
41 #include "gossip-preferences.h"
42 #include "gossip-ui-utils.h"
43
44 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
45                        EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv))
46
47 #define DEBUG_DOMAIN "StatusIcon"
48
49 struct _EmpathyStatusIconPriv {
50         MissionControl *mc;
51         GtkStatusIcon  *icon;
52         GtkWindow      *window;
53
54         GtkWidget      *popup_menu;
55         GtkWidget      *show_window_item;
56         GtkWidget      *message_item;
57         GtkWidget      *status_item;
58 };
59
60 static void     empathy_status_icon_class_init  (EmpathyStatusIconClass *klass);
61 static void     empathy_status_icon_init        (EmpathyStatusIcon      *icon);
62 static void     status_icon_finalize            (GObject                *object);
63 static void     status_icon_presence_changed_cb (MissionControl         *mc,
64                                                  McPresence              state,
65                                                  EmpathyStatusIcon      *icon);
66 static void     status_icon_toggle_visibility   (EmpathyStatusIcon      *icon);
67 static void     status_icon_activate_cb         (GtkStatusIcon          *status_icon,
68                                                  EmpathyStatusIcon      *icon);
69 static gboolean status_icon_delete_event_cb     (GtkWidget              *widget,
70                                                  GdkEvent               *event,
71                                                  EmpathyStatusIcon      *icon);
72 static void     status_icon_popup_menu_cb       (GtkStatusIcon          *status_icon,
73                                                  guint                   button,
74                                                  guint                   activate_time,
75                                                  EmpathyStatusIcon      *icon);
76 static void     status_icon_create_menu         (EmpathyStatusIcon      *icon);
77 static void     status_icon_new_message_cb      (GtkWidget              *widget,
78                                                  EmpathyStatusIcon      *icon);
79 static void     status_icon_quit_cb             (GtkWidget              *window,
80                                                  EmpathyStatusIcon      *icon);
81 static void     status_icon_show_hide_window_cb (GtkWidget              *widget,
82                                                  EmpathyStatusIcon      *icon);
83
84 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
85
86 static void
87 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
88 {
89         GObjectClass *object_class = G_OBJECT_CLASS (klass);
90
91         object_class->finalize = status_icon_finalize;
92
93         g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
94 }
95
96 static void
97 empathy_status_icon_init (EmpathyStatusIcon *icon)
98 {
99         EmpathyStatusIconPriv *priv;
100         McPresence             state;
101
102         priv = GET_PRIV (icon);
103
104         priv->icon = gtk_status_icon_new ();
105         priv->mc = mission_control_new (tp_get_bus ());
106
107         status_icon_create_menu (icon);
108
109         state = mission_control_get_presence_actual (priv->mc, NULL);
110         status_icon_presence_changed_cb (priv->mc, state, icon);
111         
112         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
113                                      "PresenceStatusActual",
114                                      G_CALLBACK (status_icon_presence_changed_cb),
115                                      icon, NULL);
116         g_signal_connect (priv->icon, "activate",
117                           G_CALLBACK (status_icon_activate_cb),
118                           icon);
119         g_signal_connect (priv->icon, "popup-menu",
120                           G_CALLBACK (status_icon_popup_menu_cb),
121                           icon);
122 }
123
124 static void
125 status_icon_finalize (GObject *object)
126 {
127         EmpathyStatusIconPriv *priv;
128
129         priv = GET_PRIV (object);
130
131         dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
132                                         "PresenceStatusActual",
133                                         G_CALLBACK (status_icon_presence_changed_cb),
134                                         object);
135
136         g_object_unref (priv->mc);
137         g_object_unref (priv->icon);
138         g_object_unref (priv->window);
139 }
140
141 EmpathyStatusIcon *
142 empathy_status_icon_new (GtkWindow *window)
143 {
144         EmpathyStatusIconPriv *priv;
145         EmpathyStatusIcon     *icon;
146
147         g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);
148
149         icon = g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
150         priv = GET_PRIV (icon);
151
152         priv->window = g_object_ref (window);
153
154         g_signal_connect (priv->window, "delete-event",
155                           G_CALLBACK (status_icon_delete_event_cb),
156                           icon);
157
158         return icon;
159 }
160
161 static void
162 status_icon_presence_changed_cb (MissionControl    *mc,
163                                  McPresence         state,
164                                  EmpathyStatusIcon *icon)
165 {
166         EmpathyStatusIconPriv *priv;
167         const gchar           *icon_name;
168         gchar                 *status;
169
170         priv = GET_PRIV (icon);
171
172         icon_name = gossip_icon_name_for_presence_state (state);
173         status = mission_control_get_presence_message_actual (priv->mc, NULL);
174         if (G_STR_EMPTY (status)) {
175                 g_free (status);
176                 status = g_strdup (gossip_presence_state_get_default_status (state));
177         }
178
179         gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
180         gtk_status_icon_set_tooltip (priv->icon, status);
181
182         g_free (status);
183
184         if (state < MC_PRESENCE_AVAILABLE) {
185                 gtk_widget_set_sensitive (priv->status_item, FALSE);
186                 gtk_widget_set_sensitive (priv->message_item, FALSE);
187         } else {
188                 gtk_widget_set_sensitive (priv->status_item, TRUE);
189                 gtk_widget_set_sensitive (priv->message_item, TRUE);
190         }
191 }
192
193 static void
194 status_icon_toggle_visibility (EmpathyStatusIcon *icon)
195 {
196         EmpathyStatusIconPriv *priv;
197         gboolean               visible;
198
199         priv = GET_PRIV (icon);
200
201         visible = gossip_window_get_is_visible (GTK_WINDOW (priv->window));
202
203         if (visible) {
204                 gtk_widget_hide (GTK_WIDGET (priv->window));
205                 gossip_conf_set_bool (gossip_conf_get (),
206                                       GOSSIP_PREFS_UI_MAIN_WINDOW_HIDDEN, TRUE);
207         } else {
208                 gossip_window_present (GTK_WINDOW (priv->window), TRUE);
209                 gossip_conf_set_bool (gossip_conf_get (),
210                                       GOSSIP_PREFS_UI_MAIN_WINDOW_HIDDEN, FALSE);
211         }
212 }
213
214 static void
215 status_icon_activate_cb (GtkStatusIcon     *status_icon,
216                          EmpathyStatusIcon *icon)
217 {
218         status_icon_toggle_visibility (icon);
219 }
220
221 static gboolean
222 status_icon_delete_event_cb (GtkWidget         *widget,
223                              GdkEvent          *event,
224                              EmpathyStatusIcon *icon)
225 {
226         status_icon_toggle_visibility (icon);
227
228         return TRUE;
229 }
230
231 static void
232 status_icon_popup_menu_cb (GtkStatusIcon     *status_icon,
233                            guint              button,
234                            guint              activate_time,
235                            EmpathyStatusIcon *icon)
236 {
237         EmpathyStatusIconPriv *priv;
238         GtkWidget             *submenu;
239         gboolean               show;
240
241         priv = GET_PRIV (icon);
242
243         show = gossip_window_get_is_visible (GTK_WINDOW (priv->window));
244
245         g_signal_handlers_block_by_func (priv->show_window_item,
246                                          status_icon_show_hide_window_cb,
247                                          icon);
248         gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (priv->show_window_item),
249                                         show);
250         g_signal_handlers_unblock_by_func (priv->show_window_item,
251                                            status_icon_show_hide_window_cb,
252                                            icon);
253
254         submenu = gossip_presence_chooser_create_menu ();
255         gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->status_item),
256                                    submenu);
257
258         gtk_menu_popup (GTK_MENU (priv->popup_menu),
259                         NULL, NULL,
260                         gtk_status_icon_position_menu,
261                         priv->icon,
262                         button,
263                         activate_time);
264 }
265
266 static void
267 status_icon_create_menu (EmpathyStatusIcon *icon)
268 {
269         EmpathyStatusIconPriv *priv;
270         GladeXML              *glade;
271
272         priv = GET_PRIV (icon);
273
274         glade = gossip_glade_get_file ("empathy-status-icon.glade",
275                                        "tray_menu",
276                                        NULL,
277                                        "tray_menu", &priv->popup_menu,
278                                        "tray_show_list", &priv->show_window_item,
279                                        "tray_new_message", &priv->message_item,
280                                        "tray_status", &priv->status_item,
281                                        NULL);
282
283         gossip_glade_connect (glade,
284                               icon,
285                               "tray_new_message", "activate", status_icon_new_message_cb,
286                               "tray_quit", "activate", status_icon_quit_cb,
287                               NULL);
288
289         g_signal_connect (priv->show_window_item, "toggled",
290                           G_CALLBACK (status_icon_show_hide_window_cb),
291                           icon);
292
293         g_object_unref (glade);
294 }
295
296 static void
297 status_icon_new_message_cb (GtkWidget         *widget,
298                             EmpathyStatusIcon *icon)
299 {
300         EmpathyStatusIconPriv *priv;
301
302         priv = GET_PRIV (icon);
303
304         //gossip_new_message_dialog_show (GTK_WINDOW (priv->window));
305 }
306
307 static void
308 status_icon_quit_cb (GtkWidget         *window,
309                      EmpathyStatusIcon *icon)
310 {
311         gtk_main_quit ();
312 }
313
314 static void
315 status_icon_show_hide_window_cb (GtkWidget         *widget,
316                                  EmpathyStatusIcon *icon)
317 {
318         status_icon_toggle_visibility (icon);
319 }
320