evolution-3.6.4/widgets/misc/e-source-config-dialog.c

No issues found

  1 /*
  2  * e-source-config-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 <http://www.gnu.org/licenses/>
 16  *
 17  */
 18 
 19 #include "e-source-config-dialog.h"
 20 
 21 #include <libevolution-utils/e-alert-dialog.h>
 22 #include <libevolution-utils/e-alert-sink.h>
 23 #include <misc/e-alert-bar.h>
 24 
 25 #define E_SOURCE_CONFIG_DIALOG_GET_PRIVATE(obj) \
 26 	(G_TYPE_INSTANCE_GET_PRIVATE \
 27 	((obj), E_TYPE_SOURCE_CONFIG_DIALOG, ESourceConfigDialogPrivate))
 28 
 29 struct _ESourceConfigDialogPrivate {
 30 	ESourceConfig *config;
 31 	ESourceRegistry *registry;
 32 
 33 	GtkWidget *alert_bar;
 34 	gulong alert_bar_visible_handler_id;
 35 };
 36 
 37 enum {
 38 	PROP_0,
 39 	PROP_CONFIG
 40 };
 41 
 42 /* Forward Declarations */
 43 static void	e_source_config_dialog_alert_sink_init
 44 					(EAlertSinkInterface *interface);
 45 
 46 G_DEFINE_TYPE_WITH_CODE (
 47 	ESourceConfigDialog,
 48 	e_source_config_dialog,
 49 	GTK_TYPE_DIALOG,
 50 	G_IMPLEMENT_INTERFACE (
 51 		E_TYPE_ALERT_SINK,
 52 		e_source_config_dialog_alert_sink_init))
 53 
 54 static void
 55 source_config_dialog_commit_cb (GObject *object,
 56                                 GAsyncResult *result,
 57                                 gpointer user_data)
 58 {
 59 	ESourceConfig *config;
 60 	ESourceConfigDialog *dialog;
 61 	GdkWindow *gdk_window;
 62 	GError *error = NULL;
 63 
 64 	config = E_SOURCE_CONFIG (object);
 65 	dialog = E_SOURCE_CONFIG_DIALOG (user_data);
 66 
 67 	/* Set the cursor back to normal. */
 68 	gdk_window = gtk_widget_get_window (GTK_WIDGET (dialog));
 69 	gdk_window_set_cursor (gdk_window, NULL);
 70 
 71 	/* Allow user interaction with window content. */
 72 	gtk_widget_set_sensitive (GTK_WIDGET (dialog), TRUE);
 73 
 74 	e_source_config_commit_finish (config, result, &error);
 75 
 76 	/* Ignore cancellations. */
 77 	if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
 78 		g_object_unref (dialog);
 79 		g_error_free (error);
 80 
 81 	} else if (error != NULL) {
 82 		e_alert_submit (
 83 			E_ALERT_SINK (dialog),
 84 			"system:simple-error",
 85 			error->message, NULL);
 86 		g_object_unref (dialog);
 87 		g_error_free (error);
 88 
 89 	} else {
 90 		gtk_widget_destroy (GTK_WIDGET (dialog));
 91 	}
 92 }
 93 
 94 static void
 95 source_config_dialog_commit (ESourceConfigDialog *dialog)
 96 {
 97 	GdkCursor *gdk_cursor;
 98 	GdkWindow *gdk_window;
 99 	ESourceConfig *config;
100 
101 	config = e_source_config_dialog_get_config (dialog);
102 
103 	/* Clear any previous alerts. */
104 	e_alert_bar_clear (E_ALERT_BAR (dialog->priv->alert_bar));
105 
106 	/* Make the cursor appear busy. */
107 	gdk_cursor = gdk_cursor_new (GDK_WATCH);
108 	gdk_window = gtk_widget_get_window (GTK_WIDGET (dialog));
109 	gdk_window_set_cursor (gdk_window, gdk_cursor);
110 	g_object_unref (gdk_cursor);
111 
112 	/* Prevent user interaction with window content. */
113 	gtk_widget_set_sensitive (GTK_WIDGET (dialog), FALSE);
114 
115 	/* XXX This operation is not cancellable. */
116 	e_source_config_commit (
117 		config, NULL,
118 		source_config_dialog_commit_cb,
119 		g_object_ref (dialog));
120 }
121 
122 static void
123 source_config_dialog_source_removed_cb (ESourceRegistry *registry,
124                                         ESource *removed_source,
125                                         ESourceConfigDialog *dialog)
126 {
127 	ESourceConfig *config;
128 	ESource *original_source;
129 
130 	/* If the ESource being edited is removed, cancel the dialog. */
131 
132 	config = e_source_config_dialog_get_config (dialog);
133 	original_source = e_source_config_get_original_source (config);
134 
135 	if (original_source == NULL)
136 		return;
137 
138 	if (!e_source_equal (original_source, removed_source))
139 		return;
140 
141 	gtk_dialog_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
142 }
143 
144 static void
145 source_config_alert_bar_visible_cb (EAlertBar *alert_bar,
146                                     GParamSpec *pspec,
147                                     ESourceConfigDialog *dialog)
148 {
149 	e_source_config_resize_window (dialog->priv->config);
150 }
151 
152 static void
153 source_config_dialog_set_config (ESourceConfigDialog *dialog,
154                                  ESourceConfig *config)
155 {
156 	ESourceRegistry *registry;
157 
158 	g_return_if_fail (E_IS_SOURCE_CONFIG (config));
159 	g_return_if_fail (dialog->priv->config == NULL);
160 
161 	dialog->priv->config = g_object_ref (config);
162 
163 	registry = e_source_config_get_registry (config);
164 	dialog->priv->registry = g_object_ref (registry);
165 
166 	g_signal_connect (
167 		registry, "source-removed",
168 		G_CALLBACK (source_config_dialog_source_removed_cb), dialog);
169 }
170 
171 static void
172 source_config_dialog_set_property (GObject *object,
173                                    guint property_id,
174                                    const GValue *value,
175                                    GParamSpec *pspec)
176 {
177 	switch (property_id) {
178 		case PROP_CONFIG:
179 			source_config_dialog_set_config (
180 				E_SOURCE_CONFIG_DIALOG (object),
181 				g_value_get_object (value));
182 			return;
183 	}
184 
185 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
186 }
187 
188 static void
189 source_config_dialog_get_property (GObject *object,
190                                    guint property_id,
191                                    GValue *value,
192                                    GParamSpec *pspec)
193 {
194 	switch (property_id) {
195 		case PROP_CONFIG:
196 			g_value_set_object (
197 				value,
198 				e_source_config_dialog_get_config (
199 				E_SOURCE_CONFIG_DIALOG (object)));
200 			return;
201 	}
202 
203 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
204 }
205 
206 static void
207 source_config_dialog_dispose (GObject *object)
208 {
209 	ESourceConfigDialogPrivate *priv;
210 
211 	priv = E_SOURCE_CONFIG_DIALOG_GET_PRIVATE (object);
212 
213 	if (priv->config != NULL) {
214 		g_object_unref (priv->config);
215 		priv->config = NULL;
216 	}
217 
218 	if (priv->registry != NULL) {
219 		g_signal_handlers_disconnect_matched (
220 			priv->registry, G_SIGNAL_MATCH_DATA,
221 			0, 0, NULL, NULL, object);
222 		g_object_unref (priv->registry);
223 		priv->registry = NULL;
224 	}
225 
226 	if (priv->alert_bar != NULL) {
227 		g_signal_handler_disconnect (
228 			priv->alert_bar,
229 			priv->alert_bar_visible_handler_id);
230 		g_object_unref (priv->alert_bar);
231 		priv->alert_bar = NULL;
232 	}
233 
234 	/* Chain up to parent's dispose() method. */
235 	G_OBJECT_CLASS (e_source_config_dialog_parent_class)->dispose (object);
236 }
237 
238 static void
239 source_config_dialog_constructed (GObject *object)
240 {
241 	ESourceConfigDialogPrivate *priv;
242 	GtkWidget *content_area;
243 	GtkWidget *config;
244 	GtkWidget *widget;
245 	gulong handler_id;
246 
247 	priv = E_SOURCE_CONFIG_DIALOG_GET_PRIVATE (object);
248 
249 	config = GTK_WIDGET (priv->config);
250 
251 	widget = gtk_dialog_get_widget_for_response (
252 		GTK_DIALOG (object), GTK_RESPONSE_OK);
253 
254 	gtk_container_set_border_width (GTK_CONTAINER (object), 5);
255 	gtk_container_set_border_width (GTK_CONTAINER (config), 5);
256 
257 	content_area = gtk_dialog_get_content_area (GTK_DIALOG (object));
258 	gtk_box_pack_start (GTK_BOX (content_area), config, TRUE, TRUE, 0);
259 	gtk_widget_show (config);
260 
261 	/* Don't use G_BINDING_SYNC_CREATE here.  The ESourceConfig widget
262 	 * is not ready to run check_complete() until after it's realized. */
263 	g_object_bind_property (
264 		config, "complete",
265 		widget, "sensitive",
266 		G_BINDING_DEFAULT);
267 
268 	widget = e_alert_bar_new ();
269 	gtk_box_pack_start (GTK_BOX (content_area), widget, FALSE, FALSE, 0);
270 	priv->alert_bar = g_object_ref (widget);
271 	/* EAlertBar controls its own visibility. */
272 
273 	handler_id = g_signal_connect (
274 		priv->alert_bar, "notify::visible",
275 		G_CALLBACK (source_config_alert_bar_visible_cb), object);
276 
277 	priv->alert_bar_visible_handler_id = handler_id;
278 }
279 
280 static void
281 source_config_dialog_response (GtkDialog *dialog,
282                                gint response_id)
283 {
284 	/* Do not chain up.  GtkDialog does not implement this method. */
285 
286 	switch (response_id) {
287 		case GTK_RESPONSE_OK:
288 			source_config_dialog_commit (
289 				E_SOURCE_CONFIG_DIALOG (dialog));
290 			break;
291 		case GTK_RESPONSE_CANCEL:
292 			gtk_widget_destroy (GTK_WIDGET (dialog));
293 			break;
294 		default:
295 			break;
296 	}
297 }
298 
299 static void
300 source_config_dialog_submit_alert (EAlertSink *alert_sink,
301                                    EAlert *alert)
302 {
303 	ESourceConfigDialogPrivate *priv;
304 	EAlertBar *alert_bar;
305 	GtkWidget *dialog;
306 	GtkWindow *parent;
307 
308 	priv = E_SOURCE_CONFIG_DIALOG_GET_PRIVATE (alert_sink);
309 
310 	switch (e_alert_get_message_type (alert)) {
311 		case GTK_MESSAGE_INFO:
312 		case GTK_MESSAGE_WARNING:
313 		case GTK_MESSAGE_ERROR:
314 			alert_bar = E_ALERT_BAR (priv->alert_bar);
315 			e_alert_bar_add_alert (alert_bar, alert);
316 			break;
317 
318 		default:
319 			parent = GTK_WINDOW (alert_sink);
320 			dialog = e_alert_dialog_new (parent, alert);
321 			gtk_dialog_run (GTK_DIALOG (dialog));
322 			gtk_widget_destroy (dialog);
323 			break;
324 	}
325 }
326 
327 static void
328 e_source_config_dialog_class_init (ESourceConfigDialogClass *class)
329 {
330 	GObjectClass *object_class;
331 	GtkDialogClass *dialog_class;
332 
333 	g_type_class_add_private (class, sizeof (ESourceConfigDialogPrivate));
334 
335 	object_class = G_OBJECT_CLASS (class);
336 	object_class->set_property = source_config_dialog_set_property;
337 	object_class->get_property = source_config_dialog_get_property;
338 	object_class->dispose = source_config_dialog_dispose;
339 	object_class->constructed = source_config_dialog_constructed;
340 
341 	dialog_class = GTK_DIALOG_CLASS (class);
342 	dialog_class->response = source_config_dialog_response;
343 
344 	g_object_class_install_property (
345 		object_class,
346 		PROP_CONFIG,
347 		g_param_spec_object (
348 			"config",
349 			"Config",
350 			"The ESourceConfig instance",
351 			E_TYPE_SOURCE_CONFIG,
352 			G_PARAM_READWRITE |
353 			G_PARAM_CONSTRUCT_ONLY |
354 			G_PARAM_STATIC_STRINGS));
355 }
356 
357 static void
358 e_source_config_dialog_alert_sink_init (EAlertSinkInterface *interface)
359 {
360 	interface->submit_alert = source_config_dialog_submit_alert;
361 }
362 
363 static void
364 e_source_config_dialog_init (ESourceConfigDialog *dialog)
365 {
366 	dialog->priv = E_SOURCE_CONFIG_DIALOG_GET_PRIVATE (dialog);
367 
368 	gtk_dialog_add_buttons (
369 		GTK_DIALOG (dialog),
370 		GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
371 		GTK_STOCK_OK, GTK_RESPONSE_OK,
372 		NULL);
373 
374 	gtk_dialog_set_default_response (
375 		GTK_DIALOG (dialog), GTK_RESPONSE_OK);
376 }
377 
378 GtkWidget *
379 e_source_config_dialog_new (ESourceConfig *config)
380 {
381 	g_return_val_if_fail (E_IS_SOURCE_CONFIG (config), NULL);
382 
383 	return g_object_new (
384 		E_TYPE_SOURCE_CONFIG_DIALOG,
385 		"config", config, NULL);
386 }
387 
388 ESourceConfig *
389 e_source_config_dialog_get_config (ESourceConfigDialog *dialog)
390 {
391 	g_return_val_if_fail (E_IS_SOURCE_CONFIG_DIALOG (dialog), NULL);
392 
393 	return dialog->priv->config;
394 }