summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Uebernickel <lars.uebernickel@canonical.com>2014-05-01 20:04:32 +0200
committerRyan Lortie <desrt@desrt.ca>2014-10-05 17:14:25 -0400
commit1edbaec7b77218e659797d77daed8d0a84123e5e (patch)
treebf8f53789946d4f1b354409855a4a222a78f6d41
parent682bca095068d2823a129bebae42bb4f27f3e118 (diff)
downloadglib-wip/listmodel.tar.gz
Add GListModelwip/listmodel
GListModel is an interface that represents a dynamic list of GObjects. Also add GListStore, a simple implementation of GListModel that stores all objects in memory. https://bugzilla.gnome.org/show_bug.cgi?id=729351
-rw-r--r--docs/reference/gio/gio-sections.txt37
-rw-r--r--gio/Makefile.am4
-rw-r--r--gio/gio.h2
-rw-r--r--gio/giotypes.h2
-rw-r--r--gio/glistmodel.c164
-rw-r--r--gio/glistmodel.h71
-rw-r--r--gio/gliststore.c366
-rw-r--r--gio/gliststore.h66
-rw-r--r--gio/tests/Makefile.am1
-rw-r--r--gio/tests/glistmodel.c132
10 files changed, 845 insertions, 0 deletions
diff --git a/docs/reference/gio/gio-sections.txt b/docs/reference/gio/gio-sections.txt
index 80980fd6d..2c0bee2ad 100644
--- a/docs/reference/gio/gio-sections.txt
+++ b/docs/reference/gio/gio-sections.txt
@@ -4244,3 +4244,40 @@ G_TYPE_NOTIFICATION
G_TYPE_NOTIFICATION_BACKEND
g_notification_get_type
</SECTION>
+
+<SECTION>
+<FILE>glistmodel</FILE>
+<TITLE>GListModel</TITLE>
+GListModel
+g_list_model_new
+<SUBSECTION>
+g_list_model_get_type
+g_list_model_get_item_type
+g_list_model_get_n_items
+g_list_model_get_item
+g_list_model_items_changed
+<SUBSECTION>
+G_TYPE_LIST_MODEL
+G_LIST_MODEL
+G_IS_LIST_MODEL
+G_LIST_MODEL_GET_IFACE
+</SECTION>
+
+<SECTION>
+<FILE>gliststore</FILE>
+<TITLE>GListStore</TITLE>
+GListStore
+g_list_store_new
+<SUBSECTION>
+g_list_store_get_type
+g_list_store_new
+g_list_store_insert
+g_list_store_append
+g_list_store_remove
+g_list_store_remove_all
+g_list_store_splice
+<SUBSECTION>
+#define G_TYPE_LIST_STORE (g_list_store_get_type ())
+#define G_LIST_STORE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_LIST_STORE, GListStore))
+#define G_IS_LIST_STORE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_LIST_STORE))
+</SECTION>
diff --git a/gio/Makefile.am b/gio/Makefile.am
index e993e2fef..daf14a90f 100644
--- a/gio/Makefile.am
+++ b/gio/Makefile.am
@@ -470,6 +470,8 @@ libgio_2_0_la_SOURCES = \
gmountprivate.h \
gioenumtypes.h \
gioenumtypes.c \
+ glistmodel.c \
+ gliststore.c \
$(appinfo_sources) \
$(unix_sources) \
$(win32_sources) \
@@ -624,6 +626,8 @@ gio_headers = \
gvolumemonitor.h \
gzlibcompressor.h \
gzlibdecompressor.h \
+ glistmodel.h \
+ gliststore.h \
$(application_headers) \
$(settings_headers) \
$(gdbus_headers) \
diff --git a/gio/gio.h b/gio/gio.h
index 3cc4af37f..a7dbf46ee 100644
--- a/gio/gio.h
+++ b/gio/gio.h
@@ -160,6 +160,8 @@
#include <gio/gmenuexporter.h>
#include <gio/gdbusmenumodel.h>
#include <gio/gnotification.h>
+#include <gio/glistmodel.h>
+#include <gio/gliststore.h>
#undef __GIO_GIO_H_INSIDE__
diff --git a/gio/giotypes.h b/gio/giotypes.h
index de62cefa2..bab254d2f 100644
--- a/gio/giotypes.h
+++ b/gio/giotypes.h
@@ -61,6 +61,8 @@ typedef struct _GPermission GPermission;
typedef struct _GMenuModel GMenuModel;
typedef struct _GNotification GNotification;
+typedef struct _GListModel GListModel;
+typedef struct _GListStore GListStore;
/**
* GDrive:
diff --git a/gio/glistmodel.c b/gio/glistmodel.c
new file mode 100644
index 000000000..6fb65d4b2
--- /dev/null
+++ b/gio/glistmodel.c
@@ -0,0 +1,164 @@
+/*
+ * Copyright © 2014 Lars Uebernickel
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Lars Uebernickel <lars@uebernic.de>
+ */
+
+#include "config.h"
+
+#include "glistmodel.h"
+
+G_DEFINE_INTERFACE (GListModel, g_list_model, G_TYPE_OBJECT);
+
+/**
+ * SECTION:glistmodel
+ * @title: GListModel
+ * @short_description: An interface describing a dynamic list of objects
+ * @include: gio/gio.h
+ *
+ * #GListModel is a dynamic list of #GObjects.
+ */
+
+/**
+ * GListModel:
+ * @get_item_type: the virtual function pointer for g_list_model_get_item_type()
+ * @get_n_items: the virtual function pointer for g_list_model_get_n_items()
+ * @get_item: the virtual function pointer for g_list_model_get_item()
+ *
+ * The virtual function table for #GListModel.
+ *
+ * Since: 2.42
+ */
+
+static guint g_list_model_changed_signal;
+
+static void
+g_list_model_default_init (GListModelInterface *iface)
+{
+ /**
+ * GListModel::items-changed
+ * @list: the #GListModel that changed
+ * @position: the position at which @list changed
+ * @removed: the number of items removed
+ * @added: the number of items added
+ *
+ * This signal is emitted whenever items were added or removed to
+ * @list. At @position, @removed items were removed and @added items
+ * were added in their place.
+ *
+ * Since: 2.42
+ */
+ g_list_model_changed_signal = g_signal_new ("items-changed",
+ G_TYPE_LIST_MODEL,
+ G_SIGNAL_RUN_LAST,
+ 0,
+ NULL, NULL,
+ g_cclosure_marshal_generic,
+ G_TYPE_NONE,
+ 3, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT);
+}
+
+/**
+ * g_list_model_get_item_type:
+ * @list: a @GListModel
+ *
+ * Gets the type of the items in @list. All items returned from
+ * g_list_model_get_type() are of that type.
+ *
+ * Returns: the #GType of the items contained in @list.
+ *
+ * Since: 2.42
+ */
+GType
+g_list_model_get_item_type (GListModel *list)
+{
+ g_return_val_if_fail (G_IS_LIST_MODEL (list), G_TYPE_NONE);
+
+ return G_LIST_MODEL_GET_IFACE (list)->get_item_type (list);
+}
+
+/**
+ * g_list_model_get_n_items:
+ * @list: a @GListModel
+ *
+ * Gets the number of items in @list.
+ *
+ * Returns: the number of items in @list.
+ *
+ * Since: 2.42
+ */
+guint
+g_list_model_get_n_items (GListModel *list)
+{
+ g_return_if_fail (G_IS_LIST_MODEL (list));
+
+ return G_LIST_MODEL_GET_IFACE (list)->get_n_items (list);
+}
+
+/**
+ * g_list_model_get_item:
+ * @list: a @GListModel
+ * @position: the position of the item to fetch
+ *
+ * Get the item at @position. If @position is greater than the number of
+ * items in @list, %NULL is returned.
+ *
+ * %NULL is never returned for an index that is smaller than the length
+ * of the list.
+ *
+ * Returns: (transfer full) (allow-none) (type GObject): the item at
+ * @position.
+ *
+ * Since: 2.42
+ */
+gpointer
+g_list_model_get_item (GListModel *list,
+ guint position)
+{
+ g_return_val_if_fail (G_IS_LIST_MODEL (list), NULL);
+
+ return G_LIST_MODEL_GET_IFACE (list)->get_item (list, position);
+}
+
+/**
+ * g_list_model_items_changed:
+ * @list: a @GListModel
+ * @position: the position at which @list changed
+ * @removed: the number of items removed
+ * @added: the number of items added
+ *
+ * Emits the #GListModel::items-changed signal on @list.
+ *
+ * This function should only be called by classes implementing
+ * #GListModel. It has to be called after the internal representation
+ * of @list has been updated, because handlers connected to this signal
+ * might query the new state of the list.
+ *
+ * Implementations may not emit this signal in response to a call to the
+ * #GListModel API.
+ *
+ * Since: 2.42
+ */
+void
+g_list_model_items_changed (GListModel *list,
+ guint position,
+ guint removed,
+ guint added)
+{
+ g_return_if_fail (G_IS_LIST_MODEL (list));
+
+ g_signal_emit (list, g_list_model_changed_signal, 0, position, removed, added);
+}
diff --git a/gio/glistmodel.h b/gio/glistmodel.h
new file mode 100644
index 000000000..0f626f12d
--- /dev/null
+++ b/gio/glistmodel.h
@@ -0,0 +1,71 @@
+/*
+ * Copyright © 2014 Lars Uebernickel
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Lars Uebernickel <lars@uebernic.de>
+ */
+
+#ifndef __G_LIST_MODEL_H__
+#define __G_LIST_MODEL_H__
+
+#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
+#error "Only <gio/gio.h> can be included directly."
+#endif
+
+#include <gio/giotypes.h>
+
+G_BEGIN_DECLS
+
+#define G_TYPE_LIST_MODEL (g_list_model_get_type ())
+#define G_LIST_MODEL(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_LIST_MODEL, GListModel))
+#define G_IS_LIST_MODEL(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_LIST_MODEL))
+#define G_LIST_MODEL_GET_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), G_TYPE_LIST_MODEL, GListModelInterface))
+
+typedef struct _GListModelInterface GListModelInterface;
+
+struct _GListModelInterface
+{
+ GTypeInterface interface;
+
+ GType (* get_item_type) (GListModel *list);
+
+ guint (* get_n_items) (GListModel *list);
+
+ gpointer (* get_item) (GListModel *list,
+ guint position);
+};
+
+GLIB_AVAILABLE_IN_2_42
+GType g_list_model_get_type (void) G_GNUC_CONST;
+
+GLIB_AVAILABLE_IN_2_42
+GType g_list_model_get_item_type (GListModel *list);
+
+GLIB_AVAILABLE_IN_2_42
+guint g_list_model_get_n_items (GListModel *list);
+
+GLIB_AVAILABLE_IN_2_42
+gpointer g_list_model_get_item (GListModel *list,
+ guint position);
+
+GLIB_AVAILABLE_IN_2_42
+void g_list_model_items_changed (GListModel *list,
+ guint position,
+ guint removed,
+ guint added);
+
+G_END_DECLS
+
+#endif
diff --git a/gio/gliststore.c b/gio/gliststore.c
new file mode 100644
index 000000000..0ebed315f
--- /dev/null
+++ b/gio/gliststore.c
@@ -0,0 +1,366 @@
+/*
+ * Copyright © 2014 Lars Uebernickel
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Lars Uebernickel <lars@uebernic.de>
+ */
+
+#include "config.h"
+
+#include "gliststore.h"
+#include "glistmodel.h"
+
+/**
+ * SECTION:glistmodel
+ * @title: GListStore
+ * @short_description: A simple implementation of #GListModel
+ * @include: gio/gio.h
+ *
+ * #GListStore is a simple implementation of #GListModel that stores all
+ * items in memory.
+ */
+
+typedef GObjectClass GListStoreClass;
+
+struct _GListStore
+{
+ GObject parent;
+
+ GType item_type;
+ GSequence *items;
+};
+
+enum
+{
+ PROP_0,
+ PROP_ITEM_TYPE,
+ N_PROPERTIES
+};
+
+static void g_list_store_iface_init (GListModelInterface *iface);
+
+G_DEFINE_TYPE_WITH_CODE (GListStore, g_list_store, G_TYPE_OBJECT,
+ G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, g_list_store_iface_init));
+
+static void
+g_list_store_dispose (GObject *object)
+{
+ GListStore *store = G_LIST_STORE (object);
+
+ g_sequence_free (store->items);
+
+ G_OBJECT_CLASS (g_list_store_parent_class)->dispose (object);
+}
+
+static void
+g_list_store_get_property (GObject *object,
+ guint property_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GListStore *store = G_LIST_STORE (object);
+
+ switch (property_id)
+ {
+ case PROP_ITEM_TYPE:
+ g_value_set_gtype (value, store->item_type);
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+ }
+}
+
+static void
+g_list_store_set_property (GObject *object,
+ guint property_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ GListStore *store = G_LIST_STORE (object);
+
+ switch (property_id)
+ {
+ case PROP_ITEM_TYPE: /* construct-only */
+ store->item_type = g_value_get_gtype (value);
+ if (!g_type_is_a (store->item_type, G_TYPE_OBJECT))
+ g_critical ("GListStore cannot store items of type '%s'. Items must be GObjects.",
+ g_type_name (store->item_type));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+ }
+}
+
+static void
+g_list_store_class_init (GListStoreClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->dispose = g_list_store_dispose;
+ object_class->get_property = g_list_store_get_property;
+ object_class->set_property = g_list_store_set_property;
+
+ /**
+ * GListStore:item-type:
+ *
+ * The type of items contained in this list store.
+ *
+ * Since: 2.42
+ **/
+ g_object_class_install_property (object_class, PROP_ITEM_TYPE,
+ g_param_spec_gtype ("item-type", "", "", G_TYPE_OBJECT,
+ G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+}
+
+static GType
+g_list_store_get_item_type (GListModel *list)
+{
+ GListStore *store = G_LIST_STORE (list);
+
+ return store->item_type;
+}
+
+static guint
+g_list_store_get_n_items (GListModel *list)
+{
+ GListStore *store = G_LIST_STORE (list);
+
+ return g_sequence_get_length (store->items);
+}
+
+static gpointer
+g_list_store_get_item (GListModel *list,
+ guint position)
+{
+ GListStore *store = G_LIST_STORE (list);
+ GSequenceIter *it;
+
+ it = g_sequence_get_iter_at_pos (store->items, position);
+
+ if (g_sequence_iter_is_end (it))
+ return NULL;
+ else
+ return g_object_ref (g_sequence_get (it));
+}
+
+static void
+g_list_store_iface_init (GListModelInterface *iface)
+{
+ iface->get_item_type = g_list_store_get_item_type;
+ iface->get_n_items = g_list_store_get_n_items;
+ iface->get_item = g_list_store_get_item;
+}
+
+static void
+g_list_store_init (GListStore *store)
+{
+ store->items = g_sequence_new (g_object_unref);
+}
+
+/**
+ * g_list_store_new:
+ * @item_type: the #Gtype of the items that will be stored in the list
+ *
+ * Creates a new #GListStore with items of type @item_type.
+ *
+ * Returns: a new #GListStore
+ * Since: 2.42
+ */
+GListStore *
+g_list_store_new (GType item_type)
+{
+ /* We only allow GObjects as item types right now. This might change
+ * in the future.
+ */
+ g_return_val_if_fail (g_type_is_a (item_type, G_TYPE_OBJECT), NULL);
+
+ return g_object_new (G_TYPE_LIST_STORE,
+ "item-type", item_type,
+ NULL);
+}
+
+/**
+ * g_list_store_insert:
+ * @store: a #GListStore
+ * @position: the position at which to insert the new item
+ * @item: the new item
+ *
+ * Inserts @item into @store at @position. @item must be of type
+ * #GListStore:item-type.
+ *
+ * Use g_list_store_splice() to insert multiple items at the same time
+ * efficiently.
+ *
+ * Since: 2.42
+ */
+void
+g_list_store_insert (GListStore *store,
+ guint position,
+ gpointer item)
+{
+ GSequenceIter *it;
+
+ g_return_if_fail (G_IS_LIST_STORE (store));
+ g_return_if_fail (g_type_is_a (G_OBJECT_TYPE (item), store->item_type));
+ g_return_if_fail (position <= g_sequence_get_length (store->items));
+
+ it = g_sequence_get_iter_at_pos (store->items, position);
+ g_sequence_insert_before (it, g_object_ref (item));
+
+ g_list_model_items_changed (G_LIST_MODEL (store), position, 0, 1);
+}
+
+/**
+ * g_list_store_append:
+ * @store: a #GListStore
+ * @item: the new item
+ *
+ * Appends @item to @store. @item must be of type #GListStore:item-type.
+ *
+ * Use g_list_store_splice() to append multiple items at the same time
+ * efficiently.
+ *
+ * Since: 2.42
+ */
+void
+g_list_store_append (GListStore *store,
+ gpointer item)
+{
+ guint n_items;
+
+ g_return_if_fail (G_IS_LIST_STORE (store));
+ g_return_if_fail (g_type_is_a (G_OBJECT_TYPE (item), store->item_type));
+
+ n_items = g_sequence_get_length (store->items);
+ g_sequence_append (store->items, g_object_ref (item));
+
+ g_list_model_items_changed (G_LIST_MODEL (store), n_items, 0, 1);
+}
+
+/**
+ * g_list_store_remove:
+ * @store: a #GListStore
+ * @position: the position of the item that is to be removed
+ *
+ * Removes the item from @store that is at @position.
+ *
+ * Use g_list_store_splice() to remove multiple items at the same time
+ * efficiently.
+ *
+ * Since: 2.42
+ */
+void
+g_list_store_remove (GListStore *store,
+ guint position)
+{
+ GSequenceIter *it;
+
+ g_return_if_fail (G_IS_LIST_STORE (store));
+
+ it = g_sequence_get_iter_at_pos (store->items, position);
+ g_return_if_fail (!g_sequence_iter_is_end (it));
+
+ g_sequence_remove (it);
+ g_list_model_items_changed (G_LIST_MODEL (store), position, 1, 0);
+}
+
+/**
+ * g_list_store_remove_all:
+ * @store: a #GListStore
+ *
+ * Removes all items from @store.
+ *
+ * Since: 2.42
+ */
+void
+g_list_store_remove_all (GListStore *store)
+{
+ guint n_items;
+
+ g_return_if_fail (G_IS_LIST_STORE (store));
+
+ n_items = g_sequence_get_length (store->items);
+ g_sequence_remove_range (g_sequence_get_begin_iter (store->items),
+ g_sequence_get_end_iter (store->items));
+
+ g_list_model_items_changed (G_LIST_MODEL (store), 0, n_items, 0);
+}
+
+/**
+ * g_list_store_splice:
+ * @store: a #GListStore
+ * @position: the position at which to make the change
+ * @removed: the number of items to remove
+ * @added: the number of items to add
+ * @items: the items to add
+ *
+ * Changes @store by removing @removed items and adding @added items to
+ * store. @items must contain @added items of type
+ * #GListStore::item-type.
+ *
+ * This function is more efficient than g_list_store_insert() and
+ * g_list_store_remove(), because it only emits
+ * #GListModel::items-changed once for the change.
+ *
+ * Since: 2.42
+ */
+void
+g_list_store_splice (GListStore *store,
+ guint position,
+ guint removed,
+ guint added,
+ gpointer *items)
+{
+ GSequenceIter *it;
+ guint n_items;
+
+ g_return_if_fail (G_IS_LIST_STORE (store));
+
+ n_items = g_sequence_get_length (store->items);
+ g_return_if_fail (position < n_items || (removed == 0 && position <= n_items));
+ g_return_if_fail (removed <= n_items - position);
+
+ it = g_sequence_get_iter_at_pos (store->items, position);
+
+ if (removed)
+ {
+ GSequenceIter *end;
+
+ end = g_sequence_iter_move (it, removed);
+ g_sequence_remove_range (it, end);
+
+ it = end;
+ }
+
+ if (added)
+ {
+ gint i;
+
+ it = g_sequence_iter_next (it);
+ for (i = 0; i < added; i++)
+ {
+ if (g_type_is_a (G_OBJECT_TYPE (items[i]), store->item_type))
+ it = g_sequence_insert_before (it, g_object_ref (items[i]));
+ else
+ g_critical ("%s: item %d is a %s instead of a %s",
+ G_STRFUNC, i, G_OBJECT_TYPE_NAME (items[i]),
+ g_type_name (store->item_type));
+ }
+ }
+
+ g_list_model_items_changed (G_LIST_MODEL (store), position, removed, added);
+}
diff --git a/gio/gliststore.h b/gio/gliststore.h
new file mode 100644
index 000000000..677ed5b67
--- /dev/null
+++ b/gio/gliststore.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright © 2014 Lars Uebernickel
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Lars Uebernickel <lars@uebernic.de>
+ */
+
+#ifndef __G_LIST_STORE_H__
+#define __G_LIST_STORE_H__
+
+#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
+#error "Only <gio/gio.h> can be included directly."
+#endif
+
+#include <gio/giotypes.h>
+
+G_BEGIN_DECLS
+
+#define G_TYPE_LIST_STORE (g_list_store_get_type ())
+#define G_LIST_STORE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_LIST_STORE, GListStore))
+#define G_IS_LIST_STORE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_LIST_STORE))
+
+GLIB_AVAILABLE_IN_2_42
+GType g_list_store_get_type (void) G_GNUC_CONST;
+
+GLIB_AVAILABLE_IN_2_42
+GListStore * g_list_store_new (GType item_type);
+
+GLIB_AVAILABLE_IN_2_42
+void g_list_store_insert (GListStore *store,
+ guint position,
+ gpointer item);
+
+GLIB_AVAILABLE_IN_2_42
+void g_list_store_append (GListStore *store,
+ gpointer item);
+
+GLIB_AVAILABLE_IN_2_42
+void g_list_store_remove (GListStore *store,
+ guint position);
+
+GLIB_AVAILABLE_IN_2_42
+void g_list_store_remove_all (GListStore *store);
+
+GLIB_AVAILABLE_IN_2_42
+void g_list_store_splice (GListStore *store,
+ guint position,
+ guint removed,
+ guint added,
+ gpointer *items);
+
+G_END_DECLS
+
+#endif
diff --git a/gio/tests/Makefile.am b/gio/tests/Makefile.am
index 4bb71a87c..b684b6e19 100644
--- a/gio/tests/Makefile.am
+++ b/gio/tests/Makefile.am
@@ -61,6 +61,7 @@ test_programs = \
tls-interaction \
vfs \
volumemonitor \
+ glistmodel \
$(NULL)
uninstalled_test_programs = \
diff --git a/gio/tests/glistmodel.c b/gio/tests/glistmodel.c
new file mode 100644
index 000000000..41e9e7b57
--- /dev/null
+++ b/gio/tests/glistmodel.c
@@ -0,0 +1,132 @@
+/*
+ * Copyright © 2014 Lars Uebernickel
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the licence or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Lars Uebernickel <lars@uebernic.de>
+ */
+
+#include <gio/gio.h>
+
+static void
+test_store_boundaries (void)
+{
+ GListStore *store;
+ GMenuItem *item;
+
+ store = g_list_store_new (G_TYPE_MENU_ITEM);
+
+ item = g_menu_item_new (NULL, NULL);
+ g_object_add_weak_pointer (G_OBJECT (item), (gpointer *) &item);
+
+ /* remove an item from an empty list */
+ g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*g_sequence*");
+ g_list_store_remove (store, 0);
+ g_test_assert_expected_messages ();
+
+ /* don't allow inserting an item past the end ... */
+ g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*g_sequence*");
+ g_list_store_insert (store, 1, item);
+ g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, 0);
+ g_test_assert_expected_messages ();
+
+ /* ... except exactly at the end */
+ g_list_store_insert (store, 0, item);
+ g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, 1);
+
+ /* remove a non-existing item at exactly the end of the list */
+ g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*g_sequence*");
+ g_list_store_remove (store, 1);
+ g_test_assert_expected_messages ();
+
+ g_list_store_remove (store, 0);
+ g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, 0);
+
+ /* splice beyond the end of the list */
+ g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*position*");
+ g_list_store_splice (store, 1, 0, 0, NULL);
+ g_test_assert_expected_messages ();
+
+ /* remove items from an empty list */
+ g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*position*");
+ g_list_store_splice (store, 0, 1, 0, NULL);
+ g_test_assert_expected_messages ();
+
+ g_list_store_append (store, item);
+ g_list_store_splice (store, 0, 1, 1, (gpointer *) &item);
+ g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, 1);
+
+ /* remove more items than exist */
+ g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*position*");
+ g_list_store_splice (store, 0, 5, 0, NULL);
+ g_test_assert_expected_messages ();
+ g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, 1);
+
+ g_object_unref (store);
+ g_object_unref (item);
+ g_assert_null (item);
+}
+
+static void
+test_store_refcounts (void)
+{
+ GListStore *store;
+ GMenuItem *items[10];
+ GMenuItem *tmp;
+ guint i;
+ guint n_items;
+
+ store = g_list_store_new (G_TYPE_MENU_ITEM);
+
+ g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, 0);
+ g_assert_null (g_list_model_get_item (G_LIST_MODEL (store), 0));
+
+ n_items = G_N_ELEMENTS (items);
+ for (i = 0; i < n_items; i++)
+ {
+ items[i] = g_menu_item_new (NULL, NULL);
+ g_object_add_weak_pointer (G_OBJECT (items[i]), (gpointer *) &items[i]);
+ g_list_store_append (store, items[i]);
+
+ g_object_unref (items[i]);
+ g_assert_nonnull (items[i]);
+ }
+
+ g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, n_items);
+ g_assert_null (g_list_model_get_item (G_LIST_MODEL (store), n_items));
+
+ tmp = g_list_model_get_item (G_LIST_MODEL (store), 3);
+ g_assert (tmp == items[3]);
+ g_object_unref (tmp);
+
+ g_list_store_remove (store, 4);
+ g_assert_null (items[4]);
+ n_items--;
+ g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (store)), ==, n_items);
+ g_assert_null (g_list_model_get_item (G_LIST_MODEL (store), n_items));
+
+ g_object_unref (store);
+ for (i = 0; i < G_N_ELEMENTS (items); i++)
+ g_assert_null (items[i]);
+}
+
+int main (int argc, char *argv[])
+{
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/glistmodel/store/boundaries", test_store_boundaries);
+ g_test_add_func ("/glistmodel/store/refcounts", test_store_refcounts);
+
+ return g_test_run ();
+}