1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2013 Jonathan Matthew <jonathan@d14n.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The Rhythmbox authors hereby grant permission for non-GPL compatible
* GStreamer plugins to be used and distributed together with GStreamer
* and Rhythmbox. This permission is above and beyond the permissions granted
* by the GPL license by which Rhythmbox is covered. If you modify this code
* you may extend this exception to your version of the code, but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <config.h>
#include <lib/rb-list-model.h>
enum {
ITEMS_CHANGED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
struct _RBListModel
{
GObject parent;
GType item_type;
GArray *items;
};
struct _RBListModelClass
{
GObjectClass parent;
};
G_DEFINE_TYPE (RBListModel, rb_list_model, G_TYPE_OBJECT);
static void rb_list_model_class_init (RBListModelClass *klass);
static void rb_list_model_init (RBListModel *model);
/**
* SECTION:rblistmodel
* @short_description: simple list model
*
* Stores a list of items and emits notification signals on changes.
*/
static void
impl_finalize (GObject *object)
{
RBListModel *model = RB_LIST_MODEL (object);
g_array_free (model->items, TRUE);
G_OBJECT_CLASS (rb_list_model_parent_class)->finalize (object);
}
static void
rb_list_model_init (RBListModel *model)
{
}
static void
rb_list_model_class_init (RBListModelClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = impl_finalize;
signals[ITEMS_CHANGED] =
g_signal_new ("items-changed",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
0,
NULL, NULL, NULL,
G_TYPE_NONE,
3, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT);
}
/**
* rb_list_model_new:
* @item_type: a #GType for items in the list
* @destroy_item: callback for destroying list items
*
* Creates a new empty list model.
*
* Return value: (transfer full): the model
*/
RBListModel *
rb_list_model_new (GType item_type, GDestroyNotify destroy_item)
{
RBListModel *model = RB_LIST_MODEL (g_object_new (RB_TYPE_LIST_MODEL, NULL));
model->item_type = item_type;
model->items = g_array_new (FALSE, FALSE, sizeof (gpointer));
g_array_set_clear_func (model->items, destroy_item);
return model;
}
/**
* rb_list_model_get_item_type:
* @model: an #RBListModel
*
* Returns the list entry type.
*
* Return value: list entry type
*/
GType
rb_list_model_get_item_type (RBListModel *model)
{
return model->item_type;
}
/**
* rb_list_model_n_items:
* @model: an #RBListModel
*
* Returns the length of the list.
*
* Return value: list length
*/
int
rb_list_model_n_items (RBListModel *model)
{
return model->items->len;
}
/**
* rb_list_model_get:
* @model: an #RBListModel
* @index: item to retrieve
*
* Returns an item from the list.
*
* Return value: (transfer none): item at the specified index
*/
gpointer
rb_list_model_get (RBListModel *model, int index)
{
g_return_val_if_fail (RB_IS_LIST_MODEL (model), NULL);
g_return_val_if_fail (index >= 0, NULL);
g_return_val_if_fail (index < model->items->len, NULL);
return g_array_index (model->items, gpointer, index);
}
/**
* rb_list_model_find:
* @model: an #RBListModel
* @item: item to find
*
* Returns the lowest index at which @item appears in the list,
* or -1 if the item is not in the list.
*
* Return value: list index
*/
int
rb_list_model_find (RBListModel *model, gpointer item)
{
int i;
g_return_val_if_fail (RB_IS_LIST_MODEL (model), -1);
if (model->item_type != G_TYPE_NONE) {
g_return_val_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (item, model->item_type), -1);
}
for (i = 0; i < model->items->len; i++) {
if (g_array_index (model->items, gpointer, i) == item)
return i;
}
return -1;
}
static void
items_changed (RBListModel *model, int position, int removed, int added)
{
g_signal_emit (model, signals[ITEMS_CHANGED], 0, position, removed, added);
}
/**
* rb_list_model_insert:
* @model: an #RBListModel
* @index: position to insert the item at
* @item: item to insert
*
* Inserts at item into the list. If @index is less than zero or
* greater than the length of the list, the item is appended to the
* list.
*/
void
rb_list_model_insert (RBListModel *model, int index, gpointer item)
{
g_return_if_fail (RB_IS_LIST_MODEL (model));
if (model->item_type != G_TYPE_NONE) {
g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (item, model->item_type));
}
if (index < 0 || index > model->items->len)
index = model->items->len;
g_array_insert_val (model->items, index, item);
items_changed (model, index, 0, 1);
}
/**
* rb_list_model_append:
* @model: an #RBListModel
* @item: item to append
*
* Appends @item to the list.
*/
void
rb_list_model_append (RBListModel *model, gpointer item)
{
rb_list_model_insert (model, -1, item);
}
/**
* rb_list_model_prepend:
* @model: an #RBListModel
* @item: item to prepend
*
* Prepends @item to the list.
*/
void
rb_list_model_prepend (RBListModel *model, gpointer item)
{
rb_list_model_insert (model, 0, item);
}
/**
* rb_list_model_remove:
* @model: an #RBListModel
* @index: index of the item to remove
*
* Removes the item at @index from the list.
*/
void
rb_list_model_remove (RBListModel *model, int index)
{
g_return_if_fail (RB_IS_LIST_MODEL (model));
g_return_if_fail (index >= 0);
g_return_if_fail (index < model->items->len);
g_array_remove_index (model->items, index);
items_changed (model, index, 1, 0);
}
/**
* rb_list_model_remove_item:
* @model: an #RBListModel
* @item: item to remove
*
* Removes @item from the list. If the item appears in the
* list multiple times, only the first instance is removed.
*/
void
rb_list_model_remove_item (RBListModel *model, gpointer item)
{
int index;
index = rb_list_model_find (model, item);
rb_list_model_remove (model, index);
}
|