* Added new ModestRetrievePicker as a replacement of
[modest] / src / hildon2 / modest-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-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>
37
38 #include <stdlib.h>
39 #include <string.h> /* For memcpy() */
40
41 /* Include config.h so that _() works: */
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 G_DEFINE_TYPE (ModestRetrievePicker, modest_retrieve_picker, HILDON_TYPE_PICKER_BUTTON);
47
48 #define MODEST_RETRIEVE_PICKER_GET_PRIVATE(o) \
49         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_RETRIEVE_PICKER, ModestRetrievePickerPrivate))
50
51 typedef struct _ModestRetrievePickerPrivate ModestRetrievePickerPrivate;
52
53 struct _ModestRetrievePickerPrivate
54 {
55         GtkTreeModel *model;
56 };
57
58 enum MODEL_COLS {
59         MODEL_COL_NAME = 0, /* a string */
60         MODEL_COL_RETRIEVE_TYPE = 1 /* a gint (a ModestAccountRetrieveType) */
61 };
62
63 void modest_retrieve_picker_fill (ModestRetrievePicker *picker, ModestProtocolType protocol);
64
65 static gchar *
66 touch_selector_print_func (HildonTouchSelector *selector)
67 {
68         GtkTreeIter iter;
69         if (hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &iter)) {
70                 GtkTreeModel *model;
71                 GValue value = {0,};
72                 
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);
76         }
77         return NULL;
78 }
79
80 static void
81 modest_retrieve_picker_finalize (GObject *object)
82 {
83         ModestRetrievePickerPrivate *priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (object);
84
85         g_object_unref (G_OBJECT (priv->model));
86
87         G_OBJECT_CLASS (modest_retrieve_picker_parent_class)->finalize (object);
88 }
89
90 static void
91 modest_retrieve_picker_class_init (ModestRetrievePickerClass *klass)
92 {
93         GObjectClass *object_class = G_OBJECT_CLASS (klass);
94
95         g_type_class_add_private (klass, sizeof (ModestRetrievePickerPrivate));
96
97         object_class->finalize = modest_retrieve_picker_finalize;
98 }
99
100 static void
101 modest_retrieve_picker_init (ModestRetrievePicker *self)
102 {
103         ModestRetrievePickerPrivate *priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (self);
104
105         priv->model = NULL;
106 }
107
108
109
110 ModestRetrievePicker*
111 modest_retrieve_picker_new (void)
112 {
113         ModestRetrievePicker *self;
114         ModestRetrievePickerPrivate *priv;
115         GtkCellRenderer *renderer;
116         GtkWidget *selector;
117
118         self = g_object_new (MODEST_TYPE_RETRIEVE_PICKER, 
119                              "arrangement", HILDON_BUTTON_ARRANGEMENT_VERTICAL,
120                              "size", HILDON_SIZE_AUTO,
121                              NULL);
122         priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (self);
123
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.
127          */
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);
131
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);
135
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);
138
139         hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self), HILDON_TOUCH_SELECTOR (selector));
140
141         return self;
142 }
143
144 /* Fill the picker box with appropriate choices.
145  * #picker: The picker box.
146  * @protocol: IMAP or POP.
147  */
148 void modest_retrieve_picker_fill (ModestRetrievePicker *picker, ModestProtocolType protocol)
149 {       
150         ModestRetrievePickerPrivate *priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (picker);
151         
152         /* Remove any existing rows: */
153         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
154         gtk_list_store_clear (liststore);
155         
156         GtkTreeIter iter;
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);
161
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);
166 }
167
168 /**
169  * Returns the selected retrieve.
170  * or NULL if no retrieve was selected. The result must be freed with g_free().
171  */
172 ModestAccountRetrieveType
173 modest_retrieve_picker_get_active_retrieve_conf (ModestRetrievePicker *picker)
174 {
175         GtkTreeIter active;
176         GtkWidget *selector;
177         gboolean found;
178
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);
181         if (found) {
182                 ModestRetrievePickerPrivate *priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (picker);
183
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;   
187         }
188
189         return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY; /* Failed. */
190 }
191
192 /* This allows us to pass more than one piece of data to the signal handler,
193  * and get a result: */
194 typedef struct 
195 {
196                 ModestRetrievePicker* self;
197                 ModestAccountRetrieveType retrieve_type;
198                 gboolean found;
199 } ForEachData;
200
201 static gboolean
202 on_model_foreach_select_id(GtkTreeModel *model, 
203         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
204 {
205         ForEachData *state = (ForEachData*)(user_data);
206         
207         gboolean result = FALSE;
208         
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) {
213                 GtkWidget *selector;
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)));
218                 
219                 state->found = TRUE;
220                 return TRUE; /* Stop walking the tree. */
221         }
222         
223         return result; /* Whether we keep walking the tree. */
224 }
225
226 /**
227  * Selects the specified retrieve, 
228  * or FALSE if no retrieve was selected.
229  */
230 gboolean
231 modest_retrieve_picker_set_active_retrieve_conf (ModestRetrievePicker *picker, 
232                                                     ModestAccountRetrieveType retrieve_type)
233 {
234         ModestRetrievePickerPrivate *priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (picker);
235         
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;
241         
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);
244
245         const gboolean result = state->found;
246         
247         /* Free the state instance: */
248         g_free(state);
249         
250         return result;
251 }
252