summaryrefslogtreecommitdiff
path: root/src/yelp-toc-pager.c
blob: 9ee5e22824e11f50d488be16fdc1a3f680d8859c (plain)
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * Copyright (C) 2003 Shaun McCance  <shaunm@gnome.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Shaun McCance  <shaunm@gnome.org>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <glib.h>
#include <libgnome/gnome-i18n.h>
#include <libgnomevfs/gnome-vfs.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>

#include "yelp-error.h"
#include "yelp-toc-pager.h"

#define d(x)

typedef struct _OMF OMF;

struct _YelpTocPagerPriv {
    GSList           *omf_pending;

    GSList           *toc_pending;

    GHashTable       *unique_hash_omf;
    GHashTable       *category_hash_omf;

    xmlParserCtxtPtr  parser;

    gint              pause_count;
    GtkFunction       unpause_func;
};

struct _OMF {
    xmlChar   *title;
    xmlChar   *description;
    xmlChar   *seriesid;

    gchar     *uniqueid;

    xmlChar   *language;
    gint       lang_priority;

    GSList    *categories;

    xmlChar   *omf_file;
    xmlChar   *xml_file;
};

static void          toc_pager_class_init      (YelpTocPagerClass *klass);
static void          toc_pager_init            (YelpTocPager      *pager);
static void          toc_pager_dispose         (GObject           *gobject);

gboolean             toc_pager_process         (YelpPager         *pager);
void                 toc_pager_cancel          (YelpPager         *pager);
gchar *              toc_pager_resolve_uri     (YelpPager         *pager,
						YelpURI           *uri);
const GtkTreeModel * toc_pager_get_sections    (YelpPager         *pager);

static void          toc_read_omf_dir          (YelpTocPager      *pager,
						const gchar       *dirstr);
static gboolean      toc_process_omf_pending   (YelpPager         *pager);
static void          toc_hash_omf              (YelpTocPager      *pager,
						OMF               *omf);
static void          toc_unhash_omf            (YelpTocPager      *pager,
						OMF               *omf);

static gboolean      toc_process_toc           (YelpTocPager      *pager);
static gboolean      toc_process_toc_pending   (YelpTocPager      *pager);

static void          omf_free                  (OMF               *omf);

static YelpPagerClass *parent_class;

static YelpTocPager   *toc_pager;

GType
yelp_toc_pager_get_type (void)
{
    static GType type = 0;

    if (!type) {
	static const GTypeInfo info = {
	    sizeof (YelpTocPagerClass),
	    NULL,
	    NULL,
	    (GClassInitFunc) toc_pager_class_init,
	    NULL,
	    NULL,
	    sizeof (YelpTocPager),
	    0,
	    (GInstanceInitFunc) toc_pager_init,
	};
	type = g_type_register_static (YELP_TYPE_PAGER,
				       "YelpTocPager", 
				       &info, 0);
    }
    return type;
}

static void
toc_pager_class_init (YelpTocPagerClass *klass)
{
    GObjectClass   *object_class = G_OBJECT_CLASS (klass);
    YelpPagerClass *pager_class  = YELP_PAGER_CLASS (klass);

    parent_class = g_type_class_peek_parent (klass);

    object_class->dispose = toc_pager_dispose;

    pager_class->process      = toc_pager_process;
    pager_class->cancel       = toc_pager_cancel;
    pager_class->resolve_uri  = toc_pager_resolve_uri;
    pager_class->get_sections = toc_pager_get_sections;
}

static void
toc_pager_init (YelpTocPager *pager)
{
    YelpTocPagerPriv *priv;

    priv = g_new0 (YelpTocPagerPriv, 1);
    pager->priv = priv;

    priv->parser = xmlNewParserCtxt ();

    priv->unique_hash_omf = g_hash_table_new (g_str_hash, g_str_equal);
    priv->category_hash_omf = g_hash_table_new (g_str_hash, g_str_equal);

    priv->pause_count = 0;
    priv->unpause_func = NULL;
}

static void
toc_pager_dispose (GObject *object)
{
    YelpTocPager *pager = YELP_TOC_PAGER (object);

    g_free (pager->priv);

    G_OBJECT_CLASS (parent_class)->dispose (object);
}

/******************************************************************************/

