| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2025 The FreeCAD project association AISBL * |
| 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 |
#ifndef GUI_DIALOG_VERSIONMIGRATOR_H |
| 25 |
#define GUI_DIALOG_VERSIONMIGRATOR_H |
| 26 |
|
| 27 |
#include <FCGlobal.h> |
| 28 |
#include <cstdint> |
| 29 |
#include <memory> |
| 30 |
#include <QDialog> |
| 31 |
#include <QStringList> |
| 32 |
|
| 33 |
|
| 34 |
namespace Gui |
| 35 |
{ |
| 36 |
|
| 37 |
class MainWindow; |
| 38 |
|
| 39 |
namespace Dialog |
| 40 |
{ |
| 41 |
|
| 42 |
class GuiExport DlgVersionMigrator final: public QDialog |
| 43 |
{ |
| 44 |
Q_OBJECT |
| 45 |
|
| 46 |
public: |
| 47 |
explicit DlgVersionMigrator(MainWindow* mw); |
| 48 |
~DlgVersionMigrator() override; |
| 49 |
Q_DISABLE_COPY_MOVE(DlgVersionMigrator) |
| 50 |
|
| 51 |
int exec() override; |
| 52 |
|
| 53 |
protected Q_SLOTS: |
| 54 |
|
| 55 |
void calculateMigrationSize(); // Async -> this starts the process and immediately returns |
| 56 |
void showSizeOfMigration(uintmax_t size); |
| 57 |
void migrate(); |
| 58 |
void share(); |
| 59 |
void freshStart(); |
| 60 |
void help(); |
| 61 |
|
| 62 |
private: |
| 63 |
MainWindow* mainWindow; |
| 64 |
QThread* sizeCalculationWorkerThread; |
| 65 |
std::unique_ptr<class Ui_DlgVersionMigrator> ui; |
| 66 |
|
| 67 |
void restart(const QString& message); |
| 68 |
}; |
| 69 |
|
| 70 |
|
| 71 |
class GuiExport PathMigrationWorker: public QObject |
| 72 |
{ |
| 73 |
Q_OBJECT |
| 74 |
|
| 75 |
public: |
| 76 |
PathMigrationWorker(std::string configDir, std::string userAppDir, int major, int minor); |
| 77 |
void run(); |
| 78 |
|
| 79 |
Q_SIGNALS: |
| 80 |
void finished(); |
| 81 |
void complete(); |
| 82 |
void completedWithWarnings(QStringList skippedFiles); |
| 83 |
void failed(); |
| 84 |
|
| 85 |
protected: |
| 86 |
/** |
| 87 |
* @brief Find any occurrence of the original config and userAppDir paths in the new copy of the |
| 88 |
* config file and replace them with updated versions. |
| 89 |
*/ |
| 90 |
void replaceOccurrencesInPreferences(); |
| 91 |
|
| 92 |
/** |
| 93 |
* @brief Locate the new user config file |
| 94 |
* |
| 95 |
* After it's been moved, this method figures out the path to the new user.cfg file. It does not |
| 96 |
* verify the existence of the file, just determines where it *should* be. |
| 97 |
* |
| 98 |
* @return The path to the new version of user.cfg. |
| 99 |
*/ |
| 100 |
std::filesystem::path locateNewPreferences() const; |
| 101 |
|
| 102 |
/** |
| 103 |
* @brief Given an old path, figure out what the new versioned one would be |
| 104 |
* |
| 105 |
* @param oldPath The old path |
| 106 |
* @return An equivalent new versioned path |
| 107 |
*/ |
| 108 |
std::string generateNewUserAppPathString(const std::string& oldPath) const; |
| 109 |
|
| 110 |
/** |
| 111 |
* @brief Replace all occurrences of oldString with newString, modifying contents in place. |
| 112 |
* |
| 113 |
* @param[inout] contents The string to do the replacement in |
| 114 |
* @param[in] oldString The string to search for |
| 115 |
* @param[in] newString The new string to put in place of oldString |
| 116 |
*/ |
| 117 |
static void replaceInContents( |
| 118 |
std::string& contents, |
| 119 |
const std::string& oldString, |
| 120 |
const std::string& newString |
| 121 |
); |
| 122 |
|
| 123 |
void writeMigrationLog(const std::vector<std::filesystem::path>& skippedPaths); |
| 124 |
|
| 125 |
private: |
| 126 |
std::string _configDir; |
| 127 |
std::string _userAppDir; |
| 128 |
int _major; |
| 129 |
int _minor; |
| 130 |
}; |
| 131 |
|
| 132 |
} // namespace Dialog |
| 133 |
} // namespace Gui |
| 134 |
|
| 135 |
#endif // GUI_DIALOG_VERSIONMIGRATOR_H |