1 /* Copyright (c) 2006, 2008 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-servertype-picker.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 (ModestServertypePicker, modest_servertype_picker, HILDON_TYPE_PICKER_BUTTON);
47 #define MODEST_SERVERTYPE_PICKER_GET_PRIVATE(o) \
48 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_SERVERTYPE_PICKER, ModestServertypePickerPrivate))
50 typedef struct _ModestServertypePickerPrivate ModestServertypePickerPrivate;
52 struct _ModestServertypePickerPrivate
58 modest_servertype_picker_finalize (GObject *object)
60 ModestServertypePickerPrivate *priv = MODEST_SERVERTYPE_PICKER_GET_PRIVATE (object);
62 g_object_unref (G_OBJECT (priv->model));
64 G_OBJECT_CLASS (modest_servertype_picker_parent_class)->finalize (object);
68 modest_servertype_picker_class_init (ModestServertypePickerClass *klass)
70 GObjectClass *object_class = G_OBJECT_CLASS (klass);
72 g_type_class_add_private (klass, sizeof (ModestServertypePickerPrivate));
74 object_class->finalize = modest_servertype_picker_finalize;
78 MODEL_COL_NAME = 0, /* a string */
79 MODEL_COL_ID = 1 /* an int. */
83 modest_servertype_picker_init (ModestServertypePicker *self)
85 ModestServertypePickerPrivate *priv;
87 priv = MODEST_SERVERTYPE_PICKER_GET_PRIVATE (self);
93 touch_selector_print_func (HildonTouchSelector *selector, gpointer userdata)
96 if (hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &iter)) {
100 model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
101 gtk_tree_model_get_value (model, &iter, MODEL_COL_NAME, &value);
102 return g_value_dup_string (&value);
109 modest_servertype_picker_fill (ModestServertypePicker *self,
110 gboolean filter_providers)
112 ModestServertypePickerPrivate *priv;
113 GtkListStore *liststore;
114 ModestProtocolRegistry *protocol_registry;
115 GSList *remote_protocols, *node;
119 /* Remove any existing rows: */
120 priv = MODEST_SERVERTYPE_PICKER_GET_PRIVATE (self);
121 protocol_registry = modest_runtime_get_protocol_registry ();
122 remote_protocols = modest_protocol_registry_get_by_tag (protocol_registry, MODEST_PROTOCOL_REGISTRY_REMOTE_STORE_PROTOCOLS);
124 liststore = GTK_LIST_STORE (priv->model);
125 gtk_list_store_clear (liststore);
127 for (node = remote_protocols; node != NULL; node = g_slist_next (node)) {
128 ModestProtocol* protocol;
131 protocol = (ModestProtocol *) node->data;
133 /* Do not include the protocols that would be listed
134 in the providers combo */
135 if (filter_providers)
136 if (modest_protocol_registry_protocol_type_is_provider (protocol_registry,
137 modest_protocol_get_type_id (protocol))) {
142 gtk_list_store_append (liststore, &iter);
143 gtk_list_store_set (liststore, &iter,
145 modest_protocol_get_type_id (protocol),
147 modest_protocol_get_display_name (protocol),
152 g_slist_free (remote_protocols);
154 /* Choose first in list */
155 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (self)));
156 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->model), &iter)) {
157 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, &iter, TRUE);
161 ModestServertypePicker*
162 modest_servertype_picker_new (HildonSizeType size,
163 HildonButtonArrangement arrangement,
164 gboolean filter_providers)
166 ModestServertypePicker *self;
167 ModestServertypePickerPrivate *priv;
168 GtkCellRenderer *renderer;
171 self = g_object_new (MODEST_TYPE_SERVERTYPE_PICKER,
172 "arrangement", arrangement,
175 priv = MODEST_SERVERTYPE_PICKER_GET_PRIVATE (self);
177 /* Create a tree model,
178 * with a string for the name, and an ID for the servertype.
179 * This must match our MODEL_COLS enum constants.
181 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
182 renderer = gtk_cell_renderer_text_new ();
183 g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
185 selector = hildon_touch_selector_new ();
186 hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), GTK_TREE_MODEL (priv->model),
187 renderer, "text", MODEL_COL_NAME, NULL);
189 hildon_touch_selector_set_model (HILDON_TOUCH_SELECTOR (selector), 0, GTK_TREE_MODEL (priv->model));
190 hildon_touch_selector_set_print_func (HILDON_TOUCH_SELECTOR (selector), (HildonTouchSelectorPrintFunc) touch_selector_print_func);
192 hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self), HILDON_TOUCH_SELECTOR (selector));
195 modest_servertype_picker_fill (self, filter_providers);
201 * Returns the selected servertype,
202 * or MODEST_PROTOCOL_REGISTRY_TYPE_INVALID if no servertype was selected.
205 modest_servertype_picker_get_active_servertype (ModestServertypePicker *self)
211 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (self)));
212 found = hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &active);
214 ModestServertypePickerPrivate *priv;
215 ModestProtocolType servertype;
217 priv = MODEST_SERVERTYPE_PICKER_GET_PRIVATE (self);
219 servertype = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
220 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &servertype, -1);
224 return MODEST_PROTOCOL_REGISTRY_TYPE_INVALID; /* Failed. */
227 /* This allows us to pass more than one piece of data to the signal handler,
228 * and get a result: */
231 ModestServertypePicker* self;
232 ModestProtocolType id;
237 on_model_foreach_select_id(GtkTreeModel *model,
238 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
240 ModestProtocolType id = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
241 ForEachData *state = (ForEachData*)(user_data);
244 /* Select the item if it has the matching ID: */
245 gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1);
246 if(id == state->id) {
247 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (state->self)));
248 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, iter, TRUE);
249 hildon_button_set_value (HILDON_BUTTON (state->self),
250 hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
253 return TRUE; /* Stop walking the tree. */
256 return FALSE; /* Keep walking the tree. */
260 * Selects the specified servertype,
261 * or MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN if no servertype was selected.
264 modest_servertype_picker_set_active_servertype (ModestServertypePicker *picker, ModestProtocolType servertype)
266 ModestServertypePickerPrivate *priv;
270 priv = MODEST_SERVERTYPE_PICKER_GET_PRIVATE (picker);
272 /* Create a state instance so we can send two items of data to the signal handler: */
273 state = g_new0 (ForEachData, 1);
274 state->self = picker;
275 state->id = servertype;
276 state->found = FALSE;
278 /* Look at each item, and select the one with the correct ID: */
279 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
281 result = state->found;
283 /* Free the state instance: */