evolution-3.6.4/modules/cal-config-caldav/e-caldav-chooser-dialog.c

No issues found

  1 /*
  2  * e-caldav-chooser-dialog.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 <webcal://www.gnu.org/licenses/>
 16  *
 17  */
 18 
 19 #include "e-caldav-chooser-dialog.h"
 20 
 21 #include <config.h>
 22 #include <glib/gi18n-lib.h>
 23 
 24 #define E_CALDAV_CHOOSER_DIALOG_GET_PRIVATE(obj) \
 25 	(G_TYPE_INSTANCE_GET_PRIVATE \
 26 	((obj), E_TYPE_CALDAV_CHOOSER_DIALOG, ECaldavChooserDialogPrivate))
 27 
 28 struct _ECaldavChooserDialogPrivate {
 29 	ECaldavChooser *chooser;
 30 	GCancellable *cancellable;
 31 
 32 	GtkWidget *info_bar;		/* not referenced */
 33 	GtkWidget *info_bar_label;	/* not referenced */
 34 };
 35 
 36 enum {
 37 	PROP_0,
 38 	PROP_CHOOSER
 39 };
 40 
 41 /* Forward Declarations */
 42 static void	caldav_chooser_dialog_populated_cb
 43 						(GObject *source_object,
 44 						 GAsyncResult *result,
 45 						 gpointer user_data);
 46 
 47 G_DEFINE_DYNAMIC_TYPE (
 48 	ECaldavChooserDialog,
 49 	e_caldav_chooser_dialog,
 50 	GTK_TYPE_DIALOG)
 51 
 52 static void
 53 caldav_chooser_dialog_done (ECaldavChooserDialog *dialog,
 54                             const GError *error)
 55 {
 56 	GdkWindow *window;
 57 
 58 	/* Reset the mouse cursor to normal. */
 59 	window = gtk_widget_get_window (GTK_WIDGET (dialog));
 60 	gdk_window_set_cursor (window, NULL);
 61 
 62 	if (error != NULL) {
 63 		GtkLabel *label;
 64 
 65 		label = GTK_LABEL (dialog->priv->info_bar_label);
 66 		gtk_label_set_text (label, error->message);
 67 		gtk_widget_show (dialog->priv->info_bar);
 68 	}
 69 }
 70 
 71 static void
 72 caldav_chooser_dialog_authenticate_cb (GObject *source_object,
 73                                        GAsyncResult *result,
 74                                        gpointer user_data)
 75 {
 76 	ESourceRegistry *registry;
 77 	ECaldavChooserDialog *dialog;
 78 	ECaldavChooser *chooser;
 79 	GError *error = NULL;
 80 
 81 	registry = E_SOURCE_REGISTRY (source_object);
 82 	dialog = E_CALDAV_CHOOSER_DIALOG (user_data);
 83 
 84 	chooser = e_caldav_chooser_dialog_get_chooser (dialog);
 85 
 86 	e_source_registry_authenticate_finish (registry, result, &error);
 87 
 88 	/* Ignore cancellations, and leave the mouse cursor alone
 89 	 * since the GdkWindow may have already been destroyed. */
 90 	if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
 91 		/* do nothing */
 92 
 93 	/* Successful authentication, so try populating again. */
 94 	} else if (error == NULL) {
 95 		e_caldav_chooser_populate (
 96 			chooser, dialog->priv->cancellable,
 97 			caldav_chooser_dialog_populated_cb,
 98 			g_object_ref (dialog));
 99 
100 	/* Still not working?  Give up and display an error message. */
101 	} else {
102 		caldav_chooser_dialog_done (dialog, error);
103 	}
104 
105 	g_clear_error (&error);
106 	g_object_unref (dialog);
107 }
108 
109 static void
110 caldav_chooser_dialog_populated_cb (GObject *source_object,
111                                     GAsyncResult *result,
112                                     gpointer user_data)
113 {
114 	ECaldavChooserDialog *dialog;
115 	ECaldavChooser *chooser;
116 	GError *error = NULL;
117 
118 	chooser = E_CALDAV_CHOOSER (source_object);
119 	dialog = E_CALDAV_CHOOSER_DIALOG (user_data);
120 
121 	e_caldav_chooser_populate_finish (chooser, result, &error);
122 
123 	/* Ignore cancellations, and leave the mouse cursor alone
124 	 * since the GdkWindow may have already been destroyed. */
125 	if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
126 		/* do nothing */
127 
128 	/* We will likely get this error on the first try, since WebDAV
129 	 * servers generally require authentication.  It means we waste a
130 	 * round-trip to the server, but we don't want to risk prompting
131 	 * for authentication unnecessarily. */
132 	} else if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED)) {
133 		ESourceRegistry *registry;
134 		ESource *source;
135 
136 		registry = e_caldav_chooser_get_registry (chooser);
137 		source = e_caldav_chooser_get_source (chooser);
138 
139 		e_source_registry_authenticate (
140 			registry, source,
141 			E_SOURCE_AUTHENTICATOR (chooser),
142 			dialog->priv->cancellable,
143 			caldav_chooser_dialog_authenticate_cb,
144 			g_object_ref (dialog));
145 
146 	/* We were either successful or got an unexpected error. */
147 	} else {
148 		caldav_chooser_dialog_done (dialog, error);
149 	}
150 
151 	g_clear_error (&error);
152 	g_object_unref (dialog);
153 }
154 
155 static void
156 caldav_chooser_dialog_row_activated_cb (GtkTreeView *tree_view,
157                                         GtkTreePath *path,
158                                         GtkTreeViewColumn *column,
159                                         GtkDialog *dialog)
160 {
161 	gtk_dialog_response (dialog, GTK_RESPONSE_APPLY);
162 }
163 
164 static void
165 caldav_chooser_dialog_selection_changed_cb (GtkTreeSelection *selection,
166                                             GtkDialog *dialog)
167 {
168 	gboolean sensitive;
169 
170 	sensitive = (gtk_tree_selection_count_selected_rows (selection) > 0);
171 
172 	gtk_dialog_set_response_sensitive (
173 		dialog, GTK_RESPONSE_APPLY, sensitive);
174 }
175 
176 static void
177 caldav_chooser_dialog_set_chooser (ECaldavChooserDialog *dialog,
178                                    ECaldavChooser *chooser)
179 {
180 	g_return_if_fail (E_IS_CALDAV_CHOOSER (chooser));
181 	g_return_if_fail (dialog->priv->chooser == NULL);
182 
183 	dialog->priv->chooser = g_object_ref_sink (chooser);
184 }
185 
186 static void
187 caldav_chooser_dialog_set_property (GObject *object,
188                                     guint property_id,
189                                     const GValue *value,
190                                     GParamSpec *pspec)
191 {
192 	switch (property_id) {
193 		case PROP_CHOOSER:
194 			caldav_chooser_dialog_set_chooser (
195 				E_CALDAV_CHOOSER_DIALOG (object),
196 				g_value_get_object (value));
197 			return;
198 	}
199 
200 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
201 }
202 
203 static void
204 caldav_chooser_dialog_get_property (GObject *object,
205                                     guint property_id,
206                                     GValue *value,
207                                     GParamSpec *pspec)
208 {
209 	switch (property_id) {
210 		case PROP_CHOOSER:
211 			g_value_set_object (
212 				value,
213 				e_caldav_chooser_dialog_get_chooser (
214 				E_CALDAV_CHOOSER_DIALOG (object)));
215 			return;
216 	}
217 
218 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
219 }
220 
221 static void
222 caldav_chooser_dialog_dispose (GObject *object)
223 {
224 	ECaldavChooserDialogPrivate *priv;
225 
226 	priv = E_CALDAV_CHOOSER_DIALOG_GET_PRIVATE (object);
227 
228 	if (priv->chooser != NULL) {
229 		g_signal_handlers_disconnect_by_func (
230 			priv->chooser, caldav_chooser_dialog_row_activated_cb,
231 			object);
232 		g_object_unref (priv->chooser);
233 		priv->chooser = NULL;
234 	}
235 
236 	if (priv->cancellable != NULL) {
237 		g_cancellable_cancel (priv->cancellable);
238 		g_object_unref (priv->cancellable);
239 		priv->cancellable = NULL;
240 	}
241 
242 	/* Chain up to parent's dispose() method. */
243 	G_OBJECT_CLASS (e_caldav_chooser_dialog_parent_class)->dispose (object);
244 }
245 
246 static void
247 caldav_chooser_dialog_constructed (GObject *object)
248 {
249 	ECaldavChooserDialog *dialog;
250 	GtkTreeSelection *selection;
251 	GtkWidget *container;
252 	GtkWidget *widget;
253 	GtkWidget *vbox;
254 	const gchar *title;
255 
256 	dialog = E_CALDAV_CHOOSER_DIALOG (object);
257 
258 	/* Chain up to parent's constructed() method. */
259 	G_OBJECT_CLASS (e_caldav_chooser_dialog_parent_class)->
260 		constructed (object);
261 
262 	switch (e_caldav_chooser_get_source_type (dialog->priv->chooser)) {
263 		case E_CAL_CLIENT_SOURCE_TYPE_EVENTS:
264 			title = _("Choose a Calendar");
265 			break;
266 		case E_CAL_CLIENT_SOURCE_TYPE_MEMOS:
267 			title = _("Choose a Memo List");
268 			break;
269 		case E_CAL_CLIENT_SOURCE_TYPE_TASKS:
270 			title = _("Choose a Task List");
271 			break;
272 		default:
273 			g_warn_if_reached ();
274 			title = "";
275 	}
276 
277 	gtk_dialog_add_button (
278 		GTK_DIALOG (dialog),
279 		GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
280 
281 	gtk_dialog_add_button (
282 		GTK_DIALOG (dialog),
283 		GTK_STOCK_APPLY, GTK_RESPONSE_APPLY);
284 
285 	gtk_dialog_set_default_response (
286 		GTK_DIALOG (dialog), GTK_RESPONSE_APPLY);
287 	gtk_dialog_set_response_sensitive (
288 		GTK_DIALOG (dialog), GTK_RESPONSE_APPLY, FALSE);
289 
290 	gtk_window_set_title (GTK_WINDOW (dialog), title);
291 	gtk_window_set_default_size (GTK_WINDOW (dialog), 400, 400);
292 	gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
293 
294 	container = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
295 
296 	widget = gtk_vbox_new (FALSE, 6);
297 	gtk_container_set_border_width (GTK_CONTAINER (widget), 5);
298 	gtk_box_pack_start (GTK_BOX (container), widget, TRUE, TRUE, 0);
299 	gtk_widget_show (widget);
300 
301 	container = vbox = widget;
302 
303 	widget = gtk_scrolled_window_new (NULL, NULL);
304 	gtk_scrolled_window_set_policy (
305 		GTK_SCROLLED_WINDOW (widget),
306 		GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
307 	gtk_scrolled_window_set_shadow_type (
308 		GTK_SCROLLED_WINDOW (widget), GTK_SHADOW_IN);
309 	gtk_box_pack_start (GTK_BOX (container), widget, TRUE, TRUE, 0);
310 	gtk_widget_show (widget);
311 
312 	container = widget;
313 
314 	widget = GTK_WIDGET (dialog->priv->chooser);
315 	gtk_container_add (GTK_CONTAINER (container), widget);
316 	gtk_widget_show (widget);
317 
318 	g_signal_connect (
319 		widget, "row-activated",
320 		G_CALLBACK (caldav_chooser_dialog_row_activated_cb), dialog);
321 
322 	/* Build the info bar, but hide it initially. */
323 
324 	container = vbox;
325 
326 	widget = gtk_info_bar_new ();
327 	gtk_info_bar_set_message_type (
328 		GTK_INFO_BAR (widget), GTK_MESSAGE_WARNING);
329 	gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
330 	dialog->priv->info_bar = widget;  /* do not reference */
331 	gtk_widget_hide (widget);
332 
333 	container = gtk_info_bar_get_content_area (GTK_INFO_BAR (widget));
334 
335 	widget = gtk_hbox_new (FALSE, 6);
336 	gtk_box_pack_start (GTK_BOX (container), widget, TRUE, TRUE, 0);
337 	gtk_widget_show (widget);
338 
339 	container = widget;
340 
341 	widget = gtk_image_new_from_stock (
342 		GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_MENU);
343 	gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
344 	gtk_widget_show (widget);
345 
346 	widget = gtk_label_new ("");
347 	gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
348 	gtk_box_pack_start (GTK_BOX (container), widget, TRUE, TRUE, 0);
349 	dialog->priv->info_bar_label = widget;  /* do not reference */
350 	gtk_widget_show (widget);
351 
352 	/* Listen for tree view selection changes. */
353 
354 	selection = gtk_tree_view_get_selection (
355 		GTK_TREE_VIEW (dialog->priv->chooser));
356 
357 	g_signal_connect (
358 		selection, "changed",
359 		G_CALLBACK (caldav_chooser_dialog_selection_changed_cb),
360 		dialog);
361 }
362 
363 static void
364 caldav_chooser_dialog_realize (GtkWidget *widget)
365 {
366 	ECaldavChooserDialogPrivate *priv;
367 	GdkCursor *cursor;
368 	GdkWindow *window;
369 	GdkDisplay *display;
370 
371 	priv = E_CALDAV_CHOOSER_DIALOG_GET_PRIVATE (widget);
372 
373 	/* Chain up to parent's realize() method. */
374 	GTK_WIDGET_CLASS (e_caldav_chooser_dialog_parent_class)->
375 		realize (widget);
376 
377 	g_return_if_fail (priv->cancellable == NULL);
378 	priv->cancellable = g_cancellable_new ();
379 
380 	/* Show a busy mouse cursor while populating. */
381 	window = gtk_widget_get_window (widget);
382 	display = gtk_widget_get_display (widget);
383 	cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
384 	gdk_window_set_cursor (window, cursor);
385 	gdk_cursor_unref (cursor);
386 
387 	e_caldav_chooser_populate (
388 		priv->chooser, priv->cancellable,
389 		caldav_chooser_dialog_populated_cb,
390 		g_object_ref (widget));
391 }
392 
393 static void
394 caldav_chooser_dialog_response (GtkDialog *dialog,
395                                 gint response_id)
396 {
397 	ECaldavChooserDialogPrivate *priv;
398 
399 	priv = E_CALDAV_CHOOSER_DIALOG_GET_PRIVATE (dialog);
400 
401 	if (response_id == GTK_RESPONSE_APPLY)
402 		e_caldav_chooser_apply_selected (priv->chooser);
403 }
404 
405 static void
406 e_caldav_chooser_dialog_class_init (ECaldavChooserDialogClass *class)
407 {
408 	GObjectClass *object_class;
409 	GtkWidgetClass *widget_class;
410 	GtkDialogClass *dialog_class;
411 
412 	g_type_class_add_private (class, sizeof (ECaldavChooserDialogPrivate));
413 
414 	object_class = G_OBJECT_CLASS (class);
415 	object_class->set_property = caldav_chooser_dialog_set_property;
416 	object_class->get_property = caldav_chooser_dialog_get_property;
417 	object_class->dispose = caldav_chooser_dialog_dispose;
418 	object_class->constructed = caldav_chooser_dialog_constructed;
419 
420 	widget_class = GTK_WIDGET_CLASS (class);
421 	widget_class->realize = caldav_chooser_dialog_realize;
422 
423 	dialog_class = GTK_DIALOG_CLASS (class);
424 	dialog_class->response = caldav_chooser_dialog_response;
425 
426 	g_object_class_install_property (
427 		object_class,
428 		PROP_CHOOSER,
429 		g_param_spec_object (
430 			"chooser",
431 			NULL,
432 			NULL,
433 			E_TYPE_CALDAV_CHOOSER,
434 			G_PARAM_READWRITE |
435 			G_PARAM_CONSTRUCT_ONLY));
436 }
437 
438 static void
439 e_caldav_chooser_dialog_class_finalize (ECaldavChooserDialogClass *class)
440 {
441 }
442 
443 static void
444 e_caldav_chooser_dialog_init (ECaldavChooserDialog *dialog)
445 {
446 	dialog->priv = E_CALDAV_CHOOSER_DIALOG_GET_PRIVATE (dialog);
447 }
448 
449 void
450 e_caldav_chooser_dialog_type_register (GTypeModule *type_module)
451 {
452 	/* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
453 	 *     function, so we have to wrap it with a public function in
454 	 *     order to register types from a separate compilation unit. */
455 	e_caldav_chooser_dialog_register_type (type_module);
456 }
457 
458 GtkWidget *
459 e_caldav_chooser_dialog_new (ECaldavChooser *chooser,
460                              GtkWindow *parent)
461 {
462 	g_return_val_if_fail (E_IS_CALDAV_CHOOSER (chooser), NULL);
463 	g_return_val_if_fail (parent == NULL || GTK_IS_WINDOW (parent), NULL);
464 
465 	return g_object_new (
466 		E_TYPE_CALDAV_CHOOSER_DIALOG,
467 		"chooser", chooser, "transient-for", parent, NULL);
468 }
469 
470 ECaldavChooser *
471 e_caldav_chooser_dialog_get_chooser (ECaldavChooserDialog *dialog)
472 {
473 	g_return_val_if_fail (E_IS_CALDAV_CHOOSER_DIALOG (dialog), NULL);
474 
475 	return dialog->priv->chooser;
476 }