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