]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-geometry.c
Remove the autogen.sh script and use gnome-autogen.sh instead.
[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 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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, 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 <libempathy/empathy-debug.h>
33
34 #include "empathy-geometry.h"
35
36 #define DEBUG_DOMAIN "Geometry"
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 #define GEOMETRY_KEY_FILENAME     "geometry.ini"
42 #define GEOMETRY_FORMAT           "%d,%d,%d,%d"
43 #define GEOMETRY_GROUP_NAME       "geometry"
44
45 static gchar *geometry_get_filename (void);
46
47 static gchar *
48 geometry_get_filename (void)
49 {
50         gchar *dir;
51         gchar *filename;
52
53         dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
54         if (!g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
55                 empathy_debug (DEBUG_DOMAIN, "Creating directory:'%s'", dir);
56                 g_mkdir_with_parents (dir, GEOMETRY_DIR_CREATE_MODE);
57         }
58
59         filename = g_build_filename (dir, GEOMETRY_KEY_FILENAME, NULL);
60         g_free (dir);
61
62         return filename;
63 }
64
65 void
66 empathy_geometry_save (const gchar *name,
67                       gint         x,
68                       gint         y,
69                       gint         w,
70                       gint         h)
71 {
72         GError      *error = NULL;
73         GKeyFile    *key_file;
74         gchar       *filename;
75         GdkScreen   *screen;
76         gint         max_width;
77         gint         max_height;
78         gchar       *content;
79         gsize        length;
80         gchar       *str;
81
82         empathy_debug (DEBUG_DOMAIN, "Saving window geometry: x:%d, y:%d, w:%d, h:%d\n",
83                       x, y, w, h);
84
85         screen = gdk_screen_get_default ();
86         max_width = gdk_screen_get_width (screen);
87         max_height = gdk_screen_get_height (screen);
88
89         w = CLAMP (w, 100, max_width);
90         h = CLAMP (h, 100, max_height);
91
92         x = CLAMP (x, 0, max_width - w);
93         y = CLAMP (y, 0, max_height - h);
94
95         str = g_strdup_printf (GEOMETRY_FORMAT, x, y, w, h);
96
97         key_file = g_key_file_new ();
98
99         filename = geometry_get_filename ();
100
101         g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
102         g_key_file_set_string (key_file, GEOMETRY_GROUP_NAME, name, str);
103
104         g_free (str);
105
106         content = g_key_file_to_data (key_file, &length, NULL);
107         if (!g_file_set_contents (filename, content, length, &error)) {
108                 g_warning ("Couldn't save window geometry, error:%d->'%s'",
109                            error->code, error->message);
110                 g_error_free (error);
111         }
112
113         g_free (content);
114         g_free (filename);
115         g_key_file_free (key_file);
116 }
117
118 void
119 empathy_geometry_load (const gchar *name,
120                       gint        *x,
121                       gint        *y,
122                       gint        *w,
123                       gint        *h)
124 {
125         GKeyFile    *key_file;
126         gchar       *filename;
127         gchar       *str = NULL;
128
129         if (x) {
130                 *x = -1;
131         }
132
133         if (y) {
134                 *y = -1;
135         }
136
137         if (w) {
138                 *w = -1;
139         }
140
141         if (h) {
142                 *h = -1;
143         }
144
145         key_file = g_key_file_new ();
146
147         filename = geometry_get_filename ();
148
149         if (g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL)) {
150                 str = g_key_file_get_string (key_file, GEOMETRY_GROUP_NAME, name, NULL);
151         }
152
153         if (str) {
154                 gint tmp_x, tmp_y, tmp_w, tmp_h;
155
156                 sscanf (str, GEOMETRY_FORMAT, &tmp_x, &tmp_y, &tmp_w, &tmp_h);
157
158                 if (x) {
159                         *x = tmp_x;
160                 }
161
162                 if (y) {
163                         *y = tmp_y;
164                 }
165
166                 if (w) {
167                         *w = tmp_w;
168                 }
169
170                 if (h) {
171                         *h = tmp_h;
172                 }
173
174                 g_free (str);
175         }
176
177         empathy_debug (DEBUG_DOMAIN, "Loading window geometry: x:%d, y:%d, w:%d, h:%d\n",
178                       x ? *x : -1,
179                       y ? *y : -1,
180                       w ? *w : -1,
181                       h ? *h : -1);
182
183         g_free (filename);
184         g_key_file_free (key_file);
185 }
186