Debian Codegraph @

src/App/BackupPolicy.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /****************************************************************************
4 * Copyright (c) 2020 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of FreeCAD. *
7 * *
8 * FreeCAD is free software: you can redistribute it and/or modify it *
9 * under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation, either version 2.1 of the *
11 * License, or (at your option) any later version. *
12 * *
13 * FreeCAD is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with FreeCAD. If not, see *
20 * <https://www.gnu.org/licenses/>. *
21 * *
22 ***************************************************************************/
23
24 #include "FCGlobal.h"
25
26 #include <boost/regex.hpp>
27 #include <string>
28 #include <Base/FileInfo.h>
29
30 namespace App
31 {
32 // Helper class to handle different backup policies: originally a private class in Document.cpp,
33 // and extracted for public access to enable direct testing since the logic involved is quite
34 // complex.
35 class AppExport BackupPolicy
36 {
37 public:
38 enum Policy
39 {
40 Standard,
41 TimeStamp
42 };
43 void setPolicy(const Policy p);
44 void setNumberOfFiles(const int count);
45 void useBackupExtension(const bool on);
46 void setDateFormat(const std::string& fmt);
47 void apply(const std::string& sourcename, const std::string& targetname);
48
49 private:
50 void applyStandard(const std::string& sourcename, const std::string& targetname) const;
51 void applyTimeStamp(const std::string& sourcename, const std::string& targetname);
52 static bool fileComparisonByDate(const Base::FileInfo& i, const Base::FileInfo& j);
53 bool startsWith(const std::string& st1, const std::string& st2) const;
54 bool checkValidString(const std::string& cmpl, const boost::regex& e) const;
55 bool checkValidComplement(const std::string& file,
56 const std::string& pbn,
57 const std::string& ext) const;
58 bool checkDigits(const std::string& cmpl) const;
59 bool renameFileNoErase(Base::FileInfo fi, const std::string& newName);
60
61 private:
62 Policy policy {Standard};
63 int numberOfFiles {1};
64 bool useFCBakExtension {true};
65 std::string saveBackupDateFormat {"%Y%m%d-%H%M%S"};
66 };
67 } // namespace App