Use new libtorrent-1.1 settings API

This commit is contained in:
Vladimir Golovnev (Glassez) 2016-06-03 17:03:17 +03:00
parent b2cb473b63
commit b5fe749f0b
12 changed files with 536 additions and 286 deletions

View file

@ -26,13 +26,17 @@
* exception statement from your version.
*/
#include "portforwarder.h"
#include <QDebug>
#include <libtorrent/session.hpp>
#include <libtorrent/version.hpp>
#include "base/logger.h"
#include "base/preferences.h"
#include "portforwarder.h"
#include "base/settingsstorage.h"
const QString KEY_ENABLED = QLatin1String("Network/PortForwardingEnabled");
namespace libt = libtorrent;
using namespace Net;
@ -42,8 +46,8 @@ PortForwarder::PortForwarder(libtorrent::session *provider, QObject *parent)
, m_active(false)
, m_provider(provider)
{
configure();
connect(Preferences::instance(), SIGNAL(changed()), SLOT(configure()));
if (SettingsStorage::instance()->loadValue(KEY_ENABLED, true).toBool())
start();
}
PortForwarder::~PortForwarder()
@ -70,6 +74,21 @@ PortForwarder *PortForwarder::instance()
return m_instance;
}
bool PortForwarder::isEnabled() const
{
return m_active;
}
void PortForwarder::setEnabled(bool enabled)
{
if (m_active != enabled) {
if (enabled)
start();
else
stop();
}
}
void PortForwarder::addPort(qint16 port)
{
if (!m_mappedPorts.contains(port)) {
@ -88,22 +107,18 @@ void PortForwarder::deletePort(qint16 port)
}
}
void PortForwarder::configure()
{
bool enable = Preferences::instance()->isUPnPEnabled();
if (m_active != enable) {
if (enable)
start();
else
stop();
}
}
void PortForwarder::start()
{
qDebug("Enabling UPnP / NAT-PMP");
#if LIBTORRENT_VERSION_NUM < 10100
m_provider->start_upnp();
m_provider->start_natpmp();
#else
libt::settings_pack settingsPack = m_provider->get_settings();
settingsPack.set_bool(libt::settings_pack::enable_upnp, true);
settingsPack.set_bool(libt::settings_pack::enable_natpmp, true);
m_provider->apply_settings(settingsPack);
#endif
foreach (qint16 port, m_mappedPorts.keys())
m_mappedPorts[port] = m_provider->add_port_mapping(libt::session::tcp, port, port);
m_active = true;
@ -113,8 +128,15 @@ void PortForwarder::start()
void PortForwarder::stop()
{
qDebug("Disabling UPnP / NAT-PMP");
#if LIBTORRENT_VERSION_NUM < 10100
m_provider->stop_upnp();
m_provider->stop_natpmp();
#else
libt::settings_pack settingsPack = m_provider->get_settings();
settingsPack.set_bool(libt::settings_pack::enable_upnp, false);
settingsPack.set_bool(libt::settings_pack::enable_natpmp, false);
m_provider->apply_settings(settingsPack);
#endif
m_active = false;
Logger::instance()->addMessage(tr("UPnP / NAT-PMP support [OFF]"), Log::INFO);
}