1 /* Copyright (c) 2007, Nokia Corporation
6 #include "modest-easysetup-serversecurity-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 (EasysetupServersecurityComboBox, easysetup_serversecurity_combo_box, GTK_TYPE_COMBO_BOX);
22 #define SERVERSECURITY_COMBO_BOX_GET_PRIVATE(o) \
23 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_SERVERSECURITY_COMBO_BOX, EasysetupServersecurityComboBoxPrivate))
25 typedef struct _EasysetupServersecurityComboBoxPrivate EasysetupServersecurityComboBoxPrivate;
27 struct _EasysetupServersecurityComboBoxPrivate
33 easysetup_serversecurity_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 easysetup_serversecurity_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 easysetup_serversecurity_combo_box_dispose (GObject *object)
55 if (G_OBJECT_CLASS (easysetup_serversecurity_combo_box_parent_class)->dispose)
56 G_OBJECT_CLASS (easysetup_serversecurity_combo_box_parent_class)->dispose (object);
60 easysetup_serversecurity_combo_box_finalize (GObject *object)
62 EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (object);
64 g_object_unref (G_OBJECT (priv->model));
66 G_OBJECT_CLASS (easysetup_serversecurity_combo_box_parent_class)->finalize (object);
70 easysetup_serversecurity_combo_box_class_init (EasysetupServersecurityComboBoxClass *klass)
72 GObjectClass *object_class = G_OBJECT_CLASS (klass);
74 g_type_class_add_private (klass, sizeof (EasysetupServersecurityComboBoxPrivate));
76 object_class->get_property = easysetup_serversecurity_combo_box_get_property;
77 object_class->set_property = easysetup_serversecurity_combo_box_set_property;
78 object_class->dispose = easysetup_serversecurity_combo_box_dispose;
79 object_class->finalize = easysetup_serversecurity_combo_box_finalize;
83 MODEL_COL_NAME = 0, /* a string */
84 MODEL_COL_ID = 1 /* an int. */
88 easysetup_serversecurity_combo_box_init (EasysetupServersecurityComboBox *self)
90 EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_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 serversecurity.
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 /* Serversecurity 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 /* The application must call easysetup_serversecurity_combo_box_fill().
113 EasysetupServersecurityComboBox*
114 easysetup_serversecurity_combo_box_new (void)
116 return g_object_new (EASYSETUP_TYPE_SERVERSECURITY_COMBO_BOX, NULL);
119 /* Fill the combo box with appropriate choices.
120 * #combobox: The combo box.
121 * @protocol: IMAP or POP.
123 void easysetup_serversecurity_combo_box_fill (EasysetupServersecurityComboBox *combobox, ModestProtocol protocol)
125 EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
127 /* Remove any existing rows: */
128 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
129 gtk_list_store_clear (liststore);
132 gtk_list_store_append (liststore, &iter);
133 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_NONE, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_none"), -1);
135 /* Select the None item: */
136 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
138 gtk_list_store_append (liststore, &iter);
139 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_TLS, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_normal"), -1);
141 /* Add security choices with protocol-specific names, as in the UI spec:
142 * (Note: Changing the title seems pointless. murrayc) */
143 if(protocol == MODEST_PROTOCOL_STORE_POP) {
144 gtk_list_store_append (liststore, &iter);
145 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_securepop3s"), -1);
146 } else if(protocol == MODEST_PROTOCOL_STORE_IMAP) {
147 gtk_list_store_append (liststore, &iter);
148 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_secureimap4"), -1);
149 } else if(protocol == MODEST_PROTOCOL_TRANSPORT_SMTP) {
150 gtk_list_store_append (liststore, &iter);
151 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_ssl"), -1);
156 * Returns the selected serversecurity,
157 * or MODEST_PROTOCOL_UNKNOWN if no serversecurity was selected.
160 easysetup_serversecurity_combo_box_get_active_serversecurity (EasysetupServersecurityComboBox *combobox)
163 const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
165 EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
167 ModestProtocol serversecurity = MODEST_PROTOCOL_UNKNOWN;
168 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &serversecurity, -1);
169 return serversecurity;
172 return MODEST_PROTOCOL_UNKNOWN; /* Failed. */
175 /* This allows us to pass more than one piece of data to the signal handler,
176 * and get a result: */
179 EasysetupServersecurityComboBox* self;
185 on_model_foreach_select_id(GtkTreeModel *model,
186 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
188 ForEachData *state = (ForEachData*)(user_data);
190 /* Select the item if it has the matching ID: */
192 gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1);
193 if(id == state->id) {
194 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
197 return TRUE; /* Stop walking the tree. */
200 return FALSE; /* Keep walking the tree. */
204 * Selects the specified serversecurity,
205 * or MODEST_PROTOCOL_UNKNOWN if no serversecurity was selected.
208 easysetup_serversecurity_combo_box_set_active_serversecurity (EasysetupServersecurityComboBox *combobox, ModestProtocol serversecurity)
210 EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
212 /* Create a state instance so we can send two items of data to the signal handler: */
213 ForEachData *state = g_new0 (ForEachData, 1);
214 state->self = combobox;
215 state->id = serversecurity;
216 state->found = FALSE;
218 /* Look at each item, and select the one with the correct ID: */
219 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
221 const gboolean result = state->found;
223 /* Free the state instance: */