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