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