~ubuntu-branches/ubuntu/precise/rhythmbox/precise-201203091205

« back to all changes in this revision

Viewing changes to podcast/rb-podcast-parse.c

Tags: upstream-0.9.5
ImportĀ upstreamĀ versionĀ 0.9.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 *
17
17
 *  You should have received a copy of the GNU General Public License
18
18
 *  along with this program; if not, write to the Free Software
19
 
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
20
20
 *
21
21
 */
22
22
 
23
23
#define _XOPEN_SOURCE
 
24
#define __EXTENSIONS__  /* get strptime */
24
25
#include <string.h>
25
26
#include <time.h>
26
27
 
28
29
#include <libxml/SAX.h>
29
30
#include <libxml/parserInternals.h>
30
31
#include <libgnomevfs/gnome-vfs.h>
 
32
#include <glib/gi18n.h>
 
33
#include <gtk/gtk.h>
31
34
 
32
35
#include "rb-debug.h"
33
36
#include "rb-podcast-parse.h" 
98
101
                ctx->channel_data->summary = dvalue;
99
102
        } else if (!strcmp (name, "description")) {
100
103
                ctx->channel_data->description = dvalue;
101
 
        } else if (!strcmp (name, "generator") ||
102
 
                   !strcmp (name, "itunes:author")) {
 
104
        } else if (!strcmp (name, "generator")) {
 
105
                if (ctx->channel_data->author == NULL)
 
106
                        ctx->channel_data->author = dvalue;
 
107
        } else if (!strcmp (name, "itunes:author")) {
 
108
                g_free (ctx->channel_data->author);
103
109
                ctx->channel_data->author = dvalue;
104
110
        } else if (!strcmp (name, "webMaster")) {
105
111
                ctx->channel_data->contact = dvalue;
297
303
        case RB_PODCAST_PARSER_STATE_CHANNEL_PROPERTY:
298
304
        case RB_PODCAST_PARSER_STATE_ITEM_PROPERTY:
299
305
        case RB_PODCAST_PARSER_STATE_IMG_PROPERTY:
 
306
                rb_debug ("nested element inside property; treating as unknown");
 
307
                ctx->in_unknown_elt++;
 
308
                break;
 
309
 
300
310
        case RB_PODCAST_PARSER_STATE_END:
301
311
                break;
302
312
        default:
401
411
}
402
412
 
403
413
 
404
 
void
 
414
gboolean
405
415
rb_podcast_parse_load_feed (RBPodcastChannel *data,
406
416
                            const char *file_name)
407
417
{
410
420
        GnomeVFSResult result;
411
421
        GnomeVFSFileInfo *info;
412
422
        gint file_size;
413
 
        gchar *buffer;
 
423
        gchar *buffer = NULL;
414
424
    
415
425
        struct RBPoadcastLoadContext *ctx = NULL;
416
426
 
417
427
        data->url = xmlCharStrdup (file_name);
418
428
 
419
 
        if (!gnome_vfs_initialized ()) {
420
 
                return;
421
 
        }
422
 
 
423
429
        if (!g_str_has_suffix (file_name, ".rss") && !g_str_has_suffix (file_name, ".xml")) {
424
430
                gboolean invalid_mime_type;
425
431
 
436
442
                        invalid_mime_type = FALSE;
437
443
                }
438
444
 
439
 
                if ((result != GNOME_VFS_OK) || invalid_mime_type) {
 
445
                if ((result != GNOME_VFS_OK)) {
440
446
                        rb_debug ("Invalid mime-type in podcast feed %s", info->mime_type);
441
447
                        gnome_vfs_file_info_unref (info);
442
 
                        return;
 
448
                        return TRUE;
 
449
                }
 
450
 
 
451
                if (invalid_mime_type) {
 
452
                        GtkWidget *dialog;
 
453
 
 
454
                        GDK_THREADS_ENTER ();
 
455
                        dialog = gtk_message_dialog_new (NULL, 0,
 
456
                                                         GTK_MESSAGE_QUESTION,
 
457
                                                         GTK_BUTTONS_YES_NO,
 
458
                                                         _("The URL '%s' does not appear to be a podcast feed. "
 
459
                                                         "It may be the wrong URL, or the feed may be broken. "
 
460
                                                         "Would you like Rhythmbox to attempt to use it anyway?"),
 
461
                                                         file_name);
 
462
                        
 
463
                        if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
 
464
                                invalid_mime_type = FALSE;
 
465
 
 
466
                        gtk_widget_destroy (dialog);
 
467
                        GDK_THREADS_LEAVE ();
443
468
                }
444
469
 
445
470
                gnome_vfs_file_info_unref (info);
 
471
 
 
472
                if (invalid_mime_type)
 
473
                        return FALSE;
446
474
        }
447
475
    
448
476
        /* first download file by gnome_vfs for use gnome network configuration */
449
477
        result = gnome_vfs_read_entire_file (file_name, &file_size, &buffer);
450
478
        if (result != GNOME_VFS_OK)
451
 
                return;
 
479
                return TRUE;
452
480
            
453
481
 
454
482
        /* initializing parse */
464
492
        ctx->prop_value = g_string_sized_new (512);
465
493
                            
466
494
        ctxt = xmlCreateMemoryParserCtxt (buffer, file_size);
 
495
        if (ctx == NULL) {
 
496
                g_free (sax_handler);
 
497
                g_free (buffer);
 
498
                g_string_free (ctx->prop_value, TRUE);
 
499
                g_free (ctx);
 
500
                return FALSE;
 
501
        }
 
502
 
467
503
        ctx->xmlctx = ctxt;
468
504
        ctxt->userData = ctx;
469
505
        ctxt->sax = sax_handler;
478
514
        g_free (ctx);
479
515
 
480
516
        data->posts = g_list_reverse (data->posts);
 
517
        return TRUE;
481
518
}
482
519
 
