Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / comphelper / backupfilehelper.hxx
blob68835b866452f50f837ba2351fa679664c25f094
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #ifndef INCLUDED_COMPHELPER_BACKUPFILEHELPER_HXX
11 #define INCLUDED_COMPHELPER_BACKUPFILEHELPER_HXX
13 #include <sal/config.h>
15 #include <comphelper/comphelperdllapi.h>
16 #include <rtl/ustring.hxx>
17 #include <osl/file.hxx>
18 #include <memory>
19 #include <set>
20 #include <vector>
22 namespace comphelper
24 /** Helper class to backup/restore a single file
26 * This is a general class to manage backups/restore of the file
27 * given by the URL. The container holding the backups is created
28 * aside the original file, e.g for 'test.txt' a container
29 * called '.test.pack' will be used. If it was not yet backed-up
30 * this container file will be created at the 1st backup and deleted
31 * when the last gets removed. The container holds a stack with a
32 * maximum given number (in the constructor) of copies, these are by
33 * default compressed, but don't have to be (see tryPush).
35 * Due to being on a low system level here, no UNO API and not much
36 * other tooling can be used, as a consequence for the container a
37 * own simple format is used and e.g. the zip lib directly.
39 * You need to hand over the URL of the file to look at and
40 * a maximum number of allowed copies. That number is internally
41 * limited to a absolute max of 10 (see implementation). The number
42 * of allowed copies is limited to [1..max].
44 * Calling tryPush() will check if there is no backup yet or if
45 * there is one that the file has changed. If yes, a new copy is
46 * created on a kind of 'stack' of copies. Tre return value can
47 * be used to see if a backup was indeed created.
49 * Calling tryPop() will do the opposite: If a backup is available,
50 * delete the orig file and re-instantiate the backup. The backup
51 * is taken off the 'stack' of copies. The return value can be
52 * used to check if this was done.
54 * isPopPossible can be called to see if there is a backup available
55 * before calling tryPop().
57 * The 'stack' of copies works by using the same path, filename
58 * and extension, but adding a '_1' -> '_(num_of_copy)' to it.
60 class COMPHELPER_DLLPUBLIC BackupFileHelper
62 private:
63 // internal data
64 std::set< OUString > maDirs;
65 std::set< std::pair< OUString, OUString > > maFiles;
67 sal_uInt16 mnNumBackups;
68 sal_uInt16 mnMode;
70 bool mbActive;
71 bool mbExtensions;
72 bool mbCompress;
74 // internal flag if _exit() was called already - a hint to evtl.
75 // not create copies of potentially not well-defined data. This
76 // may be used in destructors of static instances - which unfortunately
77 // get called on WNT but not on linux. Thus I thought about encapsulating
78 // in some '#ifdefs', but it's just more safe to always do it and
79 // allows to add a SAL_WARN when one of these destructors is called
80 // after _exit()
81 static bool mbExitWasCalled;
83 // internal detector if SafeModeName dir exists
84 static bool mbSafeModeDirExists;
86 // internal upper limit (max) of allowed backups
87 static sal_uInt16 mnMaxAllowedBackups;
89 // path to User's configuration directory and derived strings
90 static OUString maInitialBaseURL;
91 static OUString maUserConfigBaseURL;
92 static OUString maUserConfigWorkURL;
93 static OUString maRegModName;
94 static OUString maExt;
96 // get path to User's configuration directory (created on-demand)
97 static const OUString& getInitialBaseURL();
99 // the name of the SafeMode directory for temporary processing
100 static const OUString& getSafeModeName();
102 public:
103 /** Constructor to handle Backups of the given file, will internally
104 * detect configuration values and URL to initial registrymodifications
105 * and thus the User configuration directory
107 BackupFileHelper();
109 // allow to set static global flag when app had to call _exit()
110 static void setExitWasCalled();
111 static bool getExitWasCalled();
113 // This call initializes the state of the UserDirectory as needed, it may
114 // initialize to SafeMode configuration or return from it by moving files
115 // in that directory
116 static void reactOnSafeMode(bool bSafeMode);
118 /** tries to create a new backup, if there is none yet, or if the
119 * last differs from the base file. It will then put a new version
120 * on the 'stack' of copies and evtl. delete the oldest backup.
121 * Also may cleanup older backups when NumBackups given in the
122 * constructor has changed.
124 * @return bool
125 * returns true if a new backup was actually created
127 * tryPushExtensionInfo is the specialized version for ExtensionInfo
129 bool tryPush();
130 bool tryPushExtensionInfo();
132 /** finds out if a restore is possible
134 * @return bool
135 * returns true if a restore to an older backup is possible
137 * isPopPossibleExtensionInfo is the specialized version for ExtensionInfo
139 bool isPopPossible();
140 bool isPopPossibleExtensionInfo();
142 /** tries to execute a restore. Will overwrite the base file
143 * in that case and take one version off the 'stack' of copies.
144 * Also may cleanup older backups when NumBackups given in the
145 * constructor has changed.
147 * @return bool
148 * returns true if a restore was actually created
150 * tryPopExtensionInfo is the specialized version for ExtensionInfo
152 bool tryPop();
153 bool tryPopExtensionInfo();
155 /** tries to iterate the extensions and to disable all of them
157 static bool isTryDisableAllExtensionsPossible();
158 static void tryDisableAllExtensions();
160 /** Deinstall all User Extensions (installed for User only)
162 static bool isTryDeinstallUserExtensionsPossible();
163 static void tryDeinstallUserExtensions();
165 /** Reset shared Extensions
167 static bool isTryResetSharedExtensionsPossible();
168 static void tryResetSharedExtensions();
170 /** Reset bundled Extensions
172 static bool isTryResetBundledExtensionsPossible();
173 static void tryResetBundledExtensions();
175 /// Disables OpenGL and OpenCL
176 static void tryDisableHWAcceleration();
178 /** resets User-Customizations like Settings and UserInterface modifications
180 static bool isTryResetCustomizationsPossible();
181 static void tryResetCustomizations();
183 /** resets the whole UserProfile
185 static void tryResetUserProfile();
187 /** Return the profile url */
188 static const OUString& getUserProfileURL();
190 /** Return the url of the backed up profile (when in safe mode) */
191 static const OUString& getUserProfileWorkURL();
193 private:
194 // internal helper methods
195 static const rtl::OUString getPackURL();
196 static const std::vector< OUString >& getCustomizationDirNames();
197 static const std::vector< OUString >& getCustomizationFileNames();
199 // file push helpers
200 bool tryPush_Files(const std::set< OUString >& rDirs, const std::set< std::pair< OUString, OUString > >& rFiles, const OUString& rSourceURL, const OUString& rTargetURL);
201 bool tryPush_file(const OUString& rSourceURL, const OUString& rTargetURL, const OUString& rName, const OUString& rExt);
203 // file pop possibilities helper
204 bool isPopPossible_files(const std::set< OUString >& rDirs, const std::set< std::pair< OUString, OUString > >& rFiles, const OUString& rSourceURL, const OUString& rTargetURL);
205 static bool isPopPossible_file(const OUString& rSourceURL, const OUString& rTargetURL, const OUString& rName, const OUString& rExt);
207 // file pop helpers
208 bool tryPop_files(const std::set< OUString >& rDirs, const std::set< std::pair< OUString, OUString > >& rFiles, const OUString& rSourceURL, const OUString& rTargetURL);
209 bool tryPop_file(const OUString& rSourceURL, const OUString& rTargetURL, const OUString& rName, const OUString& rExt);
211 // ExtensionInfo helpers
212 bool tryPush_extensionInfo(const OUString& rTargetURL);
213 static bool isPopPossible_extensionInfo(const OUString& rTargetURL);
214 bool tryPop_extensionInfo(const OUString& rTargetURL);
216 // FileDirInfo helpers
217 void fillDirFileInfo();
221 #endif
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */