1147a1debc58eaee1e048231d1b743f94fdb88fc
[modest] / src / hildon2 / modest-easysetup-provider-combo-box.c
1 /* Copyright (c) 2006, 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-easysetup-provider-combo-box.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 (EasysetupProviderComboBox, easysetup_provider_combo_box, GTK_TYPE_COMBO_BOX);
48
49 #define PROVIDER_COMBO_BOX_GET_PRIVATE(o) \
50         (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_PROVIDER_COMBO_BOX, EasysetupProviderComboBoxPrivate))
51
52 typedef struct _EasysetupProviderComboBoxPrivate EasysetupProviderComboBoxPrivate;
53
54 struct _EasysetupProviderComboBoxPrivate
55 {
56         GtkTreeModel *model;
57 };
58
59 static void
60 easysetup_provider_combo_box_get_property (GObject *object, guint property_id,
61                                                                                                                         GValue *value, GParamSpec *pspec)
62 {
63         switch (property_id) {
64         default:
65                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
66         }
67 }
68
69 static void
70 easysetup_provider_combo_box_set_property (GObject *object, guint property_id,
71                                                                                                                         const GValue *value, GParamSpec *pspec)
72 {
73         switch (property_id) {
74         default:
75                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
76         }
77 }
78
79 static void
80 easysetup_provider_combo_box_dispose (GObject *object)
81 {
82         if (G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->dispose)
83                 G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->dispose (object);
84 }
85
86 static void
87 easysetup_provider_combo_box_finalize (GObject *object)
88 {
89         EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (object);
90
91         g_object_unref (G_OBJECT (priv->model));
92
93         G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->finalize (object);
94 }
95
96 static void
97 easysetup_provider_combo_box_class_init (EasysetupProviderComboBoxClass *klass)
98 {
99         GObjectClass *object_class = G_OBJECT_CLASS (klass);
100
101         g_type_class_add_private (klass, sizeof (EasysetupProviderComboBoxPrivate));
102
103         object_class->get_property = easysetup_provider_combo_box_get_property;
104         object_class->set_property = easysetup_provider_combo_box_set_property;
105         object_class->dispose = easysetup_provider_combo_box_dispose;
106         object_class->finalize = easysetup_provider_combo_box_finalize;
107 }
108
109 enum MODEL_COLS {
110         MODEL_COL_ID, /* a string, not an int. */
111         MODEL_COL_NAME,
112         MODEL_COL_ID_TYPE
113 };
114
115
116 /*
117  * strictly, we should sort providers with mcc=0 after the other ones.... but, we don't have
118  * that info here, so ignoring for now.
119  */
120 static gint
121 provider_sort_func (GtkTreeModel *model, GtkTreeIter *iter1, GtkTreeIter *iter2, gpointer user_data)
122 {
123         gchar *prov1, *prov2;
124         gint retval;
125         
126         gtk_tree_model_get (model, iter1, MODEL_COL_NAME, &prov1, -1);
127         gtk_tree_model_get (model, iter2, MODEL_COL_NAME, &prov2, -1);
128
129         if (strcmp (prov1, prov2) == 0) 
130                 retval = 0;
131         else if (strcmp (_("mcen_va_serviceprovider_other"), prov1) == 0)
132                 retval = -1;
133         else if (strcmp (_("mcen_va_serviceprovider_other"), prov2) == 0)
134                 retval = 1;
135         else
136                 retval = modest_text_utils_utf8_strcmp (prov1, prov2, TRUE);
137         
138         g_free (prov1);
139         g_free (prov2);
140
141         return retval;
142 }
143
144 static void
145 easysetup_provider_combo_box_init (EasysetupProviderComboBox *self)
146 {
147         EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (self);
148
149         /* Create a tree model for the combo box,
150          * with a string for the name, and a string for the ID (e.g. "vodafone.it"), and the mcc
151          * This must match our MODEL_COLS enum constants.
152          */
153         priv->model = GTK_TREE_MODEL (gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT));
154
155         /* Setup the combo box: */
156         GtkComboBox *combobox = GTK_COMBO_BOX (self);
157         gtk_combo_box_set_model (combobox, priv->model);
158
159         /* Provider column:
160          * The ID model column in not shown in the view. */
161         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
162         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
163         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,  "text", MODEL_COL_NAME, NULL);
164         
165         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(priv->model),
166                                               MODEL_COL_NAME, GTK_SORT_ASCENDING);
167         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE(priv->model),
168                                           MODEL_COL_NAME,
169                                           (GtkTreeIterCompareFunc)provider_sort_func,
170                                           NULL, NULL);
171 }
172
173 EasysetupProviderComboBox*
174 easysetup_provider_combo_box_new (void)
175 {
176         return g_object_new (EASYSETUP_TYPE_PROVIDER_COMBO_BOX, NULL);
177 }
178
179 void
180 easysetup_provider_combo_box_fill (EasysetupProviderComboBox *combobox, 
181                                    ModestPresets *presets,
182                                    gint mcc)
183 {       
184         GtkTreeIter other_iter;
185         EasysetupProviderComboBoxPrivate *priv;
186         GtkListStore *liststore;        
187         GSList *provider_ids_used_already = NULL, *provider_protos, *tmp;
188         gchar ** provider_ids = NULL;
189         gchar ** provider_names;        
190         gchar ** iter_provider_names;
191         gchar ** iter_provider_ids;
192         ModestProtocolRegistry *registry;
193
194         g_return_if_fail (EASYSETUP_IS_PROVIDER_COMBO_BOX(combobox));
195
196         priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
197         liststore = GTK_LIST_STORE (priv->model);
198         gtk_list_store_clear (liststore);
199         provider_names = modest_presets_get_providers (presets, mcc, TRUE, &provider_ids);
200
201         iter_provider_names = provider_names;
202         iter_provider_ids = provider_ids;
203
204         while(iter_provider_names && *iter_provider_names && iter_provider_ids && *iter_provider_ids) {
205                 const gchar* provider_name = *iter_provider_names;
206                 const gchar* provider_id = *iter_provider_ids;
207
208                 /* Prevent duplicate providers: */
209                 if (g_slist_find_custom (provider_ids_used_already, 
210                                          provider_id, (GCompareFunc)strcmp) == NULL) {
211                         /* printf("debug: provider_name=%s\n", provider_name); */
212
213                         /* Add the row: */
214                         GtkTreeIter iter;
215                         gtk_list_store_append (liststore, &iter);
216                         
217                         gtk_list_store_set(liststore, &iter, 
218                                            MODEL_COL_ID, provider_id, 
219                                            MODEL_COL_NAME, provider_name, 
220                                            MODEL_COL_ID_TYPE, EASYSETUP_PROVIDER_COMBO_BOX_ID_PROVIDER,
221                                            -1);
222                         
223                         provider_ids_used_already = g_slist_prepend (
224                                 provider_ids_used_already, (gpointer)g_strdup (provider_id));
225                 }
226                 
227                 ++iter_provider_names;
228                 ++iter_provider_ids;
229         }
230         
231         /* Free the result of modest_presets_get_providers()
232          * as specified by its documentation: */
233         g_strfreev (provider_names);
234         g_strfreev (provider_ids);
235
236         /* Add the provider protocols */
237         registry = modest_runtime_get_protocol_registry ();
238         provider_protos = modest_protocol_registry_get_by_tag (registry, 
239                                                                MODEST_PROTOCOL_REGISTRY_PROVIDER_PROTOCOLS);
240         tmp = provider_protos;
241         while (tmp) {
242                 GtkTreeIter iter;
243                 ModestProtocol *proto = MODEST_PROTOCOL (tmp->data);
244                 const gchar *name = modest_protocol_get_display_name (proto);
245
246                 /* only add store protocols, no need to duplicate them */
247                 if (modest_protocol_registry_protocol_type_has_tag (registry, 
248                                                                     modest_protocol_get_type_id (proto),
249                                                                     MODEST_PROTOCOL_REGISTRY_STORE_PROTOCOLS)) {
250                         gtk_list_store_append (liststore, &iter);
251                         gtk_list_store_set (liststore, &iter,
252                                             MODEL_COL_ID, modest_protocol_get_name (proto),
253                                             MODEL_COL_NAME, name,
254                                             MODEL_COL_ID_TYPE, EASYSETUP_PROVIDER_COMBO_BOX_ID_PLUGIN_PROTOCOL,
255                                             -1);
256                 }
257                 tmp = g_slist_next (tmp);
258         }
259         g_slist_free (provider_protos);
260         
261         /* Add the "Other" item: */
262         /* Note that ID 0 means "Other" for us: */
263         gtk_list_store_prepend (liststore, &other_iter);
264         gtk_list_store_set (liststore, &other_iter,
265                             MODEL_COL_ID, 0,
266                             MODEL_COL_NAME, _("mcen_va_serviceprovider_other"),
267                             MODEL_COL_ID_TYPE, EASYSETUP_PROVIDER_COMBO_BOX_ID_OTHER,
268                             -1);
269
270         /* Select the "Other" item: */
271         gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &other_iter);
272         
273         g_slist_foreach (provider_ids_used_already, (GFunc)g_free, NULL);
274         g_slist_free (provider_ids_used_already);
275 }
276
277 /**
278  * Returns the MCC number of the selected provider, 
279  * or NULL if no provider was selected, or "Other" was selected. 
280  */
281 gchar*
282 easysetup_provider_combo_box_get_active_provider_id (EasysetupProviderComboBox *combobox)
283 {
284         GtkTreeIter active;
285
286         g_return_val_if_fail (EASYSETUP_IS_PROVIDER_COMBO_BOX(combobox), NULL);
287
288         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
289         if (found) {
290                 EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
291
292                 gchar *id = NULL;
293                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &id, -1);
294                 return g_strdup(id);    
295         }
296
297         return NULL; /* Failed. */
298 }
299
300 void 
301 easysetup_provider_combo_box_set_others_provider (EasysetupProviderComboBox *combobox)
302 {
303         GtkTreeModel *model;
304         GtkTreeIter others_iter;
305
306         g_return_if_fail (EASYSETUP_IS_PROVIDER_COMBO_BOX(combobox));
307         
308         model = gtk_combo_box_get_model (GTK_COMBO_BOX (combobox));
309         
310         if (gtk_tree_model_get_iter_first (model, &others_iter))
311                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &others_iter);
312 }
313
314 EasysetupProviderComboBoxIdType 
315 easysetup_provider_combo_box_get_active_id_type (EasysetupProviderComboBox *combobox)
316 {
317         GtkTreeIter active;
318
319         g_return_val_if_fail (EASYSETUP_IS_PROVIDER_COMBO_BOX (combobox), 
320                               EASYSETUP_PROVIDER_COMBO_BOX_ID_OTHER);
321
322         if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active)) {
323                 EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
324                 EasysetupProviderComboBoxIdType id_type;
325
326                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID_TYPE, &id_type, -1);
327                 return id_type; 
328         } else {
329                 /* Fallback to other */
330                 return EASYSETUP_PROVIDER_COMBO_BOX_ID_OTHER;
331         }
332 }