[yelp: 5/17] Store and retrieve root title in storage for search results



commit c6b056db59bf38683543442b2601a20b32bf8301
Author: Shaun McCance <shaunm gnome org>
Date:   Fri Feb 11 15:44:54 2011 -0500

    Store and retrieve root title in storage for search results

 libyelp/yelp-document.c         |  105 ++++++++++++++++++++++++++++-----------
 libyelp/yelp-mallard-document.c |    3 +
 libyelp/yelp-sqlite-storage.c   |   88 +++++++++++++++++++++++++++++----
 libyelp/yelp-storage.c          |   31 +++++++++++
 libyelp/yelp-storage.h          |   30 +++++++----
 5 files changed, 207 insertions(+), 50 deletions(-)
---
diff --git a/libyelp/yelp-document.c b/libyelp/yelp-document.c
index c40663d..57827a6 100644
--- a/libyelp/yelp-document.c
+++ b/libyelp/yelp-document.c
@@ -442,6 +442,9 @@ yelp_document_get_page_id (YelpDocument *document,
 
     g_assert (document != NULL && YELP_IS_DOCUMENT (document));
 
+    if (id != NULL && g_str_has_prefix (id, "search="))
+        return g_strdup (id);
+
     g_mutex_lock (document->priv->mutex);
     ret = hash_lookup (document->priv->page_ids, id);
     if (ret)
@@ -497,7 +500,10 @@ yelp_document_get_root_id (YelpDocument *document,
     g_assert (document != NULL && YELP_IS_DOCUMENT (document));
 
     g_mutex_lock (document->priv->mutex);
-    real = hash_lookup (document->priv->page_ids, page_id);
+    if (page_id != NULL && g_str_has_prefix (page_id, "search="))
+        real = hash_lookup (document->priv->page_ids, NULL);
+    else
+        real = hash_lookup (document->priv->page_ids, page_id);
     if (real) {
 	ret = hash_lookup (document->priv->root_ids, real);
 	if (ret)
@@ -625,15 +631,22 @@ yelp_document_get_root_title (YelpDocument *document,
     g_assert (document != NULL && YELP_IS_DOCUMENT (document));
 
     g_mutex_lock (document->priv->mutex);
-    real = hash_lookup (document->priv->page_ids, page_id);
-    if (real) {
-	root = hash_lookup (document->priv->root_ids, real);
-        if (root) {
-            ret = hash_lookup (document->priv->titles, root);
-            if (ret)
-                ret = g_strdup (ret);
+    if (page_id != NULL && g_str_has_prefix (page_id, "search=")) {
+        ret = yelp_storage_get_root_title (yelp_storage_get_default (),
+                                           document->priv->doc_uri);
+    }
+    else {
+        real = hash_lookup (document->priv->page_ids, page_id);
+        if (real) {
+            root = hash_lookup (document->priv->root_ids, real);
+            if (root) {
+                ret = hash_lookup (document->priv->titles, root);
+                if (ret)
+                    ret = g_strdup (ret);
+            }
         }
     }
+
     g_mutex_unlock (document->priv->mutex);
 
     return ret;
@@ -647,6 +660,11 @@ yelp_document_get_page_title (YelpDocument *document,
 
     g_assert (document != NULL && YELP_IS_DOCUMENT (document));
 
+    if (page_id != NULL && g_str_has_prefix (page_id, "search=")) {
+        ret = g_uri_unescape_string (page_id + 7, NULL);
+        return ret;
+    }
+
     g_mutex_lock (document->priv->mutex);
     real = hash_lookup (document->priv->page_ids, page_id);
     if (real) {
@@ -679,6 +697,9 @@ yelp_document_get_page_desc (YelpDocument *document,
 
     g_assert (document != NULL && YELP_IS_DOCUMENT (document));
 
+    if (page_id != NULL && g_str_has_prefix (page_id, "search="))
+        return yelp_document_get_root_title (document, page_id);
+
     g_mutex_lock (document->priv->mutex);
     real = hash_lookup (document->priv->page_ids, page_id);
     if (real) {
@@ -711,6 +732,9 @@ yelp_document_get_page_icon (YelpDocument *document,
 
     g_assert (document != NULL && YELP_IS_DOCUMENT (document));
 
+    if (page_id != NULL && g_str_has_prefix (page_id, "search="))
+        return g_strdup ("folder-saved-search");
+
     g_mutex_lock (document->priv->mutex);
     real = hash_lookup (document->priv->page_ids, page_id);
     if (real) {
@@ -745,6 +769,7 @@ document_indexed (YelpDocument *document)
     while (document->priv->reqs_search != NULL) {
         Request *request = (Request *) document->priv->reqs_search->data;
         request->idle_funcs++;
+        g_idle_add ((GSourceFunc) request_idle_info, request);
         g_idle_add ((GSourceFunc) request_idle_contents, request);
         document->priv->reqs_search = g_slist_delete_link (document->priv->reqs_search,
                                                            document->priv->reqs_search);
@@ -860,7 +885,8 @@ document_read_contents (YelpDocument *document,
         gchar *tmp, *txt;
         GVariant *value;
         GVariantIter *iter;
-        gchar *url, *title, *desc, *icon, *index_id, *index_title; /* do not free */
+        gchar *url, *title, *desc, *icon; /* do not free */
+        gchar *index_title;
         GString *ret = g_string_new ("<html><head><style type='text/css'>");
 
         colors = yelp_settings_get_colors (yelp_settings_get_default ());
@@ -887,7 +913,8 @@ document_read_contents (YelpDocument *document,
                                 " outline: solid 1px %s;"
                                 " background: -webkit-gradient(linear, left top, left 80, from(%s), to(%s)); } "
                                 "div.title { margin-bottom: 0.2em; font-weight: bold; } "
-                                "div.desc { margin: 0; color: %s; } ",
+                                "div.desc { margin: 0; color: %s; } "
+                                "</style></head><body><div class='header'>",
                                 colors[YELP_SETTINGS_COLOR_BASE],
                                 colors[YELP_SETTINGS_COLOR_TEXT],
                                 (gtk_widget_get_default_direction() == GTK_TEXT_DIR_RTL ? "rtl" : "ltr"),
@@ -901,20 +928,21 @@ document_read_contents (YelpDocument *document,
                                 colors[YELP_SETTINGS_COLOR_BASE],
                                 colors[YELP_SETTINGS_COLOR_TEXT_LIGHT]
                                 );
-        index_id = hash_lookup (document->priv->page_ids, NULL);
-        index_title = hash_lookup (document->priv->titles, index_id);
-        if (index_title == NULL)
-            index_title = "index";
-        tmp = g_markup_printf_escaped ("</style></head><body>"
-                                       "<div class='header'><div class='trails'><div class='trail'>"
-                                       "<a href='xref:'>%s</a>&#x00A0;%s "
-                                       "</div></div></div>"
-                                       "<div class='body'>",
-                                       index_title,
-                                       (gtk_widget_get_default_direction() == GTK_TEXT_DIR_RTL ? "«" : "»")
-                                       );
-        g_string_append (ret, tmp);
-        g_free (tmp);
+
+        index_title = yelp_storage_get_root_title (yelp_storage_get_default (),
+                                                   document->priv->doc_uri);
+        if (index_title != NULL) {
+            tmp = g_markup_printf_escaped ("<div class='trails'><div class='trail'>"
+                                           "<a href='xref:'>%s</a>&#x00A0;%s "
+                                           "</div></div>",
+                                           index_title,
+                                           (gtk_widget_get_default_direction() == GTK_TEXT_DIR_RTL ? "«" : "»")
+                                           );
+            g_string_append (ret, tmp);
+            g_free (tmp);
+        }
+
+        g_string_append (ret, "</div><div class='body'>");
         g_strfreev (colors);
 
         str = hash_lookup (document->priv->contents, real);
@@ -933,18 +961,35 @@ document_read_contents (YelpDocument *document,
                                      document->priv->doc_uri,
                                      txt);
         iter = g_variant_iter_new (value);
-        while (g_variant_iter_loop (iter, "(&s&s&s&s)", &url, &title, &desc, &icon)) {
-            tmp = g_markup_printf_escaped ("<div><a class='linkdiv' href='%s'><div class='linkdiv'>"
-                                           "<div class='title'>%s</div>"
-                                           "<div class='desc'>%s</div>"
-                                           "</div></a></div>",
-                                           url, title, desc);
+        if (g_variant_iter_n_children (iter) == 0) {
+            if (index_title != NULL) {
+                gchar *t = g_strdup_printf (_("No matching help pages found in â??%sâ??."), index_title);
+                tmp = g_markup_printf_escaped ("<p>%s</p>", t);
+                g_free (t);
+            }
+            else {
+                tmp = g_markup_printf_escaped ("<p>%s</p>",
+                                               _("No matching help pages found."));
+            }
             g_string_append (ret, tmp);
             g_free (tmp);
         }
+        else {
+            while (g_variant_iter_loop (iter, "(&s&s&s&s)", &url, &title, &desc, &icon)) {
+                tmp = g_markup_printf_escaped ("<div><a class='linkdiv' href='%s'><div class='linkdiv'>"
+                                               "<div class='title'>%s</div>"
+                                               "<div class='desc'>%s</div>"
+                                               "</div></a></div>",
+                                               url, title, desc);
+                g_string_append (ret, tmp);
+                g_free (tmp);
+            }
+        }
         g_variant_iter_free (iter);
         g_variant_unref (value);
 
+        if (index_title != NULL)
+            g_free (index_title);
         g_free (txt);
         g_string_append (ret, "</div></body></html>");
         g_mutex_unlock (document->priv->mutex);
diff --git a/libyelp/yelp-mallard-document.c b/libyelp/yelp-mallard-document.c
index b3845b4..41eb134 100644
--- a/libyelp/yelp-mallard-document.c
+++ b/libyelp/yelp-mallard-document.c
@@ -1009,6 +1009,9 @@ mallard_index_threaded (YelpMallardDocument *mallard)
                                  title, desc,
                                  xml_node_get_icon (xmlDocGetRootElement (index->doc)),
                                  fulltext);
+            if (g_str_equal (id, "index"))
+                yelp_storage_set_root_title (yelp_storage_get_default (),
+                                             doc_uri, title);
             g_free (full_uri);
             g_free (title);
             g_free (desc);
diff --git a/libyelp/yelp-sqlite-storage.c b/libyelp/yelp-sqlite-storage.c
index 57ad188..f2e6619 100644
--- a/libyelp/yelp-sqlite-storage.c
+++ b/libyelp/yelp-sqlite-storage.c
@@ -40,16 +40,21 @@ static void        yelp_sqlite_storage_set_property (GObject                *obj
                                                      const GValue           *value,
                                                      GParamSpec             *pspec);
 
-static void        yelp_sqlite_storage_update      (YelpStorage      *storage,
-                                                    const gchar      *doc_uri,
-                                                    const gchar      *full_uri,
-                                                    const gchar      *title,
-                                                    const gchar      *desc,
-                                                    const gchar      *icon,
-                                                    const gchar      *text);
-static GVariant *  yelp_sqlite_storage_search      (YelpStorage      *storage,
-                                                    const gchar      *doc_uri,
-                                                    const gchar      *text);
+static void        yelp_sqlite_storage_update         (YelpStorage      *storage,
+                                                       const gchar      *doc_uri,
+                                                       const gchar      *full_uri,
+                                                       const gchar      *title,
+                                                       const gchar      *desc,
+                                                       const gchar      *icon,
+                                                       const gchar      *text);
+static GVariant *  yelp_sqlite_storage_search         (YelpStorage      *storage,
+                                                       const gchar      *doc_uri,
+                                                       const gchar      *text);
+static gchar *     yelp_sqlite_storage_get_root_title (YelpStorage      *storage,
+                                                       const gchar      *doc_uri);
+static void        yelp_sqlite_storage_set_root_title (YelpStorage      *storage,
+                                                       const gchar      *doc_uri,
+                                                       const gchar      *title);
 
 typedef struct _YelpSqliteStoragePrivate YelpSqliteStoragePrivate;
 struct _YelpSqliteStoragePrivate {
@@ -115,7 +120,14 @@ yelp_sqlite_storage_constructed (GObject *object)
                                  -1, &stmt, NULL);
     if (status != SQLITE_OK)
         return;
+    sqlite3_step (stmt);
+    sqlite3_finalize (stmt);
 
+    status = sqlite3_prepare_v2 (priv->db,
+                                 "create table titles (doc_uri text, lang text, title text);",
+                                 -1, &stmt, NULL);
+    if (status != SQLITE_OK)
+        return;
     sqlite3_step (stmt);
     sqlite3_finalize (stmt);
 }
@@ -147,6 +159,8 @@ yelp_sqlite_storage_iface_init (YelpStorageInterface *iface)
 {
     iface->update = yelp_sqlite_storage_update;
     iface->search = yelp_sqlite_storage_search;
+    iface->get_root_title = yelp_sqlite_storage_get_root_title;
+    iface->set_root_title = yelp_sqlite_storage_set_root_title;
 }
 
 YelpStorage *
@@ -274,3 +288,57 @@ yelp_sqlite_storage_search (YelpStorage   *storage,
 
     return ret;
 }
+
+static gchar *
+yelp_sqlite_storage_get_root_title (YelpStorage *storage,
+                                    const gchar *doc_uri)
+{
+    gchar *ret = NULL;
+    sqlite3_stmt *stmt = NULL;
+    YelpSqliteStoragePrivate *priv = GET_PRIV (storage);
+
+    g_mutex_lock (priv->mutex);
+
+    sqlite3_prepare_v2 (priv->db,
+                        "select title from titles where doc_uri = ? and lang = ?;",
+                        -1, &stmt, NULL);
+    sqlite3_bind_text (stmt, 1, doc_uri, -1, SQLITE_TRANSIENT);
+    sqlite3_bind_text (stmt, 2, g_get_language_names()[0], -1, SQLITE_STATIC);
+    if (sqlite3_step (stmt) == SQLITE_ROW)
+        ret = g_strdup (sqlite3_column_text (stmt, 0));
+    sqlite3_finalize (stmt);
+
+    g_mutex_unlock (priv->mutex);
+    return ret;
+}
+
+static void
+yelp_sqlite_storage_set_root_title (YelpStorage *storage,
+                                    const gchar *doc_uri,
+                                    const gchar *title)
+{
+    sqlite3_stmt *stmt = NULL;
+    YelpSqliteStoragePrivate *priv = GET_PRIV (storage);
+
+    g_mutex_lock (priv->mutex);
+
+    sqlite3_prepare_v2 (priv->db,
+                        "delete from titles where doc_uri = ? and lang = ?;",
+                        -1, &stmt, NULL);
+    sqlite3_bind_text (stmt, 1, doc_uri, -1, SQLITE_TRANSIENT);
+    sqlite3_bind_text (stmt, 2, g_get_language_names()[0], -1, SQLITE_STATIC);
+    sqlite3_step (stmt);
+    sqlite3_finalize (stmt);
+
+    sqlite3_prepare_v2 (priv->db,
+                        "insert into titles (doc_uri, lang, title)"
+                        " values (?, ?, ?);",
+                        -1, &stmt, NULL);
+    sqlite3_bind_text (stmt, 1, doc_uri, -1, SQLITE_TRANSIENT);
+    sqlite3_bind_text (stmt, 2, g_get_language_names()[0], -1, SQLITE_STATIC);
+    sqlite3_bind_text (stmt, 3, title, -1, SQLITE_TRANSIENT);
+    sqlite3_step (stmt);
+    sqlite3_finalize (stmt);
+
+    g_mutex_unlock (priv->mutex);
+}
diff --git a/libyelp/yelp-storage.c b/libyelp/yelp-storage.c
index c4886fe..e901af1 100644
--- a/libyelp/yelp-storage.c
+++ b/libyelp/yelp-storage.c
@@ -85,3 +85,34 @@ yelp_storage_search (YelpStorage   *storage,
     else
         return NULL;
 }
+
+gchar *
+yelp_storage_get_root_title (YelpStorage *storage,
+                             const gchar *doc_uri)
+{
+    YelpStorageInterface *iface;
+
+    g_return_if_fail (YELP_IS_STORAGE (storage));
+
+    iface = YELP_STORAGE_GET_INTERFACE (storage);
+
+    if (iface->search)
+        return (*iface->get_root_title) (storage, doc_uri);
+    else
+        return NULL;
+}
+
+void
+yelp_storage_set_root_title (YelpStorage *storage,
+                             const gchar *doc_uri,
+                             const gchar *title)
+{
+    YelpStorageInterface *iface;
+
+    g_return_if_fail (YELP_IS_STORAGE (storage));
+
+    iface = YELP_STORAGE_GET_INTERFACE (storage);
+
+    if (iface->search)
+        (*iface->set_root_title) (storage, doc_uri, title);
+}
diff --git a/libyelp/yelp-storage.h b/libyelp/yelp-storage.h
index 57666a0..da57579 100644
--- a/libyelp/yelp-storage.h
+++ b/libyelp/yelp-storage.h
@@ -38,16 +38,21 @@ typedef struct _YelpStorageInterface YelpStorageInterface;
 struct _YelpStorageInterface {
     GTypeInterface base_iface;
 
-    void          (*update)        (YelpStorage   *storage,
-                                    const gchar   *doc_uri,
-                                    const gchar   *full_uri,
-                                    const gchar   *title,
-                                    const gchar   *desc,
-                                    const gchar   *icon,
-                                    const gchar   *text);
-    GVariant *    (*search)        (YelpStorage   *storage,
-                                    const gchar   *doc_uri,
-                                    const gchar   *text);
+    void          (*update)         (YelpStorage   *storage,
+                                     const gchar   *doc_uri,
+                                     const gchar   *full_uri,
+                                     const gchar   *title,
+                                     const gchar   *desc,
+                                     const gchar   *icon,
+                                     const gchar   *text);
+    GVariant *    (*search)         (YelpStorage   *storage,
+                                     const gchar   *doc_uri,
+                                     const gchar   *text);
+    gchar *       (*get_root_title) (YelpStorage   *storage,
+                                     const gchar   *doc_uri);
+    void          (*set_root_title) (YelpStorage   *storage,
+                                     const gchar   *doc_uri,
+                                     const gchar   *title);
 };
 
 GType             yelp_storage_get_type       (void);
@@ -65,6 +70,11 @@ void              yelp_storage_update         (YelpStorage   *storage,
 GVariant *        yelp_storage_search         (YelpStorage   *storage,
                                                const gchar   *doc_uri,
                                                const gchar   *text);
+gchar *           yelp_storage_get_root_title (YelpStorage   *storage,
+                                               const gchar   *doc_uri);
+void              yelp_storage_set_root_title (YelpStorage   *storage,
+                                               const gchar   *doc_uri,
+                                               const gchar   *title);
 
 G_END_DECLS
 



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