summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikael Hallendal <micke@codefactory.se>2002-05-05 14:08:05 +0000
committerMikael Hallendal <hallski@src.gnome.org>2002-05-05 14:08:05 +0000
commit25fb00fa937216dd36cc74e8452b4254217deeb8 (patch)
treebb3cf8182dcc07183cf169f6957dc2d72b5e1029
parent34f76903f13f33fe8b035aa506bc7f2fbf952545 (diff)
downloadyelp-25fb00fa937216dd36cc74e8452b4254217deeb8.tar.gz
- Now implements jrb's ghelp-uri scheme. - added doc_path-argument so that
2002-05-05 Mikael Hallendal <micke@codefactory.se> * src/yelp-uri.c (uri_get_doc_type): - Now implements jrb's ghelp-uri scheme. - added doc_path-argument so that it doesn't have to be calculated twice when creating a YelpURI. (uri_get_path_from_relative): added/impl. (uri_locate_help_file): added/impl. (uri_locate_help_file_with_lang): added/impl. (yelp_uri_new): removed the GError-argument. * src/test-uri.c (main): - be a GnomeProgram so that it can actually test something :)
-rw-r--r--ChangeLog12
-rw-r--r--src/test-uri.c24
-rw-r--r--src/yelp-uri.c300
-rw-r--r--src/yelp-uri.h16
4 files changed, 259 insertions, 93 deletions
diff --git a/ChangeLog b/ChangeLog
index 6f10958f..920792f2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
2002-05-05 Mikael Hallendal <micke@codefactory.se>
+ * src/yelp-uri.c (uri_get_doc_type):
+ - Now implements jrb's ghelp-uri scheme.
+ - added doc_path-argument so that it doesn't have to be calculated
+ twice when creating a YelpURI.
+ (uri_get_path_from_relative): added/impl.
+ (uri_locate_help_file): added/impl.
+ (uri_locate_help_file_with_lang): added/impl.
+ (yelp_uri_new): removed the GError-argument.
+
+ * src/test-uri.c (main):
+ - be a GnomeProgram so that it can actually test something :)
+
* src/yelp-error.h: added YELP_ERROR_URI_NOT_EXIST
* src/yelp-uri.[ch]:
diff --git a/src/test-uri.c b/src/test-uri.c
index d08cece5..b8e41cc0 100644
--- a/src/test-uri.c
+++ b/src/test-uri.c
@@ -20,29 +20,41 @@
* Author: Mikael Hallendal <micke@codefactory.se>
*/
+#include <config.h>
+
+#include <libgnome/gnome-init.h>
+#include <libgnome/gnome-program.h>
#include <libgnomevfs/gnome-vfs.h>
+
+
#include "yelp-uri.h"
int
main (int argc, char **argv)
{
- YelpURI *uri;
- GError *error = NULL;
+ GnomeProgram *program;
+ YelpURI *uri;
if (argc < 2) {
g_print ("Usage: test-uri uri\n");
return 1;
}
+ program = gnome_program_init (PACKAGE, VERSION,
+ LIBGNOME_MODULE,
+ argc, argv,
+ GNOME_PROGRAM_STANDARD_PROPERTIES,
+ NULL);
+
gnome_vfs_init ();
- uri = yelp_uri_new (argv[1], &error);
+ uri = yelp_uri_new (argv[1]);
- if (error) {
- g_print ("Error: %s\n", error->message);
+ if (!yelp_uri_exists (uri)) {
+ g_print ("URI (%s) does not exist\n", argv[1]);
return 1;
- }
+ }
g_print ("URI_TYPE : %d\n", yelp_uri_get_type (uri));
g_print ("URI_PATH : %s\n", yelp_uri_get_path (uri));
diff --git a/src/yelp-uri.c b/src/yelp-uri.c
index 130944e4..a7aa0226 100644
--- a/src/yelp-uri.c
+++ b/src/yelp-uri.c
@@ -24,12 +24,13 @@
#include <libgnomevfs/gnome-vfs.h>
#include <libgnomevfs/gnome-vfs-mime-utils.h>
#include <libgnome/gnome-i18n.h>
+#include <libgnome/gnome-program.h>
#include "yelp-error.h"
#include "yelp-util.h"
#include "yelp-uri.h"
-#define d(x) x
+#define d(x)
struct _YelpURI {
YelpURIType type;
@@ -38,14 +39,49 @@ struct _YelpURI {
gint ref_count;
};
-static YelpURIType uri_get_doc_type (const gchar *str_uri);
-static gchar * uri_get_doc_path (const gchar *str_uri,
- GError **error);
-static gchar * uri_get_doc_section (const gchar *str_uri);
-static gchar * uri_get_absolute_path (const gchar *path);
+static gchar * uri_get_doc_path (const gchar *str_uri);
+static YelpURIType uri_get_doc_type (const gchar *str_uri,
+ const gchar *doc_path);
+static gchar * uri_get_doc_section (const gchar *str_uri);
+static gchar * uri_get_path_from_ghelp_uri (const gchar *path);
+static gchar * uri_get_path_from_relative (const gchar *path);
+static gchar * uri_locate_help_file (const gchar *path,
+ const gchar *file_name);
+static gchar * uri_locate_help_file_with_lang (const gchar *path,
+ const gchar *file_name,
+ const gchar *locate);
+
+static gchar *
+uri_get_doc_path (const gchar *str_uri)
+{
+ gchar *no_anchor_uri;
+ gchar *ret_val = NULL;
+ const gchar *ch = NULL;
+
+ /* remove the anchor from the doc path */
+ if ((ch = strchr (str_uri, '?')) || (ch = strchr (str_uri, '#'))) {
+ no_anchor_uri = g_strndup (str_uri, ch - str_uri);
+ } else {
+ no_anchor_uri = g_strdup (str_uri);
+ }
+
+ if (!g_ascii_strncasecmp (no_anchor_uri, "man:", 4)) {
+ ret_val = g_strdup (no_anchor_uri + 4);
+ }
+ else if (!g_ascii_strncasecmp (no_anchor_uri, "info:", 5)) {
+ ret_val = g_strdup (no_anchor_uri + 5);
+ }
+ else if (!g_ascii_strncasecmp (no_anchor_uri, "ghelp:", 6)) {
+ ret_val = uri_get_path_from_ghelp_uri (no_anchor_uri + 6);
+ }
+
+ g_free (no_anchor_uri);
+
+ return ret_val;
+}
static YelpURIType
-uri_get_doc_type (const gchar *str_uri)
+uri_get_doc_type (const gchar *str_uri, const gchar *doc_path)
{
YelpURIType ret_val = YELP_URI_TYPE_UNKNOWN;
@@ -59,9 +95,13 @@ uri_get_doc_type (const gchar *str_uri)
gchar *mime_type = NULL;
gchar *docpath;
- docpath = uri_get_doc_path (str_uri, NULL);
-
- if (!docpath) {
+ if (doc_path) {
+ docpath = (gchar *) doc_path;
+ } else {
+ docpath = uri_get_doc_path (str_uri);
+ }
+
+ if (!g_file_test (docpath, G_FILE_TEST_EXISTS)) {
return YELP_URI_TYPE_NON_EXISTENT;
}
@@ -81,48 +121,12 @@ uri_get_doc_type (const gchar *str_uri)
g_free (mime_type);
}
- g_free (docpath);
- }
-
- return ret_val;
-}
-
-static gchar *
-uri_get_doc_path (const gchar *str_uri, GError **error)
-{
- gchar *no_anchor_uri;
- gchar *ret_val = NULL;
- const gchar *ch = NULL;
-
- /* remove the anchor from the doc path */
- if ((ch = strchr (str_uri, '?')) || (ch = strchr (str_uri, '#'))) {
- no_anchor_uri = g_strndup (str_uri, ch - str_uri);
- } else {
- no_anchor_uri = g_strdup (str_uri);
- }
-
- if (!g_ascii_strncasecmp (no_anchor_uri, "man:", 4)) {
- ret_val = g_strdup (no_anchor_uri + 4);
- }
- else if (!g_ascii_strncasecmp (no_anchor_uri, "info:", 5)) {
- ret_val = g_strdup (no_anchor_uri + 5);
- }
- else if (!g_ascii_strncasecmp (no_anchor_uri, "ghelp:", 6)) {
- ret_val = uri_get_absolute_path (no_anchor_uri + 6);
-
- if (!g_file_test (ret_val, G_FILE_TEST_EXISTS)) {
- g_set_error (error,
- YELP_ERROR,
- YELP_ERROR_URI_NOT_EXIST,
- _("%s does not exist."), str_uri);
- g_free (ret_val);
- ret_val = NULL;
+ if (docpath != doc_path) {
+ g_free (docpath);
}
}
-
- g_free (no_anchor_uri);
- return ret_val;
+ return ret_val;
}
static gchar *
@@ -138,7 +142,7 @@ uri_get_doc_section (const gchar *str_uri)
}
static gchar *
-uri_get_absolute_path (const gchar *path)
+uri_get_path_from_ghelp_uri (const gchar *path)
{
gchar *ret_val = NULL;
gchar *work_path;
@@ -148,6 +152,7 @@ uri_get_absolute_path (const gchar *path)
g_strstrip (work_path);
if (path[0] == '/') {
+ /* Absolute URL */
gint i = 1;
gint len = strlen (work_path);
@@ -155,59 +160,175 @@ uri_get_absolute_path (const gchar *path)
++i;
}
- /* check_xml_promotion ? */
+ /* Should we always try to use xml here even if full *
+ * path to a sgml or html document is given? */
ret_val = g_strdup (work_path + (i - 1));
} else {
-/*
- * 1: ghelp:nautilus
- * 2: ghelp:AisleRiot2/Klondike
- */
+ ret_val = uri_get_path_from_relative (path);
}
g_free (work_path);
return ret_val;
- /* ... implement jrb's uri scheme */
}
-YelpURIReader *
-yelp_uri_reader_new (YelpURIReaderOpenCallback open_cb,
- YelpURIReaderReadCallback read_cb,
- YelpURIReaderCloseCallback close_cb,
- gpointer user_data)
+static gchar *
+uri_get_path_from_relative (const gchar *path)
{
- YelpURIReader *reader;
-
- reader = g_new0 (YelpURIReader, 1);
-
- reader->open_callback = open_cb;
- reader->read_callback = read_cb;
- reader->close_callback = close_cb;
- reader->user_data = user_data;
-
- return reader;
+ GnomeProgram *program;
+ GSList *ret_locations = NULL;
+ GSList *node;
+ gchar *doc_id;
+ gchar *file_name;
+ gchar *ret_val = NULL;
+ const gchar *ch;
+
+ program = gnome_program_get ();
+
+ if ((ch = strchr (path, '/'))) {
+ /* 2: ghelp:AisleRiot2/Klondike */
+ doc_id = g_strndup (path, ch - path);
+ file_name = g_strdup (ch + 1);
+ } else {
+ /* 1: ghelp:nautilus */
+ doc_id = (gchar *)path;
+ file_name = (gchar *)path;
+ }
+
+ gnome_program_locate_file (program,
+ GNOME_FILE_DOMAIN_HELP,
+ doc_id,
+ FALSE,
+ &ret_locations);
+
+ if (!ret_locations) {
+ return NULL;
+ }
+
+ for (node = ret_locations; node; node = node->next) {
+ gchar *help_path = node->data;
+
+ d(g_print ("PATH: %s\n", help_path));
+
+ ret_val = uri_locate_help_file (help_path, file_name);
+
+ if (ret_val) {
+ break;
+ }
+ }
+
+ g_slist_foreach (ret_locations, (GFunc) g_free, NULL);
+ g_slist_free (ret_locations);
+
+ d(g_print ("Absolute path: %s\n", ret_val));
+
+ if (doc_id != path) {
+ g_free (doc_id);
+ }
+
+ if (file_name != path) {
+ g_free (file_name);
+ }
+
+ return ret_val;
+}
+
+static gchar *
+uri_locate_help_file (const gchar *path, const gchar *file_name)
+{
+ gchar *ret_val;
+ const GList *lang_list;
+
+ lang_list = gnome_i18n_get_language_list ("LC_MESSAGES");
+
+ for (;lang_list != NULL; lang_list = lang_list->next) {
+ const gchar *lang = lang_list->data;
+
+ d(g_print ("lang: %s\n", lang));
+
+ /* This has to be a valid language AND a language with
+ * no encoding postfix. The language will come up without
+ * encoding next */
+ if (lang == NULL || strchr (lang, '.') != NULL) {
+ continue;
+ }
+
+ ret_val = uri_locate_help_file_with_lang (path, file_name,
+ lang);
+
+ if (!ret_val) {
+ /* Check for index file in wanted locale */
+ ret_val = uri_locate_help_file_with_lang (path,
+ "index",
+ lang);
+ }
+
+ if (ret_val) {
+ return ret_val;
+ }
+ }
+
+ /* Look in C locale since that exists for almost all documents */
+ ret_val = uri_locate_help_file_with_lang (path, file_name, "C");
+
+ if (ret_val) {
+ return ret_val;
+ }
+
+ /* Last chance, look for index-file with C lang */
+ return uri_locate_help_file_with_lang (path, "index", "C");
+}
+
+static gchar *
+uri_locate_help_file_with_lang (const gchar *path,
+ const gchar *file_name,
+ const gchar *lang)
+{
+ gchar *exts[] = {".xml", ".docbook", ".sgml", ".html", "", NULL};
+ gint i;
+
+ for (i = 0; exts[i] != NULL; i++) {
+ gchar *full;
+
+ full = g_strconcat (path, "/", lang, "/",
+ file_name, exts[i], NULL);
+
+ if (g_file_test (full, G_FILE_TEST_EXISTS)) {
+ return full;
+ }
+
+ g_free (full);
+ }
+
+ return NULL;
}
YelpURI *
-yelp_uri_new (const gchar *str_uri, GError **error)
+yelp_uri_new (const gchar *str_uri)
{
YelpURI *uri;
- GError *tmp_error = NULL;
uri = g_new0 (YelpURI, 1);
- uri->type = uri_get_doc_type (str_uri);
- uri->path = uri_get_doc_path (str_uri, &tmp_error);
+ uri->path = uri_get_doc_path (str_uri);
+ uri->type = uri_get_doc_type (str_uri, uri->path);
uri->section = uri_get_doc_section (str_uri);
uri->ref_count = 1;
- if (tmp_error) {
- g_propagate_error (error, tmp_error);
- g_error_free (tmp_error);
+ return uri;
+}
+
+gboolean
+yelp_uri_exists (YelpURI *uri)
+{
+ g_return_val_if_fail (uri != NULL, FALSE);
+
+ if (!uri->path) {
+ return FALSE;
}
- return uri;
+ return g_file_test (uri->path, G_FILE_TEST_EXISTS);
}
YelpURIType
@@ -244,7 +365,7 @@ yelp_uri_read (YelpURI *uri, YelpURIReader *reader, GError **error)
gboolean
yelp_uri_read_async (YelpURI *uri, YelpURIReader *reader, GError **error)
{
- /* For now read in a g_idle */
+ /* For now read in a g_idle and later in own thread/gnome-vfs-async */
return TRUE;
}
@@ -271,3 +392,22 @@ yelp_uri_unref (YelpURI *uri)
g_free (uri);
}
}
+
+YelpURIReader *
+yelp_uri_reader_new (YelpURIReaderOpenCallback open_cb,
+ YelpURIReaderReadCallback read_cb,
+ YelpURIReaderCloseCallback close_cb,
+ gpointer user_data)
+{
+ YelpURIReader *reader;
+
+ reader = g_new0 (YelpURIReader, 1);
+
+ reader->open_callback = open_cb;
+ reader->read_callback = read_cb;
+ reader->close_callback = close_cb;
+ reader->user_data = user_data;
+
+ return reader;
+}
+
diff --git a/src/yelp-uri.h b/src/yelp-uri.h
index 492f84bd..e4d5f970 100644
--- a/src/yelp-uri.h
+++ b/src/yelp-uri.h
@@ -54,13 +54,8 @@ typedef struct {
gpointer user_data;
} YelpURIReader;
-YelpURIReader * yelp_uri_reader_new (YelpURIReaderOpenCallback open_cb,
- YelpURIReaderReadCallback read_cb,
- YelpURIReaderCloseCallback close_cb,
- gpointer user_data);
-
-YelpURI * yelp_uri_new (const gchar *str_uri,
- GError **error);
+YelpURI * yelp_uri_new (const gchar *str_uri);
+gboolean yelp_uri_exists (YelpURI *uri);
YelpURIType yelp_uri_get_type (YelpURI *uri);
const gchar * yelp_uri_get_path (YelpURI *uri);
@@ -77,4 +72,11 @@ gboolean yelp_uri_read_async (YelpURI *uri,
void yelp_uri_ref (YelpURI *uri);
void yelp_uri_unref (YelpURI *uri);
+
+/* Convenience function for creating a Reader-struct. */
+YelpURIReader * yelp_uri_reader_new (YelpURIReaderOpenCallback open_cb,
+ YelpURIReaderReadCallback read_cb,
+ YelpURIReaderCloseCallback close_cb,
+ gpointer user_data);
+
#endif /* __YELP_URI_H__ */