Blame src/itdb_chapterdata.c

Packit 29108b
/* Time-stamp: <2007-03-21 17:30:57 jcs>
Packit 29108b
|
Packit 29108b
|  Copyright (C) 2002-2007 Jorg Schuler <jcsjcs at users sourceforge net>
Packit 29108b
|  Part of the gtkpod project.
Packit 29108b
|
Packit 29108b
|  URL: http://www.gtkpod.org/
Packit 29108b
|  URL: http://gtkpod.sourceforge.net/
Packit 29108b
|
Packit 29108b
|  The code contained in this file is free software; you can redistribute
Packit 29108b
|  it and/or modify it under the terms of the GNU Lesser General Public
Packit 29108b
|  License as published by the Free Software Foundation; either version
Packit 29108b
|  2.1 of the License, or (at your option) any later version.
Packit 29108b
|
Packit 29108b
|  This file is distributed in the hope that it will be useful,
Packit 29108b
|  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 29108b
|  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 29108b
|  Lesser General Public License for more details.
Packit 29108b
|
Packit 29108b
|  You should have received a copy of the GNU Lesser General Public
Packit 29108b
|  License along with this code; if not, write to the Free Software
Packit 29108b
|  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Packit 29108b
|
Packit 29108b
|  iTunes and iPod are trademarks of Apple
Packit 29108b
|
Packit 29108b
|  This product is not supported/written/published by Apple!
Packit 29108b
|
Packit 29108b
|  $Id: itdb_chapterdata.c 1612 2007-06-29 16:02:00Z jcsjcs $
Packit 29108b
*/
Packit 29108b
Packit 29108b
#include <config.h>
Packit 29108b
Packit 29108b
#include "itdb_device.h"
Packit 29108b
#include <stdio.h>
Packit 29108b
#include <string.h>
Packit 29108b
#include <glib/gi18n-lib.h>
Packit 29108b
Packit 29108b
/**
Packit 29108b
 * itdb_chapterdata_new:
Packit 29108b
 *
Packit 29108b
 * Creates a new #Itdb_Chapterdata
Packit 29108b
 *
Packit 29108b
 * Returns: a new #Itdb_Chapterdata to be freed with
Packit 29108b
 *               itdb_chapterdata_free() when no longer needed
Packit 29108b
 *
Packit 29108b
 * Since: 0.7.0
Packit 29108b
 */
Packit 29108b
Itdb_Chapterdata *itdb_chapterdata_new (void)
Packit 29108b
{
Packit 29108b
    Itdb_Chapterdata *chapterdata = g_new0 (Itdb_Chapterdata, 1);
Packit 29108b
    return chapterdata;
Packit 29108b
}
Packit 29108b
Packit 29108b
/**
Packit 29108b
 * itdb_chapterdata_free:
Packit 29108b
 * @chapterdata: an #Itdb_Chapterdata
Packit 29108b
 *
Packit 29108b
 * Frees memory used by @chapterdata
Packit 29108b
 *
Packit 29108b
 * Since: 0.7.0
Packit 29108b
 */
Packit 29108b
void itdb_chapterdata_free (Itdb_Chapterdata *chapterdata)
Packit 29108b
{
Packit 29108b
    g_return_if_fail (chapterdata);
Packit 29108b
    itdb_chapterdata_remove_chapters (chapterdata);
Packit 29108b
    g_free (chapterdata);
Packit 29108b
}
Packit 29108b
Packit 29108b
static GList *dup_chapters (GList *chapters)
Packit 29108b
{
Packit 29108b
    GList *it;
Packit 29108b
    GList *result;
Packit 29108b
Packit 29108b
    g_return_val_if_fail (chapters, NULL);
Packit 29108b
    result = NULL;
Packit 29108b
    for (it = chapters; it != NULL; it = it->next)
Packit 29108b
    {
Packit 29108b
	Itdb_Chapter *new_chapter;
Packit 29108b
	Itdb_Chapter *chapter;
Packit 29108b
Packit 29108b
	chapter = (Itdb_Chapter *)(it->data);
Packit 29108b
	g_return_val_if_fail (chapter, NULL);
Packit 29108b
Packit 29108b
	new_chapter = itdb_chapter_duplicate (chapter);
Packit 29108b
Packit 29108b
	result = g_list_prepend (result, new_chapter);
Packit 29108b
    }
Packit 29108b
Packit 29108b
    return g_list_reverse (result);
Packit 29108b
}
Packit 29108b
Packit 29108b
/**
Packit 29108b
 * itdb_chapterdata_duplicate:
Packit 29108b
 * @chapterdata: an #Itdb_Chapterdata
Packit 29108b
 *
Packit 29108b
 * Duplicates @chapterdata
Packit 29108b
 *
Packit 29108b
 * Returns: a new copy of @chapterdata
Packit 29108b
 *
Packit 29108b
 * Since: 0.7.0
Packit 29108b
 */