void
yelp_toc_pager_init (void)
{
    toc_pager = (YelpTocPager *) g_object_new (YELP_TYPE_TOC_PAGER, NULL);

    yelp_pager_start (YELP_PAGER (toc_pager));
}

YelpTocPager *
yelp_toc_pager_get (void)
{
    return toc_pager;
}

void
yelp_toc_pager_pause (YelpTocPager *pager)
{
    pager->priv->pause_count = pager->priv->pause_count + 1;
}

void
yelp_toc_pager_unpause (YelpTocPager *pager)
{
    pager->priv->pause_count = pager->priv->pause_count - 1;

    if (pager->priv->pause_count < 0) {
	g_warning (_("YelpTocPager: Pause count is negative."));
	pager->priv->pause_count = 0;
    }

    if (pager->priv->pause_count < 1 && pager->priv->unpause_func) {
	gtk_idle_add_priority (G_PRIORITY_LOW,
			       pager->priv->unpause_func,
			       pager);
	pager->priv->unpause_func = NULL;
    }
}

/******************************************************************************/

gboolean
toc_pager_process (YelpPager *pager)
{
    toc_read_omf_dir (YELP_TOC_PAGER (pager), DATADIR"/omf");

    gtk_idle_add_priority (G_PRIORITY_LOW,
			   (GtkFunction) toc_process_omf_pending,
			   pager);
    return FALSE;
}

void
toc_pager_cancel (YelpPager *pager)
{
    // FIXME
}

gchar *
toc_pager_resolve_uri (YelpPager *pager, YelpURI *uri)
{
    // FIXME
    return NULL;
}

const GtkTreeModel *
toc_pager_get_sections (YelpPager *pager)
{
    return NULL;
}

/******************************************************************************/

static void
toc_read_omf_dir (YelpTocPager *pager, const gchar *dirstr)
{
    GnomeVFSResult           result;
    GnomeVFSDirectoryHandle *dir;
    GnomeVFSFileInfo        *file_info;

    YelpTocPagerPriv *priv = pager->priv;

    result = gnome_vfs_directory_open (&dir, dirstr,
				       GNOME_VFS_FILE_INFO_DEFAULT);
    if (result != GNOME_VFS_OK)
	return;

    file_info = gnome_vfs_file_info_new ();

    while (gnome_vfs_directory_read_next (dir, file_info) == GNOME_VFS_OK) {
	switch (file_info->type) {
	case GNOME_VFS_FILE_TYPE_DIRECTORY:
	    if (strcmp (file_info->name, ".") && strcmp (file_info->name, "..")) {
		gchar *newdir = g_strconcat (dirstr, "/", file_info->name, NULL);
		toc_read_omf_dir (pager, newdir);
		g_free (newdir);
	    }
	    break;
	case GNOME_VFS_FILE_TYPE_REGULAR:
	case GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK:
	    priv->omf_pending =
		g_slist_prepend (priv->omf_pending,
				 g_strconcat (dirstr, "/", file_info->name, NULL));
	    break;
	default:
	    break;
	}
    }

    gnome_vfs_file_info_unref (file_info);
    gnome_vfs_directory_close (dir);

    while (gtk_events_pending ())
	gtk_main_iteration ();

}

