File indexing completed on 2024-05-12 09:02:09

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 "chatbarcache.h"
0005 
0006 #include "eventhandler.h"
0007 #include "neochatroom.h"
0008 
0009 ChatBarCache::ChatBarCache(QObject *parent)
0010     : QObject(parent)
0011 {
0012 }
0013 
0014 QString ChatBarCache::text() const
0015 {
0016     return m_text;
0017 }
0018 
0019 void ChatBarCache::setText(const QString &text)
0020 {
0021     if (text == m_text) {
0022         return;
0023     }
0024     m_text = text;
0025     Q_EMIT textChanged();
0026 }
0027 
0028 bool ChatBarCache::isReplying() const
0029 {
0030     return m_relationType == Reply && !m_relationId.isEmpty();
0031 }
0032 
0033 QString ChatBarCache::replyId() const
0034 {
0035     if (m_relationType != Reply) {
0036         return {};
0037     }
0038     return m_relationId;
0039 }
0040 
0041 void ChatBarCache::setReplyId(const QString &replyId)
0042 {
0043     if (m_relationType == Reply && m_relationId == replyId) {
0044         return;
0045     }
0046     m_relationId = replyId;
0047     if (m_relationId.isEmpty()) {
0048         m_relationType = None;
0049     } else {
0050         m_relationType = Reply;
0051     }
0052     m_attachmentPath = QString();
0053     Q_EMIT relationIdChanged();
0054     Q_EMIT attachmentPathChanged();
0055 }
0056 
0057 bool ChatBarCache::isEditing() const
0058 {
0059     return m_relationType == Edit && !m_relationId.isEmpty();
0060 }
0061 
0062 QString ChatBarCache::editId() const
0063 {
0064     if (m_relationType != Edit) {
0065         return {};
0066     }
0067     return m_relationId;
0068 }
0069 
0070 void ChatBarCache::setEditId(const QString &editId)
0071 {
0072     if (m_relationType == Edit && m_relationId == editId) {
0073         return;
0074     }
0075     m_relationId = editId;
0076     if (m_relationId.isEmpty()) {
0077         m_relationType = None;
0078     } else {
0079         m_relationType = Edit;
0080     }
0081     m_attachmentPath = QString();
0082     Q_EMIT relationIdChanged();
0083     Q_EMIT attachmentPathChanged();
0084 }
0085 
0086 QVariantMap ChatBarCache::relationUser() const
0087 {
0088     if (parent() == nullptr) {
0089         qWarning() << "ChatBarCache created with no parent, a NeoChatRoom must be set as the parent on creation.";
0090         return {};
0091     }
0092     auto room = dynamic_cast<NeoChatRoom *>(parent());
0093     if (room == nullptr) {
0094         qWarning() << "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation.";
0095         return {};
0096     }
0097     if (m_relationId.isEmpty()) {
0098         return room->getUser(nullptr);
0099     }
0100     return room->getUser(room->user((*room->findInTimeline(m_relationId))->senderId()));
0101 }
0102 
0103 QString ChatBarCache::relationMessage() const
0104 {
0105     if (parent() == nullptr) {
0106         qWarning() << "ChatBarCache created with no parent, a NeoChatRoom must be set as the parent on creation.";
0107         return {};
0108     }
0109     if (m_relationId.isEmpty()) {
0110         return {};
0111     }
0112     auto room = dynamic_cast<NeoChatRoom *>(parent());
0113     if (room == nullptr) {
0114         qWarning() << "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation.";
0115         return {};
0116     }
0117     EventHandler eventhandler;
0118     eventhandler.setRoom(room);
0119     if (auto event = room->findInTimeline(m_relationId); event != room->historyEdge()) {
0120         eventhandler.setEvent(&**event);
0121         return eventhandler.getPlainBody();
0122     }
0123     return {};
0124 }
0125 
0126 bool ChatBarCache::isThreaded() const
0127 {
0128     return !m_threadId.isEmpty();
0129 }
0130 
0131 QString ChatBarCache::threadId() const
0132 {
0133     return m_threadId;
0134 }
0135 
0136 void ChatBarCache::setThreadId(const QString &threadId)
0137 {
0138     if (m_threadId == threadId) {
0139         return;
0140     }
0141     m_threadId = threadId;
0142     Q_EMIT threadIdChanged();
0143 }
0144 
0145 QString ChatBarCache::attachmentPath() const
0146 {
0147     return m_attachmentPath;
0148 }
0149 
0150 void ChatBarCache::setAttachmentPath(const QString &attachmentPath)
0151 {
0152     if (attachmentPath == m_attachmentPath) {
0153         return;
0154     }
0155     m_attachmentPath = attachmentPath;
0156     m_relationType = None;
0157     m_relationId = QString();
0158     Q_EMIT attachmentPathChanged();
0159     Q_EMIT relationIdChanged();
0160 }
0161 
0162 QList<Mention> *ChatBarCache::mentions()
0163 {
0164     return &m_mentions;
0165 }
0166 
0167 QString ChatBarCache::savedText() const
0168 {
0169     return m_savedText;
0170 }
0171 
0172 void ChatBarCache::setSavedText(const QString &savedText)
0173 {
0174     m_savedText = savedText;
0175 }
0176 
0177 #include "moc_chatbarcache.cpp"