1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <modest-runtime.h>
31 #include "modest-easysetup-servertype-combo-box.h"
32 #include <gtk/gtkliststore.h>
33 #include <gtk/gtkcelllayout.h>
34 #include <gtk/gtkcellrenderertext.h>
35 #include <glib/gi18n.h>
38 #include <string.h> /* For memcpy() */
40 /* Include config.h so that _() works: */
45 G_DEFINE_TYPE (EasysetupServertypeComboBox, easysetup_servertype_combo_box, GTK_TYPE_COMBO_BOX);
47 #define SERVERTYPE_COMBO_BOX_GET_PRIVATE(o) \
48 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_SERVERTYPE_COMBO_BOX, EasysetupServertypeComboBoxPrivate))
50 typedef struct _EasysetupServertypeComboBoxPrivate EasysetupServertypeComboBoxPrivate;
52 struct _EasysetupServertypeComboBoxPrivate
58 easysetup_servertype_combo_box_get_property (GObject *object, guint property_id,
59 GValue *value, GParamSpec *pspec)
61 switch (property_id) {
63 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
68 easysetup_servertype_combo_box_set_property (GObject *object, guint property_id,
69 const GValue *value, GParamSpec *pspec)
71 switch (property_id) {
73 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
78 easysetup_servertype_combo_box_dispose (GObject *object)
80 if (G_OBJECT_CLASS (easysetup_servertype_combo_box_parent_class)->dispose)
81 G_OBJECT_CLASS (easysetup_servertype_combo_box_parent_class)->dispose (object);
85 easysetup_servertype_combo_box_finalize (GObject *object)
87 EasysetupServertypeComboBoxPrivate *priv = SERVERTYPE_COMBO_BOX_GET_PRIVATE (object);
89 g_object_unref (G_OBJECT (priv->model));
91 G_OBJECT_CLASS (easysetup_servertype_combo_box_parent_class)->finalize (object);
95 easysetup_servertype_combo_box_class_init (EasysetupServertypeComboBoxClass *klass)
97 GObjectClass *object_class = G_OBJECT_CLASS (klass);
99 g_type_class_add_private (klass, sizeof (EasysetupServertypeComboBoxPrivate));
101 object_class->get_property = easysetup_servertype_combo_box_get_property;
102 object_class->set_property = easysetup_servertype_combo_box_set_property;
103 object_class->dispose = easysetup_servertype_combo_box_dispose;
104 object_class->finalize = easysetup_servertype_combo_box_finalize;
108 MODEL_COL_NAME = 0, /* a string */
109 MODEL_COL_ID = 1 /* an int. */
113 easysetup_servertype_combo_box_init (EasysetupServertypeComboBox *self)
115 EasysetupServertypeComboBoxPrivate *priv = SERVERTYPE_COMBO_BOX_GET_PRIVATE (self);
117 /* Create a tree model for the combo box,
118 * with a string for the name, and an ID for the servertype.
119 * This must match our MODEL_COLS enum constants.
121 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
123 /* Setup the combo box: */
124 GtkComboBox *combobox = GTK_COMBO_BOX (self);
125 gtk_combo_box_set_model (combobox, priv->model);
127 /* Servertype column:
128 * The ID model column in not shown in the view. */
129 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
130 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
131 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
132 "text", MODEL_COL_NAME, NULL);
136 easysetup_servertype_combo_box_fill (EasysetupServertypeComboBox *combobox,
137 gboolean filter_providers)
139 EasysetupServertypeComboBoxPrivate *priv;
140 GtkListStore *liststore;
141 ModestProtocolRegistry *protocol_registry;
142 GSList *remote_protocols, *node;
145 /* Remove any existing rows: */
146 priv = SERVERTYPE_COMBO_BOX_GET_PRIVATE (combobox);
147 protocol_registry = modest_runtime_get_protocol_registry ();
148 remote_protocols = modest_protocol_registry_get_by_tag (protocol_registry, MODEST_PROTOCOL_REGISTRY_REMOTE_STORE_PROTOCOLS);
150 liststore = GTK_LIST_STORE (priv->model);
151 gtk_list_store_clear (liststore);
153 for (node = remote_protocols; node != NULL; node = g_slist_next (node)) {
154 ModestProtocol* protocol;
157 protocol = (ModestProtocol *) node->data;
159 /* Do not include the protocols that would be listed
160 in the providers combo */
161 if (filter_providers)
162 if (modest_protocol_registry_protocol_type_is_provider (protocol_registry,
163 modest_protocol_get_type_id (protocol))) {
168 gtk_list_store_append (liststore, &iter);
169 gtk_list_store_set (liststore, &iter,
171 modest_protocol_get_type_id (protocol),
173 modest_protocol_get_display_name (protocol),
178 g_slist_free (remote_protocols);
181 EasysetupServertypeComboBox*
182 easysetup_servertype_combo_box_new (gboolean filter_providers)
184 EasysetupServertypeComboBox *combo;
186 combo = g_object_new (EASYSETUP_TYPE_SERVERTYPE_COMBO_BOX, NULL);
189 easysetup_servertype_combo_box_fill (combo, filter_providers);
195 * Returns the selected servertype,
196 * or MODEST_PROTOCOL_REGISTRY_TYPE_INVALID if no servertype was selected.
199 easysetup_servertype_combo_box_get_active_servertype (EasysetupServertypeComboBox *combobox)
204 found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
206 EasysetupServertypeComboBoxPrivate *priv;
207 ModestProtocolType servertype;
209 priv = SERVERTYPE_COMBO_BOX_GET_PRIVATE (combobox);
211 servertype = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
212 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &servertype, -1);
216 return MODEST_PROTOCOL_REGISTRY_TYPE_INVALID; /* Failed. */
219 /* This allows us to pass more than one piece of data to the signal handler,
220 * and get a result: */
223 EasysetupServertypeComboBox* self;
224 ModestProtocolType id;
229 on_model_foreach_select_id(GtkTreeModel *model,
230 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
232 ModestProtocolType id = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
233 ForEachData *state = (ForEachData*)(user_data);
235 /* Select the item if it has the matching ID: */
236 gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1);
237 if(id == state->id) {
238 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
241 return TRUE; /* Stop walking the tree. */
244 return FALSE; /* Keep walking the tree. */
248 * Selects the specified servertype,
249 * or MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN if no servertype was selected.
252 easysetup_servertype_combo_box_set_active_servertype (EasysetupServertypeComboBox *combobox, ModestProtocolType servertype)
254 EasysetupServertypeComboBoxPrivate *priv;
258 priv = SERVERTYPE_COMBO_BOX_GET_PRIVATE (combobox);
260 /* Create a state instance so we can send two items of data to the signal handler: */
261 state = g_new0 (ForEachData, 1);
262 state->self = combobox;
263 state->id = servertype;
264 state->found = FALSE;
266 /* Look at each item, and select the one with the correct ID: */
267 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
269 result = state->found;
271 /* Free the state instance: */