Fixing some navigation wonkiness with error pages
[yelp.git] / src / test-document.c
bloba7adb427a045e64eced6a80c5d9a2a4813ded31b
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
3 * Copyright (C) 2006 Shaun McCance
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Cambridge, MA 02139, USA.
19 * Author: Shaun McCance <shaunm@gnome.org>
22 #include <glib.h>
23 #include <gtk/gtk.h>
24 #include <libxml/parser.h>
25 #include <libxml/xinclude.h>
27 #include "yelp-error.h"
28 #include "yelp-docbook.h"
29 #include "yelp-man.h"
30 #include "yelp-toc.h"
32 static gchar *mode = NULL;
33 static gchar **files = NULL;
34 static const GOptionEntry options[] = {
35 { "mode", 'm',
36 0, G_OPTION_ARG_STRING,
37 &mode,
38 "One of man, docbook or toc", "MODE" },
39 { G_OPTION_REMAINING,
40 0, 0, G_OPTION_ARG_FILENAME_ARRAY,
41 &files, NULL, NULL },
42 { NULL }
45 static void document_func (YelpDocument *document,
46 YelpDocumentSignal signal,
47 gint req_id,
48 gpointer *func_data,
49 gpointer user_data);
51 GMainLoop *loop;
53 static void
54 document_func (YelpDocument *document,
55 YelpDocumentSignal signal,
56 gint req_id,
57 gpointer *func_data,
58 gpointer user_data)
60 gchar contents[60];
61 gchar *contents_;
62 gsize read;
63 YelpPage *page;
64 YelpError *error;
65 switch (signal) {
66 case YELP_DOCUMENT_SIGNAL_PAGE:
67 page = (YelpPage *) func_data;
68 printf ("PAGE: %s (%i)\n", page->id, req_id);
69 printf (" PREV: %s\n", page->prev_id);
70 printf (" NEXT: %s\n", page->next_id);
71 printf (" UP: %s\n", page->up_id);
72 printf (" ROOT: %s\n", page->root_id);
73 printf (" SECTIONS: %s\n", yelp_document_get_sections (document) ? "yep": "nah");
74 yelp_page_read (page, contents, 60, &read, NULL);
75 /* contents isn't \0-terminated */
76 contents_ = g_strndup (contents, read);
77 printf (" DATA: %s\n", contents_);
78 g_free (contents_);
79 yelp_page_free (page);
80 break;
81 case YELP_DOCUMENT_SIGNAL_TITLE:
82 printf ("TITLE: %s (%i)\n", (gchar *) func_data, req_id);
83 g_free (func_data);
84 break;
85 case YELP_DOCUMENT_SIGNAL_ERROR:
86 error = (YelpError *) func_data;
87 printf ("ERROR: %s\n", yelp_error_get_title (error));
88 printf (" %s\n", yelp_error_get_message (error));
89 yelp_error_free (error);
90 break;
94 gint
95 main (gint argc, gchar **argv)
97 GOptionContext *context;
98 YelpDocument *document;
99 gint i;
101 g_thread_init (NULL);
102 gdk_threads_init ();
103 gdk_threads_leave ();
105 gtk_init (&argc, &argv);
107 context = g_option_context_new ("FILE PAGE_IDS...");
108 g_option_context_add_main_entries (context, options, NULL);
109 g_option_context_parse (context, &argc, &argv, NULL);
111 if (!mode)
112 mode = g_strdup ("docbook");
114 if ((files == NULL || files[0] == NULL) && !g_str_equal (mode, "toc")) {
115 g_printerr ("Usage: test-docbook FILE PAGE_IDS...\n");
116 return 1;
119 if (g_str_equal (mode, "man"))
120 document = yelp_man_new (files[0]);
121 else if (g_str_equal (mode, "toc"))
122 document = yelp_toc_new ();
123 else if (g_str_equal (mode, "docbook"))
124 document = yelp_docbook_new (files[0]);
125 else {
126 g_error ("Unknown test. Please try again later\n");
127 return 3;
130 if ((files == NULL || files[1] == NULL) && (!g_str_equal (mode, "toc")))
131 yelp_document_get_page (document, "x-yelp-index", (YelpDocumentFunc) document_func, NULL);
132 else if (!g_str_equal (mode, "toc")) {
133 for (i = 1; files[i]; i++)
134 yelp_document_get_page (document, files[i], (YelpDocumentFunc) document_func, NULL);
135 } else {
136 if (files) {
137 for (i = 0; files[i]; i++)
138 yelp_document_get_page (document, files[i], (YelpDocumentFunc) document_func, NULL);
139 } else {
140 yelp_document_get_page (document, "index", (YelpDocumentFunc) document_func, NULL);
143 loop = g_main_loop_new (NULL, FALSE);
144 g_main_loop_run (loop);
146 gdk_threads_leave ();
148 return 0;