aboutsummaryrefslogblamecommitdiffstats
path: root/my-evolution/e-summary-rdf.c
blob: 96ee50c11de33d89711246a1e856c55e7659e4a9 (plain) (tree)
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440























































































































































































































































































































































































































































                                                                                                                                       
/*
 * e-summary-rdf.c: RDF summary bit.
 *
 * Copyright (C) 2001 Ximian, Inc.
 *
 * Authors: Iain Holmes  <iain@ximian.com>
 *          
 * Based on code by Alan Cox
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>
#include <gnome-xml/parser.h>
#include <gnome-xml/xmlmemory.h>

#include <gal/widgets/e-unicode.h>
#include <libgnomevfs/gnome-vfs.h>
#include "e-summary.h"

struct _ESummaryRDF {
    GList *rdfs;

    char *html;
};

typedef struct _RDF {
    char *uri;
    char *html;
    GnomeVFSAsyncHandle *handle;
    GString *string;
    char *buffer;

    xmlDocPtr cache;
    ESummary *summary;

    gboolean shown;
} RDF;

int xmlSubstituteEntitiesDefaultValue = 1;
static int wipe_trackers = FALSE;

char *
e_summary_rdf_get_html (ESummary *summary)
{
    GList *rdfs;
    char *html;
    GString *string;

    if (summary->rdf == NULL) {
        return NULL;
    }

    string = g_string_new ("");
    for (rdfs = summary->rdf->rdfs; rdfs; rdfs = rdfs->next) {
        if (((RDF *)rdfs->data)->html == NULL) {
            continue;
        }

        g_string_append (string, ((RDF *)rdfs->data)->html);
    }

    html = string->str;
    g_string_free (string, FALSE);
    return html;
}

/************ RDF Parser *******************/

static char *
layer_find (xmlNodePtr node, 
        char *match, 
        char *fail)
{
    while (node!=NULL) {
#ifdef RDF_DEBUG
        xmlDebugDumpNode (stdout, node, 32);
        printf("%s.\n", node->name);
#endif
        if (strcasecmp (node->name, match)==0) {
            if (node->childs != NULL && node->childs->content != NULL) {
                return node->childs->content;
            } else {
                return fail;
            }
        }
        node = node->next;
    }
    return fail;
}

static char *
layer_find_url (xmlNodePtr node, 
        char *match, 
        char *fail)
{
    char *p = layer_find (node, match, fail);
    char *r = p;
    static char *wb = NULL;
    char *w;
    
    if (wb) {
        g_free (wb);
    }
    
    wb = w = g_malloc (3 * strlen (p));
    
    if (*r == ' ') r++; /* Fix UF bug */

    while (*r) {
        if (memcmp (r, "&amp;", 5) == 0) {
            *w++ = '&';
            r += 5;
            continue;
        }
        if (memcmp (r, "&lt;", 4) == 0) {
            *w++ = '<';
            r += 4;
            continue;
        }
        if (memcmp (r, "&gt;", 4) == 0) {
            *w++ = '>';
            r += 4;
            continue;
        }
        if (*r == '"' || *r == ' '){
            *w++ = '%';
            *w++ = "0123456789ABCDEF"[*r/16];
            *w++ = "0123456789ABCDEF"[*r&15];
            r++;
            continue;
        }
        *w++ = *r++;
    }
    *w = 0;
    return wb;
}

