Add copy comment functionality to the torrent list's context menu
[qBittorrent.git] / src / gui / transferlistwidget.cpp
blobc731775db6ade8318c5236721db6d0ab67b60cb9
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "transferlistwidget.h"
31 #include <algorithm>
33 #include <QClipboard>
34 #include <QDebug>
35 #include <QFileDialog>
36 #include <QHeaderView>
37 #include <QMenu>
38 #include <QMessageBox>
39 #include <QRegularExpression>
40 #include <QSet>
41 #include <QShortcut>
42 #include <QVector>
43 #include <QWheelEvent>
45 #include "base/bittorrent/session.h"
46 #include "base/bittorrent/torrent.h"
47 #include "base/bittorrent/trackerentry.h"
48 #include "base/global.h"
49 #include "base/logger.h"
50 #include "base/path.h"
51 #include "base/preferences.h"
52 #include "base/torrentfilter.h"
53 #include "base/utils/compare.h"
54 #include "base/utils/fs.h"
55 #include "base/utils/misc.h"
56 #include "base/utils/string.h"
57 #include "autoexpandabledialog.h"
58 #include "deletionconfirmationdialog.h"
59 #include "mainwindow.h"
60 #include "optionsdialog.h"
61 #include "previewselectdialog.h"
62 #include "speedlimitdialog.h"
63 #include "torrentcategorydialog.h"
64 #include "torrentoptionsdialog.h"
65 #include "trackerentriesdialog.h"
66 #include "transferlistdelegate.h"
67 #include "transferlistsortmodel.h"
68 #include "tristateaction.h"
69 #include "uithememanager.h"
70 #include "utils.h"
72 #ifdef Q_OS_MACOS
73 #include "macutilities.h"
74 #endif
76 namespace
78 QVector<BitTorrent::TorrentID> extractIDs(const QVector<BitTorrent::Torrent *> &torrents)
80 QVector<BitTorrent::TorrentID> torrentIDs;
81 torrentIDs.reserve(torrents.size());
82 for (const BitTorrent::Torrent *torrent : torrents)
83 torrentIDs << torrent->id();
84 return torrentIDs;
87 bool torrentContainsPreviewableFiles(const BitTorrent::Torrent *const torrent)
89 if (!torrent->hasMetadata())
90 return false;
92 for (const Path &filePath : asConst(torrent->filePaths()))
94 if (Utils::Misc::isPreviewable(filePath))
95 return true;
98 return false;
101 void openDestinationFolder(const BitTorrent::Torrent *const torrent)
103 const Path contentPath = torrent->contentPath();
104 const Path openedPath = (!contentPath.isEmpty() ? contentPath : torrent->savePath());
105 #ifdef Q_OS_MACOS
106 MacUtils::openFiles({openedPath});
107 #else
108 if (torrent->filesCount() == 1)
109 Utils::Gui::openFolderSelect(openedPath);
110 else
111 Utils::Gui::openPath(openedPath);
112 #endif
115 void removeTorrents(const QVector<BitTorrent::Torrent *> &torrents, const bool isDeleteFileSelected)
117 auto *session = BitTorrent::Session::instance();
118 const DeleteOption deleteOption = isDeleteFileSelected ? DeleteTorrentAndFiles : DeleteTorrent;
119 for (const BitTorrent::Torrent *torrent : torrents)
120 session->deleteTorrent(torrent->id(), deleteOption);
124 TransferListWidget::TransferListWidget(QWidget *parent, MainWindow *mainWindow)
125 : QTreeView {parent}
126 , m_listModel {new TransferListModel {this}}
127 , m_sortFilterModel {new TransferListSortModel {this}}
128 , m_mainWindow {mainWindow}
130 // Load settings
131 const bool columnLoaded = loadSettings();
133 // Create and apply delegate
134 setItemDelegate(new TransferListDelegate {this});
136 m_sortFilterModel->setDynamicSortFilter(true);
137 m_sortFilterModel->setSourceModel(m_listModel);
138 m_sortFilterModel->setFilterKeyColumn(TransferListModel::TR_NAME);
139 m_sortFilterModel->setFilterRole(Qt::DisplayRole);
140 m_sortFilterModel->setSortCaseSensitivity(Qt::CaseInsensitive);
141 m_sortFilterModel->setSortRole(TransferListModel::UnderlyingDataRole);
142 setModel(m_sortFilterModel);
144 // Visual settings
145 setUniformRowHeights(true);
146 setRootIsDecorated(false);
147 setAllColumnsShowFocus(true);
148 setSortingEnabled(true);
149 setSelectionMode(QAbstractItemView::ExtendedSelection);
150 setItemsExpandable(false);
151 setAutoScroll(true);
152 setDragDropMode(QAbstractItemView::DragOnly);
153 #if defined(Q_OS_MACOS)
154 setAttribute(Qt::WA_MacShowFocusRect, false);
155 #endif
156 header()->setFirstSectionMovable(true);
157 header()->setStretchLastSection(false);
158 header()->setTextElideMode(Qt::ElideRight);
160 // Default hidden columns
161 if (!columnLoaded)
163 setColumnHidden(TransferListModel::TR_ADD_DATE, true);
164 setColumnHidden(TransferListModel::TR_SEED_DATE, true);
165 setColumnHidden(TransferListModel::TR_UPLIMIT, true);
166 setColumnHidden(TransferListModel::TR_DLLIMIT, true);
167 setColumnHidden(TransferListModel::TR_TRACKER, true);
168 setColumnHidden(TransferListModel::TR_AMOUNT_DOWNLOADED, true);
169 setColumnHidden(TransferListModel::TR_AMOUNT_UPLOADED, true);
170 setColumnHidden(TransferListModel::TR_AMOUNT_DOWNLOADED_SESSION, true);
171 setColumnHidden(TransferListModel::TR_AMOUNT_UPLOADED_SESSION, true);
172 setColumnHidden(TransferListModel::TR_AMOUNT_LEFT, true);
173 setColumnHidden(TransferListModel::TR_TIME_ELAPSED, true);
174 setColumnHidden(TransferListModel::TR_SAVE_PATH, true);
175 setColumnHidden(TransferListModel::TR_DOWNLOAD_PATH, true);
176 setColumnHidden(TransferListModel::TR_INFOHASH_V1, true);
177 setColumnHidden(TransferListModel::TR_INFOHASH_V2, true);
178 setColumnHidden(TransferListModel::TR_COMPLETED, true);
179 setColumnHidden(TransferListModel::TR_RATIO_LIMIT, true);
180 setColumnHidden(TransferListModel::TR_SEEN_COMPLETE_DATE, true);
181 setColumnHidden(TransferListModel::TR_LAST_ACTIVITY, true);
182 setColumnHidden(TransferListModel::TR_TOTAL_SIZE, true);
183 setColumnHidden(TransferListModel::TR_REANNOUNCE, 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::copySelectedComments() const
579 QStringList torrentComments;
580 for (const BitTorrent::Torrent *torrent : asConst(getSelectedTorrents()))
582 if (!torrent->comment().isEmpty())
583 torrentComments << torrent->comment();
586 qApp->clipboard()->setText(torrentComments.join(u"\n---------\n"_s));
589 void TransferListWidget::hideQueuePosColumn(bool hide)
591 setColumnHidden(TransferListModel::TR_QUEUE_POSITION, hide);
592 if (!hide && (columnWidth(TransferListModel::TR_QUEUE_POSITION) == 0))
593 resizeColumnToContents(TransferListModel::TR_QUEUE_POSITION);
596 void TransferListWidget::openSelectedTorrentsFolder() const
598 QSet<Path> paths;
599 #ifdef Q_OS_MACOS
600 // On macOS you expect both the files and folders to be opened in their parent
601 // folders prehilighted for opening, so we use a custom method.
602 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
604 const Path contentPath = torrent->contentPath();
605 paths.insert(!contentPath.isEmpty() ? contentPath : torrent->savePath());
607 MacUtils::openFiles(PathList(paths.cbegin(), paths.cend()));
608 #else
609 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
611 const Path contentPath = torrent->contentPath();
612 const Path openedPath = (!contentPath.isEmpty() ? contentPath : torrent->savePath());
613 if (!paths.contains(openedPath))
615 if (torrent->filesCount() == 1)
616 Utils::Gui::openFolderSelect(openedPath);
617 else
618 Utils::Gui::openPath(openedPath);
620 paths.insert(openedPath);
622 #endif // Q_OS_MACOS
625 void TransferListWidget::previewSelectedTorrents()
627 for (const BitTorrent::Torrent *torrent : asConst(getSelectedTorrents()))
629 if (torrentContainsPreviewableFiles(torrent))
631 auto *dialog = new PreviewSelectDialog(this, torrent);
632 dialog->setAttribute(Qt::WA_DeleteOnClose);
633 connect(dialog, &PreviewSelectDialog::readyToPreviewFile, this, &TransferListWidget::previewFile);
634 dialog->show();
636 else
638 QMessageBox::critical(this, tr("Unable to preview"), tr("The selected torrent \"%1\" does not contain previewable files")
639 .arg(torrent->name()));
644 void TransferListWidget::setTorrentOptions()
646 const QVector<BitTorrent::Torrent *> selectedTorrents = getSelectedTorrents();
647 if (selectedTorrents.empty()) return;
649 auto *dialog = new TorrentOptionsDialog {this, selectedTorrents};
650 dialog->setAttribute(Qt::WA_DeleteOnClose);
651 dialog->open();
654 void TransferListWidget::recheckSelectedTorrents()
656 if (Preferences::instance()->confirmTorrentRecheck())
658 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);
659 if (ret != QMessageBox::Yes) return;
662 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
663 torrent->forceRecheck();
666 void TransferListWidget::reannounceSelectedTorrents()
668 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
669 torrent->forceReannounce();
672 int TransferListWidget::visibleColumnsCount() const
674 int count = 0;
675 for (int i = 0, iMax = header()->count(); i < iMax; ++i)
677 if (!isColumnHidden(i))
678 ++count;
681 return count;
684 // hide/show columns menu
685 void TransferListWidget::displayColumnHeaderMenu()
687 auto *menu = new QMenu(this);
688 menu->setAttribute(Qt::WA_DeleteOnClose);
689 menu->setTitle(tr("Column visibility"));
690 menu->setToolTipsVisible(true);
692 for (int i = 0; i < TransferListModel::NB_COLUMNS; ++i)
694 if (!BitTorrent::Session::instance()->isQueueingSystemEnabled() && (i == TransferListModel::TR_QUEUE_POSITION))
695 continue;
697 const auto columnName = m_listModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString();
698 QAction *action = menu->addAction(columnName, this, [this, i](const bool checked)
700 if (!checked && (visibleColumnsCount() <= 1))
701 return;
703 setColumnHidden(i, !checked);
705 if (checked && (columnWidth(i) <= 5))
706 resizeColumnToContents(i);
708 saveSettings();
710 action->setCheckable(true);
711 action->setChecked(!isColumnHidden(i));
714 menu->addSeparator();
715 QAction *resizeAction = menu->addAction(tr("Resize columns"), this, [this]()
717 for (int i = 0, count = header()->count(); i < count; ++i)
719 if (!isColumnHidden(i))
720 resizeColumnToContents(i);
722 saveSettings();
724 resizeAction->setToolTip(tr("Resize all non-hidden columns to the size of their contents"));
726 menu->popup(QCursor::pos());
729 void TransferListWidget::setSelectedTorrentsSuperSeeding(const bool enabled) const
731 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
733 if (torrent->hasMetadata())
734 torrent->setSuperSeeding(enabled);
738 void TransferListWidget::setSelectedTorrentsSequentialDownload(const bool enabled) const
740 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
741 torrent->setSequentialDownload(enabled);
744 void TransferListWidget::setSelectedFirstLastPiecePrio(const bool enabled) const
746 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
747 torrent->setFirstLastPiecePriority(enabled);
750 void TransferListWidget::setSelectedAutoTMMEnabled(const bool enabled)
752 if (enabled)
754 const QMessageBox::StandardButton btn = QMessageBox::question(this, tr("Enable automatic torrent management")
755 , tr("Are you sure you want to enable Automatic Torrent Management for the selected torrent(s)? They may be relocated.")
756 , (QMessageBox::Yes | QMessageBox::No), QMessageBox::Yes);
757 if (btn != QMessageBox::Yes) return;
760 for (BitTorrent::Torrent *const torrent : asConst(getSelectedTorrents()))
761 torrent->setAutoTMMEnabled(enabled);
764 void TransferListWidget::askNewCategoryForSelection()
766 const QString newCategoryName = TorrentCategoryDialog::createCategory(this);
767 if (!newCategoryName.isEmpty())
768 setSelectionCategory(newCategoryName);
771 void TransferListWidget::askAddTagsForSelection()
773 const QStringList tags = askTagsForSelection(tr("Add Tags"));
774 for (const QString &tag : tags)
775 addSelectionTag(tag);
778 void TransferListWidget::editTorrentTrackers()
780 const QVector<BitTorrent::Torrent *> torrents = getSelectedTorrents();
781 QVector<BitTorrent::TrackerEntry> commonTrackers;
783 if (!torrents.empty())
785 commonTrackers = torrents[0]->trackers();
787 for (const BitTorrent::Torrent *torrent : torrents)
789 QSet<BitTorrent::TrackerEntry> trackerSet;
791 for (const BitTorrent::TrackerEntry &entry : asConst(torrent->trackers()))
792 trackerSet.insert(entry);
794 commonTrackers.erase(std::remove_if(commonTrackers.begin(), commonTrackers.end()
795 , [&trackerSet](const BitTorrent::TrackerEntry &entry) { return !trackerSet.contains(entry); })
796 , commonTrackers.end());
800 auto *trackerDialog = new TrackerEntriesDialog(this);
801 trackerDialog->setAttribute(Qt::WA_DeleteOnClose);
802 trackerDialog->setTrackers(commonTrackers);
804 connect(trackerDialog, &QDialog::accepted, this, [torrents, trackerDialog]()
806 for (BitTorrent::Torrent *torrent : torrents)
807 torrent->replaceTrackers(trackerDialog->trackers());
810 trackerDialog->open();
813 void TransferListWidget::exportTorrent()
815 if (getSelectedTorrents().isEmpty())
816 return;
818 auto *fileDialog = new QFileDialog(this, tr("Choose folder to save exported .torrent files"));
819 fileDialog->setAttribute(Qt::WA_DeleteOnClose);
820 fileDialog->setFileMode(QFileDialog::Directory);
821 fileDialog->setOptions(QFileDialog::ShowDirsOnly);
822 connect(fileDialog, &QFileDialog::fileSelected, this, [this](const QString &dir)
824 const QVector<BitTorrent::Torrent *> torrents = getSelectedTorrents();
825 if (torrents.isEmpty())
826 return;
828 const Path savePath {dir};
829 if (!savePath.exists())
830 return;
832 const QString errorMsg = tr("Export .torrent file failed. Torrent: \"%1\". Save path: \"%2\". Reason: \"%3\"");
834 bool hasError = false;
835 for (const BitTorrent::Torrent *torrent : torrents)
837 const QString validName = Utils::Fs::toValidFileName(torrent->name(), u"_"_s);
838 const Path filePath = savePath / Path(validName + u".torrent");
839 if (filePath.exists())
841 LogMsg(errorMsg.arg(torrent->name(), filePath.toString(), tr("A file with the same name already exists")) , Log::WARNING);
842 hasError = true;
843 continue;
846 const nonstd::expected<void, QString> result = torrent->exportToFile(filePath);
847 if (!result)
849 LogMsg(errorMsg.arg(torrent->name(), filePath.toString(), result.error()) , Log::WARNING);
850 hasError = true;
851 continue;
855 if (hasError)
857 QMessageBox::warning(this, tr("Export .torrent file error")
858 , tr("Errors occurred when exporting .torrent files. Check execution log for details."));
862 fileDialog->open();
865 void TransferListWidget::confirmRemoveAllTagsForSelection()
867 QMessageBox::StandardButton response = QMessageBox::question(
868 this, tr("Remove All Tags"), tr("Remove all tags from selected torrents?"),
869 QMessageBox::Yes | QMessageBox::No);
870 if (response == QMessageBox::Yes)
871 clearSelectionTags();
874 QStringList TransferListWidget::askTagsForSelection(const QString &dialogTitle)
876 QStringList tags;
877 bool invalid = true;
878 while (invalid)
880 bool ok = false;
881 invalid = false;
882 const QString tagsInput = AutoExpandableDialog::getText(
883 this, dialogTitle, tr("Comma-separated tags:"), QLineEdit::Normal, {}, &ok).trimmed();
884 if (!ok || tagsInput.isEmpty())
885 return {};
886 tags = tagsInput.split(u',', Qt::SkipEmptyParts);
887 for (QString &tag : tags)
889 tag = tag.trimmed();
890 if (!BitTorrent::Session::isValidTag(tag))
892 QMessageBox::warning(this, tr("Invalid tag")
893 , tr("Tag name: '%1' is invalid").arg(tag));
894 invalid = true;
898 return tags;
901 void TransferListWidget::applyToSelectedTorrents(const std::function<void (BitTorrent::Torrent *const)> &fn)
903 // Changing the data may affect the layout of the sort/filter model, which in turn may invalidate
904 // the indexes previously obtained from selection model before we process them all.
905 // Therefore, we must map all the selected indexes to source before start processing them.
906 const QModelIndexList sourceRows = mapToSource(selectionModel()->selectedRows());
907 for (const QModelIndex &index : sourceRows)
909 BitTorrent::Torrent *const torrent = m_listModel->torrentHandle(index);
910 Q_ASSERT(torrent);
911 fn(torrent);
915 void TransferListWidget::renameSelectedTorrent()
917 const QModelIndexList selectedIndexes = selectionModel()->selectedRows();
918 if ((selectedIndexes.size() != 1) || !selectedIndexes.first().isValid())
919 return;
921 const QModelIndex mi = m_listModel->index(mapToSource(selectedIndexes.first()).row(), TransferListModel::TR_NAME);
922 BitTorrent::Torrent *const torrent = m_listModel->torrentHandle(mi);
923 if (!torrent)
924 return;
926 // Ask for a new Name
927 bool ok = false;
928 QString name = AutoExpandableDialog::getText(this, tr("Rename"), tr("New name:"), QLineEdit::Normal, torrent->name(), &ok);
929 if (ok && !name.isEmpty())
931 name.replace(QRegularExpression(u"\r?\n|\r"_s), u" "_s);
932 // Rename the torrent
933 m_listModel->setData(mi, name, Qt::DisplayRole);
937 void TransferListWidget::setSelectionCategory(const QString &category)
939 applyToSelectedTorrents([&category](BitTorrent::Torrent *torrent) { torrent->setCategory(category); });
942 void TransferListWidget::addSelectionTag(const QString &tag)
944 applyToSelectedTorrents([&tag](BitTorrent::Torrent *const torrent) { torrent->addTag(tag); });
947 void TransferListWidget::removeSelectionTag(const QString &tag)
949 applyToSelectedTorrents([&tag](BitTorrent::Torrent *const torrent) { torrent->removeTag(tag); });
952 void TransferListWidget::clearSelectionTags()
954 applyToSelectedTorrents([](BitTorrent::Torrent *const torrent) { torrent->removeAllTags(); });
957 void TransferListWidget::displayListMenu()
959 const QModelIndexList selectedIndexes = selectionModel()->selectedRows();
960 if (selectedIndexes.isEmpty())
961 return;
963 auto *listMenu = new QMenu(this);
964 listMenu->setAttribute(Qt::WA_DeleteOnClose);
965 listMenu->setToolTipsVisible(true);
967 // Create actions
969 auto *actionStart = new QAction(UIThemeManager::instance()->getIcon(u"torrent-start"_s, u"media-playback-start"_s), tr("&Resume", "Resume/start the torrent"), listMenu);
970 connect(actionStart, &QAction::triggered, this, &TransferListWidget::startSelectedTorrents);
971 auto *actionPause = new QAction(UIThemeManager::instance()->getIcon(u"torrent-stop"_s, u"media-playback-pause"_s), tr("&Pause", "Pause the torrent"), listMenu);
972 connect(actionPause, &QAction::triggered, this, &TransferListWidget::pauseSelectedTorrents);
973 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);
974 connect(actionForceStart, &QAction::triggered, this, &TransferListWidget::forceStartSelectedTorrents);
975 auto *actionDelete = new QAction(UIThemeManager::instance()->getIcon(u"list-remove"_s), tr("&Remove", "Remove the torrent"), listMenu);
976 connect(actionDelete, &QAction::triggered, this, &TransferListWidget::softDeleteSelectedTorrents);
977 auto *actionPreviewFile = new QAction(UIThemeManager::instance()->getIcon(u"view-preview"_s), tr("Pre&view file..."), listMenu);
978 connect(actionPreviewFile, &QAction::triggered, this, &TransferListWidget::previewSelectedTorrents);
979 auto *actionTorrentOptions = new QAction(UIThemeManager::instance()->getIcon(u"configure"_s), tr("Torrent &options..."), listMenu);
980 connect(actionTorrentOptions, &QAction::triggered, this, &TransferListWidget::setTorrentOptions);
981 auto *actionOpenDestinationFolder = new QAction(UIThemeManager::instance()->getIcon(u"directory"_s), tr("Open destination &folder"), listMenu);
982 connect(actionOpenDestinationFolder, &QAction::triggered, this, &TransferListWidget::openSelectedTorrentsFolder);
983 auto *actionIncreaseQueuePos = new QAction(UIThemeManager::instance()->getIcon(u"go-up"_s), tr("Move &up", "i.e. move up in the queue"), listMenu);
984 connect(actionIncreaseQueuePos, &QAction::triggered, this, &TransferListWidget::increaseQueuePosSelectedTorrents);
985 auto *actionDecreaseQueuePos = new QAction(UIThemeManager::instance()->getIcon(u"go-down"_s), tr("Move &down", "i.e. Move down in the queue"), listMenu);
986 connect(actionDecreaseQueuePos, &QAction::triggered, this, &TransferListWidget::decreaseQueuePosSelectedTorrents);
987 auto *actionTopQueuePos = new QAction(UIThemeManager::instance()->getIcon(u"go-top"_s), tr("Move to &top", "i.e. Move to top of the queue"), listMenu);
988 connect(actionTopQueuePos, &QAction::triggered, this, &TransferListWidget::topQueuePosSelectedTorrents);
989 auto *actionBottomQueuePos = new QAction(UIThemeManager::instance()->getIcon(u"go-bottom"_s), tr("Move to &bottom", "i.e. Move to bottom of the queue"), listMenu);
990 connect(actionBottomQueuePos, &QAction::triggered, this, &TransferListWidget::bottomQueuePosSelectedTorrents);
991 auto *actionSetTorrentPath = new QAction(UIThemeManager::instance()->getIcon(u"set-location"_s, u"inode-directory"_s), tr("Set loc&ation..."), listMenu);
992 connect(actionSetTorrentPath, &QAction::triggered, this, &TransferListWidget::setSelectedTorrentsLocation);
993 auto *actionForceRecheck = new QAction(UIThemeManager::instance()->getIcon(u"force-recheck"_s, u"document-edit-verify"_s), tr("Force rec&heck"), listMenu);
994 connect(actionForceRecheck, &QAction::triggered, this, &TransferListWidget::recheckSelectedTorrents);
995 auto *actionForceReannounce = new QAction(UIThemeManager::instance()->getIcon(u"reannounce"_s, u"document-edit-verify"_s), tr("Force r&eannounce"), listMenu);
996 connect(actionForceReannounce, &QAction::triggered, this, &TransferListWidget::reannounceSelectedTorrents);
997 auto *actionCopyMagnetLink = new QAction(UIThemeManager::instance()->getIcon(u"torrent-magnet"_s, u"kt-magnet"_s), tr("&Magnet link"), listMenu);
998 connect(actionCopyMagnetLink, &QAction::triggered, this, &TransferListWidget::copySelectedMagnetURIs);
999 auto *actionCopyID = new QAction(UIThemeManager::instance()->getIcon(u"help-about"_s, u"edit-copy"_s), tr("Torrent &ID"), listMenu);
1000 connect(actionCopyID, &QAction::triggered, this, &TransferListWidget::copySelectedIDs);
1001 auto *actionCopyComment = new QAction(UIThemeManager::instance()->getIcon(u"edit-copy"_s), tr("&Comment"), listMenu);
1002 connect(actionCopyComment, &QAction::triggered, this, &TransferListWidget::copySelectedComments);
1003 auto *actionCopyName = new QAction(UIThemeManager::instance()->getIcon(u"name"_s, u"edit-copy"_s), tr("&Name"), listMenu);
1004 connect(actionCopyName, &QAction::triggered, this, &TransferListWidget::copySelectedNames);
1005 auto *actionCopyHash1 = new QAction(UIThemeManager::instance()->getIcon(u"hash"_s, u"edit-copy"_s), tr("Info &hash v1"), listMenu);
1006 connect(actionCopyHash1, &QAction::triggered, this, [this]() { copySelectedInfohashes(CopyInfohashPolicy::Version1); });
1007 auto *actionCopyHash2 = new QAction(UIThemeManager::instance()->getIcon(u"hash"_s, u"edit-copy"_s), tr("Info h&ash v2"), listMenu);
1008 connect(actionCopyHash2, &QAction::triggered, this, [this]() { copySelectedInfohashes(CopyInfohashPolicy::Version2); });
1009 auto *actionSuperSeedingMode = new TriStateAction(tr("Super seeding mode"), listMenu);
1010 connect(actionSuperSeedingMode, &QAction::triggered, this, &TransferListWidget::setSelectedTorrentsSuperSeeding);
1011 auto *actionRename = new QAction(UIThemeManager::instance()->getIcon(u"edit-rename"_s), tr("Re&name..."), listMenu);
1012 connect(actionRename, &QAction::triggered, this, &TransferListWidget::renameSelectedTorrent);
1013 auto *actionSequentialDownload = new TriStateAction(tr("Download in sequential order"), listMenu);
1014 connect(actionSequentialDownload, &QAction::triggered, this, &TransferListWidget::setSelectedTorrentsSequentialDownload);
1015 auto *actionFirstLastPiecePrio = new TriStateAction(tr("Download first and last pieces first"), listMenu);
1016 connect(actionFirstLastPiecePrio, &QAction::triggered, this, &TransferListWidget::setSelectedFirstLastPiecePrio);
1017 auto *actionAutoTMM = new TriStateAction(tr("Automatic Torrent Management"), listMenu);
1018 actionAutoTMM->setToolTip(tr("Automatic mode means that various torrent properties (e.g. save path) will be decided by the associated category"));
1019 connect(actionAutoTMM, &QAction::triggered, this, &TransferListWidget::setSelectedAutoTMMEnabled);
1020 auto *actionEditTracker = new QAction(UIThemeManager::instance()->getIcon(u"edit-rename"_s), tr("Edit trac&kers..."), listMenu);
1021 connect(actionEditTracker, &QAction::triggered, this, &TransferListWidget::editTorrentTrackers);
1022 auto *actionExportTorrent = new QAction(UIThemeManager::instance()->getIcon(u"edit-copy"_s), tr("E&xport .torrent..."), listMenu);
1023 connect(actionExportTorrent, &QAction::triggered, this, &TransferListWidget::exportTorrent);
1024 // End of actions
1026 // Enable/disable pause/start action given the DL state
1027 bool needsPause = false, needsStart = false, needsForce = false, needsPreview = false;
1028 bool allSameSuperSeeding = true;
1029 bool superSeedingMode = false;
1030 bool allSameSequentialDownloadMode = true, allSamePrioFirstlast = true;
1031 bool sequentialDownloadMode = false, prioritizeFirstLast = false;
1032 bool oneHasMetadata = false, oneNotFinished = false;
1033 bool allSameCategory = true;
1034 bool allSameAutoTMM = true;
1035 bool firstAutoTMM = false;
1036 QString firstCategory;
1037 bool first = true;
1038 TagSet tagsInAny;
1039 TagSet tagsInAll;
1040 bool hasInfohashV1 = false, hasInfohashV2 = false;
1041 bool oneCanForceReannounce = false;
1043 for (const QModelIndex &index : selectedIndexes)
1045 // Get the file name
1046 // Get handle and pause the torrent
1047 const BitTorrent::Torrent *torrent = m_listModel->torrentHandle(mapToSource(index));
1048 if (!torrent) continue;
1050 if (firstCategory.isEmpty() && first)
1051 firstCategory = torrent->category();
1052 if (firstCategory != torrent->category())
1053 allSameCategory = false;
1055 const TagSet torrentTags = torrent->tags();
1056 tagsInAny.unite(torrentTags);
1058 if (first)
1060 firstAutoTMM = torrent->isAutoTMMEnabled();
1061 tagsInAll = torrentTags;
1063 else
1065 tagsInAll.intersect(torrentTags);
1068 if (firstAutoTMM != torrent->isAutoTMMEnabled())
1069 allSameAutoTMM = false;
1071 if (torrent->hasMetadata())
1072 oneHasMetadata = true;
1073 if (!torrent->isFinished())
1075 oneNotFinished = true;
1076 if (first)
1078 sequentialDownloadMode = torrent->isSequentialDownload();
1079 prioritizeFirstLast = torrent->hasFirstLastPiecePriority();
1081 else
1083 if (sequentialDownloadMode != torrent->isSequentialDownload())
1084 allSameSequentialDownloadMode = false;
1085 if (prioritizeFirstLast != torrent->hasFirstLastPiecePriority())
1086 allSamePrioFirstlast = false;
1089 else
1091 if (!oneNotFinished && allSameSuperSeeding && torrent->hasMetadata())
1093 if (first)
1094 superSeedingMode = torrent->superSeeding();
1095 else if (superSeedingMode != torrent->superSeeding())
1096 allSameSuperSeeding = false;
1100 if (!torrent->isForced())
1101 needsForce = true;
1102 else
1103 needsStart = true;
1105 const bool isPaused = torrent->isPaused();
1106 if (isPaused)
1107 needsStart = true;
1108 else
1109 needsPause = true;
1111 if (torrent->isErrored() || torrent->hasMissingFiles())
1113 // If torrent is in "errored" or "missing files" state
1114 // it cannot keep further processing until you restart it.
1115 needsStart = true;
1116 needsForce = true;
1119 if (torrent->hasMetadata())
1120 needsPreview = true;
1122 if (!hasInfohashV1 && torrent->infoHash().v1().isValid())
1123 hasInfohashV1 = true;
1124 if (!hasInfohashV2 && torrent->infoHash().v2().isValid())
1125 hasInfohashV2 = true;
1127 first = false;
1129 const bool rechecking = torrent->isChecking();
1130 if (rechecking)
1132 needsStart = true;
1133 needsPause = true;
1136 const bool queued = (BitTorrent::Session::instance()->isQueueingSystemEnabled() && torrent->isQueued());
1138 if (!isPaused && !rechecking && !queued)
1139 oneCanForceReannounce = true;
1141 if (oneHasMetadata && oneNotFinished && !allSameSequentialDownloadMode
1142 && !allSamePrioFirstlast && !allSameSuperSeeding && !allSameCategory
1143 && needsStart && needsForce && needsPause && needsPreview && !allSameAutoTMM
1144 && hasInfohashV1 && hasInfohashV2 && oneCanForceReannounce)
1146 break;
1150 if (needsStart)
1151 listMenu->addAction(actionStart);
1152 if (needsPause)
1153 listMenu->addAction(actionPause);
1154 if (needsForce)
1155 listMenu->addAction(actionForceStart);
1156 listMenu->addSeparator();
1157 listMenu->addAction(actionDelete);
1158 listMenu->addSeparator();
1159 listMenu->addAction(actionSetTorrentPath);
1160 if (selectedIndexes.size() == 1)
1161 listMenu->addAction(actionRename);
1162 listMenu->addAction(actionEditTracker);
1164 // Category Menu
1165 QStringList categories = BitTorrent::Session::instance()->categories();
1166 std::sort(categories.begin(), categories.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>());
1168 QMenu *categoryMenu = listMenu->addMenu(UIThemeManager::instance()->getIcon(u"view-categories"_s), tr("Categor&y"));
1170 categoryMenu->addAction(UIThemeManager::instance()->getIcon(u"list-add"_s), tr("&New...", "New category...")
1171 , this, &TransferListWidget::askNewCategoryForSelection);
1172 categoryMenu->addAction(UIThemeManager::instance()->getIcon(u"edit-clear"_s), tr("&Reset", "Reset category")
1173 , this, [this]() { setSelectionCategory(u""_s); });
1174 categoryMenu->addSeparator();
1176 for (const QString &category : asConst(categories))
1178 const QString escapedCategory = QString(category).replace(u'&', u"&&"_s); // avoid '&' becomes accelerator key
1179 QAction *categoryAction = categoryMenu->addAction(UIThemeManager::instance()->getIcon(u"view-categories"_s), escapedCategory
1180 , this, [this, category]() { setSelectionCategory(category); });
1182 if (allSameCategory && (category == firstCategory))
1184 categoryAction->setCheckable(true);
1185 categoryAction->setChecked(true);
1189 // Tag Menu
1190 QStringList tags(BitTorrent::Session::instance()->tags().values());
1191 std::sort(tags.begin(), tags.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>());
1193 QMenu *tagsMenu = listMenu->addMenu(UIThemeManager::instance()->getIcon(u"tags"_s, u"view-categories"_s), tr("Ta&gs"));
1195 tagsMenu->addAction(UIThemeManager::instance()->getIcon(u"list-add"_s), tr("&Add...", "Add / assign multiple tags...")
1196 , this, &TransferListWidget::askAddTagsForSelection);
1197 tagsMenu->addAction(UIThemeManager::instance()->getIcon(u"edit-clear"_s), tr("&Remove All", "Remove all tags")
1198 , this, [this]()
1200 if (Preferences::instance()->confirmRemoveAllTags())
1201 confirmRemoveAllTagsForSelection();
1202 else
1203 clearSelectionTags();
1205 tagsMenu->addSeparator();
1207 for (const QString &tag : asConst(tags))
1209 auto *action = new TriStateAction(tag, tagsMenu);
1210 action->setCloseOnInteraction(false);
1212 const Qt::CheckState initialState = tagsInAll.contains(tag) ? Qt::Checked
1213 : tagsInAny.contains(tag) ? Qt::PartiallyChecked : Qt::Unchecked;
1214 action->setCheckState(initialState);
1216 connect(action, &QAction::toggled, this, [this, tag](const bool checked)
1218 if (checked)
1219 addSelectionTag(tag);
1220 else
1221 removeSelectionTag(tag);
1224 tagsMenu->addAction(action);
1227 actionAutoTMM->setCheckState(allSameAutoTMM
1228 ? (firstAutoTMM ? Qt::Checked : Qt::Unchecked)
1229 : Qt::PartiallyChecked);
1230 listMenu->addAction(actionAutoTMM);
1232 listMenu->addSeparator();
1233 listMenu->addAction(actionTorrentOptions);
1234 if (!oneNotFinished && oneHasMetadata)
1236 actionSuperSeedingMode->setCheckState(allSameSuperSeeding
1237 ? (superSeedingMode ? Qt::Checked : Qt::Unchecked)
1238 : Qt::PartiallyChecked);
1239 listMenu->addAction(actionSuperSeedingMode);
1241 listMenu->addSeparator();
1242 bool addedPreviewAction = false;
1243 if (needsPreview)
1245 listMenu->addAction(actionPreviewFile);
1246 addedPreviewAction = true;
1248 if (oneNotFinished)
1250 actionSequentialDownload->setCheckState(allSameSequentialDownloadMode
1251 ? (sequentialDownloadMode ? Qt::Checked : Qt::Unchecked)
1252 : Qt::PartiallyChecked);
1253 listMenu->addAction(actionSequentialDownload);
1255 actionFirstLastPiecePrio->setCheckState(allSamePrioFirstlast
1256 ? (prioritizeFirstLast ? Qt::Checked : Qt::Unchecked)
1257 : Qt::PartiallyChecked);
1258 listMenu->addAction(actionFirstLastPiecePrio);
1260 addedPreviewAction = true;
1263 if (addedPreviewAction)
1264 listMenu->addSeparator();
1265 if (oneHasMetadata)
1266 listMenu->addAction(actionForceRecheck);
1267 // We can not force reannounce torrents that are paused/errored/checking/missing files/queued.
1268 // We may already have the tracker list from magnet url. So we can force reannounce torrents without metadata anyway.
1269 listMenu->addAction(actionForceReannounce);
1270 actionForceReannounce->setEnabled(oneCanForceReannounce);
1271 if (!oneCanForceReannounce)
1272 actionForceReannounce->setToolTip(tr("Can not force reannounce if torrent is Paused/Queued/Errored/Checking"));
1273 listMenu->addSeparator();
1274 listMenu->addAction(actionOpenDestinationFolder);
1275 if (BitTorrent::Session::instance()->isQueueingSystemEnabled() && oneNotFinished)
1277 listMenu->addSeparator();
1278 QMenu *queueMenu = listMenu->addMenu(
1279 UIThemeManager::instance()->getIcon(u"queued"_s), tr("&Queue"));
1280 queueMenu->addAction(actionTopQueuePos);
1281 queueMenu->addAction(actionIncreaseQueuePos);
1282 queueMenu->addAction(actionDecreaseQueuePos);
1283 queueMenu->addAction(actionBottomQueuePos);
1286 QMenu *copySubMenu = listMenu->addMenu(UIThemeManager::instance()->getIcon(u"edit-copy"_s), tr("&Copy"));
1287 copySubMenu->addAction(actionCopyName);
1288 copySubMenu->addAction(actionCopyHash1);
1289 actionCopyHash1->setEnabled(hasInfohashV1);
1290 copySubMenu->addAction(actionCopyHash2);
1291 actionCopyHash2->setEnabled(hasInfohashV2);
1292 copySubMenu->addAction(actionCopyMagnetLink);
1293 copySubMenu->addAction(actionCopyID);
1294 copySubMenu->addAction(actionCopyComment);
1296 actionExportTorrent->setToolTip(tr("Exported torrent is not necessarily the same as the imported"));
1297 listMenu->addAction(actionExportTorrent);
1299 listMenu->popup(QCursor::pos());
1302 void TransferListWidget::currentChanged(const QModelIndex &current, const QModelIndex&)
1304 qDebug("CURRENT CHANGED");
1305 BitTorrent::Torrent *torrent = nullptr;
1306 if (current.isValid())
1308 torrent = m_listModel->torrentHandle(mapToSource(current));
1309 // Fix scrolling to the lowermost visible torrent
1310 QMetaObject::invokeMethod(this, [this, current] { scrollTo(current); }, Qt::QueuedConnection);
1312 emit currentTorrentChanged(torrent);
1315 void TransferListWidget::applyCategoryFilter(const QString &category)
1317 if (category.isNull())
1318 m_sortFilterModel->disableCategoryFilter();
1319 else
1320 m_sortFilterModel->setCategoryFilter(category);
1323 void TransferListWidget::applyTagFilter(const QString &tag)
1325 if (tag.isNull())
1326 m_sortFilterModel->disableTagFilter();
1327 else
1328 m_sortFilterModel->setTagFilter(tag);
1331 void TransferListWidget::applyTrackerFilterAll()
1333 m_sortFilterModel->disableTrackerFilter();
1336 void TransferListWidget::applyTrackerFilter(const QSet<BitTorrent::TorrentID> &torrentIDs)
1338 m_sortFilterModel->setTrackerFilter(torrentIDs);
1341 void TransferListWidget::applyFilter(const QString &name, const TransferListModel::Column &type)
1343 m_sortFilterModel->setFilterKeyColumn(type);
1344 const QString pattern = (Preferences::instance()->getRegexAsFilteringPatternForTransferList()
1345 ? name : Utils::String::wildcardToRegexPattern(name));
1346 m_sortFilterModel->setFilterRegularExpression(QRegularExpression(pattern, QRegularExpression::CaseInsensitiveOption));
1349 void TransferListWidget::applyStatusFilter(int f)
1351 m_sortFilterModel->setStatusFilter(static_cast<TorrentFilter::Type>(f));
1352 // Select first item if nothing is selected
1353 if (selectionModel()->selectedRows(0).empty() && (m_sortFilterModel->rowCount() > 0))
1355 qDebug("Nothing is selected, selecting first row: %s", qUtf8Printable(m_sortFilterModel->index(0, TransferListModel::TR_NAME).data().toString()));
1356 selectionModel()->setCurrentIndex(m_sortFilterModel->index(0, TransferListModel::TR_NAME), QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
1360 void TransferListWidget::saveSettings()
1362 Preferences::instance()->setTransHeaderState(header()->saveState());
1365 bool TransferListWidget::loadSettings()
1367 return header()->restoreState(Preferences::instance()->getTransHeaderState());
1370 void TransferListWidget::wheelEvent(QWheelEvent *event)
1372 if (event->modifiers() & Qt::ShiftModifier)
1374 // Shift + scroll = horizontal scroll
1375 event->accept();
1376 QWheelEvent scrollHEvent {event->position(), event->globalPosition()
1377 , event->pixelDelta(), event->angleDelta().transposed(), event->buttons()
1378 , event->modifiers(), event->phase(), event->inverted(), event->source()};
1379 QTreeView::wheelEvent(&scrollHEvent);
1380 return;
1383 QTreeView::wheelEvent(event); // event delegated to base class