c57246d1bf86e0e9b57f907a4aff967392ff6fed
[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 "modest-platform.h"
43 #include "widgets/modest-combo-box.h"
44 #ifdef MODEST_PLATFORM_MAEMO
45 #ifdef MODEST_HILDON_VERSION_0
46 #include <hildon-widgets/hildon-number-editor.h>
47 #else
48 #include <hildon/hildon-number-editor.h>
49 #endif /*MODEST_HILDON_VERSION_0*/
50 #endif
51 /* include other impl specific header files */
52
53 #define RETURN_FALSE_ON_ERROR(error) if (error) { g_clear_error (&error); return FALSE; }
54
55 /* 'private'/'protected' functions */
56 static void modest_global_settings_dialog_class_init (ModestGlobalSettingsDialogClass *klass);
57 static void modest_global_settings_dialog_init       (ModestGlobalSettingsDialog *obj);
58 static void modest_global_settings_dialog_finalize   (GObject *obj);
59
60 static void on_response (GtkDialog *dialog,
61                          gint arg1,
62                          gpointer user_data);
63 static void get_current_settings (ModestGlobalSettingsDialogPrivate *priv, 
64                                   ModestGlobalSettingsState *state);
65
66 static ModestConnectedVia current_connection_default (void);
67
68 /* list my signals  */
69 enum {
70         /* MY_SIGNAL_1, */
71         /* MY_SIGNAL_2, */
72         LAST_SIGNAL
73 };
74
75 /* globals */
76 static GtkDialogClass *parent_class = NULL;
77
78 /* uncomment the following if you have defined any signals */
79 /* static guint signals[LAST_SIGNAL] = {0}; */
80
81 GType
82 modest_global_settings_dialog_get_type (void)
83 {
84         static GType my_type = 0;
85         if (!my_type) {
86                 static const GTypeInfo my_info = {
87                         sizeof(ModestGlobalSettingsDialogClass),
88                         NULL,           /* base init */
89                         NULL,           /* base finalize */
90                         (GClassInitFunc) modest_global_settings_dialog_class_init,
91                         NULL,           /* class finalize */
92                         NULL,           /* class data */
93                         sizeof(ModestGlobalSettingsDialog),
94                         1,              /* n_preallocs */
95                         (GInstanceInitFunc) modest_global_settings_dialog_init,
96                         NULL
97                 };
98                 my_type = g_type_register_static (GTK_TYPE_DIALOG,
99                                                   "ModestGlobalSettingsDialog",
100                                                   &my_info, 0);
101         }
102         return my_type;
103 }
104
105 static void
106 modest_global_settings_dialog_class_init (ModestGlobalSettingsDialogClass *klass)
107 {
108         GObjectClass *gobject_class;
109         gobject_class = (GObjectClass*) klass;
110
111         parent_class            = g_type_class_peek_parent (klass);
112         gobject_class->finalize = modest_global_settings_dialog_finalize;
113
114         g_type_class_add_private (gobject_class, sizeof(ModestGlobalSettingsDialogPrivate));
115
116         klass->current_connection_func = current_connection_default;
117 }
118
119 static void
120 modest_global_settings_dialog_init (ModestGlobalSettingsDialog *self)
121 {
122         ModestGlobalSettingsDialogPrivate *priv;
123
124         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
125
126         priv->notebook = gtk_notebook_new ();
127         priv->changed = FALSE;
128
129         /* Add the buttons: */
130         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_OK, GTK_RESPONSE_OK);
131         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
132     
133         /* Connect to the dialog's response signal: */
134         g_signal_connect (G_OBJECT (self), "response", G_CALLBACK (on_response), self);
135
136         /* Set title */
137         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_options"));
138 }
139
140 static void
141 modest_global_settings_dialog_finalize (GObject *obj)
142 {
143 /*      free/unref instance resources here */
144         G_OBJECT_CLASS(parent_class)->finalize (obj);
145 }
146
147 /*
148  * Creates a pair list (number,string) and adds it to the given list
149  */
150 static void
151 add_to_modest_pair_list (const gint num, const gchar *str, GSList **list)
152 {
153         gint *number;
154         ModestPair *pair;
155
156         number = g_malloc0 (sizeof (gint));
157         *number = num;
158         pair = modest_pair_new (number, g_strdup (str), FALSE);
159         *list = g_slist_prepend (*list, pair);
160 }
161
162 /*
163  * Gets a list of pairs 
164  */
165 ModestPairList *
166 _modest_global_settings_dialog_get_connected_via (void)
167 {
168         GSList *list = NULL;
169
170         add_to_modest_pair_list (MODEST_CONNECTED_VIA_WLAN, 
171                                  _("mcen_va_options_connectiontype_wlan"), 
172                                  &list);
173         add_to_modest_pair_list (MODEST_CONNECTED_VIA_ANY, 
174                                  _("mcen_va_options_connectiontype_all"), 
175                                  &list);
176
177         return (ModestPairList *) g_slist_reverse (list);
178 }
179
180 /*
181  * Gets a list of pairs of update intervals
182  */
183 ModestPairList *
184 _modest_global_settings_dialog_get_update_interval (void)
185 {
186         GSList *list = NULL;
187
188         add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_5_MIN, 
189                                  _("mcen_va_options_updateinterval_5min"), 
190                                  &list);
191         add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_10_MIN, 
192                                  _("mcen_va_options_updateinterval_10min"), 
193                                  &list);
194         add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_15_MIN, 
195                                  _("mcen_va_options_updateinterval_15min"), 
196                                  &list);
197         add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_30_MIN, 
198                                  _("mcen_va_options_updateinterval_30min"), 
199                                  &list);
200         add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_1_HOUR, 
201                                  _("mcen_va_options_updateinterval_1h"), 
202                                  &list);
203         add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_2_HOUR, 
204                                  _("mcen_va_options_updateinterval_2h"), 
205                                  &list);
206
207         return (ModestPairList *) g_slist_reverse (list);
208 }
209
210 /*
211  * Gets a list of pairs 
212  */
213 ModestPairList *
214 _modest_global_settings_dialog_get_msg_formats (void)
215 {
216         GSList *list = NULL;
217
218         add_to_modest_pair_list (MODEST_FILE_FORMAT_FORMATTED_TEXT, 
219                                  _("mcen_va_options_messageformat_html"), 
220                                  &list);
221         add_to_modest_pair_list (MODEST_FILE_FORMAT_PLAIN_TEXT, 
222                                  _("mcen_va_options_messageformat_plain"), 
223                                  &list);
224
225         return (ModestPairList *) g_slist_reverse (list);
226 }
227
228 void   
229 _modest_global_settings_dialog_load_conf (ModestGlobalSettingsDialog *self)
230 {
231         ModestConf *conf;
232         gboolean checked;
233         gint combo_id, value;
234         GError *error = NULL;
235         ModestGlobalSettingsDialogPrivate *priv;
236
237         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
238         conf = modest_runtime_get_conf ();
239
240         /* Autoupdate */
241         checked = modest_conf_get_bool (conf, MODEST_CONF_AUTO_UPDATE, &error);
242         if (error) {
243                 g_clear_error (&error);
244                 error = NULL;
245                 checked = FALSE;
246         }
247         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->auto_update), checked);
248         priv->initial_state.auto_update = checked;
249
250         /* Connected by */
251         combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, &error);
252         if (error) {
253                 g_error_free (error);
254                 error = NULL;
255                 combo_id = MODEST_CONNECTED_VIA_WLAN;
256         }
257         modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->connect_via), 
258                                         (gpointer) &combo_id);
259         priv->initial_state.connect_via = combo_id;
260
261         /* Emit toggled to update the visibility of connect_by caption */
262         gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (priv->auto_update));
263
264         /* Update interval */
265         combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_INTERVAL, &error);
266         if (error) {
267                 g_error_free (error);
268                 error = NULL;
269                 combo_id = MODEST_UPDATE_INTERVAL_15_MIN;
270         }
271         modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->update_interval), 
272                                         (gpointer) &combo_id);
273         priv->initial_state.update_interval = combo_id;
274
275         /* Size limit */
276         value  = modest_conf_get_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, &error);
277         if (error) {
278                 g_error_free (error);
279                 error = NULL;
280                 value = 1000;
281         }
282         /* It's better to do this in the subclasses, but it's just one
283            line, so we'll leave it here for the moment */
284 #ifdef MODEST_PLATFORM_MAEMO
285         hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (priv->size_limit), value);
286 #else
287         gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_limit), value);
288 #endif
289         priv->initial_state.size_limit = value;
290
291         /* Play sound */
292         checked = modest_conf_get_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, &error);
293         if (error) {
294                 g_error_free (error);
295                 error = NULL;
296                 checked = FALSE;
297         }
298         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->play_sound), checked);
299         priv->initial_state.play_sound = checked;
300
301         /* Msg format */
302         checked = modest_conf_get_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, &error);
303         if (error) {
304                 g_error_free (error);
305                 error = NULL;
306                 combo_id = MODEST_FILE_FORMAT_FORMATTED_TEXT;
307         }       
308         combo_id = (checked) ? MODEST_FILE_FORMAT_FORMATTED_TEXT : MODEST_FILE_FORMAT_PLAIN_TEXT;
309         modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->msg_format), 
310                                         (gpointer) &combo_id);
311         priv->initial_state.prefer_formatted_text = checked;
312
313         /* Include reply */
314         value = modest_conf_get_int (conf, MODEST_CONF_REPLY_TYPE, &error);
315         if (error) {
316                 g_error_free (error);
317                 error = NULL;
318                 value = MODEST_TNY_MSG_REPLY_TYPE_QUOTE;
319         }
320         if (value == MODEST_TNY_MSG_REPLY_TYPE_QUOTE)
321                 checked = TRUE;
322         else
323                 checked = FALSE;
324         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->include_reply), checked);
325         priv->initial_state.include_reply = checked;
326 }
327
328 static void 
329 get_current_settings (ModestGlobalSettingsDialogPrivate *priv, 
330                       ModestGlobalSettingsState *state) 
331 {
332         gint *id;
333
334         /* Get values from UI */
335         state->auto_update = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->auto_update));
336         id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->connect_via));
337         state->connect_via = *id;
338 #ifdef MODEST_PLATFORM_MAEMO
339         state->size_limit = hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (priv->size_limit));
340 #else
341         state->size_limit = gtk_spin_button_get_value (GTK_SPIN_BUTTON (priv->size_limit));
342 #endif
343         id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->update_interval));
344         state->update_interval = *id;
345         state->play_sound = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->play_sound));
346         id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->msg_format));
347         state->prefer_formatted_text = (*id == MODEST_FILE_FORMAT_FORMATTED_TEXT) ? TRUE : FALSE;
348         state->include_reply = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->include_reply));
349 }
350
351 gboolean
352 _modest_global_settings_dialog_save_conf (ModestGlobalSettingsDialog *self)
353 {
354         ModestConf *conf;
355         ModestGlobalSettingsState current_state;
356         GError *error = NULL;
357         ModestGlobalSettingsDialogPrivate *priv;
358
359         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
360
361         conf = modest_runtime_get_conf ();
362
363         get_current_settings (priv, &current_state);
364
365         /* Save configuration */
366         modest_conf_set_bool (conf, MODEST_CONF_AUTO_UPDATE, current_state.auto_update, &error);
367         RETURN_FALSE_ON_ERROR(error);
368         modest_conf_set_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, current_state.connect_via, NULL);
369         RETURN_FALSE_ON_ERROR(error);
370         modest_conf_set_int (conf, MODEST_CONF_UPDATE_INTERVAL, current_state.update_interval, NULL);
371         RETURN_FALSE_ON_ERROR(error);
372         modest_conf_set_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, current_state.size_limit, NULL);
373         RETURN_FALSE_ON_ERROR(error);
374         modest_conf_set_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, current_state.play_sound, NULL);
375         RETURN_FALSE_ON_ERROR(error);
376         modest_conf_set_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, current_state.prefer_formatted_text, NULL);
377         RETURN_FALSE_ON_ERROR(error);
378         modest_conf_set_int (conf, MODEST_CONF_REPLY_TYPE,
379                              (current_state.include_reply) ? MODEST_TNY_MSG_REPLY_TYPE_QUOTE : 
380                              MODEST_TNY_MSG_REPLY_TYPE_CITE, NULL);
381         RETURN_FALSE_ON_ERROR(error);
382
383         /* Apply changes */
384         if (priv->initial_state.auto_update != current_state.auto_update || 
385             priv->initial_state.connect_via != current_state.connect_via ||
386             priv->initial_state.update_interval != current_state.update_interval) {
387
388                 TnyAccountStore *account_store;
389                 TnyDevice *device;
390
391                 if (!current_state.auto_update) {
392                         modest_platform_set_update_interval (0);
393                         /* To avoid a new indentation level */
394                         goto exit;
395                 }
396
397                 account_store = TNY_ACCOUNT_STORE (modest_runtime_get_account_store ());
398                 device = tny_account_store_get_device (account_store);
399
400                 if (tny_device_is_online (device)) {
401                         /* If connected via any then set update interval */
402                         if (current_state.connect_via == MODEST_CONNECTED_VIA_ANY) {
403                                 modest_platform_set_update_interval (current_state.update_interval);
404                         } else {
405                                 /* Set update interval only if we
406                                    selected the same connect_via
407                                    method than the one already used by
408                                    the device */
409                                 ModestConnectedVia connect_via = 
410                                         MODEST_GLOBAL_SETTINGS_DIALOG_GET_CLASS(self)->current_connection_func ();
411                                 
412                                 if (current_state.connect_via == connect_via)
413                                         modest_platform_set_update_interval (current_state.update_interval);
414                                 else
415                                         modest_platform_set_update_interval (0);
416                         }
417                 } else {
418                         /* Disable autoupdate in offline mode */
419                         modest_platform_set_update_interval (0);
420                 }
421                 g_object_unref (device);                
422         }
423
424 exit:
425         return TRUE;
426 }
427
428 static gboolean
429 settings_changed (ModestGlobalSettingsState initial_state,
430                   ModestGlobalSettingsState current_state)
431 {
432         if (initial_state.auto_update != current_state.auto_update ||
433             initial_state.connect_via != current_state.connect_via ||
434             initial_state.update_interval != current_state.update_interval ||
435             initial_state.size_limit != current_state.size_limit ||
436             initial_state.play_sound != current_state.play_sound ||
437             initial_state.prefer_formatted_text != current_state.prefer_formatted_text ||
438             initial_state.include_reply != current_state.include_reply)
439                 return TRUE;
440         else
441                 return FALSE;
442 }
443
444 static void
445 on_response (GtkDialog *dialog,
446              gint arg1,
447              gpointer user_data)
448 {
449         ModestGlobalSettingsDialogPrivate *priv;
450         ModestGlobalSettingsState current_state;
451         gboolean changed;
452
453         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (user_data);
454
455         get_current_settings (priv, &current_state);
456         changed = settings_changed (priv->initial_state, current_state);
457
458         if (arg1 == GTK_RESPONSE_OK) {
459                 if (changed) {
460                         gboolean saved;
461
462                         saved = _modest_global_settings_dialog_save_conf (MODEST_GLOBAL_SETTINGS_DIALOG (dialog));
463                         if (saved) {
464                                 modest_platform_run_information_dialog (GTK_WINDOW (user_data),
465                                                                         _("mcen_ib_advsetup_settings_saved"));
466                         } else {
467                                 modest_platform_run_information_dialog (GTK_WINDOW (user_data),
468                                                                         _("mail_ib_setting_failed"));
469                         }
470                 }
471         } else {
472                 if (changed) {
473                         gint response;
474                         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (user_data), 
475                                                                             _("imum_nc_wizard_confirm_lose_changes"));
476                         /* Do not close if the user Cancels */
477                         if (response == GTK_RESPONSE_CANCEL)
478                                 g_signal_stop_emission_by_name (dialog, "response");
479                 }
480         }
481 }
482
483 static ModestConnectedVia 
484 current_connection_default (void)
485 {
486         g_warning ("You must implement %s", __FUNCTION__);
487         g_return_val_if_reached (-1);
488 }