summaryrefslogtreecommitdiff
path: root/e-util/e-source-config-dialog.c
blob: d7b4d55b694de66e75492589dfa7e0b31c64f937 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 * e-source-config-dialog.c
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib/gi18n-lib.h>

#include "e-source-config-dialog.h"

#include "e-alert-bar.h"
#include "e-alert-dialog.h"
#include "e-alert-sink.h"
#include "e-misc-utils.h"

#define E_SOURCE_CONFIG_DIALOG_GET_PRIVATE(obj) \
	(G_TYPE_INSTANCE_GET_PRIVATE \
	((obj), E_TYPE_SOURCE_CONFIG_DIALOG, ESourceConfigDialogPrivate))

struct _ESourceConfigDialogPrivate {
	ESourceConfig *config;
	ESourceRegistry *registry;

	GtkWidget *alert_bar;
	gulong alert_bar_visible_handler_id;
};

enum {
	PROP_0,
	PROP_CONFIG
};

/* Forward Declarations */
static void	e_source_config_dialog_alert_sink_init
					(EAlertSinkInterface *iface);

G_DEFINE_TYPE_WITH_CODE (
	ESourceConfigDialog,
	e_source_config_dialog,
	GTK_TYPE_DIALOG,
	G_IMPLEMENT_INTERFACE (
		E_TYPE_ALERT_SINK,
		e_source_config_dialog_alert_sink_init))

static void
source_config_dialog_commit_cb (GObject *object,
                                GAsyncResult *result,
                                gpointer user_data)
{
	ESourceConfig *config;
	ESourceConfigDialog *dialog;
	GdkWindow *gdk_window;
	GError *error = NULL;

	config = E_SOURCE_CONFIG (object);
	dialog = E_SOURCE_CONFIG_DIALOG (user_data);

	/* Set the cursor back to normal. */
	gdk_window = gtk_widget_get_window (GTK_WIDGET (dialog));
	gdk_window_set_cursor (gdk_window, NULL);

	/* Allow user interaction with window content. */
	gtk_widget_set_sensitive (GTK_WIDGET (dialog), TRUE);

	e_source_config_commit_finish (config, result, &error);

	/* Ignore cancellations. */
	if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
		g_object_unref (dialog);
		g_error_free (error);

	} else if (error != NULL) {
		e_alert_submit (
			E_ALERT_SINK (dialog),
			"system:simple-error",
			error->message, NULL);
		g_object_unref (dialog);
		g_error_free (error);

	} else {
		gtk_widget_destroy (GTK_WIDGET (dialog));
	}
}

static void
source_config_dialog_commit (ESourceConfigDialog *dialog)
{
	GdkCursor *gdk_cursor;
	GdkWindow *gdk_window;
	ESourceConfig *config;

	config = e_source_config_dialog_get_config (dialog);

	/* Clear any previous alerts. */
	e_alert_bar_clear (E_ALERT_BAR (dialog->priv->alert_bar));

	/* Make the cursor appear busy. */
	gdk_cursor = gdk_cursor_new (GDK_WATCH);
	gdk_window = gtk_widget_get_window (GTK_WIDGET (dialog));
	gdk_window_set_cursor (gdk_window, gdk_cursor);
	g_object_unref (gdk_cursor);

	/* Prevent user interaction with window content. */
	gtk_widget_set_sensitive (GTK_WIDGET (dialog), FALSE);

	/* XXX This operation is not cancellable. */
	e_source_config_commit (
		config, NULL,
		source_config_dialog_commit_cb,
		g_object_ref (dialog));
}

static void
source_config_dialog_source_removed_cb (ESourceRegistry *registry,
                                        ESource *removed_source,
                                        ESourceConfigDialog *dialog)
{
	ESourceConfig *config;
	ESource *original_source;

	/* If the ESource being edited is removed, cancel the dialog. */

	config = e_source_config_dialog_get_config (dialog);
	original_source = e_source_config_get_original_source (config);

	if (original_source == NULL)
		return;

	if (!e_source_equal (original_source, removed_source))
		return;

	gtk_dialog_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
}

