2011-09-05 22:53:46 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2012 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 "ProxyDatabasePlugin.hxx"
|
|
|
|
#include "DatabasePlugin.hxx"
|
|
|
|
#include "DatabaseSelection.hxx"
|
2013-08-10 19:43:27 +02:00
|
|
|
#include "DatabaseError.hxx"
|
2013-01-02 20:25:20 +01:00
|
|
|
#include "PlaylistVector.hxx"
|
2013-01-02 22:52:08 +01:00
|
|
|
#include "Directory.hxx"
|
2013-07-28 13:25:12 +02:00
|
|
|
#include "Song.hxx"
|
2013-10-29 20:58:35 +01:00
|
|
|
#include "SongFilter.hxx"
|
2013-10-15 09:21:13 +02:00
|
|
|
#include "Compiler.h"
|
2013-09-05 08:47:10 +02:00
|
|
|
#include "ConfigData.hxx"
|
2013-09-05 19:00:32 +02:00
|
|
|
#include "tag/TagBuilder.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
2013-10-30 16:02:44 +01:00
|
|
|
#include "protocol/Ack.hxx"
|
2011-09-05 22:53:46 +02:00
|
|
|
|
|
|
|
#include <mpd/client.h>
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
class ProxyDatabase : public Database {
|
|
|
|
std::string host;
|
|
|
|
unsigned port;
|
|
|
|
|
|
|
|
struct mpd_connection *connection;
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory *root;
|
2011-09-05 22:53:46 +02:00
|
|
|
|
|
|
|
public:
|
2013-08-04 13:51:27 +02:00
|
|
|
static Database *Create(const config_param ¶m,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error);
|
2011-09-05 22:53:46 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
virtual bool Open(Error &error) override;
|
2011-09-05 22:53:46 +02:00
|
|
|
virtual void Close() override;
|
2013-07-28 13:25:12 +02:00
|
|
|
virtual Song *GetSong(const char *uri_utf8,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error) const override;
|
2013-07-28 13:25:12 +02:00
|
|
|
virtual void ReturnSong(Song *song) const;
|
2012-08-15 23:28:19 +02:00
|
|
|
|
2011-09-05 22:53:46 +02:00
|
|
|
virtual bool Visit(const DatabaseSelection &selection,
|
|
|
|
VisitDirectory visit_directory,
|
|
|
|
VisitSong visit_song,
|
|
|
|
VisitPlaylist visit_playlist,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error) const override;
|
2011-09-05 22:53:46 +02:00
|
|
|
|
2012-08-15 21:32:34 +02:00
|
|
|
virtual bool VisitUniqueTags(const DatabaseSelection &selection,
|
2013-10-20 13:32:59 +02:00
|
|
|
TagType tag_type,
|
2012-08-15 21:32:34 +02:00
|
|
|
VisitString visit_string,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error) const override;
|
2012-08-15 21:32:34 +02:00
|
|
|
|
2012-08-15 22:20:28 +02:00
|
|
|
virtual bool GetStats(const DatabaseSelection &selection,
|
|
|
|
DatabaseStats &stats,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error) const override;
|
2012-08-15 22:20:28 +02:00
|
|
|
|
2013-10-30 19:50:22 +01:00
|
|
|
private:
|
2013-08-10 18:02:44 +02:00
|
|
|
bool Configure(const config_param ¶m, Error &error);
|
2013-10-30 19:50:22 +01:00
|
|
|
|
|
|
|
bool Connect(Error &error);
|
|
|
|
bool CheckConnection(Error &error);
|
|
|
|
bool EnsureConnected(Error &error);
|
2011-09-05 22:53:46 +02:00
|
|
|
};
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain libmpdclient_domain("libmpdclient");
|
2011-09-05 22:53:46 +02:00
|
|
|
|
2012-08-15 21:52:42 +02:00
|
|
|
static constexpr struct {
|
2013-10-20 13:32:59 +02:00
|
|
|
TagType d;
|
2012-08-15 21:52:42 +02:00
|
|
|
enum mpd_tag_type s;
|
|
|
|
} tag_table[] = {
|
|
|
|
{ TAG_ARTIST, MPD_TAG_ARTIST },
|
|
|
|
{ TAG_ALBUM, MPD_TAG_ALBUM },
|
|
|
|
{ TAG_ALBUM_ARTIST, MPD_TAG_ALBUM_ARTIST },
|
|
|
|
{ TAG_TITLE, MPD_TAG_TITLE },
|
|
|
|
{ TAG_TRACK, MPD_TAG_TRACK },
|
|
|
|
{ TAG_NAME, MPD_TAG_NAME },
|
|
|
|
{ TAG_GENRE, MPD_TAG_GENRE },
|
|
|
|
{ TAG_DATE, MPD_TAG_DATE },
|
|
|
|
{ TAG_COMPOSER, MPD_TAG_COMPOSER },
|
|
|
|
{ TAG_PERFORMER, MPD_TAG_PERFORMER },
|
|
|
|
{ TAG_COMMENT, MPD_TAG_COMMENT },
|
|
|
|
{ TAG_DISC, MPD_TAG_DISC },
|
|
|
|
{ TAG_MUSICBRAINZ_ARTISTID, MPD_TAG_MUSICBRAINZ_ARTISTID },
|
|
|
|
{ TAG_MUSICBRAINZ_ALBUMID, MPD_TAG_MUSICBRAINZ_ALBUMID },
|
|
|
|
{ TAG_MUSICBRAINZ_ALBUMARTISTID,
|
|
|
|
MPD_TAG_MUSICBRAINZ_ALBUMARTISTID },
|
|
|
|
{ TAG_MUSICBRAINZ_TRACKID, MPD_TAG_MUSICBRAINZ_TRACKID },
|
|
|
|
{ TAG_NUM_OF_ITEM_TYPES, MPD_TAG_COUNT }
|
|
|
|
};
|
|
|
|
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_const
|
2012-08-15 21:32:34 +02:00
|
|
|
static enum mpd_tag_type
|
2013-10-20 13:32:59 +02:00
|
|
|
Convert(TagType tag_type)
|
2012-08-15 21:32:34 +02:00
|
|
|
{
|
2013-01-28 21:32:01 +01:00
|
|
|
for (auto i = &tag_table[0]; i->d != TAG_NUM_OF_ITEM_TYPES; ++i)
|
2012-08-15 21:32:34 +02:00
|
|
|
if (i->d == tag_type)
|
|
|
|
return i->s;
|
|
|
|
|
|
|
|
return MPD_TAG_COUNT;
|
|
|
|
}
|
|
|
|
|
2011-09-05 22:53:46 +02:00
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
CheckError(struct mpd_connection *connection, Error &error)
|
2011-09-05 22:53:46 +02:00
|
|
|
{
|
2013-08-10 18:02:44 +02:00
|
|
|
const auto code = mpd_connection_get_error(connection);
|
|
|
|
if (code == MPD_ERROR_SUCCESS)
|
2011-09-05 22:53:46 +02:00
|
|
|
return true;
|
|
|
|
|
2013-10-30 16:02:44 +01:00
|
|
|
if (code == MPD_ERROR_SERVER) {
|
|
|
|
/* libmpdclient's "enum mpd_server_error" is the same
|
|
|
|
as our "enum ack" */
|
|
|
|
const auto server_error =
|
|
|
|
mpd_connection_get_server_error(connection);
|
|
|
|
error.Set(ack_domain, (int)server_error,
|
|
|
|
mpd_connection_get_error_message(connection));
|
|
|
|
} else {
|
|
|
|
error.Set(libmpdclient_domain, (int)code,
|
|
|
|
mpd_connection_get_error_message(connection));
|
|
|
|
}
|
|
|
|
|
2012-08-16 00:29:10 +02:00
|
|
|
mpd_connection_clear_error(connection);
|
2011-09-05 22:53:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-30 09:40:23 +01:00
|
|
|
static bool
|
|
|
|
SendConstraints(mpd_connection *connection, const SongFilter::Item &item)
|
|
|
|
{
|
|
|
|
switch (item.GetTag()) {
|
|
|
|
mpd_tag_type tag;
|
|
|
|
|
|
|
|
#if LIBMPDCLIENT_CHECK_VERSION(2,9,0)
|
|
|
|
case LOCATE_TAG_BASE_TYPE:
|
|
|
|
if (mpd_connection_cmp_server_version(connection, 0, 18, 0) < 0)
|
|
|
|
/* requires MPD 0.18 */
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return mpd_search_add_base_constraint(connection,
|
|
|
|
MPD_OPERATOR_DEFAULT,
|
|
|
|
item.GetValue().c_str());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case LOCATE_TAG_FILE_TYPE:
|
|
|
|
return mpd_search_add_uri_constraint(connection,
|
|
|
|
MPD_OPERATOR_DEFAULT,
|
|
|
|
item.GetValue().c_str());
|
|
|
|
|
|
|
|
case LOCATE_TAG_ANY_TYPE:
|
|
|
|
return mpd_search_add_any_tag_constraint(connection,
|
|
|
|
MPD_OPERATOR_DEFAULT,
|
|
|
|
item.GetValue().c_str());
|
|
|
|
|
|
|
|
default:
|
|
|
|
tag = Convert(TagType(item.GetTag()));
|
|
|
|
if (tag == MPD_TAG_COUNT)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return mpd_search_add_tag_constraint(connection,
|
|
|
|
MPD_OPERATOR_DEFAULT,
|
|
|
|
tag,
|
|
|
|
item.GetValue().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
SendConstraints(mpd_connection *connection, const SongFilter &filter)
|
|
|
|
{
|
|
|
|
for (const auto &i : filter.GetItems())
|
|
|
|
if (!SendConstraints(connection, i))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
SendConstraints(mpd_connection *connection, const DatabaseSelection &selection)
|
|
|
|
{
|
|
|
|
#if LIBMPDCLIENT_CHECK_VERSION(2,9,0)
|
|
|
|
if (!selection.uri.empty() &&
|
|
|
|
mpd_connection_cmp_server_version(connection, 0, 18, 0) >= 0) {
|
|
|
|
/* requires MPD 0.18 */
|
|
|
|
if (!mpd_search_add_base_constraint(connection,
|
|
|
|
MPD_OPERATOR_DEFAULT,
|
|
|
|
selection.uri.c_str()))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (selection.filter != nullptr &&
|
|
|
|
!SendConstraints(connection, *selection.filter))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-05 22:53:46 +02:00
|
|
|
Database *
|
2013-08-10 18:02:44 +02:00
|
|
|
ProxyDatabase::Create(const config_param ¶m, Error &error)
|
2011-09-05 22:53:46 +02:00
|
|
|
{
|
|
|
|
ProxyDatabase *db = new ProxyDatabase();
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!db->Configure(param, error)) {
|
2011-09-05 22:53:46 +02:00
|
|
|
delete db;
|
2013-10-28 23:58:17 +01:00
|
|
|
db = nullptr;
|
2011-09-05 22:53:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-08-10 18:02:44 +02:00
|
|
|
ProxyDatabase::Configure(const config_param ¶m, gcc_unused Error &error)
|
2011-09-05 22:53:46 +02:00
|
|
|
{
|
2013-08-04 13:51:27 +02:00
|
|
|
host = param.GetBlockValue("host", "");
|
|
|
|
port = param.GetBlockValue("port", 0u);
|
2011-09-05 22:53:46 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-08-10 18:02:44 +02:00
|
|
|
ProxyDatabase::Open(Error &error)
|
2011-09-05 22:53:46 +02:00
|
|
|
{
|
2013-10-30 19:50:22 +01:00
|
|
|
if (!Connect(error))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
root = Directory::NewRoot();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ProxyDatabase::Close()
|
|
|
|
{
|
|
|
|
root->Free();
|
|
|
|
|
|
|
|
if (connection != nullptr)
|
|
|
|
mpd_connection_free(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ProxyDatabase::Connect(Error &error)
|
|
|
|
{
|
|
|
|
const char *_host = host.empty() ? nullptr : host.c_str();
|
|
|
|
connection = mpd_connection_new(_host, port, 0);
|
2013-10-28 23:58:17 +01:00
|
|
|
if (connection == nullptr) {
|
2013-10-30 19:50:22 +01:00
|
|
|
error.Set(libmpdclient_domain, (int)MPD_ERROR_OOM,
|
|
|
|
"Out of memory");
|
2011-09-05 22:53:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!CheckError(connection, error)) {
|
2013-10-30 19:50:22 +01:00
|
|
|
if (connection != nullptr) {
|
|
|
|
mpd_connection_free(connection);
|
|
|
|
connection = nullptr;
|
|
|
|
}
|
|
|
|
|
2011-09-05 22:53:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-30 19:50:22 +01:00
|
|
|
bool
|
|
|
|
ProxyDatabase::CheckConnection(Error &error)
|
2011-09-05 22:53:46 +02:00
|
|
|
{
|
|
|
|
assert(connection != nullptr);
|
|
|
|
|
2013-10-30 19:50:22 +01:00
|
|
|
if (!mpd_connection_clear_error(connection)) {
|
|
|
|
mpd_connection_free(connection);
|
|
|
|
return Connect(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ProxyDatabase::EnsureConnected(Error &error)
|
|
|
|
{
|
|
|
|
return connection != nullptr
|
|
|
|
? CheckConnection(error)
|
|
|
|
: Connect(error);
|
2011-09-05 22:53:46 +02:00
|
|
|
}
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
static Song *
|
2012-08-16 00:20:02 +02:00
|
|
|
Convert(const struct mpd_song *song);
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *
|
2013-08-10 18:02:44 +02:00
|
|
|
ProxyDatabase::GetSong(const char *uri, Error &error) const
|
2011-09-05 22:53:46 +02:00
|
|
|
{
|
2013-10-30 19:50:22 +01:00
|
|
|
// TODO: eliminate the const_cast
|
|
|
|
if (!const_cast<ProxyDatabase *>(this)->EnsureConnected(error))
|
|
|
|
return nullptr;
|
2011-09-05 22:53:46 +02:00
|
|
|
|
2012-08-16 00:20:02 +02:00
|
|
|
if (!mpd_send_list_meta(connection, uri)) {
|
2013-08-10 18:02:44 +02:00
|
|
|
CheckError(connection, error);
|
2012-08-16 00:20:02 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mpd_song *song = mpd_recv_song(connection);
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *song2 = song != nullptr
|
2012-08-16 00:20:02 +02:00
|
|
|
? Convert(song)
|
|
|
|
: nullptr;
|
2013-10-30 09:37:20 +01:00
|
|
|
if (song != nullptr)
|
|
|
|
mpd_song_free(song);
|
2012-08-16 00:20:02 +02:00
|
|
|
if (!mpd_response_finish(connection)) {
|
|
|
|
if (song2 != nullptr)
|
2013-07-28 13:25:12 +02:00
|
|
|
song2->Free();
|
2012-08-16 00:20:02 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
CheckError(connection, error);
|
2012-08-16 00:20:02 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (song2 == nullptr)
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(db_domain, DB_NOT_FOUND, "No such song: %s", uri);
|
2012-08-16 00:20:02 +02:00
|
|
|
|
|
|
|
return song2;
|
2011-09-05 22:53:46 +02:00
|
|
|
}
|
|
|
|
|
2012-08-15 23:28:19 +02:00
|
|
|
void
|
2013-07-28 13:25:12 +02:00
|
|
|
ProxyDatabase::ReturnSong(Song *song) const
|
2012-08-15 23:28:19 +02:00
|
|
|
{
|
|
|
|
assert(song != nullptr);
|
2013-07-28 13:25:12 +02:00
|
|
|
assert(song->IsInDatabase());
|
|
|
|
assert(song->IsDetached());
|
2012-08-15 23:28:19 +02:00
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
song->Free();
|
2012-08-15 23:28:19 +02:00
|
|
|
}
|
|
|
|
|
2011-09-05 22:53:46 +02:00
|
|
|
static bool
|
2012-08-15 20:57:45 +02:00
|
|
|
Visit(struct mpd_connection *connection, const char *uri,
|
2013-10-29 20:58:35 +01:00
|
|
|
bool recursive, const SongFilter *filter,
|
|
|
|
VisitDirectory visit_directory, VisitSong visit_song,
|
2013-08-10 18:02:44 +02:00
|
|
|
VisitPlaylist visit_playlist, Error &error);
|
2011-09-05 22:53:46 +02:00
|
|
|
|
|
|
|
static bool
|
2012-08-15 20:57:45 +02:00
|
|
|
Visit(struct mpd_connection *connection,
|
2013-10-29 20:58:35 +01:00
|
|
|
bool recursive, const SongFilter *filter,
|
|
|
|
const struct mpd_directory *directory,
|
2011-09-05 22:53:46 +02:00
|
|
|
VisitDirectory visit_directory, VisitSong visit_song,
|
2013-08-10 18:02:44 +02:00
|
|
|
VisitPlaylist visit_playlist, Error &error)
|
2011-09-05 22:53:46 +02:00
|
|
|
{
|
2012-08-15 20:57:45 +02:00
|
|
|
const char *path = mpd_directory_get_path(directory);
|
|
|
|
|
|
|
|
if (visit_directory) {
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory *d = Directory::NewGeneric(path, &detached_root);
|
2013-08-10 18:02:44 +02:00
|
|
|
bool success = visit_directory(*d, error);
|
2013-01-02 23:06:10 +01:00
|
|
|
d->Free();
|
2012-08-15 20:57:45 +02:00
|
|
|
if (!success)
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-05 22:53:46 +02:00
|
|
|
|
2012-08-15 20:57:45 +02:00
|
|
|
if (recursive &&
|
2013-10-29 20:58:35 +01:00
|
|
|
!Visit(connection, path, recursive, filter,
|
2013-08-10 18:02:44 +02:00
|
|
|
visit_directory, visit_song, visit_playlist, error))
|
2012-08-15 20:57:45 +02:00
|
|
|
return false;
|
2011-09-05 22:53:46 +02:00
|
|
|
|
2012-08-15 20:57:45 +02:00
|
|
|
return true;
|
2011-09-05 22:53:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-10-20 13:32:59 +02:00
|
|
|
Copy(TagBuilder &tag, TagType d_tag,
|
2011-09-05 22:53:46 +02:00
|
|
|
const struct mpd_song *song, enum mpd_tag_type s_tag)
|
|
|
|
{
|
|
|
|
|
|
|
|
for (unsigned i = 0;; ++i) {
|
|
|
|
const char *value = mpd_song_get_tag(song, s_tag, i);
|
2013-10-28 23:58:17 +01:00
|
|
|
if (value == nullptr)
|
2011-09-05 22:53:46 +02:00
|
|
|
break;
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
tag.AddItem(d_tag, value);
|
2011-09-05 22:53:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
static Song *
|
2012-08-15 20:57:45 +02:00
|
|
|
Convert(const struct mpd_song *song)
|
2011-09-05 22:53:46 +02:00
|
|
|
{
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *s = Song::NewDetached(mpd_song_get_uri(song));
|
2011-09-05 22:53:46 +02:00
|
|
|
|
|
|
|
s->mtime = mpd_song_get_last_modified(song);
|
|
|
|
s->start_ms = mpd_song_get_start(song) * 1000;
|
|
|
|
s->end_ms = mpd_song_get_end(song) * 1000;
|
|
|
|
|
2013-09-05 19:00:32 +02:00
|
|
|
TagBuilder tag;
|
|
|
|
tag.SetTime(mpd_song_get_duration(song));
|
2011-09-05 22:53:46 +02:00
|
|
|
|
2013-01-28 21:32:01 +01:00
|
|
|
for (const auto *i = &tag_table[0]; i->d != TAG_NUM_OF_ITEM_TYPES; ++i)
|
2013-09-05 19:00:32 +02:00
|
|
|
Copy(tag, i->d, song, i->s);
|
2011-09-05 22:53:46 +02:00
|
|
|
|
2013-09-05 19:00:32 +02:00
|
|
|
s->tag = tag.Commit();
|
2011-09-05 22:53:46 +02:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2013-10-29 20:58:35 +01:00
|
|
|
gcc_pure
|
|
|
|
static bool
|
|
|
|
Match(const SongFilter *filter, const Song &song)
|
|
|
|
{
|
|
|
|
return filter == nullptr || filter->Match(song);
|
|
|
|
}
|
|
|
|
|
2011-09-05 22:53:46 +02:00
|
|
|
static bool
|
2013-10-29 20:58:35 +01:00
|
|
|
Visit(const SongFilter *filter,
|
|
|
|
const struct mpd_song *song,
|
2013-08-10 18:02:44 +02:00
|
|
|
VisitSong visit_song, Error &error)
|
2011-09-05 22:53:46 +02:00
|
|
|
{
|
|
|
|
if (!visit_song)
|
|
|
|
return true;
|
|
|
|
|
2013-07-28 13:25:12 +02:00
|
|
|
Song *s = Convert(song);
|
2013-10-29 20:58:35 +01:00
|
|
|
bool success = !Match(filter, *s) || visit_song(*s, error);
|
2013-07-28 13:25:12 +02:00
|
|
|
s->Free();
|
2011-09-05 22:53:46 +02:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2012-08-15 20:57:45 +02:00
|
|
|
Visit(const struct mpd_playlist *playlist,
|
2013-08-10 18:02:44 +02:00
|
|
|
VisitPlaylist visit_playlist, Error &error)
|
2011-09-05 22:53:46 +02:00
|
|
|
{
|
|
|
|
if (!visit_playlist)
|
|
|
|
return true;
|
|
|
|
|
2013-01-02 22:04:03 +01:00
|
|
|
PlaylistInfo p(mpd_playlist_get_path(playlist),
|
|
|
|
mpd_playlist_get_last_modified(playlist));
|
2011-09-05 22:53:46 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
return visit_playlist(p, detached_root, error);
|
2011-09-05 22:53:46 +02:00
|
|
|
}
|
|
|
|
|
2012-08-15 21:59:56 +02:00
|
|
|
class ProxyEntity {
|
|
|
|
struct mpd_entity *entity;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ProxyEntity(struct mpd_entity *_entity)
|
|
|
|
:entity(_entity) {}
|
|
|
|
|
|
|
|
ProxyEntity(const ProxyEntity &other) = delete;
|
|
|
|
|
|
|
|
ProxyEntity(ProxyEntity &&other)
|
|
|
|
:entity(other.entity) {
|
|
|
|
other.entity = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
~ProxyEntity() {
|
|
|
|
if (entity != nullptr)
|
|
|
|
mpd_entity_free(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProxyEntity &operator=(const ProxyEntity &other) = delete;
|
|
|
|
|
|
|
|
operator const struct mpd_entity *() const {
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static std::list<ProxyEntity>
|
2011-09-05 22:53:46 +02:00
|
|
|
ReceiveEntities(struct mpd_connection *connection)
|
|
|
|
{
|
2012-08-15 21:59:56 +02:00
|
|
|
std::list<ProxyEntity> entities;
|
2011-09-05 22:53:46 +02:00
|
|
|
struct mpd_entity *entity;
|
2013-10-28 23:58:17 +01:00
|
|
|
while ((entity = mpd_recv_entity(connection)) != nullptr)
|
2012-08-15 21:59:56 +02:00
|
|
|
entities.push_back(ProxyEntity(entity));
|
2011-09-05 22:53:46 +02:00
|
|
|
|
|
|
|
mpd_response_finish(connection);
|
|
|
|
return entities;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2012-08-15 20:57:45 +02:00
|
|
|
Visit(struct mpd_connection *connection, const char *uri,
|
2013-10-29 20:58:35 +01:00
|
|
|
bool recursive, const SongFilter *filter,
|
|
|
|
VisitDirectory visit_directory, VisitSong visit_song,
|
2013-08-10 18:02:44 +02:00
|
|
|
VisitPlaylist visit_playlist, Error &error)
|
2011-09-05 22:53:46 +02:00
|
|
|
{
|
2012-08-15 20:57:45 +02:00
|
|
|
if (!mpd_send_list_meta(connection, uri))
|
2013-08-10 18:02:44 +02:00
|
|
|
return CheckError(connection, error);
|
2011-09-05 22:53:46 +02:00
|
|
|
|
2012-08-15 21:59:56 +02:00
|
|
|
std::list<ProxyEntity> entities(ReceiveEntities(connection));
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!CheckError(connection, error))
|
2011-09-05 22:53:46 +02:00
|
|
|
return false;
|
|
|
|
|
2012-08-15 21:59:56 +02:00
|
|
|
for (const auto &entity : entities) {
|
2011-09-05 22:53:46 +02:00
|
|
|
switch (mpd_entity_get_type(entity)) {
|
|
|
|
case MPD_ENTITY_TYPE_UNKNOWN:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MPD_ENTITY_TYPE_DIRECTORY:
|
2013-10-29 20:58:35 +01:00
|
|
|
if (!Visit(connection, recursive, filter,
|
2012-08-15 22:03:02 +02:00
|
|
|
mpd_entity_get_directory(entity),
|
|
|
|
visit_directory, visit_song, visit_playlist,
|
2013-08-10 18:02:44 +02:00
|
|
|
error))
|
2012-08-15 22:03:02 +02:00
|
|
|
return false;
|
2011-09-05 22:53:46 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MPD_ENTITY_TYPE_SONG:
|
2013-10-29 20:58:35 +01:00
|
|
|
if (!Visit(filter,
|
|
|
|
mpd_entity_get_song(entity), visit_song,
|
2013-08-10 18:02:44 +02:00
|
|
|
error))
|
2012-08-15 22:03:02 +02:00
|
|
|
return false;
|
2011-09-05 22:53:46 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MPD_ENTITY_TYPE_PLAYLIST:
|
2012-08-15 20:57:45 +02:00
|
|
|
if (!Visit(mpd_entity_get_playlist(entity),
|
2013-08-10 18:02:44 +02:00
|
|
|
visit_playlist, error))
|
2012-08-15 22:03:02 +02:00
|
|
|
return false;
|
2011-09-05 22:53:46 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
return CheckError(connection, error);
|
2011-09-05 22:53:46 +02:00
|
|
|
}
|
|
|
|
|
2013-10-30 10:00:57 +01:00
|
|
|
static bool
|
|
|
|
SearchSongs(struct mpd_connection *connection,
|
|
|
|
const DatabaseSelection &selection,
|
|
|
|
VisitSong visit_song,
|
|
|
|
Error &error)
|
|
|
|
{
|
|
|
|
assert(selection.recursive);
|
|
|
|
assert(visit_song);
|
|
|
|
|
|
|
|
const bool exact = selection.filter == nullptr ||
|
|
|
|
!selection.filter->HasFoldCase();
|
|
|
|
|
|
|
|
if (!mpd_search_db_songs(connection, exact) ||
|
|
|
|
!SendConstraints(connection, selection) ||
|
|
|
|
!mpd_search_commit(connection))
|
|
|
|
return CheckError(connection, error);
|
|
|
|
|
|
|
|
bool result = true;
|
|
|
|
struct mpd_song *song;
|
|
|
|
while (result && (song = mpd_recv_song(connection)) != nullptr) {
|
|
|
|
Song *song2 = Convert(song);
|
|
|
|
mpd_song_free(song);
|
|
|
|
|
|
|
|
result = !Match(selection.filter, *song2) ||
|
|
|
|
visit_song(*song2, error);
|
|
|
|
song2->Free();
|
|
|
|
}
|
|
|
|
|
|
|
|
mpd_response_finish(connection);
|
|
|
|
return result && CheckError(connection, error);
|
|
|
|
}
|
|
|
|
|
2011-09-05 22:53:46 +02:00
|
|
|
bool
|
|
|
|
ProxyDatabase::Visit(const DatabaseSelection &selection,
|
|
|
|
VisitDirectory visit_directory,
|
|
|
|
VisitSong visit_song,
|
|
|
|
VisitPlaylist visit_playlist,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error) const
|
2011-09-05 22:53:46 +02:00
|
|
|
{
|
2013-10-30 19:50:22 +01:00
|
|
|
// TODO: eliminate the const_cast
|
|
|
|
if (!const_cast<ProxyDatabase *>(this)->EnsureConnected(error))
|
|
|
|
return nullptr;
|
2011-09-05 22:53:46 +02:00
|
|
|
|
2013-10-30 10:00:57 +01:00
|
|
|
if (!visit_directory && !visit_playlist && selection.recursive)
|
|
|
|
/* this optimized code path can only be used under
|
|
|
|
certain conditions */
|
|
|
|
return ::SearchSongs(connection, selection, visit_song, error);
|
|
|
|
|
|
|
|
/* fall back to recursive walk (slow!) */
|
2013-10-29 20:58:35 +01:00
|
|
|
return ::Visit(connection, selection.uri.c_str(),
|
|
|
|
selection.recursive, selection.filter,
|
2012-08-15 20:57:45 +02:00
|
|
|
visit_directory, visit_song, visit_playlist,
|
2013-08-10 18:02:44 +02:00
|
|
|
error);
|
2011-09-05 22:53:46 +02:00
|
|
|
}
|
|
|
|
|
2012-08-15 21:32:34 +02:00
|
|
|
bool
|
|
|
|
ProxyDatabase::VisitUniqueTags(const DatabaseSelection &selection,
|
2013-10-20 13:32:59 +02:00
|
|
|
TagType tag_type,
|
2012-08-15 21:32:34 +02:00
|
|
|
VisitString visit_string,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error) const
|
2012-08-15 21:32:34 +02:00
|
|
|
{
|
2013-10-30 19:50:22 +01:00
|
|
|
// TODO: eliminate the const_cast
|
|
|
|
if (!const_cast<ProxyDatabase *>(this)->EnsureConnected(error))
|
|
|
|
return nullptr;
|
|
|
|
|
2012-08-15 21:32:34 +02:00
|
|
|
enum mpd_tag_type tag_type2 = Convert(tag_type);
|
|
|
|
if (tag_type2 == MPD_TAG_COUNT) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(libmpdclient_domain, "Unsupported tag");
|
2012-08-15 21:32:34 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mpd_search_db_tags(connection, tag_type2))
|
2013-08-10 18:02:44 +02:00
|
|
|
return CheckError(connection, error);
|
2012-08-15 21:32:34 +02:00
|
|
|
|
2013-10-30 09:40:23 +01:00
|
|
|
if (!SendConstraints(connection, selection))
|
|
|
|
return CheckError(connection, error);
|
2012-08-15 21:32:34 +02:00
|
|
|
|
|
|
|
if (!mpd_search_commit(connection))
|
2013-08-10 18:02:44 +02:00
|
|
|
return CheckError(connection, error);
|
2012-08-15 21:32:34 +02:00
|
|
|
|
|
|
|
bool result = true;
|
|
|
|
|
|
|
|
struct mpd_pair *pair;
|
|
|
|
while (result &&
|
|
|
|
(pair = mpd_recv_pair_tag(connection, tag_type2)) != nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
result = visit_string(pair->value, error);
|
2012-08-15 21:32:34 +02:00
|
|
|
mpd_return_pair(connection, pair);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mpd_response_finish(connection) &&
|
2013-08-10 18:02:44 +02:00
|
|
|
CheckError(connection, error) &&
|
2012-08-15 21:32:34 +02:00
|
|
|
result;
|
|
|
|
}
|
|
|
|
|
2012-08-15 22:20:28 +02:00
|
|
|
bool
|
|
|
|
ProxyDatabase::GetStats(const DatabaseSelection &selection,
|
2013-08-10 18:02:44 +02:00
|
|
|
DatabaseStats &stats, Error &error) const
|
2012-08-15 22:20:28 +02:00
|
|
|
{
|
|
|
|
// TODO: match
|
|
|
|
(void)selection;
|
|
|
|
|
2013-10-30 19:50:22 +01:00
|
|
|
// TODO: eliminate the const_cast
|
|
|
|
if (!const_cast<ProxyDatabase *>(this)->EnsureConnected(error))
|
|
|
|
return nullptr;
|
|
|
|
|
2012-08-15 22:20:28 +02:00
|
|
|
struct mpd_stats *stats2 =
|
|
|
|
mpd_run_stats(connection);
|
|
|
|
if (stats2 == nullptr)
|
2013-08-10 18:02:44 +02:00
|
|
|
return CheckError(connection, error);
|
2012-08-15 22:20:28 +02:00
|
|
|
|
|
|
|
stats.song_count = mpd_stats_get_number_of_songs(stats2);
|
|
|
|
stats.total_duration = mpd_stats_get_db_play_time(stats2);
|
|
|
|
stats.artist_count = mpd_stats_get_number_of_artists(stats2);
|
|
|
|
stats.album_count = mpd_stats_get_number_of_albums(stats2);
|
|
|
|
mpd_stats_free(stats2);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-05 22:53:46 +02:00
|
|
|
const DatabasePlugin proxy_db_plugin = {
|
|
|
|
"proxy",
|
|
|
|
ProxyDatabase::Create,
|
|
|
|
};
|