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(QObject *parent)
19 : emojiModel(new TextEmoticonsCore::EmojiModel(parent))
20 {
21 emojiModel->setUnicodeEmoticonList(TextEmoticonsCore::UnicodeEmoticonManager::self()->unicodeEmojiList());
22 }
23 void loadSettings()
24 {
25 // Try reading the old key first
26 KConfigGroup group(KSharedConfig::openConfig(), QStringLiteral("EmoticonRecentUsed"));
27 if (group.hasKey("Recents")) {
28 recentIdentifier = 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(), settingsGroupName);
35 recentIdentifier = group.readEntry("LastUsedEmojis", QStringList());
36 }
37 tone = static_cast<EmojiModelManager::EmojiTone>(group.readEntry("Tone", static_cast<int>(EmojiModelManager::EmojiTone::Original)));
38 }
39
40 void writeSettings()
41 {
42 KConfigGroup group(KSharedConfig::openStateConfig(), settingsGroupName);
43 group.writeEntry("LastUsedEmojis", recentIdentifier);
44 group.writeEntry("Tone", static_cast<int>(tone));
45 group.sync();
46 }
47
48 QString settingsGroupName = QStringLiteral("EmoticonRecentUsed");
49 TextEmoticonsCore::EmojiModel *const emojiModel;
50 QStringList recentIdentifier;
51 QStringList excludeEmoticons;
52 EmojiModelManager::EmojiTone tone = EmojiModelManager::EmojiTone::All;
53};
54
55EmojiModelManager::EmojiModelManager(QObject *parent)
56 : QObject(parent)
57 , d(new EmojiModelManagerPrivate(this))
58{
59 d->loadSettings();
60}
61
62EmojiModelManager::~EmojiModelManager()
63{
64 d->writeSettings();
65}
66
67EmojiModelManager *EmojiModelManager::self()
68{
69 static EmojiModelManager s_self;
70 return &s_self;
71}
72
73TextEmoticonsCore::EmojiModel *EmojiModelManager::emojiModel() const
74{
75 return d->emojiModel;
76}
77
79{
80 d->settingsGroupName = key;
81 d->loadSettings();
82}
83
84const QStringList &EmojiModelManager::recentIdentifier() const
85{
86 return d->recentIdentifier;
87}
88
89void EmojiModelManager::setRecentIdentifier(const QStringList &newRecentIdentifier)
90{
91 d->recentIdentifier = newRecentIdentifier;
92 d->writeSettings();
93 Q_EMIT usedIdentifierChanged(d->recentIdentifier);
94}
95
96void EmojiModelManager::addIdentifier(const QString &identifier)
97{
98 if (int i = d->recentIdentifier.indexOf(identifier)) {
99 // Remove it for adding in top of list
100 if (i != -1) {
101 d->recentIdentifier.removeAt(i);
102 }
103 }
104 d->recentIdentifier.prepend(identifier);
105 d->writeSettings();
106 Q_EMIT usedIdentifierChanged(d->recentIdentifier);
107}
108
109CustomEmojiIconManager *EmojiModelManager::customEmojiIconManager() const
110{
111 return d->emojiModel->customEmojiIconManager();
112}
113
114void EmojiModelManager::setCustomEmojiIconManager(CustomEmojiIconManager *newCustomEmojiIconManager)
115{
116 d->emojiModel->setCustomEmojiIconManager(newCustomEmojiIconManager);
117}
118
119QStringList EmojiModelManager::excludeEmoticons() const
120{
121 return d->excludeEmoticons;
122}
123
124void EmojiModelManager::setExcludeEmoticons(const QStringList &emoticons)
125{
126 if (d->excludeEmoticons != emoticons) {
127 d->excludeEmoticons = emoticons;
128 d->emojiModel->setExcludeEmoticons(d->excludeEmoticons);
129 Q_EMIT excludeEmoticonsChanged();
130 }
131}
132
133EmojiModelManager::EmojiTone EmojiModelManager::emojiTone() const
134{
135 return d->tone;
136}
137
138void EmojiModelManager::setEmojiTone(EmojiModelManager::EmojiTone tone)
139{
140 if (d->tone != tone) {
141 d->tone = tone;
142 d->writeSettings();
143 Q_EMIT emojiToneChanged();
144 }
145}
146
147#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.
QObject(QObject *parent)
Q_EMITQ_EMIT
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 11:51:55 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.