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 /* Connect to the dialog's response signal: */
130 g_signal_connect (G_OBJECT (self), "response", G_CALLBACK (on_response), self);
133 gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_options"));
137 modest_global_settings_dialog_finalize (GObject *obj)
139 ModestGlobalSettingsDialogPrivate *priv =
140 MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (obj);
142 /* These had to stay alive as long as the comboboxes that used them: */
143 modest_pair_list_free (priv->connect_via_list);
144 modest_pair_list_free (priv->update_interval_list);
145 modest_pair_list_free (priv->msg_format_list);
147 G_OBJECT_CLASS(parent_class)->finalize (obj);
151 * Creates a pair list (number,string) and adds it to the given list
154 add_to_modest_pair_list (const gint num, const gchar *str, GSList **list)
159 number = g_malloc0 (sizeof (gint));
161 pair = modest_pair_new (number, g_strdup (str), FALSE);
162 *list = g_slist_prepend (*list, pair);
166 _modest_global_settings_dialog_get_connected_via (void)
169 const gchar *message;
171 #ifdef MODEST_PLATFORM_MAEMO
172 const gchar *env_var = getenv ("OSSO_PRODUCT_HARDWARE");
173 /* Check if WIMAX is available */
174 if (env_var && !strncmp (env_var, "RX-48", 5))
175 message = _("mcen_va_options_connectiontype_wlan_wimax");
177 message = _("mcen_va_options_connectiontype_wlan");
179 message = _("mcen_va_options_connectiontype_wlan");
181 add_to_modest_pair_list (MODEST_CONNECTED_VIA_WLAN_OR_WIMAX, message, &list);
182 add_to_modest_pair_list (MODEST_CONNECTED_VIA_ANY,
183 _("mcen_va_options_connectiontype_all"),
186 return (ModestPairList *) g_slist_reverse (list);
190 * Gets a list of pairs of update intervals
193 _modest_global_settings_dialog_get_update_interval (void)
197 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_5_MIN,
198 _("mcen_va_options_updateinterval_5min"),
200 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_10_MIN,
201 _("mcen_va_options_updateinterval_10min"),
203 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_15_MIN,
204 _("mcen_va_options_updateinterval_15min"),
206 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_30_MIN,
207 _("mcen_va_options_updateinterval_30min"),
209 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_1_HOUR,
210 _("mcen_va_options_updateinterval_1h"),
212 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_2_HOUR,
213 _("mcen_va_options_updateinterval_2h"),
216 return (ModestPairList *) g_slist_reverse (list);
220 * Gets a list of pairs
223 _modest_global_settings_dialog_get_msg_formats (void)
227 add_to_modest_pair_list (MODEST_FILE_FORMAT_FORMATTED_TEXT,
228 _("mcen_va_options_messageformat_html"),
230 add_to_modest_pair_list (MODEST_FILE_FORMAT_PLAIN_TEXT,
231 _("mcen_va_options_messageformat_plain"),
234 return (ModestPairList *) g_slist_reverse (list);
238 _modest_global_settings_dialog_load_conf (ModestGlobalSettingsDialog *self)
242 gint combo_id, value;
243 GError *error = NULL;
244 ModestGlobalSettingsDialogPrivate *priv;
246 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
247 conf = modest_runtime_get_conf ();
250 checked = modest_conf_get_bool (conf, MODEST_CONF_AUTO_UPDATE, &error);
252 g_clear_error (&error);
256 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->auto_update), checked);
257 priv->initial_state.auto_update = checked;
260 combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, &error);
262 g_error_free (error);
264 combo_id = MODEST_CONNECTED_VIA_WLAN_OR_WIMAX;
266 modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->connect_via),
267 (gpointer) &combo_id);
268 priv->initial_state.connect_via = combo_id;
270 /* Emit toggled to update the visibility of connect_by caption */
271 gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (priv->auto_update));
273 /* Update interval */
274 combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_INTERVAL, &error);
276 g_error_free (error);
278 combo_id = MODEST_UPDATE_INTERVAL_15_MIN;
280 modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->update_interval),
281 (gpointer) &combo_id);
282 priv->initial_state.update_interval = combo_id;
285 value = modest_conf_get_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, &error);
287 g_error_free (error);
291 /* It's better to do this in the subclasses, but it's just one
292 line, so we'll leave it here for the moment */
293 #ifdef MODEST_PLATFORM_MAEMO
294 hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (priv->size_limit), value);
296 gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_limit), value);
298 priv->initial_state.size_limit = value;
301 checked = modest_conf_get_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, &error);
303 g_error_free (error);
307 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->play_sound), checked);
308 priv->initial_state.play_sound = checked;
311 checked = modest_conf_get_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, &error);
313 g_error_free (error);
315 combo_id = MODEST_FILE_FORMAT_FORMATTED_TEXT;
317 combo_id = (checked) ? MODEST_FILE_FORMAT_FORMATTED_TEXT : MODEST_FILE_FORMAT_PLAIN_TEXT;
318 modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->msg_format),
319 (gpointer) &combo_id);
320 priv->initial_state.prefer_formatted_text = checked;
324 get_current_settings (ModestGlobalSettingsDialogPrivate *priv,
325 ModestGlobalSettingsState *state)
329 /* Get values from UI */
330 state->auto_update = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->auto_update));
331 id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->connect_via));
332 state->connect_via = *id;
333 #ifdef MODEST_PLATFORM_MAEMO
334 state->size_limit = hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (priv->size_limit));
336 state->size_limit = gtk_spin_button_get_value (GTK_SPIN_BUTTON (priv->size_limit));
338 id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->update_interval));
339 state->update_interval = *id;
340 state->play_sound = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->play_sound));
341 id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->msg_format));
342 state->prefer_formatted_text = (*id == MODEST_FILE_FORMAT_FORMATTED_TEXT) ? TRUE : FALSE;
346 _modest_global_settings_dialog_save_conf (ModestGlobalSettingsDialog *self)
349 ModestGlobalSettingsState current_state;
350 GError *error = NULL;
351 ModestGlobalSettingsDialogPrivate *priv;
353 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
355 conf = modest_runtime_get_conf ();
357 get_current_settings (priv, ¤t_state);
359 /* Save configuration */
360 modest_conf_set_bool (conf, MODEST_CONF_AUTO_UPDATE, current_state.auto_update, &error);
361 RETURN_FALSE_ON_ERROR(error);
362 modest_conf_set_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, current_state.connect_via, NULL);
363 RETURN_FALSE_ON_ERROR(error);
364 modest_conf_set_int (conf, MODEST_CONF_UPDATE_INTERVAL, current_state.update_interval, NULL);
365 RETURN_FALSE_ON_ERROR(error);
366 modest_conf_set_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, current_state.size_limit, NULL);
367 RETURN_FALSE_ON_ERROR(error);
368 modest_conf_set_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, current_state.play_sound, NULL);
369 RETURN_FALSE_ON_ERROR(error);
370 modest_conf_set_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, current_state.prefer_formatted_text, NULL);
371 RETURN_FALSE_ON_ERROR(error);
374 if (priv->initial_state.auto_update != current_state.auto_update ||
375 priv->initial_state.connect_via != current_state.connect_via ||
376 priv->initial_state.update_interval != current_state.update_interval) {
378 TnyAccountStore *account_store;
381 if (!current_state.auto_update) {
382 modest_platform_set_update_interval (0);
383 /* To avoid a new indentation level */
387 account_store = TNY_ACCOUNT_STORE (modest_runtime_get_account_store ());
388 device = tny_account_store_get_device (account_store);
390 if (tny_device_is_online (device)) {
391 /* If connected via any then set update interval */
392 if (current_state.connect_via == MODEST_CONNECTED_VIA_ANY) {
393 modest_platform_set_update_interval (current_state.update_interval);
395 /* Set update interval only if we
396 selected the same connect_via
397 method than the one already used by
399 ModestConnectedVia connect_via =
400 MODEST_GLOBAL_SETTINGS_DIALOG_GET_CLASS(self)->current_connection_func ();
402 if (current_state.connect_via == connect_via)
403 modest_platform_set_update_interval (current_state.update_interval);
405 modest_platform_set_update_interval (0);
408 /* Disable autoupdate in offline mode */
409 modest_platform_set_update_interval (0);
411 g_object_unref (device);
419 settings_changed (ModestGlobalSettingsState initial_state,
420 ModestGlobalSettingsState current_state)
422 if (initial_state.auto_update != current_state.auto_update ||
423 initial_state.connect_via != current_state.connect_via ||
424 initial_state.update_interval != current_state.update_interval ||
425 initial_state.size_limit != current_state.size_limit ||
426 initial_state.play_sound != current_state.play_sound ||
427 initial_state.prefer_formatted_text != current_state.prefer_formatted_text)
434 on_response (GtkDialog *dialog,
438 ModestGlobalSettingsDialogPrivate *priv;
439 ModestGlobalSettingsState current_state;
442 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (user_data);
444 get_current_settings (priv, ¤t_state);
445 changed = settings_changed (priv->initial_state, current_state);
447 if (arg1 == GTK_RESPONSE_OK) {
451 saved = _modest_global_settings_dialog_save_conf (MODEST_GLOBAL_SETTINGS_DIALOG (dialog));
453 modest_platform_information_banner (NULL, NULL,
454 _("mcen_ib_advsetup_settings_saved"));
456 modest_platform_information_banner (NULL, NULL,
457 _("mail_ib_setting_failed"));
463 response = modest_platform_run_confirmation_dialog (GTK_WINDOW (user_data),
464 _("imum_nc_wizard_confirm_lose_changes"));
465 /* Do not close if the user Cancels */
466 if (response == GTK_RESPONSE_CANCEL)
467 g_signal_stop_emission_by_name (dialog, "response");
472 static ModestConnectedVia
473 current_connection_default (void)
475 g_warning ("You must implement %s", __FUNCTION__);
476 g_return_val_if_reached (MODEST_CONNECTED_VIA_ANY);