WebUI: Fix reloading page after login
[qBittorrent.git] / src / webui / api / apicontroller.cpp
blobca2c318f42beb01e6a5a0626af14284eeb068a6a
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2018-2024 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 "apicontroller.h"
31 #include <algorithm>
33 #include <QHash>
34 #include <QJsonDocument>
35 #include <QList>
36 #include <QMetaObject>
38 #include "apierror.h"
40 void APIResult::clear()
42 data.clear();
43 mimeType.clear();
44 filename.clear();
47 APIController::APIController(IApplication *app, QObject *parent)
48 : ApplicationComponent(app, parent)
52 APIResult APIController::run(const QString &action, const StringMap &params, const DataMap &data)
54 m_result.clear(); // clear result
55 m_params = params;
56 m_data = data;
58 const QByteArray methodName = action.toLatin1() + "Action";
59 if (!QMetaObject::invokeMethod(this, methodName.constData()))
60 throw APIError(APIErrorType::NotFound);
62 return m_result;
65 const StringMap &APIController::params() const
67 return m_params;
70 const DataMap &APIController::data() const
72 return m_data;
75 void APIController::requireParams(const QList<QString> &requiredParams) const
77 const bool hasAllRequiredParams = std::all_of(requiredParams.cbegin(), requiredParams.cend()
78 , [this](const QString &requiredParam)
80 return params().contains(requiredParam);
81 });
83 if (!hasAllRequiredParams)
84 throw APIError(APIErrorType::BadParams);
87 void APIController::setResult(const QString &result)
89 m_result.data = result;
92 void APIController::setResult(const QJsonArray &result)
94 m_result.data = QJsonDocument(result);
97 void APIController::setResult(const QJsonObject &result)
99 m_result.data = QJsonDocument(result);
102 void APIController::setResult(const QByteArray &result, const QString &mimeType, const QString &filename)
104 m_result.data = result;
105 m_result.mimeType = mimeType;
106 m_result.filename = filename;