Browse Source

refactor(settings): prepare for integration of Paths

reviewable/pr6649/r2
sudden6 7 years ago
parent
commit
1e0c0b9b77
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
  1. 118
      src/main.cpp
  2. 33
      src/persistence/settings.cpp
  3. 11
      src/persistence/settings.h

118
src/main.cpp

@ -22,6 +22,7 @@ @@ -22,6 +22,7 @@
#include "src/net/autoupdate.h"
#include "src/net/toxuri.h"
#include "src/nexus.h"
#include "src/persistence/paths.h"
#include "src/persistence/profile.h"
#include "src/persistence/toxsave.h"
#include "src/video/camerasource.h"
@ -41,6 +42,7 @@ @@ -41,6 +42,7 @@
#include <ctime>
#include <sodium.h>
#include <stdio.h>
#include <memory>
#if defined(Q_OS_OSX)
#include "platform/install_osx.h"
@ -70,7 +72,6 @@ void cleanup() @@ -70,7 +72,6 @@ void cleanup()
Nexus::destroyInstance();
CameraSource::destroyInstance();
Settings::destroyInstance();
qDebug() << "Cleanup success";
#ifdef LOG_TO_FILE
@ -147,6 +148,56 @@ void logMessageHandler(QtMsgType type, const QMessageLogContext& ctxt, const QSt @@ -147,6 +148,56 @@ void logMessageHandler(QtMsgType type, const QMessageLogContext& ctxt, const QSt
#endif
}
void createLogFile(const Paths& paths)
{
const QString logFilePath{paths.getLogFilePath()};
if (logFilePath.isEmpty()) {
qCritical() << "Invalid path for log file";
return;
}
QFileInfo logFile{logFilePath};
QDir logFileDir{logFile.dir()};
if (!logFileDir.mkpath(".")) {
qCritical() << "Couldn't create path for log file";
return;
}
QByteArray logFilePathLocal{logFile.absoluteFilePath().toLocal8Bit()};
FILE* mainLogFilePtr = fopen(logFilePathLocal.constData(), "a");
// Trim log file if over 1MB
if (logFile.size() > 1000000) {
qDebug() << "Log file over 1MB, rotating...";
// close old logfile (need for windows)
if (mainLogFilePtr) {
fclose(mainLogFilePtr);
}
// Check if log.1 already exists, and if so, delete it
if (logFileDir.remove(logFileDir.absolutePath() + "qtox.log.1")) {
qDebug() << "Removed old log successfully";
} else {
qWarning() << "Unable to remove old log file";
}
if (!logFileDir.rename(logFileDir.absolutePath() + "qtox.log", logFileDir.absolutePath() + "qtox.log.1")) {
qCritical() << "Unable to move logs";
}
// open a new logfile
mainLogFilePtr = fopen(logFilePathLocal.constData(), "a");
}
if (!mainLogFilePtr) {
qCritical() << "Couldn't open logfile" << logFilePath;
}
logFileFile.store(mainLogFilePtr); // atomically set the logFile
}
int main(int argc, char* argv[])
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
@ -190,8 +241,20 @@ int main(int argc, char* argv[]) @@ -190,8 +241,20 @@ int main(int argc, char* argv[])
#endif
qsrand(time(nullptr));
Settings::getInstance();
QString locale = Settings::getInstance().getTranslation();
std::unique_ptr<Paths> paths{Paths::makePaths()};
if (paths == nullptr) {
qCritical() << "Couldn't access qTox directories";
exit(EXIT_FAILURE);
}
std::unique_ptr<Settings> settings{Settings::makeSettings(*paths)};
if (settings == nullptr) {
qCritical() << "Couldn't start Settings module";
exit(EXIT_FAILURE);
}
QString locale = settings->getTranslation();
Translator::translate(locale);
// Process arguments
@ -211,14 +274,14 @@ int main(int argc, char* argv[]) @@ -211,14 +274,14 @@ int main(int argc, char* argv[])
QObject::tr("Starts new instance and opens the login screen.")));
parser.process(*a);
uint32_t profileId = Settings::getInstance().getCurrentProfileId();
uint32_t profileId = settings->getCurrentProfileId();
IPC ipc(profileId);
if (!ipc.isAttached()) {
qCritical() << "Can't init IPC";
return EXIT_FAILURE;
}
QObject::connect(&Settings::getInstance(), &Settings::currentProfileIdChanged, &ipc,
QObject::connect(settings.get(), &Settings::currentProfileIdChanged, &ipc,
&IPC::setProfileId);
// For the auto-updater
@ -228,39 +291,7 @@ int main(int argc, char* argv[]) @@ -228,39 +291,7 @@ int main(int argc, char* argv[])
}
#ifdef LOG_TO_FILE
QString logFileDir = Settings::getInstance().getAppCacheDirPath();
QDir(logFileDir).mkpath(".");
QString logfile = logFileDir + "qtox.log";
FILE* mainLogFilePtr = fopen(logfile.toLocal8Bit().constData(), "a");
// Trim log file if over 1MB
if (QFileInfo(logfile).size() > 1000000) {
qDebug() << "Log file over 1MB, rotating...";
// close old logfile (need for windows)
if (mainLogFilePtr)
fclose(mainLogFilePtr);
QDir dir(logFileDir);
// Check if log.1 already exists, and if so, delete it
if (dir.remove(logFileDir + "qtox.log.1"))
qDebug() << "Removed old log successfully";
else
qWarning() << "Unable to remove old log file";
if (!dir.rename(logFileDir + "qtox.log", logFileDir + "qtox.log.1"))
qCritical() << "Unable to move logs";
// open a new logfile
mainLogFilePtr = fopen(logfile.toLocal8Bit().constData(), "a");
}
if (!mainLogFilePtr)
qCritical() << "Couldn't open logfile" << logfile;
logFileFile.store(mainLogFilePtr); // atomically set the logFile
createLogFile(*paths);
#endif
// Windows platform plugins DLL hell fix
@ -278,7 +309,7 @@ int main(int argc, char* argv[]) @@ -278,7 +309,7 @@ int main(int argc, char* argv[])
QString profileName;
bool autoLogin = Settings::getInstance().getAutoLogin();
bool autoLogin = settings->getAutoLogin();
uint32_t ipcDest = 0;
bool doIpc = true;
@ -298,7 +329,7 @@ int main(int argc, char* argv[]) @@ -298,7 +329,7 @@ int main(int argc, char* argv[])
doIpc = false;
autoLogin = false;
} else {
profileName = Settings::getInstance().getCurrentProfile();
profileName = settings->getCurrentProfile();
}
if (parser.positionalArguments().size() == 0) {
@ -353,7 +384,7 @@ int main(int argc, char* argv[]) @@ -353,7 +384,7 @@ int main(int argc, char* argv[])
}
Nexus::getInstance().setProfile(profile);
Settings::getInstance().setCurrentProfile(profileName);
settings->setCurrentProfile(profileName);
Nexus& nexus = Nexus::getInstance();
nexus.start();
@ -364,10 +395,11 @@ int main(int argc, char* argv[]) @@ -364,10 +395,11 @@ int main(int argc, char* argv[])
ipc.registerEventHandler("activate", &toxActivateEventHandler);
// Event was not handled by already running instance therefore we handle it ourselves
if (eventType == "uri")
if (eventType == "uri") {
handleToxURI(firstParam.toUtf8());
else if (eventType == "save")
} else if (eventType == "save") {
handleToxSave(firstParam.toUtf8());
}
QObject::connect(a.get(), &QApplication::aboutToQuit, cleanup);

