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

0001 /***************************************************************************
0002  *  Copyright (C) 2020 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/media/instantmessagingupdater.h"
0021 
0022 #include "data/chatroom.h"
0023 #include "instantmessaging/dicemessage.h"
0024 #include "instantmessaging/messagefactory.h"
0025 #include "instantmessaging/messageinterface.h"
0026 #include "media/mediatype.h"
0027 #include "model/filteredplayermodel.h"
0028 #include "model/instantmessagingmodel.h"
0029 #include "model/messagemodel.h"
0030 
0031 #include "network/networkmessagereader.h"
0032 #include "network/networkmessagewriter.h"
0033 #include "worker/convertionhelper.h"
0034 
0035 namespace InstantMessaging
0036 {
0037 InstantMessagingUpdater::InstantMessagingUpdater(QObject* parent) : QObject(parent) {}
0038 
0039 void InstantMessagingUpdater::openChat(InstantMessaging::ChatRoom* chat)
0040 {
0041     if(!chat)
0042         return;
0043 
0044     NetworkMessageWriter msg(NetMsg::InstantMessageCategory, NetMsg::AddChatroomAction);
0045     auto ids= chat->recipiants()->recipiantIds();
0046     if(chat->type() == ChatRoom::EXTRA)
0047     {
0048         msg.setRecipientList(ids, NetworkMessage::OneOrMany);
0049     }
0050     msg.uint8(chat->type());
0051     msg.string8(chat->uuid());
0052     msg.string32(chat->title());
0053 
0054     if(chat->type() == ChatRoom::EXTRA)
0055     {
0056         msg.uint16(ids.size());
0057         std::for_each(ids.begin(), ids.end(), [&msg](const QString& id) { msg.string16(id); });
0058     }
0059 
0060     msg.sendToServer();
0061 }
0062 
0063 void InstantMessagingUpdater::readChatroomToModel(InstantMessaging::InstantMessagingModel* model,
0064                                                   NetworkMessageReader* msg)
0065 {
0066     if(!model || !msg)
0067         return;
0068 
0069     auto type= static_cast<ChatRoom::ChatRoomType>(msg->uint8());
0070     auto uuid= msg->string8();
0071     auto title= msg->string32();
0072     auto idCount= msg->uint16();
0073     QStringList ids;
0074     for(int i= 0; i < idCount; ++i)
0075     {
0076         ids << msg->string16();
0077     }
0078     if(type == ChatRoom::EXTRA)
0079         model->insertExtraChatroom(title, ids, true, uuid);
0080     else
0081         model->insertGlobalChatroom(title, uuid);
0082 }
0083 
0084 void InstantMessagingUpdater::addChatRoom(InstantMessaging::ChatRoom* room, bool remote)
0085 {
0086     if(nullptr == room)
0087         return;
0088 
0089     if(!remote
0090        && (room->type() == InstantMessaging::ChatRoom::EXTRA
0091            || (room->type() == InstantMessaging::ChatRoom::GLOBAL && room->uuid() != QStringLiteral("global"))))
0092         openChat(room);
0093 
0094     connect(room, &InstantMessaging::ChatRoom::titleChanged, this,
0095             [this, room]() { sendOffChatRoomChanges<QString>(room, QStringLiteral("title")); });
0096 
0097     auto model= room->messageModel();
0098     connect(model, &InstantMessaging::MessageModel::messageAdded, this, [room](MessageInterface* message) {
0099         if(!message)
0100             return;
0101         if(message->type() == InstantMessaging::MessageInterface::Error)
0102             return;
0103         auto type= room->type();
0104         NetworkMessageWriter msg(NetMsg::InstantMessageCategory, NetMsg::InstantMessageAction);
0105         if(type != ChatRoom::GLOBAL && room->uuid() != QStringLiteral("global"))
0106         {
0107             auto model= room->recipiants();
0108             if(!model)
0109                 return;
0110 
0111             auto recipiants= model->recipiantIds();
0112             if(type == ChatRoom::SINGLEPLAYER)
0113                 recipiants.append(room->uuid());
0114 
0115             msg.setRecipientList(recipiants, NetworkMessage::OneOrMany);
0116         }
0117         msg.uint8(type);
0118         msg.string8(room->uuid());
0119         msg.uint8(message->type());
0120         msg.string16(message->owner());
0121         msg.string16(message->writer());
0122         msg.string32(message->text());
0123         msg.dateTime(message->dateTime());
0124         msg.string32(message->imageLink().toString());
0125 
0126         msg.sendToServer();
0127     });
0128 }
0129 
0130 void InstantMessagingUpdater::addMessageToModel(InstantMessaging::InstantMessagingModel* model,
0131                                                 NetworkMessageReader* msg)
0132 {
0133     using IM= InstantMessaging::MessageInterface;
0134     auto type= static_cast<InstantMessaging::ChatRoom::ChatRoomType>(msg->uint8());
0135     auto uuid= msg->string8();
0136     auto messageType= static_cast<IM::MessageType>(msg->uint8());
0137     auto owner= msg->string16();
0138     auto writer= msg->string16();
0139     auto text= msg->string32();
0140     auto time= msg->dateTime();
0141     auto url= QUrl::fromUserInput(msg->string32());
0142 
0143     MessageInterface* imMessage
0144         = InstantMessaging::MessageFactory::createMessage(owner, writer, time, messageType, text, url);
0145 
0146     if(imMessage == nullptr)
0147         return;
0148 
0149     model->addMessageIntoChatroom(imMessage, type, uuid);
0150 }
0151 
0152 void InstantMessagingUpdater::sendMessage() {}
0153 
0154 void InstantMessagingUpdater::closeChat() {}
0155 
0156 template <typename T>
0157 void InstantMessagingUpdater::sendOffChatRoomChanges(ChatRoom* chatRoom, const QString& property)
0158 {
0159     if(nullptr == chatRoom)
0160         return;
0161 
0162     NetworkMessageWriter msg(NetMsg::MediaCategory, NetMsg::UpdateMediaProperty);
0163     msg.uint8(static_cast<int>(Core::ContentType::INSTANTMESSAGING));
0164     msg.string8(chatRoom->uuid());
0165     msg.string16(property);
0166     auto val= chatRoom->property(property.toLocal8Bit().data());
0167     Helper::variantToType<T>(val.value<T>(), msg);
0168     msg.sendToServer();
0169 }
0170 
0171 } // namespace InstantMessaging