File indexing completed on 2024-12-08 07:33:47

0001 // SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #include "spacetreeitem.h"
0005 
0006 #include "neochatconnection.h"
0007 
0008 SpaceTreeItem::SpaceTreeItem(NeoChatConnection *connection,
0009                              SpaceTreeItem *parent,
0010                              const QString &id,
0011                              const QString &name,
0012                              const QString &canonicalAlias,
0013                              const QString &topic,
0014                              int memberCount,
0015                              const QUrl &avatarUrl,
0016                              bool allowGuests,
0017                              bool worldReadable,
0018                              bool isSpace,
0019                              Quotient::StateEvents childStates)
0020     : m_connection(connection)
0021     , m_parentItem(parent)
0022     , m_id(id)
0023     , m_name(name)
0024     , m_canonicalAlias(canonicalAlias)
0025     , m_topic(topic)
0026     , m_memberCount(memberCount)
0027     , m_avatarUrl(avatarUrl)
0028     , m_allowGuests(allowGuests)
0029     , m_worldReadable(worldReadable)
0030     , m_isSpace(isSpace)
0031     , m_childStates(std::move(childStates))
0032 {
0033 }
0034 
0035 SpaceTreeItem::~SpaceTreeItem()
0036 {
0037     qDeleteAll(m_children);
0038 }
0039 
0040 SpaceTreeItem *SpaceTreeItem::child(int number)
0041 {
0042     if (number < 0 || number >= m_children.size()) {
0043         return nullptr;
0044     }
0045     return m_children[number];
0046 }
0047 
0048 int SpaceTreeItem::childCount() const
0049 {
0050     return m_children.count();
0051 }
0052 
0053 bool SpaceTreeItem::insertChild(int row, SpaceTreeItem *newChild)
0054 {
0055     if (row < 0 || row > m_children.size()) {
0056         return false;
0057     }
0058     m_children.insert(row, newChild);
0059     return true;
0060 }
0061 
0062 bool SpaceTreeItem::removeChild(int row)
0063 {
0064     if (row < 0 || row >= m_children.size()) {
0065         return false;
0066     }
0067     delete m_children.takeAt(row);
0068     return true;
0069 }
0070 
0071 int SpaceTreeItem::row() const
0072 {
0073     if (m_parentItem) {
0074         return m_parentItem->m_children.indexOf(const_cast<SpaceTreeItem *>(this));
0075     }
0076     return 0;
0077 }
0078 
0079 SpaceTreeItem *SpaceTreeItem::parentItem() const
0080 {
0081     return m_parentItem;
0082 }
0083 
0084 QString SpaceTreeItem::id() const
0085 {
0086     return m_id;
0087 }
0088 
0089 QString SpaceTreeItem::name() const
0090 {
0091     return m_name;
0092 }
0093 
0094 QString SpaceTreeItem::canonicalAlias() const
0095 {
0096     return m_canonicalAlias;
0097 }
0098 
0099 QString SpaceTreeItem::topic() const
0100 {
0101     return m_topic;
0102 }
0103 
0104 int SpaceTreeItem::memberCount() const
0105 {
0106     return m_memberCount;
0107 }
0108 
0109 QUrl SpaceTreeItem::avatarUrl() const
0110 {
0111     if (m_avatarUrl.isEmpty() || m_avatarUrl.scheme() != QLatin1String("mxc")) {
0112         return {};
0113     }
0114     auto url = m_connection->makeMediaUrl(m_avatarUrl);
0115     if (url.scheme() == QLatin1String("mxc")) {
0116         return url;
0117     }
0118     return {};
0119 }
0120 
0121 bool SpaceTreeItem::allowGuests() const
0122 {
0123     return m_allowGuests;
0124 }
0125 
0126 bool SpaceTreeItem::worldReadable() const
0127 {
0128     return m_worldReadable;
0129 }
0130 
0131 bool SpaceTreeItem::isJoined() const
0132 {
0133     if (!m_connection) {
0134         return false;
0135     }
0136     return m_connection->room(id(), Quotient::JoinState::Join) != nullptr;
0137 }
0138 
0139 bool SpaceTreeItem::isSpace() const
0140 {
0141     return m_isSpace;
0142 }
0143 
0144 QJsonObject SpaceTreeItem::childStateContent(const SpaceTreeItem *child) const
0145 {
0146     if (child == nullptr) {
0147         return {};
0148     }
0149     if (child->parentItem() != this) {
0150         return {};
0151     }
0152     for (const auto &childState : m_childStates) {
0153         if (childState->stateKey() == child->id()) {
0154             return childState->contentJson();
0155         }
0156     }
0157     return {};
0158 }
0159 
0160 void SpaceTreeItem::setChildStates(Quotient::StateEvents childStates)
0161 {
0162     m_childStates.clear();
0163     m_childStates = std::move(childStates);
0164 }
0165 
0166 bool SpaceTreeItem::isSuggested() const
0167 {
0168     if (m_parentItem == nullptr) {
0169         return false;
0170     }
0171     const auto childStateContent = m_parentItem->childStateContent(this);
0172     return childStateContent.value(QLatin1String("suggested")).toBool();
0173 }