1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
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>
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"
48 #include "widgets/modest-combo-box.h"
50 #ifndef MODEST_TOOLKIT_GTK
51 #if MODEST_HILDON_API == 0
52 #include <hildon-widgets/hildon-number-editor.h>
54 #include <hildon/hildon-number-editor.h>
55 #endif /*MODEST_HILDON_API = 0*/
57 /* include other impl specific header files */
59 #define RETURN_FALSE_ON_ERROR(error) if (error) { g_clear_error (&error); return FALSE; }
61 /* 'private'/'protected' functions */
62 static void modest_global_settings_dialog_class_init (ModestGlobalSettingsDialogClass *klass);
63 static void modest_global_settings_dialog_init (ModestGlobalSettingsDialog *obj);
64 static void modest_global_settings_dialog_finalize (GObject *obj);
66 static void on_response (GtkDialog *dialog,
69 static void get_current_settings (ModestGlobalSettingsDialogPrivate *priv,
70 ModestGlobalSettingsState *state);
72 static ModestConnectedVia current_connection_default (void);
82 static GtkDialogClass *parent_class = NULL;
84 /* uncomment the following if you have defined any signals */
85 /* static guint signals[LAST_SIGNAL] = {0}; */
88 modest_global_settings_dialog_get_type (void)
90 static GType my_type = 0;
92 static const GTypeInfo my_info = {
93 sizeof(ModestGlobalSettingsDialogClass),
95 NULL, /* base finalize */
96 (GClassInitFunc) modest_global_settings_dialog_class_init,
97 NULL, /* class finalize */
98 NULL, /* class data */
99 sizeof(ModestGlobalSettingsDialog),
101 (GInstanceInitFunc) modest_global_settings_dialog_init,
104 #ifdef MODEST_TOOLKIT_HILDON2
105 my_type = g_type_register_static (HILDON_TYPE_DIALOG,
106 "ModestGlobalSettingsDialog",
109 my_type = g_type_register_static (GTK_TYPE_DIALOG,
110 "ModestGlobalSettingsDialog",
118 modest_global_settings_dialog_class_init (ModestGlobalSettingsDialogClass *klass)
120 GObjectClass *gobject_class;
121 gobject_class = (GObjectClass*) klass;
123 parent_class = g_type_class_peek_parent (klass);
124 gobject_class->finalize = modest_global_settings_dialog_finalize;
126 g_type_class_add_private (gobject_class, sizeof(ModestGlobalSettingsDialogPrivate));
128 klass->current_connection_func = current_connection_default;
132 modest_global_settings_dialog_init (ModestGlobalSettingsDialog *self)
134 ModestGlobalSettingsDialogPrivate *priv;
136 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
138 priv->notebook = gtk_notebook_new ();
140 /* Connect to the dialog's response signal: */
141 g_signal_connect (G_OBJECT (self), "response", G_CALLBACK (on_response), self);
144 gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_options"));
148 modest_global_settings_dialog_finalize (GObject *obj)
150 ModestGlobalSettingsDialogPrivate *priv =
151 MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (obj);
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);
158 G_OBJECT_CLASS(parent_class)->finalize (obj);
162 * Creates a pair list (number,string) and adds it to the given list
165 add_to_modest_pair_list (const gint num, const gchar *str, GSList **list)
170 number = g_malloc0 (sizeof (gint));
172 pair = modest_pair_new (number, g_strdup (str), FALSE);
173 *list = g_slist_prepend (*list, pair);
177 _modest_global_settings_dialog_get_connected_via (void)
180 const gchar *message;
182 #ifndef MODEST_TOOLKIT_GTK
183 const gchar *env_var = getenv ("OSSO_PRODUCT_HARDWARE");
184 /* Check if WIMAX is available */
185 if (env_var && !strncmp (env_var, "RX-48", 5))
186 message = _("mcen_va_options_connectiontype_wlan_wimax");
188 message = _("mcen_va_options_connectiontype_wlan");
190 message = _("mcen_va_options_connectiontype_wlan");
192 add_to_modest_pair_list (MODEST_CONNECTED_VIA_WLAN_OR_WIMAX, message, &list);
193 add_to_modest_pair_list (MODEST_CONNECTED_VIA_ANY,
194 _("mcen_va_options_connectiontype_all"),
197 return (ModestPairList *) g_slist_reverse (list);
201 * Gets a list of pairs of update intervals
204 _modest_global_settings_dialog_get_update_interval (void)
208 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_5_MIN,
209 _("mcen_va_options_updateinterval_5min"),
211 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_10_MIN,
212 _("mcen_va_options_updateinterval_10min"),
214 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_15_MIN,
215 _("mcen_va_options_updateinterval_15min"),
217 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_30_MIN,
218 _("mcen_va_options_updateinterval_30min"),
220 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_1_HOUR,
221 _("mcen_va_options_updateinterval_1h"),
223 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_2_HOUR,
224 _("mcen_va_options_updateinterval_2h"),
227 return (ModestPairList *) g_slist_reverse (list);
231 * Gets a list of pairs
234 _modest_global_settings_dialog_get_msg_formats (void)
238 add_to_modest_pair_list (MODEST_FILE_FORMAT_FORMATTED_TEXT,
239 _("mcen_va_options_messageformat_html"),
241 add_to_modest_pair_list (MODEST_FILE_FORMAT_PLAIN_TEXT,
242 _("mcen_va_options_messageformat_plain"),
245 return (ModestPairList *) g_slist_reverse (list);
249 _modest_global_settings_dialog_load_conf (ModestGlobalSettingsDialog *self)
253 gint combo_id, value;
254 GError *error = NULL;
255 ModestGlobalSettingsDialogPrivate *priv;
257 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
258 conf = modest_runtime_get_conf ();
261 checked = modest_conf_get_bool (conf, MODEST_CONF_AUTO_UPDATE, &error);
263 g_clear_error (&error);
267 #ifdef MODEST_TOOLKIT_HILDON2
268 hildon_check_button_set_active (HILDON_CHECK_BUTTON (priv->auto_update), checked);
270 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->auto_update), checked);
272 priv->initial_state.auto_update = checked;
275 combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, &error);
277 g_error_free (error);
279 combo_id = MODEST_CONNECTED_VIA_WLAN_OR_WIMAX;
281 #ifdef MODEST_TOOLKIT_HILDON2
282 modest_selector_picker_set_active_id (MODEST_SELECTOR_PICKER (priv->connect_via),
283 (gpointer) &combo_id);
285 modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->connect_via),
286 (gpointer) &combo_id);
288 priv->initial_state.connect_via = combo_id;
290 /* Emit toggled to update the visibility of connect_by caption */
291 #ifndef MODEST_TOOLKIT_HILDON2
292 gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (priv->auto_update));
295 /* Update interval */
296 combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_INTERVAL, &error);
298 g_error_free (error);
300 combo_id = MODEST_UPDATE_INTERVAL_15_MIN;
302 #ifdef MODEST_TOOLKIT_HILDON2
303 modest_selector_picker_set_active_id (MODEST_SELECTOR_PICKER (priv->update_interval),
304 (gpointer) &combo_id);
306 modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->update_interval),
307 (gpointer) &combo_id);
309 priv->initial_state.update_interval = combo_id;
312 value = modest_conf_get_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, &error);
314 g_error_free (error);
318 /* It's better to do this in the subclasses, but it's just one
319 line, so we'll leave it here for the moment */
320 #ifndef MODEST_TOOLKIT_GTK
321 hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (priv->size_limit), value);
323 gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_limit), value);
325 priv->initial_state.size_limit = value;
328 checked = modest_conf_get_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, &error);
330 g_error_free (error);
334 #ifndef MODEST_TOOLKIT_HILDON2
335 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->play_sound), checked);
337 priv->initial_state.play_sound = checked;
340 checked = modest_conf_get_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, &error);
342 g_error_free (error);
344 combo_id = MODEST_FILE_FORMAT_FORMATTED_TEXT;
346 combo_id = (checked) ? MODEST_FILE_FORMAT_FORMATTED_TEXT : MODEST_FILE_FORMAT_PLAIN_TEXT;
347 #ifdef MODEST_TOOLKIT_HILDON2
348 modest_selector_picker_set_active_id (MODEST_SELECTOR_PICKER (priv->msg_format),
349 (gpointer) &combo_id);
351 modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->msg_format),
352 (gpointer) &combo_id);
354 priv->initial_state.prefer_formatted_text = checked;
358 get_current_settings (ModestGlobalSettingsDialogPrivate *priv,
359 ModestGlobalSettingsState *state)
363 /* Get values from UI */
364 #ifdef MODEST_TOOLKIT_HILDON2
365 id = modest_selector_picker_get_active_id (MODEST_SELECTOR_PICKER (priv->connect_via));
366 state->auto_update = hildon_check_button_get_active (HILDON_CHECK_BUTTON (priv->auto_update));
368 id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->connect_via));
369 state->auto_update = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->auto_update));
371 state->connect_via = *id;
372 #ifndef MODEST_TOOLKIT_GTK
373 state->size_limit = hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (priv->size_limit));
375 state->size_limit = gtk_spin_button_get_value (GTK_SPIN_BUTTON (priv->size_limit));
378 #ifdef MODEST_TOOLKIT_HILDON2
379 id = modest_selector_picker_get_active_id (MODEST_SELECTOR_PICKER (priv->update_interval));
381 id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->update_interval));
383 state->update_interval = *id;
384 #ifdef MODEST_TOOLKIT_HILDON2
385 id = modest_selector_picker_get_active_id (MODEST_SELECTOR_PICKER (priv->msg_format));
386 state->play_sound = priv->initial_state.play_sound;
388 state->play_sound = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->play_sound));
389 id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->msg_format));
391 state->prefer_formatted_text = (*id == MODEST_FILE_FORMAT_FORMATTED_TEXT) ? TRUE : FALSE;
395 _modest_global_settings_dialog_save_conf (ModestGlobalSettingsDialog *self)
398 ModestGlobalSettingsState current_state;
399 GError *error = NULL;
400 ModestGlobalSettingsDialogPrivate *priv;
402 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
404 conf = modest_runtime_get_conf ();
406 get_current_settings (priv, ¤t_state);
408 /* Save configuration */
409 modest_conf_set_bool (conf, MODEST_CONF_AUTO_UPDATE, current_state.auto_update, &error);
410 RETURN_FALSE_ON_ERROR(error);
411 modest_conf_set_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, current_state.connect_via, NULL);
412 RETURN_FALSE_ON_ERROR(error);
413 modest_conf_set_int (conf, MODEST_CONF_UPDATE_INTERVAL, current_state.update_interval, NULL);
414 RETURN_FALSE_ON_ERROR(error);
415 modest_conf_set_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, current_state.size_limit, NULL);
416 RETURN_FALSE_ON_ERROR(error);
417 modest_conf_set_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, current_state.play_sound, NULL);
418 RETURN_FALSE_ON_ERROR(error);
419 modest_conf_set_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, current_state.prefer_formatted_text, NULL);
420 RETURN_FALSE_ON_ERROR(error);
423 if (priv->initial_state.auto_update != current_state.auto_update ||
424 priv->initial_state.connect_via != current_state.connect_via ||
425 priv->initial_state.update_interval != current_state.update_interval) {
427 TnyAccountStore *account_store;
430 if (!current_state.auto_update) {
431 modest_platform_set_update_interval (0);
432 /* To avoid a new indentation level */
436 account_store = TNY_ACCOUNT_STORE (modest_runtime_get_account_store ());
437 device = tny_account_store_get_device (account_store);
439 if (tny_device_is_online (device)) {
440 /* If connected via any then set update interval */
441 if (current_state.connect_via == MODEST_CONNECTED_VIA_ANY) {
442 modest_platform_set_update_interval (current_state.update_interval);
444 /* Set update interval only if we
445 selected the same connect_via
446 method than the one already used by
448 ModestConnectedVia connect_via =
449 MODEST_GLOBAL_SETTINGS_DIALOG_GET_CLASS(self)->current_connection_func ();
451 if (current_state.connect_via == connect_via)
452 modest_platform_set_update_interval (current_state.update_interval);
454 modest_platform_set_update_interval (0);
457 /* Disable autoupdate in offline mode */
458 modest_platform_set_update_interval (0);
460 g_object_unref (device);
468 settings_changed (ModestGlobalSettingsState initial_state,
469 ModestGlobalSettingsState current_state)
471 if (initial_state.auto_update != current_state.auto_update ||
472 initial_state.connect_via != current_state.connect_via ||
473 initial_state.update_interval != current_state.update_interval ||
474 initial_state.size_limit != current_state.size_limit ||
475 initial_state.play_sound != current_state.play_sound ||
476 initial_state.prefer_formatted_text != current_state.prefer_formatted_text)
483 on_response (GtkDialog *dialog,
487 ModestGlobalSettingsDialogPrivate *priv;
488 ModestGlobalSettingsState current_state;
491 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (user_data);
493 get_current_settings (priv, ¤t_state);
494 changed = settings_changed (priv->initial_state, current_state);
496 if (arg1 == GTK_RESPONSE_OK) {
500 saved = _modest_global_settings_dialog_save_conf (MODEST_GLOBAL_SETTINGS_DIALOG (dialog));
502 modest_platform_information_banner (NULL, NULL,
503 _("mcen_ib_advsetup_settings_saved"));
505 modest_platform_information_banner (NULL, NULL,
506 _("mail_ib_setting_failed"));
512 response = modest_platform_run_confirmation_dialog (GTK_WINDOW (user_data),
513 _("imum_nc_wizard_confirm_lose_changes"));
514 /* Do not close if the user Cancels */
515 if (response == GTK_RESPONSE_CANCEL)
516 g_signal_stop_emission_by_name (user_data, "response");
521 static ModestConnectedVia
522 current_connection_default (void)
524 g_warning ("You must implement %s", __FUNCTION__);
525 g_return_val_if_reached (MODEST_CONNECTED_VIA_ANY);