* compile fixes
[modest] / src / modest-widget-memory.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "modest-widget-memory.h"
31
32 #define PARAM_X           "x"
33 #define PARAM_Y           "y"
34 #define PARAM_HEIGHT      "height"
35 #define PARAM_WIDTH       "width"
36 #define PARAM_POS         "pos"
37
38 static gchar*
39 get_keyname (ModestConf *conf, const gchar *name, const gchar *param)
40 {
41         gchar *esc_name, *keyname;
42         esc_name = modest_conf_key_escape (conf, name);
43
44         keyname = g_strdup_printf ("%s/%s/%s",
45                                    MODEST_CONF_WIDGET_NAMESPACE, 
46                                    esc_name, param);
47         g_free (esc_name);
48         return keyname;
49 }
50
51
52
53 static gboolean
54 save_settings_widget (ModestConf *conf, GtkWidget *widget, const gchar *name)
55 {
56         gchar *key;
57
58         key = get_keyname (conf, name, PARAM_HEIGHT);
59         modest_conf_set_int (conf, key, GTK_WIDGET(widget)->allocation.height, NULL);
60         g_free (key);
61         
62         key = get_keyname (conf, name, PARAM_WIDTH);
63         modest_conf_set_int (conf, key, GTK_WIDGET(widget)->allocation.width, NULL);
64         g_free (key);
65
66         return TRUE;
67 }
68
69
70 static gboolean
71 restore_settings_widget (ModestConf *conf, GtkWidget *widget, const gchar *name)
72 {
73         GtkRequisition req;
74         gchar *key;
75
76         key = get_keyname (conf, name, PARAM_HEIGHT);
77         
78         if (modest_conf_key_exists (conf, key, NULL))
79                 req.height = modest_conf_get_int (conf, key, NULL);
80
81         g_free (key);
82         
83         key = get_keyname (conf, name, PARAM_WIDTH);
84         if (modest_conf_key_exists (conf, key, NULL))
85                 req.width = modest_conf_get_int (conf, key, NULL);
86         g_free (key);
87
88         if (req.height && req.width) 
89                 gtk_widget_size_request (widget, &req);
90
91         return TRUE;
92
93 }
94
95
96
97 static gboolean
98 save_settings_window (ModestConf *conf, GtkWindow *win, const gchar *name)
99 {
100         gchar *key;
101         int height, width;
102         
103         gtk_window_get_size (win, &width, &height);
104         
105         key = get_keyname (conf, name, PARAM_HEIGHT);
106         modest_conf_set_int (conf, key, height, NULL);
107         g_free (key);
108         
109         key = get_keyname (conf, name, PARAM_WIDTH);
110         modest_conf_set_int (conf, key, width, NULL);
111         g_free (key);
112
113         return TRUE;
114 }
115
116
117 static gboolean
118 restore_settings_window (ModestConf *conf, GtkWindow *win, const gchar *name)
119 {
120         gchar *key;
121         int height = 0, width = 0;
122
123         key = get_keyname (conf, name, PARAM_HEIGHT);
124         
125         if (modest_conf_key_exists (conf, key, NULL))
126                 height = modest_conf_get_int (conf, key, NULL);
127
128         g_free (key);
129
130         key = get_keyname (conf, name, PARAM_WIDTH);
131         if (modest_conf_key_exists (conf, key, NULL))
132                 width = modest_conf_get_int (conf, key, NULL);
133
134         g_free (key);
135
136         if (height && width)
137                 gtk_window_set_default_size (win, width, height);
138
139         return TRUE;
140 }
141
142
143 static gboolean
144 save_settings_paned (ModestConf *conf, GtkPaned *paned, const gchar *name)
145 {
146         gchar *key;
147         int pos;
148
149         pos = gtk_paned_get_position (paned);
150         
151         key = get_keyname (conf, name, PARAM_POS);
152         modest_conf_set_int (conf, key, pos, NULL);
153         g_free (key);
154         
155         return TRUE;
156 }
157
158
159 static gboolean
160 restore_settings_paned (ModestConf *conf, GtkPaned *paned, const gchar *name)
161 {
162         gchar *key;
163         int pos;
164         
165         key = get_keyname (conf, name, PARAM_POS);
166         
167         if (modest_conf_key_exists (conf, key, NULL)) {
168                 pos = modest_conf_get_int (conf, key, NULL);
169                 gtk_paned_set_position (paned, pos);
170         }
171
172         g_free (key);
173         return TRUE;
174 }
175
176
177 gboolean
178 modest_widget_memory_save_settings (ModestConf *conf, GtkWidget *widget,
179                                     const gchar *name)
180 {
181         g_return_val_if_fail (conf, FALSE);
182         g_return_val_if_fail (widget, FALSE);
183         g_return_val_if_fail (name, FALSE);
184
185         if (GTK_IS_WINDOW(widget))
186                 return save_settings_window (conf, (GtkWindow*)widget, name);
187         else if (GTK_IS_PANED(widget))
188                 return save_settings_paned (conf, (GtkPaned*)widget, name);
189         else if (GTK_IS_WIDGET(widget))
190                 return save_settings_widget (conf, widget, name);
191
192         return TRUE;
193 }
194
195
196
197 gboolean
198 modest_widget_memory_restore_settings (ModestConf *conf, GtkWidget *widget,
199                                        const gchar *name)
200 {
201         g_return_val_if_fail (conf, FALSE);
202         g_return_val_if_fail (widget, FALSE);
203         g_return_val_if_fail (name, FALSE);
204         
205         if (GTK_IS_WINDOW(widget))
206                 return restore_settings_window (conf, (GtkWindow*)widget, name);
207         else if (GTK_IS_PANED(widget))
208                 return restore_settings_paned (conf, (GtkPaned*)widget, name);
209         else if (GTK_IS_WIDGET(widget))
210                 return restore_settings_widget (conf, widget, name);
211         
212         return TRUE;
213 }