evolution-3.6.4/modules/book-config-webdav/evolution-book-config-webdav.c

No issues found

  1 /*
  2  * evolution-book-config-webdav.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 <config.h>
 20 #include <glib/gi18n-lib.h>
 21 
 22 #include <libebackend/libebackend.h>
 23 
 24 #include <misc/e-book-source-config.h>
 25 #include <misc/e-source-config-backend.h>
 26 
 27 typedef ESourceConfigBackend EBookConfigWebdav;
 28 typedef ESourceConfigBackendClass EBookConfigWebdavClass;
 29 
 30 typedef struct _Context Context;
 31 
 32 struct _Context {
 33 	GtkWidget *url_entry;
 34 	GtkWidget *avoid_ifmatch;
 35 };
 36 
 37 /* Module Entry Points */
 38 void e_module_load (GTypeModule *type_module);
 39 void e_module_unload (GTypeModule *type_module);
 40 
 41 /* Forward Declarations */
 42 GType e_book_config_webdav_get_type (void);
 43 
 44 G_DEFINE_DYNAMIC_TYPE (
 45 	EBookConfigWebdav,
 46 	e_book_config_webdav,
 47 	E_TYPE_SOURCE_CONFIG_BACKEND)
 48 
 49 static void
 50 book_config_webdav_context_free (Context *context)
 51 {
 52 	g_object_unref (context->url_entry);
 53 	g_object_unref (context->avoid_ifmatch);
 54 
 55 	g_slice_free (Context, context);
 56 }
 57 
 58 static gboolean
 59 book_config_webdav_uri_to_text (GBinding *binding,
 60                                 const GValue *source_value,
 61                                 GValue *target_value,
 62                                 gpointer user_data)
 63 {
 64 	SoupURI *soup_uri;
 65 	gchar *text;
 66 
 67 	soup_uri = g_value_get_boxed (source_value);
 68 	soup_uri_set_user (soup_uri, NULL);
 69 
 70 	text = soup_uri_to_string (soup_uri, FALSE);
 71 	g_value_take_string (target_value, text);
 72 
 73 	return TRUE;
 74 }
 75 
 76 static gboolean
 77 book_config_webdav_text_to_uri (GBinding *binding,
 78                                 const GValue *source_value,
 79                                 GValue *target_value,
 80                                 gpointer user_data)
 81 {
 82 	ESource *source;
 83 	SoupURI *soup_uri;
 84 	GObject *source_binding;
 85 	ESourceAuthentication *extension;
 86 	const gchar *extension_name;
 87 	const gchar *text;
 88 	const gchar *user;
 89 
 90 	text = g_value_get_string (source_value);
 91 	soup_uri = soup_uri_new (text);
 92 
 93 	if (soup_uri == NULL)
 94 		return FALSE;
 95 
 96 	source_binding = g_binding_get_source (binding);
 97 	source = e_source_extension_get_source (
 98 		E_SOURCE_EXTENSION (source_binding));
 99 
100 	extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
101 	extension = e_source_get_extension (source, extension_name);
102 	user = e_source_authentication_get_user (extension);
103 
104 	soup_uri_set_user (soup_uri, user);
105 
106 	g_value_take_boxed (target_value, soup_uri);
107 
108 	return TRUE;
109 }
110 
111 static void
112 book_config_webdav_insert_widgets (ESourceConfigBackend *backend,
113                                    ESource *scratch_source)
114 {
115 	ESourceConfig *config;
116 	ESourceExtension *extension;
117 	GtkWidget *widget;
118 	Context *context;
119 	const gchar *extension_name;
120 	const gchar *uid;
121 
122 	context = g_slice_new (Context);
123 	uid = e_source_get_uid (scratch_source);
124 	config = e_source_config_backend_get_config (backend);
125 
126 	g_object_set_data_full (
127 		G_OBJECT (backend), uid, context,
128 		(GDestroyNotify) book_config_webdav_context_free);
129 
130 	e_book_source_config_add_offline_toggle (
131 		E_BOOK_SOURCE_CONFIG (config), scratch_source);
132 
133 	widget = gtk_entry_new ();
134 	e_source_config_insert_widget (
135 		config, scratch_source, _("URL:"), widget);
136 	context->url_entry = g_object_ref (widget);
137 	gtk_widget_show (widget);
138 
139 	e_source_config_add_secure_connection_for_webdav (
140 		config, scratch_source);
141 
142 	e_source_config_add_user_entry (config, scratch_source);
143 
144 	widget = gtk_check_button_new_with_label (
145 		_("Avoid IfMatch (needed on Apache < 2.2.8)"));
146 	e_source_config_insert_widget (
147 		config, scratch_source, NULL, widget);
148 	context->avoid_ifmatch = g_object_ref (widget);
149 	gtk_widget_show (widget);
150 
151 	extension_name = E_SOURCE_EXTENSION_WEBDAV_BACKEND;
152 	extension = e_source_get_extension (scratch_source, extension_name);
153 
154 	g_object_bind_property (
155 		extension, "avoid-ifmatch",
156 		context->avoid_ifmatch, "active",
157 		G_BINDING_BIDIRECTIONAL |
158 		G_BINDING_SYNC_CREATE);
159 
160 	g_object_bind_property_full (
161 		extension, "soup-uri",
162 		context->url_entry, "text",
163 		G_BINDING_BIDIRECTIONAL |
164 		G_BINDING_SYNC_CREATE,
165 		book_config_webdav_uri_to_text,
166 		book_config_webdav_text_to_uri,
167 		NULL, (GDestroyNotify) NULL);
168 }
169 
170 static gboolean
171 book_config_webdav_check_complete (ESourceConfigBackend *backend,
172                                    ESource *scratch_source)
173 {
174 	SoupURI *soup_uri;
175 	GtkEntry *entry;
176 	Context *context;
177 	const gchar *uri_string;
178 	const gchar *uid;
179 	gboolean complete;
180 
181 	uid = e_source_get_uid (scratch_source);
182 	context = g_object_get_data (G_OBJECT (backend), uid);
183 	g_return_val_if_fail (context != NULL, FALSE);
184 
185 	entry = GTK_ENTRY (context->url_entry);
186 	uri_string = gtk_entry_get_text (entry);
187 
188 	soup_uri = soup_uri_new (uri_string);
189 	complete = SOUP_URI_VALID_FOR_HTTP (soup_uri);
190 
191 	if (soup_uri != NULL)
192 		soup_uri_free (soup_uri);
193 
194 	return complete;
195 }
196 
197 static void
198 e_book_config_webdav_class_init (ESourceConfigBackendClass *class)
199 {
200 	EExtensionClass *extension_class;
201 
202 	extension_class = E_EXTENSION_CLASS (class);
203 	extension_class->extensible_type = E_TYPE_BOOK_SOURCE_CONFIG;
204 
205 	class->parent_uid = "webdav-stub";
206 	class->backend_name = "webdav";
207 	class->insert_widgets = book_config_webdav_insert_widgets;
208 	class->check_complete = book_config_webdav_check_complete;
209 }
210 
211 static void
212 e_book_config_webdav_class_finalize (ESourceConfigBackendClass *class)
213 {
214 }
215 
216 static void
217 e_book_config_webdav_init (ESourceConfigBackend *backend)
218 {
219 }
220 
221 G_MODULE_EXPORT void
222 e_module_load (GTypeModule *type_module)
223 {
224 	e_book_config_webdav_register_type (type_module);
225 }
226 
227 G_MODULE_EXPORT void
228 e_module_unload (GTypeModule *type_module)
229 {
230 }