KTextAddons

unicodeemoticonparser.cpp
1/*
2 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "unicodeemoticonparser.h"
8#include "textemoticonscore_debug.h"
9#include <QJsonArray>
10#include <QJsonObject>
11
12#include <algorithm>
13using namespace TextEmoticonsCore;
14using namespace Qt::Literals::StringLiterals;
15UnicodeEmoticonParser::UnicodeEmoticonParser() = default;
16
17UnicodeEmoticonParser::~UnicodeEmoticonParser() = default;
18
19QList<UnicodeEmoticon> UnicodeEmoticonParser::parse(const QJsonObject &o) const
20{
21 auto aliases = [](const QJsonArray &alternates, const QJsonArray &ascii) {
22 QStringList ret;
23 ret.reserve(alternates.size() + ascii.size());
24 auto convert = [&](const QJsonArray &array) {
25 auto toString = [](const QJsonValue &value) {
26 return value.toString();
27 };
28 std::transform(array.begin(), array.end(), std::back_inserter(ret), toString);
29 };
30 convert(alternates);
31 convert(ascii);
32 return ret;
33 };
34
35 QList<UnicodeEmoticon> lstEmoticons;
36 const QStringList keys = o.keys();
37 for (const QString &key : keys) {
38 UnicodeEmoticon emoticon;
39 const QJsonObject emojiObj = o[key].toObject();
40 emoticon.setKey(key);
41 const QString unicodeStr = emojiObj["code_points"_L1].toObject()["fully_qualified"_L1].toString();
42 Q_ASSERT(!unicodeStr.isEmpty());
43 emoticon.setUnicode(unicodeStr);
44 const QString category = emojiObj["category"_L1].toString();
45 emoticon.setCategory(category);
46 emoticon.setIdentifier(emojiObj["shortname"_L1].toString());
47 emoticon.setOrder(emojiObj["order"_L1].toInt());
48 const auto shortnameAlternates = emojiObj["shortname_alternates"_L1].toArray();
49 const auto ascii = emojiObj["ascii"_L1].toArray();
50 emoticon.setAliases(aliases(shortnameAlternates, ascii));
51 if (emoticon.isValid()) {
52 lstEmoticons.append(std::move(emoticon));
53 }
54 }
55 auto compareOrder = [](const UnicodeEmoticon &left, const UnicodeEmoticon &right) {
56 return left.order() < right.order();
57 };
58 std::sort(lstEmoticons.begin(), lstEmoticons.end(), compareOrder);
59 return lstEmoticons;
60}
61
62int UnicodeEmoticonParser::changeOrder(const QString &name)
63{
64 // ame "🚗"Category "travel", Name "🇿"Category "regional", Name "🏳️"Category "flags")
65 if (name == "people"_L1) {
66 return 1;
67 } else if (name == "flags"_L1) {
68 return 2;
69 } else if (name == "travel"_L1) {
70 return 3;
71 } else if (name == "symbols"_L1) {
72 return 4;
73 } else if (name == "activity"_L1) {
74 return 5;
75 } else if (name == "objects"_L1) {
76 return 6;
77 } else if (name == "nature"_L1) {
78 return 7;
79 } else if (name == "food"_L1) {
80 return 8;
81 } else if (name == "regional"_L1) {
82 return 9;
83 } else {
84 qCWarning(TEXTEMOTICONSCORE_LOG) << "Missing i18n translate " << name;
85 }
86 return 20;
87}
The UnicodeEmoticon class.
char * toString(const EngineQuery &query)
QString name(GameStandardAction id)
T convert(const QVariant &value)
Category category(StandardShortcut id)
qsizetype size() const const
QStringList keys() const const
void append(QList< T > &&value)
iterator begin()
iterator end()
void reserve(qsizetype size)
bool isEmpty() const const
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:02:03 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.