New ModestServertypeComboBox for gtk.
[modest] / src / widgets / modest-servertype-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-runtime.h>
31 #include "modest-servertype-combo-box.h"
32 #include <gtk/gtkliststore.h>
33 #include <gtk/gtkcelllayout.h>
34 #include <gtk/gtkcellrenderertext.h>
35 #include <glib/gi18n.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 (ModestServertypeComboBox, modest_servertype_combo_box, GTK_TYPE_COMBO_BOX);
46
47 #define MODEST_SERVERTYPE_COMBO_BOX_GET_PRIVATE(o) \
48         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_SERVERTYPE_COMBO_BOX, ModestServertypeComboBoxPrivate))
49
50 typedef struct _ModestServertypeComboBoxPrivate ModestServertypeComboBoxPrivate;
51
52 struct _ModestServertypeComboBoxPrivate
53 {
54         GtkTreeModel *model;
55 };
56
57 static void
58 modest_servertype_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 modest_servertype_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 modest_servertype_combo_box_dispose (GObject *object)
79 {
80         if (G_OBJECT_CLASS (modest_servertype_combo_box_parent_class)->dispose)
81                 G_OBJECT_CLASS (modest_servertype_combo_box_parent_class)->dispose (object);
82 }
83
84 static void
85 modest_servertype_combo_box_finalize (GObject *object)
86 {
87         ModestServertypeComboBoxPrivate *priv = MODEST_SERVERTYPE_COMBO_BOX_GET_PRIVATE (object);
88
89         g_object_unref (G_OBJECT (priv->model));
90
91         G_OBJECT_CLASS (modest_servertype_combo_box_parent_class)->finalize (object);
92 }
93
94 static void
95 modest_servertype_combo_box_class_init (ModestServertypeComboBoxClass *klass)
96 {
97         GObjectClass *object_class = G_OBJECT_CLASS (klass);
98
99         g_type_class_add_private (klass, sizeof (ModestServertypeComboBoxPrivate));
100
101         object_class->get_property = modest_servertype_combo_box_get_property;
102         object_class->set_property = modest_servertype_combo_box_set_property;
103         object_class->dispose = modest_servertype_combo_box_dispose;
104         object_class->finalize = modest_servertype_combo_box_finalize;
105 }
106
107 enum MODEL_COLS {
108         MODEL_COL_NAME = 0, /* a string */
109         MODEL_COL_ID = 1 /* an int. */
110 };
111
112 static void
113 modest_servertype_combo_box_init (ModestServertypeComboBox *self)
114 {
115         ModestServertypeComboBoxPrivate *priv = MODEST_SERVERTYPE_COMBO_BOX_GET_PRIVATE (self);
116
117         /* Create a tree model for the combo box,
118          * with a string for the name, and an ID for the servertype.
119          * This must match our MODEL_COLS enum constants.
120          */
121         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
122
123         /* Setup the combo box: */
124         GtkComboBox *combobox = GTK_COMBO_BOX (self);
125         gtk_combo_box_set_model (combobox, priv->model);
126
127         /* Servertype column:
128          * The ID model column in not shown in the view. */
129         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
130         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
131         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer, 
132                                         "text", MODEL_COL_NAME, NULL);
133 }
134
135 static void 
136 modest_servertype_combo_box_fill (ModestServertypeComboBox *combobox,
137                                      gboolean filter_providers)
138 {       
139         ModestServertypeComboBoxPrivate *priv;
140         GtkListStore *liststore;
141         ModestProtocolRegistry *protocol_registry;
142         GSList *remote_protocols, *node;
143         GtkTreeIter iter;
144         
145         /* Remove any existing rows: */
146         priv = MODEST_SERVERTYPE_COMBO_BOX_GET_PRIVATE (combobox);
147         protocol_registry = modest_runtime_get_protocol_registry ();
148         remote_protocols = modest_protocol_registry_get_by_tag (protocol_registry, MODEST_PROTOCOL_REGISTRY_REMOTE_STORE_PROTOCOLS);
149
150         liststore = GTK_LIST_STORE (priv->model);
151         gtk_list_store_clear (liststore);
152
153         for (node = remote_protocols; node != NULL; node = g_slist_next (node)) {
154                 ModestProtocol* protocol;
155                 gboolean add = TRUE;
156
157                 protocol = (ModestProtocol *) node->data;
158
159                 /* Do not include the protocols that would be listed
160                    in the providers combo */
161                 if (filter_providers)
162                         if (modest_protocol_registry_protocol_type_is_provider (protocol_registry,
163                                                                                 modest_protocol_get_type_id (protocol))) {
164                                 add = FALSE;
165                         }
166                 
167                 if (add) {
168                         gtk_list_store_append (liststore, &iter);
169                         gtk_list_store_set (liststore, &iter, 
170                                             MODEL_COL_ID, 
171                                             modest_protocol_get_type_id (protocol),
172                                             MODEL_COL_NAME, 
173                                             modest_protocol_get_display_name (protocol),
174                                             -1);
175                 }
176         }
177         
178         g_slist_free (remote_protocols);
179 }
180
181 ModestServertypeComboBox*
182 modest_servertype_combo_box_new (gboolean filter_providers)
183 {
184         ModestServertypeComboBox *combo;
185
186         combo = g_object_new (MODEST_TYPE_SERVERTYPE_COMBO_BOX, NULL);
187
188         /* Fill the combo */    
189         modest_servertype_combo_box_fill (combo, filter_providers);
190
191         return combo;
192 }
193
194 /**
195  * Returns the selected servertype, 
196  * or MODEST_PROTOCOL_REGISTRY_TYPE_INVALID if no servertype was selected.
197  */
198 ModestProtocolType
199 modest_servertype_combo_box_get_active_servertype (ModestServertypeComboBox *combobox)
200 {
201         GtkTreeIter active;
202         gboolean found;
203
204         found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
205         if (found) {
206                 ModestServertypeComboBoxPrivate *priv;
207                 ModestProtocolType servertype;
208
209                 priv = MODEST_SERVERTYPE_COMBO_BOX_GET_PRIVATE (combobox);
210
211                 servertype = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
212                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &servertype, -1);
213                 return servertype;      
214         }
215
216         return MODEST_PROTOCOL_REGISTRY_TYPE_INVALID; /* Failed. */
217 }
218
219 /* This allows us to pass more than one piece of data to the signal handler,
220  * and get a result: */
221 typedef struct 
222 {
223                 ModestServertypeComboBox* self;
224                 ModestProtocolType id;
225                 gboolean found;
226 } ForEachData;
227
228 static gboolean
229 on_model_foreach_select_id(GtkTreeModel *model, 
230         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
231 {
232         ModestProtocolType id = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
233         ForEachData *state = (ForEachData*)(user_data);
234         
235         /* Select the item if it has the matching ID: */
236         gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1); 
237         if(id == state->id) {
238                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
239                 
240                 state->found = TRUE;
241                 return TRUE; /* Stop walking the tree. */
242         }
243         
244         return FALSE; /* Keep walking the tree. */
245 }
246
247 /**
248  * Selects the specified servertype, 
249  * or MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN if no servertype was selected.
250  */
251 gboolean
252 modest_servertype_combo_box_set_active_servertype (ModestServertypeComboBox *combobox, ModestProtocolType servertype)
253 {
254         ModestServertypeComboBoxPrivate *priv;
255         ForEachData *state;
256         gboolean result;
257         
258         priv = MODEST_SERVERTYPE_COMBO_BOX_GET_PRIVATE (combobox);
259
260         /* Create a state instance so we can send two items of data to the signal handler: */
261         state = g_new0 (ForEachData, 1);
262         state->self = combobox;
263         state->id = servertype;
264         state->found = FALSE;
265         
266         /* Look at each item, and select the one with the correct ID: */
267         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
268
269         result = state->found;
270         
271         /* Free the state instance: */
272         g_free(state);
273         
274         return result;
275 }
276