diff --git a/autotests/mentionstest.cpp b/autotests/mentionstest.cpp index 91fc819b..2162bf14 100644 --- a/autotests/mentionstest.cpp +++ b/autotests/mentionstest.cpp @@ -1,36 +1,37 @@ /* Copyright (c) 2019 Montel Laurent 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) any later version. 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; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "mentionstest.h" #include "mentions.h" #include QTEST_GUILESS_MAIN(MentionsTest) MentionsTest::MentionsTest(QObject *parent) : QObject(parent) { } void MentionsTest::shouldHaveDefaultValues() { Mentions m; QVERIFY(m.isEmpty()); QCOMPARE(m.count(), 0); QCOMPARE(m.offset(), 0); QCOMPARE(m.mentionsCount(), 0); + QCOMPARE(m.total(), 0); } diff --git a/src/ruqolacore/mentions.cpp b/src/ruqolacore/mentions.cpp index 2951f69c..e1890708 100644 --- a/src/ruqolacore/mentions.cpp +++ b/src/ruqolacore/mentions.cpp @@ -1,151 +1,164 @@ /* Copyright (c) 2019 Montel Laurent 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) any later version. 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; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "mentions.h" #include #include Mentions::Mentions() { } void Mentions::setMentions(const QVector &mentions) { mMentions = mentions; } void Mentions::clear() { mMentions.clear(); } int Mentions::count() const { return mMentions.count(); } int Mentions::mentionsCount() const { return mMentionsCount; } void Mentions::setMentionsCount(int mentionsCount) { mMentionsCount = mentionsCount; } int Mentions::offset() const { return mOffset; } void Mentions::setOffset(int offset) { mOffset = offset; } +int Mentions::total() const +{ + return mTotal; +} + +void Mentions::setTotal(int total) +{ + mTotal = total; +} + QVector Mentions::mentions() const { return mMentions; } void Mentions::parseMentions(const QJsonObject &mentions) { mMentions.clear(); + mMentionsCount = mentions[QStringLiteral("count")].toInt(); + mOffset = mentions[QStringLiteral("offset")].toInt(); + mTotal = mentions[QStringLiteral("total")].toInt(); // const QStringList lst = reacts.keys(); // QStringList users; // for (const QString &str : lst) { // users.clear(); // const QJsonObject obj = reacts.value(str).toObject(); // QJsonValue usernames = obj.value(QLatin1String("usernames")); // if (!usernames.isUndefined()) { // QJsonArray array = usernames.toArray(); // for (int i = 0; i < array.count(); ++i) { // users.append(array.at(i).toString()); // } // } // if (!users.isEmpty()) { // Mention r; // r.setMentionName(str); // r.setUserNames(users); // mMentions.append(r); // } // } } bool Mentions::operator ==(const Mentions &other) const { return mMentions == other.mentions(); } QDebug operator <<(QDebug d, const Mentions &t) { for (int i = 0, total = t.mentions().count(); i < total; ++i) { d << t.mentions().at(i); } return d; } QJsonObject Mentions::serialize(const Mentions &mentions) { QJsonObject obj; // for (int i = 0; i < mentions.mentions().count(); ++i) { // QJsonObject react; // react[QLatin1String("usernames")] = QJsonArray::fromStringList(mentions.mentions().at(i).userNames()); // obj[mentions.mentions().at(i).mentionName()] = react; // } return obj; } Mentions Mentions::fromJSon(const QJsonObject &o) { // QVector reacts; // const QStringList lst = o.keys(); // QStringList users; // for (const QString &str : lst) { // const QJsonObject obj = o.value(str).toObject(); // QJsonValue usernames = obj.value(QLatin1String("usernames")); // if (!usernames.isUndefined()) { // QJsonArray array = usernames.toArray(); // for (int i = 0; i < array.count(); ++i) { // users.append(array.at(i).toString()); // } // } // if (!users.isEmpty()) { // Mention r; // r.setMentionName(str); // r.setUserNames(users); // reacts.append(r); // } // users.clear(); // } // Mentions final; // final.setMentions(reacts); // return final; return {}; } bool Mentions::isEmpty() const { return mMentions.isEmpty(); } Mention Mentions::at(int index) const { return mMentions.at(index); } diff --git a/src/ruqolacore/mentions.h b/src/ruqolacore/mentions.h index 9b76536f..4c92c7f0 100644 --- a/src/ruqolacore/mentions.h +++ b/src/ruqolacore/mentions.h @@ -1,61 +1,65 @@ /* Copyright (c) 2019 Montel Laurent 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) any later version. 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; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef MENTIONS_H #define MENTIONS_H #include "libruqola_private_export.h" #include "mention.h" #include class LIBRUQOLACORE_TESTS_EXPORT Mentions { public: Mentions(); void setMentions(const QVector &mentions); Q_REQUIRED_RESULT QVector mentions() const; void parseMentions(const QJsonObject &array); Q_REQUIRED_RESULT bool operator ==(const Mentions &other) const; Q_REQUIRED_RESULT static QJsonObject serialize(const Mentions &mentions); Q_REQUIRED_RESULT static Mentions fromJSon(const QJsonObject &o); Q_REQUIRED_RESULT bool isEmpty() const; Mention at(int index) const; void clear(); Q_REQUIRED_RESULT int count() const; Q_REQUIRED_RESULT int mentionsCount() const; void setMentionsCount(int mentionsCount); Q_REQUIRED_RESULT int offset() const; void setOffset(int offset); + Q_REQUIRED_RESULT int total() const; + void setTotal(int total); + private: QVector mMentions; int mMentionsCount = 0; int mOffset = 0; + int mTotal = 0; }; Q_DECLARE_METATYPE(Mentions) LIBRUQOLACORE_EXPORT QDebug operator <<(QDebug d, const Mentions &t); #endif // MENTIONS_H