Update python binding
[empathy.git] / libempathy-gtk / empathy-geometry.c
blobb3bd09ca149e0c4add646fcd8e0e5047fc577608
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.
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.
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.
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.
21 * Authors: Martyn Russell <martyn@imendio.com>
22 * Xavier Claessens <xclaesse@gmail.com>
25 #include "config.h"
27 #include <sys/stat.h>
29 #include <glib.h>
30 #include <gdk/gdk.h>
32 #include "empathy-geometry.h"
34 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
35 #include <libempathy/empathy-debug.h>
37 #define GEOMETRY_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
38 #define GEOMETRY_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
40 #define GEOMETRY_KEY_FILENAME "geometry.ini"
41 #define GEOMETRY_FORMAT "%d,%d,%d,%d"
42 #define GEOMETRY_GROUP_NAME "geometry"
44 static gchar *geometry_get_filename (void);
46 static gchar *
47 geometry_get_filename (void)
49 gchar *dir;
50 gchar *filename;
52 dir = g_build_filename (g_get_home_dir (), ".gnome2", 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);
58 filename = g_build_filename (dir, GEOMETRY_KEY_FILENAME, NULL);
59 g_free (dir);
61 return filename;
64 void
65 empathy_geometry_save (const gchar *name,
66 gint x,
67 gint y,
68 gint w,
69 gint h)
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;
81 DEBUG ("Saving window geometry: x:%d, y:%d, w:%d, h:%d\n",
82 x, y, w, h);
84 screen = gdk_screen_get_default ();
85 max_width = gdk_screen_get_width (screen);
86 max_height = gdk_screen_get_height (screen);
88 w = CLAMP (w, 100, max_width);
89 h = CLAMP (h, 100, max_height);
91 x = CLAMP (x, 0, max_width - w);
92 y = CLAMP (y, 0, max_height - h);
94 str = g_strdup_printf (GEOMETRY_FORMAT, x, y, w, h);
96 key_file = g_key_file_new ();
98 filename = geometry_get_filename ();
100 g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL);
101 g_key_file_set_string (key_file, GEOMETRY_GROUP_NAME, name, str);
103 g_free (str);
105 content = g_key_file_to_data (key_file, &length, NULL);
106 if (!g_file_set_contents (filename, content, length, &error)) {
107 g_warning ("Couldn't save window geometry, error:%d->'%s'",
108 error->code, error->message);
109 g_error_free (error);
112 g_free (content);
113 g_free (filename);
114 g_key_file_free (key_file);
117 void
118 empathy_geometry_load (const gchar *name,
119 gint *x,
120 gint *y,
121 gint *w,
122 gint *h)
124 GKeyFile *key_file;
125 gchar *filename;
126 gchar *str = NULL;
128 if (x) {
129 *x = -1;
132 if (y) {
133 *y = -1;
136 if (w) {
137 *w = -1;
140 if (h) {
141 *h = -1;
144 key_file = g_key_file_new ();
146 filename = geometry_get_filename ();
148 if (g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, NULL)) {
149 str = g_key_file_get_string (key_file, GEOMETRY_GROUP_NAME, name, NULL);
152 if (str) {
153 gint tmp_x, tmp_y, tmp_w, tmp_h;
155 sscanf (str, GEOMETRY_FORMAT, &tmp_x, &tmp_y, &tmp_w, &tmp_h);
157 if (x) {
158 *x = tmp_x;
161 if (y) {
162 *y = tmp_y;
165 if (w) {
166 *w = tmp_w;
169 if (h) {
170 *h = tmp_h;
173 g_free (str);
176 DEBUG ("Loading window geometry: x:%d, y:%d, w:%d, h:%d\n",
177 x ? *x : -1, y ? *y : -1, w ? *w : -1, h ? *h : -1);
179 g_free (filename);
180 g_key_file_free (key_file);