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