Macro qtTrIdx() replaced by tr() and QT_TRANSLATE_NOOP()
[mafwsubrenderer] / mafw-gst-subtitles-renderer / tests / mafw-mock-playlist.c
1 /*
2  * This file is a part of MAFW
3  *
4  * Copyright (C) 2007, 2008, 2009 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Visa Smolander <visa.smolander@nokia.com>
7  *
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.
12  *
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.
17  *
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
21  * 02110-1301 USA
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <libmafw/mafw-playlist.h>
33 #include "mafw-mock-playlist.h"
34
35 static GList *pl_list;
36 static gchar *pl_name;
37 static gboolean pl_rep;
38 static gboolean pl_shuffle;
39
40 /* Item manipulation */
41
42 static gboolean mafw_mock_playlist_insert_item(MafwPlaylist *playlist,
43                                                      guint index,
44                                                      const gchar *objectid,
45                                                      GError **error);
46
47 static gboolean mafw_mock_playlist_remove_item(MafwPlaylist *playlist,
48                                                      guint index,
49                                                      GError **error);
50
51 static gchar *mafw_mock_playlist_get_item(MafwPlaylist *playlist,
52                                                 guint index, GError **error);
53
54 static gboolean mafw_mock_playlist_move_item(MafwPlaylist *playlist,
55                                                    guint from, guint to,
56                                                    GError **error);
57
58 static guint mafw_mock_playlist_get_size(MafwPlaylist *playlist,
59                                                GError **error);
60
61 static gboolean mafw_mock_playlist_clear(MafwPlaylist *playlist,
62                                                GError **error);
63
64 static gboolean mafw_mock_playlist_increment_use_count(MafwPlaylist *playlist,
65                                                         GError **error);
66
67 static gboolean mafw_mock_playlist_decrement_use_count(MafwPlaylist *playlist,
68                                                         GError **error);
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,
77                                                GError **error);
78
79 enum {
80         PROP_0,
81         PROP_NAME,
82         PROP_REPEAT,
83         PROP_IS_SHUFFLED,
84 };
85
86 static void set_prop(MafwMockPlaylist *playlist, guint prop,
87                      const GValue *value, GParamSpec *spec)
88 {
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);
93         } else
94                 G_OBJECT_WARN_INVALID_PROPERTY_ID(playlist, prop, spec);
95 }
96
97 static void get_prop(MafwMockPlaylist *playlist, guint prop,
98                      GValue *value, GParamSpec *spec)
99 {
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);
106         } else
107                 G_OBJECT_WARN_INVALID_PROPERTY_ID(playlist, prop, spec);
108 }
109
110 static void mafw_mock_playlist_get_starting_index(MafwPlaylist *playlist, guint *index,
111                                         gchar **object_id, GError **error)
112 {
113         if (g_list_length(pl_list) > 0) {
114                 *index = 0;
115                 *object_id = g_strdup(g_list_nth_data(pl_list, 0));
116         }
117 }
118
119 static void mafw_mock_playlist_get_last_index(MafwPlaylist *playlist,
120                                                guint *index, gchar **object_id,
121                                                GError **error)
122 {
123         *index = g_list_length(pl_list) - 1;
124         *object_id = g_strdup(g_list_nth_data(pl_list, *index));
125 }
126
127
128 gboolean mafw_mock_playlist_get_next(MafwPlaylist *playlist, guint *index,
129                                 gchar **object_id, GError **error)
130 {
131         gint size;
132         gboolean return_value = TRUE;
133
134         size = g_list_length(pl_list);
135         
136         g_return_val_if_fail(size != 0, FALSE);
137
138         if (*index == (size - 1)) {
139                 return_value = FALSE;
140         } else {
141                 *object_id = g_strdup(g_list_nth_data(pl_list, ++(*index)));
142         }
143         
144         return return_value;
145 }
146
147 gboolean mafw_mock_playlist_get_prev(MafwPlaylist *playlist, guint *index,
148                                 gchar **object_id, GError **error)
149 {
150         gint size;
151         gboolean return_value = TRUE;
152
153         size = g_list_length(pl_list);
154         
155         g_return_val_if_fail(size != 0, FALSE);
156
157         if (*index == 0) {
158                 return_value = FALSE;
159         } else {
160                 *object_id = g_strdup(g_list_nth_data(pl_list, --(*index)));
161         }
162
163         return return_value;
164 }
165
166 static void playlist_iface_init(MafwPlaylistIface *iface)
167 {
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;
180 }
181
182
183 static void mafw_mock_playlist_finalize(GObject *object)
184 {
185         g_debug(__FUNCTION__);
186         
187         while (pl_list)
188         {
189                 g_free(pl_list->data);
190                 pl_list = g_list_delete_link(pl_list, pl_list);
191         }
192         
193 }
194
195 static void mafw_mock_playlist_class_init(
196                                         MafwMockPlaylistClass *klass)
197 {
198         GObjectClass *oclass = NULL;
199
200         oclass = G_OBJECT_CLASS(klass);
201
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");
208         
209         oclass -> finalize = mafw_mock_playlist_finalize;
210 }
211
212 static void mafw_mock_playlist_init(MafwMockPlaylist *self)
213 {
214 }
215
216
217
218 G_DEFINE_TYPE_WITH_CODE(MafwMockPlaylist, mafw_mock_playlist,
219                         G_TYPE_OBJECT,
220                         G_IMPLEMENT_INTERFACE(MAFW_TYPE_PLAYLIST,
221                                               playlist_iface_init));
222
223
224 GObject *mafw_mock_playlist_new(void)
225 {
226         MafwMockPlaylist *self;
227
228         self = g_object_new(MAFW_TYPE_MOCK_PLAYLIST, NULL);
229         
230         return G_OBJECT(self);
231 }
232
233 gboolean mafw_mock_playlist_insert_item(MafwPlaylist *self, guint index,
234                                               const gchar *objectid,
235                                               GError **error)
236 {
237         pl_list = g_list_insert(pl_list, g_strdup(objectid), index);
238
239         g_signal_emit_by_name(self, "contents-changed", index, 0, 1);
240         
241         return TRUE;
242 }
243
244 gboolean mafw_mock_playlist_remove_item(MafwPlaylist *self, guint index,
245                                          GError **error)
246 {
247         GList *element;
248
249         g_return_val_if_fail(g_list_length(pl_list) > 0, FALSE);
250
251         element = g_list_nth(pl_list, index);
252         g_free(element->data);
253         pl_list = g_list_delete_link(pl_list, element);
254
255         g_signal_emit_by_name(self, "contents-changed", index, 1, 0);
256
257         return TRUE;
258 }
259
260 gchar *mafw_mock_playlist_get_item(MafwPlaylist *self, guint index,
261                                          GError **error)
262 {
263         gchar *oid = g_list_nth_data(pl_list, index);
264         
265         if (oid)
266                 oid = g_strdup(oid);
267         
268         return oid;
269 }
270
271 guint mafw_mock_playlist_get_size(MafwPlaylist *self, GError **error)
272 {
273         return g_list_length(pl_list);
274 }
275
276 static gboolean mafw_mock_playlist_move_item(MafwPlaylist *playlist,
277                                                    guint from, guint to,
278                                                    GError **error)
279 {
280         GList *element_from, *element_to;
281         gpointer data;
282         gint size;
283
284         size = g_list_length(pl_list);
285
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);
289
290         element_from = g_list_nth(pl_list, from);
291         element_to = g_list_nth(pl_list, to);
292
293         data = element_from->data;
294         element_from->data = element_to->data;
295         element_to->data = data;
296
297         g_signal_emit_by_name(playlist, "item-moved", from, to);
298
299         return TRUE;
300 }
301
302 static gboolean mafw_mock_playlist_increment_use_count(MafwPlaylist *playlist,
303                                                         GError **error)
304 {
305         return TRUE;
306 }
307
308 static gboolean mafw_mock_playlist_decrement_use_count(MafwPlaylist *playlist,
309                                                          GError **error)
310 {
311         return TRUE;    
312 }
313
314 gboolean mafw_mock_playlist_clear(MafwPlaylist *self, GError **error)
315 {
316         mafw_mock_playlist_finalize(NULL);
317         
318         return TRUE;
319 }
320
321 /* vi: set noexpandtab ts=8 sw=8 cino=t0,(0: */