024a369f7eee190e79b8308aea95b92d007d9762
[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, gpointer userdata)
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 (HildonSizeType size,
112                             HildonButtonArrangement arrangement)
113 {
114         ModestRetrievePicker *self;
115         ModestRetrievePickerPrivate *priv;
116         GtkCellRenderer *renderer;
117         GtkWidget *selector;
118
119         self = g_object_new (MODEST_TYPE_RETRIEVE_PICKER, 
120                              "arrangement", arrangement,
121                              "size", size,
122                              NULL);
123         priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (self);
124
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.
128          */
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);
132
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);
136
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),
139                                               (HildonTouchSelectorPrintFunc) touch_selector_print_func);
140
141         hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self), HILDON_TOUCH_SELECTOR (selector));
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 void modest_retrieve_picker_fill (ModestRetrievePicker *picker, ModestProtocolType protocol)
151 {       
152         ModestRetrievePickerPrivate *priv = MODEST_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, 
161                 MODEL_COL_RETRIEVE_TYPE, MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY, 
162                 MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_headers"), -1);
163
164         gtk_list_store_append (liststore, &iter);
165         gtk_list_store_set (liststore, &iter, 
166                 MODEL_COL_RETRIEVE_TYPE, MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS, 
167                 MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_messages_attachments"), -1);
168 }
169
170 /**
171  * Returns the selected retrieve.
172  * or NULL if no retrieve was selected. The result must be freed with g_free().
173  */
174 ModestAccountRetrieveType
175 modest_retrieve_picker_get_active_retrieve_conf (ModestRetrievePicker *picker)
176 {
177         GtkTreeIter active;
178         GtkWidget *selector;
179         gboolean found;
180
181         selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (picker)));
182         found = hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &active);
183         if (found) {
184                 ModestRetrievePickerPrivate *priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (picker);
185
186                 ModestAccountRetrieveType retrieve_type = MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
187                 gtk_tree_model_get (priv->model, &active, MODEL_COL_RETRIEVE_TYPE, &retrieve_type, -1);
188                 return retrieve_type;   
189         }
190
191         return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY; /* Failed. */
192 }
193
194 /* This allows us to pass more than one piece of data to the signal handler,
195  * and get a result: */
196 typedef struct 
197 {
198                 ModestRetrievePicker* self;
199                 ModestAccountRetrieveType retrieve_type;
200                 gboolean found;
201 } ForEachData;
202
203 static gboolean
204 on_model_foreach_select_id(GtkTreeModel *model, 
205         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
206 {
207         ForEachData *state = (ForEachData*)(user_data);
208         
209         gboolean result = FALSE;
210         
211         /* Select the item if it has the matching name: */
212         ModestAccountRetrieveType retrieve_type;
213         gtk_tree_model_get (model, iter, MODEL_COL_RETRIEVE_TYPE, &retrieve_type, -1); 
214         if (retrieve_type == state->retrieve_type) {
215                 GtkWidget *selector;
216                 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (state->self)));
217                 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, iter, TRUE);
218                 hildon_button_set_value (HILDON_BUTTON (state->self),
219                                          hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
220                 
221                 state->found = TRUE;
222                 return TRUE; /* Stop walking the tree. */
223         }
224         
225         return result; /* Whether we keep walking the tree. */
226 }
227
228 /**
229  * Selects the specified retrieve, 
230  * or FALSE if no retrieve was selected.
231  */
232 gboolean
233 modest_retrieve_picker_set_active_retrieve_conf (ModestRetrievePicker *picker, 
234                                                     ModestAccountRetrieveType retrieve_type)
235 {
236         ModestRetrievePickerPrivate *priv = MODEST_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->retrieve_type = retrieve_type;
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