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>
39 #include <QScopeGuard>
42 #if defined(Q_OS_MACOS)
43 #define QBT_PIXMAP_CACHE_FOR_FILE_ICONS
44 #include <QPixmapCache>
45 #elif !defined(Q_OS_WIN)
46 #include <QMimeDatabase>
50 #include "base/bittorrent/downloadpriority.h"
51 #include "base/bittorrent/torrentcontenthandler.h"
52 #include "base/exceptions.h"
53 #include "base/global.h"
54 #include "base/path.h"
55 #include "base/utils/fs.h"
56 #include "torrentcontentmodelfile.h"
57 #include "torrentcontentmodelfolder.h"
58 #include "torrentcontentmodelitem.h"
59 #include "uithememanager.h"
62 #include "macutilities.h"
67 class UnifiedFileIconProvider
: public QFileIconProvider
70 UnifiedFileIconProvider()
71 : m_textPlainIcon
{UIThemeManager::instance()->getIcon(u
"help-about"_s
, u
"text-plain"_s
)}
75 using QFileIconProvider::icon
;
77 QIcon
icon(const QFileInfo
&) const override
79 return m_textPlainIcon
;
83 QIcon m_textPlainIcon
;
86 #ifdef QBT_PIXMAP_CACHE_FOR_FILE_ICONS
87 class CachingFileIconProvider
: public UnifiedFileIconProvider
90 using QFileIconProvider::icon
;
92 QIcon
icon(const QFileInfo
&info
) const final
94 const QString ext
= info
.suffix();
98 if (QPixmapCache::find(ext
, &cached
))
101 const QPixmap pixmap
= pixmapForExtension(ext
);
102 if (!pixmap
.isNull())
104 QPixmapCache::insert(ext
, pixmap
);
108 return UnifiedFileIconProvider::icon(info
);
112 virtual QPixmap
pixmapForExtension(const QString
&ext
) const = 0;
114 #endif // QBT_PIXMAP_CACHE_FOR_FILE_ICONS
116 #if defined(Q_OS_MACOS)
117 // There is a bug on macOS, to be reported to Qt
118 // https://github.com/qbittorrent/qBittorrent/pull/6156#issuecomment-316302615
119 class MacFileIconProvider final
: public CachingFileIconProvider
121 QPixmap
pixmapForExtension(const QString
&ext
) const override
123 return MacUtils::pixmapForExtension(ext
, QSize(32, 32));
126 #elif !defined(Q_OS_WIN)
128 * @brief Tests whether QFileIconProvider actually works
130 * Some QPA plugins do not implement QPlatformTheme::fileIcon(), and
131 * QFileIconProvider::icon() returns empty icons as the result. Here we ask it for
132 * two icons for probably absent files and when both icons are null, we assume that
133 * the current QPA plugin does not implement QPlatformTheme::fileIcon().
135 bool doesQFileIconProviderWork()
137 const Path PSEUDO_UNIQUE_FILE_NAME
= Utils::Fs::tempPath() / Path(u
"qBittorrent-test-QFileIconProvider-845eb448-7ad5-4cdb-b764-b3f322a266a9"_s
);
138 QFileIconProvider provider
;
139 const QIcon testIcon1
= provider
.icon(QFileInfo((PSEUDO_UNIQUE_FILE_NAME
+ u
".pdf").data()));
140 const QIcon testIcon2
= provider
.icon(QFileInfo((PSEUDO_UNIQUE_FILE_NAME
+ u
".png").data()));
141 return (!testIcon1
.isNull() || !testIcon2
.isNull());
144 class MimeFileIconProvider final
: public UnifiedFileIconProvider
146 using QFileIconProvider::icon
;
148 QIcon
icon(const QFileInfo
&info
) const override
150 const QMimeType mimeType
= QMimeDatabase().mimeTypeForFile(info
, QMimeDatabase::MatchExtension
);
152 const auto mimeIcon
= QIcon::fromTheme(mimeType
.iconName());
153 if (!mimeIcon
.isNull())
156 const auto genericIcon
= QIcon::fromTheme(mimeType
.genericIconName());
157 if (!genericIcon
.isNull())
160 return UnifiedFileIconProvider::icon(info
);
166 TorrentContentModel::TorrentContentModel(QObject
*parent
)
167 : QAbstractItemModel(parent
)
168 , m_rootItem(new TorrentContentModelFolder(QList
<QString
>({ tr("Name"), tr("Total Size"), tr("Progress"), tr("Download Priority"), tr("Remaining"), tr("Availability") })))
169 #if defined(Q_OS_WIN)
170 , m_fileIconProvider
{new QFileIconProvider
}
171 #elif defined(Q_OS_MACOS)
172 , m_fileIconProvider
{new MacFileIconProvider
}
174 , m_fileIconProvider
{doesQFileIconProviderWork() ? new QFileIconProvider
: new MimeFileIconProvider
}
177 m_fileIconProvider
->setOptions(QFileIconProvider::DontUseCustomDirectoryIcons
);
180 TorrentContentModel::~TorrentContentModel()
182 delete m_fileIconProvider
;
186 void TorrentContentModel::updateFilesProgress()
188 Q_ASSERT(m_contentHandler
&& m_contentHandler
->hasMetadata());
190 const QList
<qreal
> &filesProgress
= m_contentHandler
->filesProgress();
191 Q_ASSERT(m_filesIndex
.size() == filesProgress
.size());
192 // XXX: Why is this necessary?
193 if (m_filesIndex
.size() != filesProgress
.size()) [[unlikely
]]
196 for (int i
= 0; i
< filesProgress
.size(); ++i
)
197 m_filesIndex
[i
]->setProgress(filesProgress
[i
]);
198 // Update folders progress in the tree
199 m_rootItem
->recalculateProgress();
200 m_rootItem
->recalculateAvailability();
203 void TorrentContentModel::updateFilesPriorities()
205 Q_ASSERT(m_contentHandler
&& m_contentHandler
->hasMetadata());
207 const QList
<BitTorrent::DownloadPriority
> fprio
= m_contentHandler
->filePriorities();
208 Q_ASSERT(m_filesIndex
.size() == fprio
.size());
209 // XXX: Why is this necessary?
210 if (m_filesIndex
.size() != fprio
.size())
213 for (int i
= 0; i
< fprio
.size(); ++i
)
214 m_filesIndex
[i
]->setPriority(static_cast<BitTorrent::DownloadPriority
>(fprio
[i
]));
217 void TorrentContentModel::updateFilesAvailability()
219 Q_ASSERT(m_contentHandler
&& m_contentHandler
->hasMetadata());
221 using HandlerPtr
= QPointer
<BitTorrent::TorrentContentHandler
>;
222 m_contentHandler
->fetchAvailableFileFractions([this, handler
= HandlerPtr(m_contentHandler
)](const QList
<qreal
> &availableFileFractions
)
224 if (handler
!= m_contentHandler
)
227 Q_ASSERT(m_filesIndex
.size() == availableFileFractions
.size());
228 // XXX: Why is this necessary?
229 if (m_filesIndex
.size() != availableFileFractions
.size()) [[unlikely
]]
232 for (int i
= 0; i
< m_filesIndex
.size(); ++i
)
233 m_filesIndex
[i
]->setAvailability(availableFileFractions
[i
]);
234 // Update folders progress in the tree
235 m_rootItem
->recalculateProgress();
239 bool TorrentContentModel::setItemPriority(const QModelIndex
&index
, BitTorrent::DownloadPriority priority
)
241 Q_ASSERT(index
.isValid());
243 auto *item
= static_cast<TorrentContentModelItem
*>(index
.internalPointer());
244 const BitTorrent::DownloadPriority currentPriority
= item
->priority();
245 if (currentPriority
== priority
)
248 item
->setPriority(priority
);
249 m_contentHandler
->prioritizeFiles(getFilePriorities());
251 // Update folders progress in the tree
252 m_rootItem
->recalculateProgress();
253 m_rootItem
->recalculateAvailability();
255 const QList
<ColumnInterval
> columns
=
257 {TorrentContentModelItem::COL_NAME
, TorrentContentModelItem::COL_NAME
},
258 {TorrentContentModelItem::COL_PRIO
, TorrentContentModelItem::COL_PRIO
}
260 notifySubtreeUpdated(index
, columns
);
265 QList
<BitTorrent::DownloadPriority
> TorrentContentModel::getFilePriorities() const
267 QList
<BitTorrent::DownloadPriority
> prio
;
268 prio
.reserve(m_filesIndex
.size());
269 for (const TorrentContentModelFile
*file
: asConst(m_filesIndex
))
270 prio
.push_back(file
->priority());
274 int TorrentContentModel::columnCount([[maybe_unused
]] const QModelIndex
&parent
) const
276 return TorrentContentModelItem::NB_COL
;
279 bool TorrentContentModel::setData(const QModelIndex
&index
, const QVariant
&value
, const int role
)
281 if (!index
.isValid())
284 if ((index
.column() == TorrentContentModelItem::COL_NAME
) && (role
== Qt::CheckStateRole
))
286 const auto checkState
= static_cast<Qt::CheckState
>(value
.toInt());
287 const BitTorrent::DownloadPriority newPrio
= (checkState
== Qt::PartiallyChecked
)
288 ? BitTorrent::DownloadPriority::Mixed
289 : ((checkState
== Qt::Unchecked
)
290 ? BitTorrent::DownloadPriority::Ignored
291 : BitTorrent::DownloadPriority::Normal
);
293 return setItemPriority(index
, newPrio
);
296 if (role
== Qt::EditRole
)
298 auto *item
= static_cast<TorrentContentModelItem
*>(index
.internalPointer());
300 switch (index
.column())
302 case TorrentContentModelItem::COL_NAME
:
304 const QString currentName
= item
->name();
305 const QString newName
= value
.toString();
306 if (currentName
!= newName
)
310 const Path parentPath
= getItemPath(index
.parent());
311 const Path oldPath
= parentPath
/ Path(currentName
);
312 const Path newPath
= parentPath
/ Path(newName
);
314 if (item
->itemType() == TorrentContentModelItem::FileType
)
315 m_contentHandler
->renameFile(oldPath
, newPath
);
317 m_contentHandler
->renameFolder(oldPath
, newPath
);
319 catch (const RuntimeError
&error
)
321 emit
renameFailed(error
.message());
325 item
->setName(newName
);
326 emit
dataChanged(index
, index
);
332 case TorrentContentModelItem::COL_PRIO
:
334 const auto newPrio
= static_cast<BitTorrent::DownloadPriority
>(value
.toInt());
335 return setItemPriority(index
, newPrio
);
347 TorrentContentModelItem::ItemType
TorrentContentModel::itemType(const QModelIndex
&index
) const
349 return static_cast<const TorrentContentModelItem
*>(index
.internalPointer())->itemType();
352 int TorrentContentModel::getFileIndex(const QModelIndex
&index
) const
354 auto *item
= static_cast<TorrentContentModelItem
*>(index
.internalPointer());
355 if (item
->itemType() == TorrentContentModelItem::FileType
)
356 return static_cast<TorrentContentModelFile
*>(item
)->fileIndex();
361 Path
TorrentContentModel::getItemPath(const QModelIndex
&index
) const
364 for (QModelIndex i
= index
; i
.isValid(); i
= i
.parent())
365 path
= Path(i
.data().toString()) / path
;
369 QVariant
TorrentContentModel::data(const QModelIndex
&index
, const int role
) const
371 if (!index
.isValid())
374 auto *item
= static_cast<TorrentContentModelItem
*>(index
.internalPointer());
378 case Qt::DecorationRole
:
379 if (index
.column() != TorrentContentModelItem::COL_NAME
)
382 if (item
->itemType() == TorrentContentModelItem::FolderType
)
383 return m_fileIconProvider
->icon(QFileIconProvider::Folder
);
385 return m_fileIconProvider
->icon(QFileInfo(item
->name()));
387 case Qt::CheckStateRole
:
388 if (index
.column() != TorrentContentModelItem::COL_NAME
)
391 if (item
->priority() == BitTorrent::DownloadPriority::Ignored
)
392 return Qt::Unchecked
;
394 if (item
->priority() == BitTorrent::DownloadPriority::Mixed
)
396 Q_ASSERT(item
->itemType() == TorrentContentModelItem::FolderType
);
398 const auto *folder
= static_cast<TorrentContentModelFolder
*>(item
);
399 const auto childItems
= folder
->children();
400 const bool hasIgnored
= std::any_of(childItems
.cbegin(), childItems
.cend()
401 , [](const TorrentContentModelItem
*childItem
)
403 return (childItem
->priority() == BitTorrent::DownloadPriority::Ignored
);
406 return hasIgnored
? Qt::PartiallyChecked
: Qt::Checked
;
411 case Qt::TextAlignmentRole
:
412 if ((index
.column() == TorrentContentModelItem::COL_SIZE
)
413 || (index
.column() == TorrentContentModelItem::COL_REMAINING
))
415 return QVariant
{Qt::AlignRight
| Qt::AlignVCenter
};
420 case Qt::DisplayRole
:
421 case Qt::ToolTipRole
:
422 return item
->displayData(index
.column());
424 case Roles::UnderlyingDataRole
:
425 return item
->underlyingData(index
.column());
434 Qt::ItemFlags
TorrentContentModel::flags(const QModelIndex
&index
) const
436 if (!index
.isValid())
437 return Qt::NoItemFlags
;
439 Qt::ItemFlags flags
{Qt::ItemIsDragEnabled
| Qt::ItemIsEnabled
| Qt::ItemIsSelectable
| Qt::ItemIsUserCheckable
};
440 if (itemType(index
) == TorrentContentModelItem::FolderType
)
441 flags
|= Qt::ItemIsAutoTristate
;
442 if (index
.column() == TorrentContentModelItem::COL_PRIO
)
443 flags
|= Qt::ItemIsEditable
;
448 QVariant
TorrentContentModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
450 if (orientation
!= Qt::Horizontal
)
455 case Qt::DisplayRole
:
456 return m_rootItem
->displayData(section
);
458 case Qt::TextAlignmentRole
:
459 if ((section
== TorrentContentModelItem::COL_SIZE
)
460 || (section
== TorrentContentModelItem::COL_REMAINING
))
462 return QVariant
{Qt::AlignRight
| Qt::AlignVCenter
};
472 QModelIndex
TorrentContentModel::index(const int row
, const int column
, const QModelIndex
&parent
) const
474 if (column
>= columnCount())
477 const TorrentContentModelFolder
*parentItem
= parent
.isValid()
478 ? static_cast<TorrentContentModelFolder
*>(parent
.internalPointer())
480 Q_ASSERT(parentItem
);
482 if (row
>= parentItem
->childCount())
485 TorrentContentModelItem
*childItem
= parentItem
->child(row
);
487 return createIndex(row
, column
, childItem
);
492 QModelIndex
TorrentContentModel::parent(const QModelIndex
&index
) const
494 if (!index
.isValid())
497 const auto *item
= static_cast<TorrentContentModelItem
*>(index
.internalPointer());
501 TorrentContentModelItem
*parentItem
= item
->parent();
502 if (parentItem
== m_rootItem
)
505 // From https://doc.qt.io/qt-6/qabstractitemmodel.html#parent:
506 // A common convention used in models that expose tree data structures is that only items
507 // in the first column have children. For that case, when reimplementing this function in
508 // a subclass the column of the returned QModelIndex would be 0.
509 return createIndex(parentItem
->row(), 0, parentItem
);
512 int TorrentContentModel::rowCount(const QModelIndex
&parent
) const
514 const TorrentContentModelFolder
*parentItem
= parent
.isValid()
515 ? dynamic_cast<TorrentContentModelFolder
*>(static_cast<TorrentContentModelItem
*>(parent
.internalPointer()))
517 return parentItem
? parentItem
->childCount() : 0;
520 QMimeData
*TorrentContentModel::mimeData(const QModelIndexList
&indexes
) const
522 if (indexes
.isEmpty())
525 const Path storagePath
= contentHandler()->actualStorageLocation();
528 paths
.reserve(indexes
.size());
530 for (const QModelIndex
&index
: indexes
)
532 if (!index
.isValid())
534 if (index
.column() != TorrentContentModelItem::COL_NAME
)
537 if (itemType(index
) == TorrentContentModelItem::FileType
)
539 const int idx
= getFileIndex(index
);
540 const Path fullPath
= storagePath
/ contentHandler()->actualFilePath(idx
);
541 paths
.append(QUrl::fromLocalFile(fullPath
.data()));
545 const Path fullPath
= storagePath
/ getItemPath(index
);
546 paths
.append(QUrl::fromLocalFile(fullPath
.data()));
550 auto *mimeData
= new QMimeData
; // lifetime will be handled by Qt
551 mimeData
->setUrls(paths
);
556 QStringList
TorrentContentModel::mimeTypes() const
558 return {u
"text/uri-list"_s
};
561 void TorrentContentModel::populate()
563 Q_ASSERT(m_contentHandler
&& m_contentHandler
->hasMetadata());
565 const int filesCount
= m_contentHandler
->filesCount();
566 m_filesIndex
.reserve(filesCount
);
568 QHash
<TorrentContentModelFolder
*, QHash
<QString
, TorrentContentModelFolder
*>> folderMap
;
569 QList
<QString
> lastParentPath
;
570 TorrentContentModelFolder
*lastParent
= m_rootItem
;
571 // Iterate over files
572 for (int i
= 0; i
< filesCount
; ++i
)
574 const QString path
= m_contentHandler
->filePath(i
).data();
576 // Iterate of parts of the path to create necessary folders
577 QList
<QStringView
> pathFolders
= QStringView(path
).split(u
'/', Qt::SkipEmptyParts
);
578 const QString fileName
= pathFolders
.takeLast().toString();
580 if (!std::equal(lastParentPath
.begin(), lastParentPath
.end()
581 , pathFolders
.begin(), pathFolders
.end()))
583 lastParentPath
.clear();
584 lastParentPath
.reserve(pathFolders
.size());
586 // rebuild the path from the root
587 lastParent
= m_rootItem
;
588 for (const QStringView pathPart
: asConst(pathFolders
))
590 const QString folderName
= pathPart
.toString();
591 lastParentPath
.push_back(folderName
);
593 TorrentContentModelFolder
*&newParent
= folderMap
[lastParent
][folderName
];
596 newParent
= new TorrentContentModelFolder(folderName
, lastParent
);
597 lastParent
->appendChild(newParent
);
600 lastParent
= newParent
;
604 // Actually create the file
605 auto *fileItem
= new TorrentContentModelFile(fileName
, m_contentHandler
->fileSize(i
), lastParent
, i
);
606 lastParent
->appendChild(fileItem
);
607 m_filesIndex
.push_back(fileItem
);
610 updateFilesProgress();
611 updateFilesPriorities();
612 updateFilesAvailability();
615 void TorrentContentModel::setContentHandler(BitTorrent::TorrentContentHandler
*contentHandler
)
618 [[maybe_unused
]] const auto modelResetGuard
= qScopeGuard([this] { endResetModel(); });
620 if (m_contentHandler
)
622 m_filesIndex
.clear();
623 m_rootItem
->deleteAllChildren();
626 m_contentHandler
= contentHandler
;
628 if (m_contentHandler
&& m_contentHandler
->hasMetadata())
632 BitTorrent::TorrentContentHandler
*TorrentContentModel::contentHandler() const
634 return m_contentHandler
;
637 void TorrentContentModel::refresh()
639 if (!m_contentHandler
|| !m_contentHandler
->hasMetadata())
642 if (!m_filesIndex
.isEmpty())
644 updateFilesProgress();
645 updateFilesPriorities();
646 updateFilesAvailability();
648 const QList
<ColumnInterval
> columns
=
650 {TorrentContentModelItem::COL_NAME
, TorrentContentModelItem::COL_NAME
},
651 {TorrentContentModelItem::COL_PROGRESS
, TorrentContentModelItem::COL_PROGRESS
},
652 {TorrentContentModelItem::COL_PRIO
, TorrentContentModelItem::COL_PRIO
},
653 {TorrentContentModelItem::COL_AVAILABILITY
, TorrentContentModelItem::COL_AVAILABILITY
}
655 notifySubtreeUpdated(index(0, 0), columns
);
665 void TorrentContentModel::notifySubtreeUpdated(const QModelIndex
&index
, const QList
<ColumnInterval
> &columns
)
667 // For best performance, `columns` entries should be arranged from left to right
669 Q_ASSERT(index
.isValid());
672 for (const ColumnInterval
&column
: columns
)
673 emit
dataChanged(index
.siblingAtColumn(column
.first()), index
.siblingAtColumn(column
.last()));
675 // propagate up the model
676 QModelIndex parentIndex
= parent(index
);
677 while (parentIndex
.isValid())
679 for (const ColumnInterval
&column
: columns
)
680 emit
dataChanged(parentIndex
.siblingAtColumn(column
.first()), parentIndex
.siblingAtColumn(column
.last()));
681 parentIndex
= parent(parentIndex
);
684 // propagate down the model
685 QList
<QModelIndex
> parentIndexes
;
687 if (hasChildren(index
))
688 parentIndexes
.push_back(index
);
690 while (!parentIndexes
.isEmpty())
692 const QModelIndex parent
= parentIndexes
.takeLast();
694 const int childCount
= rowCount(parent
);
695 const QModelIndex child
= this->index(0, 0, parent
);
697 // emit this generation
698 for (const ColumnInterval
&column
: columns
)
700 const QModelIndex childTopLeft
= child
.siblingAtColumn(column
.first());
701 const QModelIndex childBottomRight
= child
.sibling((childCount
- 1), column
.last());
702 emit
dataChanged(childTopLeft
, childBottomRight
);
705 // check generations further down
706 parentIndexes
.reserve(childCount
);
707 for (int i
= 0; i
< childCount
; ++i
)
709 const QModelIndex sibling
= child
.siblingAtRow(i
);
710 if (hasChildren(sibling
))
711 parentIndexes
.push_back(sibling
);