2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-01-19 16:59:18 +01:00
|
|
|
**
|
2017-11-15 16:19:16 +01:00
|
|
|
** Copyright (C) 2017 The Qt Company Ltd.
|
2016-01-15 14:57:40 +01:00
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-01-19 16:59:18 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-01-19 16:59:18 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2010-01-19 16:59:18 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-01-19 16:59:18 +01:00
|
|
|
|
2016-02-02 18:26:51 +01:00
|
|
|
#pragma once
|
2010-01-19 16:59:18 +01:00
|
|
|
|
2017-11-15 16:19:16 +01:00
|
|
|
#include "utils_global.h"
|
|
|
|
|
|
|
|
|
|
#include "fileutils.h"
|
2017-11-17 21:53:54 +01:00
|
|
|
#include "optional.h"
|
2017-11-20 15:31:21 +01:00
|
|
|
#include "persistentsettings.h"
|
2013-03-25 17:13:18 +01:00
|
|
|
|
2014-05-12 15:19:13 +02:00
|
|
|
#include <QHash>
|
2014-03-03 18:57:42 +01:00
|
|
|
#include <QMessageBox>
|
2017-11-20 15:31:21 +01:00
|
|
|
#include <QVariantMap>
|
2013-03-25 17:13:18 +01:00
|
|
|
|
2017-12-11 11:30:01 +01:00
|
|
|
#include <memory>
|
|
|
|
|
|
2017-11-15 16:19:16 +01:00
|
|
|
namespace Utils {
|
2010-01-19 16:59:18 +01:00
|
|
|
|
2017-12-12 12:23:03 +01:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// Helper:
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
QTCREATOR_UTILS_EXPORT int versionFromMap(const QVariantMap &data);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT int originalVersionFromMap(const QVariantMap &data);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT QByteArray settingsIdFromMap(const QVariantMap &data);
|
|
|
|
|
|
|
|
|
|
QTCREATOR_UTILS_EXPORT void setVersionInMap(QVariantMap &data, int version);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT void setOriginalVersionInMap(QVariantMap &data, int version);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT void setSettingsIdInMap(QVariantMap &data, const QByteArray &id);
|
|
|
|
|
|
2017-12-12 17:38:23 +01:00
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
// Helpers:
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
QTCREATOR_UTILS_EXPORT int versionFromMap(const QVariantMap &data);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT int originalVersionFromMap(const QVariantMap &data);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT QByteArray settingsIdFromMap(const QVariantMap &data);
|
|
|
|
|
|
|
|
|
|
QTCREATOR_UTILS_EXPORT void setVersionInMap(QVariantMap &data, int version);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT void setOriginalVersionInMap(QVariantMap &data, int version);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT void setSettingsIdInMap(QVariantMap &data, const QByteArray &id);
|
|
|
|
|
|
|
|
|
|
using SettingsMergeResult = Utils::optional<QPair<QString, QVariant>>;
|
|
|
|
|
|
2017-11-20 15:31:21 +01:00
|
|
|
// --------------------------------------------------------------------
|
2017-12-14 15:23:17 +01:00
|
|
|
// SettingsAccessor:
|
2017-11-20 15:31:21 +01:00
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
2017-12-11 11:30:01 +01:00
|
|
|
// Read/write files incl. error handling suitable for the UI:
|
2017-12-14 15:23:17 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT SettingsAccessor
|
2017-11-20 15:31:21 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2017-12-14 15:23:17 +01:00
|
|
|
SettingsAccessor(const QString &docType, const QString &displayName,
|
|
|
|
|
const QString &applicationDisplayName);
|
|
|
|
|
virtual ~SettingsAccessor() = default;
|
2017-12-11 11:30:01 +01:00
|
|
|
|
|
|
|
|
enum ProceedInfo { Continue, DiscardAndContinue };
|
|
|
|
|
typedef QHash<QMessageBox::StandardButton, ProceedInfo> ButtonMap;
|
|
|
|
|
class Issue {
|
|
|
|
|
public:
|
2017-12-12 14:58:23 +01:00
|
|
|
enum class Type { ERROR, WARNING };
|
|
|
|
|
Issue(const QString &title, const QString &message, const Type type) :
|
|
|
|
|
title{title}, message{message}, type{type}
|
|
|
|
|
{ }
|
2017-12-11 11:30:01 +01:00
|
|
|
|
|
|
|
|
QMessageBox::StandardButtons allButtons() const;
|
|
|
|
|
|
|
|
|
|
QString title;
|
|
|
|
|
QString message;
|
2017-12-12 14:58:23 +01:00
|
|
|
Type type;
|
2017-12-11 11:30:01 +01:00
|
|
|
QMessageBox::StandardButton defaultButton = QMessageBox::NoButton;
|
|
|
|
|
QMessageBox::StandardButton escapeButton = QMessageBox::Ok;
|
|
|
|
|
QHash<QMessageBox::StandardButton, ProceedInfo> buttons = {{QMessageBox::Ok, ProceedInfo::Continue}};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class RestoreData {
|
|
|
|
|
public:
|
|
|
|
|
RestoreData() = default;
|
|
|
|
|
RestoreData(const Utils::FileName &path, const QVariantMap &data) : path{path}, data{data} { }
|
2017-12-12 14:58:23 +01:00
|
|
|
RestoreData(const QString &title, const QString &message, const Issue::Type type) :
|
|
|
|
|
RestoreData(Issue(title, message, type))
|
|
|
|
|
{ }
|
2017-12-11 11:30:01 +01:00
|
|
|
RestoreData(const Issue &issue) : issue{issue} { }
|
2017-12-12 14:58:23 +01:00
|
|
|
|
|
|
|
|
bool hasIssue() const { return bool(issue); }
|
|
|
|
|
bool hasError() const { return hasIssue() && issue.value().type == Issue::Type::ERROR; }
|
|
|
|
|
bool hasWarning() const { return hasIssue() && issue.value().type == Issue::Type::WARNING; }
|
|
|
|
|
|
2017-12-11 11:30:01 +01:00
|
|
|
Utils::FileName path;
|
|
|
|
|
QVariantMap data;
|
|
|
|
|
Utils::optional<Issue> issue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
QVariantMap restoreSettings(QWidget *parent) const;
|
|
|
|
|
bool saveSettings(const QVariantMap &data, QWidget *parent) const;
|
|
|
|
|
|
|
|
|
|
const QString docType;
|
|
|
|
|
const QString displayName;
|
|
|
|
|
const QString applicationDisplayName;
|
|
|
|
|
|
|
|
|
|
void setBaseFilePath(const Utils::FileName &baseFilePath) { m_baseFilePath = baseFilePath; }
|
2018-05-06 10:55:49 +03:00
|
|
|
void setReadOnly() { m_readOnly = true; }
|
2017-12-11 11:30:01 +01:00
|
|
|
Utils::FileName baseFilePath() const { return m_baseFilePath; }
|
2017-11-20 15:31:21 +01:00
|
|
|
|
2017-12-11 11:30:01 +01:00
|
|
|
virtual RestoreData readData(const Utils::FileName &path, QWidget *parent) const;
|
2017-12-12 14:58:23 +01:00
|
|
|
virtual Utils::optional<Issue> writeData(const Utils::FileName &path, const QVariantMap &data, QWidget *parent) const;
|
2017-11-20 15:31:21 +01:00
|
|
|
|
|
|
|
|
protected:
|
2017-12-11 11:30:01 +01:00
|
|
|
// Report errors:
|
2018-03-07 17:34:09 +01:00
|
|
|
QVariantMap restoreSettings(const Utils::FileName &settingsPath, QWidget *parent) const;
|
2017-12-11 11:30:01 +01:00
|
|
|
ProceedInfo reportIssues(const Issue &issue, const FileName &path, QWidget *parent) const;
|
2017-11-20 15:31:21 +01:00
|
|
|
|
2017-12-11 11:30:01 +01:00
|
|
|
virtual QVariantMap preprocessReadSettings(const QVariantMap &data) const;
|
|
|
|
|
virtual QVariantMap prepareToWriteSettings(const QVariantMap &data) const;
|
|
|
|
|
|
2017-12-14 11:13:15 +01:00
|
|
|
virtual RestoreData readFile(const Utils::FileName &path) const;
|
|
|
|
|
virtual Utils::optional<Issue> writeFile(const Utils::FileName &path, const QVariantMap &data) const;
|
2017-11-20 15:31:21 +01:00
|
|
|
|
|
|
|
|
private:
|
2017-12-11 11:30:01 +01:00
|
|
|
Utils::FileName m_baseFilePath;
|
2017-11-20 15:31:21 +01:00
|
|
|
mutable std::unique_ptr<PersistentSettingsWriter> m_writer;
|
2018-05-06 10:55:49 +03:00
|
|
|
bool m_readOnly = false;
|
2017-11-20 15:31:21 +01:00
|
|
|
};
|
|
|
|
|
|
2017-12-12 12:23:03 +01:00
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
// BackingUpSettingsAccessor:
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT BackUpStrategy
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
virtual ~BackUpStrategy() = default;
|
|
|
|
|
|
|
|
|
|
virtual FileNameList readFileCandidates(const Utils::FileName &baseFileName) const;
|
|
|
|
|
// Return -1 if data1 is better that data2, 0 if both are equally worthwhile
|
|
|
|
|
// and 1 if data2 is better than data1
|
2017-12-14 15:23:17 +01:00
|
|
|
virtual int compare(const SettingsAccessor::RestoreData &data1,
|
|
|
|
|
const SettingsAccessor::RestoreData &data2) const;
|
2017-12-12 12:23:03 +01:00
|
|
|
|
2018-01-18 15:11:28 +01:00
|
|
|
virtual optional<FileName>
|
|
|
|
|
backupName(const QVariantMap &oldData, const FileName &path, const QVariantMap &data) const;
|
2017-12-12 12:23:03 +01:00
|
|
|
};
|
|
|
|
|
|
2017-12-14 15:23:17 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT BackingUpSettingsAccessor : public SettingsAccessor
|
2017-12-12 12:23:03 +01:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
BackingUpSettingsAccessor(const QString &docType, const QString &displayName,
|
|
|
|
|
const QString &applicationDisplayName);
|
2017-12-12 14:58:23 +01:00
|
|
|
BackingUpSettingsAccessor(std::unique_ptr<BackUpStrategy> &&strategy, const QString &docType,
|
|
|
|
|
const QString &displayName, const QString &applicationDisplayName);
|
2017-12-12 12:23:03 +01:00
|
|
|
|
2018-05-07 15:07:21 +02:00
|
|
|
RestoreData readData(const Utils::FileName &path, QWidget *parent) const override;
|
2017-12-12 14:58:23 +01:00
|
|
|
Utils::optional<Issue> writeData(const Utils::FileName &path, const QVariantMap &data,
|
2018-05-07 15:07:21 +02:00
|
|
|
QWidget *parent) const override;
|
2017-12-12 12:23:03 +01:00
|
|
|
|
|
|
|
|
BackUpStrategy *strategy() const { return m_strategy.get(); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Utils::FileNameList readFileCandidates(const FileName &path) const;
|
|
|
|
|
RestoreData bestReadFileData(const FileNameList &candidates, QWidget *parent) const;
|
2017-12-12 14:58:23 +01:00
|
|
|
void backupFile(const FileName &path, const QVariantMap &data, QWidget *parent) const;
|
2017-12-12 12:23:03 +01:00
|
|
|
|
|
|
|
|
std::unique_ptr<BackUpStrategy> m_strategy;
|
|
|
|
|
};
|
|
|
|
|
|
2017-11-15 13:49:44 +01:00
|
|
|
// --------------------------------------------------------------------
|
2017-12-12 14:58:23 +01:00
|
|
|
// UpgradingSettingsAccessor:
|
2017-11-15 13:49:44 +01:00
|
|
|
// --------------------------------------------------------------------
|
2017-11-20 16:19:05 +01:00
|
|
|
|
2017-12-12 14:58:23 +01:00
|
|
|
class UpgradingSettingsAccessor;
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT VersionedBackUpStrategy : public BackUpStrategy
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
VersionedBackUpStrategy(const UpgradingSettingsAccessor *accessor);
|
|
|
|
|
|
|
|
|
|
// Return -1 if data1 is better that data2, 0 if both are equally worthwhile
|
|
|
|
|
// and 1 if data2 is better than data1
|
2017-12-14 15:23:17 +01:00
|
|
|
int compare(const SettingsAccessor::RestoreData &data1,
|
|
|
|
|
const SettingsAccessor::RestoreData &data2) const override;
|
2017-12-12 14:58:23 +01:00
|
|
|
|
|
|
|
|
optional<FileName>
|
|
|
|
|
backupName(const QVariantMap &oldData, const FileName &path, const QVariantMap &data) const override;
|
|
|
|
|
|
|
|
|
|
const UpgradingSettingsAccessor *accessor() const { return m_accessor; }
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
const UpgradingSettingsAccessor *m_accessor = nullptr;
|
|
|
|
|
};
|
|
|
|
|
|
2017-11-20 16:19:05 +01:00
|
|
|
// Handles updating a QVariantMap from version() to version() + 1
|
2017-11-15 16:19:16 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT VersionUpgrader
|
2017-11-15 13:49:44 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2017-11-22 20:49:53 +01:00
|
|
|
VersionUpgrader(const int version, const QString &extension);
|
2017-11-15 13:49:44 +01:00
|
|
|
virtual ~VersionUpgrader() = default;
|
|
|
|
|
|
2017-11-22 20:49:53 +01:00
|
|
|
int version() const;
|
|
|
|
|
QString backupExtension() const;
|
2017-11-15 13:49:44 +01:00
|
|
|
|
|
|
|
|
virtual QVariantMap upgrade(const QVariantMap &data) = 0;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
typedef QPair<QLatin1String,QLatin1String> Change;
|
|
|
|
|
QVariantMap renameKeys(const QList<Change> &changes, QVariantMap map) const;
|
2017-11-22 20:49:53 +01:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const int m_version;
|
|
|
|
|
const QString m_extension;
|
2017-11-15 13:49:44 +01:00
|
|
|
};
|
2010-01-19 16:59:18 +01:00
|
|
|
|
2017-12-12 17:38:23 +01:00
|
|
|
class MergingSettingsAccessor;
|
2014-02-27 12:47:13 +01:00
|
|
|
|
2017-12-12 14:58:23 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT UpgradingSettingsAccessor : public BackingUpSettingsAccessor
|
2010-01-19 16:59:18 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2018-03-07 17:34:09 +01:00
|
|
|
UpgradingSettingsAccessor(const QString &docType,
|
|
|
|
|
const QString &displayName, const QString &applicationDisplayName);
|
2017-12-12 14:58:23 +01:00
|
|
|
UpgradingSettingsAccessor(std::unique_ptr<BackUpStrategy> &&strategy, const QString &docType,
|
2017-12-11 11:30:01 +01:00
|
|
|
const QString &displayName, const QString &appDisplayName);
|
2010-01-19 16:59:18 +01:00
|
|
|
|
2014-02-28 18:32:09 +01:00
|
|
|
int currentVersion() const;
|
|
|
|
|
int firstSupportedVersion() const;
|
2017-12-12 14:58:23 +01:00
|
|
|
int lastSupportedVersion() const;
|
|
|
|
|
|
|
|
|
|
QByteArray settingsId() const { return m_id; }
|
|
|
|
|
|
|
|
|
|
bool isValidVersionAndId(const int version, const QByteArray &id) const;
|
|
|
|
|
VersionUpgrader *upgrader(const int version) const;
|
|
|
|
|
|
|
|
|
|
RestoreData readData(const Utils::FileName &path, QWidget *parent) const override;
|
|
|
|
|
|
2017-12-12 17:38:23 +01:00
|
|
|
protected:
|
2017-12-12 14:58:23 +01:00
|
|
|
QVariantMap prepareToWriteSettings(const QVariantMap &data) const override;
|
|
|
|
|
|
|
|
|
|
void setSettingsId(const QByteArray &id) { m_id = id; }
|
|
|
|
|
|
|
|
|
|
bool addVersionUpgrader(std::unique_ptr<VersionUpgrader> &&upgrader);
|
|
|
|
|
RestoreData upgradeSettings(const RestoreData &data, const int targetVersion) const;
|
|
|
|
|
RestoreData validateVersionRange(const RestoreData &data) const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QByteArray m_id;
|
|
|
|
|
std::vector<std::unique_ptr<VersionUpgrader>> m_upgraders;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
2017-12-12 17:38:23 +01:00
|
|
|
// MergingSettingsAccessor:
|
2017-12-12 14:58:23 +01:00
|
|
|
// --------------------------------------------------------------------
|
2014-02-28 18:32:09 +01:00
|
|
|
|
2017-12-12 17:38:23 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT MergingSettingsAccessor : public UpgradingSettingsAccessor
|
2017-12-12 14:58:23 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2017-12-12 17:38:23 +01:00
|
|
|
struct SettingsMergeData {
|
|
|
|
|
QVariantMap main;
|
|
|
|
|
QVariantMap secondary;
|
|
|
|
|
QString key;
|
|
|
|
|
};
|
2014-02-28 16:13:34 +01:00
|
|
|
|
2017-12-12 17:38:23 +01:00
|
|
|
MergingSettingsAccessor(std::unique_ptr<BackUpStrategy> &&strategy,
|
|
|
|
|
const QString &docType, const QString &displayName,
|
|
|
|
|
const QString &applicationDisplayName);
|
2018-01-18 11:46:41 +01:00
|
|
|
|
2017-12-11 11:30:01 +01:00
|
|
|
RestoreData readData(const Utils::FileName &path, QWidget *parent) const final;
|
|
|
|
|
|
2017-12-14 15:23:17 +01:00
|
|
|
void setSecondaryAccessor(std::unique_ptr<SettingsAccessor> &&secondary);
|
2014-03-03 18:57:42 +01:00
|
|
|
|
2017-12-12 17:38:23 +01:00
|
|
|
protected:
|
2017-12-15 10:30:09 +01:00
|
|
|
|
2017-12-12 17:38:23 +01:00
|
|
|
RestoreData mergeSettings(const RestoreData &main, const RestoreData &secondary) const;
|
|
|
|
|
|
|
|
|
|
virtual SettingsMergeResult merge(const SettingsMergeData &global,
|
|
|
|
|
const SettingsMergeData &local) const = 0;
|
|
|
|
|
bool isHouseKeepingKey(const QString &key) const;
|
2013-04-04 13:30:16 +02:00
|
|
|
|
2017-12-12 17:38:23 +01:00
|
|
|
virtual QVariantMap postprocessMerge(const QVariantMap &main, const QVariantMap &secondary,
|
|
|
|
|
const QVariantMap &result) const;
|
2014-02-27 12:47:13 +01:00
|
|
|
|
2017-12-12 17:38:23 +01:00
|
|
|
private:
|
2017-12-14 15:23:17 +01:00
|
|
|
std::unique_ptr<SettingsAccessor> m_secondaryAccessor;
|
2010-01-19 16:59:18 +01:00
|
|
|
};
|
|
|
|
|
|
2017-12-12 17:38:23 +01:00
|
|
|
using SettingsMergeFunction = std::function<SettingsMergeResult(const MergingSettingsAccessor::SettingsMergeData &,
|
|
|
|
|
const MergingSettingsAccessor::SettingsMergeData &)>;
|
|
|
|
|
QTCREATOR_UTILS_EXPORT QVariant mergeQVariantMaps(const QVariantMap &mainTree, const QVariantMap &secondaryTree,
|
|
|
|
|
const SettingsMergeFunction &merge);
|
|
|
|
|
|
2017-11-15 16:19:16 +01:00
|
|
|
} // namespace Utils
|