StateFile: add struct StateFileConfig
This commit is contained in:
parent
e8a7c6cee3
commit
bcc1e51097
|
@ -175,6 +175,7 @@ libmpd_a_SOURCES = \
|
||||||
src/SongPrint.cxx src/SongPrint.hxx \
|
src/SongPrint.cxx src/SongPrint.hxx \
|
||||||
src/SongSave.cxx src/SongSave.hxx \
|
src/SongSave.cxx src/SongSave.hxx \
|
||||||
src/StateFile.cxx src/StateFile.hxx \
|
src/StateFile.cxx src/StateFile.hxx \
|
||||||
|
src/StateFileConfig.cxx src/StateFileConfig.hxx \
|
||||||
src/Stats.cxx src/Stats.hxx \
|
src/Stats.cxx src/Stats.hxx \
|
||||||
src/TagPrint.cxx src/TagPrint.hxx \
|
src/TagPrint.cxx src/TagPrint.hxx \
|
||||||
src/TagSave.cxx src/TagSave.hxx \
|
src/TagSave.cxx src/TagSave.hxx \
|
||||||
|
|
22
src/Main.cxx
22
src/Main.cxx
|
@ -93,7 +93,6 @@
|
||||||
#include "java/File.hxx"
|
#include "java/File.hxx"
|
||||||
#include "android/Environment.hxx"
|
#include "android/Environment.hxx"
|
||||||
#include "android/Context.hxx"
|
#include "android/Context.hxx"
|
||||||
#include "fs/StandardDirectory.hxx"
|
|
||||||
#include "fs/FileSystem.hxx"
|
#include "fs/FileSystem.hxx"
|
||||||
#include "org_musicpd_Bridge.h"
|
#include "org_musicpd_Bridge.h"
|
||||||
#endif
|
#endif
|
||||||
|
@ -256,26 +255,13 @@ glue_sticker_init(const ConfigData &config)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
glue_state_file_init(const ConfigData &config)
|
glue_state_file_init(const ConfigData &raw_config)
|
||||||
{
|
{
|
||||||
auto path_fs = config.GetPath(ConfigOption::STATE_FILE);
|
StateFileConfig config(raw_config);
|
||||||
if (path_fs.IsNull()) {
|
if (!config.IsEnabled())
|
||||||
#ifdef ANDROID
|
|
||||||
const auto cache_dir = GetUserCacheDir();
|
|
||||||
if (cache_dir.IsNull())
|
|
||||||
return;
|
|
||||||
|
|
||||||
path_fs = cache_dir / Path::FromFS("state");
|
|
||||||
#else
|
|
||||||
return;
|
return;
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto interval =
|
instance->state_file = new StateFile(std::move(config),
|
||||||
config.GetUnsigned(ConfigOption::STATE_FILE_INTERVAL,
|
|
||||||
StateFile::DEFAULT_INTERVAL);
|
|
||||||
|
|
||||||
instance->state_file = new StateFile(std::move(path_fs), interval,
|
|
||||||
instance->partitions.front(),
|
instance->partitions.front(),
|
||||||
instance->event_loop);
|
instance->event_loop);
|
||||||
instance->state_file->Read();
|
instance->state_file->Read();
|
||||||
|
|
|
@ -38,13 +38,9 @@
|
||||||
|
|
||||||
static constexpr Domain state_file_domain("state_file");
|
static constexpr Domain state_file_domain("state_file");
|
||||||
|
|
||||||
constexpr std::chrono::steady_clock::duration StateFile::DEFAULT_INTERVAL;
|
StateFile::StateFile(StateFileConfig &&_config,
|
||||||
|
|
||||||
StateFile::StateFile(AllocatedPath &&_path,
|
|
||||||
std::chrono::steady_clock::duration _interval,
|
|
||||||
Partition &_partition, EventLoop &_loop)
|
Partition &_partition, EventLoop &_loop)
|
||||||
:path(std::move(_path)), path_utf8(path.ToUTF8()),
|
:config(std::move(_config)), path_utf8(config.path.ToUTF8()),
|
||||||
interval(_interval),
|
|
||||||
timer_event(_loop, BIND_THIS_METHOD(OnTimeout)),
|
timer_event(_loop, BIND_THIS_METHOD(OnTimeout)),
|
||||||
partition(_partition)
|
partition(_partition)
|
||||||
{
|
{
|
||||||
|
@ -103,7 +99,7 @@ StateFile::Write()
|
||||||
"Saving state file %s", path_utf8.c_str());
|
"Saving state file %s", path_utf8.c_str());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
FileOutputStream fos(path);
|
FileOutputStream fos(config.path);
|
||||||
Write(fos);
|
Write(fos);
|
||||||
fos.Commit();
|
fos.Commit();
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
|
@ -120,7 +116,7 @@ try {
|
||||||
|
|
||||||
FormatDebug(state_file_domain, "Loading state file %s", path_utf8.c_str());
|
FormatDebug(state_file_domain, "Loading state file %s", path_utf8.c_str());
|
||||||
|
|
||||||
TextFile file(path);
|
TextFile file(config.path);
|
||||||
|
|
||||||
#ifdef ENABLE_DATABASE
|
#ifdef ENABLE_DATABASE
|
||||||
const SongLoader song_loader(partition.instance.database,
|
const SongLoader song_loader(partition.instance.database,
|
||||||
|
@ -155,7 +151,7 @@ void
|
||||||
StateFile::CheckModified()
|
StateFile::CheckModified()
|
||||||
{
|
{
|
||||||
if (!timer_event.IsActive() && IsModified())
|
if (!timer_event.IsActive() && IsModified())
|
||||||
timer_event.Schedule(interval);
|
timer_event.Schedule(config.interval);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
#ifndef MPD_STATE_FILE_HXX
|
#ifndef MPD_STATE_FILE_HXX
|
||||||
#define MPD_STATE_FILE_HXX
|
#define MPD_STATE_FILE_HXX
|
||||||
|
|
||||||
|
#include "StateFileConfig.hxx"
|
||||||
#include "event/TimerEvent.hxx"
|
#include "event/TimerEvent.hxx"
|
||||||
#include "fs/AllocatedPath.hxx"
|
#include "fs/AllocatedPath.hxx"
|
||||||
#include "Compiler.h"
|
#include "Compiler.h"
|
||||||
|
@ -32,10 +33,10 @@ class OutputStream;
|
||||||
class BufferedOutputStream;
|
class BufferedOutputStream;
|
||||||
|
|
||||||
class StateFile final {
|
class StateFile final {
|
||||||
const AllocatedPath path;
|
const StateFileConfig config;
|
||||||
|
|
||||||
const std::string path_utf8;
|
const std::string path_utf8;
|
||||||
|
|
||||||
const std::chrono::steady_clock::duration interval;
|
|
||||||
TimerEvent timer_event;
|
TimerEvent timer_event;
|
||||||
|
|
||||||
Partition &partition;
|
Partition &partition;
|
||||||
|
@ -52,9 +53,7 @@ class StateFile final {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static constexpr std::chrono::steady_clock::duration DEFAULT_INTERVAL = std::chrono::minutes(2);
|
StateFile(StateFileConfig &&_config,
|
||||||
|
|
||||||
StateFile(AllocatedPath &&path, std::chrono::steady_clock::duration interval,
|
|
||||||
Partition &partition, EventLoop &loop);
|
Partition &partition, EventLoop &loop);
|
||||||
|
|
||||||
void Read();
|
void Read();
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2003-2018 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 "StateFileConfig.hxx"
|
||||||
|
#include "config/Data.hxx"
|
||||||
|
|
||||||
|
#ifdef ANDROID
|
||||||
|
#include "fs/StandardDirectory.hxx"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
constexpr std::chrono::steady_clock::duration StateFileConfig::DEFAULT_INTERVAL;
|
||||||
|
|
||||||
|
StateFileConfig::StateFileConfig(const ConfigData &config)
|
||||||
|
:path(config.GetPath(ConfigOption::STATE_FILE)),
|
||||||
|
interval(config.GetUnsigned(ConfigOption::STATE_FILE_INTERVAL,
|
||||||
|
DEFAULT_INTERVAL))
|
||||||
|
{
|
||||||
|
#ifdef ANDROID
|
||||||
|
if (path.IsNull()) {
|
||||||
|
const auto cache_dir = GetUserCacheDir();
|
||||||
|
if (cache_dir.IsNull())
|
||||||
|
return;
|
||||||
|
|
||||||
|
path = cache_dir / Path::FromFS("state");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2003-2018 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_STATE_FILE_CONFIG_HXX
|
||||||
|
#define MPD_STATE_FILE_CONFIG_HXX
|
||||||
|
|
||||||
|
#include "check.h"
|
||||||
|
#include "fs/AllocatedPath.hxx"
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
struct ConfigData;
|
||||||
|
|
||||||
|
struct StateFileConfig {
|
||||||
|
static constexpr std::chrono::steady_clock::duration DEFAULT_INTERVAL = std::chrono::minutes(2);
|
||||||
|
|
||||||
|
AllocatedPath path;
|
||||||
|
|
||||||
|
std::chrono::steady_clock::duration interval;
|
||||||
|
|
||||||
|
explicit StateFileConfig(const ConfigData &config);
|
||||||
|
|
||||||
|
bool IsEnabled() const noexcept {
|
||||||
|
return !path.IsNull();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue