* Fixes NB#83408, select attachment is now set as children of the edit window
[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 #include "widgets/modest-combo-box.h"
45 #ifdef MODEST_PLATFORM_MAEMO
46 #ifdef MODEST_HAVE_HILDON0_WIDGETS
47 #include <hildon-widgets/hildon-number-editor.h>
48 #else
49 #include <hildon/hildon-number-editor.h>
50 #endif /*MODEST_HAVE_HILDON0_WIDGETS*/
51 #endif
52 /* include other impl specific header files */
53
54 #define RETURN_FALSE_ON_ERROR(error) if (error) { g_clear_error (&error); return FALSE; }
55
56 /* 'private'/'protected' functions */
57 static void modest_global_settings_dialog_class_init (ModestGlobalSettingsDialogClass *klass);
58 static void modest_global_settings_dialog_init       (ModestGlobalSettingsDialog *obj);
59 static void modest_global_settings_dialog_finalize   (GObject *obj);
60
61 static void on_response (GtkDialog *dialog,
62                          gint arg1,
63                          gpointer user_data);
64 static void get_current_settings (ModestGlobalSettingsDialogPrivate *priv, 
65                                   ModestGlobalSettingsState *state);
66
67 static ModestConnectedVia current_connection_default (void);
68
69 /* list my signals  */
70 enum {
71         /* MY_SIGNAL_1, */
72         /* MY_SIGNAL_2, */
73         LAST_SIGNAL
74 };
75
76 /* globals */
77 static GtkDialogClass *parent_class = NULL;
78
79 /* uncomment the following if you have defined any signals */
80 /* static guint signals[LAST_SIGNAL] = {0}; */
81
82 GType
83 modest_global_settings_dialog_get_type (void)
84 {
85         static GType my_type = 0;
86         if (!my_type) {
87                 static const GTypeInfo my_info = {
88                         sizeof(ModestGlobalSettingsDialogClass),
89                         NULL,           /* base init */
90                         NULL,           /* base finalize */
91                         (GClassInitFunc) modest_global_settings_dialog_class_init,
92                         NULL,           /* class finalize */
93                         NULL,           /* class data */
94                         sizeof(ModestGlobalSettingsDialog),
95                         1,              /* n_preallocs */
96                         (GInstanceInitFunc) modest_global_settings_dialog_init,
97                         NULL
98                 };
99                 my_type = g_type_register_static (GTK_TYPE_DIALOG,
100                                                   "ModestGlobalSettingsDialog",
101                                                   &my_info, 0);
102         }
103         return my_type;
104 }
105
106 static void
107 modest_global_settings_dialog_class_init (ModestGlobalSettingsDialogClass *klass)
108 {
109         GObjectClass *gobject_class;
110         gobject_class = (GObjectClass*) klass;
111
112         parent_class            = g_type_class_peek_parent (klass);
113         gobject_class->finalize = modest_global_settings_dialog_finalize;
114
115         g_type_class_add_private (gobject_class, sizeof(ModestGlobalSettingsDialogPrivate));
116
117         klass->current_connection_func = current_connection_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
129         /* Add the buttons: */
130         gtk_dialog_add_button (GTK_DIALOG (self), _("mcen_bd_dialog_ok"), GTK_RESPONSE_OK);
131         gtk_dialog_add_button (GTK_DIALOG (self), _("mcen_bd_dialog_cancel"), GTK_RESPONSE_CANCEL);
132     
133         /* Connect to the dialog's response signal: */
134         g_signal_connect (G_OBJECT (self), "response", G_CALLBACK (on_response), self);
135
136         /* Set title */
137         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_options"));
138 }
139
140 static void
141 modest_global_settings_dialog_finalize (GObject *obj)
142 {
143         ModestGlobalSettingsDialogPrivate *priv = 
144                 MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (obj);
145
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);
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 #ifdef MODEST_PLATFORM_MAEMO
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 void   
242 _modest_global_settings_dialog_load_conf (ModestGlobalSettingsDialog *self)
243 {
244         ModestConf *conf;
245         gboolean checked;
246         gint combo_id, value;
247         GError *error = NULL;
248         ModestGlobalSettingsDialogPrivate *priv;
249
250         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
251         conf = modest_runtime_get_conf ();
252
253         /* Autoupdate */
254         checked = modest_conf_get_bool (conf, MODEST_CONF_AUTO_UPDATE, &error);
255         if (error) {
256                 g_clear_error (&error);
257                 error = NULL;
258                 checked = FALSE;
259         }
260         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->auto_update), checked);
261         priv->initial_state.auto_update = checked;
262
263         /* Connected by */
264         combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, &error);
265         if (error) {
266                 g_error_free (error);
267                 error = NULL;
268                 combo_id = MODEST_CONNECTED_VIA_WLAN_OR_WIMAX;
269         }
270         modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->connect_via), 
271                                         (gpointer) &combo_id);
272         priv->initial_state.connect_via = combo_id;
273
274         /* Emit toggled to update the visibility of connect_by caption */
275         gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (priv->auto_update));
276
277         /* Update interval */
278         combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_INTERVAL, &error);
279         if (error) {
280                 g_error_free (error);
281                 error = NULL;
282                 combo_id = MODEST_UPDATE_INTERVAL_15_MIN;
283         }
284         modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->update_interval), 
285                                         (gpointer) &combo_id);
286         priv->initial_state.update_interval = combo_id;
287
288         /* Size limit */
289         value  = modest_conf_get_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, &error);
290         if (error) {
291                 g_error_free (error);
292                 error = NULL;
293                 value = 1000;
294         }
295         /* It's better to do this in the subclasses, but it's just one
296            line, so we'll leave it here for the moment */
297 #ifdef MODEST_PLATFORM_MAEMO
298         hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (priv->size_limit), value);
299 #else
300         gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_limit), value);
301 #endif
302         priv->initial_state.size_limit = value;
303
304         /* Play sound */
305         checked = modest_conf_get_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, &error);
306         if (error) {
307                 g_error_free (error);
308                 error = NULL;
309                 checked = FALSE;
310         }
311         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->play_sound), checked);
312         priv->initial_state.play_sound = checked;
313
314         /* Msg format */
315         checked = modest_conf_get_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, &error);
316         if (error) {
317                 g_error_free (error);
318                 error = NULL;
319                 combo_id = MODEST_FILE_FORMAT_FORMATTED_TEXT;
320         }       
321         combo_id = (checked) ? MODEST_FILE_FORMAT_FORMATTED_TEXT : MODEST_FILE_FORMAT_PLAIN_TEXT;
322         modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->msg_format), 
323                                         (gpointer) &combo_id);
324         priv->initial_state.prefer_formatted_text = checked;
325 }
326
327 static void 
328 get_current_settings (ModestGlobalSettingsDialogPrivate *priv, 
329                       ModestGlobalSettingsState *state) 
330 {
331         gint *id;
332
333         /* Get values from UI */
334         state->auto_update = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->auto_update));
335         id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->connect_via));
336         state->connect_via = *id;
337 #ifdef MODEST_PLATFORM_MAEMO
338         state->size_limit = hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (priv->size_limit));
339 #else
340         state->size_limit = gtk_spin_button_get_value (GTK_SPIN_BUTTON (priv->size_limit));
341 #endif
342         id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->update_interval));
343         state->update_interval = *id;
344         state->play_sound = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->play_sound));
345         id = modest_combo_box_get_active_id (MODEST_COMBO_BOX (priv->msg_format));
346         state->prefer_formatted_text = (*id == MODEST_FILE_FORMAT_FORMATTED_TEXT) ? TRUE : FALSE;
347 }
348
349 gboolean
350 _modest_global_settings_dialog_save_conf (ModestGlobalSettingsDialog *self)
351 {
352         ModestConf *conf;
353         ModestGlobalSettingsState current_state;
354         GError *error = NULL;
355         ModestGlobalSettingsDialogPrivate *priv;
356
357         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
358
359         conf = modest_runtime_get_conf ();
360
361         get_current_settings (priv, &current_state);
362
363         /* Save configuration */
364         modest_conf_set_bool (conf, MODEST_CONF_AUTO_UPDATE, current_state.auto_update, &error);
365         RETURN_FALSE_ON_ERROR(error);
366         modest_conf_set_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, current_state.connect_via, NULL);
367         RETURN_FALSE_ON_ERROR(error);
368         modest_conf_set_int (conf, MODEST_CONF_UPDATE_INTERVAL, current_state.update_interval, NULL);
369         RETURN_FALSE_ON_ERROR(error);
370         modest_conf_set_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, current_state.size_limit, NULL);
371         RETURN_FALSE_ON_ERROR(error);
372         modest_conf_set_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, current_state.play_sound, NULL);
373         RETURN_FALSE_ON_ERROR(error);
374         modest_conf_set_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, current_state.prefer_formatted_text, NULL);
375         RETURN_FALSE_ON_ERROR(error);
376
377         /* Apply changes */
378         if (priv->initial_state.auto_update != current_state.auto_update || 
379             priv->initial_state.connect_via != current_state.connect_via ||
380             priv->initial_state.update_interval != current_state.update_interval) {
381
382                 TnyAccountStore *account_store;
383                 TnyDevice *device;
384
385                 if (!current_state.auto_update) {
386                         modest_platform_set_update_interval (0);
387                         /* To avoid a new indentation level */
388                         goto exit;
389                 }
390
391                 account_store = TNY_ACCOUNT_STORE (modest_runtime_get_account_store ());
392                 device = tny_account_store_get_device (account_store);
393
394                 if (tny_device_is_online (device)) {
395                         /* If connected via any then set update interval */
396                         if (current_state.connect_via == MODEST_CONNECTED_VIA_ANY) {
397                                 modest_platform_set_update_interval (current_state.update_interval);
398                         } else {
399                                 /* Set update interval only if we
400                                    selected the same connect_via
401                                    method than the one already used by
402                                    the device */
403                                 ModestConnectedVia connect_via = 
404                                         MODEST_GLOBAL_SETTINGS_DIALOG_GET_CLASS(self)->current_connection_func ();
405                                 
406                                 if (current_state.connect_via == connect_via)
407                                         modest_platform_set_update_interval (current_state.update_interval);
408                                 else
409                                         modest_platform_set_update_interval (0);
410                         }
411                 } else {
412                         /* Disable autoupdate in offline mode */
413                         modest_platform_set_update_interval (0);
414                 }
415                 g_object_unref (device);
416         }
417
418 exit:
419         return TRUE;
420 }
421
422 static gboolean
423 settings_changed (ModestGlobalSettingsState initial_state,
424                   ModestGlobalSettingsState current_state)
425 {
426         if (initial_state.auto_update != current_state.auto_update ||
427             initial_state.connect_via != current_state.connect_via ||
428             initial_state.update_interval != current_state.update_interval ||
429             initial_state.size_limit != current_state.size_limit ||
430             initial_state.play_sound != current_state.play_sound ||
431             initial_state.prefer_formatted_text != current_state.prefer_formatted_text)
432                 return TRUE;
433         else
434                 return FALSE;
435 }
436
437 static void
438 on_response (GtkDialog *dialog,
439              gint arg1,
440              gpointer user_data)
441 {
442         ModestGlobalSettingsDialogPrivate *priv;
443         ModestGlobalSettingsState current_state;
444         gboolean changed;
445
446         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (user_data);
447
448         get_current_settings (priv, &current_state);
449         changed = settings_changed (priv->initial_state, current_state);
450
451         if (arg1 == GTK_RESPONSE_OK) {
452                 if (changed) {
453                         gboolean saved;
454
455                         saved = _modest_global_settings_dialog_save_conf (MODEST_GLOBAL_SETTINGS_DIALOG (dialog));
456                         if (saved) {
457                                 modest_platform_information_banner (NULL, NULL,
458                                                                     _("mcen_ib_advsetup_settings_saved"));
459                         } else {
460                                 modest_platform_information_banner (NULL, NULL,
461                                                                     _("mail_ib_setting_failed"));
462                         }
463                 }
464         } else {
465                 if (changed) {
466                         gint response;
467                         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (user_data), 
468                                                                             _("imum_nc_wizard_confirm_lose_changes"));
469                         /* Do not close if the user Cancels */
470                         if (response == GTK_RESPONSE_CANCEL)
471                                 g_signal_stop_emission_by_name (dialog, "response");
472                 }
473         }
474 }
475
476 static ModestConnectedVia 
477 current_connection_default (void)
478 {
479         g_warning ("You must implement %s", __FUNCTION__);
480         g_return_val_if_reached (MODEST_CONNECTED_VIA_ANY);
481 }