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