f77fc94835411315ce4f37f40e6c6785d1680c44
[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_HILDON_VERSION_0
46 #include <hildon-widgets/hildon-number-editor.h>
47 #else
48 #include <hildon/hildon-number-editor.h>
49 #endif /*MODEST_HILDON_VERSION_0*/
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 /*      GdkGeometry *geometry; */
124
125         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
126
127         priv->notebook = gtk_notebook_new ();
128         priv->changed = FALSE;
129
130         /* Add the buttons: */
131         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_OK, GTK_RESPONSE_OK);
132         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
133     
134         /* Connect to the dialog's response signal: */
135         g_signal_connect (G_OBJECT (self), "response", G_CALLBACK (on_response), self);
136
137         /* Set title */
138         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_options"));
139
140         /* Set geometry */
141 /*      geometry = g_malloc0(sizeof (GdkGeometry)); */
142 /*      geometry->max_width = MODEST_DIALOG_WINDOW_MAX_WIDTH; */
143 /*      geometry->min_width = MODEST_DIALOG_WINDOW_MIN_WIDTH; */
144 /*      geometry->max_height = MODEST_DIALOG_WINDOW_MAX_HEIGHT; */
145 /*      geometry->min_height = MODEST_DIALOG_WINDOW_MIN_HEIGHT; */
146 /*      gtk_window_set_geometry_hints (GTK_WINDOW (self), */
147 /*                                     GTK_WIDGET (self), */
148 /*                                     geometry, */
149 /*                                     GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE); */
150         gtk_widget_set_size_request (GTK_WIDGET (self), 
151                                      MODEST_DIALOG_WINDOW_MAX_WIDTH, 
152                                      MODEST_DIALOG_WINDOW_MAX_HEIGHT);
153 }
154
155 static void
156 modest_global_settings_dialog_finalize (GObject *obj)
157 {
158 /*      free/unref instance resources here */
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 /*
178  * Gets a list of pairs 
179  */
180 ModestPairList *
181 _modest_global_settings_dialog_get_connected_via (void)
182 {
183         GSList *list = NULL;
184
185         add_to_modest_pair_list (MODEST_CONNECTED_VIA_WLAN, 
186                                  _("mcen_va_options_connectiontype_wlan"), 
187                                  &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 void   
244 _modest_global_settings_dialog_load_conf (ModestGlobalSettingsDialog *self)
245 {
246         ModestConf *conf;
247         gboolean checked;
248         gint combo_id, value;
249         GError *error = NULL;
250         ModestGlobalSettingsDialogPrivate *priv;
251
252         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
253         conf = modest_runtime_get_conf ();
254
255         /* Autoupdate */
256         checked = modest_conf_get_bool (conf, MODEST_CONF_AUTO_UPDATE, &error);
257         if (error) {
258                 g_clear_error (&error);
259                 error = NULL;
260                 checked = FALSE;
261         }
262         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->auto_update), checked);
263         priv->initial_state.auto_update = checked;
264
265         /* Connected by */
266         combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, &error);
267         if (error) {
268                 g_error_free (error);
269                 error = NULL;
270                 combo_id = MODEST_CONNECTED_VIA_WLAN;
271         }
272         modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->connect_via), 
273                                         (gpointer) &combo_id);
274         priv->initial_state.connect_via = combo_id;
275
276         /* Emit toggled to update the visibility of connect_by caption */
277         gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (priv->auto_update));
278
279         /* Update interval */
280         combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_INTERVAL, &error);
281         if (error) {
282                 g_error_free (error);
283                 error = NULL;
284                 combo_id = MODEST_UPDATE_INTERVAL_15_MIN;
285         }
286         modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->update_interval), 
287                                         (gpointer) &combo_id);
288         priv->initial_state.update_interval = combo_id;
289
290         /* Size limit */
291         value  = modest_conf_get_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, &error);
292         if (error) {
293                 g_error_free (error);
294                 error = NULL;
295                 value = 1000;
296         }
297         /* It's better to do this in the subclasses, but it's just one
298            line, so we'll leave it here for the moment */
299 #ifdef MODEST_PLATFORM_MAEMO
300         hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (priv->size_limit), value);
301 #else
302         gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_limit, value));
303 #endif
304         priv->initial_state.size_limit = value;
305
306         /* Play sound */
307         checked = modest_conf_get_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, &error);
308         if (error) {
309                 g_error_free (error);
310                 error = NULL;
311                 checked = FALSE;
312         }
313         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->play_sound), checked);
314         priv->initial_state.play_sound = checked;
315
316         /* Msg format */
317         combo_id = modest_conf_get_int (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, &error);
318         if (error) {
319                 g_error_free (error);
320                 error = NULL;
321                 combo_id = MODEST_FILE_FORMAT_FORMATTED_TEXT;
322         }
323         modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->msg_format), 
324                                         (gpointer) &combo_id);
325         priv->initial_state.msg_format = combo_id;
326
327         /* Include reply */
328         value = modest_conf_get_int (conf, MODEST_CONF_REPLY_TYPE, &error);
329         if (error) {
330                 g_error_free (error);
331                 error = NULL;
332                 value = MODEST_TNY_MSG_REPLY_TYPE_QUOTE;
333         }
334         if (value == MODEST_TNY_MSG_REPLY_TYPE_QUOTE)
335                 checked = TRUE;
336         else
337                 checked = FALSE;
338         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->include_reply), checked);
339         priv->initial_state.include_reply = checked;
340 }
341
342 static void 
343 get_current_settings (ModestGlobalSettingsDialogPrivate *priv, 
344                       ModestGlobalSettingsState *state) 
345 {
346         gint *id;
347
348         /* Get values from UI */
349         state->auto_update = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->auto_update));
350         id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->connect_via));
351         state->connect_via = *id;
352 #ifdef MODEST_PLATFORM_MAEMO
353         state->size_limit = hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (priv->size_limit));
354 #else
355         state->size_limit = gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_limit));
356 #endif
357         id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->update_interval));
358         state->update_interval = *id;
359         state->play_sound = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->play_sound));
360         id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->msg_format));
361         state->msg_format = *id;
362         state->include_reply = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->include_reply));
363 }
364
365 gboolean
366 _modest_global_settings_dialog_save_conf (ModestGlobalSettingsDialog *self)
367 {
368         ModestConf *conf;
369         ModestGlobalSettingsState current_state;
370         GError *error = NULL;
371         ModestGlobalSettingsDialogPrivate *priv;
372
373         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
374
375         conf = modest_runtime_get_conf ();
376
377         get_current_settings (priv, &current_state);
378
379         /* Save configuration */
380         modest_conf_set_bool (conf, MODEST_CONF_AUTO_UPDATE, current_state.auto_update, &error);
381         RETURN_FALSE_ON_ERROR(error);
382         modest_conf_set_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, current_state.connect_via, NULL);
383         RETURN_FALSE_ON_ERROR(error);
384         modest_conf_set_int (conf, MODEST_CONF_UPDATE_INTERVAL, current_state.update_interval, NULL);
385         RETURN_FALSE_ON_ERROR(error);
386         modest_conf_set_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, current_state.size_limit, NULL);
387         RETURN_FALSE_ON_ERROR(error);
388         modest_conf_set_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, current_state.play_sound, NULL);
389         RETURN_FALSE_ON_ERROR(error);
390         modest_conf_set_int (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, current_state.msg_format, NULL);
391         RETURN_FALSE_ON_ERROR(error);
392         modest_conf_set_int (conf, MODEST_CONF_REPLY_TYPE,
393                              (current_state.include_reply) ? MODEST_TNY_MSG_REPLY_TYPE_QUOTE : 
394                              MODEST_TNY_MSG_REPLY_TYPE_CITE, NULL);
395         RETURN_FALSE_ON_ERROR(error);
396
397         /* Apply changes */
398         if (priv->initial_state.auto_update != current_state.auto_update || 
399             priv->initial_state.connect_via != current_state.connect_via ||
400             priv->initial_state.update_interval != current_state.update_interval) {
401
402                 TnyAccountStore *account_store;
403                 TnyDevice *device;
404
405                 if (!current_state.auto_update) {
406                         modest_platform_set_update_interval (0);
407                         /* To avoid a new indentation level */
408                         goto exit;
409                 }
410
411                 account_store = TNY_ACCOUNT_STORE (modest_runtime_get_account_store ());
412                 device = tny_account_store_get_device (account_store);
413
414                 if (tny_device_is_online (device)) {
415                         /* If connected via any then set update interval */
416                         if (current_state.connect_via == MODEST_CONNECTED_VIA_ANY) {
417                                 modest_platform_set_update_interval (current_state.update_interval);
418                         } else {
419                                 /* Set update interval only if we
420                                    selected the same connect_via
421                                    method than the one already used by
422                                    the device */
423                                 ModestConnectedVia connect_via = 
424                                         MODEST_GLOBAL_SETTINGS_DIALOG_GET_CLASS(self)->current_connection_func ();
425                                 
426                                 if (current_state.connect_via == connect_via)
427                                         modest_platform_set_update_interval (current_state.update_interval);
428                                 else
429                                         modest_platform_set_update_interval (0);
430                         }
431                 } else {
432                         /* Disable autoupdate in offline mode */
433                         modest_platform_set_update_interval (0);
434                 }
435                 g_object_unref (device);                
436         }
437
438 exit:
439         return TRUE;
440 }
441
442 static gboolean
443 settings_changed (ModestGlobalSettingsState initial_state,
444                   ModestGlobalSettingsState current_state)
445 {
446         if (initial_state.auto_update != current_state.auto_update ||
447             initial_state.connect_via != current_state.connect_via ||
448             initial_state.update_interval != current_state.update_interval ||
449             initial_state.size_limit != current_state.size_limit ||
450             initial_state.play_sound != current_state.play_sound ||
451             initial_state.msg_format != current_state.msg_format ||
452             initial_state.include_reply != current_state.include_reply)
453                 return TRUE;
454         else
455                 return FALSE;
456 }
457
458 static void
459 on_response (GtkDialog *dialog,
460              gint arg1,
461              gpointer user_data)
462 {
463         ModestGlobalSettingsDialogPrivate *priv;
464         ModestGlobalSettingsState current_state;
465         gboolean changed;
466
467         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (user_data);
468
469         get_current_settings (priv, &current_state);
470         changed = settings_changed (priv->initial_state, current_state);
471
472         if (arg1 == GTK_RESPONSE_OK) {
473                 if (changed) {
474                         gboolean saved;
475
476                         saved = _modest_global_settings_dialog_save_conf (MODEST_GLOBAL_SETTINGS_DIALOG (dialog));
477                         if (saved) {
478                                 modest_platform_run_information_dialog (GTK_WINDOW (user_data),
479                                                                         _("mcen_ib_advsetup_settings_saved"));
480                         } else {
481                                 modest_platform_run_information_dialog (GTK_WINDOW (user_data),
482                                                                         _("mail_ib_setting_failed"));
483                         }
484                 }
485         } else {
486                 if (changed) {
487                         gint response;
488                         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (user_data), 
489                                                                             _("imum_nc_wizard_confirm_lose_changes"));
490                         /* Do not close if the user Cancels */
491                         if (response == GTK_RESPONSE_CANCEL)
492                                 g_signal_stop_emission_by_name (dialog, "response");
493                 }
494         }
495 }
496
497 static ModestConnectedVia 
498 current_connection_default (void)
499 {
500         g_warning ("You must implement %s", __FUNCTION__);
501         g_return_val_if_reached (-1);
502 }