KTextAddons

unicodeemoticonmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "unicodeemoticonmanager.h"
8using namespace Qt::Literals::StringLiterals;
9
10#include "textemoticonscore_debug.h"
11#include "unicodeemoticonparser.h"
12#include <KLocalizedString>
13
14#include <QFile>
15#include <QJsonDocument>
16#include <QJsonObject>
17using namespace TextEmoticonsCore;
18class TextEmoticonsCore::UnicodeEmoticonManagerPrivate
19{
20public:
21 void loadUnicodeEmoji();
22 [[nodiscard]] QString i18nUnicodeCategory(const QString &name) const;
23 QList<UnicodeEmoticon> mUnicodeEmojiList;
24};
25
26void UnicodeEmoticonManagerPrivate::loadUnicodeEmoji()
27{
28 UnicodeEmoticonParser unicodeParser;
29 QFile file(QStringLiteral(":/emoji.json"));
30 if (!file.open(QFile::ReadOnly)) {
31 qCWarning(TEXTEMOTICONSCORE_LOG) << "Impossible to open file: " << file.errorString();
32 return;
33 }
34 const QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
35
36 const QJsonObject obj = doc.object();
37 mUnicodeEmojiList = unicodeParser.parse(obj);
38}
39
40QString UnicodeEmoticonManagerPrivate::i18nUnicodeCategory(const QString &name) const
41{
42 // Name "🚗"Category "travel", Name "🇿"Category "regional", Name "🏳️"Category "flags")
43 if (name == "symbols"_L1) {
44 return i18n("Symbols");
45 } else if (name == "activity"_L1) {
46 return i18n("Activity");
47 } else if (name == "objects"_L1) {
48 return i18n("Objects");
49 } else if (name == "nature"_L1) {
50 return i18n("Nature");
51 } else if (name == "food"_L1) {
52 return i18n("Food");
53 } else if (name == "people"_L1) {
54 return i18n("People");
55 } else if (name == "travel"_L1) {
56 return i18n("Travel");
57 } else if (name == "regional"_L1) {
58 return i18n("Regional");
59 } else if (name == "flags"_L1) {
60 return i18n("Flags");
61 } else {
62 qCWarning(TEXTEMOTICONSCORE_LOG) << "Missing i18n translate " << name;
63 }
64 return {};
65}
66
67UnicodeEmoticonManager::UnicodeEmoticonManager(QObject *parent)
68 : QObject(parent)
69 , d(new TextEmoticonsCore::UnicodeEmoticonManagerPrivate)
70{
71 d->loadUnicodeEmoji();
72}
73
74UnicodeEmoticonManager::~UnicodeEmoticonManager() = default;
75
76UnicodeEmoticonManager *UnicodeEmoticonManager::self()
77{
78 static UnicodeEmoticonManager s_self;
79 return &s_self;
80}
81
82QList<UnicodeEmoticon> UnicodeEmoticonManager::unicodeEmojiList() const
83{
84 return d->mUnicodeEmojiList;
85}
86
87int UnicodeEmoticonManager::count() const
88{
89 return d->mUnicodeEmojiList.count();
90}
91
92QList<EmoticonCategory> UnicodeEmoticonManager::categories() const
93{
94 QList<EmoticonCategory> categories;
95 QSet<QString> seen;
96 for (const UnicodeEmoticon &emo : std::as_const(d->mUnicodeEmojiList)) {
97 // Pick the first icon in each category
98 const QString category = emo.category();
99 if (!seen.contains(category)) {
100 seen.insert(category);
101 if (category == "modifier"_L1) { // pointless icons
102 continue;
103 }
105 cat.setCategory(category);
106 cat.setName(emo.unicode());
107 cat.setI18nName(d->i18nUnicodeCategory(category));
108 cat.setOrder(UnicodeEmoticonParser::changeOrder(category));
109 categories.append(std::move(cat));
110 }
111 }
112 std::sort(categories.begin(), categories.end());
113 return categories;
114}
115
116QList<UnicodeEmoticon> UnicodeEmoticonManager::emojisForCategory(const QString &category) const
117{
119
120 auto hasRequestedCategory = [category](const UnicodeEmoticon &emo) {
121 return emo.category() == category;
122 };
123 std::copy_if(d->mUnicodeEmojiList.begin(), d->mUnicodeEmojiList.end(), std::back_inserter(result), hasRequestedCategory);
124 return result;
125}
126
127UnicodeEmoticon UnicodeEmoticonManager::unicodeEmoticonForEmoji(const QString &emojiIdentifier) const
128{
129 for (const UnicodeEmoticon &emo : d->mUnicodeEmojiList) {
130 if (emo.hasEmoji(emojiIdentifier)) {
131 return emo;
132 }
133 }
134 return {};
135}
136
137#include "moc_unicodeemoticonmanager.cpp"
The EmoticonCategory class.
The UnicodeEmoticon class.
QString i18n(const char *text, const TYPE &arg...)
QString name(GameStandardAction id)
Category category(StandardShortcut id)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonObject object() const const
void append(QList< T > &&value)
iterator begin()
iterator end()
bool contains(const QSet< T > &other) const const
iterator insert(const T &value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.