Some work about adaptive color scheme for Web UI (PR #19901)
http://[316:c51a:62a3:8b9::4]/d4708/qBittorrent/src/branch/adaptive-webui
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
374 lines
11 KiB
374 lines
11 KiB
15 years ago
|
/*
|
||
10 years ago
|
* Bittorrent Client using Qt and libtorrent.
|
||
15 years ago
|
* Copyright (C) 2010 Christian Kandeler, Christophe Dumez
|
||
|
*
|
||
|
* 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.
|
||
|
*
|
||
|
* Contact : chris@qbittorrent.org
|
||
|
*/
|
||
|
|
||
|
#include <QDir>
|
||
|
#include <QFileInfo>
|
||
|
#include <QString>
|
||
10 years ago
|
#include <QStringList>
|
||
15 years ago
|
#include <QTemporaryFile>
|
||
10 years ago
|
|
||
10 years ago
|
#include "utils/misc.h"
|
||
|
#include "utils/fs.h"
|
||
10 years ago
|
#include "preferences.h"
|
||
|
#include "filesystemwatcher.h"
|
||
10 years ago
|
#include "bittorrent/session.h"
|
||
|
#include "scanfoldersmodel.h"
|
||
15 years ago
|
|
||
10 years ago
|
namespace
|
||
|
{
|
||
|
const int PathColumn = 0;
|
||
|
const int DownloadAtTorrentColumn = 1;
|
||
9 years ago
|
const int DownloadPath = 2;
|
||
15 years ago
|
}
|
||
|
|
||
10 years ago
|
class ScanFoldersModel::PathData
|
||
|
{
|
||
15 years ago
|
public:
|
||
10 years ago
|
PathData(const QString &path)
|
||
|
: path(path)
|
||
|
, downloadAtPath(false)
|
||
9 years ago
|
, downloadPath(path)
|
||
10 years ago
|
{
|
||
|
}
|
||
|
|
||
9 years ago
|
PathData(const QString &path, bool downloadAtPath, const QString &downloadPath)
|
||
10 years ago
|
: path(path)
|
||
9 years ago
|
, downloadAtPath(downloadAtPath)
|
||
|
, downloadPath(downloadPath)
|
||
10 years ago
|
{
|
||
|
}
|
||
|
|
||
9 years ago
|
const QString path; //watching directory
|
||
|
bool downloadAtPath; //if TRUE save data to watching directory
|
||
|
QString downloadPath; //if 'downloadAtPath' FALSE use this path for save data
|
||
15 years ago
|
};
|
||
|
|
||
10 years ago
|
ScanFoldersModel *ScanFoldersModel::m_instance = 0;
|
||
|
|
||
10 years ago
|
bool ScanFoldersModel::initInstance(QObject *parent)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (!m_instance) {
|
||
10 years ago
|
m_instance = new ScanFoldersModel(parent);
|
||
10 years ago
|
return true;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
void ScanFoldersModel::freeInstance()
|
||
|
{
|
||
|
if (m_instance) {
|
||
|
delete m_instance;
|
||
|
m_instance = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ScanFoldersModel *ScanFoldersModel::instance()
|
||
|
{
|
||
10 years ago
|
return m_instance;
|
||
15 years ago
|
}
|
||
|
|
||
10 years ago
|
ScanFoldersModel::ScanFoldersModel(QObject *parent)
|
||
|
: QAbstractTableModel(parent)
|
||
|
, m_fsWatcher(0)
|
||
|
{
|
||
10 years ago
|
configure();
|
||
|
connect(Preferences::instance(), SIGNAL(changed()), SLOT(configure()));
|
||
10 years ago
|
}
|
||
15 years ago
|
|
||
10 years ago
|
ScanFoldersModel::~ScanFoldersModel()
|
||
|
{
|
||
|
qDeleteAll(m_pathList);
|
||
15 years ago
|
}
|
||
15 years ago
|
|
||
10 years ago
|
int ScanFoldersModel::rowCount(const QModelIndex &parent) const
|
||
|
{
|
||
|
return parent.isValid() ? 0 : m_pathList.count();
|
||
15 years ago
|
}
|
||
|
|
||
10 years ago
|
int ScanFoldersModel::columnCount(const QModelIndex &parent) const
|
||
|
{
|
||
|
Q_UNUSED(parent);
|
||
9 years ago
|
return 3;
|
||
15 years ago
|
}
|
||
|
|
||
10 years ago
|
QVariant ScanFoldersModel::data(const QModelIndex &index, int role) const
|
||
|
{
|
||
|
if (!index.isValid() || (index.row() >= rowCount()))
|
||
|
return QVariant();
|
||
|
|
||
|
const PathData *pathData = m_pathList.at(index.row());
|
||
9 years ago
|
QVariant value;
|
||
|
|
||
|
switch (index.column()) {
|
||
|
case PathColumn:
|
||
|
if (role == Qt::DisplayRole)
|
||
|
value = Utils::Fs::toNativePath(pathData->path);
|
||
|
break;
|
||
|
case DownloadAtTorrentColumn:
|
||
|
if (role == Qt::CheckStateRole)
|
||
|
value = pathData->downloadAtPath ? Qt::Checked : Qt::Unchecked;
|
||
|
break;
|
||
|
case DownloadPath:
|
||
|
if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::ToolTipRole)
|
||
|
value = Utils::Fs::toNativePath(pathData->downloadPath);
|
||
|
break;
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
return value;
|
||
10 years ago
|
}
|
||
|
|
||
|
QVariant ScanFoldersModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||
|
{
|
||
|
if ((orientation != Qt::Horizontal) || (role != Qt::DisplayRole) || (section < 0) || (section >= columnCount()))
|
||
|
return QVariant();
|
||
15 years ago
|
|
||
9 years ago
|
QVariant title;
|
||
|
|
||
|
switch (section) {
|
||
|
case PathColumn:
|
||
|
title = tr("Watched Folder");
|
||
|
break;
|
||
|
case DownloadAtTorrentColumn:
|
||
|
title = tr("Download here");
|
||
|
break;
|
||
|
case DownloadPath:
|
||
|
title = tr("Download path");
|
||
|
break;
|
||
|
}
|
||
11 years ago
|
|
||
9 years ago
|
return title;
|
||
15 years ago
|
}
|
||
|
|
||
10 years ago
|
Qt::ItemFlags ScanFoldersModel::flags(const QModelIndex &index) const
|
||
|
{
|
||
9 years ago
|
if (!index.isValid() || (index.row() >= rowCount()))
|
||
10 years ago
|
return QAbstractTableModel::flags(index);
|
||
15 years ago
|
|
||
9 years ago
|
const PathData *pathData = m_pathList.at(index.row());
|
||
|
Qt::ItemFlags flags;
|
||
|
|
||
|
switch (index.column()) {
|
||
|
case PathColumn:
|
||
|
flags = QAbstractTableModel::flags(index);
|
||
|
break;
|
||
|
case DownloadAtTorrentColumn:
|
||
|
flags = QAbstractTableModel::flags(index) | Qt::ItemIsUserCheckable;
|
||
|
break;
|
||
|
case DownloadPath:
|
||
|
if (pathData->downloadAtPath == false)
|
||
|
flags = QAbstractTableModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsEnabled;
|
||
|
else
|
||
|
flags = QAbstractTableModel::flags(index) ^ Qt::ItemIsEnabled; //dont edit DownloadPath if checked 'downloadAtPath'
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
return flags;
|
||
10 years ago
|
}
|
||
|
|
||
|
bool ScanFoldersModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||
|
{
|
||
9 years ago
|
if (!index.isValid() || (index.row() >= rowCount()) || (index.column() > DownloadPath))
|
||
10 years ago
|
return false;
|
||
|
|
||
9 years ago
|
bool success = true;
|
||
|
|
||
|
switch (index.column()) {
|
||
|
case PathColumn:
|
||
|
success = false;
|
||
|
break;
|
||
|
case DownloadAtTorrentColumn:
|
||
|
if (role == Qt::CheckStateRole) {
|
||
|
Q_ASSERT(index.column() == DownloadAtTorrentColumn);
|
||
|
m_pathList[index.row()]->downloadAtPath = (value.toInt() == Qt::Checked);
|
||
|
emit dataChanged(index, index);
|
||
|
success = true;
|
||
|
}
|
||
|
break;
|
||
|
case DownloadPath:
|
||
|
Q_ASSERT(index.column() == DownloadPath);
|
||
|
m_pathList[index.row()]->downloadPath = value.toString();
|
||
|
emit dataChanged(index, index);
|
||
|
success = true;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
return success;
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
ScanFoldersModel::PathStatus ScanFoldersModel::addPath(const QString &path, bool downloadAtPath, const QString &downloadPath)
|
||
10 years ago
|
{
|
||
|
QDir dir(path);
|
||
|
if (!dir.exists()) return DoesNotExist;
|
||
|
if (!dir.isReadable()) return CannotRead;
|
||
|
|
||
|
const QString &canonicalPath = dir.canonicalPath();
|
||
|
if (findPathData(canonicalPath) != -1) return AlreadyInList;
|
||
|
|
||
|
if (!m_fsWatcher) {
|
||
|
m_fsWatcher = new FileSystemWatcher(this);
|
||
10 years ago
|
connect(m_fsWatcher, SIGNAL(torrentsAdded(const QStringList &)), this, SLOT(addTorrentsToSession(const QStringList &)));
|
||
15 years ago
|
}
|
||
10 years ago
|
|
||
|
beginInsertRows(QModelIndex(), rowCount(), rowCount());
|
||
9 years ago
|
QString downloadToPath = downloadPath.isEmpty() ? path : downloadPath;
|
||
|
m_pathList << new PathData(canonicalPath, downloadAtPath, downloadToPath);
|
||
10 years ago
|
endInsertRows();
|
||
|
|
||
|
// Start scanning
|
||
|
m_fsWatcher->addPath(canonicalPath);
|
||
|
return Ok;
|
||
15 years ago
|
}
|
||
|
|
||
10 years ago
|
void ScanFoldersModel::removePath(int row)
|
||
|
{
|
||
|
Q_ASSERT((row >= 0) && (row < rowCount()));
|
||
|
beginRemoveRows(QModelIndex(), row, row);
|
||
|
m_fsWatcher->removePath(m_pathList.at(row)->path);
|
||
|
m_pathList.removeAt(row);
|
||
|
endRemoveRows();
|
||
15 years ago
|
}
|
||
|
|
||
10 years ago
|
bool ScanFoldersModel::removePath(const QString &path)
|
||
|
{
|
||
|
const int row = findPathData(path);
|
||
|
if (row == -1) return false;
|
||
15 years ago
|
|
||
10 years ago
|
removePath(row);
|
||
|
return true;
|
||
15 years ago
|
}
|
||
|
|
||
10 years ago
|
ScanFoldersModel::PathStatus ScanFoldersModel::setDownloadAtPath(int row, bool downloadAtPath)
|
||
|
{
|
||
|
Q_ASSERT((row >= 0) && (row < rowCount()));
|
||
|
|
||
|
bool &oldValue = m_pathList[row]->downloadAtPath;
|
||
|
if (oldValue != downloadAtPath) {
|
||
|
if (downloadAtPath) {
|
||
|
QTemporaryFile testFile(m_pathList[row]->path + "/tmpFile");
|
||
|
if (!testFile.open()) return CannotWrite;
|
||
|
}
|
||
|
|
||
|
oldValue = downloadAtPath;
|
||
|
const QModelIndex changedIndex = index(row, DownloadAtTorrentColumn);
|
||
|
emit dataChanged(changedIndex, changedIndex);
|
||
|
}
|
||
|
|
||
|
return Ok;
|
||
15 years ago
|
}
|
||
|
|
||
10 years ago
|
bool ScanFoldersModel::downloadInTorrentFolder(const QString &filePath) const
|
||
|
{
|
||
|
const int row = findPathData(QFileInfo(filePath).dir().path());
|
||
|
Q_ASSERT(row != -1);
|
||
|
return m_pathList.at(row)->downloadAtPath;
|
||
|
}
|
||
|
|
||
9 years ago
|
QString ScanFoldersModel::downloadPathTorrentFolder(const QString &filePath) const
|
||
|
{
|
||
|
const int row = findPathData(QFileInfo(filePath).dir().path());
|
||
|
Q_ASSERT(row != -1);
|
||
|
return m_pathList.at(row)->downloadPath;
|
||
|
}
|
||
|
|
||
10 years ago
|
int ScanFoldersModel::findPathData(const QString &path) const
|
||
|
{
|
||
|
for (int i = 0; i < m_pathList.count(); ++i)
|
||
10 years ago
|
if (m_pathList.at(i)->path == Utils::Fs::fromNativePath(path))
|
||
10 years ago
|
return i;
|
||
|
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
void ScanFoldersModel::makePersistent()
|
||
|
{
|
||
|
Preferences *const pref = Preferences::instance();
|
||
|
QStringList paths;
|
||
|
QList<bool> downloadInFolderInfo;
|
||
9 years ago
|
QStringList downloadPaths;
|
||
10 years ago
|
foreach (const PathData *pathData, m_pathList) {
|
||
|
paths << pathData->path;
|
||
|
downloadInFolderInfo << pathData->downloadAtPath;
|
||
9 years ago
|
downloadPaths << pathData->downloadPath;
|
||
10 years ago
|
}
|
||
|
|
||
|
pref->setScanDirs(paths);
|
||
|
pref->setDownloadInScanDirs(downloadInFolderInfo);
|
||
9 years ago
|
pref->setScanDirsDownloadPaths(downloadPaths);
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
void ScanFoldersModel::configure()
|
||
|
{
|
||
|
Preferences *const pref = Preferences::instance();
|
||
|
|
||
|
int i = 0;
|
||
9 years ago
|
QStringList downloadPaths = pref->getScanDirsDownloadPaths();
|
||
10 years ago
|
QList<bool> downloadInDirList = pref->getDownloadInScanDirs();
|
||
|
foreach (const QString &dir, pref->getScanDirs()) {
|
||
9 years ago
|
bool downloadInDir = downloadInDirList.value(i, true);
|
||
|
QString downloadPath = downloadPaths.value(i); //empty string if out-of-bounds
|
||
|
addPath(dir, downloadInDir, downloadPath);
|
||
10 years ago
|
++i;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void ScanFoldersModel::addTorrentsToSession(const QStringList &pathList)
|
||
|
{
|
||
|
foreach (const QString &file, pathList) {
|
||
|
qDebug("File %s added", qPrintable(file));
|
||
|
if (file.endsWith(".magnet")) {
|
||
|
QFile f(file);
|
||
|
if (f.open(QIODevice::ReadOnly)) {
|
||
|
BitTorrent::Session::instance()->addTorrent(QString::fromLocal8Bit(f.readAll()));
|
||
|
f.remove();
|
||
|
}
|
||
|
else {
|
||
|
qDebug("Failed to open magnet file: %s", qPrintable(f.errorString()));
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
BitTorrent::AddTorrentParams params;
|
||
|
if (downloadInTorrentFolder(file))
|
||
|
params.savePath = QFileInfo(file).dir().path();
|
||
9 years ago
|
else
|
||
|
params.savePath = downloadPathTorrentFolder(file); //if empty it will use the default savePath
|
||
10 years ago
|
|
||
|
BitTorrent::TorrentInfo torrentInfo = BitTorrent::TorrentInfo::loadFromFile(file);
|
||
|
if (torrentInfo.isValid()) {
|
||
|
BitTorrent::Session::instance()->addTorrent(torrentInfo, params);
|
||
10 years ago
|
Utils::Fs::forceRemove(file);
|
||
10 years ago
|
}
|
||
|
else {
|
||
|
qDebug("Ignoring incomplete torrent file: %s", qPrintable(file));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|