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