1 /*
2  Copyright (C) 2010-2014 Kristian Duske
3 
4  This file is part of TrenchBroom.
5 
6  TrenchBroom 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  TrenchBroom 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 TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef TrenchBroom_Autosaver
21 #define TrenchBroom_Autosaver
22 
23 #include "IO/Path.h"
24 #include "View/ViewTypes.h"
25 
26 #include <ctime>
27 
28 namespace TrenchBroom {
29     class Logger;
30 
31     namespace IO {
32         class WritableDiskFileSystem;
33     }
34 
35     namespace View {
36         class Command;
37 
38         class Autosaver {
39         private:
40             View::MapDocumentWPtr m_document;
41             Logger* m_logger;
42 
43             time_t m_saveInterval;
44             time_t m_idleInterval;
45             size_t m_maxBackups;
46 
47             time_t m_lastSaveTime;
48             time_t m_lastModificationTime;
49             size_t m_lastModificationCount;
50         public:
51             Autosaver(View::MapDocumentWPtr document, time_t saveInterval = 10 * 60, time_t idleInterval = 3, size_t maxBackups = 50);
52             ~Autosaver();
53 
54             void triggerAutosave(Logger* logger);
55         private:
56             void autosave(View::MapDocumentSPtr document);
57             IO::WritableDiskFileSystem createBackupFileSystem(const IO::Path& mapPath) const;
58             IO::Path::List collectBackups(const IO::WritableDiskFileSystem& fs, const IO::Path& mapBasename) const;
59             bool isBackup(const IO::Path& backupPath, const IO::Path& mapBasename) const;
60             void thinBackups(IO::WritableDiskFileSystem& fs, IO::Path::List& backups) const;
61             void cleanBackups(IO::WritableDiskFileSystem& fs, IO::Path::List& backups, const IO::Path& mapBasename) const;
62             String makeBackupName(const IO::Path& mapBasename, const size_t index) const;
63         private:
64             void bindObservers();
65             void unbindObservers();
66             void documentModificationCountDidChangeNotifier();
67         };
68 
69         size_t extractBackupNo(const IO::Path& path);
70     }
71 }
72 
73 #endif /* defined(TrenchBroom_Autosaver) */
74