d6e2a31143d0170a03a84b9d2659396e9fd0efa2
[modest] / src / gnome / modest-gnome-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 <glib/gi18n.h>
35 #include <gtk/gtkbox.h>
36 #include <gtk/gtkvbox.h>
37 #include <gtk/gtknotebook.h>
38 #include <gtk/gtklabel.h>
39 #include <gtk/gtkcheckbutton.h>
40 #include <gtk/gtkhseparator.h>
41 #include <gtk/gtktable.h>
42 #include <gtk/gtkspinbutton.h>
43 #include "widgets/modest-global-settings-dialog-priv.h"
44 #include "widgets/modest-combo-box.h"
45 #include "gnome/modest-gnome-global-settings-dialog.h"
46 #include "widgets/modest-ui-constants.h"
47
48
49 /* include other impl specific header files */
50
51 /* 'private'/'protected' functions */
52 static void modest_gnome_global_settings_dialog_class_init (ModestGnomeGlobalSettingsDialogClass *klass);
53 static void modest_gnome_global_settings_dialog_init       (ModestGnomeGlobalSettingsDialog *obj);
54 static void modest_gnome_global_settings_dialog_finalize   (GObject *obj);
55
56 /* list my signals  */
57 enum {
58         /* MY_SIGNAL_1, */
59         /* MY_SIGNAL_2, */
60         LAST_SIGNAL
61 };
62
63 static GtkWidget* create_updating_page  (ModestGnomeGlobalSettingsDialog *self);
64 static GtkWidget* create_composing_page (ModestGnomeGlobalSettingsDialog *self);
65 static ModestConnectedVia current_connection (void);
66
67 typedef struct _ModestGnomeGlobalSettingsDialogPrivate ModestGnomeGlobalSettingsDialogPrivate;
68 struct _ModestGnomeGlobalSettingsDialogPrivate {
69 };
70 #define MODEST_GNOME_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
71                                                            MODEST_TYPE_GNOME_GLOBAL_SETTINGS_DIALOG, \
72                                                            ModestGnomeGlobalSettingsDialogPrivate))
73 /* globals */
74 static GtkDialogClass *parent_class = NULL;
75
76 /* uncomment the following if you have defined any signals */
77 /* static guint signals[LAST_SIGNAL] = {0}; */
78
79 GType
80 modest_gnome_global_settings_dialog_get_type (void)
81 {
82         static GType my_type = 0;
83         if (!my_type) {
84                 static const GTypeInfo my_info = {
85                         sizeof(ModestGnomeGlobalSettingsDialogClass),
86                         NULL,           /* base init */
87                         NULL,           /* base finalize */
88                         (GClassInitFunc) modest_gnome_global_settings_dialog_class_init,
89                         NULL,           /* class finalize */
90                         NULL,           /* class data */
91                         sizeof(ModestGnomeGlobalSettingsDialog),
92                         1,              /* n_preallocs */
93                         (GInstanceInitFunc) modest_gnome_global_settings_dialog_init,
94                         NULL
95                 };
96                 my_type = g_type_register_static (MODEST_TYPE_GLOBAL_SETTINGS_DIALOG,
97                                                   "ModestGnomeGlobalSettingsDialog",
98                                                   &my_info, 0);
99         }
100         return my_type;
101 }
102
103 static void
104 modest_gnome_global_settings_dialog_class_init (ModestGnomeGlobalSettingsDialogClass *klass)
105 {
106         GObjectClass *gobject_class;
107         gobject_class = (GObjectClass*) klass;
108
109         parent_class            = g_type_class_peek_parent (klass);
110         gobject_class->finalize = modest_gnome_global_settings_dialog_finalize;
111
112         g_type_class_add_private (gobject_class, sizeof(ModestGnomeGlobalSettingsDialogPrivate));
113
114         MODEST_GLOBAL_SETTINGS_DIALOG_CLASS (klass)->current_connection_func = current_connection;
115 }
116
117 static void
118 modest_gnome_global_settings_dialog_init (ModestGnomeGlobalSettingsDialog *self)
119 {
120         ModestGlobalSettingsDialogPrivate *ppriv;
121 /*      GdkGeometry *geometry; */
122
123         ppriv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
124
125         gtk_dialog_set_has_separator (GTK_DIALOG (self), FALSE);
126
127         ppriv->updating_page = create_updating_page (self);
128         ppriv->composing_page = create_composing_page (self);
129     
130         /* Add the notebook pages: */
131         gtk_notebook_append_page (GTK_NOTEBOOK (ppriv->notebook), ppriv->updating_page, 
132                 gtk_label_new (_("mcen_ti_options_updating")));
133         gtk_notebook_append_page (GTK_NOTEBOOK (ppriv->notebook), ppriv->composing_page, 
134                 gtk_label_new (_("mcen_ti_options_composing")));
135                 
136         gtk_container_add (GTK_CONTAINER (GTK_DIALOG (self)->vbox), ppriv->notebook);
137         gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (self)->vbox), 12);
138         gtk_container_set_border_width (GTK_CONTAINER (self), 12);
139         gtk_window_set_default_size (GTK_WINDOW (self), 480, -1);
140
141         gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (self)->action_area), 0);
142
143         /* Add the buttons: */
144         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
145         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_SAVE, GTK_RESPONSE_OK);
146     
147         gtk_widget_show_all (ppriv->notebook);
148 }
149
150 static void
151 modest_gnome_global_settings_dialog_finalize (GObject *obj)
152 {
153 /*      free/unref instance resources here */
154         G_OBJECT_CLASS(parent_class)->finalize (obj);
155 }
156
157 GtkWidget*
158 modest_gnome_global_settings_dialog_new (void)
159 {
160         GtkWidget *self = GTK_WIDGET(g_object_new(MODEST_TYPE_GNOME_GLOBAL_SETTINGS_DIALOG, NULL));
161
162         /* Load settings */
163         modest_gnome_global_settings_dialog_load_settings (MODEST_GLOBAL_SETTINGS_DIALOG (self));
164
165         return self;
166 }
167
168
169 /* 
170  * Adds the two widgets to a new row in the table
171  */
172 static void
173 add_to_table (GtkTable *table,
174               GtkWidget *left,
175               GtkWidget *right)
176 {
177         guint n_rows = 0;
178
179         g_object_get (G_OBJECT (table), "n-rows", &n_rows,NULL);
180
181         /* Attach label and value */
182         gtk_table_attach (table, 
183                           left, 0, 1, 
184                           n_rows, n_rows + 1, 
185                           GTK_FILL, 
186                           GTK_FILL, 
187                           0, 0);
188         gtk_table_attach (table, 
189                           right, 1, 2, 
190                           n_rows, n_rows + 1, 
191                           GTK_EXPAND | GTK_FILL, 
192                           GTK_FILL, 
193                           0, 0);
194 }
195
196 /* 
197  * We need this because the translations are comming without ":" 
198  */
199 static GtkWidget *
200 create_label (const gchar *text)
201 {
202         gchar *label_name;
203         GtkWidget *label;
204
205         label_name = g_strdup_printf ("%s:", text);
206         label = gtk_label_new (label_name);
207         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
208         g_free (label_name);
209
210         return label;
211 }
212
213 /*
214  * Creates the updating page
215  */
216 static GtkWidget*
217 create_updating_page (ModestGnomeGlobalSettingsDialog *self)
218 {
219         GtkWidget *vbox, *table_update, *table_limit;
220         GtkWidget *label;
221         ModestGlobalSettingsDialogPrivate *ppriv;
222
223         ppriv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
224
225         vbox = gtk_vbox_new (FALSE, 0);
226         table_update = gtk_table_new (3, 2, FALSE);
227         table_limit = gtk_table_new (2, 2, FALSE);
228         /* FIXME: set proper values (HIG) */
229         gtk_table_set_row_spacings (GTK_TABLE (table_update), 3);
230         gtk_table_set_col_spacings (GTK_TABLE (table_update), 12);
231         gtk_table_set_row_spacings (GTK_TABLE (table_limit), 3);
232         gtk_table_set_col_spacings (GTK_TABLE (table_limit), 12);
233
234         /* Autoupdate */
235         label = create_label (_("mcen_fi_options_autoupdate"));
236         ppriv->auto_update = gtk_check_button_new ();
237         add_to_table (GTK_TABLE (table_update), label, ppriv->auto_update);
238
239         /* Connected via */
240         label = create_label (_("mcen_fi_options_connectiontype"));
241
242         /* Note: This ModestPairList* must exist for as long as the combo
243          * that uses it, because the ModestComboBox uses the ID opaquely, 
244          * so it can't know how to manage its memory. */
245         ppriv->connect_via_list = _modest_global_settings_dialog_get_connected_via ();
246         ppriv->connect_via = modest_combo_box_new (ppriv->connect_via_list, g_int_equal);
247
248         add_to_table (GTK_TABLE (table_update), label, ppriv->connect_via);
249
250         /* Update interval */
251         label = create_label (_("mcen_fi_options_updateinterval"));
252
253         /* Note: This ModestPairList* must exist for as long as the combo
254          * that uses it, because the ModestComboBox uses the ID opaquely, 
255          * so it can't know how to manage its memory. */
256         ppriv->update_interval_list = _modest_global_settings_dialog_get_update_interval ();
257         ppriv->update_interval = modest_combo_box_new (ppriv->update_interval_list, g_int_equal);
258
259         add_to_table (GTK_TABLE (table_update), label, ppriv->update_interval);
260
261         /* Add to vbox */
262         gtk_box_pack_start (GTK_BOX (vbox), table_update, FALSE, FALSE, MODEST_MARGIN_HALF);
263
264         /* Separator */
265         gtk_box_pack_start (GTK_BOX (vbox), gtk_hseparator_new (), FALSE, FALSE, MODEST_MARGIN_HALF);
266
267         /* Limits */
268         label = create_label (_("mcen_fi_advsetup_sizelimit"));
269         ppriv->size_limit = gtk_spin_button_new (GTK_ADJUSTMENT (gtk_adjustment_new (1000, 1, 5000, 1, 1, 16)), 
270                                                  1, 0);
271         add_to_table (GTK_TABLE (table_limit), label, ppriv->size_limit);
272
273         label = create_label (_("mcen_fi_options_playsound"));
274         ppriv->play_sound = gtk_check_button_new ();
275         add_to_table (GTK_TABLE (table_limit), label, ppriv->play_sound);
276
277         /* Add to vbox */
278         gtk_box_pack_start (GTK_BOX (vbox), table_limit, FALSE, FALSE, MODEST_MARGIN_HALF);
279         gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
280
281         return vbox;
282 }
283
284 /*
285  * Creates the composing page
286  */
287 static GtkWidget* 
288 create_composing_page (ModestGnomeGlobalSettingsDialog *self)
289 {
290         GtkWidget *vbox, *table;
291         GtkWidget *label;
292
293         vbox = gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
294         table = gtk_table_new (2, 2, FALSE);
295         /* FIXME: set proper values */
296         gtk_table_set_row_spacings (GTK_TABLE (table), 3);
297         gtk_table_set_col_spacings (GTK_TABLE (table), 12);
298
299         /* Update interval */
300         label = create_label (_("mcen_fi_options_messageformat"));
301
302         ModestGlobalSettingsDialogPrivate *ppriv = 
303                 MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
304
305         /* Note: This ModestPairList* must exist for as long as the combo
306          * that uses it, because the ModestComboBox uses the ID opaquely, 
307          * so it can't know how to manage its memory. */
308         ppriv->msg_format_list = _modest_global_settings_dialog_get_msg_formats ();
309         ppriv->msg_format = modest_combo_box_new (ppriv->msg_format_list, g_int_equal);
310
311         add_to_table (GTK_TABLE (table), label, ppriv->msg_format);
312
313         /* Add to vbox */
314         gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
315         gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
316
317         return vbox;
318 }
319
320 static ModestConnectedVia 
321 current_connection (void)
322 {
323         return MODEST_CONNECTED_VIA_ANY;
324 }
325
326 static void
327 modest_gnome_global_settings_dialog_load_settings (ModestGlobalSettingsDialog *self)
328 {
329         ModestConf *conf;
330         gboolean checked;
331         gint combo_id, value;
332         GError *error = NULL;
333         ModestGlobalSettingsDialogPrivate *priv;
334
335         priv = MODEST_GLOBAL_SETTINGS_DIALOG_GET_PRIVATE (self);
336         conf = modest_runtime_get_conf ();
337
338         /* Autoupdate */
339         checked = modest_conf_get_bool (conf, MODEST_CONF_AUTO_UPDATE, &error);
340         if (error) {
341                 g_clear_error (&error);
342                 error = NULL;
343                 checked = FALSE;
344         }
345         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->auto_update), checked);
346         priv->initial_state.auto_update = checked;
347
348         /* Connected by */
349         combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_WHEN_CONNECTED_BY, &error);
350         if (error) {
351                 g_error_free (error);
352                 error = NULL;
353                 combo_id = MODEST_CONNECTED_VIA_WLAN_OR_WIMAX;
354         }
355         modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->connect_via),
356                                         (gpointer) &combo_id);
357         priv->initial_state.connect_via = combo_id;
358
359         /* Update interval */
360         combo_id = modest_conf_get_int (conf, MODEST_CONF_UPDATE_INTERVAL, &error);
361         if (error) {
362                 g_error_free (error);
363                 error = NULL;
364                 combo_id = MODEST_UPDATE_INTERVAL_15_MIN;
365         }
366         modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->update_interval),
367                                         (gpointer) &combo_id);
368         priv->initial_state.update_interval = combo_id;
369
370         /* Size limit */
371         value  = modest_conf_get_int (conf, MODEST_CONF_MSG_SIZE_LIMIT, &error);
372         if (error) {
373                 g_error_free (error);
374                 error = NULL;
375                 value = 1000;
376         }
377         /* It's better to do this in the subclasses, but it's just one
378            line, so we'll leave it here for the moment */
379         gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_limit), value);
380         priv->initial_state.size_limit = value;
381
382         /* Play sound */
383         checked = modest_conf_get_bool (conf, MODEST_CONF_PLAY_SOUND_MSG_ARRIVE, &error);
384         if (error) {
385                 g_error_free (error);
386                 error = NULL;
387                 checked = FALSE;
388         }
389         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->play_sound), checked);
390         priv->initial_state.play_sound = checked;
391
392         /* Msg format */
393         checked = modest_conf_get_bool (conf, MODEST_CONF_PREFER_FORMATTED_TEXT, &error);
394         if (error) {
395                 g_error_free (error);
396                 error = NULL;
397                 combo_id = MODEST_FILE_FORMAT_FORMATTED_TEXT;
398         }
399         combo_id = (checked) ? MODEST_FILE_FORMAT_FORMATTED_TEXT : MODEST_FILE_FORMAT_PLAIN_TEXT;
400         modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->msg_format),
401                                         (gpointer) &combo_id);
402         priv->initial_state.prefer_formatted_text = checked;
403 }