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 #include "widgets/modest-combo-box.h"
45 #ifdef MODEST_PLATFORM_MAEMO
46 #ifdef MODEST_HAVE_HILDON0_WIDGETS
47 #include <hildon-widgets/hildon-number-editor.h>
49 #include <hildon/hildon-number-editor.h>
50 #endif /*MODEST_HAVE_HILDON0_WIDGETS*/
52 /* include other impl specific header files */
54 #define RETURN_FALSE_ON_ERROR(error) if (error) { g_clear_error (&error); return FALSE; }
56 /* 'private'/'protected' functions */
57 static void modest_global_settings_dialog_class_init (ModestGlobalSettingsDialogClass *klass);
58 static void modest_global_settings_dialog_init (ModestGlobalSettingsDialog *obj);
59 static void modest_global_settings_dialog_finalize (GObject *obj);
61 static void on_response (GtkDialog *dialog,
64 static void get_current_settings (ModestGlobalSettingsDialogPrivate *priv,
65 ModestGlobalSettingsState *state);
67 static ModestConnectedVia current_connection_default (void);
77 static GtkDialogClass *parent_class = NULL;
79 /* uncomment the following if you have defined any signals */
80 /* static guint signals[LAST_SIGNAL] = {0}; */
83 modest_global_settings_dialog_get_type (void)
85 static GType my_type = 0;
87 static const GTypeInfo my_info = {
88 sizeof(ModestGlobalSettingsDialogClass),
90 NULL, /* base finalize */
91 (GClassInitFunc) modest_global_settings_dialog_class_init,
92 NULL, /* class finalize */
93 NULL, /* class data */
94 sizeof(ModestGlobalSettingsDialog),
96 (GInstanceInitFunc) modest_global_settings_dialog_init,
99 my_type = g_type_register_static (GTK_TYPE_DIALOG,
100 "ModestGlobalSettingsDialog",
107 modest_global_settings_dialog_class_init (ModestGlobalSettingsDialogClass *klass)
109 GObjectClass *gobject_class;
110 gobject_class = (GObjectClass*) klass;
112 parent_class = g_type_class_peek_parent (klass);
113 gobject_class->finalize = modest_global_settings_dialog_finalize;
115 g_type_class_add_private (gobject_class, sizeof(ModestGlobalSettingsDialogPrivate));
117 klass->current_connection_func = current_connection_default;
121 modest_global_settings_dialog_init (ModestGlobalSettingsDialog *self)
123 ModestGlobalSettingsDialogPrivate *priv;
125 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
127 priv->notebook = gtk_notebook_new ();
129 /* Add the buttons: */
130 gtk_dialog_add_button (GTK_DIALOG (self), _("mcen_bd_dialog_ok"), GTK_RESPONSE_OK);
131 gtk_dialog_add_button (GTK_DIALOG (self), _("mcen_bd_dialog_cancel"), GTK_RESPONSE_CANCEL);
133 /* Connect to the dialog's response signal: */
134 g_signal_connect (G_OBJECT (self), "response", G_CALLBACK (on_response), self);
137 gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_options"));
141 modest_global_settings_dialog_finalize (GObject *obj)
143 ModestGlobalSettingsDialogPrivate *priv =
144 MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (obj);
146 /* These had to stay alive as long as the comboboxes that used them: */
147 modest_pair_list_free (priv->connect_via_list);
148 modest_pair_list_free (priv->update_interval_list);
149 modest_pair_list_free (priv->msg_format_list);
151 G_OBJECT_CLASS(parent_class)->finalize (obj);
155 * Creates a pair list (number,string) and adds it to the given list
158 add_to_modest_pair_list (const gint num, const gchar *str, GSList **list)
163 number = g_malloc0 (sizeof (gint));
165 pair = modest_pair_new (number, g_strdup (str), FALSE);
166 *list = g_slist_prepend (*list, pair);
170 _modest_global_settings_dialog_get_connected_via (void)
173 const gchar *message;
175 #ifdef MODEST_PLATFORM_MAEMO
176 const gchar *env_var = getenv ("OSSO_PRODUCT_HARDWARE");
177 /* Check if WIMAX is available */
178 if (env_var && !strncmp (env_var, "RX-48", 5))
179 message = _("mcen_va_options_connectiontype_wlan_wimax");
181 message = _("mcen_va_options_connectiontype_wlan");
183 message = _("mcen_va_options_connectiontype_wlan");
185 add_to_modest_pair_list (MODEST_CONNECTED_VIA_WLAN_OR_WIMAX, message, &list);
186 add_to_modest_pair_list (MODEST_CONNECTED_VIA_ANY,
187 _("mcen_va_options_connectiontype_all"),
190 return (ModestPairList *) g_slist_reverse (list);
194 * Gets a list of pairs of update intervals
197 _modest_global_settings_dialog_get_update_interval (void)
201 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_5_MIN,
202 _("mcen_va_options_updateinterval_5min"),
204 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_10_MIN,
205 _("mcen_va_options_updateinterval_10min"),
207 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_15_MIN,
208 _("mcen_va_options_updateinterval_15min"),
210 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_30_MIN,
211 _("mcen_va_options_updateinterval_30min"),
213 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_1_HOUR,
214 _("mcen_va_options_updateinterval_1h"),
216 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_2_HOUR,
217 _("mcen_va_options_updateinterval_2h"),
220 return (ModestPairList *) g_slist_reverse (list);
224 * Gets a list of pairs
227 _modest_global_settings_dialog_get_msg_formats (void)
231 add_to_modest_pair_list (MODEST_FILE_FORMAT_FORMATTED_TEXT,
232 _("mcen_va_options_messageformat_html"),
234 add_to_modest_pair_list (MODEST_FILE_FORMAT_PLAIN_TEXT,
235 _("mcen_va_options_messageformat_plain"),
238 return (ModestPairList *) g_slist_reverse (list);
242 _modest_global_settings_dialog_load_conf (ModestGlobalSettingsDialog *self)
246 gint combo_id, value;
247 GError *error = NULL;
248 ModestGlobalSettingsDialogPrivate *priv;
250 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
251 conf = modest_runtime_get_conf ();
254 checked = modest_conf_get_bool (conf, MODEST_CONF_AUTO_UPDATE, &error);
256 g_clear_error (&error);
260 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->auto_update), checked);
261 priv->initial_state.auto_update = checked;
264 combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, &error);
266 g_error_free (error);
268 combo_id = MODEST_CONNECTED_VIA_WLAN_OR_WIMAX;
270 modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->connect_via),
271 (gpointer) &combo_id);
272 priv->initial_state.connect_via = combo_id;
274 /* Emit toggled to update the visibility of connect_by caption */
275 gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (priv->auto_update));
277 /* Update interval */
278 combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_INTERVAL, &error);
280 g_error_free (error);
282 combo_id = MODEST_UPDATE_INTERVAL_15_MIN;
284 modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->update_interval),
285 (gpointer) &combo_id);
286 priv->initial_state.update_interval = combo_id;
289 value = modest_conf_get_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, &error);
291 g_error_free (error);
295 /* It's better to do this in the subclasses, but it's just one
296 line, so we'll leave it here for the moment */
297 #ifdef MODEST_PLATFORM_MAEMO
298 hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (priv->size_limit), value);
300 gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_limit), value);
302 priv->initial_state.size_limit = value;
305 checked = modest_conf_get_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, &error);
307 g_error_free (error);
311 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->play_sound), checked);
312 priv->initial_state.play_sound = checked;
315 checked = modest_conf_get_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, &error);
317 g_error_free (error);
319 combo_id = MODEST_FILE_FORMAT_FORMATTED_TEXT;
321 combo_id = (checked) ? MODEST_FILE_FORMAT_FORMATTED_TEXT : MODEST_FILE_FORMAT_PLAIN_TEXT;
322 modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->msg_format),
323 (gpointer) &combo_id);
324 priv->initial_state.prefer_formatted_text = checked;
328 get_current_settings (ModestGlobalSettingsDialogPrivate *priv,
329 ModestGlobalSettingsState *state)
333 /* Get values from UI */
334 state->auto_update = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->auto_update));
335 id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->connect_via));
336 state->connect_via = *id;
337 #ifdef MODEST_PLATFORM_MAEMO
338 state->size_limit = hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (priv->size_limit));
340 state->size_limit = gtk_spin_button_get_value (GTK_SPIN_BUTTON (priv->size_limit));
342 id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->update_interval));
343 state->update_interval = *id;
344 state->play_sound = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->play_sound));
345 id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->msg_format));
346 state->prefer_formatted_text = (*id == MODEST_FILE_FORMAT_FORMATTED_TEXT) ? TRUE : FALSE;
350 _modest_global_settings_dialog_save_conf (ModestGlobalSettingsDialog *self)
353 ModestGlobalSettingsState current_state;
354 GError *error = NULL;
355 ModestGlobalSettingsDialogPrivate *priv;
357 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
359 conf = modest_runtime_get_conf ();
361 get_current_settings (priv, ¤t_state);
363 /* Save configuration */
364 modest_conf_set_bool (conf, MODEST_CONF_AUTO_UPDATE, current_state.auto_update, &error);
365 RETURN_FALSE_ON_ERROR(error);
366 modest_conf_set_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, current_state.connect_via, NULL);
367 RETURN_FALSE_ON_ERROR(error);
368 modest_conf_set_int (conf, MODEST_CONF_UPDATE_INTERVAL, current_state.update_interval, NULL);
369 RETURN_FALSE_ON_ERROR(error);
370 modest_conf_set_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, current_state.size_limit, NULL);
371 RETURN_FALSE_ON_ERROR(error);
372 modest_conf_set_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, current_state.play_sound, NULL);
373 RETURN_FALSE_ON_ERROR(error);
374 modest_conf_set_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, current_state.prefer_formatted_text, NULL);
375 RETURN_FALSE_ON_ERROR(error);
378 if (priv->initial_state.auto_update != current_state.auto_update ||
379 priv->initial_state.connect_via != current_state.connect_via ||
380 priv->initial_state.update_interval != current_state.update_interval) {
382 TnyAccountStore *account_store;
385 if (!current_state.auto_update) {
386 modest_platform_set_update_interval (0);
387 /* To avoid a new indentation level */
391 account_store = TNY_ACCOUNT_STORE (modest_runtime_get_account_store ());
392 device = tny_account_store_get_device (account_store);
394 if (tny_device_is_online (device)) {
395 /* If connected via any then set update interval */
396 if (current_state.connect_via == MODEST_CONNECTED_VIA_ANY) {
397 modest_platform_set_update_interval (current_state.update_interval);
399 /* Set update interval only if we
400 selected the same connect_via
401 method than the one already used by
403 ModestConnectedVia connect_via =
404 MODEST_GLOBAL_SETTINGS_DIALOG_GET_CLASS(self)->current_connection_func ();
406 if (current_state.connect_via == connect_via)
407 modest_platform_set_update_interval (current_state.update_interval);
409 modest_platform_set_update_interval (0);
412 /* Disable autoupdate in offline mode */
413 modest_platform_set_update_interval (0);
415 g_object_unref (device);
423 settings_changed (ModestGlobalSettingsState initial_state,
424 ModestGlobalSettingsState current_state)
426 if (initial_state.auto_update != current_state.auto_update ||
427 initial_state.connect_via != current_state.connect_via ||
428 initial_state.update_interval != current_state.update_interval ||
429 initial_state.size_limit != current_state.size_limit ||
430 initial_state.play_sound != current_state.play_sound ||
431 initial_state.prefer_formatted_text != current_state.prefer_formatted_text)
438 on_response (GtkDialog *dialog,
442 ModestGlobalSettingsDialogPrivate *priv;
443 ModestGlobalSettingsState current_state;
446 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (user_data);
448 get_current_settings (priv, ¤t_state);
449 changed = settings_changed (priv->initial_state, current_state);
451 if (arg1 == GTK_RESPONSE_OK) {
455 saved = _modest_global_settings_dialog_save_conf (MODEST_GLOBAL_SETTINGS_DIALOG (dialog));
457 modest_platform_information_banner (NULL, NULL,
458 _("mcen_ib_advsetup_settings_saved"));
460 modest_platform_information_banner (NULL, NULL,
461 _("mail_ib_setting_failed"));
467 response = modest_platform_run_confirmation_dialog (GTK_WINDOW (user_data),
468 _("imum_nc_wizard_confirm_lose_changes"));
469 /* Do not close if the user Cancels */
470 if (response == GTK_RESPONSE_CANCEL)
471 g_signal_stop_emission_by_name (dialog, "response");
476 static ModestConnectedVia
477 current_connection_default (void)
479 g_warning ("You must implement %s", __FUNCTION__);
480 g_return_val_if_reached (MODEST_CONNECTED_VIA_ANY);