* src/modest-text-utils.c:
[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 #include <modest-runtime.h>
33 #include <modest-account-mgr-helpers.h>
34 #include <modest-tny-platform-factory.h>
35 #include <modest-tny-folder.h>
36 #include <modest-init.h>
37 #include <widgets/modest-header-view.h>
38 #include <widgets/modest-msg-view.h>
39 #include <widgets/modest-folder-view.h>
40 #include "widgets/modest-main-window.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;
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         /* Save also the main window style */
162         if (MODEST_IS_MAIN_WINDOW (win)) {
163                 ModestMainWindowStyle style = modest_main_window_get_style (MODEST_MAIN_WINDOW (win));
164
165                 key = _modest_widget_memory_get_keyname (name, MODEST_WIDGET_MEMORY_PARAM_WINDOW_STYLE);
166                 modest_conf_set_int (conf, key, style, NULL);
167                 g_free (key);
168         }
169         
170         return TRUE;
171 }
172
173
174 static gboolean
175 restore_settings_window (ModestConf *conf, GtkWindow *win, const gchar *name)
176 {
177         gchar *key;
178         int height = 0, width = 0;
179
180         key = _modest_widget_memory_get_keyname (name, MODEST_WIDGET_MEMORY_PARAM_HEIGHT);
181         
182         if (modest_conf_key_exists (conf, key, NULL))
183                 height = modest_conf_get_int (conf, key, NULL);
184
185         g_free (key);
186
187         key = _modest_widget_memory_get_keyname (name, MODEST_WIDGET_MEMORY_PARAM_WIDTH);
188         if (modest_conf_key_exists (conf, key, NULL))
189                 width = modest_conf_get_int (conf, key, NULL);
190
191         g_free (key);
192
193         /* Added this ugly ifdef, because in Maemo the
194            gtk_window_set_default_size() makes "drag-motion" signal
195            report bad coordinates, so drag-and-drop do not work
196            properly */
197 #ifdef MODEST_PLATFORM_GNOME
198         if (height && width)
199                 gtk_window_set_default_size (win, width, height);
200 #endif
201
202         /* Restore also the main window style */
203         if (MODEST_IS_MAIN_WINDOW (win)) {
204                 ModestMainWindowStyle style;
205
206                 key = _modest_widget_memory_get_keyname (name, MODEST_WIDGET_MEMORY_PARAM_WINDOW_STYLE);
207                 if (modest_conf_key_exists (conf, key, NULL)) {
208                         style = (ModestMainWindowStyle) modest_conf_get_int (conf, key, NULL);          
209                         modest_main_window_set_style (MODEST_MAIN_WINDOW (win), style);
210                         g_free (key);
211                 }
212         }
213
214         return TRUE;
215 }
216
217
218 static gboolean
219 save_settings_paned (ModestConf *conf, GtkPaned *paned, const gchar *name)
220 {
221         gchar *key;
222         int pos;
223
224         /* Don't save the paned position if it's not visible, 
225          * because it could not be correct: */
226         if (GTK_WIDGET_VISIBLE (GTK_WIDGET (paned)) && GTK_WIDGET_REALIZED (GTK_WIDGET (paned))) {
227                 pos = gtk_paned_get_position (paned);
228                 
229                 key = _modest_widget_memory_get_keyname (name, MODEST_WIDGET_MEMORY_PARAM_POS);
230                 modest_conf_set_int (conf, key, pos, NULL);
231                 g_free (key);
232         }
233         
234         return TRUE;
235 }
236
237
238 static gboolean
239 restore_settings_paned (ModestConf *conf, GtkPaned *paned, const gchar *name)
240 {
241         gchar *key;
242         int pos;
243         
244         key = _modest_widget_memory_get_keyname (name, MODEST_WIDGET_MEMORY_PARAM_POS);
245         
246         if (modest_conf_key_exists (conf, key, NULL)) {
247                 pos = modest_conf_get_int (conf, key, NULL);
248                 
249                 /* TODO: Remove this hack so that paned positions can really be used.
250                  * The paned position is incorrectly saved somehow before its even visible,
251                  * when we show the main window only some time after creating it,
252                  * so this prevents a wrong value from being used. */
253                 const gint max = (GTK_WIDGET(paned)->requisition.width)/3;
254                 if (pos > max)
255                         pos = max;
256                 
257                         
258                 gtk_paned_set_position (paned, pos);
259         } else {
260                 /* The initial position must follow the 30/70 rule */
261                 gtk_paned_set_position (paned, GTK_WIDGET(paned)->requisition.width/3);
262         }
263
264         g_free (key);
265         return TRUE;
266 }
267
268
269 static gboolean
270 save_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
271                            const gchar *name)
272 {
273         gchar *key;
274         gchar *sort_key;
275         gchar *sort_value;
276         GString *str;
277         GList *cols, *cursor;
278         TnyFolder *folder;
279         TnyFolderType type;
280         ModestHeaderViewStyle style;
281         gint sort_colid;
282         GtkSortType sort_type;
283         gint sort_flag_id = 0;
284         
285         folder = modest_header_view_get_folder (header_view);
286         if (!folder || modest_header_view_is_empty (header_view)) {
287                 if (folder)
288                         g_object_unref (folder);
289                 return TRUE; /* no non-empty folder: no settings */
290         }
291         
292         type  = modest_tny_folder_guess_folder_type (folder);
293         style = modest_header_view_get_style   (header_view);
294         
295         key = _modest_widget_memory_get_keyname_with_double_type (name, type, style,
296                                                                   MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
297         sort_key = _modest_widget_memory_get_keyname_with_double_type (name, type, style,
298                                                                        MODEST_WIDGET_MEMORY_PARAM_COLUMN_SORT);
299
300         cursor = cols = modest_header_view_get_columns (header_view);
301         if (!cols) {
302                 g_warning ("DEBUG: %s: modest_header_view_get_columns() returned NULL.",
303                          __FUNCTION__);
304         }
305         
306         str = g_string_new (NULL);
307
308         /* NOTE: the exact details of this format are important, as they
309          * are also used in modest-init.
310          */
311         sort_colid = modest_header_view_get_sort_column_id (header_view, type); 
312         sort_type = modest_header_view_get_sort_type (header_view, type); 
313
314         while (cursor) {
315
316                 int col_id, width, sort;
317                 GtkTreeViewColumn *col;
318                 int column_sort_flag;
319                 
320                 col    = GTK_TREE_VIEW_COLUMN (cursor->data);
321                 col_id = GPOINTER_TO_INT(g_object_get_data (G_OBJECT(col),
322                                                             MODEST_HEADER_VIEW_COLUMN));
323                 width = gtk_tree_view_column_get_width (col);
324                 sort = 0;
325                 if (sort_colid == col_id)
326                         sort = (sort_type == GTK_SORT_ASCENDING) ? 1:0;
327                 column_sort_flag = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (col), MODEST_HEADER_VIEW_FLAG_SORT));
328                 if (column_sort_flag != 0)
329                         sort_flag_id = column_sort_flag;
330                 
331                 g_string_append_printf (str, "%d:%d:%d ", col_id, width, sort);
332                 cursor = g_list_next (cursor);
333         }
334
335         if ((str->str == NULL) || (strlen(str->str) == 0)) {
336                 /* TODO: Find out why this happens sometimes. */
337                 g_warning ("DEBUG: %s: Attempting to write an empty value to "
338                         "gconf key %s. Preventing.", __FUNCTION__, key);
339         }
340         else
341                 modest_conf_set_string (conf, key, str->str, NULL);
342
343         /* store current sort column for compact view */
344         if (sort_colid >= 0) {
345                 sort_value = g_strdup_printf("%d:%d:%d", sort_colid, sort_type, sort_flag_id);
346                 modest_conf_set_string (conf, sort_key, sort_value, NULL);
347                 g_free (sort_value);
348         }
349
350
351
352         g_free (key);
353         g_free (sort_key);      
354         g_string_free (str, TRUE);
355         g_list_free (cols);
356         g_object_unref (G_OBJECT (folder));
357         
358         return TRUE;
359 }
360
361
362 static gboolean
363 restore_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
364                               const gchar *name)
365 {
366         guint col, width;
367         gint sort;
368         gchar *key;
369         gchar *sort_key;
370         TnyFolder *folder;
371         TnyFolderType type;
372         ModestHeaderViewStyle style;
373         gint sort_flag_id = 0;
374         gint sort_colid = -1, sort_type;
375         
376         folder = modest_header_view_get_folder (header_view);
377         if (!folder)
378                 return TRUE; /* no folder: no settings */
379         
380         type = modest_tny_folder_guess_folder_type (folder);    
381         style = modest_header_view_get_style (header_view);
382
383         key = _modest_widget_memory_get_keyname_with_double_type (name, type, style,
384                                                                   MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
385         sort_key = _modest_widget_memory_get_keyname_with_double_type (name, type, style,
386                                                                        MODEST_WIDGET_MEMORY_PARAM_COLUMN_SORT);
387
388         if (modest_conf_key_exists (conf, sort_key, NULL)) {
389                 gchar *value = modest_conf_get_string (conf, sort_key, NULL);
390                 sscanf (value, "%d:%d:%d", &sort_colid, &sort_type, &sort_flag_id);
391         }
392
393         if (modest_conf_key_exists (conf, key, NULL)) {
394                 
395                 gchar *data, *cursor;
396                 GList *cols = NULL;
397                 GList *colwidths = NULL;
398                 GList *colsortables = NULL;
399                 GtkTreeModel *sortable;
400
401                 cursor = data = modest_conf_get_string (conf, key, NULL);
402                 while (cursor && sscanf (cursor, "%d:%d:%d ", &col, &width, &sort) == 3) {
403
404                         cols      = g_list_append (cols, GINT_TO_POINTER(col));
405                         colwidths = g_list_append (colwidths, GINT_TO_POINTER(width));
406                         colsortables = g_list_append (colsortables, GINT_TO_POINTER(sort));
407                         cursor = strchr (cursor + 1, ' ');
408                 }
409                 g_free (data);  
410                 
411                 /* Use defaults if gconf has no, or empty information: */
412                 /* We don't know why the value is empty sometimes. */
413                 if (g_list_length(cols) == 0) {
414                         g_warning("%s: gconf key %s was empty. Using default column IDs.\n", 
415                                 __FUNCTION__, key);
416                         g_list_free (cols);
417                         cols = NULL;
418                 }
419                 
420                 if (!cols)
421                         cols = modest_init_get_default_header_view_column_ids (type, style);
422                 
423                 if (cols) {
424                         GList *viewcolumns, *colcursor, *widthcursor;
425                         modest_header_view_set_columns (header_view, cols, type);
426                         sortable = gtk_tree_view_get_model (GTK_TREE_VIEW (header_view));
427
428                         widthcursor = colwidths;
429                         colcursor = viewcolumns = gtk_tree_view_get_columns (GTK_TREE_VIEW(header_view));
430                         while (colcursor && widthcursor) {
431                                 int width = GPOINTER_TO_INT(widthcursor->data);
432                                 int view_column_id = GPOINTER_TO_INT (g_object_get_data (
433                                                                               G_OBJECT (colcursor->data), 
434                                                                               MODEST_HEADER_VIEW_COLUMN));
435                                 if (width > 0)
436                                         gtk_tree_view_column_set_max_width(GTK_TREE_VIEW_COLUMN(colcursor->data),
437                                                                            width);
438                                 if (((view_column_id == MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN) ||
439                                      (view_column_id == MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT)) &&
440                                     (sort_flag_id != 0))
441                                         g_object_set_data (G_OBJECT (colcursor->data), 
442                                                            MODEST_HEADER_VIEW_FLAG_SORT, GINT_TO_POINTER (sort_flag_id));
443                                 colcursor = g_list_next (colcursor);
444                                 widthcursor = g_list_next (widthcursor);
445                         }
446
447                         g_list_free (cols);
448                         g_list_free (colwidths);
449                         g_list_free (colsortables);
450                         g_list_free (viewcolumns);
451                 }
452         }
453
454         if (sort_colid >= 0) {
455           GtkTreeModel *sortable = 
456                   gtk_tree_model_filter_get_model (
457                           GTK_TREE_MODEL_FILTER (gtk_tree_view_get_model (GTK_TREE_VIEW (header_view))));
458                 if (sort_colid == TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN)
459                         modest_header_view_sort_by_column_id (header_view, 0, sort_type);
460                 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable),
461                                                       sort_colid,
462                                                       sort_type);
463                 modest_header_view_sort_by_column_id (header_view, sort_colid, sort_type);
464                 gtk_tree_sortable_sort_column_changed (GTK_TREE_SORTABLE(sortable));
465         }
466
467         g_free (key);
468         g_free (sort_key);
469         
470         g_object_unref (G_OBJECT (folder));
471
472         return TRUE;
473 }
474
475
476
477 static gboolean
478 save_settings_folder_view (ModestConf *conf, ModestFolderView *folder_view,
479                            const gchar *name)
480 {
481         gchar *key;
482         const gchar* account_id;
483
484         /* Save the visible account */
485         key = _modest_widget_memory_get_keyname (name, "visible_server_account_id");
486
487         account_id = modest_folder_view_get_account_id_of_visible_server_account (folder_view);
488         if (account_id)
489                 modest_conf_set_string (conf, key, account_id, NULL);
490         else
491                 modest_conf_remove_key (conf, key, NULL);
492         g_free (key);
493
494         return TRUE;
495 }
496
497 static gboolean
498 restore_settings_folder_view (ModestConf *conf, 
499                               ModestFolderView *folder_view,
500                               const gchar *name)
501 {
502         gchar *key, *account_id;
503
504         /* Restore the visible account */
505         key = _modest_widget_memory_get_keyname (name, "visible_server_account_id");
506
507         if (modest_conf_key_exists (conf, key, NULL)) {
508                 account_id = modest_conf_get_string (conf, key, NULL);
509                 modest_folder_view_set_account_id_of_visible_server_account (folder_view, 
510                                                                              (const gchar *) account_id);
511                 g_free (account_id);
512         } else {
513                 ModestAccountMgr *mgr;
514                 gchar *default_acc;
515
516                 /* If there is no visible account id in the
517                    configuration then pick the default account as
518                    visible account */
519                 mgr = modest_runtime_get_account_mgr ();
520                 default_acc = modest_account_mgr_get_default_account (mgr);
521                 if (default_acc) {
522                         ModestAccountData *acc_data;
523                         const gchar *server_acc_id;
524
525                         acc_data = modest_account_mgr_get_account_data (mgr, (const gchar*) default_acc);
526                         server_acc_id = (const gchar *) acc_data->store_account->account_name;
527
528                         modest_conf_set_string (conf, key, server_acc_id, NULL);
529                         modest_folder_view_set_account_id_of_visible_server_account (folder_view, server_acc_id);
530
531                         g_free (default_acc);
532                 }
533         }
534
535         g_free (key);
536
537         return TRUE;
538 }
539
540
541 static gboolean
542 save_settings_msg_view (ModestConf *conf, 
543                         ModestMsgView *msg_view,
544                         const gchar *name)
545 {
546         return TRUE; /* FIXME: implement this */
547 }
548
549 static gboolean
550 restore_settings_msg_view (ModestConf *conf, ModestMsgView *msg_view,
551                               const gchar *name)
552 {
553         return TRUE; /* FIXME: implement this */
554 }
555
556
557
558 gboolean
559 modest_widget_memory_save (ModestConf *conf, GObject *widget, const gchar *name)
560 {
561         g_return_val_if_fail (conf, FALSE);
562         g_return_val_if_fail (widget, FALSE);
563         g_return_val_if_fail (name, FALSE);
564
565         if (GTK_IS_WINDOW(widget))
566                 return save_settings_window (conf, GTK_WINDOW(widget), name);
567         else if (GTK_IS_PANED(widget))
568                 return save_settings_paned (conf, GTK_PANED(widget), name);
569         else if (MODEST_IS_HEADER_VIEW(widget))
570                 return save_settings_header_view (conf, MODEST_HEADER_VIEW(widget), name);
571         else if (MODEST_IS_FOLDER_VIEW(widget))
572                 return save_settings_folder_view (conf, MODEST_FOLDER_VIEW(widget), name);
573         else if (MODEST_IS_MSG_VIEW(widget))
574                 return save_settings_msg_view (conf, MODEST_MSG_VIEW(widget), name);
575         else if (GTK_IS_WIDGET(widget))
576                 return save_settings_widget (conf, GTK_WIDGET(widget), name);
577         
578         g_printerr ("modest: %p (%s) is not a known widget\n", widget, name);   
579         return FALSE;
580 }
581
582
583
584 gboolean
585 modest_widget_memory_restore (ModestConf *conf, GObject *widget, const gchar *name)
586 {
587         g_return_val_if_fail (conf, FALSE);
588         g_return_val_if_fail (widget, FALSE);
589         g_return_val_if_fail (name, FALSE);
590
591         if (GTK_IS_WINDOW(widget))
592                 return restore_settings_window (conf, GTK_WINDOW(widget), name);
593         else if (GTK_IS_PANED(widget))
594                 return restore_settings_paned (conf, GTK_PANED(widget), name);
595         else if (MODEST_IS_HEADER_VIEW(widget))
596                 return restore_settings_header_view (conf, MODEST_HEADER_VIEW(widget), name);
597         else if (MODEST_IS_FOLDER_VIEW(widget))
598                 return restore_settings_folder_view (conf, MODEST_FOLDER_VIEW(widget), name);
599         else if (MODEST_IS_MSG_VIEW(widget))
600                 return restore_settings_msg_view (conf, MODEST_MSG_VIEW(widget), name);
601         else if (GTK_IS_WIDGET(widget))
602                 return restore_settings_widget (conf, GTK_WIDGET(widget), name);
603         
604         g_printerr ("modest: %p (%s) is not a known widget\n", widget, name);
605         return FALSE;
606 }