* modest-widget-memory.[ch]:
[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 #include <widgets/modest-header-view.h>
32 #include <string.h>
33
34 #define PARAM_X             "x"
35 #define PARAM_Y             "y"
36 #define PARAM_HEIGHT        "height"
37 #define PARAM_WIDTH         "width"
38 #define PARAM_POS           "pos"
39 #define PARAM_COLUMN_WIDTH  "column-width"
40
41 static gchar*
42 get_keyname (ModestConf *conf, const gchar *name, const gchar *param)
43 {
44         gchar *esc_name, *keyname;
45         esc_name = modest_conf_key_escape (conf, name);
46
47         keyname = g_strdup_printf ("%s/%s/%s",
48                                    MODEST_CONF_WIDGET_NAMESPACE, 
49                                    esc_name, param);
50         g_free (esc_name);
51         return keyname;
52 }
53
54
55
56 static gboolean
57 save_settings_widget (ModestConf *conf, GtkWidget *widget, const gchar *name)
58 {
59         gchar *key;
60
61         key = get_keyname (conf, name, PARAM_HEIGHT);
62         modest_conf_set_int (conf, key, GTK_WIDGET(widget)->allocation.height, NULL);
63         g_free (key);
64         
65         key = get_keyname (conf, name, PARAM_WIDTH);
66         modest_conf_set_int (conf, key, GTK_WIDGET(widget)->allocation.width, NULL);
67         g_free (key);
68
69         return TRUE;
70 }
71
72
73 static gboolean
74 restore_settings_widget (ModestConf *conf, GtkWidget *widget, const gchar *name)
75 {
76         GtkRequisition req;
77         gchar *key;
78
79         key = get_keyname (conf, name, PARAM_HEIGHT);
80         
81         if (modest_conf_key_exists (conf, key, NULL))
82                 req.height = modest_conf_get_int (conf, key, NULL);
83
84         g_free (key);
85         
86         key = get_keyname (conf, name, PARAM_WIDTH);
87         if (modest_conf_key_exists (conf, key, NULL))
88                 req.width = modest_conf_get_int (conf, key, NULL);
89         g_free (key);
90
91         if (req.height && req.width) 
92                 gtk_widget_size_request (widget, &req);
93
94         return TRUE;
95
96 }
97
98
99
100 static gboolean
101 save_settings_window (ModestConf *conf, GtkWindow *win, const gchar *name)
102 {
103         gchar *key;
104         int height, width;
105         
106         gtk_window_get_size (win, &width, &height);
107         
108         key = get_keyname (conf, name, PARAM_HEIGHT);
109         modest_conf_set_int (conf, key, height, NULL);
110         g_free (key);
111         
112         key = get_keyname (conf, name, PARAM_WIDTH);
113         modest_conf_set_int (conf, key, width, NULL);
114         g_free (key);
115         
116         return TRUE;
117 }
118
119
120 static gboolean
121 restore_settings_window (ModestConf *conf, GtkWindow *win, const gchar *name)
122 {
123         gchar *key;
124         int height = 0, width = 0;
125
126         key = get_keyname (conf, name, PARAM_HEIGHT);
127         
128         if (modest_conf_key_exists (conf, key, NULL))
129                 height = modest_conf_get_int (conf, key, NULL);
130
131         g_free (key);
132
133         key = get_keyname (conf, name, PARAM_WIDTH);
134         if (modest_conf_key_exists (conf, key, NULL))
135                 width = modest_conf_get_int (conf, key, NULL);
136
137         g_free (key);
138
139         if (height && width)
140                 gtk_window_set_default_size (win, width, height);
141
142         return TRUE;
143 }
144
145
146 static gboolean
147 save_settings_paned (ModestConf *conf, GtkPaned *paned, const gchar *name)
148 {
149         gchar *key;
150         int pos;
151
152         pos = gtk_paned_get_position (paned);
153         
154         key = get_keyname (conf, name, PARAM_POS);
155         modest_conf_set_int (conf, key, pos, NULL);
156         g_free (key);
157         
158         return TRUE;
159 }
160
161
162 static gboolean
163 restore_settings_paned (ModestConf *conf, GtkPaned *paned, const gchar *name)
164 {
165         gchar *key;
166         int pos;
167         
168         key = get_keyname (conf, name, PARAM_POS);
169         
170         if (modest_conf_key_exists (conf, key, NULL)) {
171                 pos = modest_conf_get_int (conf, key, NULL);
172                 gtk_paned_set_position (paned, pos);
173         }
174
175         g_free (key);
176         return TRUE;
177 }
178
179
180 static gboolean
181 save_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
182                            const gchar *name)
183 {
184         gchar *key;
185         GString *str;
186         GList *cols, *cursor;
187
188         cursor = cols = modest_header_view_get_columns (header_view);
189         str = g_string_new (NULL);
190         
191         while (cursor) {
192
193                 int col_id, width;
194                 GtkTreeViewColumn *col;
195                 
196                 col    = GTK_TREE_VIEW_COLUMN (cursor->data);
197                 col_id = GPOINTER_TO_INT(g_object_get_data (G_OBJECT(col),
198                                                             MODEST_HEADER_VIEW_COLUMN));
199                 width = gtk_tree_view_column_get_width (col);
200                 
201                 g_string_append_printf (str, "%d:%d ", col_id, width);  
202                 
203                 cursor = g_list_next (cursor);
204         }
205
206         key = get_keyname (conf, name, PARAM_COLUMN_WIDTH);
207         modest_conf_set_string (conf, key, str->str, NULL);
208         
209         g_string_free (str, TRUE);
210         g_list_free (cols);
211         
212         return TRUE;
213 }
214
215
216
217 static gboolean
218 restore_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
219                               const gchar *name)
220 {
221         gchar *key;     
222         key = get_keyname (conf, name, PARAM_COLUMN_WIDTH);
223         
224         if (modest_conf_key_exists (conf, key, NULL)) {
225
226                 gchar *data, *cursor;
227                 guint col, width;
228                 GList *cols = NULL;
229                 GList *colwidths = NULL;
230                 
231                 cursor = data = modest_conf_get_string (conf, key, NULL);
232                 while (cursor && sscanf (cursor, "%u:%u ", &col, &width) == 2) {
233                         cols      = g_list_append (cols, GINT_TO_POINTER(col));
234                         colwidths = g_list_append (colwidths, GINT_TO_POINTER(width));
235                         cursor = strchr (cursor + 1, ' ');
236                 }
237                 g_free (data);  
238                 
239                 if (cols) {
240                         GList *viewcolumns, *colcursor, *widthcursor;
241                         modest_header_view_set_columns (header_view, cols);
242
243                         widthcursor = colwidths;
244                         colcursor = viewcolumns = gtk_tree_view_get_columns (GTK_TREE_VIEW(header_view));
245                         while (colcursor && widthcursor) {
246                                 gtk_tree_view_column_set_fixed_width(GTK_TREE_VIEW_COLUMN(colcursor->data),
247                                                                      GPOINTER_TO_INT(widthcursor->data));
248                                 colcursor = g_list_next (colcursor);
249                                 widthcursor = g_list_next (widthcursor);
250                         }
251                         g_list_free (cols);
252                         g_list_free (colwidths);
253                         g_list_free (viewcolumns);
254                 }
255         }
256
257         g_free (key);
258         return TRUE;
259 }
260
261
262 gboolean
263 modest_widget_memory_save_settings (ModestConf *conf, GtkWidget *widget,
264                                     const gchar *name)
265 {
266         g_return_val_if_fail (conf, FALSE);
267         g_return_val_if_fail (widget, FALSE);
268         g_return_val_if_fail (name, FALSE);
269         
270         if (GTK_IS_WINDOW(widget))
271                 return save_settings_window (conf, GTK_WINDOW(widget), name);
272         else if (GTK_IS_PANED(widget))
273                 return save_settings_paned (conf, GTK_PANED(widget), name);
274         else if (MODEST_IS_HEADER_VIEW(widget))
275                 return save_settings_header_view (conf, MODEST_HEADER_VIEW(widget), name);
276         else if (GTK_IS_WIDGET(widget))
277                 return save_settings_widget (conf, widget, name);
278         
279         g_printerr ("modest: %p is not a known widget\n", widget);
280         return FALSE;
281 }
282
283
284
285 gboolean
286 modest_widget_memory_restore_settings (ModestConf *conf, GtkWidget *widget,
287                                        const gchar *name)
288 {
289         g_return_val_if_fail (conf, FALSE);
290         g_return_val_if_fail (widget, FALSE);
291         g_return_val_if_fail (name, FALSE);
292         
293         if (GTK_IS_WINDOW(widget))
294                 return restore_settings_window (conf, GTK_WINDOW(widget), name);
295         else if (GTK_IS_PANED(widget))
296                 return restore_settings_paned (conf, GTK_PANED(widget), name);
297         else if (MODEST_IS_HEADER_VIEW(widget))
298                 return restore_settings_header_view (conf, MODEST_HEADER_VIEW(widget), name);
299         else if (GTK_IS_WIDGET(widget))
300                 return restore_settings_widget (conf, widget, name);
301         
302         g_printerr ("modest: %p is not a known widget\n", widget);
303         return FALSE;
304 }