2 * Copyright (C) 2006 Andrej Kacian <andrej@kacian.sk>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
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 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
27 #include "feeditemenclosure.h"
31 void feed_parser_rss20_start(void *data, const gchar *el, const gchar **attr)
33 FeedParserCtx *ctx = (FeedParserCtx *)data;
34 FeedItemEnclosure *enclosure = NULL;
35 gchar *url, *type, *size_s;
38 /* ------------------- */
39 if( ctx->depth == 2 ) {
40 if( !strcmp(el, "item") ) { /* Start of new item */
42 if( ctx->curitem != NULL )
43 feed_item_free(ctx->curitem);
45 ctx->curitem = feed_item_new(ctx->feed);
47 } else ctx->location = 0;
48 /* ------------------- */
49 } else if( ctx->depth == 3 ) {
50 if( !strcmp(el, "enclosure") ) { /* Media enclosure */
52 url = feed_parser_get_attribute_value(attr, "url");
53 type = feed_parser_get_attribute_value(attr, "type");
54 size_s = feed_parser_get_attribute_value(attr, "length");
56 size = (gulong)atol(size_s);
58 if( url != NULL && type != NULL && size > 0 ) {
59 if( (enclosure = feed_item_enclosure_new(url, type, size)) )
60 feed_item_set_enclosure(ctx->curitem, enclosure);
63 } else if( !strcmp(el, "guid") ) { /* Unique ID */
64 type = feed_parser_get_attribute_value(attr, "isPermaLink");
65 if( type != NULL && !strcmp(type, "false") )
66 feed_item_set_id_permalink(ctx->curitem, TRUE);
68 } else ctx->location = 0;
74 void feed_parser_rss20_end(void *data, const gchar *el)
76 FeedParserCtx *ctx = (FeedParserCtx *)data;
77 Feed *feed = ctx->feed;
80 if( ctx->str != NULL )
87 switch( ctx->depth ) {
89 /* ------------------- */
92 if( !strcmp(el, "rss") ) {
93 /* we finished parsing the feed */
94 ctx->feed->items = g_slist_reverse(ctx->feed->items);
99 /* ------------------- */
102 break; /* nothing to do at this depth */
104 /* ------------------- */
107 /* decide if we just received </item>, so we can
108 * add a complete item to feed */
109 if( !strcmp(el, "item") ) {
111 /* append the complete feed item, if it is valid
112 * "All elements of an item are optional, however at least one
113 * of title or description must be present." */
114 if( ctx->curitem->title != NULL || ctx->curitem->summary != NULL ) {
116 g_slist_prepend(ctx->feed->items, (gpointer)ctx->curitem);
119 /* since it's in the linked list, lose this pointer */
122 } else if( !strcmp(el, "title") ) { /* so it wasn't end of item */
124 } else if( !strcmp(el, "description" ) ) {
125 FILL(feed->description)
126 } else if( !strcmp(el, "dc:language") ) {
128 } else if( !strcmp(el, "author") ) {
130 } else if( !strcmp(el, "admin:generatorAgent") ) {
131 FILL(feed->generator)
132 } else if( !strcmp(el, "dc:date") ) {
133 feed->date = parseISO8601Date(text);
134 } else if( !strcmp(el, "pubDate") ) {
135 feed->date = parseRFC822Date(text);
140 /* ------------------- */
143 if( ctx->curitem == NULL ) {
147 /* decide which field did we just get */
148 if( !strcmp(el, "title") ) {
149 FILL(ctx->curitem->title)
150 } else if( !strcmp(el, "author") ) {
151 FILL(ctx->curitem->author)
152 } else if( !strcmp(el, "description") ) {
153 FILL(ctx->curitem->summary)
154 } else if( !strcmp(el, "content:encoded") ) {
155 FILL(ctx->curitem->text)
156 } else if( !strcmp(el, "link") ) {
157 FILL(ctx->curitem->url)
158 } else if( !strcmp(el, "guid") ) {
159 FILL(ctx->curitem->id)
160 } else if( !strcmp(el, "wfw:commentRSS") || !strcmp(el, "wfw:commentRss") ) {
161 FILL(ctx->curitem->comments_url)
162 } else if( !strcmp(el, "dc:date") ) {
163 ctx->curitem->date_modified = parseISO8601Date(text);
164 } else if( !strcmp(el, "pubDate") ) {
165 ctx->curitem->date_modified = parseRFC822Date(text);
166 } else if( !strcmp(el, "dc:creator")) {
167 FILL(ctx->curitem->author)
174 if( ctx->str != NULL ) {
175 g_string_free(ctx->str, TRUE);