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