[gnome-builder] libide: finish implementing basic searchresult base class



commit 4e974b7ed937dea179c1b5086f4345451dcea919
Author: Christian Hergert <christian hergert me>
Date:   Mon Feb 16 13:51:47 2015 -0800

    libide: finish implementing basic searchresult base class
    
    This didn't get finished when bringing the search result down from
    GbSearchResult. Get things plumbed in and working.

 libide/ide-search-result.c |  186 +++++++++++++++++++++++++++++++++++++++++++-
 libide/ide-search-result.h |   13 +++
 2 files changed, 198 insertions(+), 1 deletions(-)
---
diff --git a/libide/ide-search-result.c b/libide/ide-search-result.c
index a6f4382..2903a41 100644
--- a/libide/ide-search-result.c
+++ b/libide/ide-search-result.c
@@ -16,28 +16,153 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <glib/gi18n.h>
+
+#include "ide-context.h"
 #include "ide-search-result.h"
 
 typedef struct
 {
-  gchar *text;
+  gchar  *title;
+  gchar  *subtitle;
+  gfloat  score;
 } IdeSearchResultPrivate;
 
 G_DEFINE_TYPE_WITH_PRIVATE (IdeSearchResult, ide_search_result, IDE_TYPE_OBJECT)
 
 enum {
   PROP_0,
+  PROP_SCORE,
+  PROP_SUBTITLE,
+  PROP_TITLE,
   LAST_PROP
 };
 
 static GParamSpec *gParamSpecs [LAST_PROP];
 
+IdeSearchResult *
+ide_search_result_new (IdeContext  *context,
+                       const gchar *title,
+                       const gchar *subtitle,
+                       gfloat       score)
+{
+  IdeSearchResult *self;
+
+  g_return_val_if_fail (IDE_IS_CONTEXT (context), NULL);
+
+  self = g_object_new (IDE_TYPE_SEARCH_RESULT,
+                       "context", context,
+                       "title", title,
+                       "subtitle", subtitle,
+                       "score", score,
+                       NULL);
+
+  return self;
+}
+
+const gchar *
+ide_search_result_get_title (IdeSearchResult *self)
+{
+  IdeSearchResultPrivate *priv = ide_search_result_get_instance_private (self);
+
+  g_return_val_if_fail (IDE_IS_SEARCH_RESULT (self), NULL);
+
+  return priv->title;
+}
+
+const gchar *
+ide_search_result_get_subtitle (IdeSearchResult *self)
+{
+  IdeSearchResultPrivate *priv = ide_search_result_get_instance_private (self);
+
+  g_return_val_if_fail (IDE_IS_SEARCH_RESULT (self), NULL);
+
+  return priv->subtitle;
+}
+
+gfloat
+ide_search_result_get_score (IdeSearchResult *self)
+{
+  IdeSearchResultPrivate *priv = ide_search_result_get_instance_private (self);
+
+  g_return_val_if_fail (IDE_IS_SEARCH_RESULT (self), 0.0);
+
+  return priv->score;
+}
+
+static void
+ide_search_result_set_title (IdeSearchResult *self,
+                             const gchar     *title)
+{
+  IdeSearchResultPrivate *priv = ide_search_result_get_instance_private (self);
+
+  g_return_if_fail (IDE_IS_SEARCH_RESULT (self));
+
+  if (priv->title != title)
+    {
+      g_free (priv->title);
+      priv->title = g_strdup (title);
+    }
+}
+
+static void
+ide_search_result_set_subtitle (IdeSearchResult *self,
+                                const gchar     *subtitle)
+{
+  IdeSearchResultPrivate *priv = ide_search_result_get_instance_private (self);
+
+  g_return_if_fail (IDE_IS_SEARCH_RESULT (self));
+
+  if (priv->subtitle != subtitle)
+    {
+      g_free (priv->subtitle);
+      priv->subtitle = g_strdup (subtitle);
+    }
+}
+
+static void
+ide_search_result_set_score (IdeSearchResult *self,
+                             gfloat           score)
+{
+  IdeSearchResultPrivate *priv = ide_search_result_get_instance_private (self);
+
+  g_return_if_fail (IDE_IS_SEARCH_RESULT (self));
+  g_return_if_fail (score >= 0.0);
+  g_return_if_fail (score <= 1.0);
+
+  priv->score = score;
+}
+
+gint
+ide_search_result_compare (const IdeSearchResult *a,
+                           const IdeSearchResult *b)
+{
+  gfloat fa;
+  gfloat fb;
+
+  g_return_val_if_fail (IDE_IS_SEARCH_RESULT ((IdeSearchResult*)a), 0);
+  g_return_val_if_fail (IDE_IS_SEARCH_RESULT ((IdeSearchResult*)b), 0);
+
+  fa = ide_search_result_get_score ((IdeSearchResult *)a);
+  fb = ide_search_result_get_score ((IdeSearchResult *)b);
+
+  if (fa < fb)
+    return -1;
+  else if (fa > fb)
+    return 1;
+  else
+    return 0;
+}
+
 static void
 ide_search_result_finalize (GObject *object)
 {
   IdeSearchResult *self = (IdeSearchResult *)object;
   IdeSearchResultPrivate *priv = ide_search_result_get_instance_private (self);
 
+  g_clear_pointer (&priv->title, g_free);
+  g_clear_pointer (&priv->subtitle, g_free);
+
   G_OBJECT_CLASS (ide_search_result_parent_class)->finalize (object);
 }
 
