ca02740c69dca5d962ab67524814e6465c690db3
[modest] / src / hildon2 / modest-hildon2-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 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif /*HAVE_CONFIG_H*/
33
34 #include <modest-hildon-includes.h>
35 #include <modest-scrollable.h>
36 #include <modest-maemo-utils.h>
37
38 #include <glib/gi18n.h>
39 #include <string.h>
40 #include <gtk/gtkbox.h>
41 #include <gtk/gtkvbox.h>
42 #include <gtk/gtknotebook.h>
43 #include <gtk/gtklabel.h>
44 #include <gtk/gtkcheckbutton.h>
45 #include "modest-runtime.h"
46 #include "widgets/modest-global-settings-dialog-priv.h"
47 #include "modest-hildon2-global-settings-dialog.h"
48 #include "widgets/modest-ui-constants.h"
49 #include "modest-text-utils.h"
50 #include "modest-defs.h"
51 #include <tny-account-store.h>
52 #include <modest-account-mgr-helpers.h>
53
54
55 #define MSG_SIZE_MAX_VAL 5000
56 #define MSG_SIZE_DEF_VAL 1000
57 #define MSG_SIZE_MIN_VAL 1
58
59 #define DEFAULT_FOCUS_WIDGET "default-focus-widget"
60
61 /* 'private'/'protected' functions */
62 static void modest_hildon2_global_settings_dialog_class_init (ModestHildon2GlobalSettingsDialogClass *klass);
63 static void modest_hildon2_global_settings_dialog_init       (ModestHildon2GlobalSettingsDialog *obj);
64 static void modest_hildon2_global_settings_dialog_finalize   (GObject *obj);
65
66 static ModestConnectedVia current_connection (void);
67
68 static GtkWidget* create_updating_page   (ModestHildon2GlobalSettingsDialog *self);
69
70 static void       on_auto_update_clicked (GtkButton *button,
71                                           gpointer user_data);
72 static void       update_sensitive       (ModestGlobalSettingsDialog *dialog);
73 static ModestPairList * get_accounts_list (void);
74
75 static void modest_hildon2_global_settings_dialog_load_settings (ModestGlobalSettingsDialog *self);
76
77 typedef struct _ModestHildon2GlobalSettingsDialogPrivate ModestHildon2GlobalSettingsDialogPrivate;
78 struct _ModestHildon2GlobalSettingsDialogPrivate {
79         ModestPairList *connect_via_list;
80 };
81 #define MODEST_HILDON2_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
82                                                            MODEST_TYPE_HILDON2_GLOBAL_SETTINGS_DIALOG, \
83                                                            ModestHildon2GlobalSettingsDialogPrivate))
84 /* globals */
85 static GtkDialogClass *parent_class = NULL;
86
87 GType
88 modest_hildon2_global_settings_dialog_get_type (void)
89 {
90         static GType my_type = 0;
91         if (!my_type) {
92                 static const GTypeInfo my_info = {
93                         sizeof(ModestHildon2GlobalSettingsDialogClass),
94                         NULL,           /* base init */
95                         NULL,           /* base finalize */
96                         (GClassInitFunc) modest_hildon2_global_settings_dialog_class_init,
97                         NULL,           /* class finalize */
98                         NULL,           /* class data */
99                         sizeof(ModestHildon2GlobalSettingsDialog),
100                         1,              /* n_preallocs */
101                         (GInstanceInitFunc) modest_hildon2_global_settings_dialog_init,
102                         NULL
103                 };
104                 my_type = g_type_register_static (MODEST_TYPE_GLOBAL_SETTINGS_DIALOG,
105                                                   "ModestHildon2GlobalSettingsDialog",
106                                                   &my_info, 0);
107         }
108         return my_type;
109 }
110
111 static void
112 modest_hildon2_global_settings_dialog_class_init (ModestHildon2GlobalSettingsDialogClass *klass)
113 {
114         GObjectClass *gobject_class;
115         gobject_class = (GObjectClass*) klass;
116
117         parent_class            = g_type_class_peek_parent (klass);
118         gobject_class->finalize = modest_hildon2_global_settings_dialog_finalize;
119
120         g_type_class_add_private (gobject_class, sizeof(ModestHildon2GlobalSettingsDialogPrivate));
121
122         MODEST_GLOBAL_SETTINGS_DIALOG_CLASS (klass)->current_connection_func = current_connection;
123 }
124
125 typedef struct {
126         ModestHildon2GlobalSettingsDialog *dia;
127         GtkWidget *focus_widget;
128 } SwitchPageHelper;
129
130
131 static void
132 modest_hildon2_global_settings_dialog_init (ModestHildon2GlobalSettingsDialog *self)
133 {
134         ModestHildon2GlobalSettingsDialogPrivate *priv;
135         ModestGlobalSettingsDialogPrivate *ppriv;
136         GtkWidget *align;
137         GtkWidget *top_vbox;
138
139         priv  = MODEST_HILDON2_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
140         ppriv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
141
142         ppriv->updating_page = create_updating_page (self);
143         top_vbox = gtk_vbox_new (FALSE, 0);
144         align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
145         gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, 0);
146
147         /* Add the buttons: */
148         gtk_dialog_add_button (GTK_DIALOG (self), _HL("wdgt_bd_save"), GTK_RESPONSE_OK);
149
150         /* Set the default focusable widgets */
151         g_object_set_data (G_OBJECT(ppriv->updating_page), DEFAULT_FOCUS_WIDGET,
152                            (gpointer)ppriv->auto_update);
153
154         gtk_container_add (GTK_CONTAINER (top_vbox), ppriv->updating_page);
155         gtk_container_add (GTK_CONTAINER (align), top_vbox);
156         gtk_container_add (GTK_CONTAINER (GTK_DIALOG (self)->vbox), align);
157         gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (self)->vbox), MODEST_MARGIN_HALF);
158         gtk_window_set_default_size (GTK_WINDOW (self), -1, MODEST_DIALOG_WINDOW_MAX_HEIGHT);
159
160         gtk_widget_show (align);
161         gtk_widget_show (top_vbox);
162 }
163
164 static void
165 modest_hildon2_global_settings_dialog_finalize (GObject *obj)
166 {
167         ModestGlobalSettingsDialogPrivate *ppriv;
168         ModestHildon2GlobalSettingsDialogPrivate *priv;
169
170         priv = MODEST_HILDON2_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (obj);
171         ppriv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (obj);
172
173 /*      free/unref instance resources here */
174         G_OBJECT_CLASS(parent_class)->finalize (obj);
175 }
176
177 GtkWidget*
178 modest_hildon2_global_settings_dialog_new (void)
179 {
180         GtkWidget *self = GTK_WIDGET(g_object_new(MODEST_TYPE_HILDON2_GLOBAL_SETTINGS_DIALOG, NULL));
181
182         /* Load settings */
183         modest_hildon2_global_settings_dialog_load_settings (MODEST_GLOBAL_SETTINGS_DIALOG (self));
184
185         return self;
186 }
187
188 /*
189  * Creates the updating page
190  */
191 static GtkWidget*
192 create_updating_page (ModestHildon2GlobalSettingsDialog *self)
193 {
194         GtkWidget *vbox;
195         GtkSizeGroup *title_size_group;
196         GtkSizeGroup *value_size_group;
197         ModestGlobalSettingsDialogPrivate *ppriv;
198         GtkWidget *scrollable, *separator;
199         ModestHildon2GlobalSettingsDialogPrivate *priv;
200
201         priv = MODEST_HILDON2_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
202         ppriv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
203         vbox = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
204
205         title_size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
206         value_size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
207
208         /* Default account selector */
209         ppriv->accounts_list = get_accounts_list ();
210         ppriv->default_account_selector = modest_toolkit_factory_create_selector (modest_runtime_get_toolkit_factory (),
211                                                                                   ppriv->accounts_list,
212                                                                                   g_str_equal);
213         if (ppriv->accounts_list == NULL) {
214                 gtk_widget_set_sensitive (GTK_WIDGET (ppriv->default_account_selector), FALSE);
215         } else {
216                 gchar *default_account;
217
218                 default_account = modest_account_mgr_get_default_account (
219                         modest_runtime_get_account_mgr ());
220                 if (default_account) {
221                         modest_selector_set_active_id (ppriv->default_account_selector,
222                                                        default_account);
223                         ppriv->initial_state.default_account = default_account;
224                 }
225         }
226         if (GTK_IS_COMBO_BOX (ppriv->default_account_selector)) {
227                 GtkWidget *caption;
228
229                 caption = modest_maemo_utils_create_vcaptioned (title_size_group,
230                                                                 _("mcen_ti_default_account"), FALSE,
231                                                                 ppriv->default_account_selector);
232                 gtk_widget_show (caption);
233                 gtk_box_pack_start (GTK_BOX (vbox), caption,
234                                     FALSE, FALSE, 0);
235         } else {
236                 modest_maemo_utils_set_vbutton_layout (title_size_group,
237                                                        _("mcen_ti_default_account"),
238                                                        ppriv->default_account_selector);
239                 gtk_box_pack_start (GTK_BOX (vbox), ppriv->default_account_selector,
240                                     FALSE, FALSE, 0);
241         }
242
243         /* Message format */
244         /* Note: This ModestPairList* must exist for as long as the picker
245          * that uses it, because the ModestSelectorPicker uses the ID opaquely,
246          * so it can't know how to manage its memory. */
247         ppriv->msg_format_list = _modest_global_settings_dialog_get_msg_formats ();
248         ppriv->msg_format = modest_toolkit_factory_create_selector (modest_runtime_get_toolkit_factory (),
249                                                                     ppriv->msg_format_list, g_int_equal);
250         if (GTK_IS_COMBO_BOX (ppriv->msg_format)) {
251                 GtkWidget *caption;
252                 caption = modest_maemo_utils_create_vcaptioned (title_size_group,
253                                                                 _("mcen_fi_options_messageformat"), FALSE,
254                                                                 ppriv->msg_format);
255                 gtk_widget_show (caption);
256                 gtk_box_pack_start (GTK_BOX (vbox), caption, FALSE, FALSE, 0);
257         } else {
258                 modest_maemo_utils_set_vbutton_layout (title_size_group,
259                                                        _("mcen_fi_options_messageformat"),
260                                                        ppriv->msg_format);
261                 gtk_box_pack_start (GTK_BOX (vbox), ppriv->msg_format, FALSE, FALSE, 0);
262         }
263
264
265         /* Separator label */
266         separator = gtk_label_new (_("mcen_ti_updating"));
267         gtk_label_set_justify ((GtkLabel *) separator, GTK_JUSTIFY_CENTER);
268         gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, FALSE, MODEST_MARGIN_DEFAULT);
269
270         /* Auto update */
271         ppriv->auto_update = modest_toolkit_factory_create_check_button (modest_runtime_get_toolkit_factory (),
272                                                                          _("mcen_fi_options_autoupdate"));
273         gtk_box_pack_start (GTK_BOX (vbox), ppriv->auto_update, FALSE, FALSE, 0);
274         g_signal_connect (ppriv->auto_update, "clicked", G_CALLBACK (on_auto_update_clicked), self);
275
276         /* Connected via */
277
278         /* Note: This ModestPairList* must exist for as long as the picker
279          * that uses it, because the ModestSelectorPicker uses the ID opaquely, 
280          * so it can't know how to manage its memory. */ 
281         ppriv->connect_via_list = _modest_global_settings_dialog_get_connected_via ();
282         ppriv->connect_via = modest_toolkit_factory_create_selector (modest_runtime_get_toolkit_factory (),
283                                                                      ppriv->connect_via_list, g_int_equal);
284         if (GTK_IS_COMBO_BOX (ppriv->connect_via)) {
285                 GtkWidget *caption;
286                 caption = modest_maemo_utils_create_vcaptioned (title_size_group,
287                                                                 _("mcen_fi_options_connectiontype"), FALSE,
288                                                                 ppriv->connect_via);
289                 gtk_widget_show (caption);
290                 gtk_box_pack_start (GTK_BOX (vbox), caption, FALSE, FALSE, 0);
291         } else {
292                 modest_maemo_utils_set_vbutton_layout (title_size_group, 
293                                                        _("mcen_fi_options_connectiontype"),
294                                                        ppriv->connect_via);
295                 gtk_box_pack_start (GTK_BOX (vbox), ppriv->connect_via, FALSE, FALSE, 0);
296         }
297
298         /* Update interval */
299
300         /* Note: This ModestPairList* must exist for as long as the picker
301          * that uses it, because the ModestSelectorPicker uses the ID opaquely, 
302          * so it can't know how to manage its memory. */ 
303         ppriv->update_interval_list = _modest_global_settings_dialog_get_update_interval ();
304         ppriv->update_interval = modest_toolkit_factory_create_selector (modest_runtime_get_toolkit_factory (),
305                                                                          ppriv->update_interval_list, g_int_equal);
306         if (GTK_IS_COMBO_BOX (ppriv->update_interval)) {
307                 GtkWidget *caption;
308                 caption = modest_maemo_utils_create_vcaptioned (title_size_group,
309                                                                 _("mcen_fi_options_updateinterval"), FALSE,
310                                                                 ppriv->update_interval);
311                 gtk_widget_show (caption);
312                 gtk_box_pack_start (GTK_BOX (vbox), caption, FALSE, FALSE, 0);
313         } else {
314                 modest_maemo_utils_set_vbutton_layout (title_size_group, 
315                                                        _("mcen_fi_options_updateinterval"), 
316                                                        ppriv->update_interval);
317                 gtk_box_pack_start (GTK_BOX (vbox), ppriv->update_interval, FALSE, FALSE, 0);
318         }
319
320         scrollable = modest_toolkit_factory_create_scrollable (modest_runtime_get_toolkit_factory ());
321
322         modest_scrollable_add_with_viewport (MODEST_SCROLLABLE (scrollable), vbox);
323         gtk_widget_show (vbox);
324         gtk_widget_show (scrollable);
325
326         g_object_unref (title_size_group);
327         g_object_unref (value_size_group);
328
329         return scrollable;
330 }
331
332
333 static void
334 update_sensitive (ModestGlobalSettingsDialog *dialog)
335 {
336         ModestGlobalSettingsDialogPrivate *ppriv;
337
338         g_return_if_fail (MODEST_IS_GLOBAL_SETTINGS_DIALOG (dialog));
339         ppriv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (dialog);
340
341         if (modest_togglable_get_active (ppriv->auto_update)) {
342                 gtk_widget_set_sensitive (ppriv->connect_via, TRUE);
343                 gtk_widget_set_sensitive (ppriv->update_interval, TRUE);
344         } else {
345                 gtk_widget_set_sensitive (ppriv->connect_via, FALSE);
346                 gtk_widget_set_sensitive (ppriv->update_interval, FALSE);
347         }
348 }
349
350 static void
351 on_auto_update_clicked (GtkButton *button,
352                         gpointer user_data)
353 {
354         g_return_if_fail (MODEST_IS_GLOBAL_SETTINGS_DIALOG (user_data));
355         update_sensitive ((ModestGlobalSettingsDialog *) user_data);
356 }
357
358 static ModestConnectedVia
359 current_connection (void)
360 {
361         return modest_platform_get_current_connection ();
362 }
363
364 static gint
365 order_by_acc_name (gconstpointer a,
366                    gconstpointer b)
367 {
368         ModestPair *pair_a, *pair_b;
369
370         pair_a = (ModestPair *) a;
371         pair_b = (ModestPair *) b;
372
373         if (pair_a->second && pair_b->second) {
374                 gint compare = g_utf8_collate ((gchar *) pair_a->second,
375                                                (gchar *) pair_b->second);
376                 if (compare > 0)
377                         compare = -1;
378                 else if (compare < 0)
379                         compare = 1;
380
381                 return compare;
382         } else {
383                 return 0;
384         }
385 }
386
387 static ModestPairList *
388 get_accounts_list (void)
389 {
390         GSList *list = NULL;
391         GSList *cursor, *account_names;
392         ModestAccountMgr *account_mgr;
393
394         account_mgr = modest_runtime_get_account_mgr ();
395
396         cursor = account_names = modest_account_mgr_account_names (account_mgr, TRUE /*only enabled*/);
397         while (cursor) {
398                 gchar *account_name;
399                 ModestAccountSettings *settings;
400                 ModestServerAccountSettings *store_settings;
401
402                 account_name = (gchar*)cursor->data;
403
404                 settings = modest_account_mgr_load_account_settings (account_mgr, account_name);
405                 if (!settings) {
406                         g_printerr ("modest: failed to get account data for %s\n", account_name);
407                         cursor = cursor->next;
408                         continue;
409                 }
410                 store_settings = modest_account_settings_get_store_settings (settings);
411
412                 /* don't display accounts without stores */
413                 if (modest_server_account_settings_get_account_name (store_settings) != NULL) {
414
415                         if (modest_account_settings_get_enabled (settings)) {
416                                 ModestPair *pair;
417
418                                 pair = modest_pair_new (
419                                         g_strdup (account_name),
420                                         g_strdup (modest_account_settings_get_display_name (settings)),
421                                         FALSE);
422                                 list = g_slist_insert_sorted (list, pair, order_by_acc_name);
423                         }
424                 }
425
426                 g_object_unref (store_settings);
427                 g_object_unref (settings);
428                 cursor = cursor->next;
429         }
430
431         return (ModestPairList *) g_slist_reverse (list);
432 }
433
434
435 static void
436 modest_hildon2_global_settings_dialog_load_settings (ModestGlobalSettingsDialog *self)
437 {
438         ModestConf *conf;
439         gboolean checked;
440         gint combo_id;
441         GError *error = NULL;
442         ModestGlobalSettingsDialogPrivate *ppriv;
443
444         ppriv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
445         conf = modest_runtime_get_conf ();
446
447         /* Autoupdate */
448         checked = modest_conf_get_bool (conf, MODEST_CONF_AUTO_UPDATE, &error);
449         if (error) {
450                 g_clear_error (&error);
451                 error = NULL;
452                 checked = FALSE;
453         }
454         modest_togglable_set_active (ppriv->auto_update, checked);
455         ppriv->initial_state.auto_update = checked;
456
457         /* Connected by */
458         combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, &error);
459         if (error) {
460                 g_error_free (error);
461                 error = NULL;
462                 combo_id = MODEST_CONNECTED_VIA_WLAN_OR_WIMAX;
463         }
464         modest_selector_set_active_id (ppriv->connect_via,
465                                        (gpointer) &combo_id);
466         ppriv->initial_state.connect_via = combo_id;
467
468         /* Update interval */
469         combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_INTERVAL, &error);
470         if (error) {
471                 g_error_free (error);
472                 error = NULL;
473                 combo_id = MODEST_UPDATE_INTERVAL_15_MIN;
474         }
475         modest_selector_set_active_id (ppriv->update_interval,
476                                        (gpointer) &combo_id);
477         ppriv->initial_state.update_interval = combo_id;
478
479         /* Play sound */
480         checked = modest_conf_get_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, &error);
481         if (error) {
482                 g_error_free (error);
483                 error = NULL;
484                 checked = FALSE;
485         }
486         ppriv->initial_state.play_sound = checked;
487
488         /* Msg format */
489         checked = modest_conf_get_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, &error);
490         if (error) {
491                 g_error_free (error);
492                 error = NULL;
493                 combo_id = MODEST_FILE_FORMAT_FORMATTED_TEXT;
494         }
495         combo_id = (checked) ? MODEST_FILE_FORMAT_FORMATTED_TEXT : MODEST_FILE_FORMAT_PLAIN_TEXT;
496         modest_selector_set_active_id (ppriv->msg_format,
497                                        (gpointer) &combo_id);
498         ppriv->initial_state.prefer_formatted_text = checked;
499
500         /* force update of sensitiveness */
501         update_sensitive (MODEST_GLOBAL_SETTINGS_DIALOG (self));
502 }