diff options
author | Max Kellermann <max@duempel.org> | 2011-09-10 18:48:10 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2011-09-13 21:47:00 +0200 |
commit | a94d4be466ea3a48389361b483f72df45834f7d2 (patch) | |
tree | 734076ad49d963a00648f58dee4cde111dc0364a /src/db | |
parent | b7d2d4cfe8b88174a7b1f41840ddc0b23dbd6a75 (diff) | |
download | mpd-a94d4be466ea3a48389361b483f72df45834f7d2.tar.gz mpd-a94d4be466ea3a48389361b483f72df45834f7d2.tar.xz mpd-a94d4be466ea3a48389361b483f72df45834f7d2.zip |
db_plugin: add method visit()
Diffstat (limited to '')
-rw-r--r-- | src/db/simple_db_plugin.c | 41 | ||||
-rw-r--r-- | src/db_plugin.h | 24 | ||||
-rw-r--r-- | src/db_selection.h | 56 |
3 files changed, 121 insertions, 0 deletions
diff --git a/src/db/simple_db_plugin.c b/src/db/simple_db_plugin.c index 8218635fc..e359b5e65 100644 --- a/src/db/simple_db_plugin.c +++ b/src/db/simple_db_plugin.c @@ -21,6 +21,8 @@ #include "simple_db_plugin.h" #include "db_internal.h" #include "db_error.h" +#include "db_selection.h" +#include "db_visitor.h" #include "db_save.h" #include "conf.h" #include "glib_compat.h" @@ -48,6 +50,17 @@ simple_db_quark(void) return g_quark_from_static_string("simple_db"); } +G_GNUC_PURE +static const struct directory * +simple_db_lookup_directory(const struct simple_db *db, const char *uri) +{ + assert(db != NULL); + assert(db->root != NULL); + assert(uri != NULL); + + return directory_lookup_directory(db->root, uri); +} + static struct db * simple_db_init(const struct config_param *param, GError **error_r) { @@ -230,6 +243,33 @@ simple_db_get_song(struct db *_db, const char *uri, GError **error_r) return song; } +static bool +simple_db_visit(struct db *_db, const struct db_selection *selection, + const struct db_visitor *visitor, void *ctx, + GError **error_r) +{ + const struct simple_db *db = (const struct simple_db *)_db; + const struct directory *directory = + simple_db_lookup_directory(db, selection->uri); + if (directory == NULL) { + struct song *song; + if (visitor->song != NULL && + (song = simple_db_get_song(_db, selection->uri, NULL)) != NULL) + return visitor->song(song, ctx, error_r); + + g_set_error(error_r, db_quark(), DB_NOT_FOUND, + "No such directory"); + return false; + } + + if (selection->recursive && visitor->directory != NULL && + !visitor->directory(directory, ctx, error_r)) + return false; + + return directory_walk(directory, selection->recursive, + visitor, ctx, error_r); +} + const struct db_plugin simple_db_plugin = { .name = "simple", .init = simple_db_init, @@ -237,6 +277,7 @@ const struct db_plugin simple_db_plugin = { .open = simple_db_open, .close = simple_db_close, .get_song = simple_db_get_song, + .visit = simple_db_visit, }; struct directory * diff --git a/src/db_plugin.h b/src/db_plugin.h index 5fec2529e..1c7e14ede 100644 --- a/src/db_plugin.h +++ b/src/db_plugin.h @@ -31,6 +31,8 @@ #include <stdbool.h> struct config_param; +struct db_selection; +struct db_visitor; struct db { const struct db_plugin *plugin; @@ -67,6 +69,13 @@ struct db_plugin { */ struct song *(*get_song)(struct db *db, const char *uri, GError **error_r); + + /** + * Visit the selected entities. + */ + bool (*visit)(struct db *db, const struct db_selection *selection, + const struct db_visitor *visitor, void *ctx, + GError **error_r); }; G_GNUC_MALLOC @@ -78,6 +87,7 @@ db_plugin_new(const struct db_plugin *plugin, const struct config_param *param, assert(plugin->init != NULL); assert(plugin->finish != NULL); assert(plugin->get_song != NULL); + assert(plugin->visit != NULL); assert(error_r == NULL || *error_r == NULL); struct db *db = plugin->init(param, error_r); @@ -129,4 +139,18 @@ db_plugin_get_song(struct db *db, const char *uri, GError **error_r) return db->plugin->get_song(db, uri, error_r); } +static inline bool +db_plugin_visit(struct db *db, const struct db_selection *selection, + const struct db_visitor *visitor, void *ctx, + GError **error_r) +{ + assert(db != NULL); + assert(db->plugin != NULL); + assert(selection != NULL); + assert(visitor != NULL); + assert(error_r == NULL || *error_r == NULL); + + return db->plugin->visit(db, selection, visitor, ctx, error_r); +} + #endif diff --git a/src/db_selection.h b/src/db_selection.h new file mode 100644 index 000000000..2cebb4907 --- /dev/null +++ b/src/db_selection.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2003-2011 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_DB_SELECTION_H +#define MPD_DB_SELECTION_H + +#include "gcc.h" + +#include <assert.h> + +struct directory; +struct song; + +struct db_selection { + /** + * The base URI of the search (UTF-8). Must not begin or end + * with a slash. NULL or an empty string searches the whole + * database. + */ + const char *uri; + + /** + * Recursively search all sub directories? + */ + bool recursive; +}; + +gcc_nonnull(1,2) +static inline void +db_selection_init(struct db_selection *selection, + const char *uri, bool recursive) +{ + assert(selection != NULL); + assert(uri != NULL); + + selection->uri = uri; + selection->recursive = recursive; +} + +#endif |