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>
31 #include "libempathy/empathy-utils.h"
32 #include "empathy-geometry.h"
33 #include "empathy-ui-utils.h"
35 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
36 #include <libempathy/empathy-debug.h>
38 #define GEOMETRY_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
39 #define GEOMETRY_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
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"
50 /* Key used to keep window's name inside the object's qdata */
51 #define GEOMETRY_NAME_KEY "geometry-name-key"
53 static guint store_id = 0;
56 geometry_real_store (GKeyFile *key_file)
63 content = g_key_file_to_data (key_file, &length, &error);
66 DEBUG ("Error: %s", error->message);
71 filename = g_build_filename (g_get_user_config_dir (),
72 PACKAGE_NAME, GEOMETRY_FILENAME, NULL);
74 if (!g_file_set_contents (filename, content, length, &error))
76 DEBUG ("Error: %s", error->message);
85 geometry_store_cb (gpointer key_file)
87 geometry_real_store (key_file);
94 geometry_schedule_store (GKeyFile *key_file)
97 g_source_remove (store_id);
99 store_id = g_timeout_add_seconds (1, geometry_store_cb, key_file);
103 geometry_get_key_file (void)
105 static GKeyFile *key_file = NULL;
109 if (key_file != NULL)
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))
115 DEBUG ("Creating directory:'%s'", dir);
116 g_mkdir_with_parents (dir, GEOMETRY_DIR_CREATE_MODE);
119 filename = g_build_filename (dir, GEOMETRY_FILENAME, NULL);
122 key_file = g_key_file_new ();
123 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
130 empathy_geometry_save (GtkWindow *window)
133 GdkWindow *gdk_window;
134 GdkWindowState window_state;
137 gchar *position_str = NULL;
142 names = g_object_get_data (G_OBJECT (window), GEOMETRY_NAME_KEY);
144 g_return_if_fail (GTK_IS_WINDOW (window));
145 g_return_if_fail (names != NULL);
147 if (!gtk_widget_get_visible (GTK_WIDGET (window)))
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;
157 /* Don't save off-screen positioning */
158 if (!EMPATHY_RECT_IS_ON_SCREEN (x, y, w, h))
161 key_file = geometry_get_key_file ();
163 /* Save window size only if not maximized */
166 position_str = g_strdup_printf (GEOMETRY_POSITION_FORMAT, x, y, w, h);
169 g_hash_table_iter_init (&iter, names);
170 while (g_hash_table_iter_next (&iter, (gpointer) &name, NULL))
174 /* escape the name so that unwanted characters such as # are removed */
175 escaped_name = g_uri_escape_string (name, NULL, TRUE);
177 g_key_file_set_boolean (key_file, GEOMETRY_MAXIMIZED_GROUP,
178 escaped_name, maximized);
180 if (position_str != NULL)
181 g_key_file_set_string (key_file, GEOMETRY_POSITION_GROUP,
182 escaped_name, position_str);
184 g_free (escaped_name);
188 geometry_schedule_store (key_file);
189 g_free (position_str);
193 empathy_geometry_load (GtkWindow *window,
201 g_return_if_fail (GTK_IS_WINDOW (window));
202 g_return_if_fail (!EMP_STR_EMPTY (name));
204 /* escape the name so that unwanted characters such as # are removed */
205 escaped_name = g_uri_escape_string (name, NULL, TRUE);
207 key_file = geometry_get_key_file ();
209 /* restore window size and position */
210 str = g_key_file_get_string (key_file, GEOMETRY_POSITION_GROUP,
216 sscanf (str, GEOMETRY_POSITION_FORMAT, &x, &y, &w, &h);
217 gtk_window_move (window, x, y);
218 gtk_window_resize (window, w, h);
221 /* restore window maximized state */
222 maximized = g_key_file_get_boolean (key_file, GEOMETRY_MAXIMIZED_GROUP,
226 gtk_window_maximize (window);
228 gtk_window_unmaximize (window);
231 g_free (escaped_name);
235 geometry_configure_event_cb (GtkWindow *window,
236 GdkEventConfigure *event,
239 empathy_geometry_save (window);
245 geometry_window_state_event_cb (GtkWindow *window,
246 GdkEventWindowState *event,
249 if ((event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED) != 0)
251 empathy_geometry_save (window);
258 geometry_map_cb (GtkWindow *window,
265 /* The WM will replace this window, restore its last position */
266 names = g_object_get_data (G_OBJECT (window), GEOMETRY_NAME_KEY);
268 g_assert (names != NULL);
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));
274 empathy_geometry_load (window, name);
278 empathy_geometry_bind (GtkWindow *window,
282 gboolean connect_sigs = FALSE;
284 g_return_if_fail (GTK_IS_WINDOW (window));
285 g_return_if_fail (!EMP_STR_EMPTY (name));
287 /* Check if this window is already bound */
288 names = g_object_get_data (G_OBJECT (window), GEOMETRY_NAME_KEY);
292 names = g_hash_table_new_full (g_str_hash, g_str_equal,
295 g_object_set_data_full (G_OBJECT (window), GEOMETRY_NAME_KEY, names,
296 (GDestroyNotify) g_hash_table_unref);
298 else if (g_hash_table_lookup (names, name) != NULL)
303 /* Store the geometry name in the window's data */
304 g_hash_table_insert (names, g_strdup (name), GUINT_TO_POINTER (TRUE));
306 /* Load initial geometry */
307 empathy_geometry_load (window, name);
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);
322 empathy_geometry_unbind (GtkWindow *window,
327 names = g_object_get_data (G_OBJECT (window), GEOMETRY_NAME_KEY);
331 g_hash_table_remove (names, name);
333 if (g_hash_table_size (names) > 0)
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);
343 g_object_set_data (G_OBJECT (window), GEOMETRY_NAME_KEY, NULL);