* Added global settings persistence
[modest] / src / widgets / modest-global-settings-dialog.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 <glib/gi18n.h>
31 #include <gtk/gtknotebook.h>
32 #include <gtk/gtkstock.h>
33 #include <gtk/gtklabel.h>
34 #include <gtk/gtktogglebutton.h>
35 #include "widgets/modest-global-settings-dialog.h"
36 #include "widgets/modest-global-settings-dialog-priv.h"
37 #include "modest-defs.h"
38 #include "modest-conf.h"
39 #include "modest-runtime.h"
40 #include "modest-ui-constants.h"
41 #include "modest-tny-msg.h"
42 #include "widgets/modest-combo-box.h"
43 #ifdef MODEST_PLATFORM_MAEMO
44 #ifdef MODEST_HILDON_VERSION_0
45 #include <hildon-widgets/hildon-number-editor.h>
46 #else
47 #include <hildon/hildon-number-editor.h>
48 #endif /*MODEST_HILDON_VERSION_0*/
49 #endif
50 /* include other impl specific header files */
51
52 /* 'private'/'protected' functions */
53 static void modest_global_settings_dialog_class_init (ModestGlobalSettingsDialogClass *klass);
54 static void modest_global_settings_dialog_init       (ModestGlobalSettingsDialog *obj);
55 static void modest_global_settings_dialog_finalize   (GObject *obj);
56
57 static void on_response (GtkDialog *dialog,
58                          gint arg1,
59                          gpointer user_data);
60
61 /* list my signals  */
62 enum {
63         /* MY_SIGNAL_1, */
64         /* MY_SIGNAL_2, */
65         LAST_SIGNAL
66 };
67
68 /* globals */
69 static GtkDialogClass *parent_class = NULL;
70
71 /* uncomment the following if you have defined any signals */
72 /* static guint signals[LAST_SIGNAL] = {0}; */
73
74 GType
75 modest_global_settings_dialog_get_type (void)
76 {
77         static GType my_type = 0;
78         if (!my_type) {
79                 static const GTypeInfo my_info = {
80                         sizeof(ModestGlobalSettingsDialogClass),
81                         NULL,           /* base init */
82                         NULL,           /* base finalize */
83                         (GClassInitFunc) modest_global_settings_dialog_class_init,
84                         NULL,           /* class finalize */
85                         NULL,           /* class data */
86                         sizeof(ModestGlobalSettingsDialog),
87                         1,              /* n_preallocs */
88                         (GInstanceInitFunc) modest_global_settings_dialog_init,
89                         NULL
90                 };
91                 my_type = g_type_register_static (GTK_TYPE_DIALOG,
92                                                   "ModestGlobalSettingsDialog",
93                                                   &my_info, 0);
94         }
95         return my_type;
96 }
97
98 static void
99 modest_global_settings_dialog_class_init (ModestGlobalSettingsDialogClass *klass)
100 {
101         GObjectClass *gobject_class;
102         gobject_class = (GObjectClass*) klass;
103
104         parent_class            = g_type_class_peek_parent (klass);
105         gobject_class->finalize = modest_global_settings_dialog_finalize;
106
107         g_type_class_add_private (gobject_class, sizeof(ModestGlobalSettingsDialogPrivate));
108 }
109
110 static void
111 modest_global_settings_dialog_init (ModestGlobalSettingsDialog *self)
112 {
113         ModestGlobalSettingsDialogPrivate *priv;
114 /*      GdkGeometry *geometry; */
115
116         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
117
118         priv->notebook = gtk_notebook_new ();
119            
120         /* Add the buttons: */
121         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_OK, GTK_RESPONSE_OK);
122         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
123     
124         /* Connect to the dialog's response signal: */
125         g_signal_connect (G_OBJECT (self), "response",
126                           G_CALLBACK (on_response), self);
127
128         /* Set title */
129         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_options"));
130
131         /* Set geometry */
132 /*      geometry = g_malloc0(sizeof (GdkGeometry)); */
133 /*      geometry->max_width = MODEST_DIALOG_WINDOW_MAX_WIDTH; */
134 /*      geometry->min_width = MODEST_DIALOG_WINDOW_MIN_WIDTH; */
135 /*      geometry->max_height = MODEST_DIALOG_WINDOW_MAX_HEIGHT; */
136 /*      geometry->min_height = MODEST_DIALOG_WINDOW_MIN_HEIGHT; */
137 /*      gtk_window_set_geometry_hints (GTK_WINDOW (self), */
138 /*                                     GTK_WIDGET (self), */
139 /*                                     geometry, */
140 /*                                     GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE); */
141         gtk_widget_set_size_request (GTK_WIDGET (self), 
142                                      MODEST_DIALOG_WINDOW_MAX_WIDTH, 
143                                      MODEST_DIALOG_WINDOW_MAX_HEIGHT);
144 }
145
146 static void
147 modest_global_settings_dialog_finalize (GObject *obj)
148 {
149 /*      free/unref instance resources here */
150         G_OBJECT_CLASS(parent_class)->finalize (obj);
151 }
152
153 /*
154  * Creates a pair list (number,string) and adds it to the given list
155  */
156 static void
157 add_to_modest_pair_list (const gint num, const gchar *str, GSList **list)
158 {
159         gint *number;
160         ModestPair *pair;
161
162         number = g_malloc0 (sizeof (gint));
163         *number = num;
164         pair = modest_pair_new (number, g_strdup (str), FALSE);
165         *list = g_slist_prepend (*list, pair);
166 }
167
168 /*
169  * Gets a list of pairs 
170  */
171 ModestPairList *
172 _modest_global_settings_dialog_get_connected_via (void)
173 {
174         GSList *list = NULL;
175
176         add_to_modest_pair_list (MODEST_CONNECTED_VIA_WLAN, 
177                                  _("mcen_va_options_connectiontype_wlan"), 
178                                  &list);
179         add_to_modest_pair_list (MODEST_CONNECTED_VIA_ANY, 
180                                  _("mcen_va_options_connectiontype_all"), 
181                                  &list);
182
183         return (ModestPairList *) g_slist_reverse (list);
184 }
185
186 /*
187  * Gets a list of pairs of update intervals
188  */
189 ModestPairList *
190 _modest_global_settings_dialog_get_update_interval (void)
191 {
192         GSList *list = NULL;
193
194         add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_5_MIN, 
195                                  _("mcen_va_options_updateinterval_5min"), 
196                                  &list);
197         add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_10_MIN, 
198                                  _("mcen_va_options_updateinterval_10min"), 
199                                  &list);
200         add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_15_MIN, 
201                                  _("mcen_va_options_updateinterval_15min"), 
202                                  &list);
203         add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_30_MIN, 
204                                  _("mcen_va_options_updateinterval_30min"), 
205                                  &list);
206         add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_1_HOUR, 
207                                  _("mcen_va_options_updateinterval_1h"), 
208                                  &list);
209         add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_2_HOUR, 
210                                  _("mcen_va_options_updateinterval_2h"), 
211                                  &list);
212
213         return (ModestPairList *) g_slist_reverse (list);
214 }
215
216 /*
217  * Gets a list of pairs 
218  */
219 ModestPairList *
220 _modest_global_settings_dialog_get_msg_formats (void)
221 {
222         GSList *list = NULL;
223
224         add_to_modest_pair_list (MODEST_FILE_FORMAT_FORMATTED_TEXT, 
225                                  _("mcen_va_options_messageformat_html"), 
226                                  &list);
227         add_to_modest_pair_list (MODEST_FILE_FORMAT_PLAIN_TEXT, 
228                                  _("mcen_va_options_messageformat_plain"), 
229                                  &list);
230
231         return (ModestPairList *) g_slist_reverse (list);
232 }
233
234 void   
235 _modest_global_settings_dialog_load_conf (ModestGlobalSettingsDialogPrivate *priv)
236 {
237         ModestConf *conf;
238         gboolean checked;
239         gint combo_id, value;
240         GError *error = NULL;
241
242         conf = modest_runtime_get_conf ();
243
244         /* Autoupdate */
245         checked = modest_conf_get_bool (conf, MODEST_CONF_AUTO_UPDATE, &error);
246         if (error) {
247                 g_clear_error (&error);
248                 error = NULL;
249                 checked = FALSE;
250         }
251         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->auto_update), checked);
252
253         /* Connected by */
254         combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, &error);
255         if (error) {
256                 g_error_free (error);
257                 error = NULL;
258                 combo_id = MODEST_CONNECTED_VIA_WLAN;
259         }
260         modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->connect_via), 
261                                         (gpointer) &combo_id);
262
263         /* Update interval */
264         combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_INTERVAL, &error);
265         if (error) {
266                 g_error_free (error);
267                 error = NULL;
268                 combo_id = MODEST_UPDATE_INTERVAL_15_MIN;
269         }
270         modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->update_interval), 
271                                         (gpointer) &combo_id);
272
273         /* Size limit */
274         value  = modest_conf_get_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, &error);
275         if (error) {
276                 g_error_free (error);
277                 error = NULL;
278                 value = 1000;
279         }
280         /* It's better to do this in the subclasses, but it's just one
281            line, so we'll leave it here for the moment */
282 #ifdef MODEST_PLATFORM_MAEMO
283         hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (priv->size_limit), value);
284 #else
285         gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_limit, value);
286 #endif
287
288         /* TODO Fix with the value */
289
290         /* Play sound */
291         checked = modest_conf_get_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, &error);
292         if (error) {
293                 g_error_free (error);
294                 error = NULL;
295                 checked = FALSE;
296         }
297         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->play_sound), checked);
298
299         /* Msg format */
300         combo_id = modest_conf_get_int (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, &error);
301         if (error) {
302                 g_error_free (error);
303                 error = NULL;
304                 combo_id = MODEST_FILE_FORMAT_FORMATTED_TEXT;
305         }
306         modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->msg_format), 
307                                         (gpointer) &combo_id);
308
309         /* Include reply */
310         value = modest_conf_get_int (conf, MODEST_CONF_REPLY_TYPE, &error);
311         if (error) {
312                 g_error_free (error);
313                 error = NULL;
314                 value = MODEST_TNY_MSG_REPLY_TYPE_QUOTE;
315         }
316         if (value == MODEST_TNY_MSG_REPLY_TYPE_QUOTE)
317                 checked = TRUE;
318         else
319                 checked = FALSE;
320         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->include_reply), checked);
321 }
322
323 void
324 _modest_global_settings_dialog_save_conf (ModestGlobalSettingsDialogPrivate *priv)
325 {
326         ModestConf *conf;
327         gboolean checked;
328         gint *combo_id, value;
329
330         conf = modest_runtime_get_conf ();
331
332         /* Autoupdate */
333         checked = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->auto_update));
334         modest_conf_set_bool (conf, MODEST_CONF_AUTO_UPDATE, checked, NULL);
335
336         /* Connected by */
337         combo_id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->connect_via));
338         modest_conf_set_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, *combo_id, NULL);
339
340         /* Update interval */
341         combo_id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->update_interval));
342         modest_conf_set_int (conf, MODEST_CONF_UPDATE_INTERVAL, *combo_id, NULL);
343
344         /* Size limit */        
345         /* It's better to do this in the subclasses, but it's just one
346            line, so we'll leave it here for the moment */
347 #ifdef MODEST_PLATFORM_MAEMO
348         value = hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (priv->size_limit));
349 #else
350         value = gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_limit);
351 #endif
352         modest_conf_set_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, value, NULL);
353
354         /* Play sound */
355         checked = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->play_sound));
356         modest_conf_set_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, checked, NULL);
357
358         /* Msg format */
359         combo_id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->msg_format));
360         modest_conf_set_int (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, *combo_id, NULL);
361
362         /* Include reply */
363         checked = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->include_reply));
364         modest_conf_set_int (conf, MODEST_CONF_REPLY_TYPE, 
365                              (checked) ? MODEST_TNY_MSG_REPLY_TYPE_QUOTE : 
366                              MODEST_TNY_MSG_REPLY_TYPE_CITE, NULL);
367 }
368
369 static void
370 on_response (GtkDialog *dialog,
371              gint arg1,
372              gpointer user_data)
373 {
374         ModestGlobalSettingsDialogPrivate *priv;
375
376         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (user_data);
377
378         if (arg1 == GTK_RESPONSE_OK)
379                 _modest_global_settings_dialog_save_conf (priv);
380 }