@@ -51,6 +176,18 @@ ide_search_result_get_property (GObject    *object,
 
   switch (prop_id)
     {
+    case PROP_TITLE:
+      g_value_set_string (value, ide_search_result_get_title (self));
+      break;
+
+    case PROP_SUBTITLE:
+      g_value_set_string (value, ide_search_result_get_subtitle (self));
+      break;
+
+    case PROP_SCORE:
+      g_value_set_float (value, ide_search_result_get_score (self));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -66,6 +203,18 @@ ide_search_result_set_property (GObject      *object,
 
   switch (prop_id)
     {
+    case PROP_TITLE:
+      ide_search_result_set_title (self, g_value_get_string (value));
+      break;
+
+    case PROP_SUBTITLE:
+      ide_search_result_set_subtitle (self, g_value_get_string (value));
+      break;
+
+    case PROP_SCORE:
+      ide_search_result_set_score (self, g_value_get_float (value));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -79,6 +228,41 @@ ide_search_result_class_init (IdeSearchResultClass *klass)
   object_class->finalize = ide_search_result_finalize;
   object_class->get_property = ide_search_result_get_property;
   object_class->set_property = ide_search_result_set_property;
+
+  gParamSpecs [PROP_TITLE] =
+    g_param_spec_string ("title",
+                         _("Title"),
+                         _("The title of the search result."),
+                         NULL,
+                         (G_PARAM_READWRITE |
+                          G_PARAM_CONSTRUCT_ONLY |
+                          G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (object_class, PROP_TITLE,
+                                   gParamSpecs [PROP_TITLE]);
+
+  gParamSpecs [PROP_SUBTITLE] =
+    g_param_spec_string ("subtitle",
+                         _("Subtitle"),
+                         _("The subtitle of the search result."),
+                         NULL,
+                         (G_PARAM_READWRITE |
+                          G_PARAM_CONSTRUCT_ONLY |
+                          G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (object_class, PROP_SUBTITLE,
+                                   gParamSpecs [PROP_SUBTITLE]);
+
+  gParamSpecs [PROP_SCORE] =
+    g_param_spec_float ("score",
+                        _("Score"),
+                        _("The score of the search result."),
+                        0.0,
+                        1.0,
+                        0.0,
+                        (G_PARAM_READWRITE |
+                         G_PARAM_CONSTRUCT_ONLY |
+                         G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (object_class, PROP_SCORE,
+                                   gParamSpecs [PROP_SCORE]);
 }
 
 static void
diff --git a/libide/ide-search-result.h b/libide/ide-search-result.h
index 00d850e..0dd6391 100644
--- a/libide/ide-search-result.h
+++ b/libide/ide-search-result.h
@@ -30,8 +30,21 @@ G_DECLARE_DERIVABLE_TYPE (IdeSearchResult, ide_search_result, IDE, SEARCH_RESULT
 struct _IdeSearchResultClass
 {
   IdeObjectClass parent;
+
+  void (*activate) (IdeSearchResult *result);
 };
 
+IdeSearchResult *ide_search_result_new          (IdeContext            *context,
+                                                 const gchar           *title,
+                                                 const gchar           *subtitle,
+                                                 gfloat                 score);
+gfloat           ide_search_result_get_score    (IdeSearchResult       *result);
+const gchar     *ide_search_result_get_title    (IdeSearchResult       *result);
+const gchar     *ide_search_result_get_subtitle (IdeSearchResult       *result);
+gint             ide_search_result_compare      (const IdeSearchResult *a,
+                                                 const IdeSearchResult *b);
+void             ide_search_result_activate     (IdeSearchResult       *result);
+
 G_END_DECLS
 
 #endif /* IDE_SEARCH_RESULT_H */


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