sledgehammer999
9 years ago
62 changed files with 2144 additions and 1534 deletions
@ -0,0 +1,244 @@
@@ -0,0 +1,244 @@
|
||||
/*
|
||||
* Bittorrent Client using Qt and libtorrent. |
||||
* Copyright (C) 2016 Vladimir Golovnev <glassez@yandex.ru> |
||||
* Copyright (C) 2014 sledgehammer999 <hammered999@gmail.com> |
||||
* |
||||
* 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 2 |
||||
* 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 |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with this program; if not, write to the Free Software |
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
* |
||||
* In addition, as a special exception, the copyright holders give permission to |
||||
* link this program with the OpenSSL project's "OpenSSL" library (or with |
||||
* modified versions of it that use the same license as the "OpenSSL" library), |
||||
* and distribute the linked executables. You must obey the GNU General Public |
||||
* License in all respects for all of the code used other than "OpenSSL". If you |
||||
* modify file(s), you may extend this exception to your version of the file(s), |
||||
* but you are not obligated to do so. If you do not wish to do so, delete this |
||||
* exception statement from your version. |
||||
*/ |
||||
|
||||
#include <QFile> |
||||
#include <QHash> |
||||
#include <QStringList> |
||||
#include <QSettings> |
||||
|
||||
#include "logger.h" |
||||
#include "utils/fs.h" |
||||
#include "settingsstorage.h" |
||||
|
||||
namespace |
||||
{ |
||||
inline QSettings *createSettings(const QString &name) |
||||
{ |
||||
#ifdef Q_OS_WIN |
||||
return new QSettings(QSettings::IniFormat, QSettings::UserScope, "qBittorrent", name); |
||||
#else |
||||
return new QSettings("qBittorrent", name); |
||||
#endif |
||||
} |
||||
|
||||
#ifdef QBT_USES_QT5 |
||||
typedef QHash<QString, QString> MappingTable; |
||||
#else |
||||
class MappingTable: public QHash<QString, QString> |
||||
{ |
||||
public: |
||||
MappingTable(std::initializer_list<std::pair<QString, QString>> list) |
||||
{ |
||||
reserve(static_cast<int>(list.size())); |
||||
for (std::initializer_list<std::pair<QString, QString>>::const_iterator it = list.begin(); it != list.end(); ++it) |
||||
insert(it->first, it->second); |
||||
} |
||||
}; |
||||
#endif |
||||
|
||||
QString mapKey(const QString &key) |
||||
{ |
||||
static const MappingTable keyMapping = { |
||||
|
||||
{ "BitTorrent/Session/MaxRatioAction", "Preferences/Bittorrent/MaxRatioAction" }, |
||||
{ "BitTorrent/Session/DefaultSavePath", "Preferences/Downloads/SavePath" }, |
||||
{ "BitTorrent/Session/TempPath", "Preferences/Downloads/TempPath" }, |
||||
{ "BitTorrent/Session/TempPathEnabled", "Preferences/Downloads/TempPathEnabled" }, |
||||
{ "BitTorrent/Session/AddTorrentPaused", "Preferences/Downloads/StartInPause" }, |
||||
#ifdef QBT_USES_QT5 |
||||
{ "AddNewTorrentDialog/TreeHeaderState", "AddNewTorrentDialog/qt5/treeHeaderState" }, |
||||
#else |
||||
{ "AddNewTorrentDialog/TreeHeaderState", "AddNewTorrentDialog/treeHeaderState" }, |
||||
#endif |
||||
{ "AddNewTorrentDialog/Width", "AddNewTorrentDialog/width" }, |
||||
{ "AddNewTorrentDialog/Position", "AddNewTorrentDialog/y" }, |
||||
{ "AddNewTorrentDialog/Expanded", "AddNewTorrentDialog/expanded" }, |
||||
{ "AddNewTorrentDialog/SavePathHistory", "TorrentAdditionDlg/save_path_history" }, |
||||
{ "AddNewTorrentDialog/Enabled", "Preferences/Downloads/NewAdditionDialog" }, |
||||
{ "AddNewTorrentDialog/TopLevel", "Preferences/Downloads/NewAdditionDialogFront" } |
||||
|
||||
}; |
||||
|
||||
return keyMapping.value(key, key); |
||||
} |
||||
} |
||||
|
||||
SettingsStorage *SettingsStorage::m_instance = nullptr; |
||||
|
||||
SettingsStorage::SettingsStorage() |
||||
: m_dirty(false) |
||||
, m_lock(QReadWriteLock::Recursive) |
||||
{ |
||||
QSettings *settings; |
||||
QStringList keys; |
||||
|
||||
#ifdef Q_OS_MAC |
||||
settings = createSettings("qBittorrent"); |
||||
#else |
||||
settings = createSettings("qBittorrent_new"); |
||||
QString newPath = settings->fileName(); |
||||
|
||||
// This means that the PC closed either due to power outage
|
||||
// or because the disk was full. In any case the settings weren't transfered
|
||||
// in their final position. So assume that qbittorrent_new.ini/qbittorrent_new.conf
|
||||
// contains the most recent settings.
|
||||
if (!settings->allKeys().isEmpty()) { |
||||
Logger::instance()->addMessage(tr("Detected unclean program exit. Using fallback file to restore settings."), Log::WARNING); |
||||
m_dirty = true; |
||||
} |
||||
else { |
||||
delete settings; |
||||
settings = createSettings("qBittorrent"); |
||||
} |
||||
#endif |
||||
|
||||
keys = settings->allKeys(); |
||||
|
||||
// Copy everything into memory. This means even keys inserted in the file manually
|
||||
// or that we don't touch directly in this code(eg disabled by ifdef). This ensures
|
||||
// that they will be copied over when save our settings to disk.
|
||||
foreach (const QString &key, keys) |
||||
m_data[key] = settings->value(key); |
||||
|
||||
//Ensures sync to disk before we attempt to manipulate the files from save().
|
||||
delete settings; |
||||
|
||||
#ifndef Q_OS_MAC |
||||
Utils::Fs::forceRemove(newPath); |
||||
|
||||
if (m_dirty) |
||||
save(); |
||||
#endif |
||||
|
||||
m_timer.setSingleShot(true); |
||||
m_timer.setInterval(5 * 1000); |
||||
connect(&m_timer, SIGNAL(timeout()), SLOT(save())); |
||||
} |
||||
|
||||
SettingsStorage::~SettingsStorage() |
||||
{ |
||||
save(); |
||||
} |
||||
|
||||
void SettingsStorage::initInstance() |
||||
{ |
||||
if (!m_instance) |
||||
m_instance = new SettingsStorage; |
||||
} |
||||
|
||||
void SettingsStorage::freeInstance() |
||||
{ |
||||
delete m_instance; |
||||
m_instance = nullptr; |
||||
} |
||||
|
||||
SettingsStorage *SettingsStorage::instance() |
||||
{ |
||||
return m_instance; |
||||
} |
||||
|
||||
bool SettingsStorage::save() |
||||
{ |
||||
QWriteLocker locker(&m_lock); |
||||
|
||||
if (!m_dirty) return false; |
||||
|
||||
#ifndef Q_OS_MAC |
||||
// QSettings delete the file before writing it out. This can result in problems
|
||||
// if the disk is full or a power outage occurs. Those events might occur
|
||||
// between deleting the file and recreating it. This is a safety measure.
|
||||
// Write everything to qBittorrent_new.ini/qBittorrent_new.conf and if it succeeds
|
||||
// replace qBittorrent_new.ini/qBittorrent.conf with it.
|
||||
QSettings *settings = createSettings("qBittorrent_new"); |
||||
#else |
||||
QSettings *settings = createSettings("qBittorrent"); |
||||
#endif |
||||
|
||||
foreach (const QString &key, m_data.keys()) |
||||
settings->setValue(key, m_data[key]); |
||||
|
||||
m_dirty = false; |
||||
locker.unlock(); |
||||
|
||||
#ifndef Q_OS_MAC |
||||
settings->sync(); // Important to get error status
|
||||
QString newPath = settings->fileName(); |
||||
QSettings::Status status = settings->status(); |
||||
|
||||
if (status != QSettings::NoError) { |
||||
if (status == QSettings::AccessError) |
||||
Logger::instance()->addMessage(tr("An access error occurred while trying to write the configuration file."), Log::CRITICAL); |
||||
else |
||||
Logger::instance()->addMessage(tr("A format error occurred while trying to write the configuration file."), Log::CRITICAL); |
||||
|
||||
delete settings; |
||||
Utils::Fs::forceRemove(newPath); |
||||
return false; |
||||
} |
||||
|
||||
delete settings; |
||||
QString finalPath = newPath; |
||||
int index = finalPath.lastIndexOf("_new", -1, Qt::CaseInsensitive); |
||||
finalPath.remove(index, 4); |
||||
Utils::Fs::forceRemove(finalPath); |
||||
QFile::rename(newPath, finalPath); |
||||
#else |
||||
delete settings; |
||||
#endif |
||||
|
||||
return true; |
||||
} |
||||
|
||||
QVariant SettingsStorage::loadValue(const QString &key, const QVariant &defaultValue) const |
||||
{ |
||||
QReadLocker locker(&m_lock); |
||||
return m_data.value(mapKey(key), defaultValue); |
||||
} |
||||
|
||||
void SettingsStorage::storeValue(const QString &key, const QVariant &value) |
||||
{ |
||||
QString realKey = mapKey(key); |
||||
QWriteLocker locker(&m_lock); |
||||
if (m_data.value(realKey) != value) { |
||||
m_dirty = true; |
||||
m_timer.start(); |
||||
m_data.insert(realKey, value); |
||||
} |
||||
} |
||||
|
||||
void SettingsStorage::removeValue(const QString &key) |
||||
{ |
||||
QString realKey = mapKey(key); |
||||
QWriteLocker locker(&m_lock); |
||||
if (m_data.contains(realKey)) { |
||||
m_dirty = true; |
||||
m_timer.start(); |
||||
m_data.remove(realKey); |
||||
} |
||||
} |
@ -0,0 +1,65 @@
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Bittorrent Client using Qt and libtorrent. |
||||
* Copyright (C) 2016 Vladimir Golovnev <glassez@yandex.ru> |
||||
* Copyright (C) 2014 sledgehammer999 <hammered999@gmail.com> |
||||
* |
||||
* 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 2 |
||||
* 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 |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with this program; if not, write to the Free Software |
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
* |
||||
* In addition, as a special exception, the copyright holders give permission to |
||||
* link this program with the OpenSSL project's "OpenSSL" library (or with |
||||
* modified versions of it that use the same license as the "OpenSSL" library), |
||||
* and distribute the linked executables. You must obey the GNU General Public |
||||
* License in all respects for all of the code used other than "OpenSSL". If you |
||||
* modify file(s), you may extend this exception to your version of the file(s), |
||||
* but you are not obligated to do so. If you do not wish to do so, delete this |
||||
* exception statement from your version. |
||||
*/ |
||||
|
||||
#ifndef SETTINGSSTORAGE_H |
||||
#define SETTINGSSTORAGE_H |
||||
|
||||
#include <QObject> |
||||
#include <QVariantHash> |
||||
#include <QTimer> |
||||
#include <QReadWriteLock> |
||||
|
||||
class SettingsStorage: public QObject |
||||
{ |
||||
Q_OBJECT |
||||
SettingsStorage(); |
||||
~SettingsStorage(); |
||||
|
||||
public: |
||||
static void initInstance(); |
||||
static void freeInstance(); |
||||
static SettingsStorage* instance(); |
||||
|
||||
QVariant loadValue(const QString &key, const QVariant &defaultValue = QVariant()) const; |
||||
void storeValue(const QString &key, const QVariant &value); |
||||
void removeValue(const QString &key); |
||||
|
||||
public slots: |
||||
bool save(); |
||||
|
||||
private: |
||||
static SettingsStorage *m_instance; |
||||
|
||||
QVariantHash m_data; |
||||
bool m_dirty; |
||||
QTimer m_timer; |
||||
mutable QReadWriteLock m_lock; |
||||
}; |
||||
|
||||
#endif // SETTINGSSTORAGE_H
|
@ -1,43 +1,43 @@
@@ -1,43 +1,43 @@
|
||||
<RCC> |
||||
<qresource prefix="/"> |
||||
<file>www/private/index.html</file> |
||||
<file>www/private/login.html</file> |
||||
<file>www/public/css/Core.css</file> |
||||
<file>www/public/css/dynamicTable.css</file> |
||||
<file>www/public/css/Layout.css</file> |
||||
<file>www/public/css/style.css</file> |
||||
<file>www/public/css/Tabs.css</file> |
||||
<file>www/public/css/Window.css</file> |
||||
<file>www/public/scripts/client.js</file> |
||||
<file>www/public/scripts/contextmenu.js</file> |
||||
<file>www/public/scripts/download.js</file> |
||||
<file>www/public/scripts/dynamicTable.js</file> |
||||
<file>www/public/scripts/excanvas-compressed.js</file> |
||||
<file>www/public/scripts/misc.js</file> |
||||
<file>www/public/scripts/mocha.js</file> |
||||
<file>www/public/scripts/mocha-init.js</file> |
||||
<file>www/public/scripts/mocha-yc.js</file> |
||||
<file>www/public/scripts/mootools-1.2-core-yc.js</file> |
||||
<file>www/public/scripts/mootools-1.2-more.js</file> |
||||
<file>www/public/scripts/parametrics.js</file> |
||||
<file>www/public/scripts/progressbar.js</file> |
||||
<file>www/public/about.html</file> |
||||
<file>www/public/addtrackers.html</file> |
||||
<file>www/public/confirmdeletion.html</file> |
||||
<file>www/public/download.html</file> |
||||
<file>www/public/downloadlimit.html</file> |
||||
<file>www/public/filters.html</file> |
||||
<file>www/public/newlabel.html</file> |
||||
<file>www/public/preferences.html</file> |
||||
<file>www/public/preferences_content.html</file> |
||||
<file>www/public/properties.html</file> |
||||
<file>www/public/properties_content.html</file> |
||||
<file>www/public/scripts/prop-general.js</file> |
||||
<file>www/public/scripts/prop-trackers.js</file> |
||||
<file>www/public/scripts/prop-webseeds.js</file> |
||||
<file>www/public/scripts/prop-files.js</file> |
||||
<file>www/public/transferlist.html</file> |
||||
<file>www/public/upload.html</file> |
||||
<file>www/public/uploadlimit.html</file> |
||||
</qresource> |
||||
<qresource prefix="/"> |
||||
<file>www/private/index.html</file> |
||||
<file>www/private/login.html</file> |
||||
<file>www/public/css/Core.css</file> |
||||
<file>www/public/css/dynamicTable.css</file> |
||||
<file>www/public/css/Layout.css</file> |
||||
<file>www/public/css/style.css</file> |
||||
<file>www/public/css/Tabs.css</file> |
||||
<file>www/public/css/Window.css</file> |
||||
<file>www/public/scripts/client.js</file> |
||||
<file>www/public/scripts/contextmenu.js</file> |
||||
<file>www/public/scripts/download.js</file> |
||||
<file>www/public/scripts/dynamicTable.js</file> |
||||
<file>www/public/scripts/excanvas-compressed.js</file> |
||||
<file>www/public/scripts/misc.js</file> |
||||
<file>www/public/scripts/mocha.js</file> |
||||
<file>www/public/scripts/mocha-init.js</file> |
||||
<file>www/public/scripts/mocha-yc.js</file> |
||||
<file>www/public/scripts/mootools-1.2-core-yc.js</file> |
||||
<file>www/public/scripts/mootools-1.2-more.js</file> |
||||
<file>www/public/scripts/parametrics.js</file> |
||||
<file>www/public/scripts/progressbar.js</file> |
||||
<file>www/public/about.html</file> |
||||
<file>www/public/addtrackers.html</file> |
||||
<file>www/public/confirmdeletion.html</file> |
||||
<file>www/public/download.html</file> |
||||
<file>www/public/downloadlimit.html</file> |
||||
<file>www/public/filters.html</file> |
||||
<file>www/public/preferences.html</file> |
||||
<file>www/public/preferences_content.html</file> |
||||
<file>www/public/properties.html</file> |
||||
<file>www/public/properties_content.html</file> |
||||
<file>www/public/scripts/prop-general.js</file> |
||||
<file>www/public/scripts/prop-trackers.js</file> |
||||
<file>www/public/scripts/prop-webseeds.js</file> |
||||
<file>www/public/scripts/prop-files.js</file> |
||||
<file>www/public/transferlist.html</file> |
||||
<file>www/public/upload.html</file> |
||||
<file>www/public/uploadlimit.html</file> |
||||
<file>www/public/newcategory.html</file> |
||||
</qresource> |
||||
</RCC> |
||||
|
Loading…
Reference in new issue