static gboolean
toc_process_omf_pending (YelpPager *pager)
{
    GSList    *first;
    gchar     *file;
    xmlDocPtr  omf_doc = NULL;
    xmlNodePtr omf_cur;
    gint       lang_priority;
    OMF       *omf     = NULL;
    OMF       *omf_old = NULL;
    GList     *langs;

    YelpTocPagerPriv *priv  = YELP_TOC_PAGER (pager)->priv;

    first = priv->omf_pending;
    priv->omf_pending = g_slist_remove_link (priv->omf_pending, first);

    file = (gchar *) first->data;

    if (!g_str_has_suffix (file, ".omf"))
	goto done;

    omf_doc = xmlCtxtReadFile (priv->parser,
			       (const char *) file,
			       NULL, 0);
    if (!omf_doc)
	goto done;

    omf_cur = xmlDocGetRootElement (omf_doc);
    if (!omf_cur)
	goto done;

    for (omf_cur = omf_cur->children; omf_cur; omf_cur = omf_cur->next) {
	if (omf_cur->type == XML_ELEMENT_NODE &&
	    !xmlStrcmp (omf_cur->name, (xmlChar *) "resource"))
	    break;
    }
    if (!omf_cur)
	goto done;

    omf = g_new0 (OMF, 1);
    omf->omf_file = (xmlChar *) g_strdup (file);

    for (omf_cur = omf_cur->children; omf_cur; omf_cur = omf_cur->next) {
	if (omf_cur->type != XML_ELEMENT_NODE)
	    continue;

	if (!xmlStrcmp (omf_cur->name, (xmlChar *) "title")) {
	    if (omf->title)
		xmlFree (omf->title);
	    omf->title = xmlNodeGetContent (omf_cur);
	    continue;
	}
	if (!xmlStrcmp (omf_cur->name, (xmlChar *) "description")) {
	    if (omf->description)
		xmlFree (omf->description);
	    omf->description = xmlNodeGetContent (omf_cur);
	    continue;
	}
	if (!xmlStrcmp (omf_cur->name, (xmlChar *) "relation")) {
	    xmlChar *seriesid =
		xmlGetProp (omf_cur, (const xmlChar *) "seriesid");

	    if (seriesid) {
		if (omf->seriesid)
		    xmlFree (omf->seriesid);
		omf->seriesid = seriesid;
	    }
	}
	if (!xmlStrcmp (omf_cur->name, (xmlChar *) "subject")) {
	    xmlChar *category =
		xmlGetProp (omf_cur, (const xmlChar *) "category");

	    if (category)
		omf->categories = g_slist_prepend (omf->categories,
						   category);
	}
	if (!xmlStrcmp (omf_cur->name, (xmlChar *) "language")) {
	    if (omf->language)
		xmlFree (omf->language);
	    omf->language = xmlGetProp (omf_cur, (const xmlChar *) "code");

	    /* The lang_priority of an OMF file is how early it appears in the
	     * list of preferred languages for the user.  Low numbers are best.
	     */
	    lang_priority = 0;
	    langs = (GList *) gnome_i18n_get_language_list ("LC_MESSAGES");
	    for ( ; langs != NULL; langs = langs->next) {
		gchar *lang = langs->data;
		lang_priority++;

		if (lang == NULL || strchr (lang, '.') != NULL)
		    continue;

		if (!xmlStrcmp ((xmlChar *) lang, omf->language)) {
		    omf->lang_priority = lang_priority;
		    break;
		}
	    }

	    if (!omf->lang_priority) {
		/* If the language didn't match a user-preferred language,
		 * omf->lang_priority doesn't get set, so is 0.
		 */
		omf_free (omf);
		goto done;
	    }
	}
	if (!xmlStrcmp (omf_cur->name, (xmlChar *) "identifier")) {
	    if (omf->xml_file)
		xmlFree (omf->xml_file);
	    omf->xml_file = xmlGetProp (omf_cur, (const xmlChar *) "url");
	}
    }

    if (!omf->seriesid) {
	omf_free (omf);
	goto done;
    }

    /* Do something better for uniqueid, since people can't seem to manage
     * to create a proper seriesid.  Ugh.
     */
    omf->uniqueid = g_strconcat ("seriesid:", (gchar *) omf->seriesid, NULL);

    /* If we have one with the same uniqueid, use the one with
     * the lowest lang_priority.
     */
    omf_old = g_hash_table_lookup (priv->unique_hash_omf, omf->uniqueid);
    if (omf_old) {
	if (omf_old->lang_priority < omf->lang_priority) {
	    omf_free (omf);
	    goto done;
	} else {
	    printf (":: %s %s\n", omf_old->omf_file, omf->omf_file);
	    toc_unhash_omf (YELP_TOC_PAGER (pager), omf_old);
	    omf_free (omf_old);

	    toc_hash_omf (YELP_TOC_PAGER (pager), omf);
	}
    } else {
	toc_hash_omf (YELP_TOC_PAGER (pager), omf);
    }
    
 done:
    xmlFreeDoc (omf_doc);
    g_free (file);
    g_slist_free_1 (first);

    if (!priv->omf_pending) {
	if (priv->pause_count > 0)
	    priv->unpause_func = (GtkFunction) toc_process_toc;
	else
	    gtk_idle_add_priority (G_PRIORITY_LOW,
				   (GtkFunction) toc_process_toc,
				   pager);
	return FALSE;
    }
    if (priv->pause_count > 0) {
	priv->unpause_func = (GtkFunction) toc_process_omf_pending;
	return FALSE;
    }
    else
	return TRUE;
}

