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