WebUI: Fix reloading page after login
[qBittorrent.git] / src / webui / api / logcontroller.cpp
blobb5a36b3e46b548967948b35210bbc0750cf37c5c
1 /*
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 "logcontroller.h"
31 #include <QJsonArray>
32 #include <QJsonObject>
33 #include <QList>
35 #include "base/global.h"
36 #include "base/logger.h"
37 #include "base/utils/string.h"
39 const QString KEY_LOG_ID = u"id"_s;
40 const QString KEY_LOG_TIMESTAMP = u"timestamp"_s;
41 const QString KEY_LOG_MSG_TYPE = u"type"_s;
42 const QString KEY_LOG_MSG_MESSAGE = u"message"_s;
43 const QString KEY_LOG_PEER_IP = u"ip"_s;
44 const QString KEY_LOG_PEER_BLOCKED = u"blocked"_s;
45 const QString KEY_LOG_PEER_REASON = u"reason"_s;
47 // Returns the log in JSON format.
48 // The return value is an array of dictionaries.
49 // The dictionary keys are:
50 // - "id": id of the message
51 // - "timestamp": milliseconds since epoch
52 // - "type": type of the message (int, see MsgType)
53 // - "message": text of the message
54 // GET params:
55 // - normal (bool): include normal messages (default true)
56 // - info (bool): include info messages (default true)
57 // - warning (bool): include warning messages (default true)
58 // - critical (bool): include critical messages (default true)
59 // - last_known_id (int): exclude messages with id <= 'last_known_id' (default -1)
60 void LogController::mainAction()
62 using Utils::String::parseBool;
64 const bool isNormal = parseBool(params()[u"normal"_s]).value_or(true);
65 const bool isInfo = parseBool(params()[u"info"_s]).value_or(true);
66 const bool isWarning = parseBool(params()[u"warning"_s]).value_or(true);
67 const bool isCritical = parseBool(params()[u"critical"_s]).value_or(true);
69 bool ok = false;
70 int lastKnownId = params()[u"last_known_id"_s].toInt(&ok);
71 if (!ok)
72 lastKnownId = -1;
74 Logger *const logger = Logger::instance();
75 QJsonArray msgList;
77 for (const Log::Msg &msg : asConst(logger->getMessages(lastKnownId)))
79 if (!(((msg.type == Log::NORMAL) && isNormal)
80 || ((msg.type == Log::INFO) && isInfo)
81 || ((msg.type == Log::WARNING) && isWarning)
82 || ((msg.type == Log::CRITICAL) && isCritical)))
83 continue;
85 msgList.append(QJsonObject
87 {KEY_LOG_ID, msg.id},
88 {KEY_LOG_TIMESTAMP, msg.timestamp},
89 {KEY_LOG_MSG_TYPE, msg.type},
90 {KEY_LOG_MSG_MESSAGE, msg.message}
91 });
94 setResult(msgList);
97 // Returns the peer log in JSON format.
98 // The return value is an array of dictionaries.
99 // The dictionary keys are:
100 // - "id": id of the message
101 // - "timestamp": milliseconds since epoch
102 // - "ip": IP of the peer
103 // - "blocked": whether or not the peer was blocked
104 // - "reason": reason of the block
105 // GET params:
106 // - last_known_id (int): exclude messages with id <= 'last_known_id' (default -1)
107 void LogController::peersAction()
109 bool ok = false;
110 int lastKnownId = params()[u"last_known_id"_s].toInt(&ok);
111 if (!ok)
112 lastKnownId = -1;
114 Logger *const logger = Logger::instance();
115 QJsonArray peerList;
117 for (const Log::Peer &peer : asConst(logger->getPeers(lastKnownId)))
119 peerList.append(QJsonObject
121 {KEY_LOG_ID, peer.id},
122 {KEY_LOG_TIMESTAMP, peer.timestamp},
123 {KEY_LOG_PEER_IP, peer.ip},
124 {KEY_LOG_PEER_BLOCKED, peer.blocked},
125 {KEY_LOG_PEER_REASON, peer.reason}
129 setResult(peerList);