File indexing completed on 2024-02-25 16:44:33

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "reaction.h"
0008 #include "emoticons/emojimanager.h"
0009 #include <KLocalizedString>
0010 Reaction::Reaction() = default;
0011 
0012 QString Reaction::convertedReactionName() const
0013 {
0014     return mCacheConvertedReactionName;
0015 }
0016 
0017 QString Reaction::convertedUsersNameAtToolTip() const
0018 {
0019     if (mUserNames.isEmpty()) {
0020         return {};
0021     } else if (mUserNames.count() == 1) {
0022         return i18n("%1 reacted with %2", mUserNames[0], mReactionName);
0023     } else {
0024         QString notificationStr;
0025         for (int i = 0, total = mUserNames.count(); i < total; ++i) {
0026             const QString user = mUserNames.at(i);
0027             if (i == 0) {
0028                 notificationStr = user;
0029             } else if (i < (total - 1)) {
0030                 notificationStr = i18n("%1, %2", notificationStr, user);
0031             } else {
0032                 notificationStr = i18n("%1 and %2", notificationStr, user);
0033             }
0034         }
0035         return i18n("%1 reacted with %2", notificationStr, mReactionName);
0036     }
0037 }
0038 
0039 bool Reaction::isAnimatedImage() const
0040 {
0041     return mIsAnimatedImage;
0042 }
0043 
0044 void Reaction::setIsAnimatedImage(bool isAnimatedImage)
0045 {
0046     mIsAnimatedImage = isAnimatedImage;
0047 }
0048 
0049 QString Reaction::reactionName() const
0050 {
0051     return mReactionName;
0052 }
0053 
0054 void Reaction::setReactionName(const QString &reactionName, EmojiManager *emojiManager)
0055 {
0056     if (mReactionName != reactionName) {
0057         mReactionName = reactionName;
0058         if (emojiManager) {
0059             mCacheConvertedReactionName = emojiManager->replaceEmojiIdentifier(mReactionName, true);
0060             mIsAnimatedImage = emojiManager->isAnimatedImage(mReactionName);
0061         }
0062     }
0063 }
0064 
0065 QStringList Reaction::userNames() const
0066 {
0067     return mUserNames;
0068 }
0069 
0070 void Reaction::setUserNames(const QStringList &userNames)
0071 {
0072     mUserNames = userNames;
0073 }
0074 
0075 int Reaction::count() const
0076 {
0077     return mUserNames.count();
0078 }
0079 
0080 bool Reaction::operator==(const Reaction &other) const
0081 {
0082     return (mUserNames == other.userNames()) && (mReactionName == other.reactionName());
0083 }
0084 
0085 QDebug operator<<(QDebug d, const Reaction &t)
0086 {
0087     d.space() << "ReactionName" << t.reactionName();
0088     d.space() << "UserNames" << t.userNames();
0089     return d;
0090 }
0091 
0092 #include "moc_reaction.cpp"