File indexing completed on 2024-05-05 05:40:10

0001 /***************************************************************************
0002  *     Copyright (C) 2018 by Renaud Guezennec                              *
0003  *     https://rolisteam.org/                                           *
0004  *                                                                         *
0005  *   rolisteam is free software; you can redistribute it and/or modify     *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "common/uploadlogtoserver.h"
0021 
0022 #include <QJsonArray>
0023 #include <QJsonDocument>
0024 #include <QJsonObject>
0025 #include <QNetworkAccessManager>
0026 #include <QNetworkReply>
0027 #include <QNetworkRequest>
0028 #include <QUrl>
0029 #include <QUrlQuery>
0030 #include <iostream>
0031 
0032 LogUploader::LogUploader() : QObject(nullptr), m_accessManager(new QNetworkAccessManager(this))
0033 {
0034     connect(m_accessManager, &QNetworkAccessManager::finished, this, &LogUploader::finished);
0035 }
0036 
0037 int LogUploader::appId() const
0038 {
0039     return m_appId;
0040 }
0041 
0042 void LogUploader::setAppId(int appId)
0043 {
0044     m_appId= appId;
0045 }
0046 
0047 QString LogUploader::version() const
0048 {
0049     return m_version;
0050 }
0051 
0052 void LogUploader::setVersion(const QString& version)
0053 {
0054     m_version= version;
0055 }
0056 
0057 QString LogUploader::uuid() const
0058 {
0059     return m_uuid;
0060 }
0061 
0062 void LogUploader::setUuid(const QString& uuid)
0063 {
0064     m_uuid= uuid;
0065 }
0066 
0067 std::vector<common::Log> LogUploader::logs() const
0068 {
0069     return m_logs;
0070 }
0071 
0072 void LogUploader::setLogs(const std::vector<common::Log>& logs)
0073 {
0074     m_logs= logs;
0075 }
0076 
0077 void LogUploader::uploadLog()
0078 {
0079     // TODO Put json key into constexpr
0080     QJsonObject obj;
0081     obj["uuid"]= m_uuid;
0082     obj["version"]= m_version;
0083     obj["app"]= m_appId;
0084     obj["conf"]= m_conf;
0085 
0086     QJsonArray array;
0087     for(auto& log : m_logs)
0088     {
0089         QJsonObject logObj;
0090         logObj["level"]= log.m_level;
0091         logObj["log"]= log.m_message;
0092         logObj["category"]= log.m_category;
0093         logObj["timestamp"]= log.m_timestamp;
0094 
0095         array.append(logObj);
0096     }
0097     obj["logs"]= array;
0098 
0099     QJsonDocument doc;
0100     doc.setObject(obj);
0101 
0102     QUrl url("https://rolisteam.org/php/uploadlog.php");
0103     QNetworkRequest request(url);
0104 
0105     request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
0106     m_postData.clear();
0107     m_postData.append("log=");
0108     m_postData.append(doc.toJson());
0109     m_accessManager->post(request, m_postData);
0110 }
0111 
0112 QString LogUploader::conf() const
0113 {
0114     return m_conf;
0115 }
0116 
0117 void LogUploader::setConf(const QString& conf)
0118 {
0119     m_conf= conf;
0120 }