| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2021 Chris Hennes <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library 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 Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#ifndef BASE_PREFERENCEPACKMANAGER_H |
| 24 |
#define BASE_PREFERENCEPACKMANAGER_H |
| 25 |
|
| 26 |
#include <vector> |
| 27 |
#include <string> |
| 28 |
#include <mutex> |
| 29 |
|
| 30 |
#include <QMetaType> |
| 31 |
|
| 32 |
#include <FCGlobal.h> |
| 33 |
|
| 34 |
#include "App/Metadata.h" |
| 35 |
|
| 36 |
namespace Gui |
| 37 |
{ |
| 38 |
|
| 39 |
/** |
| 40 |
* \class PreferencePack A collection of user preferences stored in files on disk |
| 41 |
*/ |
| 42 |
class GuiExport PreferencePack |
| 43 |
{ |
| 44 |
|
| 45 |
public: |
| 46 |
/** |
| 47 |
* Construct a preferencePack from a directory |
| 48 |
* |
| 49 |
* \param path A path to a mod directory that contains a preferencePack |
| 50 |
* \param metadata The metadata from the package.xml file describing this preferencePack |
| 51 |
*/ |
| 52 |
PreferencePack(const std::filesystem::path& path, const App::Metadata& metadata); |
| 53 |
|
| 54 |
~PreferencePack() = default; |
| 55 |
|
| 56 |
/** |
| 57 |
* Get the name of the PreferencePack |
| 58 |
*/ |
| 59 |
std::string name() const; |
| 60 |
|
| 61 |
/** |
| 62 |
* Apply the PreferencePack over the top of the current preferences set |
| 63 |
* \returns True if the preferencePack was applied, or false if not |
| 64 |
*/ |
| 65 |
bool apply() const; |
| 66 |
|
| 67 |
/** |
| 68 |
* Get the complete metadata object for this preference pack |
| 69 |
*/ |
| 70 |
App::Metadata metadata() const; |
| 71 |
|
| 72 |
private: |
| 73 |
void applyConfigChanges() const; |
| 74 |
|
| 75 |
std::filesystem::path _path; |
| 76 |
App::Metadata _metadata; |
| 77 |
}; |
| 78 |
|
| 79 |
|
| 80 |
/** |
| 81 |
* \class PreferencePackManager handles storable and loadable collections of user preferences |
| 82 |
* |
| 83 |
* This class provides some additional utility functions for allowing users to save their current |
| 84 |
* preferences as a PreferencePack based on a set of template files provided either in the main |
| 85 |
* FreeCAD distribution, or inside various installed mods. |
| 86 |
*/ |
| 87 |
class GuiExport PreferencePackManager |
| 88 |
{ |
| 89 |
public: |
| 90 |
PreferencePackManager(); |
| 91 |
~PreferencePackManager() = default; |
| 92 |
|
| 93 |
/** |
| 94 |
* Rescan the preferencePack directory and update the available PreferencePacks |
| 95 |
*/ |
| 96 |
void rescan(); |
| 97 |
|
| 98 |
/** |
| 99 |
* Get an alphabetical list of names of all visible PreferencePacks |
| 100 |
*/ |
| 101 |
std::vector<std::string> preferencePackNames() const; |
| 102 |
|
| 103 |
/** |
| 104 |
* Get a map of all visible PreferencePack names and their associated packs |
| 105 |
*/ |
| 106 |
std::map<std::string, PreferencePack> preferencePacks() const; |
| 107 |
|
| 108 |
/** |
| 109 |
* Apply the named preferencePack |
| 110 |
* \return True if the preferencePack was applied, or false if it was not |
| 111 |
*/ |
| 112 |
bool apply(const std::string& preferencePackName) const; |
| 113 |
|
| 114 |
/** |
| 115 |
* Check the visibility of the specified pack |
| 116 |
* \return True if the preferencePack is visible, or false if not. All packs are visible by |
| 117 |
* default, but can be marked as "invisible" (i.e. not returned by the manager in lists of |
| 118 |
* packs) by using the toggleVisibility function. |
| 119 |
*/ |
| 120 |
bool isVisible(const std::string& addonName, const std::string& preferencePackName) const; |
| 121 |
|
| 122 |
/** |
| 123 |
* Toggle the visibility of the named preference pack in a named addon |
| 124 |
*/ |
| 125 |
void toggleVisibility(const std::string& addonName, const std::string& preferencePackName); |
| 126 |
|
| 127 |
/** |
| 128 |
* Deletes the user-saved pack specified by name |
| 129 |
*/ |
| 130 |
void deleteUserPack(const std::string& name); |
| 131 |
|
| 132 |
|
| 133 |
/** |
| 134 |
* \struct TemplateFile A file containing a set of preferences that can be saved into |
| 135 |
* a PreferencePack |
| 136 |
* |
| 137 |
* PreferencePacks can contain any parameters at all, but inside FreeCAD there is no |
| 138 |
* centralized list of all of these parameters, and at any given time the user.cfg file |
| 139 |
* usually does not store a value for all possible parameters. Instead, it simply allows |
| 140 |
* calling code to use whatever default values that code sets. This is all completely |
| 141 |
* hidden from the users: FreeCAD behaves as though those values exist in the config file. |
| 142 |
* |
| 143 |
* When a user saves their current configuration as a pack, they likely expect that saved |
| 144 |
* configuration to include those default values, so that if they later apply their saved |
| 145 |
* configuration those defaults are restored. To enable this a set of template files |
| 146 |
* listing default values for various types of parameters can be used. Each file is |
| 147 |
* presented to the user as a checkable box when saving a new preferences pack, and the |
| 148 |
* intention is that these files will list out the various user-facing parameters that |
| 149 |
* someone might want to save into a preferences pack. |
| 150 |
* |
| 151 |
* These files are themselves valid user.cfg files, that if loaded and applied will result |
| 152 |
* in the default values of their contained variables being set. For this to work, these |
| 153 |
* files should be kept up-to-date with the default values set in the code. They are not |
| 154 |
* required to contain an exhaustive listing of all possible parameters: in most cases it |
| 155 |
* is enough that they list the variables that a user would expect for a given name. For |
| 156 |
* example, "Sketcher Colors.cfg" should list out all of the default colors used in |
| 157 |
* Sketcher that a user can set in Preferences, but it is not necessary that it contain any |
| 158 |
* color that is only used internally, and it should not include things like fonts, or |
| 159 |
* behavior information. |
| 160 |
* |
| 161 |
* The base FreeCAD installation includes default templates in: |
| 162 |
* $INSTALL_DIR/data/Gui/PreferencePackTemplates/ |
| 163 |
* |
| 164 |
* External add-ons are also searched for any directory called PreferencePackTemplates or |
| 165 |
* preference_pack_templates -- either of which is expected to contain appearance and/or |
| 166 |
* behavior subdirectories. In this way external add-ons can allow a user to easily save |
| 167 |
* their preferences to a PreferencePack, or even to add additional templates representing |
| 168 |
* sets of core FreeCAD preferences. |
| 169 |
*/ |
| 170 |
struct TemplateFile |
| 171 |
{ |
| 172 |
std::string group; // Generally the Add-On/Mod/Package name |
| 173 |
std::string name; |
| 174 |
std::filesystem::path path; |
| 175 |
}; |
| 176 |
|
| 177 |
/** |
| 178 |
* Save current settings as a (possibly new) preferencePack |
| 179 |
* |
| 180 |
* If the named preferencePack does not exist, this creates it on disk. If it does exist, this |
| 181 |
* overwrites the original. |
| 182 |
*/ |
| 183 |
void save( |
| 184 |
const std::string& name, |
| 185 |
const std::string& directory, |
| 186 |
const std::vector<TemplateFile>& templates |
| 187 |
); |
| 188 |
|
| 189 |
|
| 190 |
std::vector<TemplateFile> templateFiles(bool rescan = false); |
| 191 |
|
| 192 |
/** |
| 193 |
* Get a list of all available config file backups. Backups are currently stored for one week. |
| 194 |
*/ |
| 195 |
std::vector<std::filesystem::path> configBackups() const; |
| 196 |
|
| 197 |
/** |
| 198 |
* Import an existing config file as a preference pack with a given name. |
| 199 |
*/ |
| 200 |
void importConfig(const std::string& packName, const std::filesystem::path& path); |
| 201 |
|
| 202 |
/** |
| 203 |
* Get a list of all mod directories. |
| 204 |
*/ |
| 205 |
std::vector<std::filesystem::path> modPaths() const; |
| 206 |
|
| 207 |
/** |
| 208 |
* Get the path to the saved preference packs. |
| 209 |
*/ |
| 210 |
std::filesystem::path getSavedPreferencePacksPath() const; |
| 211 |
|
| 212 |
/** |
| 213 |
* Get the path to the preference packs of the resource directory. |
| 214 |
*/ |
| 215 |
std::filesystem::path getResourcePreferencePacksPath() const; |
| 216 |
|
| 217 |
/** |
| 218 |
* Collect all preference packs of a directory. |
| 219 |
*/ |
| 220 |
std::vector<std::string> getPacksFromDirectory(const std::filesystem::path& path) const; |
| 221 |
|
| 222 |
private: |
| 223 |
void FindPreferencePacksInPackage(const std::filesystem::path& mod); |
| 224 |
void TryFindPreferencePacksInPackage(const std::filesystem::path& mod); |
| 225 |
|
| 226 |
void BackupCurrentConfig() const; |
| 227 |
|
| 228 |
void DeleteOldBackups() const; |
| 229 |
|
| 230 |
void AddPackToMetadata(const std::string& packName) const; |
| 231 |
|
| 232 |
std::vector<std::filesystem::path> _preferencePackPaths; |
| 233 |
std::vector<TemplateFile> _templateFiles; |
| 234 |
std::map<std::string, PreferencePack> _preferencePacks; |
| 235 |
mutable std::mutex _mutex; |
| 236 |
}; |
| 237 |
|
| 238 |
} // namespace Gui |
| 239 |
|
| 240 |
Q_DECLARE_METATYPE(Gui::PreferencePackManager::TemplateFile) // So it can be used with QVariant |
| 241 |
|
| 242 |
|
| 243 |
#endif |