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

540 lines
14 KiB
C++
Raw Normal View History

2014-06-25 04:11:11 +08:00
/*
Copyright (C) 2013 by Maxim Biro <nurupo.contributions@gmail.com>
This file is part of Tox Qt GUI.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the COPYING file for more details.
*/
#include "settings.h"
2014-08-01 20:46:28 +08:00
#include "smileypack.h"
2014-06-25 04:11:11 +08:00
2014-09-11 21:44:34 +08:00
#include <QFont>
2014-06-25 04:11:11 +08:00
#include <QApplication>
#include <QDir>
#include <QFile>
#include <QSettings>
#include <QStandardPaths>
#include <QDebug>
2014-08-01 20:46:28 +08:00
#include <QList>
2014-06-25 04:11:11 +08:00
const QString Settings::FILENAME = "settings.ini";
2014-07-13 04:58:58 +08:00
bool Settings::makeToxPortable{false};
2014-06-25 04:11:11 +08:00
Settings::Settings() :
loaded(false), useCustomDhtList{false}
2014-06-25 04:11:11 +08:00
{
load();
}
Settings::~Settings()
{
save();
}
Settings& Settings::getInstance()
{
static Settings settings;
return settings;
}
void Settings::load()
{
if (loaded) {
2014-06-25 04:11:11 +08:00
return;
}
2014-06-25 04:11:11 +08:00
2014-07-13 04:58:58 +08:00
QFile portableSettings(FILENAME);
if (portableSettings.exists())
{
QSettings ps(FILENAME, QSettings::IniFormat);
ps.beginGroup("General");
makeToxPortable = ps.value("makeToxPortable", false).toBool();
ps.endGroup();
}
else
makeToxPortable = false;
2014-07-13 04:58:58 +08:00
QString filePath = QDir(getSettingsDirPath()).filePath(FILENAME);
2014-06-25 04:11:11 +08:00
//if no settings file exist -- use the default one
QFile file(filePath);
if (!file.exists()) {
qDebug() << "No settings file found, using defaults";
2014-06-25 04:11:11 +08:00
filePath = ":/conf/" + FILENAME;
}
qDebug() << "Settings: Loading from "<<filePath;
2014-06-25 04:11:11 +08:00
QSettings s(filePath, QSettings::IniFormat);
s.beginGroup("DHT Server");
if (s.value("useCustomList").toBool())
{
useCustomDhtList = true;
qDebug() << "Using custom bootstrap nodes list";
int serverListSize = s.beginReadArray("dhtServerList");
for (int i = 0; i < serverListSize; i ++) {
s.setArrayIndex(i);
DhtServer server;
server.name = s.value("name").toString();
server.userId = s.value("userId").toString();
server.address = s.value("address").toString();
server.port = s.value("port").toInt();
dhtServerList << server;
}
s.endArray();
2014-06-25 04:11:11 +08:00
}
else
useCustomDhtList=false;
2014-06-25 04:11:11 +08:00
s.endGroup();
2014-09-01 04:26:45 +08:00
friendAddresses.clear();
s.beginGroup("Friends");
int size = s.beginReadArray("fullAddresses");
for (int i = 0; i < size; i ++) {
s.setArrayIndex(i);
friendAddresses.append(s.value("addr").toString());
}
s.endArray();
s.endGroup();
2014-06-25 04:11:11 +08:00
s.beginGroup("General");
2014-07-02 06:47:06 +08:00
enableIPv6 = s.value("enableIPv6", true).toBool();
2014-07-05 01:22:43 +08:00
useTranslations = s.value("useTranslations", true).toBool();
2014-07-13 04:58:58 +08:00
makeToxPortable = s.value("makeToxPortable", false).toBool();
2014-06-25 04:11:11 +08:00
s.endGroup();
s.beginGroup("Widgets");
QList<QString> objectNames = s.childKeys();
for (const QString& name : objectNames) {
widgetSettings[name] = s.value(name).toByteArray();
}
s.endGroup();
s.beginGroup("GUI");
enableSmoothAnimation = s.value("smoothAnimation", true).toBool();
2014-08-11 00:50:40 +08:00
smileyPack = s.value("smileyPack", QString()).toString();
2014-06-25 04:11:11 +08:00
customEmojiFont = s.value("customEmojiFont", true).toBool();
emojiFontFamily = s.value("emojiFontFamily", "DejaVu Sans").toString();
emojiFontPointSize = s.value("emojiFontPointSize", QApplication::font().pointSize()).toInt();
firstColumnHandlePos = s.value("firstColumnHandlePos", 50).toInt();
secondColumnHandlePosFromRight = s.value("secondColumnHandlePosFromRight", 50).toInt();
timestampFormat = s.value("timestampFormat", "hh:mm").toString();
minimizeOnClose = s.value("minimizeOnClose", false).toBool();
2014-08-11 00:14:50 +08:00
useNativeStyle = s.value("nativeStyle", false).toBool();
useNativeDecoration = s.value("nativeDecoration", true).toBool();
2014-08-11 23:33:32 +08:00
s.endGroup();
s.beginGroup("State");
windowGeometry = s.value("windowGeometry", QByteArray()).toByteArray();
windowState = s.value("windowState", QByteArray()).toByteArray();
2014-08-11 20:07:27 +08:00
splitterState = s.value("splitterState", QByteArray()).toByteArray();
2014-06-25 04:11:11 +08:00
s.endGroup();
s.beginGroup("Privacy");
typingNotification = s.value("typingNotification", false).toBool();
s.endGroup();
2014-08-11 00:50:40 +08:00
// try to set a smiley pack if none is selected
if (!SmileyPack::isValid(smileyPack) && !SmileyPack::listSmileyPacks().isEmpty())
smileyPack = SmileyPack::listSmileyPacks()[0].second;
// Read the embedded DHT bootsrap nodes list if needed
if (dhtServerList.isEmpty())
{
qDebug() << "Using embeded bootstrap nodes list";
QSettings rcs(":/conf/settings.ini", QSettings::IniFormat);
rcs.beginGroup("DHT Server");
int serverListSize = rcs.beginReadArray("dhtServerList");
for (int i = 0; i < serverListSize; i ++) {
rcs.setArrayIndex(i);
DhtServer server;
server.name = rcs.value("name").toString();
server.userId = rcs.value("userId").toString();
server.address = rcs.value("address").toString();
server.port = rcs.value("port").toInt();
dhtServerList << server;
}
rcs.endArray();
rcs.endGroup();
}
2014-06-25 04:11:11 +08:00
loaded = true;
}
void Settings::save()
{
QString filePath = QDir(getSettingsDirPath()).filePath(FILENAME);
2014-07-13 04:58:58 +08:00
save(filePath);
}
2014-06-25 04:11:11 +08:00
2014-07-13 04:58:58 +08:00
void Settings::save(QString path)
{
qDebug() << "Settings: Saving in "<<path;
2014-07-13 04:58:58 +08:00
QSettings s(path, QSettings::IniFormat);
2014-06-25 04:11:11 +08:00
s.clear();
s.beginGroup("DHT Server");
s.setValue("useCustomList", useCustomDhtList);
2014-06-25 04:11:11 +08:00
s.beginWriteArray("dhtServerList", dhtServerList.size());
for (int i = 0; i < dhtServerList.size(); i ++) {
s.setArrayIndex(i);
s.setValue("name", dhtServerList[i].name);
s.setValue("userId", dhtServerList[i].userId);
s.setValue("address", dhtServerList[i].address);
s.setValue("port", dhtServerList[i].port);
}
s.endArray();
s.endGroup();
2014-09-01 04:26:45 +08:00
s.beginGroup("Friends");
s.beginWriteArray("fullAddresses", friendAddresses.size());
for (int i = 0; i < friendAddresses.size(); i ++) {
s.setArrayIndex(i);
s.setValue("addr", friendAddresses[i]);
}
s.endArray();
s.endGroup();
2014-06-25 04:11:11 +08:00
s.beginGroup("General");
2014-07-02 06:47:06 +08:00
s.setValue("enableIPv6", enableIPv6);
2014-07-05 01:22:43 +08:00
s.setValue("useTranslations",useTranslations);
2014-07-13 04:58:58 +08:00
s.setValue("makeToxPortable",makeToxPortable);
2014-06-25 04:11:11 +08:00
s.endGroup();
s.beginGroup("Widgets");
const QList<QString> widgetNames = widgetSettings.keys();
for (const QString& name : widgetNames) {
s.setValue(name, widgetSettings.value(name));
}
s.endGroup();
s.beginGroup("GUI");
s.setValue("smoothAnimation", enableSmoothAnimation);
s.setValue("smileyPack", smileyPack);
s.setValue("customEmojiFont", customEmojiFont);
s.setValue("emojiFontFamily", emojiFontFamily);
s.setValue("emojiFontPointSize", emojiFontPointSize);
s.setValue("firstColumnHandlePos", firstColumnHandlePos);
s.setValue("secondColumnHandlePosFromRight", secondColumnHandlePosFromRight);
s.setValue("timestampFormat", timestampFormat);
s.setValue("minimizeOnClose", minimizeOnClose);
2014-08-11 00:14:50 +08:00
s.setValue("nativeStyle", useNativeStyle);
s.setValue("nativeDecoration", useNativeDecoration);
2014-08-11 23:33:32 +08:00
s.endGroup();
s.beginGroup("State");
s.setValue("windowGeometry", windowGeometry);
s.setValue("windowState", windowState);
2014-08-11 20:07:27 +08:00
s.setValue("splitterState", splitterState);
2014-06-25 04:11:11 +08:00
s.endGroup();
s.beginGroup("Privacy");
s.setValue("typingNotification", typingNotification);
s.endGroup();
}
QString Settings::getSettingsDirPath()
{
2014-07-13 04:58:58 +08:00
if (makeToxPortable)
return ".";
2014-06-25 04:11:11 +08:00
// workaround for https://bugreports.qt-project.org/browse/QTBUG-38845
#ifdef Q_OS_WIN
return QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
#else
return QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QDir::separator() + "tox");
2014-06-25 04:11:11 +08:00
#endif
}
2014-09-24 23:55:54 +08:00
QPixmap Settings::getSavedAvatar(const QString &ownerId)
2014-09-24 22:51:16 +08:00
{
QDir dir(getSettingsDirPath());
2014-09-28 06:19:30 +08:00
QString filePath = dir.filePath("avatars/"+ownerId.left(64)+".png");
QFileInfo info(filePath);
2014-09-24 22:51:16 +08:00
QPixmap pic;
if (!info.exists())
{
2014-09-28 06:19:30 +08:00
QString filePath = dir.filePath("avatar_"+ownerId.left(64));
if (!QFileInfo(filePath).exists()) // try without truncation, for old self avatars
2014-09-28 06:36:19 +08:00
filePath = dir.filePath("avatar_"+ownerId);
pic.load(filePath);
saveAvatar(pic, ownerId);
QFile::remove(filePath);
}
else
pic.load(filePath);
2014-09-24 22:51:16 +08:00
return pic;
}
2014-09-24 23:55:54 +08:00
void Settings::saveAvatar(QPixmap& pic, const QString& ownerId)
2014-09-24 22:51:16 +08:00
{
QDir dir(getSettingsDirPath());
dir.mkdir("avatars/");
2014-09-28 06:19:30 +08:00
// ignore nospam (good idea, and also the addFriend funcs which call getAvatar don't have it)
QString filePath = dir.filePath("avatars/"+ownerId.left(64)+".png");
2014-09-24 22:51:16 +08:00
pic.save(filePath, "png");
}
void Settings::saveAvatarHash(const QByteArray& hash, const QString& ownerId)
{
QDir dir(getSettingsDirPath());
dir.mkdir("avatars/");
QFile file(dir.filePath("avatars/"+ownerId.left(64)+".hash"));
if (!file.open(QIODevice::WriteOnly))
return;
file.write(hash);
file.close();
}
QByteArray Settings::getAvatarHash(const QString& ownerId)
{
QDir dir(getSettingsDirPath());
dir.mkdir("avatars/");
QFile file(dir.filePath("avatars/"+ownerId.left(64)+".hash"));
if (!file.open(QIODevice::ReadOnly))
return QByteArray();
QByteArray out = file.readAll();
file.close();
return out;
}
2014-06-25 04:11:11 +08:00
const QList<Settings::DhtServer>& Settings::getDhtServerList() const
{
return dhtServerList;
}
void Settings::setDhtServerList(const QList<DhtServer>& newDhtServerList)
{
dhtServerList = newDhtServerList;
emit dhtServerListChanged();
}
2014-07-02 06:47:06 +08:00
bool Settings::getEnableIPv6() const
{
return enableIPv6;
}
void Settings::setEnableIPv6(bool newValue)
{
enableIPv6 = newValue;
}
2014-07-13 04:58:58 +08:00
bool Settings::getMakeToxPortable() const
{
return makeToxPortable;
}
void Settings::setMakeToxPortable(bool newValue)
{
makeToxPortable = newValue;
save(FILENAME); // Commit to the portable file that we don't want to use it
if (!newValue) // Update the new file right now if not already done
save();
2014-07-13 04:58:58 +08:00
}
2014-07-05 01:22:43 +08:00
bool Settings::getUseTranslations() const
{
return useTranslations;
}
void Settings::setUseTranslations(bool newValue)
{
useTranslations = newValue;
}
2014-06-25 04:11:11 +08:00
bool Settings::getEnableLogging() const
{
return enableLogging;
}
void Settings::setEnableLogging(bool newValue)
{
enableLogging = newValue;
}
bool Settings::getEncryptLogs() const
{
return encryptLogs;
}
void Settings::setEncryptLogs(bool newValue)
{
encryptLogs = newValue;
}
void Settings::setWidgetData(const QString& uniqueName, const QByteArray& data)
{
widgetSettings[uniqueName] = data;
}
QByteArray Settings::getWidgetData(const QString& uniqueName) const
{
return widgetSettings.value(uniqueName);
}
bool Settings::isAnimationEnabled() const
{
return enableSmoothAnimation;
}
void Settings::setAnimationEnabled(bool newValue)
{
enableSmoothAnimation = newValue;
}
2014-07-25 20:52:14 +08:00
QString Settings::getSmileyPack() const
2014-06-25 04:11:11 +08:00
{
return smileyPack;
}
2014-07-25 20:52:14 +08:00
void Settings::setSmileyPack(const QString &value)
2014-06-25 04:11:11 +08:00
{
smileyPack = value;
emit smileyPackChanged();
}
bool Settings::isCurstomEmojiFont() const
{
return customEmojiFont;
}
void Settings::setCurstomEmojiFont(bool value)
{
customEmojiFont = value;
emit emojiFontChanged();
}
int Settings::getEmojiFontPointSize() const
{
return emojiFontPointSize;
}
void Settings::setEmojiFontPointSize(int value)
{
emojiFontPointSize = value;
emit emojiFontChanged();
}
int Settings::getFirstColumnHandlePos() const
{
return firstColumnHandlePos;
}
void Settings::setFirstColumnHandlePos(const int pos)
{
firstColumnHandlePos = pos;
}
int Settings::getSecondColumnHandlePosFromRight() const
{
return secondColumnHandlePosFromRight;
}
void Settings::setSecondColumnHandlePosFromRight(const int pos)
{
secondColumnHandlePosFromRight = pos;
}
const QString &Settings::getTimestampFormat() const
{
return timestampFormat;
}
void Settings::setTimestampFormat(const QString &format)
{
timestampFormat = format;
emit timestampFormatChanged();
}
QString Settings::getEmojiFontFamily() const
{
return emojiFontFamily;
}
void Settings::setEmojiFontFamily(const QString &value)
{
emojiFontFamily = value;
emit emojiFontChanged();
}
2014-08-11 00:14:50 +08:00
bool Settings::getUseNativeStyle() const
{
return useNativeStyle;
}
void Settings::setUseNativeStyle(bool value)
{
useNativeStyle = value;
}
bool Settings::getUseNativeDecoration() const
{
return useNativeDecoration;
}
void Settings::setUseNativeDecoration(bool value)
{
useNativeDecoration = value;
}
QByteArray Settings::getWindowGeometry() const
{
return windowGeometry;
}
void Settings::setWindowGeometry(const QByteArray &value)
{
windowGeometry = value;
}
QByteArray Settings::getWindowState() const
{
return windowState;
}
void Settings::setWindowState(const QByteArray &value)
{
windowState = value;
}
2014-08-11 20:07:27 +08:00
QByteArray Settings::getSplitterState() const
{
return splitterState;
}
void Settings::setSplitterState(const QByteArray &value)
{
splitterState = value;
}
2014-06-25 04:11:11 +08:00
bool Settings::isMinimizeOnCloseEnabled() const
{
return minimizeOnClose;
}
void Settings::setMinimizeOnClose(bool newValue)
{
minimizeOnClose = newValue;
}
bool Settings::isTypingNotificationEnabled() const
{
return typingNotification;
}
void Settings::setTypingNotification(bool enabled)
{
typingNotification = enabled;
}