summaryrefslogtreecommitdiffstats
path: root/src/itdb_chapterdata.c
blob: c74e6d69d974eeb18fbb28cbc85779a41b67e326 (plain)
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
/* Time-stamp: <2007-03-21 17:30:57 jcs>
|
|  Copyright (C) 2002-2007 Jorg Schuler <jcsjcs at users sourceforge net>
|  Part of the gtkpod project.
|
|  URL: http://www.gtkpod.org/
|  URL: http://gtkpod.sourceforge.net/
|
|  The code contained in this file 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.1 of the License, or (at your option) any later version.
|
|  This file 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 code; if not, write to the Free Software
|  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
|
|  iTunes and iPod are trademarks of Apple
|
|  This product is not supported/written/published by Apple!
|
|  $Id: itdb_chapterdata.c 1612 2007-06-29 16:02:00Z jcsjcs $
*/

#include <config.h>

#include "itdb_device.h"
#include "itdb_private.h"
#include "itdb_endianness.h"
#include <stdio.h>
#include <string.h>
#include <glib/gi18n-lib.h>

/**
 * itdb_chapterdata_new:
 *
 * Creates a new #Itdb_Chapterdata
 *
 * Returns: a new #Itdb_Chapterdata to be freed with
 *               itdb_chapterdata_free() when no longer needed
 *
 * Since: 0.7.0
 */
Itdb_Chapterdata *itdb_chapterdata_new (void)
{
    Itdb_Chapterdata *chapterdata = g_new0 (Itdb_Chapterdata, 1);
    return chapterdata;
}

/**
 * itdb_chapterdata_free:
 * @chapterdata: an #Itdb_Chapterdata
 *
 * Frees memory used by @chapterdata
 *
 * Since: 0.7.0
 */
void itdb_chapterdata_free (Itdb_Chapterdata *chapterdata)
{
    g_return_if_fail (chapterdata);
    itdb_chapterdata_remove_chapters (chapterdata);
    g_free (chapterdata);
}

static GList *dup_chapters (GList *chapters)
{
    GList *it;
    GList *result;

    g_return_val_if_fail (chapters, NULL);
    result = NULL;
    for (it = chapters; it != NULL; it = it->next)
    {
	Itdb_Chapter *new_chapter;
	Itdb_Chapter *chapter;

	chapter = (Itdb_Chapter *)(it->data);
	g_return_val_if_fail (chapter, NULL);

	new_chapter = itdb_chapter_duplicate (chapter);

	result = g_list_prepend (result, new_chapter);
    }

    return g_list_reverse (result);
}

/**
 * itdb_chapterdata_duplicate:
 * @chapterdata: an #Itdb_Chapterdata
 *
 * Duplicates @chapterdata
 *
 * Returns: a new copy of @chapterdata
 *
 * Since: 0.7.0
 */
Itdb_Chapterdata *itdb_chapterdata_duplicate (Itdb_Chapterdata *chapterdata)
{
    Itdb_Chapterdata *dup;
    int numchapters;
    g_return_val_if_fail (chapterdata, NULL);

    dup = g_new0 (Itdb_Chapterdata, 1);

    memcpy (dup, chapterdata, sizeof (Itdb_Chapterdata));

    numchapters = g_list_length (chapterdata->chapters);
    if (chapterdata->chapters)
	dup->chapters = dup_chapters (chapterdata->chapters);
    else
	dup->chapters = NULL;

    return dup;
}

/**
 * itdb_chapterdata_remove_chapter:
 * @chapterdata: an #Itdb_Chapterdata
 * @chapter:     an #Itdb_Chapter
 *
 * Removes @chapter from @chapterdata. The memory used by @chapter is freed.
 *
 * Since: 0.7.0
 */
void
itdb_chapterdata_remove_chapter (Itdb_Chapterdata *chapterdata, Itdb_Chapter *chapter)
{
    g_return_if_fail (chapterdata);
    g_return_if_fail (chapter);

    chapterdata->chapters = g_list_remove (chapterdata->chapters, chapter);
    itdb_chapter_free (chapter);
}

/**
 * itdb_chapterdata_remove_chapters:
 * @chapterdata: an #Itdb_Chapterdata
 *
 * Removes all chapters from @chapterdata
 *
 * Since: 0.7.0
 */
void
itdb_chapterdata_remove_chapters (Itdb_Chapterdata *chapterdata)
{
    g_return_if_fail (chapterdata);

    while (chapterdata->chapters)
    {
	Itdb_Chapter *chapter = chapterdata->chapters->data;
	g_return_if_fail (chapter);
	itdb_chapterdata_remove_chapter (chapterdata, chapter);
    }
}

/**
 * itdb_chapter_new:
 *
 * Creates a new #Itdb_Chapter
 *
 * Returns: newly allocated #Itdb_Chapter to be freed with itdb_chapter_free()
 * after use
 *
 * Since: 0.7.0
 */
Itdb_Chapter *itdb_chapter_new (void)
{
    Itdb_Chapter *chapter = g_new0 (Itdb_Chapter, 1);
    return chapter;
}

/**
 * itdb_chapter_free:
 * @chapter: an #Itdb_Chapter
 *
 * Frees the memory used by @chapter
 *
 * Since: 0.7.0
 */
void itdb_chapter_free (Itdb_Chapter *chapter)
{
    g_return_if_fail (chapter);

    g_free (chapter->chaptertitle);
    g_free (chapter);
}

/**
 * itdb_chapter_duplicate:
 * @chapter: an #Itdb_Chapter
 *
 * Duplicates the data contained in @chapter
 *
 * Returns: a newly allocated copy of @chapter to be freed with
 * itdb_chapter_free() after use
 *
 * Since: 0.7.0
 */
Itdb_Chapter *itdb_chapter_duplicate (Itdb_Chapter *chapter)
{
    Itdb_Chapter *new_chapter;

    g_return_val_if_fail (chapter, NULL);

    new_chapter = itdb_chapter_new ();
    memcpy (new_chapter, chapter, sizeof (Itdb_Chapter));
    new_chapter->chaptertitle = g_strdup (chapter->chaptertitle);

    return new_chapter;
}

/**
 * itdb_chapterdata_add_chapter
 * @chapterdata:  an #Itdb_Chapterdata
 * @startpos:     chapter start time in milliseconds
 * @chaptertitle: chapter title
 *
 * Appends a chapter to existing chapters in @chapterdata.
 *
 * Returns: TRUE if the chapter could be successfully added, FALSE
 * otherwise.
 *
 * Since: 0.7.0
 */
gboolean
itdb_chapterdata_add_chapter (Itdb_Chapterdata *chapterdata,
			      gint32 startpos,
			      gchar *chaptertitle)
{
    Itdb_Chapter *chapter;

    g_return_val_if_fail (chapterdata, FALSE);
    g_return_val_if_fail (chaptertitle, FALSE);

    chapter = itdb_chapter_new ();
    if (startpos == 0)
	chapter->startpos = 1; /* first chapter should have startpos of 1 */
    else
	chapter->startpos = startpos;
    chapter->chaptertitle = g_strdup (chaptertitle);
    chapterdata->chapters = g_list_append (chapterdata->chapters, chapter);

    return TRUE;
}