1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Asynchronous settings saving

This commit is contained in:
tux3 2015-06-05 18:01:50 +02:00
parent 61de5d4c67
commit c9202f6794
No known key found for this signature in database
GPG Key ID: 7E086DD661263264
3 changed files with 51 additions and 8 deletions

View File

@ -95,7 +95,7 @@ int main(int argc, char *argv[])
#endif
qsrand(time(0));
Settings::getInstance();
Translator::translate();
// Process arguments
@ -110,7 +110,6 @@ int main(int argc, char *argv[])
#ifndef Q_OS_ANDROID
IPC::getInstance();
#endif
Settings::getInstance(); // Build our Settings singleton as soon as QApplication is ready, not before
if (parser.isSet("p"))
{
@ -272,6 +271,7 @@ int main(int argc, char *argv[])
#endif
Nexus::destroyInstance();
Settings::destroyInstance();
qDebug() << "Clean exit with status"<<errorcode;
return errorcode;
}

View File

@ -37,21 +37,34 @@
#include <QStyleFactory>
#include <QCryptographicHash>
#include <QMutexLocker>
#include <QThread>
#define SHOW_SYSTEM_TRAY_DEFAULT (bool) true
const QString Settings::globalSettingsFile = "qtox.ini";
Settings* Settings::settings{nullptr};
QMutex Settings::bigLock{QMutex::Recursive};
QThread* Settings::settingsThread{nullptr};
Settings::Settings() :
loaded(false), useCustomDhtList{false},
makeToxPortable{false}, currentProfileId(0)
{
settingsThread = new QThread();
settingsThread->setObjectName("qTox Settings");
settingsThread->start(QThread::LowPriority);
moveToThread(settingsThread);
load();
}
Settings::~Settings()
{
sync();
settingsThread->exit(0);
settingsThread->wait();
delete settingsThread;
}
Settings& Settings::getInstance()
{
if (!settings)
@ -60,6 +73,12 @@ Settings& Settings::getInstance()
return *settings;
}
void Settings::destroyInstance()
{
delete settings;
settings = nullptr;
}
void Settings::load()
{
QMutexLocker locker{&bigLock};
@ -263,12 +282,20 @@ void Settings::load()
void Settings::save(bool writePersonal)
{
QString filePath = QDir(getSettingsDirPath()).filePath(globalSettingsFile);
if (QThread::currentThread() != settingsThread)
return (void) QMetaObject::invokeMethod(&getInstance(), "save",
Q_ARG(bool, writePersonal));
QString filePath = getSettingsDirPath()+globalSettingsFile;
save(filePath, writePersonal);
}
void Settings::save(QString path, bool writePersonal)
{
if (QThread::currentThread() != settingsThread)
return (void) QMetaObject::invokeMethod(&getInstance(), "save",
Q_ARG(QString, path), Q_ARG(bool, writePersonal));
#ifndef Q_OS_ANDROID
if (IPC::getInstance().isCurrentOwner())
#endif
@ -1289,3 +1316,15 @@ void Settings::createSettingsDir()
if (!directory.exists() && !directory.mkpath(directory.absolutePath()))
qCritical() << "Error while creating directory " << dir;
}
void Settings::sync()
{
if (QThread::currentThread() != settingsThread)
{
QMetaObject::invokeMethod(&getInstance(), "sync", Qt::BlockingQueuedConnection);
return;
}
QMutexLocker locker{&bigLock};
qApp->processEvents();
}

View File

@ -32,16 +32,18 @@ class Settings : public QObject
{
Q_OBJECT
public:
~Settings() = default;
static Settings& getInstance();
static void destroyInstance();
QString getSettingsDirPath(); ///< The returned path ends with a directory separator
void createSettingsDir(); ///< Creates a path to the settings dir, if it doesn't already exist
void createPersonal(QString basename); ///< Write a default personnal .ini settings file for a profile
public slots:
void load();
void save(bool writePersonal = true);
void save(QString path, bool writePersonal = true);
void save(bool writePersonal = true); ///< Asynchronous
void save(QString path, bool writePersonal = true); ///< Asynchronous
void sync(); ///< Waits for all asynchronous operations to complete
signals:
void dhtServerListChanged();
@ -254,6 +256,7 @@ public:
private:
Settings();
~Settings();
Settings(Settings &settings) = delete;
Settings& operator=(const Settings&) = delete;
static uint32_t makeProfileId(const QString& profile);
@ -352,6 +355,7 @@ private:
static QMutex bigLock;
static Settings* settings;
static const QString globalSettingsFile;
static QThread* settingsThread;
};
#endif // SETTINGS_HPP