mpd/src/StateFile.cxx

123 lines
3.2 KiB
C++
Raw Normal View History

/*
2013-01-02 19:52:57 +01:00
* Copyright (C) 2003-2013 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"
2013-01-02 19:52:57 +01:00
#include "StateFile.hxx"
#include "OutputState.hxx"
#include "PlaylistState.hxx"
2013-01-03 10:12:41 +01:00
#include "TextFile.hxx"
#include "Partition.hxx"
2013-01-07 08:59:11 +01:00
#include "Volume.hxx"
#include "event/Loop.hxx"
#include <glib.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
2008-12-29 17:29:23 +01:00
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "state_file"
StateFile::StateFile(Path &&_path, Partition &_partition, EventLoop &_loop)
:TimeoutMonitor(_loop), path(std::move(_path)), partition(_partition),
2013-01-14 11:00:22 +01:00
prev_volume_version(0), prev_output_version(0),
prev_playlist_version(0)
{
ScheduleSeconds(5 * 60);
2013-01-14 11:00:22 +01:00
}
2013-01-14 11:00:22 +01:00
void
StateFile::Write()
{
g_debug("Saving state file %s", path.c_str());
2013-01-14 11:00:22 +01:00
FILE *fp = fopen(path.c_str(), "w");
if (G_UNLIKELY(!fp)) {
2008-12-29 17:29:23 +01:00
g_warning("failed to create %s: %s",
2013-01-14 11:00:22 +01:00
path.c_str(), g_strerror(errno));
return;
}
save_sw_volume_state(fp);
2009-10-06 10:30:10 +02:00
audio_output_state_save(fp);
playlist_state_save(fp, &partition.playlist, &partition.pc);
fclose(fp);
prev_volume_version = sw_volume_state_get_hash();
prev_output_version = audio_output_state_get_version();
prev_playlist_version = playlist_state_get_hash(&partition.playlist,
&partition.pc);
}
2013-01-14 11:00:22 +01:00
void
StateFile::Read()
{
bool success;
2013-01-14 11:00:22 +01:00
g_debug("Loading state file %s", path.c_str());
TextFile file(path);
2013-01-03 10:16:05 +01:00
if (file.HasFailed()) {
g_warning("failed to open %s: %s",
2013-01-14 11:00:22 +01:00
path.c_str(), g_strerror(errno));
return;
}
const char *line;
2013-01-03 10:16:05 +01:00
while ((line = file.ReadLine()) != NULL) {
success = read_sw_volume_state(line) ||
2009-10-06 10:30:10 +02:00
audio_output_state_read(line) ||
playlist_state_restore(line, file, &partition.playlist,
&partition.pc);
if (!success)
g_warning("Unrecognized line in state file: %s", line);
}
prev_volume_version = sw_volume_state_get_hash();
prev_output_version = audio_output_state_get_version();
prev_playlist_version = playlist_state_get_hash(&partition.playlist,
&partition.pc);
}
2013-01-14 11:00:22 +01:00
inline void
StateFile::AutoWrite()
{
if (prev_volume_version == sw_volume_state_get_hash() &&
prev_output_version == audio_output_state_get_version() &&
prev_playlist_version == playlist_state_get_hash(&partition.playlist,
&partition.pc))
/* nothing has changed - don't save the state file,
don't spin up the hard disk */
return;
2013-01-14 11:00:22 +01:00
Write();
}
2013-01-14 11:00:22 +01:00
/**
* This function is called every 5 minutes by the GLib main loop, and
* saves the state file.
*/
bool
StateFile::OnTimeout()
{
AutoWrite();
2013-01-14 11:00:22 +01:00
return true;
}