danicoinwallet/src/Settings.cpp
2015-04-30 15:11:48 +03:00

225 lines
7 KiB
C++

// Copyright (c) 2011-2015 The Cryptonote developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <QApplication>
#include <QFile>
#include <QJsonDocument>
#include <QSettings>
#include <QStandardPaths>
#include <QTextCodec>
#include <common/util.h>
#include "CommandLineParser.h"
#include "CurrencyAdapter.h"
#include "Settings.h"
namespace WalletGui {
Settings& Settings::instance() {
static Settings inst;
return inst;
}
Settings::Settings() : QObject(), m_cmdLineParser(nullptr) {
}
Settings::~Settings() {
}
void Settings::setCommandLineParser(CommandLineParser* _cmdLineParser) {
Q_CHECK_PTR(_cmdLineParser);
m_cmdLineParser = _cmdLineParser;
}
void Settings::load() {
QFile cfgFile(getDataDir().absoluteFilePath(QCoreApplication::applicationName() + ".cfg"));
if (cfgFile.open(QIODevice::ReadOnly)) {
m_settings = QJsonDocument::fromJson(cfgFile.readAll()).object();
cfgFile.close();
if (!m_settings.contains("walletFile")) {
m_addressBookFile = getDataDir().absoluteFilePath(QCoreApplication::applicationName() + ".addressbook");
} else {
m_addressBookFile = m_settings.value("walletFile").toString();
m_addressBookFile.replace(m_addressBookFile.lastIndexOf(".wallet"), 7, ".addressbook");
}
} else {
m_addressBookFile = getDataDir().absoluteFilePath(QCoreApplication::applicationName() + ".addressbook");
}
}
QDir Settings::getDataDir() const {
Q_CHECK_PTR(m_cmdLineParser);
return QDir(m_cmdLineParser->getDataDir());
}
QString Settings::getWalletFile() const {
return m_settings.contains("walletFile") ? m_settings.value("walletFile").toString() :
getDataDir().absoluteFilePath(QCoreApplication::applicationName() + ".wallet");
}
QString Settings::getAddressBookFile() const {
return m_addressBookFile;
}
bool Settings::isEncrypted() const {
return m_settings.contains("encrypted") ? m_settings.value("encrypted").toBool() : false;
}
QString Settings::getVersion() const {
return GIT_REVISION;
}
bool Settings::isStartOnLoginEnabled() const {
bool res = false;
#ifdef Q_OS_MAC
QDir autorunDir = QDir::home();
if (!autorunDir.cd("Library") || !autorunDir.cd("LaunchAgents")) {
return false;
}
QString autorunFilePath = autorunDir.absoluteFilePath(QCoreApplication::applicationName() + ".plist");
if (!QFile::exists(autorunFilePath)) {
return false;
}
QSettings autorunSettings(autorunFilePath, QSettings::NativeFormat);
res = autorunSettings.value("RunAtLoad", false).toBool();
#elif defined(Q_OS_LINUX)
QString configPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
if (configPath.isEmpty()) {
return false;
}
QDir autorunDir(configPath);
if (!autorunDir.cd("autostart")) {
return false;
}
QString autorunFilePath = autorunDir.absoluteFilePath(QCoreApplication::applicationName() + ".desktop");
res = QFile::exists(autorunFilePath);
#elif defined(Q_OS_WIN)
QSettings autorunSettings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
QString keyName = QString("%1Wallet").arg(CurrencyAdapter::instance().getCurrencyDisplayName());
res = autorunSettings.contains(keyName) &&
!QDir::fromNativeSeparators(autorunSettings.value(keyName).toString()).compare(QApplication::applicationFilePath());
#endif
return res;
}
#ifdef Q_OS_WIN
bool Settings::isMinimizeToTrayEnabled() const {
return m_settings.contains("minimizeToTray") ? m_settings.value("minimizeToTray").toBool() : false;
}
bool Settings::isCloseToTrayEnabled() const {
return m_settings.contains("closeToTray") ? m_settings.value("closeToTray").toBool() : false;
}
#endif
void Settings::setWalletFile(const QString& _file) {
if (_file.endsWith(".wallet") || _file.endsWith(".keys")) {
m_settings.insert("walletFile", _file);
} else {
m_settings.insert("walletFile", _file + ".wallet");
}
saveSettings();
m_addressBookFile = m_settings.value("walletFile").toString();
m_addressBookFile.replace(m_addressBookFile.lastIndexOf(".wallet"), 7, ".addressbook");
}
void Settings::setEncrypted(bool _encrypted) {
if (isEncrypted() != _encrypted) {
m_settings.insert("encrypted", _encrypted);
saveSettings();
}
}
void Settings::setCurrentTheme(const QString& _theme) {
}
void Settings::setStartOnLoginEnabled(bool _enable) {
#ifdef Q_OS_MAC
QDir autorunDir = QDir::home();
if (!autorunDir.cd("Library") || !autorunDir.cd("LaunchAgents")) {
return;
}
QString autorunFilePath = autorunDir.absoluteFilePath(QCoreApplication::applicationName() + ".plist");
QSettings autorunSettings(autorunFilePath, QSettings::NativeFormat);
autorunSettings.setValue("Label", "org." + QCoreApplication::applicationName());
autorunSettings.setValue("Program", QApplication::applicationFilePath());
autorunSettings.setValue("RunAtLoad", _enable);
autorunSettings.setValue("ProcessType", "InterActive");
#elif defined(Q_OS_LINUX)
QString configPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
if (configPath.isEmpty()) {
return;
}
QDir autorunDir(configPath);
if(!autorunDir.exists("autostart")) {
autorunDir.mkdir("autostart");
}
if (!autorunDir.cd("autostart")) {
return;
}
QString autorunFilePath = autorunDir.absoluteFilePath(QCoreApplication::applicationName() + ".desktop");
QFile autorunFile(autorunFilePath);
if (!autorunFile.open(QFile::WriteOnly | QFile::Truncate)) {
return;
}
if (_enable) {
autorunFile.write("[Desktop Entry]\n");
autorunFile.write("Type=Application\n");
autorunFile.write(QString("Name=%1 Wallet\n").arg(CurrencyAdapter::instance().getCurrencyDisplayName()).toLocal8Bit());
autorunFile.write(QString("Exec=%1\n").arg(QApplication::applicationFilePath()).toLocal8Bit());
autorunFile.write("Terminal=false\n");
autorunFile.write("Hidden=false\n");
autorunFile.close();
} else {
QFile::remove(autorunFilePath);
}
#elif defined(Q_OS_WIN)
QSettings autorunSettings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
QString keyName = QString("%1Wallet").arg(CurrencyAdapter::instance().getCurrencyDisplayName());
if (_enable) {
autorunSettings.setValue(keyName, QDir::toNativeSeparators(QApplication::applicationFilePath()));
} else {
autorunSettings.remove(keyName);
}
#endif
}
#ifdef Q_OS_WIN
void Settings::setMinimizeToTrayEnabled(bool _enable) {
if (isMinimizeToTrayEnabled() != _enable) {
m_settings.insert("minimizeToTray", _enable);
saveSettings();
}
}
void Settings::setCloseToTrayEnabled(bool _enable) {
if (isCloseToTrayEnabled() != _enable) {
m_settings.insert("closeToTray", _enable);
saveSettings();
}
}
#endif
void Settings::saveSettings() const {
QFile cfgFile(getDataDir().absoluteFilePath(QCoreApplication::applicationName() + ".cfg"));
if (cfgFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
QJsonDocument cfg_doc(m_settings);
cfgFile.write(cfg_doc.toJson());
cfgFile.close();
}
}
}