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>
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"
45 #include "widgets/modest-combo-box.h"
47 #ifndef MODEST_TOOLKIT_GTK
48 #if MODEST_HILDON_API == 0
49 #include <hildon-widgets/hildon-number-editor.h>
51 #include <hildon/hildon-number-editor.h>
52 #endif /*MODEST_HILDON_API = 0*/
54 /* include other impl specific header files */
56 #define RETURN_FALSE_ON_ERROR(error) if (error) { g_clear_error (&error); return FALSE; }
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);
63 static void on_response (GtkDialog *dialog,
66 static gboolean on_delete_event (GtkWidget *widget,
70 static void get_current_settings (ModestGlobalSettingsDialogPrivate *priv,
71 ModestGlobalSettingsState *state);
73 static ModestConnectedVia current_connection_default (void);
75 static gboolean modest_global_settings_dialog_save_settings_default (ModestGlobalSettingsDialog *self);
85 static GtkDialogClass *parent_class = NULL;
87 /* uncomment the following if you have defined any signals */
88 /* static guint signals[LAST_SIGNAL] = {0}; */
91 modest_global_settings_dialog_get_type (void)
93 static GType my_type = 0;
95 static const GTypeInfo my_info = {
96 sizeof(ModestGlobalSettingsDialogClass),
98 NULL, /* base finalize */
99 (GClassInitFunc) modest_global_settings_dialog_class_init,
100 NULL, /* class finalize */
101 NULL, /* class data */
102 sizeof(ModestGlobalSettingsDialog),
104 (GInstanceInitFunc) modest_global_settings_dialog_init,
107 #ifdef MODEST_TOOLKIT_HILDON2
108 my_type = g_type_register_static (HILDON_TYPE_DIALOG,
109 "ModestGlobalSettingsDialog",
112 my_type = g_type_register_static (GTK_TYPE_DIALOG,
113 "ModestGlobalSettingsDialog",
121 modest_global_settings_dialog_class_init (ModestGlobalSettingsDialogClass *klass)
123 GObjectClass *gobject_class;
124 gobject_class = (GObjectClass*) klass;
126 parent_class = g_type_class_peek_parent (klass);
127 gobject_class->finalize = modest_global_settings_dialog_finalize;
129 g_type_class_add_private (gobject_class, sizeof(ModestGlobalSettingsDialogPrivate));
131 klass->current_connection_func = current_connection_default;
132 klass->save_settings_func = modest_global_settings_dialog_save_settings_default;
136 modest_global_settings_dialog_init (ModestGlobalSettingsDialog *self)
138 ModestGlobalSettingsDialogPrivate *priv;
140 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
142 priv->notebook = gtk_notebook_new ();
143 priv->default_account_selector = NULL;
144 priv->accounts_list = NULL;
146 /* Connect to the dialog's "response" and "delete-event" signals */
147 g_signal_connect (G_OBJECT (self), "response", G_CALLBACK (on_response), self);
148 g_signal_connect (G_OBJECT (self), "delete-event", G_CALLBACK (on_delete_event), self);
151 gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_options"));
155 modest_global_settings_dialog_finalize (GObject *obj)
157 ModestGlobalSettingsDialogPrivate *priv =
158 MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (obj);
160 /* These had to stay alive as long as the comboboxes that used them: */
161 modest_pair_list_free (priv->connect_via_list);
162 modest_pair_list_free (priv->update_interval_list);
163 modest_pair_list_free (priv->msg_format_list);
164 modest_pair_list_free (priv->accounts_list);
166 G_OBJECT_CLASS(parent_class)->finalize (obj);
170 * Creates a pair list (number,string) and adds it to the given list
173 add_to_modest_pair_list (const gint num, const gchar *str, GSList **list)
178 number = g_malloc0 (sizeof (gint));
180 pair = modest_pair_new (number, g_strdup (str), FALSE);
181 *list = g_slist_prepend (*list, pair);
185 _modest_global_settings_dialog_get_connected_via (void)
188 const gchar *message;
190 #ifndef MODEST_TOOLKIT_GTK
191 const gchar *env_var = getenv ("OSSO_PRODUCT_HARDWARE");
192 /* Check if WIMAX is available */
193 if (env_var && !strncmp (env_var, "RX-48", 5))
194 message = _("mcen_va_options_connectiontype_wlan_wimax");
196 message = _("mcen_va_options_connectiontype_wlan");
198 message = _("mcen_va_options_connectiontype_wlan");
200 add_to_modest_pair_list (MODEST_CONNECTED_VIA_WLAN_OR_WIMAX, message, &list);
201 add_to_modest_pair_list (MODEST_CONNECTED_VIA_ANY,
202 _("mcen_va_options_connectiontype_all"),
205 return (ModestPairList *) g_slist_reverse (list);
209 * Gets a list of pairs of update intervals
212 _modest_global_settings_dialog_get_update_interval (void)
216 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_5_MIN,
217 _("mcen_va_options_updateinterval_5min"),
219 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_10_MIN,
220 _("mcen_va_options_updateinterval_10min"),
222 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_15_MIN,
223 _("mcen_va_options_updateinterval_15min"),
225 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_30_MIN,
226 _("mcen_va_options_updateinterval_30min"),
228 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_1_HOUR,
229 _("mcen_va_options_updateinterval_1h"),
231 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_2_HOUR,
232 _("mcen_va_options_updateinterval_2h"),
235 return (ModestPairList *) g_slist_reverse (list);
239 * Gets a list of pairs
242 _modest_global_settings_dialog_get_msg_formats (void)
246 add_to_modest_pair_list (MODEST_FILE_FORMAT_FORMATTED_TEXT,
247 _("mcen_va_options_messageformat_html"),
249 add_to_modest_pair_list (MODEST_FILE_FORMAT_PLAIN_TEXT,
250 _("mcen_va_options_messageformat_plain"),
253 return (ModestPairList *) g_slist_reverse (list);
257 get_current_settings (ModestGlobalSettingsDialogPrivate *priv,
258 ModestGlobalSettingsState *state)
262 /* Get values from UI */
263 #ifdef MODEST_TOOLKIT_HILDON2
264 id = modest_selector_picker_get_active_id (MODEST_SELECTOR_PICKER (priv->connect_via));
265 state->auto_update = hildon_check_button_get_active (HILDON_CHECK_BUTTON (priv->auto_update));
266 state->default_account = modest_selector_picker_get_active_id (MODEST_SELECTOR_PICKER (priv->default_account_selector));
268 id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->connect_via));
269 state->auto_update = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->auto_update));
270 state->default_account = NULL;
272 state->connect_via = *id;
273 #ifndef MODEST_TOOLKIT_GTK
274 #ifndef MODEST_TOOLKIT_HILDON2
275 state->size_limit = hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (priv->size_limit));
278 state->size_limit = gtk_spin_button_get_value (GTK_SPIN_BUTTON (priv->size_limit));
281 #ifdef MODEST_TOOLKIT_HILDON2
282 id = modest_selector_picker_get_active_id (MODEST_SELECTOR_PICKER (priv->update_interval));
284 id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->update_interval));
286 state->update_interval = *id;
287 #ifdef MODEST_TOOLKIT_HILDON2
288 id = modest_selector_picker_get_active_id (MODEST_SELECTOR_PICKER (priv->msg_format));
289 state->play_sound = priv->initial_state.play_sound;
291 state->play_sound = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->play_sound));
292 #ifndef MODEST_TOOLKIT_GTK
293 id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->msg_format));
296 #ifdef MODEST_TOOLKIT_GTK
297 state->prefer_formatted_text = FALSE;
299 state->prefer_formatted_text = (*id == MODEST_FILE_FORMAT_FORMATTED_TEXT) ? TRUE : FALSE;
304 modest_global_settings_dialog_save_settings_default (ModestGlobalSettingsDialog *self)
307 ModestGlobalSettingsState current_state = {0,};
308 GError *error = NULL;
309 ModestGlobalSettingsDialogPrivate *priv;
311 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
313 conf = modest_runtime_get_conf ();
315 get_current_settings (priv, ¤t_state);
317 /* Save configuration */
318 modest_conf_set_bool (conf, MODEST_CONF_AUTO_UPDATE, current_state.auto_update, &error);
319 RETURN_FALSE_ON_ERROR(error);
320 modest_conf_set_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, current_state.connect_via, NULL);
321 RETURN_FALSE_ON_ERROR(error);
322 modest_conf_set_int (conf, MODEST_CONF_UPDATE_INTERVAL, current_state.update_interval, NULL);
323 RETURN_FALSE_ON_ERROR(error);
324 modest_conf_set_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, current_state.size_limit, NULL);
325 RETURN_FALSE_ON_ERROR(error);
326 modest_conf_set_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, current_state.play_sound, NULL);
327 RETURN_FALSE_ON_ERROR(error);
328 modest_conf_set_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, current_state.prefer_formatted_text, NULL);
329 RETURN_FALSE_ON_ERROR(error);
330 if (current_state.default_account &&
331 (!priv->initial_state.default_account ||
332 strcmp (current_state.default_account, priv->initial_state.default_account)!= 0)) {
333 modest_account_mgr_set_default_account (modest_runtime_get_account_mgr (),
334 current_state.default_account);
338 if (priv->initial_state.auto_update != current_state.auto_update ||
339 priv->initial_state.connect_via != current_state.connect_via ||
340 priv->initial_state.update_interval != current_state.update_interval) {
342 TnyAccountStore *account_store;
345 if (!current_state.auto_update) {
346 modest_platform_set_update_interval (0);
347 /* To avoid a new indentation level */
351 account_store = TNY_ACCOUNT_STORE (modest_runtime_get_account_store ());
352 device = tny_account_store_get_device (account_store);
354 if (tny_device_is_online (device)) {
355 /* If connected via any then set update interval */
356 if (current_state.connect_via == MODEST_CONNECTED_VIA_ANY) {
357 modest_platform_set_update_interval (current_state.update_interval);
359 /* Set update interval only if we
360 selected the same connect_via
361 method than the one already used by
363 ModestConnectedVia connect_via =
364 MODEST_GLOBAL_SETTINGS_DIALOG_GET_CLASS(self)->current_connection_func ();
366 if (current_state.connect_via == connect_via)
367 modest_platform_set_update_interval (current_state.update_interval);
369 modest_platform_set_update_interval (0);
372 /* Disable autoupdate in offline mode */
373 modest_platform_set_update_interval (0);
375 g_object_unref (device);
383 settings_changed (ModestGlobalSettingsState initial_state,
384 ModestGlobalSettingsState current_state)
386 if (initial_state.auto_update != current_state.auto_update ||
387 initial_state.connect_via != current_state.connect_via ||
388 initial_state.update_interval != current_state.update_interval ||
389 initial_state.size_limit != current_state.size_limit ||
390 initial_state.play_sound != current_state.play_sound ||
391 initial_state.prefer_formatted_text != current_state.prefer_formatted_text ||
392 (current_state.default_account &&
393 (!initial_state.default_account ||
394 strcmp (current_state.default_account, initial_state.default_account)!= 0)))
401 on_delete_event (GtkWidget *widget,
405 ModestGlobalSettingsDialogPrivate *priv;
406 ModestGlobalSettingsState current_state;
408 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (user_data);
410 /* If settings changed, them the response method already asked
411 the user, because it's always executed before (see
412 GtkDialog code). If it's not then simply close */
413 get_current_settings (priv, ¤t_state);
414 return settings_changed (priv->initial_state, current_state);
418 on_response (GtkDialog *dialog,
422 ModestGlobalSettingsDialogPrivate *priv;
423 ModestGlobalSettingsState current_state = {0,};
424 gboolean changed = FALSE;
426 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (user_data);
428 get_current_settings (priv, ¤t_state);
429 changed = settings_changed (priv->initial_state, current_state);
431 if (arg1 == GTK_RESPONSE_OK) {
435 saved = modest_global_settings_dialog_save_settings (MODEST_GLOBAL_SETTINGS_DIALOG (dialog));
437 modest_platform_information_banner (NULL, NULL,
438 _("mcen_ib_advsetup_settings_saved"));
440 modest_platform_information_banner (NULL, NULL,
441 _("mail_ib_setting_failed"));
447 response = modest_platform_run_confirmation_dialog (GTK_WINDOW (user_data),
448 _("imum_nc_wizard_confirm_lose_changes"));
449 /* Do not close if the user Cancels */
450 if (response != GTK_RESPONSE_OK)
451 g_signal_stop_emission_by_name (user_data, "response");
456 static ModestConnectedVia
457 current_connection_default (void)
459 g_warning ("You must implement %s", __FUNCTION__);
460 g_return_val_if_reached (MODEST_CONNECTED_VIA_ANY);
464 modest_global_settings_dialog_save_settings (ModestGlobalSettingsDialog *self)
466 g_return_val_if_fail (MODEST_IS_GLOBAL_SETTINGS_DIALOG (self), FALSE);
468 return MODEST_GLOBAL_SETTINGS_DIALOG_GET_CLASS(self)->save_settings_func (self);