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 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 for (int i = 0; i < splitted.size(); ++i) {
00088 QDomElement emoText = m_themeXml.createElement("string");
00089 QDomText txt = m_themeXml.createTextNode(splitted.at(i).trimmed());
00090 emoText.appendChild(txt);
00091 arr.appendChild(emoText);
00092 }
00093
00094 dict.appendChild(arr);
00095
00096 el = m_themeXml.createElement("key");
00097 el.appendChild(m_themeXml.createTextNode("Name"));
00098 dict.appendChild(el);
00099
00100 el = m_themeXml.createElement("string");
00101 el.appendChild(m_themeXml.createTextNode(QFileInfo(emo).baseName()));
00102 dict.appendChild(el);
00103
00104 fce.appendChild(dict);
00105
00106 addEmoticonIndex(emo, splitted);
00107 addEmoticonsMap(emo, splitted);
00108 return true;
00109 }
00110
00111 void AdiumEmoticons::save()
00112 {
00113 QFile fp(themePath() + '/' + fileName());
00114
00115 if (!fp.exists()) {
00116 kWarning() << fp.fileName() << "doesn't exist!";
00117 return;
00118 }
00119
00120 if (!fp.open(QIODevice::WriteOnly)) {
00121 kWarning() << fp.fileName() << "can't open WriteOnly!";
00122 return;
00123 }
00124
00125 QTextStream emoStream(&fp);
00126 emoStream << m_themeXml.toString(4);
00127 fp.close();
00128 }
00129
00130 bool AdiumEmoticons::loadTheme(const QString &path)
00131 {
00132 KEmoticonsProvider::loadTheme(path);
00133
00134 QFile fp(path);
00135
00136 if (!fp.exists()) {
00137 kWarning() << path << "doesn't exist!";
00138 return false;
00139 }
00140
00141 if (!fp.open(QIODevice::ReadOnly)) {
00142 kWarning() << fp.fileName() << "can't open ReadOnly!";
00143 return false;
00144 }
00145
00146 QString error;
00147 int eli, eco;
00148 if (!m_themeXml.setContent(&fp, &error, &eli, &eco)) {
00149 kWarning() << fp.fileName() << "can't copy to xml!";
00150 kWarning() << error << "line:" << eli << "column:" << eco;
00151 fp.close();
00152 return false;
00153 }
00154
00155 fp.close();
00156
00157 QDomElement fce = m_themeXml.firstChildElement("plist").firstChildElement("dict").firstChildElement("dict");
00158
00159 if (fce.isNull()) {
00160 return false;
00161 }
00162
00163 QDomNodeList nl = fce.childNodes();
00164
00165 clearEmoticonsMap();
00166 QString name;
00167 for (uint i = 0; i < nl.length(); i++) {
00168 QDomElement de = nl.item(i).toElement();
00169
00170 if (!de.isNull() && de.tagName() == "key") {
00171 name = KGlobal::dirs()->findResource("emoticons", themeName() + '/' + de.text());
00172 continue;
00173 } else if (!de.isNull() && de.tagName() == "dict") {
00174 QDomElement arr = de.firstChildElement("array");
00175 QDomNodeList snl = arr.childNodes();
00176 QStringList sl;
00177
00178 for (uint k = 0; k < snl.length(); k++) {
00179 QDomElement sde = snl.item(k).toElement();
00180
00181 if (!sde.isNull() && sde.tagName() == "string") {
00182 sl << sde.text();
00183 }
00184 }
00185 if (!name.isEmpty()) {
00186 addEmoticonIndex(name, sl);
00187 addEmoticonsMap(name, sl);
00188 name = QString();
00189 }
00190 }
00191 }
00192
00193 return true;
00194 }
00195
00196 void AdiumEmoticons::createNew()
00197 {
00198 QString path = KGlobal::dirs()->saveLocation("emoticons", themeName(), false);
00199
00200 QFile fp(path + '/' + "Emoticons.plist");
00201
00202 if (!fp.open(QIODevice::WriteOnly)) {
00203 kWarning() << fp.fileName() << "can't open WriteOnly!";
00204 return;
00205 }
00206
00207 QDomDocumentType ty = QDomImplementation().createDocumentType("plist", "-//Apple Computer//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd");
00208 QDomDocument doc(ty);
00209 doc.appendChild(doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\""));
00210
00211 QDomElement plist = doc.createElement("plist");
00212 plist.setAttribute("version", "1.0");
00213 doc.appendChild(plist);
00214
00215 QDomElement dict = doc.createElement("dict");
00216 plist.appendChild(dict);
00217
00218 QDomElement el = doc.createElement("key");
00219 el.appendChild(doc.createTextNode("AdiumSetVersion"));
00220 dict.appendChild(el);
00221
00222 el = doc.createElement("integer");
00223 el.appendChild(doc.createTextNode("1"));
00224 dict.appendChild(el);
00225
00226 el = doc.createElement("key");
00227 el.appendChild(doc.createTextNode("Emoticons"));
00228 dict.appendChild(el);
00229
00230 dict.appendChild(doc.createElement("dict"));
00231
00232
00233 QTextStream emoStream(&fp);
00234 emoStream << doc.toString(4);
00235 fp.close();
00236 }
00237
00238