a85f68004fdc1ae90c0c949581957610a90e0f6b
[modest] / src / widgets / 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         ModestProtocol protocol;
31 };
32
33 static void
34 easysetup_serversecurity_combo_box_get_property (GObject *object, guint property_id,
35                                                                                                                         GValue *value, GParamSpec *pspec)
36 {
37         switch (property_id) {
38         default:
39                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
40         }
41 }
42
43 static void
44 easysetup_serversecurity_combo_box_set_property (GObject *object, guint property_id,
45                                                                                                                         const GValue *value, GParamSpec *pspec)
46 {
47         switch (property_id) {
48         default:
49                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
50         }
51 }
52
53 static void
54 easysetup_serversecurity_combo_box_dispose (GObject *object)
55 {
56         if (G_OBJECT_CLASS (easysetup_serversecurity_combo_box_parent_class)->dispose)
57                 G_OBJECT_CLASS (easysetup_serversecurity_combo_box_parent_class)->dispose (object);
58 }
59
60 static void
61 easysetup_serversecurity_combo_box_finalize (GObject *object)
62 {
63         EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (object);
64
65         g_object_unref (G_OBJECT (priv->model));
66
67         G_OBJECT_CLASS (easysetup_serversecurity_combo_box_parent_class)->finalize (object);
68 }
69
70 static void
71 easysetup_serversecurity_combo_box_class_init (EasysetupServersecurityComboBoxClass *klass)
72 {
73         GObjectClass *object_class = G_OBJECT_CLASS (klass);
74
75         g_type_class_add_private (klass, sizeof (EasysetupServersecurityComboBoxPrivate));
76
77         object_class->get_property = easysetup_serversecurity_combo_box_get_property;
78         object_class->set_property = easysetup_serversecurity_combo_box_set_property;
79         object_class->dispose = easysetup_serversecurity_combo_box_dispose;
80         object_class->finalize = easysetup_serversecurity_combo_box_finalize;
81 }
82
83 enum MODEL_COLS {
84         MODEL_COL_NAME = 0, /* a string */
85         MODEL_COL_ID = 1 /* an int. */
86 };
87
88 static void
89 easysetup_serversecurity_combo_box_init (EasysetupServersecurityComboBox *self)
90 {
91         EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (self);
92
93         /* Create a tree model for the combo box,
94          * with a string for the name, and an ID for the serversecurity.
95          * This must match our MODEL_COLS enum constants.
96          */
97         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
98
99         /* Setup the combo box: */
100         GtkComboBox *combobox = GTK_COMBO_BOX (self);
101         gtk_combo_box_set_model (combobox, priv->model);
102
103         /* Serversecurity column:
104          * The ID model column in not shown in the view. */
105         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
106         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
107         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer, 
108         "text", MODEL_COL_NAME, NULL);
109         
110         /* The application must call easysetup_serversecurity_combo_box_fill().
111          */
112 }
113
114 EasysetupServersecurityComboBox*
115 easysetup_serversecurity_combo_box_new (void)
116 {
117         return g_object_new (EASYSETUP_TYPE_SERVERSECURITY_COMBO_BOX, NULL);
118 }
119
120 /* Fill the combo box with appropriate choices.
121  * #combobox: The combo box.
122  * @protocol: IMAP or POP.
123  */
124 void easysetup_serversecurity_combo_box_fill (EasysetupServersecurityComboBox *combobox, ModestProtocol protocol)
125 {       
126         EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
127         priv->protocol = protocol; /* Remembered for later. */
128         
129         /* Remove any existing rows: */
130         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
131         gtk_list_store_clear (liststore);
132         
133         GtkTreeIter iter;
134         gtk_list_store_append (liststore, &iter);
135         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_NONE, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_none"), -1);
136         
137         /* Select the None item: */
138         gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
139         
140         gtk_list_store_append (liststore, &iter);
141         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_TLS, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_normal"), -1);
142         
143         /* Add security choices with protocol-specific names, as in the UI spec:
144          * (Note: Changing the title seems pointless. murrayc) */
145         if(protocol == MODEST_PROTOCOL_STORE_POP) {
146                 gtk_list_store_append (liststore, &iter);
147                 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_securepop3s"), -1);
148         } else if(protocol == MODEST_PROTOCOL_STORE_IMAP) {
149                 gtk_list_store_append (liststore, &iter);
150                 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_secureimap4"), -1);
151         } else if(protocol == MODEST_PROTOCOL_TRANSPORT_SMTP) {
152                 gtk_list_store_append (liststore, &iter);
153                 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_ssl"), -1);
154         }
155 }
156
157 static gint get_port_for_security (ModestProtocol protocol, ModestProtocol security)
158 {
159         /* See the UI spec, section Email Wizards, Incoming Details [MSG-WIZ001]: */
160         gint result = 0;
161
162         /* Get the default port number for this protocol with this security: */
163         if(protocol == MODEST_PROTOCOL_STORE_POP) {
164                 if ((security ==  MODEST_PROTOCOL_SECURITY_NONE) || (security ==  MODEST_PROTOCOL_SECURITY_TLS))
165                         result = 110;
166                 else if (security ==  MODEST_PROTOCOL_SECURITY_SSL)
167                         result = 995;
168         } else if (protocol == MODEST_PROTOCOL_STORE_IMAP) {
169                 if ((security ==  MODEST_PROTOCOL_SECURITY_NONE) || (security ==  MODEST_PROTOCOL_SECURITY_TLS))
170                         result = 143;
171                 else if (security ==  MODEST_PROTOCOL_SECURITY_SSL)
172                         result = 993;
173         } else if (protocol == MODEST_PROTOCOL_TRANSPORT_SMTP) {
174                 if ((security ==  MODEST_PROTOCOL_SECURITY_NONE) || (security ==  MODEST_PROTOCOL_SECURITY_TLS))
175                         result = 25;
176                 else if (security ==  MODEST_PROTOCOL_SECURITY_SSL)
177                         result = 465;
178         }
179
180         return result;
181 }
182
183 /**
184  * Returns the selected serversecurity, 
185  * or MODEST_PROTOCOL_UNKNOWN if no serversecurity was selected.
186  */
187 ModestProtocol
188 easysetup_serversecurity_combo_box_get_active_serversecurity (EasysetupServersecurityComboBox *combobox)
189 {
190         GtkTreeIter active;
191         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
192         if (found) {
193                 EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
194
195                 ModestProtocol serversecurity = MODEST_PROTOCOL_UNKNOWN;
196                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &serversecurity, -1);
197                 return serversecurity;  
198         }
199
200         return MODEST_PROTOCOL_UNKNOWN; /* Failed. */
201 }
202
203 /**
204  * Returns the default port to be used for the selected serversecurity, 
205  * or 0 if no serversecurity was selected.
206  */
207 gint
208 easysetup_serversecurity_combo_box_get_active_serversecurity_port (EasysetupServersecurityComboBox *combobox)
209 {
210         EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
211         
212         const ModestProtocol security = easysetup_serversecurity_combo_box_get_active_serversecurity 
213                 (combobox);
214         return get_port_for_security (priv->protocol, security);
215 }
216         
217 /* This allows us to pass more than one piece of data to the signal handler,
218  * and get a result: */
219 typedef struct 
220 {
221                 EasysetupServersecurityComboBox* self;
222                 gint id;
223                 gboolean found;
224 } ForEachData;
225
226 static gboolean
227 on_model_foreach_select_id(GtkTreeModel *model, 
228         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
229 {
230         ForEachData *state = (ForEachData*)(user_data);
231         
232         /* Select the item if it has the matching ID: */
233         guint id = 0;
234         gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1); 
235         if(id == state->id) {
236                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
237                 
238                 state->found = TRUE;
239                 return TRUE; /* Stop walking the tree. */
240         }
241         
242         return FALSE; /* Keep walking the tree. */
243 }
244
245 /**
246  * Selects the specified serversecurity, 
247  * or MODEST_PROTOCOL_UNKNOWN if no serversecurity was selected.
248  */
249 gboolean
250 easysetup_serversecurity_combo_box_set_active_serversecurity (EasysetupServersecurityComboBox *combobox, ModestProtocol serversecurity)
251 {
252         EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
253         
254         /* Create a state instance so we can send two items of data to the signal handler: */
255         ForEachData *state = g_new0 (ForEachData, 1);
256         state->self = combobox;
257         state->id = serversecurity;
258         state->found = FALSE;
259         
260         /* Look at each item, and select the one with the correct ID: */
261         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
262
263         const gboolean result = state->found;
264         
265         /* Free the state instance: */
266         g_free(state);
267         
268         return result;
269 }
270