File indexing completed on 2024-10-13 04:29:56

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "emoticons/customemoji.h"
0008 #include "utils.h"
0009 
0010 #include <QJsonArray>
0011 #include <QJsonObject>
0012 
0013 CustomEmoji::CustomEmoji() = default;
0014 
0015 CustomEmoji::~CustomEmoji() = default;
0016 
0017 bool CustomEmoji::hasEmoji(const QString &identifier) const
0018 {
0019     return (mEmojiIdentifier == identifier) || mAliases.contains(identifier);
0020 }
0021 
0022 qint64 CustomEmoji::updatedAt() const
0023 {
0024     return mUpdatedAt;
0025 }
0026 
0027 void CustomEmoji::setUpdatedAt(qint64 updatedAt)
0028 {
0029     mUpdatedAt = updatedAt;
0030 }
0031 
0032 bool CustomEmoji::isAnimatedImage() const
0033 {
0034     return mExtension == QLatin1String("gif");
0035 }
0036 
0037 void CustomEmoji::parseEmoji(const QJsonObject &emoji, bool useIsoDate)
0038 {
0039     mIdentifier = emoji.value(QLatin1String("_id")).toString();
0040     mExtension = emoji.value(QLatin1String("extension")).toString();
0041     mName = emoji.value(QLatin1String("name")).toString();
0042     mEmojiIdentifier = QLatin1Char(':') + mName + QLatin1Char(':');
0043     if (useIsoDate) {
0044         mUpdatedAt = Utils::parseDate(QStringLiteral("_updatedAt"), emoji);
0045     } else {
0046         mUpdatedAt = Utils::parseIsoDate(QStringLiteral("_updatedAt"), emoji);
0047     }
0048     const QJsonArray array = emoji.value(QLatin1String("aliases")).toArray();
0049     const auto arrayCount = array.count();
0050     QStringList lst;
0051     lst.reserve(arrayCount);
0052     for (auto i = 0; i < arrayCount; ++i) {
0053         lst.append(QLatin1Char(':') + array.at(i).toString() + QLatin1Char(':'));
0054     }
0055     mAliases = lst;
0056 }
0057 
0058 bool CustomEmoji::isValid() const
0059 {
0060     // Add more check ?
0061     return !mIdentifier.isEmpty() && !mName.isEmpty();
0062 }
0063 
0064 QString CustomEmoji::emojiUrl(const QString &serverUrl) const
0065 {
0066     QString url = serverUrl + emojiFileName();
0067     // ???? http ? not https ???
0068     if (!url.startsWith(QLatin1String("http://")) && !url.startsWith(QLatin1String("https://"))) {
0069         url.prepend(QLatin1String("http://"));
0070     }
0071     return url;
0072 }
0073 
0074 QString CustomEmoji::emojiFileName() const
0075 {
0076     return QStringLiteral("/emoji-custom/%1.%2").arg(mName, mExtension);
0077 }
0078 
0079 QString CustomEmoji::generateAnimatedUrlFromCustomEmoji(const QString &serverUrl) const
0080 {
0081     if (mCachedHtml.isEmpty()) {
0082         const QString url = emojiUrl(serverUrl);
0083         // https://rocket.chat/docs/developer-guides/realtime-api/method-calls/list-custom-emoji/#list-custom-emoji
0084         // http://yourhost.com/emoji-custom/Emoji%20Name.png
0085         // TODO customize size.
0086         // Don't store url as cached html
0087         // mCachedHtml = url;
0088         return url;
0089     }
0090     return mCachedHtml;
0091 }
0092 
0093 QString CustomEmoji::generateHtmlFromCustomEmojiLocalPath(const QString &emojiLocalPath) const
0094 {
0095     if (mCachedHtml.isEmpty()) {
0096         mCachedHtml = QStringLiteral("<img height='22' width='22' src='%1' title='%2'/>").arg(emojiLocalPath, mEmojiIdentifier);
0097     }
0098     return mCachedHtml;
0099 }
0100 
0101 void CustomEmoji::clearCachedHtml()
0102 {
0103     mCachedHtml.clear();
0104 }
0105 
0106 QStringList CustomEmoji::aliases() const
0107 {
0108     return mAliases;
0109 }
0110 
0111 void CustomEmoji::setAliases(const QStringList &aliases)
0112 {
0113     mAliases = aliases;
0114 }
0115 
0116 QString CustomEmoji::emojiIdentifier() const
0117 {
0118     return mEmojiIdentifier;
0119 }
0120 
0121 void CustomEmoji::setEmojiIdentifier(const QString &emojiIdentifier)
0122 {
0123     mEmojiIdentifier = emojiIdentifier;
0124 }
0125 
0126 QString CustomEmoji::cachedHtml() const
0127 {
0128     return mCachedHtml;
0129 }
0130 
0131 QString CustomEmoji::identifier() const
0132 {
0133     return mIdentifier;
0134 }
0135 
0136 void CustomEmoji::setIdentifier(const QString &identifier)
0137 {
0138     mIdentifier = identifier;
0139 }
0140 
0141 QString CustomEmoji::extension() const
0142 {
0143     return mExtension;
0144 }
0145 
0146 void CustomEmoji::setExtension(const QString &extension)
0147 {
0148     mExtension = extension;
0149 }
0150 
0151 void CustomEmoji::setName(const QString &name)
0152 {
0153     mName = name;
0154 }
0155 
0156 QString CustomEmoji::name() const
0157 {
0158     return mName;
0159 }
0160 
0161 bool CustomEmoji::operator==(const CustomEmoji &other) const
0162 {
0163     return (mName == other.name()) && (mExtension == other.extension()) && (mIdentifier == other.identifier()) && (mAliases == other.aliases())
0164         && (mEmojiIdentifier == other.emojiIdentifier()) && (mUpdatedAt == other.updatedAt());
0165 }
0166 
0167 QDebug operator<<(QDebug d, const CustomEmoji &t)
0168 {
0169     d.space() << "Name:" << t.name();
0170     d.space() << "Identifier:" << t.identifier();
0171     d.space() << "extension:" << t.extension();
0172     d.space() << "aliases:" << t.aliases();
0173     d.space() << "UpdatedAt:" << t.updatedAt();
0174     d.space() << "EmojiIdentifier:" << t.emojiIdentifier();
0175     return d;
0176 }
0177 
0178 #include "moc_customemoji.cpp"