• 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
  • providers
  • kde
kde_emoticons.cpp
Go to the documentation of this file.
1 /**********************************************************************************
2  * Copyright (C) 2008 by Carlo Segato <brandon.ml@gmail.com> *
3  * *
4  * This library is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU Lesser General Public *
6  * License as published by the Free Software Foundation; either *
7  * version 2.1 of the License, or (at your option) any later version. *
8  * *
9  * This library is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12  * Lesser General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU Lesser General Public *
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.*
16  * *
17  **********************************************************************************/
18 
19 #include "kde_emoticons.h"
20 
21 #include <QtCore/QFile>
22 #include <QtCore/QFileInfo>
23 #include <QtGui/QImageReader>
24 #include <QtGui/QTextDocument>
25 
26 #include <kpluginfactory.h>
27 #include <kdebug.h>
28 #include <kstandarddirs.h>
29 
30 K_PLUGIN_FACTORY(KdeEmoticonsFactory, registerPlugin<KdeEmoticons>();)
31 K_EXPORT_PLUGIN(KdeEmoticonsFactory("KdeEmoticons"))
32 
33 KdeEmoticons::KdeEmoticons(QObject *parent, const QVariantList &args)
34  : KEmoticonsProvider(parent)
35 {
36  Q_UNUSED(args);
37 }
38 
39 bool KdeEmoticons::removeEmoticon(const QString &emo)
40 {
41  QString emoticon = QFileInfo(emoticonsMap().key(emo.split(' '))).fileName();
42  QDomElement fce = m_themeXml.firstChildElement("messaging-emoticon-map");
43 
44  if (fce.isNull())
45  return false;
46 
47  QDomNodeList nl = fce.childNodes();
48  for (uint i = 0; i < nl.length(); i++) {
49  QDomElement de = nl.item(i).toElement();
50  if (!de.isNull() && de.tagName() == "emoticon" && (de.attribute("file") == emoticon || de.attribute("file") == QFileInfo(emoticon).baseName())) {
51  fce.removeChild(de);
52  removeEmoticonsMap(emoticonsMap().key(emo.split(' ')));
53  removeEmoticonIndex(emoticon, emo.split(' '));
54  return true;
55  }
56  }
57  return false;
58 }
59 
60 bool KdeEmoticons::addEmoticon(const QString &emo, const QString &text, AddEmoticonOption option)
61 {
62  KEmoticonsProvider::addEmoticon(emo, text, option);
63 
64  const QStringList splitted = text.split(' ');
65  QDomElement fce = m_themeXml.firstChildElement("messaging-emoticon-map");
66 
67  if (fce.isNull())
68  return false;
69 
70  QDomElement emoticon = m_themeXml.createElement("emoticon");
71  emoticon.setAttribute("file", QFileInfo(emo).fileName());
72  fce.appendChild(emoticon);
73  QStringList::const_iterator constIterator;
74  for (constIterator = splitted.begin(); constIterator != splitted.end(); ++constIterator) {
75  QDomElement emoText = m_themeXml.createElement("string");
76  QDomText txt = m_themeXml.createTextNode((*constIterator).trimmed());
77  emoText.appendChild(txt);
78  emoticon.appendChild(emoText);
79  }
80 
81  addEmoticonIndex(emo, splitted);
82  addEmoticonsMap(emo, splitted);
83  return true;
84 }
85 
86 void KdeEmoticons::save()
87 {
88  QFile fp(themePath() + '/' + fileName());
89 
90  if (!fp.exists()) {
91  kWarning() << fp.fileName() << "doesn't exist!";
92  return;
93  }
94 
95  if (!fp.open(QIODevice::WriteOnly)) {
96  kWarning() << fp.fileName() << "can't open WriteOnly!";
97  return;
98  }
99 
100  QTextStream emoStream(&fp);
101  emoStream.setCodec( "UTF-8" );
102  emoStream << m_themeXml.toString(4);
103  fp.close();
104 }
105 
106 bool KdeEmoticons::loadTheme(const QString &path)
107 {
108  KEmoticonsProvider::loadTheme(path);
109 
110  QFile fp(path);
111 
112  if (!fp.exists()) {
113  kWarning() << path << "doesn't exist!";
114  return false;
115  }
116 
117  if (!fp.open(QIODevice::ReadOnly)) {
118  kWarning() << fp.fileName() << "can't open ReadOnly!";
119  return false;
120  }
121 
122  QString error;
123  int eli, eco;
124  if (!m_themeXml.setContent(&fp, &error, &eli, &eco)) {
125  kWarning() << fp.fileName() << "can't copy to xml!";
126  kWarning() << error << "line:" << eli << "column:" << eco;
127  fp.close();
128  return false;
129  }
130 
131  fp.close();
132 
133  QDomElement fce = m_themeXml.firstChildElement("messaging-emoticon-map");
134 
135  if (fce.isNull())
136  return false;
137 
138  QDomNodeList nl = fce.childNodes();
139 
140  clearEmoticonsMap();
141 
142  for (uint i = 0; i < nl.length(); i++) {
143  QDomElement de = nl.item(i).toElement();
144 
145  if (!de.isNull() && de.tagName() == "emoticon") {
146  QDomNodeList snl = de.childNodes();
147  QStringList sl;
148 
149  for (uint k = 0; k < snl.length(); k++) {
150  QDomElement sde = snl.item(k).toElement();
151 
152  if (!sde.isNull() && sde.tagName() == "string") {
153  sl << sde.text();
154  }
155  }
156 
157  QString emo = KGlobal::dirs()->findResource("emoticons", themeName() + '/' + de.attribute("file"));
158 
159  if (emo.isEmpty()) {
160  QList<QByteArray> ext = QImageReader::supportedImageFormats();
161 
162  for (int j = 0; j < ext.size(); ++j) {
163  emo = KGlobal::dirs()->findResource("emoticons", themeName() + '/' + de.attribute("file") + '.' + ext.at(j));
164  if (!emo.isEmpty()) {
165  break;
166  }
167  }
168 
169  if (emo.isEmpty()) {
170  continue;
171  }
172  }
173 
174  addEmoticonIndex(emo, sl);
175  addEmoticonsMap(emo, sl);
176  }
177  }
178 
179  return true;
180 }
181 
182 void KdeEmoticons::createNew()
183 {
184  QString path = KGlobal::dirs()->saveLocation("emoticons", themeName());
185 
186  QFile fp(path + '/' + "emoticons.xml");
187 
188  if (!fp.open(QIODevice::WriteOnly)) {
189  kWarning() << fp.fileName() << "can't open WriteOnly!";
190  return;
191  }
192 
193  QDomDocument doc;
194  doc.appendChild(doc.createProcessingInstruction("xml", "version=\"1.0\""));
195  doc.appendChild(doc.createElement("messaging-emoticon-map"));
196 
197  QTextStream emoStream(&fp);
198  emoStream.setCodec( "UTF-8" );
199  emoStream << doc.toString(4);
200  fp.close();
201 }
202 
203 // kate: space-indent on; indent-width 4; replace-tabs on;
KStandardDirs::saveLocation
QString saveLocation(const char *type, const QString &suffix=QString(), bool create=true) const
KEmoticonsProvider::addEmoticonIndex
void addEmoticonIndex(const QString &path, const QStringList &emoList)
Add an emoticon to the index.
Definition: kemoticonsprovider.cpp:135
KEmoticonsProvider::fileName
QString fileName() const
Returns the file name of the theme.
Definition: kemoticonsprovider.cpp:99
kdebug.h
K_PLUGIN_FACTORY
K_PLUGIN_FACTORY(ProxyScoutFactory, registerPlugin< KPAC::ProxyScout >();) namespace KPAC
KdeEmoticons::removeEmoticon
bool removeEmoticon(const QString &emo)
Remove the emoticon emo, this will not delete the image file too.
Definition: kde_emoticons.cpp:39
KGlobal::dirs
KStandardDirs * dirs()
KdeEmoticons::loadTheme
bool loadTheme(const QString &path)
Load the theme inside the directory path.
Definition: kde_emoticons.cpp:106
KEmoticonsProvider
This is the base class for the emoticons provider plugins.
Definition: kemoticonsprovider.h:35
QString
QObject
KEmoticonsProvider::emoticonsMap
QHash< QString, QStringList > emoticonsMap() const
Returns a QHash that contains the emoticons path as keys and the text as values.
Definition: kemoticonsprovider.cpp:121
KEmoticonsProvider::addEmoticonsMap
void addEmoticonsMap(QString key, QStringList value)
Insert a new item in the emoticons map.
Definition: kemoticonsprovider.cpp:109
KdeEmoticons
Definition: kde_emoticons.h:26
QStringList
KEmoticonsProvider::removeEmoticonsMap
void removeEmoticonsMap(QString key)
Remove an item from the emoticons map.
Definition: kemoticonsprovider.cpp:116
KEmoticonsProvider::removeEmoticonIndex
void removeEmoticonIndex(const QString &path, const QStringList &emoList)
Remove an emoticon from the index.
Definition: kemoticonsprovider.cpp:158
KEmoticonsProvider::loadTheme
virtual bool loadTheme(const QString &path)
Load the theme inside the directory path.
Definition: kemoticonsprovider.cpp:55
KdeEmoticons::addEmoticon
bool addEmoticon(const QString &emo, const QString &text, AddEmoticonOption option=DoNotCopy)
Add the emoticon emo with text text.
Definition: kde_emoticons.cpp:60
kde_emoticons.h
KEmoticonsProvider::themePath
QString themePath() const
Returns the theme path.
Definition: kemoticonsprovider.cpp:94
kstandarddirs.h
KdeEmoticons::save
void save()
Save the emoticon theme.
Definition: kde_emoticons.cpp:86
kpluginfactory.h
KStandardDirs::findResource
QString findResource(const char *type, const QString &filename) const
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KEmoticonsProvider::AddEmoticonOption
AddEmoticonOption
Options to pass to addEmoticon.
Definition: kemoticonsprovider.h:53
KdeEmoticons::createNew
void createNew()
Create a new theme.
Definition: kde_emoticons.cpp:182
KEmoticonsProvider::clearEmoticonsMap
void clearEmoticonsMap()
Clears the emoticons map.
Definition: kemoticonsprovider.cpp:104
KEmoticonsProvider::addEmoticon
virtual bool addEmoticon(const QString &emo, const QString &text, AddEmoticonOption option=DoNotCopy)
Add the emoticon emo with text text.
Definition: kemoticonsprovider.cpp:70
QList< QByteArray >
K_EXPORT_PLUGIN
#define K_EXPORT_PLUGIN(factory)
KEmoticonsProvider::themeName
QString themeName() const
Returns the theme name.
Definition: kemoticonsprovider.cpp:84
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