diff options
author | Don Scorgie <dscorgie@src.gnome.org> | 2007-06-11 18:50:09 +0000 |
---|---|---|
committer | Don Scorgie <dscorgie@src.gnome.org> | 2007-06-11 18:50:09 +0000 |
commit | cd284ca43c02b0d3f687653e22a28a2722f021b9 (patch) | |
tree | 2f72ac41f2d87a5f5e7e7488e60ef1932ae382cc | |
parent | 5de8d38f5160373e70fba5e12a9fe6d91fddffb7 (diff) | |
download | yelp-cd284ca43c02b0d3f687653e22a28a2722f021b9.tar.gz |
Add gnome-vfs-init() and section (for results section) Add support for
* src/test-resolver.c:
Add gnome-vfs-init() and section (for results section)
* src/yelp-utils.c:
Add support for resolving info and full path uri's.
* src/yelp-utils.h:
Add long description of resolver function
svn path=/branches/yelp-spoon/; revision=2819
-rw-r--r-- | ChangeLog | 11 | ||||
-rw-r--r-- | src/test-resolver.c | 10 | ||||
-rw-r--r-- | src/yelp-utils.c | 216 | ||||
-rw-r--r-- | src/yelp-utils.h | 15 |
4 files changed, 237 insertions, 15 deletions
@@ -1,3 +1,14 @@ +2007-06-11 Don Scorgie <dscorgie@svn.gnome.org> + + * src/test-resolver.c: + Add gnome-vfs-init() and section (for results section) + + * src/yelp-utils.c: + Add support for resolving info and full path uri's. + + * src/yelp-utils.h: + Add long description of resolver function + 2007-06-02 Don Scorgie <dscorgie@cvs.gnome.org> * src/test-resolver.c: diff --git a/src/test-resolver.c b/src/test-resolver.c index e9c8b89d..34b2aa58 100644 --- a/src/test-resolver.c +++ b/src/test-resolver.c @@ -33,6 +33,9 @@ main (int argc, char *argv[]) { int i=1; + /* Used within yelp-utils */ + gnome_vfs_init (); + if (argc % 2 != 1) { printf ("Usage: %s [<test-uri> <type> <test-uri> <type> ... ]\n", argv[0]); printf ("type can be one of:\n"); @@ -49,33 +52,38 @@ main (int argc, char *argv[]) char *uri = argv[i]; char *type = argv[i+1]; char *result = NULL; + char *section = NULL; YelpSpoonType restype = YELP_TYPE_ERROR; printf ("uri: %s type: %s\n", argv[i], argv[i+1]); - restype = yelp_uri_resolve (argv[i], &result); if (g_str_equal (type, "doc")) { + restype = yelp_uri_resolve (argv[i], &result, §ion); if (restype != YELP_TYPE_DOC || !TestFile (result)) { printf ("Failed doc test %s. Aborting.\n", uri); return 2; } } else if (g_str_equal (type, "info")) { + restype = yelp_uri_resolve (argv[i], &result, §ion); if (restype != YELP_TYPE_INFO || !TestFile (result)) { printf ("Failed doc test %s. Aborting.\n", uri); return 3; } } else if (g_str_equal (type, "man")) { + restype = yelp_uri_resolve (argv[i], &result, §ion); if (restype != YELP_TYPE_MAN || !TestFile (result)) { printf ("Failed doc test %s. Aborting.\n", uri); return 4; } } else if (g_str_equal (type, "external")) { + restype = yelp_uri_resolve (argv[i], &result, §ion); if (restype != YELP_TYPE_EXTERNAL || !TestFile (result)) { printf ("Failed doc test %s. Aborting.\n", uri, restype, YELP_TYPE_EXTERNAL); return 5; } } else if (g_str_equal (type, "error")) { + restype = yelp_uri_resolve (argv[i], &result, §ion); if (restype != YELP_TYPE_ERROR || result != NULL) { printf ("Failed doc test %s. Aborting.\n", uri); return 6; diff --git a/src/yelp-utils.c b/src/yelp-utils.c index ae3e85f1..7781426c 100644 --- a/src/yelp-utils.c +++ b/src/yelp-utils.c @@ -1045,56 +1045,246 @@ convert_info_uri (gchar *uri) return doc_uri; } -#else +#else /*DON_UTIL*/ + +#include <spoon-info.h> + gchar * resolve_process_ghelp (char *uri) { + return NULL; +} + +gchar * +resolve_get_section (const gchar *uri) +{ + gchar *sect_delimit; + gchar *sect; + sect_delimit = strrchr (uri, '#'); + if (!sect_delimit) { + sect_delimit = strrchr (uri, '?'); + } + if (!sect_delimit) { + return NULL; + } + sect = g_strdup (sect_delimit+1); + + return sect; +} + +gboolean +resolve_is_man_path (const gchar *path, const gchar *encoding) +{ + gchar **cats; + gchar **iter; + + cats = spoon_man_get_categories (); + + iter = cats; + + if (encoding && *encoding) { + while (iter) { + gchar *ending = g_strdup_printf ("%s.%s", *iter, encoding); + if (g_str_has_suffix (path, ending)) { + free (ending); + return TRUE; + } + g_free (ending); + iter++; + } + } else { + while (iter) { + if (g_str_has_suffix (path, *iter)) { + return TRUE; + } + iter++; + } + } + return FALSE; } YelpSpoonType -yelp_uri_resolve (gchar *uri, gchar **result) +resolve_full_file (const gchar *path) +{ + gchar *mime_type; + YelpSpoonType type; + + if (!g_file_test (path, G_FILE_TEST_EXISTS)) { + return YELP_TYPE_ERROR; + } + + mime_type = gnome_vfs_get_mime_type (path); + if (mime_type == NULL) + return YELP_TYPE_ERROR; + + if (g_str_equal (mime_type, "text/xml") || g_str_equal (mime_type, "application/docbook+xml") || g_str_equal (mime_type, "application/xml")) + type = YELP_TYPE_DOC; + else if (g_str_equal (mime_type, "text/html") || + g_str_equal (mime_type, "application/xhtml+xml")) + type = YELP_TYPE_HTML; + /* No distinction between HTML and XHTML now. They're handled the same way */ + else if (g_str_equal (mime_type, "application/x-gzip")) { + if (g_str_has_suffix (path, ".info.gz")) { + type = YELP_TYPE_INFO; + } else if (resolve_is_man_path (path, "gz")) { + type = YELP_TYPE_MAN; + } + + } else if (g_str_equal (mime_type, "application/x-bzip")) { + if (g_str_has_suffix (path, ".info.bz2")) { + type = YELP_TYPE_INFO; + } else if (resolve_is_man_path (path, "bz2")) { + type = YELP_TYPE_MAN; + } + } else if (g_str_equal (mime_type, "text/plain")) { + if (g_str_has_suffix (path, ".info")) { + type = YELP_TYPE_INFO; + } else if (resolve_is_man_path (path, NULL)) { + type = YELP_TYPE_MAN; + } + } else { + type = YELP_TYPE_EXTERNAL; + } + + g_free (mime_type); + return type; + +} + +gchar * +resolve_remove_section (const gchar *uri, const gchar *sect) +{ + if (sect && *sect) + return (g_strndup (uri, (strlen(uri) - strlen(sect) - 1 /*for the delimiter char */))); + else + return (g_strdup (uri)); +} + +YelpSpoonType +yelp_uri_resolve (gchar *uri, gchar **result, gchar **section) { YelpSpoonType ret = YELP_TYPE_ERROR; + gchar *intern_section = NULL; + gchar *intern_uri = NULL; g_assert (result != NULL); if (*result != NULL) { - g_warning ("Warning: result is not empty: %s. Not searching.", *result); + g_warning ("result is not empty: %s.", *result); return ret; } - + g_assert (section != NULL); + if (*section != NULL) { + g_warning ("section is not empty: %s.", *section); + return ret; + } + if (uri == NULL) { + g_warning ("URI is NULL"); + return ret; + } + intern_section = resolve_get_section(uri); + intern_uri = resolve_remove_section (uri, intern_section); if (!strncmp (uri, "ghelp:", 6) || !strncmp (uri, "gnome-help:", 11)) { printf ("ghelp\n"); *result = resolve_process_ghelp (uri); if (*result) { ret = YELP_TYPE_DOC; + *section = intern_section; } } else if (!strncmp (uri, "man:", 4)) { printf ("man\n"); /* Man page */ } else if (!strncmp (uri, "info:", 5)) { - printf ("info\n"); /* info page */ + + gchar *info_name = intern_uri; + gchar *info_sect = intern_section; + gboolean free_stuff = FALSE; + SpoonInfoEntry *entry = NULL; + + if (!intern_section) { + gchar *lbrace = NULL; + gchar *rbrace = NULL; + lbrace = strchr (info_name, '('); + rbrace = strchr (info_name, ')'); + if (lbrace && rbrace) { + info_name = g_strndup (lbrace+1, (rbrace-lbrace-1)); + info_sect = g_strdup (rbrace+1); + free_stuff = TRUE; + } else { + info_name += 5; + } + } else { + info_name += 5; + } + + entry = spoon_info_find_from_uri (info_name, info_sect); + if (entry) { + ret = YELP_TYPE_INFO; + *section = g_strdup (entry->section); + *result = g_strdup (entry->base_filename); + } else { + ret = YELP_TYPE_ERROR; + *section = NULL; + *result = NULL; + } + if (free_stuff) { + g_free (info_name); + g_free (info_sect); + } } else if (!strncmp (uri, "file:", 5)) { - printf ("file\n"); + ret = resolve_full_file (&intern_uri[5]); + if (ret == YELP_TYPE_EXTERNAL) { + *section = NULL; + *result = g_strdup (uri); + } + else if (ret == YELP_TYPE_ERROR) { + *section = NULL; + *result = NULL; + } else { + *result = g_strdup (&intern_uri[5]); + *section = intern_section; + } /* full file path. Ensure file exists and determine type */ } else if (!strncmp (uri, "x-yelp-toc:", 11)) { - printf("toc\n"); + ret = YELP_TYPE_TOC; + *section = intern_section; + *result = NULL; /* TOC page */ } else if (!strncmp (uri, "x-yelp-search:", 14)) { - printf ("search\n"); - /* Search pager request */ + /* Search pager request. *result contains the search terms */ + *result = g_strdup (uri+14); + *section = NULL; + ret = YELP_TYPE_SEARCH; } else if (g_file_test (uri, G_FILE_TEST_EXISTS)) { - printf ("Full path\n"); - /* Probably full path */ + /* Full path */ + ret = resolve_full_file (&intern_uri[5]); + if (ret == YELP_TYPE_EXTERNAL) { + *section = NULL; + *result = g_strdup (uri); + } + else if (ret == YELP_TYPE_ERROR) { + *section = NULL; + *result = NULL; + } else { + *result = g_strdup (&intern_uri[5]); + *section = intern_section; + } + } else if (*uri == '/' || g_str_has_suffix (uri, ".xml")) { + /* Quite probable it was supposed to be ours, but + * the file doesn't exist. Hence, we should bin it + */ + ret = YELP_TYPE_ERROR; + *result = NULL; + *section = NULL; } else { /* We really don't care what it is. It's not ours. Let * someone else handle it */ - printf ("Ext\n"); ret = YELP_TYPE_EXTERNAL; *result = g_strdup (uri); + *section = NULL; } return ret; } -#endif +#endif /*DON_UTIL*/ diff --git a/src/yelp-utils.h b/src/yelp-utils.h index fc0bfae2..3290ea4c 100644 --- a/src/yelp-utils.h +++ b/src/yelp-utils.h @@ -126,7 +126,20 @@ typedef enum { YELP_TYPE_ERROR } YelpSpoonType; -YelpSpoonType yelp_uri_resolve (gchar *uri, gchar **result); + +/* Generic resolver function. Takes in the uri (which can be + * anything) and returns the type (enum above) + * The result is filled with a new string that the callee + * must free, except when returning YELP_TYPE_ERROR, when it will + * be NULL. The result is the base filename for the document. + * The section will be filled when the requested uri has a section + * otherwise, it will be NULL + * Both *result and *section must be NULL when calling (otherwise + * we throw an error + */ +YelpSpoonType yelp_uri_resolve (gchar *uri, + gchar **result, + gchar **section); #endif #endif /* __YELP_UTILS_H__ */ |