~midori/midori/win32theme

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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 Copyright (C) 2009-2010 Dale Whittaker <dayul@users.sf.net>

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 See the file COPYING for the full license text.
*/

#include "feed-atom.h"

static gboolean
atom_is_valid (FeedParser* fparser)
{
    xmlNodePtr node;

    node = fparser->node;

    if (!(xmlStrcmp (node->name, BAD_CAST "feed")) &&
        !(xmlStrcmp (node->ns->href, BAD_CAST "http://www.w3.org/2005/Atom"))
       )
        return TRUE;

    return FALSE;
}

static gboolean
atom_update (FeedParser* fparser)
{
    xmlNodePtr node;
    xmlNodePtr child;
    gint64 date;
    gint64 newdate;

    date = katze_item_get_added (fparser->item);

    node = fparser->node;
    child = node->children;
    while (child)
    {
        if (child->type == XML_ELEMENT_NODE)
        {
            if (!(xmlStrcmp (child->name, BAD_CAST "updated")))
            {
                fparser->node = child;
                newdate = feed_get_element_date (fparser);
                fparser->node = node;
                return (date != newdate);
            }
        }
        child = child->next;
    }
    return TRUE;
}

static gboolean
atom_preferred_link (const gchar* old,
                     const gchar* new)
{
    guint i;
    gint iold;
    gint inew;
    gchar* rels[5] =
    {
        "enclosure",
        "via",
        "related",
        "alternate",
        "self",
    };

    iold = inew = -1;
    for (i = 0; i < 5; i++)
    {
        if (old && g_str_equal (old, rels[i]))
            iold = i;
        if (new && g_str_equal (new, rels[i]))
            inew = i;
    }
    return (inew > iold);
}

static void
atom_get_link (KatzeItem* item,
               xmlNodePtr node)
{
    gchar* oldtype;
    gchar* newtype;
    gchar* oldrel;
    gchar* newrel;
    gchar* href;
    gboolean oldishtml;
    gboolean newishtml;
    gboolean newlink;

    newlink = FALSE;
    oldtype = (gchar*)katze_item_get_meta_string (item, "feedpanel:linktype");
    oldrel = (gchar*)katze_item_get_meta_string (item, "feedpanel:linkrel");

    newtype = (gchar*)xmlGetProp (node, BAD_CAST "type");
    newrel = (gchar*)xmlGetProp (node, BAD_CAST "rel");
    href = (gchar*)xmlGetProp (node, BAD_CAST "href");

    if (!newrel)
        newrel = g_strdup ("alternate");

    oldishtml = (oldtype && g_str_equal (oldtype, "text/html"));
    newishtml = (newtype && g_str_equal (newtype, "text/html"));

    /* prefer HTML links over anything else.
     * if the previous link was already HTML, decide which link
     * we prefer.
     */
    if ((newishtml && oldishtml) || (!newishtml && !oldishtml))
        newlink = atom_preferred_link (oldrel, newrel);
    else
        newlink = newishtml;

    if (newlink)
    {
        katze_item_set_uri (item, href);
        katze_item_set_meta_string (item, "feedpanel:linkrel", newrel);
        katze_item_set_meta_string (item, "feedpanel:linktype", newtype);
    }

    xmlFree (href);
    xmlFree (newrel);
    xmlFree (newtype);
}

static void
atom_preparse_entry (FeedParser* fparser)
{
    fparser->item = katze_item_new ();
}

static void
atom_parse_entry (FeedParser* fparser)
{
    xmlNodePtr node;
    gchar* content;
    gint64 date;

    node = fparser->node;
    content = NULL;

    if (!xmlStrcmp (node->name, BAD_CAST "id"))
    {
        content = feed_get_element_string (fparser);
        katze_item_set_token (fparser->item, content);
    }
    else if (!xmlStrcmp (node->name, BAD_CAST "title"))
    {
        content = feed_get_element_string (fparser);
        katze_item_set_name (fparser->item, content);
    }
    else if (!xmlStrcmp (node->name, BAD_CAST "summary"))
    {
        content = feed_get_element_markup (fparser);
        katze_item_set_text (fparser->item, content);
    }
    else if (!xmlStrcmp (node->name, BAD_CAST "updated"))
    {
        date = feed_get_element_date (fparser);
        katze_item_set_added (fparser->item, date);
    }
    else if (!xmlStrcmp (node->name, BAD_CAST "icon"))
    {
        content = feed_get_element_string (fparser);
        katze_item_set_icon (fparser->item, content);
    }
    /* FIXME content can be used in some cases where there
     * is no summary, but it needs additional work,
     * as it can be HTML, or base64 encoded.
     * see the spec.
     */
    else if (!xmlStrcmp (node->name, BAD_CAST "content"))
    {
        /* Only retrieve content if there is no summary */
        if (!katze_item_get_text (fparser->item))
        {
            content = feed_get_element_markup (fparser);
            katze_item_set_text (fparser->item, content);
        }
    }
    else if (!(xmlStrcmp (node->name, BAD_CAST "link")))
        atom_get_link (fparser->item, node);

    g_free (content);
}