33
src/persistence/settings.cpp

@ -22,6 +22,7 @@ @@ -22,6 +22,7 @@
#include "src/core/core.h"
#include "src/core/corefile.h"
#include "src/nexus.h"
#include "src/persistence/paths.h"
#include "src/persistence/profile.h"
#include "src/persistence/profilelocker.h"
#include "src/persistence/settingsserializer.h"
@ -62,8 +63,19 @@ Settings* Settings::settings{nullptr}; @@ -62,8 +63,19 @@ Settings* Settings::settings{nullptr};
QMutex Settings::bigLock{QMutex::Recursive};
QThread* Settings::settingsThread{nullptr};
Settings::Settings()
: loaded(false)
/**
* @brief Factory method for the Settings class
* @param paths Paths object, providing all important paths
* @return Settings object on success, nullptr else
*/
Settings* Settings::makeSettings(const Paths& paths) {
settings = new Settings{paths};
return settings;
}
Settings::Settings(const Paths& paths)
: paths{paths}
, loaded(false)
, useCustomDhtList{false}
, makeToxPortable{false}
, currentProfileId(0)
@ -81,6 +93,7 @@ Settings::~Settings() @@ -81,6 +93,7 @@ Settings::~Settings()
settingsThread->exit(0);
settingsThread->wait();
delete settingsThread;
settings = nullptr;
}
/**
@ -88,18 +101,9 @@ Settings::~Settings() @@ -88,18 +101,9 @@ Settings::~Settings()
*/
Settings& Settings::getInstance()
{
if (!settings)
settings = new Settings();
return *settings;
}
void Settings::destroyInstance()
{
delete settings;
settings = nullptr;
}
void Settings::loadGlobal()
{
QMutexLocker locker{&bigLock};
@ -175,8 +179,11 @@ void Settings::loadGlobal() @@ -175,8 +179,11 @@ void Settings::loadGlobal()
}
autoAwayTime = s.value("autoAwayTime", 10).toInt();
checkUpdates = s.value("checkUpdates", true).toBool();
notifySound = s.value("notifySound", true).toBool(); // note: notifySound and busySound UI elements are now under UI settings
busySound = s.value("busySound", false).toBool(); // page, but kept under General in settings file to be backwards compatible
// note: notifySound and busySound UI elements are now under UI settings
// page, but kept under General in settings file to be backwards compatible
notifySound = s.value("notifySound", true).toBool();
busySound = s.value("busySound", false).toBool();
fauxOfflineMessaging = s.value("fauxOfflineMessaging", true).toBool();
autoSaveEnabled = s.value("autoSaveEnabled", false).toBool();
globalAutoAcceptDir = s.value("globalAutoAcceptDir",

11
src/persistence/settings.h

@ -37,6 +37,7 @@ @@ -37,6 +37,7 @@
#include <QObject>
#include <QPixmap>
class Paths;
class Profile;
namespace Db {
@ -134,8 +135,9 @@ public: @@ -134,8 +135,9 @@ public:
};
public:
static Settings* makeSettings(const Paths& paths);
~Settings() override;
static Settings& getInstance();
static void destroyInstance();
QString getSettingsDirPath() const;
QString getAppDataDirPath() const;
QString getAppCacheDirPath() const;
@ -572,8 +574,7 @@ public: @@ -572,8 +574,7 @@ public:
private:
struct friendProp;
Settings();
~Settings();
Settings(const Paths &paths);
Settings(Settings& settings) = delete;
Settings& operator=(const Settings&) = delete;
void savePersonal(QString profileName, const ToxEncrypt* passkey);
@ -583,6 +584,7 @@ public slots: @@ -583,6 +584,7 @@ public slots:
void savePersonal(Profile* profile);
private:
const Paths& paths;
bool loaded;
bool useCustomDhtList;
@ -693,7 +695,8 @@ private: @@ -693,7 +695,8 @@ private:
friendProp() = delete;
friendProp(QString addr)
: addr(addr)
{}
{
}
QString alias = "";
QString addr = "";
QString autoAcceptDir = "";

Loading…
Cancel
Save