1 /* Copyright (c) 2007, Nokia Corporation
6 #include "modest-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 (ModestServersecurityComboBox, modest_serversecurity_combo_box, GTK_TYPE_COMBO_BOX);
22 #define SERVERSECURITY_COMBO_BOX_GET_PRIVATE(o) \
23 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_SERVERSECURITY_COMBO_BOX, ModestServersecurityComboBoxPrivate))
25 typedef struct _ModestServersecurityComboBoxPrivate ModestServersecurityComboBoxPrivate;
27 struct _ModestServersecurityComboBoxPrivate
30 ModestProtocol protocol;
34 modest_serversecurity_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_serversecurity_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_serversecurity_combo_box_dispose (GObject *object)
56 if (G_OBJECT_CLASS (modest_serversecurity_combo_box_parent_class)->dispose)
57 G_OBJECT_CLASS (modest_serversecurity_combo_box_parent_class)->dispose (object);
61 modest_serversecurity_combo_box_finalize (GObject *object)
63 ModestServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (object);
65 g_object_unref (G_OBJECT (priv->model));
67 G_OBJECT_CLASS (modest_serversecurity_combo_box_parent_class)->finalize (object);
71 modest_serversecurity_combo_box_class_init (ModestServersecurityComboBoxClass *klass)
73 GObjectClass *object_class = G_OBJECT_CLASS (klass);
75 g_type_class_add_private (klass, sizeof (ModestServersecurityComboBoxPrivate));
77 object_class->get_property = modest_serversecurity_combo_box_get_property;
78 object_class->set_property = modest_serversecurity_combo_box_set_property;
79 object_class->dispose = modest_serversecurity_combo_box_dispose;
80 object_class->finalize = modest_serversecurity_combo_box_finalize;
84 MODEL_COL_NAME = 0, /* a string */
85 MODEL_COL_ID = 1 /* an int. */
89 modest_serversecurity_combo_box_init (ModestServersecurityComboBox *self)
91 ModestServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (self);
93 /* Create a tree model for the combo box,
94 * with a string for the name, and an ID for the serversecurity.
95 * This must match our MODEL_COLS enum constants.
97 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
99 /* Setup the combo box: */
100 GtkComboBox *combobox = GTK_COMBO_BOX (self);
101 gtk_combo_box_set_model (combobox, priv->model);
103 /* Serversecurity column:
104 * The ID model column in not shown in the view. */
105 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
106 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
107 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
108 "text", MODEL_COL_NAME, NULL);
110 /* The application must call modest_serversecurity_combo_box_fill().
114 ModestServersecurityComboBox*
115 modest_serversecurity_combo_box_new (void)
117 return g_object_new (MODEST_TYPE_SERVERSECURITY_COMBO_BOX, NULL);
120 /* Fill the combo box with appropriate choices.
121 * #combobox: The combo box.
122 * @protocol: IMAP or POP.
124 void modest_serversecurity_combo_box_fill (ModestServersecurityComboBox *combobox, ModestProtocol protocol)
126 ModestServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
127 priv->protocol = protocol; /* Remembered for later. */
129 /* Remove any existing rows: */
130 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
131 gtk_list_store_clear (liststore);
134 gtk_list_store_append (liststore, &iter);
135 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_NONE, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_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_SECURITY_TLS, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_normal"), -1);
143 /* Add security choices with protocol-specific names, as in the UI spec:
144 * (Note: Changing the title seems pointless. murrayc) */
145 if(protocol == MODEST_PROTOCOL_STORE_POP) {
146 gtk_list_store_append (liststore, &iter);
147 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_securepop3s"), -1);
148 } else if(protocol == MODEST_PROTOCOL_STORE_IMAP) {
149 gtk_list_store_append (liststore, &iter);
150 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_secureimap4"), -1);
151 } else if(protocol == MODEST_PROTOCOL_TRANSPORT_SMTP) {
152 gtk_list_store_append (liststore, &iter);
153 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_SECURITY_SSL, MODEL_COL_NAME, _("mcen_fi_advsetup_other_security_ssl"), -1);
157 static gint get_port_for_security (ModestProtocol protocol, ModestProtocol security)
159 /* See the UI spec, section Email Wizards, Incoming Details [MSG-WIZ001]: */
162 /* Get the default port number for this protocol with this security: */
163 if(protocol == MODEST_PROTOCOL_STORE_POP) {
164 if ((security == MODEST_PROTOCOL_SECURITY_NONE) || (security == MODEST_PROTOCOL_SECURITY_TLS))
166 else if (security == MODEST_PROTOCOL_SECURITY_SSL)
168 } else if (protocol == MODEST_PROTOCOL_STORE_IMAP) {
169 if ((security == MODEST_PROTOCOL_SECURITY_NONE) || (security == MODEST_PROTOCOL_SECURITY_TLS))
171 else if (security == MODEST_PROTOCOL_SECURITY_SSL)
173 } else if (protocol == MODEST_PROTOCOL_TRANSPORT_SMTP) {
174 if ((security == MODEST_PROTOCOL_SECURITY_NONE) || (security == MODEST_PROTOCOL_SECURITY_TLS))
176 else if (security == MODEST_PROTOCOL_SECURITY_SSL)
184 * Returns the selected serversecurity,
185 * or MODEST_PROTOCOL_UNKNOWN if no serversecurity was selected.
188 modest_serversecurity_combo_box_get_active_serversecurity (ModestServersecurityComboBox *combobox)
191 const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
193 ModestServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
195 ModestProtocol serversecurity = MODEST_PROTOCOL_UNKNOWN;
196 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &serversecurity, -1);
197 return serversecurity;
200 return MODEST_PROTOCOL_UNKNOWN; /* Failed. */
204 * Returns the default port to be used for the selected serversecurity,
205 * or 0 if no serversecurity was selected.
208 modest_serversecurity_combo_box_get_active_serversecurity_port (ModestServersecurityComboBox *combobox)
210 ModestServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
212 const ModestProtocol security = modest_serversecurity_combo_box_get_active_serversecurity
214 return get_port_for_security (priv->protocol, security);
217 /* This allows us to pass more than one piece of data to the signal handler,
218 * and get a result: */
221 ModestServersecurityComboBox* self;
227 on_model_foreach_select_id(GtkTreeModel *model,
228 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
230 ForEachData *state = (ForEachData*)(user_data);
232 /* Select the item if it has the matching ID: */
234 gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1);
235 if(id == state->id) {
236 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
239 return TRUE; /* Stop walking the tree. */
242 return FALSE; /* Keep walking the tree. */
246 * Selects the specified serversecurity,
247 * or MODEST_PROTOCOL_UNKNOWN if no serversecurity was selected.
250 modest_serversecurity_combo_box_set_active_serversecurity (ModestServersecurityComboBox *combobox, ModestProtocol serversecurity)
252 ModestServersecurityComboBoxPrivate *priv = SERVERSECURITY_COMBO_BOX_GET_PRIVATE (combobox);
254 /* Create a state instance so we can send two items of data to the signal handler: */
255 ForEachData *state = g_new0 (ForEachData, 1);
256 state->self = combobox;
257 state->id = serversecurity;
258 state->found = FALSE;
260 /* Look at each item, and select the one with the correct ID: */
261 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
263 const gboolean result = state->found;
265 /* Free the state instance: */