diff options
author | Max Kellermann <max@duempel.org> | 2013-01-03 00:30:15 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2013-01-03 00:37:18 +0100 |
commit | b4b0b34e5a131f02f723f40cf9566cc43e37cf85 (patch) | |
tree | c2afa49fe0b8f879a4c0c393e36916fde3c8ea8a /src | |
parent | fa3d1156a6bdbb8fab2a90e1716a1f152f7e8104 (diff) | |
download | mpd-b4b0b34e5a131f02f723f40cf9566cc43e37cf85.tar.gz mpd-b4b0b34e5a131f02f723f40cf9566cc43e37cf85.tar.xz mpd-b4b0b34e5a131f02f723f40cf9566cc43e37cf85.zip |
database.h: eliminate db_*_song()
Use the C++ API.
Diffstat (limited to 'src')
-rw-r--r-- | src/DatabaseGlue.cxx | 22 | ||||
-rw-r--r-- | src/DatabasePrint.cxx | 1 | ||||
-rw-r--r-- | src/InotifyUpdate.cxx | 2 | ||||
-rw-r--r-- | src/PlaylistEdit.cxx | 41 | ||||
-rw-r--r-- | src/PlaylistFile.cxx | 17 | ||||
-rw-r--r-- | src/PlaylistPrint.cxx | 33 | ||||
-rw-r--r-- | src/PlaylistSong.cxx | 11 | ||||
-rw-r--r-- | src/QueueSave.cxx | 36 | ||||
-rw-r--r-- | src/StickerCommands.cxx | 72 | ||||
-rw-r--r-- | src/database.h | 9 |
10 files changed, 102 insertions, 142 deletions
diff --git a/src/DatabaseGlue.cxx b/src/DatabaseGlue.cxx index 0b2bb0c37..e0f39f7b9 100644 --- a/src/DatabaseGlue.cxx +++ b/src/DatabaseGlue.cxx @@ -131,28 +131,6 @@ db_get_directory(const char *name) return music_root->LookupDirectory(name); } -struct song * -db_get_song(const char *file) -{ - assert(file != NULL); - - g_debug("get song: %s", file); - - if (db == NULL) - return NULL; - - return db->GetSong(file, NULL); -} - -void -db_return_song(struct song *song) -{ - assert(db != nullptr); - assert(song != nullptr); - - db->ReturnSong(song); -} - bool db_save(GError **error_r) { diff --git a/src/DatabasePrint.cxx b/src/DatabasePrint.cxx index 2bdb2e4fe..1d78bed44 100644 --- a/src/DatabasePrint.cxx +++ b/src/DatabasePrint.cxx @@ -27,7 +27,6 @@ #include "Directory.hxx" extern "C" { -#include "database.h" #include "client.h" #include "song.h" #include "tag.h" diff --git a/src/InotifyUpdate.cxx b/src/InotifyUpdate.cxx index 878da888e..5440c0a84 100644 --- a/src/InotifyUpdate.cxx +++ b/src/InotifyUpdate.cxx @@ -21,13 +21,13 @@ #include "InotifyUpdate.hxx" #include "InotifySource.hxx" #include "InotifyQueue.hxx" -#include "database.h" #include "Mapper.hxx" extern "C" { #include "path.h" } +#include <glib.h> #include <assert.h> #include <sys/inotify.h> #include <sys/stat.h> diff --git a/src/PlaylistEdit.cxx b/src/PlaylistEdit.cxx index 76e102862..d44de0581 100644 --- a/src/PlaylistEdit.cxx +++ b/src/PlaylistEdit.cxx @@ -27,12 +27,14 @@ extern "C" { #include "playlist_internal.h" #include "player_control.h" -#include "database.h" #include "uri.h" #include "song.h" #include "idle.h" } +#include "DatabaseGlue.hxx" +#include "DatabasePlugin.hxx" + #include <stdlib.h> static void playlist_increment_version(struct playlist *playlist) @@ -103,37 +105,30 @@ playlist_append_song(struct playlist *playlist, struct player_control *pc, return PLAYLIST_RESULT_SUCCESS; } -static struct song * -song_by_uri(const char *uri) -{ - struct song *song; - - song = db_get_song(uri); - if (song != NULL) - return song; - - if (uri_has_scheme(uri)) - return song_remote_new(uri); - - return NULL; -} - enum playlist_result playlist_append_uri(struct playlist *playlist, struct player_control *pc, const char *uri, unsigned *added_id) { - struct song *song; - g_debug("add to playlist: %s", uri); - song = song_by_uri(uri); - if (song == NULL) - return PLAYLIST_RESULT_NO_SUCH_SONG; + const Database *db = nullptr; + struct song *song; + if (uri_has_scheme(uri)) { + song = song_remote_new(uri); + } else { + db = GetDatabase(nullptr); + if (db == nullptr) + return PLAYLIST_RESULT_NO_SUCH_SONG; + + song = db->GetSong(uri, nullptr); + if (song == nullptr) + return PLAYLIST_RESULT_NO_SUCH_SONG; + } enum playlist_result result = playlist_append_song(playlist, pc, song, added_id); - if (song_in_database(song)) - db_return_song(song); + if (db != nullptr) + db->ReturnSong(song); return result; } diff --git a/src/PlaylistFile.cxx b/src/PlaylistFile.cxx index 896307df9..7459bd69a 100644 --- a/src/PlaylistFile.cxx +++ b/src/PlaylistFile.cxx @@ -20,6 +20,8 @@ #include "config.h" #include "PlaylistFile.hxx" #include "PlaylistSave.hxx" +#include "DatabasePlugin.hxx" +#include "DatabaseGlue.hxx" #include "song.h" #include "io_error.h" #include "Mapper.hxx" @@ -28,7 +30,6 @@ extern "C" { #include "text_file.h" #include "path.h" #include "uri.h" -#include "database.h" #include "idle.h" #include "conf.h" } @@ -423,16 +424,16 @@ spl_append_uri(const char *url, const char *utf8file, GError **error_r) song_free(song); return success; } else { - struct song *song = db_get_song(url); - if (song == NULL) { - g_set_error_literal(error_r, playlist_quark(), - PLAYLIST_RESULT_NO_SUCH_SONG, - "No such song"); + const Database *db = GetDatabase(error_r); + if (db == nullptr) + return false; + + song *song = db->GetSong(url, error_r); + if (song == nullptr) return false; - } bool success = spl_append_song(utf8file, song, error_r); - db_return_song(song); + db->ReturnSong(song); return success; } } diff --git a/src/PlaylistPrint.cxx b/src/PlaylistPrint.cxx index 88c0a0a06..6fc354f30 100644 --- a/src/PlaylistPrint.cxx +++ b/src/PlaylistPrint.cxx @@ -24,13 +24,14 @@ #include "PlaylistSong.hxx" #include "QueuePrint.hxx" #include "SongPrint.hxx" +#include "DatabaseGlue.hxx" +#include "DatabasePlugin.hxx" extern "C" { #include "playlist_list.h" #include "playlist_plugin.h" #include "playlist.h" #include "song.h" -#include "database.h" #include "client.h" #include "input_stream.h" } @@ -112,6 +113,22 @@ playlist_print_changes_position(struct client *client, queue_print_changes_position(client, &playlist->queue, version); } +static bool +PrintSongDetails(struct client *client, const char *uri_utf8) +{ + const Database *db = GetDatabase(nullptr); + if (db == nullptr) + return false; + + song *song = db->GetSong(uri_utf8, nullptr); + if (song == nullptr) + return false; + + song_print_info(client, song); + db->ReturnSong(song); + return true; +} + bool spl_print(struct client *client, const char *name_utf8, bool detail, GError **error_r) @@ -124,21 +141,9 @@ spl_print(struct client *client, const char *name_utf8, bool detail, } for (const auto &uri_utf8 : contents) { - bool wrote = false; - - if (detail) { - struct song *song = db_get_song(uri_utf8.c_str()); - if (song) { - song_print_info(client, song); - db_return_song(song); - wrote = true; - } - } - - if (!wrote) { + if (!detail || !PrintSongDetails(client, uri_utf8.c_str())) client_printf(client, SONG_FILE "%s\n", uri_utf8.c_str()); - } } return true; diff --git a/src/PlaylistSong.cxx b/src/PlaylistSong.cxx index 72d2760c6..6bdb164fd 100644 --- a/src/PlaylistSong.cxx +++ b/src/PlaylistSong.cxx @@ -20,9 +20,10 @@ #include "config.h" #include "PlaylistSong.hxx" #include "Mapper.hxx" +#include "DatabasePlugin.hxx" +#include "DatabaseGlue.hxx" extern "C" { -#include "database.h" #include "song.h" #include "uri.h" #include "path.h" @@ -105,13 +106,17 @@ playlist_check_load_song(const struct song *song, const char *uri, bool secure) if (dest == NULL) return NULL; } else { - struct song *tmp = db_get_song(uri); + const Database *db = GetDatabase(nullptr); + if (db == nullptr) + return nullptr; + + struct song *tmp = db->GetSong(uri, nullptr); if (tmp == NULL) /* not found in database */ return NULL; dest = song_dup_detached(tmp); - db_return_song(tmp); + db->ReturnSong(tmp); } return apply_song_metadata(dest, song); diff --git a/src/QueueSave.cxx b/src/QueueSave.cxx index 96713c700..d8b698a9a 100644 --- a/src/QueueSave.cxx +++ b/src/QueueSave.cxx @@ -21,11 +21,12 @@ #include "QueueSave.hxx" #include "song.h" #include "SongSave.hxx" +#include "DatabasePlugin.hxx" +#include "DatabaseGlue.hxx" extern "C" { #include "queue.h" #include "uri.h" -#include "database.h" #include "text_file.h" } @@ -69,20 +70,10 @@ queue_save(FILE *fp, const struct queue *queue) } } -static struct song * -get_song(const char *uri) -{ - return uri_has_scheme(uri) - ? song_remote_new(uri) - : db_get_song(uri); -} - void queue_load_song(FILE *fp, GString *buffer, const char *line, struct queue *queue) { - struct song *song; - if (queue_is_full(queue)) return; @@ -95,6 +86,9 @@ queue_load_song(FILE *fp, GString *buffer, const char *line, return; } + const Database *db = nullptr; + struct song *song; + if (g_str_has_prefix(line, SONG_BEGIN)) { const char *uri = line + sizeof(SONG_BEGIN) - 1; if (!uri_has_scheme(uri) && !g_path_is_absolute(uri)) @@ -115,15 +109,23 @@ queue_load_song(FILE *fp, GString *buffer, const char *line, return; } - line = endptr + 1; + const char *uri = endptr + 1; - song = get_song(line); - if (song == NULL) - return; + if (uri_has_scheme(uri)) { + song = song_remote_new(uri); + } else { + db = GetDatabase(nullptr); + if (db == nullptr) + return; + + song = db->GetSong(uri, nullptr); + if (song == nullptr) + return; + } } queue_append(queue, song, priority); - if (song_in_database(song)) - db_return_song(song); + if (db != nullptr) + db->ReturnSong(song); } diff --git a/src/StickerCommands.cxx b/src/StickerCommands.cxx index f4636e02c..a4f83a583 100644 --- a/src/StickerCommands.cxx +++ b/src/StickerCommands.cxx @@ -21,9 +21,12 @@ #include "StickerCommands.hxx" #include "SongPrint.hxx" #include "DatabaseLock.hxx" +#include "DatabasePlugin.hxx" +#include "DatabaseGlue.hxx" #include "SongSticker.hxx" #include "StickerPrint.hxx" #include "StickerDatabase.hxx" +#include "CommandError.hxx" extern "C" { #include "protocol/result.h" @@ -51,20 +54,19 @@ sticker_song_find_print_cb(struct song *song, const char *value, static enum command_return handle_sticker_song(struct client *client, int argc, char *argv[]) { + GError *error = nullptr; + const Database *db = GetDatabase(&error); + if (db == nullptr) + return print_error(client, error); + /* get song song_id key */ if (argc == 5 && strcmp(argv[1], "get") == 0) { - struct song *song; - char *value; - - song = db_get_song(argv[3]); - if (song == NULL) { - command_error(client, ACK_ERROR_NO_EXIST, - "no such song"); - return COMMAND_RETURN_ERROR; - } + song *song = db->GetSong(argv[3], &error); + if (song == nullptr) + return print_error(client, error); - value = sticker_song_get_value(song, argv[4]); - db_return_song(song); + char *value = sticker_song_get_value(song, argv[4]); + db->ReturnSong(song); if (value == NULL) { command_error(client, ACK_ERROR_NO_EXIST, "no such sticker"); @@ -77,18 +79,12 @@ handle_sticker_song(struct client *client, int argc, char *argv[]) return COMMAND_RETURN_OK; /* list song song_id */ } else if (argc == 4 && strcmp(argv[1], "list") == 0) { - struct song *song; - struct sticker *sticker; - - song = db_get_song(argv[3]); - if (song == NULL) { - command_error(client, ACK_ERROR_NO_EXIST, - "no such song"); - return COMMAND_RETURN_ERROR; - } + song *song = db->GetSong(argv[3], &error); + if (song == nullptr) + return print_error(client, error); - sticker = sticker_song_get(song); - db_return_song(song); + sticker *sticker = sticker_song_get(song); + db->ReturnSong(song); if (sticker) { sticker_print(client, sticker); sticker_free(sticker); @@ -97,18 +93,12 @@ handle_sticker_song(struct client *client, int argc, char *argv[]) return COMMAND_RETURN_OK; /* set song song_id id key */ } else if (argc == 6 && strcmp(argv[1], "set") == 0) { - struct song *song; - bool ret; + song *song = db->GetSong(argv[3], &error); + if (song == nullptr) + return print_error(client, error); - song = db_get_song(argv[3]); - if (song == NULL) { - command_error(client, ACK_ERROR_NO_EXIST, - "no such song"); - return COMMAND_RETURN_ERROR; - } - - ret = sticker_song_set_value(song, argv[4], argv[5]); - db_return_song(song); + bool ret = sticker_song_set_value(song, argv[4], argv[5]); + db->ReturnSong(song); if (!ret) { command_error(client, ACK_ERROR_SYSTEM, "failed to set sticker value"); @@ -119,20 +109,14 @@ handle_sticker_song(struct client *client, int argc, char *argv[]) /* delete song song_id [key] */ } else if ((argc == 4 || argc == 5) && strcmp(argv[1], "delete") == 0) { - struct song *song; - bool ret; - - song = db_get_song(argv[3]); - if (song == NULL) { - command_error(client, ACK_ERROR_NO_EXIST, - "no such song"); - return COMMAND_RETURN_ERROR; - } + song *song = db->GetSong(argv[3], &error); + if (song == nullptr) + return print_error(client, error); - ret = argc == 4 + bool ret = argc == 4 ? sticker_song_delete(song) : sticker_song_delete_value(song, argv[4]); - db_return_song(song); + db->ReturnSong(song); if (!ret) { command_error(client, ACK_ERROR_SYSTEM, "no such sticker"); diff --git a/src/database.h b/src/database.h index e18663525..9cde50ba1 100644 --- a/src/database.h +++ b/src/database.h @@ -69,15 +69,6 @@ gcc_pure struct directory * db_get_directory(const char *name); -gcc_nonnull(1) -gcc_pure -struct song * -db_get_song(const char *file); - -gcc_nonnull(1) -void -db_return_song(struct song *song); - /** * May only be used if db_is_simple() returns true. */ |