KTextAddons

emojimodelmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2021-2025 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "emojimodelmanager.h"
8
9#include "unicodeemoticonmanager.h"
10#include <KConfigGroup>
11#include <KSharedConfig>
12#include <TextEmoticonsCore/EmojiModel>
13using namespace TextEmoticonsCore;
14
15class EmojiModelManager::EmojiModelManagerPrivate
16{
17public:
18 EmojiModelManagerPrivate(EmojiModelManager *q)
19 : mEmojiModel(new TextEmoticonsCore::EmojiModel(q))
20 {
21 mEmojiModel->setUnicodeEmoticonList(TextEmoticonsCore::UnicodeEmoticonManager::self()->unicodeEmojiList());
22 }
23 void loadRecentUsed()
24 {
25 // Try reading the old key first
26 KConfigGroup group(KSharedConfig::openConfig(), QStringLiteral("EmoticonRecentUsed"));
27 if (group.hasKey("Recents")) {
28 mRecentIdentifier = group.readEntry("Recents", QStringList());
29 // Make sure to clean up the old key, so we don't read it again
30 group.deleteEntry("Recents");
31 group.sync();
32 } else {
33 // Read the new key from state config
34 group = KConfigGroup(KSharedConfig::openStateConfig(), mSettingsGroupName);
35 mRecentIdentifier = group.readEntry("LastUsedEmojis", QStringList());
36 }
37 }
38
39 void writeRecentUsed()
40 {
41 KConfigGroup group(KSharedConfig::openStateConfig(), mSettingsGroupName);
42 group.writeEntry("LastUsedEmojis", mRecentIdentifier);
43 group.sync();
44 }
45
46 QString mSettingsGroupName = QStringLiteral("EmoticonRecentUsed");
47 TextEmoticonsCore::EmojiModel *const mEmojiModel;
48 QStringList mRecentIdentifier;
49 QStringList mExcludeEmoticons;
50};
51
52EmojiModelManager::EmojiModelManager(QObject *parent)
53 : QObject(parent)
54 , d(new EmojiModelManagerPrivate(this))
55{
56 d->loadRecentUsed();
57}
58
59EmojiModelManager::~EmojiModelManager()
60{
61 d->writeRecentUsed();
62}
63
64EmojiModelManager *EmojiModelManager::self()
65{
66 static EmojiModelManager s_self;
67 return &s_self;
68}
69
70TextEmoticonsCore::EmojiModel *EmojiModelManager::emojiModel() const
71{
72 return d->mEmojiModel;
73}
74
76{
77 d->mSettingsGroupName = key;
78 d->loadRecentUsed();
79}
80
81const QStringList &EmojiModelManager::recentIdentifier() const
82{
83 return d->mRecentIdentifier;
84}
85
86void EmojiModelManager::setRecentIdentifier(const QStringList &newRecentIdentifier)
87{
88 d->mRecentIdentifier = newRecentIdentifier;
89 d->writeRecentUsed();
90 Q_EMIT usedIdentifierChanged(d->mRecentIdentifier);
91}
92
93void EmojiModelManager::addIdentifier(const QString &identifier)
94{
95 if (int i = d->mRecentIdentifier.indexOf(identifier)) {
96 // Remove it for adding in top of list
97 if (i != -1) {
98 d->mRecentIdentifier.removeAt(i);
99 }
100 }
101 d->mRecentIdentifier.prepend(identifier);
102 d->writeRecentUsed();
103 Q_EMIT usedIdentifierChanged(d->mRecentIdentifier);
104}
105
106CustomEmojiIconManager *EmojiModelManager::customEmojiIconManager() const
107{
108 return d->mEmojiModel->customEmojiIconManager();
109}
110
111void EmojiModelManager::setCustomEmojiIconManager(CustomEmojiIconManager *newCustomEmojiIconManager)
112{
113 d->mEmojiModel->setCustomEmojiIconManager(newCustomEmojiIconManager);
114}
115
116QStringList EmojiModelManager::excludeEmoticons() const
117{
118 return d->mExcludeEmoticons;
119}
120
121void EmojiModelManager::setExcludeEmoticons(const QStringList &emoticons)
122{
123 if (d->mExcludeEmoticons != emoticons) {
124 d->mExcludeEmoticons = emoticons;
125 d->mEmojiModel->setExcludeEmoticons(d->mExcludeEmoticons);
126 Q_EMIT excludeEmoticonsChanged();
127 }
128}
129
130#include "moc_emojimodelmanager.cpp"
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
The EmojiModelManager class.
void setRecentSettingsGroupName(const QString &key)
Set the settings group name used to store the recent identifiers.
The EmojiModel class.
Definition emojimodel.h:21
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 3 2025 11:46:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.