1 /* Copyright (c) 2007, Nokia Corporation
6 #define _GNU_SOURCE /* So we can use the getline() function, which is a convenient GNU extension. */
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>
16 #include <string.h> /* For memcpy() */
18 G_DEFINE_TYPE (EasysetupSecureauthComboBox, easysetup_secureauth_combo_box, GTK_TYPE_COMBO_BOX);
20 #define SECUREAUTH_COMBO_BOX_GET_PRIVATE(o) \
21 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_SECUREAUTH_COMBO_BOX, EasysetupSecureauthComboBoxPrivate))
23 typedef struct _EasysetupSecureauthComboBoxPrivate EasysetupSecureauthComboBoxPrivate;
25 struct _EasysetupSecureauthComboBoxPrivate
31 easysetup_secureauth_combo_box_get_property (GObject *object, guint property_id,
32 GValue *value, GParamSpec *pspec)
34 switch (property_id) {
36 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
41 easysetup_secureauth_combo_box_set_property (GObject *object, guint property_id,
42 const GValue *value, GParamSpec *pspec)
44 switch (property_id) {
46 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
51 easysetup_secureauth_combo_box_dispose (GObject *object)
53 if (G_OBJECT_CLASS (easysetup_secureauth_combo_box_parent_class)->dispose)
54 G_OBJECT_CLASS (easysetup_secureauth_combo_box_parent_class)->dispose (object);
58 easysetup_secureauth_combo_box_finalize (GObject *object)
60 EasysetupSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (object);
62 g_object_unref (G_OBJECT (priv->model));
64 G_OBJECT_CLASS (easysetup_secureauth_combo_box_parent_class)->finalize (object);
68 easysetup_secureauth_combo_box_class_init (EasysetupSecureauthComboBoxClass *klass)
70 GObjectClass *object_class = G_OBJECT_CLASS (klass);
72 g_type_class_add_private (klass, sizeof (EasysetupSecureauthComboBoxPrivate));
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;
81 MODEL_COL_NAME = 0, /* a string */
82 MODEL_COL_ID = 1 /* an int. */
85 void easysetup_secureauth_combo_box_fill (EasysetupSecureauthComboBox *combobox);
88 easysetup_secureauth_combo_box_init (EasysetupSecureauthComboBox *self)
90 EasysetupSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (self);
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.
96 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
98 /* Setup the combo box: */
99 GtkComboBox *combobox = GTK_COMBO_BOX (self);
100 gtk_combo_box_set_model (combobox, priv->model);
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);
109 easysetup_secureauth_combo_box_fill (self);
112 EasysetupSecureauthComboBox*
113 easysetup_secureauth_combo_box_new (void)
115 return g_object_new (EASYSETUP_TYPE_SECUREAUTH_COMBO_BOX, NULL);
118 /* Fill the combo box with appropriate choices.
119 * #combobox: The combo box.
120 * @protocol: IMAP or POP.
122 void easysetup_secureauth_combo_box_fill (EasysetupSecureauthComboBox *combobox)
124 EasysetupSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
126 /* Remove any existing rows: */
127 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
128 gtk_list_store_clear (liststore);
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);
134 /* Select the None item: */
135 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
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);
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);
146 * Returns the selected secureauth,
147 * or MODEST_PROTOCOL_UNKNOWN if no secureauth was selected.
150 easysetup_secureauth_combo_box_get_active_secureauth (EasysetupSecureauthComboBox *combobox)
153 const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
155 EasysetupSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
157 ModestProtocol secureauth = MODEST_PROTOCOL_UNKNOWN;
158 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &secureauth, -1);
162 return MODEST_PROTOCOL_UNKNOWN; /* Failed. */
165 /* This allows us to pass more than one piece of data to the signal handler,
166 * and get a result: */
169 EasysetupSecureauthComboBox* self;
175 on_model_foreach_select_id(GtkTreeModel *model,
176 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
178 ForEachData *state = (ForEachData*)(user_data);
180 /* Select the item if it has the matching ID: */
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);
187 return TRUE; /* Stop walking the tree. */
190 return FALSE; /* Keep walking the tree. */
194 * Selects the specified secureauth,
195 * or MODEST_PROTOCOL_UNKNOWN if no secureauth was selected.
198 easysetup_secureauth_combo_box_set_active_secureauth (EasysetupSecureauthComboBox *combobox, ModestProtocol secureauth)
200 EasysetupSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
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;
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);
211 const gboolean result = state->found;
213 /* Free the state instance: */