StateFile.hxx 2.14 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20 21
#ifndef MPD_STATE_FILE_HXX
#define MPD_STATE_FILE_HXX
22

23
#include "StateFileConfig.hxx"
24
#include "event/TimerEvent.hxx"
25
#include "fs/AllocatedPath.hxx"
26
#include "util/Compiler.h"
27
#include "config.h"
28 29

#include <string>
30
#include <chrono>
31

32
struct Partition;
33 34
class OutputStream;
class BufferedOutputStream;
35

36
class StateFile final {
37 38
	const StateFileConfig config;

39
	const std::string path_utf8;
40

41
	TimerEvent timer_event;
42

43 44 45 46 47 48
	Partition &partition;

	/**
	 * These version numbers determine whether we need to save the state
	 * file.  If nothing has changed, we won't let the hard drive spin up.
	 */
49 50
	unsigned prev_volume_version = 0, prev_output_version = 0,
		prev_playlist_version = 0;
51

52 53 54 55
#ifdef ENABLE_DATABASE
	unsigned prev_storage_version = 0;
#endif

56
public:
57
	StateFile(StateFileConfig &&_config,
58
		  Partition &partition, EventLoop &loop);
59

60 61
	void Read();
	void Write();
62

63 64 65
	/**
	 * Schedules a write if MPD's state was modified.
	 */
66
	void CheckModified() noexcept;
67

68
private:
69
	void Write(OutputStream &os);
70 71
	void Write(BufferedOutputStream &os);

72 73 74
	/**
	 * Save the current state versions for use with IsModified().
	 */
75
	void RememberVersions() noexcept;
76 77 78 79 80 81

	/**
	 * Check if MPD's state was modified since the last
	 * RememberVersions() call.
	 */
	gcc_pure
82
	bool IsModified() const noexcept;
83

84
	/* callback for #timer_event */
85
	void OnTimeout() noexcept;
86
};
87 88

#endif /* STATE_FILE_H */