evolution-3.6.4/modules/cal-config-webcal/evolution-cal-config-webcal.c

No issues found

  1 /*
  2  * evolution-cal-config-webcal.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-cal-source-config.h>
 25 #include <misc/e-interval-chooser.h>
 26 #include <misc/e-source-config-backend.h>
 27 
 28 typedef ESourceConfigBackend ECalConfigWebcal;
 29 typedef ESourceConfigBackendClass ECalConfigWebcalClass;
 30 
 31 typedef struct _Context Context;
 32 
 33 struct _Context {
 34 	GtkWidget *url_entry;
 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_cal_config_webcal_get_type (void);
 43 
 44 G_DEFINE_DYNAMIC_TYPE (
 45 	ECalConfigWebcal,
 46 	e_cal_config_webcal,
 47 	E_TYPE_SOURCE_CONFIG_BACKEND)
 48 
 49 static void
 50 cal_config_webcal_context_free (Context *context)
 51 {
 52 	g_object_unref (context->url_entry);
 53 
 54 	g_slice_free (Context, context);
 55 }
 56 
 57 static gboolean
 58 cal_config_webcal_uri_to_text (GBinding *binding,
 59                                const GValue *source_value,
 60                                GValue *target_value,
 61                                gpointer user_data)
 62 {
 63 	SoupURI *soup_uri;
 64 	gchar *text;
 65 
 66 	soup_uri = g_value_get_boxed (source_value);
 67 	soup_uri_set_user (soup_uri, NULL);
 68 
 69 	text = soup_uri_to_string (soup_uri, FALSE);
 70 	g_value_take_string (target_value, text);
 71 
 72 	return TRUE;
 73 }
 74 
 75 static gboolean
 76 cal_config_webcal_text_to_uri (GBinding *binding,
 77                                const GValue *source_value,
 78                                GValue *target_value,
 79                                gpointer user_data)
 80 {
 81 	ESource *source;
 82 	SoupURI *soup_uri;
 83 	ESourceAuthentication *extension;
 84 	const gchar *extension_name;
 85 	const gchar *text;
 86 	const gchar *user;
 87 
 88 	text = g_value_get_string (source_value);
 89 	soup_uri = soup_uri_new (text);
 90 
 91 	if (soup_uri == NULL)
 92 		return FALSE;
 93 
 94 	source = E_SOURCE (user_data);
 95 	extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
 96 	extension = e_source_get_extension (source, extension_name);
 97 	user = e_source_authentication_get_user (extension);
 98 
 99 	soup_uri_set_user (soup_uri, user);
100 
101 	g_value_take_boxed (target_value, soup_uri);
102 
103 	return TRUE;
104 }
105 
106 static void
107 cal_config_webcal_insert_widgets (ESourceConfigBackend *backend,
108                                   ESource *scratch_source)
109 {
110 	ESourceConfig *config;
111 	ESourceExtension *extension;
112 	GtkWidget *widget;
113 	Context *context;
114 	const gchar *extension_name;
115 	const gchar *uid;
116 
117 	context = g_slice_new (Context);
118 	uid = e_source_get_uid (scratch_source);
119 	config = e_source_config_backend_get_config (backend);
120 
121 	g_object_set_data_full (
122 		G_OBJECT (backend), uid, context,
123 		(GDestroyNotify) cal_config_webcal_context_free);
124 
125 	e_cal_source_config_add_offline_toggle (
126 		E_CAL_SOURCE_CONFIG (config), scratch_source);
127 
128 	widget = gtk_entry_new ();
129 	e_source_config_insert_widget (
130 		config, scratch_source, _("URL:"), widget);
131 	context->url_entry = g_object_ref (widget);
132 	gtk_widget_show (widget);
133 
134 	e_source_config_add_secure_connection_for_webdav (
135 		config, scratch_source);
136 
137 	e_source_config_add_user_entry (config, scratch_source);
138 
139 	e_source_config_add_refresh_interval (config, scratch_source);
140 
141 	extension_name = E_SOURCE_EXTENSION_WEBDAV_BACKEND;
142 	extension = e_source_get_extension (scratch_source, extension_name);
143 
144 	g_object_bind_property_full (
145 		extension, "soup-uri",
146 		context->url_entry, "text",
147 		G_BINDING_BIDIRECTIONAL |
148 		G_BINDING_SYNC_CREATE,
149 		cal_config_webcal_uri_to_text,
150 		cal_config_webcal_text_to_uri,
151 		g_object_ref (scratch_source),
152 		(GDestroyNotify) g_object_unref);
153 }
154 
155 static gboolean
156 cal_config_webcal_check_complete (ESourceConfigBackend *backend,
157                                   ESource *scratch_source)
158 {
159 	SoupURI *soup_uri;
160 	GtkEntry *entry;
161 	Context *context;
162 	const gchar *uri_string;
163 	const gchar *scheme;
164 	const gchar *uid;
165 	gboolean complete;
166 
167 	uid = e_source_get_uid (scratch_source);
168 	context = g_object_get_data (G_OBJECT (backend), uid);
169 	g_return_val_if_fail (context != NULL, FALSE);
170 
171 	entry = GTK_ENTRY (context->url_entry);
172 	uri_string = gtk_entry_get_text (entry);
173 
174 	soup_uri = soup_uri_new (uri_string);
175 
176 	/* XXX webcal:// is a non-standard scheme, but we accept it.
177 	 *     Just convert it to http:// for the URI validity test. */
178 	scheme = soup_uri_get_scheme (soup_uri);
179 	if (g_strcmp0 (scheme, "webcal") == 0)
180 		soup_uri_set_scheme (soup_uri, SOUP_URI_SCHEME_HTTP);
181 
182 	complete = SOUP_URI_VALID_FOR_HTTP (soup_uri);
183 
184 	if (soup_uri != NULL)
185 		soup_uri_free (soup_uri);
186 
187 	return complete;
188 }
189 
190 static void
191 e_cal_config_webcal_class_init (ESourceConfigBackendClass *class)
192 {
193 	EExtensionClass *extension_class;
194 
195 	extension_class = E_EXTENSION_CLASS (class);
196 	extension_class->extensible_type = E_TYPE_CAL_SOURCE_CONFIG;
197 
198 	class->parent_uid = "webcal-stub";
199 	class->backend_name = "webcal";
200 	class->insert_widgets = cal_config_webcal_insert_widgets;
201 	class->check_complete = cal_config_webcal_check_complete;
202 }
203 
204 static void
205 e_cal_config_webcal_class_finalize (ESourceConfigBackendClass *class)
206 {
207 }
208 
209 static void
210 e_cal_config_webcal_init (ESourceConfigBackend *backend)
211 {
212 }
213 
214 G_MODULE_EXPORT void
215 e_module_load (GTypeModule *type_module)
216 {
217 	e_cal_config_webcal_register_type (type_module);
218 }
219 
220 G_MODULE_EXPORT void
221 e_module_unload (GTypeModule *type_module)
222 {
223 }