2009-11-19 14:09:03 +03:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
|
|
|
* Copyright (C) 2006 Christophe Dumez
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Contact : chris@qbittorrent.org
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TRACKERLIST_H
|
|
|
|
#define TRACKERLIST_H
|
|
|
|
|
|
|
|
#include <QTreeWidget>
|
|
|
|
#include <QTreeWidgetItem>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QMenu>
|
2009-11-19 15:27:52 +03:00
|
|
|
#include <QSettings>
|
2009-11-19 14:09:03 +03:00
|
|
|
#include <QHash>
|
|
|
|
#include <QAction>
|
2009-11-19 18:43:00 +03:00
|
|
|
#include <QColor>
|
2009-11-19 14:09:03 +03:00
|
|
|
#include "propertieswidget.h"
|
|
|
|
#include "trackersadditiondlg.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "bittorrent.h"
|
|
|
|
|
2009-11-19 18:04:43 +03:00
|
|
|
enum TrackerListColumn {COL_URL, COL_STATUS, COL_PEERS, COL_MSG};
|
2009-11-21 18:18:09 +03:00
|
|
|
#define NB_STICKY_ITEM 3
|
2009-11-19 14:09:03 +03:00
|
|
|
|
|
|
|
class TrackerList: public QTreeWidget {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private:
|
|
|
|
PropertiesWidget *properties;
|
|
|
|
QHash<QString, QTreeWidgetItem*> tracker_items;
|
2009-11-19 18:43:00 +03:00
|
|
|
QTreeWidgetItem* dht_item;
|
2009-11-21 18:18:09 +03:00
|
|
|
QTreeWidgetItem* pex_item;
|
|
|
|
QTreeWidgetItem* lsd_item;
|
2009-11-19 14:09:03 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
TrackerList(PropertiesWidget *properties): QTreeWidget(), properties(properties) {
|
|
|
|
// Graphical settings
|
|
|
|
setRootIsDecorated(false);
|
|
|
|
setAllColumnsShowFocus(true);
|
|
|
|
setItemsExpandable(false);
|
|
|
|
setSelectionMode(QAbstractItemView::ExtendedSelection);
|
|
|
|
// Context menu
|
|
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showTrackerListMenu(QPoint)));
|
|
|
|
// Set header
|
|
|
|
QStringList header;
|
|
|
|
header << tr("URL");
|
|
|
|
header << tr("Status");
|
2009-11-19 18:04:43 +03:00
|
|
|
header << tr("Peers");
|
2009-11-19 14:09:03 +03:00
|
|
|
header << tr("Message");
|
|
|
|
setHeaderItem(new QTreeWidgetItem(header));
|
2009-11-21 18:23:27 +03:00
|
|
|
dht_item = new QTreeWidgetItem(QStringList("** "+tr("[DHT]")+" **"));
|
2009-11-19 18:43:00 +03:00
|
|
|
insertTopLevelItem(0, dht_item);
|
|
|
|
setRowColor(0, QColor("grey"));
|
2009-11-21 18:23:27 +03:00
|
|
|
pex_item = new QTreeWidgetItem(QStringList("** "+tr("[PeX]")+" **"));
|
2009-11-21 18:18:09 +03:00
|
|
|
insertTopLevelItem(1, pex_item);
|
|
|
|
setRowColor(1, QColor("grey"));
|
2009-11-21 18:23:27 +03:00
|
|
|
lsd_item = new QTreeWidgetItem(QStringList("** "+tr("[LSD]")+" **"));
|
2009-11-21 18:18:09 +03:00
|
|
|
insertTopLevelItem(2, lsd_item);
|
|
|
|
setRowColor(2, QColor("grey"));
|
2009-11-19 15:27:52 +03:00
|
|
|
loadSettings();
|
2009-11-19 14:09:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
~TrackerList() {
|
2009-11-19 15:27:52 +03:00
|
|
|
saveSettings();
|
2009-11-19 14:09:03 +03:00
|
|
|
}
|
|
|
|
|
2009-11-19 18:43:00 +03:00
|
|
|
protected:
|
|
|
|
QList<QTreeWidgetItem*> getSelectedTrackerItems() const {
|
|
|
|
QList<QTreeWidgetItem*> selected_items = selectedItems();
|
|
|
|
QList<QTreeWidgetItem*> selected_trackers;
|
|
|
|
foreach(QTreeWidgetItem *item, selectedItems()) {
|
|
|
|
if(indexOfTopLevelItem(item) >= NB_STICKY_ITEM) { // Ignore STICKY ITEMS
|
|
|
|
selected_trackers << item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return selected_trackers;
|
|
|
|
}
|
|
|
|
|
2009-11-19 14:09:03 +03:00
|
|
|
public slots:
|
|
|
|
|
2009-11-19 18:43:00 +03:00
|
|
|
void setRowColor(int row, QColor color) {
|
|
|
|
unsigned int nbColumns = columnCount();
|
|
|
|
QTreeWidgetItem *item = topLevelItem(row);
|
|
|
|
for(unsigned int i=0; i<nbColumns; ++i) {
|
|
|
|
item->setData(i, Qt::ForegroundRole, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-19 15:54:53 +03:00
|
|
|
void clear() {
|
|
|
|
qDeleteAll(tracker_items.values());
|
|
|
|
tracker_items.clear();
|
2009-11-19 18:43:00 +03:00
|
|
|
dht_item->setText(COL_PEERS, "");
|
|
|
|
dht_item->setText(COL_STATUS, "");
|
|
|
|
dht_item->setText(COL_MSG, "");
|
2009-11-21 18:18:09 +03:00
|
|
|
pex_item->setText(COL_PEERS, "");
|
|
|
|
pex_item->setText(COL_STATUS, "");
|
|
|
|
pex_item->setText(COL_MSG, "");
|
|
|
|
lsd_item->setText(COL_PEERS, "");
|
|
|
|
lsd_item->setText(COL_STATUS, "");
|
|
|
|
lsd_item->setText(COL_MSG, "");
|
2009-11-19 18:43:00 +03:00
|
|
|
}
|
|
|
|
|
2009-11-21 18:18:09 +03:00
|
|
|
void loadStickyItems(const QTorrentHandle &h) {
|
|
|
|
// XXX: libtorrent should provide this info...
|
|
|
|
// Count peers from DHT, LSD, PeX
|
|
|
|
uint nb_dht=0, nb_lsd=0, nb_pex=0;
|
|
|
|
std::vector<peer_info> peers;
|
|
|
|
h.get_peer_info(peers);
|
|
|
|
std::vector<peer_info>::iterator it;
|
|
|
|
for(it=peers.begin(); it!=peers.end(); it++) {
|
2009-11-22 14:16:30 +03:00
|
|
|
if(it->source & peer_info::dht)
|
|
|
|
++nb_dht;
|
|
|
|
if(it->source & peer_info::lsd)
|
|
|
|
++nb_lsd;
|
|
|
|
if(it->source & peer_info::pex)
|
|
|
|
++nb_pex;
|
2009-11-21 18:18:09 +03:00
|
|
|
}
|
2009-11-19 18:43:00 +03:00
|
|
|
// load DHT information
|
|
|
|
if(properties->getBTSession()->isDHTEnabled() && !h.priv()) {
|
|
|
|
dht_item->setText(COL_STATUS, tr("Working"));
|
|
|
|
} else {
|
|
|
|
dht_item->setText(COL_STATUS, tr("Disabled"));
|
|
|
|
}
|
2009-11-21 18:18:09 +03:00
|
|
|
dht_item->setText(COL_PEERS, QString::number(nb_dht));
|
2009-11-19 18:43:00 +03:00
|
|
|
if(h.priv()) {
|
|
|
|
dht_item->setText(COL_MSG, tr("This torrent is private"));
|
|
|
|
}
|
2009-11-21 18:18:09 +03:00
|
|
|
// Load PeX Information
|
|
|
|
pex_item->setText(COL_STATUS, tr("Working"));
|
|
|
|
pex_item->setText(COL_PEERS, QString::number(nb_pex));
|
|
|
|
// Load LSD Information
|
|
|
|
if(properties->getBTSession()->isLSDEnabled())
|
|
|
|
lsd_item->setText(COL_STATUS, tr("Working"));
|
|
|
|
else
|
|
|
|
lsd_item->setText(COL_STATUS, tr("Disabled"));
|
|
|
|
lsd_item->setText(COL_PEERS, QString::number(nb_lsd));
|
2009-11-19 15:54:53 +03:00
|
|
|
}
|
|
|
|
|
2009-11-19 14:09:03 +03:00
|
|
|
void loadTrackers() {
|
|
|
|
// Load trackers from torrent handle
|
|
|
|
QTorrentHandle h = properties->getCurrentTorrent();
|
|
|
|
if(!h.is_valid()) return;
|
2009-11-21 18:18:09 +03:00
|
|
|
loadStickyItems(h);
|
2009-11-19 18:43:00 +03:00
|
|
|
// Load actual trackers information
|
2009-11-21 18:18:09 +03:00
|
|
|
QHash<QString, TrackerInfos> trackers_data = properties->getBTSession()->getTrackersInfo(h.hash());
|
2009-11-19 18:43:00 +03:00
|
|
|
QStringList old_trackers_urls = tracker_items.keys();
|
2009-11-19 14:09:03 +03:00
|
|
|
std::vector<announce_entry> trackers = h.trackers();
|
|
|
|
std::vector<announce_entry>::iterator it;
|
|
|
|
for(it = trackers.begin(); it != trackers.end(); it++) {
|
|
|
|
QStringList item_list;
|
2009-11-24 14:14:02 +03:00
|
|
|
QString tracker_url = misc::toQString(it->url);
|
2009-11-19 14:09:03 +03:00
|
|
|
QTreeWidgetItem *item = tracker_items.value(tracker_url, 0);
|
|
|
|
if(!item) {
|
|
|
|
item = new QTreeWidgetItem();
|
|
|
|
item->setText(COL_URL, tracker_url);
|
|
|
|
addTopLevelItem(item);
|
|
|
|
tracker_items[tracker_url] = item;
|
|
|
|
} else {
|
|
|
|
old_trackers_urls.removeOne(tracker_url);
|
|
|
|
}
|
2009-11-22 14:16:30 +03:00
|
|
|
TrackerInfos data = trackers_data.value(tracker_url, TrackerInfos(tracker_url));
|
|
|
|
QString error_message = data.last_message.trimmed();
|
|
|
|
#ifdef LIBTORRENT_0_15
|
2009-11-24 14:14:02 +03:00
|
|
|
if(it->verified) {
|
2009-11-19 14:09:03 +03:00
|
|
|
item->setText(COL_STATUS, tr("Working"));
|
|
|
|
} else {
|
2009-11-24 14:14:02 +03:00
|
|
|
if(it->updating && it->fails == 0) {
|
2009-11-19 14:09:03 +03:00
|
|
|
item->setText(COL_STATUS, tr("Updating..."));
|
|
|
|
} else {
|
2009-11-24 14:14:02 +03:00
|
|
|
if(it->fails > 0) {
|
2009-11-19 14:09:03 +03:00
|
|
|
item->setText(COL_STATUS, tr("Not working"));
|
|
|
|
} else {
|
|
|
|
item->setText(COL_STATUS, tr("Not contacted yet"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-22 14:16:30 +03:00
|
|
|
#else
|
2009-11-23 10:18:28 +03:00
|
|
|
if(data.verified) {
|
2009-11-22 14:16:30 +03:00
|
|
|
item->setText(COL_STATUS, tr("Working"));
|
|
|
|
} else {
|
2009-11-23 10:18:28 +03:00
|
|
|
if(data.fail_count > 0)
|
|
|
|
item->setText(COL_STATUS, tr("Not working"));
|
|
|
|
else
|
|
|
|
item->setText(COL_STATUS, tr("Not contacted yet"));
|
2009-11-22 14:16:30 +03:00
|
|
|
}
|
|
|
|
#endif
|
2009-11-19 18:04:43 +03:00
|
|
|
item->setText(COL_PEERS, QString::number(trackers_data.value(tracker_url, TrackerInfos(tracker_url)).num_peers));
|
2009-11-22 14:16:30 +03:00
|
|
|
item->setText(COL_MSG, error_message);
|
2009-11-19 14:09:03 +03:00
|
|
|
}
|
|
|
|
// Remove old trackers
|
|
|
|
foreach(const QString &tracker, old_trackers_urls) {
|
|
|
|
delete tracker_items.take(tracker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask the user for new trackers and add them to the torrent
|
|
|
|
void askForTrackers(){
|
|
|
|
QStringList trackers = TrackersAdditionDlg::asForTrackers();
|
|
|
|
if(!trackers.empty()) {
|
|
|
|
QTorrentHandle h = properties->getCurrentTorrent();
|
|
|
|
if(!h.is_valid()) return;
|
|
|
|
foreach(const QString& tracker, trackers) {
|
|
|
|
announce_entry url(tracker.toStdString());
|
|
|
|
url.tier = 0;
|
|
|
|
h.add_tracker(url);
|
|
|
|
}
|
|
|
|
// Reannounce to new trackers
|
|
|
|
h.force_reannounce();
|
|
|
|
// Reload tracker list
|
|
|
|
loadTrackers();
|
|
|
|
// XXX: I don't think this is necessary now
|
|
|
|
//BTSession->saveTrackerFile(h.hash());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void deleteSelectedTrackers(){
|
|
|
|
QTorrentHandle h = properties->getCurrentTorrent();
|
|
|
|
if(!h.is_valid()) {
|
|
|
|
clear();
|
|
|
|
return;
|
|
|
|
}
|
2009-11-19 18:43:00 +03:00
|
|
|
QList<QTreeWidgetItem *> selected_items = getSelectedTrackerItems();
|
2009-11-19 14:09:03 +03:00
|
|
|
if(selected_items.isEmpty()) return;
|
|
|
|
QStringList urls_to_remove;
|
|
|
|
foreach(QTreeWidgetItem *item, selected_items){
|
|
|
|
QString tracker_url = item->data(COL_URL, Qt::DisplayRole).toString();
|
|
|
|
urls_to_remove << tracker_url;
|
|
|
|
tracker_items.remove(tracker_url);
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
// Iterate of trackers and remove selected ones
|
|
|
|
std::vector<announce_entry> trackers = h.trackers();
|
|
|
|
std::vector<announce_entry>::iterator it = trackers.begin();
|
|
|
|
while(it != trackers.end()) {
|
|
|
|
int index = urls_to_remove.indexOf(misc::toQString((*it).url));
|
|
|
|
if(index >= 0) {
|
|
|
|
trackers.erase(it);
|
|
|
|
urls_to_remove.removeAt(index);
|
|
|
|
} else {
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h.replace_trackers(trackers);
|
|
|
|
h.force_reannounce();
|
|
|
|
// Reload Trackers
|
|
|
|
loadTrackers();
|
|
|
|
//XXX: I don't think this is necessary
|
|
|
|
//BTSession->saveTrackerFile(h.hash());
|
|
|
|
}
|
|
|
|
|
|
|
|
void showTrackerListMenu(QPoint) {
|
2009-11-19 18:43:00 +03:00
|
|
|
QList<QTreeWidgetItem*> selected_items = getSelectedTrackerItems();
|
2009-11-19 14:09:03 +03:00
|
|
|
QMenu menu;
|
|
|
|
// Add actions
|
|
|
|
QAction *addAct = menu.addAction(QIcon(":/Icons/oxygen/list-add.png"), tr("Add a new tracker"));
|
|
|
|
QAction *delAct = 0;
|
2009-11-19 18:43:00 +03:00
|
|
|
if(!getSelectedTrackerItems().isEmpty()) {
|
2009-11-19 14:09:03 +03:00
|
|
|
delAct = menu.addAction(QIcon(":/Icons/oxygen/list-remove.png"), "Remove tracker");
|
|
|
|
}
|
|
|
|
QAction *act = menu.exec(QCursor::pos());
|
|
|
|
if(act == addAct) {
|
|
|
|
askForTrackers();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(act == delAct) {
|
|
|
|
deleteSelectedTrackers();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-19 15:27:52 +03:00
|
|
|
void loadSettings() {
|
|
|
|
QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
|
|
|
|
QVariantList contentColsWidths = settings.value(QString::fromUtf8("TorrentProperties/Trackers/trackersColsWidth"), QVariantList()).toList();
|
|
|
|
if(!contentColsWidths.empty()) {
|
|
|
|
for(int i=0; i<contentColsWidths.size(); ++i) {
|
|
|
|
setColumnWidth(i, contentColsWidths.at(i).toInt());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setColumnWidth(0, 300);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void saveSettings() const {
|
|
|
|
QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
|
|
|
|
QVariantList contentColsWidths;
|
|
|
|
for(int i=0; i<columnCount(); ++i) {
|
|
|
|
contentColsWidths.append(columnWidth(i));
|
|
|
|
}
|
|
|
|
settings.setValue(QString::fromUtf8("TorrentProperties/Trackers/trackersColsWidth"), contentColsWidths);
|
|
|
|
}
|
|
|
|
|
2009-11-19 14:09:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // TRACKERLIST_H
|