1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-23 21:14:33 +00:00
qBittorrent/src/gui/properties/trackerlistwidget.cpp

693 lines
23 KiB
C++
Raw Normal View History

/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "trackerlistwidget.h"
#include <QAction>
#include <QApplication>
2019-06-02 12:13:34 +03:00
#include <QClipboard>
#include <QColor>
2013-02-09 21:18:52 +04:00
#include <QDebug>
#include <QHeaderView>
#include <QMenu>
2015-06-02 12:09:15 +03:00
#include <QMessageBox>
2019-06-02 12:13:34 +03:00
#include <QShortcut>
#include <QStringList>
#include <QTableView>
#include <QTreeWidgetItem>
#include <QUrl>
#include <QVector>
2015-06-02 12:09:15 +03:00
#include "base/bittorrent/peerinfo.h"
2015-09-25 11:10:05 +03:00
#include "base/bittorrent/session.h"
#include "base/bittorrent/torrent.h"
2015-09-25 11:10:05 +03:00
#include "base/bittorrent/trackerentry.h"
#include "base/global.h"
2015-09-25 11:10:05 +03:00
#include "base/preferences.h"
2020-04-30 10:53:43 +03:00
#include "gui/autoexpandabledialog.h"
#include "gui/uithememanager.h"
2015-06-02 12:09:15 +03:00
#include "propertieswidget.h"
#include "trackersadditiondialog.h"
#define NB_STICKY_ITEM 3
TrackerListWidget::TrackerListWidget(PropertiesWidget *properties)
: QTreeWidget()
, m_properties(properties)
{
// Set header
// Must be set before calling loadSettings() otherwise the header is reset on restart
setHeaderLabels(headerLabels());
// Load settings
loadSettings();
// Graphical settings
setRootIsDecorated(false);
setAllColumnsShowFocus(true);
setItemsExpandable(false);
setSelectionMode(QAbstractItemView::ExtendedSelection);
header()->setStretchLastSection(false); // Must be set after loadSettings() in order to work
header()->setTextElideMode(Qt::ElideRight);
// Ensure that at least one column is visible at all times
if (visibleColumnsCount() == 0)
setColumnHidden(COL_URL, false);
// To also mitigate the above issue, we have to resize each column when
// its size is 0, because explicitly 'showing' the column isn't enough
// in the above scenario.
2018-09-07 14:12:38 +03:00
for (int i = 0; i < COL_COUNT; ++i)
if ((columnWidth(i) <= 0) && !isColumnHidden(i))
resizeColumnToContents(i);
// Context menu
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, &QWidget::customContextMenuRequested, this, &TrackerListWidget::showTrackerListMenu);
// Header
header()->setContextMenuPolicy(Qt::CustomContextMenu);
connect(header(), &QWidget::customContextMenuRequested, this, &TrackerListWidget::displayColumnHeaderMenu);
connect(header(), &QHeaderView::sectionMoved, this, &TrackerListWidget::saveSettings);
connect(header(), &QHeaderView::sectionResized, this, &TrackerListWidget::saveSettings);
connect(header(), &QHeaderView::sortIndicatorChanged, this, &TrackerListWidget::saveSettings);
// Set DHT, PeX, LSD items
m_DHTItem = new QTreeWidgetItem({ "", "** [DHT] **", "", "0", "", "", "0" });
insertTopLevelItem(0, m_DHTItem);
setRowColor(0, QColor("grey"));
m_PEXItem = new QTreeWidgetItem({ "", "** [PeX] **", "", "0", "", "", "0" });
insertTopLevelItem(1, m_PEXItem);
setRowColor(1, QColor("grey"));
m_LSDItem = new QTreeWidgetItem({ "", "** [LSD] **", "", "0", "", "", "0" });
insertTopLevelItem(2, m_LSDItem);
setRowColor(2, QColor("grey"));
// Set static items alignment
const Qt::Alignment alignment = (Qt::AlignRight | Qt::AlignVCenter);
m_DHTItem->setTextAlignment(COL_PEERS, alignment);
m_PEXItem->setTextAlignment(COL_PEERS, alignment);
m_LSDItem->setTextAlignment(COL_PEERS, alignment);
m_DHTItem->setTextAlignment(COL_SEEDS, alignment);
m_PEXItem->setTextAlignment(COL_SEEDS, alignment);
m_LSDItem->setTextAlignment(COL_SEEDS, alignment);
m_DHTItem->setTextAlignment(COL_LEECHES, alignment);
m_PEXItem->setTextAlignment(COL_LEECHES, alignment);
m_LSDItem->setTextAlignment(COL_LEECHES, alignment);
m_DHTItem->setTextAlignment(COL_DOWNLOADED, alignment);
m_PEXItem->setTextAlignment(COL_DOWNLOADED, alignment);
m_LSDItem->setTextAlignment(COL_DOWNLOADED, alignment);
// Set header alignment
headerItem()->setTextAlignment(COL_TIER, alignment);
headerItem()->setTextAlignment(COL_PEERS, alignment);
headerItem()->setTextAlignment(COL_SEEDS, alignment);
headerItem()->setTextAlignment(COL_LEECHES, alignment);
headerItem()->setTextAlignment(COL_DOWNLOADED, alignment);
// Set hotkeys
const auto *editHotkey = new QShortcut(Qt::Key_F2, this, nullptr, nullptr, Qt::WidgetShortcut);
connect(editHotkey, &QShortcut::activated, this, &TrackerListWidget::editSelectedTracker);
const auto *deleteHotkey = new QShortcut(QKeySequence::Delete, this, nullptr, nullptr, Qt::WidgetShortcut);
connect(deleteHotkey, &QShortcut::activated, this, &TrackerListWidget::deleteSelectedTrackers);
const auto *copyHotkey = new QShortcut(QKeySequence::Copy, this, nullptr, nullptr, Qt::WidgetShortcut);
connect(copyHotkey, &QShortcut::activated, this, &TrackerListWidget::copyTrackerUrl);
connect(this, &QAbstractItemView::doubleClicked, this, &TrackerListWidget::editSelectedTracker);
2011-06-12 13:55:19 +00:00
// This hack fixes reordering of first column with Qt5.
// https://github.com/qtproject/qtbase/commit/e0fc088c0c8bc61dbcaf5928b24986cd61a22777
QTableView unused;
2017-03-10 11:43:59 +02:00
unused.setVerticalHeader(header());
header()->setParent(this);
unused.setVerticalHeader(new QHeaderView(Qt::Horizontal));
}
TrackerListWidget::~TrackerListWidget()
{
saveSettings();
}
QVector<QTreeWidgetItem *> TrackerListWidget::getSelectedTrackerItems() const
{
const QList<QTreeWidgetItem *> selectedTrackerItems = selectedItems();
QVector<QTreeWidgetItem *> selectedTrackers;
selectedTrackers.reserve(selectedTrackerItems.size());
2020-11-16 10:02:11 +03:00
for (QTreeWidgetItem *item : selectedTrackerItems)
{
if (indexOfTopLevelItem(item) >= NB_STICKY_ITEM) // Ignore STICKY ITEMS
selectedTrackers << item;
}
return selectedTrackers;
}
void TrackerListWidget::setRowColor(const int row, const QColor &color)
{
2018-09-07 14:12:38 +03:00
const int nbColumns = columnCount();
QTreeWidgetItem *item = topLevelItem(row);
2018-09-07 14:12:38 +03:00
for (int i = 0; i < nbColumns; ++i)
item->setData(i, Qt::ForegroundRole, color);
}
void TrackerListWidget::moveSelectionUp()
{
BitTorrent::Torrent *const torrent = m_properties->getCurrentTorrent();
2020-11-16 10:02:11 +03:00
if (!torrent)
{
clear();
return;
}
const QVector<QTreeWidgetItem *> selectedTrackerItems = getSelectedTrackerItems();
if (selectedTrackerItems.isEmpty()) return;
bool change = false;
2020-11-16 10:02:11 +03:00
for (QTreeWidgetItem *item : selectedTrackerItems)
{
int index = indexOfTopLevelItem(item);
2020-11-16 10:02:11 +03:00
if (index > NB_STICKY_ITEM)
{
insertTopLevelItem(index - 1, takeTopLevelItem(index));
change = true;
}
}
if (!change) return;
// Restore selection
QItemSelectionModel *selection = selectionModel();
for (QTreeWidgetItem *item : selectedTrackerItems)
selection->select(indexFromItem(item), (QItemSelectionModel::Rows | QItemSelectionModel::Select));
setSelectionModel(selection);
// Update torrent trackers
QVector<BitTorrent::TrackerEntry> trackers;
trackers.reserve(topLevelItemCount());
2020-11-16 10:02:11 +03:00
for (int i = NB_STICKY_ITEM; i < topLevelItemCount(); ++i)
{
const QString trackerURL = topLevelItem(i)->data(COL_URL, Qt::DisplayRole).toString();
2021-03-01 16:06:08 +03:00
trackers.append({trackerURL, (i - NB_STICKY_ITEM)});
}
torrent->replaceTrackers(trackers);
// Reannounce
if (!torrent->isPaused())
torrent->forceReannounce();
}
void TrackerListWidget::moveSelectionDown()
{
BitTorrent::Torrent *const torrent = m_properties->getCurrentTorrent();
2020-11-16 10:02:11 +03:00
if (!torrent)
{
clear();
return;
}
const QVector<QTreeWidgetItem *> selectedTrackerItems = getSelectedTrackerItems();
if (selectedTrackerItems.isEmpty()) return;
bool change = false;
2020-11-16 10:02:11 +03:00
for (int i = selectedItems().size() - 1; i >= 0; --i)
{
int index = indexOfTopLevelItem(selectedTrackerItems.at(i));
2020-11-16 10:02:11 +03:00
if (index < (topLevelItemCount() - 1))
{
insertTopLevelItem(index + 1, takeTopLevelItem(index));
change = true;
}
}
if (!change) return;
// Restore selection
QItemSelectionModel *selection = selectionModel();
for (QTreeWidgetItem *item : selectedTrackerItems)
selection->select(indexFromItem(item), (QItemSelectionModel::Rows | QItemSelectionModel::Select));
setSelectionModel(selection);
// Update torrent trackers
QVector<BitTorrent::TrackerEntry> trackers;
trackers.reserve(topLevelItemCount());
2020-11-16 10:02:11 +03:00
for (int i = NB_STICKY_ITEM; i < topLevelItemCount(); ++i)
{
const QString trackerURL = topLevelItem(i)->data(COL_URL, Qt::DisplayRole).toString();
2021-03-01 16:06:08 +03:00
trackers.append({trackerURL, (i - NB_STICKY_ITEM)});
}
torrent->replaceTrackers(trackers);
// Reannounce
if (!torrent->isPaused())
torrent->forceReannounce();
}
void TrackerListWidget::clear()
{
qDeleteAll(m_trackerItems);
m_trackerItems.clear();
m_DHTItem->setText(COL_STATUS, "");
m_DHTItem->setText(COL_SEEDS, "");
m_DHTItem->setText(COL_LEECHES, "");
m_DHTItem->setText(COL_MSG, "");
m_PEXItem->setText(COL_STATUS, "");
m_PEXItem->setText(COL_SEEDS, "");
m_PEXItem->setText(COL_LEECHES, "");
m_PEXItem->setText(COL_MSG, "");
m_LSDItem->setText(COL_STATUS, "");
m_LSDItem->setText(COL_SEEDS, "");
m_LSDItem->setText(COL_LEECHES, "");
m_LSDItem->setText(COL_MSG, "");
}
void TrackerListWidget::loadStickyItems(const BitTorrent::Torrent *torrent)
{
const QString working {tr("Working")};
const QString disabled {tr("Disabled")};
const QString torrentDisabled {tr("Disabled for this torrent")};
const auto *session = BitTorrent::Session::instance();
// load DHT information
if (torrent->isPrivate() || torrent->isDHTDisabled())
m_DHTItem->setText(COL_STATUS, torrentDisabled);
else if (!session->isDHTEnabled())
m_DHTItem->setText(COL_STATUS, disabled);
else
m_DHTItem->setText(COL_STATUS, working);
// Load PeX Information
if (torrent->isPrivate() || torrent->isPEXDisabled())
m_PEXItem->setText(COL_STATUS, torrentDisabled);
else if (!session->isPeXEnabled())
m_PEXItem->setText(COL_STATUS, disabled);
else
m_PEXItem->setText(COL_STATUS, working);
// Load LSD Information
if (torrent->isPrivate() || torrent->isLSDDisabled())
m_LSDItem->setText(COL_STATUS, torrentDisabled);
else if (!session->isLSDEnabled())
m_LSDItem->setText(COL_STATUS, disabled);
else
m_LSDItem->setText(COL_STATUS, working);
2020-11-16 10:02:11 +03:00
if (torrent->isPrivate())
{
QString privateMsg = tr("This torrent is private");
m_DHTItem->setText(COL_MSG, privateMsg);
m_PEXItem->setText(COL_MSG, privateMsg);
m_LSDItem->setText(COL_MSG, privateMsg);
}
// XXX: libtorrent should provide this info...
// Count peers from DHT, PeX, LSD
uint seedsDHT = 0, seedsPeX = 0, seedsLSD = 0, peersDHT = 0, peersPeX = 0, peersLSD = 0;
2020-11-16 10:02:11 +03:00
for (const BitTorrent::PeerInfo &peer : asConst(torrent->peers()))
{
if (peer.isConnecting()) continue;
2020-11-16 10:02:11 +03:00
if (peer.fromDHT())
{
if (peer.isSeed())
++seedsDHT;
else
++peersDHT;
}
2020-11-16 10:02:11 +03:00
if (peer.fromPeX())
{
if (peer.isSeed())
++seedsPeX;
else
++peersPeX;
}
2020-11-16 10:02:11 +03:00
if (peer.fromLSD())
{
if (peer.isSeed())
++seedsLSD;
else
++peersLSD;
}
}
m_DHTItem->setText(COL_SEEDS, QString::number(seedsDHT));
m_DHTItem->setText(COL_LEECHES, QString::number(peersDHT));
m_PEXItem->setText(COL_SEEDS, QString::number(seedsPeX));
m_PEXItem->setText(COL_LEECHES, QString::number(peersPeX));
m_LSDItem->setText(COL_SEEDS, QString::number(seedsLSD));
m_LSDItem->setText(COL_LEECHES, QString::number(peersLSD));
}
void TrackerListWidget::loadTrackers()
{
// Load trackers from torrent handle
const BitTorrent::Torrent *torrent = m_properties->getCurrentTorrent();
if (!torrent) return;
loadStickyItems(torrent);
// Load actual trackers information
QStringList oldTrackerURLs = m_trackerItems.keys();
2020-11-16 10:02:11 +03:00
for (const BitTorrent::TrackerEntry &entry : asConst(torrent->trackers()))
{
2021-03-01 16:06:08 +03:00
const QString trackerURL = entry.url;
2018-09-07 14:12:38 +03:00
QTreeWidgetItem *item = m_trackerItems.value(trackerURL, nullptr);
2020-11-16 10:02:11 +03:00
if (!item)
{
item = new QTreeWidgetItem();
item->setText(COL_URL, trackerURL);
item->setToolTip(COL_URL, trackerURL);
addTopLevelItem(item);
m_trackerItems[trackerURL] = item;
}
2020-11-16 10:02:11 +03:00
else
{
oldTrackerURLs.removeOne(trackerURL);
}
2021-03-01 16:06:08 +03:00
item->setText(COL_TIER, QString::number(entry.tier));
2021-03-01 16:06:08 +03:00
switch (entry.status)
2020-11-16 10:02:11 +03:00
{
case BitTorrent::TrackerEntry::Working:
item->setText(COL_STATUS, tr("Working"));
break;
case BitTorrent::TrackerEntry::Updating:
item->setText(COL_STATUS, tr("Updating..."));
break;
case BitTorrent::TrackerEntry::NotWorking:
item->setText(COL_STATUS, tr("Not working"));
break;
case BitTorrent::TrackerEntry::NotContacted:
item->setText(COL_STATUS, tr("Not contacted yet"));
break;
}
item->setText(COL_MSG, entry.message);
item->setToolTip(COL_MSG, entry.message);
item->setText(COL_PEERS, ((entry.numPeers > -1)
? QString::number(entry.numPeers)
: tr("N/A")));
2021-03-01 16:06:08 +03:00
item->setText(COL_SEEDS, ((entry.numSeeds > -1)
? QString::number(entry.numSeeds)
: tr("N/A")));
2021-03-01 16:06:08 +03:00
item->setText(COL_LEECHES, ((entry.numLeeches > -1)
? QString::number(entry.numLeeches)
: tr("N/A")));
2021-03-01 16:06:08 +03:00
item->setText(COL_DOWNLOADED, ((entry.numDownloaded > -1)
? QString::number(entry.numDownloaded)
: tr("N/A")));
const Qt::Alignment alignment = (Qt::AlignRight | Qt::AlignVCenter);
item->setTextAlignment(COL_TIER, alignment);
item->setTextAlignment(COL_PEERS, alignment);
item->setTextAlignment(COL_SEEDS, alignment);
item->setTextAlignment(COL_LEECHES, alignment);
item->setTextAlignment(COL_DOWNLOADED, alignment);
}
// Remove old trackers
for (const QString &tracker : asConst(oldTrackerURLs))
delete m_trackerItems.take(tracker);
}
// Ask the user for new trackers and add them to the torrent
void TrackerListWidget::askForTrackers()
{
BitTorrent::Torrent *const torrent = m_properties->getCurrentTorrent();
if (!torrent) return;
QVector<BitTorrent::TrackerEntry> trackers;
for (const QString &tracker : asConst(TrackersAdditionDialog::askForTrackers(this, torrent)))
2021-03-01 16:06:08 +03:00
trackers.append({tracker});
torrent->addTrackers(trackers);
}
void TrackerListWidget::copyTrackerUrl()
{
const QVector<QTreeWidgetItem *> selectedTrackerItems = getSelectedTrackerItems();
if (selectedTrackerItems.isEmpty()) return;
2017-10-26 10:10:30 +03:00
QStringList urlsToCopy;
2020-11-16 10:02:11 +03:00
for (const QTreeWidgetItem *item : selectedTrackerItems)
{
QString trackerURL = item->data(COL_URL, Qt::DisplayRole).toString();
qDebug() << QString("Copy: ") + trackerURL;
2017-10-26 10:10:30 +03:00
urlsToCopy << trackerURL;
2012-11-06 23:03:19 +04:00
}
2018-06-06 16:48:17 +03:00
QApplication::clipboard()->setText(urlsToCopy.join('\n'));
2012-11-06 23:03:19 +04:00
}
void TrackerListWidget::deleteSelectedTrackers()
{
BitTorrent::Torrent *const torrent = m_properties->getCurrentTorrent();
2020-11-16 10:02:11 +03:00
if (!torrent)
{
clear();
return;
}
2015-04-19 18:17:47 +03:00
const QVector<QTreeWidgetItem *> selectedTrackerItems = getSelectedTrackerItems();
if (selectedTrackerItems.isEmpty()) return;
2017-10-26 10:10:30 +03:00
QStringList urlsToRemove;
2020-11-16 10:02:11 +03:00
for (const QTreeWidgetItem *item : selectedTrackerItems)
{
QString trackerURL = item->data(COL_URL, Qt::DisplayRole).toString();
2017-10-26 10:10:30 +03:00
urlsToRemove << trackerURL;
m_trackerItems.remove(trackerURL);
delete item;
}
// Iterate over the trackers and remove the selected ones
const QVector<BitTorrent::TrackerEntry> trackers = torrent->trackers();
QVector<BitTorrent::TrackerEntry> remainingTrackers;
remainingTrackers.reserve(trackers.size());
2020-11-16 10:02:11 +03:00
for (const BitTorrent::TrackerEntry &entry : trackers)
{
2021-03-01 16:06:08 +03:00
if (!urlsToRemove.contains(entry.url))
remainingTrackers.push_back(entry);
}
torrent->replaceTrackers(remainingTrackers);
if (!torrent->isPaused())
torrent->forceReannounce();
}
void TrackerListWidget::editSelectedTracker()
{
BitTorrent::Torrent *const torrent = m_properties->getCurrentTorrent();
2015-04-19 18:17:47 +03:00
if (!torrent) return;
const QVector<QTreeWidgetItem *> selectedTrackerItems = getSelectedTrackerItems();
if (selectedTrackerItems.isEmpty()) return;
// During multi-select only process item selected last
const QUrl trackerURL = selectedTrackerItems.last()->text(COL_URL);
2020-03-06 15:04:29 +08:00
bool ok = false;
const QUrl newTrackerURL = AutoExpandableDialog::getText(this, tr("Tracker editing"), tr("Tracker URL:"),
QLineEdit::Normal, trackerURL.toString(), &ok).trimmed();
if (!ok) return;
2020-11-16 10:02:11 +03:00
if (!newTrackerURL.isValid())
{
QMessageBox::warning(this, tr("Tracker editing failed"), tr("The tracker URL entered is invalid."));
return;
}
if (newTrackerURL == trackerURL) return;
QVector<BitTorrent::TrackerEntry> trackers = torrent->trackers();
bool match = false;
2020-11-16 10:02:11 +03:00
for (BitTorrent::TrackerEntry &entry : trackers)
{
2021-03-01 16:06:08 +03:00
if (newTrackerURL == QUrl(entry.url))
2020-11-16 10:02:11 +03:00
{
QMessageBox::warning(this, tr("Tracker editing failed"), tr("The tracker URL already exists."));
return;
}
2021-03-01 16:06:08 +03:00
if (!match && (trackerURL == QUrl(entry.url)))
2020-11-16 10:02:11 +03:00
{
match = true;
2021-03-01 16:06:08 +03:00
entry.url = newTrackerURL.toString();
}
}
2013-02-10 20:15:36 +04:00
2015-04-19 18:17:47 +03:00
torrent->replaceTrackers(trackers);
if (!torrent->isPaused())
torrent->forceReannounce();
2013-01-11 22:58:38 +04:00
}
void TrackerListWidget::reannounceSelected()
{
const QList<QTreeWidgetItem *> selItems = selectedItems();
if (selItems.isEmpty()) return;
BitTorrent::Torrent *const torrent = m_properties->getCurrentTorrent();
2015-04-19 18:17:47 +03:00
if (!torrent) return;
const QVector<BitTorrent::TrackerEntry> trackers = torrent->trackers();
2020-11-16 10:02:11 +03:00
for (const QTreeWidgetItem *item : selItems)
{
// DHT case
2020-11-16 10:02:11 +03:00
if (item == m_DHTItem)
{
torrent->forceDHTAnnounce();
continue;
}
// Trackers case
2020-11-16 10:02:11 +03:00
for (int i = 0; i < trackers.size(); ++i)
{
2021-03-01 16:06:08 +03:00
if (item->text(COL_URL) == trackers[i].url)
2020-11-16 10:02:11 +03:00
{
torrent->forceReannounce(i);
break;
}
}
}
2015-04-19 18:17:47 +03:00
loadTrackers();
}
void TrackerListWidget::showTrackerListMenu(const QPoint &)
{
BitTorrent::Torrent *const torrent = m_properties->getCurrentTorrent();
if (!torrent) return;
QMenu *menu = new QMenu(this);
menu->setAttribute(Qt::WA_DeleteOnClose);
// Add actions
2021-01-17 14:56:56 +08:00
menu->addAction(UIThemeManager::instance()->getIcon("list-add"), tr("Add a new tracker...")
, this, &TrackerListWidget::askForTrackers);
2020-11-16 10:02:11 +03:00
if (!getSelectedTrackerItems().isEmpty())
{
2021-01-17 14:56:56 +08:00
menu->addAction(UIThemeManager::instance()->getIcon("edit-rename"),tr("Edit tracker URL...")
, this, &TrackerListWidget::editSelectedTracker);
menu->addAction(UIThemeManager::instance()->getIcon("list-remove"), tr("Remove tracker")
, this, &TrackerListWidget::deleteSelectedTrackers);
menu->addAction(UIThemeManager::instance()->getIcon("edit-copy"), tr("Copy tracker URL")
, this, &TrackerListWidget::copyTrackerUrl);
}
2020-11-16 10:02:11 +03:00
if (!torrent->isPaused())
{
2021-01-17 14:56:56 +08:00
menu->addAction(UIThemeManager::instance()->getIcon("view-refresh"), tr("Force reannounce to selected trackers")
, this, &TrackerListWidget::reannounceSelected);
menu->addSeparator();
2021-01-17 14:56:56 +08:00
menu->addAction(UIThemeManager::instance()->getIcon("view-refresh"), tr("Force reannounce to all trackers")
, this, [this]()
{
BitTorrent::Torrent *h = m_properties->getCurrentTorrent();
h->forceReannounce();
h->forceDHTAnnounce();
});
}
menu->popup(QCursor::pos());
}
void TrackerListWidget::loadSettings()
{
2017-03-10 11:43:59 +02:00
header()->restoreState(Preferences::instance()->getPropTrackerListState());
}
void TrackerListWidget::saveSettings() const
{
Preferences::instance()->setPropTrackerListState(header()->saveState());
}
2017-03-10 11:43:59 +02:00
QStringList TrackerListWidget::headerLabels()
2017-03-10 11:43:59 +02:00
{
2020-11-16 10:02:11 +03:00
return
{
tr("Tier")
2017-03-10 11:43:59 +02:00
, tr("URL")
, tr("Status")
, tr("Peers")
, tr("Seeds")
, tr("Leeches")
2017-03-10 11:43:59 +02:00
, tr("Downloaded")
, tr("Message")
};
}
int TrackerListWidget::visibleColumnsCount() const
2017-03-10 11:43:59 +02:00
{
int count = 0;
for (int i = 0, iMax = header()->count(); i < iMax; ++i)
2020-11-16 10:02:11 +03:00
{
2017-03-10 11:43:59 +02:00
if (!isColumnHidden(i))
++count;
2017-03-10 11:43:59 +02:00
}
return count;
2017-03-10 11:43:59 +02:00
}
void TrackerListWidget::displayColumnHeaderMenu()
2017-03-10 11:43:59 +02:00
{
QMenu *menu = new QMenu(this);
menu->setAttribute(Qt::WA_DeleteOnClose);
menu->setTitle(tr("Column visibility"));
menu->setToolTipsVisible(true);
2020-11-16 10:02:11 +03:00
for (int i = 0; i < COL_COUNT; ++i)
{
QAction *action = menu->addAction(headerLabels().at(i), this, [this, i](const bool checked)
{
if (!checked && (visibleColumnsCount() <= 1))
return;
setColumnHidden(i, !checked);
if (checked && (columnWidth(i) <= 5))
resizeColumnToContents(i);
saveSettings();
});
action->setCheckable(true);
action->setChecked(!isColumnHidden(i));
}
menu->addSeparator();
QAction *resizeAction = menu->addAction(tr("Resize columns"), this, [this]()
{
for (int i = 0, count = header()->count(); i < count; ++i)
{
if (!isColumnHidden(i))
resizeColumnToContents(i);
}
saveSettings();
});
resizeAction->setToolTip(tr("Resize all non-hidden columns to the size of their contents"));
menu->popup(QCursor::pos());
2017-03-10 11:43:59 +02:00
}