Removed both "retrieve" and "limit retrieve" pickers from Account Settings
[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, gpointer userdata)
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 (HildonSizeType size,
108                                   HildonButtonArrangement arrangement)
109 {
110         ModestLimitRetrievePicker *self;
111         ModestLimitRetrievePickerPrivate *priv;
112         GtkCellRenderer *renderer;
113         GtkWidget *selector;
114
115         self = g_object_new (MODEST_TYPE_LIMIT_RETRIEVE_PICKER, 
116                              "arrangement", arrangement,
117                              "size", size,
118                              NULL);
119         priv = MODEST_LIMIT_RETRIEVE_PICKER_GET_PRIVATE (self);
120
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.
124          */
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);
128
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);
132
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);
135
136         hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self), HILDON_TOUCH_SELECTOR (selector));
137
138         modest_limit_retrieve_picker_fill (self);
139
140         /* For theming purpouses. Widget name must end in Button-finger */
141         gtk_widget_set_name ((GtkWidget *) self, "ModestLimitRetrievePickerButton-finger");
142
143         return self;
144 }
145
146 /* Fill the picker box with appropriate choices.
147  * #picker: The picker box.
148  * @protocol: IMAP or POP.
149  */
150 static void modest_limit_retrieve_picker_fill (ModestLimitRetrievePicker *picker)
151 {       
152         ModestLimitRetrievePickerPrivate *priv = MODEST_LIMIT_RETRIEVE_PICKER_GET_PRIVATE (picker);
153         
154         /* Remove any existing rows: */
155         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
156         gtk_list_store_clear (liststore);
157         
158         GtkTreeIter iter;
159         gtk_list_store_append (liststore, &iter);
160         gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 0, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_nolimit"), -1);
161         
162         gtk_list_store_append (liststore, &iter);
163         gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 200, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_200"), -1);
164         
165         gtk_list_store_append (liststore, &iter);
166         gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 100, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_100"), -1);
167
168         gtk_list_store_append (liststore, &iter);
169         gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 50, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_50"), -1);
170
171         gtk_list_store_append (liststore, &iter);
172         gtk_list_store_set (liststore, &iter, MODEL_COL_NUM, 20, MODEL_COL_NAME, _("mcen_fi_advsetup_retrieve_20"), -1);
173 }
174
175 /**
176  * Returns the selected limit_retrieve, 
177  * or 0 if no limit_retrieve was selected.
178  */
179 gint
180 modest_limit_retrieve_picker_get_active_limit_retrieve (ModestLimitRetrievePicker *picker)
181 {
182         GtkTreeIter active;
183         gboolean found;
184         GtkWidget *selector;
185
186         selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (picker)));
187         found = hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &active);
188         if (found) {
189                 ModestLimitRetrievePickerPrivate *priv = MODEST_LIMIT_RETRIEVE_PICKER_GET_PRIVATE (picker);
190
191                 gint limit_retrieve = 0;
192                 gtk_tree_model_get (priv->model, &active, MODEL_COL_NUM, &limit_retrieve, -1);
193                 return limit_retrieve;  
194         }
195
196         return 0; /* Failed. */
197 }
198
199 /* This allows us to pass more than one piece of data to the signal handler,
200  * and get a result: */
201 typedef struct 
202 {
203                 ModestLimitRetrievePicker* self;
204                 gint num;
205                 gboolean found;
206 } ForEachData;
207
208 static gboolean
209 on_model_foreach_select_id(GtkTreeModel *model, 
210         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
211 {
212         ForEachData *state = (ForEachData*)(user_data);
213         
214         gboolean result = FALSE;
215         
216         /* Select the item if it has the matching name: */
217         gint num = 0;
218         gtk_tree_model_get (model, iter, MODEL_COL_NUM, &num, -1); 
219         if(num == state->num) {
220                 GtkWidget *selector;
221                 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (state->self)));
222                 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, iter, TRUE);
223                 hildon_button_set_value (HILDON_BUTTON (state->self),
224                                          hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
225                 
226                 state->found = TRUE;
227                 return TRUE; /* Stop walking the tree. */
228         }
229         
230         return result; /* Whether we keep walking the tree. */
231 }
232
233 /**
234  * Selects the specified limit_retrieve, 
235  * or FALSE if no limit_retrieve was selected.
236  */
237 gboolean
238 modest_limit_retrieve_picker_set_active_limit_retrieve (ModestLimitRetrievePicker *picker, gint limit_retrieve)
239 {
240         ModestLimitRetrievePickerPrivate *priv = MODEST_LIMIT_RETRIEVE_PICKER_GET_PRIVATE (picker);
241         
242         /* Create a state instance so we can send two items of data to the signal handler: */
243         ForEachData *state = g_new0 (ForEachData, 1);
244         state->self = picker;
245         state->num = limit_retrieve;
246         state->found = FALSE;
247         
248         /* Look at each item, and select the one with the correct ID: */
249         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
250
251         const gboolean result = state->found;
252         
253         /* Free the state instance: */
254         g_free(state);
255         
256         return result;
257 }
258