X-Git-Url: http://git.maemo.org/git/?p=modest;a=blobdiff_plain;f=src%2Fwidgets%2Fmodest-global-settings-dialog.c;h=83e221b16341bd19a37753e8dd8ce5ed1fb44a3d;hp=bea91cfbc4940241a575cb1a5672154acef575a6;hb=11a7e8bd60ce134adc068ecedefd64faa1dd7ddf;hpb=f0d8f53cb1a775d9caff277a4538c2aedb4e358b diff --git a/src/widgets/modest-global-settings-dialog.c b/src/widgets/modest-global-settings-dialog.c index bea91cf..83e221b 100644 --- a/src/widgets/modest-global-settings-dialog.c +++ b/src/widgets/modest-global-settings-dialog.c @@ -28,15 +28,43 @@ */ #include -#include "modest-global-settings-dialog.h" +#include +#include +#include +#include +#include "widgets/modest-global-settings-dialog.h" +#include "widgets/modest-global-settings-dialog-priv.h" #include "modest-defs.h" +#include "modest-conf.h" +#include "modest-runtime.h" #include "modest-ui-constants.h" +#include "modest-tny-msg.h" +#include "modest-platform.h" +#include "widgets/modest-combo-box.h" +#ifdef MODEST_PLATFORM_MAEMO +#ifdef MODEST_HILDON_VERSION_0 +#include +#else +#include +#endif /*MODEST_HILDON_VERSION_0*/ +#endif /* include other impl specific header files */ +#define RETURN_FALSE_ON_ERROR(error) if (error) { g_clear_error (&error); return FALSE; } + /* 'private'/'protected' functions */ static void modest_global_settings_dialog_class_init (ModestGlobalSettingsDialogClass *klass); static void modest_global_settings_dialog_init (ModestGlobalSettingsDialog *obj); static void modest_global_settings_dialog_finalize (GObject *obj); + +static void on_response (GtkDialog *dialog, + gint arg1, + gpointer user_data); +static void get_current_settings (ModestGlobalSettingsDialogPrivate *priv, + ModestGlobalSettingsState *state); + +static ModestConnectedVia current_connection_default (void); + /* list my signals */ enum { /* MY_SIGNAL_1, */ @@ -44,19 +72,6 @@ enum { LAST_SIGNAL }; -static GtkWidget* create_updating_page (void); -static GtkWidget* create_composing_page (void); - -typedef struct _ModestGlobalSettingsDialogPrivate ModestGlobalSettingsDialogPrivate; -struct _ModestGlobalSettingsDialogPrivate { - GtkWidget *notebook; - GtkWidget *updating_page; - GtkWidget *composing_page; - gboolean modified; -}; -#define MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \ - MODEST_TYPE_GLOBAL_SETTINGS_DIALOG, \ - ModestGlobalSettingsDialogPrivate)) /* globals */ static GtkDialogClass *parent_class = NULL; @@ -98,12 +113,7 @@ modest_global_settings_dialog_class_init (ModestGlobalSettingsDialogClass *klass g_type_class_add_private (gobject_class, sizeof(ModestGlobalSettingsDialogPrivate)); - /* signal definitions go here, e.g.: */ -/* signals[MY_SIGNAL_1] = */ -/* g_signal_new ("my_signal_1",....); */ -/* signals[MY_SIGNAL_2] = */ -/* g_signal_new ("my_signal_2",....); */ -/* etc. */ + klass->current_connection_func = current_connection_default; } static void @@ -113,32 +123,15 @@ modest_global_settings_dialog_init (ModestGlobalSettingsDialog *self) priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self); - priv->modified = FALSE; priv->notebook = gtk_notebook_new (); - priv->updating_page = create_updating_page (); - priv->composing_page = create_composing_page (); - - /* Add the notebook pages: */ - gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), priv->updating_page, - gtk_label_new (_("mcen_ti_options_updating"))); - gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), priv->composing_page, - gtk_label_new (_("mcen_ti_options_composing"))); - - gtk_container_add (GTK_CONTAINER (GTK_DIALOG (self)->vbox), priv->notebook); - gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (self)->vbox), MODEST_MARGIN_HALF); - gtk_widget_show (priv->notebook); - + priv->changed = FALSE; + /* Add the buttons: */ gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_OK, GTK_RESPONSE_OK); gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL); -/* /\* Connect to the dialog's response signal: *\/ */ -/* /\* We use connect-before */ -/* * so we can stop the signal emission, */ -/* * to stop the default signal handler from closing the dialog. */ -/* *\/ */ -/* g_signal_connect (G_OBJECT (self), "response", */ -/* G_CALLBACK (on_response), self); */ + /* Connect to the dialog's response signal: */ + g_signal_connect (G_OBJECT (self), "response", G_CALLBACK (on_response), self); /* Set title */ gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_options")); @@ -147,35 +140,356 @@ modest_global_settings_dialog_init (ModestGlobalSettingsDialog *self) static void modest_global_settings_dialog_finalize (GObject *obj) { -/* free/unref instance resources here */ + ModestGlobalSettingsDialogPrivate *priv = + MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (obj); + + /* These had to stay alive as long as the comboboxes that used them: */ + modest_pair_list_free (priv->connect_via_list); + modest_pair_list_free (priv->update_interval_list); + modest_pair_list_free (priv->msg_format_list); + G_OBJECT_CLASS(parent_class)->finalize (obj); } -GtkWidget* -modest_global_settings_dialog_new (void) +/* + * Creates a pair list (number,string) and adds it to the given list + */ +static void +add_to_modest_pair_list (const gint num, const gchar *str, GSList **list) { - return GTK_WIDGET(g_object_new(MODEST_TYPE_GLOBAL_SETTINGS_DIALOG, NULL)); + gint *number; + ModestPair *pair; + + number = g_malloc0 (sizeof (gint)); + *number = num; + pair = modest_pair_new (number, g_strdup (str), FALSE); + *list = g_slist_prepend (*list, pair); } -/* insert many other interesting function implementations */ -/* such as modest_global_settings_dialog_do_something, or modest_global_settings_dialog_has_foo */ +/* + * Gets a list of pairs + */ +ModestPairList * +_modest_global_settings_dialog_get_connected_via (void) +{ + GSList *list = NULL; + + add_to_modest_pair_list (MODEST_CONNECTED_VIA_WLAN, + _("mcen_va_options_connectiontype_wlan"), + &list); + add_to_modest_pair_list (MODEST_CONNECTED_VIA_ANY, + _("mcen_va_options_connectiontype_all"), + &list); + + return (ModestPairList *) g_slist_reverse (list); +} + +/* + * Gets a list of pairs of update intervals + */ +ModestPairList * +_modest_global_settings_dialog_get_update_interval (void) +{ + GSList *list = NULL; + + add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_5_MIN, + _("mcen_va_options_updateinterval_5min"), + &list); + add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_10_MIN, + _("mcen_va_options_updateinterval_10min"), + &list); + add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_15_MIN, + _("mcen_va_options_updateinterval_15min"), + &list); + add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_30_MIN, + _("mcen_va_options_updateinterval_30min"), + &list); + add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_1_HOUR, + _("mcen_va_options_updateinterval_1h"), + &list); + add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_2_HOUR, + _("mcen_va_options_updateinterval_2h"), + &list); -static GtkWidget* -create_updating_page (void) + return (ModestPairList *) g_slist_reverse (list); +} + +/* + * Gets a list of pairs + */ +ModestPairList * +_modest_global_settings_dialog_get_msg_formats (void) { - GtkWidget *box; + GSList *list = NULL; - box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE); + add_to_modest_pair_list (MODEST_FILE_FORMAT_FORMATTED_TEXT, + _("mcen_va_options_messageformat_html"), + &list); + add_to_modest_pair_list (MODEST_FILE_FORMAT_PLAIN_TEXT, + _("mcen_va_options_messageformat_plain"), + &list); - return box; + return (ModestPairList *) g_slist_reverse (list); } -static GtkWidget* -create_composing_page (void) +void +_modest_global_settings_dialog_load_conf (ModestGlobalSettingsDialog *self) { - GtkWidget *box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE); + ModestConf *conf; + gboolean checked; + gint combo_id, value; + GError *error = NULL; + ModestGlobalSettingsDialogPrivate *priv; + + priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self); + conf = modest_runtime_get_conf (); + + /* Autoupdate */ + checked = modest_conf_get_bool (conf, MODEST_CONF_AUTO_UPDATE, &error); + if (error) { + g_clear_error (&error); + error = NULL; + checked = FALSE; + } + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->auto_update), checked); + priv->initial_state.auto_update = checked; + + /* Connected by */ + combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, &error); + if (error) { + g_error_free (error); + error = NULL; + combo_id = MODEST_CONNECTED_VIA_WLAN; + } + modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->connect_via), + (gpointer) &combo_id); + priv->initial_state.connect_via = combo_id; + + /* Emit toggled to update the visibility of connect_by caption */ + gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (priv->auto_update)); + + /* Update interval */ + combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_INTERVAL, &error); + if (error) { + g_error_free (error); + error = NULL; + combo_id = MODEST_UPDATE_INTERVAL_15_MIN; + } + modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->update_interval), + (gpointer) &combo_id); + priv->initial_state.update_interval = combo_id; + + /* Size limit */ + value = modest_conf_get_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, &error); + if (error) { + g_error_free (error); + error = NULL; + value = 1000; + } + /* It's better to do this in the subclasses, but it's just one + line, so we'll leave it here for the moment */ +#ifdef MODEST_PLATFORM_MAEMO + hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (priv->size_limit), value); +#else + gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_limit), value); +#endif + priv->initial_state.size_limit = value; + + /* Play sound */ + checked = modest_conf_get_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, &error); + if (error) { + g_error_free (error); + error = NULL; + checked = FALSE; + } + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->play_sound), checked); + priv->initial_state.play_sound = checked; - box = gtk_vbox_new (FALSE, MODEST_MARGIN_NONE); + /* Msg format */ + checked = modest_conf_get_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, &error); + if (error) { + g_error_free (error); + error = NULL; + combo_id = MODEST_FILE_FORMAT_FORMATTED_TEXT; + } + combo_id = (checked) ? MODEST_FILE_FORMAT_FORMATTED_TEXT : MODEST_FILE_FORMAT_PLAIN_TEXT; + modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->msg_format), + (gpointer) &combo_id); + priv->initial_state.prefer_formatted_text = checked; - return box; + /* Include reply */ + value = modest_conf_get_int (conf, MODEST_CONF_REPLY_TYPE, &error); + if (error) { + g_error_free (error); + error = NULL; + value = MODEST_TNY_MSG_REPLY_TYPE_QUOTE; + } + if (value == MODEST_TNY_MSG_REPLY_TYPE_QUOTE) + checked = TRUE; + else + checked = FALSE; + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->include_reply), checked); + priv->initial_state.include_reply = checked; +} + +static void +get_current_settings (ModestGlobalSettingsDialogPrivate *priv, + ModestGlobalSettingsState *state) +{ + gint *id; + + /* Get values from UI */ + state->auto_update = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->auto_update)); + id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->connect_via)); + state->connect_via = *id; +#ifdef MODEST_PLATFORM_MAEMO + state->size_limit = hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (priv->size_limit)); +#else + state->size_limit = gtk_spin_button_get_value (GTK_SPIN_BUTTON (priv->size_limit)); +#endif + id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->update_interval)); + state->update_interval = *id; + state->play_sound = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->play_sound)); + id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->msg_format)); + state->prefer_formatted_text = (*id == MODEST_FILE_FORMAT_FORMATTED_TEXT) ? TRUE : FALSE; + state->include_reply = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->include_reply)); +} + +gboolean +_modest_global_settings_dialog_save_conf (ModestGlobalSettingsDialog *self) +{ + ModestConf *conf; + ModestGlobalSettingsState current_state; + GError *error = NULL; + ModestGlobalSettingsDialogPrivate *priv; + + priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self); + + conf = modest_runtime_get_conf (); + + get_current_settings (priv, ¤t_state); + + /* Save configuration */ + modest_conf_set_bool (conf, MODEST_CONF_AUTO_UPDATE, current_state.auto_update, &error); + RETURN_FALSE_ON_ERROR(error); + modest_conf_set_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, current_state.connect_via, NULL); + RETURN_FALSE_ON_ERROR(error); + modest_conf_set_int (conf, MODEST_CONF_UPDATE_INTERVAL, current_state.update_interval, NULL); + RETURN_FALSE_ON_ERROR(error); + modest_conf_set_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, current_state.size_limit, NULL); + RETURN_FALSE_ON_ERROR(error); + modest_conf_set_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, current_state.play_sound, NULL); + RETURN_FALSE_ON_ERROR(error); + modest_conf_set_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, current_state.prefer_formatted_text, NULL); + RETURN_FALSE_ON_ERROR(error); + modest_conf_set_int (conf, MODEST_CONF_REPLY_TYPE, + (current_state.include_reply) ? MODEST_TNY_MSG_REPLY_TYPE_QUOTE : + MODEST_TNY_MSG_REPLY_TYPE_CITE, NULL); + RETURN_FALSE_ON_ERROR(error); + + /* Apply changes */ + if (priv->initial_state.auto_update != current_state.auto_update || + priv->initial_state.connect_via != current_state.connect_via || + priv->initial_state.update_interval != current_state.update_interval) { + + TnyAccountStore *account_store; + TnyDevice *device; + + if (!current_state.auto_update) { + modest_platform_set_update_interval (0); + /* To avoid a new indentation level */ + goto exit; + } + + account_store = TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()); + device = tny_account_store_get_device (account_store); + + if (tny_device_is_online (device)) { + /* If connected via any then set update interval */ + if (current_state.connect_via == MODEST_CONNECTED_VIA_ANY) { + modest_platform_set_update_interval (current_state.update_interval); + } else { + /* Set update interval only if we + selected the same connect_via + method than the one already used by + the device */ + ModestConnectedVia connect_via = + MODEST_GLOBAL_SETTINGS_DIALOG_GET_CLASS(self)->current_connection_func (); + + if (current_state.connect_via == connect_via) + modest_platform_set_update_interval (current_state.update_interval); + else + modest_platform_set_update_interval (0); + } + } else { + /* Disable autoupdate in offline mode */ + modest_platform_set_update_interval (0); + } + g_object_unref (device); + } + +exit: + return TRUE; +} + +static gboolean +settings_changed (ModestGlobalSettingsState initial_state, + ModestGlobalSettingsState current_state) +{ + if (initial_state.auto_update != current_state.auto_update || + initial_state.connect_via != current_state.connect_via || + initial_state.update_interval != current_state.update_interval || + initial_state.size_limit != current_state.size_limit || + initial_state.play_sound != current_state.play_sound || + initial_state.prefer_formatted_text != current_state.prefer_formatted_text || + initial_state.include_reply != current_state.include_reply) + return TRUE; + else + return FALSE; +} + +static void +on_response (GtkDialog *dialog, + gint arg1, + gpointer user_data) +{ + ModestGlobalSettingsDialogPrivate *priv; + ModestGlobalSettingsState current_state; + gboolean changed; + + priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (user_data); + + get_current_settings (priv, ¤t_state); + changed = settings_changed (priv->initial_state, current_state); + + if (arg1 == GTK_RESPONSE_OK) { + if (changed) { + gboolean saved; + + saved = _modest_global_settings_dialog_save_conf (MODEST_GLOBAL_SETTINGS_DIALOG (dialog)); + if (saved) { + modest_platform_run_information_dialog (GTK_WINDOW (user_data), + _("mcen_ib_advsetup_settings_saved")); + } else { + modest_platform_run_information_dialog (GTK_WINDOW (user_data), + _("mail_ib_setting_failed")); + } + } + } else { + if (changed) { + gint response; + response = modest_platform_run_confirmation_dialog (GTK_WINDOW (user_data), + _("imum_nc_wizard_confirm_lose_changes")); + /* Do not close if the user Cancels */ + if (response == GTK_RESPONSE_CANCEL) + g_signal_stop_emission_by_name (dialog, "response"); + } + } +} + +static ModestConnectedVia +current_connection_default (void) +{ + g_warning ("You must implement %s", __FUNCTION__); + g_return_val_if_reached (MODEST_CONNECTED_VIA_ANY); }