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