static void
source_config_alert_bar_visible_cb (EAlertBar *alert_bar,
                                    GParamSpec *pspec,
                                    ESourceConfigDialog *dialog)
{
	e_source_config_resize_window (dialog->priv->config);
}

static void
source_config_dialog_set_config (ESourceConfigDialog *dialog,
                                 ESourceConfig *config)
{
	ESourceRegistry *registry;

	g_return_if_fail (E_IS_SOURCE_CONFIG (config));
	g_return_if_fail (dialog->priv->config == NULL);

	dialog->priv->config = g_object_ref (config);

	registry = e_source_config_get_registry (config);
	dialog->priv->registry = g_object_ref (registry);

	g_signal_connect (
		registry, "source-removed",
		G_CALLBACK (source_config_dialog_source_removed_cb), dialog);
}

static void
source_config_dialog_set_property (GObject *object,
                                   guint property_id,
                                   const GValue *value,
                                   GParamSpec *pspec)
{
	switch (property_id) {
		case PROP_CONFIG:
			source_config_dialog_set_config (
				E_SOURCE_CONFIG_DIALOG (object),
				g_value_get_object (value));
			return;
	}

	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
source_config_dialog_get_property (GObject *object,
                                   guint property_id,
                                   GValue *value,
                                   GParamSpec *pspec)
{
	switch (property_id) {
		case PROP_CONFIG:
			g_value_set_object (
				value,
				e_source_config_dialog_get_config (
				E_SOURCE_CONFIG_DIALOG (object)));
			return;
	}

	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
source_config_dialog_dispose (GObject *object)
{
	ESourceConfigDialogPrivate *priv;

	priv = E_SOURCE_CONFIG_DIALOG_GET_PRIVATE (object);

	if (priv->config != NULL) {
		g_object_unref (priv->config);
		priv->config = NULL;
	}

	if (priv->registry != NULL) {
		g_signal_handlers_disconnect_matched (
			priv->registry, G_SIGNAL_MATCH_DATA,
			0, 0, NULL, NULL, object);
		g_object_unref (priv->registry);
		priv->registry = NULL;
	}

	if (priv->alert_bar != NULL) {
		g_signal_handler_disconnect (
			priv->alert_bar,
			priv->alert_bar_visible_handler_id);
		g_object_unref (priv->alert_bar);
		priv->alert_bar = NULL;
	}

	/* Chain up to parent's dispose() method. */
	G_OBJECT_CLASS (e_source_config_dialog_parent_class)->dispose (object);
}

static void
source_config_dialog_constructed (GObject *object)
{
	ESourceConfigDialogPrivate *priv;
	GtkWidget *content_area;
	GtkWidget *config;
	GtkWidget *widget;
	gulong handler_id;

	/* Chain up to parent's method. */
	G_OBJECT_CLASS (e_source_config_dialog_parent_class)->constructed (object);

	priv = E_SOURCE_CONFIG_DIALOG_GET_PRIVATE (object);

	config = GTK_WIDGET (priv->config);

	widget = gtk_dialog_get_widget_for_response (
		GTK_DIALOG (object), GTK_RESPONSE_OK);

	gtk_container_set_border_width (GTK_CONTAINER (object), 5);
	gtk_container_set_border_width (GTK_CONTAINER (config), 5);

	content_area = gtk_dialog_get_content_area (GTK_DIALOG (object));
	gtk_box_pack_start (GTK_BOX (content_area), config, TRUE, TRUE, 0);
	gtk_widget_show (config);

	/* Don't use G_BINDING_SYNC_CREATE here.  The ESourceConfig widget
	 * is not ready to run check_complete() until after it's realized. */
	e_binding_bind_property (
		config, "complete",
		widget, "sensitive",
		G_BINDING_DEFAULT);

	widget = e_alert_bar_new ();
	gtk_box_pack_start (GTK_BOX (content_area), widget, FALSE, FALSE, 0);
	priv->alert_bar = g_object_ref (widget);
	/* EAlertBar controls its own visibility. */

	handler_id = e_signal_connect_notify (
		priv->alert_bar, "notify::visible",
		G_CALLBACK (source_config_alert_bar_visible_cb), object);

	priv->alert_bar_visible_handler_id = handler_id;
}

static void
source_config_dialog_response (GtkDialog *dialog,
                               gint response_id)
{
	/* Do not chain up.  GtkDialog does not implement this method. */

	switch (response_id) {
		case GTK_RESPONSE_OK:
			source_config_dialog_commit (
				E_SOURCE_CONFIG_DIALOG (dialog));
			break;
		case GTK_RESPONSE_CANCEL:
			gtk_widget_destroy (GTK_WIDGET (dialog));
			break;
		default:
			break;
	}
}

static void
source_config_dialog_submit_alert (EAlertSink *alert_sink,
                                   EAlert *alert)
{
	ESourceConfigDialogPrivate *priv;
	EAlertBar *alert_bar;
	GtkWidget *dialog;
	GtkWindow *parent;

	priv = E_SOURCE_CONFIG_DIALOG_GET_PRIVATE (alert_sink);

	switch (e_alert_get_message_type (alert)) {
		case GTK_MESSAGE_INFO:
		case GTK_MESSAGE_WARNING:
		case GTK_MESSAGE_ERROR:
			alert_bar = E_ALERT_BAR (priv->alert_bar);
			e_alert_bar_add_alert (alert_bar, alert);
			break;

		default:
			parent = GTK_WINDOW (alert_sink);
			dialog = e_alert_dialog_new (parent, alert);
			gtk_dialog_run (GTK_DIALOG (dialog));
			gtk_widget_destroy (dialog);
			break;
	}
}

static void
e_source_config_dialog_class_init (ESourceConfigDialogClass *class)
{
	GObjectClass *object_class;
	GtkDialogClass *dialog_class;

	g_type_class_add_private (class, sizeof (ESourceConfigDialogPrivate));

	object_class = G_OBJECT_CLASS (class);
	object_class->set_property = source_config_dialog_set_property;
	object_class->get_property = source_config_dialog_get_property;
	object_class->dispose = source_config_dialog_dispose;
	object_class->constructed = source_config_dialog_constructed;

	dialog_class = GTK_DIALOG_CLASS (class);
	dialog_class->response = source_config_dialog_response;

	g_object_class_install_property (
		object_class,
		PROP_CONFIG,
		g_param_spec_object (
			"config",
			"Config",
			"The ESourceConfig instance",
			E_TYPE_SOURCE_CONFIG,
			G_PARAM_READWRITE |
			G_PARAM_CONSTRUCT_ONLY |
			G_PARAM_STATIC_STRINGS));
}

static void
e_source_config_dialog_alert_sink_init (EAlertSinkInterface *iface)
{
	iface->submit_alert = source_config_dialog_submit_alert;
}

static void
e_source_config_dialog_init (ESourceConfigDialog *dialog)
{
	dialog->priv = E_SOURCE_CONFIG_DIALOG_GET_PRIVATE (dialog);

	gtk_dialog_add_buttons (
		GTK_DIALOG (dialog),
		_("_Cancel"), GTK_RESPONSE_CANCEL,
		_("_OK"), GTK_RESPONSE_OK,
		NULL);

	gtk_dialog_set_default_response (
		GTK_DIALOG (dialog), GTK_RESPONSE_OK);
}

GtkWidget *
e_source_config_dialog_new (ESourceConfig *config)
{
	g_return_val_if_fail (E_IS_SOURCE_CONFIG (config), NULL);

	return g_object_new (
		E_TYPE_SOURCE_CONFIG_DIALOG,
		"config", config, NULL);
}

ESourceConfig *
e_source_config_dialog_get_config (ESourceConfigDialog *dialog)
{
	g_return_val_if_fail (E_IS_SOURCE_CONFIG_DIALOG (dialog), NULL);

	return dialog->priv->config;
}