KTextAddons

emojimodelmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2021-2024 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;
14namespace
15{
16static const char myEmoticonRecentUsedGroupName[] = "EmoticonRecentUsed";
17}
18
19class EmojiModelManager::EmojiModelManagerPrivate
20{
21public:
22 EmojiModelManagerPrivate(EmojiModelManager *q)
23 : mEmojiModel(new TextEmoticonsCore::EmojiModel(q))
24 {
25 mEmojiModel->setUnicodeEmoticonList(TextEmoticonsCore::UnicodeEmoticonManager::self()->unicodeEmojiList());
26 }
27 void loadRecentUsed()
28 {
29 KConfigGroup group(KSharedConfig::openConfig(), QLatin1StringView(myEmoticonRecentUsedGroupName));
30 mRecentIdentifier = group.readEntry("Recents", QStringList());
31 }
32
33 void writeRecentUsed()
34 {
35 KConfigGroup group(KSharedConfig::openConfig(), QLatin1StringView(myEmoticonRecentUsedGroupName));
36 group.writeEntry("Recents", mRecentIdentifier);
37 group.sync();
38 }
39
40 TextEmoticonsCore::EmojiModel *const mEmojiModel;
41 QStringList mRecentIdentifier;
42 QStringList mExcludeEmoticons;
43};
44
45EmojiModelManager::EmojiModelManager(QObject *parent)
46 : QObject(parent)
47 , d(new EmojiModelManagerPrivate(this))
48{
49 d->loadRecentUsed();
50}
51
52EmojiModelManager::~EmojiModelManager()
53{
54 d->writeRecentUsed();
55}
56
57EmojiModelManager *EmojiModelManager::self()
58{
59 static EmojiModelManager s_self;
60 return &s_self;
61}
62
63TextEmoticonsCore::EmojiModel *EmojiModelManager::emojiModel() const
64{
65 return d->mEmojiModel;
66}
67
68const QStringList &EmojiModelManager::recentIdentifier() const
69{
70 return d->mRecentIdentifier;
71}
72
73void EmojiModelManager::setRecentIdentifier(const QStringList &newRecentIdentifier)
74{
75 d->mRecentIdentifier = newRecentIdentifier;
76 d->writeRecentUsed();
77 Q_EMIT usedIdentifierChanged(d->mRecentIdentifier);
78}
79
80void EmojiModelManager::addIdentifier(const QString &identifier)
81{
82 if (int i = d->mRecentIdentifier.indexOf(identifier)) {
83 // Remove it for adding in top of list
84 if (i != -1) {
85 d->mRecentIdentifier.removeAt(i);
86 }
87 }
88 d->mRecentIdentifier.prepend(identifier);
89 d->writeRecentUsed();
90 Q_EMIT usedIdentifierChanged(d->mRecentIdentifier);
91}
92
93CustomEmojiIconManager *EmojiModelManager::customEmojiIconManager() const
94{
95 return d->mEmojiModel->customEmojiIconManager();
96}
97
98void EmojiModelManager::setCustomEmojiIconManager(CustomEmojiIconManager *newCustomEmojiIconManager)
99{
100 d->mEmojiModel->setCustomEmojiIconManager(newCustomEmojiIconManager);
101}
102
103QStringList EmojiModelManager::excludeEmoticons() const
104{
105 return d->mExcludeEmoticons;
106}
107
108void EmojiModelManager::setExcludeEmoticons(const QStringList &emoticons)
109{
110 if (d->mExcludeEmoticons != emoticons) {
111 d->mExcludeEmoticons = emoticons;
112 d->mEmojiModel->setExcludeEmoticons(d->mExcludeEmoticons);
113 Q_EMIT excludeEmoticonsChanged();
114 }
115}
116
117#include "moc_emojimodelmanager.cpp"
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
The EmojiModelManager class.
The EmojiModel class.
Definition emojimodel.h:21
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:49:44 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.