No issues found
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2 of the License, or (at your option) version 3.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with the program; if not, see <http://www.gnu.org/licenses/>
14 *
15 *
16 * Authors:
17 * David Trowbridge <trowbrds@cs.colorado.edu>
18 * Gary Ekker <gekker@novell.com>
19 *
20 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
21 *
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "publish-location.h"
29
30 #include <string.h>
31 #include <libxml/tree.h>
32 #include <libedataserverui/libedataserverui.h>
33
34 static EPublishUri *
35 migrateURI (const gchar *xml,
36 xmlDocPtr doc)
37 {
38 GSettings *settings;
39 GSList *events = NULL;
40 gchar **set_uris;
41 GPtrArray *uris_array;
42 xmlChar *location, *enabled, *frequency, *username;
43 xmlNodePtr root, p;
44 EPublishUri *uri;
45 gchar *password, *temp;
46 SoupURI *soup_uri;
47 gint ii;
48 gboolean found = FALSE;
49
50 uri = g_new0 (EPublishUri, 1);
51
52 root = doc->children;
53 location = xmlGetProp (root, (const guchar *)"location");
54 enabled = xmlGetProp (root, (const guchar *)"enabled");
55 frequency = xmlGetProp (root, (const guchar *)"frequency");
56 username = xmlGetProp (root, (const guchar *)"username");
57
58 soup_uri = soup_uri_new ((gchar *) location);
59
60 if (soup_uri == NULL) {
61 g_warning ("Could not form the uri for %s \n", location);
62 goto cleanup;
63 }
64
65 soup_uri_set_user (soup_uri, (gchar *) username);
66
67 temp = soup_uri_to_string (soup_uri, FALSE);
68 uri->location = g_strdup_printf ("dav://%s", strstr (temp, "//") + 2);
69 g_free (temp);
70
71 soup_uri_free (soup_uri);
72
73 if (enabled != NULL)
74 uri->enabled = atoi ((gchar *) enabled);
75 if (frequency != NULL)
76 uri->publish_frequency = atoi ((gchar *) frequency);
77 uri->publish_format = URI_PUBLISH_AS_FB;
78
79 password = e_passwords_get_password (NULL, (gchar *) location);
80 if (password) {
81 e_passwords_forget_password (NULL, (gchar *) location);
82 e_passwords_add_password (uri->location, password);
83 e_passwords_remember_password (NULL, uri->location);
84 }
85
86 for (p = root->children; p != NULL; p = p->next) {
87 xmlChar *uid = xmlGetProp (p, (const guchar *)"uid");
88 if (strcmp ((gchar *) p->name, "source") == 0) {
89 events = g_slist_append (events, uid);
90 } else {
91 g_free (uid);
92 }
93 }
94 uri->events = events;
95
96 uris_array = g_ptr_array_new_full (3, g_free);
97
98 settings = g_settings_new (PC_SETTINGS_ID);
99 set_uris = g_settings_get_strv (settings, PC_SETTINGS_URIS);
100
101 for (ii = 0; set_uris && set_uris[ii]; ii++) {
102 const gchar *str = set_uris[ii];
103 if (!found && g_str_equal (xml, str)) {
104 found = TRUE;
105 g_ptr_array_add (uris_array, e_publish_uri_to_xml (uri));
106 } else {
107 g_ptr_array_add (uris_array, g_strdup (str));
108 }
109 }
110
111 g_strfreev (set_uris);
112
113 /* this should not happen, right? */
114 if (!found)
115 g_ptr_array_add (uris_array, e_publish_uri_to_xml (uri));
116 g_ptr_array_add (uris_array, NULL);
117
118 g_settings_set_strv (settings, PC_SETTINGS_URIS, (const gchar * const *) uris_array->pdata);
119
120 g_ptr_array_free (uris_array, TRUE);
121 g_object_unref (settings);
122
123 cleanup:
124 xmlFree (location);
125 xmlFree (enabled);
126 xmlFree (frequency);
127 xmlFree (username);
128 xmlFreeDoc (doc);
129
130 return uri;
131 }
132
133 EPublishUri *
134 e_publish_uri_from_xml (const gchar *xml)
135 {
136 xmlDocPtr doc;
137 xmlNodePtr root, p;
138 xmlChar *location, *enabled, *frequency, *fb_duration_value, *fb_duration_type;
139 xmlChar *publish_time, *format, *username = NULL;
140 GSList *events = NULL;
141 EPublishUri *uri;
142
143 doc = xmlParseDoc ((const guchar *) xml);
144 if (doc == NULL)
145 return NULL;
146
147 root = doc->children;
148 if (strcmp ((gchar *) root->name, "uri") != 0)
149 return NULL;
150
151 if ((username = xmlGetProp (root, (const guchar *)"username"))) {
152 xmlFree (username);
153 return migrateURI (xml, doc);
154
155 }
156
157 uri = g_new0 (EPublishUri, 1);
158
159 location = xmlGetProp (root, (const guchar *)"location");
160 enabled = xmlGetProp (root, (const guchar *)"enabled");
161 frequency = xmlGetProp (root, (const guchar *)"frequency");
162 format = xmlGetProp (root, (const guchar *)"format");
163 publish_time = xmlGetProp (root, (const guchar *)"publish_time");
164 fb_duration_value = xmlGetProp (root, (xmlChar *)"fb_duration_value");
165 fb_duration_type = xmlGetProp (root, (xmlChar *)"fb_duration_type");
166
167 if (location != NULL)
168 uri->location = (gchar *) location;
169 if (enabled != NULL)
170 uri->enabled = atoi ((gchar *) enabled);
171 if (frequency != NULL)
172 uri->publish_frequency = atoi ((gchar *) frequency);
173 if (format != NULL)
174 uri->publish_format = atoi ((gchar *) format);
175 if (publish_time != NULL)
176 uri->last_pub_time = (gchar *) publish_time;
177
178 if (fb_duration_value)
179 uri->fb_duration_value = atoi ((gchar *) fb_duration_value);
180 else
181 uri->fb_duration_value = -1;
182
183 if (uri->fb_duration_value < 1)
184 uri->fb_duration_value = 6;
185 else if (uri->fb_duration_value > 100)
186 uri->fb_duration_value = 100;
187
188 if (fb_duration_type && g_str_equal ((gchar *) fb_duration_type, "days"))
189 uri->fb_duration_type = FB_DURATION_DAYS;
190 else if (fb_duration_type && g_str_equal ((gchar *) fb_duration_type, "months"))
191 uri->fb_duration_type = FB_DURATION_MONTHS;
192 else
193 uri->fb_duration_type = FB_DURATION_WEEKS;
194
195 uri->password = g_strdup ("");
196
197 for (p = root->children; p != NULL; p = p->next) {
198 xmlChar *uid = xmlGetProp (p, (const guchar *)"uid");
199 if (strcmp ((gchar *) p->name, "event") == 0) {
200 events = g_slist_append (events, uid);
201 } else {
202 g_free (uid);
203 }
204 }
205 uri->events = events;
206
207 xmlFree (enabled);
208 xmlFree (frequency);
209 xmlFree (format);
210 xmlFree (fb_duration_value);
211 xmlFree (fb_duration_type);
212 xmlFreeDoc (doc);
213
214 return uri;
215 }
216
217 gchar *
218 e_publish_uri_to_xml (EPublishUri *uri)
219 {
220 xmlDocPtr doc;
221 xmlNodePtr root;
222 gchar *enabled, *frequency, *format;
223 GSList *calendars = NULL;
224 xmlChar *xml_buffer;
225 gchar *returned_buffer;
226 gint xml_buffer_size;
227
228 g_return_val_if_fail (uri != NULL, NULL);
229 g_return_val_if_fail (uri->location != NULL, NULL);
230
231 doc = xmlNewDoc ((const guchar *)"1.0");
232
233 root = xmlNewDocNode (doc, NULL, (const guchar *)"uri", NULL);
234 enabled = g_strdup_printf ("%d", uri->enabled);
235 frequency = g_strdup_printf ("%d", uri->publish_frequency);
236 format = g_strdup_printf ("%d", uri->publish_format);
237 xmlSetProp (root, (const guchar *)"location", (guchar *) uri->location);
238 xmlSetProp (root, (const guchar *)"enabled", (guchar *) enabled);
239 xmlSetProp (root, (const guchar *)"frequency", (guchar *) frequency);
240 xmlSetProp (root, (const guchar *)"format", (guchar *) format);
241 xmlSetProp (root, (const guchar *)"publish_time", (guchar *) uri->last_pub_time);
242
243 g_free (format);
244 format = g_strdup_printf ("%d", uri->fb_duration_value);
245 xmlSetProp (root, (xmlChar *)"fb_duration_value", (xmlChar *) format);
246
247 if (uri->fb_duration_type == FB_DURATION_DAYS)
248 xmlSetProp (root, (xmlChar *)"fb_duration_type", (xmlChar *)"days");
249 else if (uri->fb_duration_type == FB_DURATION_MONTHS)
250 xmlSetProp (root, (xmlChar *)"fb_duration_type", (xmlChar *)"months");
251 else
252 xmlSetProp (root, (xmlChar *)"fb_duration_type", (xmlChar *)"weeks");
253
254 for (calendars = uri->events; calendars != NULL;
255 calendars = g_slist_next (calendars)) {
256 xmlNodePtr node;
257 node = xmlNewChild (root, NULL, (const guchar *)"event", NULL);
258 xmlSetProp (node, (const guchar *)"uid", calendars->data);
259 }
260 xmlDocSetRootElement (doc, root);
261
262 xmlDocDumpMemory (doc, &xml_buffer, &xml_buffer_size);
263 xmlFreeDoc (doc);
264
265 returned_buffer = g_malloc (xml_buffer_size + 1);
266 memcpy (returned_buffer, xml_buffer, xml_buffer_size);
267 returned_buffer[xml_buffer_size] = '\0';
268 xmlFree (xml_buffer);
269 g_free (enabled);
270 g_free (frequency);
271 g_free (format);
272
273 return returned_buffer;
274 }