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

Allow only main (IPC owner) instance to save global settings

This commit is contained in:
novist 2015-02-21 14:49:27 +02:00
parent 886ee3ff10
commit 41fd5ab558
5 changed files with 61 additions and 25 deletions

View File

@ -81,6 +81,15 @@ IPC::~IPC()
}
}
IPC& IPC::getInstance()
{
#ifdef Q_OS_ANDROID
Q_ASSERT(0 && "IPC can not be used on android");
#endif
static IPC instance;
return instance;
}
time_t IPC::postEvent(const QString &name, const QByteArray& data/*=QByteArray()*/, uint32_t dest/*=0*/)
{
QByteArray binName = name.toUtf8();

View File

@ -34,6 +34,7 @@ using IPCEventHandler = std::function<bool (const QByteArray&)>;
class IPC : public QThread
{
Q_OBJECT
IPC();
protected:
static const int EVENT_TIMER_MS = 1000;
static const int EVENT_GC_TIMEOUT = 5;
@ -41,9 +42,10 @@ protected:
static const int OWNERSHIP_TIMEOUT_S = 5;
public:
IPC();
~IPC();
static IPC& getInstance();
struct IPCEvent
{
uint32_t dest;

View File

@ -70,6 +70,9 @@ int main(int argc, char *argv[])
parser.addOption(QCommandLineOption("p", QObject::tr("Starts new instance and loads specified profile."), QObject::tr("profile")));
parser.process(a);
#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"))
@ -126,7 +129,7 @@ int main(int argc, char *argv[])
#ifndef Q_OS_ANDROID
// Inter-process communication
IPC ipc;
IPC& ipc = IPC::getInstance();
ipc.registerEventHandler("uri", &toxURIEventHandler);
ipc.registerEventHandler("save", &toxSaveEventHandler);
ipc.registerEventHandler("activate", &toxActivateEventHandler);

View File

@ -23,6 +23,7 @@
#ifdef QTOX_PLATFORM_EXT
#include "src/platform/autorun.h"
#endif
#include "src/ipc.h"
#include <QFont>
#include <QApplication>
@ -333,7 +334,18 @@ void Settings::save(bool writePersonal)
void Settings::save(QString path, bool writePersonal)
{
qDebug() << "Settings: Saving in "<<path;
#ifndef Q_OS_ANDROID
if (IPC::getInstance().isCurrentOwner())
#endif
saveGlobal(path);
if (writePersonal) // Core::switchConfiguration
savePersonal(path);
}
void Settings::saveGlobal(QString path)
{
qDebug() << "Settings: Saving in " << path;
QSettings s(path, QSettings::IniFormat);
@ -420,31 +432,39 @@ void Settings::save(QString path, bool writePersonal)
s.beginGroup("Video");
s.setValue("camVideoRes",camVideoRes);
s.endGroup();
}
if (writePersonal && !currentProfile.isEmpty()) // Core::switchConfiguration
void Settings::savePersonal(QString path)
{
if (currentProfile.isEmpty())
{
QSettings ps(QFileInfo(path).dir().filePath(currentProfile + ".ini"), QSettings::IniFormat);
ps.beginGroup("Friends");
ps.beginWriteArray("Friend", friendLst.size());
int index = 0;
for (auto& frnd : friendLst)
{
ps.setArrayIndex(index);
ps.setValue("addr", frnd.addr);
ps.setValue("alias", frnd.alias);
ps.setValue("autoAcceptDir", frnd.autoAcceptDir);
index++;
}
ps.endArray();
ps.endGroup();
ps.beginGroup("Privacy");
ps.setValue("typingNotification", typingNotification);
ps.setValue("enableLogging", enableLogging);
ps.setValue("encryptLogs", encryptLogs);
ps.setValue("encryptTox", encryptTox);
ps.endGroup();
qDebug() << "Settings: could not save personal settings because currentProfile profile is empty";
return;
}
qDebug() << "Settings: Saving personal in " << path;
QSettings ps(QFileInfo(path).dir().filePath(currentProfile + ".ini"), QSettings::IniFormat);
ps.beginGroup("Friends");
ps.beginWriteArray("Friend", friendLst.size());
int index = 0;
for (auto& frnd : friendLst)
{
ps.setArrayIndex(index);
ps.setValue("addr", frnd.addr);
ps.setValue("alias", frnd.alias);
ps.setValue("autoAcceptDir", frnd.autoAcceptDir);
index++;
}
ps.endArray();
ps.endGroup();
ps.beginGroup("Privacy");
ps.setValue("typingNotification", typingNotification);
ps.setValue("enableLogging", enableLogging);
ps.setValue("encryptLogs", encryptLogs);
ps.setValue("encryptTox", encryptTox);
ps.endGroup();
}
uint32_t Settings::makeProfileId(const QString& profile)

View File

@ -254,6 +254,8 @@ private:
Settings(Settings &settings) = delete;
Settings& operator=(const Settings&) = delete;
static uint32_t makeProfileId(const QString& profile);
void saveGlobal(QString path);
void savePersonal(QString path);
static const QString FILENAME;
static const QString OLDFILENAME;