• 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
  • adium
adium_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 "adium_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 
29 K_PLUGIN_FACTORY(AdiumEmoticonsFactory, registerPlugin<AdiumEmoticons>();)
30 K_EXPORT_PLUGIN(AdiumEmoticonsFactory("AdiumEmoticons"))
31 
32 AdiumEmoticons::AdiumEmoticons(QObject *parent, const QVariantList &args)
33  : KEmoticonsProvider(parent)
34 {
35  Q_UNUSED(args)
36 }
37 
38 bool AdiumEmoticons::removeEmoticon(const QString &emo)
39 {
40  QString emoticon = QFileInfo(emoticonsMap().key(emo.split(' '))).fileName();
41  QDomElement fce = m_themeXml.firstChildElement("plist").firstChildElement("dict").firstChildElement("dict");
42 
43  if (fce.isNull()) {
44  return false;
45  }
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() == "key" && (de.text() == emoticon)) {
51  QDomElement dict = de.nextSiblingElement();
52  if (!dict.isNull() && dict.tagName() == "dict") {
53  fce.removeChild(dict);
54  }
55 
56  fce.removeChild(de);
57  removeEmoticonsMap(emoticonsMap().key(emo.split(' ')));
58  removeEmoticonIndex(emoticon, emo.split(' '));
59  return true;
60  }
61  }
62  return false;
63 }
64 
65 bool AdiumEmoticons::addEmoticon(const QString &emo, const QString &text, AddEmoticonOption option)
66 {
67  KEmoticonsProvider::addEmoticon(emo, text, option);
68 
69  const QStringList splitted = text.split(' ');
70  QDomElement fce = m_themeXml.firstChildElement("plist").firstChildElement("dict").firstChildElement("dict");
71 
72  if (fce.isNull()) {
73  return false;
74  }
75 
76  QDomElement emoticon = m_themeXml.createElement("key");
77  emoticon.appendChild(m_themeXml.createTextNode(QFileInfo(emo).fileName()));
78  fce.appendChild(emoticon);
79 
80  QDomElement dict = m_themeXml.createElement("dict");
81  QDomElement el = m_themeXml.createElement("key");
82  el.appendChild(m_themeXml.createTextNode("Equivalents"));
83  dict.appendChild(el);
84 
85  QDomElement arr = m_themeXml.createElement("array");
86 
87  QStringList::const_iterator constIterator;
88  for (constIterator = splitted.begin(); constIterator != splitted.end(); ++constIterator) {
89  QDomElement emoText = m_themeXml.createElement("string");
90  QDomText txt = m_themeXml.createTextNode((*constIterator).trimmed());
91  emoText.appendChild(txt);
92  arr.appendChild(emoText);
93  }
94 
95  dict.appendChild(arr);
96 
97  el = m_themeXml.createElement("key");
98  el.appendChild(m_themeXml.createTextNode("Name"));
99  dict.appendChild(el);
100 
101  el = m_themeXml.createElement("string");
102  el.appendChild(m_themeXml.createTextNode(QFileInfo(emo).baseName()));
103  dict.appendChild(el);
104 
105  fce.appendChild(dict);
106 
107  addEmoticonIndex(emo, splitted);
108  addEmoticonsMap(emo, splitted);
109  return true;
110 }
111 
112 void AdiumEmoticons::save()
113 {
114  QFile fp(themePath() + '/' + fileName());
115 
116  if (!fp.exists()) {
117  kWarning() << fp.fileName() << "doesn't exist!";
118  return;
119  }
120 
121  if (!fp.open(QIODevice::WriteOnly)) {
122  kWarning() << fp.fileName() << "can't open WriteOnly!";
123  return;
124  }
125 
126  QTextStream emoStream(&fp);
127  emoStream.setCodec( "UTF-8" );
128  emoStream << m_themeXml.toString(4);
129  fp.close();
130 }
131 
132 bool AdiumEmoticons::loadTheme(const QString &path)
133 {
134  KEmoticonsProvider::loadTheme(path);
135 
136  QFile fp(path);
137 
138  if (!fp.exists()) {
139  kWarning() << path << "doesn't exist!";
140  return false;
141  }
142 
143  if (!fp.open(QIODevice::ReadOnly)) {
144  kWarning() << fp.fileName() << "can't open ReadOnly!";
145  return false;
146  }
147 
148  QString error;
149  int eli, eco;
150  if (!m_themeXml.setContent(&fp, &error, &eli, &eco)) {
151  kWarning() << fp.fileName() << "can't copy to xml!";
152  kWarning() << error << "line:" << eli << "column:" << eco;
153  fp.close();
154  return false;
155  }
156 
157  fp.close();
158 
159  QDomElement fce = m_themeXml.firstChildElement("plist").firstChildElement("dict").firstChildElement("dict");
160 
161  if (fce.isNull()) {
162  return false;
163  }
164 
165  QDomNodeList nl = fce.childNodes();
166 
167  clearEmoticonsMap();
168  QString name;
169  for (uint i = 0; i < nl.length(); i++) {
170  QDomElement de = nl.item(i).toElement();
171 
172  if (!de.isNull() && de.tagName() == "key") {
173  name = KGlobal::dirs()->findResource("emoticons", themeName() + '/' + de.text());
174  continue;
175  } else if (!de.isNull() && de.tagName() == "dict") {
176  QDomElement arr = de.firstChildElement("array");
177  QDomNodeList snl = arr.childNodes();
178  QStringList sl;
179 
180  for (uint k = 0; k < snl.length(); k++) {
181  QDomElement sde = snl.item(k).toElement();
182 
183  if (!sde.isNull() && sde.tagName() == "string") {
184  sl << sde.text();
185  }
186  }
187  if (!name.isEmpty()) {
188  addEmoticonIndex(name, sl);
189  addEmoticonsMap(name, sl);
190  name.clear();
191  }
192  }
193  }
194 
195  return true;
196 }
197 
198 void AdiumEmoticons::createNew()
199 {
200  QString path = KGlobal::dirs()->saveLocation("emoticons", themeName());
201 
202  QFile fp(path + '/' + "Emoticons.plist");
203 
204  if (!fp.open(QIODevice::WriteOnly)) {
205  kWarning() << fp.fileName() << "can't open WriteOnly!";
206  return;
207  }
208 
209  QDomDocumentType ty = QDomImplementation().createDocumentType("plist", "-//Apple Computer//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd");
210  QDomDocument doc(ty);
211  doc.appendChild(doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\""));
212 
213  QDomElement plist = doc.createElement("plist");
214  plist.setAttribute("version", "1.0");
215  doc.appendChild(plist);
216 
217  QDomElement dict = doc.createElement("dict");
218  plist.appendChild(dict);
219 
220  QDomElement el = doc.createElement("key");
221  el.appendChild(doc.createTextNode("AdiumSetVersion"));
222  dict.appendChild(el);
223 
224  el = doc.createElement("integer");
225  el.appendChild(doc.createTextNode("1"));
226  dict.appendChild(el);
227 
228  el = doc.createElement("key");
229  el.appendChild(doc.createTextNode("Emoticons"));
230  dict.appendChild(el);
231 
232  dict.appendChild(doc.createElement("dict"));
233 
234 
235  QTextStream emoStream(&fp);
236  emoStream.setCodec( "UTF-8" );
237  emoStream << doc.toString(4);
238  fp.close();
239 }
240 
241 // 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
AdiumEmoticons::save
void save()
Save the emoticon theme.
Definition: adium_emoticons.cpp:112
kdebug.h
K_PLUGIN_FACTORY
K_PLUGIN_FACTORY(ProxyScoutFactory, registerPlugin< KPAC::ProxyScout >();) namespace KPAC
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
AdiumEmoticons::createNew
void createNew()
Create a new theme.
Definition: adium_emoticons.cpp:198
QString
AdiumEmoticons::removeEmoticon
bool removeEmoticon(const QString &emo)
Remove the emoticon emo, this will not delete the image file too.
Definition: adium_emoticons.cpp:38
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
AdiumEmoticons::addEmoticon
bool addEmoticon(const QString &emo, const QString &text, AddEmoticonOption option=DoNotCopy)
Add the emoticon emo with text text.
Definition: adium_emoticons.cpp:65
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
adium_emoticons.h
kstandarddirs.h
kpluginfactory.h
AdiumEmoticons::loadTheme
bool loadTheme(const QString &path)
Load the theme inside the directory path.
Definition: adium_emoticons.cpp:132
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
AdiumEmoticons
Definition: adium_emoticons.h:26
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: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