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

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 "updater/controller/servermanagerupdater.h"
0021 
0022 #include <QJsonDocument>
0023 #include <QMetaObject>
0024 #include <QSettings>
0025 
0026 #include "network/channelmodel.h"
0027 #include "network/serverconnection.h"
0028 #include "worker/networkhelper.h"
0029 #include <network/serverconnectionmanager.h>
0030 
0031 ServerManagerUpdater::ServerManagerUpdater(ServerConnectionManager* ctrl, bool internal, QObject* parent)
0032     : QObject(parent), m_ctrl(ctrl), m_internal{internal}
0033 {
0034     if(!m_ctrl)
0035         return;
0036     auto model= m_ctrl->channelModel();
0037     Q_ASSERT(model);
0038 
0039     auto computeModel= [this]() {
0040         auto model= m_ctrl->channelModel();
0041         setChannelData(helper::network::jsonObjectToByteArray(helper::network::channelModelToJSonObject(model)));
0042 
0043         if(!m_internal)
0044         {
0045             QSettings settings("Rolisteam", "roliserver");
0046             QJsonDocument doc(helper::network::channelModelToJSonObject(model));
0047             settings.setValue(helper::key::channel_data, doc);
0048         }
0049     };
0050     connect(model, &ChannelModel::modelChanged, this, computeModel);
0051     connect(model, &ChannelModel::dataChanged, this, computeModel);
0052     connect(model, &ChannelModel::rowsInserted, this, computeModel);
0053     connect(model, &ChannelModel::rowsMoved, this, computeModel);
0054     connect(model, &ChannelModel::rowsRemoved, this, computeModel);
0055 
0056     connect(this, &ServerManagerUpdater::channelsDataChanged, this, [this]() {
0057         auto conns= m_ctrl->connections();
0058         for(auto conn : conns)
0059         {
0060             NetworkMessageWriter* msg= new NetworkMessageWriter(NetMsg::AdministrationCategory, NetMsg::SetChannelList);
0061             msg->byteArray32(m_channelData);
0062             QMetaObject::invokeMethod(conn, "sendMessage", Qt::QueuedConnection,
0063                                       Q_ARG(NetworkMessage*, static_cast<NetworkMessage*>(msg)), Q_ARG(bool, true));
0064         }
0065         // delete msg;
0066     });
0067 }
0068 
0069 QByteArray ServerManagerUpdater::channelsData()
0070 {
0071     return m_channelData;
0072 }
0073 
0074 void ServerManagerUpdater::setChannelData(const QByteArray& array)
0075 {
0076     if(array == m_channelData)
0077         return;
0078     m_channelData= array;
0079     emit channelsDataChanged();
0080 }