Fix Enter key behavior when add new torrent
[qBittorrent.git] / src / gui / torrentcontentmodel.cpp
blob61c43cebc6a275333931e53c40968cb9868d2e52
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 #else
44 #include <QMimeDatabase>
45 #include <QMimeType>
46 #endif
48 #if defined Q_OS_WIN || defined Q_OS_MACOS
49 #define QBT_PIXMAP_CACHE_FOR_FILE_ICONS
50 #include <QPixmapCache>
51 #endif
53 #include "base/bittorrent/downloadpriority.h"
54 #include "base/bittorrent/torrentcontenthandler.h"
55 #include "base/exceptions.h"
56 #include "base/global.h"
57 #include "base/path.h"
58 #include "base/utils/fs.h"
59 #include "torrentcontentmodelfile.h"
60 #include "torrentcontentmodelfolder.h"
61 #include "torrentcontentmodelitem.h"
62 #include "uithememanager.h"
64 #ifdef Q_OS_MACOS
65 #include "macutilities.h"
66 #endif
68 namespace
70 class UnifiedFileIconProvider : public QFileIconProvider
72 public:
73 UnifiedFileIconProvider()
74 : m_textPlainIcon {UIThemeManager::instance()->getIcon(u"help-about"_s, u"text-plain"_s)}
78 using QFileIconProvider::icon;
80 QIcon icon(const QFileInfo &) const override
82 return m_textPlainIcon;
85 private:
86 QIcon m_textPlainIcon;
89 #ifdef QBT_PIXMAP_CACHE_FOR_FILE_ICONS
90 class CachingFileIconProvider : public UnifiedFileIconProvider
92 public:
93 using QFileIconProvider::icon;
95 QIcon icon(const QFileInfo &info) const final
97 const QString ext = info.suffix();
98 if (!ext.isEmpty())
100 QPixmap cached;
101 if (QPixmapCache::find(ext, &cached))
102 return {cached};
104 const QPixmap pixmap = pixmapForExtension(ext);
105 if (!pixmap.isNull())
107 QPixmapCache::insert(ext, pixmap);
108 return {pixmap};
111 return UnifiedFileIconProvider::icon(info);
114 protected:
115 virtual QPixmap pixmapForExtension(const QString &ext) const = 0;
117 #endif // QBT_PIXMAP_CACHE_FOR_FILE_ICONS
119 #if defined(Q_OS_WIN)
120 // See QTBUG-25319 for explanation why this is required
121 class WinShellFileIconProvider final : public CachingFileIconProvider
123 QPixmap pixmapForExtension(const QString &ext) const override
125 const std::wstring extWStr = QString(u'.' + ext).toStdWString();
127 SHFILEINFOW sfi {};
128 const HRESULT hr = ::SHGetFileInfoW(extWStr.c_str(),
129 FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(sfi), (SHGFI_ICON | SHGFI_USEFILEATTRIBUTES));
130 if (FAILED(hr))
131 return {};
133 const auto iconPixmap = QPixmap::fromImage(QImage::fromHICON(sfi.hIcon));
134 ::DestroyIcon(sfi.hIcon);
135 return iconPixmap;
138 #elif defined(Q_OS_MACOS)
139 // There is a similar bug on macOS, to be reported to Qt
140 // https://github.com/qbittorrent/qBittorrent/pull/6156#issuecomment-316302615
141 class MacFileIconProvider final : public CachingFileIconProvider
143 QPixmap pixmapForExtension(const QString &ext) const override
145 return MacUtils::pixmapForExtension(ext, QSize(32, 32));
148 #else
150 * @brief Tests whether QFileIconProvider actually works
152 * Some QPA plugins do not implement QPlatformTheme::fileIcon(), and
153 * QFileIconProvider::icon() returns empty icons as the result. Here we ask it for
154 * two icons for probably absent files and when both icons are null, we assume that
155 * the current QPA plugin does not implement QPlatformTheme::fileIcon().
157 bool doesQFileIconProviderWork()
159 const Path PSEUDO_UNIQUE_FILE_NAME = Utils::Fs::tempPath() / Path(u"qBittorrent-test-QFileIconProvider-845eb448-7ad5-4cdb-b764-b3f322a266a9"_s);
160 QFileIconProvider provider;
161 const QIcon testIcon1 = provider.icon(QFileInfo((PSEUDO_UNIQUE_FILE_NAME + u".pdf").data()));
162 const QIcon testIcon2 = provider.icon(QFileInfo((PSEUDO_UNIQUE_FILE_NAME + u".png").data()));
163 return (!testIcon1.isNull() || !testIcon2.isNull());
166 class MimeFileIconProvider final : public UnifiedFileIconProvider
168 using QFileIconProvider::icon;
170 QIcon icon(const QFileInfo &info) const override
172 const QMimeType mimeType = QMimeDatabase().mimeTypeForFile(info, QMimeDatabase::MatchExtension);
174 const auto mimeIcon = QIcon::fromTheme(mimeType.iconName());
175 if (!mimeIcon.isNull())
176 return mimeIcon;
178 const auto genericIcon = QIcon::fromTheme(mimeType.genericIconName());
179 if (!genericIcon.isNull())
180 return genericIcon;
182 return UnifiedFileIconProvider::icon(info);
185 #endif // Q_OS_WIN
188 TorrentContentModel::TorrentContentModel(QObject *parent)
189 : QAbstractItemModel(parent)
190 , m_rootItem(new TorrentContentModelFolder(QVector<QString>({ tr("Name"), tr("Total Size"), tr("Progress"), tr("Download Priority"), tr("Remaining"), tr("Availability") })))
191 #if defined(Q_OS_WIN)
192 , m_fileIconProvider {new WinShellFileIconProvider}
193 #elif defined(Q_OS_MACOS)
194 , m_fileIconProvider {new MacFileIconProvider}
195 #else
196 , m_fileIconProvider {doesQFileIconProviderWork() ? new QFileIconProvider : new MimeFileIconProvider}
197 #endif
199 m_fileIconProvider->setOptions(QFileIconProvider::DontUseCustomDirectoryIcons);
202 TorrentContentModel::~TorrentContentModel()
204 delete m_fileIconProvider;
205 delete m_rootItem;
208 void TorrentContentModel::updateFilesProgress()
210 Q_ASSERT(m_contentHandler && m_contentHandler->hasMetadata());
212 const QVector<qreal> &filesProgress = m_contentHandler->filesProgress();
213 Q_ASSERT(m_filesIndex.size() == filesProgress.size());
214 // XXX: Why is this necessary?
215 if (m_filesIndex.size() != filesProgress.size()) [[unlikely]]
216 return;
218 for (int i = 0; i < filesProgress.size(); ++i)
219 m_filesIndex[i]->setProgress(filesProgress[i]);
220 // Update folders progress in the tree
221 m_rootItem->recalculateProgress();
222 m_rootItem->recalculateAvailability();
225 void TorrentContentModel::updateFilesPriorities()
227 Q_ASSERT(m_contentHandler && m_contentHandler->hasMetadata());
229 const QVector<BitTorrent::DownloadPriority> fprio = m_contentHandler->filePriorities();
230 Q_ASSERT(m_filesIndex.size() == fprio.size());
231 // XXX: Why is this necessary?
232 if (m_filesIndex.size() != fprio.size())
233 return;
235 for (int i = 0; i < fprio.size(); ++i)
236 m_filesIndex[i]->setPriority(static_cast<BitTorrent::DownloadPriority>(fprio[i]));
239 void TorrentContentModel::updateFilesAvailability()
241 Q_ASSERT(m_contentHandler && m_contentHandler->hasMetadata());
243 using HandlerPtr = QPointer<BitTorrent::TorrentContentHandler>;
244 m_contentHandler->fetchAvailableFileFractions([this, handler = HandlerPtr(m_contentHandler)](const QVector<qreal> &availableFileFractions)
246 if (handler != m_contentHandler)
247 return;
249 Q_ASSERT(m_filesIndex.size() == availableFileFractions.size());
250 // XXX: Why is this necessary?
251 if (m_filesIndex.size() != availableFileFractions.size()) [[unlikely]]
252 return;
254 for (int i = 0; i < m_filesIndex.size(); ++i)
255 m_filesIndex[i]->setAvailability(availableFileFractions[i]);
256 // Update folders progress in the tree
257 m_rootItem->recalculateProgress();
261 bool TorrentContentModel::setItemPriority(const QModelIndex &index, BitTorrent::DownloadPriority priority)
263 Q_ASSERT(index.isValid());
265 auto *item = static_cast<TorrentContentModelItem *>(index.internalPointer());
266 const BitTorrent::DownloadPriority currentPriority = item->priority();
267 if (currentPriority == priority)
268 return false;
270 item->setPriority(priority);
271 m_contentHandler->prioritizeFiles(getFilePriorities());
273 // Update folders progress in the tree
274 m_rootItem->recalculateProgress();
275 m_rootItem->recalculateAvailability();
277 const QVector<ColumnInterval> columns =
279 {TorrentContentModelItem::COL_NAME, TorrentContentModelItem::COL_NAME},
280 {TorrentContentModelItem::COL_PRIO, TorrentContentModelItem::COL_PRIO}
282 notifySubtreeUpdated(index, columns);
284 return true;
287 QVector<BitTorrent::DownloadPriority> TorrentContentModel::getFilePriorities() const
289 QVector<BitTorrent::DownloadPriority> prio;
290 prio.reserve(m_filesIndex.size());
291 for (const TorrentContentModelFile *file : asConst(m_filesIndex))
292 prio.push_back(file->priority());
293 return prio;
296 int TorrentContentModel::columnCount([[maybe_unused]] const QModelIndex &parent) const
298 return TorrentContentModelItem::NB_COL;
301 bool TorrentContentModel::setData(const QModelIndex &index, const QVariant &value, const int role)
303 if (!index.isValid())
304 return false;
306 if ((index.column() == TorrentContentModelItem::COL_NAME) && (role == Qt::CheckStateRole))
308 const auto checkState = static_cast<Qt::CheckState>(value.toInt());
309 const BitTorrent::DownloadPriority newPrio = (checkState == Qt::PartiallyChecked)
310 ? BitTorrent::DownloadPriority::Mixed
311 : ((checkState == Qt::Unchecked)
312 ? BitTorrent::DownloadPriority::Ignored
313 : BitTorrent::DownloadPriority::Normal);
315 return setItemPriority(index, newPrio);
318 if (role == Qt::EditRole)
320 auto *item = static_cast<TorrentContentModelItem *>(index.internalPointer());
322 switch (index.column())
324 case TorrentContentModelItem::COL_NAME:
326 const QString currentName = item->name();
327 const QString newName = value.toString();
328 if (currentName != newName)
332 const Path parentPath = getItemPath(index.parent());
333 const Path oldPath = parentPath / Path(currentName);
334 const Path newPath = parentPath / Path(newName);
336 if (item->itemType() == TorrentContentModelItem::FileType)
337 m_contentHandler->renameFile(oldPath, newPath);
338 else
339 m_contentHandler->renameFolder(oldPath, newPath);
341 catch (const RuntimeError &error)
343 emit renameFailed(error.message());
344 return false;
347 item->setName(newName);
348 emit dataChanged(index, index);
349 return true;
352 break;
354 case TorrentContentModelItem::COL_PRIO:
356 const auto newPrio = static_cast<BitTorrent::DownloadPriority>(value.toInt());
357 return setItemPriority(index, newPrio);
359 break;
361 default:
362 break;
366 return false;
369 TorrentContentModelItem::ItemType TorrentContentModel::itemType(const QModelIndex &index) const
371 return static_cast<const TorrentContentModelItem *>(index.internalPointer())->itemType();
374 int TorrentContentModel::getFileIndex(const QModelIndex &index) const
376 auto *item = static_cast<TorrentContentModelItem *>(index.internalPointer());
377 if (item->itemType() == TorrentContentModelItem::FileType)
378 return static_cast<TorrentContentModelFile *>(item)->fileIndex();
380 return -1;
383 Path TorrentContentModel::getItemPath(const QModelIndex &index) const
385 Path path;
386 for (QModelIndex i = index; i.isValid(); i = i.parent())
387 path = Path(i.data().toString()) / path;
388 return path;
391 QVariant TorrentContentModel::data(const QModelIndex &index, const int role) const
393 if (!index.isValid())
394 return {};
396 auto *item = static_cast<TorrentContentModelItem *>(index.internalPointer());
398 switch (role)
400 case Qt::DecorationRole:
401 if (index.column() != TorrentContentModelItem::COL_NAME)
402 return {};
404 if (item->itemType() == TorrentContentModelItem::FolderType)
405 return m_fileIconProvider->icon(QFileIconProvider::Folder);
407 return m_fileIconProvider->icon(QFileInfo(item->name()));
409 case Qt::CheckStateRole:
410 if (index.column() != TorrentContentModelItem::COL_NAME)
411 return {};
413 if (item->priority() == BitTorrent::DownloadPriority::Ignored)
414 return Qt::Unchecked;
416 if (item->priority() == BitTorrent::DownloadPriority::Mixed)
418 Q_ASSERT(item->itemType() == TorrentContentModelItem::FolderType);
420 const auto *folder = static_cast<TorrentContentModelFolder *>(item);
421 const auto childItems = folder->children();
422 const bool hasIgnored = std::any_of(childItems.cbegin(), childItems.cend()
423 , [](const TorrentContentModelItem *childItem)
425 return (childItem->priority() == BitTorrent::DownloadPriority::Ignored);
428 return hasIgnored ? Qt::PartiallyChecked : Qt::Checked;
431 return Qt::Checked;
433 case Qt::TextAlignmentRole:
434 if ((index.column() == TorrentContentModelItem::COL_SIZE)
435 || (index.column() == TorrentContentModelItem::COL_REMAINING))
437 return QVariant {Qt::AlignRight | Qt::AlignVCenter};
440 return {};
442 case Qt::DisplayRole:
443 case Qt::ToolTipRole:
444 return item->displayData(index.column());
446 case Roles::UnderlyingDataRole:
447 return item->underlyingData(index.column());
449 default:
450 break;
453 return {};
456 Qt::ItemFlags TorrentContentModel::flags(const QModelIndex &index) const
458 if (!index.isValid())
459 return Qt::NoItemFlags;
461 Qt::ItemFlags flags {Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable};
462 if (itemType(index) == TorrentContentModelItem::FolderType)
463 flags |= Qt::ItemIsAutoTristate;
464 if (index.column() == TorrentContentModelItem::COL_PRIO)
465 flags |= Qt::ItemIsEditable;
467 return flags;
470 QVariant TorrentContentModel::headerData(int section, Qt::Orientation orientation, int role) const
472 if (orientation != Qt::Horizontal)
473 return {};
475 switch (role)
477 case Qt::DisplayRole:
478 return m_rootItem->displayData(section);
480 case Qt::TextAlignmentRole:
481 if ((section == TorrentContentModelItem::COL_SIZE)
482 || (section == TorrentContentModelItem::COL_REMAINING))
484 return QVariant {Qt::AlignRight | Qt::AlignVCenter};
487 return {};
489 default:
490 return {};
494 QModelIndex TorrentContentModel::index(const int row, const int column, const QModelIndex &parent) const
496 if (column >= columnCount())
497 return {};
499 const TorrentContentModelFolder *parentItem = parent.isValid()
500 ? static_cast<TorrentContentModelFolder *>(parent.internalPointer())
501 : m_rootItem;
502 Q_ASSERT(parentItem);
504 if (row >= parentItem->childCount())
505 return {};
507 TorrentContentModelItem *childItem = parentItem->child(row);
508 if (childItem)
509 return createIndex(row, column, childItem);
511 return {};
514 QModelIndex TorrentContentModel::parent(const QModelIndex &index) const
516 if (!index.isValid())
517 return {};
519 const auto *item = static_cast<TorrentContentModelItem *>(index.internalPointer());
520 if (!item)
521 return {};
523 TorrentContentModelItem *parentItem = item->parent();
524 if (parentItem == m_rootItem)
525 return {};
527 // From https://doc.qt.io/qt-6/qabstractitemmodel.html#parent:
528 // A common convention used in models that expose tree data structures is that only items
529 // in the first column have children. For that case, when reimplementing this function in
530 // a subclass the column of the returned QModelIndex would be 0.
531 return createIndex(parentItem->row(), 0, parentItem);
534 int TorrentContentModel::rowCount(const QModelIndex &parent) const
536 const TorrentContentModelFolder *parentItem = parent.isValid()
537 ? dynamic_cast<TorrentContentModelFolder *>(static_cast<TorrentContentModelItem *>(parent.internalPointer()))
538 : m_rootItem;
539 return parentItem ? parentItem->childCount() : 0;
542 void TorrentContentModel::populate()
544 Q_ASSERT(m_contentHandler && m_contentHandler->hasMetadata());
546 const int filesCount = m_contentHandler->filesCount();
547 m_filesIndex.reserve(filesCount);
549 QHash<TorrentContentModelFolder *, QHash<QString, TorrentContentModelFolder *>> folderMap;
550 QVector<QString> lastParentPath;
551 TorrentContentModelFolder *lastParent = m_rootItem;
552 // Iterate over files
553 for (int i = 0; i < filesCount; ++i)
555 const QString path = m_contentHandler->filePath(i).data();
557 // Iterate of parts of the path to create necessary folders
558 QList<QStringView> pathFolders = QStringView(path).split(u'/', Qt::SkipEmptyParts);
559 const QString fileName = pathFolders.takeLast().toString();
561 if (!std::equal(lastParentPath.begin(), lastParentPath.end()
562 , pathFolders.begin(), pathFolders.end()))
564 lastParentPath.clear();
565 lastParentPath.reserve(pathFolders.size());
567 // rebuild the path from the root
568 lastParent = m_rootItem;
569 for (const QStringView pathPart : asConst(pathFolders))
571 const QString folderName = pathPart.toString();
572 lastParentPath.push_back(folderName);
574 TorrentContentModelFolder *&newParent = folderMap[lastParent][folderName];
575 if (!newParent)
577 newParent = new TorrentContentModelFolder(folderName, lastParent);
578 lastParent->appendChild(newParent);
581 lastParent = newParent;
585 // Actually create the file
586 auto *fileItem = new TorrentContentModelFile(fileName, m_contentHandler->fileSize(i), lastParent, i);
587 lastParent->appendChild(fileItem);
588 m_filesIndex.push_back(fileItem);
591 updateFilesProgress();
592 updateFilesPriorities();
593 updateFilesAvailability();
596 void TorrentContentModel::setContentHandler(BitTorrent::TorrentContentHandler *contentHandler)
598 beginResetModel();
599 [[maybe_unused]] const auto modelResetGuard = qScopeGuard([this] { endResetModel(); });
601 if (m_contentHandler)
603 m_filesIndex.clear();
604 m_rootItem->deleteAllChildren();
607 m_contentHandler = contentHandler;
609 if (m_contentHandler && m_contentHandler->hasMetadata())
610 populate();
613 BitTorrent::TorrentContentHandler *TorrentContentModel::contentHandler() const
615 return m_contentHandler;
618 void TorrentContentModel::refresh()
620 if (!m_contentHandler || !m_contentHandler->hasMetadata())
621 return;
623 if (!m_filesIndex.isEmpty())
625 updateFilesProgress();
626 updateFilesPriorities();
627 updateFilesAvailability();
629 const QVector<ColumnInterval> columns =
631 {TorrentContentModelItem::COL_NAME, TorrentContentModelItem::COL_NAME},
632 {TorrentContentModelItem::COL_PROGRESS, TorrentContentModelItem::COL_PROGRESS},
633 {TorrentContentModelItem::COL_PRIO, TorrentContentModelItem::COL_PRIO},
634 {TorrentContentModelItem::COL_AVAILABILITY, TorrentContentModelItem::COL_AVAILABILITY}
636 notifySubtreeUpdated(index(0, 0), columns);
638 else
640 beginResetModel();
641 populate();
642 endResetModel();
646 void TorrentContentModel::notifySubtreeUpdated(const QModelIndex &index, const QVector<ColumnInterval> &columns)
648 // For best performance, `columns` entries should be arranged from left to right
650 Q_ASSERT(index.isValid());
652 // emit itself
653 for (const ColumnInterval &column : columns)
654 emit dataChanged(index.siblingAtColumn(column.first()), index.siblingAtColumn(column.last()));
656 // propagate up the model
657 QModelIndex parentIndex = parent(index);
658 while (parentIndex.isValid())
660 for (const ColumnInterval &column : columns)
661 emit dataChanged(parentIndex.siblingAtColumn(column.first()), parentIndex.siblingAtColumn(column.last()));
662 parentIndex = parent(parentIndex);
665 // propagate down the model
666 QVector<QModelIndex> parentIndexes;
668 if (hasChildren(index))
669 parentIndexes.push_back(index);
671 while (!parentIndexes.isEmpty())
673 const QModelIndex parent = parentIndexes.takeLast();
675 const int childCount = rowCount(parent);
676 const QModelIndex child = this->index(0, 0, parent);
678 // emit this generation
679 for (const ColumnInterval &column : columns)
681 const QModelIndex childTopLeft = child.siblingAtColumn(column.first());
682 const QModelIndex childBottomRight = child.sibling((childCount - 1), column.last());
683 emit dataChanged(childTopLeft, childBottomRight);
686 // check generations further down
687 parentIndexes.reserve(childCount);
688 for (int i = 0; i < childCount; ++i)
690 const QModelIndex sibling = child.siblingAtRow(i);
691 if (hasChildren(sibling))
692 parentIndexes.push_back(sibling);