* Fixes NB#90510, removes a memory leak
[modest] / src / widgets / modest-secureauth-combo-box.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  */
5
6 #include "modest-secureauth-combo-box.h"
7 #include <modest-runtime.h>
8 #include <gtk/gtkliststore.h>
9 #include <gtk/gtkcelllayout.h>
10 #include <gtk/gtkcellrenderertext.h>
11 #include <glib/gi18n.h>
12
13 #include <stdlib.h>
14 #include <string.h> /* For memcpy() */
15
16 /* Include config.h so that _() works: */
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 G_DEFINE_TYPE (ModestSecureauthComboBox, modest_secureauth_combo_box, GTK_TYPE_COMBO_BOX);
22
23 #define SECUREAUTH_COMBO_BOX_GET_PRIVATE(o) \
24         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_SECUREAUTH_COMBO_BOX, ModestSecureauthComboBoxPrivate))
25
26 typedef struct _ModestSecureauthComboBoxPrivate ModestSecureauthComboBoxPrivate;
27
28 struct _ModestSecureauthComboBoxPrivate
29 {
30         GtkTreeModel *model;
31 };
32
33 static void
34 modest_secureauth_combo_box_get_property (GObject *object, guint property_id,
35                                           GValue *value, GParamSpec *pspec)
36 {
37         switch (property_id) {
38         default:
39                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
40         }
41 }
42
43 static void
44 modest_secureauth_combo_box_set_property (GObject *object, guint property_id,
45                                           const GValue *value, GParamSpec *pspec)
46 {
47         switch (property_id) {
48         default:
49                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
50         }
51 }
52
53 static void
54 modest_secureauth_combo_box_dispose (GObject *object)
55 {
56         if (G_OBJECT_CLASS (modest_secureauth_combo_box_parent_class)->dispose)
57                 G_OBJECT_CLASS (modest_secureauth_combo_box_parent_class)->dispose (object);
58 }
59
60 static void
61 modest_secureauth_combo_box_finalize (GObject *object)
62 {
63         ModestSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (object);
64
65         g_object_unref (G_OBJECT (priv->model));
66
67         G_OBJECT_CLASS (modest_secureauth_combo_box_parent_class)->finalize (object);
68 }
69
70 static void
71 modest_secureauth_combo_box_class_init (ModestSecureauthComboBoxClass *klass)
72 {
73         GObjectClass *object_class = G_OBJECT_CLASS (klass);
74
75         g_type_class_add_private (klass, sizeof (ModestSecureauthComboBoxPrivate));
76
77         object_class->get_property = modest_secureauth_combo_box_get_property;
78         object_class->set_property = modest_secureauth_combo_box_set_property;
79         object_class->dispose = modest_secureauth_combo_box_dispose;
80         object_class->finalize = modest_secureauth_combo_box_finalize;
81 }
82
83 enum MODEL_COLS {
84         MODEL_COL_NAME = 0, /* a string */
85         MODEL_COL_ID = 1 /* an int. */
86 };
87
88 void modest_secureauth_combo_box_fill (ModestSecureauthComboBox *combobox);
89
90 static void
91 modest_secureauth_combo_box_init (ModestSecureauthComboBox *self)
92 {
93         ModestSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (self);
94
95         /* Create a tree model for the combo box,
96          * with a string for the name, and an ID for the secureauth.
97          * This must match our MODEL_COLS enum constants.
98          */
99         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
100
101         /* Setup the combo box: */
102         GtkComboBox *combobox = GTK_COMBO_BOX (self);
103         gtk_combo_box_set_model (combobox, priv->model);
104
105         /* Secureauth column:
106          * The ID model column in not shown in the view. */
107         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
108         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
109         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer, 
110         "text", MODEL_COL_NAME, NULL);
111         
112         modest_secureauth_combo_box_fill (self);
113 }
114
115 ModestSecureauthComboBox*
116 modest_secureauth_combo_box_new (void)
117 {
118         return g_object_new (MODEST_TYPE_SECUREAUTH_COMBO_BOX, NULL);
119 }
120
121 /* Fill the combo box with appropriate choices.
122  * #combobox: The combo box.
123  * @protocol: IMAP or POP.
124  */
125 void modest_secureauth_combo_box_fill (ModestSecureauthComboBox *combobox)
126 {       
127         ModestSecureauthComboBoxPrivate *priv;
128         GtkListStore *liststore;
129         ModestProtocolRegistry *protocol_registry;
130         GSList *protocols, *node;
131         GtkTreeIter iter;
132
133         priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
134         
135         /* Remove any existing rows: */
136         liststore = GTK_LIST_STORE (priv->model);
137         gtk_list_store_clear (liststore);
138
139         protocol_registry = modest_runtime_get_protocol_registry ();
140         protocols = modest_protocol_registry_get_by_tag (protocol_registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS);
141
142         for (node = protocols; node != NULL; node = g_slist_next (node)) {
143                 ModestProtocol *protocol;
144                 protocol = (ModestProtocol *) node->data;
145
146                 gtk_list_store_append (liststore, &iter);
147                 gtk_list_store_set (liststore, &iter, 
148                                     MODEL_COL_ID, (gint)modest_protocol_get_type_id (protocol),
149                                     MODEL_COL_NAME, modest_protocol_get_display_name (protocol),
150                                     -1);
151         }       
152         g_slist_free (protocols);
153 }
154
155 /**
156  * Returns the selected secureauth, 
157  * or MODEST_PROTOCOL_REGISTRY_TYPE_INVALID if no secureauth was selected.
158  */
159 ModestProtocolType
160 modest_secureauth_combo_box_get_active_secureauth (ModestSecureauthComboBox *combobox)
161 {
162         GtkTreeIter active;
163         gboolean found;
164
165         found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
166         if (found) {
167                 ModestSecureauthComboBoxPrivate *priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
168
169                 ModestProtocolType secureauth = MODEST_PROTOCOLS_AUTH_NONE;
170                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &secureauth, -1);
171                 return secureauth;      
172         }
173
174         return MODEST_PROTOCOL_REGISTRY_TYPE_INVALID; /* Failed. */
175 }
176
177 /* This allows us to pass more than one piece of data to the signal handler,
178  * and get a result: */
179 typedef struct 
180 {
181                 ModestSecureauthComboBox* self;
182                 ModestProtocolType id;
183                 gboolean found;
184 } ForEachData;
185
186 static gboolean
187 on_model_foreach_select_id(GtkTreeModel *model, 
188         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
189 {
190         ForEachData *state;
191         ModestProtocolType id = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
192
193         state = (ForEachData*)(user_data);
194         
195         /* Select the item if it has the matching ID: */
196         gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1); 
197         if(id == state->id) {
198                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
199                 
200                 state->found = TRUE;
201                 return TRUE; /* Stop walking the tree. */
202         }
203         
204         return FALSE; /* Keep walking the tree. */
205 }
206
207 /**
208  * Selects the specified secureauth, 
209  * or MODEST_PROTOCOL_REGISTRY_TYPE_INVALID if no secureauth was selected.
210  */
211 gboolean
212 modest_secureauth_combo_box_set_active_secureauth (ModestSecureauthComboBox *combobox, ModestProtocolType secureauth)
213 {
214         ModestSecureauthComboBoxPrivate *priv;
215         ForEachData *state;
216         gboolean result;
217
218         priv = SECUREAUTH_COMBO_BOX_GET_PRIVATE (combobox);
219         
220         /* Create a state instance so we can send two items of data to the signal handler: */
221         state = g_new0 (ForEachData, 1);
222         state->self = combobox;
223         state->id = secureauth;
224         state->found = FALSE;
225         
226         /* Look at each item, and select the one with the correct ID: */
227         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
228
229         result = state->found;
230         
231         /* Free the state instance: */
232         g_free(state);
233         
234         return result;
235 }
236