]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-geometry.c
Put configuration data in XDG_CONFIG_DIRS (GNOME bug 494007)
[empathy.git] / libempathy-gtk / empathy-geometry.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2006-2007 Imendio AB
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * Authors: Martyn Russell <martyn@imendio.com>
22  *          Xavier Claessens <xclaesse@gmail.com>
23  */
24
25 #include "config.h"
26
27 #include <sys/stat.h>
28
29 #include <glib.h>
30 #include <gdk/gdk.h>
31
32 #include "empathy-geometry.h"
33
34 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
35 #include <libempathy/empathy-debug.h>
36
37 #define GEOMETRY_DIR_CREATE_MODE  (S_IRUSR | S_IWUSR | S_IXUSR)
38 #define GEOMETRY_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
39
40 #define GEOMETRY_KEY_FILENAME     "geometry.ini"
41 #define GEOMETRY_FORMAT           "%d,%d,%d,%d"
42 #define GEOMETRY_GROUP_NAME       "geometry"
43
44 static gchar *geometry_get_filename (void);
45
46 static gchar *
47 geometry_get_filename (void)
48 {
49         gchar *dir;
50         gchar *filename;
51
52         dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
53         if (!g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
54                 DEBUG ("Creating directory:'%s'", dir);
55                 g_mkdir_with_parents (dir, GEOMETRY_DIR_CREATE_MODE);
56         }
57
58         filename = g_build_filename (dir, GEOMETRY_KEY_FILENAME, NULL);
59         g_free (dir);
60
61         return filename;
62 }
63
64 void
65 empathy_geometry_save (const gchar *name,
66                       gint         x,
67                       gint         y,
68                       gint         w,
69                       gint         h)
70 {
71         GError      *error = NULL;
72         GKeyFile    *key_file;
73         gchar       *filename;
74         GdkScreen   *screen;
75         gint         max_width;
76         gint         max_height;
77         gchar       *content;
78         gsize        length;
79         gchar       *str;
80         gchar       *escaped_name;
81
82         /* escape the name so that unwanted characters such as # are removed */
83         escaped_name = g_uri_escape_string (name, NULL, TRUE);
84
85         DEBUG ("Saving window geometry: name:%s x:%d, y:%d, w:%d, h:%d\n",
86                 escaped_name, x, y, w, h);
87
88         screen = gdk_screen_get_default ();
89         max_width = gdk_screen_get_width (screen);
90         max_height = gdk_screen_get_height (screen);
91
92         w = CLAMP (w, 100, max_width);
93         h = CLAMP (h, 100, max_height);
94
95         x = CLAMP (x, 0, max_width - w);
96         y = CLAMP (y, 0, max_height - h);
97
98         str = g_strdup_printf (GEOMETRY_FORMAT, x, y, w, h);
99
100         key_file = g_key_file_new ();
101
102         filename = geometry_get_filename ();
103
104         g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
105         g_key_file_set_string (key_file, GEOMETRY_GROUP_NAME, escaped_name, str);
106
107         g_free (str);
108
109         content = g_key_file_to_data (key_file, &length, NULL);
110         if (!g_file_set_contents (filename, content, length, &error)) {
111                 g_warning ("Couldn't save window geometry, error:%d->'%s'",
112                            error->code, error->message);
113                 g_error_free (error);
114         }
115
116         g_free (content);
117         g_free (filename);
118         g_free (escaped_name);
119         g_key_file_free (key_file);
120 }
121
122 void
123 empathy_geometry_load (const gchar *name,
124                       gint        *x,
125                       gint        *y,
126                       gint        *w,
127                       gint        *h)
128 {
129         GKeyFile    *key_file;
130         gchar       *filename;
131         gchar       *str = NULL;
132         gchar       *escaped_name;
133
134         /* escape the name so that unwanted characters such as # are removed */
135         escaped_name = g_uri_escape_string (name, NULL, TRUE);
136
137         if (x) {
138                 *x = -1;
139         }
140
141         if (y) {
142                 *y = -1;
143         }
144
145         if (w) {
146                 *w = -1;
147         }
148
149         if (h) {
150                 *h = -1;
151         }
152
153         key_file = g_key_file_new ();
154
155         filename = geometry_get_filename ();
156
157         if (g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL)) {
158                 str = g_key_file_get_string (key_file, GEOMETRY_GROUP_NAME, escaped_name, NULL);
159         }
160
161         if (str) {
162                 gint tmp_x, tmp_y, tmp_w, tmp_h;
163
164                 sscanf (str, GEOMETRY_FORMAT, &tmp_x, &tmp_y, &tmp_w, &tmp_h);
165
166                 if (x) {
167                         *x = tmp_x;
168                 }
169
170                 if (y) {
171                         *y = tmp_y;
172                 }
173
174                 if (w) {
175                         *w = tmp_w;
176                 }
177
178                 if (h) {
179                         *h = tmp_h;
180                 }
181
182                 g_free (str);
183         }
184
185         DEBUG ("Loading window geometry: x:%d, y:%d, w:%d, h:%d\n",
186                 x ? *x : -1, y ? *y : -1, w ? *w : -1, h ? *h : -1);
187
188         g_free (filename);
189         g_free (escaped_name);
190         g_key_file_free (key_file);
191 }
192