Packit 29108b
Itdb_Chapterdata *itdb_chapterdata_duplicate (Itdb_Chapterdata *chapterdata)
Packit 29108b
{
Packit 29108b
    Itdb_Chapterdata *dup;
Packit 29108b
    g_return_val_if_fail (chapterdata, NULL);
Packit 29108b
Packit 29108b
    dup = g_new0 (Itdb_Chapterdata, 1);
Packit 29108b
Packit 29108b
    memcpy (dup, chapterdata, sizeof (Itdb_Chapterdata));
Packit 29108b
Packit 29108b
    if (chapterdata->chapters)
Packit 29108b
	dup->chapters = dup_chapters (chapterdata->chapters);
Packit 29108b
    else
Packit 29108b
	dup->chapters = NULL;
Packit 29108b
Packit 29108b
    return dup;
Packit 29108b
}
Packit 29108b
Packit 29108b
/**
Packit 29108b
 * itdb_chapterdata_remove_chapter:
Packit 29108b
 * @chapterdata: an #Itdb_Chapterdata
Packit 29108b
 * @chapter:     an #Itdb_Chapter
Packit 29108b
 *
Packit 29108b
 * Removes @chapter from @chapterdata. The memory used by @chapter is freed.
Packit 29108b
 *
Packit 29108b
 * Since: 0.7.0
Packit 29108b
 */
Packit 29108b
void
Packit 29108b
itdb_chapterdata_remove_chapter (Itdb_Chapterdata *chapterdata, Itdb_Chapter *chapter)
Packit 29108b
{
Packit 29108b
    itdb_chapterdata_unlink_chapter(chapterdata, chapter);
Packit 29108b
    itdb_chapter_free (chapter);
Packit 29108b
}
Packit 29108b
Packit 29108b
void
Packit 29108b
itdb_chapterdata_unlink_chapter (Itdb_Chapterdata *chapterdata, Itdb_Chapter *chapter)
Packit 29108b
{
Packit 29108b
    g_return_if_fail (chapterdata);
Packit 29108b
    g_return_if_fail (chapter);
Packit 29108b
Packit 29108b
    chapterdata->chapters = g_list_remove (chapterdata->chapters, chapter);
Packit 29108b
}
Packit 29108b
Packit 29108b
/**
Packit 29108b
 * itdb_chapterdata_remove_chapters:
Packit 29108b
 * @chapterdata: an #Itdb_Chapterdata
Packit 29108b
 *
Packit 29108b
 * Removes all chapters from @chapterdata
Packit 29108b
 *
Packit 29108b
 * Since: 0.7.0
Packit 29108b
 */
Packit 29108b
void
Packit 29108b
itdb_chapterdata_remove_chapters (Itdb_Chapterdata *chapterdata)
Packit 29108b
{
Packit 29108b
    g_return_if_fail (chapterdata);
Packit 29108b
Packit 29108b
    while (chapterdata->chapters)
Packit 29108b
    {
Packit 29108b
	Itdb_Chapter *chapter = chapterdata->chapters->data;
Packit 29108b
	g_return_if_fail (chapter);
Packit 29108b
	itdb_chapterdata_remove_chapter (chapterdata, chapter);
Packit 29108b
    }
Packit 29108b
}
Packit 29108b
Packit 29108b
/**
Packit 29108b
 * itdb_chapter_new:
Packit 29108b
 *
Packit 29108b
 * Creates a new #Itdb_Chapter
Packit 29108b
 *
Packit 29108b
 * Returns: newly allocated #Itdb_Chapter to be freed with itdb_chapter_free()
Packit 29108b
 * after use
Packit 29108b
 *
Packit 29108b
 * Since: 0.7.0
Packit 29108b
 */
