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-serversecurity-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 (EasysetupServersecurityComboBox, easysetup_serversecurity_combo_box, GTK_TYPE_COMBO_BOX);
20 #define SERVERSECURITY_COMBO_BOX_GET_PRIVATE(o) \
21 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_SERVERSECURITY_COMBO_BOX, EasysetupServersecurityComboBoxPrivate))
23 typedef struct _EasysetupServersecurityComboBoxPrivate EasysetupServersecurityComboBoxPrivate;
25 struct _EasysetupServersecurityComboBoxPrivate
31 easysetup_serversecurity_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_serversecurity_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_serversecurity_combo_box_dispose (GObject *object)
53 if (G_OBJECT_CLASS (easysetup_serversecurity_combo_box_parent_class)->dispose)
54 G_OBJECT_CLASS (easysetup_serversecurity_combo_box_parent_class)->dispose (object);
58 easysetup_serversecurity_combo_box_finalize (GObject *object)
60 EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (object);
62 g_object_unref (G_OBJECT (priv->model));
64 G_OBJECT_CLASS (easysetup_serversecurity_combo_box_parent_class)->finalize (object);
68 easysetup_serversecurity_combo_box_class_init (EasysetupServersecurityComboBoxClass *klass)
70 GObjectClass *object_class = G_OBJECT_CLASS (klass);
72 g_type_class_add_private (klass, sizeof (EasysetupServersecurityComboBoxPrivate));
74 object_class->get_property = easysetup_serversecurity_combo_box_get_property;
75 object_class->set_property = easysetup_serversecurity_combo_box_set_property;
76 object_class->dispose = easysetup_serversecurity_combo_box_dispose;
77 object_class->finalize = easysetup_serversecurity_combo_box_finalize;
81 MODEL_COL_NAME = 0, /* a string */
82 MODEL_COL_ID = 1 /* an int. */
86 easysetup_serversecurity_combo_box_init (EasysetupServersecurityComboBox *self)
88 EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (self);
90 /* Create a tree model for the combo box,
91 * with a string for the name, and an ID for the serversecurity.
92 * This must match our MODEL_COLS enum constants.
94 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
96 /* Setup the combo box: */
97 GtkComboBox *combobox = GTK_COMBO_BOX (self);
98 gtk_combo_box_set_model (combobox, priv->model);
100 /* Serversecurity column:
101 * The ID model column in not shown in the view. */
102 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
103 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
104 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
105 "text", MODEL_COL_NAME, NULL);
107 /* The application must call easysetup_serversecurity_combo_box_fill().
111 EasysetupServersecurityComboBox*
112 easysetup_serversecurity_combo_box_new (void)
114 return g_object_new (EASYSETUP_TYPE_SERVERSECURITY_COMBO_BOX, NULL);
117 /* Fill the combo box with appropriate choices.
118 * #combobox: The combo box.
119 * @protocol: IMAP or POP.
121 void easysetup_serversecurity_combo_box_fill (EasysetupServersecurityComboBox *combobox, ModestProtocol protocol)
123 EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
125 /* Remove any existing rows: */
126 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
127 gtk_list_store_clear (liststore);
130 gtk_list_store_append (liststore, &iter);
131 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_NONE, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_none"), -1);
133 /* Select the None item: */
134 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
136 gtk_list_store_append (liststore, &iter);
137 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_TLS, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_normal"), -1);
139 /* Add security choices with protocol-specific names, as in the UI spec:
140 * (Note: Changing the title seems pointless. murrayc) */
141 if(protocol == MODEST_PROTOCOL_STORE_POP) {
142 gtk_list_store_append (liststore, &iter);
143 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_securepop3s"), -1);
144 } else if(protocol == MODEST_PROTOCOL_STORE_IMAP) {
145 gtk_list_store_append (liststore, &iter);
146 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_secureimap4"), -1);
147 } else if(protocol == MODEST_PROTOCOL_TRANSPORT_SMTP) {
148 gtk_list_store_append (liststore, &iter);
149 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_ssl"), -1);
154 * Returns the selected serversecurity,
155 * or MODEST_PROTOCOL_UNKNOWN if no serversecurity was selected.
158 easysetup_serversecurity_combo_box_get_active_serversecurity (EasysetupServersecurityComboBox *combobox)
161 const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
163 EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
165 ModestProtocol serversecurity = MODEST_PROTOCOL_UNKNOWN;
166 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &serversecurity, -1);
167 return serversecurity;
170 return MODEST_PROTOCOL_UNKNOWN; /* Failed. */
173 /* This allows us to pass more than one piece of data to the signal handler,
174 * and get a result: */
177 EasysetupServersecurityComboBox* self;
183 on_model_foreach_select_id(GtkTreeModel *model,
184 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
186 ForEachData *state = (ForEachData*)(user_data);
188 /* Select the item if it has the matching ID: */
190 gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1);
191 if(id == state->id) {
192 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
195 return TRUE; /* Stop walking the tree. */
198 return FALSE; /* Keep walking the tree. */
202 * Selects the specified serversecurity,
203 * or MODEST_PROTOCOL_UNKNOWN if no serversecurity was selected.
206 easysetup_serversecurity_combo_box_set_active_serversecurity (EasysetupServersecurityComboBox *combobox, ModestProtocol serversecurity)
208 EasysetupServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
210 /* Create a state instance so we can send two items of data to the signal handler: */
211 ForEachData *state = g_new0 (ForEachData, 1);
212 state->self = combobox;
213 state->id = serversecurity;
214 state->found = FALSE;
216 /* Look at each item, and select the one with the correct ID: */
217 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
219 const gboolean result = state->found;
221 /* Free the state instance: */