]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-geometry.c
Merge branch 'sasl'
[empathy.git] / libempathy-gtk / empathy-geometry.c
1 /*
2  * Copyright (C) 2006-2007 Imendio AB
3  * Copyright (C) 2007-2008 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., 51 Franklin St, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors: Martyn Russell <martyn@imendio.com>
21  *          Xavier Claessens <xclaesse@gmail.com>
22  */
23
24 #include "config.h"
25
26 #include <sys/stat.h>
27
28 #include <glib.h>
29 #include <gdk/gdk.h>
30
31 #include "libempathy/empathy-utils.h"
32 #include "empathy-geometry.h"
33 #include "empathy-ui-utils.h"
34
35 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
36 #include <libempathy/empathy-debug.h>
37
38 #define GEOMETRY_DIR_CREATE_MODE  (S_IRUSR | S_IWUSR | S_IXUSR)
39 #define GEOMETRY_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
40
41 /* geometry.ini file contains 2 groups:
42  *  - one with position and size of each window
43  *  - one with the maximized state of each window
44  * Windows are identified by a name. (e.g. "main-window") */
45 #define GEOMETRY_FILENAME             "geometry.ini"
46 #define GEOMETRY_POSITION_FORMAT      "%d,%d,%d,%d" /* "x,y,w,h" */
47 #define GEOMETRY_POSITION_GROUP       "geometry"
48 #define GEOMETRY_MAXIMIZED_GROUP      "maximized"
49
50 /* Key used to keep window's name inside the object's qdata */
51 #define GEOMETRY_NAME_KEY             "geometry-name-key"
52
53 static guint store_id = 0;
54
55 static void
56 geometry_real_store (GKeyFile *key_file)
57 {
58   gchar *filename;
59   gchar *content;
60   gsize length;
61   GError *error = NULL;
62
63   content = g_key_file_to_data (key_file, &length, &error);
64   if (error != NULL)
65     {
66       DEBUG ("Error: %s", error->message);
67       g_error_free (error);
68       return;
69     }
70
71   filename = g_build_filename (g_get_user_config_dir (),
72     PACKAGE_NAME, GEOMETRY_FILENAME, NULL);
73
74   if (!g_file_set_contents (filename, content, length, &error))
75     {
76       DEBUG ("Error: %s", error->message);
77       g_error_free (error);
78     }
79
80   g_free (content);
81   g_free (filename);
82 }
83
84 static gboolean
85 geometry_store_cb (gpointer key_file)
86 {
87   geometry_real_store (key_file);
88   store_id = 0;
89
90   return FALSE;
91 }
92
93 static void
94 geometry_schedule_store (GKeyFile *key_file)
95 {
96   if (store_id != 0)
97     g_source_remove (store_id);
98
99   store_id = g_timeout_add_seconds (1, geometry_store_cb, key_file);
100 }
101
102 static GKeyFile *
103 geometry_get_key_file (void)
104 {
105   static GKeyFile *key_file = NULL;
106   gchar *dir;
107   gchar *filename;
108
109   if (key_file != NULL)
110     return key_file;
111
112   dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
113   if (!g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
114     {
115       DEBUG ("Creating directory:'%s'", dir);
116       g_mkdir_with_parents (dir, GEOMETRY_DIR_CREATE_MODE);
117     }
118
119   filename = g_build_filename (dir, GEOMETRY_FILENAME, NULL);
120   g_free (dir);
121
122   key_file = g_key_file_new ();
123   g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
124   g_free (filename);
125
126   return key_file;
127 }
128
129 static void
130 empathy_geometry_save (GtkWindow *window)
131 {
132   GKeyFile *key_file;
133   GdkWindow *gdk_window;
134   GdkWindowState window_state;
135   gint x, y, w, h;
136   gboolean maximized;
137   gchar *position_str = NULL;
138   GHashTable *names;
139   GHashTableIter iter;
140   const gchar *name;
141
142   names = g_object_get_data (G_OBJECT (window), GEOMETRY_NAME_KEY);
143
144   g_return_if_fail (GTK_IS_WINDOW (window));
145   g_return_if_fail (names != NULL);
146
147   if (!gtk_widget_get_visible (GTK_WIDGET (window)))
148     return;
149
150   /* Get window geometry */
151   gtk_window_get_position (window, &x, &y);
152   gtk_window_get_size (window, &w, &h);
153   gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
154   window_state = gdk_window_get_state (gdk_window);
155   maximized = (window_state & GDK_WINDOW_STATE_MAXIMIZED) != 0;
156
157   /* Don't save off-screen positioning */
158   if (!EMPATHY_RECT_IS_ON_SCREEN (x, y, w, h))
159     return;
160
161   key_file = geometry_get_key_file ();
162
163   /* Save window size only if not maximized */
164   if (!maximized)
165     {
166       position_str = g_strdup_printf (GEOMETRY_POSITION_FORMAT, x, y, w, h);
167     }
168
169   g_hash_table_iter_init (&iter, names);
170   while (g_hash_table_iter_next (&iter, (gpointer) &name, NULL))
171     {
172       gchar *escaped_name;
173
174       /* escape the name so that unwanted characters such as # are removed */
175       escaped_name = g_uri_escape_string (name, NULL, TRUE);
176
177       g_key_file_set_boolean (key_file, GEOMETRY_MAXIMIZED_GROUP,
178           escaped_name, maximized);
179
180       if (position_str != NULL)
181         g_key_file_set_string (key_file, GEOMETRY_POSITION_GROUP,
182             escaped_name, position_str);
183
184       g_free (escaped_name);
185     }
186
187
188   geometry_schedule_store (key_file);
189   g_free (position_str);
190 }
191
192 static void
193 empathy_geometry_load (GtkWindow *window,
194     const gchar *name)
195 {
196   GKeyFile *key_file;
197   gchar    *escaped_name;
198   gchar    *str;
199   gboolean  maximized;
200
201   g_return_if_fail (GTK_IS_WINDOW (window));
202   g_return_if_fail (!EMP_STR_EMPTY (name));
203
204   /* escape the name so that unwanted characters such as # are removed */
205   escaped_name = g_uri_escape_string (name, NULL, TRUE);
206
207   key_file = geometry_get_key_file ();
208
209   /* restore window size and position */
210   str = g_key_file_get_string (key_file, GEOMETRY_POSITION_GROUP,
211       escaped_name, NULL);
212   if (str)
213     {
214       gint x, y, w, h;
215
216       sscanf (str, GEOMETRY_POSITION_FORMAT, &x, &y, &w, &h);
217       gtk_window_move (window, x, y);
218       gtk_window_resize (window, w, h);
219     }
220
221   /* restore window maximized state */
222   maximized = g_key_file_get_boolean (key_file, GEOMETRY_MAXIMIZED_GROUP,
223       escaped_name, NULL);
224
225   if (maximized)
226     gtk_window_maximize (window);
227   else
228     gtk_window_unmaximize (window);
229
230   g_free (str);
231   g_free (escaped_name);
232 }
233
234 static gboolean
235 geometry_configure_event_cb (GtkWindow *window,
236     GdkEventConfigure *event,
237     gpointer user_data)
238 {
239   empathy_geometry_save (window);
240
241   return FALSE;
242 }
243
244 static gboolean
245 geometry_window_state_event_cb (GtkWindow *window,
246     GdkEventWindowState *event,
247     gpointer user_data)
248 {
249   if ((event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED) != 0)
250     {
251       empathy_geometry_save (window);
252     }
253
254   return FALSE;
255 }
256
257 static void
258 geometry_map_cb (GtkWindow *window,
259     gpointer user_data)
260 {
261   GHashTable *names;
262   GHashTableIter iter;
263   const gchar *name;
264
265   /* The WM will replace this window, restore its last position */
266   names = g_object_get_data (G_OBJECT (window), GEOMETRY_NAME_KEY);
267
268   g_assert (names != NULL);
269
270   /* Use the first name we get in the hash table */
271   g_hash_table_iter_init (&iter, names);
272   g_assert (g_hash_table_iter_next (&iter, (gpointer) &name, NULL));
273
274   empathy_geometry_load (window, name);
275 }
276
277 void
278 empathy_geometry_bind (GtkWindow *window,
279     const gchar *name)
280 {
281   GHashTable *names;
282   gboolean connect_sigs = FALSE;
283
284   g_return_if_fail (GTK_IS_WINDOW (window));
285   g_return_if_fail (!EMP_STR_EMPTY (name));
286
287   /* Check if this window is already bound */
288   names = g_object_get_data (G_OBJECT (window), GEOMETRY_NAME_KEY);
289   if (names == NULL)
290     {
291       connect_sigs = TRUE;
292       names = g_hash_table_new_full (g_str_hash, g_str_equal,
293           g_free, NULL);
294
295       g_object_set_data_full (G_OBJECT (window), GEOMETRY_NAME_KEY, names,
296           (GDestroyNotify) g_hash_table_unref);
297     }
298   else if (g_hash_table_lookup (names, name) != NULL)
299     {
300       return;
301     }
302
303   /* Store the geometry name in the window's data */
304   g_hash_table_insert (names, g_strdup (name), GUINT_TO_POINTER (TRUE));
305
306   /* Load initial geometry */
307   empathy_geometry_load (window, name);
308
309   if (!connect_sigs)
310     return;
311
312   /* Track geometry changes */
313   g_signal_connect (window, "configure-event",
314     G_CALLBACK (geometry_configure_event_cb), NULL);
315   g_signal_connect (window, "window-state-event",
316     G_CALLBACK (geometry_window_state_event_cb), NULL);
317   g_signal_connect (window, "map",
318     G_CALLBACK (geometry_map_cb), NULL);
319 }
320
321 void
322 empathy_geometry_unbind (GtkWindow *window,
323     const gchar *name)
324 {
325   GHashTable *names;
326
327   names = g_object_get_data (G_OBJECT (window), GEOMETRY_NAME_KEY);
328   if (names == NULL)
329     return;
330
331   g_hash_table_remove (names, name);
332
333   if (g_hash_table_size (names) > 0)
334     return;
335
336   g_signal_handlers_disconnect_by_func (window,
337     geometry_configure_event_cb, NULL);
338   g_signal_handlers_disconnect_by_func (window,
339     geometry_window_state_event_cb, NULL);
340   g_signal_handlers_disconnect_by_func (window,
341     geometry_map_cb, NULL);
342
343   g_object_set_data (G_OBJECT (window), GEOMETRY_NAME_KEY, NULL);
344 }