Commit 4c4b3be3 authored by Leandro Melo's avatar Leandro Melo Committed by Leandro T. C. Melo

ProjectExplorer: Refactor file settings acessor

This is the first patch of a series. The final goal is to introduce
shared settings into Creator.

This particular patch shouldn't affect functionality, but it changes
a bit the design of the settings acessor and prepares the code for
the upcoming commits.

In addition it also makes restoring the settings slightly more
robust, since it will try to load a compatible "old" file from the
quick check before it actually performs the more expensive check.

NOTE: Files userfileacessor.* will be renamed in a following
commit to settingsacessor.*. It's not done right now because I
think it will make the review easier on gerrit.

Reviewed-by: Tobias Hunger
Change-Id: I950c813936afb6c55c770e0d6f061eb27b37c47b
Reviewed-on: http://codereview.qt-project.org/5081Reviewed-by: default avatarLeandro T. C. Melo <leandro.melo@nokia.com>
parent d8dedb43
......@@ -217,14 +217,12 @@ Target *Project::target(const QString &id) const
void Project::saveSettings()
{
emit aboutToSaveSettings();
UserFileAccessor accessor;
accessor.saveSettings(this, toMap());
SettingsAccessor::instance()->saveSettings(this, toMap());
}
bool Project::restoreSettings()
{
UserFileAccessor accessor;
QVariantMap map(accessor.restoreSettings(this));
QVariantMap map(SettingsAccessor::instance()->restoreSettings(this));
bool ok = fromMap(map);
if (ok)
emit settingsLoaded();
......
......@@ -35,46 +35,72 @@
#include <QtCore/QVariantMap>
namespace ProjectExplorer {
class Project;
class UserFileVersionHandler
{
public:
UserFileVersionHandler();
virtual ~UserFileVersionHandler();
// The user file version this handler accepts for input.
virtual int userFileVersion() const = 0;
virtual QString displayUserFileVersion() const = 0;
// Update from userFileVersion() to userFileVersion() + 1
virtual QVariantMap update(Project *project, const QVariantMap &map) = 0;
protected:
typedef QPair<QLatin1String,QLatin1String> Change;
QVariantMap renameKeys(const QList<Change> &changes, QVariantMap map);
};
namespace Internal {
class UserFileVersionHandler;
}
class UserFileAccessor
class SettingsAccessor
{
public:
UserFileAccessor();
~UserFileAccessor();
QVariantMap restoreSettings(Project * project);
bool saveSettings(Project * project, const QVariantMap &map);
~SettingsAccessor();
int latestUserFileVersion() const;
static SettingsAccessor *instance();
QString mapIdTo(const QString &id, int version);
QVariantMap restoreSettings(Project *project) const;
bool saveSettings(const Project *project, const QVariantMap &map) const;
private:
SettingsAccessor();
// Takes ownership of the handler!
void addVersionHandler(UserFileVersionHandler *handler);
void addVersionHandler(Internal::UserFileVersionHandler *handler);
// The relevant data from the settings currently in use.
class SettingsData
{
public:
SettingsData() : m_version(-1), m_usingBackup(false) {}
SettingsData(const QVariantMap &map) : m_version(-1), m_usingBackup(false), m_map(map) {}
int m_version;
bool m_usingBackup;
QVariantMap m_map;
QString m_fileName;
};
// The entity which actually reads/writes to the settings file.
class FileAccessor
{
public:
FileAccessor(const QByteArray &id,
const QString &defaultSuffix,
const QString &environmentSuffix,
bool envSpecific);
bool readFile(Project *project, SettingsData *settings) const;
bool writeFile(const Project *project, const SettingsData *settings) const;
private:
void assignSuffix(const QString &defaultSuffix, const QString &environmentSuffix);
QString assembleFileName(const Project *project) const;
bool findNewestCompatibleSetting(SettingsData *settings) const;
QByteArray m_id;
QString m_suffix;
bool m_environmentSpecific;
};
static bool verifyEnvironmentId(const QString &id);
QMap<int, UserFileVersionHandler *> m_handlers;
QMap<int, Internal::UserFileVersionHandler *> m_handlers;
int m_firstVersion;
int m_lastVersion;
const FileAccessor m_userFileAcessor;
};
} // namespace ProjectExplorer
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment