Bump to 4.6.7
[qBittorrent.git] / src / gui / transferlistwidget.cpp
blob7fc717eced458e2e1c94d81a69412dcc1db3b54e
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2023-2024 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2006 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 "transferlistwidget.h"
32 #include <algorithm>
34 #include <QClipboard>
35 #include <QDebug>
36 #include <QFileDialog>
37 #include <QHeaderView>
38 #include <QMenu>
39 #include <QMessageBox>
40 #include <QRegularExpression>
41 #include <QSet>
42 #include <QShortcut>
43 #include <QVector>
44 #include <QWheelEvent>
46 #include "base/bittorrent/session.h"
47 #include "base/bittorrent/torrent.h"
48 #include "base/bittorrent/trackerentry.h"
49 #include "base/global.h"
50 #include "base/logger.h"
51 #include "base/path.h"
52 #include "base/preferences.h"
53 #include "base/torrentfilter.h"
54 #include "base/utils/compare.h"
55 #include "base/utils/fs.h"
56 #include "base/utils/misc.h"
57 #include "base/utils/string.h"
58 #include "autoexpandabledialog.h"
59 #include "deletionconfirmationdialog.h"
60 #include "mainwindow.h"
61 #include "optionsdialog.h"
62 #include "previewselectdialog.h"
63 #include "speedlimitdialog.h"
64 #include "torrentcategorydialog.h"
65 #include "torrentoptionsdialog.h"
66 #include "trackerentriesdialog.h"
67 #include "transferlistdelegate.h"
68 #include "transferlistsortmodel.h"
69 #include "tristateaction.h"
70 #include "uithememanager.h"
71 #include "utils.h"
73 #ifdef Q_OS_MACOS
74 #include "macutilities.h"
75 #endif
77 namespace
79 QVector<BitTorrent::TorrentID> extractIDs(const QVector<BitTorrent::Torrent *> &torrents)
81 QVector<BitTorrent::TorrentID> torrentIDs;
82 torrentIDs.reserve(torrents.size());
83 for (const BitTorrent::Torrent *torrent : torrents)
84 torrentIDs << torrent->id();
85 return torrentIDs;
88 bool torrentContainsPreviewableFiles(const BitTorrent::Torrent *const torrent)
90 if (!torrent->hasMetadata())
91 return false;
93 for (const Path &filePath : asConst(torrent->filePaths()))
95 if (Utils::Misc::isPreviewable(filePath))
96 return true;
99 return false;
102 void openDestinationFolder(const BitTorrent::Torrent *const torrent)
104 const Path contentPath = torrent->contentPath();
105 const Path openedPath = (!contentPath.isEmpty() ? contentPath : torrent->savePath());
106 #ifdef Q_OS_MACOS
107 MacUtils::openFiles({openedPath});
108 #else
109 if (torrent->filesCount() == 1)
110 Utils::Gui::openFolderSelect(openedPath);
111 else
112 Utils::Gui::openPath(openedPath);
113 #endif
116 void removeTorrents(const QVector<BitTorrent::Torrent *> &torrents, const bool isDeleteFileSelected)
118 auto *session = BitTorrent::Session::instance();
119 const DeleteOption deleteOption = isDeleteFileSelected ? DeleteTorrentAndFiles : DeleteTorrent;
120 for (const BitTorrent::Torrent *torrent : torrents)
121 session->deleteTorrent(torrent->id(), deleteOption);
125 TransferListWidget::TransferListWidget(QWidget *parent, MainWindow *mainWindow)
126 : QTreeView {parent}
127 , m_listModel {new TransferListModel {this}}
128 , m_sortFilterModel {new TransferListSortModel {this}}
129 , m_mainWindow {mainWindow}
131 // Load settings
132 const bool columnLoaded = loadSettings();
134 // Create and apply delegate
135 setItemDelegate(new TransferListDelegate {this});
137 m_sortFilterModel->setDynamicSortFilter(true);
138 m_sortFilterModel->setSourceModel(m_listModel);
139 m_sortFilterModel->setFilterKeyColumn(TransferListModel::TR_NAME);
140 m_sortFilterModel->setFilterRole(Qt::DisplayRole);
141 m_sortFilterModel->setSortCaseSensitivity(Qt::CaseInsensitive);
142 m_sortFilterModel->setSortRole(TransferListModel::UnderlyingDataRole);
143 setModel(m_sortFilterModel);
145 // Visual settings
146 setUniformRowHeights(true);
147 setRootIsDecorated(false);
148 setAllColumnsShowFocus(true);
149 setSortingEnabled(true);
150 setSelectionMode(QAbstractItemView::ExtendedSelection);
151 setItemsExpandable(false);
152 setAutoScroll(true);
153 setDragDropMode(QAbstractItemView::DragOnly);
154 #if defined(Q_OS_MACOS)
155 setAttribute(Qt::WA_MacShowFocusRect, false);
156 #endif
157 header()->setFirstSectionMovable(true);
158 header()->setStretchLastSection(false);
159 header()->setTextElideMode(Qt::ElideRight);
161 // Default hidden columns
162 if (!columnLoaded)
164 setColumnHidden(TransferListModel::TR_ADD_DATE, true);
165 setColumnHidden(TransferListModel::TR_SEED_DATE, true);
166 setColumnHidden(TransferListModel::TR_UPLIMIT, true);
167 setColumnHidden(TransferListModel::TR_DLLIMIT, true);
168 setColumnHidden(TransferListModel::TR_TRACKER, true);
169 setColumnHidden(TransferListModel::TR_AMOUNT_DOWNLOADED, true);
170 setColumnHidden(TransferListModel::TR_AMOUNT_UPLOADED, true);
171 setColumnHidden(TransferListModel::TR_AMOUNT_DOWNLOADED_SESSION, true);
172 setColumnHidden(TransferListModel::TR_AMOUNT_UPLOADED_SESSION, true);
173 setColumnHidden(TransferListModel::TR_AMOUNT_LEFT, true);
174 setColumnHidden(TransferListModel::TR_TIME_ELAPSED, true);
175 setColumnHidden(TransferListModel::TR_SAVE_PATH, true);
176 setColumnHidden(TransferListModel::TR_DOWNLOAD_PATH, true);
177 setColumnHidden(TransferListModel::TR_INFOHASH_V1, true);
178 setColumnHidden(TransferListModel::TR_INFOHASH_V2, true);
179 setColumnHidden(TransferListModel::TR_COMPLETED, true);
180 setColumnHidden(TransferListModel::TR_RATIO_LIMIT, true);
181 setColumnHidden(TransferListModel::TR_SEEN_COMPLETE_DATE, true);
182 setColumnHidden(TransferListModel::TR_LAST_ACTIVITY, true);
183 setColumnHidden(TransferListModel::TR_TOTAL_SIZE, true);
186 //Ensure that at least one column is visible at all times
187 bool atLeastOne = false;
188 for (int i = 0; i < TransferListModel::NB_COLUMNS; ++i)
190 if (!isColumnHidden(i))
192 atLeastOne = true;
193 break;
196 if (!atLeastOne)
197 setColumnHidden(TransferListModel::TR_NAME, false);
199 //When adding/removing columns between versions some may
200 //end up being size 0 when the new version is launched with
201 //a conf file from the previous version.
202 for (int i = 0; i < TransferListModel::NB_COLUMNS; ++i)
204 if ((columnWidth(i) <= 0) && (!isColumnHidden(i)))
205 resizeColumnToContents(i);
208 setContextMenuPolicy(Qt::CustomContextMenu);
210 // Listen for list events
211 connect(this, &QAbstractItemView::doubleClicked, this, &TransferListWidget::torrentDoubleClicked);
212 connect(this, &QWidget::customContextMenuRequested, this, &TransferListWidget::displayListMenu);
213 header()->setContextMenuPolicy(Qt::CustomContextMenu);
214 connect(header(), &QWidget::customContextMenuRequested, this, &TransferListWidget::displayColumnHeaderMenu);
215 connect(header(), &QHeaderView::sectionMoved, this, &TransferListWidget::saveSettings);
216 connect(header(), &QHeaderView::sectionResized, this, &TransferListWidget::saveSettings);
217 connect(header(), &QHeaderView::sortIndicatorChanged, this, &TransferListWidget::saveSettings);
219 const auto *editHotkey = new QShortcut(Qt::Key_F2, this, nullptr, nullptr, Qt::WidgetShortcut);
220 connect(editHotkey, &QShortcut::activated, this, &TransferListWidget::renameSelectedTorrent);
221 const auto *deleteHotkey = new QShortcut(QKeySequence::Delete, this, nullptr, nullptr, Qt::WidgetShortcut);
222 connect(deleteHotkey, &QShortcut::activated, this, &TransferListWidget::softDeleteSelectedTorrents);
223 const auto *permDeleteHotkey = new QShortcut(Qt::SHIFT + Qt::Key_Delete, this, nullptr, nullptr, Qt::WidgetShortcut);
224 connect(permDeleteHotkey, &QShortcut::activated, this, &TransferListWidget::permDeleteSelectedTorrents);
225 const auto *doubleClickHotkeyReturn = new QShortcut(Qt::Key_Return, this, nullptr, nullptr, Qt::WidgetShortcut);
226 connect(doubleClickHotkeyReturn, &QShortcut::activated, this, &TransferListWidget::torrentDoubleClicked);
227 const auto *doubleClickHotkeyEnter = new QShortcut(Qt::Key_Enter, this, nullptr, nullptr, Qt::WidgetShortcut);
228 connect(doubleClickHotkeyEnter, &QShortcut::activated, this, &TransferListWidget::torrentDoubleClicked);
229 const auto *recheckHotkey = new QShortcut(Qt::CTRL + Qt::Key_R, this, nullptr, nullptr, Qt::WidgetShortcut);
230 connect(recheckHotkey, &QShortcut::activated, this, &TransferListWidget::recheckSelectedTorrents);
231 const auto *forceStartHotkey = new QShortcut(Qt::CTRL + Qt::Key_M, this, nullptr, nullptr, Qt::WidgetShortcut);
232 connect(forceStartHotkey, &QShortcut::activated, this, &TransferListWidget::forceStartSelectedTorrents);
235 TransferListWidget::~TransferListWidget()
237 // Save settings
238 saveSettings();
241 TransferListModel *TransferListWidget::getSourceModel() const
243 return m_listModel;
246 void TransferListWidget::previewFile(const Path &filePath)
248 Utils::Gui::openPath(filePath);
251 QModelIndex TransferListWidget::mapToSource(const QModelIndex &index) const
253 Q_ASSERT(index.isValid());
254 if (index.model() == m_sortFilterModel)
255 return m_sortFilterModel->mapToSource(index);
256 return index;
259 QModelIndexList TransferListWidget::mapToSource(const QModelIndexList &indexes) const
261 QModelIndexList result;
262 result.reserve(indexes.size());
263 for (const QModelIndex &index : indexes)
264 result.append(mapToSource(index));
266 return result;
269 QModelIndex TransferListWidget::mapFromSource(const QModelIndex &index) const
271 Q_ASSERT(index.isValid());
272 Q_ASSERT(index.model() == m_sortFilterModel);
273 return m_sortFilterModel->mapFromSource(index);
276 void TransferListWidget::torrentDoubleClicked()
278 const QModelIndexList selectedIndexes = selectionModel()->selectedRows();
279 if ((selectedIndexes.size() != 1) || !selectedIndexes.first().isValid())
280 return;
282 const QModelIndex index = m_listModel->index(mapToSource(selectedIndexes.first()).row());
283 BitTorrent::Torrent *const torrent = m_listModel->torrentHandle(index);
284 if (!torrent)
285 return;
287 int action;
288 if (torrent->isFinished())
289 action = Preferences::instance()->getActionOnDblClOnTorrentFn();
290 else
291 action = Preferences::instance()->getActionOnDblClOnTorrentDl();
293 switch (action)
295 case TOGGLE_PAUSE:
296 if (torrent->isPaused())
297 torrent->resume();
298 else
299 torrent->pause();
300 break;
301 case PREVIEW_FILE:
302 if (torrentContainsPreviewableFiles(torrent))
304 auto *dialog = new PreviewSelectDialog(this, torrent);
305 dialog->setAttribute(Qt::WA_DeleteOnClose);
306 connect(dialog, &PreviewSelectDialog::readyToPreviewFile, this, &TransferListWidget::previewFile);
307 dialog->show();
309 else
311 openDestinationFolder(torrent);
313 break;
314 case OPEN_DEST:
315 openDestinationFolder(torrent);
316 break;
317 case SHOW_OPTIONS:
318 setTorrentOptions();
319 break;
323 QVector<BitTorrent::Torrent *> TransferListWidget::getSelectedTorrents() const
325 const QModelIndexList selectedRows = selectionModel()->selectedRows();
327 QVector<BitTorrent::Torrent *> torrents;
328 torrents.reserve(selectedRows.size());
329 for (const QModelIndex &index : selectedRows)
330 torrents << m_listModel->torrentHandle(mapToSource(index));
331 return torrents;
334 QVector<BitTorrent::Torrent *> TransferListWidget::getVisibleTorrents() const
336 const int visibleTorrentsCount = m_sortFilterModel->rowCount();
338 QVector<BitTorrent::Torrent *> torrents;
339 torrents.reserve(visibleTorrentsCount);
340 for (int i = 0; i < visibleTorrentsCount; ++i)
341 torrents << m_listModel->torrentHandle(mapToSource(m_sortFilterModel->index(i, 0)));
342 return torrents;
345 void TransferListWidget::setSelectedTorrentsLocation()
347 const QVector<BitTorrent::Torrent *> torrents = getSelectedTorrents();
348 if (torrents.isEmpty())
349 return;
351 const Path oldLocation = torrents[0]->savePath();
353 auto *fileDialog = new QFileDialog(this, tr("Choose save path"), oldLocation.data());
354 fileDialog->setAttribute(Qt::WA_DeleteOnClose);
355 fileDialog->setFileMode(QFileDialog::Directory);
356 fileDialog->setOptions(QFileDialog::DontConfirmOverwrite | QFileDialog::ShowDirsOnly | QFileDialog::HideNameFilterDetails);
357 connect(fileDialog, &QDialog::accepted, this, [this, fileDialog]()
359 const QVector<BitTorrent::Torrent *> torrents = getSelectedTorrents();
360 if (torrents.isEmpty())
361 return;
363 const Path newLocation {fileDialog->selectedFiles().constFirst()};
364 if (!newLocation.exists())
365 return;
367 // Actually move storage
368 for (BitTorrent::Torrent *const torrent : torrents)
370 torrent->setAutoTMMEnabled(false);
371 torrent->setSavePath(newLocation);
375 fileDialog->open();
378 void TransferListWidget::pauseAllTorrents()
380 if (Preferences::instance()->confirmPauseAndResumeAll())
382 // Show confirmation if user would really like to Pause All
383 const QMessageBox::StandardButton ret = QMessageBox::question(this, tr("Confirm pause")
384 , tr("Would you like to pause all torrents?"), (QMessageBox::Yes | QMessageBox::No));
386 if (ret != QMessageBox::Yes)
387 return;
390 for (BitTorrent::Torrent *const torrent : asConst(BitTorrent::Session::instance()->torrents()))
391 torrent->pause();
394 void TransferListWidget::resumeAllTorrents()
396 if (Preferences::instance()->confirmPauseAndResumeAll())
398 // Show confirmation if user would really like to Resume All
399 const QMessageBox::StandardButton ret = QMessageBox::question(this, tr("Confirm resume")
400 , tr("Would you like to resume all torrents?"), (QMessageBox::Yes | QMessageBox::No));
402 if (ret != QMessageBox::Yes)
403 return;
406 for (BitTorrent::Torrent *const torrent : asConst(BitTorrent::Session::instance()->torrents()))
407 torrent->resume();
410 void TransferListWidget::startSelectedTorrents()
412 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
413 torrent->resume();
416 void TransferListWidget::forceStartSelectedTorrents()
418 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
419 torrent->resume(BitTorrent::TorrentOperatingMode::Forced);
422 void TransferListWidget::startVisibleTorrents()
424 for (BitTorrent::Torrent *const torrent : asConst(getVisibleTorrents()))
425 torrent->resume();
428 void TransferListWidget::pauseSelectedTorrents()
430 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
431 torrent->pause();
434 void TransferListWidget::pauseVisibleTorrents()
436 for (BitTorrent::Torrent *const torrent : asConst(getVisibleTorrents()))
437 torrent->pause();
440 void TransferListWidget::softDeleteSelectedTorrents()
442 deleteSelectedTorrents(false);
445 void TransferListWidget::permDeleteSelectedTorrents()
447 deleteSelectedTorrents(true);
450 void TransferListWidget::deleteSelectedTorrents(const bool deleteLocalFiles)
452 if (m_mainWindow->currentTabWidget() != this) return;
454 const QVector<BitTorrent::Torrent *> torrents = getSelectedTorrents();
455 if (torrents.empty()) return;
457 if (Preferences::instance()->confirmTorrentDeletion())
459 auto *dialog = new DeletionConfirmationDialog(this, torrents.size(), torrents[0]->name(), deleteLocalFiles);
460 dialog->setAttribute(Qt::WA_DeleteOnClose);
461 connect(dialog, &DeletionConfirmationDialog::accepted, this, [this, dialog]()
463 // Some torrents might be removed when waiting for user input, so refetch the torrent list
464 // NOTE: this will only work when dialog is modal
465 removeTorrents(getSelectedTorrents(), dialog->isDeleteFileSelected());
467 dialog->open();
469 else
471 removeTorrents(torrents, deleteLocalFiles);
475 void TransferListWidget::deleteVisibleTorrents()
477 const QVector<BitTorrent::Torrent *> torrents = getVisibleTorrents();
478 if (torrents.empty()) return;
480 if (Preferences::instance()->confirmTorrentDeletion())
482 auto *dialog = new DeletionConfirmationDialog(this, torrents.size(), torrents[0]->name(), false);
483 dialog->setAttribute(Qt::WA_DeleteOnClose);
484 connect(dialog, &DeletionConfirmationDialog::accepted, this, [this, dialog]()
486 // Some torrents might be removed when waiting for user input, so refetch the torrent list
487 // NOTE: this will only work when dialog is modal
488 removeTorrents(getVisibleTorrents(), dialog->isDeleteFileSelected());
490 dialog->open();
492 else
494 removeTorrents(torrents, false);
498 void TransferListWidget::increaseQueuePosSelectedTorrents()
500 qDebug() << Q_FUNC_INFO;
501 if (m_mainWindow->currentTabWidget() == this)
502 BitTorrent::Session::instance()->increaseTorrentsQueuePos(extractIDs(getSelectedTorrents()));
505 void TransferListWidget::decreaseQueuePosSelectedTorrents()
507 qDebug() << Q_FUNC_INFO;
508 if (m_mainWindow->currentTabWidget() == this)
509 BitTorrent::Session::instance()->decreaseTorrentsQueuePos(extractIDs(getSelectedTorrents()));
512 void TransferListWidget::topQueuePosSelectedTorrents()
514 if (m_mainWindow->currentTabWidget() == this)
515 BitTorrent::Session::instance()->topTorrentsQueuePos(extractIDs(getSelectedTorrents()));
518 void TransferListWidget::bottomQueuePosSelectedTorrents()
520 if (m_mainWindow->currentTabWidget() == this)
521 BitTorrent::Session::instance()->bottomTorrentsQueuePos(extractIDs(getSelectedTorrents()));
524 void TransferListWidget::copySelectedMagnetURIs() const
526 QStringList magnetUris;
527 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
528 magnetUris << torrent->createMagnetURI();
530 qApp->clipboard()->setText(magnetUris.join(u'\n'));
533 void TransferListWidget::copySelectedNames() const
535 QStringList torrentNames;
536 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
537 torrentNames << torrent->name();
539 qApp->clipboard()->setText(torrentNames.join(u'\n'));
542 void TransferListWidget::copySelectedInfohashes(const CopyInfohashPolicy policy) const
544 const auto selectedTorrents = getSelectedTorrents();
545 QStringList infoHashes;
546 infoHashes.reserve(selectedTorrents.size());
547 switch (policy)
549 case CopyInfohashPolicy::Version1:
550 for (const BitTorrent::Torrent *torrent : selectedTorrents)
552 if (const auto infoHash = torrent->infoHash().v1(); infoHash.isValid())
553 infoHashes << infoHash.toString();
555 break;
556 case CopyInfohashPolicy::Version2:
557 for (const BitTorrent::Torrent *torrent : selectedTorrents)
559 if (const auto infoHash = torrent->infoHash().v2(); infoHash.isValid())
560 infoHashes << infoHash.toString();
562 break;
565 qApp->clipboard()->setText(infoHashes.join(u'\n'));
568 void TransferListWidget::copySelectedIDs() const
570 QStringList torrentIDs;
571 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
572 torrentIDs << torrent->id().toString();
574 qApp->clipboard()->setText(torrentIDs.join(u'\n'));
577 void TransferListWidget::hideQueuePosColumn(bool hide)
579 setColumnHidden(TransferListModel::TR_QUEUE_POSITION, hide);
580 if (!hide && (columnWidth(TransferListModel::TR_QUEUE_POSITION) == 0))
581 resizeColumnToContents(TransferListModel::TR_QUEUE_POSITION);
584 void TransferListWidget::openSelectedTorrentsFolder() const
586 QSet<Path> paths;
587 #ifdef Q_OS_MACOS
588 // On macOS you expect both the files and folders to be opened in their parent
589 // folders prehilighted for opening, so we use a custom method.
590 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
592 const Path contentPath = torrent->contentPath();
593 paths.insert(!contentPath.isEmpty() ? contentPath : torrent->savePath());
595 MacUtils::openFiles(PathList(paths.cbegin(), paths.cend()));
596 #else
597 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
599 const Path contentPath = torrent->contentPath();
600 const Path openedPath = (!contentPath.isEmpty() ? contentPath : torrent->savePath());
601 if (!paths.contains(openedPath))
603 if (torrent->filesCount() == 1)
604 Utils::Gui::openFolderSelect(openedPath);
605 else
606 Utils::Gui::openPath(openedPath);
608 paths.insert(openedPath);
610 #endif // Q_OS_MACOS
613 void TransferListWidget::previewSelectedTorrents()
615 for (const BitTorrent::Torrent *torrent : asConst(getSelectedTorrents()))
617 if (torrentContainsPreviewableFiles(torrent))
619 auto *dialog = new PreviewSelectDialog(this, torrent);
620 dialog->setAttribute(Qt::WA_DeleteOnClose);
621 connect(dialog, &PreviewSelectDialog::readyToPreviewFile, this, &TransferListWidget::previewFile);
622 dialog->show();
624 else
626 QMessageBox::critical(this, tr("Unable to preview"), tr("The selected torrent \"%1\" does not contain previewable files")
627 .arg(torrent->name()));
632 void TransferListWidget::setTorrentOptions()
634 const QVector<BitTorrent::Torrent *> selectedTorrents = getSelectedTorrents();
635 if (selectedTorrents.empty()) return;
637 auto *dialog = new TorrentOptionsDialog {this, selectedTorrents};
638 dialog->setAttribute(Qt::WA_DeleteOnClose);
639 dialog->open();
642 void TransferListWidget::recheckSelectedTorrents()
644 if (Preferences::instance()->confirmTorrentRecheck())
646 QMessageBox::StandardButton ret = QMessageBox::question(this, tr("Recheck confirmation"), tr("Are you sure you want to recheck the selected torrent(s)?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
647 if (ret != QMessageBox::Yes) return;
650 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
651 torrent->forceRecheck();
654 void TransferListWidget::reannounceSelectedTorrents()
656 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
657 torrent->forceReannounce();
660 int TransferListWidget::visibleColumnsCount() const
662 int count = 0;
663 for (int i = 0, iMax = header()->count(); i < iMax; ++i)
665 if (!isColumnHidden(i))
666 ++count;
669 return count;
672 // hide/show columns menu
673 void TransferListWidget::displayColumnHeaderMenu()
675 auto *menu = new QMenu(this);
676 menu->setAttribute(Qt::WA_DeleteOnClose);
677 menu->setTitle(tr("Column visibility"));
678 menu->setToolTipsVisible(true);
680 for (int i = 0; i < TransferListModel::NB_COLUMNS; ++i)
682 if (!BitTorrent::Session::instance()->isQueueingSystemEnabled() && (i == TransferListModel::TR_QUEUE_POSITION))
683 continue;
685 const auto columnName = m_listModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString();
686 QAction *action = menu->addAction(columnName, this, [this, i](const bool checked)
688 if (!checked && (visibleColumnsCount() <= 1))
689 return;
691 setColumnHidden(i, !checked);
693 if (checked && (columnWidth(i) <= 5))
694 resizeColumnToContents(i);
696 saveSettings();
698 action->setCheckable(true);
699 action->setChecked(!isColumnHidden(i));
702 menu->addSeparator();
703 QAction *resizeAction = menu->addAction(tr("Resize columns"), this, [this]()
705 for (int i = 0, count = header()->count(); i < count; ++i)
707 if (!isColumnHidden(i))
708 resizeColumnToContents(i);
710 saveSettings();
712 resizeAction->setToolTip(tr("Resize all non-hidden columns to the size of their contents"));
714 menu->popup(QCursor::pos());
717 void TransferListWidget::setSelectedTorrentsSuperSeeding(const bool enabled) const
719 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
721 if (torrent->hasMetadata())
722 torrent->setSuperSeeding(enabled);
726 void TransferListWidget::setSelectedTorrentsSequentialDownload(const bool enabled) const
728 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
729 torrent->setSequentialDownload(enabled);
732 void TransferListWidget::setSelectedFirstLastPiecePrio(const bool enabled) const
734 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
735 torrent->setFirstLastPiecePriority(enabled);
738 void TransferListWidget::setSelectedAutoTMMEnabled(const bool enabled)
740 if (enabled)
742 const QMessageBox::StandardButton btn = QMessageBox::question(this, tr("Enable automatic torrent management")
743 , tr("Are you sure you want to enable Automatic Torrent Management for the selected torrent(s)? They may be relocated.")
744 , (QMessageBox::Yes | QMessageBox::No), QMessageBox::Yes);
745 if (btn != QMessageBox::Yes) return;
748 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
749 torrent->setAutoTMMEnabled(enabled);
752 void TransferListWidget::askNewCategoryForSelection()
754 const QString newCategoryName = TorrentCategoryDialog::createCategory(this);
755 if (!newCategoryName.isEmpty())
756 setSelectionCategory(newCategoryName);
759 void TransferListWidget::askAddTagsForSelection()
761 const QStringList tags = askTagsForSelection(tr("Add Tags"));
762 for (const QString &tag : tags)
763 addSelectionTag(tag);
766 void TransferListWidget::editTorrentTrackers()
768 const QVector<BitTorrent::Torrent *> torrents = getSelectedTorrents();
769 QVector<BitTorrent::TrackerEntry> commonTrackers;
771 if (!torrents.empty())
773 commonTrackers = torrents[0]->trackers();
775 for (const BitTorrent::Torrent *torrent : torrents)
777 QSet<BitTorrent::TrackerEntry> trackerSet;
779 for (const BitTorrent::TrackerEntry &entry : asConst(torrent->trackers()))
780 trackerSet.insert(entry);
782 commonTrackers.erase(std::remove_if(commonTrackers.begin(), commonTrackers.end()
783 , [&trackerSet](const BitTorrent::TrackerEntry &entry) { return !trackerSet.contains(entry); })
784 , commonTrackers.end());
788 auto *trackerDialog = new TrackerEntriesDialog(this);
789 trackerDialog->setAttribute(Qt::WA_DeleteOnClose);
790 trackerDialog->setTrackers(commonTrackers);
792 connect(trackerDialog, &QDialog::accepted, this, [torrents, trackerDialog]()
794 for (BitTorrent::Torrent *torrent : torrents)
795 torrent->replaceTrackers(trackerDialog->trackers());
798 trackerDialog->open();
801 void TransferListWidget::exportTorrent()
803 if (getSelectedTorrents().isEmpty())
804 return;
806 auto *fileDialog = new QFileDialog(this, tr("Choose folder to save exported .torrent files"));
807 fileDialog->setAttribute(Qt::WA_DeleteOnClose);
808 fileDialog->setFileMode(QFileDialog::Directory);
809 fileDialog->setOptions(QFileDialog::ShowDirsOnly);
810 connect(fileDialog, &QFileDialog::fileSelected, this, [this](const QString &dir)
812 const QVector<BitTorrent::Torrent *> torrents = getSelectedTorrents();
813 if (torrents.isEmpty())
814 return;
816 const Path savePath {dir};
817 if (!savePath.exists())
818 return;
820 const QString errorMsg = tr("Export .torrent file failed. Torrent: \"%1\". Save path: \"%2\". Reason: \"%3\"");
822 bool hasError = false;
823 for (const BitTorrent::Torrent *torrent : torrents)
825 const QString validName = Utils::Fs::toValidFileName(torrent->name(), u"_"_s);
826 const Path filePath = savePath / Path(validName + u".torrent");
827 if (filePath.exists())
829 LogMsg(errorMsg.arg(torrent->name(), filePath.toString(), tr("A file with the same name already exists")) , Log::WARNING);
830 hasError = true;
831 continue;
834 const nonstd::expected<void, QString> result = torrent->exportToFile(filePath);
835 if (!result)
837 LogMsg(errorMsg.arg(torrent->name(), filePath.toString(), result.error()) , Log::WARNING);
838 hasError = true;
839 continue;
843 if (hasError)
845 QMessageBox::warning(this, tr("Export .torrent file error")
846 , tr("Errors occurred when exporting .torrent files. Check execution log for details."));
850 fileDialog->open();
853 void TransferListWidget::confirmRemoveAllTagsForSelection()
855 QMessageBox::StandardButton response = QMessageBox::question(
856 this, tr("Remove All Tags"), tr("Remove all tags from selected torrents?"),
857 QMessageBox::Yes | QMessageBox::No);
858 if (response == QMessageBox::Yes)
859 clearSelectionTags();
862 QStringList TransferListWidget::askTagsForSelection(const QString &dialogTitle)
864 QStringList tags;
865 bool invalid = true;
866 while (invalid)
868 bool ok = false;
869 invalid = false;
870 const QString tagsInput = AutoExpandableDialog::getText(
871 this, dialogTitle, tr("Comma-separated tags:"), QLineEdit::Normal, {}, &ok).trimmed();
872 if (!ok || tagsInput.isEmpty())
873 return {};
874 tags = tagsInput.split(u',', Qt::SkipEmptyParts);
875 for (QString &tag : tags)
877 tag = tag.trimmed();
878 if (!BitTorrent::Session::isValidTag(tag))
880 QMessageBox::warning(this, tr("Invalid tag")
881 , tr("Tag name: '%1' is invalid").arg(tag));
882 invalid = true;
886 return tags;
889 void TransferListWidget::applyToSelectedTorrents(const std::function<void (BitTorrent::Torrent *const)> &fn)
891 // Changing the data may affect the layout of the sort/filter model, which in turn may invalidate
892 // the indexes previously obtained from selection model before we process them all.
893 // Therefore, we must map all the selected indexes to source before start processing them.
894 const QModelIndexList sourceRows = mapToSource(selectionModel()->selectedRows());
895 for (const QModelIndex &index : sourceRows)
897 BitTorrent::Torrent *const torrent = m_listModel->torrentHandle(index);
898 Q_ASSERT(torrent);
899 fn(torrent);
903 void TransferListWidget::renameSelectedTorrent()
905 const QModelIndexList selectedIndexes = selectionModel()->selectedRows();
906 if ((selectedIndexes.size() != 1) || !selectedIndexes.first().isValid())
907 return;
909 const QModelIndex mi = m_listModel->index(mapToSource(selectedIndexes.first()).row(), TransferListModel::TR_NAME);
910 BitTorrent::Torrent *const torrent = m_listModel->torrentHandle(mi);
911 if (!torrent)
912 return;
914 // Ask for a new Name
915 bool ok = false;
916 QString name = AutoExpandableDialog::getText(this, tr("Rename"), tr("New name:"), QLineEdit::Normal, torrent->name(), &ok);
917 if (ok && !name.isEmpty())
919 name.replace(QRegularExpression(u"\r?\n|\r"_s), u" "_s);
920 // Rename the torrent
921 m_listModel->setData(mi, name, Qt::DisplayRole);
925 void TransferListWidget::setSelectionCategory(const QString &category)
927 applyToSelectedTorrents([&category](BitTorrent::Torrent *torrent) { torrent->setCategory(category); });
930 void TransferListWidget::addSelectionTag(const QString &tag)
932 applyToSelectedTorrents([&tag](BitTorrent::Torrent *const torrent) { torrent->addTag(tag); });
935 void TransferListWidget::removeSelectionTag(const QString &tag)
937 applyToSelectedTorrents([&tag](BitTorrent::Torrent *const torrent) { torrent->removeTag(tag); });
940 void TransferListWidget::clearSelectionTags()
942 applyToSelectedTorrents([](BitTorrent::Torrent *const torrent) { torrent->removeAllTags(); });
945 void TransferListWidget::displayListMenu()
947 const QModelIndexList selectedIndexes = selectionModel()->selectedRows();
948 if (selectedIndexes.isEmpty())
949 return;
951 auto *listMenu = new QMenu(this);
952 listMenu->setAttribute(Qt::WA_DeleteOnClose);
953 listMenu->setToolTipsVisible(true);
955 // Create actions
957 auto *actionStart = new QAction(UIThemeManager::instance()->getIcon(u"torrent-start"_s, u"media-playback-start"_s), tr("&Resume", "Resume/start the torrent"), listMenu);
958 connect(actionStart, &QAction::triggered, this, &TransferListWidget::startSelectedTorrents);
959 auto *actionPause = new QAction(UIThemeManager::instance()->getIcon(u"torrent-stop"_s, u"media-playback-pause"_s), tr("&Pause", "Pause the torrent"), listMenu);
960 connect(actionPause, &QAction::triggered, this, &TransferListWidget::pauseSelectedTorrents);
961 auto *actionForceStart = new QAction(UIThemeManager::instance()->getIcon(u"torrent-start-forced"_s, u"media-playback-start"_s), tr("Force Resu&me", "Force Resume/start the torrent"), listMenu);
962 connect(actionForceStart, &QAction::triggered, this, &TransferListWidget::forceStartSelectedTorrents);
963 auto *actionDelete = new QAction(UIThemeManager::instance()->getIcon(u"list-remove"_s), tr("&Remove", "Remove the torrent"), listMenu);
964 connect(actionDelete, &QAction::triggered, this, &TransferListWidget::softDeleteSelectedTorrents);
965 auto *actionPreviewFile = new QAction(UIThemeManager::instance()->getIcon(u"view-preview"_s), tr("Pre&view file..."), listMenu);
966 connect(actionPreviewFile, &QAction::triggered, this, &TransferListWidget::previewSelectedTorrents);
967 auto *actionTorrentOptions = new QAction(UIThemeManager::instance()->getIcon(u"configure"_s), tr("Torrent &options..."), listMenu);
968 connect(actionTorrentOptions, &QAction::triggered, this, &TransferListWidget::setTorrentOptions);
969 auto *actionOpenDestinationFolder = new QAction(UIThemeManager::instance()->getIcon(u"directory"_s), tr("Open destination &folder"), listMenu);
970 connect(actionOpenDestinationFolder, &QAction::triggered, this, &TransferListWidget::openSelectedTorrentsFolder);
971 auto *actionIncreaseQueuePos = new QAction(UIThemeManager::instance()->getIcon(u"go-up"_s), tr("Move &up", "i.e. move up in the queue"), listMenu);
972 connect(actionIncreaseQueuePos, &QAction::triggered, this, &TransferListWidget::increaseQueuePosSelectedTorrents);
973 auto *actionDecreaseQueuePos = new QAction(UIThemeManager::instance()->getIcon(u"go-down"_s), tr("Move &down", "i.e. Move down in the queue"), listMenu);
974 connect(actionDecreaseQueuePos, &QAction::triggered, this, &TransferListWidget::decreaseQueuePosSelectedTorrents);
975 auto *actionTopQueuePos = new QAction(UIThemeManager::instance()->getIcon(u"go-top"_s), tr("Move to &top", "i.e. Move to top of the queue"), listMenu);
976 connect(actionTopQueuePos, &QAction::triggered, this, &TransferListWidget::topQueuePosSelectedTorrents);
977 auto *actionBottomQueuePos = new QAction(UIThemeManager::instance()->getIcon(u"go-bottom"_s), tr("Move to &bottom", "i.e. Move to bottom of the queue"), listMenu);
978 connect(actionBottomQueuePos, &QAction::triggered, this, &TransferListWidget::bottomQueuePosSelectedTorrents);
979 auto *actionSetTorrentPath = new QAction(UIThemeManager::instance()->getIcon(u"set-location"_s, u"inode-directory"_s), tr("Set loc&ation..."), listMenu);
980 connect(actionSetTorrentPath, &QAction::triggered, this, &TransferListWidget::setSelectedTorrentsLocation);
981 auto *actionForceRecheck = new QAction(UIThemeManager::instance()->getIcon(u"force-recheck"_s, u"document-edit-verify"_s), tr("Force rec&heck"), listMenu);
982 connect(actionForceRecheck, &QAction::triggered, this, &TransferListWidget::recheckSelectedTorrents);
983 auto *actionForceReannounce = new QAction(UIThemeManager::instance()->getIcon(u"reannounce"_s, u"document-edit-verify"_s), tr("Force r&eannounce"), listMenu);
984 connect(actionForceReannounce, &QAction::triggered, this, &TransferListWidget::reannounceSelectedTorrents);
985 auto *actionCopyMagnetLink = new QAction(UIThemeManager::instance()->getIcon(u"torrent-magnet"_s, u"kt-magnet"_s), tr("&Magnet link"), listMenu);
986 connect(actionCopyMagnetLink, &QAction::triggered, this, &TransferListWidget::copySelectedMagnetURIs);
987 auto *actionCopyID = new QAction(UIThemeManager::instance()->getIcon(u"help-about"_s, u"edit-copy"_s), tr("Torrent &ID"), listMenu);
988 connect(actionCopyID, &QAction::triggered, this, &TransferListWidget::copySelectedIDs);
989 auto *actionCopyName = new QAction(UIThemeManager::instance()->getIcon(u"name"_s, u"edit-copy"_s), tr("&Name"), listMenu);
990 connect(actionCopyName, &QAction::triggered, this, &TransferListWidget::copySelectedNames);
991 auto *actionCopyHash1 = new QAction(UIThemeManager::instance()->getIcon(u"hash"_s, u"edit-copy"_s), tr("Info &hash v1"), listMenu);
992 connect(actionCopyHash1, &QAction::triggered, this, [this]() { copySelectedInfohashes(CopyInfohashPolicy::Version1); });
993 auto *actionCopyHash2 = new QAction(UIThemeManager::instance()->getIcon(u"hash"_s, u"edit-copy"_s), tr("Info h&ash v2"), listMenu);
994 connect(actionCopyHash2, &QAction::triggered, this, [this]() { copySelectedInfohashes(CopyInfohashPolicy::Version2); });
995 auto *actionSuperSeedingMode = new TriStateAction(tr("Super seeding mode"), listMenu);
996 connect(actionSuperSeedingMode, &QAction::triggered, this, &TransferListWidget::setSelectedTorrentsSuperSeeding);
997 auto *actionRename = new QAction(UIThemeManager::instance()->getIcon(u"edit-rename"_s), tr("Re&name..."), listMenu);
998 connect(actionRename, &QAction::triggered, this, &TransferListWidget::renameSelectedTorrent);
999 auto *actionSequentialDownload = new TriStateAction(tr("Download in sequential order"), listMenu);
1000 connect(actionSequentialDownload, &QAction::triggered, this, &TransferListWidget::setSelectedTorrentsSequentialDownload);
1001 auto *actionFirstLastPiecePrio = new TriStateAction(tr("Download first and last pieces first"), listMenu);
1002 connect(actionFirstLastPiecePrio, &QAction::triggered, this, &TransferListWidget::setSelectedFirstLastPiecePrio);
1003 auto *actionAutoTMM = new TriStateAction(tr("Automatic Torrent Management"), listMenu);
1004 actionAutoTMM->setToolTip(tr("Automatic mode means that various torrent properties (e.g. save path) will be decided by the associated category"));
1005 connect(actionAutoTMM, &QAction::triggered, this, &TransferListWidget::setSelectedAutoTMMEnabled);
1006 auto *actionEditTracker = new QAction(UIThemeManager::instance()->getIcon(u"edit-rename"_s), tr("Edit trac&kers..."), listMenu);
1007 connect(actionEditTracker, &QAction::triggered, this, &TransferListWidget::editTorrentTrackers);
1008 auto *actionExportTorrent = new QAction(UIThemeManager::instance()->getIcon(u"edit-copy"_s), tr("E&xport .torrent..."), listMenu);
1009 connect(actionExportTorrent, &QAction::triggered, this, &TransferListWidget::exportTorrent);
1010 // End of actions
1012 // Enable/disable pause/start action given the DL state
1013 bool needsPause = false, needsStart = false, needsForce = false, needsPreview = false;
1014 bool allSameSuperSeeding = true;
1015 bool superSeedingMode = false;
1016 bool allSameSequentialDownloadMode = true, allSamePrioFirstlast = true;
1017 bool sequentialDownloadMode = false, prioritizeFirstLast = false;
1018 bool oneHasMetadata = false, oneNotFinished = false;
1019 bool allSameCategory = true;
1020 bool allSameAutoTMM = true;
1021 bool firstAutoTMM = false;
1022 QString firstCategory;
1023 bool first = true;
1024 TagSet tagsInAny;
1025 TagSet tagsInAll;
1026 bool hasInfohashV1 = false, hasInfohashV2 = false;
1027 bool oneCanForceReannounce = false;
1029 for (const QModelIndex &index : selectedIndexes)
1031 // Get the file name
1032 // Get handle and pause the torrent
1033 const BitTorrent::Torrent *torrent = m_listModel->torrentHandle(mapToSource(index));
1034 if (!torrent) continue;
1036 if (firstCategory.isEmpty() && first)
1037 firstCategory = torrent->category();
1038 if (firstCategory != torrent->category())
1039 allSameCategory = false;
1041 const TagSet torrentTags = torrent->tags();
1042 tagsInAny.unite(torrentTags);
1044 if (first)
1046 firstAutoTMM = torrent->isAutoTMMEnabled();
1047 tagsInAll = torrentTags;
1049 else
1051 tagsInAll.intersect(torrentTags);
1054 if (firstAutoTMM != torrent->isAutoTMMEnabled())
1055 allSameAutoTMM = false;
1057 if (torrent->hasMetadata())
1058 oneHasMetadata = true;
1059 if (!torrent->isFinished())
1061 oneNotFinished = true;
1062 if (first)
1064 sequentialDownloadMode = torrent->isSequentialDownload();
1065 prioritizeFirstLast = torrent->hasFirstLastPiecePriority();
1067 else
1069 if (sequentialDownloadMode != torrent->isSequentialDownload())
1070 allSameSequentialDownloadMode = false;
1071 if (prioritizeFirstLast != torrent->hasFirstLastPiecePriority())
1072 allSamePrioFirstlast = false;
1075 else
1077 if (!oneNotFinished && allSameSuperSeeding && torrent->hasMetadata())
1079 if (first)
1080 superSeedingMode = torrent->superSeeding();
1081 else if (superSeedingMode != torrent->superSeeding())
1082 allSameSuperSeeding = false;
1086 if (!torrent->isForced())
1087 needsForce = true;
1088 else
1089 needsStart = true;
1091 const bool isPaused = torrent->isPaused();
1092 if (isPaused)
1093 needsStart = true;
1094 else
1095 needsPause = true;
1097 if (torrent->isErrored() || torrent->hasMissingFiles())
1099 // If torrent is in "errored" or "missing files" state
1100 // it cannot keep further processing until you restart it.
1101 needsStart = true;
1102 needsForce = true;
1105 if (torrent->hasMetadata())
1106 needsPreview = true;
1108 if (!hasInfohashV1 && torrent->infoHash().v1().isValid())
1109 hasInfohashV1 = true;
1110 if (!hasInfohashV2 && torrent->infoHash().v2().isValid())
1111 hasInfohashV2 = true;
1113 first = false;
1115 const bool rechecking = torrent->isChecking();
1116 if (rechecking)
1118 needsStart = true;
1119 needsPause = true;
1122 const bool queued = (BitTorrent::Session::instance()->isQueueingSystemEnabled() && torrent->isQueued());
1124 if (!isPaused && !rechecking && !queued)
1125 oneCanForceReannounce = true;
1127 if (oneHasMetadata && oneNotFinished && !allSameSequentialDownloadMode
1128 && !allSamePrioFirstlast && !allSameSuperSeeding && !allSameCategory
1129 && needsStart && needsForce && needsPause && needsPreview && !allSameAutoTMM
1130 && hasInfohashV1 && hasInfohashV2 && oneCanForceReannounce)
1132 break;
1136 if (needsStart)
1137 listMenu->addAction(actionStart);
1138 if (needsPause)
1139 listMenu->addAction(actionPause);
1140 if (needsForce)
1141 listMenu->addAction(actionForceStart);
1142 listMenu->addSeparator();
1143 listMenu->addAction(actionDelete);
1144 listMenu->addSeparator();
1145 listMenu->addAction(actionSetTorrentPath);
1146 if (selectedIndexes.size() == 1)
1147 listMenu->addAction(actionRename);
1148 listMenu->addAction(actionEditTracker);
1150 // Category Menu
1151 QStringList categories = BitTorrent::Session::instance()->categories();
1152 std::sort(categories.begin(), categories.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>());
1154 QMenu *categoryMenu = listMenu->addMenu(UIThemeManager::instance()->getIcon(u"view-categories"_s), tr("Categor&y"));
1156 categoryMenu->addAction(UIThemeManager::instance()->getIcon(u"list-add"_s), tr("&New...", "New category...")
1157 , this, &TransferListWidget::askNewCategoryForSelection);
1158 categoryMenu->addAction(UIThemeManager::instance()->getIcon(u"edit-clear"_s), tr("&Reset", "Reset category")
1159 , this, [this]() { setSelectionCategory(u""_s); });
1160 categoryMenu->addSeparator();
1162 for (const QString &category : asConst(categories))
1164 const QString escapedCategory = QString(category).replace(u'&', u"&&"_s); // avoid '&' becomes accelerator key
1165 QAction *categoryAction = categoryMenu->addAction(UIThemeManager::instance()->getIcon(u"view-categories"_s), escapedCategory
1166 , this, [this, category]() { setSelectionCategory(category); });
1168 if (allSameCategory && (category == firstCategory))
1170 categoryAction->setCheckable(true);
1171 categoryAction->setChecked(true);
1175 // Tag Menu
1176 QStringList tags(BitTorrent::Session::instance()->tags().values());
1177 std::sort(tags.begin(), tags.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>());
1179 QMenu *tagsMenu = listMenu->addMenu(UIThemeManager::instance()->getIcon(u"tags"_s, u"view-categories"_s), tr("Ta&gs"));
1181 tagsMenu->addAction(UIThemeManager::instance()->getIcon(u"list-add"_s), tr("&Add...", "Add / assign multiple tags...")
1182 , this, &TransferListWidget::askAddTagsForSelection);
1183 tagsMenu->addAction(UIThemeManager::instance()->getIcon(u"edit-clear"_s), tr("&Remove All", "Remove all tags")
1184 , this, [this]()
1186 if (Preferences::instance()->confirmRemoveAllTags())
1187 confirmRemoveAllTagsForSelection();
1188 else
1189 clearSelectionTags();
1191 tagsMenu->addSeparator();
1193 for (const QString &tag : asConst(tags))
1195 auto *action = new TriStateAction(Utils::Gui::tagToWidgetText(tag), tagsMenu);
1196 action->setCloseOnInteraction(false);
1198 const Qt::CheckState initialState = tagsInAll.contains(tag) ? Qt::Checked
1199 : tagsInAny.contains(tag) ? Qt::PartiallyChecked : Qt::Unchecked;
1200 action->setCheckState(initialState);
1202 connect(action, &QAction::toggled, this, [this, tag](const bool checked)
1204 if (checked)
1205 addSelectionTag(tag);
1206 else
1207 removeSelectionTag(tag);
1210 tagsMenu->addAction(action);
1213 actionAutoTMM->setCheckState(allSameAutoTMM
1214 ? (firstAutoTMM ? Qt::Checked : Qt::Unchecked)
1215 : Qt::PartiallyChecked);
1216 listMenu->addAction(actionAutoTMM);
1218 listMenu->addSeparator();
1219 listMenu->addAction(actionTorrentOptions);
1220 if (!oneNotFinished && oneHasMetadata)
1222 actionSuperSeedingMode->setCheckState(allSameSuperSeeding
1223 ? (superSeedingMode ? Qt::Checked : Qt::Unchecked)
1224 : Qt::PartiallyChecked);
1225 listMenu->addAction(actionSuperSeedingMode);
1227 listMenu->addSeparator();
1228 bool addedPreviewAction = false;
1229 if (needsPreview)
1231 listMenu->addAction(actionPreviewFile);
1232 addedPreviewAction = true;
1234 if (oneNotFinished)
1236 actionSequentialDownload->setCheckState(allSameSequentialDownloadMode
1237 ? (sequentialDownloadMode ? Qt::Checked : Qt::Unchecked)
1238 : Qt::PartiallyChecked);
1239 listMenu->addAction(actionSequentialDownload);
1241 actionFirstLastPiecePrio->setCheckState(allSamePrioFirstlast
1242 ? (prioritizeFirstLast ? Qt::Checked : Qt::Unchecked)
1243 : Qt::PartiallyChecked);
1244 listMenu->addAction(actionFirstLastPiecePrio);
1246 addedPreviewAction = true;
1249 if (addedPreviewAction)
1250 listMenu->addSeparator();
1251 if (oneHasMetadata)
1252 listMenu->addAction(actionForceRecheck);
1253 // We can not force reannounce torrents that are paused/errored/checking/missing files/queued.
1254 // We may already have the tracker list from magnet url. So we can force reannounce torrents without metadata anyway.
1255 listMenu->addAction(actionForceReannounce);
1256 actionForceReannounce->setEnabled(oneCanForceReannounce);
1257 if (!oneCanForceReannounce)
1258 actionForceReannounce->setToolTip(tr("Can not force reannounce if torrent is Paused/Queued/Errored/Checking"));
1259 listMenu->addSeparator();
1260 listMenu->addAction(actionOpenDestinationFolder);
1261 if (BitTorrent::Session::instance()->isQueueingSystemEnabled() && oneNotFinished)
1263 listMenu->addSeparator();
1264 QMenu *queueMenu = listMenu->addMenu(
1265 UIThemeManager::instance()->getIcon(u"queued"_s), tr("&Queue"));
1266 queueMenu->addAction(actionTopQueuePos);
1267 queueMenu->addAction(actionIncreaseQueuePos);
1268 queueMenu->addAction(actionDecreaseQueuePos);
1269 queueMenu->addAction(actionBottomQueuePos);
1272 QMenu *copySubMenu = listMenu->addMenu(UIThemeManager::instance()->getIcon(u"edit-copy"_s), tr("&Copy"));
1273 copySubMenu->addAction(actionCopyName);
1274 copySubMenu->addAction(actionCopyHash1);
1275 actionCopyHash1->setEnabled(hasInfohashV1);
1276 copySubMenu->addAction(actionCopyHash2);
1277 actionCopyHash2->setEnabled(hasInfohashV2);
1278 copySubMenu->addAction(actionCopyMagnetLink);
1279 copySubMenu->addAction(actionCopyID);
1281 actionExportTorrent->setToolTip(tr("Exported torrent is not necessarily the same as the imported"));
1282 listMenu->addAction(actionExportTorrent);
1284 listMenu->popup(QCursor::pos());
1287 void TransferListWidget::currentChanged(const QModelIndex &current, const QModelIndex&)
1289 qDebug("CURRENT CHANGED");
1290 BitTorrent::Torrent *torrent = nullptr;
1291 if (current.isValid())
1293 torrent = m_listModel->torrentHandle(mapToSource(current));
1294 // Fix scrolling to the lowermost visible torrent
1295 QMetaObject::invokeMethod(this, [this, current] { scrollTo(current); }, Qt::QueuedConnection);
1297 emit currentTorrentChanged(torrent);
1300 void TransferListWidget::applyCategoryFilter(const QString &category)
1302 if (category.isNull())
1303 m_sortFilterModel->disableCategoryFilter();
1304 else
1305 m_sortFilterModel->setCategoryFilter(category);
1308 void TransferListWidget::applyTagFilter(const QString &tag)
1310 if (tag.isNull())
1311 m_sortFilterModel->disableTagFilter();
1312 else
1313 m_sortFilterModel->setTagFilter(tag);
1316 void TransferListWidget::applyTrackerFilterAll()
1318 m_sortFilterModel->disableTrackerFilter();
1321 void TransferListWidget::applyTrackerFilter(const QSet<BitTorrent::TorrentID> &torrentIDs)
1323 m_sortFilterModel->setTrackerFilter(torrentIDs);
1326 void TransferListWidget::applyFilter(const QString &name, const TransferListModel::Column &type)
1328 m_sortFilterModel->setFilterKeyColumn(type);
1329 const QString pattern = (Preferences::instance()->getRegexAsFilteringPatternForTransferList()
1330 ? name : Utils::String::wildcardToRegexPattern(name));
1331 m_sortFilterModel->setFilterRegularExpression(QRegularExpression(pattern, QRegularExpression::CaseInsensitiveOption));
1334 void TransferListWidget::applyStatusFilter(const int filterIndex)
1336 const auto filterType = static_cast<TorrentFilter::Type>(filterIndex);
1337 m_sortFilterModel->setStatusFilter(((filterType >= TorrentFilter::All) && (filterType < TorrentFilter::_Count)) ? filterType : TorrentFilter::All);
1338 // Select first item if nothing is selected
1339 if (selectionModel()->selectedRows(0).empty() && (m_sortFilterModel->rowCount() > 0))
1341 qDebug("Nothing is selected, selecting first row: %s", qUtf8Printable(m_sortFilterModel->index(0, TransferListModel::TR_NAME).data().toString()));
1342 selectionModel()->setCurrentIndex(m_sortFilterModel->index(0, TransferListModel::TR_NAME), QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
1346 void TransferListWidget::saveSettings()
1348 Preferences::instance()->setTransHeaderState(header()->saveState());
1351 bool TransferListWidget::loadSettings()
1353 return header()->restoreState(Preferences::instance()->getTransHeaderState());
1356 void TransferListWidget::wheelEvent(QWheelEvent *event)
1358 if (event->modifiers() & Qt::ShiftModifier)
1360 // Shift + scroll = horizontal scroll
1361 event->accept();
1362 QWheelEvent scrollHEvent {event->position(), event->globalPosition()
1363 , event->pixelDelta(), event->angleDelta().transposed(), event->buttons()
1364 , event->modifiers(), event->phase(), event->inverted(), event->source()};
1365 QTreeView::wheelEvent(&scrollHEvent);
1366 return;
1369 QTreeView::wheelEvent(event); // event delegated to base class