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

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 "controller/view_controller/sharednotecontroller.h"
0021 
0022 #include "data/player.h"
0023 #include "model/playermodel.h"
0024 #include "utils/iohelper.h"
0025 #include "worker/iohelper.h"
0026 
0027 QPointer<PlayerModel> SharedNoteController::m_playerModel;
0028 
0029 SharedNoteController::SharedNoteController(const QString& ownerId, const QString& local, const QString& uuid,
0030                                            QObject* parent)
0031     : MediaControllerBase(uuid, Core::ContentType::SHAREDNOTE, parent)
0032     , m_participantModel(new ParticipantModel(ownerId, m_playerModel))
0033 {
0034     setOwnerId(ownerId);
0035     setLocalId(local);
0036 
0037     connect(this, &SharedNoteController::ownerIdChanged, m_participantModel.get(), &ParticipantModel::setOwner);
0038     connect(this, &SharedNoteController::localIdChanged, this,
0039             [this]() {
0040 
0041             });
0042 
0043     connect(this, &SharedNoteController::urlChanged, this,
0044             [this](const QUrl& path) { setText(utils::IOHelper::readTextFile(path.toLocalFile())); });
0045 
0046     connect(m_participantModel.get(), &ParticipantModel::userReadPermissionChanged, this,
0047             [this](const QString& id, bool b)
0048             {
0049                 if(b)
0050                     emit openShareNoteTo(id);
0051                 else
0052                     emit closeShareNoteTo(id);
0053             });
0054     connect(m_participantModel.get(), &ParticipantModel::userWritePermissionChanged, this,
0055             &SharedNoteController::userCanWrite);
0056     connect(m_participantModel.get(), &ParticipantModel::userReadPermissionChanged, this,
0057             [this](const QString& playerId, bool)
0058             {
0059                 if(playerId != localId())
0060                     return;
0061                 emit permissionChanged(permission());
0062             });
0063     connect(m_participantModel.get(), &ParticipantModel::userWritePermissionChanged, this,
0064             [this](const QString& playerId, bool)
0065             {
0066                 if(playerId != localId())
0067                     return;
0068                 emit permissionChanged(permission());
0069             });
0070 
0071     setParticipantPanelVisible(localIsOwner());
0072 
0073     connect(this, &SharedNoteController::textChanged, this, [this] { setModified(); });
0074     connect(this, &SharedNoteController::highligthedSyntaxChanged, this, [this] { setModified(); });
0075     connect(this, &SharedNoteController::markdownVisibleChanged, this, [this] { setModified(); });
0076 }
0077 
0078 SharedNoteController::~SharedNoteController()= default;
0079 
0080 void SharedNoteController::setPlayerModel(PlayerModel* model)
0081 {
0082     m_playerModel= model;
0083 }
0084 
0085 void SharedNoteController::setPermission(ParticipantModel::Permission perm)
0086 {
0087     m_participantModel->setPlayerPermission(localId(), perm);
0088 }
0089 
0090 QString SharedNoteController::text() const
0091 {
0092     return m_text;
0093 }
0094 
0095 bool SharedNoteController::localCanWrite() const
0096 {
0097     return m_participantModel->permissionFor(localId()) == ParticipantModel::Permission::readWrite;
0098 }
0099 
0100 QString SharedNoteController::textUpdate() const
0101 {
0102     return m_latestCommand;
0103 }
0104 
0105 ParticipantModel::Permission SharedNoteController::permission() const
0106 {
0107     return m_participantModel->permissionFor(localId());
0108 }
0109 
0110 bool SharedNoteController::participantPanelVisible() const
0111 {
0112     return m_participantVisible;
0113 }
0114 
0115 ParticipantModel* SharedNoteController::participantModel() const
0116 {
0117     return m_participantModel.get();
0118 }
0119 
0120 SharedNoteController::HighlightedSyntax SharedNoteController::highligthedSyntax() const
0121 {
0122     return m_highlightedSyntax;
0123 }
0124 
0125 bool SharedNoteController::markdownVisible() const
0126 {
0127     return m_markdownPreview;
0128 }
0129 
0130 PlayerModel* SharedNoteController::playerModel() const
0131 {
0132     return m_playerModel;
0133 }
0134 
0135 bool SharedNoteController::canWrite(Player* player) const
0136 {
0137     if(nullptr == player)
0138         return false;
0139     return (m_participantModel->permissionFor(player->uuid()) == ParticipantModel::readWrite);
0140 }
0141 
0142 bool SharedNoteController::canRead(Player* player) const
0143 {
0144     if(nullptr == player)
0145         return false;
0146     bool write= canWrite(player);
0147     bool read= (m_participantModel->permissionFor(player->uuid()) == ParticipantModel::readOnly);
0148     return write || read;
0149 }
0150 
0151 QString SharedNoteController::updateCmd() const
0152 {
0153     return m_updateCmd;
0154 }
0155 
0156 void SharedNoteController::setParticipantPanelVisible(bool b)
0157 {
0158     if(b == m_participantVisible)
0159         return;
0160     m_participantVisible= b;
0161     emit participantPanelVisibleChanged(m_participantVisible);
0162 }
0163 
0164 void SharedNoteController::setText(const QString& text)
0165 {
0166     if(text == m_text)
0167         return;
0168     m_text= text;
0169     emit textChanged(m_text);
0170 }
0171 
0172 void SharedNoteController::setHighligthedSyntax(SharedNoteController::HighlightedSyntax syntax)
0173 {
0174     if(m_highlightedSyntax == syntax)
0175         return;
0176     m_highlightedSyntax= syntax;
0177     emit highligthedSyntaxChanged();
0178 }
0179 
0180 void SharedNoteController::demoteCurrentItem(const QModelIndex& index)
0181 {
0182     m_participantModel->demotePlayer(index);
0183 }
0184 
0185 void SharedNoteController::promoteCurrentItem(const QModelIndex& index)
0186 {
0187     m_participantModel->promotePlayer(index);
0188 }
0189 
0190 void SharedNoteController::setMarkdownVisible(bool b)
0191 {
0192     if(m_markdownPreview == b)
0193         return;
0194     m_markdownPreview= b;
0195     emit markdownVisibleChanged(m_markdownPreview);
0196 }
0197 
0198 void SharedNoteController::setTextUpdate(const QString& cmd)
0199 {
0200     if(cmd == m_latestCommand)
0201         return;
0202     m_latestCommand= cmd;
0203     emit textUpdateChanged(m_latestCommand);
0204 }
0205 
0206 void SharedNoteController::setUpdateCmd(const QString& cmd)
0207 {
0208     auto tmpCmd= cmd;
0209     if(!cmd.startsWith("doc:"))
0210         return;
0211 
0212     tmpCmd.remove(0, 4);
0213     static QRegularExpression rx("(\\d+)\\s(\\d+)\\s(\\d+)\\s(.*)");
0214     QRegularExpressionMatch match;
0215     if(!cmd.contains(rx, &match))
0216         return;
0217 
0218     int pos= match.captured(1).toInt();
0219     int charsRemoved= match.captured(2).toInt();
0220     int charsAdded= match.captured(3).toInt();
0221     tmpCmd= match.captured(4);
0222     m_updateCmd = cmd;
0223     emit updateCmdChanged();
0224     emit collabTextChanged(pos, charsRemoved, charsAdded, tmpCmd);
0225 }