Blame gio/glistmodel.c

Packit ae235b
/*
Packit ae235b
 * Copyright 2015 Lars Uebernickel
Packit ae235b
 * Copyright 2015 Ryan Lortie
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Authors:
Packit ae235b
 *     Lars Uebernickel <lars@uebernic.de>
Packit ae235b
 *     Ryan Lortie <desrt@desrt.ca>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "glistmodel.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
G_DEFINE_INTERFACE (GListModel, g_list_model, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:glistmodel
Packit ae235b
 * @title: GListModel
Packit ae235b
 * @short_description: An interface describing a dynamic list of objects
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GListStore
Packit ae235b
 *
Packit ae235b
 * #GListModel is an interface that represents a mutable list of
Packit ae235b
 * #GObjects. Its main intention is as a model for various widgets in
Packit ae235b
 * user interfaces, such as list views, but it can also be used as a
Packit ae235b
 * convenient method of returning lists of data, with support for
Packit ae235b
 * updates.
Packit ae235b
 *
Packit ae235b
 * Each object in the list may also report changes in itself via some
Packit ae235b
 * mechanism (normally the #GObject::notify signal).  Taken together
Packit ae235b
 * with the #GListModel::items-changed signal, this provides for a list
Packit ae235b
 * that can change its membership, and in which the members can change
Packit ae235b
 * their individual properties.
Packit ae235b
 *
Packit ae235b
 * A good example would be the list of visible wireless network access
Packit ae235b
 * points, where each access point can report dynamic properties such as
Packit ae235b
 * signal strength.
Packit ae235b
 *
Packit ae235b
 * It is important to note that the #GListModel itself does not report
Packit ae235b
 * changes to the individual items.  It only reports changes to the list
Packit ae235b
 * membership.  If you want to observe changes to the objects themselves
Packit ae235b
 * then you need to connect signals to the objects that you are
Packit ae235b
 * interested in.
Packit ae235b
 *
Packit ae235b
 * All items in a #GListModel are of (or derived from) the same type.
Packit ae235b
 * g_list_model_get_item_type() returns that type.  The type may be an
Packit ae235b
 * interface, in which case all objects in the list must implement it.
Packit ae235b
 *
Packit ae235b
 * The semantics are close to that of an array:
Packit ae235b
 * g_list_model_get_n_items() returns the number of items in the list and
Packit ae235b
 * g_list_model_get_item() returns an item at a (0-based) position. In
Packit ae235b
 * order to allow implementations to calculate the list length lazily,
Packit ae235b
 * you can also iterate over items: starting from 0, repeatedly call
Packit ae235b
 * g_list_model_get_item() until it returns %NULL.
Packit ae235b
 *
Packit ae235b
 * An implementation may create objects lazily, but must take care to
Packit ae235b
 * return the same object for a given position until all references to
Packit ae235b
 * it are gone.
Packit ae235b
 *
Packit ae235b
 * On the other side, a consumer is expected only to hold references on
Packit ae235b
 * objects that are currently "user visible", in order to faciliate the
Packit ae235b
 * maximum level of laziness in the implementation of the list and to
Packit ae235b
 * reduce the required number of signal connections at a given time.
Packit ae235b
 *
Packit ae235b
 * This interface is intended only to be used from a single thread.  The
Packit ae235b
 * thread in which it is appropriate to use it depends on the particular
Packit ae235b
 * implementation, but typically it will be from the thread that owns
Packit ae235b
 * the [thread-default main context][g-main-context-push-thread-default]
Packit ae235b
 * in effect at the time that the model was created.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GListModelInterface:
Packit ae235b
 * @g_iface: parent #GTypeInterface
Packit ae235b
 * @get_item_type: the virtual function pointer for g_list_model_get_item_type()
Packit ae235b
 * @get_n_items: the virtual function pointer for g_list_model_get_n_items()
Packit ae235b
 * @get_item: the virtual function pointer for g_list_model_get_item()
Packit ae235b
 *
Packit ae235b
 * The virtual function table for #GListModel.
Packit ae235b
 *
Packit ae235b
 * Since: 2.44
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GListModelInterface::get_item:
Packit ae235b
 * @list: a #GListModel
Packit ae235b
 * @position: the position of the item to fetch
Packit ae235b
 *
Packit ae235b
 * Get the item at @position. If @position is greater than the number of
Packit ae235b
 * items in @list, %NULL is returned.
Packit ae235b
 *
Packit ae235b
 * %NULL is never returned for an index that is smaller than the length
Packit ae235b
 * of the list.  See g_list_model_get_n_items().
Packit ae235b
 *
Packit ae235b
 * Returns: (type GObject) (transfer full) (nullable): the object at @position.
Packit ae235b
 *
Packit ae235b
 * Since: 2.44
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GListModel:
Packit ae235b
 *
Packit ae235b
 * #GListModel is an opaque data structure and can only be accessed
Packit ae235b
 * using the following functions.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
static guint g_list_model_changed_signal;
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_list_model_default_init (GListModelInterface *iface)
Packit ae235b
{
Packit ae235b
  /**
Packit ae235b
   * GListModel::items-changed:
Packit ae235b
   * @list: the #GListModel that changed
Packit ae235b
   * @position: the position at which @list changed
Packit ae235b
   * @removed: the number of items removed
Packit ae235b
   * @added: the number of items added
Packit ae235b
   *
Packit ae235b
   * This signal is emitted whenever items were added or removed to
Packit ae235b
   * @list. At @position, @removed items were removed and @added items
Packit ae235b
   * were added in their place.
Packit ae235b
   *
Packit ae235b
   * Since: 2.44
Packit ae235b
   */
