Merge pull request #2892 from glassez/bittorrent

Core classes redesign (Issue #2433).
This commit is contained in:
sledgehammer999 2015-06-11 23:54:05 +03:00
commit 3aa0a845cd
207 changed files with 15273 additions and 13581 deletions

View file

@ -50,8 +50,8 @@
#include <winreg.h>
#endif
#include "misc.h"
#include "fs_utils.h"
#include "core/utils/fs.h"
#include "core/utils/misc.h"
Preferences* Preferences::m_instance = 0;
@ -116,15 +116,18 @@ Preferences::~Preferences()
save();
}
Preferences * Preferences::instance()
Preferences *Preferences::instance()
{
if (!m_instance)
m_instance = new Preferences;
return m_instance;
}
void Preferences::drop()
void Preferences::initInstance()
{
if (!m_instance)
m_instance = new Preferences;
}
void Preferences::freeInstance()
{
if (m_instance) {
delete m_instance;
@ -132,12 +135,11 @@ void Preferences::drop()
}
}
void Preferences::save()
bool Preferences::save()
{
QReadLocker locker(&lock);
if (!dirty)
return;
if (!dirty) return false;
#ifndef Q_OS_MAC
// QSettings delete the file before writing it out. This can result in problems
@ -160,20 +162,20 @@ void Preferences::save()
settings->sync(); // Important to get error status
if (settings->status() == QSettings::AccessError) {
delete settings;
return;
return false;
}
QString new_path = settings->fileName();
delete settings;
QString final_path = new_path;
int index = final_path.lastIndexOf("_new", -1, Qt::CaseInsensitive);
final_path.remove(index, 4);
fsutils::forceRemove(final_path);
Utils::Fs::forceRemove(final_path);
QFile::rename(new_path, final_path);
#else
delete settings;
#endif
emit changed();
return true;
}
const QVariant Preferences::value(const QString &key, const QVariant &defaultValue) const
@ -345,7 +347,7 @@ void Preferences::setWinStartup(bool b)
{
QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
if (b) {
const QString bin_path = "\"" + fsutils::toNativePath(qApp->applicationFilePath()) + "\"";
const QString bin_path = "\"" + Utils::Fs::toNativePath(qApp->applicationFilePath()) + "\"";
settings.setValue("qBittorrent", bin_path);
}
else {
@ -359,13 +361,13 @@ QString Preferences::getSavePath() const
{
QString save_path = value("Preferences/Downloads/SavePath").toString();
if (!save_path.isEmpty())
return fsutils::fromNativePath(save_path);
return fsutils::QDesktopServicesDownloadLocation();
return Utils::Fs::fromNativePath(save_path);
return Utils::Fs::QDesktopServicesDownloadLocation();
}
void Preferences::setSavePath(const QString &save_path)
{
setValue("Preferences/Downloads/SavePath", fsutils::fromNativePath(save_path));
setValue("Preferences/Downloads/SavePath", Utils::Fs::fromNativePath(save_path));
}
bool Preferences::isTempPathEnabled() const
@ -381,12 +383,12 @@ void Preferences::setTempPathEnabled(bool enabled)
QString Preferences::getTempPath() const
{
const QString temp = QDir(getSavePath()).absoluteFilePath("temp");
return fsutils::fromNativePath(value("Preferences/Downloads/TempPath", temp).toString());
return Utils::Fs::fromNativePath(value("Preferences/Downloads/TempPath", temp).toString());
}
void Preferences::setTempPath(const QString &path)
{
setValue("Preferences/Downloads/TempPath", fsutils::fromNativePath(path));
setValue("Preferences/Downloads/TempPath", Utils::Fs::fromNativePath(path));
}
bool Preferences::useIncompleteFilesExtension() const
@ -411,12 +413,12 @@ void Preferences::setAppendTorrentLabel(bool b)
QString Preferences::lastLocationPath() const
{
return fsutils::fromNativePath(value("Preferences/Downloads/LastLocationPath").toString());
return Utils::Fs::fromNativePath(value("Preferences/Downloads/LastLocationPath").toString());
}
void Preferences::setLastLocationPath(const QString &path)
{
setValue("Preferences/Downloads/LastLocationPath", fsutils::fromNativePath(path));
setValue("Preferences/Downloads/LastLocationPath", Utils::Fs::fromNativePath(path));
}
bool Preferences::preAllocateAllFiles() const
@ -467,7 +469,7 @@ QStringList Preferences::getScanDirs() const
QStringList newList;
foreach (const QString& s, originalList)
newList << fsutils::fromNativePath(s);
newList << Utils::Fs::fromNativePath(s);
return newList;
}
@ -477,28 +479,28 @@ void Preferences::setScanDirs(const QStringList &dirs)
QStringList newList;
if (!dirs.isEmpty())
foreach (const QString& s, dirs)
newList << fsutils::fromNativePath(s);
newList << Utils::Fs::fromNativePath(s);
setValue("Preferences/Downloads/ScanDirs", newList);
}
QList<bool> Preferences::getDownloadInScanDirs() const
{
return misc::boolListfromStringList(value("Preferences/Downloads/DownloadInScanDirs").toStringList());
return Utils::Misc::boolListfromStringList(value("Preferences/Downloads/DownloadInScanDirs").toStringList());
}
void Preferences::setDownloadInScanDirs(const QList<bool> &list)
{
setValue("Preferences/Downloads/DownloadInScanDirs", misc::toStringList(list));
setValue("Preferences/Downloads/DownloadInScanDirs", Utils::Misc::toStringList(list));
}
QString Preferences::getScanDirsLastPath() const
{
return fsutils::fromNativePath(value("Preferences/Downloads/ScanDirsLastPath").toString());
return Utils::Fs::fromNativePath(value("Preferences/Downloads/ScanDirsLastPath").toString());
}
void Preferences::setScanDirsLastPath(const QString &path)
{
setValue("Preferences/Downloads/ScanDirsLastPath", fsutils::fromNativePath(path));
setValue("Preferences/Downloads/ScanDirsLastPath", Utils::Fs::fromNativePath(path));
}
bool Preferences::isTorrentExportEnabled() const
@ -508,12 +510,12 @@ bool Preferences::isTorrentExportEnabled() const
QString Preferences::getTorrentExportDir() const
{
return fsutils::fromNativePath(value("Preferences/Downloads/TorrentExportDir").toString());
return Utils::Fs::fromNativePath(value("Preferences/Downloads/TorrentExportDir").toString());
}
void Preferences::setTorrentExportDir(QString path)
{
setValue("Preferences/Downloads/TorrentExportDir", fsutils::fromNativePath(path.trimmed()));
setValue("Preferences/Downloads/TorrentExportDir", Utils::Fs::fromNativePath(path.trimmed()));
}
bool Preferences::isFinishedTorrentExportEnabled() const
@ -523,12 +525,12 @@ bool Preferences::isFinishedTorrentExportEnabled() const
QString Preferences::getFinishedTorrentExportDir() const
{
return fsutils::fromNativePath(value("Preferences/Downloads/FinishedTorrentExportDir").toString());
return Utils::Fs::fromNativePath(value("Preferences/Downloads/FinishedTorrentExportDir").toString());
}
void Preferences::setFinishedTorrentExportDir(QString path)
{
setValue("Preferences/Downloads/FinishedTorrentExportDir", fsutils::fromNativePath(path.trimmed()));
setValue("Preferences/Downloads/FinishedTorrentExportDir", Utils::Fs::fromNativePath(path.trimmed()));
}
bool Preferences::isMailNotificationEnabled() const
@ -950,12 +952,12 @@ void Preferences::setGlobalMaxRatio(qreal ratio)
setValue("Preferences/Bittorrent/MaxRatio", ratio);
}
int Preferences::getMaxRatioAction() const
MaxRatioAction Preferences::getMaxRatioAction() const
{
return value("Preferences/Bittorrent/MaxRatioAction", PAUSE_ACTION).toInt();
return value("Preferences/Bittorrent/MaxRatioAction", MaxRatioAction::Pause).toInt();
}
void Preferences::setMaxRatioAction(int act)
void Preferences::setMaxRatioAction(MaxRatioAction act)
{
setValue("Preferences/Bittorrent/MaxRatioAction", act);
}
@ -973,12 +975,12 @@ void Preferences::setFilteringEnabled(bool enabled)
QString Preferences::getFilter() const
{
return fsutils::fromNativePath(value("Preferences/IPFilter/File").toString());
return Utils::Fs::fromNativePath(value("Preferences/IPFilter/File").toString());
}
void Preferences::setFilter(const QString &path)
{
setValue("Preferences/IPFilter/File", fsutils::fromNativePath(path));
setValue("Preferences/IPFilter/File", Utils::Fs::fromNativePath(path));
}
QStringList Preferences::bannedIPs() const
@ -1273,12 +1275,12 @@ void Preferences::setAutoRunEnabled(bool enabled)
QString Preferences::getAutoRunProgram() const
{
return fsutils::fromNativePath(value("AutoRun/program").toString());
return Utils::Fs::fromNativePath(value("AutoRun/program").toString());
}
void Preferences::setAutoRunProgram(const QString &program)
{
setValue("AutoRun/program", fsutils::fromNativePath(program));
setValue("AutoRun/program", Utils::Fs::fromNativePath(program));
}
bool Preferences::shutdownWhenDownloadsComplete() const
@ -1786,12 +1788,12 @@ bool Preferences::isMagnetLinkAssocSet()
// Check magnet link assoc
QRegExp exe_reg("\"([^\"]+)\".*");
QString shell_command = fsutils::toNativePath(settings.value("magnet/shell/open/command/Default", "").toString());
QString shell_command = Utils::Fs::toNativePath(settings.value("magnet/shell/open/command/Default", "").toString());
if (exe_reg.indexIn(shell_command) < 0)
return false;
QString assoc_exe = exe_reg.cap(1);
qDebug("exe: %s", qPrintable(assoc_exe));
if (assoc_exe.compare(fsutils::toNativePath(qApp->applicationFilePath()), Qt::CaseInsensitive) != 0)
if (assoc_exe.compare(Utils::Fs::toNativePath(qApp->applicationFilePath()), Qt::CaseInsensitive) != 0)
return false;
return true;
@ -1827,9 +1829,9 @@ void Preferences::setMagnetLinkAssoc(bool set)
settings.setValue("magnet/Default", "URL:Magnet link");
settings.setValue("magnet/Content Type", "application/x-magnet");
settings.setValue("magnet/URL Protocol", "");
settings.setValue("magnet/DefaultIcon/Default", fsutils::toNativePath(icon_str));
settings.setValue("magnet/DefaultIcon/Default", Utils::Fs::toNativePath(icon_str));
settings.setValue("magnet/shell/Default", "open");
settings.setValue("magnet/shell/open/command/Default", fsutils::toNativePath(command_str));
settings.setValue("magnet/shell/open/command/Default", Utils::Fs::toNativePath(command_str));
}
else if (isMagnetLinkAssocSet()) {
settings.remove("magnet");
@ -2477,3 +2479,9 @@ void Preferences::setHostNameCookies(const QString &host_name, const QList<QByte
hosts_table.insert(host_name, raw_cookies);
setValue("Rss/hosts_cookies", hosts_table);
}
void Preferences::apply()
{
if (save())
emit changed();
}