File indexing completed on 2024-12-22 04:28:17

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "unicodeemoticonparser.h"
0008 #include "textemoticonscore_debug.h"
0009 #include <QJsonArray>
0010 #include <QJsonObject>
0011 
0012 #include <algorithm>
0013 using namespace TextEmoticonsCore;
0014 UnicodeEmoticonParser::UnicodeEmoticonParser() = default;
0015 
0016 UnicodeEmoticonParser::~UnicodeEmoticonParser() = default;
0017 
0018 QList<UnicodeEmoticon> UnicodeEmoticonParser::parse(const QJsonObject &o) const
0019 {
0020     auto aliases = [](const QJsonArray &alternates, const QJsonArray &ascii) {
0021         QStringList ret;
0022         ret.reserve(alternates.size() + ascii.size());
0023         auto convert = [&](const QJsonArray &array) {
0024             auto toString = [](const QJsonValue &value) {
0025                 return value.toString();
0026             };
0027             std::transform(array.begin(), array.end(), std::back_inserter(ret), toString);
0028         };
0029         convert(alternates);
0030         convert(ascii);
0031         return ret;
0032     };
0033 
0034     QList<UnicodeEmoticon> lstEmoticons;
0035     const QStringList keys = o.keys();
0036     for (const QString &key : keys) {
0037         UnicodeEmoticon emoticon;
0038         const QJsonObject emojiObj = o[key].toObject();
0039         emoticon.setKey(key);
0040         const QString unicodeStr = emojiObj[QLatin1String("code_points")].toObject()[QLatin1String("fully_qualified")].toString();
0041         Q_ASSERT(!unicodeStr.isEmpty());
0042         emoticon.setUnicode(unicodeStr);
0043         const QString category = emojiObj[QLatin1String("category")].toString();
0044         emoticon.setCategory(category);
0045         emoticon.setIdentifier(emojiObj[QLatin1String("shortname")].toString());
0046         emoticon.setOrder(emojiObj[QLatin1String("order")].toInt());
0047         const auto shortnameAlternates = emojiObj[QLatin1String("shortname_alternates")].toArray();
0048         const auto ascii = emojiObj[QLatin1String("ascii")].toArray();
0049         emoticon.setAliases(aliases(shortnameAlternates, ascii));
0050         if (emoticon.isValid()) {
0051             lstEmoticons.append(std::move(emoticon));
0052         }
0053     }
0054     auto compareOrder = [](const UnicodeEmoticon &left, const UnicodeEmoticon &right) {
0055         return left.order() < right.order();
0056     };
0057     std::sort(lstEmoticons.begin(), lstEmoticons.end(), compareOrder);
0058     return lstEmoticons;
0059 }
0060 
0061 int UnicodeEmoticonParser::changeOrder(const QString &name)
0062 {
0063     // ame "🚗"Category "travel", Name "🇿"Category "regional", Name "🏳️"Category "flags")
0064     if (name == QLatin1String("people")) {
0065         return 1;
0066     } else if (name == QLatin1String("flags")) {
0067         return 2;
0068     } else if (name == QLatin1String("travel")) {
0069         return 3;
0070     } else if (name == QLatin1String("symbols")) {
0071         return 4;
0072     } else if (name == QLatin1String("activity")) {
0073         return 5;
0074     } else if (name == QLatin1String("objects")) {
0075         return 6;
0076     } else if (name == QLatin1String("nature")) {
0077         return 7;
0078     } else if (name == QLatin1String("food")) {
0079         return 8;
0080     } else if (name == QLatin1String("regional")) {
0081         return 9;
0082     } else {
0083         qCWarning(TEXTEMOTICONSCORE_LOG) << "Missing i18n translate " << name;
0084     }
0085     return 20;
0086 }