From ad6a40d5f4b48099c4a88a645e03f6ac17c35657 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Mon, 22 Jul 2019 19:50:42 +0300 Subject: [PATCH] Extract PeerAddress class into separate file --- src/base/CMakeLists.txt | 2 + src/base/base.pri | 2 + src/base/bittorrent/peeraddress.cpp | 58 ++++++++++++++++++++++ src/base/bittorrent/peeraddress.h | 44 ++++++++++++++++ src/base/bittorrent/peerinfo.cpp | 20 ++------ src/base/bittorrent/peerinfo.h | 10 +--- src/base/bittorrent/torrenthandle.cpp | 1 + src/gui/properties/peerlistwidget.cpp | 1 + src/gui/properties/peersadditiondialog.cpp | 29 +---------- src/gui/properties/peersadditiondialog.h | 2 - src/webui/api/synccontroller.cpp | 1 + 11 files changed, 115 insertions(+), 55 deletions(-) create mode 100644 src/base/bittorrent/peeraddress.cpp create mode 100644 src/base/bittorrent/peeraddress.h diff --git a/src/base/CMakeLists.txt b/src/base/CMakeLists.txt index 1a736ede2..f2fa92f2d 100644 --- a/src/base/CMakeLists.txt +++ b/src/base/CMakeLists.txt @@ -7,6 +7,7 @@ bittorrent/cachestatus.h bittorrent/downloadpriority.h bittorrent/infohash.h bittorrent/magneturi.h +bittorrent/peeraddress.h bittorrent/peerinfo.h bittorrent/private/bandwidthscheduler.h bittorrent/private/filterparserthread.h @@ -81,6 +82,7 @@ unicodestrings.h bittorrent/downloadpriority.cpp bittorrent/infohash.cpp bittorrent/magneturi.cpp +bittorrent/peeraddress.cpp bittorrent/peerinfo.cpp bittorrent/private/bandwidthscheduler.cpp bittorrent/private/filterparserthread.cpp diff --git a/src/base/base.pri b/src/base/base.pri index f5a0b025d..074e8d118 100644 --- a/src/base/base.pri +++ b/src/base/base.pri @@ -6,6 +6,7 @@ HEADERS += \ $$PWD/bittorrent/downloadpriority.h \ $$PWD/bittorrent/infohash.h \ $$PWD/bittorrent/magneturi.h \ + $$PWD/bittorrent/peeraddress.h \ $$PWD/bittorrent/peerinfo.h \ $$PWD/bittorrent/private/bandwidthscheduler.h \ $$PWD/bittorrent/private/filterparserthread.h \ @@ -80,6 +81,7 @@ SOURCES += \ $$PWD/bittorrent/downloadpriority.cpp \ $$PWD/bittorrent/infohash.cpp \ $$PWD/bittorrent/magneturi.cpp \ + $$PWD/bittorrent/peeraddress.cpp \ $$PWD/bittorrent/peerinfo.cpp \ $$PWD/bittorrent/private/bandwidthscheduler.cpp \ $$PWD/bittorrent/private/filterparserthread.cpp \ diff --git a/src/base/bittorrent/peeraddress.cpp b/src/base/bittorrent/peeraddress.cpp new file mode 100644 index 000000000..8c4207c60 --- /dev/null +++ b/src/base/bittorrent/peeraddress.cpp @@ -0,0 +1,58 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2019 Vladimir Golovnev + * + * 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 "peeraddress.h" + +#include +#include + +BitTorrent::PeerAddress BitTorrent::PeerAddress::parse(QString peerAddressStr) +{ + PeerAddress addr; + QStringList ipPort; + + if ((peerAddressStr[0] == '[') && (peerAddressStr.indexOf("]:") != -1)) // IPv6 + ipPort = peerAddressStr.remove(QChar('[')).split("]:"); + else if (peerAddressStr.indexOf(':') != -1) // IPv4 + ipPort = peerAddressStr.split(':'); + else + return addr; + + QHostAddress ip(ipPort[0]); + if (ip.isNull()) + return addr; + + bool ok; + int port = ipPort[1].toInt(&ok); + if (!ok || (port < 1) || (port > 65535)) + return addr; + + addr.ip = ip; + addr.port = port; + return addr; +} diff --git a/src/base/bittorrent/peeraddress.h b/src/base/bittorrent/peeraddress.h new file mode 100644 index 000000000..0a9450ba2 --- /dev/null +++ b/src/base/bittorrent/peeraddress.h @@ -0,0 +1,44 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2019 Vladimir Golovnev + * + * 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. + */ + +#pragma once + +#include + +class QString; + +namespace BitTorrent +{ + struct PeerAddress + { + QHostAddress ip; + ushort port = 0; + + static PeerAddress parse(QString peerAddressStr); + }; +} diff --git a/src/base/bittorrent/peerinfo.cpp b/src/base/bittorrent/peerinfo.cpp index 82f8982ff..1a1215ee9 100644 --- a/src/base/bittorrent/peerinfo.cpp +++ b/src/base/bittorrent/peerinfo.cpp @@ -34,24 +34,10 @@ #include "base/net/geoipmanager.h" #include "base/unicodestrings.h" #include "base/utils/string.h" +#include "peeraddress.h" using namespace BitTorrent; -// PeerAddress - -PeerAddress::PeerAddress() - : port(0) -{ -} - -PeerAddress::PeerAddress(const QHostAddress &ip, ushort port) - : ip(ip) - , port(port) -{ -} - -// PeerInfo - PeerInfo::PeerInfo(const TorrentHandle *torrent, const lt::peer_info &nativeInfo) : m_nativeInfo(nativeInfo) { @@ -183,8 +169,8 @@ bool PeerInfo::isPlaintextEncrypted() const PeerAddress PeerInfo::address() const { - return PeerAddress(QHostAddress(QString::fromStdString(m_nativeInfo.ip.address().to_string())), - m_nativeInfo.ip.port()); + return {QHostAddress(QString::fromStdString(m_nativeInfo.ip.address().to_string())) + , m_nativeInfo.ip.port()}; } QString PeerInfo::client() const diff --git a/src/base/bittorrent/peerinfo.h b/src/base/bittorrent/peerinfo.h index a0741743d..9e407a1d5 100644 --- a/src/base/bittorrent/peerinfo.h +++ b/src/base/bittorrent/peerinfo.h @@ -39,15 +39,7 @@ class QBitArray; namespace BitTorrent { class TorrentHandle; - - struct PeerAddress - { - QHostAddress ip; - ushort port; - - PeerAddress(); - PeerAddress(const QHostAddress &ip, ushort port); - }; + struct PeerAddress; class PeerInfo { diff --git a/src/base/bittorrent/torrenthandle.cpp b/src/base/bittorrent/torrenthandle.cpp index 006298cc5..5a3932356 100644 --- a/src/base/bittorrent/torrenthandle.cpp +++ b/src/base/bittorrent/torrenthandle.cpp @@ -65,6 +65,7 @@ #include "base/utils/fs.h" #include "base/utils/string.h" #include "downloadpriority.h" +#include "peeraddress.h" #include "peerinfo.h" #include "session.h" #include "trackerentry.h" diff --git a/src/gui/properties/peerlistwidget.cpp b/src/gui/properties/peerlistwidget.cpp index 81d78a2ba..d877a97a3 100644 --- a/src/gui/properties/peerlistwidget.cpp +++ b/src/gui/properties/peerlistwidget.cpp @@ -40,6 +40,7 @@ #include #include +#include "base/bittorrent/peeraddress.h" #include "base/bittorrent/peerinfo.h" #include "base/bittorrent/session.h" #include "base/bittorrent/torrenthandle.h" diff --git a/src/gui/properties/peersadditiondialog.cpp b/src/gui/properties/peersadditiondialog.cpp index c39016a5f..f3396129a 100644 --- a/src/gui/properties/peersadditiondialog.cpp +++ b/src/gui/properties/peersadditiondialog.cpp @@ -31,6 +31,7 @@ #include #include +#include "base/bittorrent/peeraddress.h" #include "base/global.h" #include "ui_peersadditiondialog.h" @@ -63,7 +64,7 @@ void PeersAdditionDialog::validateInput() return; } for (const QString &peer : asConst(m_ui->textEditPeers->toPlainText().trimmed().split('\n'))) { - BitTorrent::PeerAddress addr = parsePeer(peer); + const BitTorrent::PeerAddress addr = BitTorrent::PeerAddress::parse(peer); if (!addr.ip.isNull()) { m_peersList.append(addr); } @@ -77,29 +78,3 @@ void PeersAdditionDialog::validateInput() } accept(); } - -BitTorrent::PeerAddress PeersAdditionDialog::parsePeer(QString peer) -{ - BitTorrent::PeerAddress addr; - QStringList ipPort; - - if ((peer[0] == '[') && (peer.indexOf("]:") != -1)) // IPv6 - ipPort = peer.remove(QChar('[')).split("]:"); - else if (peer.indexOf(':') != -1) // IPv4 - ipPort = peer.split(':'); - else - return addr; - - QHostAddress ip(ipPort[0]); - if (ip.isNull()) - return addr; - - bool ok; - int port = ipPort[1].toInt(&ok); - if (!ok || (port < 1) || (port > 65535)) - return addr; - - addr.ip = ip; - addr.port = port; - return addr; -} diff --git a/src/gui/properties/peersadditiondialog.h b/src/gui/properties/peersadditiondialog.h index f583c34bb..ee8ddfc13 100644 --- a/src/gui/properties/peersadditiondialog.h +++ b/src/gui/properties/peersadditiondialog.h @@ -53,8 +53,6 @@ protected slots: void validateInput(); private: - BitTorrent::PeerAddress parsePeer(QString peer); - Ui::PeersAdditionDialog *m_ui; QList m_peersList; }; diff --git a/src/webui/api/synccontroller.cpp b/src/webui/api/synccontroller.cpp index 557cee639..dac4fb316 100644 --- a/src/webui/api/synccontroller.cpp +++ b/src/webui/api/synccontroller.cpp @@ -34,6 +34,7 @@ #include #include +#include "base/bittorrent/peeraddress.h" #include "base/bittorrent/peerinfo.h" #include "base/bittorrent/session.h" #include "base/bittorrent/torrenthandle.h"