2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2022-2024 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"
34 #include <QFileIconProvider>
38 #include <QScopeGuard>
40 #if defined(Q_OS_MACOS)
41 #define QBT_PIXMAP_CACHE_FOR_FILE_ICONS
42 #include <QPixmapCache>
43 #elif !defined(Q_OS_WIN)
44 #include <QMimeDatabase>
48 #include "base/bittorrent/downloadpriority.h"
49 #include "base/bittorrent/torrentcontenthandler.h"
50 #include "base/exceptions.h"
51 #include "base/global.h"
52 #include "base/path.h"
53 #include "base/utils/fs.h"
54 #include "torrentcontentmodelfile.h"
55 #include "torrentcontentmodelfolder.h"
56 #include "torrentcontentmodelitem.h"
57 #include "uithememanager.h"
60 #include "macutilities.h"
65 class UnifiedFileIconProvider
: public QFileIconProvider
68 UnifiedFileIconProvider()
69 : m_textPlainIcon
{UIThemeManager::instance()->getIcon(u
"help-about"_s
, u
"text-plain"_s
)}
73 using QFileIconProvider::icon
;
75 QIcon
icon(const QFileInfo
&) const override
77 return m_textPlainIcon
;
81 QIcon m_textPlainIcon
;
84 #ifdef QBT_PIXMAP_CACHE_FOR_FILE_ICONS
85 class CachingFileIconProvider
: public UnifiedFileIconProvider
88 using QFileIconProvider::icon
;
90 QIcon
icon(const QFileInfo
&info
) const final
92 const QString ext
= info
.suffix();
96 if (QPixmapCache::find(ext
, &cached
))
99 const QPixmap pixmap
= pixmapForExtension(ext
);
100 if (!pixmap
.isNull())
102 QPixmapCache::insert(ext
, pixmap
);
106 return UnifiedFileIconProvider::icon(info
);
110 virtual QPixmap
pixmapForExtension(const QString
&ext
) const = 0;
112 #endif // QBT_PIXMAP_CACHE_FOR_FILE_ICONS
114 #if defined(Q_OS_MACOS)
115 // There is a bug on macOS, to be reported to Qt
116 // https://github.com/qbittorrent/qBittorrent/pull/6156#issuecomment-316302615
117 class MacFileIconProvider final
: public CachingFileIconProvider
119 QPixmap
pixmapForExtension(const QString
&ext
) const override
121 return MacUtils::pixmapForExtension(ext
, QSize(32, 32));
124 #elif !defined(Q_OS_WIN)
126 * @brief Tests whether QFileIconProvider actually works
128 * Some QPA plugins do not implement QPlatformTheme::fileIcon(), and
129 * QFileIconProvider::icon() returns empty icons as the result. Here we ask it for
130 * two icons for probably absent files and when both icons are null, we assume that
131 * the current QPA plugin does not implement QPlatformTheme::fileIcon().
133 bool doesQFileIconProviderWork()
135 const Path PSEUDO_UNIQUE_FILE_NAME
= Utils::Fs::tempPath() / Path(u
"qBittorrent-test-QFileIconProvider-845eb448-7ad5-4cdb-b764-b3f322a266a9"_s
);
136 QFileIconProvider provider
;
137 const QIcon testIcon1
= provider
.icon(QFileInfo((PSEUDO_UNIQUE_FILE_NAME
+ u
".pdf").data()));
138 const QIcon testIcon2
= provider
.icon(QFileInfo((PSEUDO_UNIQUE_FILE_NAME
+ u
".png").data()));
139 return (!testIcon1
.isNull() || !testIcon2
.isNull());
142 class MimeFileIconProvider final
: public UnifiedFileIconProvider
144 using QFileIconProvider::icon
;
146 QIcon
icon(const QFileInfo
&info
) const override
148 const QMimeType mimeType
= QMimeDatabase().mimeTypeForFile(info
, QMimeDatabase::MatchExtension
);
150 const auto mimeIcon
= QIcon::fromTheme(mimeType
.iconName());
151 if (!mimeIcon
.isNull())
154 const auto genericIcon
= QIcon::fromTheme(mimeType
.genericIconName());
155 if (!genericIcon
.isNull())
158 return UnifiedFileIconProvider::icon(info
);
164 TorrentContentModel::TorrentContentModel(QObject
*parent
)
165 : QAbstractItemModel(parent
)
166 , m_rootItem(new TorrentContentModelFolder(QList
<QString
>({ tr("Name"), tr("Total Size"), tr("Progress"), tr("Download Priority"), tr("Remaining"), tr("Availability") })))
167 #if defined(Q_OS_WIN)
168 , m_fileIconProvider
{new QFileIconProvider
}
169 #elif defined(Q_OS_MACOS)
170 , m_fileIconProvider
{new MacFileIconProvider
}
172 , m_fileIconProvider
{doesQFileIconProviderWork() ? new QFileIconProvider
: new MimeFileIconProvider
}
175 m_fileIconProvider
->setOptions(QFileIconProvider::DontUseCustomDirectoryIcons
);
178 TorrentContentModel::~TorrentContentModel()
180 delete m_fileIconProvider
;
184 void TorrentContentModel::updateFilesProgress()
186 Q_ASSERT(m_contentHandler
&& m_contentHandler
->hasMetadata());
188 const QList
<qreal
> &filesProgress
= m_contentHandler
->filesProgress();
189 Q_ASSERT(m_filesIndex
.size() == filesProgress
.size());
190 // XXX: Why is this necessary?
191 if (m_filesIndex
.size() != filesProgress
.size()) [[unlikely
]]
194 for (int i
= 0; i
< filesProgress
.size(); ++i
)
195 m_filesIndex
[i
]->setProgress(filesProgress
[i
]);
196 // Update folders progress in the tree
197 m_rootItem
->recalculateProgress();
198 m_rootItem
->recalculateAvailability();
201 void TorrentContentModel::updateFilesPriorities()
203 Q_ASSERT(m_contentHandler
&& m_contentHandler
->hasMetadata());
205 const QList
<BitTorrent::DownloadPriority
> fprio
= m_contentHandler
->filePriorities();
206 Q_ASSERT(m_filesIndex
.size() == fprio
.size());
207 // XXX: Why is this necessary?
208 if (m_filesIndex
.size() != fprio
.size())
211 for (int i
= 0; i
< fprio
.size(); ++i
)
212 m_filesIndex
[i
]->setPriority(static_cast<BitTorrent::DownloadPriority
>(fprio
[i
]));
215 void TorrentContentModel::updateFilesAvailability()
217 Q_ASSERT(m_contentHandler
&& m_contentHandler
->hasMetadata());
219 using HandlerPtr
= QPointer
<BitTorrent::TorrentContentHandler
>;
220 m_contentHandler
->fetchAvailableFileFractions([this, handler
= HandlerPtr(m_contentHandler
)](const QList
<qreal
> &availableFileFractions
)
222 if (handler
!= m_contentHandler
)
225 Q_ASSERT(m_filesIndex
.size() == availableFileFractions
.size());
226 // XXX: Why is this necessary?
227 if (m_filesIndex
.size() != availableFileFractions
.size()) [[unlikely
]]
230 for (int i
= 0; i
< m_filesIndex
.size(); ++i
)
231 m_filesIndex
[i
]->setAvailability(availableFileFractions
[i
]);
232 // Update folders progress in the tree
233 m_rootItem
->recalculateProgress();
237 bool TorrentContentModel::setItemPriority(const QModelIndex
&index
, BitTorrent::DownloadPriority priority
)
239 Q_ASSERT(index
.isValid());
241 auto *item
= static_cast<TorrentContentModelItem
*>(index
.internalPointer());
242 const BitTorrent::DownloadPriority currentPriority
= item
->priority();
243 if (currentPriority
== priority
)
246 item
->setPriority(priority
);
247 m_contentHandler
->prioritizeFiles(getFilePriorities());
249 // Update folders progress in the tree
250 m_rootItem
->recalculateProgress();
251 m_rootItem
->recalculateAvailability();
253 const QList
<ColumnInterval
> columns
=
255 {TorrentContentModelItem::COL_NAME
, TorrentContentModelItem::COL_NAME
},
256 {TorrentContentModelItem::COL_PRIO
, TorrentContentModelItem::COL_PRIO
}
258 notifySubtreeUpdated(index
, columns
);
263 QList
<BitTorrent::DownloadPriority
> TorrentContentModel::getFilePriorities() const
265 QList
<BitTorrent::DownloadPriority
> prio
;
266 prio
.reserve(m_filesIndex
.size());
267 for (const TorrentContentModelFile
*file
: asConst(m_filesIndex
))
268 prio
.push_back(file
->priority());
272 int TorrentContentModel::columnCount([[maybe_unused
]] const QModelIndex
&parent
) const
274 return TorrentContentModelItem::NB_COL
;
277 bool TorrentContentModel::setData(const QModelIndex
&index
, const QVariant
&value
, const int role
)
279 if (!index
.isValid())
282 if ((index
.column() == TorrentContentModelItem::COL_NAME
) && (role
== Qt::CheckStateRole
))
284 const auto checkState
= static_cast<Qt::CheckState
>(value
.toInt());
285 const BitTorrent::DownloadPriority newPrio
= (checkState
== Qt::PartiallyChecked
)
286 ? BitTorrent::DownloadPriority::Mixed
287 : ((checkState
== Qt::Unchecked
)
288 ? BitTorrent::DownloadPriority::Ignored
289 : BitTorrent::DownloadPriority::Normal
);
291 return setItemPriority(index
, newPrio
);
294 if (role
== Qt::EditRole
)
296 auto *item
= static_cast<TorrentContentModelItem
*>(index
.internalPointer());
298 switch (index
.column())
300 case TorrentContentModelItem::COL_NAME
:
302 const QString currentName
= item
->name();
303 const QString newName
= value
.toString();
304 if (currentName
!= newName
)
308 const Path parentPath
= getItemPath(index
.parent());
309 const Path oldPath
= parentPath
/ Path(currentName
);
310 const Path newPath
= parentPath
/ Path(newName
);
312 if (item
->itemType() == TorrentContentModelItem::FileType
)
313 m_contentHandler
->renameFile(oldPath
, newPath
);
315 m_contentHandler
->renameFolder(oldPath
, newPath
);
317 catch (const RuntimeError
&error
)
319 emit
renameFailed(error
.message());
323 item
->setName(newName
);
324 emit
dataChanged(index
, index
);
330 case TorrentContentModelItem::COL_PRIO
:
332 const auto newPrio
= static_cast<BitTorrent::DownloadPriority
>(value
.toInt());
333 return setItemPriority(index
, newPrio
);
345 TorrentContentModelItem::ItemType
TorrentContentModel::itemType(const QModelIndex
&index
) const
347 return static_cast<const TorrentContentModelItem
*>(index
.internalPointer())->itemType();
350 int TorrentContentModel::getFileIndex(const QModelIndex
&index
) const
352 auto *item
= static_cast<TorrentContentModelItem
*>(index
.internalPointer());
353 if (item
->itemType() == TorrentContentModelItem::FileType
)
354 return static_cast<TorrentContentModelFile
*>(item
)->fileIndex();
359 Path
TorrentContentModel::getItemPath(const QModelIndex
&index
) const
362 for (QModelIndex i
= index
; i
.isValid(); i
= i
.parent())
363 path
= Path(i
.data().toString()) / path
;
367 QVariant
TorrentContentModel::data(const QModelIndex
&index
, const int role
) const
369 if (!index
.isValid())
372 auto *item
= static_cast<TorrentContentModelItem
*>(index
.internalPointer());
376 case Qt::DecorationRole
:
377 if (index
.column() != TorrentContentModelItem::COL_NAME
)
380 if (item
->itemType() == TorrentContentModelItem::FolderType
)
381 return m_fileIconProvider
->icon(QFileIconProvider::Folder
);
383 return m_fileIconProvider
->icon(QFileInfo(item
->name()));
385 case Qt::CheckStateRole
:
386 if (index
.column() != TorrentContentModelItem::COL_NAME
)
389 if (item
->priority() == BitTorrent::DownloadPriority::Ignored
)
390 return Qt::Unchecked
;
392 if (item
->priority() == BitTorrent::DownloadPriority::Mixed
)
394 Q_ASSERT(item
->itemType() == TorrentContentModelItem::FolderType
);
396 const auto *folder
= static_cast<TorrentContentModelFolder
*>(item
);
397 const auto childItems
= folder
->children();
398 const bool hasIgnored
= std::any_of(childItems
.cbegin(), childItems
.cend()
399 , [](const TorrentContentModelItem
*childItem
)
401 return (childItem
->priority() == BitTorrent::DownloadPriority::Ignored
);
404 return hasIgnored
? Qt::PartiallyChecked
: Qt::Checked
;
409 case Qt::TextAlignmentRole
:
410 if ((index
.column() == TorrentContentModelItem::COL_SIZE
)
411 || (index
.column() == TorrentContentModelItem::COL_REMAINING
))
413 return QVariant
{Qt::AlignRight
| Qt::AlignVCenter
};
418 case Qt::DisplayRole
:
419 case Qt::ToolTipRole
:
420 return item
->displayData(index
.column());
422 case Roles::UnderlyingDataRole
:
423 return item
->underlyingData(index
.column());
432 Qt::ItemFlags
TorrentContentModel::flags(const QModelIndex
&index
) const
434 if (!index
.isValid())
435 return Qt::NoItemFlags
;
437 Qt::ItemFlags flags
{Qt::ItemIsEnabled
| Qt::ItemIsSelectable
| Qt::ItemIsUserCheckable
};
438 if (itemType(index
) == TorrentContentModelItem::FolderType
)
439 flags
|= Qt::ItemIsAutoTristate
;
440 if (index
.column() == TorrentContentModelItem::COL_PRIO
)
441 flags
|= Qt::ItemIsEditable
;
446 QVariant
TorrentContentModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
448 if (orientation
!= Qt::Horizontal
)
453 case Qt::DisplayRole
:
454 return m_rootItem
->displayData(section
);
456 case Qt::TextAlignmentRole
:
457 if ((section
== TorrentContentModelItem::COL_SIZE
)
458 || (section
== TorrentContentModelItem::COL_REMAINING
))
460 return QVariant
{Qt::AlignRight
| Qt::AlignVCenter
};
470 QModelIndex
TorrentContentModel::index(const int row
, const int column
, const QModelIndex
&parent
) const
472 if (column
>= columnCount())
475 const TorrentContentModelFolder
*parentItem
= parent
.isValid()
476 ? static_cast<TorrentContentModelFolder
*>(parent
.internalPointer())
478 Q_ASSERT(parentItem
);
480 if (row
>= parentItem
->childCount())
483 TorrentContentModelItem
*childItem
= parentItem
->child(row
);
485 return createIndex(row
, column
, childItem
);
490 QModelIndex
TorrentContentModel::parent(const QModelIndex
&index
) const
492 if (!index
.isValid())
495 const auto *item
= static_cast<TorrentContentModelItem
*>(index
.internalPointer());
499 TorrentContentModelItem
*parentItem
= item
->parent();
500 if (parentItem
== m_rootItem
)
503 // From https://doc.qt.io/qt-6/qabstractitemmodel.html#parent:
504 // A common convention used in models that expose tree data structures is that only items
505 // in the first column have children. For that case, when reimplementing this function in
506 // a subclass the column of the returned QModelIndex would be 0.
507 return createIndex(parentItem
->row(), 0, parentItem
);
510 int TorrentContentModel::rowCount(const QModelIndex
&parent
) const
512 const TorrentContentModelFolder
*parentItem
= parent
.isValid()
513 ? dynamic_cast<TorrentContentModelFolder
*>(static_cast<TorrentContentModelItem
*>(parent
.internalPointer()))
515 return parentItem
? parentItem
->childCount() : 0;
518 void TorrentContentModel::populate()
520 Q_ASSERT(m_contentHandler
&& m_contentHandler
->hasMetadata());
522 const int filesCount
= m_contentHandler
->filesCount();
523 m_filesIndex
.reserve(filesCount
);
525 QHash
<TorrentContentModelFolder
*, QHash
<QString
, TorrentContentModelFolder
*>> folderMap
;
526 QList
<QString
> lastParentPath
;
527 TorrentContentModelFolder
*lastParent
= m_rootItem
;
528 // Iterate over files
529 for (int i
= 0; i
< filesCount
; ++i
)
531 const QString path
= m_contentHandler
->filePath(i
).data();
533 // Iterate of parts of the path to create necessary folders
534 QList
<QStringView
> pathFolders
= QStringView(path
).split(u
'/', Qt::SkipEmptyParts
);
535 const QString fileName
= pathFolders
.takeLast().toString();
537 if (!std::equal(lastParentPath
.begin(), lastParentPath
.end()
538 , pathFolders
.begin(), pathFolders
.end()))
540 lastParentPath
.clear();
541 lastParentPath
.reserve(pathFolders
.size());
543 // rebuild the path from the root
544 lastParent
= m_rootItem
;
545 for (const QStringView pathPart
: asConst(pathFolders
))
547 const QString folderName
= pathPart
.toString();
548 lastParentPath
.push_back(folderName
);
550 TorrentContentModelFolder
*&newParent
= folderMap
[lastParent
][folderName
];
553 newParent
= new TorrentContentModelFolder(folderName
, lastParent
);
554 lastParent
->appendChild(newParent
);
557 lastParent
= newParent
;
561 // Actually create the file
562 auto *fileItem
= new TorrentContentModelFile(fileName
, m_contentHandler
->fileSize(i
), lastParent
, i
);
563 lastParent
->appendChild(fileItem
);
564 m_filesIndex
.push_back(fileItem
);
567 updateFilesProgress();
568 updateFilesPriorities();
569 updateFilesAvailability();
572 void TorrentContentModel::setContentHandler(BitTorrent::TorrentContentHandler
*contentHandler
)
575 [[maybe_unused
]] const auto modelResetGuard
= qScopeGuard([this] { endResetModel(); });
577 if (m_contentHandler
)
579 m_filesIndex
.clear();
580 m_rootItem
->deleteAllChildren();
583 m_contentHandler
= contentHandler
;
585 if (m_contentHandler
&& m_contentHandler
->hasMetadata())
589 BitTorrent::TorrentContentHandler
*TorrentContentModel::contentHandler() const
591 return m_contentHandler
;
594 void TorrentContentModel::refresh()
596 if (!m_contentHandler
|| !m_contentHandler
->hasMetadata())
599 if (!m_filesIndex
.isEmpty())
601 updateFilesProgress();
602 updateFilesPriorities();
603 updateFilesAvailability();
605 const QList
<ColumnInterval
> columns
=
607 {TorrentContentModelItem::COL_NAME
, TorrentContentModelItem::COL_NAME
},
608 {TorrentContentModelItem::COL_PROGRESS
, TorrentContentModelItem::COL_PROGRESS
},
609 {TorrentContentModelItem::COL_PRIO
, TorrentContentModelItem::COL_PRIO
},
610 {TorrentContentModelItem::COL_AVAILABILITY
, TorrentContentModelItem::COL_AVAILABILITY
}
612 notifySubtreeUpdated(index(0, 0), columns
);
622 void TorrentContentModel::notifySubtreeUpdated(const QModelIndex
&index
, const QList
<ColumnInterval
> &columns
)
624 // For best performance, `columns` entries should be arranged from left to right
626 Q_ASSERT(index
.isValid());
629 for (const ColumnInterval
&column
: columns
)
630 emit
dataChanged(index
.siblingAtColumn(column
.first()), index
.siblingAtColumn(column
.last()));
632 // propagate up the model
633 QModelIndex parentIndex
= parent(index
);
634 while (parentIndex
.isValid())
636 for (const ColumnInterval
&column
: columns
)
637 emit
dataChanged(parentIndex
.siblingAtColumn(column
.first()), parentIndex
.siblingAtColumn(column
.last()));
638 parentIndex
= parent(parentIndex
);
641 // propagate down the model
642 QList
<QModelIndex
> parentIndexes
;
644 if (hasChildren(index
))
645 parentIndexes
.push_back(index
);
647 while (!parentIndexes
.isEmpty())
649 const QModelIndex parent
= parentIndexes
.takeLast();
651 const int childCount
= rowCount(parent
);
652 const QModelIndex child
= this->index(0, 0, parent
);
654 // emit this generation
655 for (const ColumnInterval
&column
: columns
)
657 const QModelIndex childTopLeft
= child
.siblingAtColumn(column
.first());
658 const QModelIndex childBottomRight
= child
.sibling((childCount
- 1), column
.last());
659 emit
dataChanged(childTopLeft
, childBottomRight
);
662 // check generations further down
663 parentIndexes
.reserve(childCount
);
664 for (int i
= 0; i
< childCount
; ++i
)
666 const QModelIndex sibling
= child
.siblingAtRow(i
);
667 if (hasChildren(sibling
))
668 parentIndexes
.push_back(sibling
);