Add option to auto hide zero status filters
[qBittorrent.git] / src / gui / torrentcontentmodel.cpp
blob2644cb4557314c70ac04e89d7a495d5f07654b25
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2022-2023 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2006-2012 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "torrentcontentmodel.h"
32 #include <algorithm>
34 #include <QFileIconProvider>
35 #include <QFileInfo>
36 #include <QIcon>
37 #include <QPointer>
38 #include <QScopeGuard>
40 #if defined(Q_OS_WIN)
41 #include <Windows.h>
42 #include <Shellapi.h>
43 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
44 #include <QtWin>
45 #endif
46 #else
47 #include <QMimeDatabase>
48 #include <QMimeType>
49 #endif
51 #if defined Q_OS_WIN || defined Q_OS_MACOS
52 #define QBT_PIXMAP_CACHE_FOR_FILE_ICONS
53 #include <QPixmapCache>
54 #endif
56 #include "base/bittorrent/downloadpriority.h"
57 #include "base/bittorrent/torrentcontenthandler.h"
58 #include "base/exceptions.h"
59 #include "base/global.h"
60 #include "base/path.h"
61 #include "base/utils/fs.h"
62 #include "torrentcontentmodelfile.h"
63 #include "torrentcontentmodelfolder.h"
64 #include "torrentcontentmodelitem.h"
65 #include "uithememanager.h"
67 #ifdef Q_OS_MACOS
68 #include "macutilities.h"
69 #endif
71 namespace
73 class UnifiedFileIconProvider : public QFileIconProvider
75 public:
76 UnifiedFileIconProvider()
77 : m_textPlainIcon {UIThemeManager::instance()->getIcon(u"help-about"_qs, u"text-plain"_qs)}
81 using QFileIconProvider::icon;
83 QIcon icon(const QFileInfo &) const override
85 return m_textPlainIcon;
88 private:
89 QIcon m_textPlainIcon;
92 #ifdef QBT_PIXMAP_CACHE_FOR_FILE_ICONS
93 class CachingFileIconProvider : public UnifiedFileIconProvider
95 public:
96 using QFileIconProvider::icon;
98 QIcon icon(const QFileInfo &info) const final
100 const QString ext = info.suffix();
101 if (!ext.isEmpty())
103 QPixmap cached;
104 if (QPixmapCache::find(ext, &cached))
105 return {cached};
107 const QPixmap pixmap = pixmapForExtension(ext);
108 if (!pixmap.isNull())
110 QPixmapCache::insert(ext, pixmap);
111 return {pixmap};
114 return UnifiedFileIconProvider::icon(info);
117 protected:
118 virtual QPixmap pixmapForExtension(const QString &ext) const = 0;
120 #endif // QBT_PIXMAP_CACHE_FOR_FILE_ICONS
122 #if defined(Q_OS_WIN)
123 // See QTBUG-25319 for explanation why this is required
124 class WinShellFileIconProvider final : public CachingFileIconProvider
126 QPixmap pixmapForExtension(const QString &ext) const override
128 const std::wstring extWStr = QString(u'.' + ext).toStdWString();
130 SHFILEINFOW sfi {};
131 const HRESULT hr = ::SHGetFileInfoW(extWStr.c_str(),
132 FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(sfi), (SHGFI_ICON | SHGFI_USEFILEATTRIBUTES));
133 if (FAILED(hr))
134 return {};
136 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
137 const auto iconPixmap = QPixmap::fromImage(QImage::fromHICON(sfi.hIcon));
138 #else
139 const QPixmap iconPixmap = QtWin::fromHICON(sfi.hIcon);
140 #endif
141 ::DestroyIcon(sfi.hIcon);
142 return iconPixmap;
145 #elif defined(Q_OS_MACOS)
146 // There is a similar bug on macOS, to be reported to Qt
147 // https://github.com/qbittorrent/qBittorrent/pull/6156#issuecomment-316302615
148 class MacFileIconProvider final : public CachingFileIconProvider
150 QPixmap pixmapForExtension(const QString &ext) const override
152 return MacUtils::pixmapForExtension(ext, QSize(32, 32));
155 #else
157 * @brief Tests whether QFileIconProvider actually works
159 * Some QPA plugins do not implement QPlatformTheme::fileIcon(), and
160 * QFileIconProvider::icon() returns empty icons as the result. Here we ask it for
161 * two icons for probably absent files and when both icons are null, we assume that
162 * the current QPA plugin does not implement QPlatformTheme::fileIcon().
164 bool doesQFileIconProviderWork()
166 const Path PSEUDO_UNIQUE_FILE_NAME = Utils::Fs::tempPath() / Path(u"qBittorrent-test-QFileIconProvider-845eb448-7ad5-4cdb-b764-b3f322a266a9"_qs);
167 QFileIconProvider provider;
168 const QIcon testIcon1 = provider.icon(QFileInfo((PSEUDO_UNIQUE_FILE_NAME + u".pdf").data()));
169 const QIcon testIcon2 = provider.icon(QFileInfo((PSEUDO_UNIQUE_FILE_NAME + u".png").data()));
170 return (!testIcon1.isNull() || !testIcon2.isNull());
173 class MimeFileIconProvider final : public UnifiedFileIconProvider
175 using QFileIconProvider::icon;
177 QIcon icon(const QFileInfo &info) const override
179 const QMimeType mimeType = QMimeDatabase().mimeTypeForFile(info, QMimeDatabase::MatchExtension);
181 const auto mimeIcon = QIcon::fromTheme(mimeType.iconName());
182 if (!mimeIcon.isNull())
183 return mimeIcon;
185 const auto genericIcon = QIcon::fromTheme(mimeType.genericIconName());
186 if (!genericIcon.isNull())
187 return genericIcon;
189 return UnifiedFileIconProvider::icon(info);
192 #endif // Q_OS_WIN
195 TorrentContentModel::TorrentContentModel(QObject *parent)
196 : QAbstractItemModel(parent)
197 , m_rootItem(new TorrentContentModelFolder(QVector<QString>({ tr("Name"), tr("Total Size"), tr("Progress"), tr("Download Priority"), tr("Remaining"), tr("Availability") })))
198 #if defined(Q_OS_WIN)
199 , m_fileIconProvider {new WinShellFileIconProvider}
200 #elif defined(Q_OS_MACOS)
201 , m_fileIconProvider {new MacFileIconProvider}
202 #else
203 , m_fileIconProvider {doesQFileIconProviderWork() ? new QFileIconProvider : new MimeFileIconProvider}
204 #endif
206 m_fileIconProvider->setOptions(QFileIconProvider::DontUseCustomDirectoryIcons);
209 TorrentContentModel::~TorrentContentModel()
211 delete m_fileIconProvider;
212 delete m_rootItem;
215 void TorrentContentModel::updateFilesProgress()
217 Q_ASSERT(m_contentHandler && m_contentHandler->hasMetadata());
219 const QVector<qreal> &filesProgress = m_contentHandler->filesProgress();
220 Q_ASSERT(m_filesIndex.size() == filesProgress.size());
221 // XXX: Why is this necessary?
222 if (Q_UNLIKELY(m_filesIndex.size() != filesProgress.size()))
223 return;
225 for (int i = 0; i < filesProgress.size(); ++i)
226 m_filesIndex[i]->setProgress(filesProgress[i]);
227 // Update folders progress in the tree
228 m_rootItem->recalculateProgress();
229 m_rootItem->recalculateAvailability();
232 void TorrentContentModel::updateFilesPriorities()
234 Q_ASSERT(m_contentHandler && m_contentHandler->hasMetadata());
236 const QVector<BitTorrent::DownloadPriority> fprio = m_contentHandler->filePriorities();
237 Q_ASSERT(m_filesIndex.size() == fprio.size());
238 // XXX: Why is this necessary?
239 if (m_filesIndex.size() != fprio.size())
240 return;
242 for (int i = 0; i < fprio.size(); ++i)
243 m_filesIndex[i]->setPriority(static_cast<BitTorrent::DownloadPriority>(fprio[i]));
246 void TorrentContentModel::updateFilesAvailability()
248 Q_ASSERT(m_contentHandler && m_contentHandler->hasMetadata());
250 using HandlerPtr = QPointer<BitTorrent::TorrentContentHandler>;
251 m_contentHandler->fetchAvailableFileFractions([this, handler = HandlerPtr(m_contentHandler)](const QVector<qreal> &availableFileFractions)
253 if (handler != m_contentHandler)
254 return;
256 Q_ASSERT(m_filesIndex.size() == availableFileFractions.size());
257 // XXX: Why is this necessary?
258 if (Q_UNLIKELY(m_filesIndex.size() != availableFileFractions.size()))
259 return;
261 for (int i = 0; i < m_filesIndex.size(); ++i)
262 m_filesIndex[i]->setAvailability(availableFileFractions[i]);
263 // Update folders progress in the tree
264 m_rootItem->recalculateProgress();
268 bool TorrentContentModel::setItemPriority(const QModelIndex &index, BitTorrent::DownloadPriority priority)
270 Q_ASSERT(index.isValid());
272 auto *item = static_cast<TorrentContentModelItem *>(index.internalPointer());
273 const BitTorrent::DownloadPriority currentPriority = item->priority();
274 if (currentPriority == priority)
275 return false;
277 item->setPriority(priority);
278 m_contentHandler->prioritizeFiles(getFilePriorities());
280 // Update folders progress in the tree
281 m_rootItem->recalculateProgress();
282 m_rootItem->recalculateAvailability();
284 const QVector<ColumnInterval> columns =
286 {TorrentContentModelItem::COL_NAME, TorrentContentModelItem::COL_NAME},
287 {TorrentContentModelItem::COL_PRIO, TorrentContentModelItem::COL_PRIO}
289 notifySubtreeUpdated(index, columns);
291 return true;
294 QVector<BitTorrent::DownloadPriority> TorrentContentModel::getFilePriorities() const
296 QVector<BitTorrent::DownloadPriority> prio;
297 prio.reserve(m_filesIndex.size());
298 for (const TorrentContentModelFile *file : asConst(m_filesIndex))
299 prio.push_back(file->priority());
300 return prio;
303 int TorrentContentModel::columnCount(const QModelIndex &parent) const
305 Q_UNUSED(parent);
306 return TorrentContentModelItem::NB_COL;
309 bool TorrentContentModel::setData(const QModelIndex &index, const QVariant &value, const int role)
311 if (!index.isValid())
312 return false;
314 if ((index.column() == TorrentContentModelItem::COL_NAME) && (role == Qt::CheckStateRole))
316 const auto checkState = static_cast<Qt::CheckState>(value.toInt());
317 const BitTorrent::DownloadPriority newPrio = (checkState == Qt::PartiallyChecked)
318 ? BitTorrent::DownloadPriority::Mixed
319 : ((checkState == Qt::Unchecked)
320 ? BitTorrent::DownloadPriority::Ignored
321 : BitTorrent::DownloadPriority::Normal);
323 return setItemPriority(index, newPrio);
326 if (role == Qt::EditRole)
328 auto *item = static_cast<TorrentContentModelItem *>(index.internalPointer());
330 switch (index.column())
332 case TorrentContentModelItem::COL_NAME:
334 const QString currentName = item->name();
335 const QString newName = value.toString();
336 if (currentName != newName)
340 const Path parentPath = getItemPath(index.parent());
341 const Path oldPath = parentPath / Path(currentName);
342 const Path newPath = parentPath / Path(newName);
344 if (item->itemType() == TorrentContentModelItem::FileType)
345 m_contentHandler->renameFile(oldPath, newPath);
346 else
347 m_contentHandler->renameFolder(oldPath, newPath);
349 catch (const RuntimeError &error)
351 emit renameFailed(error.message());
352 return false;
355 item->setName(newName);
356 emit dataChanged(index, index);
357 return true;
360 break;
362 case TorrentContentModelItem::COL_PRIO:
364 const auto newPrio = static_cast<BitTorrent::DownloadPriority>(value.toInt());
365 return setItemPriority(index, newPrio);
367 break;
369 default:
370 break;
374 return false;
377 TorrentContentModelItem::ItemType TorrentContentModel::itemType(const QModelIndex &index) const
379 return static_cast<const TorrentContentModelItem *>(index.internalPointer())->itemType();
382 int TorrentContentModel::getFileIndex(const QModelIndex &index) const
384 auto *item = static_cast<TorrentContentModelItem *>(index.internalPointer());
385 if (item->itemType() == TorrentContentModelItem::FileType)
386 return static_cast<TorrentContentModelFile *>(item)->fileIndex();
388 return -1;
391 Path TorrentContentModel::getItemPath(const QModelIndex &index) const
393 Path path;
394 for (QModelIndex i = index; i.isValid(); i = i.parent())
395 path = Path(i.data().toString()) / path;
396 return path;
399 QVariant TorrentContentModel::data(const QModelIndex &index, const int role) const
401 if (!index.isValid())
402 return {};
404 auto *item = static_cast<TorrentContentModelItem *>(index.internalPointer());
406 switch (role)
408 case Qt::DecorationRole:
409 if (index.column() != TorrentContentModelItem::COL_NAME)
410 return {};
412 if (item->itemType() == TorrentContentModelItem::FolderType)
413 return m_fileIconProvider->icon(QFileIconProvider::Folder);
415 return m_fileIconProvider->icon(QFileInfo(item->name()));
417 case Qt::CheckStateRole:
418 if (index.column() != TorrentContentModelItem::COL_NAME)
419 return {};
421 if (item->priority() == BitTorrent::DownloadPriority::Ignored)
422 return Qt::Unchecked;
424 if (item->priority() == BitTorrent::DownloadPriority::Mixed)
426 Q_ASSERT(item->itemType() == TorrentContentModelItem::FolderType);
428 const auto *folder = static_cast<TorrentContentModelFolder *>(item);
429 const auto childItems = folder->children();
430 const bool hasIgnored = std::any_of(childItems.cbegin(), childItems.cend()
431 , [](const TorrentContentModelItem *childItem)
433 return (childItem->priority() == BitTorrent::DownloadPriority::Ignored);
436 return hasIgnored ? Qt::PartiallyChecked : Qt::Checked;
439 return Qt::Checked;
441 case Qt::TextAlignmentRole:
442 if ((index.column() == TorrentContentModelItem::COL_SIZE)
443 || (index.column() == TorrentContentModelItem::COL_REMAINING))
445 return QVariant {Qt::AlignRight | Qt::AlignVCenter};
448 return {};
450 case Qt::DisplayRole:
451 case Qt::ToolTipRole:
452 return item->displayData(index.column());
454 case Roles::UnderlyingDataRole:
455 return item->underlyingData(index.column());
457 default:
458 break;
461 return {};
464 Qt::ItemFlags TorrentContentModel::flags(const QModelIndex &index) const
466 if (!index.isValid())
467 return Qt::NoItemFlags;
469 Qt::ItemFlags flags {Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable};
470 if (itemType(index) == TorrentContentModelItem::FolderType)
471 flags |= Qt::ItemIsAutoTristate;
472 if (index.column() == TorrentContentModelItem::COL_PRIO)
473 flags |= Qt::ItemIsEditable;
475 return flags;
478 QVariant TorrentContentModel::headerData(int section, Qt::Orientation orientation, int role) const
480 if (orientation != Qt::Horizontal)
481 return {};
483 switch (role)
485 case Qt::DisplayRole:
486 return m_rootItem->displayData(section);
488 case Qt::TextAlignmentRole:
489 if ((section == TorrentContentModelItem::COL_SIZE)
490 || (section == TorrentContentModelItem::COL_REMAINING))
492 return QVariant {Qt::AlignRight | Qt::AlignVCenter};
495 return {};
497 default:
498 return {};
502 QModelIndex TorrentContentModel::index(const int row, const int column, const QModelIndex &parent) const
504 if (column >= columnCount())
505 return {};
507 const TorrentContentModelFolder *parentItem = parent.isValid()
508 ? static_cast<TorrentContentModelFolder *>(parent.internalPointer())
509 : m_rootItem;
510 Q_ASSERT(parentItem);
512 if (row >= parentItem->childCount())
513 return {};
515 TorrentContentModelItem *childItem = parentItem->child(row);
516 if (childItem)
517 return createIndex(row, column, childItem);
519 return {};
522 QModelIndex TorrentContentModel::parent(const QModelIndex &index) const
524 if (!index.isValid())
525 return {};
527 const auto *item = static_cast<TorrentContentModelItem *>(index.internalPointer());
528 if (!item)
529 return {};
531 TorrentContentModelItem *parentItem = item->parent();
532 if (parentItem == m_rootItem)
533 return {};
535 // From https://doc.qt.io/qt-6/qabstractitemmodel.html#parent:
536 // A common convention used in models that expose tree data structures is that only items
537 // in the first column have children. For that case, when reimplementing this function in
538 // a subclass the column of the returned QModelIndex would be 0.
539 return createIndex(parentItem->row(), 0, parentItem);
542 int TorrentContentModel::rowCount(const QModelIndex &parent) const
544 const TorrentContentModelFolder *parentItem = parent.isValid()
545 ? dynamic_cast<TorrentContentModelFolder *>(static_cast<TorrentContentModelItem *>(parent.internalPointer()))
546 : m_rootItem;
547 return parentItem ? parentItem->childCount() : 0;
550 void TorrentContentModel::populate()
552 Q_ASSERT(m_contentHandler && m_contentHandler->hasMetadata());
554 const int filesCount = m_contentHandler->filesCount();
555 m_filesIndex.reserve(filesCount);
557 QHash<TorrentContentModelFolder *, QHash<QString, TorrentContentModelFolder *>> folderMap;
558 QVector<QString> lastParentPath;
559 TorrentContentModelFolder *lastParent = m_rootItem;
560 // Iterate over files
561 for (int i = 0; i < filesCount; ++i)
563 const QString path = m_contentHandler->filePath(i).data();
565 // Iterate of parts of the path to create necessary folders
566 QList<QStringView> pathFolders = QStringView(path).split(u'/', Qt::SkipEmptyParts);
567 const QString fileName = pathFolders.takeLast().toString();
569 if (!std::equal(lastParentPath.begin(), lastParentPath.end()
570 , pathFolders.begin(), pathFolders.end()))
572 lastParentPath.clear();
573 lastParentPath.reserve(pathFolders.size());
575 // rebuild the path from the root
576 lastParent = m_rootItem;
577 for (const QStringView pathPart : asConst(pathFolders))
579 const QString folderName = pathPart.toString();
580 lastParentPath.push_back(folderName);
582 TorrentContentModelFolder *&newParent = folderMap[lastParent][folderName];
583 if (!newParent)
585 newParent = new TorrentContentModelFolder(folderName, lastParent);
586 lastParent->appendChild(newParent);
589 lastParent = newParent;
593 // Actually create the file
594 auto *fileItem = new TorrentContentModelFile(fileName, m_contentHandler->fileSize(i), lastParent, i);
595 lastParent->appendChild(fileItem);
596 m_filesIndex.push_back(fileItem);
599 updateFilesProgress();
600 updateFilesPriorities();
601 updateFilesAvailability();
604 void TorrentContentModel::setContentHandler(BitTorrent::TorrentContentHandler *contentHandler)
606 beginResetModel();
607 [[maybe_unused]] const auto modelResetGuard = qScopeGuard([this] { endResetModel(); });
609 if (m_contentHandler)
611 m_filesIndex.clear();
612 m_rootItem->deleteAllChildren();
615 m_contentHandler = contentHandler;
617 if (m_contentHandler && m_contentHandler->hasMetadata())
618 populate();
621 BitTorrent::TorrentContentHandler *TorrentContentModel::contentHandler() const
623 return m_contentHandler;
626 void TorrentContentModel::refresh()
628 if (!m_contentHandler || !m_contentHandler->hasMetadata())
629 return;
631 if (!m_filesIndex.isEmpty())
633 updateFilesProgress();
634 updateFilesPriorities();
635 updateFilesAvailability();
637 const QVector<ColumnInterval> columns =
639 {TorrentContentModelItem::COL_NAME, TorrentContentModelItem::COL_NAME},
640 {TorrentContentModelItem::COL_PROGRESS, TorrentContentModelItem::COL_PROGRESS},
641 {TorrentContentModelItem::COL_PRIO, TorrentContentModelItem::COL_PRIO},
642 {TorrentContentModelItem::COL_AVAILABILITY, TorrentContentModelItem::COL_AVAILABILITY}
644 notifySubtreeUpdated(index(0, 0), columns);
646 else
648 beginResetModel();
649 populate();
650 endResetModel();
654 void TorrentContentModel::notifySubtreeUpdated(const QModelIndex &index, const QVector<ColumnInterval> &columns)
656 // For best performance, `columns` entries should be arranged from left to right
658 Q_ASSERT(index.isValid());
660 // emit itself
661 for (const ColumnInterval &column : columns)
662 emit dataChanged(index.siblingAtColumn(column.first()), index.siblingAtColumn(column.last()));
664 // propagate up the model
665 QModelIndex parentIndex = parent(index);
666 while (parentIndex.isValid())
668 for (const ColumnInterval &column : columns)
669 emit dataChanged(parentIndex.siblingAtColumn(column.first()), parentIndex.siblingAtColumn(column.last()));
670 parentIndex = parent(parentIndex);
673 // propagate down the model
674 QVector<QModelIndex> parentIndexes;
676 if (hasChildren(index))
677 parentIndexes.push_back(index);
679 while (!parentIndexes.isEmpty())
681 const QModelIndex parent = parentIndexes.takeLast();
683 const int childCount = rowCount(parent);
684 const QModelIndex child = this->index(0, 0, parent);
686 // emit this generation
687 for (const ColumnInterval &column : columns)
689 const QModelIndex childTopLeft = child.siblingAtColumn(column.first());
690 const QModelIndex childBottomRight = child.sibling((childCount - 1), column.last());
691 emit dataChanged(childTopLeft, childBottomRight);
694 // check generations further down
695 parentIndexes.reserve(childCount);
696 for (int i = 0; i < childCount; ++i)
698 const QModelIndex sibling = child.siblingAtRow(i);
699 if (hasChildren(sibling))
700 parentIndexes.push_back(sibling);