KUtils
kde_emoticons.cpp
Go to the documentation of this file.
00001 /********************************************************************************** 00002 * Copyright (C) 2008 by Carlo Segato <brandon.ml@gmail.com> * 00003 * * 00004 * This library is free software; you can redistribute it and/or * 00005 * modify it under the terms of the GNU Lesser General Public * 00006 * License as published by the Free Software Foundation; either * 00007 * version 2.1 of the License, or (at your option) any later version. * 00008 * * 00009 * This library is distributed in the hope that it will be useful, * 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00012 * Lesser General Public License for more details. * 00013 * * 00014 * You should have received a copy of the GNU Lesser General Public * 00015 * License along with this library. If not, see <http://www.gnu.org/licenses/>.* 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.setCodec( "UTF-8" ); 00102 emoStream << m_themeXml.toString(4); 00103 fp.close(); 00104 } 00105 00106 bool KdeEmoticons::loadTheme(const QString &path) 00107 { 00108 KEmoticonsProvider::loadTheme(path); 00109 00110 QFile fp(path); 00111 00112 if (!fp.exists()) { 00113 kWarning() << path << "doesn't exist!"; 00114 return false; 00115 } 00116 00117 if (!fp.open(QIODevice::ReadOnly)) { 00118 kWarning() << fp.fileName() << "can't open ReadOnly!"; 00119 return false; 00120 } 00121 00122 QString error; 00123 int eli, eco; 00124 if (!m_themeXml.setContent(&fp, &error, &eli, &eco)) { 00125 kWarning() << fp.fileName() << "can't copy to xml!"; 00126 kWarning() << error << "line:" << eli << "column:" << eco; 00127 fp.close(); 00128 return false; 00129 } 00130 00131 fp.close(); 00132 00133 QDomElement fce = m_themeXml.firstChildElement("messaging-emoticon-map"); 00134 00135 if (fce.isNull()) 00136 return false; 00137 00138 QDomNodeList nl = fce.childNodes(); 00139 00140 clearEmoticonsMap(); 00141 00142 for (uint i = 0; i < nl.length(); i++) { 00143 QDomElement de = nl.item(i).toElement(); 00144 00145 if (!de.isNull() && de.tagName() == "emoticon") { 00146 QDomNodeList snl = de.childNodes(); 00147 QStringList sl; 00148 00149 for (uint k = 0; k < snl.length(); k++) { 00150 QDomElement sde = snl.item(k).toElement(); 00151 00152 if (!sde.isNull() && sde.tagName() == "string") { 00153 sl << sde.text(); 00154 } 00155 } 00156 00157 QString emo = KGlobal::dirs()->findResource("emoticons", themeName() + '/' + de.attribute("file")); 00158 00159 if (emo.isEmpty()) { 00160 QList<QByteArray> ext = QImageReader::supportedImageFormats(); 00161 00162 for (int j = 0; j < ext.size(); ++j) { 00163 emo = KGlobal::dirs()->findResource("emoticons", themeName() + '/' + de.attribute("file") + '.' + ext.at(j)); 00164 if (!emo.isEmpty()) { 00165 break; 00166 } 00167 } 00168 00169 if (emo.isEmpty()) { 00170 continue; 00171 } 00172 } 00173 00174 addEmoticonIndex(emo, sl); 00175 addEmoticonsMap(emo, sl); 00176 } 00177 } 00178 00179 return true; 00180 } 00181 00182 void KdeEmoticons::createNew() 00183 { 00184 QString path = KGlobal::dirs()->saveLocation("emoticons", themeName()); 00185 00186 QFile fp(path + '/' + "emoticons.xml"); 00187 00188 if (!fp.open(QIODevice::WriteOnly)) { 00189 kWarning() << fp.fileName() << "can't open WriteOnly!"; 00190 return; 00191 } 00192 00193 QDomDocument doc; 00194 doc.appendChild(doc.createProcessingInstruction("xml", "version=\"1.0\"")); 00195 doc.appendChild(doc.createElement("messaging-emoticon-map")); 00196 00197 QTextStream emoStream(&fp); 00198 emoStream.setCodec( "UTF-8" ); 00199 emoStream << doc.toString(4); 00200 fp.close(); 00201 } 00202 00203 // kate: space-indent on; indent-width 4; replace-tabs on;
KDE 4.6 API Reference