1 /* Copyright (c) 2007, Nokia Corporation
6 #include "modest-secureauth-combo-box.h"
7 #include <gtk/gtkliststore.h>
8 #include <gtk/gtkcelllayout.h>
9 #include <gtk/gtkcellrenderertext.h>
10 #include <glib/gi18n.h>
13 #include <string.h> /* For memcpy() */
15 /* Include config.h so that _() works: */
20 G_DEFINE_TYPE (ModestSecureauthComboBox, modest_secureauth_combo_box, GTK_TYPE_COMBO_BOX);
22 #define SECUREAUTH_COMBO_BOX_GET_PRIVATE(o) \
23 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_SECUREAUTH_COMBO_BOX, ModestSecureauthComboBoxPrivate))
25 typedef struct _ModestSecureauthComboBoxPrivate ModestSecureauthComboBoxPrivate;
27 struct _ModestSecureauthComboBoxPrivate
33 modest_secureauth_combo_box_get_property (GObject *object, guint property_id,
34 GValue *value, GParamSpec *pspec)
36 switch (property_id) {
38 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
43 modest_secureauth_combo_box_set_property (GObject *object, guint property_id,
44 const GValue *value, GParamSpec *pspec)
46 switch (property_id) {
48 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
53 modest_secureauth_combo_box_dispose (GObject *object)
55 if (G_OBJECT_CLASS (modest_secureauth_combo_box_parent_class)->dispose)
56 G_OBJECT_CLASS (modest_secureauth_combo_box_parent_class)->dispose (object);
60 modest_secureauth_combo_box_finalize (GObject *object)
62 ModestSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (object);
64 g_object_unref (G_OBJECT (priv->model));
66 G_OBJECT_CLASS (modest_secureauth_combo_box_parent_class)->finalize (object);
70 modest_secureauth_combo_box_class_init (ModestSecureauthComboBoxClass *klass)
72 GObjectClass *object_class = G_OBJECT_CLASS (klass);
74 g_type_class_add_private (klass, sizeof (ModestSecureauthComboBoxPrivate));
76 object_class->get_property = modest_secureauth_combo_box_get_property;
77 object_class->set_property = modest_secureauth_combo_box_set_property;
78 object_class->dispose = modest_secureauth_combo_box_dispose;
79 object_class->finalize = modest_secureauth_combo_box_finalize;
83 MODEL_COL_NAME = 0, /* a string */
84 MODEL_COL_ID = 1 /* an int. */
87 void modest_secureauth_combo_box_fill (ModestSecureauthComboBox *combobox);
90 modest_secureauth_combo_box_init (ModestSecureauthComboBox *self)
92 ModestSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (self);
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.
98 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
100 /* Setup the combo box: */
101 GtkComboBox *combobox = GTK_COMBO_BOX (self);
102 gtk_combo_box_set_model (combobox, priv->model);
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);
111 modest_secureauth_combo_box_fill (self);
114 ModestSecureauthComboBox*
115 modest_secureauth_combo_box_new (void)
117 return g_object_new (MODEST_TYPE_SECUREAUTH_COMBO_BOX, NULL);
120 /* Fill the combo box with appropriate choices.
121 * #combobox: The combo box.
122 * @protocol: IMAP or POP.
124 void modest_secureauth_combo_box_fill (ModestSecureauthComboBox *combobox)
126 ModestSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
128 /* Remove any existing rows: */
129 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
130 gtk_list_store_clear (liststore);
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,
135 _("mcen_fi_advsetup_smtp_none"), -1);
137 /* Select the None item: */
138 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
140 gtk_list_store_append (liststore, &iter);
141 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_AUTH_PASSWORD, MODEL_COL_NAME,
142 _("mcen_fi_advsetup_smtp_login"), -1);
144 gtk_list_store_append (liststore, &iter);
145 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_AUTH_CRAMMD5, MODEL_COL_NAME,
146 _("mcen_fi_advsetup_smtp_cram_md5"), -1);
150 * Returns the selected secureauth,
151 * or MODEST_PROTOCOL_AUTH_NONE if no secureauth was selected.
154 modest_secureauth_combo_box_get_active_secureauth (ModestSecureauthComboBox *combobox)
157 const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
159 ModestSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
161 ModestAuthProtocol secureauth = MODEST_PROTOCOL_AUTH_NONE;
162 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &secureauth, -1);
166 return MODEST_PROTOCOL_AUTH_NONE; /* Failed. */
169 /* This allows us to pass more than one piece of data to the signal handler,
170 * and get a result: */
173 ModestSecureauthComboBox* self;
179 on_model_foreach_select_id(GtkTreeModel *model,
180 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
182 ForEachData *state = (ForEachData*)(user_data);
184 /* Select the item if it has the matching ID: */
186 gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1);
187 if(id == state->id) {
188 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
191 return TRUE; /* Stop walking the tree. */
194 return FALSE; /* Keep walking the tree. */
198 * Selects the specified secureauth,
199 * or MODEST_PROTOCOL_AUTH_NONE if no secureauth was selected.
202 modest_secureauth_combo_box_set_active_secureauth (ModestSecureauthComboBox *combobox, ModestAuthProtocol secureauth)
204 ModestSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
206 /* Create a state instance so we can send two items of data to the signal handler: */
207 ForEachData *state = g_new0 (ForEachData, 1);
208 state->self = combobox;
209 state->id = secureauth;
210 state->found = FALSE;
212 /* Look at each item, and select the one with the correct ID: */
213 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
215 const gboolean result = state->found;
217 /* Free the state instance: */