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