Packit 29108b
Itdb_Chapter *itdb_chapter_new (void)
Packit 29108b
{
Packit 29108b
    Itdb_Chapter *chapter = g_new0 (Itdb_Chapter, 1);
Packit 29108b
    return chapter;
Packit 29108b
}
Packit 29108b
Packit 29108b
/**
Packit 29108b
 * itdb_chapter_free:
Packit 29108b
 * @chapter: an #Itdb_Chapter
Packit 29108b
 *
Packit 29108b
 * Frees the memory used by @chapter
Packit 29108b
 *
Packit 29108b
 * Since: 0.7.0
Packit 29108b
 */
Packit 29108b
void itdb_chapter_free (Itdb_Chapter *chapter)
Packit 29108b
{
Packit 29108b
    g_return_if_fail (chapter);
Packit 29108b
Packit 29108b
    g_free (chapter->chaptertitle);
Packit 29108b
    g_free (chapter);
Packit 29108b
}
Packit 29108b
Packit 29108b
/**
Packit 29108b
 * itdb_chapter_duplicate:
Packit 29108b
 * @chapter: an #Itdb_Chapter
Packit 29108b
 *
Packit 29108b
 * Duplicates the data contained in @chapter
Packit 29108b
 *
Packit 29108b
 * Returns: a newly allocated copy of @chapter to be freed with
Packit 29108b
 * itdb_chapter_free() after use
Packit 29108b
 *
Packit 29108b
 * Since: 0.7.0
Packit 29108b
 */
Packit 29108b
Itdb_Chapter *itdb_chapter_duplicate (Itdb_Chapter *chapter)
Packit 29108b
{
Packit 29108b
    Itdb_Chapter *new_chapter;
Packit 29108b
Packit 29108b
    g_return_val_if_fail (chapter, NULL);
Packit 29108b
Packit 29108b
    new_chapter = itdb_chapter_new ();
Packit 29108b
    memcpy (new_chapter, chapter, sizeof (Itdb_Chapter));
Packit 29108b
    new_chapter->chaptertitle = g_strdup (chapter->chaptertitle);
Packit 29108b
Packit 29108b
    return new_chapter;
Packit 29108b
}
Packit 29108b
Packit 29108b
/**
Packit 29108b
 * itdb_chapterdata_add_chapter
Packit 29108b
 * @chapterdata:  an #Itdb_Chapterdata
Packit 29108b
 * @startpos:     chapter start time in milliseconds
Packit 29108b
 * @chaptertitle: chapter title
Packit 29108b
 *
Packit 29108b
 * Appends a chapter to existing chapters in @chapterdata.
Packit 29108b
 *
Packit 29108b
 * Returns: TRUE if the chapter could be successfully added, FALSE
Packit 29108b
 * otherwise.
Packit 29108b
 *
Packit 29108b
 * Since: 0.7.0
Packit 29108b
 */
Packit 29108b
gboolean
Packit 29108b
itdb_chapterdata_add_chapter (Itdb_Chapterdata *chapterdata,
Packit 29108b
			      guint32 startpos,
Packit 29108b
			      gchar *chaptertitle)
Packit 29108b
{
Packit 29108b
    Itdb_Chapter *chapter;
Packit 29108b
Packit 29108b
    g_return_val_if_fail (chapterdata, FALSE);
Packit 29108b
    g_return_val_if_fail (chaptertitle, FALSE);
Packit 29108b
Packit 29108b
    chapter = itdb_chapter_new ();
Packit 29108b
    if (startpos == 0)
Packit 29108b
	chapter->startpos = 1; /* first chapter should have startpos of 1 */
Packit 29108b
    else
Packit 29108b
	chapter->startpos = startpos;
Packit 29108b
    chapter->chaptertitle = g_strdup (chaptertitle);
Packit 29108b
    chapterdata->chapters = g_list_append (chapterdata->chapters, chapter);
Packit 29108b
Packit 29108b
    return TRUE;
Packit 29108b
}