2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2018 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 "transfercontroller.h"
31 #include <QJsonObject>
34 #include "base/bittorrent/peeraddress.h"
35 #include "base/bittorrent/peerinfo.h"
36 #include "base/bittorrent/session.h"
37 #include "base/bittorrent/sessionstatus.h"
38 #include "base/global.h"
39 #include "base/utils/string.h"
42 const QString KEY_TRANSFER_DLSPEED
= u
"dl_info_speed"_s
;
43 const QString KEY_TRANSFER_DLDATA
= u
"dl_info_data"_s
;
44 const QString KEY_TRANSFER_DLRATELIMIT
= u
"dl_rate_limit"_s
;
45 const QString KEY_TRANSFER_UPSPEED
= u
"up_info_speed"_s
;
46 const QString KEY_TRANSFER_UPDATA
= u
"up_info_data"_s
;
47 const QString KEY_TRANSFER_UPRATELIMIT
= u
"up_rate_limit"_s
;
48 const QString KEY_TRANSFER_DHT_NODES
= u
"dht_nodes"_s
;
49 const QString KEY_TRANSFER_CONNECTION_STATUS
= u
"connection_status"_s
;
51 // Returns the global transfer information in JSON format.
52 // The return value is a JSON-formatted dictionary.
53 // The dictionary keys are:
54 // - "dl_info_speed": Global download rate
55 // - "dl_info_data": Data downloaded this session
56 // - "up_info_speed": Global upload rate
57 // - "up_info_data": Data uploaded this session
58 // - "dl_rate_limit": Download rate limit
59 // - "up_rate_limit": Upload rate limit
60 // - "dht_nodes": DHT nodes connected to
61 // - "connection_status": Connection status
62 void TransferController::infoAction()
64 const BitTorrent::SessionStatus
&sessionStatus
= BitTorrent::Session::instance()->status();
68 dict
[KEY_TRANSFER_DLSPEED
] = static_cast<qint64
>(sessionStatus
.payloadDownloadRate
);
69 dict
[KEY_TRANSFER_DLDATA
] = static_cast<qint64
>(sessionStatus
.totalPayloadDownload
);
70 dict
[KEY_TRANSFER_UPSPEED
] = static_cast<qint64
>(sessionStatus
.payloadUploadRate
);
71 dict
[KEY_TRANSFER_UPDATA
] = static_cast<qint64
>(sessionStatus
.totalPayloadUpload
);
72 dict
[KEY_TRANSFER_DLRATELIMIT
] = BitTorrent::Session::instance()->downloadSpeedLimit();
73 dict
[KEY_TRANSFER_UPRATELIMIT
] = BitTorrent::Session::instance()->uploadSpeedLimit();
74 dict
[KEY_TRANSFER_DHT_NODES
] = static_cast<qint64
>(sessionStatus
.dhtNodes
);
75 if (!BitTorrent::Session::instance()->isListening())
76 dict
[KEY_TRANSFER_CONNECTION_STATUS
] = u
"disconnected"_s
;
78 dict
[KEY_TRANSFER_CONNECTION_STATUS
] = sessionStatus
.hasIncomingConnections
? u
"connected"_s
: u
"firewalled"_s
;
83 void TransferController::uploadLimitAction()
85 setResult(QString::number(BitTorrent::Session::instance()->uploadSpeedLimit()));
88 void TransferController::downloadLimitAction()
90 setResult(QString::number(BitTorrent::Session::instance()->downloadSpeedLimit()));
93 void TransferController::setUploadLimitAction()
95 requireParams({u
"limit"_s
});
96 qlonglong limit
= params()[u
"limit"_s
].toLongLong();
97 if (limit
== 0) limit
= -1;
99 BitTorrent::Session::instance()->setUploadSpeedLimit(limit
);
102 void TransferController::setDownloadLimitAction()
104 requireParams({u
"limit"_s
});
105 qlonglong limit
= params()[u
"limit"_s
].toLongLong();
106 if (limit
== 0) limit
= -1;
108 BitTorrent::Session::instance()->setDownloadSpeedLimit(limit
);
111 void TransferController::toggleSpeedLimitsModeAction()
113 BitTorrent::Session
*const session
= BitTorrent::Session::instance();
114 session
->setAltGlobalSpeedLimitEnabled(!session
->isAltGlobalSpeedLimitEnabled());
117 void TransferController::speedLimitsModeAction()
119 setResult(QString::number(BitTorrent::Session::instance()->isAltGlobalSpeedLimitEnabled()));
122 void TransferController::setSpeedLimitsModeAction()
124 requireParams({u
"mode"_s
});
126 const std::optional
<int> mode
= Utils::String::parseInt(params().value(u
"mode"_s
));
128 throw APIError(APIErrorType::BadParams
, tr("'mode': invalid argument"));
130 // Any non-zero values are considered as alternative mode
131 BitTorrent::Session::instance()->setAltGlobalSpeedLimitEnabled(mode
!= 0);
134 void TransferController::banPeersAction()
136 requireParams({u
"peers"_s
});
138 const QStringList peers
= params()[u
"peers"_s
].split(u
'|');
139 for (const QString
&peer
: peers
)
141 const BitTorrent::PeerAddress addr
= BitTorrent::PeerAddress::parse(peer
.trimmed());
142 if (!addr
.ip
.isNull())
143 BitTorrent::Session::instance()->banIP(addr
.ip
.toString());