static gboolean
toc_process_toc (YelpTocPager *pager)
{
    xmlDocPtr    toc_doc;
    xmlNodePtr   toc_node;
    GError      *error = NULL;

    YelpTocPagerPriv *priv = pager->priv;

    printf ("toc_process_toc\n");
    toc_doc = xmlCtxtReadFile (priv->parser,
			       DATADIR "/yelp/toc.xml",
			       NULL, 0);

    if (!toc_doc) {
	g_set_error (&error,
		     YELP_ERROR,
		     YELP_ERROR_FAILED_TOC,
		     _("The table of contents could not be read."));
	yelp_pager_error (YELP_PAGER (pager), error);
	return FALSE;
    }

    toc_node = xmlDocGetRootElement (toc_doc);

    if (!xmlStrcmp (toc_node->name, "toc")) {
	g_set_error (&error,
		     YELP_ERROR,
		     YELP_ERROR_FAILED_TOC,
		     _("The table of contents could not be read."));
	yelp_pager_error (YELP_PAGER (pager), error);
	return FALSE;
    }

    priv->toc_pending = g_slist_append (priv->toc_pending, toc_node);

    gtk_idle_add_priority (G_PRIORITY_LOW,
			   (GtkFunction) toc_process_toc_pending,
			   pager);

    xmlFreeDoc (toc_doc);
    return FALSE;
}

static gboolean
toc_process_toc_pending (YelpTocPager *pager)
{
    GSList      *first;
    xmlNodePtr   node;
    xmlNodePtr   cur;
    xmlChar     *id;
    gchar       *url;

    YelpTocPagerPriv *priv = pager->priv;

    first = priv->toc_pending;
    priv->toc_pending = g_slist_remove_link (priv->toc_pending, first);

    node = first->data;

    id = xmlGetProp (node, (const xmlChar *) "id");
    url = g_strconcat ("toc:", (gchar *) id, NULL);
    xmlFree (id);

    for (cur = node->children; cur; cur = cur->next) {
	printf ("cur: %s\n", cur->name);
    }

    g_slist_free_1 (first);

    if (!priv->toc_pending) {
	return FALSE;
    }
    else if (priv->pause_count > 0) {
	priv->unpause_func = (GtkFunction) toc_process_toc_pending;
	return FALSE;
    }
    else
	return TRUE;
}

static void
toc_hash_omf (YelpTocPager *pager, OMF *omf)
{
    GSList *category;

    g_hash_table_insert (pager->priv->unique_hash_omf,
			 omf->uniqueid,
			 omf);

    for (category = omf->categories; category; category = category->next) {
	gchar  *catstr = (gchar *) category->data;
	GSList *omfs =
	    (GSList *) g_hash_table_lookup (pager->priv->category_hash_omf,
					    catstr);
	omfs = g_slist_prepend (omfs, omf);

	g_hash_table_insert (pager->priv->category_hash_omf,
			     catstr, omfs);
    }
}

static void
toc_unhash_omf (YelpTocPager *pager, OMF *omf)
{
    GSList *category;

    g_hash_table_remove (pager->priv->unique_hash_omf,
			 omf->uniqueid);

    for (category = omf->categories; category; category = category->next) {
	gchar  *catstr = (gchar *) category->data;
	GSList *omfs =
	    (GSList *) g_hash_table_lookup (pager->priv->category_hash_omf,
					    catstr);
	if (omfs) {
	    omfs = g_slist_remove (omfs, omf);
	    g_hash_table_insert (pager->priv->category_hash_omf,
				 catstr, omfs);
	}
    }
}

static void
omf_free (OMF *omf)
{
    GSList *category;

    if (omf) {
	xmlFree (omf->title);
	xmlFree (omf->description);
	xmlFree (omf->seriesid);
	xmlFree (omf->uniqueid);
	xmlFree (omf->language);

	for (category = omf->categories; category; category = category->next)
	    xmlFree ((xmlChar *) category->data);
	g_slist_free (omf->categories);

	xmlFree (omf->omf_file);
	xmlFree (omf->xml_file);
    }
    g_free (omf);
}