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.setCodec( "UTF-8" );
00121 emoStream << m_themeXml.toString(4);
00122 fp.close();
00123 }
00124
00125 bool XmppEmoticons::loadTheme(const QString &path)
00126 {
00127 KEmoticonsProvider::loadTheme(path);
00128
00129 QFile fp(path);
00130
00131 if (!fp.exists()) {
00132 kWarning() << path << "doesn't exist!";
00133 return false;
00134 }
00135
00136 if (!fp.open(QIODevice::ReadOnly)) {
00137 kWarning() << fp.fileName() << "can't open ReadOnly!";
00138 return false;
00139 }
00140
00141 QString error;
00142 int eli, eco;
00143 if (!m_themeXml.setContent(&fp, &error, &eli, &eco)) {
00144 kWarning() << fp.fileName() << "can't copy to xml!";
00145 kWarning() << error << "line:" << eli << "column:" << eco;
00146 fp.close();
00147 return false;
00148 }
00149
00150 fp.close();
00151
00152 QDomElement fce = m_themeXml.firstChildElement("icondef");
00153
00154 if (fce.isNull()) {
00155 return false;
00156 }
00157
00158 QDomNodeList nl = fce.childNodes();
00159
00160 clearEmoticonsMap();
00161
00162 for (uint i = 0; i < nl.length(); i++) {
00163 QDomElement de = nl.item(i).toElement();
00164
00165 if (!de.isNull() && de.tagName() == "icon") {
00166 QDomNodeList snl = de.childNodes();
00167 QStringList sl;
00168 QString emo;
00169 QStringList mime;
00170 mime << "image/png" << "image/gif" << "image/bmp" << "image/jpeg";
00171
00172 for (uint k = 0; k < snl.length(); k++) {
00173 QDomElement sde = snl.item(k).toElement();
00174
00175 if (!sde.isNull() && sde.tagName() == "text") {
00176 sl << sde.text();
00177 } else if (!sde.isNull() && sde.tagName() == "object" && mime.contains(sde.attribute("mime"))) {
00178 emo = sde.text();
00179 }
00180 }
00181
00182 emo = KGlobal::dirs()->findResource("emoticons", themeName() + '/' + emo);
00183
00184 if (emo.isNull()) {
00185 continue;
00186 }
00187
00188 addEmoticonIndex(emo, sl);
00189 addEmoticonsMap(emo, sl);
00190 }
00191 }
00192
00193 return true;
00194 }
00195
00196 void XmppEmoticons::createNew()
00197 {
00198 QString path = KGlobal::dirs()->saveLocation("emoticons", themeName(), false);
00199
00200 QFile fp(path + '/' + "icondef.xml");
00201
00202 if (!fp.open(QIODevice::WriteOnly)) {
00203 kWarning() << fp.fileName() << "can't open WriteOnly!";
00204 return;
00205 }
00206
00207 QDomDocument doc;
00208 doc.appendChild(doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\""));
00209 doc.appendChild(doc.createElement("icondef"));
00210
00211 QTextStream emoStream(&fp);
00212 emoStream.setCodec( "UTF-8" );
00213 emoStream << doc.toString(4);
00214 fp.close();
00215 }
00216
00217