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