From 17f5ffcaec07975bb15893363fd8262cbc7be3c6 Mon Sep 17 00:00:00 2001 From: Ivan Sorokin Date: Tue, 4 Nov 2014 12:47:07 +0300 Subject: [PATCH 1/4] Split filterparserthread into .h and .cpp --- src/qtlibtorrent/filterparserthread.cpp | 389 ++++++++++++++++++++++++ src/qtlibtorrent/filterparserthread.h | 366 +--------------------- src/qtlibtorrent/qtlibtorrent.pri | 3 +- 3 files changed, 401 insertions(+), 357 deletions(-) create mode 100644 src/qtlibtorrent/filterparserthread.cpp diff --git a/src/qtlibtorrent/filterparserthread.cpp b/src/qtlibtorrent/filterparserthread.cpp new file mode 100644 index 000000000..84b9f3c3d --- /dev/null +++ b/src/qtlibtorrent/filterparserthread.cpp @@ -0,0 +1,389 @@ +/* + * 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 + */ + +#include "filterparserthread.h" + +FilterParserThread::FilterParserThread(QObject* parent, libtorrent::session *s) : QThread(parent), s(s), abort(false) { + +} + +FilterParserThread::~FilterParserThread() { + abort = true; + wait(); +} + +// Parser for eMule ip filter in DAT format +int FilterParserThread::parseDATFilterFile(QString filePath) { + int ruleCount = 0; + QFile file(filePath); + if (file.exists()) { + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + std::cerr << "I/O Error: Could not open ip filer file in read mode." << std::endl; + return ruleCount; + } + unsigned int nbLine = 0; + while (!file.atEnd() && !abort) { + ++nbLine; + QByteArray line = file.readLine(); + // Ignoring empty lines + line = line.trimmed(); + if (line.isEmpty()) continue; + // Ignoring commented lines + if (line.startsWith('#') || line.startsWith("//")) continue; + + // Line should be splitted by commas + QList partsList = line.split(','); + const uint nbElem = partsList.size(); + + // IP Range should be splitted by a dash + QList IPs = partsList.first().split('-'); + if (IPs.size() != 2) { + qDebug("Ipfilter.dat: line %d is malformed.", nbLine); + qDebug("Line was %s", line.constData()); + continue; + } + + boost::system::error_code ec; + const QString strStartIP = cleanupIPAddress(IPs.at(0)); + if (strStartIP.isEmpty()) { + qDebug("Ipfilter.dat: line %d is malformed.", nbLine); + qDebug("Start IP of the range is malformated: %s", qPrintable(strStartIP)); + continue; + } + libtorrent::address startAddr = libtorrent::address::from_string(qPrintable(strStartIP), ec); + if (ec) { + qDebug("Ipfilter.dat: line %d is malformed.", nbLine); + qDebug("Start IP of the range is malformated: %s", qPrintable(strStartIP)); + continue; + } + const QString strEndIP = cleanupIPAddress(IPs.at(1)); + if (strEndIP.isEmpty()) { + qDebug("Ipfilter.dat: line %d is malformed.", nbLine); + qDebug("End IP of the range is malformated: %s", qPrintable(strEndIP)); + continue; + } + libtorrent::address endAddr = libtorrent::address::from_string(qPrintable(strEndIP), ec); + if (ec) { + qDebug("Ipfilter.dat: line %d is malformed.", nbLine); + qDebug("End IP of the range is malformated: %s", qPrintable(strEndIP)); + continue; + } + if (startAddr.is_v4() != endAddr.is_v4()) { + qDebug("Ipfilter.dat: line %d is malformed.", nbLine); + qDebug("One IP is IPv4 and the other is IPv6!"); + continue; + } + + // Check if there is an access value (apparently not mandatory) + int nbAccess = 0; + if (nbElem > 1) { + // There is possibly one + nbAccess = partsList.at(1).trimmed().toInt(); + } + + if (nbAccess > 127) { + // Ignoring this rule because access value is too high + continue; + } + // Now Add to the filter + try { + filter.add_rule(startAddr, endAddr, libtorrent::ip_filter::blocked); + ++ruleCount; + }catch(exception) { + qDebug("Bad line in filter file, avoided crash..."); + } + } + file.close(); + } + return ruleCount; +} + +// Parser for PeerGuardian ip filter in p2p format +int FilterParserThread::parseP2PFilterFile(QString filePath) { + int ruleCount = 0; + QFile file(filePath); + if (file.exists()) { + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + std::cerr << "I/O Error: Could not open ip filer file in read mode." << std::endl; + return ruleCount; + } + unsigned int nbLine = 0; + while (!file.atEnd() && !abort) { + ++nbLine; + QByteArray line = file.readLine().trimmed(); + if (line.isEmpty()) continue; + // Ignoring commented lines + if (line.startsWith('#') || line.startsWith("//")) continue; + // Line is splitted by : + QList partsList = line.split(':'); + if (partsList.size() < 2) { + qDebug("p2p file: line %d is malformed.", nbLine); + continue; + } + // Get IP range + QList IPs = partsList.last().split('-'); + if (IPs.size() != 2) { + qDebug("p2p file: line %d is malformed.", nbLine); + qDebug("line was: %s", line.constData()); + continue; + } + boost::system::error_code ec; + QString strStartIP = cleanupIPAddress(IPs.at(0)); + if (strStartIP.isEmpty()) { + qDebug("p2p file: line %d is malformed.", nbLine); + qDebug("Start IP is invalid: %s", qPrintable(strStartIP)); + continue; + } + libtorrent::address startAddr = libtorrent::address::from_string(qPrintable(strStartIP), ec); + if (ec) { + qDebug("p2p file: line %d is malformed.", nbLine); + qDebug("Start IP is invalid: %s", qPrintable(strStartIP)); + continue; + } + QString strEndIP = cleanupIPAddress(IPs.at(1)); + if (strEndIP.isEmpty()) { + qDebug("p2p file: line %d is malformed.", nbLine); + qDebug("End IP is invalid: %s", qPrintable(strStartIP)); + continue; + } + libtorrent::address endAddr = libtorrent::address::from_string(qPrintable(strEndIP), ec); + if (ec) { + qDebug("p2p file: line %d is malformed.", nbLine); + qDebug("End IP is invalid: %s", qPrintable(strStartIP)); + continue; + } + if (startAddr.is_v4() != endAddr.is_v4()) { + qDebug("p2p file: line %d is malformed.", nbLine); + qDebug("Line was: %s", line.constData()); + continue; + } + try { + filter.add_rule(startAddr, endAddr, libtorrent::ip_filter::blocked); + ++ruleCount; + } catch(std::exception&) { + qDebug("p2p file: line %d is malformed.", nbLine); + qDebug("Line was: %s", line.constData()); + continue; + } + } + file.close(); + } + return ruleCount; +} + +int FilterParserThread::getlineInStream(QDataStream& stream, string& name, char delim) { + char c; + int total_read = 0; + int read; + do { + read = stream.readRawData(&c, 1); + total_read += read; + if (read > 0) { + if (c != delim) { + name += c; + } else { + // Delim found + return total_read; + } + } + } while(read > 0); + return total_read; +} + +// Parser for PeerGuardian ip filter in p2p format +int FilterParserThread::parseP2BFilterFile(QString filePath) { + int ruleCount = 0; + QFile file(filePath); + if (file.exists()) { + if (!file.open(QIODevice::ReadOnly)) { + std::cerr << "I/O Error: Could not open ip filer file in read mode." << std::endl; + return ruleCount; + } + QDataStream stream(&file); + // Read header + char buf[7]; + unsigned char version; + if ( + !stream.readRawData(buf, sizeof(buf)) || + memcmp(buf, "\xFF\xFF\xFF\xFFP2B", 7) || + !stream.readRawData((char*)&version, sizeof(version)) + ) { + std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl; + return ruleCount; + } + + if (version==1 || version==2) { + qDebug ("p2b version 1 or 2"); + unsigned int start, end; + + string name; + while(getlineInStream(stream, name, '\0') && !abort) { + if ( + !stream.readRawData((char*)&start, sizeof(start)) || + !stream.readRawData((char*)&end, sizeof(end)) + ) { + std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl; + return ruleCount; + } + // Network byte order to Host byte order + // asio address_v4 contructor expects it + // that way + libtorrent::address_v4 first(ntohl(start)); + libtorrent::address_v4 last(ntohl(end)); + // Apply to bittorrent session + try { + filter.add_rule(first, last, libtorrent::ip_filter::blocked); + ++ruleCount; + } catch(std::exception&) {} + } + } + else if (version==3) { + qDebug ("p2b version 3"); + unsigned int namecount; + if (!stream.readRawData((char*)&namecount, sizeof(namecount))) { + std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl; + return ruleCount; + } + namecount=ntohl(namecount); + // Reading names although, we don't really care about them + for (unsigned int i=0; iget_ip_filter(); + if (isRunning()) { + // Already parsing a filter, abort first + abort = true; + wait(); + } + abort = false; + filePath = _filePath; + // Run it + start(); +} + +void FilterParserThread::processFilterList(libtorrent::session *s, const QStringList& IPs) { + // First, import current filter + libtorrent::ip_filter filter = s->get_ip_filter(); + foreach (const QString &ip, IPs) { + qDebug("Manual ban of peer %s", ip.toLocal8Bit().constData()); + boost::system::error_code ec; + libtorrent::address addr = libtorrent::address::from_string(ip.toLocal8Bit().constData(), ec); + Q_ASSERT(!ec); + if (!ec) + filter.add_rule(addr, addr, libtorrent::ip_filter::blocked); + } + s->set_ip_filter(filter); +} + +QString FilterParserThread::cleanupIPAddress(QString _ip) { + QHostAddress ip(_ip.trimmed()); + if (ip.isNull()) { + return QString(); + } + return ip.toString(); +} + +void FilterParserThread::run() { + qDebug("Processing filter file"); + int ruleCount = 0; + if (filePath.endsWith(".p2p", Qt::CaseInsensitive)) { + // PeerGuardian p2p file + ruleCount = parseP2PFilterFile(filePath); + } else { + if (filePath.endsWith(".p2b", Qt::CaseInsensitive)) { + // PeerGuardian p2b file + ruleCount = parseP2BFilterFile(filePath); + } else { + // Default: eMule DAT format + ruleCount = parseDATFilterFile(filePath); + } + } + if (abort) + return; + try { + s->set_ip_filter(filter); + emit IPFilterParsed(ruleCount); + } catch(std::exception&) { + emit IPFilterError(); + } + qDebug("IP Filter thread: finished parsing, filter applied"); +} diff --git a/src/qtlibtorrent/filterparserthread.h b/src/qtlibtorrent/filterparserthread.h index 9ef911a9f..1991182a1 100644 --- a/src/qtlibtorrent/filterparserthread.h +++ b/src/qtlibtorrent/filterparserthread.h @@ -55,375 +55,29 @@ class FilterParserThread : public QThread { Q_OBJECT public: - FilterParserThread(QObject* parent, libtorrent::session *s) : QThread(parent), s(s), abort(false) { + FilterParserThread(QObject* parent, libtorrent::session *s); + ~FilterParserThread(); - } - - ~FilterParserThread() { - abort = true; - wait(); - } - - // Parser for eMule ip filter in DAT format - int parseDATFilterFile(QString filePath) { - int ruleCount = 0; - QFile file(filePath); - if (file.exists()) { - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { - std::cerr << "I/O Error: Could not open ip filer file in read mode." << std::endl; - return ruleCount; - } - unsigned int nbLine = 0; - while (!file.atEnd() && !abort) { - ++nbLine; - QByteArray line = file.readLine(); - // Ignoring empty lines - line = line.trimmed(); - if (line.isEmpty()) continue; - // Ignoring commented lines - if (line.startsWith('#') || line.startsWith("//")) continue; - - // Line should be splitted by commas - QList partsList = line.split(','); - const uint nbElem = partsList.size(); - - // IP Range should be splitted by a dash - QList IPs = partsList.first().split('-'); - if (IPs.size() != 2) { - qDebug("Ipfilter.dat: line %d is malformed.", nbLine); - qDebug("Line was %s", line.constData()); - continue; - } - - boost::system::error_code ec; - const QString strStartIP = cleanupIPAddress(IPs.at(0)); - if (strStartIP.isEmpty()) { - qDebug("Ipfilter.dat: line %d is malformed.", nbLine); - qDebug("Start IP of the range is malformated: %s", qPrintable(strStartIP)); - continue; - } - libtorrent::address startAddr = libtorrent::address::from_string(qPrintable(strStartIP), ec); - if (ec) { - qDebug("Ipfilter.dat: line %d is malformed.", nbLine); - qDebug("Start IP of the range is malformated: %s", qPrintable(strStartIP)); - continue; - } - const QString strEndIP = cleanupIPAddress(IPs.at(1)); - if (strEndIP.isEmpty()) { - qDebug("Ipfilter.dat: line %d is malformed.", nbLine); - qDebug("End IP of the range is malformated: %s", qPrintable(strEndIP)); - continue; - } - libtorrent::address endAddr = libtorrent::address::from_string(qPrintable(strEndIP), ec); - if (ec) { - qDebug("Ipfilter.dat: line %d is malformed.", nbLine); - qDebug("End IP of the range is malformated: %s", qPrintable(strEndIP)); - continue; - } - if (startAddr.is_v4() != endAddr.is_v4()) { - qDebug("Ipfilter.dat: line %d is malformed.", nbLine); - qDebug("One IP is IPv4 and the other is IPv6!"); - continue; - } - - // Check if there is an access value (apparently not mandatory) - int nbAccess = 0; - if (nbElem > 1) { - // There is possibly one - nbAccess = partsList.at(1).trimmed().toInt(); - } - - if (nbAccess > 127) { - // Ignoring this rule because access value is too high - continue; - } - // Now Add to the filter - try { - filter.add_rule(startAddr, endAddr, libtorrent::ip_filter::blocked); - ++ruleCount; - }catch(exception) { - qDebug("Bad line in filter file, avoided crash..."); - } - } - file.close(); - } - return ruleCount; - } - - // Parser for PeerGuardian ip filter in p2p format - int parseP2PFilterFile(QString filePath) { - int ruleCount = 0; - QFile file(filePath); - if (file.exists()) { - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { - std::cerr << "I/O Error: Could not open ip filer file in read mode." << std::endl; - return ruleCount; - } - unsigned int nbLine = 0; - while (!file.atEnd() && !abort) { - ++nbLine; - QByteArray line = file.readLine().trimmed(); - if (line.isEmpty()) continue; - // Ignoring commented lines - if (line.startsWith('#') || line.startsWith("//")) continue; - // Line is splitted by : - QList partsList = line.split(':'); - if (partsList.size() < 2) { - qDebug("p2p file: line %d is malformed.", nbLine); - continue; - } - // Get IP range - QList IPs = partsList.last().split('-'); - if (IPs.size() != 2) { - qDebug("p2p file: line %d is malformed.", nbLine); - qDebug("line was: %s", line.constData()); - continue; - } - boost::system::error_code ec; - QString strStartIP = cleanupIPAddress(IPs.at(0)); - if (strStartIP.isEmpty()) { - qDebug("p2p file: line %d is malformed.", nbLine); - qDebug("Start IP is invalid: %s", qPrintable(strStartIP)); - continue; - } - libtorrent::address startAddr = libtorrent::address::from_string(qPrintable(strStartIP), ec); - if (ec) { - qDebug("p2p file: line %d is malformed.", nbLine); - qDebug("Start IP is invalid: %s", qPrintable(strStartIP)); - continue; - } - QString strEndIP = cleanupIPAddress(IPs.at(1)); - if (strEndIP.isEmpty()) { - qDebug("p2p file: line %d is malformed.", nbLine); - qDebug("End IP is invalid: %s", qPrintable(strStartIP)); - continue; - } - libtorrent::address endAddr = libtorrent::address::from_string(qPrintable(strEndIP), ec); - if (ec) { - qDebug("p2p file: line %d is malformed.", nbLine); - qDebug("End IP is invalid: %s", qPrintable(strStartIP)); - continue; - } - if (startAddr.is_v4() != endAddr.is_v4()) { - qDebug("p2p file: line %d is malformed.", nbLine); - qDebug("Line was: %s", line.constData()); - continue; - } - try { - filter.add_rule(startAddr, endAddr, libtorrent::ip_filter::blocked); - ++ruleCount; - } catch(std::exception&) { - qDebug("p2p file: line %d is malformed.", nbLine); - qDebug("Line was: %s", line.constData()); - continue; - } - } - file.close(); - } - return ruleCount; - } - - int getlineInStream(QDataStream& stream, string& name, char delim) { - char c; - int total_read = 0; - int read; - do { - read = stream.readRawData(&c, 1); - total_read += read; - if (read > 0) { - if (c != delim) { - name += c; - } else { - // Delim found - return total_read; - } - } - } while(read > 0); - return total_read; - } - - // Parser for PeerGuardian ip filter in p2p format - int parseP2BFilterFile(QString filePath) { - int ruleCount = 0; - QFile file(filePath); - if (file.exists()) { - if (!file.open(QIODevice::ReadOnly)) { - std::cerr << "I/O Error: Could not open ip filer file in read mode." << std::endl; - return ruleCount; - } - QDataStream stream(&file); - // Read header - char buf[7]; - unsigned char version; - if ( - !stream.readRawData(buf, sizeof(buf)) || - memcmp(buf, "\xFF\xFF\xFF\xFFP2B", 7) || - !stream.readRawData((char*)&version, sizeof(version)) - ) { - std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl; - return ruleCount; - } - - if (version==1 || version==2) { - qDebug ("p2b version 1 or 2"); - unsigned int start, end; - - string name; - while(getlineInStream(stream, name, '\0') && !abort) { - if ( - !stream.readRawData((char*)&start, sizeof(start)) || - !stream.readRawData((char*)&end, sizeof(end)) - ) { - std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl; - return ruleCount; - } - // Network byte order to Host byte order - // asio address_v4 contructor expects it - // that way - libtorrent::address_v4 first(ntohl(start)); - libtorrent::address_v4 last(ntohl(end)); - // Apply to bittorrent session - try { - filter.add_rule(first, last, libtorrent::ip_filter::blocked); - ++ruleCount; - } catch(std::exception&) {} - } - } - else if (version==3) { - qDebug ("p2b version 3"); - unsigned int namecount; - if (!stream.readRawData((char*)&namecount, sizeof(namecount))) { - std::cerr << "Parsing Error: The filter file is not a valid PeerGuardian P2B file." << std::endl; - return ruleCount; - } - namecount=ntohl(namecount); - // Reading names although, we don't really care about them - for (unsigned int i=0; iget_ip_filter(); - if (isRunning()) { - // Already parsing a filter, abort first - abort = true; - wait(); - } - abort = false; - filePath = _filePath; - // Run it - start(); - } - - static void processFilterList(libtorrent::session *s, const QStringList& IPs) { - // First, import current filter - libtorrent::ip_filter filter = s->get_ip_filter(); - foreach (const QString &ip, IPs) { - qDebug("Manual ban of peer %s", ip.toLocal8Bit().constData()); - boost::system::error_code ec; - libtorrent::address addr = libtorrent::address::from_string(ip.toLocal8Bit().constData(), ec); - Q_ASSERT(!ec); - if (!ec) - filter.add_rule(addr, addr, libtorrent::ip_filter::blocked); - } - s->set_ip_filter(filter); - } + int parseDATFilterFile(QString filePath); + int parseP2PFilterFile(QString filePath); + int getlineInStream(QDataStream& stream, string& name, char delim); + int parseP2BFilterFile(QString filePath); + void processFilterFile(QString _filePath); + static void processFilterList(libtorrent::session *s, const QStringList& IPs); signals: void IPFilterParsed(int ruleCount); void IPFilterError(); protected: - QString cleanupIPAddress(QString _ip) { - QHostAddress ip(_ip.trimmed()); - if (ip.isNull()) { - return QString(); - } - return ip.toString(); - } - - void run() { - qDebug("Processing filter file"); - int ruleCount = 0; - if (filePath.endsWith(".p2p", Qt::CaseInsensitive)) { - // PeerGuardian p2p file - ruleCount = parseP2PFilterFile(filePath); - } else { - if (filePath.endsWith(".p2b", Qt::CaseInsensitive)) { - // PeerGuardian p2b file - ruleCount = parseP2BFilterFile(filePath); - } else { - // Default: eMule DAT format - ruleCount = parseDATFilterFile(filePath); - } - } - if (abort) - return; - try { - s->set_ip_filter(filter); - emit IPFilterParsed(ruleCount); - } catch(std::exception&) { - emit IPFilterError(); - } - qDebug("IP Filter thread: finished parsing, filter applied"); - } + QString cleanupIPAddress(QString _ip); + void run(); private: libtorrent::session *s; libtorrent::ip_filter filter; bool abort; QString filePath; - }; #endif diff --git a/src/qtlibtorrent/qtlibtorrent.pri b/src/qtlibtorrent/qtlibtorrent.pri index 72f16b04a..25acd8f73 100644 --- a/src/qtlibtorrent/qtlibtorrent.pri +++ b/src/qtlibtorrent/qtlibtorrent.pri @@ -13,7 +13,8 @@ SOURCES += $$PWD/qbtsession.cpp \ $$PWD/qtorrenthandle.cpp \ $$PWD/torrentspeedmonitor.cpp \ $$PWD/alertdispatcher.cpp \ - $$PWD/torrentstatistics.cpp + $$PWD/torrentstatistics.cpp \ + $$PWD/filterparserthread.cpp !contains(DEFINES, DISABLE_GUI) { HEADERS += $$PWD/torrentmodel.h \ From 6347700ee37989c9cf126864c490314978a4a597 Mon Sep 17 00:00:00 2001 From: Ivan Sorokin Date: Tue, 4 Nov 2014 13:00:42 +0300 Subject: [PATCH 2/4] Move libtorrent includes to .cpp This commit probably fixes #2119. The only important change in this commit is moving session::get_ip_filter() from FilterParserThread::processFilterFile() to FilterParserThread::run(). Previously we called it in main thread, but now we calls it in worker thread. I don't now what libtorrent contract about threads, but I assume that if it is ok to set_ip_filter from other thread, it is ok to get it. --- src/qtlibtorrent/filterparserthread.cpp | 21 +++++++++++++-------- src/qtlibtorrent/filterparserthread.h | 15 +++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/qtlibtorrent/filterparserthread.cpp b/src/qtlibtorrent/filterparserthread.cpp index 84b9f3c3d..ea9a8dc6d 100644 --- a/src/qtlibtorrent/filterparserthread.cpp +++ b/src/qtlibtorrent/filterparserthread.cpp @@ -30,6 +30,12 @@ #include "filterparserthread.h" +#include +#include + +#include +#include + FilterParserThread::FilterParserThread(QObject* parent, libtorrent::session *s) : QThread(parent), s(s), abort(false) { } @@ -40,7 +46,7 @@ FilterParserThread::~FilterParserThread() { } // Parser for eMule ip filter in DAT format -int FilterParserThread::parseDATFilterFile(QString filePath) { +int FilterParserThread::parseDATFilterFile(QString filePath, libtorrent::ip_filter& filter) { int ruleCount = 0; QFile file(filePath); if (file.exists()) { @@ -126,7 +132,7 @@ int FilterParserThread::parseDATFilterFile(QString filePath) { } // Parser for PeerGuardian ip filter in p2p format -int FilterParserThread::parseP2PFilterFile(QString filePath) { +int FilterParserThread::parseP2PFilterFile(QString filePath, libtorrent::ip_filter& filter) { int ruleCount = 0; QFile file(filePath); if (file.exists()) { @@ -218,7 +224,7 @@ int FilterParserThread::getlineInStream(QDataStream& stream, string& name, char } // Parser for PeerGuardian ip filter in p2p format -int FilterParserThread::parseP2BFilterFile(QString filePath) { +int FilterParserThread::parseP2BFilterFile(QString filePath, libtorrent::ip_filter& filter) { int ruleCount = 0; QFile file(filePath); if (file.exists()) { @@ -327,8 +333,6 @@ int FilterParserThread::parseP2BFilterFile(QString filePath) { // * PeerGuardian Text (P2P): http://wiki.phoenixlabs.org/wiki/P2P_Format // * PeerGuardian Binary (P2B): http://wiki.phoenixlabs.org/wiki/P2B_Format void FilterParserThread::processFilterFile(QString _filePath) { - // First, import current filter - filter = s->get_ip_filter(); if (isRunning()) { // Already parsing a filter, abort first abort = true; @@ -364,17 +368,18 @@ QString FilterParserThread::cleanupIPAddress(QString _ip) { void FilterParserThread::run() { qDebug("Processing filter file"); + libtorrent::ip_filter filter = s->get_ip_filter(); int ruleCount = 0; if (filePath.endsWith(".p2p", Qt::CaseInsensitive)) { // PeerGuardian p2p file - ruleCount = parseP2PFilterFile(filePath); + ruleCount = parseP2PFilterFile(filePath, filter); } else { if (filePath.endsWith(".p2b", Qt::CaseInsensitive)) { // PeerGuardian p2b file - ruleCount = parseP2BFilterFile(filePath); + ruleCount = parseP2BFilterFile(filePath, filter); } else { // Default: eMule DAT format - ruleCount = parseDATFilterFile(filePath); + ruleCount = parseDATFilterFile(filePath, filter); } } if (abort) diff --git a/src/qtlibtorrent/filterparserthread.h b/src/qtlibtorrent/filterparserthread.h index 1991182a1..7d41acbf5 100644 --- a/src/qtlibtorrent/filterparserthread.h +++ b/src/qtlibtorrent/filterparserthread.h @@ -32,13 +32,13 @@ #define FILTERPARSERTHREAD_H #include -#include #include #include -#include -#include -#include +namespace libtorrent { + class session; + struct ip_filter; +} using namespace std; @@ -58,10 +58,10 @@ public: FilterParserThread(QObject* parent, libtorrent::session *s); ~FilterParserThread(); - int parseDATFilterFile(QString filePath); - int parseP2PFilterFile(QString filePath); + int parseDATFilterFile(QString filePath, libtorrent::ip_filter& filter); + int parseP2PFilterFile(QString filePath, libtorrent::ip_filter& filter); int getlineInStream(QDataStream& stream, string& name, char delim); - int parseP2BFilterFile(QString filePath); + int parseP2BFilterFile(QString filePath, libtorrent::ip_filter& filter); void processFilterFile(QString _filePath); static void processFilterList(libtorrent::session *s, const QStringList& IPs); @@ -75,7 +75,6 @@ protected: private: libtorrent::session *s; - libtorrent::ip_filter filter; bool abort; QString filePath; }; From ed197a193d59d90640cea974981b349dfd87aca3 Mon Sep 17 00:00:00 2001 From: Ivan Sorokin Date: Wed, 5 Nov 2014 02:54:06 +0300 Subject: [PATCH 3/4] Split statusbar into .h and .cpp --- src/src.pro | 3 +- src/statusbar.cpp | 250 ++++++++++++++++++++++++++++++++++++++++++++++ src/statusbar.h | 222 ++-------------------------------------- 3 files changed, 262 insertions(+), 213 deletions(-) create mode 100644 src/statusbar.cpp diff --git a/src/src.pro b/src/src.pro index 37c9d3fc1..a6d960373 100644 --- a/src/src.pro +++ b/src/src.pro @@ -178,7 +178,8 @@ nox { autoexpandabledialog.cpp \ statsdialog.cpp \ messageboxraised.cpp \ - statussortfilterproxymodel.cpp + statussortfilterproxymodel.cpp \ + statusbar.cpp win32 { HEADERS += programupdater.h diff --git a/src/statusbar.cpp b/src/statusbar.cpp new file mode 100644 index 000000000..408f2a2bd --- /dev/null +++ b/src/statusbar.cpp @@ -0,0 +1,250 @@ +/* + * 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 + */ + +#include "statusbar.h" + +#include +#include + +#include "qbtsession.h" +#include "speedlimitdlg.h" +#include "iconprovider.h" +#include "preferences.h" +#include "misc.h" + +#include +#include + +StatusBar::StatusBar(QStatusBar *bar) + : m_bar(bar) +{ + Preferences* const pref = Preferences::instance(); + connect(QBtSession::instance(), SIGNAL(alternativeSpeedsModeChanged(bool)), this, SLOT(updateAltSpeedsBtn(bool))); + container = new QWidget(bar); + layout = new QHBoxLayout(container); + layout->setContentsMargins(0,0,0,0); + + container->setLayout(layout); + connecStatusLblIcon = new QPushButton(bar); + connecStatusLblIcon->setFlat(true); + connecStatusLblIcon->setFocusPolicy(Qt::NoFocus); + connecStatusLblIcon->setFixedWidth(32); + connecStatusLblIcon->setCursor(Qt::PointingHandCursor); + connecStatusLblIcon->setIcon(QIcon(":/Icons/skin/firewalled.png")); + connecStatusLblIcon->setToolTip(QString::fromUtf8("")+tr("Connection status:")+QString::fromUtf8("
")+QString::fromUtf8("")+tr("No direct connections. This may indicate network configuration problems.")+QString::fromUtf8("")); + dlSpeedLbl = new QPushButton(bar); + dlSpeedLbl->setIconSize(QSize(16,16)); + dlSpeedLbl->setIcon(QIcon(":/Icons/skin/download.png")); + //dlSpeedLbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred); + connect(dlSpeedLbl, SIGNAL(clicked()), this, SLOT(capDownloadSpeed())); + dlSpeedLbl->setFlat(true); + dlSpeedLbl->setFocusPolicy(Qt::NoFocus); + dlSpeedLbl->setCursor(Qt::PointingHandCursor); + + altSpeedsBtn = new QPushButton(bar); + altSpeedsBtn->setFixedWidth(36); + altSpeedsBtn->setIconSize(QSize(32,32)); + altSpeedsBtn->setFlat(true); + altSpeedsBtn->setFocusPolicy(Qt::NoFocus); + altSpeedsBtn->setCursor(Qt::PointingHandCursor); + updateAltSpeedsBtn(pref->isAltBandwidthEnabled()); + + connect(altSpeedsBtn, SIGNAL(clicked()), this, SLOT(toggleAlternativeSpeeds())); + + upSpeedLbl = new QPushButton(bar); + upSpeedLbl->setIconSize(QSize(16,16)); + upSpeedLbl->setIcon(QIcon(":/Icons/skin/seeding.png")); + //upSpeedLbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred); + connect(upSpeedLbl, SIGNAL(clicked()), this, SLOT(capUploadSpeed())); + upSpeedLbl->setFlat(true); + upSpeedLbl->setFocusPolicy(Qt::NoFocus); + upSpeedLbl->setCursor(Qt::PointingHandCursor); + DHTLbl = new QLabel(tr("DHT: %1 nodes").arg(0), bar); + DHTLbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred); + statusSep1 = new QFrame(bar); + statusSep1->setFixedSize(3, dlSpeedLbl->fontMetrics().height()); + statusSep1->setFrameStyle(QFrame::VLine); + statusSep1->setFrameShadow(QFrame::Raised); + statusSep2 = new QFrame(bar); + statusSep2->setFixedSize(3, dlSpeedLbl->fontMetrics().height()); + statusSep2->setFrameStyle(QFrame::VLine); + statusSep2->setFrameShadow(QFrame::Raised); + statusSep3 = new QFrame(bar); + statusSep3->setFixedSize(3, dlSpeedLbl->fontMetrics().height()); + statusSep3->setFrameStyle(QFrame::VLine); + statusSep3->setFrameShadow(QFrame::Raised); + statusSep4 = new QFrame(bar); + statusSep4->setFixedSize(3, dlSpeedLbl->fontMetrics().height()); + statusSep4->setFrameStyle(QFrame::VLine); + statusSep4->setFrameShadow(QFrame::Raised); + layout->addWidget(DHTLbl); + layout->addWidget(statusSep1); + layout->addWidget(connecStatusLblIcon); + layout->addWidget(statusSep2); + layout->addWidget(altSpeedsBtn); + layout->addWidget(statusSep4); + layout->addWidget(dlSpeedLbl); + layout->addWidget(statusSep3); + layout->addWidget(upSpeedLbl); + + bar->addPermanentWidget(container); + container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); + //bar->setStyleSheet("QWidget {padding-top: 0; padding-bottom: 0; margin-top: 0; margin-bottom: 0;}\n"); + container->setContentsMargins(0, 0, 0, 1); + bar->setContentsMargins(0, 0, 0, 0); + container->setFixedHeight(dlSpeedLbl->fontMetrics().height()+7); + bar->setFixedHeight(dlSpeedLbl->fontMetrics().height()+9); + // Is DHT enabled + DHTLbl->setVisible(pref->isDHTEnabled()); + refreshTimer = new QTimer(bar); + refreshStatusBar(); + connect(refreshTimer, SIGNAL(timeout()), this, SLOT(refreshStatusBar())); + refreshTimer->start(1500); +} + +StatusBar::~StatusBar() { + qDebug() << Q_FUNC_INFO; +} + +QPushButton* StatusBar::connectionStatusButton() const { + return connecStatusLblIcon; +} + +void StatusBar::showRestartRequired() { + // Restart required notification + const QString restart_text = tr("qBittorrent needs to be restarted"); + QLabel *restartIconLbl = new QLabel(m_bar); + restartIconLbl->setPixmap(QPixmap(":/Icons/oxygen/dialog-warning.png").scaled(QSize(24,24))); + restartIconLbl->setToolTip(restart_text); + m_bar->insertWidget(0,restartIconLbl); + QLabel *restartLbl = new QLabel(m_bar); + restartLbl->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred); + m_bar->insertWidget(1, restartLbl); + QFontMetrics fm(restartLbl->font()); + restartLbl->setText(fm.elidedText(restart_text, Qt::ElideRight, restartLbl->width())); + QBtSession::instance()->addConsoleMessage(tr("qBittorrent was just updated and needs to be restarted for the changes to be effective."), "red"); +} + +void StatusBar::stopTimer() { + refreshTimer->stop(); +} + +void StatusBar::refreshStatusBar() { + // Update connection status + const libtorrent::session_status sessionStatus = QBtSession::instance()->getSessionStatus(); + if (!QBtSession::instance()->getSession()->is_listening()) { + connecStatusLblIcon->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/disconnected.png"))); + connecStatusLblIcon->setToolTip(QString::fromUtf8("")+tr("Connection Status:")+QString::fromUtf8("
")+tr("Offline. This usually means that qBittorrent failed to listen on the selected port for incoming connections.")); + } else { + if (sessionStatus.has_incoming_connections) { + // Connection OK + connecStatusLblIcon->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/connected.png"))); + connecStatusLblIcon->setToolTip(QString::fromUtf8("")+tr("Connection Status:")+QString::fromUtf8("
")+tr("Online")); + }else{ + connecStatusLblIcon->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/firewalled.png"))); + connecStatusLblIcon->setToolTip(QString::fromUtf8("")+tr("Connection status:")+QString::fromUtf8("
")+QString::fromUtf8("")+tr("No direct connections. This may indicate network configuration problems.")+QString::fromUtf8("")); + } + } + // Update Number of DHT nodes + if (QBtSession::instance()->isDHTEnabled()) { + DHTLbl->setVisible(true); + //statusSep1->setVisible(true); + DHTLbl->setText(tr("DHT: %1 nodes").arg(QString::number(sessionStatus.dht_nodes))); + } else { + DHTLbl->setVisible(false); + //statusSep1->setVisible(false); + } + // Update speed labels + dlSpeedLbl->setText(tr("%1/s", "Per second").arg(misc::friendlyUnit(sessionStatus.payload_download_rate))+" ("+misc::friendlyUnit(sessionStatus.total_payload_download)+")"); + upSpeedLbl->setText(tr("%1/s", "Per second").arg(misc::friendlyUnit(sessionStatus.payload_upload_rate))+" ("+misc::friendlyUnit(sessionStatus.total_payload_upload)+")"); +} + +void StatusBar::updateAltSpeedsBtn(bool alternative) { + if (alternative) { + altSpeedsBtn->setIcon(QIcon(":/Icons/slow.png")); + altSpeedsBtn->setToolTip(tr("Click to switch to regular speed limits")); + altSpeedsBtn->setDown(true); + } else { + altSpeedsBtn->setIcon(QIcon(":/Icons/slow_off.png")); + altSpeedsBtn->setToolTip(tr("Click to switch to alternative speed limits")); + altSpeedsBtn->setDown(false); + } +} + +void StatusBar::toggleAlternativeSpeeds() { + Preferences* const pref = Preferences::instance(); + if (pref->isSchedulerEnabled()) { + pref->setSchedulerEnabled(false); + m_bar->showMessage(tr("Manual change of rate limits mode. The scheduler is disabled."), 5000); + } + QBtSession::instance()->useAlternativeSpeedsLimit(!pref->isAltBandwidthEnabled()); +} + +void StatusBar::capDownloadSpeed() { + bool ok = false; + int cur_limit = QBtSession::instance()->getSession()->settings().download_rate_limit; + long new_limit = SpeedLimitDialog::askSpeedLimit(&ok, tr("Global Download Speed Limit"), cur_limit); + if (ok) { + Preferences* const pref = Preferences::instance(); + bool alt = pref->isAltBandwidthEnabled(); + if (new_limit <= 0) { + qDebug("Setting global download rate limit to Unlimited"); + QBtSession::instance()->setDownloadRateLimit(-1); + if (!alt) + pref->setGlobalDownloadLimit(-1); + } else { + qDebug("Setting global download rate limit to %.1fKb/s", new_limit/1024.); + QBtSession::instance()->setDownloadRateLimit(new_limit); + if (!alt) + pref->setGlobalDownloadLimit(new_limit/1024.); + } + } +} + +void StatusBar::capUploadSpeed() { + bool ok = false; + int cur_limit = QBtSession::instance()->getSession()->settings().upload_rate_limit; + long new_limit = SpeedLimitDialog::askSpeedLimit(&ok, tr("Global Upload Speed Limit"), cur_limit); + if (ok) { + Preferences* const pref = Preferences::instance(); + bool alt = pref->isAltBandwidthEnabled(); + if (new_limit <= 0) { + qDebug("Setting global upload rate limit to Unlimited"); + QBtSession::instance()->setUploadRateLimit(-1); + if (!alt) + Preferences::instance()->setGlobalUploadLimit(-1); + } else { + qDebug("Setting global upload rate limit to %.1fKb/s", new_limit/1024.); + QBtSession::instance()->setUploadRateLimit(new_limit); + if (!alt) + Preferences::instance()->setGlobalUploadLimit(new_limit/1024.); + } + } +} diff --git a/src/statusbar.h b/src/statusbar.h index 1870e87f3..d8587d6d8 100644 --- a/src/statusbar.h +++ b/src/statusbar.h @@ -38,226 +38,24 @@ #include #include #include -#include -#include -#include "qbtsession.h" -#include "speedlimitdlg.h" -#include "iconprovider.h" -#include "preferences.h" -#include "misc.h" - -#include -#include class StatusBar: public QObject { Q_OBJECT public: - StatusBar(QStatusBar *bar): m_bar(bar) { - Preferences* const pref = Preferences::instance(); - connect(QBtSession::instance(), SIGNAL(alternativeSpeedsModeChanged(bool)), this, SLOT(updateAltSpeedsBtn(bool))); - container = new QWidget(bar); - layout = new QHBoxLayout(container); - layout->setContentsMargins(0,0,0,0); - - container->setLayout(layout); - connecStatusLblIcon = new QPushButton(bar); - connecStatusLblIcon->setFlat(true); - connecStatusLblIcon->setFocusPolicy(Qt::NoFocus); - connecStatusLblIcon->setFixedWidth(32); - connecStatusLblIcon->setCursor(Qt::PointingHandCursor); - connecStatusLblIcon->setIcon(QIcon(":/Icons/skin/firewalled.png")); - connecStatusLblIcon->setToolTip(QString::fromUtf8("")+tr("Connection status:")+QString::fromUtf8("
")+QString::fromUtf8("")+tr("No direct connections. This may indicate network configuration problems.")+QString::fromUtf8("")); - dlSpeedLbl = new QPushButton(bar); - dlSpeedLbl->setIconSize(QSize(16,16)); - dlSpeedLbl->setIcon(QIcon(":/Icons/skin/download.png")); - //dlSpeedLbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred); - connect(dlSpeedLbl, SIGNAL(clicked()), this, SLOT(capDownloadSpeed())); - dlSpeedLbl->setFlat(true); - dlSpeedLbl->setFocusPolicy(Qt::NoFocus); - dlSpeedLbl->setCursor(Qt::PointingHandCursor); - - altSpeedsBtn = new QPushButton(bar); - altSpeedsBtn->setFixedWidth(36); - altSpeedsBtn->setIconSize(QSize(32,32)); - altSpeedsBtn->setFlat(true); - altSpeedsBtn->setFocusPolicy(Qt::NoFocus); - altSpeedsBtn->setCursor(Qt::PointingHandCursor); - updateAltSpeedsBtn(pref->isAltBandwidthEnabled()); - - connect(altSpeedsBtn, SIGNAL(clicked()), this, SLOT(toggleAlternativeSpeeds())); - - upSpeedLbl = new QPushButton(bar); - upSpeedLbl->setIconSize(QSize(16,16)); - upSpeedLbl->setIcon(QIcon(":/Icons/skin/seeding.png")); - //upSpeedLbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred); - connect(upSpeedLbl, SIGNAL(clicked()), this, SLOT(capUploadSpeed())); - upSpeedLbl->setFlat(true); - upSpeedLbl->setFocusPolicy(Qt::NoFocus); - upSpeedLbl->setCursor(Qt::PointingHandCursor); - DHTLbl = new QLabel(tr("DHT: %1 nodes").arg(0), bar); - DHTLbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred); - statusSep1 = new QFrame(bar); - statusSep1->setFixedSize(3, dlSpeedLbl->fontMetrics().height()); - statusSep1->setFrameStyle(QFrame::VLine); - statusSep1->setFrameShadow(QFrame::Raised); - statusSep2 = new QFrame(bar); - statusSep2->setFixedSize(3, dlSpeedLbl->fontMetrics().height()); - statusSep2->setFrameStyle(QFrame::VLine); - statusSep2->setFrameShadow(QFrame::Raised); - statusSep3 = new QFrame(bar); - statusSep3->setFixedSize(3, dlSpeedLbl->fontMetrics().height()); - statusSep3->setFrameStyle(QFrame::VLine); - statusSep3->setFrameShadow(QFrame::Raised); - statusSep4 = new QFrame(bar); - statusSep4->setFixedSize(3, dlSpeedLbl->fontMetrics().height()); - statusSep4->setFrameStyle(QFrame::VLine); - statusSep4->setFrameShadow(QFrame::Raised); - layout->addWidget(DHTLbl); - layout->addWidget(statusSep1); - layout->addWidget(connecStatusLblIcon); - layout->addWidget(statusSep2); - layout->addWidget(altSpeedsBtn); - layout->addWidget(statusSep4); - layout->addWidget(dlSpeedLbl); - layout->addWidget(statusSep3); - layout->addWidget(upSpeedLbl); - - bar->addPermanentWidget(container); - container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); - //bar->setStyleSheet("QWidget {padding-top: 0; padding-bottom: 0; margin-top: 0; margin-bottom: 0;}\n"); - container->setContentsMargins(0, 0, 0, 1); - bar->setContentsMargins(0, 0, 0, 0); - container->setFixedHeight(dlSpeedLbl->fontMetrics().height()+7); - bar->setFixedHeight(dlSpeedLbl->fontMetrics().height()+9); - // Is DHT enabled - DHTLbl->setVisible(pref->isDHTEnabled()); - refreshTimer = new QTimer(bar); - refreshStatusBar(); - connect(refreshTimer, SIGNAL(timeout()), this, SLOT(refreshStatusBar())); - refreshTimer->start(1500); - } - - ~StatusBar() { - qDebug() << Q_FUNC_INFO; - } + StatusBar(QStatusBar *bar); + ~StatusBar(); - QPushButton* connectionStatusButton() const { - return connecStatusLblIcon; - } + QPushButton* connectionStatusButton() const; public slots: - void showRestartRequired() { - // Restart required notification - const QString restart_text = tr("qBittorrent needs to be restarted"); - QLabel *restartIconLbl = new QLabel(m_bar); - restartIconLbl->setPixmap(QPixmap(":/Icons/oxygen/dialog-warning.png").scaled(QSize(24,24))); - restartIconLbl->setToolTip(restart_text); - m_bar->insertWidget(0,restartIconLbl); - QLabel *restartLbl = new QLabel(m_bar); - restartLbl->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred); - m_bar->insertWidget(1, restartLbl); - QFontMetrics fm(restartLbl->font()); - restartLbl->setText(fm.elidedText(restart_text, Qt::ElideRight, restartLbl->width())); - QBtSession::instance()->addConsoleMessage(tr("qBittorrent was just updated and needs to be restarted for the changes to be effective."), "red"); - } - - void stopTimer() { - refreshTimer->stop(); - } - - void refreshStatusBar() { - // Update connection status - const libtorrent::session_status sessionStatus = QBtSession::instance()->getSessionStatus(); - if (!QBtSession::instance()->getSession()->is_listening()) { - connecStatusLblIcon->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/disconnected.png"))); - connecStatusLblIcon->setToolTip(QString::fromUtf8("")+tr("Connection Status:")+QString::fromUtf8("
")+tr("Offline. This usually means that qBittorrent failed to listen on the selected port for incoming connections.")); - } else { - if (sessionStatus.has_incoming_connections) { - // Connection OK - connecStatusLblIcon->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/connected.png"))); - connecStatusLblIcon->setToolTip(QString::fromUtf8("")+tr("Connection Status:")+QString::fromUtf8("
")+tr("Online")); - }else{ - connecStatusLblIcon->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/firewalled.png"))); - connecStatusLblIcon->setToolTip(QString::fromUtf8("")+tr("Connection status:")+QString::fromUtf8("
")+QString::fromUtf8("")+tr("No direct connections. This may indicate network configuration problems.")+QString::fromUtf8("")); - } - } - // Update Number of DHT nodes - if (QBtSession::instance()->isDHTEnabled()) { - DHTLbl->setVisible(true); - //statusSep1->setVisible(true); - DHTLbl->setText(tr("DHT: %1 nodes").arg(QString::number(sessionStatus.dht_nodes))); - } else { - DHTLbl->setVisible(false); - //statusSep1->setVisible(false); - } - // Update speed labels - dlSpeedLbl->setText(tr("%1/s", "Per second").arg(misc::friendlyUnit(sessionStatus.payload_download_rate))+" ("+misc::friendlyUnit(sessionStatus.total_payload_download)+")"); - upSpeedLbl->setText(tr("%1/s", "Per second").arg(misc::friendlyUnit(sessionStatus.payload_upload_rate))+" ("+misc::friendlyUnit(sessionStatus.total_payload_upload)+")"); - } - - void updateAltSpeedsBtn(bool alternative) { - if (alternative) { - altSpeedsBtn->setIcon(QIcon(":/Icons/slow.png")); - altSpeedsBtn->setToolTip(tr("Click to switch to regular speed limits")); - altSpeedsBtn->setDown(true); - } else { - altSpeedsBtn->setIcon(QIcon(":/Icons/slow_off.png")); - altSpeedsBtn->setToolTip(tr("Click to switch to alternative speed limits")); - altSpeedsBtn->setDown(false); - } - } - - void toggleAlternativeSpeeds() { - Preferences* const pref = Preferences::instance(); - if (pref->isSchedulerEnabled()) { - pref->setSchedulerEnabled(false); - m_bar->showMessage(tr("Manual change of rate limits mode. The scheduler is disabled."), 5000); - } - QBtSession::instance()->useAlternativeSpeedsLimit(!pref->isAltBandwidthEnabled()); - } - - void capDownloadSpeed() { - bool ok = false; - int cur_limit = QBtSession::instance()->getSession()->settings().download_rate_limit; - long new_limit = SpeedLimitDialog::askSpeedLimit(&ok, tr("Global Download Speed Limit"), cur_limit); - if (ok) { - Preferences* const pref = Preferences::instance(); - bool alt = pref->isAltBandwidthEnabled(); - if (new_limit <= 0) { - qDebug("Setting global download rate limit to Unlimited"); - QBtSession::instance()->setDownloadRateLimit(-1); - if (!alt) - pref->setGlobalDownloadLimit(-1); - } else { - qDebug("Setting global download rate limit to %.1fKb/s", new_limit/1024.); - QBtSession::instance()->setDownloadRateLimit(new_limit); - if (!alt) - pref->setGlobalDownloadLimit(new_limit/1024.); - } - } - } - - void capUploadSpeed() { - bool ok = false; - int cur_limit = QBtSession::instance()->getSession()->settings().upload_rate_limit; - long new_limit = SpeedLimitDialog::askSpeedLimit(&ok, tr("Global Upload Speed Limit"), cur_limit); - if (ok) { - Preferences* const pref = Preferences::instance(); - bool alt = pref->isAltBandwidthEnabled(); - if (new_limit <= 0) { - qDebug("Setting global upload rate limit to Unlimited"); - QBtSession::instance()->setUploadRateLimit(-1); - if (!alt) - Preferences::instance()->setGlobalUploadLimit(-1); - } else { - qDebug("Setting global upload rate limit to %.1fKb/s", new_limit/1024.); - QBtSession::instance()->setUploadRateLimit(new_limit); - if (!alt) - Preferences::instance()->setGlobalUploadLimit(new_limit/1024.); - } - } - } + void showRestartRequired(); + void stopTimer(); + void refreshStatusBar(); + void updateAltSpeedsBtn(bool alternative); + void toggleAlternativeSpeeds(); + void capDownloadSpeed(); + void capUploadSpeed(); private: QStatusBar *m_bar; From da867fd022787893c763abd0a3aa6182b5bc30c7 Mon Sep 17 00:00:00 2001 From: Ivan Sorokin Date: Wed, 5 Nov 2014 03:05:21 +0300 Subject: [PATCH 4/4] Split trackerlogin into .h and .cpp --- src/mainwindow.cpp | 2 ++ src/src.pro | 3 ++- src/trackerlogin.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++ src/trackerlogin.h | 28 ++++------------------ 4 files changed, 65 insertions(+), 25 deletions(-) create mode 100644 src/trackerlogin.cpp diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 6be49ca86..c0e233816 100755 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -89,6 +89,8 @@ void qt_mac_set_dock_menu(QMenu *menu); #include "downloadthread.h" #endif +#include + using namespace libtorrent; #define TIME_TRAY_BALLOON 5000 diff --git a/src/src.pro b/src/src.pro index a6d960373..b00956a2e 100644 --- a/src/src.pro +++ b/src/src.pro @@ -179,7 +179,8 @@ nox { statsdialog.cpp \ messageboxraised.cpp \ statussortfilterproxymodel.cpp \ - statusbar.cpp + statusbar.cpp \ + trackerlogin.cpp win32 { HEADERS += programupdater.h diff --git a/src/trackerlogin.cpp b/src/trackerlogin.cpp new file mode 100644 index 000000000..d0ab212b7 --- /dev/null +++ b/src/trackerlogin.cpp @@ -0,0 +1,57 @@ +/* + * 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 + */ + +#include "trackerlogin.h" + +trackerLogin::trackerLogin(QWidget *parent, QTorrentHandle h) + : QDialog(parent) + , h(h) +{ + setupUi(this); + setAttribute(Qt::WA_DeleteOnClose); + login_logo->setPixmap(QPixmap(QString::fromUtf8(":/Icons/oxygen/encrypted.png"))); + tracker_url->setText(h.current_tracker()); + connect(this, SIGNAL(trackerLoginCancelled(QPair)), parent, SLOT(addUnauthenticatedTracker(QPair))); + show(); +} + +trackerLogin::~trackerLogin() {} + +void trackerLogin::on_loginButton_clicked() { + // login + h.set_tracker_login(lineUsername->text(), linePasswd->text()); + close(); +} + +void trackerLogin::on_cancelButton_clicked() { + // Emit a signal to GUI to stop asking for authentication + emit trackerLoginCancelled(QPair(h, h.current_tracker())); + close(); +} diff --git a/src/trackerlogin.h b/src/trackerlogin.h index 4d017a399..f9679308d 100644 --- a/src/trackerlogin.h +++ b/src/trackerlogin.h @@ -32,9 +32,6 @@ #define TRACKERLOGIN_H #include -#include - -#include #include "ui_login.h" #include "qtorrenthandle.h" @@ -46,32 +43,15 @@ class trackerLogin : public QDialog, private Ui::authentication{ QTorrentHandle h; public: - trackerLogin(QWidget *parent, QTorrentHandle h): QDialog(parent), h(h) { - setupUi(this); - setAttribute(Qt::WA_DeleteOnClose); - login_logo->setPixmap(QPixmap(QString::fromUtf8(":/Icons/oxygen/encrypted.png"))); - tracker_url->setText(h.current_tracker()); - connect(this, SIGNAL(trackerLoginCancelled(QPair)), parent, SLOT(addUnauthenticatedTracker(QPair))); - show(); - } - - ~trackerLogin() {} + trackerLogin(QWidget *parent, QTorrentHandle h); + ~trackerLogin(); signals: void trackerLoginCancelled(QPair tracker); public slots: - void on_loginButton_clicked() { - // login - h.set_tracker_login(lineUsername->text(), linePasswd->text()); - close(); - } - - void on_cancelButton_clicked() { - // Emit a signal to GUI to stop asking for authentication - emit trackerLoginCancelled(QPair(h, h.current_tracker())); - close(); - } + void on_loginButton_clicked(); + void on_cancelButton_clicked(); }; #endif