[gnome-photos/wip/rishi/manager-model: 8/8] base-manager: Implement GListModel
- From: Debarshi Ray <debarshir src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-photos/wip/rishi/manager-model: 8/8] base-manager: Implement GListModel
- Date: Sat, 5 Nov 2016 14:59:41 +0000 (UTC)
commit c48e9dbe9cfa8a8a9af43b82a8483da08878b877
Author: Debarshi Ray <debarshir gnome org>
Date: Thu Oct 13 07:13:52 2016 +0200
base-manager: Implement GListModel
src/photos-base-manager.c | 210 ++++++++++++++++++++++++++++++++++++++++++---
src/photos-base-manager.h | 2 +-
2 files changed, 199 insertions(+), 13 deletions(-)
---
diff --git a/src/photos-base-manager.c b/src/photos-base-manager.c
index 26d89b3..2b18692 100644
--- a/src/photos-base-manager.c
+++ b/src/photos-base-manager.c
@@ -25,6 +25,7 @@
#include "config.h"
+#include <gio/gio.h>
#include <glib.h>
#include "photos-base-manager.h"
@@ -33,16 +34,23 @@
struct _PhotosBaseManagerPrivate
{
+ GCompareDataFunc *sort_func;
GHashTable *objects;
GObject *active_object;
+ GSequence *sequence;
+ GSequenceIter *last_iter;
gchar *action_id;
gchar *title;
+ gpointer sort_data;
+ guint last_position;
};
enum
{
PROP_0,
PROP_ACTION_ID,
+ PROP_SORT_DATA,
+ PROP_SORT_FUNC,
PROP_TITLE
};
@@ -57,8 +65,58 @@ enum
static guint signals[LAST_SIGNAL] = { 0 };
+static void photos_base_manager_list_model_iface_init (GListModelInterface *iface);
-G_DEFINE_TYPE_WITH_PRIVATE (PhotosBaseManager, photos_base_manager, G_TYPE_OBJECT);
+
+G_DEFINE_TYPE_WITH_CODE (PhotosBaseManager, photos_base_manager, G_TYPE_OBJECT,
+ G_ADD_PRIVATE (PhotosBaseManager)
+ G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,
photos_base_manager_list_model_iface_init));
+
+
+typedef struct _PhotosBaseManagerObjectData PhotosBaseManagerObjectData;
+
+struct _PhotosBaseManagerObjectData
+{
+ GObject *object;
+ GSequenceIter *iter;
+};
+
+
+static PhotosBaseManagerObjectData *
+photos_base_manager_object_data_new (GObject *object, GSequenceIter *iter)
+{
+ PhotosBaseManagerObjectData *object_data;
+
+ object_data = g_slice_new0 (PhotosBaseManagerObjectData);
+ object_data->object = g_object_ref (object);
+ object_data->iter = iter;
+ return object_data;
+}
+
+
+static void
+photos_base_manager_object_data_free (PhotosBaseManagerObjectData *object_data)
+{
+ g_object_unref (object_data->object);
+ g_slice_free (PhotosBaseManagerObjectData, object_data);
+}
+
+
+static void
+photos_base_manager_objects_changed (PhotosBaseManager *self, guint position, guint removed, guint added)
+{
+ PhotosBaseManagerPrivate *priv;
+
+ priv = photos_base_manager_get_instance_private (self);
+
+ if (position <= priv->last_position)
+ {
+ priv->last_iter = NULL;
+ priv->last_position = G_MAXUINT;
+ }
+
+ g_list_model_items_changed (G_LIST_MODEL (self), position, removed, added);
+}
static void
@@ -66,7 +124,10 @@ photos_base_manager_default_add_object (PhotosBaseManager *self, GObject *object
{
PhotosBaseManagerPrivate *priv;
GObject *old_object;
+ GSequenceIter *iter;
+ PhotosBaseManagerObjectData *object_data;
const gchar *id;
+ guint position;
priv = photos_base_manager_get_instance_private (self);
@@ -75,7 +136,21 @@ photos_base_manager_default_add_object (PhotosBaseManager *self, GObject *object
if (old_object != NULL)
return;
- g_hash_table_insert (priv->objects, g_strdup (id), g_object_ref (object));
+ if (priv->sort_func == NULL)
+ {
+ position = photos_base_manager_get_objects_count (self);
+ iter = g_sequence_append (priv->sequence, g_object_ref (object));
+ }
+ else
+ {
+ iter = g_sequence_insert_sorted (priv->sequence, g_object_ref (object), priv->sort_func,
priv->sort_data);
+ position = g_sequence_iter_get_position (iter);
+ }
+
+ object_data = photos_base_manager_object_data_new (object, iter);
+ g_hash_table_insert (priv->objects, g_strdup (id), object_data);
+
+ photos_base_manager_objects_changed (self, position, 0, 1);
g_signal_emit (self, signals[OBJECT_ADDED], 0, object);
}
@@ -102,13 +177,15 @@ photos_base_manager_default_get_object_by_id (PhotosBaseManager *self, const gch
{
PhotosBaseManagerPrivate *priv;
GObject *ret_val = NULL;
+ PhotosBaseManagerObjectData *object_data;
priv = photos_base_manager_get_instance_private (self);
if (id == NULL)
goto out;
- ret_val = g_hash_table_lookup (priv->objects, id);
+ object_data = g_hash_table_lookup (priv->objects, id);
+ ret_val = object_data->object;
out:
return ret_val;
@@ -137,16 +214,24 @@ photos_base_manager_default_remove_object_by_id (PhotosBaseManager *self, const
{
PhotosBaseManagerPrivate *priv;
GObject *object;
+ PhotosBaseManagerObjectData *object_data;
+ guint position;
priv = photos_base_manager_get_instance_private (self);
- object = photos_base_manager_get_object_by_id (self, id);
- if (object == NULL)
+ object_data = photos_base_manager_get_object_by_id (self, id);
+ if (object_data == NULL)
return;
- g_object_ref (object);
+ position = g_sequence_iter_get_position (object_data->iter);
+ g_sequence_remove (object_data->iter);
+
+ object = g_object_ref (object_data->object);
g_hash_table_remove (priv->objects, id);
+
+ photos_base_manager_objects_changed (self, position, 1, 0);
g_signal_emit (self, signals[OBJECT_REMOVED], 0, object);
+
g_object_unref (object);
}
@@ -166,6 +251,60 @@ photos_base_manager_default_set_active_object (PhotosBaseManager *self, GObject
}
+static gpointer
+photos_base_manager_get_item (GListModel *list, guint position)
+{
+ PhotosBaseManager *self = PHOTOS_BASE_MANAGER (list);
+ PhotosBaseManagerPrivate *priv;
+ GSequenceIter *iter = NULL;
+ gpointer ret_val = NULL;
+
+ priv = photos_base_manager_get_instance_private (self);
+
+ if (priv->last_position != G_MAXUINT)
+ {
+ if (priv->last_position == position + 1)
+ iter = g_sequence_iter_prev (priv->last_iter);
+ else if (priv->last_position == position - 1)
+ iter = g_sequence_iter_next (priv->last_iter);
+ else if (priv->last_position == position)
+ iter = priv->last_iter;
+ }
+
+ if (iter == NULL)
+ iter = g_sequence_get_iter_at_pos (priv->sequence, position);
+
+ priv->last_iter = iter;
+ priv->last_position = position;
+
+ if (g_sequence_iter_is_end (iter))
+ goto out;
+
+ ret_val = g_object_ref (g_sequence_get (iter));
+
+ out:
+ return ret_val;
+}
+
+
+static GType
+photos_base_manager_get_item_type (GListModel *list)
+{
+ return PHOTOS_TYPE_FILTERABLE;
+}
+
+
+static guint
+photos_base_manager_get_n_items (GListModel *list)
+{
+ PhotosBaseManager *self = PHOTOS_BASE_MANAGER (list);
+ guint count;
+
+ count = photos_base_manager_get_objects_count (self);
+ return count;
+}
+
+
static void
photos_base_manager_dispose (GObject *object)
{
@@ -181,6 +320,7 @@ photos_base_manager_dispose (GObject *object)
}
g_clear_object (&priv->active_object);
+ g_clear_pointer (&priv->sequence, (GDestroyNotify) g_sequence_free);
G_OBJECT_CLASS (photos_base_manager_parent_class)->dispose (object);
}
@@ -215,6 +355,14 @@ photos_base_manager_set_property (GObject *object, guint prop_id, const GValue *
priv->action_id = g_value_dup_string (value);
break;
+ case PROP_SORT_DATA:
+ priv->sort_data = g_value_get_pointer (value);
+ break;
+
+ case PROP_SORT_FUNC:
+ priv->sort_func = g_value_get_pointer (value);
+ break;
+
case PROP_TITLE:
priv->title = g_value_dup_string (value);
break;
@@ -232,7 +380,12 @@ photos_base_manager_init (PhotosBaseManager *self)
PhotosBaseManagerPrivate *priv;
priv = photos_base_manager_get_instance_private (self);
- priv->objects = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
+ priv->objects = g_hash_table_new_full (g_str_hash,
+ g_str_equal,
+ g_free,
+ (GDestroyNotify) photos_base_manager_object_data_free);
+ priv->sequence = g_sequence_new (g_object_unref);
+ priv->last_position = G_MAXUINT;
}
@@ -262,6 +415,20 @@ photos_base_manager_class_init (PhotosBaseManagerClass *class)
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
g_object_class_install_property (object_class,
+ PROP_SORT_DATA,
+ g_param_spec_pointer ("sort-data",
+ "Sort data",
+ "Arbitrary data to be passed to the comparison
function",
+ G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
+
+ g_object_class_install_property (object_class,
+ PROP_SORT_FUNC,
+ g_param_spec_pointer ("sort-func",
+ "Sort function",
+ "Pairwise comparison function for sorting",
+ G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
+
+ g_object_class_install_property (object_class,
PROP_TITLE,
g_param_spec_string ("title",
"Title",
@@ -314,10 +481,19 @@ photos_base_manager_class_init (PhotosBaseManagerClass *class)
}
+static void
+photos_base_manager_list_model_iface_init (GListModelInterface *iface)
+{
+ iface->get_item = photos_base_manager_get_item;
+ iface->get_item_type = photos_base_manager_get_item_type;
+ iface->get_n_items = photos_base_manager_get_n_items;
+}
+
+
PhotosBaseManager *
-photos_base_manager_new (void)
+photos_base_manager_new (GCompareDataFunc sort_func, gpointer sort_data)
{
- return g_object_new (PHOTOS_TYPE_BASE_MANAGER, NULL);
+ return g_object_new (PHOTOS_TYPE_BASE_MANAGER, "sort-func", sort_func, NULL);
}
@@ -335,13 +511,23 @@ void
photos_base_manager_clear (PhotosBaseManager *self)
{
PhotosBaseManagerPrivate *priv;
+ GSequenceIter *begin_iter;
+ GSequenceIter *end_iter;
+ guint count;
g_return_if_fail (PHOTOS_IS_BASE_MANAGER (self));
priv = photos_base_manager_get_instance_private (self);
+ begin_iter = g_sequence_get_begin_iter (priv->sequence);
+ end_iter = g_sequence_get_end_iter (priv->sequence);
+ g_sequence_remove_range (begin_iter, end_iter);
+
g_hash_table_remove_all (priv->objects);
g_clear_object (&priv->active_object);
+
+ count = photos_base_manager_get_objects_count (self);
+ photos_base_manager_objects_changed (self, 0, count, 0);
g_signal_emit (self, signals[CLEAR], 0);
}
@@ -371,7 +557,7 @@ photos_base_manager_get_all_filter (PhotosBaseManager *self)
{
PhotosBaseManagerPrivate *priv;
GHashTableIter iter;
- GObject *object;
+ PhotosBaseManagerObjectData *object_data;
const gchar *blank = "(true)";
const gchar *id;
gchar *filter;
@@ -389,13 +575,13 @@ photos_base_manager_get_all_filter (PhotosBaseManager *self)
i = 0;
g_hash_table_iter_init (&iter, priv->objects);
- while (g_hash_table_iter_next (&iter, (gpointer *) &id, (gpointer *) &object))
+ while (g_hash_table_iter_next (&iter, (gpointer *) &id, (gpointer *) &object_data))
{
if (g_strcmp0 (id, "all") != 0)
{
gchar *str;
- str = photos_filterable_get_filter (PHOTOS_FILTERABLE (object));
+ str = photos_filterable_get_filter (PHOTOS_FILTERABLE (object_data->object));
if (g_strcmp0 (str, blank) == 0)
g_free (str);
else
diff --git a/src/photos-base-manager.h b/src/photos-base-manager.h
index e6298d5..5a40900 100644
--- a/src/photos-base-manager.h
+++ b/src/photos-base-manager.h
@@ -55,7 +55,7 @@ struct _PhotosBaseManagerClass
void (*object_removed) (PhotosBaseManager *self, GObject *object);
};
-PhotosBaseManager *photos_base_manager_new (void);
+PhotosBaseManager *photos_base_manager_new (GCompareDataFunc sort_func, gpointer
sort_data);
void photos_base_manager_add_object (PhotosBaseManager *self, GObject *object);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]