• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KUtils

  • kutils
  • kemoticons
kemoticons.cpp
Go to the documentation of this file.
1/**********************************************************************************
2 * Copyright (C) 2007 by Carlo Segato <brandon.ml@gmail.com> *
3 * Copyright (C) 2008 Montel Laurent <montel@kde.org> *
4 * *
5 * This library is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU Lesser General Public *
7 * License as published by the Free Software Foundation; either *
8 * version 2.1 of the License, or (at your option) any later version. *
9 * *
10 * This library is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * Lesser General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU Lesser General Public *
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.*
17 * *
18 **********************************************************************************/
19
20#include "kemoticons.h"
21#include "kemoticonsprovider.h"
22
23#include <QFile>
24#include <QDir>
25
26#include <kpluginloader.h>
27#include <kdebug.h>
28#include <kstandarddirs.h>
29#include <kconfiggroup.h>
30#include <ktar.h>
31#include <kzip.h>
32#include <kmimetype.h>
33#include <kdirwatch.h>
34
35class KEmoticonsPrivate
36{
37public:
38 KEmoticonsPrivate(KEmoticons *parent);
39 ~KEmoticonsPrivate();
40 void loadServiceList();
41 KEmoticonsProvider *loadProvider(const KService::Ptr &service);
42 KEmoticonsTheme loadTheme(const QString &name);
43
44 QList<KService::Ptr> m_loaded;
45 QHash<QString, KEmoticonsTheme> m_themes;
46 KDirWatch *m_dirwatch;
47 KEmoticons *q;
48
49 //private slots
50 void themeChanged(const QString &path);
51};
52
53KEmoticonsPrivate::KEmoticonsPrivate(KEmoticons *parent)
54 : q(parent)
55{
56}
57
58KEmoticonsPrivate::~KEmoticonsPrivate()
59{
60 delete m_dirwatch;
61}
62
63bool priorityLessThan(const KService::Ptr &s1, const KService::Ptr &s2)
64{
65 return (s1->property("X-KDE-Priority").toInt() > s2->property("X-KDE-Priority").toInt());
66}
67
68void KEmoticonsPrivate::loadServiceList()
69{
70 QString constraint("(exist Library)");
71 m_loaded = KServiceTypeTrader::self()->query("KEmoticons", constraint);
72 qSort(m_loaded.begin(), m_loaded.end(), priorityLessThan);
73}
74
75KEmoticonsProvider *KEmoticonsPrivate::loadProvider(const KService::Ptr &service)
76{
77 KPluginFactory *factory = KPluginLoader(service->library()).factory();
78 if (!factory) {
79 kWarning() << "Invalid plugin factory for" << service->library();
80 return 0;
81 }
82 KEmoticonsProvider *provider = factory->create<KEmoticonsProvider>(0);
83 return provider;
84}
85
86void KEmoticonsPrivate::themeChanged(const QString &path)
87{
88 QFileInfo info(path);
89 QString name = info.dir().dirName();
90
91 if (m_themes.contains(name)) {
92 loadTheme(name);
93 }
94}
95
96KEmoticonsTheme KEmoticonsPrivate::loadTheme(const QString &name)
97{
98 const int numberOfTheme = m_loaded.size();
99 for (int i = 0; i < numberOfTheme; ++i) {
100 const QString fName = m_loaded.at(i)->property("X-KDE-EmoticonsFileName").toString();
101 const QString path = KGlobal::dirs()->findResource("emoticons", name + '/' + fName);
102
103 if (QFile::exists(path)) {
104 KEmoticonsProvider *provider = loadProvider(m_loaded.at(i));
105 KEmoticonsTheme theme(provider);
106 theme.loadTheme(path);
107 m_themes.insert(name, theme);
108
109 if (!m_dirwatch->contains(path)) {
110 m_dirwatch->addFile(path);
111 }
112 return theme;
113 }
114 }
115 return KEmoticonsTheme();
116}
117
118KEmoticons::KEmoticons()
119 : d(new KEmoticonsPrivate(this))
120{
121 d->loadServiceList();
122 d->m_dirwatch = new KDirWatch;
123 connect(d->m_dirwatch, SIGNAL(dirty(QString)), this, SLOT(themeChanged(QString)));
124}
125
126KEmoticons::~KEmoticons()
127{
128 delete d;
129}
130
131KEmoticonsTheme KEmoticons::theme()
132{
133 return theme(currentThemeName());
134}
135
136KEmoticonsTheme KEmoticons::theme(const QString &name)
137{
138 if (d->m_themes.contains(name)) {
139 return d->m_themes.value(name);
140 }
141
142 return d->loadTheme(name);
143}
144
145QString KEmoticons::currentThemeName()
146{
147 KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "Emoticons");
148 QString name = config.readEntry("emoticonsTheme", "kde4");
149 return name;
150}
151
152QStringList KEmoticons::themeList()
153{
154 QStringList ls;
155 const QStringList themeDirs = KGlobal::dirs()->findDirs("emoticons", "");
156
157 for (int i = 0; i < themeDirs.count(); ++i) {
158 QDir themeQDir(themeDirs[i]);
159 themeQDir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
160 themeQDir.setSorting(QDir::Name);
161 ls << themeQDir.entryList();
162 }
163 return ls;
164}
165
166void KEmoticons::setTheme(const KEmoticonsTheme &theme)
167{
168 setTheme(theme.themeName());
169}
170
171void KEmoticons::setTheme(const QString &theme)
172{
173 KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "Emoticons");
174 config.writeEntry("emoticonsTheme", theme);
175 config.sync();
176}
177
178KEmoticonsTheme KEmoticons::newTheme(const QString &name, const KService::Ptr &service)
179{
180 KEmoticonsProvider *provider = d->loadProvider(service);
181 KEmoticonsTheme theme(provider);
182 theme.setThemeName(name);
183
184 theme.createNew();
185
186 return theme;
187}
188
189QStringList KEmoticons::installTheme(const QString &archiveName)
190{
191 QStringList foundThemes;
192 KArchiveEntry *currentEntry = 0L;
193 KArchiveDirectory* currentDir = 0L;
194 KArchive *archive = 0L;
195
196 QString localThemesDir(KStandardDirs::locateLocal("emoticons", QString()));
197
198 if (localThemesDir.isEmpty()) {
199 kError() << "Could not find a suitable place in which to install the emoticon theme";
200 return QStringList();
201 }
202
203 const QString currentBundleMimeType = KMimeType::findByPath(archiveName, 0, false)->name();
204
205 if (currentBundleMimeType == "application/zip" ||
206 currentBundleMimeType == "application/x-zip" ||
207 currentBundleMimeType == "application/x-zip-compressed") {
208 archive = new KZip(archiveName);
209 } else if (currentBundleMimeType == "application/x-compressed-tar" ||
210 currentBundleMimeType == "application/x-bzip-compressed-tar" ||
211 currentBundleMimeType == "application/x-lzma-compressed-tar" ||
212 currentBundleMimeType == "application/x-xz-compressed-tar" ||
213 currentBundleMimeType == "application/x-gzip" ||
214 currentBundleMimeType == "application/x-bzip" ||
215 currentBundleMimeType == "application/x-lzma" ||
216 currentBundleMimeType == "application/x-xz") {
217 archive = new KTar(archiveName);
218 } else if (archiveName.endsWith(QLatin1String("jisp")) || archiveName.endsWith(QLatin1String("zip"))) {
219 archive = new KZip(archiveName);
220 } else {
221 archive = new KTar(archiveName);
222 }
223
224 if (!archive || !archive->open(QIODevice::ReadOnly)) {
225 kError() << "Could not open" << archiveName << "for unpacking";
226 delete archive;
227 return QStringList();
228 }
229
230 const KArchiveDirectory* rootDir = archive->directory();
231
232 // iterate all the dirs looking for an emoticons.xml file
233 const QStringList entries = rootDir->entries();
234 for (QStringList::ConstIterator it = entries.begin(); it != entries.end(); ++it) {
235 currentEntry = const_cast<KArchiveEntry*>(rootDir->entry(*it));
236
237 if (currentEntry->isDirectory()) {
238 currentDir = dynamic_cast<KArchiveDirectory*>(currentEntry);
239
240 for (int i = 0; i < d->m_loaded.size(); ++i) {
241 QString fName = d->m_loaded.at(i)->property("X-KDE-EmoticonsFileName").toString();
242
243 if (currentDir && currentDir->entry(fName) != NULL) {
244 foundThemes.append(currentDir->name());
245 }
246 }
247 }
248 }
249
250 if (foundThemes.isEmpty()) {
251 kError() << "The file" << archiveName << "is not a valid emoticon theme archive";
252 archive->close();
253 delete archive;
254 return QStringList();
255 }
256
257 for (int themeIndex = 0; themeIndex < foundThemes.size(); ++themeIndex) {
258 const QString &theme = foundThemes[themeIndex];
259
260 currentEntry = const_cast<KArchiveEntry *>(rootDir->entry(theme));
261 if (currentEntry == 0) {
262 kDebug() << "couldn't get next archive entry";
263 continue;
264 }
265
266 if (currentEntry->isDirectory()) {
267 currentDir = dynamic_cast<KArchiveDirectory*>(currentEntry);
268
269 if (currentDir == 0) {
270 kDebug() << "couldn't cast archive entry to KArchiveDirectory";
271 continue;
272 }
273
274 currentDir->copyTo(localThemesDir + theme);
275 }
276 }
277
278 archive->close();
279 delete archive;
280
281 return foundThemes;
282}
283
284void KEmoticons::setParseMode(KEmoticonsTheme::ParseMode mode)
285{
286 KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "Emoticons");
287 config.writeEntry("parseMode", int(mode));
288 config.sync();
289}
290
291KEmoticonsTheme::ParseMode KEmoticons::parseMode()
292{
293 KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "Emoticons");
294 return (KEmoticonsTheme::ParseMode) config.readEntry("parseMode", int(KEmoticonsTheme::RelaxedParse));
295}
296
297#include "kemoticons.moc"
298
299// kate: space-indent on; indent-width 4; replace-tabs on;
KArchiveDirectory
KArchiveDirectory::entries
QStringList entries() const
KArchiveDirectory::entry
const KArchiveEntry * entry(const QString &name) const
KArchiveDirectory::copyTo
void copyTo(const QString &dest, bool recursive=true) const
KArchiveEntry
KArchiveEntry::isDirectory
virtual bool isDirectory() const
KArchiveEntry::name
QString name() const
KArchive
KArchive::close
virtual bool close()
KArchive::open
virtual bool open(QIODevice::OpenMode mode)
KArchive::directory
const KArchiveDirectory * directory() const
KConfigGroup
KDirWatch
KEmoticonsProvider
This is the base class for the emoticons provider plugins.
Definition: kemoticonsprovider.h:36
KEmoticonsTheme
This class contains the emoticons theme.
Definition: kemoticonstheme.h:35
KEmoticonsTheme::setThemeName
void setThemeName(const QString &name)
Set the theme name.
Definition: kemoticonstheme.cpp:118
KEmoticonsTheme::RelaxedParse
@ RelaxedParse
Parse mode where all possible emoticon matches are allowed.
Definition: kemoticonstheme.h:44
KEmoticonsTheme::themeName
QString themeName() const
Returns the theme name.
Definition: kemoticonstheme.cpp:109
KEmoticonsTheme::createNew
void createNew()
Create a new theme.
Definition: kemoticonstheme.cpp:154
KEmoticons
This class can be used to retrieve, install, create emoticons theme.
Definition: kemoticons.h:45
KEmoticons::parseMode
static KEmoticonsTheme::ParseMode parseMode()
Returns the current parse mode.
Definition: kemoticons.cpp:291
KEmoticons::themeList
static QStringList themeList()
Returns a list of installed theme.
Definition: kemoticons.cpp:152
KEmoticons::setTheme
static void setTheme(const KEmoticonsTheme &theme)
Set theme as the current theme.
Definition: kemoticons.cpp:166
KEmoticons::~KEmoticons
~KEmoticons()
Destruct the object.
Definition: kemoticons.cpp:126
KEmoticons::installTheme
QStringList installTheme(const QString &archiveName)
Install all themes inside the archive archiveName.
Definition: kemoticons.cpp:189
KEmoticons::theme
KEmoticonsTheme theme()
Retrieve the current emoticons theme.
Definition: kemoticons.cpp:131
KEmoticons::setParseMode
static void setParseMode(KEmoticonsTheme::ParseMode mode)
Set the parse mode to mode.
Definition: kemoticons.cpp:284
KEmoticons::currentThemeName
static QString currentThemeName()
Retrieve the current emoticon theme name.
Definition: kemoticons.cpp:145
KEmoticons::KEmoticons
KEmoticons()
Default constructor.
Definition: kemoticons.cpp:118
KEmoticons::newTheme
KEmoticonsTheme newTheme(const QString &name, const KService::Ptr &service)
Create a new emoticons theme.
Definition: kemoticons.cpp:178
KMimeType::findByPath
static Ptr findByPath(const QString &path, mode_t mode=0, bool fast_mode=false, int *accuracy=0)
KPluginFactory::create
T * create(const QString &keyword, QObject *parent=0, const QVariantList &args=QVariantList())
KPluginLoader
KPluginLoader::factory
KPluginFactory * factory()
KServiceTypeTrader::self
static KServiceTypeTrader * self()
KServiceTypeTrader::query
KService::List query(const QString &servicetype, const QString &constraint=QString()) const
KSharedConfig::openConfig
static KSharedConfig::Ptr openConfig(const KComponentData &componentData, const QString &fileName=QString(), OpenFlags mode=FullConfig, const char *resourceType="config")
KSharedPtr< KService >
KStandardDirs::findDirs
QStringList findDirs(const char *type, const QString &reldir) const
KStandardDirs::findResource
QString findResource(const char *type, const QString &filename) const
KStandardDirs::locateLocal
static QString locateLocal(const char *type, const QString &filename, bool createDir, const KComponentData &cData=KGlobal::mainComponent())
KTar
KZip
QHash
QList
kDebug
#define kDebug
kWarning
#define kWarning
kconfiggroup.h
kdebug.h
kdirwatch.h
priorityLessThan
bool priorityLessThan(const KService::Ptr &s1, const KService::Ptr &s2)
Definition: kemoticons.cpp:63
kemoticons.h
kemoticonsprovider.h
kmimetype.h
kpluginloader.h
kstandarddirs.h
ktar.h
kzip.h
KGlobal::dirs
KStandardDirs * dirs()
config
KSharedConfigPtr config()
name
const char * name(StandardAction id)
KPluginFactory
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KUtils

Skip menu "KUtils"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal