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