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/sharednotecontrollerupdater.h" 0021 0022 #include <QSet> 0023 #include <stdexcept> 0024 0025 #include "controller/view_controller/sharednotecontroller.h" 0026 #include "model/contentmodel.h" 0027 #include "network/networkmessagereader.h" 0028 #include "network/networkmessagewriter.h" 0029 #include "worker/convertionhelper.h" 0030 #include "worker/messagehelper.h" 0031 0032 SharedNoteControllerUpdater::SharedNoteControllerUpdater(FilteredContentModel* model, 0033 campaign::CampaignManager* campaign, QObject* parent) 0034 : MediaUpdaterInterface(campaign, parent), m_notesModel(model) 0035 { 0036 } 0037 0038 void SharedNoteControllerUpdater::addMediaController(MediaControllerBase* ctrl) 0039 { 0040 addSharedNoteController(dynamic_cast<SharedNoteController*>(ctrl)); 0041 } 0042 0043 void SharedNoteControllerUpdater::addSharedNoteController(SharedNoteController* noteCtrl) 0044 { 0045 if(noteCtrl == nullptr || m_noteReaders.find(noteCtrl) != m_noteReaders.end()) 0046 return; 0047 0048 m_noteReaders.insert({noteCtrl, QSet<QString>()}); 0049 0050 connect(noteCtrl, &SharedNoteController::openShareNoteTo, this, [this, noteCtrl](const QString& id) { 0051 MessageHelper::shareNotesTo(noteCtrl, {id}); 0052 try 0053 { 0054 auto& it= m_noteReaders.at(noteCtrl); 0055 it.insert(id); 0056 } 0057 catch(const std::out_of_range&) 0058 { 0059 Q_ASSERT(false); 0060 } 0061 }); 0062 0063 connect(noteCtrl, &SharedNoteController::closeShareNoteTo, this, [this, noteCtrl](const QString& id) { 0064 MessageHelper::closeNoteTo(noteCtrl, id); 0065 try 0066 { 0067 auto& it= m_noteReaders.at(noteCtrl); 0068 it.remove(id); 0069 } 0070 catch(...) 0071 { 0072 Q_ASSERT(false); 0073 } 0074 }); 0075 0076 connect(noteCtrl, &SharedNoteController::textUpdateChanged, this, 0077 [this, noteCtrl]() { sendOffChanges<QString>(noteCtrl, QStringLiteral("updateCmd")); }); 0078 connect(noteCtrl, &SharedNoteController::userCanWrite, this, 0079 [this, noteCtrl](QString id, bool write) { sendOffPermissionChanged(noteCtrl, write, id); }); 0080 0081 connect(noteCtrl, &SharedNoteController::modifiedChanged, this, [noteCtrl, this]() { 0082 if(noteCtrl->modified()) 0083 { 0084 saveMediaController(noteCtrl); 0085 } 0086 }); 0087 } 0088 0089 void SharedNoteControllerUpdater::sendOffPermissionChanged(SharedNoteController* ctrl, bool b, const QString& id) 0090 { 0091 if(m_updatingFromNetwork) 0092 return; 0093 0094 NetworkMessageWriter msg(NetMsg::MediaCategory, NetMsg::UpdateMediaProperty); 0095 if(!id.isEmpty()) 0096 { 0097 msg.setRecipientList({id}, NetworkMessage::OneOrMany); 0098 } 0099 msg.uint8(static_cast<int>(Core::ContentType::SHAREDNOTE)); 0100 msg.string8(ctrl->uuid()); 0101 msg.string16(QStringLiteral("permission")); 0102 auto perm= b ? ParticipantModel::Permission::readWrite : ParticipantModel::Permission::readOnly; 0103 Helper::variantToType<ParticipantModel::Permission>(perm, msg); 0104 msg.sendToServer(); 0105 } 0106 0107 NetWorkReceiver::SendType SharedNoteControllerUpdater::processMessage(NetworkMessageReader* msg) 0108 { 0109 if(msg->category() == NetMsg::MediaCategory && NetMsg::UpdateMediaProperty) 0110 { 0111 auto id= msg->string8(); 0112 auto ctrls= m_notesModel->contentController<SharedNoteController*>(); 0113 auto it 0114 = std::find_if(ctrls.begin(), ctrls.end(), [id](SharedNoteController* ctrl) { return id == ctrl->uuid(); }); 0115 if(it != ctrls.end()) 0116 updateProperty(msg, (*it)); 0117 } 0118 0119 return MediaUpdaterInterface::processMessage(msg); 0120 } 0121 0122 void SharedNoteControllerUpdater::updateProperty(NetworkMessageReader* msg, SharedNoteController* ctrl) 0123 { 0124 if(nullptr == msg || nullptr == ctrl) 0125 return; 0126 0127 auto property= msg->string16(); 0128 0129 QVariant var; 0130 0131 if(property == QStringLiteral("updateCmd")) 0132 { 0133 var= QVariant::fromValue(msg->string32()); 0134 } 0135 else if(property == QStringLiteral("permission")) 0136 { 0137 var= QVariant::fromValue(static_cast<ParticipantModel::Permission>(msg->uint8())); 0138 } 0139 0140 m_updatingFromNetwork= true; 0141 auto feedback= ctrl->setProperty(property.toLocal8Bit().data(), var); 0142 Q_ASSERT(feedback); 0143 m_updatingFromNetwork= false; 0144 }