Line 0
Link Here
|
|
|
1 |
/* |
2 |
arch-tag: Implementation of Rhythmbox playlist parser |
3 |
|
4 |
Copyright (C) 2002, 2003, 2004 Bastien Nocera |
5 |
Copyright (C) 2003,2004 Colin Walters <walters@rhythmbox.org> |
6 |
|
7 |
The Gnome Library is free software; you can redistribute it and/or |
8 |
modify it under the terms of the GNU Library General Public License as |
9 |
published by the Free Software Foundation; either version 2 of the |
10 |
License, or (at your option) any later version. |
11 |
|
12 |
The Gnome Library is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
Library General Public License for more details. |
16 |
|
17 |
You should have received a copy of the GNU Library General Public |
18 |
License along with the Gnome Library; see the file COPYING.LIB. If not, |
19 |
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
20 |
Boston, MA 02111-1307, USA. |
21 |
|
22 |
Author: Bastien Nocera <hadess@hadess.net> |
23 |
*/ |
24 |
|
25 |
#ifdef HAVE_CONFIG_H |
26 |
#include "config.h" |
27 |
#endif /* HAVE_CONFIG_H */ |
28 |
|
29 |
#include "totem-pl-parser.h" |
30 |
|
31 |
#include "totemplparser-marshal.h" |
32 |
#include "totem-disc.h" |
33 |
|
34 |
#include <glib.h> |
35 |
#include <glib/gi18n.h> |
36 |
#include <gtk/gtk.h> |
37 |
#include <libxml/tree.h> |
38 |
#include <libxml/parser.h> |
39 |
#include <libgnomevfs/gnome-vfs.h> |
40 |
#include <libgnomevfs/gnome-vfs-mime.h> |
41 |
#include <libgnomevfs/gnome-vfs-mime-utils.h> |
42 |
#include <libgnomevfs/gnome-vfs-utils.h> |
43 |
#include <string.h> |
44 |
|
45 |
#define READ_CHUNK_SIZE 8192 |
46 |
#define MIME_READ_CHUNK_SIZE 1024 |
47 |
#define RECURSE_LEVEL_MAX 4 |
48 |
|
49 |
typedef TotemPlParserResult (*PlaylistCallback) (TotemPlParser *parser, const char *url, gpointer data); |
50 |
static gboolean totem_pl_parser_scheme_is_ignored (TotemPlParser *parser, const char *url); |
51 |
static gboolean totem_pl_parser_ignore (TotemPlParser *parser, const char *url); |
52 |
static TotemPlParserResult totem_pl_parser_parse_internal (TotemPlParser *parser, const char *url); |
53 |
static TotemPlParserResult totem_pl_parser_add_asx (TotemPlParser *parser, const char *url, gpointer data); |
54 |
|
55 |
typedef struct { |
56 |
char *mimetype; |
57 |
PlaylistCallback func; |
58 |
} PlaylistTypes; |
59 |
|
60 |
struct TotemPlParserPrivate |
61 |
{ |
62 |
GList *ignore_schemes; |
63 |
GList *ignore_mimetypes; |
64 |
guint recurse_level; |
65 |
gboolean fallback; |
66 |
}; |
67 |
|
68 |
/* Signals */ |
69 |
enum { |
70 |
ENTRY, |
71 |
PLAYLIST_START, |
72 |
PLAYLIST_END, |
73 |
LAST_SIGNAL |
74 |
}; |
75 |
|
76 |
static int totem_pl_parser_table_signals[LAST_SIGNAL] = { 0 }; |
77 |
|
78 |
static GObjectClass *parent_class = NULL; |
79 |
|
80 |
static void totem_pl_parser_class_init (TotemPlParserClass *class); |
81 |
static void totem_pl_parser_init (TotemPlParser *parser); |
82 |
static void totem_pl_parser_finalize (GObject *object); |
83 |
|
84 |
G_DEFINE_TYPE(TotemPlParser, totem_pl_parser, G_TYPE_OBJECT) |
85 |
|
86 |
static void |
87 |
totem_pl_parser_class_init (TotemPlParserClass *klass) |
88 |
{ |
89 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
90 |
|
91 |
parent_class = g_type_class_peek_parent (klass); |
92 |
|
93 |
object_class->finalize = totem_pl_parser_finalize; |
94 |
|
95 |
/* Signals */ |
96 |
totem_pl_parser_table_signals[ENTRY] = |
97 |
g_signal_new ("entry", |
98 |
G_TYPE_FROM_CLASS (klass), |
99 |
G_SIGNAL_RUN_LAST, |
100 |
G_STRUCT_OFFSET (TotemPlParserClass, entry), |
101 |
NULL, NULL, |
102 |
totemplparser_marshal_VOID__STRING_STRING_STRING, |
103 |
G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING); |
104 |
totem_pl_parser_table_signals[PLAYLIST_START] = |
105 |
g_signal_new ("playlist-start", |
106 |
G_TYPE_FROM_CLASS (klass), |
107 |
G_SIGNAL_RUN_LAST, |
108 |
G_STRUCT_OFFSET (TotemPlParserClass, playlist_start), |
109 |
NULL, NULL, |
110 |
g_cclosure_marshal_VOID__STRING, |
111 |
G_TYPE_NONE, 1, G_TYPE_STRING); |
112 |
totem_pl_parser_table_signals[PLAYLIST_START] = |
113 |
g_signal_new ("playlist-end", |
114 |
G_TYPE_FROM_CLASS (klass), |
115 |
G_SIGNAL_RUN_LAST, |
116 |
G_STRUCT_OFFSET (TotemPlParserClass, playlist_end), |
117 |
NULL, NULL, |
118 |
g_cclosure_marshal_VOID__STRING, |
119 |
G_TYPE_NONE, 1, G_TYPE_STRING); |
120 |
} |
121 |
|
122 |
GQuark |
123 |
totem_pl_parser_error_quark (void) |
124 |
{ |
125 |
static GQuark quark; |
126 |
if (!quark) |
127 |
quark = g_quark_from_static_string ("totem_pl_parser_error"); |
128 |
|
129 |
return quark; |
130 |
} |
131 |
|
132 |
TotemPlParser * |
133 |
totem_pl_parser_new (void) |
134 |
{ |
135 |
return TOTEM_PL_PARSER (g_object_new (TOTEM_TYPE_PL_PARSER, NULL)); |
136 |
} |
137 |
|
138 |
static const char * |
139 |
my_gnome_vfs_get_mime_type_with_data (const char *uri, gpointer *data) |
140 |
{ |
141 |
GnomeVFSResult result; |
142 |
GnomeVFSHandle *handle; |
143 |
char *buffer; |
144 |
const char *mimetype; |
145 |
GnomeVFSFileSize total_bytes_read; |
146 |
GnomeVFSFileSize bytes_read; |
147 |
|
148 |
*data = NULL; |
149 |
|
150 |
/* Open the file. */ |
151 |
result = gnome_vfs_open (&handle, uri, GNOME_VFS_OPEN_READ); |
152 |
if (result != GNOME_VFS_OK) |
153 |
return NULL; |
154 |
|
155 |
/* Read the whole thing, up to MIME_READ_CHUNK_SIZE */ |
156 |
buffer = NULL; |
157 |
total_bytes_read = 0; |
158 |
do { |
159 |
buffer = g_realloc (buffer, total_bytes_read |
160 |
+ MIME_READ_CHUNK_SIZE); |
161 |
result = gnome_vfs_read (handle, |
162 |
buffer + total_bytes_read, |
163 |
MIME_READ_CHUNK_SIZE, |
164 |
&bytes_read); |
165 |
if (result != GNOME_VFS_OK && result != GNOME_VFS_ERROR_EOF) { |
166 |
g_free (buffer); |
167 |
gnome_vfs_close (handle); |
168 |
return NULL; |
169 |
} |
170 |
|
171 |
/* Check for overflow. */ |
172 |
if (total_bytes_read + bytes_read < total_bytes_read) { |
173 |
g_free (buffer); |
174 |
gnome_vfs_close (handle); |
175 |
return NULL; |
176 |
} |
177 |
|
178 |
total_bytes_read += bytes_read; |
179 |
} while (result == GNOME_VFS_OK |
180 |
&& total_bytes_read < MIME_READ_CHUNK_SIZE); |
181 |
|
182 |
/* Close the file. */ |
183 |
result = gnome_vfs_close (handle); |
184 |
if (result != GNOME_VFS_OK) { |
185 |
g_free (buffer); |
186 |
return NULL; |
187 |
} |
188 |
|
189 |
/* Return the file null-terminated. */ |
190 |
buffer = g_realloc (buffer, total_bytes_read + 1); |
191 |
buffer[total_bytes_read] = '\0'; |
192 |
*data = buffer; |
193 |
|
194 |
mimetype = gnome_vfs_get_mime_type_for_data (*data, total_bytes_read); |
195 |
|
196 |
return mimetype; |
197 |
} |
198 |
|
199 |
static char* |
200 |
totem_pl_parser_base_url (const char *url) |
201 |
{ |
202 |
/* Yay, let's reconstruct the base by hand */ |
203 |
GnomeVFSURI *uri, *parent; |
204 |
char *base; |
205 |
|
206 |
uri = gnome_vfs_uri_new (url); |
207 |
if (uri == NULL) |
208 |
return NULL; |
209 |
|
210 |
parent = gnome_vfs_uri_get_parent (uri); |
211 |
if (!parent) { |
212 |
parent = uri; |
213 |
} |
214 |
base = gnome_vfs_uri_to_string (parent, 0); |
215 |
|
216 |
gnome_vfs_uri_unref (uri); |
217 |
if (parent != uri) { |
218 |
gnome_vfs_uri_unref (parent); |
219 |
} |
220 |
|
221 |
return base; |
222 |
} |
223 |
|
224 |
static gboolean |
225 |
write_string (GnomeVFSHandle *handle, const char *buf, GError **error) |
226 |
{ |
227 |
GnomeVFSResult res; |
228 |
GnomeVFSFileSize written; |
229 |
guint len; |
230 |
|
231 |
len = strlen (buf); |
232 |
res = gnome_vfs_write (handle, buf, len, &written); |
233 |
if (res != GNOME_VFS_OK || written < len) { |
234 |
g_set_error (error, |
235 |
TOTEM_PL_PARSER_ERROR, |
236 |
TOTEM_PL_PARSER_ERROR_VFS_WRITE, |
237 |
_("Couldn't write parser: %s"), |
238 |
gnome_vfs_result_to_string (res)); |
239 |
gnome_vfs_close (handle); |
240 |
return FALSE; |
241 |
} |
242 |
|
243 |
return TRUE; |
244 |
} |
245 |
|
246 |
static int |
247 |
totem_pl_parser_num_entries (TotemPlParser *parser, GtkTreeModel *model, |
248 |
TotemPlParserIterFunc func, gpointer user_data) |
249 |
{ |
250 |
int num_entries, i, ignored; |
251 |
|
252 |
num_entries = gtk_tree_model_iter_n_children (model, NULL); |
253 |
ignored = 0; |
254 |
|
255 |
for (i = 1; i <= num_entries; i++) |
256 |
{ |
257 |
GtkTreeIter iter; |
258 |
char *path, *url, *title; |
259 |
|
260 |
path = g_strdup_printf ("%d", i - 1); |
261 |
gtk_tree_model_get_iter_from_string (model, &iter, path); |
262 |
g_free (path); |
263 |
|
264 |
func (model, &iter, &url, &title, user_data); |
265 |
if (totem_pl_parser_scheme_is_ignored (parser, url) != FALSE) |
266 |
ignored++; |
267 |
|
268 |
g_free (url); |
269 |
g_free (title); |
270 |
} |
271 |
|
272 |
return num_entries - ignored; |
273 |
} |
274 |
|
275 |
static char * |
276 |
totem_pl_parser_relative (const char *url, const char *output) |
277 |
{ |
278 |
char *url_base, *output_base; |
279 |
char *base, *needle; |
280 |
|
281 |
base = NULL; |
282 |
url_base = totem_pl_parser_base_url (url); |
283 |
if (url_base == NULL) |
284 |
return NULL; |
285 |
|
286 |
output_base = totem_pl_parser_base_url (output); |
287 |
|
288 |
needle = strstr (url_base, output_base); |
289 |
if (needle != NULL) |
290 |
{ |
291 |
GnomeVFSURI *uri; |
292 |
char *newurl; |
293 |
|
294 |
uri = gnome_vfs_uri_new (url); |
295 |
newurl = gnome_vfs_uri_to_string (uri, 0); |
296 |
if (newurl[strlen (output_base)] == '/') { |
297 |
base = g_strdup (newurl + strlen (output_base) + 1); |
298 |
} else { |
299 |
base = g_strdup (newurl + strlen (output_base)); |
300 |
} |
301 |
gnome_vfs_uri_unref (uri); |
302 |
g_free (newurl); |
303 |
|
304 |
/* And finally unescape the string */ |
305 |
newurl = gnome_vfs_unescape_string (base, NULL); |
306 |
g_free (base); |
307 |
base = newurl; |
308 |
} |
309 |
|
310 |
g_free (url_base); |
311 |
g_free (output_base); |
312 |
|
313 |
return base; |
314 |
} |
315 |
|
316 |
static gboolean |
317 |
totem_pl_parser_write_pls (TotemPlParser *parser, GtkTreeModel *model, |
318 |
TotemPlParserIterFunc func, |
319 |
const char *output, const char *title, |
320 |
gpointer user_data, GError **error) |
321 |
{ |
322 |
GnomeVFSHandle *handle; |
323 |
GnomeVFSResult res; |
324 |
int num_entries_total, num_entries, i; |
325 |
char *buf; |
326 |
gboolean success; |
327 |
|
328 |
num_entries = totem_pl_parser_num_entries (parser, model, func, user_data); |
329 |
num_entries_total = gtk_tree_model_iter_n_children (model, NULL); |
330 |
|
331 |
res = gnome_vfs_open (&handle, output, GNOME_VFS_OPEN_WRITE); |
332 |
if (res == GNOME_VFS_ERROR_NOT_FOUND) { |
333 |
res = gnome_vfs_create (&handle, output, |
334 |
GNOME_VFS_OPEN_WRITE, FALSE, |
335 |
GNOME_VFS_PERM_USER_WRITE |
336 |
| GNOME_VFS_PERM_USER_READ |
337 |
| GNOME_VFS_PERM_GROUP_READ); |
338 |
} |
339 |
|
340 |
if (res != GNOME_VFS_OK) { |
341 |
g_set_error(error, |
342 |
TOTEM_PL_PARSER_ERROR, |
343 |
TOTEM_PL_PARSER_ERROR_VFS_OPEN, |
344 |
_("Couldn't open file '%s': %s"), |
345 |
output, gnome_vfs_result_to_string (res)); |
346 |
return FALSE; |
347 |
} |
348 |
|
349 |
buf = g_strdup ("[playlist]\n"); |
350 |
success = write_string (handle, buf, error); |
351 |
g_free (buf); |
352 |
if (success == FALSE) |
353 |
return FALSE; |
354 |
|
355 |
if (title != NULL) { |
356 |
buf = g_strdup_printf ("X-GNOME-Title=%s\n", title); |
357 |
success = write_string (handle, buf, error); |
358 |
g_free (buf); |
359 |
if (success == FALSE) |
360 |
{ |
361 |
gnome_vfs_close (handle); |
362 |
return FALSE; |
363 |
} |
364 |
} |
365 |
|
366 |
buf = g_strdup_printf ("NumberOfEntries=%d\n", num_entries); |
367 |
success = write_string (handle, buf, error); |
368 |
g_free (buf); |
369 |
if (success == FALSE) |
370 |
{ |
371 |
gnome_vfs_close (handle); |
372 |
return FALSE; |
373 |
} |
374 |
|
375 |
for (i = 1; i <= num_entries_total; i++) { |
376 |
GtkTreeIter iter; |
377 |
char *path, *url, *title, *relative; |
378 |
|
379 |
path = g_strdup_printf ("%d", i - 1); |
380 |
gtk_tree_model_get_iter_from_string (model, &iter, path); |
381 |
g_free (path); |
382 |
|
383 |
func (model, &iter, &url, &title, user_data); |
384 |
|
385 |
if (totem_pl_parser_scheme_is_ignored (parser, url) != FALSE) |
386 |
{ |
387 |
g_free (url); |
388 |
g_free (title); |
389 |
continue; |
390 |
} |
391 |
|
392 |
relative = totem_pl_parser_relative (url, output); |
393 |
buf = g_strdup_printf ("File%d=%s\n", i, |
394 |
relative ? relative : url); |
395 |
g_free (relative); |
396 |
g_free (url); |
397 |
success = write_string (handle, buf, error); |
398 |
g_free (buf); |
399 |
if (success == FALSE) |
400 |
{ |
401 |
gnome_vfs_close (handle); |
402 |
g_free (title); |
403 |
return FALSE; |
404 |
} |
405 |
|
406 |
buf = g_strdup_printf ("Title%d=%s\n", i, title); |
407 |
success = write_string (handle, buf, error); |
408 |
g_free (buf); |
409 |
g_free (title); |
410 |
if (success == FALSE) |
411 |
{ |
412 |
gnome_vfs_close (handle); |
413 |
return FALSE; |
414 |
} |
415 |
} |
416 |
|
417 |
gnome_vfs_close (handle); |
418 |
return TRUE; |
419 |
} |
420 |
|
421 |
static char * |
422 |
totem_pl_parser_url_to_dos (const char *url, const char *output) |
423 |
{ |
424 |
char *retval, *i; |
425 |
|
426 |
retval = totem_pl_parser_relative (url, output); |
427 |
|
428 |
if (retval == NULL) |
429 |
retval = g_strdup (url); |
430 |
|
431 |
/* Don't change URIs, but change smb:// */ |
432 |
if (g_str_has_prefix (retval, "smb://") != FALSE) |
433 |
{ |
434 |
char *tmp; |
435 |
tmp = g_strdup (retval + strlen ("smb:")); |
436 |
g_free (retval); |
437 |
retval = tmp; |
438 |
} |
439 |
|
440 |
if (strstr (retval, "://") != NULL) |
441 |
return retval; |
442 |
|
443 |
i = retval; |
444 |
while (*i != '\0') |
445 |
{ |
446 |
if (*i == '/') |
447 |
*i = '\\'; |
448 |
i++; |
449 |
} |
450 |
|
451 |
return retval; |
452 |
} |
453 |
|
454 |
static gboolean |
455 |
totem_pl_parser_write_m3u (TotemPlParser *parser, GtkTreeModel *model, |
456 |
TotemPlParserIterFunc func, const char *output, |
457 |
gboolean dos_compatible, gpointer user_data, GError **error) |
458 |
{ |
459 |
GnomeVFSHandle *handle; |
460 |
GnomeVFSResult res; |
461 |
int num_entries_total, i; |
462 |
gboolean success; |
463 |
char *buf; |
464 |
|
465 |
res = gnome_vfs_open (&handle, output, GNOME_VFS_OPEN_WRITE); |
466 |
if (res == GNOME_VFS_ERROR_NOT_FOUND) { |
467 |
res = gnome_vfs_create (&handle, output, |
468 |
GNOME_VFS_OPEN_WRITE, FALSE, |
469 |
GNOME_VFS_PERM_USER_WRITE |
470 |
| GNOME_VFS_PERM_USER_READ |
471 |
| GNOME_VFS_PERM_GROUP_READ); |
472 |
} |
473 |
|
474 |
if (res != GNOME_VFS_OK) { |
475 |
g_set_error(error, |
476 |
TOTEM_PL_PARSER_ERROR, |
477 |
TOTEM_PL_PARSER_ERROR_VFS_OPEN, |
478 |
_("Couldn't open file '%s': %s"), |
479 |
output, gnome_vfs_result_to_string (res)); |
480 |
return FALSE; |
481 |
} |
482 |
|
483 |
num_entries_total = gtk_tree_model_iter_n_children (model, NULL); |
484 |
|
485 |
for (i = 1; i <= num_entries_total; i++) { |
486 |
GtkTreeIter iter; |
487 |
char *path, *url, *title; |
488 |
|
489 |
path = g_strdup_printf ("%d", i - 1); |
490 |
gtk_tree_model_get_iter_from_string (model, &iter, path); |
491 |
g_free (path); |
492 |
|
493 |
func (model, &iter, &url, &title, user_data); |
494 |
|
495 |
if (totem_pl_parser_scheme_is_ignored (parser, url) != FALSE) |
496 |
{ |
497 |
g_free (url); |
498 |
g_free (title); |
499 |
continue; |
500 |
} |
501 |
|
502 |
if (dos_compatible != FALSE) |
503 |
{ |
504 |
char *dos; |
505 |
|
506 |
dos = totem_pl_parser_url_to_dos (url, output); |
507 |
buf = g_strdup_printf ("%s\r\n", dos); |
508 |
g_free (dos); |
509 |
} else { |
510 |
char *relative; |
511 |
|
512 |
relative = totem_pl_parser_relative (url, output); |
513 |
buf = g_strdup_printf ("%s\n", relative); |
514 |
g_free (relative); |
515 |
} |
516 |
|
517 |
success = write_string (handle, url, error); |
518 |
|
519 |
if (success == FALSE) |
520 |
{ |
521 |
gnome_vfs_close (handle); |
522 |
return FALSE; |
523 |
} |
524 |
} |
525 |
|
526 |
return TRUE; |
527 |
} |
528 |
|
529 |
gboolean |
530 |
totem_pl_parser_write_with_title (TotemPlParser *parser, GtkTreeModel *model, |
531 |
TotemPlParserIterFunc func, |
532 |
const char *output, const char *title, |
533 |
TotemPlParserType type, |
534 |
gpointer user_data, GError **error) |
535 |
{ |
536 |
switch (type) |
537 |
{ |
538 |
case TOTEM_PL_PARSER_PLS: |
539 |
return totem_pl_parser_write_pls (parser, model, func, |
540 |
output, title, user_data, error); |
541 |
case TOTEM_PL_PARSER_M3U: |
542 |
case TOTEM_PL_PARSER_M3U_DOS: |
543 |
return totem_pl_parser_write_m3u (parser, model, func, |
544 |
output, (type == TOTEM_PL_PARSER_M3U_DOS), |
545 |
user_data, error); |
546 |
default: |
547 |
g_assert_not_reached (); |
548 |
} |
549 |
|
550 |
return FALSE; |
551 |
} |
552 |
|
553 |
gboolean |
554 |
totem_pl_parser_write (TotemPlParser *parser, GtkTreeModel *model, |
555 |
TotemPlParserIterFunc func, |
556 |
const char *output, TotemPlParserType type, |
557 |
gpointer user_data, |
558 |
GError **error) |
559 |
{ |
560 |
return totem_pl_parser_write_with_title (parser, model, func, output, |
561 |
NULL, type, user_data, error); |
562 |
} |
563 |
|
564 |
static int |
565 |
read_ini_line_int (char **lines, const char *key) |
566 |
{ |
567 |
int retval = -1; |
568 |
int i; |
569 |
|
570 |
if (lines == NULL || key == NULL) |
571 |
return -1; |
572 |
|
573 |
for (i = 0; (lines[i] != NULL && retval == -1); i++) { |
574 |
if (g_ascii_strncasecmp (lines[i], key, strlen (key)) == 0) { |
575 |
char **bits; |
576 |
|
577 |
bits = g_strsplit (lines[i], "=", 2); |
578 |
if (bits[0] == NULL || bits [1] == NULL) { |
579 |
g_strfreev (bits); |
580 |
return -1; |
581 |
} |
582 |
|
583 |
retval = (gint) g_strtod (bits[1], NULL); |
584 |
g_strfreev (bits); |
585 |
} |
586 |
} |
587 |
|
588 |
return retval; |
589 |
} |
590 |
|
591 |
static char* |
592 |
read_ini_line_string (char **lines, const char *key, gboolean dos_mode) |
593 |
{ |
594 |
char *retval = NULL; |
595 |
int i; |
596 |
|
597 |
if (lines == NULL || key == NULL) |
598 |
return NULL; |
599 |
|
600 |
for (i = 0; (lines[i] != NULL && retval == NULL); i++) { |
601 |
if (g_ascii_strncasecmp (lines[i], key, strlen (key)) == 0) { |
602 |
char **bits; |
603 |
ssize_t len; |
604 |
|
605 |
bits = g_strsplit (lines[i], "=", 2); |
606 |
if (bits[0] == NULL || bits [1] == NULL) { |
607 |
g_strfreev (bits); |
608 |
return NULL; |
609 |
} |
610 |
|
611 |
retval = g_strdup (bits[1]); |
612 |
len = strlen (retval); |
613 |
if (dos_mode && len >= 2 && retval[len-2] == '\r') { |
614 |
retval[len-2] = '\n'; |
615 |
retval[len-1] = '\0'; |
616 |
} |
617 |
|
618 |
g_strfreev (bits); |
619 |
} |
620 |
} |
621 |
|
622 |
return retval; |
623 |
} |
624 |
|
625 |
static void |
626 |
totem_pl_parser_init (TotemPlParser *parser) |
627 |
{ |
628 |
parser->priv = g_new0 (TotemPlParserPrivate, 1); |
629 |
} |
630 |
|
631 |
static void |
632 |
totem_pl_parser_finalize (GObject *object) |
633 |
{ |
634 |
TotemPlParser *parser = TOTEM_PL_PARSER (object); |
635 |
|
636 |
g_return_if_fail (object != NULL); |
637 |
g_return_if_fail (parser->priv != NULL); |
638 |
|
639 |
g_list_foreach (parser->priv->ignore_schemes, (GFunc) g_free, NULL); |
640 |
g_list_free (parser->priv->ignore_schemes); |
641 |
|
642 |
g_list_foreach (parser->priv->ignore_mimetypes, (GFunc) g_free, NULL); |
643 |
g_list_free (parser->priv->ignore_mimetypes); |
644 |
|
645 |
g_free (parser->priv); |
646 |
parser->priv = NULL; |
647 |
|
648 |
if (G_OBJECT_CLASS (parent_class)->finalize != NULL) { |
649 |
(* G_OBJECT_CLASS (parent_class)->finalize) (object); |
650 |
} |
651 |
} |
652 |
|
653 |
static gboolean |
654 |
totem_pl_parser_check_utf8 (const char *title) |
655 |
{ |
656 |
return title ? g_utf8_validate (title, -1, NULL) : FALSE; |
657 |
} |
658 |
|
659 |
static void |
660 |
totem_pl_parser_add_one_url (TotemPlParser *parser, const char *url, const char *title) |
661 |
{ |
662 |
g_signal_emit (G_OBJECT (parser), totem_pl_parser_table_signals[ENTRY], |
663 |
0, url, |
664 |
totem_pl_parser_check_utf8 (title) ? title : NULL, |
665 |
NULL); |
666 |
} |
667 |
|
668 |
static void |
669 |
totem_pl_parser_add_one_url_ext (TotemPlParser *parser, const char *url, |
670 |
const char *title, const char *genre) |
671 |
{ |
672 |
g_signal_emit (G_OBJECT (parser), totem_pl_parser_table_signals[ENTRY], |
673 |
0, url, |
674 |
totem_pl_parser_check_utf8 (title) ? title : NULL, |
675 |
genre); |
676 |
} |
677 |
|
678 |
static TotemPlParserResult |
679 |
totem_pl_parser_add_ram (TotemPlParser *parser, const char *url, gpointer data) |
680 |
{ |
681 |
gboolean retval = TOTEM_PL_PARSER_RESULT_UNHANDLED; |
682 |
char *contents, **lines; |
683 |
int size, i; |
684 |
const char *split_char; |
685 |
|
686 |
if (gnome_vfs_read_entire_file (url, &size, &contents) != GNOME_VFS_OK) |
687 |
return TOTEM_PL_PARSER_RESULT_ERROR; |
688 |
|
689 |
/* figure out whether we're a unix or dos RAM file */ |
690 |
if (strstr(contents,"\x0d") == NULL) |
691 |
split_char = "\n"; |
692 |
else |
693 |
split_char = "\x0d\n"; |
694 |
|
695 |
lines = g_strsplit (contents, split_char, 0); |
696 |
g_free (contents); |
697 |
|
698 |
for (i = 0; lines[i] != NULL; i++) { |
699 |
if (strcmp (lines[i], "") == 0) |
700 |
continue; |
701 |
|
702 |
retval = TOTEM_PL_PARSER_RESULT_SUCCESS; |
703 |
|
704 |
/* Either it's a URI, or it has a proper path ... */ |
705 |
if (strstr(lines[i], "://") != NULL |
706 |
|| lines[i][0] == G_DIR_SEPARATOR) { |
707 |
/* .ram files can contain .smil entries */ |
708 |
if (totem_pl_parser_parse_internal (parser, lines[i]) != TOTEM_PL_PARSER_RESULT_SUCCESS) |
709 |
{ |
710 |
totem_pl_parser_add_one_url (parser, |
711 |
lines[i], NULL); |
712 |
} |
713 |
} else if (strcmp (lines[i], "--stop--") == 0) { |
714 |
/* For Real Media playlists, handle the stop command */ |
715 |
break; |
716 |
} else { |
717 |
char *fullpath, *base; |
718 |
|
719 |
/* Try with a base */ |
720 |
base = totem_pl_parser_base_url (url); |
721 |
|
722 |
fullpath = g_strdup_printf ("%s/%s", base, lines[i]); |
723 |
if (totem_pl_parser_parse_internal (parser, fullpath) != TOTEM_PL_PARSER_RESULT_SUCCESS) |
724 |
{ |
725 |
totem_pl_parser_add_one_url (parser, fullpath, NULL); |
726 |
} |
727 |
g_free (fullpath); |
728 |
g_free (base); |
729 |
} |
730 |
} |
731 |
|
732 |
g_strfreev (lines); |
733 |
|
734 |
return retval; |
735 |
} |
736 |
|
737 |
static const char * |
738 |
totem_pl_parser_get_extinfo_title (gboolean extinfo, char **lines, int i) |
739 |
{ |
740 |
const char *retval; |
741 |
|
742 |
if (extinfo == FALSE || lines == NULL) |
743 |
return NULL; |
744 |
|
745 |
if (i == 0) |
746 |
return NULL; |
747 |
|
748 |
retval = strstr (lines[i-1], "#EXTINF:"); |
749 |
retval = strstr (retval, ","); |
750 |
if (retval == NULL || retval[0] == '\0') |
751 |
return NULL; |
752 |
|
753 |
retval++; |
754 |
|
755 |
return retval; |
756 |
} |
757 |
|
758 |
static TotemPlParserResult |
759 |
totem_pl_parser_add_m3u (TotemPlParser *parser, const char *url, gpointer data) |
760 |
{ |
761 |
gboolean retval = TOTEM_PL_PARSER_RESULT_UNHANDLED; |
762 |
char *contents, **lines; |
763 |
int size, i; |
764 |
const char *split_char; |
765 |
gboolean extinfo; |
766 |
|
767 |
if (gnome_vfs_read_entire_file (url, &size, &contents) != GNOME_VFS_OK) |
768 |
return FALSE; |
769 |
|
770 |
/* is TRUE if there's an EXTINF on the previous line */ |
771 |
extinfo = FALSE; |
772 |
|
773 |
/* figure out whether we're a unix m3u or dos m3u */ |
774 |
if (strstr(contents,"\x0d") == NULL) |
775 |
split_char = "\n"; |
776 |
else |
777 |
split_char = "\x0d\n"; |
778 |
|
779 |
lines = g_strsplit (contents, split_char, 0); |
780 |
g_free (contents); |
781 |
|
782 |
for (i = 0; lines[i] != NULL; i++) { |
783 |
if (lines[i][0] == '\0') |
784 |
continue; |
785 |
|
786 |
retval = TOTEM_PL_PARSER_RESULT_SUCCESS; |
787 |
|
788 |
/* Ignore comments, but mark it if we have extra info */ |
789 |
if (lines[i][0] == '#') { |
790 |
if (strstr (lines[i], "#EXTINF") != NULL) |
791 |
extinfo = TRUE; |
792 |
continue; |
793 |
} |
794 |
|
795 |
/* Either it's a URI, or it has a proper path ... */ |
796 |
if (strstr(lines[i], "://") != NULL |
797 |
|| lines[i][0] == G_DIR_SEPARATOR) { |
798 |
totem_pl_parser_add_one_url (parser, lines[i], |
799 |
totem_pl_parser_get_extinfo_title (extinfo, lines, i)); |
800 |
extinfo = FALSE; |
801 |
} else if (lines[i][0] == '\\' && lines[i][1] == '\\') { |
802 |
/* ... Or it's in the windows smb form |
803 |
* (\\machine\share\filename), Note drive names |
804 |
* (C:\ D:\ etc) are unhandled (unknown base for |
805 |
* drive letters) */ |
806 |
char *tmpurl; |
807 |
|
808 |
lines[i] = g_strdelimit (lines[i], "\\", '/'); |
809 |
tmpurl = g_strjoin (NULL, "smb:", lines[i], NULL); |
810 |
|
811 |
totem_pl_parser_add_one_url (parser, lines[i], |
812 |
totem_pl_parser_get_extinfo_title (extinfo, lines, i)); |
813 |
extinfo = FALSE; |
814 |
|
815 |
g_free (tmpurl); |
816 |
} else { |
817 |
/* Try with a base */ |
818 |
char *fullpath, *base, sep; |
819 |
|
820 |
base = totem_pl_parser_base_url (url); |
821 |
sep = (split_char[0] == '\n' ? '/' : '\\'); |
822 |
if (sep == '\\') |
823 |
lines[i] = g_strdelimit (lines[i], "\\", '/'); |
824 |
fullpath = g_strdup_printf ("%s/%s", base, lines[i]); |
825 |
totem_pl_parser_add_one_url (parser, fullpath, |
826 |
totem_pl_parser_get_extinfo_title (extinfo, lines, i)); |
827 |
g_free (fullpath); |
828 |
g_free (base); |
829 |
extinfo = FALSE; |
830 |
} |
831 |
} |
832 |
|
833 |
g_strfreev (lines); |
834 |
|
835 |
return retval; |
836 |
} |
837 |
|
838 |
static TotemPlParserResult |
839 |
totem_pl_parser_add_asf_reference_parser (TotemPlParser *parser, |
840 |
const char *url, gpointer data) |
841 |
{ |
842 |
gboolean retval = TOTEM_PL_PARSER_RESULT_UNHANDLED; |
843 |
char *contents, **lines, *ref, *split_char; |
844 |
int size; |
845 |
|
846 |
if (gnome_vfs_read_entire_file (url, &size, &contents) != GNOME_VFS_OK) |
847 |
return TOTEM_PL_PARSER_RESULT_ERROR; |
848 |
|
849 |
if (strstr(contents,"\x0d") == NULL) { |
850 |
split_char = "\n"; |
851 |
} else { |
852 |
split_char = "\x0d\n"; |
853 |
} |
854 |
|
855 |
lines = g_strsplit (contents, split_char, 0); |
856 |
g_free (contents); |
857 |
|
858 |
ref = read_ini_line_string (lines, "Ref1", FALSE); |
859 |
|
860 |
if (ref == NULL) { |
861 |
g_strfreev (lines); |
862 |
return totem_pl_parser_add_asx (parser, url, data); |
863 |
} |
864 |
|
865 |
/* change http to mmsh, thanks Microsoft */ |
866 |
if (g_str_has_prefix (ref, "http") != FALSE) |
867 |
memcpy(ref, "mmsh", 4); |
868 |
|
869 |
totem_pl_parser_add_one_url (parser, ref, NULL); |
870 |
retval = TOTEM_PL_PARSER_RESULT_SUCCESS; |
871 |
g_free (ref); |
872 |
|
873 |
g_strfreev (lines); |
874 |
|
875 |
return retval; |
876 |
} |
877 |
|
878 |
static TotemPlParserResult |
879 |
totem_pl_parser_add_asf_parser (TotemPlParser *parser, |
880 |
const char *url, gpointer data) |
881 |
{ |
882 |
gboolean retval = TOTEM_PL_PARSER_RESULT_UNHANDLED; |
883 |
char *contents, *ref; |
884 |
int size; |
885 |
|
886 |
if (g_str_has_prefix (data, "ASF ") == FALSE) { |
887 |
return totem_pl_parser_add_asf_reference_parser (parser, url, data); |
888 |
} |
889 |
|
890 |
if (gnome_vfs_read_entire_file (url, &size, &contents) != GNOME_VFS_OK) |
891 |
return TOTEM_PL_PARSER_RESULT_ERROR; |
892 |
|
893 |
if (size <= 4) { |
894 |
g_free (contents); |
895 |
return TOTEM_PL_PARSER_RESULT_ERROR; |
896 |
} |
897 |
|
898 |
/* Skip 'ASF ' */ |
899 |
ref = contents + 4; |
900 |
if (g_str_has_prefix (ref, "http") != FALSE) { |
901 |
memcpy(ref, "mmsh", 4); |
902 |
totem_pl_parser_add_one_url (parser, ref, NULL); |
903 |
retval = TOTEM_PL_PARSER_RESULT_SUCCESS; |
904 |
} |
905 |
|
906 |
g_free (contents); |
907 |
return retval; |
908 |
} |
909 |
|
910 |
static TotemPlParserResult |
911 |
totem_pl_parser_add_pls (TotemPlParser *parser, const char *url, gpointer data) |
912 |
{ |
913 |
gboolean retval = TOTEM_PL_PARSER_RESULT_UNHANDLED; |
914 |
char *contents, **lines; |
915 |
int size, i, num_entries; |
916 |
char *split_char, *playlist_title; |
917 |
gboolean dos_mode = FALSE; |
918 |
|
919 |
if (gnome_vfs_read_entire_file (url, &size, &contents) != GNOME_VFS_OK) |
920 |
return TOTEM_PL_PARSER_RESULT_ERROR; |
921 |
|
922 |
if (size == 0) |
923 |
{ |
924 |
g_free (contents); |
925 |
return TOTEM_PL_PARSER_RESULT_SUCCESS; |
926 |
} |
927 |
|
928 |
/* figure out whether we're a unix pls or dos pls */ |
929 |
if (strstr(contents,"\x0d") == NULL) { |
930 |
split_char = "\n"; |
931 |
} else { |
932 |
split_char = "\x0d\n"; |
933 |
dos_mode = TRUE; |
934 |
} |
935 |
lines = g_strsplit (contents, split_char, 0); |
936 |
g_free (contents); |
937 |
|
938 |
/* [playlist] */ |
939 |
i = 0; |
940 |
playlist_title = NULL; |
941 |
|
942 |
/* Ignore empty lines */ |
943 |
while (lines[i] != NULL && strcmp (lines[i], "") == 0) |
944 |
i++; |
945 |
|
946 |
if (lines[i] == NULL |
947 |
|| g_ascii_strncasecmp (lines[i], "[playlist]", |
948 |
(gsize)strlen ("[playlist]")) != 0) |
949 |
goto bail; |
950 |
|
951 |
playlist_title = read_ini_line_string (lines, |
952 |
"X-GNOME-Title", dos_mode); |
953 |
|
954 |
if (playlist_title != NULL) { |
955 |
g_signal_emit (G_OBJECT (parser), |
956 |
totem_pl_parser_table_signals[PLAYLIST_START], |
957 |
0, playlist_title); |
958 |
} |
959 |
|
960 |
/* numberofentries=? */ |
961 |
num_entries = read_ini_line_int (lines, "numberofentries"); |
962 |
if (num_entries == -1) |
963 |
goto bail; |
964 |
|
965 |
retval = TOTEM_PL_PARSER_RESULT_SUCCESS; |
966 |
|
967 |
for (i = 1; i <= num_entries; i++) { |
968 |
char *file, *title, *genre; |
969 |
char *file_key, *title_key, *genre_key; |
970 |
|
971 |
file_key = g_strdup_printf ("file%d", i); |
972 |
title_key = g_strdup_printf ("title%d", i); |
973 |
/* Genre is our own little extension */ |
974 |
genre_key = g_strdup_printf ("genre%d", i); |
975 |
|
976 |
file = read_ini_line_string (lines, (const char*)file_key, dos_mode); |
977 |
title = read_ini_line_string (lines, (const char*)title_key, dos_mode); |
978 |
genre = read_ini_line_string (lines, (const char*)genre_key, dos_mode); |
979 |
|
980 |
g_free (file_key); |
981 |
g_free (title_key); |
982 |
g_free (genre_key); |
983 |
|
984 |
if (file == NULL) |
985 |
{ |
986 |
g_free (file); |
987 |
g_free (title); |
988 |
g_free (genre); |
989 |
continue; |
990 |
} |
991 |
|
992 |
if (strstr (file, "://") != NULL |
993 |
|| file[0] == G_DIR_SEPARATOR) { |
994 |
totem_pl_parser_add_one_url_ext (parser, |
995 |
file, title, genre); |
996 |
} else { |
997 |
char *uri, *base, *escaped; |
998 |
|
999 |
/* Try with a base */ |
1000 |
base = totem_pl_parser_base_url (url); |
1001 |
escaped = gnome_vfs_escape_path_string (file); |
1002 |
|
1003 |
uri = g_strdup_printf ("%s/%s", base, escaped); |
1004 |
|
1005 |
totem_pl_parser_add_one_url_ext (parser, |
1006 |
uri, title, genre); |
1007 |
|
1008 |
g_free (escaped); |
1009 |
g_free (uri); |
1010 |
g_free (base); |
1011 |
} |
1012 |
|
1013 |
g_free (file); |
1014 |
g_free (title); |
1015 |
g_free (genre); |
1016 |
} |
1017 |
|
1018 |
if (playlist_title != NULL) { |
1019 |
g_signal_emit (G_OBJECT (parser), |
1020 |
totem_pl_parser_table_signals[PLAYLIST_END], |
1021 |
0, playlist_title); |
1022 |
} |
1023 |
|
1024 |
bail: |
1025 |
g_free (playlist_title); |
1026 |
g_strfreev (lines); |
1027 |
|
1028 |
return retval; |
1029 |
} |
1030 |
|
1031 |
static gboolean |
1032 |
parse_asx_entry (TotemPlParser *parser, char *base, xmlDocPtr doc, |
1033 |
xmlNodePtr parent, const char *pl_title) |
1034 |
{ |
1035 |
xmlNodePtr node; |
1036 |
guchar *title, *url; |
1037 |
gboolean retval = FALSE; |
1038 |
char *fullpath = NULL; |
1039 |
|
1040 |
title = NULL; |
1041 |
url = NULL; |
1042 |
|
1043 |
for (node = parent->children; node != NULL; node = node->next) { |
1044 |
if (node->name == NULL) |
1045 |
continue; |
1046 |
|
1047 |
/* ENTRY should only have one ref and one title nodes */ |
1048 |
if (g_ascii_strcasecmp ((char *)node->name, "ref") == 0 |
1049 |
|| g_ascii_strcasecmp ((char *)node->name, "entryref") == 0) { |
1050 |
url = xmlGetProp (node, (guchar *)"href"); |
1051 |
if (url == NULL) |
1052 |
url = xmlGetProp (node, (guchar *)"HREF"); |
1053 |
continue; |
1054 |
} |
1055 |
|
1056 |
if (g_ascii_strcasecmp ((char *)node->name, "title") == 0) |
1057 |
title = xmlNodeListGetString(doc, node->children, 1); |
1058 |
} |
1059 |
|
1060 |
if (url == NULL) { |
1061 |
g_free (title); |
1062 |
return FALSE; |
1063 |
} |
1064 |
|
1065 |
if (strstr ((char *)url, "://") == NULL && url[0] != '/') { |
1066 |
fullpath = g_strdup_printf ("%s/%s", base, url); |
1067 |
} else { |
1068 |
fullpath = g_strdup ((char *)url); |
1069 |
} |
1070 |
|
1071 |
/* .asx files can contain references to other .asx files */ |
1072 |
if (totem_pl_parser_parse_internal (parser, fullpath) != TOTEM_PL_PARSER_RESULT_SUCCESS) { |
1073 |
totem_pl_parser_add_one_url (parser, fullpath, |
1074 |
(char *)title ? (char *)title : pl_title); |
1075 |
} |
1076 |
|
1077 |
g_free (fullpath); |
1078 |
g_free (title); |
1079 |
g_free (url); |
1080 |
|
1081 |
return retval; |
1082 |
} |
1083 |
|
1084 |
static gboolean |
1085 |
parse_asx_entries (TotemPlParser *parser, char *base, xmlDocPtr doc, |
1086 |
xmlNodePtr parent) |
1087 |
{ |
1088 |
guchar *title = NULL; |
1089 |
xmlNodePtr node; |
1090 |
gboolean retval = FALSE; |
1091 |
|
1092 |
for (node = parent->children; node != NULL; node = node->next) { |
1093 |
if (node->name == NULL) |
1094 |
continue; |
1095 |
|
1096 |
if (g_ascii_strcasecmp ((char *)node->name, "title") == 0) { |
1097 |
title = xmlNodeListGetString(doc, node->children, 1); |
1098 |
} |
1099 |
|
1100 |
if (g_ascii_strcasecmp ((char *)node->name, "entry") == 0) { |
1101 |
/* Whee found an entry here, find the REF and TITLE */ |
1102 |
if (parse_asx_entry (parser, base, doc, node, (char *)title) != FALSE) |
1103 |
retval = TRUE; |
1104 |
} |
1105 |
if (g_ascii_strcasecmp ((char *)node->name, "entryref") == 0) { |
1106 |
/* Found an entryref, give the parent instead of the |
1107 |
* children to the parser */ |
1108 |
if (parse_asx_entry (parser, base, doc, parent, (char *)title) != FALSE) |
1109 |
retval = TRUE; |
1110 |
} |
1111 |
if (g_ascii_strcasecmp ((char *)node->name, "repeat") == 0) { |
1112 |
/* Repeat at the top-level */ |
1113 |
if (parse_asx_entries (parser, base, doc, node) != FALSE) |
1114 |
retval = TRUE; |
1115 |
} |
1116 |
} |
1117 |
|
1118 |
g_free (title); |
1119 |
|
1120 |
return retval; |
1121 |
} |
1122 |
|
1123 |
static TotemPlParserResult |
1124 |
totem_pl_parser_add_asx (TotemPlParser *parser, const char *url, gpointer data) |
1125 |
{ |
1126 |
xmlDocPtr doc; |
1127 |
xmlNodePtr node; |
1128 |
char *contents = NULL, *base; |
1129 |
int size; |
1130 |
gboolean retval = TOTEM_PL_PARSER_RESULT_UNHANDLED; |
1131 |
|
1132 |
if (gnome_vfs_read_entire_file (url, &size, &contents) != GNOME_VFS_OK) |
1133 |
return FALSE; |
1134 |
|
1135 |
doc = xmlParseMemory (contents, size); |
1136 |
if (doc == NULL) |
1137 |
doc = xmlRecoverMemory (contents, size); |
1138 |
g_free (contents); |
1139 |
|
1140 |
/* If the document has no root, or no name */ |
1141 |
if(!doc || !doc->children || !doc->children->name) { |
1142 |
if (doc != NULL) |
1143 |
xmlFreeDoc(doc); |
1144 |
return TOTEM_PL_PARSER_RESULT_ERROR; |
1145 |
} |
1146 |
|
1147 |
base = totem_pl_parser_base_url (url); |
1148 |
|
1149 |
for (node = doc->children; node != NULL; node = node->next) |
1150 |
if (parse_asx_entries (parser, base, doc, node) != FALSE) |
1151 |
retval = TOTEM_PL_PARSER_RESULT_SUCCESS; |
1152 |
|
1153 |
g_free (base); |
1154 |
xmlFreeDoc(doc); |
1155 |
return retval; |
1156 |
} |
1157 |
|
1158 |
static TotemPlParserResult |
1159 |
totem_pl_parser_add_ra (TotemPlParser *parser, const char *url, gpointer data) |
1160 |
{ |
1161 |
if (data == NULL |
1162 |
|| (g_str_has_prefix (data, "http://") == FALSE |
1163 |
&& g_str_has_prefix (data, "rtsp://") == FALSE |
1164 |
&& g_str_has_prefix (data, "pnm://") == FALSE)) { |
1165 |
totem_pl_parser_add_one_url (parser, url, NULL); |
1166 |
return TOTEM_PL_PARSER_RESULT_SUCCESS; |
1167 |
} |
1168 |
|
1169 |
return totem_pl_parser_add_ram (parser, url, NULL); |
1170 |
} |
1171 |
|
1172 |
static gboolean |
1173 |
parse_smil_video_entry (TotemPlParser *parser, char *base, |
1174 |
char *url, char *title) |
1175 |
{ |
1176 |
if (strstr (url, "://") != NULL || url[0] == '/') { |
1177 |
totem_pl_parser_add_one_url (parser, url, title); |
1178 |
} else { |
1179 |
char *fullpath; |
1180 |
|
1181 |
fullpath = g_strdup_printf ("%s/%s", base, url); |
1182 |
totem_pl_parser_add_one_url (parser, fullpath, title); |
1183 |
|
1184 |
g_free (fullpath); |
1185 |
} |
1186 |
|
1187 |
return TRUE; |
1188 |
} |
1189 |
|
1190 |
static gboolean |
1191 |
parse_smil_entry (TotemPlParser *parser, char *base, xmlDocPtr doc, |
1192 |
xmlNodePtr parent) |
1193 |
{ |
1194 |
xmlNodePtr node; |
1195 |
guchar *title, *url; |
1196 |
gboolean retval = FALSE; |
1197 |
|
1198 |
title = NULL; |
1199 |
url = NULL; |
1200 |
|
1201 |
for (node = parent->children; node != NULL; node = node->next) |
1202 |
{ |
1203 |
if (node->name == NULL) |
1204 |
continue; |
1205 |
|
1206 |
/* ENTRY should only have one ref and one title nodes */ |
1207 |
if (g_ascii_strcasecmp ((char *)node->name, "video") == 0) { |
1208 |
url = xmlGetProp (node, (guchar *)"src"); |
1209 |
title = xmlGetProp (node, (guchar *)"title"); |
1210 |
|
1211 |
if (url != NULL) { |
1212 |
if (parse_smil_video_entry (parser, |
1213 |
base, (char *)url, (char *)title) != FALSE) |
1214 |
retval = TRUE; |
1215 |
} |
1216 |
|
1217 |
g_free (title); |
1218 |
g_free (url); |
1219 |
} else { |
1220 |
if (parse_smil_entry (parser, |
1221 |
base, doc, node) != FALSE) |
1222 |
retval = TRUE; |
1223 |
} |
1224 |
} |
1225 |
|
1226 |
return retval; |
1227 |
} |
1228 |
|
1229 |
static gboolean |
1230 |
parse_smil_entries (TotemPlParser *parser, char *base, xmlDocPtr doc, |
1231 |
xmlNodePtr parent) |
1232 |
{ |
1233 |
xmlNodePtr node; |
1234 |
gboolean retval = FALSE; |
1235 |
|
1236 |
for (node = parent->children; node != NULL; node = node->next) { |
1237 |
if (node->name == NULL) |
1238 |
continue; |
1239 |
|
1240 |
if (g_ascii_strcasecmp ((char *)node->name, "body") == 0) { |
1241 |
if (parse_smil_entry (parser, base, |
1242 |
doc, node) != FALSE) |
1243 |
retval = TRUE; |
1244 |
} |
1245 |
|
1246 |
} |
1247 |
|
1248 |
return retval; |
1249 |
} |
1250 |
|
1251 |
static TotemPlParserResult |
1252 |
totem_pl_parser_add_smil (TotemPlParser *parser, const char *url, gpointer data) |
1253 |
{ |
1254 |
xmlDocPtr doc; |
1255 |
xmlNodePtr node; |
1256 |
char *contents = NULL, *base; |
1257 |
int size; |
1258 |
gboolean retval = TOTEM_PL_PARSER_RESULT_UNHANDLED; |
1259 |
|
1260 |
if (gnome_vfs_read_entire_file (url, &size, &contents) != GNOME_VFS_OK) |
1261 |
return TOTEM_PL_PARSER_RESULT_ERROR; |
1262 |
|
1263 |
doc = xmlParseMemory (contents, size); |
1264 |
if (doc == NULL) |
1265 |
doc = xmlRecoverMemory (contents, size); |
1266 |
g_free (contents); |
1267 |
|
1268 |
/* If the document has no root, or no name */ |
1269 |
if(!doc || !doc->children |
1270 |
|| !doc->children->name |
1271 |
|| g_ascii_strcasecmp ((char *)doc->children->name, |
1272 |
"smil") != 0) { |
1273 |
if (doc != NULL) |
1274 |
xmlFreeDoc (doc); |
1275 |
return TOTEM_PL_PARSER_RESULT_ERROR; |
1276 |
} |
1277 |
|
1278 |
base = totem_pl_parser_base_url (url); |
1279 |
|
1280 |
for (node = doc->children; node != NULL; node = node->next) |
1281 |
if (parse_smil_entries (parser, base, doc, node) != FALSE) |
1282 |
retval = TOTEM_PL_PARSER_RESULT_SUCCESS; |
1283 |
|
1284 |
g_free (base); |
1285 |
xmlFreeDoc (doc); |
1286 |
|
1287 |
return retval; |
1288 |
} |
1289 |
|
1290 |
static TotemPlParserResult |
1291 |
totem_pl_parser_add_asf (TotemPlParser *parser, const char *url, gpointer data) |
1292 |
{ |
1293 |
if (data != NULL && |
1294 |
(g_str_has_prefix (data, "[Reference]") == FALSE |
1295 |
&& g_ascii_strncasecmp (data, "<ASX", strlen ("<ASX")) != 0 |
1296 |
&& strstr (data, "<ASX") == NULL) |
1297 |
&& g_str_has_prefix (data, "ASF ") == FALSE) { |
1298 |
totem_pl_parser_add_one_url (parser, url, NULL); |
1299 |
return TOTEM_PL_PARSER_RESULT_SUCCESS; |
1300 |
} |
1301 |
|
1302 |
return totem_pl_parser_add_asf_parser (parser, url, data); |
1303 |
} |
1304 |
|
1305 |
static TotemPlParserResult |
1306 |
totem_pl_parser_add_desktop (TotemPlParser *parser, const char *url, gpointer data) |
1307 |
{ |
1308 |
char *contents, **lines; |
1309 |
const char *path, *display_name, *type; |
1310 |
int size; |
1311 |
|
1312 |
if (gnome_vfs_read_entire_file (url, &size, &contents) != GNOME_VFS_OK) |
1313 |
return TOTEM_PL_PARSER_RESULT_ERROR; |
1314 |
|
1315 |
lines = g_strsplit (contents, "\n", 0); |
1316 |
g_free (contents); |
1317 |
|
1318 |
type = read_ini_line_string (lines, "Type", FALSE); |
1319 |
if (type == NULL || g_ascii_strcasecmp (type, "Link") != 0) { |
1320 |
g_strfreev (lines); |
1321 |
return TOTEM_PL_PARSER_RESULT_ERROR; |
1322 |
} |
1323 |
|
1324 |
path = read_ini_line_string (lines, "URL", FALSE); |
1325 |
if (path == NULL) { |
1326 |
g_strfreev (lines); |
1327 |
return TOTEM_PL_PARSER_RESULT_ERROR; |
1328 |
} |
1329 |
|
1330 |
display_name = read_ini_line_string (lines, "Name", FALSE); |
1331 |
|
1332 |
if (totem_pl_parser_ignore (parser, path) == FALSE) { |
1333 |
totem_pl_parser_add_one_url (parser, path, display_name); |
1334 |
} else { |
1335 |
if (totem_pl_parser_parse_internal (parser, path) != TOTEM_PL_PARSER_RESULT_SUCCESS) |
1336 |
totem_pl_parser_add_one_url (parser, path, display_name); |
1337 |
} |
1338 |
|
1339 |
g_strfreev (lines); |
1340 |
|
1341 |
return TOTEM_PL_PARSER_RESULT_SUCCESS; |
1342 |
} |
1343 |
|
1344 |
static int |
1345 |
totem_pl_parser_dir_compare (GnomeVFSFileInfo *a, GnomeVFSFileInfo *b) |
1346 |
{ |
1347 |
if (a->name == NULL) { |
1348 |
if (b->name == NULL) |
1349 |
return 0; |
1350 |
else |
1351 |
return -1; |
1352 |
} else { |
1353 |
if (b->name == NULL) |
1354 |
return 1; |
1355 |
else |
1356 |
return strcmp (a->name, b->name); |
1357 |
} |
1358 |
} |
1359 |
|
1360 |
static TotemPlParserResult |
1361 |
totem_pl_parser_add_directory (TotemPlParser *parser, const char *url, |
1362 |
gpointer data) |
1363 |
{ |
1364 |
MediaType type; |
1365 |
GList *list, *l; |
1366 |
GnomeVFSResult res; |
1367 |
|
1368 |
if (parser->priv->recurse_level == 1) { |
1369 |
char *media_url; |
1370 |
|
1371 |
type = totem_cd_detect_type_from_dir (url, &media_url, NULL); |
1372 |
if (type != MEDIA_TYPE_DATA && type != MEDIA_TYPE_ERROR) { |
1373 |
if (media_url != NULL) { |
1374 |
totem_pl_parser_add_one_url (parser, media_url, NULL); |
1375 |
g_free (media_url); |
1376 |
return TOTEM_PL_PARSER_RESULT_SUCCESS; |
1377 |
} |
1378 |
} |
1379 |
} |
1380 |
|
1381 |
res = gnome_vfs_directory_list_load (&list, url, |
1382 |
GNOME_VFS_FILE_INFO_DEFAULT); |
1383 |
if (res != GNOME_VFS_OK) |
1384 |
return TOTEM_PL_PARSER_RESULT_ERROR; |
1385 |
|
1386 |
list = g_list_sort (list, (GCompareFunc) totem_pl_parser_dir_compare); |
1387 |
l = list; |
1388 |
|
1389 |
while (l != NULL) { |
1390 |
char *name, *fullpath; |
1391 |
GnomeVFSFileInfo *info = l->data; |
1392 |
TotemPlParserResult ret; |
1393 |
|
1394 |
if (info->name != NULL && (strcmp (info->name, ".") == 0 |
1395 |
|| strcmp (info->name, "..") == 0)) { |
1396 |
l = l->next; |
1397 |
continue; |
1398 |
} |
1399 |
|
1400 |
name = gnome_vfs_escape_string (info->name); |
1401 |
fullpath = g_strconcat (url, "/", name, NULL); |
1402 |
g_free (name); |
1403 |
|
1404 |
ret = totem_pl_parser_parse_internal (parser, fullpath); |
1405 |
if (ret != TOTEM_PL_PARSER_RESULT_SUCCESS && ret != TOTEM_PL_PARSER_RESULT_IGNORED) |
1406 |
totem_pl_parser_add_one_url (parser, fullpath, NULL); |
1407 |
|
1408 |
l = l->next; |
1409 |
} |
1410 |
|
1411 |
g_list_foreach (list, (GFunc) gnome_vfs_file_info_unref, NULL); |
1412 |
g_list_free (list); |
1413 |
|
1414 |
return TOTEM_PL_PARSER_RESULT_SUCCESS; |
1415 |
} |
1416 |
|
1417 |
/* These ones need a special treatment, mostly parser formats */ |
1418 |
static PlaylistTypes special_types[] = { |
1419 |
{ "audio/x-mpegurl", totem_pl_parser_add_m3u }, |
1420 |
{ "audio/playlist", totem_pl_parser_add_m3u }, |
1421 |
{ "audio/x-ms-asx", totem_pl_parser_add_asx }, |
1422 |
{ "audio/x-scpls", totem_pl_parser_add_pls }, |
1423 |
{ "application/x-smil", totem_pl_parser_add_smil }, |
1424 |
{ "application/x-gnome-app-info", totem_pl_parser_add_desktop }, |
1425 |
{ "application/x-desktop", totem_pl_parser_add_desktop }, |
1426 |
{ "x-directory/normal", totem_pl_parser_add_directory }, |
1427 |
{ "video/x-ms-wvx", totem_pl_parser_add_asx }, |
1428 |
{ "audio/x-ms-wax", totem_pl_parser_add_asx }, |
1429 |
}; |
1430 |
|
1431 |
static PlaylistTypes ignore_types[] = { |
1432 |
{ "image/*", NULL }, |
1433 |
{ "text/plain", NULL }, |
1434 |
{ "application/x-rar", NULL }, |
1435 |
{ "application/zip", NULL }, |
1436 |
}; |
1437 |
|
1438 |
/* These ones are "dual" types, might be a video, might be a parser */ |
1439 |
static PlaylistTypes dual_types[] = { |
1440 |
{ "audio/x-real-audio", totem_pl_parser_add_ra }, |
1441 |
{ "audio/x-pn-realaudio", totem_pl_parser_add_ra }, |
1442 |
{ "application/vnd.rn-realmedia", totem_pl_parser_add_ra }, |
1443 |
{ "audio/x-pn-realaudio-plugin", totem_pl_parser_add_ra }, |
1444 |
{ "text/plain", totem_pl_parser_add_ra }, |
1445 |
{ "video/x-ms-asf", totem_pl_parser_add_asf }, |
1446 |
{ "video/x-ms-wmv", totem_pl_parser_add_asf }, |
1447 |
}; |
1448 |
|
1449 |
static gboolean |
1450 |
totem_pl_parser_scheme_is_ignored (TotemPlParser *parser, const char *url) |
1451 |
{ |
1452 |
GList *l; |
1453 |
|
1454 |
if (parser->priv->ignore_schemes == NULL) |
1455 |
return FALSE; |
1456 |
|
1457 |
for (l = parser->priv->ignore_schemes; l != NULL; l = l->next) |
1458 |
{ |
1459 |
const char *scheme = l->data; |
1460 |
if (g_str_has_prefix (url, scheme) != FALSE) |
1461 |
return TRUE; |
1462 |
} |
1463 |
|
1464 |
return FALSE; |
1465 |
} |
1466 |
|
1467 |
static gboolean |
1468 |
totem_pl_parser_mimetype_is_ignored (TotemPlParser *parser, |
1469 |
const char *mimetype) |
1470 |
{ |
1471 |
GList *l; |
1472 |
|
1473 |
if (parser->priv->ignore_mimetypes == NULL) |
1474 |
return FALSE; |
1475 |
|
1476 |
for (l = parser->priv->ignore_mimetypes; l != NULL; l = l->next) |
1477 |
{ |
1478 |
const char *item = l->data; |
1479 |
if (strcmp (mimetype, item) == 0) |
1480 |
return TRUE; |
1481 |
} |
1482 |
|
1483 |
return FALSE; |
1484 |
|
1485 |
} |
1486 |
|
1487 |
static gboolean |
1488 |
totem_pl_parser_ignore (TotemPlParser *parser, const char *url) |
1489 |
{ |
1490 |
const char *mimetype; |
1491 |
guint i; |
1492 |
|
1493 |
if (totem_pl_parser_scheme_is_ignored (parser, url) != FALSE) |
1494 |
return TRUE; |
1495 |
|
1496 |
mimetype = gnome_vfs_get_file_mime_type (url, NULL, TRUE); |
1497 |
if (mimetype == NULL || strcmp (mimetype, "application/octet-stream") == 0) |
1498 |
return FALSE; |
1499 |
|
1500 |
for (i = 0; i < G_N_ELEMENTS (special_types); i++) |
1501 |
if (strcmp (special_types[i].mimetype, mimetype) == 0) |
1502 |
return FALSE; |
1503 |
|
1504 |
for (i = 0; i < G_N_ELEMENTS (dual_types); i++) |
1505 |
if (strcmp (dual_types[i].mimetype, mimetype) == 0) |
1506 |
return FALSE; |
1507 |
|
1508 |
/* It's a remote file that could be an m3u file */ |
1509 |
if (strcmp (mimetype, "audio/x-mp3") == 0) |
1510 |
{ |
1511 |
if (strstr (url, "m3u") != NULL) |
1512 |
return FALSE; |
1513 |
} |
1514 |
|
1515 |
return TRUE; |
1516 |
} |
1517 |
|
1518 |
static TotemPlParserResult |
1519 |
totem_pl_parser_parse_internal (TotemPlParser *parser, const char *url) |
1520 |
{ |
1521 |
const char *mimetype; |
1522 |
guint i; |
1523 |
gpointer data = NULL; |
1524 |
gboolean ret = FALSE; |
1525 |
char *super; |
1526 |
|
1527 |
if (parser->priv->recurse_level > RECURSE_LEVEL_MAX) |
1528 |
return TOTEM_PL_PARSER_RESULT_ERROR; |
1529 |
|
1530 |
mimetype = gnome_vfs_get_mime_type (url); |
1531 |
if (!mimetype) |
1532 |
mimetype = my_gnome_vfs_get_mime_type_with_data (url, &data); |
1533 |
|
1534 |
if (mimetype == NULL) |
1535 |
return TOTEM_PL_PARSER_RESULT_UNHANDLED; |
1536 |
|
1537 |
if (totem_pl_parser_mimetype_is_ignored (parser, mimetype) != FALSE) { |
1538 |
g_free (data); |
1539 |
return TOTEM_PL_PARSER_RESULT_IGNORED; |
1540 |
} |
1541 |
|
1542 |
super = gnome_vfs_get_supertype_from_mime_type (mimetype); |
1543 |
for (i = 0; i < G_N_ELEMENTS (ignore_types); i++) { |
1544 |
if (gnome_vfs_mime_type_is_supertype (ignore_types[i].mimetype) != FALSE) { |
1545 |
if (strcmp (super, ignore_types[i].mimetype) == 0) { |
1546 |
g_free (data); |
1547 |
g_free (super); |
1548 |
return TOTEM_PL_PARSER_RESULT_IGNORED; |
1549 |
} |
1550 |
} else { |
1551 |
GnomeVFSMimeEquivalence eq; |
1552 |
|
1553 |
eq = gnome_vfs_mime_type_get_equivalence (mimetype, ignore_types[i].mimetype); |
1554 |
if (eq == GNOME_VFS_MIME_PARENT || eq == GNOME_VFS_MIME_IDENTICAL) { |
1555 |
g_free (data); |
1556 |
return TOTEM_PL_PARSER_RESULT_IGNORED; |
1557 |
} |
1558 |
} |
1559 |
} |
1560 |
|
1561 |
parser->priv->recurse_level++; |
1562 |
|
1563 |
for (i = 0; i < G_N_ELEMENTS(special_types); i++) { |
1564 |
if (strcmp (special_types[i].mimetype, mimetype) == 0) { |
1565 |
ret = (* special_types[i].func) (parser, url, data); |
1566 |
g_free (data); |
1567 |
break; |
1568 |
} |
1569 |
} |
1570 |
|
1571 |
for (i = 0; i < G_N_ELEMENTS(dual_types); i++) { |
1572 |
if (strcmp (dual_types[i].mimetype, mimetype) == 0) { |
1573 |
if (data == NULL) { |
1574 |
mimetype = my_gnome_vfs_get_mime_type_with_data (url, &data); |
1575 |
} |
1576 |
ret = (* dual_types[i].func) (parser, url, data); |
1577 |
g_free (data); |
1578 |
break; |
1579 |
} |
1580 |
} |
1581 |
|
1582 |
parser->priv->recurse_level--; |
1583 |
|
1584 |
if (ret == FALSE && parser->priv->fallback) { |
1585 |
totem_pl_parser_add_one_url (parser, url, NULL); |
1586 |
return TOTEM_PL_PARSER_RESULT_SUCCESS; |
1587 |
} |
1588 |
|
1589 |
if (ret) |
1590 |
return TOTEM_PL_PARSER_RESULT_SUCCESS; |
1591 |
else |
1592 |
return TOTEM_PL_PARSER_RESULT_UNHANDLED; |
1593 |
} |
1594 |
|
1595 |
TotemPlParserResult |
1596 |
totem_pl_parser_parse (TotemPlParser *parser, const char *url, |
1597 |
gboolean fallback) |
1598 |
{ |
1599 |
g_return_val_if_fail (TOTEM_IS_PL_PARSER (parser), TOTEM_PL_PARSER_RESULT_UNHANDLED); |
1600 |
g_return_val_if_fail (url != NULL, TOTEM_PL_PARSER_RESULT_UNHANDLED); |
1601 |
|
1602 |
if (totem_pl_parser_scheme_is_ignored (parser, url) != FALSE) |
1603 |
return TOTEM_PL_PARSER_RESULT_UNHANDLED; |
1604 |
|
1605 |
g_return_val_if_fail (strstr (url, "://") != NULL, |
1606 |
TOTEM_PL_PARSER_RESULT_IGNORED); |
1607 |
|
1608 |
parser->priv->recurse_level = 0; |
1609 |
parser->priv->fallback = fallback; |
1610 |
return totem_pl_parser_parse_internal (parser, url); |
1611 |
} |
1612 |
|
1613 |
void |
1614 |
totem_pl_parser_add_ignored_scheme (TotemPlParser *parser, |
1615 |
const char *scheme) |
1616 |
{ |
1617 |
g_return_if_fail (TOTEM_IS_PL_PARSER (parser)); |
1618 |
|
1619 |
parser->priv->ignore_schemes = g_list_prepend |
1620 |
(parser->priv->ignore_schemes, g_strdup (scheme)); |
1621 |
} |
1622 |
|
1623 |
void |
1624 |
totem_pl_parser_add_ignored_mimetype (TotemPlParser *parser, |
1625 |
const char *mimetype) |
1626 |
{ |
1627 |
g_return_if_fail (TOTEM_IS_PL_PARSER (parser)); |
1628 |
|
1629 |
parser->priv->ignore_mimetypes = g_list_prepend |
1630 |
(parser->priv->ignore_mimetypes, g_strdup (mimetype)); |
1631 |
} |
1632 |
|