diff options
author | Max Kellermann <max@duempel.org> | 2014-01-15 18:25:58 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2014-01-17 22:58:27 +0100 |
commit | 32ec6723115e9d534e3af685ea083bf74da5483d (patch) | |
tree | 22c9e08e08f80cda7556c4c2f105b831711085a7 /src | |
parent | e2812f722d04ca2f338e00d59d2a649545863b03 (diff) | |
download | mpd-32ec6723115e9d534e3af685ea083bf74da5483d.tar.gz mpd-32ec6723115e9d534e3af685ea083bf74da5483d.tar.xz mpd-32ec6723115e9d534e3af685ea083bf74da5483d.zip |
DatabaseSong: new library merging duplicate code
Diffstat (limited to 'src')
-rw-r--r-- | src/DatabaseSong.cxx | 40 | ||||
-rw-r--r-- | src/DatabaseSong.hxx | 38 | ||||
-rw-r--r-- | src/PlaylistEdit.cxx | 14 | ||||
-rw-r--r-- | src/PlaylistFile.cxx | 18 | ||||
-rw-r--r-- | src/PlaylistSong.cxx | 15 |
5 files changed, 90 insertions, 35 deletions
diff --git a/src/DatabaseSong.cxx b/src/DatabaseSong.cxx new file mode 100644 index 000000000..9afd109cb --- /dev/null +++ b/src/DatabaseSong.cxx @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2003-2014 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. + */ + +#include "config.h" +#include "DatabaseSong.hxx" +#include "DatabaseGlue.hxx" +#include "DatabasePlugin.hxx" +#include "DetachedSong.hxx" + +DetachedSong * +DatabaseDetachSong(const char *uri, Error &error) +{ + const Database *db = GetDatabase(error); + if (db == nullptr) + return nullptr; + + Song *tmp = db->GetSong(uri, error); + if (tmp == nullptr) + return nullptr; + + DetachedSong *song = new DetachedSong(*tmp); + db->ReturnSong(tmp); + return song; +} diff --git a/src/DatabaseSong.hxx b/src/DatabaseSong.hxx new file mode 100644 index 000000000..0200af6b8 --- /dev/null +++ b/src/DatabaseSong.hxx @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2003-2014 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_DATABASE_SONG_HXX +#define MPD_DATABASE_SONG_HXX + +#include "Compiler.h" + +class DetachedSong; +class Error; + +/** + * Look up a song in the database and convert it to a #DetachedSong + * instance. The caller is responsible for freeing it. + * + * @return nullptr on error + */ +gcc_malloc gcc_nonnull_all +DetachedSong * +DatabaseDetachSong(const char *uri, Error &error); + +#endif diff --git a/src/PlaylistEdit.cxx b/src/PlaylistEdit.cxx index 3f9015765..24d9d9653 100644 --- a/src/PlaylistEdit.cxx +++ b/src/PlaylistEdit.cxx @@ -32,8 +32,7 @@ #include "Song.hxx" #include "DetachedSong.hxx" #include "Idle.hxx" -#include "DatabaseGlue.hxx" -#include "DatabasePlugin.hxx" +#include "DatabaseSong.hxx" #include "Log.hxx" #include <stdlib.h> @@ -113,16 +112,9 @@ playlist::AppendURI(PlayerControl &pc, if (uri_has_scheme(uri)) { song = new DetachedSong(uri); } else { - const Database *db = GetDatabase(); - if (db == nullptr) + song = DatabaseDetachSong(uri, IgnoreError()); + if (song == nullptr) return PlaylistResult::NO_SUCH_SONG; - - Song *tmp = db->GetSong(uri, IgnoreError()); - if (tmp == nullptr) - return PlaylistResult::NO_SUCH_SONG; - - song = new DetachedSong(*tmp); - db->ReturnSong(tmp); } PlaylistResult result = AppendSong(pc, std::move(*song), added_id); diff --git a/src/PlaylistFile.cxx b/src/PlaylistFile.cxx index d4e357a45..2707bcb76 100644 --- a/src/PlaylistFile.cxx +++ b/src/PlaylistFile.cxx @@ -22,8 +22,7 @@ #include "PlaylistSave.hxx" #include "PlaylistInfo.hxx" #include "PlaylistVector.hxx" -#include "DatabasePlugin.hxx" -#include "DatabaseGlue.hxx" +#include "DatabaseSong.hxx" #include "DetachedSong.hxx" #include "Mapper.hxx" #include "fs/TextFile.hxx" @@ -405,18 +404,13 @@ spl_append_uri(const char *url, const char *utf8file, Error &error) return spl_append_song(utf8file, DetachedSong(url), error); } else { - const Database *db = GetDatabase(error); - if (db == nullptr) + DetachedSong *song = DatabaseDetachSong(url, error); + if (song == nullptr) return false; - Song *tmp = db->GetSong(url, error); - if (tmp == nullptr) - return false; - - const DetachedSong song(*tmp); - db->ReturnSong(tmp); - - return spl_append_song(utf8file, song, error); + bool success = spl_append_song(utf8file, *song, error); + delete song; + return success; } } diff --git a/src/PlaylistSong.cxx b/src/PlaylistSong.cxx index 4fbfb65a4..5d61c7f71 100644 --- a/src/PlaylistSong.cxx +++ b/src/PlaylistSong.cxx @@ -20,8 +20,7 @@ #include "config.h" #include "PlaylistSong.hxx" #include "Mapper.hxx" -#include "DatabasePlugin.hxx" -#include "DatabaseGlue.hxx" +#include "DatabaseSong.hxx" #include "ls.hxx" #include "tag/Tag.hxx" #include "tag/TagBuilder.hxx" @@ -106,17 +105,9 @@ playlist_check_load_song(const DetachedSong *song, const char *uri, bool secure) return nullptr; } } else { - const Database *db = GetDatabase(); - if (db == nullptr) + dest = DatabaseDetachSong(uri, IgnoreError()); + if (dest == nullptr) return nullptr; - - Song *tmp = db->GetSong(uri, IgnoreError()); - if (tmp == nullptr) - /* not found in database */ - return nullptr; - - dest = new DetachedSong(*tmp); - db->ReturnSong(tmp); } return apply_song_metadata(dest, song); |