KUtils
adium_emoticons.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "adium_emoticons.h"
00020
00021 #include <QtCore/QFile>
00022 #include <QtCore/QFileInfo>
00023 #include <QtGui/QImageReader>
00024
00025 #include <kpluginfactory.h>
00026 #include <kdebug.h>
00027 #include <kstandarddirs.h>
00028
00029 K_PLUGIN_FACTORY(AdiumEmoticonsFactory, registerPlugin<AdiumEmoticons>();)
00030 K_EXPORT_PLUGIN(AdiumEmoticonsFactory("AdiumEmoticons"))
00031
00032 AdiumEmoticons::AdiumEmoticons(QObject *parent, const QVariantList &args)
00033 : KEmoticonsProvider(parent)
00034 {
00035 Q_UNUSED(args)
00036 }
00037
00038 bool AdiumEmoticons::removeEmoticon(const QString &emo)
00039 {
00040 QString emoticon = QFileInfo(emoticonsMap().key(emo.split(' '))).fileName();
00041 QDomElement fce = m_themeXml.firstChildElement("plist").firstChildElement("dict").firstChildElement("dict");
00042
00043 if (fce.isNull()) {
00044 return false;
00045 }
00046
00047 QDomNodeList nl = fce.childNodes();
00048 for (uint i = 0; i < nl.length(); i++) {
00049 QDomElement de = nl.item(i).toElement();
00050 if (!de.isNull() && de.tagName() == "key" && (de.text() == emoticon)) {
00051 QDomElement dict = de.nextSiblingElement();
00052 if (!dict.isNull() && dict.tagName() == "dict") {
00053 fce.removeChild(dict);
00054 }
00055
00056 fce.removeChild(de);
00057 removeEmoticonsMap(emoticonsMap().key(emo.split(' ')));
00058 removeEmoticonIndex(emoticon, emo.split(' '));
00059 return true;
00060 }
00061 }
00062 return false;
00063 }
00064
00065 bool AdiumEmoticons::addEmoticon(const QString &emo, const QString &text, AddEmoticonOption option)
00066 {
00067 KEmoticonsProvider::addEmoticon(emo, text, option);
00068
00069 const QStringList splitted = text.split(' ');
00070 QDomElement fce = m_themeXml.firstChildElement("plist").firstChildElement("dict").firstChildElement("dict");
00071
00072 if (fce.isNull()) {
00073 return false;
00074 }
00075
00076 QDomElement emoticon = m_themeXml.createElement("key");
00077 emoticon.appendChild(m_themeXml.createTextNode(QFileInfo(emo).fileName()));
00078 fce.appendChild(emoticon);
00079
00080 QDomElement dict = m_themeXml.createElement("dict");
00081 QDomElement el = m_themeXml.createElement("key");
00082 el.appendChild(m_themeXml.createTextNode("Equivalents"));
00083 dict.appendChild(el);
00084
00085 QDomElement arr = m_themeXml.createElement("array");
00086
00087 QStringList::const_iterator constIterator;
00088 for (constIterator = splitted.begin(); constIterator != splitted.end(); ++constIterator) {
00089 QDomElement emoText = m_themeXml.createElement("string");
00090 QDomText txt = m_themeXml.createTextNode((*constIterator).trimmed());
00091 emoText.appendChild(txt);
00092 arr.appendChild(emoText);
00093 }
00094
00095 dict.appendChild(arr);
00096
00097 el = m_themeXml.createElement("key");
00098 el.appendChild(m_themeXml.createTextNode("Name"));
00099 dict.appendChild(el);
00100
00101 el = m_themeXml.createElement("string");
00102 el.appendChild(m_themeXml.createTextNode(QFileInfo(emo).baseName()));
00103 dict.appendChild(el);
00104
00105 fce.appendChild(dict);
00106
00107 addEmoticonIndex(emo, splitted);
00108 addEmoticonsMap(emo, splitted);
00109 return true;
00110 }
00111
00112 void AdiumEmoticons::save()
00113 {
00114 QFile fp(themePath() + '/' + fileName());
00115
00116 if (!fp.exists()) {
00117 kWarning() << fp.fileName() << "doesn't exist!";
00118 return;
00119 }
00120
00121 if (!fp.open(QIODevice::WriteOnly)) {
00122 kWarning() << fp.fileName() << "can't open WriteOnly!";
00123 return;
00124 }
00125
00126 QTextStream emoStream(&fp);
00127 emoStream << m_themeXml.toString(4);
00128 fp.close();
00129 }
00130
00131 bool AdiumEmoticons::loadTheme(const QString &path)
00132 {
00133 KEmoticonsProvider::loadTheme(path);
00134
00135 QFile fp(path);
00136
00137 if (!fp.exists()) {
00138 kWarning() << path << "doesn't exist!";
00139 return false;
00140 }
00141
00142 if (!fp.open(QIODevice::ReadOnly)) {
00143 kWarning() << fp.fileName() << "can't open ReadOnly!";
00144 return false;
00145 }
00146
00147 QString error;
00148 int eli, eco;
00149 if (!m_themeXml.setContent(&fp, &error, &eli, &eco)) {
00150 kWarning() << fp.fileName() << "can't copy to xml!";
00151 kWarning() << error << "line:" << eli << "column:" << eco;
00152 fp.close();
00153 return false;
00154 }
00155
00156 fp.close();
00157
00158 QDomElement fce = m_themeXml.firstChildElement("plist").firstChildElement("dict").firstChildElement("dict");
00159
00160 if (fce.isNull()) {
00161 return false;
00162 }
00163
00164 QDomNodeList nl = fce.childNodes();
00165
00166 clearEmoticonsMap();
00167 QString name;
00168 for (uint i = 0; i < nl.length(); i++) {
00169 QDomElement de = nl.item(i).toElement();
00170
00171 if (!de.isNull() && de.tagName() == "key") {
00172 name = KGlobal::dirs()->findResource("emoticons", themeName() + '/' + de.text());
00173 continue;
00174 } else if (!de.isNull() && de.tagName() == "dict") {
00175 QDomElement arr = de.firstChildElement("array");
00176 QDomNodeList snl = arr.childNodes();
00177 QStringList sl;
00178
00179 for (uint k = 0; k < snl.length(); k++) {
00180 QDomElement sde = snl.item(k).toElement();
00181
00182 if (!sde.isNull() && sde.tagName() == "string") {
00183 sl << sde.text();
00184 }
00185 }
00186 if (!name.isEmpty()) {
00187 addEmoticonIndex(name, sl);
00188 addEmoticonsMap(name, sl);
00189 name.clear();
00190 }
00191 }
00192 }
00193
00194 return true;
00195 }
00196
00197 void AdiumEmoticons::createNew()
00198 {
00199 QString path = KGlobal::dirs()->saveLocation("emoticons", themeName(), false);
00200
00201 QFile fp(path + '/' + "Emoticons.plist");
00202
00203 if (!fp.open(QIODevice::WriteOnly)) {
00204 kWarning() << fp.fileName() << "can't open WriteOnly!";
00205 return;
00206 }
00207
00208 QDomDocumentType ty = QDomImplementation().createDocumentType("plist", "-//Apple Computer//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd");
00209 QDomDocument doc(ty);
00210 doc.appendChild(doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\""));
00211
00212 QDomElement plist = doc.createElement("plist");
00213 plist.setAttribute("version", "1.0");
00214 doc.appendChild(plist);
00215
00216 QDomElement dict = doc.createElement("dict");
00217 plist.appendChild(dict);
00218
00219 QDomElement el = doc.createElement("key");
00220 el.appendChild(doc.createTextNode("AdiumSetVersion"));
00221 dict.appendChild(el);
00222
00223 el = doc.createElement("integer");
00224 el.appendChild(doc.createTextNode("1"));
00225 dict.appendChild(el);
00226
00227 el = doc.createElement("key");
00228 el.appendChild(doc.createTextNode("Emoticons"));
00229 dict.appendChild(el);
00230
00231 dict.appendChild(doc.createElement("dict"));
00232
00233
00234 QTextStream emoStream(&fp);
00235 emoStream << doc.toString(4);
00236 fp.close();
00237 }
00238
00239