KUtils
xmpp_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 "xmpp_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 #include <kmimetype.h>
00029
00030 K_PLUGIN_FACTORY(XmppEmoticonsFactory, registerPlugin<XmppEmoticons>();)
00031 K_EXPORT_PLUGIN(XmppEmoticonsFactory("XmppEmoticons"))
00032
00033 XmppEmoticons::XmppEmoticons(QObject *parent, const QVariantList &args)
00034 : KEmoticonsProvider(parent)
00035 {
00036 Q_UNUSED(args);
00037 }
00038
00039 bool XmppEmoticons::removeEmoticon(const QString &emo)
00040 {
00041 QString emoticon = QFileInfo(emoticonsMap().key(emo.split(" "))).fileName();
00042 QDomElement fce = m_themeXml.firstChildElement("icondef");
00043
00044 if (fce.isNull())
00045 return false;
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() == "icon") {
00051 QDomNodeList snl = de.childNodes();
00052 QStringList sl;
00053 QStringList mime;
00054
00055 for (uint k = 0; k < snl.length(); k++) {
00056 QDomElement sde = snl.item(k).toElement();
00057
00058 if (!sde.isNull() && sde.tagName() == "object" && sde.text() == emoticon) {
00059 fce.removeChild(de);
00060 removeEmoticonsMap(emoticonsMap().key(emo.split(" ")));
00061 removeEmoticonIndex(emoticon, emo.split(" "));
00062 return true;
00063 }
00064 }
00065 }
00066 }
00067 return false;
00068 }
00069
00070 bool XmppEmoticons::addEmoticon(const QString &emo, const QString &text, AddEmoticonOption option)
00071 {
00072 KEmoticonsProvider::addEmoticon(emo, text, option);
00073
00074 const QStringList splitted = text.split(" ");
00075 QDomElement fce = m_themeXml.firstChildElement("icondef");
00076
00077 if (fce.isNull()) {
00078 return false;
00079 }
00080
00081 QDomElement emoticon = m_themeXml.createElement("icon");
00082 fce.appendChild(emoticon);
00083 QStringList::const_iterator constIterator;
00084
00085 for (constIterator = splitted.begin(); constIterator != splitted.end(); constIterator++) {
00086 QDomElement emotext = m_themeXml.createElement("text");
00087 QDomText txt = m_themeXml.createTextNode((*constIterator).trimmed());
00088 emotext.appendChild(txt);
00089 emoticon.appendChild(emotext);
00090 }
00091
00092 QDomElement emoElement = m_themeXml.createElement("object");
00093 KMimeType::Ptr mimePtr = KMimeType::findByPath(emo, 0, true);
00094 emoElement.setAttribute("mime", mimePtr->name());
00095 QDomText txt = m_themeXml.createTextNode(QFileInfo(emo).fileName());
00096
00097 emoElement.appendChild(txt);
00098 emoticon.appendChild(emoElement);
00099
00100 addEmoticonIndex(emo, splitted);
00101 addEmoticonsMap(emo, splitted);
00102 return true;
00103 }
00104
00105 void XmppEmoticons::save()
00106 {
00107 QFile fp(themePath() + '/' + fileName());
00108
00109 if (!fp.exists()) {
00110 kWarning() << fp.fileName() << "doesn't exist!";
00111 return;
00112 }
00113
00114 if (!fp.open(QIODevice::WriteOnly)) {
00115 kWarning() << fp.fileName() << "can't open WriteOnly!";
00116 return;
00117 }
00118
00119 QTextStream emoStream(&fp);
00120 emoStream << m_themeXml.toString(4);
00121 fp.close();
00122 }
00123
00124 bool XmppEmoticons::loadTheme(const QString &path)
00125 {
00126 KEmoticonsProvider::loadTheme(path);
00127
00128 QFile fp(path);
00129
00130 if (!fp.exists()) {
00131 kWarning() << path << "doesn't exist!";
00132 return false;
00133 }
00134
00135 if (!fp.open(QIODevice::ReadOnly)) {
00136 kWarning() << fp.fileName() << "can't open ReadOnly!";
00137 return false;
00138 }
00139
00140 QString error;
00141 int eli, eco;
00142 if (!m_themeXml.setContent(&fp, &error, &eli, &eco)) {
00143 kWarning() << fp.fileName() << "can't copy to xml!";
00144 kWarning() << error << "line:" << eli << "column:" << eco;
00145 fp.close();
00146 return false;
00147 }
00148
00149 fp.close();
00150
00151 QDomElement fce = m_themeXml.firstChildElement("icondef");
00152
00153 if (fce.isNull()) {
00154 return false;
00155 }
00156
00157 QDomNodeList nl = fce.childNodes();
00158
00159 clearEmoticonsMap();
00160
00161 for (uint i = 0; i < nl.length(); i++) {
00162 QDomElement de = nl.item(i).toElement();
00163
00164 if (!de.isNull() && de.tagName() == "icon") {
00165 QDomNodeList snl = de.childNodes();
00166 QStringList sl;
00167 QString emo;
00168 QStringList mime;
00169 mime << "image/png" << "image/gif" << "image/bmp" << "image/jpeg";
00170
00171 for (uint k = 0; k < snl.length(); k++) {
00172 QDomElement sde = snl.item(k).toElement();
00173
00174 if (!sde.isNull() && sde.tagName() == "text") {
00175 sl << sde.text();
00176 } else if (!sde.isNull() && sde.tagName() == "object" && mime.contains(sde.attribute("mime"))) {
00177 emo = sde.text();
00178 }
00179 }
00180
00181 emo = KGlobal::dirs()->findResource("emoticons", themeName() + '/' + emo);
00182
00183 if (emo.isNull()) {
00184 continue;
00185 }
00186
00187 addEmoticonIndex(emo, sl);
00188 addEmoticonsMap(emo, sl);
00189 }
00190 }
00191
00192 return true;
00193 }
00194
00195 void XmppEmoticons::createNew()
00196 {
00197 QString path = KGlobal::dirs()->saveLocation("emoticons", themeName(), false);
00198
00199 QFile fp(path + '/' + "icondef.xml");
00200
00201 if (!fp.open(QIODevice::WriteOnly)) {
00202 kWarning() << fp.fileName() << "can't open WriteOnly!";
00203 return;
00204 }
00205
00206 QDomDocument doc;
00207 doc.appendChild(doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\""));
00208 doc.appendChild(doc.createElement("icondef"));
00209
00210 QTextStream emoStream(&fp);
00211 emoStream << doc.toString(4);
00212 fp.close();
00213 }
00214
00215