File indexing completed on 2024-05-26 05:04:26

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "discussions/discussion.h"
0008 #include "utils.h"
0009 #include <KLocalizedString>
0010 #include <QDateTime>
0011 #include <QJsonObject>
0012 
0013 Discussion::Discussion() = default;
0014 
0015 QString Discussion::description() const
0016 {
0017     return mDescription;
0018 }
0019 
0020 void Discussion::setDescription(const QString &description)
0021 {
0022     mDescription = description;
0023 }
0024 
0025 QString Discussion::parentRoomId() const
0026 {
0027     return mParentRoomId;
0028 }
0029 
0030 void Discussion::setParentRoomId(const QString &parentRoomId)
0031 {
0032     mParentRoomId = parentRoomId;
0033 }
0034 
0035 int Discussion::numberMessages() const
0036 {
0037     return mNumberMessages;
0038 }
0039 
0040 void Discussion::setNumberMessages(int numberMessages)
0041 {
0042     mNumberMessages = numberMessages;
0043 }
0044 
0045 qint64 Discussion::lastMessage() const
0046 {
0047     return mLastMessage;
0048 }
0049 
0050 void Discussion::setLastMessage(qint64 lastMessage)
0051 {
0052     mLastMessage = lastMessage;
0053     QLocale l;
0054     mLastMessageDateTimeStr = l.toString(QDateTime::fromMSecsSinceEpoch(mLastMessage), QLocale::LongFormat);
0055 }
0056 
0057 QString Discussion::timeStampDisplay() const
0058 {
0059     return mTimeStampDateTimeStr;
0060 }
0061 
0062 QString Discussion::fname() const
0063 {
0064     return mFname;
0065 }
0066 
0067 void Discussion::setFname(const QString &fname)
0068 {
0069     mFname = fname;
0070 }
0071 
0072 const QString &Discussion::userName() const
0073 {
0074     return mUserName;
0075 }
0076 
0077 void Discussion::setUserName(const QString &newUserName)
0078 {
0079     mUserName = newUserName;
0080 }
0081 
0082 QString Discussion::lastMessageDisplay() const
0083 {
0084     return i18n("(Last Message: %1)", mLastMessageDateTimeStr);
0085 }
0086 
0087 qint64 Discussion::timeStamp() const
0088 {
0089     return mTimeStamp;
0090 }
0091 
0092 void Discussion::setTimeStamp(qint64 timeStamp)
0093 {
0094     mTimeStamp = timeStamp;
0095     QLocale l;
0096     mTimeStampDateTimeStr = l.toString(QDateTime::fromMSecsSinceEpoch(mTimeStamp), QLocale::LongFormat);
0097 }
0098 
0099 QDebug operator<<(QDebug d, const Discussion &t)
0100 {
0101     d.space() << "Parent Id" << t.parentRoomId();
0102     d.space() << "Last Message" << t.lastMessage();
0103     d.space() << "Number of Messages" << t.numberMessages();
0104     d.space() << "Description" << t.description();
0105     d.space() << "Discussion Room Id" << t.discussionRoomId();
0106     d.space() << "timestamp" << t.timeStamp();
0107     d.space() << "fname" << t.fname();
0108     d.space() << "mUserName" << t.userName();
0109     return d;
0110 }
0111 
0112 bool Discussion::operator==(const Discussion &other) const
0113 {
0114     return (description() == other.description()) && (parentRoomId() == other.parentRoomId()) && (numberMessages() == other.numberMessages())
0115         && (lastMessage() == other.lastMessage()) && (discussionRoomId() == other.discussionRoomId()) && (timeStamp() == other.timeStamp())
0116         && (fname() == other.fname()) && (userName() == other.userName());
0117 }
0118 
0119 void Discussion::parseDiscussion(const QJsonObject &o)
0120 {
0121     mParentRoomId = o.value(QLatin1String("prid")).toString();
0122     mDescription = o.value(QLatin1String("description")).toString();
0123     mFname = o.value(QLatin1String("fname")).toString();
0124     mNumberMessages = o.value(QLatin1String("msgs")).toInt();
0125     mDiscussionRoomId = o.value(QLatin1String("_id")).toString();
0126     setLastMessage(Utils::parseIsoDate(QStringLiteral("lm"), o));
0127     setTimeStamp(Utils::parseIsoDate(QStringLiteral("ts"), o));
0128     const QJsonValue ownerValue = o.value(QLatin1String("u"));
0129     if (!ownerValue.isUndefined()) {
0130         const QJsonObject objOwner = ownerValue.toObject();
0131         mUserName = objOwner.value(QLatin1String("username")).toString();
0132     }
0133 }
0134 
0135 QString Discussion::discussionRoomId() const
0136 {
0137     return mDiscussionRoomId;
0138 }
0139 
0140 void Discussion::setDiscussionRoomId(const QString &discussionRoomId)
0141 {
0142     mDiscussionRoomId = discussionRoomId;
0143 }
0144 
0145 #include "moc_discussion.cpp"