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-servertype-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 (EasysetupServertypeComboBox, easysetup_servertype_combo_box, GTK_TYPE_COMBO_BOX);
20 #define SERVERTYPE_COMBO_BOX_GET_PRIVATE(o) \
21 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_SERVERTYPE_COMBO_BOX, EasysetupServertypeComboBoxPrivate))
23 typedef struct _EasysetupServertypeComboBoxPrivate EasysetupServertypeComboBoxPrivate;
25 struct _EasysetupServertypeComboBoxPrivate
31 easysetup_servertype_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_servertype_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_servertype_combo_box_dispose (GObject *object)
53 if (G_OBJECT_CLASS (easysetup_servertype_combo_box_parent_class)->dispose)
54 G_OBJECT_CLASS (easysetup_servertype_combo_box_parent_class)->dispose (object);
58 easysetup_servertype_combo_box_finalize (GObject *object)
60 EasysetupServertypeComboBoxPrivate *priv = SERVERTYPE_COMBO_BOX_GET_PRIVATE (object);
62 g_object_unref (G_OBJECT (priv->model));
64 G_OBJECT_CLASS (easysetup_servertype_combo_box_parent_class)->finalize (object);
68 easysetup_servertype_combo_box_class_init (EasysetupServertypeComboBoxClass *klass)
70 GObjectClass *object_class = G_OBJECT_CLASS (klass);
72 g_type_class_add_private (klass, sizeof (EasysetupServertypeComboBoxPrivate));
74 object_class->get_property = easysetup_servertype_combo_box_get_property;
75 object_class->set_property = easysetup_servertype_combo_box_set_property;
76 object_class->dispose = easysetup_servertype_combo_box_dispose;
77 object_class->finalize = easysetup_servertype_combo_box_finalize;
81 MODEL_COL_NAME = 0, /* a string */
82 MODEL_COL_ID = 1 /* an int. */
86 easysetup_servertype_combo_box_fill (EasysetupServertypeComboBox *combobox);
89 easysetup_servertype_combo_box_init (EasysetupServertypeComboBox *self)
91 EasysetupServertypeComboBoxPrivate *priv = SERVERTYPE_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 servertype.
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 /* Servertype 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 easysetup_servertype_combo_box_fill (self);
113 EasysetupServertypeComboBox*
114 easysetup_servertype_combo_box_new (void)
116 return g_object_new (EASYSETUP_TYPE_SERVERTYPE_COMBO_BOX, NULL);
119 void easysetup_servertype_combo_box_fill (EasysetupServertypeComboBox *combobox)
121 EasysetupServertypeComboBoxPrivate *priv = SERVERTYPE_COMBO_BOX_GET_PRIVATE (combobox);
123 /* Remove any existing rows: */
124 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
125 gtk_list_store_clear (liststore);
128 gtk_list_store_append (liststore, &iter);
129 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_STORE_POP, MODEL_COL_NAME, _("mail_fi_emailtype_pop3"), -1);
131 /* Select the POP item: */
132 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
134 gtk_list_store_append (liststore, &iter);
135 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, (gint)MODEST_PROTOCOL_STORE_IMAP, MODEL_COL_NAME, _("mail_fi_emailtype_imap"), -1);
139 * Returns the selected servertype,
140 * or MODEST_PROTOCOL_UNKNOWN if no servertype was selected.
143 easysetup_servertype_combo_box_get_active_servertype (EasysetupServertypeComboBox *combobox)
146 const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
148 EasysetupServertypeComboBoxPrivate *priv = SERVERTYPE_COMBO_BOX_GET_PRIVATE (combobox);
150 ModestProtocol servertype = MODEST_PROTOCOL_UNKNOWN;
151 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &servertype, -1);
155 return MODEST_PROTOCOL_UNKNOWN; /* Failed. */
158 /* This allows us to pass more than one piece of data to the signal handler,
159 * and get a result: */
162 EasysetupServertypeComboBox* self;
168 on_model_foreach_select_id(GtkTreeModel *model,
169 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
171 ForEachData *state = (ForEachData*)(user_data);
173 /* Select the item if it has the matching ID: */
175 gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1);
176 if(id == state->id) {
177 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
180 return TRUE; /* Stop walking the tree. */
183 return FALSE; /* Keep walking the tree. */
187 * Selects the specified servertype,
188 * or MODEST_PROTOCOL_UNKNOWN if no servertype was selected.
191 easysetup_servertype_combo_box_set_active_servertype (EasysetupServertypeComboBox *combobox, ModestProtocol servertype)
193 EasysetupServertypeComboBoxPrivate *priv = SERVERTYPE_COMBO_BOX_GET_PRIVATE (combobox);
195 /* Create a state instance so we can send two items of data to the signal handler: */
196 ForEachData *state = g_new0 (ForEachData, 1);
197 state->self = combobox;
198 state->id = servertype;
199 state->found = FALSE;
201 /* Look at each item, and select the one with the correct ID: */
202 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
204 const gboolean result = state->found;
206 /* Free the state instance: */