static void
atom_postparse_entry (FeedParser* fparser)
{
    if (!*fparser->error)
    {
        /*
        * Verify that the required Atom elements are added
        * (as per the spec)
        */
        if (!katze_item_get_token (fparser->item) ||
            !katze_item_get_name (fparser->item) ||
            !katze_item_get_uri (fparser->item) ||
            !katze_item_get_added (fparser->item))
        {
            feed_parser_set_error (fparser, FEED_PARSE_ERROR_MISSING_ELEMENT,
                                   _("Failed to find required Atom \"entry\" elements in XML data."));
        }
    }

    if (KATZE_IS_ITEM (fparser->item))
    {
        katze_item_set_meta_string (fparser->item, "feedpanel:linkrel", NULL);
        katze_item_set_meta_string (fparser->item, "feedpanel:linktype", NULL);

        if (*fparser->error)
        {
            g_object_unref (fparser->item);
            fparser->item = NULL;
        }
    }
}

static void
atom_parse_feed (FeedParser* fparser)
{
    FeedParser* eparser;
    xmlNodePtr node;
    gchar* content;
    gint64 date;

    node = fparser->node;
    content = NULL;

    if (!xmlStrcmp (node->name, BAD_CAST "id"))
    {
        content = feed_get_element_string (fparser);
        katze_item_set_token (fparser->item, content);
    }
    else if (!xmlStrcmp (node->name, BAD_CAST "title"))
    {
        content = feed_get_element_string (fparser);
        katze_item_set_name (fparser->item, content);
    }
    else if (!xmlStrcmp (node->name, BAD_CAST "subtitle"))
    {
        content = feed_get_element_markup (fparser);
        katze_item_set_text (fparser->item, content);
    }
    else if (!xmlStrcmp (node->name, BAD_CAST "updated"))
    {
        date = feed_get_element_date (fparser);
        katze_item_set_added (fparser->item, date);
    }
    else if (!xmlStrcmp (node->name, BAD_CAST "icon"))
    {
        content = feed_get_element_string (fparser);
        katze_item_set_icon (fparser->item, content);
    }
    else if (!(xmlStrcmp (node->name, BAD_CAST "link")))
    {
        atom_get_link (fparser->item, node);
    }
    else if (!xmlStrcmp (node->name, BAD_CAST "entry"))
    {
        eparser = g_new0 (FeedParser, 1);
        eparser->doc = fparser->doc;
        eparser->node = fparser->node;
        eparser->error = fparser->error;
        eparser->preparse = atom_preparse_entry;
        eparser->parse = atom_parse_entry;
        eparser->postparse = atom_postparse_entry;

        feed_parse_node (eparser);

        if (KATZE_IS_ITEM (eparser->item))
        {
            KatzeItem* item;
            if (!(item = feed_item_exists (KATZE_ARRAY (fparser->item), eparser->item)))
                katze_array_add_item (KATZE_ARRAY (fparser->item), eparser->item);
            else
            {
                g_object_unref (eparser->item);
                katze_array_move_item (KATZE_ARRAY (fparser->item), item, 0);
            }
        }
        g_free (eparser);

    }
    g_free (content);
}

static void
atom_postparse_feed (FeedParser* fparser)
{
    if (KATZE_IS_ARRAY (fparser->item))
    {
        katze_item_set_meta_string (fparser->item, "feedpanel:linkrel", NULL);
        katze_item_set_meta_string (fparser->item, "feedpanel:linktype", NULL);
    }

    if (!*fparser->error)
    {
        /*
         * Verify that the required Atom elements are added
         * (as per the spec)
         */
        if (!katze_item_get_token (fparser->item) ||
            !katze_item_get_name (fparser->item) ||
            !katze_item_get_added (fparser->item))
        {
            feed_parser_set_error (fparser, FEED_PARSE_ERROR_MISSING_ELEMENT,
                                   _("Failed to find required Atom \"feed\" elements in XML data."));
        }
    }
}

FeedParser*
atom_init_parser (void)
{
    FeedParser* fparser;

    fparser = g_new0 (FeedParser, 1);
    g_return_val_if_fail (fparser, NULL);

    fparser->isvalid = atom_is_valid;
    fparser->update = atom_update;
    fparser->parse = atom_parse_feed;
    fparser->postparse = atom_postparse_feed;

    return fparser;
}