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-retrieve-picker.h"
31 #include "modest-defs.h" /* For the conf names. */
32 #include "modest-account-settings.h"
33 #include <gtk/gtkliststore.h>
34 #include <gtk/gtkcelllayout.h>
35 #include <gtk/gtkcellrenderertext.h>
36 #include <glib/gi18n.h>
39 #include <string.h> /* For memcpy() */
41 /* Include config.h so that _() works: */
46 G_DEFINE_TYPE (ModestRetrievePicker, modest_retrieve_picker, HILDON_TYPE_PICKER_BUTTON);
48 #define MODEST_RETRIEVE_PICKER_GET_PRIVATE(o) \
49 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_RETRIEVE_PICKER, ModestRetrievePickerPrivate))
51 typedef struct _ModestRetrievePickerPrivate ModestRetrievePickerPrivate;
53 struct _ModestRetrievePickerPrivate
59 MODEL_COL_NAME = 0, /* a string */
60 MODEL_COL_RETRIEVE_TYPE = 1 /* a gint (a ModestAccountRetrieveType) */
63 void modest_retrieve_picker_fill (ModestRetrievePicker *picker, ModestProtocolType protocol);
66 touch_selector_print_func (HildonTouchSelector *selector)
69 if (hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &iter)) {
73 model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
74 gtk_tree_model_get_value (model, &iter, MODEL_COL_NAME, &value);
75 return g_value_dup_string (&value);
81 modest_retrieve_picker_finalize (GObject *object)
83 ModestRetrievePickerPrivate *priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (object);
85 g_object_unref (G_OBJECT (priv->model));
87 G_OBJECT_CLASS (modest_retrieve_picker_parent_class)->finalize (object);
91 modest_retrieve_picker_class_init (ModestRetrievePickerClass *klass)
93 GObjectClass *object_class = G_OBJECT_CLASS (klass);
95 g_type_class_add_private (klass, sizeof (ModestRetrievePickerPrivate));
97 object_class->finalize = modest_retrieve_picker_finalize;
101 modest_retrieve_picker_init (ModestRetrievePicker *self)
103 ModestRetrievePickerPrivate *priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (self);
110 ModestRetrievePicker*
111 modest_retrieve_picker_new (void)
113 ModestRetrievePicker *self;
114 ModestRetrievePickerPrivate *priv;
115 GtkCellRenderer *renderer;
118 self = g_object_new (MODEST_TYPE_RETRIEVE_PICKER,
119 "arrangement", HILDON_BUTTON_ARRANGEMENT_VERTICAL,
120 "size", HILDON_SIZE_AUTO,
122 priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (self);
124 /* Create a tree model,
125 * with a string for the name, and an ID for the servertype.
126 * This must match our MODEL_COLS enum constants.
128 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
129 renderer = gtk_cell_renderer_text_new ();
130 g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
132 selector = hildon_touch_selector_new ();
133 hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), GTK_TREE_MODEL (priv->model),
134 renderer, "text", MODEL_COL_NAME, NULL);
136 hildon_touch_selector_set_model (HILDON_TOUCH_SELECTOR (selector), 0, GTK_TREE_MODEL (priv->model));
137 hildon_touch_selector_set_print_func (HILDON_TOUCH_SELECTOR (selector), touch_selector_print_func);
139 hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self), HILDON_TOUCH_SELECTOR (selector));
144 /* Fill the picker box with appropriate choices.
145 * #picker: The picker box.
146 * @protocol: IMAP or POP.
148 void modest_retrieve_picker_fill (ModestRetrievePicker *picker, ModestProtocolType protocol)
150 ModestRetrievePickerPrivate *priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (picker);
152 /* Remove any existing rows: */
153 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
154 gtk_list_store_clear (liststore);
157 gtk_list_store_append (liststore, &iter);
158 gtk_list_store_set (liststore, &iter,
159 MODEL_COL_RETRIEVE_TYPE, MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY,
160 MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_headers"), -1);
162 gtk_list_store_append (liststore, &iter);
163 gtk_list_store_set (liststore, &iter,
164 MODEL_COL_RETRIEVE_TYPE, MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS,
165 MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_messages_attachments"), -1);
169 * Returns the selected retrieve.
170 * or NULL if no retrieve was selected. The result must be freed with g_free().
172 ModestAccountRetrieveType
173 modest_retrieve_picker_get_active_retrieve_conf (ModestRetrievePicker *picker)
179 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (picker)));
180 found = hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &active);
182 ModestRetrievePickerPrivate *priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (picker);
184 ModestAccountRetrieveType retrieve_type = MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
185 gtk_tree_model_get (priv->model, &active, MODEL_COL_RETRIEVE_TYPE, &retrieve_type, -1);
186 return retrieve_type;
189 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY; /* Failed. */
192 /* This allows us to pass more than one piece of data to the signal handler,
193 * and get a result: */
196 ModestRetrievePicker* self;
197 ModestAccountRetrieveType retrieve_type;
202 on_model_foreach_select_id(GtkTreeModel *model,
203 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
205 ForEachData *state = (ForEachData*)(user_data);
207 gboolean result = FALSE;
209 /* Select the item if it has the matching name: */
210 ModestAccountRetrieveType retrieve_type;
211 gtk_tree_model_get (model, iter, MODEL_COL_RETRIEVE_TYPE, &retrieve_type, -1);
212 if (retrieve_type == state->retrieve_type) {
214 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (state->self)));
215 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, iter, TRUE);
216 hildon_button_set_value (HILDON_BUTTON (state->self),
217 hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
220 return TRUE; /* Stop walking the tree. */
223 return result; /* Whether we keep walking the tree. */
227 * Selects the specified retrieve,
228 * or FALSE if no retrieve was selected.
231 modest_retrieve_picker_set_active_retrieve_conf (ModestRetrievePicker *picker,
232 ModestAccountRetrieveType retrieve_type)
234 ModestRetrievePickerPrivate *priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (picker);
236 /* Create a state instance so we can send two items of data to the signal handler: */
237 ForEachData *state = g_new0 (ForEachData, 1);
238 state->self = picker;
239 state->retrieve_type = retrieve_type;
240 state->found = FALSE;
242 /* Look at each item, and select the one with the correct ID: */
243 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
245 const gboolean result = state->found;
247 /* Free the state instance: */