core/playlists/library: Add pl_library_{init, deinit}() functions

The pl_library_init() function is used to allocate a playlist for each
library path already in the database, and pl_library_deinit() is then
used to free this memory during cleanup.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-05-09 07:52:10 -04:00 committed by Anna Schumaker
parent e4930704a2
commit 261f6d91d7
7 changed files with 128 additions and 2 deletions

View File

@ -2,21 +2,25 @@
* Copyright 2013 (c) Anna Schumaker.
*/
#include <core/playlist.h>
#include <core/playlists/library.h>
#include <core/playlists/system.h>
struct playlist_type *playlist_types[] = {
[PL_SYSTEM] = &pl_system,
[PL_LIBRARY] = &pl_library,
};
void playlist_init(struct queue_ops *ops)
{
pl_system_init(ops);
pl_library_init(ops);
}
void playlist_deinit()
{
pl_system_deinit();
pl_library_deinit();
}
bool playlist_add(enum playlist_type_t type, const gchar *name,

63
core/playlists/library.c Normal file
View File

@ -0,0 +1,63 @@
/*
* Copyright 2016 (c) Anna Schumaker.
*/
#include <core/idle.h>
#include <core/playlists/library.h>
static struct queue_ops *lib_ops = NULL;
static struct playlist *__lib_pl_alloc(const gchar *path)
{
struct playlist *playlist = g_malloc(sizeof(struct playlist));
playlist->pl_name = path;
playlist->pl_type = PL_LIBRARY;
queue_init(&playlist->pl_queue, Q_ENABLED | Q_REPEAT, lib_ops, playlist);
return playlist;
}
static void __lib_pl_free(struct playlist *playlist)
{
if (playlist) {
queue_deinit(&playlist->pl_queue);
g_free(playlist);
}
}
struct playlist_type pl_library;
static bool __lib_pl_init(void *data)
{
struct db_entry *dbe, *next;
struct playlist *playlist;
db_for_each(dbe, next, library_db_get()) {
playlist = __lib_pl_alloc(LIBRARY(dbe)->li_path);
LIBRARY(dbe)->li_playlist = playlist;
}
return true;
}
void pl_library_init(struct queue_ops *ops)
{
lib_ops = ops;
idle_schedule(IDLE_SYNC, __lib_pl_init, NULL);
}
void pl_library_deinit()
{
struct db_entry *dbe, *next;
struct playlist *playlist;
db_for_each(dbe, next, library_db_get()) {
playlist = LIBRARY(dbe)->li_playlist;
LIBRARY(dbe)->li_playlist = NULL;
__lib_pl_free(playlist);
}
}

View File

@ -0,0 +1,19 @@
/*
* Copyright 2016 (c) Anna Schumaker.
*/
#ifndef OCARINA_CORE_PLAYLISTS_LIBRARY_H
#define OCARINA_CORE_PLAYLISTS_LIBRARY_H
#include <core/playlists/type.h>
/* Library playlist type. */
extern struct playlist_type pl_library;
/* Called to initialize library playlists. */
void pl_library_init(struct queue_ops *);
/* Called to deinitialize system playlists. */
void pl_library_deinit();
#endif /* OCARINA_CORE_PLAYLISTS_LIBRARY_H */

View File

@ -11,6 +11,7 @@
enum playlist_type_t {
PL_SYSTEM,
PL_LIBRARY,
PL_MAX_TYPE,
};

View File

@ -12,6 +12,7 @@ tags/library
tags/track
queue
playlists/system
playlists/library
playlist
collection
tempq

View File

@ -7,9 +7,10 @@ def PlaylistTest(name):
run = CoreTest("playlists/%s" % name)
Alias("tests/core/playlists", run)
if len(res) > 0 and testing_group([ "tests/core/playlists" ]):
Depends(run, res[1])
Depends(run, res[-1])
return run
res += [ PlaylistTest("system") ]
res += [ PlaylistTest("system") ]
res += [ PlaylistTest("library") ]
Return("res")

View File

@ -0,0 +1,37 @@
/*
* Copyright 2016 (c) Anna Schumaker.
*/
#include <core/filter.h>
#include <core/idle.h>
#include <core/playlists/library.h>
#include <core/tags/library.h>
#include <core/tags/tags.h>
#include <tests/test.h>
void test_library()
{
struct library *library;
idle_init_sync();
filter_init();
tags_init();
while (idle_run_task()) {};
library = library_find("tests/Music");
test_equal(library->li_playlist, NULL);
pl_library_init(NULL);
while (idle_run_task()) {};
test_not_equal(library->li_playlist, NULL);
pl_library_deinit();
test_equal(library->li_playlist, NULL);
tags_deinit();
filter_deinit();
idle_deinit();
}
DECLARE_UNIT_TESTS(
UNIT_TEST("Library Playlists", test_library),
);