2007-04-04 Murray Cumming <murrayc@murrayc.com>
[modest] / src / maemo / easysetup / modest-easysetup-serversecurity-combo-box.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  */
5
6 #include "modest-easysetup-serversecurity-combo-box.h"
7 #include <gtk/gtkliststore.h>
8 #include <gtk/gtkcelllayout.h>
9 #include <gtk/gtkcellrenderertext.h>
10 #include <glib/gi18n.h>
11
12 #include <stdlib.h>
13 #include <string.h> /* For memcpy() */
14
15 /* Include config.h so that _() works: */
16 #ifdef HAVE_CONFIG_H
17 #include <config.h>
18 #endif
19
20 G_DEFINE_TYPE (EasysetupServersecurityComboBox, easysetup_serversecurity_combo_box, GTK_TYPE_COMBO_BOX);
21
22 #define SERVERSECURITY_COMBO_BOX_GET_PRIVATE(o) \
23         (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_SERVERSECURITY_COMBO_BOX, EasysetupServersecurityComboBoxPrivate))
24
25 typedef struct _EasysetupServersecurityComboBoxPrivate EasysetupServersecurityComboBoxPrivate;
26
27 struct _EasysetupServersecurityComboBoxPrivate
28 {
29         GtkTreeModel *model;
30 };
31
32 static void
33 easysetup_serversecurity_combo_box_get_property (GObject *object, guint property_id,
34                                                                                                                         GValue *value, GParamSpec *pspec)
35 {
36         switch (property_id) {
37         default:
38                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
39         }
40 }
41
42 static void
43 easysetup_serversecurity_combo_box_set_property (GObject *object, guint property_id,
44                                                                                                                         const GValue *value, GParamSpec *pspec)
45 {
46         switch (property_id) {
47         default:
48                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
49         }
50 }
51
52 static void
53 easysetup_serversecurity_combo_box_dispose (GObject *object)
54 {
55         if (G_OBJECT_CLASS (easysetup_serversecurity_combo_box_parent_class)->dispose)
56                 G_OBJECT_CLASS (easysetup_serversecurity_combo_box_parent_class)->dispose (object);
57 }
58
59 static void
60 easysetup_serversecurity_combo_box_finalize (GObject *object)
61 {
62         EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (object);
63
64         g_object_unref (G_OBJECT (priv->model));
65
66         G_OBJECT_CLASS (easysetup_serversecurity_combo_box_parent_class)->finalize (object);
67 }
68
69 static void
70 easysetup_serversecurity_combo_box_class_init (EasysetupServersecurityComboBoxClass *klass)
71 {
72         GObjectClass *object_class = G_OBJECT_CLASS (klass);
73
74         g_type_class_add_private (klass, sizeof (EasysetupServersecurityComboBoxPrivate));
75
76         object_class->get_property = easysetup_serversecurity_combo_box_get_property;
77         object_class->set_property = easysetup_serversecurity_combo_box_set_property;
78         object_class->dispose = easysetup_serversecurity_combo_box_dispose;
79         object_class->finalize = easysetup_serversecurity_combo_box_finalize;
80 }
81
82 enum MODEL_COLS {
83         MODEL_COL_NAME = 0, /* a string */
84         MODEL_COL_ID = 1 /* an int. */
85 };
86
87 static void
88 easysetup_serversecurity_combo_box_init (EasysetupServersecurityComboBox *self)
89 {
90         EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (self);
91
92         /* Create a tree model for the combo box,
93          * with a string for the name, and an ID for the serversecurity.
94          * This must match our MODEL_COLS enum constants.
95          */
96         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
97
98         /* Setup the combo box: */
99         GtkComboBox *combobox = GTK_COMBO_BOX (self);
100         gtk_combo_box_set_model (combobox, priv->model);
101
102         /* Serversecurity column:
103          * The ID model column in not shown in the view. */
104         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
105         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
106         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer, 
107         "text", MODEL_COL_NAME, NULL);
108         
109         /* The application must call easysetup_serversecurity_combo_box_fill().
110          */
111 }
112
113 EasysetupServersecurityComboBox*
114 easysetup_serversecurity_combo_box_new (void)
115 {
116         return g_object_new (EASYSETUP_TYPE_SERVERSECURITY_COMBO_BOX, NULL);
117 }
118
119 /* Fill the combo box with appropriate choices.
120  * #combobox: The combo box.
121  * @protocol: IMAP or POP.
122  */
123 void easysetup_serversecurity_combo_box_fill (EasysetupServersecurityComboBox *combobox, ModestProtocol protocol)
124 {       
125         EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
126         
127         /* Remove any existing rows: */
128         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
129         gtk_list_store_clear (liststore);
130         
131         GtkTreeIter iter;
132         gtk_list_store_append (liststore, &iter);
133         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_NONE, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_none"), -1);
134         
135         /* Select the None item: */
136         gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
137         
138         gtk_list_store_append (liststore, &iter);
139         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_TLS, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_normal"), -1);
140         
141         /* Add security choices with protocol-specific names, as in the UI spec:
142          * (Note: Changing the title seems pointless. murrayc) */
143         if(protocol == MODEST_PROTOCOL_STORE_POP) {
144                 gtk_list_store_append (liststore, &iter);
145                 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_securepop3s"), -1);
146         } else if(protocol == MODEST_PROTOCOL_STORE_IMAP) {
147                 gtk_list_store_append (liststore, &iter);
148                 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_secureimap4"), -1);
149         } else if(protocol == MODEST_PROTOCOL_TRANSPORT_SMTP) {
150                 gtk_list_store_append (liststore, &iter);
151                 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_ssl"), -1);
152         }
153 }
154
155 /**
156  * Returns the selected serversecurity, 
157  * or MODEST_PROTOCOL_UNKNOWN if no serversecurity was selected.
158  */
159 ModestProtocol
160 easysetup_serversecurity_combo_box_get_active_serversecurity (EasysetupServersecurityComboBox *combobox)
161 {
162         GtkTreeIter active;
163         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
164         if (found) {
165                 EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
166
167                 ModestProtocol serversecurity = MODEST_PROTOCOL_UNKNOWN;
168                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &serversecurity, -1);
169                 return serversecurity;  
170         }
171
172         return MODEST_PROTOCOL_UNKNOWN; /* Failed. */
173 }
174
175 /* This allows us to pass more than one piece of data to the signal handler,
176  * and get a result: */
177 typedef struct 
178 {
179                 EasysetupServersecurityComboBox* self;
180                 gint id;
181                 gboolean found;
182 } ForEachData;
183
184 static gboolean
185 on_model_foreach_select_id(GtkTreeModel *model, 
186         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
187 {
188         ForEachData *state = (ForEachData*)(user_data);
189         
190         /* Select the item if it has the matching ID: */
191         guint id = 0;
192         gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1); 
193         if(id == state->id) {
194                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
195                 
196                 state->found = TRUE;
197                 return TRUE; /* Stop walking the tree. */
198         }
199         
200         return FALSE; /* Keep walking the tree. */
201 }
202
203 /**
204  * Selects the specified serversecurity, 
205  * or MODEST_PROTOCOL_UNKNOWN if no serversecurity was selected.
206  */
207 gboolean
208 easysetup_serversecurity_combo_box_set_active_serversecurity (EasysetupServersecurityComboBox *combobox, ModestProtocol serversecurity)
209 {
210         EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
211         
212         /* Create a state instance so we can send two items of data to the signal handler: */
213         ForEachData *state = g_new0 (ForEachData, 1);
214         state->self = combobox;
215         state->id = serversecurity;
216         state->found = FALSE;
217         
218         /* Look at each item, and select the one with the correct ID: */
219         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
220
221         const gboolean result = state->found;
222         
223         /* Free the state instance: */
224         g_free(state);
225         
226         return result;
227 }
228