• 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
  • xmpp
xmpp_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 "xmpp_emoticons.h"
20 
21 #include <QtCore/QFile>
22 #include <QtCore/QFileInfo>
23 #include <QtGui/QImageReader>
24 
25 #include <kpluginfactory.h>
26 #include <kdebug.h>
27 #include <kstandarddirs.h>
28 #include <kmimetype.h>
29 
30 K_PLUGIN_FACTORY(XmppEmoticonsFactory, registerPlugin<XmppEmoticons>();)
31 K_EXPORT_PLUGIN(XmppEmoticonsFactory("XmppEmoticons"))
32 
33 XmppEmoticons::XmppEmoticons(QObject *parent, const QVariantList &args)
34  : KEmoticonsProvider(parent)
35 {
36  Q_UNUSED(args);
37 }
38 
39 bool XmppEmoticons::removeEmoticon(const QString &emo)
40 {
41  QString emoticon = QFileInfo(emoticonsMap().key(emo.split(' '))).fileName();
42  QDomElement fce = m_themeXml.firstChildElement("icondef");
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() == "icon") {
51  QDomNodeList snl = de.childNodes();
52  QStringList sl;
53  QStringList mime;
54 
55  for (uint k = 0; k < snl.length(); k++) {
56  QDomElement sde = snl.item(k).toElement();
57 
58  if (!sde.isNull() && sde.tagName() == "object" && sde.text() == emoticon) {
59  fce.removeChild(de);
60  removeEmoticonsMap(emoticonsMap().key(emo.split(' ')));
61  removeEmoticonIndex(emoticon, emo.split(' '));
62  return true;
63  }
64  }
65  }
66  }
67  return false;
68 }
69 
70 bool XmppEmoticons::addEmoticon(const QString &emo, const QString &text, AddEmoticonOption option)
71 {
72  KEmoticonsProvider::addEmoticon(emo, text, option);
73 
74  const QStringList splitted = text.split(' ');
75  QDomElement fce = m_themeXml.firstChildElement("icondef");
76 
77  if (fce.isNull()) {
78  return false;
79  }
80 
81  QDomElement emoticon = m_themeXml.createElement("icon");
82  fce.appendChild(emoticon);
83  QStringList::const_iterator constIterator;
84 
85  for (constIterator = splitted.begin(); constIterator != splitted.end(); ++constIterator) {
86  QDomElement emotext = m_themeXml.createElement("text");
87  QDomText txt = m_themeXml.createTextNode((*constIterator).trimmed());
88  emotext.appendChild(txt);
89  emoticon.appendChild(emotext);
90  }
91 
92  QDomElement emoElement = m_themeXml.createElement("object");
93  KMimeType::Ptr mimePtr = KMimeType::findByPath(emo, 0, true);
94  emoElement.setAttribute("mime", mimePtr->name());
95  QDomText txt = m_themeXml.createTextNode(QFileInfo(emo).fileName());
96 
97  emoElement.appendChild(txt);
98  emoticon.appendChild(emoElement);
99 
100  addEmoticonIndex(emo, splitted);
101  addEmoticonsMap(emo, splitted);
102  return true;
103 }
104 
105 void XmppEmoticons::save()
106 {
107  QFile fp(themePath() + '/' + fileName());
108 
109  if (!fp.exists()) {
110  kWarning() << fp.fileName() << "doesn't exist!";
111  return;
112  }
113 
114  if (!fp.open(QIODevice::WriteOnly)) {
115  kWarning() << fp.fileName() << "can't open WriteOnly!";
116  return;
117  }
118 
119  QTextStream emoStream(&fp);
120  emoStream.setCodec( "UTF-8" );
121  emoStream << m_themeXml.toString(4);
122  fp.close();
123 }
124 
125 bool XmppEmoticons::loadTheme(const QString &path)
126 {
127  KEmoticonsProvider::loadTheme(path);
128 
129  QFile fp(path);
130 
131  if (!fp.exists()) {
132  kWarning() << path << "doesn't exist!";
133  return false;
134  }
135 
136  if (!fp.open(QIODevice::ReadOnly)) {
137  kWarning() << fp.fileName() << "can't open ReadOnly!";
138  return false;
139  }
140 
141  QString error;
142  int eli, eco;
143  if (!m_themeXml.setContent(&fp, &error, &eli, &eco)) {
144  kWarning() << fp.fileName() << "can't copy to xml!";
145  kWarning() << error << "line:" << eli << "column:" << eco;
146  fp.close();
147  return false;
148  }
149 
150  fp.close();
151 
152  QDomElement fce = m_themeXml.firstChildElement("icondef");
153 
154  if (fce.isNull()) {
155  return false;
156  }
157 
158  QDomNodeList nl = fce.childNodes();
159 
160  clearEmoticonsMap();
161 
162  for (uint i = 0; i < nl.length(); i++) {
163  QDomElement de = nl.item(i).toElement();
164 
165  if (!de.isNull() && de.tagName() == "icon") {
166  QDomNodeList snl = de.childNodes();
167  QStringList sl;
168  QString emo;
169  QStringList mime;
170  mime << "image/png" << "image/gif" << "image/bmp" << "image/jpeg";
171 
172  for (uint k = 0; k < snl.length(); k++) {
173  QDomElement sde = snl.item(k).toElement();
174 
175  if (!sde.isNull() && sde.tagName() == "text") {
176  sl << sde.text();
177  } else if (!sde.isNull() && sde.tagName() == "object" && mime.contains(sde.attribute("mime"))) {
178  emo = sde.text();
179  }
180  }
181 
182  emo = KGlobal::dirs()->findResource("emoticons", themeName() + '/' + emo);
183 
184  if (emo.isNull()) {
185  continue;
186  }
187 
188  addEmoticonIndex(emo, sl);
189  addEmoticonsMap(emo, sl);
190  }
191  }
192 
193  return true;
194 }
195 
196 void XmppEmoticons::createNew()
197 {
198  QString path = KGlobal::dirs()->saveLocation("emoticons", themeName());
199 
200  QFile fp(path + '/' + "icondef.xml");
201 
202  if (!fp.open(QIODevice::WriteOnly)) {
203  kWarning() << fp.fileName() << "can't open WriteOnly!";
204  return;
205  }
206 
207  QDomDocument doc;
208  doc.appendChild(doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\""));
209  doc.appendChild(doc.createElement("icondef"));
210 
211  QTextStream emoStream(&fp);
212  emoStream.setCodec( "UTF-8" );
213  emoStream << doc.toString(4);
214  fp.close();
215 }
216 
217 // 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
kmimetype.h
XmppEmoticons::save
void save()
Save the emoticon theme.
Definition: xmpp_emoticons.cpp:105
XmppEmoticons
Definition: xmpp_emoticons.h:26
K_PLUGIN_FACTORY
K_PLUGIN_FACTORY(ProxyScoutFactory, registerPlugin< KPAC::ProxyScout >();) namespace KPAC
KGlobal::dirs
KStandardDirs * dirs()
KEmoticonsProvider
This is the base class for the emoticons provider plugins.
Definition: kemoticonsprovider.h:35
XmppEmoticons::addEmoticon
bool addEmoticon(const QString &emo, const QString &text, AddEmoticonOption option=DoNotCopy)
Add the emoticon emo with text text.
Definition: xmpp_emoticons.cpp:70
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
XmppEmoticons::removeEmoticon
bool removeEmoticon(const QString &emo)
Remove the emoticon emo, this will not delete the image file too.
Definition: xmpp_emoticons.cpp:39
XmppEmoticons::createNew
void createNew()
Create a new theme.
Definition: xmpp_emoticons.cpp:196
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
KEmoticonsProvider::themePath
QString themePath() const
Returns the theme path.
Definition: kemoticonsprovider.cpp:94
kstandarddirs.h
kpluginfactory.h
XmppEmoticons::loadTheme
bool loadTheme(const QString &path)
Load the theme inside the directory path.
Definition: xmpp_emoticons.cpp:125
KStandardDirs::findResource
QString findResource(const char *type, const QString &filename) const
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
xmpp_emoticons.h
KEmoticonsProvider::AddEmoticonOption
AddEmoticonOption
Options to pass to addEmoticon.
Definition: kemoticonsprovider.h:53
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
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:35 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