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