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