diff --git a/autotests/reactiontest.cpp b/autotests/reactiontest.cpp index cde034ec..1f1f530a 100644 --- a/autotests/reactiontest.cpp +++ b/autotests/reactiontest.cpp @@ -1,46 +1,63 @@ /* Copyright (c) 2018-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 "reactiontest.h" #include "messages/reaction.h" #include QTEST_GUILESS_MAIN(ReactionTest) ReactionTest::ReactionTest(QObject *parent) : QObject(parent) { } void ReactionTest::shouldHaveDefaultValue() { Reaction r; QVERIFY(r.userNames().isEmpty()); QVERIFY(r.reactionName().isEmpty()); QCOMPARE(r.count(), 0); } void ReactionTest::shouldReturnCount() { Reaction r; r.setReactionName(QStringLiteral("bla")); QCOMPARE(r.reactionName(), QStringLiteral("bla")); r.setUserNames({QStringLiteral("dd"), QStringLiteral("dd2")}); QCOMPARE(r.count(), 2); } + +void ReactionTest::shouldShowReactionsToolTip() +{ + Reaction r; + r.setReactionName(QStringLiteral(":foo:")); + QCOMPARE(r.convertedUsersNameAtToolTip(), QString()); + QStringList userNames; + userNames.append(QStringLiteral("bla")); + r.setUserNames(userNames); + QCOMPARE(r.convertedUsersNameAtToolTip(), QStringLiteral("bla had reacted with :foo:")); + userNames.append(QStringLiteral("blo")); + r.setUserNames(userNames); + QCOMPARE(r.convertedUsersNameAtToolTip(), QStringLiteral("bla and blo had reacted with :foo:")); + userNames.append(QStringLiteral("bli")); + r.setUserNames(userNames); + QCOMPARE(r.convertedUsersNameAtToolTip(), QStringLiteral("bla, blo and bli had reacted with :foo:")); +} diff --git a/autotests/reactiontest.h b/autotests/reactiontest.h index 2799a100..f6ee942f 100644 --- a/autotests/reactiontest.h +++ b/autotests/reactiontest.h @@ -1,36 +1,37 @@ /* Copyright (c) 2018-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 REACTIONTEST_H #define REACTIONTEST_H #include class ReactionTest : public QObject { Q_OBJECT public: explicit ReactionTest(QObject *parent = nullptr); ~ReactionTest() = default; private Q_SLOTS: void shouldHaveDefaultValue(); void shouldReturnCount(); + void shouldShowReactionsToolTip(); }; #endif // REACTIONTEST_H diff --git a/src/ruqolacore/messages/reaction.cpp b/src/ruqolacore/messages/reaction.cpp index c3208c66..e1ac1386 100644 --- a/src/ruqolacore/messages/reaction.cpp +++ b/src/ruqolacore/messages/reaction.cpp @@ -1,86 +1,98 @@ /* Copyright (c) 2018-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 "reaction.h" #include "emoticons/emojimanager.h" #include #include Reaction::Reaction() { } QString Reaction::convertedReactionName() const { return mCacheConvertedReactionName; } QString Reaction::convertedUsersNameAtToolTip() const { - if (mUserNames.count() == 1) { - return i18n("%1 has reacted with %2", mUserNames.at(0), mReactionName); + if (mUserNames.isEmpty()) { + return QString(); + } else if (mUserNames.count() == 1) { + return i18n("%1 had reacted with %2", mUserNames[0], mReactionName); } else { - const QString users = mUserNames.join(QLatin1Char(',')); - return i18n("%1 has reacted with %2", users, mReactionName); + QString notificationStr; + for (int i = 0; i < mUserNames.count(); ++i) { + const QString user = mUserNames.at(i); + if (i == 0) { + notificationStr = user; + } else if (i < mUserNames.count() - 1) { + notificationStr = i18n("%1, %2", notificationStr, user); + } else { + notificationStr = i18n("%1 and %2", notificationStr, user); + } + } + return i18n("%1 had reacted with %2", notificationStr, mReactionName); } } QString Reaction::reactionName() const { return mReactionName; } void Reaction::setReactionName(const QString &reactionName, EmojiManager *emojiManager) { if (mReactionName != reactionName) { mReactionName = reactionName; if (emojiManager) { mCacheConvertedReactionName = emojiManager->replaceEmojiIdentifier(mReactionName); } else { const KTextToHTML::Options convertFlags = KTextToHTML::ReplaceSmileys; mCacheConvertedReactionName = KTextToHTML::convertToHtml(mReactionName, convertFlags); } } } QStringList Reaction::userNames() const { return mUserNames; } void Reaction::setUserNames(const QStringList &userNames) { mUserNames = userNames; } int Reaction::count() const { return mUserNames.count(); } bool Reaction::operator ==(const Reaction &other) const { return (mUserNames == other.userNames()) && (mReactionName == other.reactionName()); } QDebug operator <<(QDebug d, const Reaction &t) { d << "ReactionName " << t.reactionName(); d << "UserNames " << t.userNames(); return d; }