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 (HildonSizeType size,
112 HildonButtonArrangement arrangement)
114 ModestRetrievePicker *self;
115 ModestRetrievePickerPrivate *priv;
116 GtkCellRenderer *renderer;
119 self = g_object_new (MODEST_TYPE_RETRIEVE_PICKER,
120 "arrangement", arrangement,
123 priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (self);
125 /* Create a tree model,
126 * with a string for the name, and an ID for the servertype.
127 * This must match our MODEL_COLS enum constants.
129 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
130 renderer = gtk_cell_renderer_text_new ();
131 g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
133 selector = hildon_touch_selector_new ();
134 hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), GTK_TREE_MODEL (priv->model),
135 renderer, "text", MODEL_COL_NAME, NULL);
137 hildon_touch_selector_set_model (HILDON_TOUCH_SELECTOR (selector), 0, GTK_TREE_MODEL (priv->model));
138 hildon_touch_selector_set_print_func (HILDON_TOUCH_SELECTOR (selector), touch_selector_print_func);
140 hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self), HILDON_TOUCH_SELECTOR (selector));
145 /* Fill the picker box with appropriate choices.
146 * #picker: The picker box.
147 * @protocol: IMAP or POP.
149 void modest_retrieve_picker_fill (ModestRetrievePicker *picker, ModestProtocolType protocol)
151 ModestRetrievePickerPrivate *priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (picker);
153 /* Remove any existing rows: */
154 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
155 gtk_list_store_clear (liststore);
158 gtk_list_store_append (liststore, &iter);
159 gtk_list_store_set (liststore, &iter,
160 MODEL_COL_RETRIEVE_TYPE, MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY,
161 MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_headers"), -1);
163 gtk_list_store_append (liststore, &iter);
164 gtk_list_store_set (liststore, &iter,
165 MODEL_COL_RETRIEVE_TYPE, MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS,
166 MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_messages_attachments"), -1);
170 * Returns the selected retrieve.
171 * or NULL if no retrieve was selected. The result must be freed with g_free().
173 ModestAccountRetrieveType
174 modest_retrieve_picker_get_active_retrieve_conf (ModestRetrievePicker *picker)
180 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (picker)));
181 found = hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &active);
183 ModestRetrievePickerPrivate *priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (picker);
185 ModestAccountRetrieveType retrieve_type = MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
186 gtk_tree_model_get (priv->model, &active, MODEL_COL_RETRIEVE_TYPE, &retrieve_type, -1);
187 return retrieve_type;
190 return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY; /* Failed. */
193 /* This allows us to pass more than one piece of data to the signal handler,
194 * and get a result: */
197 ModestRetrievePicker* self;
198 ModestAccountRetrieveType retrieve_type;
203 on_model_foreach_select_id(GtkTreeModel *model,
204 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
206 ForEachData *state = (ForEachData*)(user_data);
208 gboolean result = FALSE;
210 /* Select the item if it has the matching name: */
211 ModestAccountRetrieveType retrieve_type;
212 gtk_tree_model_get (model, iter, MODEL_COL_RETRIEVE_TYPE, &retrieve_type, -1);
213 if (retrieve_type == state->retrieve_type) {
215 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (state->self)));
216 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, iter, TRUE);
217 hildon_button_set_value (HILDON_BUTTON (state->self),
218 hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
221 return TRUE; /* Stop walking the tree. */
224 return result; /* Whether we keep walking the tree. */
228 * Selects the specified retrieve,
229 * or FALSE if no retrieve was selected.
232 modest_retrieve_picker_set_active_retrieve_conf (ModestRetrievePicker *picker,
233 ModestAccountRetrieveType retrieve_type)
235 ModestRetrievePickerPrivate *priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (picker);
237 /* Create a state instance so we can send two items of data to the signal handler: */
238 ForEachData *state = g_new0 (ForEachData, 1);
239 state->self = picker;
240 state->retrieve_type = retrieve_type;
241 state->found = FALSE;
243 /* Look at each item, and select the one with the correct ID: */
244 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
246 const gboolean result = state->found;
248 /* Free the state instance: */