Remove not used size_limit item in settings dialog
[modest] / src / widgets / modest-global-settings-dialog.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
32 #include <string.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 "modest-hildon-includes.h"
43 #include <hildon/hildon-number-editor.h>
44 #endif
45 /* include other impl specific header files */
46
47 #define RETURN_FALSE_ON_ERROR(error) if (error) { g_clear_error (&error); return FALSE; }
48
49 /* 'private'/'protected' functions */
50 static void modest_global_settings_dialog_class_init (ModestGlobalSettingsDialogClass *klass);
51 static void modest_global_settings_dialog_init       (ModestGlobalSettingsDialog *obj);
52 static void modest_global_settings_dialog_finalize   (GObject *obj);
53
54 static void on_response (GtkDialog *dialog,
55                          gint arg1,
56                          gpointer user_data);
57 static gboolean on_delete_event (GtkWidget *widget,
58                                  GdkEvent  *event,
59                                  gpointer   user_data);
60
61 static void get_current_settings (ModestGlobalSettingsDialogPrivate *priv,
62                                   ModestGlobalSettingsState *state);
63
64 static ModestConnectedVia current_connection_default (void);
65
66 static gboolean modest_global_settings_dialog_save_settings_default (ModestGlobalSettingsDialog *self);
67
68 /* list my signals  */
69 enum {
70         /* MY_SIGNAL_1, */
71         /* MY_SIGNAL_2, */
72         LAST_SIGNAL
73 };
74
75 /* globals */
76 static GtkDialogClass *parent_class = NULL;
77
78 /* uncomment the following if you have defined any signals */
79 /* static guint signals[LAST_SIGNAL] = {0}; */
80
81 GType
82 modest_global_settings_dialog_get_type (void)
83 {
84         static GType my_type = 0;
85         if (!my_type) {
86                 static const GTypeInfo my_info = {
87                         sizeof(ModestGlobalSettingsDialogClass),
88                         NULL,           /* base init */
89                         NULL,           /* base finalize */
90                         (GClassInitFunc) modest_global_settings_dialog_class_init,
91                         NULL,           /* class finalize */
92                         NULL,           /* class data */
93                         sizeof(ModestGlobalSettingsDialog),
94                         1,              /* n_preallocs */
95                         (GInstanceInitFunc) modest_global_settings_dialog_init,
96                         NULL
97                 };
98                 my_type = g_type_register_static (GTK_TYPE_DIALOG,
99                                                   "ModestGlobalSettingsDialog",
100                                                   &my_info, 0);
101         }
102         return my_type;
103 }
104
105 static void
106 modest_global_settings_dialog_class_init (ModestGlobalSettingsDialogClass *klass)
107 {
108         GObjectClass *gobject_class;
109         gobject_class = (GObjectClass*) klass;
110
111         parent_class            = g_type_class_peek_parent (klass);
112         gobject_class->finalize = modest_global_settings_dialog_finalize;
113
114         g_type_class_add_private (gobject_class, sizeof(ModestGlobalSettingsDialogPrivate));
115
116         klass->current_connection_func = current_connection_default;
117         klass->save_settings_func = modest_global_settings_dialog_save_settings_default;
118 }
119
120 static void
121 modest_global_settings_dialog_init (ModestGlobalSettingsDialog *self)
122 {
123         ModestGlobalSettingsDialogPrivate *priv;
124
125         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
126
127         priv->notebook = gtk_notebook_new ();
128         priv->default_account_selector = NULL;
129         priv->accounts_list = NULL;
130
131         /* Connect to the dialog's "response" and "delete-event" signals */
132         g_signal_connect (G_OBJECT (self), "response", G_CALLBACK (on_response), self);
133         g_signal_connect (G_OBJECT (self), "delete-event", G_CALLBACK (on_delete_event), self);
134
135         /* Set title */
136         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_options"));
137 }
138
139 static void
140 modest_global_settings_dialog_finalize (GObject *obj)
141 {
142         ModestGlobalSettingsDialogPrivate *priv = 
143                 MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (obj);
144
145         /* These had to stay alive as long as the comboboxes that used them: */
146         modest_pair_list_free (priv->connect_via_list);
147         modest_pair_list_free (priv->update_interval_list);
148         modest_pair_list_free (priv->msg_format_list);
149         modest_pair_list_free (priv->accounts_list);
150
151         G_OBJECT_CLASS(parent_class)->finalize (obj);
152 }
153
154 /*
155  * Creates a pair list (number,string) and adds it to the given list
156  */
157 static void
158 add_to_modest_pair_list (const gint num, const gchar *str, GSList **list)
159 {
160         gint *number;
161         ModestPair *pair;
162
163         number = g_malloc0 (sizeof (gint));
164         *number = num;
165         pair = modest_pair_new (number, g_strdup (str), FALSE);
166         *list = g_slist_prepend (*list, pair);
167 }
168
169 ModestPairList *
170 _modest_global_settings_dialog_get_connected_via (void)
171 {
172         GSList *list = NULL;
173         const gchar *message;
174
175 #ifndef MODEST_TOOLKIT_GTK
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");
180         else
181                 message = _("mcen_va_options_connectiontype_wlan");
182 #else
183         message = _("mcen_va_options_connectiontype_wlan");
184 #endif
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"), 
188                                  &list);
189
190         return (ModestPairList *) g_slist_reverse (list);
191 }
192
193 /*
194  * Gets a list of pairs of update intervals
195  */
196 ModestPairList *
197 _modest_global_settings_dialog_get_update_interval (void)
198 {
199         GSList *list = NULL;
200
201         add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_5_MIN, 
202                                  _("mcen_va_options_updateinterval_5min"), 
203                                  &list);
204         add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_10_MIN, 
205                                  _("mcen_va_options_updateinterval_10min"), 
206                                  &list);
207         add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_15_MIN, 
208                                  _("mcen_va_options_updateinterval_15min"), 
209                                  &list);
210         add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_30_MIN, 
211                                  _("mcen_va_options_updateinterval_30min"), 
212                                  &list);
213         add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_1_HOUR, 
214                                  _("mcen_va_options_updateinterval_1h"), 
215                                  &list);
216         add_to_modest_pair_list (MODEST_UPDATE_INTERVAL_2_HOUR, 
217                                  _("mcen_va_options_updateinterval_2h"), 
218                                  &list);
219
220         return (ModestPairList *) g_slist_reverse (list);
221 }
222
223 /*
224  * Gets a list of pairs 
225  */
226 ModestPairList *
227 _modest_global_settings_dialog_get_msg_formats (void)
228 {
229         GSList *list = NULL;
230
231         add_to_modest_pair_list (MODEST_FILE_FORMAT_FORMATTED_TEXT, 
232                                  _("mcen_va_options_messageformat_html"), 
233                                  &list);
234         add_to_modest_pair_list (MODEST_FILE_FORMAT_PLAIN_TEXT, 
235                                  _("mcen_va_options_messageformat_plain"), 
236                                  &list);
237
238         return (ModestPairList *) g_slist_reverse (list);
239 }
240
241 static void
242 get_current_settings (ModestGlobalSettingsDialogPrivate *priv,
243                       ModestGlobalSettingsState *state)
244 {
245         gint *id;
246
247         /* Get values from UI */
248         state->notifications = modest_togglable_get_active (priv->notifications);
249         state->add_to_contacts = modest_togglable_get_active (priv->add_to_contacts);
250         state->auto_update = modest_togglable_get_active (priv->auto_update);
251         id = modest_selector_get_active_id (priv->connect_via);
252         state->default_account = modest_selector_get_active_id (priv->default_account_selector);
253         state->connect_via = *id;
254
255         id = modest_selector_get_active_id (priv->update_interval);
256         state->update_interval = *id;
257         id = modest_selector_get_active_id (priv->msg_format);
258         state->play_sound = priv->initial_state.play_sound;
259         state->prefer_formatted_text = (*id == MODEST_FILE_FORMAT_FORMATTED_TEXT) ? TRUE : FALSE;
260 }
261
262 static gboolean
263 modest_global_settings_dialog_save_settings_default (ModestGlobalSettingsDialog *self)
264 {
265         ModestConf *conf;
266         ModestGlobalSettingsState current_state = {0,};
267         GError *error = NULL;
268         ModestGlobalSettingsDialogPrivate *priv;
269
270         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
271
272         conf = modest_runtime_get_conf ();
273
274         get_current_settings (priv, &current_state);
275
276         /* Save configuration */
277         modest_conf_set_bool (conf, MODEST_CONF_NOTIFICATIONS, current_state.notifications, &error);
278         RETURN_FALSE_ON_ERROR(error);
279         modest_conf_set_bool (conf, MODEST_CONF_AUTO_ADD_TO_CONTACTS, current_state.add_to_contacts, &error);
280         RETURN_FALSE_ON_ERROR(error);
281         modest_conf_set_bool (conf, MODEST_CONF_AUTO_UPDATE, current_state.auto_update, &error);
282         RETURN_FALSE_ON_ERROR(error);
283         modest_conf_set_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, current_state.connect_via, NULL);
284         RETURN_FALSE_ON_ERROR(error);
285         modest_conf_set_int (conf, MODEST_CONF_UPDATE_INTERVAL, current_state.update_interval, NULL);
286         RETURN_FALSE_ON_ERROR(error);
287         modest_conf_set_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, current_state.play_sound, NULL);
288         RETURN_FALSE_ON_ERROR(error);
289         modest_conf_set_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, current_state.prefer_formatted_text, NULL);
290         RETURN_FALSE_ON_ERROR(error);
291         if (current_state.default_account &&
292             (!priv->initial_state.default_account ||
293              strcmp (current_state.default_account, priv->initial_state.default_account)!= 0)) {
294                 modest_account_mgr_set_default_account (modest_runtime_get_account_mgr (),
295                                                         current_state.default_account);
296         }
297
298         /* Apply changes */
299         if (priv->initial_state.auto_update != current_state.auto_update ||
300             priv->initial_state.connect_via != current_state.connect_via ||
301             priv->initial_state.update_interval != current_state.update_interval) {
302
303                 TnyAccountStore *account_store;
304                 TnyDevice *device;
305                 
306                 if (!current_state.auto_update) {
307                         modest_platform_set_update_interval (0);
308                         /* To avoid a new indentation level */
309                         goto exit;
310                 }
311                 
312                 account_store = TNY_ACCOUNT_STORE (modest_runtime_get_account_store ());
313                 device = tny_account_store_get_device (account_store);
314                 
315                 if (tny_device_is_online (device)) {
316                         /* If connected via any then set update interval */
317                         if (current_state.connect_via == MODEST_CONNECTED_VIA_ANY) {
318                                 modest_platform_set_update_interval (current_state.update_interval);
319                         } else {
320                                 /* Set update interval only if we
321                                    selected the same connect_via
322                                    method than the one already used by
323                                    the device */
324                                 ModestConnectedVia connect_via =
325                                         MODEST_GLOBAL_SETTINGS_DIALOG_GET_CLASS(self)->current_connection_func ();
326                                 
327                                 if (current_state.connect_via == connect_via)
328                                         modest_platform_set_update_interval (current_state.update_interval);
329                                 else
330                                         modest_platform_set_update_interval (0);
331                         }
332                 } else {
333                         /* Disable autoupdate in offline mode */
334                         modest_platform_set_update_interval (0);
335                 }
336                 g_object_unref (device);
337         }
338         
339 exit:
340         return TRUE;
341 }
342
343 static gboolean
344 settings_changed (ModestGlobalSettingsState initial_state,
345                   ModestGlobalSettingsState current_state)
346 {
347         if (initial_state.auto_update != current_state.auto_update ||
348             initial_state.notifications != current_state.notifications ||
349             initial_state.add_to_contacts != current_state.add_to_contacts ||
350             initial_state.connect_via != current_state.connect_via ||
351             initial_state.update_interval != current_state.update_interval ||
352             initial_state.play_sound != current_state.play_sound ||
353             initial_state.prefer_formatted_text != current_state.prefer_formatted_text ||
354             (current_state.default_account &&
355              (!initial_state.default_account || 
356               strcmp (current_state.default_account, initial_state.default_account)!= 0)))
357                 return TRUE;
358         else
359                 return FALSE;
360 }
361
362 static gboolean
363 on_delete_event (GtkWidget *widget,
364                  GdkEvent  *event,
365                  gpointer   user_data)
366 {
367         ModestGlobalSettingsDialogPrivate *priv;
368         ModestGlobalSettingsState current_state;
369
370         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (user_data);
371
372         /* If settings changed, them the response method already asked
373            the user, because it's always executed before (see
374            GtkDialog code). If it's not then simply close */
375         get_current_settings (priv, &current_state);
376         return settings_changed (priv->initial_state, current_state);
377 }
378
379 static void
380 on_response (GtkDialog *dialog,
381              gint arg1,
382              gpointer user_data)
383 {
384         ModestGlobalSettingsDialogPrivate *priv;
385         ModestGlobalSettingsState current_state = {0,};
386         gboolean changed = FALSE;
387
388         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (user_data);
389
390         get_current_settings (priv, &current_state);
391         changed = settings_changed (priv->initial_state, current_state);
392
393         if (arg1 == GTK_RESPONSE_OK) {
394                 if (changed) {
395                         gboolean saved;
396
397                         saved = modest_global_settings_dialog_save_settings (MODEST_GLOBAL_SETTINGS_DIALOG (dialog));
398                         if (saved) {
399                                 modest_platform_information_banner (NULL, NULL,
400                                                                     _("mcen_ib_advsetup_settings_saved"));
401                         } else {
402                                 modest_platform_information_banner (NULL, NULL,
403                                                                     _("mail_ib_setting_failed"));
404                         }
405                 }
406         } else {
407                 if (changed) {
408                         gint response;
409                         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (user_data),
410                                                                             _("imum_nc_wizard_confirm_lose_changes"));
411                         /* Do not close if the user Cancels */
412                         if (response != GTK_RESPONSE_OK)
413                                 g_signal_stop_emission_by_name (user_data, "response");
414                 }
415         }
416 }
417
418 static ModestConnectedVia
419 current_connection_default (void)
420 {
421         g_warning ("You must implement %s", __FUNCTION__);
422         g_return_val_if_reached (MODEST_CONNECTED_VIA_ANY);
423 }
424
425 gboolean
426 modest_global_settings_dialog_save_settings (ModestGlobalSettingsDialog *self)
427 {
428         g_return_val_if_fail (MODEST_IS_GLOBAL_SETTINGS_DIALOG (self), FALSE);
429
430         return MODEST_GLOBAL_SETTINGS_DIALOG_GET_CLASS(self)->save_settings_func (self);
431 }