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