483
520
static uintmax_t
489
526
        /* RFC 2822 date format */
490
527
        result = strptime (date_str, "%a, %d %b %Y %T", &tm);
491
528
 
 
529
        /* same as above, but without comma */
 
530
        if (result == NULL) {
 
531
                memset (&tm, 0, sizeof (struct tm));
 
532
                result = strptime (date_str, "%a %d %b %Y %T", &tm);
 
533
        }
 
534
 
 
535
        /* close-to-RFC 2822, but with extra 0 */
 
536
        if (result == NULL) {
 
537
                memset (&tm, 0, sizeof (struct tm));
 
538
                result = strptime (date_str, "%a, %d %b %Y 0%T", &tm);
 
539
        }
 
540
 
492
541
        /* format without weekday */
493
542
        if (result == NULL) {
494
543
                memset (&tm, 0, sizeof (struct tm));
507
556
                result = strptime (date_str, "%Y-%m-%d %T", &tm);
508
557
        }
509
558
 
 
559
        /* ISO date like without timezone */
 
560
        if (result == NULL) {
 
561
        memset (&tm, 0, sizeof (struct tm));
 
562
                result = strptime (date_str, "%Y-%m-%d", &tm);
 
563
        }
 
564
 
510
565
        /* Broken weekday short names */
511
566
        if (result == NULL) {
512
567
                char *tmp;
520
575
                }
521
576
        }
522
577
 
 
578
        /* format with timezone offset from GMT */
 
579
        if (result == NULL) {
 
580
                memset (&tm, 0, sizeof (struct tm));
 
581
                result = strptime (date_str, "%a %b %d %T %z %Y", &tm);
 
582
        }
 
583
 
 
584
        /* format with timezone name */
 
585
        if (result == NULL) {
 
586
                char *tmp;
 
587
 
 
588
                memset (&tm, 0, sizeof (struct tm));
 
589
        
 
590
                /* match first part of time string */
 
591
                result = strptime (date_str, "%a %b %d %T ", &tm);
 
592
 
 
593
                /* look for anything with a timezone name-like format
 
594
                   i.e. at least one all caps alphabetical character */
 
595
                if (result != NULL) {
 
596
                        size_t n;
 
597
 
 
598
                        n = strspn(result, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
 
599
                        tmp = result+n;
 
600
 
 
601
                        /* make sure there was at least one character that matched */
 
602
                        if ((tmp != NULL) && n > 0) 
 
603
                                /* remaining part must be the year */
 
604
                                result = strptime (tmp, "%Y", &tm);
 
605
                        else 
 
606
                                result = NULL;
 
607
                }
 
608
        }
 
609
 
523
610
        if (result == NULL) {
524
611
                memset (&tm, 0, sizeof (struct tm));    
525
612
                rb_debug ("unable to convert date string %s", date_str);