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

KUtils

  • sources
  • kde-4.12
  • kdelibs
  • 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 
35 class KEmoticonsPrivate
36 {
37 public:
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 
53 KEmoticonsPrivate::KEmoticonsPrivate(KEmoticons *parent)
54  : q(parent)
55 {
56 }
57 
58 KEmoticonsPrivate::~KEmoticonsPrivate()
59 {
60  delete m_dirwatch;
61 }
62 
63 bool 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 
68 void 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 
75 KEmoticonsProvider *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 
86 void 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 
96 KEmoticonsTheme 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 
118 KEmoticons::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 
126 KEmoticons::~KEmoticons()
127 {
128  delete d;
129 }
130 
131 KEmoticonsTheme KEmoticons::theme()
132 {
133  return theme(currentThemeName());
134 }
135 
136 KEmoticonsTheme 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 
145 QString KEmoticons::currentThemeName()
146 {
147  KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "Emoticons");
148  QString name = config.readEntry("emoticonsTheme", "kde4");
149  return name;
150 }
151 
152 QStringList 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 
166 void KEmoticons::setTheme(const KEmoticonsTheme &theme)
167 {
168  setTheme(theme.themeName());
169 }
170 
171 void KEmoticons::setTheme(const QString &theme)
172 {
173  KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "Emoticons");
174  config.writeEntry("emoticonsTheme", theme);
175  config.sync();
176 }
177 
178 KEmoticonsTheme 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 
189 QStringList 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 
284 void KEmoticons::setParseMode(KEmoticonsTheme::ParseMode mode)
285 {
286  KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "Emoticons");
287  config.writeEntry("parseMode", int(mode));
288  config.sync();
289 }
290 
291 KEmoticonsTheme::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;
KSharedPtr
KEmoticonsTheme
This class contains the emoticons theme.
Definition: kemoticonstheme.h:34
kdebug.h
KEmoticons::~KEmoticons
~KEmoticons()
Destruct the object.
Definition: kemoticons.cpp:126
kmimetype.h
KArchiveEntry::isDirectory
virtual bool isDirectory() const
KServiceTypeTrader::self
static KServiceTypeTrader * self()
KEmoticons::newTheme
KEmoticonsTheme newTheme(const QString &name, const KService::Ptr &service)
Create a new emoticons theme.
Definition: kemoticons.cpp:178
kdirwatch.h
KStandardDirs::findDirs
QStringList findDirs(const char *type, const QString &reldir) const
KEmoticons::installTheme
QStringList installTheme(const QString &archiveName)
Install all themes inside the archive archiveName.
Definition: kemoticons.cpp:189
KArchive
KGlobal::dirs
KStandardDirs * dirs()
name
const char * name(StandardAction id)
KEmoticonsProvider
This is the base class for the emoticons provider plugins.
Definition: kemoticonsprovider.h:35
kError
static QDebug kError(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KConfigGroup::writeEntry
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
KEmoticons::setParseMode
static void setParseMode(KEmoticonsTheme::ParseMode mode)
Set the parse mode to mode.
Definition: kemoticons.cpp:284
KEmoticons
This class can be used to retrieve, install, create emoticons theme.
Definition: kemoticons.h:44
QString
QHash< QString, KEmoticonsTheme >
kemoticonsprovider.h
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KEmoticons::setTheme
static void setTheme(const KEmoticonsTheme &theme)
Set theme as the current theme.
Definition: kemoticons.cpp:166
KEmoticons::parseMode
static KEmoticonsTheme::ParseMode parseMode()
Returns the current parse mode.
Definition: kemoticons.cpp:291
KEmoticonsTheme::RelaxedParse
Parse mode where all possible emoticon matches are allowed.
Definition: kemoticonstheme.h:44
config
KSharedConfigPtr config()
kemoticons.h
KEmoticons::theme
KEmoticonsTheme theme()
Retrieve the current emoticons theme.
Definition: kemoticons.cpp:131
kzip.h
KEmoticonsTheme::createNew
void createNew()
Create a new theme.
Definition: kemoticonstheme.cpp:154
KEmoticons::themeList
static QStringList themeList()
Returns a list of installed theme.
Definition: kemoticons.cpp:152
priorityLessThan
bool priorityLessThan(const KService::Ptr &s1, const KService::Ptr &s2)
Definition: kemoticons.cpp:63
KPluginLoader
kpluginloader.h
KTar
QStringList
KArchiveEntry
KEmoticonsTheme::setThemeName
void setThemeName(const QString &name)
Set the theme name.
Definition: kemoticonstheme.cpp:118
KZip
KArchiveDirectory::copyTo
void copyTo(const QString &dest, bool recursive=true) const
KArchiveDirectory::entry
const KArchiveEntry * entry(const QString &name) const
ktar.h
KConfigGroup
KEmoticons::KEmoticons
KEmoticons()
Default constructor.
Definition: kemoticons.cpp:118
KServiceTypeTrader::query
KService::List query(const QString &servicetype, const QString &constraint=QString()) const
KArchiveDirectory
KStandardDirs::locateLocal
static QString locateLocal(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
kstandarddirs.h
KEmoticonsTheme::themeName
QString themeName() const
Returns the theme name.
Definition: kemoticonstheme.cpp:109
KStandardDirs::findResource
QString findResource(const char *type, const QString &filename) const
KEmoticons::currentThemeName
static QString currentThemeName()
Retrieve the current emoticon theme name.
Definition: kemoticons.cpp:145
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KArchiveDirectory::name
QString name() const
KDirWatch
KConfigGroup::sync
void sync()
KSharedConfig::openConfig
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, const char *resourceType="config")
KPluginFactory
KConfigGroup::readEntry
T readEntry(const QString &key, const T &aDefault) const
KArchiveDirectory::entries
QStringList entries() const
kconfiggroup.h
QList< KService::Ptr >
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:50:34 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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