Packit ae235b
  g_list_model_changed_signal = g_signal_new (I_("items-changed"),
Packit ae235b
                                              G_TYPE_LIST_MODEL,
Packit ae235b
                                              G_SIGNAL_RUN_LAST,
Packit ae235b
                                              0,
Packit ae235b
                                              NULL, NULL,
Packit ae235b
                                              g_cclosure_marshal_generic,
Packit ae235b
                                              G_TYPE_NONE,
Packit ae235b
                                              3, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_list_model_get_item_type:
Packit ae235b
 * @list: a #GListModel
Packit ae235b
 *
Packit ae235b
 * Gets the type of the items in @list. All items returned from
Packit ae235b
 * g_list_model_get_type() are of that type or a subtype, or are an
Packit ae235b
 * implementation of that interface.
Packit ae235b
 *
Packit ae235b
 * The item type of a #GListModel can not change during the life of the
Packit ae235b
 * model.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GType of the items contained in @list.
Packit ae235b
 *
Packit ae235b
 * Since: 2.44
Packit ae235b
 */
Packit ae235b
GType
Packit ae235b
g_list_model_get_item_type (GListModel *list)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_LIST_MODEL (list), G_TYPE_NONE);
Packit ae235b
Packit ae235b
  return G_LIST_MODEL_GET_IFACE (list)->get_item_type (list);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_list_model_get_n_items:
Packit ae235b
 * @list: a #GListModel
Packit ae235b
 *
Packit ae235b
 * Gets the number of items in @list.
Packit ae235b
 *
Packit ae235b
 * Depending on the model implementation, calling this function may be
Packit ae235b
 * less efficient than iterating the list with increasing values for
Packit ae235b
 * @position until g_list_model_get_item() returns %NULL.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of items in @list.
Packit ae235b
 *
Packit ae235b
 * Since: 2.44
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_list_model_get_n_items (GListModel *list)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_LIST_MODEL (list), 0);
Packit ae235b
Packit ae235b
  return G_LIST_MODEL_GET_IFACE (list)->get_n_items (list);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_list_model_get_item: (skip)
Packit ae235b
 * @list: a #GListModel
Packit ae235b
 * @position: the position of the item to fetch
Packit ae235b
 *
Packit ae235b
 * Get the item at @position. If @position is greater than the number of
Packit ae235b
 * items in @list, %NULL is returned.
Packit ae235b
 *
Packit ae235b
 * %NULL is never returned for an index that is smaller than the length
Packit ae235b
 * of the list.  See g_list_model_get_n_items().
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full) (nullable): the item at @position.
Packit ae235b
 *
Packit ae235b
 * Since: 2.44
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
g_list_model_get_item (GListModel *list,
Packit ae235b
                       guint       position)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_LIST_MODEL (list), NULL);
Packit ae235b
Packit ae235b
  return G_LIST_MODEL_GET_IFACE (list)->get_item (list, position);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_list_model_get_object: (rename-to g_list_model_get_item)
Packit ae235b
 * @list: a #GListModel
Packit ae235b
 * @position: the position of the item to fetch
Packit ae235b
 *
Packit ae235b
 * Get the item at @position. If @position is greater than the number of
Packit ae235b
 * items in @list, %NULL is returned.
Packit ae235b
 *
Packit ae235b
 * %NULL is never returned for an index that is smaller than the length
Packit ae235b
 * of the list.  See g_list_model_get_n_items().
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full) (nullable): the object at @position.
Packit ae235b
 *
Packit ae235b
 * Since: 2.44
Packit ae235b
 */
Packit ae235b
GObject *
Packit ae235b
g_list_model_get_object (GListModel *list,
Packit ae235b
                         guint       position)
Packit ae235b
{
Packit ae235b
  gpointer item;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_LIST_MODEL (list), NULL);
Packit ae235b
Packit ae235b
  item = g_list_model_get_item (list, position);
Packit ae235b
Packit ae235b
  return G_OBJECT (item);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_list_model_items_changed:
Packit ae235b
 * @list: a #GListModel
Packit ae235b
 * @position: the position at which @list changed
Packit ae235b
 * @removed: the number of items removed
Packit ae235b
 * @added: the number of items added
Packit ae235b
 *
Packit ae235b
 * Emits the #GListModel::items-changed signal on @list.
Packit ae235b
 *
Packit ae235b
 * This function should only be called by classes implementing
Packit ae235b
 * #GListModel. It has to be called after the internal representation
Packit ae235b
 * of @list has been updated, because handlers connected to this signal
Packit ae235b
 * might query the new state of the list.
Packit ae235b
 *
Packit ae235b
 * Implementations must only make changes to the model (as visible to
Packit ae235b
 * its consumer) in places that will not cause problems for that
Packit ae235b
 * consumer.  For models that are driven directly by a write API (such
Packit ae235b
 * as #GListStore), changes can be reported in response to uses of that
Packit ae235b
 * API.  For models that represent remote data, changes should only be
Packit ae235b
 * made from a fresh mainloop dispatch.  It is particularly not
Packit ae235b
 * permitted to make changes in response to a call to the #GListModel
Packit ae235b
 * consumer API.
Packit ae235b
 *
Packit ae235b
 * Stated another way: in general, it is assumed that code making a
Packit ae235b
 * series of accesses to the model via the API, without returning to the
Packit ae235b
 * mainloop, and without calling other code, will continue to view the
Packit ae235b
 * same contents of the model.
Packit ae235b
 *
Packit ae235b
 * Since: 2.44
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_list_model_items_changed (GListModel *list,
Packit ae235b
                            guint       position,
Packit ae235b
                            guint       removed,
Packit ae235b
                            guint       added)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_LIST_MODEL (list));
Packit ae235b
Packit ae235b
  g_signal_emit (list, g_list_model_changed_signal, 0, position, removed, added);
Packit ae235b
}