[yelp/yelp-3-0] [test-uri] Implementing ghelp URIs



commit dcd70c40748baf378fa2357f94f5acd8d6db1d89
Author: Shaun McCance <shaunm gnome org>
Date:   Thu Sep 10 00:29:29 2009 -0500

    [test-uri] Implementing ghelp URIs

 libyelp/yelp-uri.c             |   99 +++++++++++++++++++++++++++++++++++++++-
 tests/uri/ghelp-mallard-1.test |    4 ++
 2 files changed, 102 insertions(+), 1 deletions(-)
---
diff --git a/libyelp/yelp-uri.c b/libyelp/yelp-uri.c
index 1c402f7..d4cbd37 100644
--- a/libyelp/yelp-uri.c
+++ b/libyelp/yelp-uri.c
@@ -270,7 +270,16 @@ resolve_ghelp_uri (YelpUri *ret, gchar *arg)
     /* ghelp:/path/to/file
      * ghelp:document
      */
-    gchar *colon;
+    const gchar * const *datadirs = g_get_system_data_dirs ();
+    const gchar * const *langs = g_get_language_names ();
+    const gchar const *helpdirs[3] = {"help", "gnome/help", NULL};
+    gchar *docid = NULL;
+    gchar *filename = NULL;
+    gchar **searchpath = NULL;
+    gint searchi, searchmax;
+    gchar *colon, *hash, *slash, *pageid; /* don't free */
+    gint datadir_i, helpdir_i, lang_i;
+    YelpUriDocumentType type = YELP_URI_DOCUMENT_TYPE_UNKNOWN;
 
     colon = strchr (arg, ':');
     if (!colon) {
@@ -287,6 +296,94 @@ resolve_ghelp_uri (YelpUri *ret, gchar *arg)
         return;
     }
 
+    hash = strchr (colon, '?');
+    if (!hash)
+        hash = strchr (colon, '#');
+    if (hash) {
+        resolve_page_and_frag (ret, hash + 1);
+        docid = g_strndup (colon, hash - colon);
+    }
+    else {
+        docid = g_strdup (colon);
+    }
+
+    slash = strchr (docid, '/');
+    if (slash) {
+        *slash = '\0';
+        pageid = slash + 1;
+    }
+    else {
+        pageid = docid;
+    }
+
+    searchi = 0;
+    searchmax = 10;
+    searchpath = g_new0 (gchar *, 10);
+
+    for (datadir_i = 0; datadirs[datadir_i]; datadir_i++) {
+        for (helpdir_i = 0; helpdirs[helpdir_i]; helpdir_i++) {
+            for (lang_i = 0; langs[lang_i]; lang_i++) {
+                gchar *helpdir = g_strdup_printf ("%s%s/%s/%s",
+                                                  datadirs[datadir_i], helpdirs[helpdir_i], docid, langs[lang_i]);
+                if (!g_file_test (helpdir, G_FILE_TEST_IS_DIR)) {
+                    g_free (helpdir);
+                    continue;
+                }
+
+                if (searchi + 1 >= searchmax) {
+                    searchmax += 5;
+                    searchpath = g_renew (gchar *, searchpath, searchmax);
+                }
+                searchpath[searchi] = helpdir;
+                searchpath[++searchi] = NULL;
+                /* FIXME: append to path */
+
+                if (type != YELP_URI_DOCUMENT_TYPE_UNKNOWN)
+                    /* We've already found it.  We're just adding to the search path now. */
+                    continue;
+
+                filename = g_strdup_printf ("%s/index.page", helpdir);
+                if (g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
+                    type = YELP_URI_DOCUMENT_TYPE_MALLARD;
+                    g_free (filename);
+                    filename = g_strdup (helpdir);
+                    continue;
+                }
+                g_free (filename);
+
+                filename = g_strdup_printf ("%s/%s.xml", helpdir, pageid);
+                if (g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
+                    type = YELP_URI_DOCUMENT_TYPE_DOCBOOK;
+                    continue;
+                }
+                g_free (filename);
+
+                filename = g_strdup_printf ("%s/%s.html", helpdir, pageid);
+                if (g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
+                    type = YELP_URI_DOCUMENT_TYPE_HTML;
+                    continue;
+                }
+                g_free (filename);
+            } /* end for langs */
+        } /* end for helpdirs */
+    } /* end for datadirs */
+
+    if (type == YELP_URI_DOCUMENT_TYPE_UNKNOWN) {
+        g_strfreev (searchpath);
+        ret->priv->doctype = YELP_URI_DOCUMENT_TYPE_NOT_FOUND;
+    }
+    else {
+        ret->priv->doctype = type;
+        ret->priv->gfile = g_file_new_for_path (filename);
+        ret->priv->search_path = searchpath;
+        if (type == YELP_URI_DOCUMENT_TYPE_MALLARD && pageid != docid) {
+            if (ret->priv->page_id)
+                g_free (ret->priv->page_id);
+            ret->priv->page_id = g_strdup (pageid);
+        }
+    }
+
+    g_free (docid);
 }
 
 static void
diff --git a/tests/uri/ghelp-mallard-1.test b/tests/uri/ghelp-mallard-1.test
new file mode 100644
index 0000000..bbb9a24
--- /dev/null
+++ b/tests/uri/ghelp-mallard-1.test
@@ -0,0 +1,4 @@
+ghelp:gnome-doc-mallard-spec
+TYPE:  MALLARD
+URI:   file:///usr/share/gnome/help/gnome-doc-mallard-spec/C
+PATH:  /usr/share/gnome/help/gnome-doc-mallard-spec/C



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]