Set metadata asynchronously
[qBittorrent.git] / src / base / bittorrent / portforwarderimpl.cpp
blob13088c8a25f8105000a6381dfe2f74f65b33cbd6
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2019-2022 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "portforwarderimpl.h"
31 #include <utility>
33 #include "base/bittorrent/sessionimpl.h"
35 PortForwarderImpl::PortForwarderImpl(BitTorrent::SessionImpl *provider, QObject *parent)
36 : Net::PortForwarder(parent)
37 , m_storeActive {u"Network/PortForwardingEnabled"_qs, true}
38 , m_provider {provider}
40 if (isEnabled())
41 start();
44 PortForwarderImpl::~PortForwarderImpl()
46 stop();
49 bool PortForwarderImpl::isEnabled() const
51 return m_storeActive;
54 void PortForwarderImpl::setEnabled(const bool enabled)
56 if (m_storeActive == enabled)
57 return;
59 if (enabled)
60 start();
61 else
62 stop();
63 m_storeActive = enabled;
66 void PortForwarderImpl::setPorts(const QString &profile, QSet<quint16> ports)
68 const QSet<quint16> oldForwardedPorts = std::accumulate(m_portProfiles.cbegin(), m_portProfiles.cend(), QSet<quint16>());
70 m_portProfiles[profile] = ports;
71 const QSet<quint16> newForwardedPorts = std::accumulate(m_portProfiles.cbegin(), m_portProfiles.cend(), QSet<quint16>());
73 m_provider->removeMappedPorts(oldForwardedPorts - newForwardedPorts);
74 m_provider->addMappedPorts(newForwardedPorts - oldForwardedPorts);
77 void PortForwarderImpl::removePorts(const QString &profile)
79 setPorts(profile, {});
82 void PortForwarderImpl::start()
84 m_provider->enablePortMapping();
85 for (const QSet<quint16> &ports : asConst(m_portProfiles))
86 m_provider->addMappedPorts(ports);
89 void PortForwarderImpl::stop()
91 m_provider->disablePortMapping();