Use GTK+ single includes
[modest] / src / widgets / modest-secureauth-combo-box.c
1 /*
2  * Copyright (C) 2007 Nokia Corporation, all rights reserved.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in the
13  *   documentation and/or other materials provided with the distribution.
14  * * Neither the name of the Nokia Corporation nor the names of its
15  *   contributors may be used to endorse or promote products derived from
16  *   this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
19  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "modest-secureauth-combo-box.h"
32 #include <modest-runtime.h>
33 #include <gtk/gtk.h>
34 #include <glib/gi18n.h>
35
36 #include <stdlib.h>
37 #include <string.h> /* For memcpy() */
38
39 /* Include config.h so that _() works: */
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 G_DEFINE_TYPE (ModestSecureauthComboBox, modest_secureauth_combo_box, GTK_TYPE_COMBO_BOX);
45
46 #define SECUREAUTH_COMBO_BOX_GET_PRIVATE(o) \
47         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_SECUREAUTH_COMBO_BOX, ModestSecureauthComboBoxPrivate))
48
49 typedef struct _ModestSecureauthComboBoxPrivate ModestSecureauthComboBoxPrivate;
50
51 struct _ModestSecureauthComboBoxPrivate
52 {
53         GtkTreeModel *model;
54 };
55
56 static void
57 modest_secureauth_combo_box_get_property (GObject *object, guint property_id,
58                                           GValue *value, GParamSpec *pspec)
59 {
60         switch (property_id) {
61         default:
62                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
63         }
64 }
65
66 static void
67 modest_secureauth_combo_box_set_property (GObject *object, guint property_id,
68                                           const GValue *value, GParamSpec *pspec)
69 {
70         switch (property_id) {
71         default:
72                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
73         }
74 }
75
76 static void
77 modest_secureauth_combo_box_dispose (GObject *object)
78 {
79         if (G_OBJECT_CLASS (modest_secureauth_combo_box_parent_class)->dispose)
80                 G_OBJECT_CLASS (modest_secureauth_combo_box_parent_class)->dispose (object);
81 }
82
83 static void
84 modest_secureauth_combo_box_finalize (GObject *object)
85 {
86         ModestSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (object);
87
88         g_object_unref (G_OBJECT (priv->model));
89
90         G_OBJECT_CLASS (modest_secureauth_combo_box_parent_class)->finalize (object);
91 }
92
93 static void
94 modest_secureauth_combo_box_class_init (ModestSecureauthComboBoxClass *klass)
95 {
96         GObjectClass *object_class = G_OBJECT_CLASS (klass);
97
98         g_type_class_add_private (klass, sizeof (ModestSecureauthComboBoxPrivate));
99
100         object_class->get_property = modest_secureauth_combo_box_get_property;
101         object_class->set_property = modest_secureauth_combo_box_set_property;
102         object_class->dispose = modest_secureauth_combo_box_dispose;
103         object_class->finalize = modest_secureauth_combo_box_finalize;
104 }
105
106 enum MODEL_COLS {
107         MODEL_COL_NAME = 0, /* a string */
108         MODEL_COL_ID = 1 /* an int. */
109 };
110
111 void modest_secureauth_combo_box_fill (ModestSecureauthComboBox *combobox);
112
113 static void
114 modest_secureauth_combo_box_init (ModestSecureauthComboBox *self)
115 {
116         ModestSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (self);
117
118         /* Create a tree model for the combo box,
119          * with a string for the name, and an ID for the secureauth.
120          * This must match our MODEL_COLS enum constants.
121          */
122         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
123
124         /* Setup the combo box: */
125         GtkComboBox *combobox = GTK_COMBO_BOX (self);
126         gtk_combo_box_set_model (combobox, priv->model);
127
128         /* Secureauth column:
129          * The ID model column in not shown in the view. */
130         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
131         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
132         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer, 
133         "text", MODEL_COL_NAME, NULL);
134         
135         modest_secureauth_combo_box_fill (self);
136 }
137
138 ModestSecureauthComboBox*
139 modest_secureauth_combo_box_new (void)
140 {
141         return g_object_new (MODEST_TYPE_SECUREAUTH_COMBO_BOX, NULL);
142 }
143
144 /* Fill the combo box with appropriate choices.
145  * #combobox: The combo box.
146  * @protocol: IMAP or POP.
147  */
148 void modest_secureauth_combo_box_fill (ModestSecureauthComboBox *combobox)
149 {       
150         ModestSecureauthComboBoxPrivate *priv;
151         GtkListStore *liststore;
152         ModestProtocolRegistry *protocol_registry;
153         GSList *protocols, *node;
154         GtkTreeIter iter;
155
156         priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
157         
158         /* Remove any existing rows: */
159         liststore = GTK_LIST_STORE (priv->model);
160         gtk_list_store_clear (liststore);
161
162         protocol_registry = modest_runtime_get_protocol_registry ();
163         protocols = modest_protocol_registry_get_by_tag (protocol_registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS);
164
165         for (node = protocols; node != NULL; node = g_slist_next (node)) {
166                 ModestProtocol *protocol;
167                 protocol = (ModestProtocol *) node->data;
168
169                 gtk_list_store_append (liststore, &iter);
170                 gtk_list_store_set (liststore, &iter, 
171                                     MODEL_COL_ID, (gint)modest_protocol_get_type_id (protocol),
172                                     MODEL_COL_NAME, modest_protocol_get_display_name (protocol),
173                                     -1);
174         }       
175         g_slist_free (protocols);
176 }
177
178 /**
179  * Returns the selected secureauth, 
180  * or MODEST_PROTOCOL_REGISTRY_TYPE_INVALID if no secureauth was selected.
181  */
182 ModestProtocolType
183 modest_secureauth_combo_box_get_active_secureauth (ModestSecureauthComboBox *combobox)
184 {
185         GtkTreeIter active;
186         gboolean found;
187
188         found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
189         if (found) {
190                 ModestSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
191
192                 ModestProtocolType secureauth = MODEST_PROTOCOLS_AUTH_NONE;
193                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &secureauth, -1);
194                 return secureauth;      
195         }
196
197         return MODEST_PROTOCOL_REGISTRY_TYPE_INVALID; /* Failed. */
198 }
199
200 /* This allows us to pass more than one piece of data to the signal handler,
201  * and get a result: */
202 typedef struct 
203 {
204                 ModestSecureauthComboBox* self;
205                 ModestProtocolType id;
206                 gboolean found;
207 } ForEachData;
208
209 static gboolean
210 on_model_foreach_select_id(GtkTreeModel *model, 
211         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
212 {
213         ForEachData *state;
214         ModestProtocolType id = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
215
216         state = (ForEachData*)(user_data);
217         
218         /* Select the item if it has the matching ID: */
219         gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1); 
220         if(id == state->id) {
221                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
222                 
223                 state->found = TRUE;
224                 return TRUE; /* Stop walking the tree. */
225         }
226         
227         return FALSE; /* Keep walking the tree. */
228 }
229
230 /**
231  * Selects the specified secureauth, 
232  * or MODEST_PROTOCOL_REGISTRY_TYPE_INVALID if no secureauth was selected.
233  */
234 gboolean
235 modest_secureauth_combo_box_set_active_secureauth (ModestSecureauthComboBox *combobox, ModestProtocolType secureauth)
236 {
237         ModestSecureauthComboBoxPrivate *priv;
238         ForEachData *state;
239         gboolean result;
240
241         priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
242         
243         /* Create a state instance so we can send two items of data to the signal handler: */
244         state = g_new0 (ForEachData, 1);
245         state->self = combobox;
246         state->id = secureauth;
247         state->found = FALSE;
248         
249         /* Look at each item, and select the one with the correct ID: */
250         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
251
252         result = state->found;
253         
254         /* Free the state instance: */
255         g_free(state);
256         
257         return result;
258 }
259