[frogr] Implemented a more generic way to reorder pictures from the model



commit 37718423abe32d5348c2b853ae9327a8d2ae8e9c
Author: Mario Sanchez Prada <msanchez igalia com>
Date:   Sun Apr 24 04:45:37 2011 +0200

    Implemented a more generic way to reorder pictures from the model
    
    From now on, there won't be any more the need of having to define
    specific compare functions in the main view to be passed to the main
    view's model in order to properly sort the pictures. Instead, just
    passing the property name and the direction (ascending / descending)
    will be enough, and main view's model will take care of all the needed
    logic to properly perform the comparison in all the cases

 src/frogr-main-view-model.c |   65 +++++++++++++++++++++++++++++++++++++++++-
 src/frogr-main-view-model.h |    3 +-
 src/frogr-main-view.c       |   59 ++++++++-------------------------------
 3 files changed, 77 insertions(+), 50 deletions(-)
---
diff --git a/src/frogr-main-view-model.c b/src/frogr-main-view-model.c
index 868ed0a..fcebc5d 100644
--- a/src/frogr-main-view-model.c
+++ b/src/frogr-main-view-model.c
@@ -65,6 +65,58 @@ static guint signals[N_SIGNALS] = { 0 };
 
 /* Private API */
 
+static gint
+_compare_pictures_by_property_asc (FrogrPicture *p1, FrogrPicture *p2,
+                                   const gchar *property_name)
+{
+  g_return_val_if_fail (FROGR_IS_PICTURE (p1), 0);
+  g_return_val_if_fail (FROGR_IS_PICTURE (p2), 0);
+
+  GParamSpec *pspec1 = NULL;
+  GParamSpec *pspec2 = NULL;
+  GValue value1 = { 0 };
+  GValue value2 = { 0 };
+  gint result = 0;
+
+  pspec1 = g_object_class_find_property (G_OBJECT_GET_CLASS (p1), property_name);
+  pspec2 = g_object_class_find_property (G_OBJECT_GET_CLASS (p2), property_name);
+
+  /* GObjects not supported */
+  if (pspec1->value_type == G_TYPE_OBJECT)
+    return 0;
+
+  /* They should be the same! */
+  if (pspec1->value_type != pspec2->value_type)
+    return 0;
+
+  g_value_init (&value1, pspec1->value_type);
+  g_value_init (&value2, pspec1->value_type);
+
+  g_object_get_property (G_OBJECT (p1), property_name, &value1);
+  g_object_get_property (G_OBJECT (p2), property_name, &value2);
+
+  if (G_VALUE_HOLDS_BOOLEAN (&value1))
+    result = g_value_get_boolean (&value1) - g_value_get_boolean (&value2);
+  else if (G_VALUE_HOLDS_INT (&value1))
+    result = g_value_get_int (&value1) - g_value_get_int (&value2);
+  else if (G_VALUE_HOLDS_LONG (&value1))
+    result = g_value_get_long (&value1) - g_value_get_long (&value2);
+  else if (G_VALUE_HOLDS_STRING (&value1))
+    result = g_strcmp0 (g_value_get_string (&value1), g_value_get_string (&value2));
+
+  g_value_unset (&value1);
+  g_value_unset (&value2);
+
+  return result;
+}
+
+static gint
+_compare_pictures_by_property_desc (FrogrPicture *p1, FrogrPicture *p2,
+                                   const gchar *property_name)
+{
+  return _compare_pictures_by_property_asc (p2, p1, property_name);
+}
+
 static void
 _frogr_main_view_model_dispose (GObject* object)
 {
@@ -240,11 +292,13 @@ frogr_main_view_model_get_pictures (FrogrMainViewModel *self)
 
 void
 frogr_main_view_model_reorder_pictures (FrogrMainViewModel *self,
-                                        GCompareFunc compare_func)
+                                        const gchar *property_name,
+                                        gboolean ascending)
 {
   g_return_if_fail(FROGR_IS_MAIN_VIEW_MODEL (self));
 
   FrogrMainViewModelPrivate *priv = NULL;
+  GCompareDataFunc compare_func = NULL;
   GSList *old_list = NULL;
   GSList *old_item = NULL;
   gint *new_order = 0;
@@ -258,7 +312,14 @@ frogr_main_view_model_reorder_pictures (FrogrMainViewModel *self,
   old_list = g_slist_copy (priv->pictures_list);
   new_order = g_new0 (gint, g_slist_length (old_list));
 
-  priv->pictures_list = g_slist_sort (priv->pictures_list, compare_func);
+  if (ascending)
+    compare_func = (GCompareDataFunc) _compare_pictures_by_property_asc;
+  else
+    compare_func = (GCompareDataFunc) _compare_pictures_by_property_desc;
+
+  priv->pictures_list = g_slist_sort_with_data (priv->pictures_list,
+                                                compare_func,
+                                                (gchar*) property_name);
 
   /* Build the new_order array */
   old_pos = 0;
diff --git a/src/frogr-main-view-model.h b/src/frogr-main-view-model.h
index c63329a..b856d43 100644
--- a/src/frogr-main-view-model.h
+++ b/src/frogr-main-view-model.h
@@ -67,7 +67,8 @@ guint frogr_main_view_model_n_pictures (FrogrMainViewModel *self);
 GSList *frogr_main_view_model_get_pictures (FrogrMainViewModel *self);
 
 void frogr_main_view_model_reorder_pictures (FrogrMainViewModel *self,
-                                             GCompareFunc compare_func);
+                                             const gchar *property_name,
+                                             gboolean ascending);
 
 void frogr_main_view_model_add_set (FrogrMainViewModel *self,
                                     FrogrPhotoSet *fset);
diff --git a/src/frogr-main-view.c b/src/frogr-main-view.c
index 86aa32b..9f705a4 100644
--- a/src/frogr-main-view.c
+++ b/src/frogr-main-view.c
@@ -195,11 +195,6 @@ static void _load_pictures (FrogrMainView *self, GSList *fileuris);
 static void _upload_pictures (FrogrMainView *self);
 static void _reorder_pictures (FrogrMainView *self, SortingCriteria criteria);
 
-static gint _compare_pictures_by_title_asc (FrogrPicture *p1, FrogrPicture *p2);
-static gint _compare_pictures_by_title_desc (FrogrPicture *p1, FrogrPicture *p2);
-static gint _compare_pictures_by_date_asc (FrogrPicture *p1, FrogrPicture *p2);
-static gint _compare_pictures_by_date_desc (FrogrPicture *p1, FrogrPicture *p2);
-
 static void _progress_dialog_response (GtkDialog *dialog,
                                        gint response_id,
                                        gpointer data);
@@ -1285,63 +1280,33 @@ static void
 _reorder_pictures (FrogrMainView *self, SortingCriteria criteria)
 {
   FrogrMainViewPrivate *priv = FROGR_MAIN_VIEW_GET_PRIVATE (self);
-  GCompareFunc compare_func = NULL;
+  gchar *property_name = NULL;
+  gboolean ascending = FALSE;
 
   switch (criteria)
     {
     case SORT_BY_TITLE_ASC:
-      compare_func = (GCompareFunc)_compare_pictures_by_title_asc;
+      property_name = g_strdup ("title");
+      ascending = TRUE;
       break;
     case SORT_BY_TITLE_DESC:
-      compare_func = (GCompareFunc)_compare_pictures_by_title_desc;
+      property_name = g_strdup ("title");
+      ascending = FALSE;
       break;
     case SORT_BY_DATE_ASC:
-      compare_func = (GCompareFunc)_compare_pictures_by_date_asc;
+      property_name = g_strdup ("datetime");
+      ascending = TRUE;
       break;
     case SORT_BY_DATE_DESC:
-      compare_func = (GCompareFunc)_compare_pictures_by_date_desc;
+      property_name = g_strdup ("datetime");
+      ascending = FALSE;
       break;
     default:
       g_assert_not_reached ();
     }
 
-frogr_main_view_model_reorder_pictures (priv->model, compare_func);
-}
-
-static gint
-_compare_pictures_by_title_asc (FrogrPicture *p1, FrogrPicture *p2)
-{
-  g_return_val_if_fail (FROGR_IS_PICTURE (p1), 0);
-  g_return_val_if_fail (FROGR_IS_PICTURE (p2), 0);
-
-  const gchar *title_p1 = frogr_picture_get_title (FROGR_PICTURE (p1));
-  const gchar *title_p2 = frogr_picture_get_title (FROGR_PICTURE (p2));
-
-  return g_strcmp0 (title_p1, title_p2);
-}
-
-static gint
-_compare_pictures_by_title_desc (FrogrPicture *p1, FrogrPicture *p2)
-{
-  return _compare_pictures_by_title_asc (p2, p1);
-}
-
-static gint
-_compare_pictures_by_date_asc (FrogrPicture *p1, FrogrPicture *p2)
-{
-  g_return_val_if_fail (FROGR_IS_PICTURE (p1), 0);
-  g_return_val_if_fail (FROGR_IS_PICTURE (p2), 0);
-
-  glong datetime_p1 = frogr_picture_get_datetime (FROGR_PICTURE (p1));
-  glong datetime_p2 = frogr_picture_get_datetime (FROGR_PICTURE (p2));
-
-  return datetime_p1 - datetime_p2;
-}
-
-static gint
-_compare_pictures_by_date_desc (FrogrPicture *p1, FrogrPicture *p2)
-{
-  return _compare_pictures_by_date_asc (p2, p1);
+  frogr_main_view_model_reorder_pictures (priv->model, property_name, ascending);
+  g_free (property_name);
 }
 
 static void



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