* Fixes NB#80917, added wimax support
[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_HAVE_HILDON0_WIDGETS
46 #include <hildon-widgets/hildon-number-editor.h>
47 #else
48 #include <hildon/hildon-number-editor.h>
49 #endif /*MODEST_HAVE_HILDON0_WIDGETS*/
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
128         /* Add the buttons: */
129         gtk_dialog_add_button (GTK_DIALOG (self), _("mcen_bd_dialog_ok"), GTK_RESPONSE_OK);
130         gtk_dialog_add_button (GTK_DIALOG (self), _("mcen_bd_dialog_cancel"), GTK_RESPONSE_CANCEL);
131     
132         /* Connect to the dialog's response signal: */
133         g_signal_connect (G_OBJECT (self), "response", G_CALLBACK (on_response), self);
134
135         /* Set title */
136         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_options"));
137 }
138
139 static void
140 modest_global_settings_dialog_finalize (GObject *obj)
141 {
142         ModestGlobalSettingsDialogPrivate *priv = 
143                 MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (obj);
144
145         /* These had to stay alive as long as the comboboxes that used them: */
146         modest_pair_list_free (priv->connect_via_list);
147         modest_pair_list_free (priv->update_interval_list);
148         modest_pair_list_free (priv->msg_format_list);
149         
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_OR_WIMAX, 
177                                  _("mcen_va_options_connectiontype_wlan_wimax"), 
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 (ModestGlobalSettingsDialog *self)
236 {
237         ModestConf *conf;
238         gboolean checked;
239         gint combo_id, value;
240         GError *error = NULL;
241         ModestGlobalSettingsDialogPrivate *priv;
242
243         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
244         conf = modest_runtime_get_conf ();
245
246         /* Autoupdate */
247         checked = modest_conf_get_bool (conf, MODEST_CONF_AUTO_UPDATE, &error);
248         if (error) {
249                 g_clear_error (&error);
250                 error = NULL;
251                 checked = FALSE;
252         }
253         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->auto_update), checked);
254         priv->initial_state.auto_update = checked;
255
256         /* Connected by */
257         combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, &error);
258         if (error) {
259                 g_error_free (error);
260                 error = NULL;
261                 combo_id = MODEST_CONNECTED_VIA_WLAN_OR_WIMAX;
262         }
263         modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->connect_via), 
264                                         (gpointer) &combo_id);
265         priv->initial_state.connect_via = combo_id;
266
267         /* Emit toggled to update the visibility of connect_by caption */
268         gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (priv->auto_update));
269
270         /* Update interval */
271         combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_INTERVAL, &error);
272         if (error) {
273                 g_error_free (error);
274                 error = NULL;
275                 combo_id = MODEST_UPDATE_INTERVAL_15_MIN;
276         }
277         modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->update_interval), 
278                                         (gpointer) &combo_id);
279         priv->initial_state.update_interval = combo_id;
280
281         /* Size limit */
282         value  = modest_conf_get_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, &error);
283         if (error) {
284                 g_error_free (error);
285                 error = NULL;
286                 value = 1000;
287         }
288         /* It's better to do this in the subclasses, but it's just one
289            line, so we'll leave it here for the moment */
290 #ifdef MODEST_PLATFORM_MAEMO
291         hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (priv->size_limit), value);
292 #else
293         gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_limit), value);
294 #endif
295         priv->initial_state.size_limit = value;
296
297         /* Play sound */
298         checked = modest_conf_get_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, &error);
299         if (error) {
300                 g_error_free (error);
301                 error = NULL;
302                 checked = FALSE;
303         }
304         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->play_sound), checked);
305         priv->initial_state.play_sound = checked;
306
307         /* Msg format */
308         checked = modest_conf_get_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, &error);
309         if (error) {
310                 g_error_free (error);
311                 error = NULL;
312                 combo_id = MODEST_FILE_FORMAT_FORMATTED_TEXT;
313         }       
314         combo_id = (checked) ? MODEST_FILE_FORMAT_FORMATTED_TEXT : MODEST_FILE_FORMAT_PLAIN_TEXT;
315         modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->msg_format), 
316                                         (gpointer) &combo_id);
317         priv->initial_state.prefer_formatted_text = checked;
318 }
319
320 static void 
321 get_current_settings (ModestGlobalSettingsDialogPrivate *priv, 
322                       ModestGlobalSettingsState *state) 
323 {
324         gint *id;
325
326         /* Get values from UI */
327         state->auto_update = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->auto_update));
328         id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->connect_via));
329         state->connect_via = *id;
330 #ifdef MODEST_PLATFORM_MAEMO
331         state->size_limit = hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (priv->size_limit));
332 #else
333         state->size_limit = gtk_spin_button_get_value (GTK_SPIN_BUTTON (priv->size_limit));
334 #endif
335         id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->update_interval));
336         state->update_interval = *id;
337         state->play_sound = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->play_sound));
338         id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->msg_format));
339         state->prefer_formatted_text = (*id == MODEST_FILE_FORMAT_FORMATTED_TEXT) ? TRUE : FALSE;
340 }
341
342 gboolean
343 _modest_global_settings_dialog_save_conf (ModestGlobalSettingsDialog *self)
344 {
345         ModestConf *conf;
346         ModestGlobalSettingsState current_state;
347         GError *error = NULL;
348         ModestGlobalSettingsDialogPrivate *priv;
349
350         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
351
352         conf = modest_runtime_get_conf ();
353
354         get_current_settings (priv, &current_state);
355
356         /* Save configuration */
357         modest_conf_set_bool (conf, MODEST_CONF_AUTO_UPDATE, current_state.auto_update, &error);
358         RETURN_FALSE_ON_ERROR(error);
359         modest_conf_set_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, current_state.connect_via, NULL);
360         RETURN_FALSE_ON_ERROR(error);
361         modest_conf_set_int (conf, MODEST_CONF_UPDATE_INTERVAL, current_state.update_interval, NULL);
362         RETURN_FALSE_ON_ERROR(error);
363         modest_conf_set_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, current_state.size_limit, NULL);
364         RETURN_FALSE_ON_ERROR(error);
365         modest_conf_set_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, current_state.play_sound, NULL);
366         RETURN_FALSE_ON_ERROR(error);
367         modest_conf_set_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, current_state.prefer_formatted_text, NULL);
368         RETURN_FALSE_ON_ERROR(error);
369
370         /* Apply changes */
371         if (priv->initial_state.auto_update != current_state.auto_update || 
372             priv->initial_state.connect_via != current_state.connect_via ||
373             priv->initial_state.update_interval != current_state.update_interval) {
374
375                 TnyAccountStore *account_store;
376                 TnyDevice *device;
377
378                 if (!current_state.auto_update) {
379                         modest_platform_set_update_interval (0);
380                         /* To avoid a new indentation level */
381                         goto exit;
382                 }
383
384                 account_store = TNY_ACCOUNT_STORE (modest_runtime_get_account_store ());
385                 device = tny_account_store_get_device (account_store);
386
387                 if (tny_device_is_online (device)) {
388                         /* If connected via any then set update interval */
389                         if (current_state.connect_via == MODEST_CONNECTED_VIA_ANY) {
390                                 modest_platform_set_update_interval (current_state.update_interval);
391                         } else {
392                                 /* Set update interval only if we
393                                    selected the same connect_via
394                                    method than the one already used by
395                                    the device */
396                                 ModestConnectedVia connect_via = 
397                                         MODEST_GLOBAL_SETTINGS_DIALOG_GET_CLASS(self)->current_connection_func ();
398                                 
399                                 if (current_state.connect_via == connect_via)
400                                         modest_platform_set_update_interval (current_state.update_interval);
401                                 else
402                                         modest_platform_set_update_interval (0);
403                         }
404                 } else {
405                         /* Disable autoupdate in offline mode */
406                         modest_platform_set_update_interval (0);
407                 }
408                 g_object_unref (device);
409         }
410
411 exit:
412         return TRUE;
413 }
414
415 static gboolean
416 settings_changed (ModestGlobalSettingsState initial_state,
417                   ModestGlobalSettingsState current_state)
418 {
419         if (initial_state.auto_update != current_state.auto_update ||
420             initial_state.connect_via != current_state.connect_via ||
421             initial_state.update_interval != current_state.update_interval ||
422             initial_state.size_limit != current_state.size_limit ||
423             initial_state.play_sound != current_state.play_sound ||
424             initial_state.prefer_formatted_text != current_state.prefer_formatted_text)
425                 return TRUE;
426         else
427                 return FALSE;
428 }
429
430 static void
431 on_response (GtkDialog *dialog,
432              gint arg1,
433              gpointer user_data)
434 {
435         ModestGlobalSettingsDialogPrivate *priv;
436         ModestGlobalSettingsState current_state;
437         gboolean changed;
438
439         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (user_data);
440
441         get_current_settings (priv, &current_state);
442         changed = settings_changed (priv->initial_state, current_state);
443
444         if (arg1 == GTK_RESPONSE_OK) {
445                 if (changed) {
446                         gboolean saved;
447
448                         saved = _modest_global_settings_dialog_save_conf (MODEST_GLOBAL_SETTINGS_DIALOG (dialog));
449                         if (saved) {
450                                 modest_platform_run_information_dialog (GTK_WINDOW (user_data),
451                                                                         _("mcen_ib_advsetup_settings_saved"));
452                         } else {
453                                 modest_platform_run_information_dialog (GTK_WINDOW (user_data),
454                                                                         _("mail_ib_setting_failed"));
455                         }
456                 }
457         } else {
458                 if (changed) {
459                         gint response;
460                         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (user_data), 
461                                                                             _("imum_nc_wizard_confirm_lose_changes"));
462                         /* Do not close if the user Cancels */
463                         if (response == GTK_RESPONSE_CANCEL)
464                                 g_signal_stop_emission_by_name (dialog, "response");
465                 }
466         }
467 }
468
469 static ModestConnectedVia 
470 current_connection_default (void)
471 {
472         g_warning ("You must implement %s", __FUNCTION__);
473         g_return_val_if_reached (MODEST_CONNECTED_VIA_ANY);
474 }