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