From cd830768d6373356e4e55adf609cafe0b7729bd3 Mon Sep 17 00:00:00 2001 From: Nick Tiskov Date: Fri, 11 Jan 2013 22:58:38 +0400 Subject: [PATCH 1/4] Allow to edit tracker URI --- src/properties/properties.pri | 6 +- src/properties/trackereditdlg.h | 121 +++++++++++++++++++++++++++++++ src/properties/trackereditdlg.ui | 62 ++++++++++++++++ src/properties/trackerlist.cpp | 22 ++++++ src/properties/trackerlist.h | 1 + 5 files changed, 210 insertions(+), 2 deletions(-) create mode 100644 src/properties/trackereditdlg.h create mode 100644 src/properties/trackereditdlg.ui diff --git a/src/properties/properties.pri b/src/properties/properties.pri index 48736b0e1..dca4064b7 100644 --- a/src/properties/properties.pri +++ b/src/properties/properties.pri @@ -2,7 +2,8 @@ INCLUDEPATH += $$PWD FORMS += $$PWD/propertieswidget.ui \ $$PWD/trackersadditiondlg.ui \ - $$PWD/peer.ui + $$PWD/peer.ui \ + $$PWD/trackereditdlg.ui HEADERS += $$PWD/propertieswidget.h \ $$PWD/peerlistwidget.h \ @@ -13,7 +14,8 @@ HEADERS += $$PWD/propertieswidget.h \ $$PWD/peeraddition.h \ $$PWD/trackersadditiondlg.h \ $$PWD/pieceavailabilitybar.h \ - $$PWD/proptabbar.h + $$PWD/proptabbar.h \ + $$PWD/trackereditdlg.h SOURCES += $$PWD/propertieswidget.cpp \ $$PWD/peerlistwidget.cpp \ diff --git a/src/properties/trackereditdlg.h b/src/properties/trackereditdlg.h new file mode 100644 index 000000000..9e7ee8c88 --- /dev/null +++ b/src/properties/trackereditdlg.h @@ -0,0 +1,121 @@ +/* + * 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 TRACKEREDITDLG_H +#define TRACKEREDITDLG_H + +#include +#include +#include "ui_trackereditdlg.h" +#include "qtorrenthandle.h" +#include "misc.h" + +using namespace libtorrent; + +class TrackerEditDlg : public QDialog, private Ui::TrackerEditDlg { + Q_OBJECT + +private: + QTorrentHandle h; + QString m_tracker_URI; + +public: + TrackerEditDlg(QTorrentHandle h, QString tracker_URI, QWidget *parent=0) : QDialog(parent), h(h) { + setupUi(this); + this->m_tracker_URI = tracker_URI.trimmed(); + this->tracker_URI_edit->setText(m_tracker_URI); + connect(this->button_OK,SIGNAL(clicked()),this,SLOT(TrackerEditDlg::on_button_OK_clicked())); + connect(this->button_Cancel,SIGNAL(clicked()),this,SLOT(TrackerEditDlg::on_button_Cancel_clicked())); + } + + ~TrackerEditDlg() {} + +public slots: + void on_button_OK_clicked() { + if (replaceTracker()) + emit TrackerEditDlg::accept(); + else + emit TrackerEditDlg::reject(); + } + + void on_button_Cancel_clicked() { + emit TrackerEditDlg::reject(); + } + +private: + bool replaceTracker() { + if (!h.is_valid()) return false; + + QString new_URI = this->tracker_URI_edit->text().trimmed(); + if (new_URI.isEmpty() + || new_URI == this->m_tracker_URI) return false; + + std::vector trackers = h.trackers(); + if (trackers.empty()) return false; + + std::vector new_trackers; + + std::vector::iterator it = trackers.begin(); + std::vector::iterator itend = trackers.end(); + bool match = false; + + for ( ; it != itend; ++it) { + if (new_URI == misc::toQString((*it).url)) + return false; // Tracker already exists; silently ignoring + + announce_entry URI(*it); + + if (this->m_tracker_URI == misc::toQString((*it).url) && !match) { + announce_entry temp_URI(new_URI.toStdString()); + URI = temp_URI; + URI.tier = (*it).tier; + match = true; + } + + new_trackers.push_back(URI); + } + if (match == false) return false; // Found no tracker to replace + + h.replace_trackers(new_trackers); + return true; + } + +public: + static bool editSelectedTracker(QTorrentHandle h, QString tracker_URI) { + TrackerEditDlg dlg(h,tracker_URI); + if (dlg.exec() != QDialog::Accepted) { + return false; + } + return true; + } + +}; + +#endif diff --git a/src/properties/trackereditdlg.ui b/src/properties/trackereditdlg.ui new file mode 100644 index 000000000..2745f533e --- /dev/null +++ b/src/properties/trackereditdlg.ui @@ -0,0 +1,62 @@ + + + TrackerEditDlg + + + + 0 + 0 + 370 + 88 + + + + Edit Tracker Dialog + + + + + + Tracker URI: + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + OK + + + + + + + Cancel + + + + + + + + + + diff --git a/src/properties/trackerlist.cpp b/src/properties/trackerlist.cpp index 325c46071..4435adf78 100644 --- a/src/properties/trackerlist.cpp +++ b/src/properties/trackerlist.cpp @@ -40,6 +40,7 @@ #include "trackerlist.h" #include "propertieswidget.h" #include "trackersadditiondlg.h" +#include "trackereditdlg.h" #include "iconprovider.h" #include "qbtsession.h" #include "qinisettings.h" @@ -343,6 +344,21 @@ void TrackerList::deleteSelectedTrackers() { loadTrackers(); } +void TrackerList::editSelectedTracker() { + QTorrentHandle h = properties->getCurrentTorrent(); + if (!h.is_valid()) return; + + QList selected_items = getSelectedTrackerItems(); + if (selected_items.isEmpty()) return; + // During multi-select only process item selected last + QString tracker_URI = selected_items.last()->data(COL_URL, Qt::DisplayRole).toString(); + if (!TrackerEditDlg::editSelectedTracker(h,tracker_URI)) + return; + + h.force_reannounce(); + loadTrackers(); +} + void TrackerList::showTrackerListMenu(QPoint) { QTorrentHandle h = properties->getCurrentTorrent(); if (!h.is_valid()) return; @@ -352,8 +368,10 @@ void TrackerList::showTrackerListMenu(QPoint) { QAction *addAct = menu.addAction(IconProvider::instance()->getIcon("list-add"), tr("Add a new tracker...")); QAction *copyAct = menu.addAction(IconProvider::instance()->getIcon("edit-copy"), tr("Copy tracker url")); QAction *delAct = 0; + QAction *editAct = 0; if (!getSelectedTrackerItems().isEmpty()) { delAct = menu.addAction(IconProvider::instance()->getIcon("list-remove"), tr("Remove tracker")); + editAct = menu.addAction(IconProvider::instance()->getIcon("edit-rename"),tr("Edit selected tracker URI")); } QAction *act = menu.exec(QCursor::pos()); if (act == 0) return; @@ -369,6 +387,10 @@ void TrackerList::showTrackerListMenu(QPoint) { deleteSelectedTrackers(); return; } + if (act == editAct) { + editSelectedTracker(); + return; + } } void TrackerList::loadSettings() { diff --git a/src/properties/trackerlist.h b/src/properties/trackerlist.h index d4923be22..19aee9ae0 100644 --- a/src/properties/trackerlist.h +++ b/src/properties/trackerlist.h @@ -72,6 +72,7 @@ public slots: void askForTrackers(); void copyTrackerUrl(); void deleteSelectedTrackers(); + void editSelectedTracker(); void showTrackerListMenu(QPoint); void loadSettings(); void saveSettings() const; From 4cb783d5bf0d47aeca71092c99def846ec3ee51f Mon Sep 17 00:00:00 2001 From: Nick Tiskov Date: Sun, 10 Feb 2013 20:15:36 +0400 Subject: [PATCH 2/4] Use QInputDilog for tracker editing --- src/properties/properties.pri | 6 +- src/properties/trackereditdlg.h | 121 ------------------------------- src/properties/trackereditdlg.ui | 62 ---------------- src/properties/trackerlist.cpp | 56 ++++++++++++-- 4 files changed, 52 insertions(+), 193 deletions(-) delete mode 100644 src/properties/trackereditdlg.h delete mode 100644 src/properties/trackereditdlg.ui diff --git a/src/properties/properties.pri b/src/properties/properties.pri index dca4064b7..48736b0e1 100644 --- a/src/properties/properties.pri +++ b/src/properties/properties.pri @@ -2,8 +2,7 @@ INCLUDEPATH += $$PWD FORMS += $$PWD/propertieswidget.ui \ $$PWD/trackersadditiondlg.ui \ - $$PWD/peer.ui \ - $$PWD/trackereditdlg.ui + $$PWD/peer.ui HEADERS += $$PWD/propertieswidget.h \ $$PWD/peerlistwidget.h \ @@ -14,8 +13,7 @@ HEADERS += $$PWD/propertieswidget.h \ $$PWD/peeraddition.h \ $$PWD/trackersadditiondlg.h \ $$PWD/pieceavailabilitybar.h \ - $$PWD/proptabbar.h \ - $$PWD/trackereditdlg.h + $$PWD/proptabbar.h SOURCES += $$PWD/propertieswidget.cpp \ $$PWD/peerlistwidget.cpp \ diff --git a/src/properties/trackereditdlg.h b/src/properties/trackereditdlg.h deleted file mode 100644 index 9e7ee8c88..000000000 --- a/src/properties/trackereditdlg.h +++ /dev/null @@ -1,121 +0,0 @@ -/* - * 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 TRACKEREDITDLG_H -#define TRACKEREDITDLG_H - -#include -#include -#include "ui_trackereditdlg.h" -#include "qtorrenthandle.h" -#include "misc.h" - -using namespace libtorrent; - -class TrackerEditDlg : public QDialog, private Ui::TrackerEditDlg { - Q_OBJECT - -private: - QTorrentHandle h; - QString m_tracker_URI; - -public: - TrackerEditDlg(QTorrentHandle h, QString tracker_URI, QWidget *parent=0) : QDialog(parent), h(h) { - setupUi(this); - this->m_tracker_URI = tracker_URI.trimmed(); - this->tracker_URI_edit->setText(m_tracker_URI); - connect(this->button_OK,SIGNAL(clicked()),this,SLOT(TrackerEditDlg::on_button_OK_clicked())); - connect(this->button_Cancel,SIGNAL(clicked()),this,SLOT(TrackerEditDlg::on_button_Cancel_clicked())); - } - - ~TrackerEditDlg() {} - -public slots: - void on_button_OK_clicked() { - if (replaceTracker()) - emit TrackerEditDlg::accept(); - else - emit TrackerEditDlg::reject(); - } - - void on_button_Cancel_clicked() { - emit TrackerEditDlg::reject(); - } - -private: - bool replaceTracker() { - if (!h.is_valid()) return false; - - QString new_URI = this->tracker_URI_edit->text().trimmed(); - if (new_URI.isEmpty() - || new_URI == this->m_tracker_URI) return false; - - std::vector trackers = h.trackers(); - if (trackers.empty()) return false; - - std::vector new_trackers; - - std::vector::iterator it = trackers.begin(); - std::vector::iterator itend = trackers.end(); - bool match = false; - - for ( ; it != itend; ++it) { - if (new_URI == misc::toQString((*it).url)) - return false; // Tracker already exists; silently ignoring - - announce_entry URI(*it); - - if (this->m_tracker_URI == misc::toQString((*it).url) && !match) { - announce_entry temp_URI(new_URI.toStdString()); - URI = temp_URI; - URI.tier = (*it).tier; - match = true; - } - - new_trackers.push_back(URI); - } - if (match == false) return false; // Found no tracker to replace - - h.replace_trackers(new_trackers); - return true; - } - -public: - static bool editSelectedTracker(QTorrentHandle h, QString tracker_URI) { - TrackerEditDlg dlg(h,tracker_URI); - if (dlg.exec() != QDialog::Accepted) { - return false; - } - return true; - } - -}; - -#endif diff --git a/src/properties/trackereditdlg.ui b/src/properties/trackereditdlg.ui deleted file mode 100644 index 2745f533e..000000000 --- a/src/properties/trackereditdlg.ui +++ /dev/null @@ -1,62 +0,0 @@ - - - TrackerEditDlg - - - - 0 - 0 - 370 - 88 - - - - Edit Tracker Dialog - - - - - - Tracker URI: - - - - - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - OK - - - - - - - Cancel - - - - - - - - - - diff --git a/src/properties/trackerlist.cpp b/src/properties/trackerlist.cpp index 4435adf78..1bd0c8950 100644 --- a/src/properties/trackerlist.cpp +++ b/src/properties/trackerlist.cpp @@ -35,12 +35,13 @@ #include #include #include +#include +#include #include #include #include "trackerlist.h" #include "propertieswidget.h" #include "trackersadditiondlg.h" -#include "trackereditdlg.h" #include "iconprovider.h" #include "qbtsession.h" #include "qinisettings.h" @@ -346,15 +347,57 @@ void TrackerList::deleteSelectedTrackers() { void TrackerList::editSelectedTracker() { QTorrentHandle h = properties->getCurrentTorrent(); - if (!h.is_valid()) return; + if (!h.is_valid()) + return; QList selected_items = getSelectedTrackerItems(); if (selected_items.isEmpty()) return; // During multi-select only process item selected last - QString tracker_URI = selected_items.last()->data(COL_URL, Qt::DisplayRole).toString(); - if (!TrackerEditDlg::editSelectedTracker(h,tracker_URI)) + QUrl tracker_url = selected_items.last()->text(COL_URL); + + QInputDialog editDlg(this); + editDlg.setInputMode(QInputDialog::TextInput); + editDlg.setLabelText(tr("Tracker URL:")); + editDlg.setWindowTitle(tr("Edit Tracker Dialog")); + editDlg.setTextValue(tracker_url.toString()); + QSize dlgSize = editDlg.size(); + dlgSize.setWidth(350); + editDlg.resize(dlgSize); + + bool dlgResult = editDlg.exec(); + if(!dlgResult) return; + if (!h.is_valid()) + return; + + QUrl new_tracker_url = editDlg.textValue().trimmed(); + if (new_tracker_url.isEmpty() || !new_tracker_url.isValid() || new_tracker_url == tracker_url) + return; + + std::vector trackers = h.trackers(); + if (trackers.empty()) + return; + + std::vector::iterator it = trackers.begin(); + std::vector::iterator itend = trackers.end(); + bool match = false; + + for ( ; it != itend; ++it) { + if (new_tracker_url == QUrl(misc::toQString(it->url))) + return; // Tracker already exists; silently ignoring + + if (tracker_url == QUrl(misc::toQString(it->url)) && !match) { + announce_entry temp_URI(new_tracker_url.toString().toStdString()); + temp_URI.tier = it->tier; + match = true; + *it = temp_URI; + } + } + if (!match) + return; // Found no tracker to replace + + h.replace_trackers(trackers); h.force_reannounce(); loadTrackers(); } @@ -366,12 +409,13 @@ void TrackerList::showTrackerListMenu(QPoint) { QMenu menu; // Add actions QAction *addAct = menu.addAction(IconProvider::instance()->getIcon("list-add"), tr("Add a new tracker...")); - QAction *copyAct = menu.addAction(IconProvider::instance()->getIcon("edit-copy"), tr("Copy tracker url")); + QAction *copyAct = 0; QAction *delAct = 0; QAction *editAct = 0; if (!getSelectedTrackerItems().isEmpty()) { delAct = menu.addAction(IconProvider::instance()->getIcon("list-remove"), tr("Remove tracker")); - editAct = menu.addAction(IconProvider::instance()->getIcon("edit-rename"),tr("Edit selected tracker URI")); + copyAct = menu.addAction(IconProvider::instance()->getIcon("edit-copy"), tr("Copy tracker url")); + editAct = menu.addAction(IconProvider::instance()->getIcon("edit-rename"),tr("Edit selected tracker URL")); } QAction *act = menu.exec(QCursor::pos()); if (act == 0) return; From a7a4557b29bac19711be6ba7090f66fc65cbfb71 Mon Sep 17 00:00:00 2001 From: Nick Tiskov Date: Sun, 10 Feb 2013 22:04:48 +0400 Subject: [PATCH 3/4] fixup! Use QInputDilog for tracker editing --- src/properties/trackerlist.cpp | 96 +++++++++++++++++----------------- 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/src/properties/trackerlist.cpp b/src/properties/trackerlist.cpp index 1bd0c8950..28cce682e 100644 --- a/src/properties/trackerlist.cpp +++ b/src/properties/trackerlist.cpp @@ -346,59 +346,61 @@ void TrackerList::deleteSelectedTrackers() { } void TrackerList::editSelectedTracker() { - QTorrentHandle h = properties->getCurrentTorrent(); - if (!h.is_valid()) - return; + try { + QTorrentHandle h = properties->getCurrentTorrent(); - QList selected_items = getSelectedTrackerItems(); - if (selected_items.isEmpty()) return; - // During multi-select only process item selected last - QUrl tracker_url = selected_items.last()->text(COL_URL); + QList selected_items = getSelectedTrackerItems(); + if (selected_items.isEmpty()) + return; + // During multi-select only process item selected last + QUrl tracker_url = selected_items.last()->text(COL_URL); - QInputDialog editDlg(this); - editDlg.setInputMode(QInputDialog::TextInput); - editDlg.setLabelText(tr("Tracker URL:")); - editDlg.setWindowTitle(tr("Edit Tracker Dialog")); - editDlg.setTextValue(tracker_url.toString()); - QSize dlgSize = editDlg.size(); - dlgSize.setWidth(350); - editDlg.resize(dlgSize); + QInputDialog editDlg(this); + editDlg.setInputMode(QInputDialog::TextInput); + editDlg.setLabelText(tr("Tracker URL:")); + editDlg.setWindowTitle(tr("Tracker editing")); + editDlg.setTextValue(tracker_url.toString()); + QSize dlgSize = editDlg.size(); + dlgSize.setWidth(350); + editDlg.resize(dlgSize); - bool dlgResult = editDlg.exec(); - if(!dlgResult) - return; + if(!editDlg.exec()) + return; - if (!h.is_valid()) - return; - - QUrl new_tracker_url = editDlg.textValue().trimmed(); - if (new_tracker_url.isEmpty() || !new_tracker_url.isValid() || new_tracker_url == tracker_url) - return; - - std::vector trackers = h.trackers(); - if (trackers.empty()) - return; - - std::vector::iterator it = trackers.begin(); - std::vector::iterator itend = trackers.end(); - bool match = false; - - for ( ; it != itend; ++it) { - if (new_tracker_url == QUrl(misc::toQString(it->url))) - return; // Tracker already exists; silently ignoring - - if (tracker_url == QUrl(misc::toQString(it->url)) && !match) { - announce_entry temp_URI(new_tracker_url.toString().toStdString()); - temp_URI.tier = it->tier; - match = true; - *it = temp_URI; + QUrl new_tracker_url = editDlg.textValue().trimmed(); + if (!new_tracker_url.isValid()) { + QMessageBox::warning(this, tr("Tracker editing failed"), tr("The tracker URL entered is invalid.")); + return; } - } - if (!match) - return; // Found no tracker to replace + else if (new_tracker_url == tracker_url) + return; + + std::vector trackers = h.trackers(); + std::vector::iterator it = trackers.begin(); + std::vector::iterator itend = trackers.end(); + bool match = false; + + for ( ; it != itend; ++it) { + if (new_tracker_url == QUrl(misc::toQString(it->url))) { + QMessageBox::warning(this, tr("Tracker editing failed"), tr("The tracker URL already exists.")); + return; + } + + if (tracker_url == QUrl(misc::toQString(it->url)) && !match) { + announce_entry new_entry(new_tracker_url.toString().toStdString()); + new_entry.tier = it->tier; + match = true; + *it = new_entry; + } + } + + h.replace_trackers(trackers); + h.force_reannounce(); + h.force_dht_announce(); + } catch(invalid_handle&) { + return; + } - h.replace_trackers(trackers); - h.force_reannounce(); loadTrackers(); } From e05cce9b59ab849f404b65a0af2de46f72ff0ab3 Mon Sep 17 00:00:00 2001 From: Nick Tiskov Date: Sun, 10 Feb 2013 22:39:15 +0400 Subject: [PATCH 4/4] Small fix to tracker editing --- src/properties/trackerlist.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/properties/trackerlist.cpp b/src/properties/trackerlist.cpp index 28cce682e..8464e314e 100644 --- a/src/properties/trackerlist.cpp +++ b/src/properties/trackerlist.cpp @@ -372,7 +372,7 @@ void TrackerList::editSelectedTracker() { QMessageBox::warning(this, tr("Tracker editing failed"), tr("The tracker URL entered is invalid.")); return; } - else if (new_tracker_url == tracker_url) + if (new_tracker_url == tracker_url) return; std::vector trackers = h.trackers();