evolution-3.6.4/widgets/misc/e-auth-combo-box.c

No issues found

  1 /*
  2  * e-auth-combo-box.c
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) version 3.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 16  *
 17  */
 18 
 19 #include "e-auth-combo-box.h"
 20 
 21 #define E_AUTH_COMBO_BOX_GET_PRIVATE(obj) \
 22 	(G_TYPE_INSTANCE_GET_PRIVATE \
 23 	((obj), E_TYPE_AUTH_COMBO_BOX, EAuthComboBoxPrivate))
 24 
 25 struct _EAuthComboBoxPrivate {
 26 	CamelProvider *provider;
 27 };
 28 
 29 enum {
 30 	PROP_0,
 31 	PROP_PROVIDER
 32 };
 33 
 34 enum {
 35 	COLUMN_MECHANISM,
 36 	COLUMN_DISPLAY_NAME,
 37 	COLUMN_STRIKETHROUGH,
 38 	COLUMN_AUTHTYPE,
 39 	NUM_COLUMNS
 40 };
 41 
 42 G_DEFINE_TYPE (
 43 	EAuthComboBox,
 44 	e_auth_combo_box,
 45 	GTK_TYPE_COMBO_BOX)
 46 
 47 static void
 48 auth_combo_box_rebuild_model (EAuthComboBox *combo_box)
 49 {
 50 	GtkComboBox *gtk_combo_box;
 51 	CamelProvider *provider;
 52 	GtkTreeModel *model;
 53 	GList *link;
 54 	const gchar *active_id;
 55 
 56 	provider = e_auth_combo_box_get_provider (combo_box);
 57 
 58 	gtk_combo_box = GTK_COMBO_BOX (combo_box);
 59 	model = gtk_combo_box_get_model (gtk_combo_box);
 60 	active_id = gtk_combo_box_get_active_id (gtk_combo_box);
 61 
 62 	gtk_list_store_clear (GTK_LIST_STORE (model));
 63 
 64 	if (provider == NULL)
 65 		return;
 66 
 67 	for (link = provider->authtypes; link != NULL; link = link->next) {
 68 		CamelServiceAuthType *authtype = link->data;
 69 		GtkTreeIter iter;
 70 
 71 		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
 72 
 73 		gtk_list_store_set (
 74 			GTK_LIST_STORE (model), &iter,
 75 			COLUMN_MECHANISM, authtype->authproto,
 76 			COLUMN_DISPLAY_NAME, authtype->name,
 77 			COLUMN_AUTHTYPE, authtype,
 78 			-1);
 79 	}
 80 
 81 	/* Try selecting the previous mechanism. */
 82 	if (active_id != NULL)
 83 		gtk_combo_box_set_active_id (gtk_combo_box, active_id);
 84 
 85 	/* Or else fall back to the first mechanism. */
 86 	if (gtk_combo_box_get_active (gtk_combo_box) == -1)
 87 		gtk_combo_box_set_active (gtk_combo_box, 0);
 88 }
 89 
 90 static void
 91 auth_combo_box_set_property (GObject *object,
 92                              guint property_id,
 93                              const GValue *value,
 94                              GParamSpec *pspec)
 95 {
 96 	switch (property_id) {
 97 		case PROP_PROVIDER:
 98 			e_auth_combo_box_set_provider (
 99 				E_AUTH_COMBO_BOX (object),
100 				g_value_get_pointer (value));
101 			return;
102 	}
103 
104 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
105 }
106 
107 static void
108 auth_combo_box_get_property (GObject *object,
109                              guint property_id,
110                              GValue *value,
111                              GParamSpec *pspec)
112 {
113 	switch (property_id) {
114 		case PROP_PROVIDER:
115 			g_value_set_pointer (
116 				value,
117 				e_auth_combo_box_get_provider (
118 				E_AUTH_COMBO_BOX (object)));
119 			return;
120 	}
121 
122 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
123 }
124 
125 static void
126 auth_combo_box_constructed (GObject *object)
127 {
128 	GtkComboBox *combo_box;
129 	GtkListStore *list_store;
130 	GtkCellLayout *cell_layout;
131 	GtkCellRenderer *cell_renderer;
132 
133 	/* Chain up to parent's constructed() method. */
134 	G_OBJECT_CLASS (e_auth_combo_box_parent_class)->constructed (object);
135 
136 	list_store = gtk_list_store_new (
137 		NUM_COLUMNS,
138 		G_TYPE_STRING,		/* COLUMN_MECHANISM */
139 		G_TYPE_STRING,		/* COLUMN_DISPLAY_NAME */
140 		G_TYPE_BOOLEAN,		/* COLUMN_STRIKETHROUGH */
141 		G_TYPE_POINTER);	/* COLUMN_AUTHTYPE */
142 
143 	combo_box = GTK_COMBO_BOX (object);
144 	gtk_combo_box_set_model (combo_box, GTK_TREE_MODEL (list_store));
145 	gtk_combo_box_set_id_column (combo_box, COLUMN_MECHANISM);
146 	g_object_unref (list_store);
147 
148 	cell_layout = GTK_CELL_LAYOUT (object);
149 	cell_renderer = gtk_cell_renderer_text_new ();
150 	gtk_cell_layout_pack_start (cell_layout, cell_renderer, TRUE);
151 
152 	gtk_cell_layout_set_attributes (
153 		cell_layout, cell_renderer,
154 		"text", COLUMN_DISPLAY_NAME,
155 		"strikethrough", COLUMN_STRIKETHROUGH,
156 		NULL);
157 }
158 
159 static void
160 e_auth_combo_box_class_init (EAuthComboBoxClass *class)
161 {
162 	GObjectClass *object_class;
163 
164 	g_type_class_add_private (class, sizeof (EAuthComboBoxPrivate));
165 
166 	object_class = G_OBJECT_CLASS (class);
167 	object_class->set_property = auth_combo_box_set_property;
168 	object_class->get_property = auth_combo_box_get_property;
169 	object_class->constructed = auth_combo_box_constructed;
170 
171 	g_object_class_install_property (
172 		object_class,
173 		PROP_PROVIDER,
174 		g_param_spec_pointer (
175 			"provider",
176 			"Provider",
177 			"The provider to query for auth mechanisms",
178 			G_PARAM_READWRITE |
179 			G_PARAM_STATIC_STRINGS));
180 }
181 
182 static void
183 e_auth_combo_box_init (EAuthComboBox *combo_box)
184 {
185 	combo_box->priv = E_AUTH_COMBO_BOX_GET_PRIVATE (combo_box);
186 }
187 
188 GtkWidget *
189 e_auth_combo_box_new (void)
190 {
191 	return g_object_new (E_TYPE_AUTH_COMBO_BOX, NULL);
192 }
193 
194 CamelProvider *
195 e_auth_combo_box_get_provider (EAuthComboBox *combo_box)
196 {
197 	g_return_val_if_fail (E_IS_AUTH_COMBO_BOX (combo_box), NULL);
198 
199 	return combo_box->priv->provider;
200 }
201 
202 void
203 e_auth_combo_box_set_provider (EAuthComboBox *combo_box,
204                                CamelProvider *provider)
205 {
206 	g_return_if_fail (E_IS_AUTH_COMBO_BOX (combo_box));
207 
208 	if (provider == combo_box->priv->provider)
209 		return;
210 
211 	combo_box->priv->provider = provider;
212 
213 	g_object_notify (G_OBJECT (combo_box), "provider");
214 
215 	auth_combo_box_rebuild_model (combo_box);
216 }
217 
218 void
219 e_auth_combo_box_update_available (EAuthComboBox *combo_box,
220                                    GList *available_authtypes)
221 {
222 	GtkComboBox *gtk_combo_box;
223 	GtkTreeModel *model;
224 	GtkTreeIter iter;
225 	gint active_index;
226 	gint available_index = -1;
227 	gint index = 0;
228 	gboolean iter_set;
229 
230 	g_return_if_fail (E_IS_AUTH_COMBO_BOX (combo_box));
231 
232 	gtk_combo_box = GTK_COMBO_BOX (combo_box);
233 	model = gtk_combo_box_get_model (gtk_combo_box);
234 	active_index = gtk_combo_box_get_active (gtk_combo_box);
235 
236 	iter_set = gtk_tree_model_get_iter_first (model, &iter);
237 
238 	while (iter_set) {
239 		CamelServiceAuthType *authtype;
240 		gboolean available;
241 
242 		gtk_tree_model_get (
243 			model, &iter, COLUMN_AUTHTYPE, &authtype, -1);
244 
245 		available = (g_list_find (
246 			available_authtypes, authtype) != NULL);
247 
248 		gtk_list_store_set (
249 			GTK_LIST_STORE (model), &iter,
250 			COLUMN_STRIKETHROUGH, !available, -1);
251 
252 		if (index == active_index && !available)
253 			active_index = -1;
254 
255 		if (available && available_index == -1)
256 			available_index = index;
257 
258 		iter_set = gtk_tree_model_iter_next (model, &iter);
259 		index++;
260 	}
261 
262 	/* If the active combo_box item turned out to be unavailable
263 	 * (or there was no active item), select the first available. */
264 	if (active_index == -1 && available_index != -1)
265 		gtk_combo_box_set_active (gtk_combo_box, available_index);
266 }