Use osso_abook_aggregator_find_contacts_for_email_address instead of the e-d-s call
[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         /* For theming purpouses. Widget name must end in Button-finger */
144         gtk_widget_set_name ((GtkWidget *) self, "ModestRetrievePickerButton-finger");
145
146         return self;
147 }
148
149 /* Fill the picker box with appropriate choices.
150  * #picker: The picker box.
151  * @protocol: IMAP or POP.
152  */
153 void modest_retrieve_picker_fill (ModestRetrievePicker *picker, ModestProtocolType protocol)
154 {       
155         ModestRetrievePickerPrivate *priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (picker);
156         
157         /* Remove any existing rows: */
158         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
159         gtk_list_store_clear (liststore);
160         
161         GtkTreeIter iter;
162         gtk_list_store_append (liststore, &iter);
163         gtk_list_store_set (liststore, &iter, 
164                 MODEL_COL_RETRIEVE_TYPE, MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY, 
165                 MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_headers"), -1);
166
167         gtk_list_store_append (liststore, &iter);
168         gtk_list_store_set (liststore, &iter, 
169                 MODEL_COL_RETRIEVE_TYPE, MODEST_ACCOUNT_RETRIEVE_MESSAGES_AND_ATTACHMENTS, 
170                 MODEL_COL_NAME, _("mcen_fi_advsetup_retrievetype_messages_attachments"), -1);
171 }
172
173 /**
174  * Returns the selected retrieve.
175  * or NULL if no retrieve was selected. The result must be freed with g_free().
176  */
177 ModestAccountRetrieveType
178 modest_retrieve_picker_get_active_retrieve_conf (ModestRetrievePicker *picker)
179 {
180         GtkTreeIter active;
181         GtkWidget *selector;
182         gboolean found;
183
184         selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (picker)));
185         found = hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &active);
186         if (found) {
187                 ModestRetrievePickerPrivate *priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (picker);
188
189                 ModestAccountRetrieveType retrieve_type = MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY;
190                 gtk_tree_model_get (priv->model, &active, MODEL_COL_RETRIEVE_TYPE, &retrieve_type, -1);
191                 return retrieve_type;   
192         }
193
194         return MODEST_ACCOUNT_RETRIEVE_HEADERS_ONLY; /* Failed. */
195 }
196
197 /* This allows us to pass more than one piece of data to the signal handler,
198  * and get a result: */
199 typedef struct 
200 {
201                 ModestRetrievePicker* self;
202                 ModestAccountRetrieveType retrieve_type;
203                 gboolean found;
204 } ForEachData;
205
206 static gboolean
207 on_model_foreach_select_id(GtkTreeModel *model, 
208         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
209 {
210         ForEachData *state = (ForEachData*)(user_data);
211         
212         gboolean result = FALSE;
213         
214         /* Select the item if it has the matching name: */
215         ModestAccountRetrieveType retrieve_type;
216         gtk_tree_model_get (model, iter, MODEL_COL_RETRIEVE_TYPE, &retrieve_type, -1); 
217         if (retrieve_type == state->retrieve_type) {
218                 GtkWidget *selector;
219                 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (state->self)));
220                 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, iter, TRUE);
221                 hildon_button_set_value (HILDON_BUTTON (state->self),
222                                          hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
223                 
224                 state->found = TRUE;
225                 return TRUE; /* Stop walking the tree. */
226         }
227         
228         return result; /* Whether we keep walking the tree. */
229 }
230
231 /**
232  * Selects the specified retrieve, 
233  * or FALSE if no retrieve was selected.
234  */
235 gboolean
236 modest_retrieve_picker_set_active_retrieve_conf (ModestRetrievePicker *picker, 
237                                                     ModestAccountRetrieveType retrieve_type)
238 {
239         ModestRetrievePickerPrivate *priv = MODEST_RETRIEVE_PICKER_GET_PRIVATE (picker);
240         
241         /* Create a state instance so we can send two items of data to the signal handler: */
242         ForEachData *state = g_new0 (ForEachData, 1);
243         state->self = picker;
244         state->retrieve_type = retrieve_type;
245         state->found = FALSE;
246         
247         /* Look at each item, and select the one with the correct ID: */
248         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
249
250         const gboolean result = state->found;
251         
252         /* Free the state instance: */
253         g_free(state);
254         
255         return result;
256 }
257