1 /*
2  *    Copyright 2014, 2017, 2019 Kai Pastor
3  *
4  *    This file is part of OpenOrienteering.
5  *
6  *    OpenOrienteering is free software: you can redistribute it and/or modify
7  *    it under the terms of the GNU General Public License as published by
8  *    the Free Software Foundation, either version 3 of the License, or
9  *    (at your option) any later version.
10  *
11  *    OpenOrienteering is distributed in the hope that it will be useful,
12  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *    GNU General Public License for more details.
15  *
16  *    You should have received a copy of the GNU General Public License
17  *    along with OpenOrienteering.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef OPENORIENTEERING_AUTOSAVE_H
21 #define OPENORIENTEERING_AUTOSAVE_H
22 
23 #include <QObject>
24 #include <QTimer>
25 
26 class QString;
27 
28 namespace OpenOrienteering {
29 
30 class Autosave;
31 
32 
33 /**
34  * @brief AutosavePrivate is a helper class of Autosave.
35  *
36  * AutosavePrivate implements most of Autosave's behaviour.
37  * Autosave is meant to be used through inheritance.
38  * Due to the implementation of QObject and moc, only the first inherited class
39  * may be derived from QObject. That is why Autosave itself is not derived from
40  * QObject, but rather uses this helper class.
41  */
42 class AutosavePrivate : public QObject
43 {
44 	Q_OBJECT
45 
46 public:
47 	explicit AutosavePrivate(Autosave& autosave);
48 
49 	~AutosavePrivate() override;
50 
51 	bool autosaveNeeded() const;
52 
53 	void setAutosaveNeeded(bool needed);
54 
55 public slots:
56 	void autosave();
57 
58 	void settingsChanged();
59 
60 private:
61 	Q_DISABLE_COPY(AutosavePrivate)
62 
63 	Autosave& document;
64 	QTimer autosave_timer;
65 	int  autosave_interval = 0;
66 	bool autosave_needed = false;
67 };
68 
69 
70 
71 /**
72  * @brief Class Autosave implements autosaving behaviour.
73  *
74  * Autosaving means that data which has been modified is automatically saved
75  * after some period of time if no regular saving (typically triggered by the
76  * user) happens before.
77  *
78  * Classes which wish to implement autosaving may inherit from this class.
79  * Inheriting classes must implement autosave(). Furthermore, they must call
80  * setAutosaveNeeded() when changes need to be taken care of by Autosave, or
81  * when normal saving has terminated the need to perform autosaving.
82  *
83  * Autosaving, as implemented by autosave(), may succeed, fail temporarily
84  * (e.g. during editing), or fail permanently (e.g. for lack of disk space).
85  * On success or permanent failure, autosave() will be called again after the
86  * regular autosaving period.
87  * On temporary failure, autosave() will be called again after five seconds.
88  *
89  * The autosave period (in minutes) is taken from the setting
90  * Settings::General_AutosaveInterval.
91  */
92 class Autosave
93 {
94 public:
95 	/** @brief Possible results of autosave attempts. */
96 	enum AutosaveResult
97 	{
98 		Success,          ///< Autosaving succeeded.
99 		PermanentFailure, ///< Autosaving failed for some persistent reason.
100 		TemporaryFailure  ///< Autosaving failed for some transient reason and shall be retried soon.
101 	};
102 
103 	/** @brief Returns the autosave file path for the given path. */
104 	virtual QString autosavePath(const QString &path) const;
105 
106 	/** @brief Performs an autosave, if possible. */
107 	virtual AutosaveResult autosave() = 0;
108 
109 	/** @brief Informs Autosave whether autosaving is needed or not. */
110 	void setAutosaveNeeded(bool);
111 
112 	/** @brief Returns true if autosave is known to be needed. */
113 	bool autosaveNeeded() const;
114 
115 protected:
116 	/** @brief Initializes the autosave feature. */
117 	Autosave();
118 
119 	/** @brief Destructs the autosave feature. */
120 	virtual ~Autosave();
121 
122 private:
123 	friend class AutosavePrivate;
124 
125 	AutosavePrivate autosave_controller;
126 };
127 
128 
129 }  // namespace OpenOrienteering
130 
131 #endif
132