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