* all:
[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 #include <modest-tny-platform-factory.h>
33 #include <modest-tny-folder.h>
34 #include <widgets/modest-header-view.h>
35 #include <widgets/modest-msg-view.h>
36 #include <widgets/modest-folder-view.h>
37
38 #include <string.h>
39
40 #define PARAM_X             "x"
41 #define PARAM_Y             "y"
42 #define PARAM_HEIGHT        "height"
43 #define PARAM_WIDTH         "width"
44 #define PARAM_POS           "pos"
45 #define PARAM_COLUMN_WIDTH  "column-width"
46
47 static gchar*
48 get_keyname (ModestConf *conf, const gchar *name, const gchar *param)
49 {
50         gchar *esc_name, *keyname;
51         esc_name = modest_conf_key_escape (name);
52
53         keyname = g_strdup_printf ("%s/%s/%s",
54                                    MODEST_CONF_WIDGET_NAMESPACE, 
55                                    esc_name, param);
56         g_free (esc_name);
57         return keyname;
58 }
59
60
61 static gchar*
62 get_keyname_with_type (ModestConf *conf, const gchar *name, guint type, const gchar *param)
63 {
64         gchar *esc_name, *keyname;
65         esc_name = modest_conf_key_escape (name);
66
67         keyname = g_strdup_printf ("%s/%s/%s_%d",
68                                    MODEST_CONF_WIDGET_NAMESPACE, 
69                                    esc_name, param, type);
70         g_free (esc_name);
71         return keyname;
72 }
73
74
75 static gboolean
76 save_settings_widget (ModestConf *conf, GtkWidget *widget, const gchar *name)
77 {
78         gchar *key;
79
80         key = get_keyname (conf, name, PARAM_HEIGHT);
81         modest_conf_set_int (conf, key, GTK_WIDGET(widget)->allocation.height, NULL);
82         g_free (key);
83         
84         key = get_keyname (conf, name, PARAM_WIDTH);
85         modest_conf_set_int (conf, key, GTK_WIDGET(widget)->allocation.width, NULL);
86         g_free (key);
87
88         return TRUE;
89 }
90
91
92 static gboolean
93 restore_settings_widget (ModestConf *conf, GtkWidget *widget, const gchar *name)
94 {
95         GtkRequisition req;
96         gchar *key;
97
98         key = get_keyname (conf, name, PARAM_HEIGHT);
99         
100         if (modest_conf_key_exists (conf, key, NULL))
101                 req.height = modest_conf_get_int (conf, key, NULL);
102
103         g_free (key);
104         
105         key = get_keyname (conf, name, PARAM_WIDTH);
106         if (modest_conf_key_exists (conf, key, NULL))
107                 req.width = modest_conf_get_int (conf, key, NULL);
108         g_free (key);
109
110         if (req.height && req.width) 
111                 gtk_widget_size_request (widget, &req);
112
113         return TRUE;
114
115 }
116
117
118
119 static gboolean
120 save_settings_window (ModestConf *conf, GtkWindow *win, const gchar *name)
121 {
122         gchar *key;
123         int height, width;
124         
125         gtk_window_get_size (win, &width, &height);
126         
127         key = get_keyname (conf, name, PARAM_HEIGHT);
128         modest_conf_set_int (conf, key, height, NULL);
129         g_free (key);
130         
131         key = get_keyname (conf, name, PARAM_WIDTH);
132         modest_conf_set_int (conf, key, width, NULL);
133         g_free (key);
134         
135         return TRUE;
136 }
137
138
139 static gboolean
140 restore_settings_window (ModestConf *conf, GtkWindow *win, const gchar *name)
141 {
142         gchar *key;
143         int height = 0, width = 0;
144
145         key = get_keyname (conf, name, PARAM_HEIGHT);
146         
147         if (modest_conf_key_exists (conf, key, NULL))
148                 height = modest_conf_get_int (conf, key, NULL);
149
150         g_free (key);
151
152         key = get_keyname (conf, name, PARAM_WIDTH);
153         if (modest_conf_key_exists (conf, key, NULL))
154                 width = modest_conf_get_int (conf, key, NULL);
155
156         g_free (key);
157
158         if (height && width)
159                 gtk_window_set_default_size (win, width, height);
160
161         return TRUE;
162 }
163
164
165 static gboolean
166 save_settings_paned (ModestConf *conf, GtkPaned *paned, const gchar *name)
167 {
168         gchar *key;
169         int pos;
170
171         pos = gtk_paned_get_position (paned);
172         
173         key = get_keyname (conf, name, PARAM_POS);
174         modest_conf_set_int (conf, key, pos, NULL);
175         g_free (key);
176         
177         return TRUE;
178 }
179
180
181 static gboolean
182 restore_settings_paned (ModestConf *conf, GtkPaned *paned, const gchar *name)
183 {
184         gchar *key;
185         int pos;
186         
187         key = get_keyname (conf, name, PARAM_POS);
188         
189         if (modest_conf_key_exists (conf, key, NULL)) {
190                 pos = modest_conf_get_int (conf, key, NULL);
191                 gtk_paned_set_position (paned, pos);
192         }
193
194         g_free (key);
195         return TRUE;
196 }
197
198
199 static gboolean
200 save_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
201                            const gchar *name)
202 {
203         gchar *key;
204         GString *str;
205         GList *cols, *cursor;
206         TnyFolder *folder;
207         TnyFolderType type;
208
209         if (modest_header_view_get_state (header_view) &
210             MODEST_HEADER_VIEW_STATE_IS_EMPTY)
211                 return TRUE; /* don't save the settings in the empty case */
212
213         folder = modest_header_view_get_folder (header_view);
214         if (!folder) 
215                 return TRUE; /* no folder: no settings */ 
216         
217         type = modest_tny_folder_guess_folder_type (folder);
218         key = get_keyname_with_type (conf, name, type, PARAM_COLUMN_WIDTH);
219
220         cursor = cols = modest_header_view_get_columns (header_view);
221         str = g_string_new (NULL);
222         
223         while (cursor) {
224
225                 int col_id, width;
226                 GtkTreeViewColumn *col;
227                 
228                 col    = GTK_TREE_VIEW_COLUMN (cursor->data);
229                 col_id = GPOINTER_TO_INT(g_object_get_data (G_OBJECT(col),
230                                                             MODEST_HEADER_VIEW_COLUMN));
231                 width = gtk_tree_view_column_get_width (col);
232                 
233                 g_string_append_printf (str, "%d:%d ", col_id, width);  
234                 
235                 cursor = g_list_next (cursor);
236         }
237
238         modest_conf_set_string (conf, key, str->str, NULL);
239
240         g_free (key);   
241         g_string_free (str, TRUE);
242         g_list_free (cols);
243         
244         return TRUE;
245 }
246
247
248 static gboolean
249 restore_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
250                               const gchar *name)
251 {
252         gchar *key;
253         TnyFolder *folder;
254         TnyFolderType type;
255
256         folder = modest_header_view_get_folder (header_view);
257         if (!folder) 
258                 return TRUE; /* no folder: no settings */ 
259         
260         type = modest_tny_folder_guess_folder_type (folder);
261         
262         key = get_keyname_with_type (conf, name, type, PARAM_COLUMN_WIDTH);
263         if (modest_conf_key_exists (conf, key, NULL)) {
264                 
265                 gchar *data, *cursor;
266                 guint col, width;
267                 GList *cols = NULL;
268                 GList *colwidths = NULL;
269         
270                 cursor = data = modest_conf_get_string (conf, key, NULL);
271                 while (cursor && sscanf (cursor, "%u:%u ", &col, &width) == 2) {
272                         cols      = g_list_append (cols, GINT_TO_POINTER(col));
273                         colwidths = g_list_append (colwidths, GINT_TO_POINTER(width));
274                         cursor = strchr (cursor + 1, ' ');
275                 }
276                 g_free (data);  
277                 
278                 if (cols) {
279                         GList *viewcolumns, *colcursor, *widthcursor;
280                         modest_header_view_set_columns (header_view, cols);
281
282                         widthcursor = colwidths;
283                         colcursor = viewcolumns = gtk_tree_view_get_columns (GTK_TREE_VIEW(header_view));
284                         while (colcursor && widthcursor) {
285                                 int width = GPOINTER_TO_INT(widthcursor->data);
286                                 if (width > 0)
287                                         gtk_tree_view_column_set_fixed_width(GTK_TREE_VIEW_COLUMN(colcursor->data),
288                                                                              width);
289                                 colcursor = g_list_next (colcursor);
290                                 widthcursor = g_list_next (widthcursor);
291                         }
292                         g_list_free (cols);
293                         g_list_free (colwidths);
294                         g_list_free (viewcolumns);
295                 }
296         }
297
298         g_free (key);
299         return TRUE;
300 }
301
302
303
304 static gboolean
305 save_settings_folder_view (ModestConf *conf, ModestFolderView *folder_view,
306                            const gchar *name)
307 {
308         return TRUE; /* FIXME: implement this */
309 }
310
311 static gboolean
312 restore_settings_folder_view (ModestConf *conf, ModestFolderView *folder_view,
313                               const gchar *name)
314 {
315         return TRUE; /* FIXME: implement this */
316 }
317
318
319 static gboolean
320 save_settings_msg_view (ModestConf *conf, ModestMsgView *msg_view,
321                            const gchar *name)
322 {
323         return TRUE; /* FIXME: implement this */
324 }
325
326 static gboolean
327 restore_settings_msg_view (ModestConf *conf, ModestMsgView *msg_view,
328                               const gchar *name)
329 {
330         return TRUE; /* FIXME: implement this */
331 }
332
333
334
335 gboolean
336 modest_widget_memory_save (ModestConf *conf, GObject *widget, const gchar *name)
337 {
338         g_return_val_if_fail (conf, FALSE);
339         g_return_val_if_fail (widget, FALSE);
340         g_return_val_if_fail (name, FALSE);
341         
342         if (GTK_IS_WINDOW(widget))
343                 return save_settings_window (conf, GTK_WINDOW(widget), name);
344         else if (GTK_IS_PANED(widget))
345                 return save_settings_paned (conf, GTK_PANED(widget), name);
346         else if (MODEST_IS_HEADER_VIEW(widget))
347                 return save_settings_header_view (conf, MODEST_HEADER_VIEW(widget), name);
348         else if (MODEST_IS_FOLDER_VIEW(widget))
349                 return save_settings_folder_view (conf, MODEST_FOLDER_VIEW(widget), name);
350         else if (MODEST_IS_MSG_VIEW(widget))
351                 return save_settings_msg_view (conf, MODEST_MSG_VIEW(widget), name);
352         else if (GTK_IS_WIDGET(widget))
353                 return save_settings_widget (conf, GTK_WIDGET(widget), name);
354         
355         g_printerr ("modest: %p (%s) is not a known widget\n", widget, name);   
356         return FALSE;
357 }
358
359
360
361 gboolean
362 modest_widget_memory_restore (ModestConf *conf, GObject *widget, const gchar *name)
363 {
364         g_return_val_if_fail (conf, FALSE);
365         g_return_val_if_fail (widget, FALSE);
366         g_return_val_if_fail (name, FALSE);
367         
368         if (GTK_IS_WINDOW(widget))
369                 return restore_settings_window (conf, GTK_WINDOW(widget), name);
370         else if (GTK_IS_PANED(widget))
371                 return restore_settings_paned (conf, GTK_PANED(widget), name);
372         else if (MODEST_IS_HEADER_VIEW(widget))
373                 return restore_settings_header_view (conf, MODEST_HEADER_VIEW(widget), name);
374         else if (MODEST_IS_FOLDER_VIEW(widget))
375                 return restore_settings_folder_view (conf, MODEST_FOLDER_VIEW(widget), name);
376         else if (MODEST_IS_MSG_VIEW(widget))
377                 return restore_settings_msg_view (conf, MODEST_MSG_VIEW(widget), name);
378         else if (GTK_IS_WIDGET(widget))
379                 return restore_settings_widget (conf, GTK_WIDGET(widget), name);
380         
381         g_printerr ("modest: %p (%s) is not a known widget\n", widget, name);
382         return FALSE;
383 }