2 * Copyright (C) 2006-2007 Imendio AB
3 * Copyright (C) 2007-2008 Collabora Ltd.
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.
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.
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
20 * Authors: Martyn Russell <martyn@imendio.com>
21 * Xavier Claessens <xclaesse@gmail.com>
25 #include "empathy-geometry.h"
29 #include "empathy-ui-utils.h"
30 #include "empathy-utils.h"
32 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
33 #include "empathy-debug.h"
35 #define GEOMETRY_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
36 #define GEOMETRY_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
38 /* geometry.ini file contains 2 groups:
39 * - one with position and size of each window
40 * - one with the maximized state of each window
41 * Windows are identified by a name. (e.g. "main-window") */
42 #define GEOMETRY_FILENAME "geometry.ini"
43 #define GEOMETRY_POSITION_FORMAT "%d,%d,%d,%d" /* "x,y,w,h" */
44 #define GEOMETRY_POSITION_GROUP "geometry"
45 #define GEOMETRY_MAXIMIZED_GROUP "maximized"
47 /* Key used to keep window's name inside the object's qdata */
48 #define GEOMETRY_NAME_KEY "geometry-name-key"
50 static guint store_id = 0;
53 geometry_real_store (GKeyFile *key_file)
60 content = g_key_file_to_data (key_file, &length, &error);
63 DEBUG ("Error: %s", error->message);
68 filename = g_build_filename (g_get_user_config_dir (),
69 PACKAGE_NAME, GEOMETRY_FILENAME, NULL);
71 if (!g_file_set_contents (filename, content, length, &error))
73 DEBUG ("Error: %s", error->message);
82 geometry_store_cb (gpointer key_file)
84 geometry_real_store (key_file);
91 geometry_schedule_store (GKeyFile *key_file)
94 g_source_remove (store_id);
96 store_id = g_timeout_add_seconds (1, geometry_store_cb, key_file);
100 geometry_get_key_file (void)
102 static GKeyFile *key_file = NULL;
106 if (key_file != NULL)
109 dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
110 if (!g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
112 DEBUG ("Creating directory:'%s'", dir);
113 g_mkdir_with_parents (dir, GEOMETRY_DIR_CREATE_MODE);
116 filename = g_build_filename (dir, GEOMETRY_FILENAME, NULL);
119 key_file = g_key_file_new ();
120 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
127 empathy_geometry_save_values (GtkWindow *window,
135 gchar *position_str = NULL;
140 names = g_object_get_data (G_OBJECT (window), GEOMETRY_NAME_KEY);
142 g_return_if_fail (GTK_IS_WINDOW (window));
143 g_return_if_fail (names != NULL);
145 /* Don't save off-screen positioning */
146 if (!EMPATHY_RECT_IS_ON_SCREEN (x, y, w, h))
149 key_file = geometry_get_key_file ();
151 /* Save window size only if not maximized */
154 position_str = g_strdup_printf (GEOMETRY_POSITION_FORMAT, x, y, w, h);
157 g_hash_table_iter_init (&iter, names);
158 while (g_hash_table_iter_next (&iter, (gpointer) &name, NULL))
162 /* escape the name so that unwanted characters such as # are removed */
163 escaped_name = g_uri_escape_string (name, NULL, TRUE);
165 g_key_file_set_boolean (key_file, GEOMETRY_MAXIMIZED_GROUP,
166 escaped_name, maximized);
168 if (position_str != NULL)
169 g_key_file_set_string (key_file, GEOMETRY_POSITION_GROUP,
170 escaped_name, position_str);
172 g_free (escaped_name);
176 geometry_schedule_store (key_file);
177 g_free (position_str);
181 empathy_geometry_save (GtkWindow *window)
183 GdkWindow *gdk_window;
184 GdkWindowState window_state;
188 g_return_if_fail (GTK_IS_WINDOW (window));
190 if (!gtk_widget_get_visible (GTK_WIDGET (window)))
193 /* Get window geometry */
194 gtk_window_get_position (window, &x, &y);
195 gtk_window_get_size (window, &w, &h);
197 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
198 window_state = gdk_window_get_state (gdk_window);
199 maximized = (window_state & GDK_WINDOW_STATE_MAXIMIZED) != 0;
201 empathy_geometry_save_values (window, x, y, w, h, maximized);
205 empathy_geometry_load (GtkWindow *window,
213 g_return_if_fail (GTK_IS_WINDOW (window));
214 g_return_if_fail (!EMP_STR_EMPTY (name));
216 /* escape the name so that unwanted characters such as # are removed */
217 escaped_name = g_uri_escape_string (name, NULL, TRUE);
219 key_file = geometry_get_key_file ();
221 /* restore window size and position */
222 str = g_key_file_get_string (key_file, GEOMETRY_POSITION_GROUP,
228 sscanf (str, GEOMETRY_POSITION_FORMAT, &x, &y, &w, &h);
229 gtk_window_move (window, x, y);
230 gtk_window_resize (window, w, h);
233 /* restore window maximized state */
234 maximized = g_key_file_get_boolean (key_file, GEOMETRY_MAXIMIZED_GROUP,
238 gtk_window_maximize (window);
240 gtk_window_unmaximize (window);
243 g_free (escaped_name);
247 geometry_configure_event_cb (GtkWindow *window,
248 GdkEventConfigure *event,
251 empathy_geometry_save (window);
257 geometry_window_state_event_cb (GtkWindow *window,
258 GdkEventWindowState *event,
261 if ((event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED) != 0)
263 empathy_geometry_save (window);
270 geometry_map_cb (GtkWindow *window,
277 /* The WM will replace this window, restore its last position */
278 names = g_object_get_data (G_OBJECT (window), GEOMETRY_NAME_KEY);
280 g_assert (names != NULL);
282 /* Use the first name we get in the hash table */
283 g_hash_table_iter_init (&iter, names);
284 g_assert (g_hash_table_iter_next (&iter, (gpointer) &name, NULL));
286 empathy_geometry_load (window, name);
290 empathy_geometry_bind (GtkWindow *window,
294 gboolean connect_sigs = FALSE;
296 g_return_if_fail (GTK_IS_WINDOW (window));
297 g_return_if_fail (!EMP_STR_EMPTY (name));
299 /* Check if this window is already bound */
300 names = g_object_get_data (G_OBJECT (window), GEOMETRY_NAME_KEY);
304 names = g_hash_table_new_full (g_str_hash, g_str_equal,
307 g_object_set_data_full (G_OBJECT (window), GEOMETRY_NAME_KEY, names,
308 (GDestroyNotify) g_hash_table_unref);
310 else if (g_hash_table_lookup (names, name) != NULL)
315 /* Store the geometry name in the window's data */
316 g_hash_table_insert (names, g_strdup (name), GUINT_TO_POINTER (TRUE));
318 /* Load initial geometry */
319 empathy_geometry_load (window, name);
324 /* Track geometry changes */
325 g_signal_connect (window, "configure-event",
326 G_CALLBACK (geometry_configure_event_cb), NULL);
327 g_signal_connect (window, "window-state-event",
328 G_CALLBACK (geometry_window_state_event_cb), NULL);
329 g_signal_connect (window, "map",
330 G_CALLBACK (geometry_map_cb), NULL);
334 empathy_geometry_unbind (GtkWindow *window,
339 names = g_object_get_data (G_OBJECT (window), GEOMETRY_NAME_KEY);
343 g_hash_table_remove (names, name);
345 if (g_hash_table_size (names) > 0)
348 g_signal_handlers_disconnect_by_func (window,
349 geometry_configure_event_cb, NULL);
350 g_signal_handlers_disconnect_by_func (window,
351 geometry_window_state_event_cb, NULL);
352 g_signal_handlers_disconnect_by_func (window,
353 geometry_map_cb, NULL);
355 g_object_set_data (G_OBJECT (window), GEOMETRY_NAME_KEY, NULL);