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