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

Clean Settings interface

This commit is contained in:
tux3 2015-06-05 16:24:47 +02:00
parent 0923e2b733
commit 1ded562d9d
No known key found for this signature in database
GPG Key ID: 7E086DD661263264
11 changed files with 68 additions and 69 deletions

View File

@ -419,7 +419,7 @@ bool Core::checkConnection()
void Core::bootstrapDht() void Core::bootstrapDht()
{ {
const Settings& s = Settings::getInstance(); const Settings& s = Settings::getInstance();
QList<Settings::DhtServer> dhtServerList = s.getDhtServerList(); QList<DhtServer> dhtServerList = s.getDhtServerList();
int listSize = dhtServerList.size(); int listSize = dhtServerList.size();
if (listSize == 0) if (listSize == 0)
@ -434,7 +434,7 @@ void Core::bootstrapDht()
int i=0; int i=0;
while (i < 2) // i think the more we bootstrap, the more we jitter because the more we overwrite nodes while (i < 2) // i think the more we bootstrap, the more we jitter because the more we overwrite nodes
{ {
const Settings::DhtServer& dhtServer = dhtServerList[j % listSize]; const DhtServer& dhtServer = dhtServerList[j % listSize];
if (tox_bootstrap(tox, dhtServer.address.toLatin1().data(), if (tox_bootstrap(tox, dhtServer.address.toLatin1().data(),
dhtServer.port, CUserId(dhtServer.userId).data(), nullptr)) dhtServer.port, CUserId(dhtServer.userId).data(), nullptr))
{ {

View File

@ -243,8 +243,8 @@ void CoreFile::onFileReceiveCallback(Tox*, uint32_t friendId, uint32_t fileId, u
{ {
// Avatars of size 0 means explicitely no avatar // Avatars of size 0 means explicitely no avatar
emit core->friendAvatarRemoved(friendId); emit core->friendAvatarRemoved(friendId);
QFile::remove(QDir(Settings::getSettingsDirPath()).filePath("avatars/"+friendAddr.left(64)+".png")); QFile::remove(Settings::getInstance().getSettingsDirPath()+"avatars/"+friendAddr.left(64)+".png");
QFile::remove(QDir(Settings::getSettingsDirPath()).filePath("avatars/"+friendAddr.left(64)+".hash")); QFile::remove(Settings::getInstance().getSettingsDirPath()+"avatars/"+friendAddr.left(64)+".hash");
return; return;
} }
else else

View File

@ -11,6 +11,14 @@ class QTimer;
enum class Status : int {Online = 0, Away, Busy, Offline}; enum class Status : int {Online = 0, Away, Busy, Offline};
struct DhtServer
{
QString name;
QString userId;
QString address;
quint16 port;
};
struct ToxFile struct ToxFile
{ {
enum FileStatus enum FileStatus

View File

@ -359,7 +359,7 @@ HistoryKeeper::ChatType HistoryKeeper::convertToChatType(int ct)
QString HistoryKeeper::getHistoryPath(QString currentProfile, int encrypted) QString HistoryKeeper::getHistoryPath(QString currentProfile, int encrypted)
{ {
QDir baseDir(Settings::getSettingsDirPath()); QDir baseDir(Settings::getInstance().getSettingsDirPath());
if (currentProfile.isEmpty()) if (currentProfile.isEmpty())
currentProfile = Settings::getInstance().getCurrentProfile(); currentProfile = Settings::getInstance().getCurrentProfile();

View File

@ -144,7 +144,7 @@ int main(int argc, char *argv[])
#ifdef LOG_TO_FILE #ifdef LOG_TO_FILE
logFile = new QTextStream; logFile = new QTextStream;
QFile logfile(Settings::getSettingsDirPath()+"qtox.log"); QFile logfile(Settings::getInstance().getSettingsDirPath()+"qtox.log");
if (logfile.open(QIODevice::Append)) if (logfile.open(QIODevice::Append))
{ {
logFile->setDevice(&logfile); logFile->setDevice(&logfile);

View File

@ -42,10 +42,10 @@
const QString Settings::globalSettingsFile = "qtox.ini"; const QString Settings::globalSettingsFile = "qtox.ini";
Settings* Settings::settings{nullptr}; Settings* Settings::settings{nullptr};
bool Settings::makeToxPortable{false};
Settings::Settings() : Settings::Settings() :
loaded(false), useCustomDhtList{false}, currentProfileId(0) loaded(false), useCustomDhtList{false},
makeToxPortable{false}, currentProfileId(0)
{ {
load(); load();
} }
@ -420,10 +420,10 @@ QString Settings::getSettingsDirPath()
// workaround for https://bugreports.qt-project.org/browse/QTBUG-38845 // workaround for https://bugreports.qt-project.org/browse/QTBUG-38845
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
return QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + QDir::separator() return QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + QDir::separator()
+ "AppData" + QDir::separator() + "Roaming" + QDir::separator() + "tox" + QDir::separator()); + "AppData" + QDir::separator() + "Roaming" + QDir::separator() + "tox")+QDir::separator();
#else #else
return QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) return QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)
+ QDir::separator() + "tox" + QDir::separator()); + QDir::separator() + "tox")+QDir::separator();
#endif #endif
} }
@ -484,7 +484,7 @@ QByteArray Settings::getAvatarHash(const QString& ownerId)
return out; return out;
} }
const QList<Settings::DhtServer>& Settings::getDhtServerList() const const QList<DhtServer>& Settings::getDhtServerList() const
{ {
return dhtServerList; return dhtServerList;
} }

View File

@ -20,6 +20,7 @@
#include <QHash> #include <QHash>
#include <QObject> #include <QObject>
#include <QPixmap> #include <QPixmap>
#include "src/core/corestructs.h"
class ToxId; class ToxId;
namespace Db { enum class syncType; } namespace Db { enum class syncType; }
@ -32,20 +33,22 @@ class Settings : public QObject
public: public:
~Settings() = default; ~Settings() = default;
static Settings& getInstance(); static Settings& getInstance();
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 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
void createPersonal(QString basename); ///< Write a default personnal settings file for a profile void save(bool writePersonal = true);
void save(QString path, bool writePersonal = true);
void load();
static QString getSettingsDirPath(); ///< The returned path ends with a directory separator signals:
void dhtServerListChanged();
struct DhtServer void smileyPackChanged();
{ void emojiFontChanged();
QString name;
QString userId;
QString address;
quint16 port;
};
public:
// Getter/setters
const QList<DhtServer>& getDhtServerList() const; const QList<DhtServer>& getDhtServerList() const;
void setDhtServerList(const QList<DhtServer>& newDhtServerList); void setDhtServerList(const QList<DhtServer>& newDhtServerList);
@ -146,28 +149,6 @@ public:
QSize getCamVideoRes() const; QSize getCamVideoRes() const;
void setCamVideoRes(QSize newValue); void setCamVideoRes(QSize newValue);
// Assume all widgets have unique names
// Don't use it to save every single thing you want to save, use it
// for some general purpose widgets, such as MainWindows or Splitters,
// which have widget->saveX() and widget->loadX() methods.
QByteArray getWidgetData(const QString& uniqueName) const;
void setWidgetData(const QString& uniqueName, const QByteArray& data);
// Wrappers around getWidgetData() and setWidgetData()
// Assume widget has a unique objectName set
template <class T>
void restoreGeometryState(T* widget) const
{
widget->restoreGeometry(getWidgetData(widget->objectName() + "Geometry"));
widget->restoreState(getWidgetData(widget->objectName() + "State"));
}
template <class T>
void saveGeometryState(const T* widget)
{
setWidgetData(widget->objectName() + "Geometry", widget->saveGeometry());
setWidgetData(widget->objectName() + "State", widget->saveState());
}
bool isAnimationEnabled() const; bool isAnimationEnabled() const;
void setAnimationEnabled(bool newValue); void setAnimationEnabled(bool newValue);
@ -248,14 +229,29 @@ public:
bool getAutoLogin() const; bool getAutoLogin() const;
void setAutoLogin(bool state); void setAutoLogin(bool state);
public: // Assume all widgets have unique names
void save(bool writePersonal = true); // Don't use it to save every single thing you want to save, use it
void save(QString path, bool writePersonal = true); // for some general purpose widgets, such as MainWindows or Splitters,
void load(); // which have widget->saveX() and widget->loadX() methods.
QByteArray getWidgetData(const QString& uniqueName) const;
void setWidgetData(const QString& uniqueName, const QByteArray& data);
// Wrappers around getWidgetData() and setWidgetData()
// Assume widget has a unique objectName set
template <class T>
void restoreGeometryState(T* widget) const
{
widget->restoreGeometry(getWidgetData(widget->objectName() + "Geometry"));
widget->restoreState(getWidgetData(widget->objectName() + "State"));
}
template <class T>
void saveGeometryState(const T* widget)
{
setWidgetData(widget->objectName() + "Geometry", widget->saveGeometry());
setWidgetData(widget->objectName() + "State", widget->saveState());
}
private: private:
static Settings* settings;
Settings(); Settings();
Settings(Settings &settings) = delete; Settings(Settings &settings) = delete;
Settings& operator=(const Settings&) = delete; Settings& operator=(const Settings&) = delete;
@ -263,9 +259,7 @@ private:
void saveGlobal(QString path); void saveGlobal(QString path);
void savePersonal(QString path); void savePersonal(QString path);
static const QString globalSettingsFile; private:
static const QString OLDFILENAME;
bool loaded; bool loaded;
bool useCustomDhtList; bool useCustomDhtList;
@ -279,7 +273,7 @@ private:
bool groupchatPosition; bool groupchatPosition;
bool enableIPv6; bool enableIPv6;
QString translation; QString translation;
static bool makeToxPortable; bool makeToxPortable;
bool autostartInTray; bool autostartInTray;
bool closeToTray; bool closeToTray;
bool minimizeToTray; bool minimizeToTray;
@ -354,10 +348,8 @@ private:
int themeColor; int themeColor;
signals: static Settings* settings;
void dhtServerListChanged(); static const QString globalSettingsFile;
void smileyPackChanged();
void emojiFontChanged();
}; };
#endif // SETTINGS_HPP #endif // SETTINGS_HPP

View File

@ -144,7 +144,7 @@ QByteArray Profile::loadToxSave()
/// TODO: Cache the data, invalidate it only when we save /// TODO: Cache the data, invalidate it only when we save
QByteArray data; QByteArray data;
QString path = Settings::getSettingsDirPath() + QDir::separator() + name + ".tox"; QString path = Settings::getInstance().getSettingsDirPath() + name + ".tox";
QFile saveFile(path); QFile saveFile(path);
qint64 fileSize; qint64 fileSize;
qDebug() << "Loading tox save "<<path; qDebug() << "Loading tox save "<<path;
@ -211,7 +211,7 @@ void Profile::saveToxSave(QByteArray data)
ProfileLocker::assertLock(); ProfileLocker::assertLock();
assert(ProfileLocker::getCurLockName() == name); assert(ProfileLocker::getCurLockName() == name);
QString path = Settings::getSettingsDirPath() + QDir::separator() + name + ".tox"; QString path = Settings::getInstance().getSettingsDirPath() + name + ".tox";
qDebug() << "Saving tox save to "<<path; qDebug() << "Saving tox save to "<<path;
QSaveFile saveFile(path); QSaveFile saveFile(path);
if (!saveFile.open(QIODevice::WriteOnly)) if (!saveFile.open(QIODevice::WriteOnly))
@ -239,7 +239,7 @@ void Profile::saveToxSave(QByteArray data)
bool Profile::exists(QString name) bool Profile::exists(QString name)
{ {
QString path = Settings::getSettingsDirPath() + QDir::separator() + name; QString path = Settings::getInstance().getSettingsDirPath() + name;
return QFile::exists(path+".tox") && QFile::exists(path+".ini"); return QFile::exists(path+".tox") && QFile::exists(path+".ini");
} }
@ -251,7 +251,7 @@ bool Profile::isEncrypted()
bool Profile::isEncrypted(QString name) bool Profile::isEncrypted(QString name)
{ {
uint8_t data[encryptHeaderSize] = {0}; uint8_t data[encryptHeaderSize] = {0};
QString path = Settings::getSettingsDirPath() + QDir::separator() + name + ".tox"; QString path = Settings::getInstance().getSettingsDirPath() + name + ".tox";
QFile saveFile(path); QFile saveFile(path);
if (!saveFile.open(QIODevice::ReadOnly)) if (!saveFile.open(QIODevice::ReadOnly))
{ {
@ -283,7 +283,7 @@ void Profile::remove()
i--; i--;
} }
} }
QString path = Settings::getSettingsDirPath() + QDir::separator() + name; QString path = Settings::getInstance().getSettingsDirPath() + name;
QFile::remove(path+".tox"); QFile::remove(path+".tox");
QFile::remove(path+".ini"); QFile::remove(path+".ini");
@ -293,8 +293,8 @@ void Profile::remove()
bool Profile::rename(QString newName) bool Profile::rename(QString newName)
{ {
QString path = Settings::getSettingsDirPath() + QDir::separator() + name, QString path = Settings::getInstance().getSettingsDirPath() + name,
newPath = Settings::getSettingsDirPath() + QDir::separator() + newName; newPath = Settings::getInstance().getSettingsDirPath() + newName;
if (!ProfileLocker::lock(newName)) if (!ProfileLocker::lock(newName))
return false; return false;

View File

@ -876,11 +876,11 @@ void ChatForm::doScreenshot()
connect(screenshotGrabber, &ScreenshotGrabber::screenshotTaken, this, &ChatForm::onScreenshotTaken); connect(screenshotGrabber, &ScreenshotGrabber::screenshotTaken, this, &ChatForm::onScreenshotTaken);
screenshotGrabber->showGrabber(); screenshotGrabber->showGrabber();
// Create dir for screenshots // Create dir for screenshots
QDir(Settings::getSettingsDirPath()).mkdir("screenshots"); QDir(Settings::getInstance().getSettingsDirPath()).mkdir("screenshots");
} }
void ChatForm::onScreenshotTaken(const QPixmap &pixmap) { void ChatForm::onScreenshotTaken(const QPixmap &pixmap) {
QTemporaryFile file(QDir(Settings::getSettingsDirPath() + QDir::separator() + "screenshots" + QDir::separator()).filePath("qTox-Screenshot-XXXXXXXX.png")); QTemporaryFile file(Settings::getInstance().getSettingsDirPath()+"screenshots"+QDir::separator()+"qTox-Screenshot-XXXXXXXX.png");
if (!file.open()) if (!file.open())
{ {
QMessageBox::warning(this, tr("Failed to open temporary file", "Temporary file for screenshot"), QMessageBox::warning(this, tr("Failed to open temporary file", "Temporary file for screenshot"),

View File

@ -264,7 +264,7 @@ void ProfileForm::onExportClicked()
GUI::showWarning(tr("Location not writable","Title of permissions popup"), tr("You do not have permission to write that location. Choose another, or cancel the save dialog.", "text of permissions popup")); GUI::showWarning(tr("Location not writable","Title of permissions popup"), tr("You do not have permission to write that location. Choose another, or cancel the save dialog.", "text of permissions popup"));
return; return;
} }
if (!QFile::copy(QDir(Settings::getSettingsDirPath()).filePath(current), path)) if (!QFile::copy(Settings::getInstance().getSettingsDirPath()+current, path))
GUI::showWarning(tr("Failed to copy file"), tr("The file you chose could not be written to.")); GUI::showWarning(tr("Failed to copy file"), tr("The file you chose could not be written to."));
} }
} }

View File

@ -17,7 +17,6 @@
#include "src/core/core.h" #include "src/core/core.h"
#include "src/misc/settings.h" #include "src/misc/settings.h"
#include <QCoreApplication> #include <QCoreApplication>
#include <QDir>
#include <QFileInfo> #include <QFileInfo>
bool toxSaveEventHandler(const QByteArray& eventData) bool toxSaveEventHandler(const QByteArray& eventData)
@ -55,7 +54,7 @@ bool handleToxSave(const QString& path)
return false; return false;
} }
QString profilePath = QDir(Settings::getSettingsDirPath()).filePath(profile + Core::TOX_EXT); QString profilePath = Settings::getInstance().getSettingsDirPath()+profile+Core::TOX_EXT;
if (QFileInfo(profilePath).exists() && !GUI::askQuestion(QObject::tr("Profile already exists", "import confirm title"), if (QFileInfo(profilePath).exists() && !GUI::askQuestion(QObject::tr("Profile already exists", "import confirm title"),
QObject::tr("A profile named \"%1\" already exists. Do you want to erase it?", "import confirm text").arg(profile))) QObject::tr("A profile named \"%1\" already exists. Do you want to erase it?", "import confirm text").arg(profile)))