Bug 790069 - Free/Busy Publishing should not include X-SUMMARY and X-LOCATION
[evolution.git] / src / plugins / publish-calendar / url-editor-dialog.c
blobc202a5e753621bc4d310dafe5bd4e8f188881f8d
1 /*
2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU Lesser General Public License as published by
4 * the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful, but
7 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
9 * for more details.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, see <http://www.gnu.org/licenses/>.
15 * Authors:
16 * David Trowbridge <trowbrds@cs.colorado.edu>
18 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
22 #include "evolution-config.h"
24 #include "url-editor-dialog.h"
26 #include <string.h>
27 #include <glib/gi18n.h>
29 #include <e-util/e-util.h>
30 #include <e-util/e-util-private.h>
32 #include <shell/e-shell.h>
34 G_DEFINE_TYPE (
35 UrlEditorDialog,
36 url_editor_dialog,
37 GTK_TYPE_DIALOG)
39 static void
40 create_uri (UrlEditorDialog *dialog)
42 EPublishUri *uri;
44 uri = dialog->uri;
46 if (uri->service_type == TYPE_URI) {
47 if (uri->location)
48 g_free (uri->location);
49 uri->location = g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->server_entry)));
50 } else {
51 const gchar *method = "file";
52 gchar *server, *file, *port, *username, *password;
54 server = g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->server_entry)));
55 file = g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->file_entry)));
56 port = g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->port_entry)));
57 username = g_uri_escape_string (gtk_entry_get_text (GTK_ENTRY (dialog->username_entry)), "", FALSE);
58 password = g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->password_entry)));
60 switch (uri->service_type) {
61 case TYPE_SMB:
62 method = "smb";
63 break;
65 case TYPE_SFTP:
66 method = "sftp";
67 break;
69 case TYPE_ANON_FTP:
70 g_free (username);
71 username = g_strdup ("anonymous");
72 method = "ftp";
73 break;
75 case TYPE_FTP:
76 method = "ftp";
77 break;
79 case TYPE_DAV:
80 method = "dav";
81 break;
83 case TYPE_DAVS:
84 method = "davs";
85 break;
88 if (uri->location)
89 g_free (uri->location);
90 uri->location = g_strdup_printf (
91 "%s://%s%s%s%s%s%s%s",
92 method,
93 username, (username[0] != '\0') ? "@" : "",
94 server,
95 (port[0] != '\0') ? ":" : "", port,
96 (file[0] != '/') ? "/" : "", file);
98 g_free (server);
99 g_free (file);
100 g_free (port);
101 g_free (username);
102 g_free (password);
105 uri->fb_duration_value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (dialog->fb_duration_spin));
106 uri->fb_duration_type = gtk_combo_box_get_active (GTK_COMBO_BOX (dialog->fb_duration_combo));
109 static void
110 check_input (UrlEditorDialog *dialog)
112 gint n = 0;
113 GList *sources;
114 EPublishUri *uri;
116 uri = dialog->uri;
118 if (gtk_combo_box_get_active (GTK_COMBO_BOX (dialog->type_selector)) == URI_PUBLISH_AS_ICAL) {
119 gtk_widget_hide (dialog->fb_duration_label);
120 gtk_widget_hide (dialog->fb_duration_spin);
121 gtk_widget_hide (dialog->fb_duration_combo);
122 } else {
123 gtk_widget_show (dialog->fb_duration_label);
124 gtk_widget_show (dialog->fb_duration_spin);
125 gtk_widget_show (dialog->fb_duration_combo);
128 if (gtk_widget_get_sensitive (dialog->events_selector)) {
129 sources = e_source_selector_get_selection (
130 E_SOURCE_SELECTOR (dialog->events_selector));
131 n += g_list_length (sources);
132 g_list_free_full (sources, (GDestroyNotify) g_object_unref);
134 if (n == 0)
135 goto fail;
137 /* This should probably be more complex, since ' ' isn't a valid server name */
138 switch (uri->service_type) {
139 case TYPE_SMB:
140 case TYPE_SFTP:
141 case TYPE_FTP:
142 case TYPE_DAV:
143 case TYPE_DAVS:
144 case TYPE_ANON_FTP:
145 if (!strlen (gtk_entry_get_text (GTK_ENTRY (dialog->server_entry)))) goto fail;
146 if (!strlen (gtk_entry_get_text (GTK_ENTRY (dialog->file_entry)))) goto fail;
147 break;
148 case TYPE_URI:
149 if (!strlen (gtk_entry_get_text (GTK_ENTRY (dialog->server_entry)))) goto fail;
150 break;
153 create_uri (dialog);
155 gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, TRUE);
156 return;
157 fail:
158 gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, FALSE);
161 static void
162 source_selection_changed (ESourceSelector *selector,
163 UrlEditorDialog *dialog)
165 check_input (dialog);
168 static void
169 publish_service_changed (GtkComboBox *combo,
170 UrlEditorDialog *dialog)
172 gint selected = gtk_combo_box_get_active (combo);
173 EPublishUri *uri;
175 uri = dialog->uri;
177 /* Big mess that switches around all the fields to match the source type
178 * the user has selected. Tries to keep field contents where possible */
179 switch (selected) {
180 case TYPE_SMB:
181 gtk_label_set_text_with_mnemonic (GTK_LABEL (dialog->server_label), "_Server:");
182 gtk_label_set_text_with_mnemonic (GTK_LABEL (dialog->port_label), "_Port:");
183 gtk_label_set_text_with_mnemonic (GTK_LABEL (dialog->port_label), "S_hare:");
184 gtk_entry_set_text (GTK_ENTRY (dialog->port_entry), "");
185 gtk_widget_show (dialog->file_hbox);
186 gtk_widget_show (dialog->optional_label);
187 gtk_widget_show (dialog->port_hbox);
188 gtk_widget_show (dialog->username_hbox);
189 gtk_widget_show (dialog->password_hbox);
190 gtk_widget_show (dialog->remember_pw);
191 break;
192 case TYPE_SFTP:
193 case TYPE_FTP:
194 case TYPE_DAV:
195 case TYPE_DAVS:
196 gtk_label_set_text_with_mnemonic (GTK_LABEL (dialog->server_label), "_Server:");
197 gtk_label_set_text_with_mnemonic (GTK_LABEL (dialog->port_label), "_Port:");
198 if (uri->service_type == TYPE_SMB)
199 gtk_entry_set_text (GTK_ENTRY (dialog->port_entry), "");
200 else if (uri->service_type == TYPE_URI)
201 gtk_entry_set_text (GTK_ENTRY (dialog->server_entry), "");
202 gtk_widget_show (dialog->file_hbox);
203 gtk_widget_show (dialog->optional_label);
204 gtk_widget_show (dialog->port_hbox);
205 gtk_widget_show (dialog->username_hbox);
206 gtk_widget_show (dialog->password_hbox);
207 gtk_widget_show (dialog->remember_pw);
208 break;
209 case TYPE_ANON_FTP:
210 gtk_label_set_text_with_mnemonic (GTK_LABEL (dialog->server_label), "_Server:");
211 gtk_label_set_text_with_mnemonic (GTK_LABEL (dialog->port_label), "_Port:");
212 if (uri->service_type == TYPE_SMB)
213 gtk_entry_set_text (GTK_ENTRY (dialog->port_entry), "");
214 else if (uri->service_type == TYPE_URI)
215 gtk_entry_set_text (GTK_ENTRY (dialog->server_entry), "");
216 gtk_widget_show (dialog->file_hbox);
217 gtk_widget_show (dialog->optional_label);
218 gtk_widget_show (dialog->port_hbox);
219 gtk_widget_hide (dialog->username_hbox);
220 gtk_widget_hide (dialog->password_hbox);
221 gtk_widget_hide (dialog->remember_pw);
222 break;
223 case TYPE_URI:
224 gtk_label_set_text_with_mnemonic (GTK_LABEL (dialog->server_label), "_Location (URI):");
225 if (uri->service_type != TYPE_URI)
226 gtk_entry_set_text (GTK_ENTRY (dialog->server_entry), "");
227 gtk_widget_hide (dialog->file_hbox);
228 gtk_widget_hide (dialog->optional_label);
229 gtk_widget_hide (dialog->port_hbox);
230 gtk_widget_hide (dialog->username_hbox);
231 gtk_widget_hide (dialog->password_hbox);
232 gtk_widget_hide (dialog->remember_pw);
234 uri->service_type = selected;
235 check_input (dialog);
238 static void
239 type_selector_changed (GtkComboBox *combo,
240 UrlEditorDialog *dialog)
242 gint selected = gtk_combo_box_get_active (combo);
243 EPublishUri *uri;
245 uri = dialog->uri;
246 uri->publish_format = selected;
248 check_input (dialog);
251 static void
252 frequency_changed_cb (GtkComboBox *combo,
253 UrlEditorDialog *dialog)
255 gint selected = gtk_combo_box_get_active (combo);
257 EPublishUri *uri;
259 uri = dialog->uri;
260 uri->publish_frequency = selected;
263 static void
264 server_entry_changed (GtkEntry *entry,
265 UrlEditorDialog *dialog)
267 check_input (dialog);
270 static void
271 file_entry_changed (GtkEntry *entry,
272 UrlEditorDialog *dialog)
274 check_input (dialog);
277 static void
278 port_entry_changed (GtkEntry *entry,
279 UrlEditorDialog *dialog)
283 static void
284 username_entry_changed (GtkEntry *entry,
285 UrlEditorDialog *dialog)
289 static void
290 password_entry_changed (GtkEntry *entry,
291 UrlEditorDialog *dialog)
295 static void
296 remember_pw_toggled (GtkToggleButton *toggle,
297 UrlEditorDialog *dialog)
301 static void
302 set_from_uri (UrlEditorDialog *dialog)
304 EPublishUri *uri;
305 SoupURI *soup_uri;
306 const gchar *scheme;
307 const gchar *user;
308 const gchar *host;
309 const gchar *path;
310 guint port;
312 uri = dialog->uri;
314 soup_uri = soup_uri_new (uri->location);
315 g_return_if_fail (soup_uri != NULL);
317 /* determine our service type */
318 scheme = soup_uri_get_scheme (soup_uri);
319 g_return_if_fail (scheme != NULL);
321 if (strcmp (scheme, "smb") == 0)
322 uri->service_type = TYPE_SMB;
323 else if (strcmp (scheme, "sftp") == 0)
324 uri->service_type = TYPE_SFTP;
325 else if (strcmp (scheme, "ftp") == 0)
326 /* we set TYPE_FTP here for now. if we don't find a
327 * username later, we'll change it to TYPE_ANON_FTP */
328 uri->service_type = TYPE_FTP;
329 else if (strcmp (scheme, "dav") == 0)
330 uri->service_type = TYPE_DAV;
331 else if (strcmp (scheme, "davs") == 0)
332 uri->service_type = TYPE_DAVS;
333 else
334 uri->service_type = TYPE_URI;
336 user = soup_uri_get_user (soup_uri);
337 host = soup_uri_get_host (soup_uri);
338 port = soup_uri_get_port (soup_uri);
339 path = soup_uri_get_path (soup_uri);
341 if (user != NULL)
342 gtk_entry_set_text (GTK_ENTRY (dialog->username_entry), user);
344 if (host != NULL)
345 gtk_entry_set_text (GTK_ENTRY (dialog->server_entry), host);
347 if (port > 0) {
348 gchar *port_str;
349 port_str = g_strdup_printf ("%d", port);
350 gtk_entry_set_text (GTK_ENTRY (dialog->port_entry), port_str);
351 g_free (port_str);
354 if (path != NULL)
355 gtk_entry_set_text (GTK_ENTRY (dialog->file_entry), path);
357 if (uri->service_type == TYPE_URI)
358 gtk_entry_set_text (GTK_ENTRY (dialog->server_entry), uri->location);
360 gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->publish_service), uri->service_type);
362 soup_uri_free (soup_uri);
365 static gboolean
366 url_editor_dialog_construct (UrlEditorDialog *dialog)
368 EShell *shell;
369 GtkWidget *toplevel;
370 GtkWidget *content_area;
371 GtkSizeGroup *group;
372 EPublishUri *uri;
373 ESourceRegistry *registry;
375 dialog->builder = gtk_builder_new ();
376 e_load_ui_builder_definition (dialog->builder, "publish-calendar.ui");
378 #define GW(name) ((dialog->name) = e_builder_get_widget (dialog->builder, #name))
379 GW (type_selector);
380 GW (fb_duration_label);
381 GW (fb_duration_spin);
382 GW (fb_duration_combo);
383 GW (publish_frequency);
385 GW (events_swin);
387 GW (publish_service);
388 GW (server_entry);
389 GW (file_entry);
391 GW (port_entry);
392 GW (username_entry);
393 GW (password_entry);
394 GW (remember_pw);
396 GW (optional_label);
398 GW (port_hbox);
399 GW (username_hbox);
400 GW (password_hbox);
401 GW (server_hbox);
402 GW (file_hbox);
404 GW (port_label);
405 GW (username_label);
406 GW (password_label);
407 GW (server_label);
408 GW (file_label);
409 #undef GW
411 uri = dialog->uri;
413 content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
414 toplevel = e_builder_get_widget (dialog->builder, "publishing toplevel");
415 gtk_container_add (GTK_CONTAINER (content_area), toplevel);
417 gtk_container_set_border_width (GTK_CONTAINER (dialog), 6);
419 dialog->cancel = gtk_dialog_add_button (GTK_DIALOG (dialog), _("_Cancel"), GTK_RESPONSE_CANCEL);
420 dialog->ok = gtk_dialog_add_button (GTK_DIALOG (dialog), _("_OK"), GTK_RESPONSE_OK);
421 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
422 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
423 gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, FALSE);
425 shell = e_shell_get_default ();
426 registry = e_shell_get_registry (shell);
427 dialog->events_selector = e_source_selector_new (
428 registry, E_SOURCE_EXTENSION_CALENDAR);
429 gtk_widget_show (dialog->events_selector);
430 gtk_container_add (GTK_CONTAINER (dialog->events_swin), dialog->events_selector);
432 group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
433 gtk_size_group_add_widget (group, dialog->type_selector);
434 gtk_size_group_add_widget (group, dialog->publish_frequency);
435 g_object_unref (group);
437 group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
438 gtk_size_group_add_widget (group, dialog->publish_service);
439 gtk_size_group_add_widget (group, dialog->server_entry);
440 gtk_size_group_add_widget (group, dialog->file_entry);
441 gtk_size_group_add_widget (group, dialog->port_entry);
442 gtk_size_group_add_widget (group, dialog->username_entry);
443 gtk_size_group_add_widget (group, dialog->password_entry);
444 g_object_unref (group);
446 if (uri == NULL) {
447 gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->publish_frequency), 0);
448 gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->type_selector), 0);
449 gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->publish_service), 0);
451 dialog->uri = g_new0 (EPublishUri, 1);
452 uri = dialog->uri;
453 uri->enabled = TRUE;
454 } else {
455 ESource *source;
456 GSList *p;
458 for (p = uri->events; p; p = g_slist_next (p)) {
459 const gchar *uid = p->data;
461 source = e_source_registry_ref_source (
462 registry, uid);
463 e_source_selector_select_source (
464 E_SOURCE_SELECTOR (dialog->events_selector),
465 source);
466 g_object_unref (source);
469 if (uri->location && strlen (uri->location)) {
470 set_from_uri (dialog);
473 gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->publish_frequency), uri->publish_frequency);
474 gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->type_selector), uri->publish_format);
476 uri->password = e_passwords_get_password (uri->location);
477 if (uri->password) {
478 if (strlen (uri->password) != 0) {
479 gtk_entry_set_text (GTK_ENTRY (dialog->password_entry), uri->password);
480 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->remember_pw), TRUE);
481 } else {
482 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->remember_pw), FALSE);
487 gtk_spin_button_set_value (GTK_SPIN_BUTTON (dialog->fb_duration_spin), uri->fb_duration_value);
488 gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->fb_duration_combo), uri->fb_duration_type);
490 type_selector_changed (GTK_COMBO_BOX (dialog->type_selector), dialog);
491 frequency_changed_cb (GTK_COMBO_BOX (dialog->publish_frequency), dialog);
492 publish_service_changed (GTK_COMBO_BOX (dialog->publish_service), dialog);
494 g_signal_connect (
495 dialog->publish_service, "changed",
496 G_CALLBACK (publish_service_changed), dialog);
497 g_signal_connect (
498 dialog->type_selector, "changed",
499 G_CALLBACK (type_selector_changed), dialog);
500 g_signal_connect (
501 dialog->publish_frequency, "changed",
502 G_CALLBACK (frequency_changed_cb), dialog);
503 g_signal_connect (
504 dialog->events_selector, "selection_changed",
505 G_CALLBACK (source_selection_changed), dialog);
507 g_signal_connect (
508 dialog->server_entry, "changed",
509 G_CALLBACK (server_entry_changed), dialog);
510 g_signal_connect (
511 dialog->file_entry, "changed",
512 G_CALLBACK (file_entry_changed), dialog);
513 g_signal_connect (
514 dialog->port_entry, "changed",
515 G_CALLBACK (port_entry_changed), dialog);
516 g_signal_connect (
517 dialog->username_entry, "changed",
518 G_CALLBACK (username_entry_changed), dialog);
519 g_signal_connect (
520 dialog->password_entry,"changed",
521 G_CALLBACK (password_entry_changed), dialog);
522 g_signal_connect (
523 dialog->remember_pw, "toggled",
524 G_CALLBACK (remember_pw_toggled), dialog);
526 check_input (dialog);
528 return TRUE;
531 GtkWidget *
532 url_editor_dialog_new (GtkTreeModel *url_list_model,
533 EPublishUri *uri)
535 UrlEditorDialog *dialog;
537 dialog = (UrlEditorDialog *) g_object_new (URL_EDITOR_DIALOG_TYPE, NULL);
538 dialog->url_list_model = g_object_ref (url_list_model);
539 dialog->uri = uri;
541 if (!uri)
542 gtk_window_set_title (GTK_WINDOW (dialog), _("New Location"));
543 else
544 gtk_window_set_title (GTK_WINDOW (dialog), _("Edit Location"));
546 if (url_editor_dialog_construct (dialog))
547 return GTK_WIDGET (dialog);
549 g_object_unref (dialog);
550 return NULL;
553 static void
554 url_editor_dialog_dispose (GObject *obj)
556 UrlEditorDialog *dialog = (UrlEditorDialog *) obj;
558 if (dialog->url_list_model) {
559 g_object_unref (dialog->url_list_model);
560 dialog->url_list_model = NULL;
562 if (dialog->builder) {
563 g_object_unref (dialog->builder);
564 dialog->builder = NULL;
567 G_OBJECT_CLASS (url_editor_dialog_parent_class)->dispose (obj);
570 static void
571 url_editor_dialog_class_init (UrlEditorDialogClass *class)
573 GObjectClass *object_class;
575 object_class = G_OBJECT_CLASS (class);
576 object_class->dispose = url_editor_dialog_dispose;
579 static void
580 url_editor_dialog_init (UrlEditorDialog *dialog)
584 gboolean
585 url_editor_dialog_run (UrlEditorDialog *dialog)
587 gint response;
589 response = gtk_dialog_run (GTK_DIALOG (dialog));
590 if (response == GTK_RESPONSE_OK) {
591 GList *list, *link;
593 if (dialog->uri->password)
594 g_free (dialog->uri->password);
595 if (dialog->uri->events) {
596 g_slist_foreach (dialog->uri->events, (GFunc) g_free, NULL);
597 dialog->uri->events = NULL;
600 create_uri (dialog);
602 dialog->uri->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->password_entry)));
604 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->remember_pw))) {
605 e_passwords_add_password (dialog->uri->location, dialog->uri->password);
606 e_passwords_remember_password (dialog->uri->location);
607 } else {
608 e_passwords_forget_password (dialog->uri->location);
611 list = e_source_selector_get_selection (
612 E_SOURCE_SELECTOR (dialog->events_selector));
614 for (link = list; link != NULL; link = g_list_next (link)) {
615 ESource *source;
616 const gchar *uid;
618 source = E_SOURCE (link->data);
619 uid = e_source_get_uid (source);
620 dialog->uri->events = g_slist_append (
621 dialog->uri->events, g_strdup (uid));
624 g_list_free_full (list, (GDestroyNotify) g_object_unref);
626 gtk_widget_hide (GTK_WIDGET (dialog));
628 return response == GTK_RESPONSE_OK;