* Added a new account key called type for server accounts
[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 (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 (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_free (key);   
236         g_string_free (str, TRUE);
237         g_list_free (cols);
238         
239         return TRUE;
240 }
241
242
243 static gboolean
244 restore_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
245                               const gchar *name)
246 {
247         gchar *key;
248         TnyFolder *folder;
249         TnyFolderType type;
250
251         folder = modest_header_view_get_folder (header_view);
252         if (!folder) 
253                 return TRUE; /* no folder: no settings */ 
254         
255         type = modest_folder_view_guess_folder_type (folder);
256         
257         key = get_keyname_with_type (conf, name, type, PARAM_COLUMN_WIDTH);
258         if (modest_conf_key_exists (conf, key, NULL)) {
259                 
260                 gchar *data, *cursor;
261                 guint col, width;
262                 GList *cols = NULL;
263                 GList *colwidths = NULL;
264         
265                 cursor = data = modest_conf_get_string (conf, key, NULL);
266                 while (cursor && sscanf (cursor, "%u:%u ", &col, &width) == 2) {
267                         cols      = g_list_append (cols, GINT_TO_POINTER(col));
268                         colwidths = g_list_append (colwidths, GINT_TO_POINTER(width));
269                         cursor = strchr (cursor + 1, ' ');
270                 }
271                 g_free (data);  
272                 
273                 if (cols) {
274                         GList *viewcolumns, *colcursor, *widthcursor;
275                         modest_header_view_set_columns (header_view, cols);
276
277                         widthcursor = colwidths;
278                         colcursor = viewcolumns = gtk_tree_view_get_columns (GTK_TREE_VIEW(header_view));
279                         while (colcursor && widthcursor) {
280                                 int width = GPOINTER_TO_INT(widthcursor->data);
281                                 if (width > 0)
282                                         gtk_tree_view_column_set_fixed_width(GTK_TREE_VIEW_COLUMN(colcursor->data),
283                                                                              width);
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_free (key);
294         return TRUE;
295 }
296
297
298
299 static gboolean
300 save_settings_folder_view (ModestConf *conf, ModestFolderView *folder_view,
301                            const gchar *name)
302 {
303         return TRUE; /* FIXME: implement this */
304 }
305
306 static gboolean
307 restore_settings_folder_view (ModestConf *conf, ModestFolderView *folder_view,
308                               const gchar *name)
309 {
310         return TRUE; /* FIXME: implement this */
311 }
312
313
314 static gboolean
315 save_settings_msg_view (ModestConf *conf, ModestMsgView *msg_view,
316                            const gchar *name)
317 {
318         return TRUE; /* FIXME: implement this */
319 }
320
321 static gboolean
322 restore_settings_msg_view (ModestConf *conf, ModestMsgView *msg_view,
323                               const gchar *name)
324 {
325         return TRUE; /* FIXME: implement this */
326 }
327
328
329
330 gboolean
331 modest_widget_memory_save (ModestConf *conf, GObject *widget, const gchar *name)
332 {
333         g_return_val_if_fail (conf, FALSE);
334         g_return_val_if_fail (widget, FALSE);
335         g_return_val_if_fail (name, FALSE);
336         
337         if (GTK_IS_WINDOW(widget))
338                 return save_settings_window (conf, GTK_WINDOW(widget), name);
339         else if (GTK_IS_PANED(widget))
340                 return save_settings_paned (conf, GTK_PANED(widget), name);
341         else if (MODEST_IS_HEADER_VIEW(widget))
342                 return save_settings_header_view (conf, MODEST_HEADER_VIEW(widget), name);
343         else if (MODEST_IS_FOLDER_VIEW(widget))
344                 return save_settings_folder_view (conf, MODEST_FOLDER_VIEW(widget), name);
345         else if (MODEST_IS_MSG_VIEW(widget))
346                 return save_settings_msg_view (conf, MODEST_MSG_VIEW(widget), name);
347         else if (GTK_IS_WIDGET(widget))
348                 return save_settings_widget (conf, GTK_WIDGET(widget), name);
349         
350         g_printerr ("modest: %p (%s) is not a known widget\n", widget, name);   
351         return FALSE;
352 }
353
354
355
356 gboolean
357 modest_widget_memory_restore (ModestConf *conf, GObject *widget, const gchar *name)
358 {
359         g_return_val_if_fail (conf, FALSE);
360         g_return_val_if_fail (widget, FALSE);
361         g_return_val_if_fail (name, FALSE);
362         
363         if (GTK_IS_WINDOW(widget))
364                 return restore_settings_window (conf, GTK_WINDOW(widget), name);
365         else if (GTK_IS_PANED(widget))
366                 return restore_settings_paned (conf, GTK_PANED(widget), name);
367         else if (MODEST_IS_HEADER_VIEW(widget))
368                 return restore_settings_header_view (conf, MODEST_HEADER_VIEW(widget), name);
369         else if (MODEST_IS_FOLDER_VIEW(widget))
370                 return restore_settings_folder_view (conf, MODEST_FOLDER_VIEW(widget), name);
371         else if (MODEST_IS_MSG_VIEW(widget))
372                 return restore_settings_msg_view (conf, MODEST_MSG_VIEW(widget), name);
373         else if (GTK_IS_WIDGET(widget))
374                 return restore_settings_widget (conf, GTK_WIDGET(widget), name);
375         
376         g_printerr ("modest: %p (%s) is not a known widget\n", widget, name);
377         return FALSE;
378 }