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

0001 /***************************************************************************
0002  *  Copyright (C) 2021 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software 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 "worker/networkhelper.h"
0021 
0022 #include <QJsonDocument>
0023 
0024 #include "media/mediatype.h"
0025 #include "network/channel.h"
0026 #include "network/channelmodel.h"
0027 #include "network/serverconnection.h"
0028 
0029 namespace helper
0030 {
0031 namespace network
0032 {
0033 QByteArray jsonObjectToByteArray(const QJsonObject& obj)
0034 {
0035     QJsonDocument doc;
0036     doc.setObject(obj);
0037     return doc.toJson(QJsonDocument::Indented);
0038 }
0039 
0040 QJsonObject channelItemToJsonObject(const TreeItem* item)
0041 {
0042     QJsonObject jsonObj;
0043     if(item->isLeaf())
0044     {
0045         auto client= dynamic_cast<const ServerConnection*>(item);
0046         if(client)
0047         {
0048             jsonObj[Core::jsonNetwork::JSON_TYPE]= Core::jsonNetwork::JSON_TYPE_CLIENT;
0049             jsonObj[Core::jsonNetwork::JSON_NAME]= client->name();
0050             jsonObj[Core::jsonNetwork::JSON_ADMIN]= client->isAdmin();
0051             jsonObj[Core::jsonNetwork::JSON_ID]= client->uuid();
0052             jsonObj[Core::jsonNetwork::JSON_IDPLAYER]= client->playerId();
0053         }
0054     }
0055     else
0056     {
0057         auto channel= dynamic_cast<const Channel*>(item);
0058         if(channel)
0059         {
0060 
0061             jsonObj[Core::jsonNetwork::JSON_TYPE]= Core::jsonNetwork::JSON_TYPE_CHANNEL;
0062             jsonObj[Core::jsonNetwork::JSON_NAME]= channel->name();
0063             jsonObj[Core::jsonNetwork::JSON_DESCRIPTION]= channel->description();
0064             jsonObj[Core::jsonNetwork::JSON_PASSWORD]= QString::fromUtf8(channel->password().toBase64());
0065             jsonObj[Core::jsonNetwork::JSON_USERLISTED]= channel->usersListed();
0066             jsonObj[Core::jsonNetwork::JSON_MEMORYSIZE]= static_cast<int>(channel->memorySize());
0067             jsonObj[Core::jsonNetwork::JSON_LOCKED]= channel->locked();
0068             jsonObj[Core::jsonNetwork::JSON_ID]= channel->uuid();
0069             QJsonArray array;
0070             auto const& children= channel->childrenItem();
0071             for(auto& child : children)
0072             {
0073                 auto childObj= channelItemToJsonObject(child);
0074                 array.append(childObj);
0075             }
0076             jsonObj[Core::jsonNetwork::JSON_CHILDREN]= array;
0077         }
0078     }
0079     return jsonObj;
0080 }
0081 
0082 QJsonObject channelModelToJSonObject(ChannelModel* model)
0083 {
0084     if(!model)
0085         return {};
0086 
0087     auto channels= model->modelData();
0088     QJsonObject obj;
0089     QJsonArray array;
0090     for(auto& item : channels) // int i = 0; i< m_root->childCount(); ++i)
0091     {
0092         if(nullptr == item)
0093             continue;
0094         auto jsonObj= channelItemToJsonObject(item);
0095         array.append(jsonObj);
0096     }
0097     obj[Core::jsonNetwork::JSON_CHANNELS]= array;
0098     return obj;
0099 }
0100 
0101 TreeItem* readClient(const QJsonObject& obj)
0102 {
0103     auto res= new ServerConnection(nullptr, nullptr);
0104 
0105     res->setName(obj[Core::jsonNetwork::JSON_NAME].toString());
0106     res->setIsAdmin(obj[Core::jsonNetwork::JSON_ADMIN].toBool());
0107     res->setUuid(obj[Core::jsonNetwork::JSON_ID].toString());
0108     res->setPlayerId(obj[Core::jsonNetwork::JSON_IDPLAYER].toString());
0109 
0110     return res;
0111 }
0112 
0113 TreeItem* readChannel(const QJsonObject& obj)
0114 {
0115     auto res= new Channel();
0116     res->setPassword(QByteArray::fromBase64(obj[Core::jsonNetwork::JSON_PASSWORD].toString().toUtf8()));
0117     res->setName(obj[Core::jsonNetwork::JSON_NAME].toString());
0118     res->setUsersListed(obj[Core::jsonNetwork::JSON_USERLISTED].toBool());
0119     res->setMemorySize(obj[Core::jsonNetwork::JSON_MEMORYSIZE].toInt());
0120     res->setLocked(obj[Core::jsonNetwork::JSON_LOCKED].toBool());
0121     res->setUuid(obj[Core::jsonNetwork::JSON_ID].toString());
0122     res->setName(obj[Core::jsonNetwork::JSON_NAME].toString());
0123 
0124     auto children= obj[Core::jsonNetwork::JSON_CHILDREN].toArray();
0125     for(auto childRef : children)
0126     {
0127         auto child= childRef.toObject();
0128         TreeItem* treeItem= nullptr;
0129         if(child[Core::jsonNetwork::JSON_TYPE].toString() == Core::jsonNetwork::JSON_TYPE_CHANNEL)
0130         {
0131             treeItem= readChannel(child);
0132         }
0133         else
0134         {
0135             treeItem= readClient(child);
0136         }
0137         res->addChild(treeItem);
0138     }
0139 
0140     return res;
0141 }
0142 
0143 void fetchChannelModel(ChannelModel* model, const QJsonObject& obj)
0144 {
0145     auto array= obj[Core::jsonNetwork::JSON_CHANNELS].toArray();
0146 
0147     QList<TreeItem*> children;
0148     for(auto itemRef : array)
0149     {
0150         auto item= itemRef.toObject();
0151         QPointer<TreeItem> treeItem;
0152         if(item[Core::jsonNetwork::JSON_TYPE].toString() == Core::jsonNetwork::JSON_TYPE_CHANNEL)
0153         {
0154             treeItem= readChannel(item);
0155         }
0156         else
0157         {
0158             treeItem= readClient(item);
0159         }
0160         children.append(treeItem);
0161     }
0162 
0163     model->resetData(children);
0164 }
0165 
0166 } // namespace network
0167 } // namespace helper