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>
35 #include "widgets/modest-global-settings-dialog.h"
36 #include "widgets/modest-global-settings-dialog-priv.h"
37 #include "modest-defs.h"
38 #include "modest-conf.h"
39 #include "modest-runtime.h"
40 #include "modest-ui-constants.h"
41 #include "modest-tny-msg.h"
42 #include "modest-platform.h"
43 #include "widgets/modest-combo-box.h"
44 #ifdef MODEST_PLATFORM_MAEMO
45 #ifdef MODEST_HILDON_VERSION_0
46 #include <hildon-widgets/hildon-number-editor.h>
48 #include <hildon/hildon-number-editor.h>
49 #endif /*MODEST_HILDON_VERSION_0*/
51 /* include other impl specific header files */
53 #define RETURN_FALSE_ON_ERROR(error) if (error) { g_clear_error (&error); return FALSE; }
55 /* 'private'/'protected' functions */
56 static void modest_global_settings_dialog_class_init (ModestGlobalSettingsDialogClass *klass);
57 static void modest_global_settings_dialog_init (ModestGlobalSettingsDialog *obj);
58 static void modest_global_settings_dialog_finalize (GObject *obj);
60 static void on_response (GtkDialog *dialog,
63 static void get_current_settings (ModestGlobalSettingsDialogPrivate *priv,
64 ModestGlobalSettingsState *state);
66 static ModestConnectedVia current_connection_default (void);
76 static GtkDialogClass *parent_class = NULL;
78 /* uncomment the following if you have defined any signals */
79 /* static guint signals[LAST_SIGNAL] = {0}; */
82 modest_global_settings_dialog_get_type (void)
84 static GType my_type = 0;
86 static const GTypeInfo my_info = {
87 sizeof(ModestGlobalSettingsDialogClass),
89 NULL, /* base finalize */
90 (GClassInitFunc) modest_global_settings_dialog_class_init,
91 NULL, /* class finalize */
92 NULL, /* class data */
93 sizeof(ModestGlobalSettingsDialog),
95 (GInstanceInitFunc) modest_global_settings_dialog_init,
98 my_type = g_type_register_static (GTK_TYPE_DIALOG,
99 "ModestGlobalSettingsDialog",
106 modest_global_settings_dialog_class_init (ModestGlobalSettingsDialogClass *klass)
108 GObjectClass *gobject_class;
109 gobject_class = (GObjectClass*) klass;
111 parent_class = g_type_class_peek_parent (klass);
112 gobject_class->finalize = modest_global_settings_dialog_finalize;
114 g_type_class_add_private (gobject_class, sizeof(ModestGlobalSettingsDialogPrivate));
116 klass->current_connection_func = current_connection_default;
120 modest_global_settings_dialog_init (ModestGlobalSettingsDialog *self)
122 ModestGlobalSettingsDialogPrivate *priv;
124 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
126 priv->notebook = gtk_notebook_new ();
127 priv->changed = FALSE;
129 /* Add the buttons: */
130 gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_OK, GTK_RESPONSE_OK);
131 gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_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 * Gets a list of pairs
173 _modest_global_settings_dialog_get_connected_via (void)
177 add_to_modest_pair_list (MODEST_CONNECTED_VIA_WLAN,
178 _("mcen_va_options_connectiontype_wlan"),
180 add_to_modest_pair_list (MODEST_CONNECTED_VIA_ANY,
181 _("mcen_va_options_connectiontype_all"),
184 return (ModestPairList *) g_slist_reverse (list);
188 * Gets a list of pairs of update intervals
191 _modest_global_settings_dialog_get_update_interval (void)
195 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_5_MIN,
196 _("mcen_va_options_updateinterval_5min"),
198 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_10_MIN,
199 _("mcen_va_options_updateinterval_10min"),
201 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_15_MIN,
202 _("mcen_va_options_updateinterval_15min"),
204 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_30_MIN,
205 _("mcen_va_options_updateinterval_30min"),
207 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_1_HOUR,
208 _("mcen_va_options_updateinterval_1h"),
210 add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_2_HOUR,
211 _("mcen_va_options_updateinterval_2h"),
214 return (ModestPairList *) g_slist_reverse (list);
218 * Gets a list of pairs
221 _modest_global_settings_dialog_get_msg_formats (void)
225 add_to_modest_pair_list (MODEST_FILE_FORMAT_FORMATTED_TEXT,
226 _("mcen_va_options_messageformat_html"),
228 add_to_modest_pair_list (MODEST_FILE_FORMAT_PLAIN_TEXT,
229 _("mcen_va_options_messageformat_plain"),
232 return (ModestPairList *) g_slist_reverse (list);
236 _modest_global_settings_dialog_load_conf (ModestGlobalSettingsDialog *self)
240 gint combo_id, value;
241 GError *error = NULL;
242 ModestGlobalSettingsDialogPrivate *priv;
244 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
245 conf = modest_runtime_get_conf ();
248 checked = modest_conf_get_bool (conf, MODEST_CONF_AUTO_UPDATE, &error);
250 g_clear_error (&error);
254 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->auto_update), checked);
255 priv->initial_state.auto_update = checked;
258 combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, &error);
260 g_error_free (error);
262 combo_id = MODEST_CONNECTED_VIA_WLAN;
264 modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->connect_via),
265 (gpointer) &combo_id);
266 priv->initial_state.connect_via = combo_id;
268 /* Emit toggled to update the visibility of connect_by caption */
269 gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (priv->auto_update));
271 /* Update interval */
272 combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_INTERVAL, &error);
274 g_error_free (error);
276 combo_id = MODEST_UPDATE_INTERVAL_15_MIN;
278 modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->update_interval),
279 (gpointer) &combo_id);
280 priv->initial_state.update_interval = combo_id;
283 value = modest_conf_get_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, &error);
285 g_error_free (error);
289 /* It's better to do this in the subclasses, but it's just one
290 line, so we'll leave it here for the moment */
291 #ifdef MODEST_PLATFORM_MAEMO
292 hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (priv->size_limit), value);
294 gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_limit), value);
296 priv->initial_state.size_limit = value;
299 checked = modest_conf_get_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, &error);
301 g_error_free (error);
305 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->play_sound), checked);
306 priv->initial_state.play_sound = checked;
309 checked = modest_conf_get_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, &error);
311 g_error_free (error);
313 combo_id = MODEST_FILE_FORMAT_FORMATTED_TEXT;
315 combo_id = (checked) ? MODEST_FILE_FORMAT_FORMATTED_TEXT : MODEST_FILE_FORMAT_PLAIN_TEXT;
316 modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->msg_format),
317 (gpointer) &combo_id);
318 priv->initial_state.prefer_formatted_text = checked;
321 value = modest_conf_get_int (conf, MODEST_CONF_REPLY_TYPE, &error);
323 g_error_free (error);
325 value = MODEST_TNY_MSG_REPLY_TYPE_QUOTE;
327 if (value == MODEST_TNY_MSG_REPLY_TYPE_QUOTE)
331 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->include_reply), checked);
332 priv->initial_state.include_reply = checked;
336 get_current_settings (ModestGlobalSettingsDialogPrivate *priv,
337 ModestGlobalSettingsState *state)
341 /* Get values from UI */
342 state->auto_update = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->auto_update));
343 id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->connect_via));
344 state->connect_via = *id;
345 #ifdef MODEST_PLATFORM_MAEMO
346 state->size_limit = hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (priv->size_limit));
348 state->size_limit = gtk_spin_button_get_value (GTK_SPIN_BUTTON (priv->size_limit));
350 id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->update_interval));
351 state->update_interval = *id;
352 state->play_sound = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->play_sound));
353 id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->msg_format));
354 state->prefer_formatted_text = (*id == MODEST_FILE_FORMAT_FORMATTED_TEXT) ? TRUE : FALSE;
355 state->include_reply = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->include_reply));
359 _modest_global_settings_dialog_save_conf (ModestGlobalSettingsDialog *self)
362 ModestGlobalSettingsState current_state;
363 GError *error = NULL;
364 ModestGlobalSettingsDialogPrivate *priv;
366 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
368 conf = modest_runtime_get_conf ();
370 get_current_settings (priv, ¤t_state);
372 /* Save configuration */
373 modest_conf_set_bool (conf, MODEST_CONF_AUTO_UPDATE, current_state.auto_update, &error);
374 RETURN_FALSE_ON_ERROR(error);
375 modest_conf_set_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, current_state.connect_via, NULL);
376 RETURN_FALSE_ON_ERROR(error);
377 modest_conf_set_int (conf, MODEST_CONF_UPDATE_INTERVAL, current_state.update_interval, NULL);
378 RETURN_FALSE_ON_ERROR(error);
379 modest_conf_set_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, current_state.size_limit, NULL);
380 RETURN_FALSE_ON_ERROR(error);
381 modest_conf_set_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, current_state.play_sound, NULL);
382 RETURN_FALSE_ON_ERROR(error);
383 modest_conf_set_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, current_state.prefer_formatted_text, NULL);
384 RETURN_FALSE_ON_ERROR(error);
385 modest_conf_set_int (conf, MODEST_CONF_REPLY_TYPE,
386 (current_state.include_reply) ? MODEST_TNY_MSG_REPLY_TYPE_QUOTE :
387 MODEST_TNY_MSG_REPLY_TYPE_CITE, NULL);
388 RETURN_FALSE_ON_ERROR(error);
391 if (priv->initial_state.auto_update != current_state.auto_update ||
392 priv->initial_state.connect_via != current_state.connect_via ||
393 priv->initial_state.update_interval != current_state.update_interval) {
395 TnyAccountStore *account_store;
398 if (!current_state.auto_update) {
399 modest_platform_set_update_interval (0);
400 /* To avoid a new indentation level */
404 account_store = TNY_ACCOUNT_STORE (modest_runtime_get_account_store ());
405 device = tny_account_store_get_device (account_store);
407 if (tny_device_is_online (device)) {
408 /* If connected via any then set update interval */
409 if (current_state.connect_via == MODEST_CONNECTED_VIA_ANY) {
410 modest_platform_set_update_interval (current_state.update_interval);
412 /* Set update interval only if we
413 selected the same connect_via
414 method than the one already used by
416 ModestConnectedVia connect_via =
417 MODEST_GLOBAL_SETTINGS_DIALOG_GET_CLASS(self)->current_connection_func ();
419 if (current_state.connect_via == connect_via)
420 modest_platform_set_update_interval (current_state.update_interval);
422 modest_platform_set_update_interval (0);
425 /* Disable autoupdate in offline mode */
426 modest_platform_set_update_interval (0);
428 g_object_unref (device);
436 settings_changed (ModestGlobalSettingsState initial_state,
437 ModestGlobalSettingsState current_state)
439 if (initial_state.auto_update != current_state.auto_update ||
440 initial_state.connect_via != current_state.connect_via ||
441 initial_state.update_interval != current_state.update_interval ||
442 initial_state.size_limit != current_state.size_limit ||
443 initial_state.play_sound != current_state.play_sound ||
444 initial_state.prefer_formatted_text != current_state.prefer_formatted_text ||
445 initial_state.include_reply != current_state.include_reply)
452 on_response (GtkDialog *dialog,
456 ModestGlobalSettingsDialogPrivate *priv;
457 ModestGlobalSettingsState current_state;
460 priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (user_data);
462 get_current_settings (priv, ¤t_state);
463 changed = settings_changed (priv->initial_state, current_state);
465 if (arg1 == GTK_RESPONSE_OK) {
469 saved = _modest_global_settings_dialog_save_conf (MODEST_GLOBAL_SETTINGS_DIALOG (dialog));
471 modest_platform_run_information_dialog (GTK_WINDOW (user_data),
472 _("mcen_ib_advsetup_settings_saved"));
474 modest_platform_run_information_dialog (GTK_WINDOW (user_data),
475 _("mail_ib_setting_failed"));
481 response = modest_platform_run_confirmation_dialog (GTK_WINDOW (user_data),
482 _("imum_nc_wizard_confirm_lose_changes"));
483 /* Do not close if the user Cancels */
484 if (response == GTK_RESPONSE_CANCEL)
485 g_signal_stop_emission_by_name (dialog, "response");
490 static ModestConnectedVia
491 current_connection_default (void)
493 g_warning ("You must implement %s", __FUNCTION__);
494 g_return_val_if_reached (MODEST_CONNECTED_VIA_ANY);