File indexing completed on 2025-01-12 04:18:40

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "unicodeemoticonmanager.h"
0008 #include "textemoticonscore_debug.h"
0009 #include "unicodeemoticonparser.h"
0010 #include <KLocalizedString>
0011 
0012 #include <QFile>
0013 #include <QJsonDocument>
0014 #include <QJsonObject>
0015 using namespace TextEmoticonsCore;
0016 class TextEmoticonsCore::UnicodeEmoticonManagerPrivate
0017 {
0018 public:
0019     void loadUnicodeEmoji();
0020     [[nodiscard]] QString i18nUnicodeCategory(const QString &name) const;
0021     QList<UnicodeEmoticon> mUnicodeEmojiList;
0022 };
0023 
0024 void UnicodeEmoticonManagerPrivate::loadUnicodeEmoji()
0025 {
0026     UnicodeEmoticonParser unicodeParser;
0027     QFile file(QStringLiteral(":/emoji.json"));
0028     if (!file.open(QFile::ReadOnly)) {
0029         qCWarning(TEXTEMOTICONSCORE_LOG) << "Impossible to open file: " << file.errorString();
0030         return;
0031     }
0032     const QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
0033 
0034     const QJsonObject obj = doc.object();
0035     mUnicodeEmojiList = unicodeParser.parse(obj);
0036 }
0037 
0038 QString UnicodeEmoticonManagerPrivate::i18nUnicodeCategory(const QString &name) const
0039 {
0040     // Name "🚗"Category "travel", Name "🇿"Category "regional", Name "🏳️"Category "flags")
0041     if (name == QLatin1String("symbols")) {
0042         return i18n("Symbols");
0043     } else if (name == QLatin1String("activity")) {
0044         return i18n("Activity");
0045     } else if (name == QLatin1String("objects")) {
0046         return i18n("Objects");
0047     } else if (name == QLatin1String("nature")) {
0048         return i18n("Nature");
0049     } else if (name == QLatin1String("food")) {
0050         return i18n("Food");
0051     } else if (name == QLatin1String("people")) {
0052         return i18n("People");
0053     } else if (name == QLatin1String("travel")) {
0054         return i18n("Travel");
0055     } else if (name == QLatin1String("regional")) {
0056         return i18n("Regional");
0057     } else if (name == QLatin1String("flags")) {
0058         return i18n("Flags");
0059     } else {
0060         qCWarning(TEXTEMOTICONSCORE_LOG) << "Missing i18n translate " << name;
0061     }
0062     return {};
0063 }
0064 
0065 UnicodeEmoticonManager::UnicodeEmoticonManager(QObject *parent)
0066     : QObject(parent)
0067     , d(new TextEmoticonsCore::UnicodeEmoticonManagerPrivate)
0068 {
0069     d->loadUnicodeEmoji();
0070 }
0071 
0072 UnicodeEmoticonManager::~UnicodeEmoticonManager() = default;
0073 
0074 UnicodeEmoticonManager *UnicodeEmoticonManager::self()
0075 {
0076     static UnicodeEmoticonManager s_self;
0077     return &s_self;
0078 }
0079 
0080 QList<UnicodeEmoticon> UnicodeEmoticonManager::unicodeEmojiList() const
0081 {
0082     return d->mUnicodeEmojiList;
0083 }
0084 
0085 int UnicodeEmoticonManager::count() const
0086 {
0087     return d->mUnicodeEmojiList.count();
0088 }
0089 
0090 QList<EmoticonCategory> UnicodeEmoticonManager::categories() const
0091 {
0092     QList<EmoticonCategory> categories;
0093     QSet<QString> seen;
0094     for (const UnicodeEmoticon &emo : std::as_const(d->mUnicodeEmojiList)) {
0095         // Pick the first icon in each category
0096         const QString category = emo.category();
0097         if (!seen.contains(category)) {
0098             seen.insert(category);
0099             if (category == QLatin1String("modifier")) { // pointless icons
0100                 continue;
0101             }
0102             EmoticonCategory cat;
0103             cat.setCategory(category);
0104             cat.setName(emo.unicode());
0105             cat.setI18nName(d->i18nUnicodeCategory(category));
0106             cat.setOrder(UnicodeEmoticonParser::changeOrder(category));
0107             categories.append(std::move(cat));
0108         }
0109     }
0110     std::sort(categories.begin(), categories.end());
0111     return categories;
0112 }
0113 
0114 QList<UnicodeEmoticon> UnicodeEmoticonManager::emojisForCategory(const QString &category) const
0115 {
0116     QList<UnicodeEmoticon> result;
0117 
0118     auto hasRequestedCategory = [category](const UnicodeEmoticon &emo) {
0119         return emo.category() == category;
0120     };
0121     std::copy_if(d->mUnicodeEmojiList.begin(), d->mUnicodeEmojiList.end(), std::back_inserter(result), hasRequestedCategory);
0122     return result;
0123 }
0124 
0125 UnicodeEmoticon UnicodeEmoticonManager::unicodeEmoticonForEmoji(const QString &emojiIdentifier) const
0126 {
0127     for (const UnicodeEmoticon &emo : d->mUnicodeEmojiList) {
0128         if (emo.hasEmoji(emojiIdentifier)) {
0129             return emo;
0130         }
0131     }
0132     return {};
0133 }
0134 
0135 #include "moc_unicodeemoticonmanager.cpp"