2011-09-05 22:53:46 +02:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 The Music Player Daemon Project
|
2011-09-05 22:53:46 +02:00
|
|
|
* 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"
|
2014-02-19 22:54:52 +01:00
|
|
|
#include "db/Interface.hxx"
|
2014-01-24 16:18:50 +01:00
|
|
|
#include "db/DatabasePlugin.hxx"
|
|
|
|
#include "db/DatabaseListener.hxx"
|
|
|
|
#include "db/Selection.hxx"
|
|
|
|
#include "db/DatabaseError.hxx"
|
2014-01-27 11:05:21 +01:00
|
|
|
#include "db/PlaylistInfo.hxx"
|
2014-01-24 16:18:50 +01:00
|
|
|
#include "db/LightDirectory.hxx"
|
|
|
|
#include "db/LightSong.hxx"
|
2014-02-19 22:54:52 +01:00
|
|
|
#include "db/Stats.hxx"
|
2013-10-29 20:58:35 +01:00
|
|
|
#include "SongFilter.hxx"
|
2013-10-15 09:21:13 +02:00
|
|
|
#include "Compiler.h"
|
2015-01-21 22:13:44 +01:00
|
|
|
#include "config/Block.hxx"
|
2013-09-05 19:00:32 +02:00
|
|
|
#include "tag/TagBuilder.hxx"
|
2014-01-19 10:51:34 +01:00
|
|
|
#include "tag/Tag.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
2016-03-18 23:10:42 +01:00
|
|
|
#include "util/ScopeExit.hxx"
|
2013-10-30 16:02:44 +01:00
|
|
|
#include "protocol/Ack.hxx"
|
2014-01-10 23:44:03 +01:00
|
|
|
#include "event/SocketMonitor.hxx"
|
|
|
|
#include "event/IdleMonitor.hxx"
|
|
|
|
#include "Log.hxx"
|
2011-09-05 22:53:46 +02:00
|
|
|
|
|
|
|
#include <mpd/client.h>
|
2014-01-10 23:44:03 +01:00
|
|
|
#include <mpd/async.h>
|
2011-09-05 22:53:46 +02:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
|
|
|
|
2014-01-19 10:51:34 +01:00
|
|
|
class ProxySong : public LightSong {
|
|
|
|
Tag tag2;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ProxySong(const mpd_song *song);
|
|
|
|
};
|
|
|
|
|
|
|
|
class AllocatedProxySong : public ProxySong {
|
|
|
|
mpd_song *const song;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit AllocatedProxySong(mpd_song *_song)
|
|
|
|
:ProxySong(_song), song(_song) {}
|
|
|
|
|
|
|
|
~AllocatedProxySong() {
|
|
|
|
mpd_song_free(song);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-10 23:44:03 +01:00
|
|
|
class ProxyDatabase final : public Database, SocketMonitor, IdleMonitor {
|
|
|
|
DatabaseListener &listener;
|
|
|
|
|
2011-09-05 22:53:46 +02:00
|
|
|
std::string host;
|
|
|
|
unsigned port;
|
2015-01-15 23:33:10 +01:00
|
|
|
bool keepalive;
|
2011-09-05 22:53:46 +02:00
|
|
|
|
|
|
|
struct mpd_connection *connection;
|
|
|
|
|
2013-11-22 00:45:27 +01:00
|
|
|
/* this is mutable because GetStats() must be "const" */
|
|
|
|
mutable time_t update_stamp;
|
|
|
|
|
2014-01-10 23:44:03 +01:00
|
|
|
/**
|
|
|
|
* The libmpdclient idle mask that was removed from the other
|
|
|
|
* MPD. This will be handled by the next OnIdle() call.
|
|
|
|
*/
|
|
|
|
unsigned idle_received;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the #connection currently "idle"? That is, did we send
|
|
|
|
* the "idle" command to it?
|
|
|
|
*/
|
|
|
|
bool is_idle;
|
|
|
|
|
2011-09-05 22:53:46 +02:00
|
|
|
public:
|
2014-01-10 23:44:03 +01:00
|
|
|
ProxyDatabase(EventLoop &_loop, DatabaseListener &_listener)
|
2014-02-19 23:17:21 +01:00
|
|
|
:Database(proxy_db_plugin),
|
|
|
|
SocketMonitor(_loop), IdleMonitor(_loop),
|
2014-01-10 23:44:03 +01:00
|
|
|
listener(_listener) {}
|
|
|
|
|
2014-01-11 01:01:54 +01:00
|
|
|
static Database *Create(EventLoop &loop, DatabaseListener &listener,
|
2015-01-21 22:13:44 +01:00
|
|
|
const ConfigBlock &block,
|
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;
|
2014-01-19 10:51:34 +01:00
|
|
|
virtual const LightSong *GetSong(const char *uri_utf8,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error) const override;
|
2014-12-26 14:26:31 +01:00
|
|
|
void ReturnSong(const LightSong *song) const override;
|
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,
|
2015-08-24 11:53:30 +02:00
|
|
|
TagType tag_type, tag_mask_t group_mask,
|
2014-04-24 10:20:24 +02:00
|
|
|
VisitTag visit_tag,
|
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
|
|
|
|
2014-07-29 23:16:46 +02:00
|
|
|
virtual unsigned Update(const char *uri_utf8, bool discard,
|
|
|
|
Error &error) override;
|
|
|
|
|
2013-11-22 00:35:29 +01:00
|
|
|
virtual time_t GetUpdateStamp() const override {
|
2013-11-22 00:45:27 +01:00
|
|
|
return update_stamp;
|
2013-11-22 00:35:29 +01:00
|
|
|
}
|
|
|
|
|
2013-10-30 19:50:22 +01:00
|
|
|
private:
|
2015-01-21 22:13:44 +01:00
|
|
|
bool Configure(const ConfigBlock &block, Error &error);
|
2013-10-30 19:50:22 +01:00
|
|
|
|
|
|
|
bool Connect(Error &error);
|
|
|
|
bool CheckConnection(Error &error);
|
|
|
|
bool EnsureConnected(Error &error);
|
2014-01-11 00:21:29 +01:00
|
|
|
|
|
|
|
void Disconnect();
|
2014-01-10 23:44:03 +01:00
|
|
|
|
|
|
|
/* virtual methods from SocketMonitor */
|
|
|
|
virtual bool OnSocketReady(unsigned flags) override;
|
|
|
|
|
|
|
|
/* virtual methods from IdleMonitor */
|
|
|
|
virtual void OnIdle() override;
|
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 },
|
2014-09-27 18:38:23 +02:00
|
|
|
#if LIBMPDCLIENT_CHECK_VERSION(2,10,0)
|
|
|
|
{ TAG_MUSICBRAINZ_RELEASETRACKID,
|
|
|
|
MPD_TAG_MUSICBRAINZ_RELEASETRACKID },
|
|
|
|
#endif
|
2012-08-15 21:52:42 +02:00
|
|
|
{ TAG_NUM_OF_ITEM_TYPES, MPD_TAG_COUNT }
|
|
|
|
};
|
|
|
|
|
2014-01-19 10:51:34 +01:00
|
|
|
static void
|
|
|
|
Copy(TagBuilder &tag, TagType d_tag,
|
|
|
|
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);
|
|
|
|
if (value == nullptr)
|
|
|
|
break;
|
|
|
|
|
|
|
|
tag.AddItem(d_tag, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ProxySong::ProxySong(const mpd_song *song)
|
|
|
|
{
|
|
|
|
directory = nullptr;
|
|
|
|
uri = mpd_song_get_uri(song);
|
2014-06-21 12:37:23 +02:00
|
|
|
real_uri = nullptr;
|
2014-01-19 10:51:34 +01:00
|
|
|
tag = &tag2;
|
|
|
|
mtime = mpd_song_get_last_modified(song);
|
2014-06-23 08:57:51 +02:00
|
|
|
|
|
|
|
#if LIBMPDCLIENT_CHECK_VERSION(2,3,0)
|
2014-08-28 12:35:26 +02:00
|
|
|
start_time = SongTime::FromS(mpd_song_get_start(song));
|
|
|
|
end_time = SongTime::FromS(mpd_song_get_end(song));
|
2014-06-23 08:57:51 +02:00
|
|
|
#else
|
2014-08-28 12:35:26 +02:00
|
|
|
start_time = end_time = SongTime::zero();
|
2014-06-23 08:57:51 +02:00
|
|
|
#endif
|
2014-01-19 10:51:34 +01:00
|
|
|
|
|
|
|
TagBuilder tag_builder;
|
2014-08-29 12:14:27 +02:00
|
|
|
|
|
|
|
const unsigned duration = mpd_song_get_duration(song);
|
|
|
|
if (duration > 0)
|
|
|
|
tag_builder.SetDuration(SignedSongTime::FromS(duration));
|
2014-01-19 10:51:34 +01:00
|
|
|
|
|
|
|
for (const auto *i = &tag_table[0]; i->d != TAG_NUM_OF_ITEM_TYPES; ++i)
|
|
|
|
Copy(tag_builder, i->d, song, i->s);
|
|
|
|
|
|
|
|
tag_builder.Commit(tag2);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2016-03-18 23:10:42 +01:00
|
|
|
AtScopeExit(connection) {
|
|
|
|
mpd_connection_clear_error(connection);
|
|
|
|
};
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
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,
|
2015-06-25 23:14:40 +02:00
|
|
|
item.GetValue());
|
2013-10-30 09:40:23 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
case LOCATE_TAG_FILE_TYPE:
|
|
|
|
return mpd_search_add_uri_constraint(connection,
|
|
|
|
MPD_OPERATOR_DEFAULT,
|
2015-06-25 23:14:40 +02:00
|
|
|
item.GetValue());
|
2013-10-30 09:40:23 +01:00
|
|
|
|
|
|
|
case LOCATE_TAG_ANY_TYPE:
|
|
|
|
return mpd_search_add_any_tag_constraint(connection,
|
|
|
|
MPD_OPERATOR_DEFAULT,
|
2015-06-25 23:14:40 +02:00
|
|
|
item.GetValue());
|
2013-10-30 09:40:23 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
tag = Convert(TagType(item.GetTag()));
|
|
|
|
if (tag == MPD_TAG_COUNT)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return mpd_search_add_tag_constraint(connection,
|
|
|
|
MPD_OPERATOR_DEFAULT,
|
|
|
|
tag,
|
2015-06-25 23:14:40 +02:00
|
|
|
item.GetValue());
|
2013-10-30 09:40:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 *
|
2014-01-10 23:44:03 +01:00
|
|
|
ProxyDatabase::Create(EventLoop &loop, DatabaseListener &listener,
|
2015-01-21 22:13:44 +01:00
|
|
|
const ConfigBlock &block, Error &error)
|
2011-09-05 22:53:46 +02:00
|
|
|
{
|
2014-01-10 23:44:03 +01:00
|
|
|
ProxyDatabase *db = new ProxyDatabase(loop, listener);
|
2015-01-21 22:13:44 +01:00
|
|
|
if (!db->Configure(block, 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
|
2015-01-21 22:13:44 +01:00
|
|
|
ProxyDatabase::Configure(const ConfigBlock &block, gcc_unused Error &error)
|
2011-09-05 22:53:46 +02:00
|
|
|
{
|
2015-01-21 22:13:44 +01:00
|
|
|
host = block.GetBlockValue("host", "");
|
|
|
|
port = block.GetBlockValue("port", 0u);
|
|
|
|
keepalive = block.GetBlockValue("keepalive", false);
|
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;
|
|
|
|
|
2013-11-22 00:45:27 +01:00
|
|
|
update_stamp = 0;
|
2013-10-30 19:50:22 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ProxyDatabase::Close()
|
|
|
|
{
|
|
|
|
if (connection != nullptr)
|
2014-01-11 00:21:29 +01:00
|
|
|
Disconnect();
|
2013-10-30 19:50:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-03-06 13:35:42 +01:00
|
|
|
if (!CheckError(connection, error)) {
|
|
|
|
mpd_connection_free(connection);
|
|
|
|
connection = nullptr;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-15 23:33:10 +01:00
|
|
|
#if LIBMPDCLIENT_CHECK_VERSION(2, 10, 0)
|
|
|
|
mpd_connection_set_keepalive(connection, keepalive);
|
|
|
|
#endif
|
|
|
|
|
2014-01-10 23:44:03 +01:00
|
|
|
idle_received = unsigned(-1);
|
|
|
|
is_idle = false;
|
|
|
|
|
|
|
|
SocketMonitor::Open(mpd_async_get_fd(mpd_connection_get_async(connection)));
|
|
|
|
IdleMonitor::Schedule();
|
|
|
|
|
2011-09-05 22:53:46 +02:00
|
|
|
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)) {
|
2014-01-11 00:21:29 +01:00
|
|
|
Disconnect();
|
2013-10-30 19:50:22 +01:00
|
|
|
return Connect(error);
|
|
|
|
}
|
|
|
|
|
2014-01-10 23:44:03 +01:00
|
|
|
if (is_idle) {
|
|
|
|
unsigned idle = mpd_run_noidle(connection);
|
|
|
|
if (idle == 0 && !CheckError(connection, error)) {
|
|
|
|
Disconnect();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
idle_received |= idle;
|
|
|
|
is_idle = false;
|
|
|
|
IdleMonitor::Schedule();
|
|
|
|
}
|
|
|
|
|
2013-10-30 19:50:22 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ProxyDatabase::EnsureConnected(Error &error)
|
|
|
|
{
|
|
|
|
return connection != nullptr
|
|
|
|
? CheckConnection(error)
|
|
|
|
: Connect(error);
|
2011-09-05 22:53:46 +02:00
|
|
|
}
|
|
|
|
|
2014-01-11 00:21:29 +01:00
|
|
|
void
|
|
|
|
ProxyDatabase::Disconnect()
|
|
|
|
{
|
|
|
|
assert(connection != nullptr);
|
|
|
|
|
2014-01-10 23:44:03 +01:00
|
|
|
IdleMonitor::Cancel();
|
|
|
|
SocketMonitor::Steal();
|
|
|
|
|
2014-01-11 00:21:29 +01:00
|
|
|
mpd_connection_free(connection);
|
|
|
|
connection = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-01-10 23:44:03 +01:00
|
|
|
bool
|
|
|
|
ProxyDatabase::OnSocketReady(gcc_unused unsigned flags)
|
|
|
|
{
|
|
|
|
assert(connection != nullptr);
|
|
|
|
|
|
|
|
if (!is_idle) {
|
|
|
|
// TODO: can this happen?
|
|
|
|
IdleMonitor::Schedule();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned idle = (unsigned)mpd_recv_idle(connection, false);
|
|
|
|
if (idle == 0) {
|
|
|
|
Error error;
|
|
|
|
if (!CheckError(connection, error)) {
|
|
|
|
LogError(error);
|
|
|
|
Disconnect();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* let OnIdle() handle this */
|
|
|
|
idle_received |= idle;
|
|
|
|
is_idle = false;
|
|
|
|
IdleMonitor::Schedule();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ProxyDatabase::OnIdle()
|
|
|
|
{
|
|
|
|
assert(connection != nullptr);
|
|
|
|
|
|
|
|
/* handle previous idle events */
|
|
|
|
|
|
|
|
if (idle_received & MPD_IDLE_DATABASE)
|
|
|
|
listener.OnDatabaseModified();
|
|
|
|
|
|
|
|
idle_received = 0;
|
|
|
|
|
|
|
|
/* send a new idle command to the other MPD */
|
|
|
|
|
|
|
|
if (is_idle)
|
|
|
|
// TODO: can this happen?
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!mpd_send_idle_mask(connection, MPD_IDLE_DATABASE)) {
|
|
|
|
Error error;
|
|
|
|
if (!CheckError(connection, error))
|
|
|
|
LogError(error);
|
|
|
|
|
|
|
|
SocketMonitor::Steal();
|
|
|
|
mpd_connection_free(connection);
|
|
|
|
connection = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
is_idle = true;
|
|
|
|
SocketMonitor::ScheduleRead();
|
|
|
|
}
|
|
|
|
|
2014-01-19 10:51:34 +01:00
|
|
|
const LightSong *
|
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);
|
2014-01-19 11:37:42 +01:00
|
|
|
if (!mpd_response_finish(connection) &&
|
|
|
|
!CheckError(connection, error)) {
|
|
|
|
if (song != nullptr)
|
|
|
|
mpd_song_free(song);
|
2012-08-16 00:20:02 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-02-28 11:47:03 +01:00
|
|
|
if (song == nullptr)
|
|
|
|
throw DatabaseError(DatabaseErrorCode::NOT_FOUND,
|
|
|
|
"No such song");
|
2012-08-16 00:20:02 +02:00
|
|
|
|
2014-01-19 10:51:34 +01:00
|
|
|
return new AllocatedProxySong(song);
|
2011-09-05 22:53:46 +02:00
|
|
|
}
|
|
|
|
|
2012-08-15 23:28:19 +02:00
|
|
|
void
|
2014-01-19 10:51:34 +01:00
|
|
|
ProxyDatabase::ReturnSong(const LightSong *_song) const
|
2012-08-15 23:28:19 +02:00
|
|
|
{
|
2014-01-19 10:51:34 +01:00
|
|
|
assert(_song != nullptr);
|
2012-08-15 23:28:19 +02:00
|
|
|
|
2014-01-19 10:51:34 +01:00
|
|
|
AllocatedProxySong *song = (AllocatedProxySong *)
|
|
|
|
const_cast<LightSong *>(_song);
|
|
|
|
delete song;
|
2012-08-15 23:28:19 +02:00
|
|
|
}
|
|
|
|
|
2011-09-05 22:53:46 +02:00
|
|
|
static bool
|
2014-01-22 22:40:42 +01: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
|
2014-01-22 22:40:42 +01: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);
|
2014-01-22 23:09:36 +01:00
|
|
|
#if LIBMPDCLIENT_CHECK_VERSION(2,9,0)
|
2014-01-22 23:07:21 +01:00
|
|
|
time_t mtime = mpd_directory_get_last_modified(directory);
|
2014-01-22 23:09:36 +01:00
|
|
|
#else
|
|
|
|
time_t mtime = 0;
|
|
|
|
#endif
|
2012-08-15 20:57:45 +02:00
|
|
|
|
2014-01-09 13:16:32 +01:00
|
|
|
if (visit_directory &&
|
2014-01-22 23:07:21 +01:00
|
|
|
!visit_directory(LightDirectory(path, mtime), error))
|
2014-01-09 13:16:32 +01:00
|
|
|
return false;
|
2011-09-05 22:53:46 +02:00
|
|
|
|
2012-08-15 20:57:45 +02:00
|
|
|
if (recursive &&
|
2014-01-22 22:40:42 +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
|
|
|
}
|
|
|
|
|
2013-10-29 20:58:35 +01:00
|
|
|
gcc_pure
|
|
|
|
static bool
|
2014-01-19 10:51:34 +01:00
|
|
|
Match(const SongFilter *filter, const LightSong &song)
|
2013-10-29 20:58:35 +01:00
|
|
|
{
|
|
|
|
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,
|
2014-01-19 10:51:34 +01:00
|
|
|
const 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;
|
|
|
|
|
2014-01-19 10:51:34 +01:00
|
|
|
const ProxySong song(_song);
|
|
|
|
return !Match(filter, song) || visit_song(song, error);
|
2011-09-05 22:53:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2014-01-22 22:40:42 +01: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
|
|
|
|
2014-01-22 22:40:42 +01:00
|
|
|
return visit_playlist(p, LightDirectory::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
|
2014-01-22 22:40:42 +01: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:
|
2014-01-22 22:40:42 +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:
|
2014-01-22 22:40:42 +01: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) {
|
2014-01-19 10:51:34 +01:00
|
|
|
AllocatedProxySong song2(song);
|
2013-10-30 10:00:57 +01:00
|
|
|
|
2014-01-19 10:51:34 +01:00
|
|
|
result = !Match(selection.filter, song2) ||
|
|
|
|
visit_song(song2, error);
|
2013-10-30 10:00:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mpd_response_finish(connection);
|
|
|
|
return result && CheckError(connection, error);
|
|
|
|
}
|
|
|
|
|
2014-06-23 09:12:51 +02:00
|
|
|
/**
|
|
|
|
* Check whether we can use the "base" constraint. Requires
|
|
|
|
* libmpdclient 2.9 and MPD 0.18.
|
|
|
|
*/
|
|
|
|
gcc_pure
|
|
|
|
static bool
|
|
|
|
ServerSupportsSearchBase(const struct mpd_connection *connection)
|
|
|
|
{
|
|
|
|
#if LIBMPDCLIENT_CHECK_VERSION(2,9,0)
|
|
|
|
return mpd_connection_cmp_server_version(connection, 0, 18, 0) >= 0;
|
|
|
|
#else
|
|
|
|
(void)connection;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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))
|
2014-12-26 13:50:54 +01:00
|
|
|
return false;
|
2011-09-05 22:53:46 +02:00
|
|
|
|
2014-06-23 09:12:51 +02:00
|
|
|
if (!visit_directory && !visit_playlist && selection.recursive &&
|
|
|
|
(ServerSupportsSearchBase(connection)
|
|
|
|
? !selection.IsEmpty()
|
|
|
|
: selection.HasOtherThanBase()))
|
2013-10-30 10:00:57 +01:00
|
|
|
/* this optimized code path can only be used under
|
|
|
|
certain conditions */
|
|
|
|
return ::SearchSongs(connection, selection, visit_song, error);
|
|
|
|
|
|
|
|
/* fall back to recursive walk (slow!) */
|
2014-01-22 22:40:42 +01:00
|
|
|
return ::Visit(connection, selection.uri.c_str(),
|
2013-10-29 20:58:35 +01:00
|
|
|
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,
|
2015-08-24 11:53:30 +02:00
|
|
|
gcc_unused tag_mask_t group_mask,
|
2014-04-24 10:20:24 +02:00
|
|
|
VisitTag visit_tag,
|
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))
|
2014-12-26 13:50:54 +01:00
|
|
|
return false;
|
2013-10-30 19:50:22 +01:00
|
|
|
|
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
|
|
|
|
2014-04-24 10:20:24 +02:00
|
|
|
// TODO: use group_mask
|
|
|
|
|
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) {
|
2016-03-18 23:10:42 +01:00
|
|
|
AtScopeExit(this, pair) {
|
|
|
|
mpd_return_pair(connection, pair);
|
|
|
|
};
|
|
|
|
|
2014-04-24 10:20:24 +02:00
|
|
|
TagBuilder tag;
|
|
|
|
tag.AddItem(tag_type, pair->value);
|
2014-09-18 17:22:31 +02:00
|
|
|
|
|
|
|
if (tag.IsEmpty())
|
|
|
|
/* if no tag item has been added, then the
|
|
|
|
given value was not acceptable
|
|
|
|
(e.g. empty); forcefully insert an empty
|
|
|
|
tag in this case, as the caller expects the
|
|
|
|
given tag type to be present */
|
|
|
|
tag.AddEmptyItem(tag_type);
|
|
|
|
|
2014-04-24 10:20:24 +02:00
|
|
|
result = visit_tag(tag.Commit(), error);
|
2012-08-15 21:32:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
2014-12-26 13:50:54 +01:00
|
|
|
return false;
|
2013-10-30 19:50:22 +01:00
|
|
|
|
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
|
|
|
|
2013-11-22 00:45:27 +01:00
|
|
|
update_stamp = (time_t)mpd_stats_get_db_update_time(stats2);
|
|
|
|
|
2012-08-15 22:20:28 +02:00
|
|
|
stats.song_count = mpd_stats_get_number_of_songs(stats2);
|
2014-08-29 23:22:46 +02:00
|
|
|
stats.total_duration = std::chrono::seconds(mpd_stats_get_db_play_time(stats2));
|
2012-08-15 22:20:28 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-07-29 23:16:46 +02:00
|
|
|
unsigned
|
|
|
|
ProxyDatabase::Update(const char *uri_utf8, bool discard,
|
|
|
|
Error &error)
|
|
|
|
{
|
|
|
|
if (!EnsureConnected(error))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
unsigned id = discard
|
|
|
|
? mpd_run_rescan(connection, uri_utf8)
|
|
|
|
: mpd_run_update(connection, uri_utf8);
|
|
|
|
if (id == 0)
|
|
|
|
CheckError(connection, error);
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2011-09-05 22:53:46 +02:00
|
|
|
const DatabasePlugin proxy_db_plugin = {
|
|
|
|
"proxy",
|
2014-02-17 20:57:56 +01:00
|
|
|
DatabasePlugin::FLAG_REQUIRE_STORAGE,
|
2011-09-05 22:53:46 +02:00
|
|
|
ProxyDatabase::Create,
|
|
|
|
};
|