4fb130cefbc52c4a53cd0c01d43256bc6e5beb69
[modest] / src / hildon2 / modest-provider-picker.c
1 /* Copyright (c) 2006, 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-provider-picker.h"
31 #include <gtk/gtkliststore.h>
32 #include <gtk/gtkcelllayout.h>
33 #include <gtk/gtkcellrenderertext.h>
34 #include <glib/gi18n.h>
35 #include <modest-text-utils.h>
36 #include "modest-protocol-registry.h"
37 #include "modest-runtime.h"
38
39 #include <stdlib.h>
40 #include <string.h> /* For memcpy() */
41
42 /* Include config.h so that _() works: */
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46
47 G_DEFINE_TYPE (ModestProviderPicker, modest_provider_picker, HILDON_TYPE_PICKER_BUTTON);
48
49 #define MODEST_PROVIDER_PICKER_GET_PRIVATE(o) \
50         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_PROVIDER_PICKER, ModestProviderPickerPrivate))
51
52 typedef struct _ModestProviderPickerPrivate ModestProviderPickerPrivate;
53
54 struct _ModestProviderPickerPrivate
55 {
56         GtkTreeModel *model;
57 };
58 static void
59 modest_provider_picker_finalize (GObject *object)
60 {
61         ModestProviderPickerPrivate *priv = MODEST_PROVIDER_PICKER_GET_PRIVATE (object);
62
63         g_object_unref (G_OBJECT (priv->model));
64
65         G_OBJECT_CLASS (modest_provider_picker_parent_class)->finalize (object);
66 }
67
68 static void
69 modest_provider_picker_class_init (ModestProviderPickerClass *klass)
70 {
71         GObjectClass *object_class = G_OBJECT_CLASS (klass);
72
73         g_type_class_add_private (klass, sizeof (ModestProviderPickerPrivate));
74
75         object_class->finalize = modest_provider_picker_finalize;
76 }
77
78 enum MODEL_COLS {
79         MODEL_COL_ID, /* a string, not an int. */
80         MODEL_COL_NAME,
81         MODEL_COL_ID_TYPE
82 };
83
84
85 /*
86  * strictly, we should sort providers with mcc=0 after the other ones.... but, we don't have
87  * that info here, so ignoring for now.
88  */
89 static gint
90 provider_sort_func (GtkTreeModel *model, GtkTreeIter *iter1, GtkTreeIter *iter2, gpointer user_data)
91 {
92         gchar *prov1, *prov2;
93         gint retval;
94         
95         gtk_tree_model_get (model, iter1, MODEL_COL_NAME, &prov1, -1);
96         gtk_tree_model_get (model, iter2, MODEL_COL_NAME, &prov2, -1);
97
98         if (strcmp (prov1, prov2) == 0) 
99                 retval = 0;
100         else if (strcmp (_("mcen_va_serviceprovider_other"), prov1) == 0)
101                 retval = -1;
102         else if (strcmp (_("mcen_va_serviceprovider_other"), prov2) == 0)
103                 retval = 1;
104         else
105                 retval = modest_text_utils_utf8_strcmp (prov1, prov2, TRUE);
106         
107         g_free (prov1);
108         g_free (prov2);
109
110         return retval;
111 }
112
113 static gchar *
114 touch_selector_print_func (HildonTouchSelector *selector)
115 {
116         GtkTreeIter iter;
117         if (hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &iter)) {
118                 GtkTreeModel *model;
119                 GValue value = {0,};
120                 
121                 model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
122                 gtk_tree_model_get_value (model, &iter, MODEL_COL_NAME, &value);
123                 return g_value_dup_string (&value);
124         }
125         return NULL;
126 }
127
128
129 static void
130 modest_provider_picker_init (ModestProviderPicker *self)
131 {
132         ModestProviderPickerPrivate *priv;
133
134         priv = MODEST_PROVIDER_PICKER_GET_PRIVATE (self);
135         priv->model = NULL;
136 }
137
138 ModestProviderPicker*
139 modest_provider_picker_new (HildonSizeType size,
140                             HildonButtonArrangement arrangement)
141 {
142         ModestProviderPickerPrivate *priv;
143         ModestProviderPicker *self;
144         GtkCellRenderer *renderer;
145         GtkWidget *selector;
146
147         self = g_object_new (MODEST_TYPE_PROVIDER_PICKER, 
148                              "arrangement", arrangement,
149                              "size", size,
150                              NULL);
151         priv = MODEST_PROVIDER_PICKER_GET_PRIVATE (self);
152
153         /* Create the tree model for the selector,
154          * with a string for the name, and a string for the ID (e.g. "vodafone.it"), and the mcc
155          * This must match our MODEL_COLS enum constants.
156          */
157         priv->model = GTK_TREE_MODEL (gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT));
158         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(priv->model),
159                                               MODEL_COL_NAME, GTK_SORT_ASCENDING);
160         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE(priv->model),
161                                           MODEL_COL_NAME,
162                                           (GtkTreeIterCompareFunc)provider_sort_func,
163                                           NULL, NULL);
164
165         renderer = gtk_cell_renderer_text_new ();
166         g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
167
168         selector = hildon_touch_selector_new ();
169         hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), GTK_TREE_MODEL (priv->model),
170                                              renderer, "text", MODEL_COL_NAME, NULL);
171
172         hildon_touch_selector_set_model (HILDON_TOUCH_SELECTOR (selector), 0, GTK_TREE_MODEL (priv->model));
173         hildon_touch_selector_set_print_func (HILDON_TOUCH_SELECTOR (selector), touch_selector_print_func);
174
175         hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self), HILDON_TOUCH_SELECTOR (selector));
176         modest_provider_picker_set_others_provider (MODEST_PROVIDER_PICKER (self));
177
178         return self;
179 }
180
181 void
182 modest_provider_picker_fill (ModestProviderPicker *self, 
183                              ModestPresets *presets,
184                              gint mcc)
185 {       
186         GtkTreeIter other_iter;
187         ModestProviderPickerPrivate *priv;
188         GtkListStore *liststore;        
189         GSList *provider_ids_used_already = NULL, *provider_protos, *tmp;
190         gchar ** provider_ids = NULL;
191         gchar ** provider_names;        
192         gchar ** iter_provider_names;
193         gchar ** iter_provider_ids;
194         ModestProtocolRegistry *registry;
195         GtkWidget *selector;
196
197         g_return_if_fail (MODEST_IS_PROVIDER_PICKER(self));
198
199         priv = MODEST_PROVIDER_PICKER_GET_PRIVATE (self);
200         liststore = GTK_LIST_STORE (priv->model);
201         gtk_list_store_clear (liststore);
202         provider_names = modest_presets_get_providers (presets, mcc, TRUE, &provider_ids);
203
204         iter_provider_names = provider_names;
205         iter_provider_ids = provider_ids;
206
207         while(iter_provider_names && *iter_provider_names && iter_provider_ids && *iter_provider_ids) {
208                 const gchar* provider_name = *iter_provider_names;
209                 const gchar* provider_id = *iter_provider_ids;
210
211                 /* Prevent duplicate providers: */
212                 if (g_slist_find_custom (provider_ids_used_already, 
213                                          provider_id, (GCompareFunc)strcmp) == NULL) {
214                         /* printf("debug: provider_name=%s\n", provider_name); */
215
216                         /* Add the row: */
217                         GtkTreeIter iter;
218                         gtk_list_store_append (liststore, &iter);
219                         
220                         gtk_list_store_set(liststore, &iter, 
221                                            MODEL_COL_ID, provider_id, 
222                                            MODEL_COL_NAME, provider_name, 
223                                            MODEL_COL_ID_TYPE, MODEST_PROVIDER_PICKER_ID_PROVIDER,
224                                            -1);
225                         
226                         provider_ids_used_already = g_slist_prepend (
227                                 provider_ids_used_already, (gpointer)g_strdup (provider_id));
228                 }
229                 
230                 ++iter_provider_names;
231                 ++iter_provider_ids;
232         }
233         
234         /* Free the result of modest_presets_get_providers()
235          * as specified by its documentation: */
236         g_strfreev (provider_names);
237         g_strfreev (provider_ids);
238
239         /* Add the provider protocols */
240         registry = modest_runtime_get_protocol_registry ();
241         provider_protos = modest_protocol_registry_get_by_tag (registry, 
242                                                                MODEST_PROTOCOL_REGISTRY_PROVIDER_PROTOCOLS);
243         tmp = provider_protos;
244         while (tmp) {
245                 GtkTreeIter iter;
246                 ModestProtocol *proto = MODEST_PROTOCOL (tmp->data);
247                 const gchar *name = modest_protocol_get_display_name (proto);
248
249                 /* only add store protocols, no need to duplicate them */
250                 if (modest_protocol_registry_protocol_type_has_tag (registry, 
251                                                                     modest_protocol_get_type_id (proto),
252                                                                     MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS)) {
253                         gtk_list_store_append (liststore, &iter);
254                         gtk_list_store_set (liststore, &iter,
255                                             MODEL_COL_ID, modest_protocol_get_name (proto),
256                                             MODEL_COL_NAME, name,
257                                             MODEL_COL_ID_TYPE, MODEST_PROVIDER_PICKER_ID_PLUGIN_PROTOCOL,
258                                             -1);
259                 }
260                 tmp = g_slist_next (tmp);
261         }
262         g_slist_free (provider_protos);
263         
264         g_slist_foreach (provider_ids_used_already, (GFunc)g_free, NULL);
265         g_slist_free (provider_ids_used_already);
266
267         /* Add the "Other" item: */
268         /* Note that ID 0 means "Other" for us: */
269         gtk_list_store_prepend (liststore, &other_iter);
270         gtk_list_store_set (liststore, &other_iter,
271                             MODEL_COL_ID, 0,
272                             MODEL_COL_NAME, _("mcen_va_serviceprovider_other"),
273                             MODEL_COL_ID_TYPE, MODEST_PROVIDER_PICKER_ID_OTHER,
274                             -1);
275
276         /* Select the "Other" item: */
277         selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (self)));
278         hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, &other_iter, TRUE);
279         hildon_button_set_value (HILDON_BUTTON (self),
280                                  hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
281         
282 }
283
284 /**
285  * Returns the MCC number of the selected provider, 
286  * or NULL if no provider was selected, or "Other" was selected. 
287  */
288 gchar*
289 modest_provider_picker_get_active_provider_id (ModestProviderPicker *self)
290 {
291         GtkTreeIter active;
292         gboolean found;
293         GtkWidget *selector;
294
295         g_return_val_if_fail (MODEST_IS_PROVIDER_PICKER(self), NULL);
296
297         selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (self)));
298         found = hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &active);
299         if (found) {
300                 ModestProviderPickerPrivate *priv = MODEST_PROVIDER_PICKER_GET_PRIVATE (self);
301
302                 gchar *id = NULL;
303                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &id, -1);
304                 return g_strdup(id);    
305         }
306
307         return NULL; /* Failed. */
308 }
309
310 void 
311 modest_provider_picker_set_others_provider (ModestProviderPicker *self)
312 {
313         GtkTreeModel *model;
314         GtkTreeIter others_iter;
315         GtkWidget *selector;
316
317         g_return_if_fail (MODEST_IS_PROVIDER_PICKER(self));
318         
319         selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (self)));
320         model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
321         
322         if (gtk_tree_model_get_iter_first (model, &others_iter)) {
323                 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, &others_iter, TRUE);
324                 hildon_button_set_value (HILDON_BUTTON (self),
325                                          hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
326         }
327 }
328
329 ModestProviderPickerIdType 
330 modest_provider_picker_get_active_id_type (ModestProviderPicker *self)
331 {
332         GtkTreeIter active;
333         GtkWidget *selector;
334
335         g_return_val_if_fail (MODEST_IS_PROVIDER_PICKER (self), 
336                               MODEST_PROVIDER_PICKER_ID_OTHER);
337
338         selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON(self)));
339
340         if (hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector), 0, &active)) {
341                 ModestProviderPickerPrivate *priv = MODEST_PROVIDER_PICKER_GET_PRIVATE (self);
342                 ModestProviderPickerIdType id_type;
343
344                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID_TYPE, &id_type, -1);
345                 return id_type; 
346         } else {
347                 /* Fallback to other */
348                 return MODEST_PROVIDER_PICKER_ID_OTHER;
349         }
350 }