[gnome-builder/wip/libide] search: add devhelp provider



commit 80434bdc959ec47f0aed4fc3e699780621860bde
Author: Erick Pérez Castellanos <erick red gmail com>
Date:   Fri Feb 27 14:17:20 2015 -0500

    search: add devhelp provider
    
    https://bugzilla.gnome.org/show_bug.cgi?id=744899

 build/autotools/autoconf.d/50_dependencies.post-am |    1 +
 libide/Makefile.am                                 |    2 +
 libide/devhelp/ide-devhelp-search-provider.c       |  205 ++++++++++++++++++++
 libide/devhelp/ide-devhelp-search-provider.h       |   41 ++++
 4 files changed, 249 insertions(+), 0 deletions(-)
---
diff --git a/build/autotools/autoconf.d/50_dependencies.post-am 
b/build/autotools/autoconf.d/50_dependencies.post-am
index ea1d76f..9ffabdf 100644
--- a/build/autotools/autoconf.d/50_dependencies.post-am
+++ b/build/autotools/autoconf.d/50_dependencies.post-am
@@ -14,6 +14,7 @@ PKG_CHECK_MODULES(BUILDER, [gtk+-3.0 >= gtk_required_version
 PKG_CHECK_MODULES(LIBIDE, [gio-2.0 >= glib_required_version
                            gio-unix-2.0 >= glib_required_version
                            gtksourceview-3.0 >= gtksourceview_required_version
+                           libdevhelp-3.0 >= 3.14.0
                            libgit2-glib-1.0 >= ggit_required_version
                            gjs-1.0 >= gjs_required_version
                            gjs-internals-1.0 >= gjs_required_version
diff --git a/libide/Makefile.am b/libide/Makefile.am
index 0ec6c62..0490048 100644
--- a/libide/Makefile.am
+++ b/libide/Makefile.am
@@ -24,6 +24,8 @@ libide_1_0_la_public_sources = \
        libide/clang/ide-clang-symbol-resolver.h \
        libide/clang/ide-clang-translation-unit.c \
        libide/clang/ide-clang-translation-unit.h \
+       libide/devhelp/ide-devhelp-search-provider.c \
+       libide/devhelp/ide-devhelp-search-provider.h \
        libide/directory/ide-directory-build-system.c \
        libide/directory/ide-directory-build-system.h \
        libide/directory/ide-directory-vcs.c \
diff --git a/libide/devhelp/ide-devhelp-search-provider.c b/libide/devhelp/ide-devhelp-search-provider.c
new file mode 100644
index 0000000..084213c
--- /dev/null
+++ b/libide/devhelp/ide-devhelp-search-provider.c
@@ -0,0 +1,205 @@
+/* ide-devhelp-search-provider.c
+ *
+ * Copyright (C) 2015 Erick Pérez Castellanos <erick red gmail com>
+ *
+ * This file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "devhelp-search"
+
+#include "ide-devhelp-search-provider.h"
+
+#include <ctype.h>
+#include <glib/gi18n.h>
+#include <devhelp/devhelp.h>
+
+#include "ide-search-reducer.h"
+#include "ide-search-result.h"
+#include "ide-search-context.h"
+
+typedef struct
+{
+  DhBookManager  *book_manager;
+  DhKeywordModel *keywords_model;
+} IdeDevhelpSearchProviderPrivate;
+
+struct _IdeDevhelpSearchProvider
+{
+  IdeSearchProvider                parent;
+
+  /*< private >*/
+  IdeDevhelpSearchProviderPrivate *priv;
+};
+
+typedef struct
+{
+  IdeSearchContext *context;
+  gchar            *search_terms;
+  gsize             max_results;
+} PopulateState;
+
+G_DEFINE_TYPE_WITH_PRIVATE (IdeDevhelpSearchProvider,
+                            ide_devhelp_search_provider,
+                            IDE_TYPE_SEARCH_PROVIDER)
+
+static GQuark      gQuarkLink;
+
+static void
+populate_get_matches_cb (GObject      *source_object,
+                         GAsyncResult *res,
+                         gpointer      user_data)
+{
+  IdeDevhelpSearchProviderPrivate *priv;
+  IdeContext* context;
+  PopulateState *state;
+  g_auto(IdeSearchReducer) reducer = { 0 };
+  GtkTreeIter iter;
+  gboolean valid;
+  gint count = 0;
+  gint total;
+
+  priv = IDE_DEVHELP_SEARCH_PROVIDER (source_object)->priv;
+  state = (PopulateState*) user_data;
+  context = ide_object_get_context (IDE_OBJECT (state->context));
+
+  dh_keyword_model_filter (priv->keywords_model, state->search_terms, NULL, NULL);
+
+  total = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->keywords_model),
+                                          NULL);
+  if (state->max_results != 0)
+    total = MIN (total, state->max_results);
+
+  /* initialize our reducer, which helps us prevent creating unnecessary
+   * objects that will simply be discarded */
+  ide_search_reducer_init (&reducer,
+                           state->context,
+                           IDE_SEARCH_PROVIDER (source_object),
+                           state->max_results);
+
+  valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->keywords_model),
+                                         &iter);
+  while (valid)
+    {
+      g_autoptr(IdeSearchResult) result = NULL;
+      DhLink *link = NULL;
+      g_autofree gchar *name = NULL;
+      gfloat score = .0;
+
+      gtk_tree_model_get (GTK_TREE_MODEL (priv->keywords_model), &iter,
+                          DH_KEYWORD_MODEL_COL_NAME, &name,
+                          DH_KEYWORD_MODEL_COL_LINK, &link,
+                          -1);
+
+      valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->keywords_model),
+                                        &iter);
+
+      count++;
+      score = (total - count) / (gfloat) (total + 1);
+      if (!ide_search_reducer_accepts (&reducer, score))
+        continue;
+
+      if (!g_str_is_ascii (name))
+        {
+          gchar *ascii_name = g_str_to_ascii (name, NULL);
+          g_free (name);
+          name = ascii_name;
+        }
+
+      if ((dh_link_get_flags (link) & DH_LINK_FLAGS_DEPRECATED) != 0)
+        {
+          gchar *italic_name = g_strdup_printf ("<i>%s</i>", name);
+          g_free (name);
+          name = italic_name;
+        }
+
+      result = ide_search_result_new (context,
+                                      name,
+                                      dh_link_get_book_name (link),
+                                      score);
+      g_object_set_qdata_full (G_OBJECT (result),
+                               gQuarkLink,
+                               dh_link_get_uri (link),
+                               g_free);
+
+      /* push the result through the search reducer */
+      ide_search_reducer_push (&reducer, result);
+    }
+
+  ide_search_context_provider_completed (state->context,
+                                         IDE_SEARCH_PROVIDER (source_object));
+
+  g_free (state->search_terms);
+  g_object_unref (state->context);
+  g_slice_free (PopulateState, state);
+}
+
+void
+ide_devhelp_search_provider_populate (IdeSearchProvider *provider,
+                                      IdeSearchContext  *context,
+                                      const gchar       *search_terms,
+                                      gsize              max_results,
+                                      GCancellable      *cancellable)
+{
+  PopulateState *state;
+  g_autoptr(GTask) task = NULL;
+
+  state = g_new0 (PopulateState, 1);
+  state->context = g_object_ref (context);
+  state->search_terms = g_strdup (search_terms);
+  state->max_results = max_results;
+
+  task = g_task_new (provider, cancellable, populate_get_matches_cb, state);
+  g_task_return_pointer (task, NULL, NULL);
+}
+
+static void
+ide_devhelp_search_provider_constructed (GObject *object)
+{
+  IdeDevhelpSearchProvider *self = IDE_DEVHELP_SEARCH_PROVIDER (object);
+
+  dh_book_manager_populate (self->priv->book_manager);
+  dh_keyword_model_set_words (self->priv->keywords_model, self->priv->book_manager);
+}
+
+static void
+ide_devhelp_search_provider_finalize (GObject *object)
+{
+  IdeDevhelpSearchProvider *self = IDE_DEVHELP_SEARCH_PROVIDER (object);
+
+  g_clear_object (&self->priv->book_manager);
+
+  G_OBJECT_CLASS (ide_devhelp_search_provider_parent_class)->finalize (object);
+}
+
+static void
+ide_devhelp_search_provider_class_init (IdeDevhelpSearchProviderClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  IdeSearchProviderClass *provider_class = IDE_SEARCH_PROVIDER_CLASS (klass);
+
+  object_class->constructed = ide_devhelp_search_provider_constructed;
+  object_class->finalize = ide_devhelp_search_provider_finalize;
+
+  provider_class->populate = ide_devhelp_search_provider_populate;
+
+  gQuarkLink = g_quark_from_static_string ("LINK");
+}
+
+static void
+ide_devhelp_search_provider_init (IdeDevhelpSearchProvider *self)
+{
+  self->priv = ide_devhelp_search_provider_get_instance_private (self);
+  self->priv->book_manager = dh_book_manager_new ();
+  self->priv->keywords_model = dh_keyword_model_new ();
+}
diff --git a/libide/devhelp/ide-devhelp-search-provider.h b/libide/devhelp/ide-devhelp-search-provider.h
new file mode 100644
index 0000000..fda5f2f
--- /dev/null
+++ b/libide/devhelp/ide-devhelp-search-provider.h
@@ -0,0 +1,41 @@
+/* ide-devhelp-search-provider.h
+ *
+ * Copyright (C) 2015 Erick Pérez Castellanos <erick red gmail com>
+ *
+ * This file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef IDE_DEVHELP_SEARCH_PROVIDER_H
+#define IDE_DEVHELP_SEARCH_PROVIDER_H
+
+#include "ide-search-provider.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_DEVHELP_SEARCH_PROVIDER (ide_devhelp_search_provider_get_type())
+
+G_DECLARE_FINAL_TYPE (IdeDevhelpSearchProvider,
+                      ide_devhelp_search_provider,
+                      IDE,
+                      DEVHELP_SEARCH_PROVIDER,
+                      IdeSearchProvider)
+
+struct _IdeDevhelpSearchProviderClass
+{
+  IdeSearchProviderClass parent;
+};
+
+G_END_DECLS
+
+#endif /* IDE_DEVHELP_SEARCH_PROVIDER_H */


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