2 * This file is a part of MAFW
4 * Copyright (C) 2007, 2008, 2009 Nokia Corporation, all rights reserved.
6 * Contact: Visa Smolander <visa.smolander@nokia.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
32 #include <libmafw/mafw-playlist.h>
33 #include "mafw-mock-playlist.h"
35 static GList *pl_list;
36 static gchar *pl_name;
37 static gboolean pl_rep;
38 static gboolean pl_shuffle;
40 /* Item manipulation */
42 static gboolean mafw_mock_playlist_insert_item(MafwPlaylist *playlist,
44 const gchar *objectid,
47 static gboolean mafw_mock_playlist_remove_item(MafwPlaylist *playlist,
51 static gchar *mafw_mock_playlist_get_item(MafwPlaylist *playlist,
52 guint index, GError **error);
54 static gboolean mafw_mock_playlist_move_item(MafwPlaylist *playlist,
58 static guint mafw_mock_playlist_get_size(MafwPlaylist *playlist,
61 static gboolean mafw_mock_playlist_clear(MafwPlaylist *playlist,
64 static gboolean mafw_mock_playlist_increment_use_count(MafwPlaylist *playlist,
67 static gboolean mafw_mock_playlist_decrement_use_count(MafwPlaylist *playlist,
69 gboolean mafw_mock_playlist_get_prev(MafwPlaylist *playlist, guint *index,
70 gchar **object_id, GError **error);
71 gboolean mafw_mock_playlist_get_next(MafwPlaylist *playlist, guint *index,
72 gchar **object_id, GError **error);
73 static void mafw_mock_playlist_get_starting_index(MafwPlaylist *playlist, guint *index,
74 gchar **object_id, GError **error);
75 static void mafw_mock_playlist_get_last_index(MafwPlaylist *playlist,
76 guint *index, gchar **object_id,
86 static void set_prop(MafwMockPlaylist *playlist, guint prop,
87 const GValue *value, GParamSpec *spec)
89 if (prop == PROP_NAME) {
90 pl_name = g_value_dup_string(value);
91 } else if (prop == PROP_REPEAT) {
92 pl_rep = g_value_get_boolean(value);
94 G_OBJECT_WARN_INVALID_PROPERTY_ID(playlist, prop, spec);
97 static void get_prop(MafwMockPlaylist *playlist, guint prop,
98 GValue *value, GParamSpec *spec)
100 if (prop == PROP_NAME) {
101 g_value_take_string(value, pl_name);
102 } else if (prop == PROP_REPEAT) {
103 g_value_set_boolean(value, pl_rep);
104 } else if (prop == PROP_IS_SHUFFLED) {
105 g_value_set_boolean(value, pl_shuffle);
107 G_OBJECT_WARN_INVALID_PROPERTY_ID(playlist, prop, spec);
110 static void mafw_mock_playlist_get_starting_index(MafwPlaylist *playlist, guint *index,
111 gchar **object_id, GError **error)
113 if (g_list_length(pl_list) > 0) {
115 *object_id = g_strdup(g_list_nth_data(pl_list, 0));
119 static void mafw_mock_playlist_get_last_index(MafwPlaylist *playlist,
120 guint *index, gchar **object_id,
123 *index = g_list_length(pl_list) - 1;
124 *object_id = g_strdup(g_list_nth_data(pl_list, *index));
128 gboolean mafw_mock_playlist_get_next(MafwPlaylist *playlist, guint *index,
129 gchar **object_id, GError **error)
132 gboolean return_value = TRUE;
134 size = g_list_length(pl_list);
136 g_return_val_if_fail(size != 0, FALSE);
138 if (*index == (size - 1)) {
139 return_value = FALSE;
141 *object_id = g_strdup(g_list_nth_data(pl_list, ++(*index)));
147 gboolean mafw_mock_playlist_get_prev(MafwPlaylist *playlist, guint *index,
148 gchar **object_id, GError **error)
151 gboolean return_value = TRUE;
153 size = g_list_length(pl_list);
155 g_return_val_if_fail(size != 0, FALSE);
158 return_value = FALSE;
160 *object_id = g_strdup(g_list_nth_data(pl_list, --(*index)));
166 static void playlist_iface_init(MafwPlaylistIface *iface)
168 iface->get_item = mafw_mock_playlist_get_item;
169 iface->insert_item = mafw_mock_playlist_insert_item;
170 iface->clear = mafw_mock_playlist_clear;
171 iface->get_size = mafw_mock_playlist_get_size;
172 iface->remove_item = mafw_mock_playlist_remove_item;
173 iface->move_item = mafw_mock_playlist_move_item;
174 iface->get_starting_index = mafw_mock_playlist_get_starting_index;
175 iface->get_last_index = mafw_mock_playlist_get_last_index;
176 iface->get_next = mafw_mock_playlist_get_next;
177 iface->get_prev = mafw_mock_playlist_get_prev;
178 iface->increment_use_count = mafw_mock_playlist_increment_use_count;
179 iface->decrement_use_count = mafw_mock_playlist_decrement_use_count;
183 static void mafw_mock_playlist_finalize(GObject *object)
185 g_debug(__FUNCTION__);
189 g_free(pl_list->data);
190 pl_list = g_list_delete_link(pl_list, pl_list);
195 static void mafw_mock_playlist_class_init(
196 MafwMockPlaylistClass *klass)
198 GObjectClass *oclass = NULL;
200 oclass = G_OBJECT_CLASS(klass);
202 oclass->set_property = (gpointer)set_prop;
203 oclass->get_property = (gpointer)get_prop;
204 g_object_class_override_property(oclass, PROP_NAME, "name");
205 g_object_class_override_property(oclass, PROP_REPEAT, "repeat");
206 g_object_class_override_property(oclass,
207 PROP_IS_SHUFFLED, "is-shuffled");
209 oclass -> finalize = mafw_mock_playlist_finalize;
212 static void mafw_mock_playlist_init(MafwMockPlaylist *self)
218 G_DEFINE_TYPE_WITH_CODE(MafwMockPlaylist, mafw_mock_playlist,
220 G_IMPLEMENT_INTERFACE(MAFW_TYPE_PLAYLIST,
221 playlist_iface_init));
224 GObject *mafw_mock_playlist_new(void)
226 MafwMockPlaylist *self;
228 self = g_object_new(MAFW_TYPE_MOCK_PLAYLIST, NULL);
230 return G_OBJECT(self);
233 gboolean mafw_mock_playlist_insert_item(MafwPlaylist *self, guint index,
234 const gchar *objectid,
237 pl_list = g_list_insert(pl_list, g_strdup(objectid), index);
239 g_signal_emit_by_name(self, "contents-changed", index, 0, 1);
244 gboolean mafw_mock_playlist_remove_item(MafwPlaylist *self, guint index,
249 g_return_val_if_fail(g_list_length(pl_list) > 0, FALSE);
251 element = g_list_nth(pl_list, index);
252 g_free(element->data);
253 pl_list = g_list_delete_link(pl_list, element);
255 g_signal_emit_by_name(self, "contents-changed", index, 1, 0);
260 gchar *mafw_mock_playlist_get_item(MafwPlaylist *self, guint index,
263 gchar *oid = g_list_nth_data(pl_list, index);
271 guint mafw_mock_playlist_get_size(MafwPlaylist *self, GError **error)
273 return g_list_length(pl_list);
276 static gboolean mafw_mock_playlist_move_item(MafwPlaylist *playlist,
277 guint from, guint to,
280 GList *element_from, *element_to;
284 size = g_list_length(pl_list);
286 g_return_val_if_fail(size > 0, FALSE);
287 g_return_val_if_fail(from != to, FALSE);
288 g_return_val_if_fail((from < size) && (to < size), FALSE);
290 element_from = g_list_nth(pl_list, from);
291 element_to = g_list_nth(pl_list, to);
293 data = element_from->data;
294 element_from->data = element_to->data;
295 element_to->data = data;
297 g_signal_emit_by_name(playlist, "item-moved", from, to);
302 static gboolean mafw_mock_playlist_increment_use_count(MafwPlaylist *playlist,
308 static gboolean mafw_mock_playlist_decrement_use_count(MafwPlaylist *playlist,
314 gboolean mafw_mock_playlist_clear(MafwPlaylist *self, GError **error)
316 mafw_mock_playlist_finalize(NULL);
321 /* vi: set noexpandtab ts=8 sw=8 cino=t0,(0: */