Wiped ModestMainWindow out
[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-defs.h>
31 #include <modest-widget-memory.h>
32 #include <modest-widget-memory-priv.h>
33 #include <modest-runtime.h>
34 #include <modest-account-mgr-helpers.h>
35 #include <modest-tny-platform-factory.h>
36 #include <modest-tny-folder.h>
37 #include <modest-init.h>
38 #include <widgets/modest-header-view.h>
39 #include <widgets/modest-msg-view.h>
40 #include <widgets/modest-folder-view.h>
41 #include <string.h>
42
43 gchar*
44 _modest_widget_memory_get_keyname (const gchar *name, const gchar *param)
45 {
46         gchar *esc_name, *keyname;
47
48         g_return_val_if_fail (name, NULL);
49         g_return_val_if_fail (param, NULL);
50         
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 gchar*
62 _modest_widget_memory_get_keyname_with_type (const gchar *name, guint type,
63                                              const gchar *param)
64 {
65         gchar *esc_name, *keyname;
66         
67         g_return_val_if_fail (name, NULL);
68         g_return_val_if_fail (param, NULL);
69
70         esc_name = modest_conf_key_escape (name);
71
72         keyname = g_strdup_printf ("%s/%s/%s_%d",
73                                    MODEST_CONF_WIDGET_NAMESPACE, 
74                                    esc_name, param, type);
75         g_free (esc_name);
76         return keyname;
77 }
78
79
80 gchar*
81 _modest_widget_memory_get_keyname_with_double_type (const gchar *name,
82                                                     guint type1, guint type2,
83                                                     const gchar *param)
84 {
85         gchar *esc_name, *keyname;
86         
87         g_return_val_if_fail (name, NULL);
88         g_return_val_if_fail (param, NULL);
89
90         esc_name = modest_conf_key_escape (name);
91
92         keyname = g_strdup_printf ("%s/%s/%s_%d_%d",
93                                    MODEST_CONF_WIDGET_NAMESPACE, 
94                                    esc_name, param, type1, type2);
95         g_free (esc_name);
96         return keyname;
97 }
98
99
100
101 static gboolean
102 save_settings_widget (ModestConf *conf, GtkWidget *widget, const gchar *name)
103 {
104         gchar *key;
105
106         key = _modest_widget_memory_get_keyname (name, MODEST_WIDGET_MEMORY_PARAM_HEIGHT);
107         modest_conf_set_int (conf, key, GTK_WIDGET(widget)->allocation.height, NULL);
108         g_free (key);
109         
110         key = _modest_widget_memory_get_keyname (name, MODEST_WIDGET_MEMORY_PARAM_WIDTH);
111         modest_conf_set_int (conf, key, GTK_WIDGET(widget)->allocation.width, NULL);
112         g_free (key);
113
114         return TRUE;
115 }
116
117
118 static gboolean
119 restore_settings_widget (ModestConf *conf, GtkWidget *widget, const gchar *name)
120 {
121         GtkRequisition req = {0, 0};
122         gchar *key;
123
124         key = _modest_widget_memory_get_keyname (name, MODEST_WIDGET_MEMORY_PARAM_HEIGHT);
125         
126         if (modest_conf_key_exists (conf, key, NULL))
127                 req.height = modest_conf_get_int (conf, key, NULL);
128
129         g_free (key);
130         
131         key = _modest_widget_memory_get_keyname (name, MODEST_WIDGET_MEMORY_PARAM_WIDTH);
132         if (modest_conf_key_exists (conf, key, NULL))
133                 req.width = modest_conf_get_int (conf, key, NULL);
134         g_free (key);
135
136         if (req.height && req.width) 
137                 gtk_widget_size_request (widget, &req);
138
139         return TRUE;
140
141 }
142
143
144
145 static gboolean
146 save_settings_window (ModestConf *conf, GtkWindow *win, const gchar *name)
147 {
148         gchar *key;
149         int height, width;
150         
151         gtk_window_get_size (win, &width, &height);
152         
153         key = _modest_widget_memory_get_keyname (name, MODEST_WIDGET_MEMORY_PARAM_HEIGHT);
154         modest_conf_set_int (conf, key, height, NULL);
155         g_free (key);
156         
157         key = _modest_widget_memory_get_keyname (name, MODEST_WIDGET_MEMORY_PARAM_WIDTH);
158         modest_conf_set_int (conf, key, width, NULL);
159         g_free (key);
160
161         return TRUE;
162 }
163
164
165 static gboolean
166 restore_settings_window (ModestConf *conf, GtkWindow *win, const gchar *name)
167 {
168         gchar *key;
169         int height = 0, width = 0;
170
171         key = _modest_widget_memory_get_keyname (name, MODEST_WIDGET_MEMORY_PARAM_HEIGHT);
172         
173         if (modest_conf_key_exists (conf, key, NULL))
174                 height = modest_conf_get_int (conf, key, NULL);
175
176         g_free (key);
177
178         key = _modest_widget_memory_get_keyname (name, MODEST_WIDGET_MEMORY_PARAM_WIDTH);
179         if (modest_conf_key_exists (conf, key, NULL))
180                 width = modest_conf_get_int (conf, key, NULL);
181
182         g_free (key);
183
184         /* Added this ugly ifdef, because in Maemo the
185            gtk_window_set_default_size() makes "drag-motion" signal
186            report bad coordinates, so drag-and-drop do not work
187            properly */
188 #ifdef MODEST_TOOLKIT_GTK
189         if (height && width)
190                 gtk_window_set_default_size (win, width, height);
191 #endif
192
193
194         return TRUE;
195 }
196
197
198 static gboolean
199 save_settings_paned (ModestConf *conf, GtkPaned *paned, const gchar *name)
200 {
201         gchar *key;
202         gint pos;
203         gdouble percent;
204
205         /* Don't save the paned position if it's not visible, 
206          * because it could not be correct: */
207         if (GTK_WIDGET_REALIZED (GTK_WIDGET (paned))) {
208                 pos = gtk_paned_get_position (paned);
209                 percent = (gdouble) (pos * 100) / (gdouble) GTK_WIDGET (paned)->allocation.width;
210
211                 key = _modest_widget_memory_get_keyname (name, MODEST_WIDGET_MEMORY_PARAM_POS);
212                 modest_conf_set_float (conf, key, percent, NULL);
213                 g_free (key);
214         }
215         
216         return TRUE;
217 }
218
219
220 static gboolean
221 restore_settings_paned (ModestConf *conf, GtkPaned *paned, const gchar *name)
222 {
223         gchar *key;
224         gdouble percent;
225         gint pos;
226         
227         key = _modest_widget_memory_get_keyname (name, MODEST_WIDGET_MEMORY_PARAM_POS); 
228         percent = modest_conf_get_float (conf, key, NULL);
229         
230         if (GTK_WIDGET_VISIBLE (GTK_WIDGET (paned)) && GTK_WIDGET_REALIZED (GTK_WIDGET (paned))) {
231                 pos = GTK_WIDGET (paned)->allocation.width * percent /100;
232                 gtk_paned_set_position (paned, pos);
233         }
234
235         g_free (key);
236         return TRUE;
237 }
238
239
240 static gboolean
241 save_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
242                            const gchar *name)
243 {
244         gchar *key;
245         gchar *sort_key;
246         gchar *sort_value;
247         GString *str;
248         GList *cols, *cursor;
249         TnyFolder *folder;
250         TnyFolderType type;
251         ModestHeaderViewStyle style;
252         gint sort_colid;
253         GtkSortType sort_type;
254         gint sort_flag_id = 0;
255         
256         folder = modest_header_view_get_folder (header_view);
257         if (!folder || modest_header_view_is_empty (header_view)) {
258                 if (folder)
259                         g_object_unref (folder);
260                 return TRUE; /* no non-empty folder: no settings */
261         }
262         
263         type  = modest_tny_folder_guess_folder_type (folder);
264         if (type == TNY_FOLDER_TYPE_INVALID)
265                 g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
266         
267         style = modest_header_view_get_style   (header_view);
268         
269         key = _modest_widget_memory_get_keyname_with_double_type (name, type, style,
270                                                                   MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
271         sort_key = _modest_widget_memory_get_keyname_with_double_type (name, type, style,
272                                                                        MODEST_WIDGET_MEMORY_PARAM_COLUMN_SORT);
273
274         cursor = cols = modest_header_view_get_columns (header_view);
275         if (!cols) {
276                 g_warning ("DEBUG: %s: modest_header_view_get_columns() returned NULL.",
277                          __FUNCTION__);
278         }
279         
280         str = g_string_new (NULL);
281
282         /* NOTE: the exact details of this format are important, as they
283          * are also used in modest-init.
284          */
285         sort_colid = modest_header_view_get_sort_column_id (header_view, type); 
286         sort_type = modest_header_view_get_sort_type (header_view, type); 
287
288         while (cursor) {
289
290                 int col_id, width, sort;
291                 GtkTreeViewColumn *col;
292                 int column_sort_flag;
293                 
294                 col    = GTK_TREE_VIEW_COLUMN (cursor->data);
295                 col_id = GPOINTER_TO_INT(g_object_get_data (G_OBJECT(col),
296                                                             MODEST_HEADER_VIEW_COLUMN));
297                 width = gtk_tree_view_column_get_width (col);
298                 sort = 0;
299                 if (sort_colid == col_id)
300                         sort = (sort_type == GTK_SORT_ASCENDING) ? 1:0;
301                 column_sort_flag = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (col), MODEST_HEADER_VIEW_FLAG_SORT));
302                 if (column_sort_flag != 0)
303                         sort_flag_id = column_sort_flag;
304                 
305                 g_string_append_printf (str, "%d:%d:%d ", col_id, width, sort);
306                 cursor = g_list_next (cursor);
307         }
308
309         if ((str->str == NULL) || (strlen(str->str) == 0)) {
310                 /* TODO: Find out why this happens sometimes. */
311                 g_warning ("DEBUG: %s: Attempting to write an empty value to "
312                         "gconf key %s. Preventing.", __FUNCTION__, key);
313         }
314         else
315                 modest_conf_set_string (conf, key, str->str, NULL);
316
317         /* store current sort column for compact view */
318         if (sort_colid >= 0) {
319                 sort_value = g_strdup_printf("%d:%d:%d", sort_colid, sort_type, sort_flag_id);
320                 modest_conf_set_string (conf, sort_key, sort_value, NULL);
321                 g_free (sort_value);
322         }
323
324
325
326         g_free (key);
327         g_free (sort_key);      
328         g_string_free (str, TRUE);
329         g_list_free (cols);
330         g_object_unref (G_OBJECT (folder));
331         
332         return TRUE;
333 }
334
335
336 static gboolean
337 restore_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
338                               const gchar *name)
339 {
340         guint col, width;
341         gint sort;
342         gchar *key;
343         gchar *sort_key;
344         TnyFolder *folder;
345         TnyFolderType type;
346         ModestHeaderViewStyle style;
347         gint sort_flag_id = 0;
348         gint sort_colid = -1, sort_type = GTK_SORT_DESCENDING;
349         
350         folder = modest_header_view_get_folder (header_view);
351         if (!folder)
352                 return TRUE; /* no folder: no settings */
353         
354         type = modest_tny_folder_guess_folder_type (folder);    
355         if (type == TNY_FOLDER_TYPE_INVALID)
356                 g_warning ("%s: BUG: TNY_FOLDER_TYPE_INVALID", __FUNCTION__);
357
358         style = modest_header_view_get_style (header_view);
359
360         key = _modest_widget_memory_get_keyname_with_double_type (name, type, style,
361                                                                   MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
362         sort_key = _modest_widget_memory_get_keyname_with_double_type (name, type, style,
363                                                                        MODEST_WIDGET_MEMORY_PARAM_COLUMN_SORT);
364
365         if (modest_conf_key_exists (conf, sort_key, NULL)) {
366                 gchar *value = modest_conf_get_string (conf, sort_key, NULL);
367                 sscanf (value, "%d:%d:%d", &sort_colid, &sort_type, &sort_flag_id);
368                 g_free (value);
369         }
370
371         if (modest_conf_key_exists (conf, key, NULL)) {
372                 
373                 gchar *data, *cursor;
374                 GList *cols = NULL;
375                 GList *colwidths = NULL;
376                 GList *colsortables = NULL;
377                 GtkTreeModel *sortable;
378
379                 cursor = data = modest_conf_get_string (conf, key, NULL);
380                 while (cursor && sscanf (cursor, "%d:%d:%d ", &col, &width, &sort) == 3) {
381
382                         cols      = g_list_append (cols, GINT_TO_POINTER(col));
383                         colwidths = g_list_append (colwidths, GINT_TO_POINTER(width));
384                         colsortables = g_list_append (colsortables, GINT_TO_POINTER(sort));
385                         cursor = strchr (cursor + 1, ' ');
386                 }
387                 g_free (data);  
388                 
389                 /* Use defaults if gconf has no, or empty information: */
390                 /* We don't know why the value is empty sometimes. */
391                 if (g_list_length(cols) == 0) {
392                         g_warning("%s: gconf key %s was empty. Using default column IDs.\n", 
393                                 __FUNCTION__, key);
394                         g_list_free (cols);
395                         cols = NULL;
396                 }
397                 
398                 if (!cols)
399                         cols = modest_init_get_default_header_view_column_ids (type, style);
400                 
401                 if (cols) {
402                         GList *viewcolumns, *colcursor, *widthcursor;
403                         modest_header_view_set_columns (header_view, cols, type);
404                         sortable = gtk_tree_view_get_model (GTK_TREE_VIEW (header_view));
405
406                         widthcursor = colwidths;
407                         colcursor = viewcolumns = gtk_tree_view_get_columns (GTK_TREE_VIEW(header_view));
408                         while (colcursor && widthcursor) {
409                                 int width = GPOINTER_TO_INT(widthcursor->data);
410                                 int view_column_id = GPOINTER_TO_INT (g_object_get_data (
411                                                                               G_OBJECT (colcursor->data), 
412                                                                               MODEST_HEADER_VIEW_COLUMN));
413                                 if (width > 0)
414                                         gtk_tree_view_column_set_max_width(GTK_TREE_VIEW_COLUMN(colcursor->data),
415                                                                            width);
416                                 if (((view_column_id == MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN) ||
417                                      (view_column_id == MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT)) &&
418                                     (sort_flag_id != 0))
419                                         g_object_set_data (G_OBJECT (colcursor->data), 
420                                                            MODEST_HEADER_VIEW_FLAG_SORT, GINT_TO_POINTER (sort_flag_id));
421                                 colcursor = g_list_next (colcursor);
422                                 widthcursor = g_list_next (widthcursor);
423                         }
424
425                         g_list_free (cols);
426                         g_list_free (colwidths);
427                         g_list_free (colsortables);
428                         g_list_free (viewcolumns);
429                 }
430         }
431
432         if (sort_colid >= 0) {
433                 GtkTreeModel *sortable = gtk_tree_view_get_model (GTK_TREE_VIEW (header_view));
434                 if (sort_colid == TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN)
435                         modest_header_view_sort_by_column_id (header_view, 0, sort_type);
436                 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (sortable),
437                                                       sort_colid,
438                                                       sort_type);
439                 modest_header_view_sort_by_column_id (header_view, sort_colid, sort_type);
440                 gtk_tree_sortable_sort_column_changed (GTK_TREE_SORTABLE (sortable));
441         }
442
443         g_free (key);
444         g_free (sort_key);
445         
446         g_object_unref (G_OBJECT (folder));
447
448         return TRUE;
449 }
450
451
452
453 static gboolean
454 save_settings_folder_view (ModestConf *conf, ModestFolderView *folder_view,
455                            const gchar *name)
456 {
457         return TRUE;
458 }
459
460 static gboolean
461 restore_settings_folder_view (ModestConf *conf, 
462                               ModestFolderView *folder_view,
463                               const gchar *name)
464 {
465         ModestAccountMgr *mgr;
466         gchar *default_acc;
467
468         /* Always show the default account as visible server account */
469         mgr = modest_runtime_get_account_mgr ();
470         default_acc = modest_account_mgr_get_default_account (mgr);
471         if (default_acc) {
472                 ModestAccountSettings *settings;
473                 const gchar *server_acc_id;
474
475                 settings = modest_account_mgr_load_account_settings (mgr, (const gchar*) default_acc);
476                 /* If there was any problem with the settings storage
477                    the settings could be NULL */
478                 if (settings) {
479                         ModestServerAccountSettings *store_settings;
480                         store_settings = modest_account_settings_get_store_settings (settings);
481
482                         if (store_settings) {
483                                 server_acc_id = modest_server_account_settings_get_account_name (store_settings);
484                                 modest_folder_view_set_account_id_of_visible_server_account (folder_view, server_acc_id);
485                                 g_object_unref (store_settings);
486                         }
487                         g_object_unref (settings);
488                 }
489                 g_free (default_acc);
490         }
491         return TRUE;
492 }
493
494
495 static gboolean
496 save_settings_msg_view (ModestConf *conf, 
497                         ModestMsgView *msg_view,
498                         const gchar *name)
499 {
500         return TRUE; /* FIXME: implement this */
501 }
502
503 static gboolean
504 restore_settings_msg_view (ModestConf *conf, ModestMsgView *msg_view,
505                               const gchar *name)
506 {
507         return TRUE; /* FIXME: implement this */
508 }
509
510
511
512 gboolean
513 modest_widget_memory_save (ModestConf *conf, GObject *widget, const gchar *name)
514 {
515         g_return_val_if_fail (conf, FALSE);
516         g_return_val_if_fail (widget, FALSE);
517         g_return_val_if_fail (name, FALSE);
518
519         if (GTK_IS_WINDOW(widget))
520                 return save_settings_window (conf, GTK_WINDOW(widget), name);
521         else if (GTK_IS_PANED(widget))
522                 return save_settings_paned (conf, GTK_PANED(widget), name);
523         else if (MODEST_IS_HEADER_VIEW(widget))
524                 return save_settings_header_view (conf, MODEST_HEADER_VIEW(widget), name);
525         else if (MODEST_IS_FOLDER_VIEW(widget))
526                 return save_settings_folder_view (conf, MODEST_FOLDER_VIEW(widget), name);
527         else if (MODEST_IS_MSG_VIEW(widget))
528                 return save_settings_msg_view (conf, MODEST_MSG_VIEW(widget), name);
529         else if (GTK_IS_WIDGET(widget))
530                 return save_settings_widget (conf, GTK_WIDGET(widget), name);
531         
532         g_printerr ("modest: %p (%s) is not a known widget\n", widget, name);   
533         return FALSE;
534 }
535
536
537
538 gboolean
539 modest_widget_memory_restore (ModestConf *conf, GObject *widget, const gchar *name)
540 {
541         g_return_val_if_fail (conf, FALSE);
542         g_return_val_if_fail (widget, FALSE);
543         g_return_val_if_fail (name, FALSE);
544
545         if (GTK_IS_WINDOW(widget))
546                 return restore_settings_window (conf, GTK_WINDOW(widget), name);
547         else if (GTK_IS_PANED(widget))
548                 return restore_settings_paned (conf, GTK_PANED(widget), name);
549         else if (MODEST_IS_HEADER_VIEW(widget))
550                 return restore_settings_header_view (conf, MODEST_HEADER_VIEW(widget), name);
551         else if (MODEST_IS_FOLDER_VIEW(widget))
552                 return restore_settings_folder_view (conf, MODEST_FOLDER_VIEW(widget), name);
553         else if (MODEST_IS_MSG_VIEW(widget))
554                 return restore_settings_msg_view (conf, MODEST_MSG_VIEW(widget), name);
555         else if (GTK_IS_WIDGET(widget))
556                 return restore_settings_widget (conf, GTK_WIDGET(widget), name);
557         
558         g_printerr ("modest: %p (%s) is not a known widget\n", widget, name);
559         return FALSE;
560 }