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