File indexing completed on 2024-04-28 04:59:37

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 
0118     if (auto event = room->findInTimeline(m_relationId); event != room->historyEdge()) {
0119         EventHandler eventhandler(room, &**event);
0120         return eventhandler.getPlainBody();
0121     }
0122     return {};
0123 }
0124 
0125 bool ChatBarCache::isThreaded() const
0126 {
0127     return !m_threadId.isEmpty();
0128 }
0129 
0130 QString ChatBarCache::threadId() const
0131 {
0132     return m_threadId;
0133 }
0134 
0135 void ChatBarCache::setThreadId(const QString &threadId)
0136 {
0137     if (m_threadId == threadId) {
0138         return;
0139     }
0140     m_threadId = threadId;
0141     Q_EMIT threadIdChanged();
0142 }
0143 
0144 QString ChatBarCache::attachmentPath() const
0145 {
0146     return m_attachmentPath;
0147 }
0148 
0149 void ChatBarCache::setAttachmentPath(const QString &attachmentPath)
0150 {
0151     if (attachmentPath == m_attachmentPath) {
0152         return;
0153     }
0154     m_attachmentPath = attachmentPath;
0155     m_relationType = None;
0156     m_relationId = QString();
0157     Q_EMIT attachmentPathChanged();
0158     Q_EMIT relationIdChanged();
0159 }
0160 
0161 QList<Mention> *ChatBarCache::mentions()
0162 {
0163     return &m_mentions;
0164 }
0165 
0166 QString ChatBarCache::savedText() const
0167 {
0168     return m_savedText;
0169 }
0170 
0171 void ChatBarCache::setSavedText(const QString &savedText)
0172 {
0173     m_savedText = savedText;
0174 }
0175 
0176 #include "moc_chatbarcache.cpp"