* sort the providers (almost) correctly,
[modest] / src / maemo / easysetup / 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
37 #include <stdlib.h>
38 #include <string.h> /* For memcpy() */
39
40 /* Include config.h so that _() works: */
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44
45 G_DEFINE_TYPE (EasysetupProviderComboBox, easysetup_provider_combo_box, GTK_TYPE_COMBO_BOX);
46
47 #define PROVIDER_COMBO_BOX_GET_PRIVATE(o) \
48         (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_PROVIDER_COMBO_BOX, EasysetupProviderComboBoxPrivate))
49
50 typedef struct _EasysetupProviderComboBoxPrivate EasysetupProviderComboBoxPrivate;
51
52 struct _EasysetupProviderComboBoxPrivate
53 {
54         GtkTreeModel *model;
55 };
56
57 static void
58 easysetup_provider_combo_box_get_property (GObject *object, guint property_id,
59                                                                                                                         GValue *value, GParamSpec *pspec)
60 {
61         switch (property_id) {
62         default:
63                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
64         }
65 }
66
67 static void
68 easysetup_provider_combo_box_set_property (GObject *object, guint property_id,
69                                                                                                                         const GValue *value, GParamSpec *pspec)
70 {
71         switch (property_id) {
72         default:
73                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
74         }
75 }
76
77 static void
78 easysetup_provider_combo_box_dispose (GObject *object)
79 {
80         if (G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->dispose)
81                 G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->dispose (object);
82 }
83
84 static void
85 easysetup_provider_combo_box_finalize (GObject *object)
86 {
87         EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (object);
88
89         g_object_unref (G_OBJECT (priv->model));
90
91         G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->finalize (object);
92 }
93
94 static void
95 easysetup_provider_combo_box_class_init (EasysetupProviderComboBoxClass *klass)
96 {
97         GObjectClass *object_class = G_OBJECT_CLASS (klass);
98
99         g_type_class_add_private (klass, sizeof (EasysetupProviderComboBoxPrivate));
100
101         object_class->get_property = easysetup_provider_combo_box_get_property;
102         object_class->set_property = easysetup_provider_combo_box_set_property;
103         object_class->dispose = easysetup_provider_combo_box_dispose;
104         object_class->finalize = easysetup_provider_combo_box_finalize;
105 }
106
107 enum MODEL_COLS {
108         MODEL_COL_NAME = 0,
109         MODEL_COL_ID   = 1 /* a string, not an int. */
110 };
111
112
113 /*
114  * strictly, we should sort providers with mcc=0 after the other ones.... but, we don't have
115  * that info here, so ignoring for now.
116  */
117 gint
118 provider_sort_func (GtkTreeModel *model, GtkTreeIter *iter1, GtkTreeIter *iter2, gpointer user_data)
119 {
120         gchar *prov1, *prov2;
121         gint retval;
122         
123         gtk_tree_model_get (model, iter1, MODEL_COL_NAME, &prov1, -1);
124         gtk_tree_model_get (model, iter2, MODEL_COL_NAME, &prov2, -1);
125
126         if (strcmp (prov1, prov2) == 0) 
127                 retval = 0;
128         else if (strcmp (_("mcen_va_serviceprovider_other"), prov1) == 0)
129                 retval = -1;
130         else if (strcmp (_("mcen_va_serviceprovider_other"), prov2) == 0)
131                 retval = 1;
132         else
133                 retval = modest_text_utils_utf8_strcmp (prov1, prov2, TRUE);
134         
135         g_free (prov1);
136         g_free (prov2);
137
138         return retval;
139 }
140
141 static void
142 easysetup_provider_combo_box_init (EasysetupProviderComboBox *self)
143 {
144         EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (self);
145
146         /* Create a tree model for the combo box,
147          * with a string for the name, and a string for the ID (e.g. "vodafone.it"), and the mcc
148          * This must match our MODEL_COLS enum constants.
149          */
150         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING));
151
152         /* Setup the combo box: */
153         GtkComboBox *combobox = GTK_COMBO_BOX (self);
154         gtk_combo_box_set_model (combobox, priv->model);
155
156         /* Provider column:
157          * The ID model column in not shown in the view. */
158         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
159         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
160         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,  "text", MODEL_COL_NAME, NULL);
161         
162         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(priv->model),
163                                               MODEL_COL_NAME, GTK_SORT_ASCENDING);
164         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE(priv->model),
165                                           MODEL_COL_NAME,
166                                           (GtkTreeIterCompareFunc)provider_sort_func,
167                                           NULL, NULL);
168 }
169
170 EasysetupProviderComboBox*
171 easysetup_provider_combo_box_new (void)
172 {
173         return g_object_new (EASYSETUP_TYPE_PROVIDER_COMBO_BOX, NULL);
174 }
175
176 void
177 easysetup_provider_combo_box_fill (EasysetupProviderComboBox *combobox, ModestPresets *presets,
178                                    gint mcc)
179 {       
180         EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
181         
182         /* Remove any existing rows: */
183         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
184         gtk_list_store_clear (liststore);
185         
186         GSList *provider_ids_used_already = NULL;
187         
188         /* Add the appropriate rows for this country, from the presets file: */
189         gchar ** provider_ids = NULL;
190         gchar ** provider_names = modest_presets_get_providers (presets, mcc, 
191                                                                 TRUE /* include_globals */, &provider_ids);     
192         gchar ** iter_provider_names = provider_names;
193         gchar ** iter_provider_ids = provider_ids;
194         while(iter_provider_names && *iter_provider_names && iter_provider_ids && *iter_provider_ids) {
195                 const gchar* provider_name = *iter_provider_names;
196                 const gchar* provider_id = *iter_provider_ids;
197                 
198                 /* Prevent duplicate providers: */
199                 if (g_slist_find_custom (provider_ids_used_already, 
200                                          provider_id, (GCompareFunc)strcmp) == NULL) {
201                         /* printf("debug: provider_name=%s\n", provider_name); */
202
203                         /* Add the row: */
204                         GtkTreeIter iter;
205                         gtk_list_store_append (liststore, &iter);
206                         
207                         gtk_list_store_set(liststore, &iter, 
208                                            MODEL_COL_ID, provider_id, 
209                                            MODEL_COL_NAME, provider_name, -1);
210                         
211                         provider_ids_used_already = g_slist_prepend (
212                                 provider_ids_used_already, (gpointer)g_strdup (provider_id));
213                 }
214
215                         ++iter_provider_names;
216                         ++iter_provider_ids;
217                 }
218
219         /* Free the result of modest_presets_get_providers()
220          * as specified by its documentation: */
221         g_strfreev (provider_names);
222         g_strfreev (provider_ids);
223
224         
225         /* Add the "Other" item: */
226         /* Note that ID 0 means "Other" for us: */
227         /* TODO: We need a Logical ID for this text. */
228         GtkTreeIter iter;
229         gtk_list_store_prepend (liststore, &iter);
230         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, 0, MODEL_COL_NAME, _("mcen_va_serviceprovider_other"),
231                             -1);
232         
233         /* Select the "Other" item: */
234         gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
235         
236         g_slist_foreach (provider_ids_used_already, (GFunc)g_free, NULL);
237         g_slist_free (provider_ids_used_already);
238 }
239
240 /**
241  * Returns the MCC number of the selected provider, 
242  * or NULL if no provider was selected, or "Other" was selected. 
243  */
244 gchar*
245 easysetup_provider_combo_box_get_active_provider_id (EasysetupProviderComboBox *combobox)
246 {
247         GtkTreeIter active;
248         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
249         if (found) {
250                 EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
251
252                 gchar *id = NULL;
253                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &id, -1);
254                 return g_strdup(id);    
255         }
256
257         return NULL; /* Failed. */
258 }