c9000940a360696d5027d0956228b644948b3563
[modest] / src / maemo / easysetup / modest-easysetup-secureauth-combo-box.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  */
5
6 #include "modest-easysetup-secureauth-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 (EasysetupSecureauthComboBox, easysetup_secureauth_combo_box, GTK_TYPE_COMBO_BOX);
21
22 #define SECUREAUTH_COMBO_BOX_GET_PRIVATE(o) \
23         (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_SECUREAUTH_COMBO_BOX, EasysetupSecureauthComboBoxPrivate))
24
25 typedef struct _EasysetupSecureauthComboBoxPrivate EasysetupSecureauthComboBoxPrivate;
26
27 struct _EasysetupSecureauthComboBoxPrivate
28 {
29         GtkTreeModel *model;
30 };
31
32 static void
33 easysetup_secureauth_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_secureauth_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_secureauth_combo_box_dispose (GObject *object)
54 {
55         if (G_OBJECT_CLASS (easysetup_secureauth_combo_box_parent_class)->dispose)
56                 G_OBJECT_CLASS (easysetup_secureauth_combo_box_parent_class)->dispose (object);
57 }
58
59 static void
60 easysetup_secureauth_combo_box_finalize (GObject *object)
61 {
62         EasysetupSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (object);
63
64         g_object_unref (G_OBJECT (priv->model));
65
66         G_OBJECT_CLASS (easysetup_secureauth_combo_box_parent_class)->finalize (object);
67 }
68
69 static void
70 easysetup_secureauth_combo_box_class_init (EasysetupSecureauthComboBoxClass *klass)
71 {
72         GObjectClass *object_class = G_OBJECT_CLASS (klass);
73
74         g_type_class_add_private (klass, sizeof (EasysetupSecureauthComboBoxPrivate));
75
76         object_class->get_property = easysetup_secureauth_combo_box_get_property;
77         object_class->set_property = easysetup_secureauth_combo_box_set_property;
78         object_class->dispose = easysetup_secureauth_combo_box_dispose;
79         object_class->finalize = easysetup_secureauth_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 void easysetup_secureauth_combo_box_fill (EasysetupSecureauthComboBox *combobox);
88
89 static void
90 easysetup_secureauth_combo_box_init (EasysetupSecureauthComboBox *self)
91 {
92         EasysetupSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (self);
93
94         /* Create a tree model for the combo box,
95          * with a string for the name, and an ID for the secureauth.
96          * This must match our MODEL_COLS enum constants.
97          */
98         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
99
100         /* Setup the combo box: */
101         GtkComboBox *combobox = GTK_COMBO_BOX (self);
102         gtk_combo_box_set_model (combobox, priv->model);
103
104         /* Secureauth column:
105          * The ID model column in not shown in the view. */
106         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
107         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
108         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer, 
109         "text", MODEL_COL_NAME, NULL);
110         
111         easysetup_secureauth_combo_box_fill (self);
112 }
113
114 EasysetupSecureauthComboBox*
115 easysetup_secureauth_combo_box_new (void)
116 {
117         return g_object_new (EASYSETUP_TYPE_SECUREAUTH_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_secureauth_combo_box_fill (EasysetupSecureauthComboBox *combobox)
125 {       
126         EasysetupSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
127         
128         /* Remove any existing rows: */
129         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
130         gtk_list_store_clear (liststore);
131         
132         GtkTreeIter iter;
133         gtk_list_store_append (liststore, &iter);
134         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_AUTH_NONE, MODEL_COL_NAME, _("mcen_fi_advsetup_smtp_none"), -1);
135         
136         /* Select the None item: */
137         gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
138         
139         gtk_list_store_append (liststore, &iter);
140         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_AUTH_PASSWORD, MODEL_COL_NAME, _("mcen_fi_advsetup_smtp_login"), -1);
141         
142         gtk_list_store_append (liststore, &iter);
143         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_AUTH_CRAMMD5, MODEL_COL_NAME, _("mcen_fi_advsetup_smtp_cram_md5"), -1);
144 }
145
146 /**
147  * Returns the selected secureauth, 
148  * or MODEST_PROTOCOL_UNKNOWN if no secureauth was selected.
149  */
150 ModestProtocol
151 easysetup_secureauth_combo_box_get_active_secureauth (EasysetupSecureauthComboBox *combobox)
152 {
153         GtkTreeIter active;
154         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
155         if (found) {
156                 EasysetupSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
157
158                 ModestProtocol secureauth = MODEST_PROTOCOL_UNKNOWN;
159                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &secureauth, -1);
160                 return secureauth;      
161         }
162
163         return MODEST_PROTOCOL_UNKNOWN; /* Failed. */
164 }
165
166 /* This allows us to pass more than one piece of data to the signal handler,
167  * and get a result: */
168 typedef struct 
169 {
170                 EasysetupSecureauthComboBox* self;
171                 gint id;
172                 gboolean found;
173 } ForEachData;
174
175 static gboolean
176 on_model_foreach_select_id(GtkTreeModel *model, 
177         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
178 {
179         ForEachData *state = (ForEachData*)(user_data);
180         
181         /* Select the item if it has the matching ID: */
182         guint id = 0;
183         gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1); 
184         if(id == state->id) {
185                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
186                 
187                 state->found = TRUE;
188                 return TRUE; /* Stop walking the tree. */
189         }
190         
191         return FALSE; /* Keep walking the tree. */
192 }
193
194 /**
195  * Selects the specified secureauth, 
196  * or MODEST_PROTOCOL_UNKNOWN if no secureauth was selected.
197  */
198 gboolean
199 easysetup_secureauth_combo_box_set_active_secureauth (EasysetupSecureauthComboBox *combobox, ModestProtocol secureauth)
200 {
201         EasysetupSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
202         
203         /* Create a state instance so we can send two items of data to the signal handler: */
204         ForEachData *state = g_new0 (ForEachData, 1);
205         state->self = combobox;
206         state->id = secureauth;
207         state->found = FALSE;
208         
209         /* Look at each item, and select the one with the correct ID: */
210         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
211
212         const gboolean result = state->found;
213         
214         /* Free the state instance: */
215         g_free(state);
216         
217         return result;
218 }
219