[gnome-builder/global-search: 3/4] GbSearchContext: use context to manage search process
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder/global-search: 3/4] GbSearchContext: use context to manage search process
- Date: Sat, 13 Dec 2014 07:49:57 +0000 (UTC)
commit 19f5f3246d0c6aaac37ad286023b31ef509c5422
Author: Christian Hergert <christian hergert me>
Date: Fri Dec 12 23:26:15 2014 -0800
GbSearchContext: use context to manage search process
src/search/gb-search-context.c | 132 ++++++++++++++++++++++++++++++++++++++--
src/search/gb-search-context.h | 4 +-
2 files changed, 129 insertions(+), 7 deletions(-)
---
diff --git a/src/search/gb-search-context.c b/src/search/gb-search-context.c
index fa95d6f..680ee5d 100644
--- a/src/search/gb-search-context.c
+++ b/src/search/gb-search-context.c
@@ -26,21 +26,63 @@
struct _GbSearchContextPrivate
{
+ GCancellable *cancellable;
+ GList *providers;
+ gchar *search_text;
};
G_DEFINE_TYPE_WITH_PRIVATE (GbSearchContext, gb_search_context, G_TYPE_OBJECT)
enum {
PROP_0,
+ PROP_PROVIDERS,
+ PROP_SEARCH_TEXT,
LAST_PROP
};
-//static GParamSpec *gParamSpecs [LAST_PROP];
+static GParamSpec *gParamSpecs [LAST_PROP];
+/**
+ * gb_search_context_new:
+ * @providers: (element-type GbSearchProvider*) (transfer none): A #GList
+ *
+ * Creates a new search context with the provided search providers.
+ *
+ * Returns: (transfer full): A newly allocated #GbSearchContext.
+ */
GbSearchContext *
-gb_search_context_new (void)
+gb_search_context_new (const GList *providers,
+ const gchar *search_text)
{
- return g_object_new (GB_TYPE_SEARCH_CONTEXT, NULL);
+ return g_object_new (GB_TYPE_SEARCH_CONTEXT,
+ "providers", providers,
+ "search-text", search_text,
+ NULL);
+}
+
+/**
+ * gb_search_context_get_cancellable:
+ * @context: A #GbSearchContext
+ *
+ * Retrieves the cancellable to cancel the search request. If the search has
+ * completed, this will return NULL.
+ *
+ * Returns: (transfer none): A #GCancellable or %NULL.
+ */
+GCancellable *
+gb_search_context_get_cancellable (GbSearchContext *context)
+{
+ g_return_val_if_fail (GB_IS_SEARCH_CONTEXT (context), NULL);
+
+ return context->priv->cancellable;
+}
+
+void
+gb_search_context_cancel (GbSearchContext *context)
+{
+ g_return_if_fail (GB_IS_SEARCH_CONTEXT (context));
+
+ g_cancellable_cancel (context->priv->cancellable);
}
void
@@ -53,10 +95,55 @@ gb_search_context_add_proposals (GbSearchContext *context,
g_return_if_fail (GB_IS_SEARCH_PROVIDER (provider));
}
+const gchar *
+gb_search_context_get_search_text (GbSearchContext *context)
+{
+ g_return_val_if_fail (GB_IS_SEARCH_CONTEXT (context), NULL);
+
+ return context->priv->search_text;
+}
+
+static void
+gb_search_context_set_search_text (GbSearchContext *context,
+ const gchar *search_text)
+{
+ g_return_if_fail (GB_IS_SEARCH_CONTEXT (context));
+ g_return_if_fail (search_text);
+
+ if (search_text != context->priv->search_text)
+ {
+ g_free (context->priv->search_text);
+ context->priv->search_text = g_strdup (search_text);
+ g_object_notify_by_pspec (G_OBJECT (context),
+ gParamSpecs [PROP_SEARCH_TEXT]);
+ }
+}
+
+static void
+gb_search_context_set_providers (GbSearchContext *context,
+ const GList *providers)
+{
+ GbSearchContextPrivate *priv;
+
+ g_return_if_fail (GB_IS_SEARCH_CONTEXT (context));
+
+ priv = context->priv;
+
+ g_list_foreach (priv->providers, (GFunc)g_object_unref, NULL);
+ g_list_free (priv->providers);
+
+ priv->providers = g_list_copy ((GList *)providers);
+ g_list_foreach (priv->providers, (GFunc)g_object_ref, NULL);
+}
+
static void
gb_search_context_finalize (GObject *object)
{
- //GbSearchContextPrivate *priv = GB_SEARCH_CONTEXT (object)->priv;
+ GbSearchContextPrivate *priv = GB_SEARCH_CONTEXT (object)->priv;
+
+ g_list_foreach (priv->providers, (GFunc)g_object_unref, NULL);
+ g_clear_pointer (&priv->providers, g_list_free);
+ g_clear_pointer (&priv->search_text, g_free);
G_OBJECT_CLASS (gb_search_context_parent_class)->finalize (object);
}
@@ -67,10 +154,14 @@ gb_search_context_get_property (GObject *object,
GValue *value,
GParamSpec *pspec)
{
- //GbSearchContext *self = GB_SEARCH_CONTEXT (object);
+ GbSearchContext *self = GB_SEARCH_CONTEXT (object);
switch (prop_id)
{
+ case PROP_SEARCH_TEXT:
+ g_value_set_string (value, gb_search_context_get_search_text (self));
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
@@ -82,10 +173,18 @@ gb_search_context_set_property (GObject *object,
const GValue *value,
GParamSpec *pspec)
{
- //GbSearchContext *self = GB_SEARCH_CONTEXT (object);
+ GbSearchContext *self = GB_SEARCH_CONTEXT (object);
switch (prop_id)
{
+ case PROP_PROVIDERS:
+ gb_search_context_set_providers (self, g_value_get_pointer (value));
+ break;
+
+ case PROP_SEARCH_TEXT:
+ gb_search_context_set_search_text (self, g_value_get_string (value));
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
@@ -99,6 +198,27 @@ gb_search_context_class_init (GbSearchContextClass *klass)
object_class->finalize = gb_search_context_finalize;
object_class->get_property = gb_search_context_get_property;
object_class->set_property = gb_search_context_set_property;
+
+ gParamSpecs [PROP_SEARCH_TEXT] =
+ g_param_spec_string ("search-text",
+ _("Search Text"),
+ _("The search text for the context."),
+ NULL,
+ (G_PARAM_READWRITE |
+ G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class, PROP_SEARCH_TEXT,
+ gParamSpecs [PROP_SEARCH_TEXT]);
+
+ gParamSpecs [PROP_PROVIDERS] =
+ g_param_spec_pointer ("providers",
+ _("Providers"),
+ _("The providers for the search context."),
+ (G_PARAM_READABLE |
+ G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class, PROP_PROVIDERS,
+ gParamSpecs [PROP_PROVIDERS]);
}
static void
diff --git a/src/search/gb-search-context.h b/src/search/gb-search-context.h
index c718679..31a48b9 100644
--- a/src/search/gb-search-context.h
+++ b/src/search/gb-search-context.h
@@ -47,7 +47,9 @@ struct _GbSearchContextClass
};
GType gb_search_context_get_type (void);
-GbSearchContext *gb_search_context_new (void);
+GbSearchContext *gb_search_context_new (const GList *providers,
+ const gchar *search_text);
+void gb_search_context_cancel (GbSearchContext *context);
void gb_search_context_add_results (GbSearchContext *context,
GbSearchProvider *provider,
GList *results,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]