2007-06-01 Murray Cumming <murrayc@murrayc.com>
[modest] / src / maemo / modest-maemo-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 #ifdef MODEST_HILDON_VERSION_0
35 #include <hildon-widgets/hildon-caption.h>
36 #include <hildon-widgets/hildon-number-editor.h>
37 #include <hildon-widgets/hildon-banner.h>
38 #else
39 #include <hildon/hildon-caption.h>
40 #include <hildon/hildon-number-editor.h>
41 #include <hildon/hildon-banner.h>
42 #endif /*MODEST_HILDON_VERSION_0*/
43
44 #include <glib/gi18n.h>
45 #include <string.h>
46 #include <gtk/gtkbox.h>
47 #include <gtk/gtkvbox.h>
48 #include <gtk/gtknotebook.h>
49 #include <gtk/gtklabel.h>
50 #include <gtk/gtkcheckbutton.h>
51 #include <gtk/gtkhseparator.h>
52 #include "modest-runtime.h"
53 #include "widgets/modest-global-settings-dialog-priv.h"
54 #include "widgets/modest-combo-box.h"
55 #include "maemo/modest-maemo-global-settings-dialog.h"
56 #include "widgets/modest-ui-constants.h"
57 #include <tny-account-store.h>
58 #include <tny-maemo-conic-device.h>
59
60 #define MSG_SIZE_MAX_VAL 5000
61 #define MSG_SIZE_DEF_VAL 1000
62 #define MSG_SIZE_MIN_VAL 1
63
64 /* 'private'/'protected' functions */
65 static void modest_maemo_global_settings_dialog_class_init (ModestMaemoGlobalSettingsDialogClass *klass);
66 static void modest_maemo_global_settings_dialog_init       (ModestMaemoGlobalSettingsDialog *obj);
67 static void modest_maemo_global_settings_dialog_finalize   (GObject *obj);
68
69 static ModestConnectedVia current_connection (void);
70
71 /* list my signals  */
72 enum {
73         /* MY_SIGNAL_1, */
74         /* MY_SIGNAL_2, */
75         LAST_SIGNAL
76 };
77
78 static GtkWidget* create_updating_page   (ModestMaemoGlobalSettingsDialog *self);
79 static GtkWidget* create_composing_page  (ModestMaemoGlobalSettingsDialog *self);
80
81 static gboolean   on_range_error         (HildonNumberEditor *editor, 
82                                           HildonNumberEditorErrorType type,
83                                           gpointer user_data);
84
85 static void       on_auto_update_toggled (GtkToggleButton *togglebutton,
86                                           gpointer user_data);
87
88 typedef struct _ModestMaemoGlobalSettingsDialogPrivate ModestMaemoGlobalSettingsDialogPrivate;
89 struct _ModestMaemoGlobalSettingsDialogPrivate {
90         ModestPairList *connect_via_list;
91 };
92 #define MODEST_MAEMO_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
93                                                            MODEST_TYPE_MAEMO_GLOBAL_SETTINGS_DIALOG, \
94                                                            ModestMaemoGlobalSettingsDialogPrivate))
95 /* globals */
96 static GtkDialogClass *parent_class = NULL;
97
98 /* uncomment the following if you have defined any signals */
99 /* static guint signals[LAST_SIGNAL] = {0}; */
100
101 GType
102 modest_maemo_global_settings_dialog_get_type (void)
103 {
104         static GType my_type = 0;
105         if (!my_type) {
106                 static const GTypeInfo my_info = {
107                         sizeof(ModestMaemoGlobalSettingsDialogClass),
108                         NULL,           /* base init */
109                         NULL,           /* base finalize */
110                         (GClassInitFunc) modest_maemo_global_settings_dialog_class_init,
111                         NULL,           /* class finalize */
112                         NULL,           /* class data */
113                         sizeof(ModestMaemoGlobalSettingsDialog),
114                         1,              /* n_preallocs */
115                         (GInstanceInitFunc) modest_maemo_global_settings_dialog_init,
116                         NULL
117                 };
118                 my_type = g_type_register_static (MODEST_TYPE_GLOBAL_SETTINGS_DIALOG,
119                                                   "ModestMaemoGlobalSettingsDialog",
120                                                   &my_info, 0);
121         }
122         return my_type;
123 }
124
125 static void
126 modest_maemo_global_settings_dialog_class_init (ModestMaemoGlobalSettingsDialogClass *klass)
127 {
128         GObjectClass *gobject_class;
129         gobject_class = (GObjectClass*) klass;
130
131         parent_class            = g_type_class_peek_parent (klass);
132         gobject_class->finalize = modest_maemo_global_settings_dialog_finalize;
133
134         g_type_class_add_private (gobject_class, sizeof(ModestMaemoGlobalSettingsDialogPrivate));
135
136         MODEST_GLOBAL_SETTINGS_DIALOG_CLASS (klass)->current_connection_func = current_connection;
137 }
138
139 static void
140 modest_maemo_global_settings_dialog_init (ModestMaemoGlobalSettingsDialog *self)
141 {
142         ModestGlobalSettingsDialogPrivate *ppriv;
143
144         ppriv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
145
146         ppriv->updating_page = create_updating_page (self);
147         ppriv->composing_page = create_composing_page (self);
148     
149         /* Add the notebook pages: */
150         gtk_notebook_append_page (GTK_NOTEBOOK (ppriv->notebook), ppriv->updating_page, 
151                 gtk_label_new (_("mcen_ti_options_updating")));
152         gtk_notebook_append_page (GTK_NOTEBOOK (ppriv->notebook), ppriv->composing_page, 
153                 gtk_label_new (_("mcen_ti_options_composing")));
154                 
155         gtk_container_add (GTK_CONTAINER (GTK_DIALOG (self)->vbox), ppriv->notebook);
156         gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (self)->vbox), MODEST_MARGIN_HALF);
157
158         /* Load current config */
159         _modest_global_settings_dialog_load_conf (MODEST_GLOBAL_SETTINGS_DIALOG (self));
160         gtk_widget_show_all (ppriv->notebook);
161 }
162
163 static void
164 modest_maemo_global_settings_dialog_finalize (GObject *obj)
165 {
166 /*      free/unref instance resources here */
167         G_OBJECT_CLASS(parent_class)->finalize (obj);
168 }
169
170 GtkWidget*
171 modest_maemo_global_settings_dialog_new (void)
172 {
173         return GTK_WIDGET(g_object_new(MODEST_TYPE_MAEMO_GLOBAL_SETTINGS_DIALOG, NULL));
174 }
175
176
177 /*
178  * Creates the updating page
179  */
180 static GtkWidget*
181 create_updating_page (ModestMaemoGlobalSettingsDialog *self)
182 {
183         GtkWidget *vbox, *vbox_update, *vbox_limit, *caption;
184         GtkSizeGroup *size_group;
185         ModestGlobalSettingsDialogPrivate *ppriv;
186
187         ppriv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
188
189         vbox = gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
190
191         vbox_update = gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
192         size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
193
194         /* Auto update */
195         ppriv->auto_update = gtk_check_button_new ();
196         caption = hildon_caption_new (size_group, 
197                                       _("mcen_fi_options_autoupdate"), 
198                                       ppriv->auto_update, 
199                                       NULL, 
200                                       HILDON_CAPTION_MANDATORY);
201         gtk_box_pack_start (GTK_BOX (vbox_update), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
202         g_signal_connect (ppriv->auto_update, "toggled", G_CALLBACK (on_auto_update_toggled), self);
203
204         /* Connected via */
205
206         /* Note: This ModestPairList* must exist for as long as the combo
207          * that uses it, because the ModestComboBox uses the ID opaquely, 
208          * so it can't know how to manage its memory. */ 
209         ppriv->connect_via_list = _modest_global_settings_dialog_get_connected_via ();
210         ppriv->connect_via = modest_combo_box_new (ppriv->connect_via_list, g_int_equal);
211
212         caption = hildon_caption_new (size_group, 
213                                       _("mcen_fi_options_connectiontype"),
214                                       ppriv->connect_via, 
215                                       NULL, 
216                                       HILDON_CAPTION_MANDATORY);
217         gtk_box_pack_start (GTK_BOX (vbox_update), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
218
219         /* Update interval */
220
221         /* Note: This ModestPairList* must exist for as long as the combo
222          * that uses it, because the ModestComboBox uses the ID opaquely, 
223          * so it can't know how to manage its memory. */ 
224         ppriv->update_interval_list = _modest_global_settings_dialog_get_update_interval ();
225         ppriv->update_interval = modest_combo_box_new (ppriv->update_interval_list, g_int_equal);
226
227         caption = hildon_caption_new (size_group, 
228                                       _("mcen_fi_options_updateinterval"),
229                                       ppriv->update_interval, 
230                                       NULL, 
231                                       HILDON_CAPTION_MANDATORY);
232         gtk_box_pack_start (GTK_BOX (vbox_update), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
233
234         /* Add to vbox */
235         gtk_box_pack_start (GTK_BOX (vbox), vbox_update, FALSE, FALSE, MODEST_MARGIN_HALF);
236
237         /* Separator */
238         gtk_box_pack_start (GTK_BOX (vbox), gtk_hseparator_new (), FALSE, FALSE, MODEST_MARGIN_HALF);
239
240         /* Limits */
241         vbox_limit = gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
242         size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
243
244         /* Size limit */
245         ppriv->size_limit = hildon_number_editor_new (MSG_SIZE_MIN_VAL, MSG_SIZE_MAX_VAL);;
246         hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (ppriv->size_limit), MSG_SIZE_DEF_VAL);;
247         g_signal_connect (ppriv->size_limit, "range_error", G_CALLBACK (on_range_error), self);
248         caption = hildon_caption_new (size_group, 
249                                       _("mcen_fi_advsetup_sizelimit"), 
250                                       ppriv->size_limit, 
251                                       NULL, 
252                                       HILDON_CAPTION_MANDATORY);
253         gtk_box_pack_start (GTK_BOX (vbox_limit), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
254
255         /* Play sound */
256         ppriv->play_sound = gtk_check_button_new ();
257         caption = hildon_caption_new (size_group, 
258                                       _("mcen_fi_options_playsound"), 
259                                       ppriv->play_sound, 
260                                       NULL, 
261                                       HILDON_CAPTION_MANDATORY);
262         gtk_box_pack_start (GTK_BOX (vbox_limit), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
263
264         /* Add to vbox */
265         gtk_box_pack_start (GTK_BOX (vbox), vbox_limit, FALSE, FALSE, MODEST_MARGIN_HALF);
266
267         return vbox;
268 }
269
270 /*
271  * Creates the composing page
272  */
273 static GtkWidget* 
274 create_composing_page (ModestMaemoGlobalSettingsDialog *self)
275 {
276         GtkWidget *vbox;
277         GtkSizeGroup *size_group;
278         ModestGlobalSettingsDialogPrivate *ppriv;
279         GtkWidget *caption;
280
281         ppriv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
282         size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
283         vbox = gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
284
285         /* Update interval */
286
287         /* Note: This ModestPairList* must exist for as long as the combo
288          * that uses it, because the ModestComboBox uses the ID opaquely, 
289          * so it can't know how to manage its memory. */ 
290         ppriv->msg_format_list = _modest_global_settings_dialog_get_msg_formats ();
291         ppriv->msg_format = modest_combo_box_new (ppriv->msg_format_list, g_int_equal);
292
293         caption = hildon_caption_new (size_group, 
294                                       _("mcen_fi_options_messageformat"),
295                                       ppriv->msg_format, 
296                                       NULL, 
297                                       HILDON_CAPTION_MANDATORY);
298         gtk_box_pack_start (GTK_BOX (vbox), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
299
300         /* Reply */
301         ppriv->include_reply = gtk_check_button_new ();
302         caption = hildon_caption_new (size_group, 
303                                       _("mcen_va_options_include_original_inreply"), 
304                                       ppriv->include_reply, 
305                                       NULL, 
306                                       HILDON_CAPTION_MANDATORY);
307         gtk_box_pack_start (GTK_BOX (vbox), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
308
309         return vbox;
310 }
311
312 static void
313 on_auto_update_toggled (GtkToggleButton *togglebutton,
314                         gpointer user_data)
315 {
316         ModestGlobalSettingsDialogPrivate *ppriv;
317         GtkWidget *caption1, *caption2;
318
319         ppriv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (user_data);
320         caption1 = gtk_widget_get_ancestor (ppriv->connect_via, HILDON_TYPE_CAPTION);
321         caption2 = gtk_widget_get_ancestor (ppriv->update_interval, HILDON_TYPE_CAPTION);
322
323         if (gtk_toggle_button_get_active (togglebutton)) {
324                 gtk_widget_set_sensitive (caption1, TRUE);
325                 gtk_widget_set_sensitive (caption2, TRUE);
326         } else {
327                 gtk_widget_set_sensitive (caption1, FALSE);
328                 gtk_widget_set_sensitive (caption2, FALSE);
329         }
330 }
331
332 static gboolean
333 on_range_error (HildonNumberEditor *editor, 
334                 HildonNumberEditorErrorType type,
335                 gpointer user_data)
336 {
337         gchar *msg;
338         gint new_val;
339
340         switch (type) {
341 #ifdef MODEST_HILDON_VERSION_0
342         case MAXIMUM_VALUE_EXCEED:
343 #else
344         case HILDON_NUMBER_EDITOR_ERROR_MAXIMUM_VALUE_EXCEED:
345 #endif
346                 msg = g_strdup_printf (_("ckct_ib_maximum_value"), MSG_SIZE_MAX_VAL);
347                 new_val = MSG_SIZE_MAX_VAL;
348                 break;
349 #ifdef MODEST_HILDON_VERSION_0
350         case MINIMUM_VALUE_EXCEED:
351 #else
352         case HILDON_NUMBER_EDITOR_ERROR_MINIMUM_VALUE_EXCEED:
353 #endif
354                 msg = g_strdup_printf (_("ckct_ib_minimum_value"), MSG_SIZE_MIN_VAL);
355                 new_val = MSG_SIZE_MIN_VAL;
356                 break;
357 #ifdef MODEST_HILDON_VERSION_0
358         case ERRONEOUS_VALUE:
359 #else
360         case HILDON_NUMBER_EDITOR_ERROR_ERRONEOUS_VALUE:
361 #endif
362                 msg = g_strdup_printf (_("ckct_ib_set_a_value_within_range"), 
363                                        MSG_SIZE_MIN_VAL, 
364                                        MSG_SIZE_MAX_VAL);
365                 /* FIXME: use the previous */
366                 new_val = MSG_SIZE_DEF_VAL;
367                 break;
368         default:
369                 g_return_val_if_reached (FALSE);
370         }
371
372         /* Restore value */
373         hildon_number_editor_set_value (editor, new_val);
374
375         /* Show error */
376         hildon_banner_show_information (GTK_WIDGET (user_data), NULL, msg);
377
378         /* Free */
379         g_free (msg);
380
381         return TRUE;
382 }
383
384 static ModestConnectedVia
385 current_connection (void)
386 {
387         TnyAccountStore *account_store;
388         TnyDevice *device;
389         ModestConnectedVia retval = MODEST_CONNECTED_VIA_ANY;
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                 return MODEST_CONNECTED_VIA_ANY;
396
397         /* Get iap id */
398         const gchar *iap_id = tny_maemo_conic_device_get_current_iap_id (TNY_MAEMO_CONIC_DEVICE (device));
399         if (iap_id) {
400                 ConIcIap *iap = tny_maemo_conic_device_get_iap (
401                         TNY_MAEMO_CONIC_DEVICE (device), iap_id);
402                 const gchar *bearer_type = con_ic_iap_get_bearer_type (iap);
403                         
404                 if (!strcmp (bearer_type, CON_IC_BEARER_WLAN_INFRA) ||
405                     !strcmp (bearer_type, CON_IC_BEARER_WLAN_ADHOC))
406                         retval = MODEST_CONNECTED_VIA_WLAN;
407                 else
408                         retval = MODEST_CONNECTED_VIA_ANY;
409         
410                 g_object_unref (iap);
411         }
412         g_object_unref (device);
413
414         return retval;
415 }