fc900af9955a5022763cfa9c4db95247a6247eed
[modest] / src / maemo / easysetup / modest-easysetup-country-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 #ifndef _GNU_SOURCE
31 #define _GNU_SOURCE /* So we can use the getline() function, which is a convenient GNU extension. */
32 #endif
33
34 #include <stdio.h>
35
36 #include <modest-utils.h>
37 #include "modest-easysetup-country-combo-box.h"
38 #include <gtk/gtkliststore.h>
39 #include <gtk/gtkcelllayout.h>
40 #include <gtk/gtkcellrenderertext.h>
41
42 #include <stdlib.h>
43 #include <string.h> /* For memcpy() */
44 #include <langinfo.h>
45 #include <locale.h>
46 #include <libintl.h> /* For dgettext(). */
47
48 /* Include config.h so that _() works: */
49 #ifdef HAVE_CONFIG_H
50 #include <config.h>
51 #endif
52
53 #define MAX_LINE_LEN 128 /* max length of a line in MCC file */
54
55 #if MODEST_HILDON_API < 2
56 G_DEFINE_TYPE (EasysetupCountryComboBox, easysetup_country_combo_box, GTK_TYPE_COMBO_BOX);
57 #else
58 G_DEFINE_TYPE (EasysetupCountryComboBox, easysetup_country_combo_box, HILDON_TYPE_PICKER_BUTTON);
59 #endif
60
61 typedef struct
62 {
63         gint locale_mcc;
64 /*      GtkTreeModel *model; */
65 } ModestEasysetupCountryComboBoxPrivate;
66
67 #define MODEST_EASYSETUP_COUNTRY_COMBO_BOX_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
68                                                         MODEST_EASYSETUP_TYPE_COUNTRY_COMBO_BOX, \
69                                                         ModestEasysetupCountryComboBoxPrivate))
70
71 static void
72 easysetup_country_combo_box_get_property (GObject *object, guint property_id,
73                                                                                                                         GValue *value, GParamSpec *pspec)
74 {
75         switch (property_id) {
76         default:
77                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
78         }
79 }
80
81 static void
82 easysetup_country_combo_box_set_property (GObject *object, guint property_id,
83                                                                                                                         const GValue *value, GParamSpec *pspec)
84 {
85         switch (property_id) {
86         default:
87                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
88         }
89 }
90
91 static void
92 easysetup_country_combo_box_dispose (GObject *object)
93 {
94         if (G_OBJECT_CLASS (easysetup_country_combo_box_parent_class)->dispose)
95                 G_OBJECT_CLASS (easysetup_country_combo_box_parent_class)->dispose (object);
96 }
97
98 enum MODEL_COLS {
99         MODEL_COL_NAME = 0, /* string */
100         MODEL_COL_MCC  = 1 /* the 'effective mcc' for this country */
101 };
102
103         
104 static void
105 easysetup_country_combo_box_finalize (GObject *object)
106 {
107         G_OBJECT_CLASS (easysetup_country_combo_box_parent_class)->finalize (object);
108 }
109
110 static void
111 easysetup_country_combo_box_class_init (EasysetupCountryComboBoxClass *klass)
112 {
113         GObjectClass *object_class = G_OBJECT_CLASS (klass);
114
115         g_type_class_add_private (klass, sizeof (ModestEasysetupCountryComboBoxPrivate));
116
117         object_class->get_property = easysetup_country_combo_box_get_property;
118         object_class->set_property = easysetup_country_combo_box_set_property;
119         object_class->dispose = easysetup_country_combo_box_dispose;
120         object_class->finalize = easysetup_country_combo_box_finalize;
121 }
122
123
124 static void
125 easysetup_country_combo_box_init (EasysetupCountryComboBox *self)
126 {
127         ModestEasysetupCountryComboBoxPrivate *priv = MODEST_EASYSETUP_COUNTRY_COMBO_BOX_GET_PRIVATE (self);
128         priv->locale_mcc = 0;
129 }
130
131 void
132 easysetup_country_combo_box_load_data(EasysetupCountryComboBox *self)
133 {
134         ModestEasysetupCountryComboBoxPrivate *priv;
135         GtkTreeModel *model;
136
137         priv = MODEST_EASYSETUP_COUNTRY_COMBO_BOX_GET_PRIVATE (self);
138         /* Create a tree model for the combo box,
139          * with a string for the name, and an int for the MCC ID.
140          * This must match our MODEL_COLS enum constants.
141          */
142         model = modest_utils_create_country_model ();
143         
144         /* Country column:
145          * The ID model column in not shown in the view. */
146         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
147         g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
148
149 #if MODEST_HILDON_API < 2
150         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (self), renderer, TRUE);
151         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (self), renderer, 
152                                         "text", MODEST_UTILS_COUNTRY_MODEL_COLUMN_NAME, NULL);
153 #else
154         GtkWidget *selector = hildon_touch_selector_new ();
155         hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self), HILDON_TOUCH_SELECTOR (selector));
156         hildon_touch_selector_append_column (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (self)),
157                                              model,
158                                              renderer, "text", MODEST_UTILS_COUNTRY_MODEL_COLUMN_NAME, NULL);
159 #endif
160
161         modest_utils_fill_country_model (model, &(priv->locale_mcc));
162
163         /* Set this _after_ loading from file, it makes loading faster */
164 #if MODEST_HILDON_API < 2
165         gtk_combo_box_set_model (GTK_COMBO_BOX (self), model);
166 #else
167         hildon_touch_selector_set_model (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (self)),
168                                          0, model);
169 #endif
170 }
171
172 EasysetupCountryComboBox*
173 easysetup_country_combo_box_new (void)
174 {
175 #if MODEST_HILDON_API >= 2
176         return g_object_new (MODEST_EASYSETUP_TYPE_COUNTRY_COMBO_BOX, 
177                              "arrangement", HILDON_BUTTON_ARRANGEMENT_VERTICAL,
178                              "size", HILDON_SIZE_AUTO,
179                              NULL);
180 #else
181         return g_object_new (MODEST_EASYSETUP_TYPE_COUNTRY_COMBO_BOX, 
182                              NULL);
183 #endif
184 }
185
186 /**
187  * Returns the MCC number of the selected country, or 0 if no country was selected. 
188  */
189 gint
190 easysetup_country_combo_box_get_active_country_mcc (EasysetupCountryComboBox *self)
191 {
192         GtkTreeIter active;
193         gboolean found;
194
195 #if MODEST_HILDON_API < 2
196         found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &active);
197 #else
198         found = hildon_touch_selector_get_selected (hildon_picker_button_get_selector
199                                                     (HILDON_PICKER_BUTTON (self)), 0, &active);
200 #endif
201         if (found) {
202                 gint mcc = 0;
203 #if MODEST_HILDON_API < 2
204                 gtk_tree_model_get (gtk_combo_box_get_model (GTK_COMBO_BOX (self)), 
205                                     &active, MODEST_UTILS_COUNTRY_MODEL_COLUMN_MCC, &mcc, -1);
206 #else
207                 gtk_tree_model_get (hildon_touch_selector_get_model (hildon_picker_button_get_selector
208                                                                      (HILDON_PICKER_BUTTON (self)), 
209                                                                      0), 
210                                     &active, MODEST_UTILS_COUNTRY_MODEL_COLUMN_MCC, &mcc, -1);
211 #endif
212                 return mcc;     
213         }
214         return 0; /* Failed. */
215 }
216
217
218 /**
219  * Selects the MCC number of the selected country.
220  * Specify 0 to select no country. 
221  */
222 gboolean
223 easysetup_country_combo_box_set_active_country_locale (EasysetupCountryComboBox *self)
224 {
225         ModestEasysetupCountryComboBoxPrivate *priv = MODEST_EASYSETUP_COUNTRY_COMBO_BOX_GET_PRIVATE (self);
226         GtkTreeIter iter;
227         gint current_mcc;
228         GtkTreeModel *model;
229
230 #if MODEST_HILDON_API < 2
231         model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
232         g_message ("HILDON < 2");
233 #else
234         model = hildon_touch_selector_get_model (hildon_picker_button_get_selector 
235                                                  (HILDON_PICKER_BUTTON (self)), 0);
236         g_message ("HILDON >= 2");
237 #endif
238         if (!gtk_tree_model_get_iter_first (model, &iter))
239                 return FALSE;
240         do {
241                 gtk_tree_model_get (model, &iter, MODEST_UTILS_COUNTRY_MODEL_COLUMN_MCC, &current_mcc, -1);
242                 if (priv->locale_mcc == current_mcc) {
243 #if MODEST_HILDON_API < 2
244                         gtk_combo_box_set_active_iter (GTK_COMBO_BOX (self), &iter);
245 #else
246                         hildon_touch_selector_select_iter (hildon_picker_button_get_selector 
247                                                            (HILDON_PICKER_BUTTON (self)), 0, 
248                                                            &iter, TRUE);
249 #endif
250                         return TRUE;
251                 }
252         } while (gtk_tree_model_iter_next (model, &iter));
253         
254         return FALSE; /* not found */
255 }
256