* Added limit retrieve picker as a replacement in hildon 2 for the
[modest] / src / hildon2 / modest-limit-retrieve-picker.c
1 /* Copyright (c) 2007, 2008, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
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>
35
36 #include <stdlib.h>
37 #include <string.h> /* For memcpy() */
38
39 /* Include config.h so that _() works: */
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 G_DEFINE_TYPE (ModestLimitRetrievePicker, modest_limit_retrieve_picker, HILDON_TYPE_PICKER_BUTTON);
45
46 #define MODEST_LIMIT_RETRIEVE_PICKER_GET_PRIVATE(o) \
47         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_LIMIT_RETRIEVE_PICKER, ModestLimitRetrievePickerPrivate))
48
49 typedef struct _ModestLimitRetrievePickerPrivate ModestLimitRetrievePickerPrivate;
50
51 struct _ModestLimitRetrievePickerPrivate
52 {
53         GtkTreeModel *model;
54 };
55
56 static void
57 modest_limit_retrieve_picker_finalize (GObject *object)
58 {
59         ModestLimitRetrievePickerPrivate *priv = MODEST_LIMIT_RETRIEVE_PICKER_GET_PRIVATE (object);
60
61         g_object_unref (G_OBJECT (priv->model));
62
63         G_OBJECT_CLASS (modest_limit_retrieve_picker_parent_class)->finalize (object);
64 }
65
66 static void
67 modest_limit_retrieve_picker_class_init (ModestLimitRetrievePickerClass *klass)
68 {
69         GObjectClass *object_class = G_OBJECT_CLASS (klass);
70
71         g_type_class_add_private (klass, sizeof (ModestLimitRetrievePickerPrivate));
72
73         object_class->finalize = modest_limit_retrieve_picker_finalize;
74 }
75
76 enum MODEL_COLS {
77         MODEL_COL_NAME = 0, /* a string */
78         MODEL_COL_NUM = 1 /* an int */
79 };
80
81 static void modest_limit_retrieve_picker_fill (ModestLimitRetrievePicker *picker);
82
83 static gchar *
84 touch_selector_print_func (HildonTouchSelector *selector)
85 {
86         GtkTreeIter iter;
87         if (hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &iter)) {
88                 GtkTreeModel *model;
89                 GValue value = {0,};
90                 
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);
94         }
95         return NULL;
96 }
97
98 static void
99 modest_limit_retrieve_picker_init (ModestLimitRetrievePicker *self)
100 {
101         ModestLimitRetrievePickerPrivate *priv = MODEST_LIMIT_RETRIEVE_PICKER_GET_PRIVATE (self);
102
103         priv->model = NULL;
104 }
105
106 ModestLimitRetrievePicker*
107 modest_limit_retrieve_picker_new (void)
108 {
109         ModestLimitRetrievePicker *self;
110         ModestLimitRetrievePickerPrivate *priv;
111         GtkCellRenderer *renderer;
112         GtkWidget *selector;
113
114         self = g_object_new (MODEST_TYPE_LIMIT_RETRIEVE_PICKER, 
115                              "arrangement", HILDON_BUTTON_ARRANGEMENT_VERTICAL,
116                              "size", HILDON_SIZE_AUTO,
117                              NULL);
118         priv = MODEST_LIMIT_RETRIEVE_PICKER_GET_PRIVATE (self);
119
120         /* Create a tree model,
121          * with a string for the name, and an ID for the servertype.
122          * This must match our MODEL_COLS enum constants.
123          */
124         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
125         renderer = gtk_cell_renderer_text_new ();
126         g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
127
128         selector = hildon_touch_selector_new ();
129         hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), GTK_TREE_MODEL (priv->model),
130                                              renderer, "text", MODEL_COL_NAME, NULL);
131
132         hildon_touch_selector_set_model (HILDON_TOUCH_SELECTOR (selector), 0, GTK_TREE_MODEL (priv->model));
133         hildon_touch_selector_set_print_func (HILDON_TOUCH_SELECTOR (selector), touch_selector_print_func);
134
135         hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self), HILDON_TOUCH_SELECTOR (selector));
136
137         modest_limit_retrieve_picker_fill (self);
138
139         return self;
140 }
141
142 /* Fill the picker box with appropriate choices.
143  * #picker: The picker box.
144  * @protocol: IMAP or POP.
145  */
146 static void modest_limit_retrieve_picker_fill (ModestLimitRetrievePicker *picker)
147 {       
148         ModestLimitRetrievePickerPrivate *priv = MODEST_LIMIT_RETRIEVE_PICKER_GET_PRIVATE (picker);
149         
150         /* Remove any existing rows: */
151         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
152         gtk_list_store_clear (liststore);
153         
154         GtkTreeIter iter;
155         gtk_list_store_append (liststore, &iter);
156         gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 0, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_nolimit"), -1);
157         
158         gtk_list_store_append (liststore, &iter);
159         gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 200, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_200"), -1);
160         
161         gtk_list_store_append (liststore, &iter);
162         gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 100, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_100"), -1);
163
164         gtk_list_store_append (liststore, &iter);
165         gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 50, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_50"), -1);
166
167         gtk_list_store_append (liststore, &iter);
168         gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 20, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_20"), -1);
169 }
170
171 /**
172  * Returns the selected limit_retrieve, 
173  * or 0 if no limit_retrieve was selected.
174  */
175 gint
176 modest_limit_retrieve_picker_get_active_limit_retrieve (ModestLimitRetrievePicker *picker)
177 {
178         GtkTreeIter active;
179         gboolean found;
180         GtkWidget *selector;
181
182         selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (picker)));
183         found = hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &active);
184         if (found) {
185                 ModestLimitRetrievePickerPrivate *priv = MODEST_LIMIT_RETRIEVE_PICKER_GET_PRIVATE (picker);
186
187                 gint limit_retrieve = 0;
188                 gtk_tree_model_get (priv->model, &active, MODEL_COL_NUM, &limit_retrieve, -1);
189                 return limit_retrieve;  
190         }
191
192         return 0; /* Failed. */
193 }
194
195 /* This allows us to pass more than one piece of data to the signal handler,
196  * and get a result: */
197 typedef struct 
198 {
199                 ModestLimitRetrievePicker* self;
200                 gint num;
201                 gboolean found;
202 } ForEachData;
203
204 static gboolean
205 on_model_foreach_select_id(GtkTreeModel *model, 
206         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
207 {
208         ForEachData *state = (ForEachData*)(user_data);
209         
210         gboolean result = FALSE;
211         
212         /* Select the item if it has the matching name: */
213         gint num = 0;
214         gtk_tree_model_get (model, iter, MODEL_COL_NUM, &num, -1); 
215         if(num == state->num) {
216                 GtkWidget *selector;
217                 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (state->self)));
218                 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, iter, TRUE);
219                 hildon_button_set_value (HILDON_BUTTON (state->self),
220                                          hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
221                 
222                 state->found = TRUE;
223                 return TRUE; /* Stop walking the tree. */
224         }
225         
226         return result; /* Whether we keep walking the tree. */
227 }
228
229 /**
230  * Selects the specified limit_retrieve, 
231  * or FALSE if no limit_retrieve was selected.
232  */
233 gboolean
234 modest_limit_retrieve_picker_set_active_limit_retrieve (ModestLimitRetrievePicker *picker, gint limit_retrieve)
235 {
236         ModestLimitRetrievePickerPrivate *priv = MODEST_LIMIT_RETRIEVE_PICKER_GET_PRIVATE (picker);
237         
238         /* Create a state instance so we can send two items of data to the signal handler: */
239         ForEachData *state = g_new0 (ForEachData, 1);
240         state->self = picker;
241         state->num = limit_retrieve;
242         state->found = FALSE;
243         
244         /* Look at each item, and select the one with the correct ID: */
245         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
246
247         const gboolean result = state->found;
248         
249         /* Free the state instance: */
250         g_free(state);
251         
252         return result;
253 }
254