01c84533a05161f4dc7303fbdecaca13ae2f3831
[modest] / src / maemo / easysetup / modest-easysetup-secureauth-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-secureauth-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 (EasysetupSecureauthComboBox, easysetup_secureauth_combo_box, GTK_TYPE_COMBO_BOX);
19
20 #define SECUREAUTH_COMBO_BOX_GET_PRIVATE(o) \
21         (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_SECUREAUTH_COMBO_BOX, EasysetupSecureauthComboBoxPrivate))
22
23 typedef struct _EasysetupSecureauthComboBoxPrivate EasysetupSecureauthComboBoxPrivate;
24
25 struct _EasysetupSecureauthComboBoxPrivate
26 {
27         GtkTreeModel *model;
28 };
29
30 static void
31 easysetup_secureauth_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_secureauth_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_secureauth_combo_box_dispose (GObject *object)
52 {
53         if (G_OBJECT_CLASS (easysetup_secureauth_combo_box_parent_class)->dispose)
54                 G_OBJECT_CLASS (easysetup_secureauth_combo_box_parent_class)->dispose (object);
55 }
56
57 static void
58 easysetup_secureauth_combo_box_finalize (GObject *object)
59 {
60         EasysetupSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (object);
61
62         g_object_unref (G_OBJECT (priv->model));
63
64         G_OBJECT_CLASS (easysetup_secureauth_combo_box_parent_class)->finalize (object);
65 }
66
67 static void
68 easysetup_secureauth_combo_box_class_init (EasysetupSecureauthComboBoxClass *klass)
69 {
70         GObjectClass *object_class = G_OBJECT_CLASS (klass);
71
72         g_type_class_add_private (klass, sizeof (EasysetupSecureauthComboBoxPrivate));
73
74         object_class->get_property = easysetup_secureauth_combo_box_get_property;
75         object_class->set_property = easysetup_secureauth_combo_box_set_property;
76         object_class->dispose = easysetup_secureauth_combo_box_dispose;
77         object_class->finalize = easysetup_secureauth_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 void easysetup_secureauth_combo_box_fill (EasysetupSecureauthComboBox *combobox);
86
87 static void
88 easysetup_secureauth_combo_box_init (EasysetupSecureauthComboBox *self)
89 {
90         EasysetupSecureauthComboBoxPrivate *priv = SECUREAUTH_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 secureauth.
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         /* Secureauth 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         easysetup_secureauth_combo_box_fill (self);
110 }
111
112 EasysetupSecureauthComboBox*
113 easysetup_secureauth_combo_box_new (void)
114 {
115         return g_object_new (EASYSETUP_TYPE_SECUREAUTH_COMBO_BOX, NULL);
116 }
117
118 /* Fill the combo box with appropriate choices.
119  * #combobox: The combo box.
120  * @protocol: IMAP or POP.
121  */
122 void easysetup_secureauth_combo_box_fill (EasysetupSecureauthComboBox *combobox)
123 {       
124         EasysetupSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
125         
126         /* Remove any existing rows: */
127         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
128         gtk_list_store_clear (liststore);
129         
130         GtkTreeIter iter;
131         gtk_list_store_append (liststore, &iter);
132         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_AUTH_NONE, MODEL_COL_NAME, _("mcen_fi_advsetup_smtp_none"), -1);
133         
134         /* Select the None item: */
135         gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
136         
137         gtk_list_store_append (liststore, &iter);
138         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_AUTH_PASSWORD, MODEL_COL_NAME, _("mcen_fi_advsetup_smtp_login"), -1);
139         
140         /* TODO: I can't find a ModestProtocol enum flag for this: */
141         gtk_list_store_append (liststore, &iter);
142         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)0, MODEL_COL_NAME, _("mcen_fi_advsetup_smtp_cram_md5"), -1);
143 }
144
145 /**
146  * Returns the selected secureauth, 
147  * or MODEST_PROTOCOL_UNKNOWN if no secureauth was selected.
148  */
149 ModestProtocol
150 easysetup_secureauth_combo_box_get_active_secureauth (EasysetupSecureauthComboBox *combobox)
151 {
152         GtkTreeIter active;
153         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
154         if (found) {
155                 EasysetupSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
156
157                 ModestProtocol secureauth = MODEST_PROTOCOL_UNKNOWN;
158                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &secureauth, -1);
159                 return secureauth;      
160         }
161
162         return MODEST_PROTOCOL_UNKNOWN; /* Failed. */
163 }
164
165 /* This allows us to pass more than one piece of data to the signal handler,
166  * and get a result: */
167 typedef struct 
168 {
169                 EasysetupSecureauthComboBox* self;
170                 gint id;
171                 gboolean found;
172 } ForEachData;
173
174 static gboolean
175 on_model_foreach_select_id(GtkTreeModel *model, 
176         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
177 {
178         ForEachData *state = (ForEachData*)(user_data);
179         
180         /* Select the item if it has the matching ID: */
181         guint id = 0;
182         gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1); 
183         if(id == state->id) {
184                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
185                 
186                 state->found = TRUE;
187                 return TRUE; /* Stop walking the tree. */
188         }
189         
190         return FALSE; /* Keep walking the tree. */
191 }
192
193 /**
194  * Selects the specified secureauth, 
195  * or MODEST_PROTOCOL_UNKNOWN if no secureauth was selected.
196  */
197 gboolean
198 easysetup_secureauth_combo_box_set_active_secureauth (EasysetupSecureauthComboBox *combobox, ModestProtocol secureauth)
199 {
200         EasysetupSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
201         
202         /* Create a state instance so we can send two items of data to the signal handler: */
203         ForEachData *state = g_new0 (ForEachData, 1);
204         state->self = combobox;
205         state->id = secureauth;
206         state->found = FALSE;
207         
208         /* Look at each item, and select the one with the correct ID: */
209         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
210
211         const gboolean result = state->found;
212         
213         /* Free the state instance: */
214         g_free(state);
215         
216         return result;
217 }
218