Fixed several invalid #ifdef statements
[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         state->size_limit = modest_number_entry_get_value (priv->size_limit);
255
256         id = modest_selector_get_active_id (priv->update_interval);
257         state->update_interval = *id;
258         id = modest_selector_get_active_id (priv->msg_format);
259         state->play_sound = priv->initial_state.play_sound;
260         state->prefer_formatted_text = (*id == MODEST_FILE_FORMAT_FORMATTED_TEXT) ? TRUE : FALSE;
261 }
262
263 static gboolean
264 modest_global_settings_dialog_save_settings_default (ModestGlobalSettingsDialog *self)
265 {
266         ModestConf *conf;
267         ModestGlobalSettingsState current_state = {0,};
268         GError *error = NULL;
269         ModestGlobalSettingsDialogPrivate *priv;
270
271         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
272
273         conf = modest_runtime_get_conf ();
274
275         get_current_settings (priv, &current_state);
276
277         /* Save configuration */
278         modest_conf_set_bool (conf, MODEST_CONF_NOTIFICATIONS, current_state.notifications, &error);
279         RETURN_FALSE_ON_ERROR(error);
280         modest_conf_set_bool (conf, MODEST_CONF_AUTO_ADD_TO_CONTACTS, current_state.add_to_contacts, &error);
281         RETURN_FALSE_ON_ERROR(error);
282         modest_conf_set_bool (conf, MODEST_CONF_AUTO_UPDATE, current_state.auto_update, &error);
283         RETURN_FALSE_ON_ERROR(error);
284         modest_conf_set_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, current_state.connect_via, NULL);
285         RETURN_FALSE_ON_ERROR(error);
286         modest_conf_set_int (conf, MODEST_CONF_UPDATE_INTERVAL, current_state.update_interval, NULL);
287         RETURN_FALSE_ON_ERROR(error);
288         modest_conf_set_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, current_state.size_limit, NULL);
289         RETURN_FALSE_ON_ERROR(error);
290         modest_conf_set_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, current_state.play_sound, NULL);
291         RETURN_FALSE_ON_ERROR(error);
292         modest_conf_set_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, current_state.prefer_formatted_text, NULL);
293         RETURN_FALSE_ON_ERROR(error);
294         if (current_state.default_account &&
295             (!priv->initial_state.default_account ||
296              strcmp (current_state.default_account, priv->initial_state.default_account)!= 0)) {
297                 modest_account_mgr_set_default_account (modest_runtime_get_account_mgr (),
298                                                         current_state.default_account);
299         }
300
301         /* Apply changes */
302         if (priv->initial_state.auto_update != current_state.auto_update ||
303             priv->initial_state.connect_via != current_state.connect_via ||
304             priv->initial_state.update_interval != current_state.update_interval) {
305
306                 TnyAccountStore *account_store;
307                 TnyDevice *device;
308                 
309                 if (!current_state.auto_update) {
310                         modest_platform_set_update_interval (0);
311                         /* To avoid a new indentation level */
312                         goto exit;
313                 }
314                 
315                 account_store = TNY_ACCOUNT_STORE (modest_runtime_get_account_store ());
316                 device = tny_account_store_get_device (account_store);
317                 
318                 if (tny_device_is_online (device)) {
319                         /* If connected via any then set update interval */
320                         if (current_state.connect_via == MODEST_CONNECTED_VIA_ANY) {
321                                 modest_platform_set_update_interval (current_state.update_interval);
322                         } else {
323                                 /* Set update interval only if we
324                                    selected the same connect_via
325                                    method than the one already used by
326                                    the device */
327                                 ModestConnectedVia connect_via =
328                                         MODEST_GLOBAL_SETTINGS_DIALOG_GET_CLASS(self)->current_connection_func ();
329                                 
330                                 if (current_state.connect_via == connect_via)
331                                         modest_platform_set_update_interval (current_state.update_interval);
332                                 else
333                                         modest_platform_set_update_interval (0);
334                         }
335                 } else {
336                         /* Disable autoupdate in offline mode */
337                         modest_platform_set_update_interval (0);
338                 }
339                 g_object_unref (device);
340         }
341         
342 exit:
343         return TRUE;
344 }
345
346 static gboolean
347 settings_changed (ModestGlobalSettingsState initial_state,
348                   ModestGlobalSettingsState current_state)
349 {
350         if (initial_state.auto_update != current_state.auto_update ||
351             initial_state.notifications != current_state.notifications ||
352             initial_state.add_to_contacts != current_state.add_to_contacts ||
353             initial_state.connect_via != current_state.connect_via ||
354             initial_state.update_interval != current_state.update_interval ||
355             initial_state.size_limit != current_state.size_limit ||
356             initial_state.play_sound != current_state.play_sound ||
357             initial_state.prefer_formatted_text != current_state.prefer_formatted_text ||
358             (current_state.default_account &&
359              (!initial_state.default_account || 
360               strcmp (current_state.default_account, initial_state.default_account)!= 0)))
361                 return TRUE;
362         else
363                 return FALSE;
364 }
365
366 static gboolean
367 on_delete_event (GtkWidget *widget,
368                  GdkEvent  *event,
369                  gpointer   user_data)
370 {
371         ModestGlobalSettingsDialogPrivate *priv;
372         ModestGlobalSettingsState current_state;
373
374         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (user_data);
375
376         /* If settings changed, them the response method already asked
377            the user, because it's always executed before (see
378            GtkDialog code). If it's not then simply close */
379         get_current_settings (priv, &current_state);
380         return settings_changed (priv->initial_state, current_state);
381 }
382
383 static void
384 on_response (GtkDialog *dialog,
385              gint arg1,
386              gpointer user_data)
387 {
388         ModestGlobalSettingsDialogPrivate *priv;
389         ModestGlobalSettingsState current_state = {0,};
390         gboolean changed = FALSE;
391
392         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (user_data);
393
394         get_current_settings (priv, &current_state);
395         changed = settings_changed (priv->initial_state, current_state);
396
397         if (arg1 == GTK_RESPONSE_OK) {
398                 if (changed) {
399                         gboolean saved;
400
401                         saved = modest_global_settings_dialog_save_settings (MODEST_GLOBAL_SETTINGS_DIALOG (dialog));
402                         if (saved) {
403                                 modest_platform_information_banner (NULL, NULL,
404                                                                     _("mcen_ib_advsetup_settings_saved"));
405                         } else {
406                                 modest_platform_information_banner (NULL, NULL,
407                                                                     _("mail_ib_setting_failed"));
408                         }
409                 }
410         } else {
411                 if (changed) {
412                         gint response;
413                         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (user_data),
414                                                                             _("imum_nc_wizard_confirm_lose_changes"));
415                         /* Do not close if the user Cancels */
416                         if (response != GTK_RESPONSE_OK)
417                                 g_signal_stop_emission_by_name (user_data, "response");
418                 }
419         }
420 }
421
422 static ModestConnectedVia
423 current_connection_default (void)
424 {
425         g_warning ("You must implement %s", __FUNCTION__);
426         g_return_val_if_reached (MODEST_CONNECTED_VIA_ANY);
427 }
428
429 gboolean
430 modest_global_settings_dialog_save_settings (ModestGlobalSettingsDialog *self)
431 {
432         g_return_val_if_fail (MODEST_IS_GLOBAL_SETTINGS_DIALOG (self), FALSE);
433
434         return MODEST_GLOBAL_SETTINGS_DIALOG_GET_CLASS(self)->save_settings_func (self);
435 }