static void 
tree_walk (xmlNodePtr root,
       RDF *r,
       GString *html)
{
    xmlNodePtr walk;
    xmlNodePtr rewalk = root;
    xmlNodePtr channel = NULL;
    xmlNodePtr image = NULL;
    xmlNodePtr item[16];
    int items = 0;
    int limit = 10;
    int i;
    char *t, *u;
    char *tmp;

    /* FIXME: Need arrows */
    if (r->shown == FALSE) {
        char *p;

        /* FIXME: Hash table & UID */
        p = g_strdup_printf ("<font size=\"-2\"><a href=\"rdf://%d\">(+)</a></font> ", r);
        g_string_append (html, p);
        g_free (p);
    } else {
        char *p;

        /* FIXME: Hash table & UID */
        p = g_strdup_printf ("<font size=\"-2\"><a href=\"rdf://%d\">(-)</a></font>", r);
        g_string_append (html, p);
        g_free (p);
    }
    
    do {
        walk = rewalk;
        rewalk = NULL;
        
        while (walk!=NULL){
#ifdef RDF_DEBUG
            printf ("%p, %s\n", walk, walk->name);
#endif
            if (strcasecmp (walk->name, "rdf") == 0) {
                rewalk = walk->childs;
                walk = walk->next;
                continue;
            }
            if (strcasecmp (walk->name, "rss") == 0){
                rewalk = walk->childs;
                walk = walk->next;
                continue;
            }
            /* This is the channel top level */
#ifdef RDF_DEBUG
            printf ("Top level '%s'.\n", walk->name);
#endif
            if (strcasecmp (walk->name, "channel") == 0) {
                channel = walk;
                rewalk = channel->childs;
            }
            if (strcasecmp (walk->name, "image") == 0) {
                image = walk;
                g_print ("Image\n");
            }
            if (strcasecmp (walk->name, "item") == 0 && items < 16) {
                item[items++] = walk;
            }
            walk = walk->next;
        }
    }
    while (rewalk);
    
    if (channel == NULL) {
        fprintf(stderr, "No channel definition.\n");
        return;
    }

    t = layer_find(channel->childs, "title", "");
    u = layer_find(channel->childs, "link", "");

    if (*u != '\0') {
        char *full;

        full = g_strdup_printf ("<a href=\"%s\">", u);
        g_string_append (html, full);
    }
    g_string_append (html, e_utf8_from_locale_string (t));
    if (*u != '\0') {
        g_string_append (html, "</a>");
    }
    g_string_append (html, "</b></dt>");

    if (r->shown == FALSE) {
        return;
    }

    g_string_append (html, "<ul>");

    items = MIN (limit, items);
    for (i = 0; i < items; i++) {
        char *p = layer_find (item[i]->childs, "title", "No information");
        
        if (wipe_trackers) {
            char *p = layer_find_url (item[i]->childs, "link", "");
            char *x = strchr (p, '?');
            unsigned char *r, *w;
            int n;
            if (x == NULL)
                continue;
            x++;
            r = x;
            w = x;
            while (*r) {
                if (*r == '+') {
                    *w++ = ' ';
                } else if (*r == '%') {
                    sscanf (r+1, "%02x", &n);
                    *w++ = n;
                    r += 2;
                } else {
                    *w++ = *r;
                    }
                r++;
            }
            *w = 0;
            tmp = g_strdup_printf ("<LI><font size=\"-1\"><A href=\"%s\">\n", x+4);
            g_string_append (html, tmp);
            g_free (tmp);
        }
        else {
            tmp = g_strdup_printf ("<LI><font size=\"-1\"><A href=\"%s\">\n", layer_find_url(item[i]->childs, "link", ""));
            g_string_append (html, tmp);
            g_free (tmp);
        }
        
        tmp = g_strdup_printf ("%s\n</A></font></li>", e_utf8_from_locale_string (p));
        g_string_append (html, tmp);
        g_free (tmp);
    }
    g_string_append (html, "</UL></dl>");
}

static void
display_doc (RDF *r)
{
    GString *html;

    html = g_string_new ("<dl><dt><img src=\"ico-rdf.png\" align=\"middle\" "
                 "width=\"48\" height=\"48\"><b>");

    tree_walk (r->cache->root, r, html);

    if (r->html != NULL) {
        g_free (r->html);
    }
    r->html = html->str;
    g_string_free (html, FALSE);

    e_summary_draw (r->summary);
}

