]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-geometry.c
add myself to AUTHORS
[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 void
130 empathy_geometry_save (GtkWindow *window,
131     const gchar *name)
132 {
133   GKeyFile *key_file;
134   GdkWindow *gdk_window;
135   GdkWindowState window_state;
136   gchar *escaped_name;
137   gint x, y, w, h;
138   gboolean maximized;
139
140   g_return_if_fail (GTK_IS_WINDOW (window));
141   g_return_if_fail (!EMP_STR_EMPTY (name));
142
143   if (!GTK_WIDGET_VISIBLE (window))
144     return;
145
146   /* escape the name so that unwanted characters such as # are removed */
147   escaped_name = g_uri_escape_string (name, NULL, TRUE);
148
149   /* Get window geometry */
150   gtk_window_get_position (window, &x, &y);
151   gtk_window_get_size (window, &w, &h);
152   gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
153   window_state = gdk_window_get_state (gdk_window);
154   maximized = (window_state & GDK_WINDOW_STATE_MAXIMIZED) != 0;
155
156   /* Don't save off-screen positioning */
157   if (!EMPATHY_RECT_IS_ON_SCREEN (x, y, w, h))
158     return;
159
160   key_file = geometry_get_key_file ();
161
162   /* Save window size only if not maximized */
163   if (!maximized)
164     {
165       gchar *str;
166
167       str = g_strdup_printf (GEOMETRY_POSITION_FORMAT, x, y, w, h);
168       g_key_file_set_string (key_file, GEOMETRY_POSITION_GROUP,
169           escaped_name, str);
170       g_free (str);
171     }
172
173   g_key_file_set_boolean (key_file, GEOMETRY_MAXIMIZED_GROUP,
174       escaped_name, maximized);
175
176   geometry_schedule_store (key_file);
177 }
178
179 void
180 empathy_geometry_load (GtkWindow *window,
181     const gchar *name)
182 {
183   GKeyFile *key_file;
184   gchar    *escaped_name;
185   gchar    *str;
186   gboolean  maximized;
187
188   g_return_if_fail (GTK_IS_WINDOW (window));
189   g_return_if_fail (!EMP_STR_EMPTY (name));
190
191   /* escape the name so that unwanted characters such as # are removed */
192   escaped_name = g_uri_escape_string (name, NULL, TRUE);
193
194   key_file = geometry_get_key_file ();
195
196   /* restore window size and position */
197   str = g_key_file_get_string (key_file, GEOMETRY_POSITION_GROUP,
198       escaped_name, NULL);
199   if (str)
200     {
201       gint x, y, w, h;
202
203       sscanf (str, GEOMETRY_POSITION_FORMAT, &x, &y, &w, &h);
204       gtk_window_move (window, x, y);
205       gtk_window_resize (window, w, h);
206     }
207
208   /* restore window maximized state */
209   maximized = g_key_file_get_boolean (key_file, GEOMETRY_MAXIMIZED_GROUP,
210       escaped_name, NULL);
211
212   if (maximized)
213     gtk_window_maximize (window);
214   else
215     gtk_window_unmaximize (window);
216
217   g_free (str);
218   g_free (escaped_name);
219 }
220
221 static gboolean
222 geometry_configure_event_cb (GtkWindow *window,
223     GdkEventConfigure *event,
224     gpointer user_data)
225 {
226   gchar *name;
227
228   name = g_object_get_data (G_OBJECT (window), GEOMETRY_NAME_KEY);
229   empathy_geometry_save (window, name);
230
231   return FALSE;
232 }
233
234 static gboolean
235 geometry_window_state_event_cb (GtkWindow *window,
236     GdkEventWindowState *event,
237     gpointer user_data)
238 {
239   if ((event->changed_mask & GDK_WINDOW_STATE_MAXIMIZED) != 0)
240     {
241       gchar *name;
242
243       name = g_object_get_data (G_OBJECT (window), GEOMETRY_NAME_KEY);
244       empathy_geometry_save (window, name);
245     }
246
247   return FALSE;
248 }
249
250 static void
251 geometry_map_cb (GtkWindow *window,
252     gpointer user_data)
253 {
254   gchar *name;
255
256   /* The WM will replace this window, restore its last position */
257   name = g_object_get_data (G_OBJECT (window), GEOMETRY_NAME_KEY);
258   empathy_geometry_load (window, name);
259 }
260
261 void
262 empathy_geometry_bind (GtkWindow *window,
263     const gchar *name)
264 {
265   gchar *str;
266
267   g_return_if_fail (GTK_IS_WINDOW (window));
268   g_return_if_fail (!EMP_STR_EMPTY (name));
269
270   /* Check if this window is already bound */
271   str = g_object_get_data (G_OBJECT (window), GEOMETRY_NAME_KEY);
272   if (str != NULL)
273     return;
274
275   /* Store the geometry name in the window's data */
276   str = g_strdup (name);
277   g_object_set_data_full (G_OBJECT (window), GEOMETRY_NAME_KEY, str, g_free);
278
279   /* Load initial geometry */
280   empathy_geometry_load (window, name);
281
282   /* Track geometry changes */
283   g_signal_connect (window, "configure-event",
284     G_CALLBACK (geometry_configure_event_cb), NULL);
285   g_signal_connect (window, "window-state-event",
286     G_CALLBACK (geometry_window_state_event_cb), NULL);
287   g_signal_connect (window, "map",
288     G_CALLBACK (geometry_map_cb), NULL);
289 }
290
291 void
292 empathy_geometry_unbind (GtkWindow *window)
293 {
294   g_signal_handlers_disconnect_by_func (window,
295     geometry_configure_event_cb, NULL);
296   g_signal_handlers_disconnect_by_func (window,
297     geometry_window_state_event_cb, NULL);
298   g_signal_handlers_disconnect_by_func (window,
299     geometry_map_cb, NULL);
300
301   g_object_set_data (G_OBJECT (window), GEOMETRY_NAME_KEY, NULL);
302 }