Merged the new plugin system from branch plugin-system.
[modest] / src / widgets / modest-secureauth-combo-box.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  */
5
6 #include "modest-secureauth-combo-box.h"
7 #include <modest-runtime.h>
8 #include <gtk/gtkliststore.h>
9 #include <gtk/gtkcelllayout.h>
10 #include <gtk/gtkcellrenderertext.h>
11 #include <glib/gi18n.h>
12
13 #include <stdlib.h>
14 #include <string.h> /* For memcpy() */
15
16 /* Include config.h so that _() works: */
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 G_DEFINE_TYPE (ModestSecureauthComboBox, modest_secureauth_combo_box, GTK_TYPE_COMBO_BOX);
22
23 #define SECUREAUTH_COMBO_BOX_GET_PRIVATE(o) \
24         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_SECUREAUTH_COMBO_BOX, ModestSecureauthComboBoxPrivate))
25
26 typedef struct _ModestSecureauthComboBoxPrivate ModestSecureauthComboBoxPrivate;
27
28 struct _ModestSecureauthComboBoxPrivate
29 {
30         GtkTreeModel *model;
31 };
32
33 static void
34 modest_secureauth_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 modest_secureauth_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 modest_secureauth_combo_box_dispose (GObject *object)
55 {
56         if (G_OBJECT_CLASS (modest_secureauth_combo_box_parent_class)->dispose)
57                 G_OBJECT_CLASS (modest_secureauth_combo_box_parent_class)->dispose (object);
58 }
59
60 static void
61 modest_secureauth_combo_box_finalize (GObject *object)
62 {
63         ModestSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (object);
64
65         g_object_unref (G_OBJECT (priv->model));
66
67         G_OBJECT_CLASS (modest_secureauth_combo_box_parent_class)->finalize (object);
68 }
69
70 static void
71 modest_secureauth_combo_box_class_init (ModestSecureauthComboBoxClass *klass)
72 {
73         GObjectClass *object_class = G_OBJECT_CLASS (klass);
74
75         g_type_class_add_private (klass, sizeof (ModestSecureauthComboBoxPrivate));
76
77         object_class->get_property = modest_secureauth_combo_box_get_property;
78         object_class->set_property = modest_secureauth_combo_box_set_property;
79         object_class->dispose = modest_secureauth_combo_box_dispose;
80         object_class->finalize = modest_secureauth_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 void modest_secureauth_combo_box_fill (ModestSecureauthComboBox *combobox);
89
90 static void
91 modest_secureauth_combo_box_init (ModestSecureauthComboBox *self)
92 {
93         ModestSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (self);
94
95         /* Create a tree model for the combo box,
96          * with a string for the name, and an ID for the secureauth.
97          * This must match our MODEL_COLS enum constants.
98          */
99         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
100
101         /* Setup the combo box: */
102         GtkComboBox *combobox = GTK_COMBO_BOX (self);
103         gtk_combo_box_set_model (combobox, priv->model);
104
105         /* Secureauth column:
106          * The ID model column in not shown in the view. */
107         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
108         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
109         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer, 
110         "text", MODEL_COL_NAME, NULL);
111         
112         modest_secureauth_combo_box_fill (self);
113 }
114
115 ModestSecureauthComboBox*
116 modest_secureauth_combo_box_new (void)
117 {
118         return g_object_new (MODEST_TYPE_SECUREAUTH_COMBO_BOX, NULL);
119 }
120
121 /* Fill the combo box with appropriate choices.
122  * #combobox: The combo box.
123  * @protocol: IMAP or POP.
124  */
125 void modest_secureauth_combo_box_fill (ModestSecureauthComboBox *combobox)
126 {       
127         ModestSecureauthComboBoxPrivate *priv;
128         GtkListStore *liststore;
129         ModestProtocolRegistry *protocol_registry;
130         GSList *protocols, *node;
131         GtkTreeIter iter;
132
133         priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
134         
135         /* Remove any existing rows: */
136         liststore = GTK_LIST_STORE (priv->model);
137         gtk_list_store_clear (liststore);
138
139         protocol_registry = modest_runtime_get_protocol_registry ();
140         protocols = modest_protocol_registry_get_by_tag (protocol_registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS);
141
142         for (node = protocols; node != NULL; node = g_slist_next (node)) {
143                 ModestProtocol *protocol;
144                 protocol = (ModestProtocol *) node->data;
145
146                 gtk_list_store_append (liststore, &iter);
147                 gtk_list_store_set (liststore, &iter, 
148                                     MODEL_COL_ID, (gint)modest_protocol_get_type_id (protocol),
149                                     MODEL_COL_NAME, modest_protocol_get_display_name (protocol),
150                                     -1);
151         }       
152 }
153
154 /**
155  * Returns the selected secureauth, 
156  * or MODEST_PROTOCOL_REGISTRY_TYPE_INVALID if no secureauth was selected.
157  */
158 ModestProtocolType
159 modest_secureauth_combo_box_get_active_secureauth (ModestSecureauthComboBox *combobox)
160 {
161         GtkTreeIter active;
162         gboolean found;
163
164         found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
165         if (found) {
166                 ModestSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
167
168                 ModestProtocolType secureauth = MODEST_PROTOCOLS_AUTH_NONE;
169                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &secureauth, -1);
170                 return secureauth;      
171         }
172
173         return MODEST_PROTOCOL_REGISTRY_TYPE_INVALID; /* Failed. */
174 }
175
176 /* This allows us to pass more than one piece of data to the signal handler,
177  * and get a result: */
178 typedef struct 
179 {
180                 ModestSecureauthComboBox* self;
181                 ModestProtocolType id;
182                 gboolean found;
183 } ForEachData;
184
185 static gboolean
186 on_model_foreach_select_id(GtkTreeModel *model, 
187         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
188 {
189         ForEachData *state;
190         ModestProtocolType id = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
191
192         state = (ForEachData*)(user_data);
193         
194         /* Select the item if it has the matching ID: */
195         gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1); 
196         if(id == state->id) {
197                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
198                 
199                 state->found = TRUE;
200                 return TRUE; /* Stop walking the tree. */
201         }
202         
203         return FALSE; /* Keep walking the tree. */
204 }
205
206 /**
207  * Selects the specified secureauth, 
208  * or MODEST_PROTOCOL_REGISTRY_TYPE_INVALID if no secureauth was selected.
209  */
210 gboolean
211 modest_secureauth_combo_box_set_active_secureauth (ModestSecureauthComboBox *combobox, ModestProtocolType secureauth)
212 {
213         ModestSecureauthComboBoxPrivate *priv;
214         ForEachData *state;
215         gboolean result;
216
217         priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
218         
219         /* Create a state instance so we can send two items of data to the signal handler: */
220         state = g_new0 (ForEachData, 1);
221         state->self = combobox;
222         state->id = secureauth;
223         state->found = FALSE;
224         
225         /* Look at each item, and select the one with the correct ID: */
226         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
227
228         result = state->found;
229         
230         /* Free the state instance: */
231         g_free(state);
232         
233         return result;
234 }
235