[gnome-builder] GbSearchResult: add score property



commit 172d733708cf475614d0eeb671e98d1f46fc0e2a
Author: Christian Hergert <christian hergert me>
Date:   Mon Dec 15 21:42:32 2014 -0800

    GbSearchResult: add score property

 src/search/gb-search-result.c |   54 +++++++++++++++++++++++++++++++++++++----
 src/search/gb-search-result.h |    7 ++++-
 2 files changed, 54 insertions(+), 7 deletions(-)
---
diff --git a/src/search/gb-search-result.c b/src/search/gb-search-result.c
index 879c2c6..3de3c56 100644
--- a/src/search/gb-search-result.c
+++ b/src/search/gb-search-result.c
@@ -16,6 +16,8 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <glib/gi18n.h>
+
 #include "gb-search-result.h"
 
 struct _GbSearchResultPrivate
@@ -28,6 +30,7 @@ G_DEFINE_TYPE_WITH_PRIVATE (GbSearchResult, gb_search_result, GTK_TYPE_BIN)
 
 enum {
   PROP_0,
+  PROP_SCORE,
   LAST_PROP
 };
 
@@ -45,6 +48,28 @@ gb_search_result_new (void)
   return g_object_new (GB_TYPE_SEARCH_RESULT, NULL);
 }
 
+gfloat
+gb_search_result_get_score (GbSearchResult *result)
+{
+  g_return_val_if_fail (GB_IS_SEARCH_RESULT (result), 0.0f);
+
+  return result->priv->score;
+}
+
+void
+gb_search_result_set_score (GbSearchResult *result,
+                            gfloat          score)
+{
+  g_return_if_fail (GB_IS_SEARCH_RESULT (result));
+
+  if (result->priv->score != score)
+    {
+      result->priv->score = score;
+      g_object_notify_by_pspec (G_OBJECT (result),
+                                gParamSpecs [PROP_SCORE]);
+    }
+}
+
 gint
 gb_search_result_compare_func (gconstpointer result1,
                                gconstpointer result2)
@@ -53,13 +78,13 @@ gb_search_result_compare_func (gconstpointer result1,
   const GbSearchResult *r2 = result2;
   gint ret;
 
-  ret = r1->priv->priority - r2->priv->priority;
+  ret = r2->priv->priority - r1->priv->priority;
 
   if (ret == 0)
     {
-      if (r1->priv->score > r2->priv->score)
+      if (r2->priv->score > r1->priv->score)
         return 1;
-      else if (r1->priv->score < r2->priv->score)
+      else if (r2->priv->score < r1->priv->score)
         return -1;
     }
 
@@ -86,10 +111,14 @@ gb_search_result_get_property (GObject    *object,
                                GValue     *value,
                                GParamSpec *pspec)
 {
-  //GbSearchResult *self = GB_SEARCH_RESULT (object);
+  GbSearchResult *self = GB_SEARCH_RESULT (object);
 
   switch (prop_id)
     {
+    case PROP_SCORE:
+      g_value_set_float (value, gb_search_result_get_score (self));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -101,10 +130,14 @@ gb_search_result_set_property (GObject      *object,
                                const GValue *value,
                                GParamSpec   *pspec)
 {
-  //GbSearchResult *self = GB_SEARCH_RESULT (object);
+  GbSearchResult *self = GB_SEARCH_RESULT (object);
 
   switch (prop_id)
     {
+    case PROP_SCORE:
+      gb_search_result_set_score (self, g_value_get_float (value));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -119,6 +152,17 @@ gb_search_result_class_init (GbSearchResultClass *klass)
   object_class->get_property = gb_search_result_get_property;
   object_class->set_property = gb_search_result_set_property;
 
+  gParamSpecs [PROP_SCORE] =
+    g_param_spec_float ("score",
+                        _("Score"),
+                        _("The score for the result."),
+                        0.0f,
+                        1.0f,
+                        0.0f,
+                        (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (object_class, PROP_SCORE,
+                                   gParamSpecs [PROP_SCORE]);
+
   gSignals [ACTIVATE] =
     g_signal_new ("activate",
                   G_TYPE_FROM_CLASS (klass),
diff --git a/src/search/gb-search-result.h b/src/search/gb-search-result.h
index 73b14ec..db24fec 100644
--- a/src/search/gb-search-result.h
+++ b/src/search/gb-search-result.h
@@ -49,10 +49,13 @@ struct _GbSearchResultClass
 };
 
 void       gb_search_result_activate     (GbSearchResult *result);
-gint       gb_search_result_compare_func (gconstpointer result1,
-                                          gconstpointer result2);
+gint       gb_search_result_compare_func (gconstpointer   result1,
+                                          gconstpointer   result2);
 GType      gb_search_result_get_type     (void);
 GtkWidget *gb_search_result_new          (void);
+gfloat     gb_search_result_get_score    (GbSearchResult *result);
+void       gb_search_result_set_score    (GbSearchResult *result,
+                                          gfloat          score);
 
 G_END_DECLS
 


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