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