1 /* Copyright (c) 2007, 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-limit-retrieve-picker.h"
31 #include <gtk/gtkliststore.h>
32 #include <gtk/gtkcelllayout.h>
33 #include <gtk/gtkcellrenderertext.h>
34 #include <glib/gi18n.h>
37 #include <string.h> /* For memcpy() */
39 /* Include config.h so that _() works: */
44 G_DEFINE_TYPE (ModestLimitRetrievePicker, modest_limit_retrieve_picker, HILDON_TYPE_PICKER_BUTTON);
46 #define MODEST_LIMIT_RETRIEVE_PICKER_GET_PRIVATE(o) \
47 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_LIMIT_RETRIEVE_PICKER, ModestLimitRetrievePickerPrivate))
49 typedef struct _ModestLimitRetrievePickerPrivate ModestLimitRetrievePickerPrivate;
51 struct _ModestLimitRetrievePickerPrivate
57 modest_limit_retrieve_picker_finalize (GObject *object)
59 ModestLimitRetrievePickerPrivate *priv = MODEST_LIMIT_RETRIEVE_PICKER_GET_PRIVATE (object);
61 g_object_unref (G_OBJECT (priv->model));
63 G_OBJECT_CLASS (modest_limit_retrieve_picker_parent_class)->finalize (object);
67 modest_limit_retrieve_picker_class_init (ModestLimitRetrievePickerClass *klass)
69 GObjectClass *object_class = G_OBJECT_CLASS (klass);
71 g_type_class_add_private (klass, sizeof (ModestLimitRetrievePickerPrivate));
73 object_class->finalize = modest_limit_retrieve_picker_finalize;
77 MODEL_COL_NAME = 0, /* a string */
78 MODEL_COL_NUM = 1 /* an int */
81 static void modest_limit_retrieve_picker_fill (ModestLimitRetrievePicker *picker);
84 touch_selector_print_func (HildonTouchSelector *selector, gpointer userdata)
87 if (hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &iter)) {
91 model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
92 gtk_tree_model_get_value (model, &iter, MODEL_COL_NAME, &value);
93 return g_value_dup_string (&value);
99 modest_limit_retrieve_picker_init (ModestLimitRetrievePicker *self)
101 ModestLimitRetrievePickerPrivate *priv = MODEST_LIMIT_RETRIEVE_PICKER_GET_PRIVATE (self);
106 ModestLimitRetrievePicker*
107 modest_limit_retrieve_picker_new (HildonSizeType size,
108 HildonButtonArrangement arrangement)
110 ModestLimitRetrievePicker *self;
111 ModestLimitRetrievePickerPrivate *priv;
112 GtkCellRenderer *renderer;
115 self = g_object_new (MODEST_TYPE_LIMIT_RETRIEVE_PICKER,
116 "arrangement", arrangement,
119 priv = MODEST_LIMIT_RETRIEVE_PICKER_GET_PRIVATE (self);
121 /* Create a tree model,
122 * with a string for the name, and an ID for the servertype.
123 * This must match our MODEL_COLS enum constants.
125 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
126 renderer = gtk_cell_renderer_text_new ();
127 g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
129 selector = hildon_touch_selector_new ();
130 hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), GTK_TREE_MODEL (priv->model),
131 renderer, "text", MODEL_COL_NAME, NULL);
133 hildon_touch_selector_set_model (HILDON_TOUCH_SELECTOR (selector), 0, GTK_TREE_MODEL (priv->model));
134 hildon_touch_selector_set_print_func (HILDON_TOUCH_SELECTOR (selector), (HildonTouchSelectorPrintFunc) touch_selector_print_func);
136 hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self), HILDON_TOUCH_SELECTOR (selector));
138 modest_limit_retrieve_picker_fill (self);
143 /* Fill the picker box with appropriate choices.
144 * #picker: The picker box.
145 * @protocol: IMAP or POP.
147 static void modest_limit_retrieve_picker_fill (ModestLimitRetrievePicker *picker)
149 ModestLimitRetrievePickerPrivate *priv = MODEST_LIMIT_RETRIEVE_PICKER_GET_PRIVATE (picker);
151 /* Remove any existing rows: */
152 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
153 gtk_list_store_clear (liststore);
156 gtk_list_store_append (liststore, &iter);
157 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 0, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_nolimit"), -1);
159 gtk_list_store_append (liststore, &iter);
160 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 200, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_200"), -1);
162 gtk_list_store_append (liststore, &iter);
163 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 100, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_100"), -1);
165 gtk_list_store_append (liststore, &iter);
166 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 50, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_50"), -1);
168 gtk_list_store_append (liststore, &iter);
169 gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 20, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_20"), -1);
173 * Returns the selected limit_retrieve,
174 * or 0 if no limit_retrieve was selected.
177 modest_limit_retrieve_picker_get_active_limit_retrieve (ModestLimitRetrievePicker *picker)
183 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (picker)));
184 found = hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &active);
186 ModestLimitRetrievePickerPrivate *priv = MODEST_LIMIT_RETRIEVE_PICKER_GET_PRIVATE (picker);
188 gint limit_retrieve = 0;
189 gtk_tree_model_get (priv->model, &active, MODEL_COL_NUM, &limit_retrieve, -1);
190 return limit_retrieve;
193 return 0; /* Failed. */
196 /* This allows us to pass more than one piece of data to the signal handler,
197 * and get a result: */
200 ModestLimitRetrievePicker* self;
206 on_model_foreach_select_id(GtkTreeModel *model,
207 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
209 ForEachData *state = (ForEachData*)(user_data);
211 gboolean result = FALSE;
213 /* Select the item if it has the matching name: */
215 gtk_tree_model_get (model, iter, MODEL_COL_NUM, &num, -1);
216 if(num == state->num) {
218 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (state->self)));
219 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, iter, TRUE);
220 hildon_button_set_value (HILDON_BUTTON (state->self),
221 hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
224 return TRUE; /* Stop walking the tree. */
227 return result; /* Whether we keep walking the tree. */
231 * Selects the specified limit_retrieve,
232 * or FALSE if no limit_retrieve was selected.
235 modest_limit_retrieve_picker_set_active_limit_retrieve (ModestLimitRetrievePicker *picker, gint limit_retrieve)
237 ModestLimitRetrievePickerPrivate *priv = MODEST_LIMIT_RETRIEVE_PICKER_GET_PRIVATE (picker);
239 /* Create a state instance so we can send two items of data to the signal handler: */
240 ForEachData *state = g_new0 (ForEachData, 1);
241 state->self = picker;
242 state->num = limit_retrieve;
243 state->found = FALSE;
245 /* Look at each item, and select the one with the correct ID: */
246 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
248 const gboolean result = state->found;
250 /* Free the state instance: */