static void
close_callback (GnomeVFSAsyncHandle *handle,
        GnomeVFSResult result,
        RDF *r)
{
    char *xml;
    xmlDocPtr doc;

    if (r->handle == NULL) {
        g_free (r->buffer);
        g_string_free (r->string, TRUE);
        return;
    }

    r->handle = NULL;
    g_free (r->buffer);
    xml = r->string->str;
    g_string_free (r->string, FALSE);

    if (r->cache != NULL) {
        xmlFreeDoc (r->cache);
    }

    doc = xmlParseMemory (xml, strlen (xml));
    if (doc == NULL) {
        if (r->html != NULL) {
            g_free (r->html);
        }
        r->html = g_strdup ("<b>Error parsing XML</b>");

        e_summary_draw (r->summary);
        g_free (xml);
        return;
    }

    g_free (xml);
    r->cache = doc;

    /* Draw it */
    display_doc (r);
}

static void
read_callback (GnomeVFSAsyncHandle *handle,
           GnomeVFSResult result,
           gpointer buffer,
           GnomeVFSFileSize bytes_requested,
           GnomeVFSFileSize bytes_read,
           RDF *r)
{
    if (result != GNOME_VFS_OK && result != GNOME_VFS_ERROR_EOF) {
        r->html = g_strdup ("<b>Error downloading RDF</b>");

        e_summary_draw (r->summary);
        r->handle = NULL;
        gnome_vfs_async_close (handle, 
                       (GnomeVFSAsyncCloseCallback) close_callback, r);
        return;
    }

    if (bytes_read == 0) {
        gnome_vfs_async_close (handle,
                       (GnomeVFSAsyncCloseCallback) close_callback, r);
    } else {
        *((char *) buffer + bytes_read) = 0;
        g_string_append (r->string, (const char *) buffer);
        gnome_vfs_async_read (handle, buffer, 4095,
                      (GnomeVFSAsyncReadCallback) read_callback, r);
    }
}

static void
open_callback (GnomeVFSAsyncHandle *handle,
           GnomeVFSResult result,
           RDF *r)
{
    if (result != GNOME_VFS_OK) {
        r->html = g_strdup ("<b>Error downloading RDF</b>");

        e_summary_draw (r->summary);
        return;
    }

    r->string = g_string_new ("");
    r->buffer = g_new (char, 4096);

    gnome_vfs_async_read (handle, r->buffer, 4095,
                  (GnomeVFSAsyncReadCallback) read_callback, r);
}

static void
e_summary_rdf_add_uri (ESummary *summary,
               const char *uri)
{
    RDF *r;

    r = g_new0 (RDF, 1);
    r->summary = summary;
    r->uri = g_strdup (uri);
    r->shown = TRUE;
    summary->rdf->rdfs = g_list_prepend (summary->rdf->rdfs, r);

    gnome_vfs_async_open (&r->handle, r->uri, GNOME_VFS_OPEN_READ,
                  (GnomeVFSAsyncOpenCallback) open_callback, r);
}

static void
e_summary_rdf_protocol (ESummary *summary,
            const char *uri,
            void *closure)
{
    RDF *r;
    int a;

    a = atoi (uri + 6);
    if (a == 0) {
        g_warning ("A == 0");
        return;
    }

    r = (RDF *) GINT_TO_POINTER (a);
    r->shown = !r->shown;

    display_doc (r);
}

void
e_summary_rdf_init (ESummary *summary)
{
    ESummaryRDF *rdf;

    g_return_if_fail (summary != NULL);
    g_return_if_fail (IS_E_SUMMARY (summary));

    rdf = g_new0 (ESummaryRDF, 1);
    summary->rdf = rdf;

    e_summary_add_protocol_listener (summary, "rdf", e_summary_rdf_protocol, rdf);
    e_summary_rdf_add_uri (summary, "http://news.gnome.org/gnome-news/rdf");
    return;
}