1 /* Copyright (c) 2007, Nokia Corporation
6 #include "modest-easysetup-servertype-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 (EasysetupServertypeComboBox, easysetup_servertype_combo_box, GTK_TYPE_COMBO_BOX);
22 #define SERVERTYPE_COMBO_BOX_GET_PRIVATE(o) \
23 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_SERVERTYPE_COMBO_BOX, EasysetupServertypeComboBoxPrivate))
25 typedef struct _EasysetupServertypeComboBoxPrivate EasysetupServertypeComboBoxPrivate;
27 struct _EasysetupServertypeComboBoxPrivate
33 easysetup_servertype_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_servertype_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_servertype_combo_box_dispose (GObject *object)
55 if (G_OBJECT_CLASS (easysetup_servertype_combo_box_parent_class)->dispose)
56 G_OBJECT_CLASS (easysetup_servertype_combo_box_parent_class)->dispose (object);
60 easysetup_servertype_combo_box_finalize (GObject *object)
62 EasysetupServertypeComboBoxPrivate *priv = SERVERTYPE_COMBO_BOX_GET_PRIVATE (object);
64 g_object_unref (G_OBJECT (priv->model));
66 G_OBJECT_CLASS (easysetup_servertype_combo_box_parent_class)->finalize (object);
70 easysetup_servertype_combo_box_class_init (EasysetupServertypeComboBoxClass *klass)
72 GObjectClass *object_class = G_OBJECT_CLASS (klass);
74 g_type_class_add_private (klass, sizeof (EasysetupServertypeComboBoxPrivate));
76 object_class->get_property = easysetup_servertype_combo_box_get_property;
77 object_class->set_property = easysetup_servertype_combo_box_set_property;
78 object_class->dispose = easysetup_servertype_combo_box_dispose;
79 object_class->finalize = easysetup_servertype_combo_box_finalize;
83 MODEL_COL_NAME = 0, /* a string */
84 MODEL_COL_ID = 1 /* an int. */
88 easysetup_servertype_combo_box_fill (EasysetupServertypeComboBox *combobox);
91 easysetup_servertype_combo_box_init (EasysetupServertypeComboBox *self)
93 EasysetupServertypeComboBoxPrivate *priv = SERVERTYPE_COMBO_BOX_GET_PRIVATE (self);
95 /* Create a tree model for the combo box,
96 * with a string for the name, and an ID for the servertype.
97 * This must match our MODEL_COLS enum constants.
99 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
101 /* Setup the combo box: */
102 GtkComboBox *combobox = GTK_COMBO_BOX (self);
103 gtk_combo_box_set_model (combobox, priv->model);
105 /* Servertype column:
106 * The ID model column in not shown in the view. */
107 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
108 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
109 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
110 "text", MODEL_COL_NAME, NULL);
112 easysetup_servertype_combo_box_fill (self);
115 EasysetupServertypeComboBox*
116 easysetup_servertype_combo_box_new (void)
118 return g_object_new (EASYSETUP_TYPE_SERVERTYPE_COMBO_BOX, NULL);
121 void easysetup_servertype_combo_box_fill (EasysetupServertypeComboBox *combobox)
123 EasysetupServertypeComboBoxPrivate *priv = SERVERTYPE_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_STORE_POP, MODEL_COL_NAME, _("mail_fi_emailtype_pop3"), -1);
133 /* Select the POP 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_STORE_IMAP, MODEL_COL_NAME, _("mail_fi_emailtype_imap"), -1);
141 * Returns the selected servertype,
142 * or MODEST_PROTOCOL_UNKNOWN if no servertype was selected.
145 easysetup_servertype_combo_box_get_active_servertype (EasysetupServertypeComboBox *combobox)
148 const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
150 EasysetupServertypeComboBoxPrivate *priv = SERVERTYPE_COMBO_BOX_GET_PRIVATE (combobox);
152 ModestProtocol servertype = MODEST_PROTOCOL_UNKNOWN;
153 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &servertype, -1);
157 return MODEST_PROTOCOL_UNKNOWN; /* Failed. */
160 /* This allows us to pass more than one piece of data to the signal handler,
161 * and get a result: */
164 EasysetupServertypeComboBox* self;
170 on_model_foreach_select_id(GtkTreeModel *model,
171 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
173 ForEachData *state = (ForEachData*)(user_data);
175 /* Select the item if it has the matching ID: */
177 gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1);
178 if(id == state->id) {
179 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
182 return TRUE; /* Stop walking the tree. */
185 return FALSE; /* Keep walking the tree. */
189 * Selects the specified servertype,
190 * or MODEST_PROTOCOL_UNKNOWN if no servertype was selected.
193 easysetup_servertype_combo_box_set_active_servertype (EasysetupServertypeComboBox *combobox, ModestProtocol servertype)
195 EasysetupServertypeComboBoxPrivate *priv = SERVERTYPE_COMBO_BOX_GET_PRIVATE (combobox);
197 /* Create a state instance so we can send two items of data to the signal handler: */
198 ForEachData *state = g_new0 (ForEachData, 1);
199 state->self = combobox;
200 state->id = servertype;
201 state->found = FALSE;
203 /* Look at each item, and select the one with the correct ID: */
204 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
206 const gboolean result = state->found;
208 /* Free the state instance: */