-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgvfsdavmount.c
295 lines (230 loc) · 6.69 KB
/
gvfsdavmount.c
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
#include <config.h>
#include <gio/gio.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <libsoup/soup.h>
/* LibXML2 includes */
#include <libxml/parser.h>
#include <libxml/tree.h>
#define DM_NS "http://purl.org/NET/webdav/mount"
static void
show_error_dialog (const char *primary, const char *secondary)
{
GtkWidget *dlg;
dlg = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
"%s", primary);
if (secondary)
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dlg),
"%s", secondary);
gtk_dialog_run (GTK_DIALOG (dlg));
gtk_main_quit ();
}
static void
show_error_dialog_from_error (const char *msg, GError *error)
{
const char *primary = NULL;
const char *secondary = NULL;
if (msg) {
primary = msg;
secondary = error->message;
} else if (error != NULL) {
primary = error->message;
} else {
primary = _("Error mounting WebDAV");
}
show_error_dialog (primary, secondary);
g_error_free (error);
}
/* xml utils */
static inline gboolean
node_has_name_ns (xmlNodePtr node, const char *name, const char *ns_href)
{
gboolean has_name;
gboolean has_ns;
g_return_val_if_fail (node != NULL, FALSE);
has_name = has_ns = TRUE;
if (name)
has_name = node->name && ! strcmp ((char *) node->name, name);
if (ns_href)
has_ns = node->ns && node->ns->href &&
! g_ascii_strcasecmp ((char *) node->ns->href, ns_href);
return has_name && has_ns;
}
static inline gboolean
node_is_element (xmlNodePtr node)
{
return node->type == XML_ELEMENT_NODE && node->name != NULL;
}
static const char *
node_get_content (xmlNodePtr node)
{
if (node == NULL)
return NULL;
switch (node->type)
{
case XML_ELEMENT_NODE:
return node_get_content (node->children);
break;
case XML_TEXT_NODE:
return (const char *) node->content;
break;
default:
return NULL;
}
}
/* ********* */
static void
mount_done_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
GFile *file;
gboolean res;
GError *error = NULL;
file = G_FILE (object);
res = g_file_mount_enclosing_volume_finish (file,
result,
&error);
if (res == TRUE) {
/* spawn nautilus with the file */
} else {
show_error_dialog_from_error ("Error during mount", error);
}
g_object_unref (file);
gtk_main_quit ();
}
static void
mount_and_open_file (GFile *file)
{
GMountOperation *mount_op;
mount_op = g_mount_operation_new ();
g_file_mount_enclosing_volume (file, 0, mount_op, NULL,
mount_done_cb, mount_op);
}
static GFile *
parse_file (xmlDocPtr doc, GError **error)
{
GMountOperation *mount_op;
const char *mount_base;
const char *target;
xmlNodePtr root;
xmlNodePtr node;
GFile *file;
char *new_base;
char *uri;
uri = new_base = NULL;
if (doc == NULL) {
return NULL;
}
root = xmlDocGetRootElement (doc);
if (root == NULL || root->children == NULL) {
goto out;
}
if (! node_has_name_ns (root, "mount", DM_NS)) {
goto out;
}
for (node = root->children; node; node = node->next) {
if (!node_is_element (node))
continue;
if (node_has_name_ns (node, "url", DM_NS))
mount_base = g_strdup (node_get_content (node));
else if (node_has_name_ns (node, "open", DM_NS))
target = g_strdup (node_get_content (node));
}
if (mount_base == NULL || target == NULL) {
goto out;
}
/* skip the http part (XXX: make sure it really starts
* with http) */
mount_base += 4;
new_base = g_strconcat ("dav", mount_base, NULL);
uri = g_build_path ("/", new_base, target, NULL);
file = g_file_new_for_uri (uri);
out:
g_free (uri);
g_free (new_base);
xmlFreeDoc (doc);
return file;
}
static void
message_ready (SoupSession *session,
SoupMessage *msg,
gpointer user_data)
{
xmlDocPtr doc;
GFile *file;
if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
show_error_dialog (_("HTTP Error"), msg->reason_phrase);
return;
}
doc = xmlReadMemory (msg->response_body->data,
msg->response_body->length,
"response.xml",
NULL,
XML_PARSE_NOWARNING |
XML_PARSE_NOBLANKS |
XML_PARSE_NSCLEAN |
XML_PARSE_NOCDATA |
XML_PARSE_COMPACT);
file = parse_file (doc, NULL);
mount_and_open_file (file);
g_object_unref (file);
g_object_unref (session);
}
static void
fetch_file_from_web (const char *url)
{
SoupSession *session;
SoupMessage *msg;
session = soup_session_sync_new ();
msg = soup_message_new (SOUP_METHOD_GET, url);
soup_session_queue_message (session, msg,
message_ready, NULL);
}
static gboolean
parse_and_open_file (gpointer data)
{
xmlDocPtr doc;
GFile *file;
const char *url;
url = (const char *) data;
doc = xmlReadFile (data, NULL,
XML_PARSE_NOWARNING |
XML_PARSE_NOBLANKS |
XML_PARSE_NSCLEAN |
XML_PARSE_NOCDATA |
XML_PARSE_COMPACT);
file = parse_file (doc, NULL);
mount_and_open_file (file);
g_object_unref (file);
return FALSE;
}
int
main (int argc, char **argv)
{
char *url;
gboolean force_rtl = FALSE;
gboolean web = FALSE;
GError *error = NULL;
GOptionEntry options[] = {
{ "web", 'w', 0, G_OPTION_ARG_NONE, &web, "The given location is on a http server", NULL },
{ "right-to-left", 'r', 0, G_OPTION_ARG_NONE, &force_rtl, "Force right-to-left layout.", NULL },
{ NULL }
};
g_thread_init (NULL);
if (!gtk_init_with_args (&argc, &argv, "path", options, NULL, &error)) {
g_print ("Failed to parse args: %s\n", error->message);
g_error_free (error);
return 1;
}
if (force_rtl)
gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
url = argv[1];
if (web) {
fetch_file_from_web (url);
} else {
g_idle_add (parse_and_open_file, url);
}
gtk_main ();
return 0;
}