diff --git a/src/ruqolacore/room.cpp b/src/ruqolacore/room.cpp index 3dba7cdd..fbfacc21 100644 --- a/src/ruqolacore/room.cpp +++ b/src/ruqolacore/room.cpp @@ -1,963 +1,958 @@ /* * Copyright 2016 Riccardo Iaconelli * Copyright 2017-2018 Laurent Montel * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #include "rocketchataccount.h" #include "notificationoptionswrapper.h" #include "room.h" #include "utils.h" #include "ruqola_debug.h" #include "model/usersforroommodel.h" #include "model/usersforroomfilterproxymodel.h" #include "model/filesforroommodel.h" #include "model/filesforroomfilterproxymodel.h" #include "model/messagemodel.h" #include #include #include Room::Room(RocketChatAccount *account, QObject *parent) : QObject(parent) , mRocketChatAccount(account) { mUsersModelForRoom = new UsersForRoomModel(this); mUsersModelForRoom->setObjectName(QStringLiteral("usersforroommodel")); mUsersModelForRoomProxyModel = new UsersForRoomFilterProxyModel(this); mUsersModelForRoomProxyModel->setObjectName(QStringLiteral("usersforroommodelproxymodel")); mUsersModelForRoomProxyModel->setSourceModel(mUsersModelForRoom); mFilesModelForRoom = new FilesForRoomModel(mRocketChatAccount, this); mFilesModelForRoom->setObjectName(QStringLiteral("filesmodelforrooms")); mFilesForRoomFilterProxyModel = new FilesForRoomFilterProxyModel(this); mFilesForRoomFilterProxyModel->setObjectName(QStringLiteral("filesforroomfiltermodelproxy")); mFilesForRoomFilterProxyModel->setSourceModel(mFilesModelForRoom); mMessageModel = new MessageModel(QString(), mRocketChatAccount, this, this); } bool Room::operator==(const Room &other) const { //qDebug() << " other.id"< we need to clear it. setRoomCreatorUserId(QString()); setRoomCreatorUserName(QString()); } } bool Room::selected() const { return mSelected; } void Room::setSelected(bool selected) { if (mSelected != selected) { mSelected = selected; //Add signal otherwise it's not necessary to check value } } int Room::unread() const { return mUnread; } void Room::setUnread(int unread) { if (mUnread != unread) { mUnread = unread; Q_EMIT unreadChanged(); } } qint64 Room::jitsiTimeout() const { return mJitsiTimeout; } void Room::setJitsiTimeout(const qint64 &jitsiTimeout) { if (mJitsiTimeout != jitsiTimeout) { mJitsiTimeout = jitsiTimeout; Q_EMIT jitsiTimeoutChanged(); } } QStringList Room::mutedUsers() const { return mMutedUsers; } void Room::setMutedUsers(const QStringList &mutedUsers) { if (mMutedUsers != mutedUsers) { mMutedUsers = mutedUsers; Q_EMIT mutedUsersChanged(); } } QString Room::roomCreatorUserId() const { return mRoomCreateUserId; } void Room::setRoomCreatorUserId(const QString &userId) { mRoomCreateUserId = userId; } QString Room::roomOwnerUserName() const { return mRoomCreatorUserName; } void Room::setRoomCreatorUserName(const QString &userName) { mRoomCreatorUserName = userName; } QString Room::roomId() const { return mRoomId; } void Room::setRoomId(const QString &id) { if (mRoomId != id) { mRoomId = id; mMessageModel->setRoomID(id); } } bool Room::alert() const { return mAlert; } void Room::setBlocker(bool block) { if (mBlocker != block) { mBlocker = block; Q_EMIT blockerChanged(); } } bool Room::blocker() const { return mBlocker; } void Room::setAlert(bool alert) { if (mAlert != alert) { mAlert = alert; Q_EMIT alertChanged(); } } bool Room::open() const { return mOpen; } void Room::setOpen(bool open) { if (mOpen != open) { mOpen = open; Q_EMIT openChanged(); } } bool Room::readOnly() const { return mReadOnly; } void Room::setReadOnly(bool readOnly) { if (mReadOnly != readOnly) { mReadOnly = readOnly; Q_EMIT readOnlyChanged(); } } QString Room::topic() const { return mTopic; } void Room::setTopic(const QString &topic) { if (mTopic != topic) { mTopic = topic; Q_EMIT topicChanged(); } } bool Room::favorite() const { return mFavorite; } void Room::setFavorite(bool favorite) { if (mFavorite != favorite) { mFavorite = favorite; Q_EMIT favoriteChanged(); } } QString Room::channelType() const { return mChannelType; } void Room::setChannelType(const QString &channelType) { mChannelType = channelType; } QString Room::markdownAnnouncement() const { return Utils::markdownToRichText(mAnnouncement); } QString Room::announcement() const { return mAnnouncement; } void Room::setAnnouncement(const QString &announcement) { if (mAnnouncement != announcement) { mAnnouncement = announcement; Q_EMIT announcementChanged(); } } void Room::setName(const QString &name) { if (mName != name) { mName = name; Q_EMIT nameChanged(); } } void Room::parseInsertRoom(const QJsonObject &json) { QString roomID = json.value(QLatin1String("_id")).toString(); setRoomId(roomID); setName(json[QStringLiteral("name")].toString()); setJitsiTimeout(Utils::parseDate(QStringLiteral("jitsiTimeout"), json)); //topic/announcement/description is not part of update subscription const QString roomType = json.value(QLatin1String("t")).toString(); setChannelType(roomType); const QJsonValue favoriteValue = json.value(QLatin1String("f")); if (!favoriteValue.isUndefined()) { setFavorite(favoriteValue.toBool()); } - //Only private room has this settings. - if (roomType == QLatin1String("p")) { - setReadOnly(json[QStringLiteral("ro")].toBool()); - } + setReadOnly(json[QStringLiteral("ro")].toBool()); if (json.contains(QLatin1String("userMentions"))) { setUserMentions(json[QStringLiteral("userMentions")].toInt()); } if (json.contains(QLatin1String("announcement"))) { setAnnouncement(json[QStringLiteral("announcement")].toString()); } if (json.contains(QLatin1String("description"))) { setDescription(json[QStringLiteral("description")].toString()); } setUpdatedAt(Utils::parseDate(QStringLiteral("_updatedAt"), json)); setUnread(json[QStringLiteral("unread")].toInt()); setOpen(json[QStringLiteral("open")].toBool()); setAlert(json[QStringLiteral("alert")].toBool()); const QJsonValue blockerValue = json.value(QLatin1String("blocker")); if (!blockerValue.isUndefined()) { setBlocker(blockerValue.toBool()); } else { setBlocker(false); } //setE2eKeyId(json[QStringLiteral("e2eKeyId")].toString()); setE2EKey(json[QStringLiteral("E2EKey")].toString()); if (json.contains(QLatin1String("encrypted"))) { setEncrypted(json[QStringLiteral("encrypted")].toBool()); } else { setEncrypted(false); } //Blocked ??? const QJsonValue archivedValue = json.value(QLatin1String("archived")); if (!archivedValue.isUndefined()) { setArchived(archivedValue.toBool()); } else { setArchived(false); } parseCommonData(json); const QJsonValue ownerValue = json.value(QLatin1String("u")); if (!ownerValue.isUndefined()) { const QJsonObject objOwner = ownerValue.toObject(); setRoomCreatorUserId(objOwner.value(QLatin1String("_id")).toString()); setRoomCreatorUserName(objOwner.value(QLatin1String("username")).toString()); } else { //When room is initialized we are the owner. When we update room we have the real //owner and if it's empty => we need to clear it. setRoomCreatorUserId(QString()); setRoomCreatorUserName(QString()); } //qDebug() << " *thus" << *this; mNotificationOptions.parseNotificationOptions(json); } qint64 Room::lastSeeAt() const { return mLastSeeAt; } void Room::setLastSeeAt(qint64 lastSeeAt) { mLastSeeAt = lastSeeAt; //Add signal otherwise it's not necessary to check value } bool Room::blocked() const { return mBlocked; } void Room::setBlocked(bool blocked) { if (mBlocked != blocked) { mBlocked = blocked; Q_EMIT blockedChanged(); } } QStringList Room::roles() const { return mRoles; } void Room::setRoles(const QStringList &roles) { if (mRoles != roles) { mRoles = roles; Q_EMIT rolesChanged(); } } QStringList Room::ignoredUsers() const { return mIgnoredUsers; } void Room::setIgnoredUsers(const QStringList &ignoredUsers) { if (mIgnoredUsers != ignoredUsers) { mIgnoredUsers = ignoredUsers; Q_EMIT ignoredUsersChanged(); } } void Room::parseSubscriptionRoom(const QJsonObject &json) { QString roomID = json.value(QLatin1String("rid")).toString(); if (roomID.isEmpty()) { roomID = json.value(QLatin1String("_id")).toString(); } setRoomId(roomID); setName(json[QStringLiteral("name")].toString()); setJitsiTimeout(Utils::parseDate(QStringLiteral("jitsiTimeout"), json)); //topic/announcement/description is not part of update subscription const QString roomType = json.value(QLatin1String("t")).toString(); setChannelType(roomType); const QJsonValue favoriteValue = json.value(QLatin1String("f")); if (!favoriteValue.isUndefined()) { setFavorite(favoriteValue.toBool()); } setE2EKey(json[QStringLiteral("E2EKey")].toString()); - //Only private room has this settings. - if (roomType == QLatin1String("p")) { - setReadOnly(json[QStringLiteral("ro")].toBool()); - } + setReadOnly(json[QStringLiteral("ro")].toBool()); setUpdatedAt(Utils::parseDate(QStringLiteral("_updatedAt"), json)); setUnread(json[QStringLiteral("unread")].toInt()); setUserMentions(json[QStringLiteral("userMentions")].toInt()); setOpen(json[QStringLiteral("open")].toBool()); setAlert(json[QStringLiteral("alert")].toBool()); const QJsonValue blockerValue = json.value(QLatin1String("blocker")); if (!blockerValue.isUndefined()) { setBlocker(blockerValue.toBool()); } else { setBlocker(false); } //TODO e2ekey //TODO blocked ? const QJsonValue archivedValue = json.value(QLatin1String("archived")); if (!archivedValue.isUndefined()) { setArchived(archivedValue.toBool()); } else { setArchived(false); } parseCommonData(json); // const QJsonValue ownerValue = json.value(QLatin1String("u")); // if (!ownerValue.isUndefined()) { // const QJsonObject objOwner = ownerValue.toObject(); // setRoomCreatorUserId(objOwner.value(QLatin1String("_id")).toString()); // setRoomCreatorUserName(objOwner.value(QLatin1String("username")).toString()); // } else { // //When room is initialized we are the owner. When we update room we have the real // //owner and if it's empty => we need to clear it. // setRoomCreatorUserId(QString()); // setRoomCreatorUserName(QString()); // } //qDebug() << " *thus" << *this; mNotificationOptions.parseNotificationOptions(json); //TODO encrypted ? //TODO add muted } void Room::parseCommonData(const QJsonObject &json) { const QJsonArray mutedArray = json.value(QLatin1String("muted")).toArray(); QStringList lst; lst.reserve(mutedArray.count()); for (int i = 0; i < mutedArray.count(); ++i) { lst << mutedArray.at(i).toString(); } setMutedUsers(lst); const QJsonArray ignoredArray = json.value(QLatin1String("ignored")).toArray(); QStringList lstIgnored; lstIgnored.reserve(ignoredArray.count()); for (int i = 0; i < ignoredArray.count(); ++i) { lstIgnored << ignoredArray.at(i).toString(); } setIgnoredUsers(lstIgnored); const QJsonArray rolesArray = json.value(QLatin1String("roles")).toArray(); QStringList lstRoles; lstRoles.reserve(rolesArray.count()); for (int i = 0; i < rolesArray.count(); ++i) { lstRoles << rolesArray.at(i).toString(); } setRoles(lstRoles); } bool Room::joinCodeRequired() const { return mJoinCodeRequired; } void Room::setJoinCodeRequired(bool joinCodeRequired) { if (mJoinCodeRequired != joinCodeRequired) { mJoinCodeRequired = joinCodeRequired; Q_EMIT joinCodeRequiredChanged(); } } QString Room::e2eKeyId() const { return mE2eKeyId; } void Room::setE2eKeyId(const QString &e2eKeyId) { if (mE2eKeyId != e2eKeyId) { mE2eKeyId = e2eKeyId; Q_EMIT encryptionKeyIdChanged(); } } QString Room::e2EKey() const { return mE2EKey; } void Room::setE2EKey(const QString &e2EKey) { if (mE2EKey != e2EKey) { mE2EKey = e2EKey; Q_EMIT encryptionKeyChanged(); } } bool Room::encrypted() const { return mEncrypted; } void Room::setEncrypted(bool encrypted) { if (mEncrypted != encrypted) { mEncrypted = encrypted; Q_EMIT encryptedChanged(); } } Room *Room::fromJSon(const QJsonObject &o) { //FIXME Room *r = new Room(nullptr); r->setRoomId(o[QStringLiteral("rid")].toString()); r->setChannelType(o[QStringLiteral("t")].toString()); r->setName(o[QStringLiteral("name")].toString()); r->setRoomCreatorUserName(o[QStringLiteral("roomCreatorUserName")].toString()); r->setRoomCreatorUserId(o[QStringLiteral("roomCreatorUserID")].toString()); r->setTopic(o[QStringLiteral("topic")].toString()); r->setJitsiTimeout(static_cast(o[QStringLiteral("jitsiTimeout")].toDouble())); r->setReadOnly(o[QStringLiteral("ro")].toBool()); r->setUnread(o[QStringLiteral("unread")].toInt(0)); r->setUserMentions(o[QStringLiteral("userMentions")].toInt(0)); r->setAnnouncement(o[QStringLiteral("announcement")].toString()); r->setSelected(o[QStringLiteral("selected")].toBool()); r->setFavorite(o[QStringLiteral("favorite")].toBool()); r->setAlert(o[QStringLiteral("alert")].toBool()); r->setOpen(o[QStringLiteral("open")].toBool()); r->setArchived(o[QStringLiteral("archived")].toBool()); r->setDescription(o[QStringLiteral("description")].toString()); r->setBlocker(o[QStringLiteral("blocker")].toBool()); r->setBlocked(o[QStringLiteral("blocked")].toBool()); r->setEncrypted(o[QStringLiteral("encrypted")].toBool()); r->setE2EKey(o[QStringLiteral("e2ekey")].toString()); r->setE2eKeyId(o[QStringLiteral("e2ekeyid")].toString()); r->setJoinCodeRequired(o[QStringLiteral("joinCodeRequired")].toBool()); r->setUpdatedAt(static_cast(o[QStringLiteral("updatedAt")].toDouble())); r->setLastSeeAt(static_cast(o[QStringLiteral("lastSeeAt")].toDouble())); const QJsonArray mutedArray = o.value(QLatin1String("mutedUsers")).toArray(); QStringList lst; lst.reserve(mutedArray.count()); for (int i = 0; i < mutedArray.count(); ++i) { lst <setMutedUsers(lst); const QJsonArray ignoredArray = o.value(QLatin1String("ignored")).toArray(); QStringList lstIgnored; lstIgnored.reserve(ignoredArray.count()); for (int i = 0; i < ignoredArray.count(); ++i) { lstIgnored <setIgnoredUsers(lstIgnored); const QJsonArray rolesArray = o.value(QLatin1String("roles")).toArray(); QStringList lstRoles; lstRoles.reserve(rolesArray.count()); for (int i = 0; i < rolesArray.count(); ++i) { lstRoles <setRoles(lstRoles); const QJsonObject notificationsObj = o.value(QLatin1String("notifications")).toObject(); const NotificationOptions notifications = NotificationOptions::fromJSon(notificationsObj); r->setNotificationOptions(notifications); return r; } QByteArray Room::serialize(Room *r, bool toBinary) { QJsonDocument d; QJsonObject o; //todo add timestamp o[QStringLiteral("rid")] = r->roomId(); o[QStringLiteral("t")] = r->channelType(); o[QStringLiteral("name")] = r->name(); o[QStringLiteral("roomCreatorUserName")] = r->roomOwnerUserName(); o[QStringLiteral("roomCreatorUserID")] = r->roomCreatorUserId(); if (!r->topic().isEmpty()) { o[QStringLiteral("topic")] = r->topic(); } o[QStringLiteral("jitsiTimeout")] = r->jitsiTimeout(); o[QStringLiteral("updatedAt")] = r->updatedAt(); o[QStringLiteral("lastSeeAt")] = r->lastSeeAt(); o[QStringLiteral("ro")] = r->readOnly(); o[QStringLiteral("unread")] = r->unread(); if (!r->announcement().isEmpty()) { o[QStringLiteral("announcement")] = r->announcement(); } o[QStringLiteral("selected")] = r->selected(); o[QStringLiteral("favorite")] = r->favorite(); o[QStringLiteral("alert")] = r->alert(); o[QStringLiteral("open")] = r->open(); o[QStringLiteral("blocker")] = r->blocker(); o[QStringLiteral("blocked")] = r->blocked(); o[QStringLiteral("encrypted")] = r->encrypted(); o[QStringLiteral("archived")] = r->archived(); if (r->joinCodeRequired()) { o[QStringLiteral("joinCodeRequired")] = true; } if (!r->e2EKey().isEmpty()) { o[QStringLiteral("e2ekey")] = r->e2EKey(); } if (!r->e2eKeyId().isEmpty()) { o[QStringLiteral("e2ekeyid")] = r->e2eKeyId(); } if (!r->description().isEmpty()) { o[QStringLiteral("description")] = r->description(); } o[QStringLiteral("userMentions")] = r->userMentions(); if (!r->mutedUsers().isEmpty()) { QJsonArray array; const int nbMuted = r->mutedUsers().count(); for (int i = 0; i < nbMuted; ++i) { array.append(r->mutedUsers().at(i)); } o[QStringLiteral("mutedUsers")] = array; } if (!r->ignoredUsers().isEmpty()) { QJsonArray array; const int nbIgnoredUsers = r->ignoredUsers().count(); for (int i = 0; i < nbIgnoredUsers; ++i) { array.append(r->ignoredUsers().at(i)); } o[QStringLiteral("ignored")] = array; } if (!r->roles().isEmpty()) { QJsonArray array; const int nbRoles = r->roles().count(); for (int i = 0; i < nbRoles; ++i) { array.append(r->roles().at(i)); } o[QStringLiteral("roles")] = array; } o[QStringLiteral("notifications")] = NotificationOptions::serialize(r->notificationOptions()); d.setObject(o); if (toBinary) { return d.toBinaryData(); } return d.toJson(QJsonDocument::Indented); } UsersForRoomModel *Room::usersModelForRoom() const { return mUsersModelForRoom; } UsersForRoomFilterProxyModel *Room::usersModelForRoomProxyModel() const { return mUsersModelForRoomProxyModel; } FilesForRoomModel *Room::filesModelForRoom() const { return mFilesModelForRoom; } FilesForRoomFilterProxyModel *Room::filesForRoomFilterProxyModel() const { return mFilesForRoomFilterProxyModel; } MessageModel *Room::messageModel() const { return mMessageModel; } QString Room::inputMessage() const { return mInputMessage; } void Room::setInputMessage(const QString &inputMessage) { mInputMessage = inputMessage; } bool Room::archived() const { return mArchived; } void Room::setArchived(bool archived) { if (mArchived != archived) { mArchived = archived; Q_EMIT archivedChanged(); } } QString Room::description() const { return mDescription; } void Room::setDescription(const QString &description) { if (mDescription != description) { mDescription = description; Q_EMIT descriptionChanged(); } } bool Room::encryptedEnabled() const { if (mRocketChatAccount) { return mRocketChatAccount->encryptedEnabled(); } return false; } bool Room::userIsIgnored(const QString &userId) { return mIgnoredUsers.contains(userId); } QString Room::roomMessageInfo() const { if (mReadOnly) { return i18n("Channel is read only."); } if (mBlocker) { return i18n("You have blocked this channel."); } if (mBlocked) { return i18n("Channel was blocked."); } return QString(); }