1 /* Copyright (c) 2007, Nokia Corporation
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>
14 #include <string.h> /* For memcpy() */
16 /* Include config.h so that _() works: */
21 G_DEFINE_TYPE (ModestSecureauthComboBox, modest_secureauth_combo_box, GTK_TYPE_COMBO_BOX);
23 #define SECUREAUTH_COMBO_BOX_GET_PRIVATE(o) \
24 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_SECUREAUTH_COMBO_BOX, ModestSecureauthComboBoxPrivate))
26 typedef struct _ModestSecureauthComboBoxPrivate ModestSecureauthComboBoxPrivate;
28 struct _ModestSecureauthComboBoxPrivate
34 modest_secureauth_combo_box_get_property (GObject *object, guint property_id,
35 GValue *value, GParamSpec *pspec)
37 switch (property_id) {
39 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
44 modest_secureauth_combo_box_set_property (GObject *object, guint property_id,
45 const GValue *value, GParamSpec *pspec)
47 switch (property_id) {
49 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
54 modest_secureauth_combo_box_dispose (GObject *object)
56 if (G_OBJECT_CLASS (modest_secureauth_combo_box_parent_class)->dispose)
57 G_OBJECT_CLASS (modest_secureauth_combo_box_parent_class)->dispose (object);
61 modest_secureauth_combo_box_finalize (GObject *object)
63 ModestSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (object);
65 g_object_unref (G_OBJECT (priv->model));
67 G_OBJECT_CLASS (modest_secureauth_combo_box_parent_class)->finalize (object);
71 modest_secureauth_combo_box_class_init (ModestSecureauthComboBoxClass *klass)
73 GObjectClass *object_class = G_OBJECT_CLASS (klass);
75 g_type_class_add_private (klass, sizeof (ModestSecureauthComboBoxPrivate));
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;
84 MODEL_COL_NAME = 0, /* a string */
85 MODEL_COL_ID = 1 /* an int. */
88 void modest_secureauth_combo_box_fill (ModestSecureauthComboBox *combobox);
91 modest_secureauth_combo_box_init (ModestSecureauthComboBox *self)
93 ModestSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (self);
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.
99 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
101 /* Setup the combo box: */
102 GtkComboBox *combobox = GTK_COMBO_BOX (self);
103 gtk_combo_box_set_model (combobox, priv->model);
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);
112 modest_secureauth_combo_box_fill (self);
115 ModestSecureauthComboBox*
116 modest_secureauth_combo_box_new (void)
118 return g_object_new (MODEST_TYPE_SECUREAUTH_COMBO_BOX, NULL);
121 /* Fill the combo box with appropriate choices.
122 * #combobox: The combo box.
123 * @protocol: IMAP or POP.
125 void modest_secureauth_combo_box_fill (ModestSecureauthComboBox *combobox)
127 ModestSecureauthComboBoxPrivate *priv;
128 GtkListStore *liststore;
129 ModestProtocolRegistry *protocol_registry;
130 GSList *protocols, *node;
133 priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
135 /* Remove any existing rows: */
136 liststore = GTK_LIST_STORE (priv->model);
137 gtk_list_store_clear (liststore);
139 protocol_registry = modest_runtime_get_protocol_registry ();
140 protocols = modest_protocol_registry_get_by_tag (protocol_registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS);
142 for (node = protocols; node != NULL; node = g_slist_next (node)) {
143 ModestProtocol *protocol;
144 protocol = (ModestProtocol *) node->data;
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),
152 g_slist_free (protocols);
156 * Returns the selected secureauth,
157 * or MODEST_PROTOCOL_REGISTRY_TYPE_INVALID if no secureauth was selected.
160 modest_secureauth_combo_box_get_active_secureauth (ModestSecureauthComboBox *combobox)
165 found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
167 ModestSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
169 ModestProtocolType secureauth = MODEST_PROTOCOLS_AUTH_NONE;
170 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &secureauth, -1);
174 return MODEST_PROTOCOL_REGISTRY_TYPE_INVALID; /* Failed. */
177 /* This allows us to pass more than one piece of data to the signal handler,
178 * and get a result: */
181 ModestSecureauthComboBox* self;
182 ModestProtocolType id;
187 on_model_foreach_select_id(GtkTreeModel *model,
188 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
191 ModestProtocolType id = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
193 state = (ForEachData*)(user_data);
195 /* Select the item if it has the matching ID: */
196 gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1);
197 if(id == state->id) {
198 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
201 return TRUE; /* Stop walking the tree. */
204 return FALSE; /* Keep walking the tree. */
208 * Selects the specified secureauth,
209 * or MODEST_PROTOCOL_REGISTRY_TYPE_INVALID if no secureauth was selected.
212 modest_secureauth_combo_box_set_active_secureauth (ModestSecureauthComboBox *combobox, ModestProtocolType secureauth)
214 ModestSecureauthComboBoxPrivate *priv;
218 priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
220 /* Create a state instance so we can send two items of data to the signal handler: */
221 state = g_new0 (ForEachData, 1);
222 state->self = combobox;
223 state->id = secureauth;
224 state->found = FALSE;
226 /* Look at each item, and select the one with the correct ID: */
227 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
229 result = state->found;
231 /* Free the state instance: */