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.

571 lines
19 KiB

/*
7 years ago
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2006-2012 Christophe Dumez <chris@qbittorrent.org>
*
* 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.
*/
7 years ago
#include "torrentcontentmodel.h"
#include <algorithm>
#include <QFileIconProvider>
#include <QFileInfo>
#include <QIcon>
#if defined(Q_OS_WIN)
#include <Windows.h>
#include <Shellapi.h>
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
#include <QtWin>
#endif
#else
#include <QMimeDatabase>
#include <QMimeType>
#endif
#if defined Q_OS_WIN || defined Q_OS_MACOS
#define QBT_PIXMAP_CACHE_FOR_FILE_ICONS
#include <QPixmapCache>
#endif
#include "base/bittorrent/abstractfilestorage.h"
#include "base/bittorrent/downloadpriority.h"
#include "base/global.h"
#include "base/path.h"
#include "base/utils/fs.h"
#include "torrentcontentmodelfile.h"
7 years ago
#include "torrentcontentmodelfolder.h"
#include "torrentcontentmodelitem.h"
#include "uithememanager.h"
7 years ago
#ifdef Q_OS_MACOS
#include "macutilities.h"
#endif
namespace
{
7 years ago
class UnifiedFileIconProvider : public QFileIconProvider
{
public:
UnifiedFileIconProvider()
: m_textPlainIcon {UIThemeManager::instance()->getIcon(u"help-about"_qs)}
{
}
using QFileIconProvider::icon;
QIcon icon(const QFileInfo &) const override
{
return m_textPlainIcon;
}
private:
QIcon m_textPlainIcon;
};
#ifdef QBT_PIXMAP_CACHE_FOR_FILE_ICONS
7 years ago
class CachingFileIconProvider : public UnifiedFileIconProvider
{
public:
using QFileIconProvider::icon;
QIcon icon(const QFileInfo &info) const final
{
const QString ext = info.suffix();
if (!ext.isEmpty())
{
QPixmap cached;
3 years ago
if (QPixmapCache::find(ext, &cached))
return {cached};
const QPixmap pixmap = pixmapForExtension(ext);
if (!pixmap.isNull())
{
QPixmapCache::insert(ext, pixmap);
return {pixmap};
}
}
return UnifiedFileIconProvider::icon(info);
}
protected:
virtual QPixmap pixmapForExtension(const QString &ext) const = 0;
};
7 years ago
#endif // QBT_PIXMAP_CACHE_FOR_FILE_ICONS
#if defined(Q_OS_WIN)
// See QTBUG-25319 for explanation why this is required
7 years ago
class WinShellFileIconProvider final : public CachingFileIconProvider
{
QPixmap pixmapForExtension(const QString &ext) const override
{
const std::wstring extWStr = QString(u'.' + ext).toStdWString();
3 years ago
SHFILEINFOW sfi {};
const HRESULT hr = ::SHGetFileInfoW(extWStr.c_str(),
FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(sfi), (SHGFI_ICON | SHGFI_USEFILEATTRIBUTES));
if (FAILED(hr))
return {};
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
3 years ago
const auto iconPixmap = QPixmap::fromImage(QImage::fromHICON(sfi.hIcon));
#else
3 years ago
const QPixmap iconPixmap = QtWin::fromHICON(sfi.hIcon);
#endif
::DestroyIcon(sfi.hIcon);
return iconPixmap;
}
};
#elif defined(Q_OS_MACOS)
// There is a similar bug on macOS, to be reported to Qt
// https://github.com/qbittorrent/qBittorrent/pull/6156#issuecomment-316302615
class MacFileIconProvider final : public CachingFileIconProvider
{
QPixmap pixmapForExtension(const QString &ext) const override
{
return MacUtils::pixmapForExtension(ext, QSize(32, 32));
}
};
#else
/**
* @brief Tests whether QFileIconProvider actually works
*
* Some QPA plugins do not implement QPlatformTheme::fileIcon(), and
* QFileIconProvider::icon() returns empty icons as the result. Here we ask it for
* two icons for probably absent files and when both icons are null, we assume that
* the current QPA plugin does not implement QPlatformTheme::fileIcon().
*/
bool doesQFileIconProviderWork()
{
const Path PSEUDO_UNIQUE_FILE_NAME = Utils::Fs::tempPath() / Path(u"qBittorrent-test-QFileIconProvider-845eb448-7ad5-4cdb-b764-b3f322a266a9"_qs);
QFileIconProvider provider;
const QIcon testIcon1 = provider.icon(QFileInfo((PSEUDO_UNIQUE_FILE_NAME + u".pdf").data()));
const QIcon testIcon2 = provider.icon(QFileInfo((PSEUDO_UNIQUE_FILE_NAME + u".png").data()));
return (!testIcon1.isNull() || !testIcon2.isNull());
}
3 years ago
class MimeFileIconProvider final : public UnifiedFileIconProvider
{
using QFileIconProvider::icon;
QIcon icon(const QFileInfo &info) const override
{
3 years ago
const QMimeType mimeType = QMimeDatabase().mimeTypeForFile(info, QMimeDatabase::MatchExtension);
3 years ago
const auto mimeIcon = QIcon::fromTheme(mimeType.iconName());
if (!mimeIcon.isNull())
return mimeIcon;
const auto genericIcon = QIcon::fromTheme(mimeType.genericIconName());
if (!genericIcon.isNull())
return genericIcon;
return UnifiedFileIconProvider::icon(info);
}
};
7 years ago
#endif // Q_OS_WIN
}
TorrentContentModel::TorrentContentModel(QObject *parent)
: QAbstractItemModel(parent)
, m_rootItem(new TorrentContentModelFolder(QVector<QString>({ tr("Name"), tr("Total Size"), tr("Progress"), tr("Download Priority"), tr("Remaining"), tr("Availability") })))
#if defined(Q_OS_WIN)
, m_fileIconProvider {new WinShellFileIconProvider}
#elif defined(Q_OS_MACOS)
, m_fileIconProvider {new MacFileIconProvider}
#else
, m_fileIconProvider {doesQFileIconProviderWork() ? new QFileIconProvider : new MimeFileIconProvider}
#endif
{
m_fileIconProvider->setOptions(QFileIconProvider::DontUseCustomDirectoryIcons);
}
TorrentContentModel::~TorrentContentModel()
{
delete m_fileIconProvider;
delete m_rootItem;
}
void TorrentContentModel::updateFilesProgress(const QVector<qreal> &fp)
{
Q_ASSERT(m_filesIndex.size() == fp.size());
// XXX: Why is this necessary?
if (m_filesIndex.size() != fp.size()) return;
emit layoutAboutToBeChanged();
for (int i = 0; i < fp.size(); ++i)
m_filesIndex[i]->setProgress(fp[i]);
// Update folders progress in the tree
m_rootItem->recalculateProgress();
m_rootItem->recalculateAvailability();
emit dataChanged(index(0, 0), index((rowCount() - 1), (columnCount() - 1)));
}
void TorrentContentModel::updateFilesPriorities(const QVector<BitTorrent::DownloadPriority> &fprio)
{
Q_ASSERT(m_filesIndex.size() == fprio.size());
// XXX: Why is this necessary?
if (m_filesIndex.size() != fprio.size())
return;
emit layoutAboutToBeChanged();
for (int i = 0; i < fprio.size(); ++i)
m_filesIndex[i]->setPriority(static_cast<BitTorrent::DownloadPriority>(fprio[i]));
emit dataChanged(index(0, 0), index((rowCount() - 1), (columnCount() - 1)));
}
void TorrentContentModel::updateFilesAvailability(const QVector<qreal> &fa)
{
Q_ASSERT(m_filesIndex.size() == fa.size());
// XXX: Why is this necessary?
if (m_filesIndex.size() != fa.size()) return;
emit layoutAboutToBeChanged();
5 years ago
for (int i = 0; i < m_filesIndex.size(); ++i)
m_filesIndex[i]->setAvailability(fa[i]);
// Update folders progress in the tree
m_rootItem->recalculateProgress();
emit dataChanged(index(0, 0), index((rowCount() - 1), (columnCount() - 1)));
}
QVector<BitTorrent::DownloadPriority> TorrentContentModel::getFilePriorities() const
{
QVector<BitTorrent::DownloadPriority> prio;
prio.reserve(m_filesIndex.size());
for (const TorrentContentModelFile *file : asConst(m_filesIndex))
prio.push_back(file->priority());
return prio;
}
bool TorrentContentModel::allFiltered() const
{
return std::all_of(m_filesIndex.cbegin(), m_filesIndex.cend(), [](const TorrentContentModelFile *fileItem)
{
return (fileItem->priority() == BitTorrent::DownloadPriority::Ignored);
});
}
7 years ago
int TorrentContentModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return static_cast<TorrentContentModelItem*>(parent.internalPointer())->columnCount();
return m_rootItem->columnCount();
}
7 years ago
bool TorrentContentModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid())
return false;
if ((index.column() == TorrentContentModelItem::COL_NAME) && (role == Qt::CheckStateRole))
{
auto *item = static_cast<TorrentContentModelItem*>(index.internalPointer());
qDebug("setData(%s, %d)", qUtf8Printable(item->name()), value.toInt());
BitTorrent::DownloadPriority prio = BitTorrent::DownloadPriority::Normal;
if (value.toInt() == Qt::PartiallyChecked)
prio = BitTorrent::DownloadPriority::Mixed;
else if (value.toInt() == Qt::Unchecked)
prio = BitTorrent::DownloadPriority::Ignored;
if (item->priority() != prio)
{
item->setPriority(prio);
// Update folders progress in the tree
m_rootItem->recalculateProgress();
m_rootItem->recalculateAvailability();
emit dataChanged(this->index(0, 0), this->index((rowCount() - 1), (columnCount() - 1)));
emit filteredFilesChanged();
}
return true;
}
if (role == Qt::EditRole)
{
Q_ASSERT(index.isValid());
auto *item = static_cast<TorrentContentModelItem*>(index.internalPointer());
switch (index.column())
{
case TorrentContentModelItem::COL_NAME:
item->setName(value.toString());
break;
case TorrentContentModelItem::COL_PRIO:
{
const BitTorrent::DownloadPriority previousPrio = item->priority();
const auto newPrio = static_cast<BitTorrent::DownloadPriority>(value.toInt());
item->setPriority(newPrio);
if ((newPrio != previousPrio) && ((newPrio == BitTorrent::DownloadPriority::Ignored)
|| (previousPrio == BitTorrent::DownloadPriority::Ignored)))
{
emit filteredFilesChanged();
}
}
break;
default:
return false;
}
emit dataChanged(index, index);
return true;
}
return false;
}
7 years ago
TorrentContentModelItem::ItemType TorrentContentModel::itemType(const QModelIndex &index) const
{
return static_cast<const TorrentContentModelItem*>(index.internalPointer())->itemType();
}
7 years ago
int TorrentContentModel::getFileIndex(const QModelIndex &index)
{
auto *item = static_cast<TorrentContentModelItem*>(index.internalPointer());
if (item->itemType() == TorrentContentModelItem::FileType)
return static_cast<TorrentContentModelFile*>(item)->fileIndex();
Q_ASSERT(item->itemType() == TorrentContentModelItem::FileType);
return -1;
}
QVariant TorrentContentModel::data(const QModelIndex &index, const int role) const
{
if (!index.isValid())
return {};
auto *item = static_cast<TorrentContentModelItem*>(index.internalPointer());
switch (role)
{
case Qt::DecorationRole:
{
if (index.column() != TorrentContentModelItem::COL_NAME)
return {};
if (item->itemType() == TorrentContentModelItem::FolderType)
return m_fileIconProvider->icon(QFileIconProvider::Folder);
return m_fileIconProvider->icon(QFileInfo(item->name()));
}
case Qt::CheckStateRole:
{
if (index.column() != TorrentContentModelItem::COL_NAME)
return {};
if (item->priority() == BitTorrent::DownloadPriority::Ignored)
return Qt::Unchecked;
if (item->priority() == BitTorrent::DownloadPriority::Mixed)
return Qt::PartiallyChecked;
return Qt::Checked;
}
case Qt::TextAlignmentRole:
if ((index.column() == TorrentContentModelItem::COL_SIZE)
|| (index.column() == TorrentContentModelItem::COL_REMAINING))
return QVariant {Qt::AlignRight | Qt::AlignVCenter};
return {};
case Qt::DisplayRole:
case Qt::ToolTipRole:
return item->displayData(index.column());
case Roles::UnderlyingDataRole:
return item->underlyingData(index.column());
default:
return {};
}
}
7 years ago
Qt::ItemFlags TorrentContentModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
Qt::ItemFlags flags {Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable};
if (itemType(index) == TorrentContentModelItem::FolderType)
flags |= Qt::ItemIsAutoTristate;
if (index.column() == TorrentContentModelItem::COL_PRIO)
flags |= Qt::ItemIsEditable;
return flags;
}
QVariant TorrentContentModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation != Qt::Horizontal)
return {};
switch (role)
{
case Qt::DisplayRole:
return m_rootItem->displayData(section);
case Qt::TextAlignmentRole:
if ((section == TorrentContentModelItem::COL_SIZE)
|| (section == TorrentContentModelItem::COL_REMAINING))
return QVariant {Qt::AlignRight | Qt::AlignVCenter};
return {};
default:
return {};
}
}
7 years ago
QModelIndex TorrentContentModel::index(int row, int column, const QModelIndex &parent) const
{
if (parent.isValid() && (parent.column() != 0))
return {};
if (column >= TorrentContentModelItem::NB_COL)
return {};
TorrentContentModelFolder *parentItem = nullptr;
if (!parent.isValid())
parentItem = m_rootItem;
else
parentItem = static_cast<TorrentContentModelFolder*>(parent.internalPointer());
Q_ASSERT(parentItem);
if (row >= parentItem->childCount())
return {};
7 years ago
TorrentContentModelItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
return {};
}
7 years ago
QModelIndex TorrentContentModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return {};
auto *childItem = static_cast<TorrentContentModelItem*>(index.internalPointer());
if (!childItem)
return {};
TorrentContentModelItem *parentItem = childItem->parent();
if (parentItem == m_rootItem)
return {};
return createIndex(parentItem->row(), 0, parentItem);
}
7 years ago
int TorrentContentModel::rowCount(const QModelIndex &parent) const
{
if (parent.column() > 0)
return 0;
TorrentContentModelFolder *parentItem = nullptr;
if (!parent.isValid())
parentItem = m_rootItem;
else
parentItem = dynamic_cast<TorrentContentModelFolder*>(static_cast<TorrentContentModelItem*>(parent.internalPointer()));
return parentItem ? parentItem->childCount() : 0;
}
void TorrentContentModel::clear()
{
qDebug("clear called");
beginResetModel();
m_filesIndex.clear();
m_rootItem->deleteAllChildren();
endResetModel();
}
void TorrentContentModel::setupModelData(const BitTorrent::AbstractFileStorage &info)
{
qDebug("setup model data called");
const int filesCount = info.filesCount();
if (filesCount <= 0)
return;
emit layoutAboutToBeChanged();
// Initialize files_index array
qDebug("Torrent contains %d files", filesCount);
m_filesIndex.reserve(filesCount);
QHash<TorrentContentModelFolder *, QHash<QString, TorrentContentModelFolder *>> folderMap;
QVector<QString> lastParentPath;
TorrentContentModelFolder *lastParent = m_rootItem;
// Iterate over files
for (int i = 0; i < filesCount; ++i)
{
const QString path = info.filePath(i).data();
// Iterate of parts of the path to create necessary folders
QList<QStringView> pathFolders = QStringView(path).split(u'/', Qt::SkipEmptyParts);
const QString fileName = pathFolders.takeLast().toString();
if (!std::equal(lastParentPath.begin(), lastParentPath.end()
, pathFolders.begin(), pathFolders.end()))
{
lastParentPath.clear();
lastParentPath.reserve(pathFolders.size());
// rebuild the path from the root
lastParent = m_rootItem;
for (const QStringView pathPart : asConst(pathFolders))
{
const QString folderName = pathPart.toString();
lastParentPath.push_back(folderName);
TorrentContentModelFolder *&newParent = folderMap[lastParent][folderName];
if (!newParent)
{
newParent = new TorrentContentModelFolder(folderName, lastParent);
lastParent->appendChild(newParent);
}
lastParent = newParent;
}
}
// Actually create the file
TorrentContentModelFile *fileItem = new TorrentContentModelFile(
fileName, info.fileSize(i), lastParent, i);
lastParent->appendChild(fileItem);
m_filesIndex.push_back(fileItem);
}
emit layoutChanged();
}
void TorrentContentModel::selectAll()
{
for (int i = 0; i < m_rootItem->childCount(); ++i)
{
TorrentContentModelItem* child = m_rootItem->child(i);
if (child->priority() == BitTorrent::DownloadPriority::Ignored)
child->setPriority(BitTorrent::DownloadPriority::Normal);
}
emit dataChanged(index(0, 0), index((rowCount() - 1), (columnCount() - 1)));
}
void TorrentContentModel::selectNone()
{
for (int i = 0; i < m_rootItem->childCount(); ++i)
m_rootItem->child(i)->setPriority(BitTorrent::DownloadPriority::Ignored);
emit dataChanged(index(0, 0), index((rowCount() - 1), (columnCount() - 1)));
}