/********************************************************************************** * Copyright (C) 2008 by Carlo Segato * * * * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or (at your option) any later version. * * * * This library is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public * * License along with this library. If not, see .* * * **********************************************************************************/ #include "kde_emoticons.h" #include #include #include #include #include #include #include K_PLUGIN_FACTORY(KdeEmoticonsFactory, registerPlugin();) K_EXPORT_PLUGIN(KdeEmoticonsFactory("KdeEmoticons")) KdeEmoticons::KdeEmoticons(QObject *parent, const QVariantList &args) : KEmoticonsProvider(parent) { Q_UNUSED(args); } bool KdeEmoticons::removeEmoticon(const QString &emo) { QString emoticon = QFileInfo(emoticonsMap().key(emo.split(' '))).fileName(); QDomElement fce = m_themeXml.firstChildElement("messaging-emoticon-map"); if (fce.isNull()) return false; QDomNodeList nl = fce.childNodes(); // the lenght() method had a TODO which is done in Katie #ifdef QT_KATIE for (int i = 0; i < nl.length(); i++ ) #else for (uint i = 0; i < nl.length(); i++ ) #endif { QDomElement de = nl.item(i).toElement(); if (!de.isNull() && de.tagName() == "emoticon" && (de.attribute("file") == emoticon || de.attribute("file") == QFileInfo(emoticon).baseName())) { fce.removeChild(de); removeEmoticonsMap(emoticonsMap().key(emo.split(' '))); removeEmoticonIndex(emoticon, emo.split(' ')); return true; } } return false; } bool KdeEmoticons::addEmoticon(const QString &emo, const QString &text, AddEmoticonOption option) { KEmoticonsProvider::addEmoticon(emo, text, option); const QStringList splitted = text.split(' '); QDomElement fce = m_themeXml.firstChildElement("messaging-emoticon-map"); if (fce.isNull()) return false; QDomElement emoticon = m_themeXml.createElement("emoticon"); emoticon.setAttribute("file", QFileInfo(emo).fileName()); fce.appendChild(emoticon); foreach (const QString it, splitted) { QDomElement emoText = m_themeXml.createElement("string"); QDomText txt = m_themeXml.createTextNode(it.trimmed()); emoText.appendChild(txt); emoticon.appendChild(emoText); } addEmoticonIndex(emo, splitted); addEmoticonsMap(emo, splitted); return true; } void KdeEmoticons::save() { QFile fp(themePath() + '/' + fileName()); if (!fp.exists()) { kWarning() << fp.fileName() << "doesn't exist!"; return; } if (!fp.open(QIODevice::WriteOnly)) { kWarning() << fp.fileName() << "can't open WriteOnly!"; return; } QTextStream emoStream(&fp); emoStream.setCodec( "UTF-8" ); emoStream << m_themeXml.toString(4); fp.close(); } bool KdeEmoticons::loadTheme(const QString &path) { KEmoticonsProvider::loadTheme(path); QFile fp(path); if (!fp.exists()) { kWarning() << path << "doesn't exist!"; return false; } if (!fp.open(QIODevice::ReadOnly)) { kWarning() << fp.fileName() << "can't open ReadOnly!"; return false; } QString error; int eli, eco; if (!m_themeXml.setContent(&fp, &error, &eli, &eco)) { kWarning() << fp.fileName() << "can't copy to xml!"; kWarning() << error << "line:" << eli << "column:" << eco; fp.close(); return false; } fp.close(); QDomElement fce = m_themeXml.firstChildElement("messaging-emoticon-map"); if (fce.isNull()) return false; QDomNodeList nl = fce.childNodes(); clearEmoticonsMap(); // the lenght() method had a TODO which is done in Katie #ifdef QT_KATIE for (int i = 0; i < nl.length(); i++ ) #else for (uint i = 0; i < nl.length(); i++ ) #endif { QDomElement de = nl.item(i).toElement(); if (!de.isNull() && de.tagName() == "emoticon") { QDomNodeList snl = de.childNodes(); QStringList sl; #ifdef QT_KATIE for (int k = 0; i < snl.length(); k++ ) #else for (uint k = 0; i < snl.length(); k++ ) #endif { QDomElement sde = snl.item(k).toElement(); if (!sde.isNull() && sde.tagName() == "string") { sl << sde.text(); } } QString emo = KGlobal::dirs()->findResource("emoticons", themeName() + '/' + de.attribute("file")); if (emo.isEmpty()) { QList ext = QImageReader::supportedImageFormats(); for (int j = 0; j < ext.size(); ++j) { emo = KGlobal::dirs()->findResource("emoticons", themeName() + '/' + de.attribute("file") + '.' + ext.at(j)); if (!emo.isEmpty()) { break; } } if (emo.isEmpty()) { continue; } } addEmoticonIndex(emo, sl); addEmoticonsMap(emo, sl); } } return true; } void KdeEmoticons::createNew() { QString path = KGlobal::dirs()->saveLocation("emoticons", themeName()); QFile fp(path + '/' + "emoticons.xml"); if (!fp.open(QIODevice::WriteOnly)) { kWarning() << fp.fileName() << "can't open WriteOnly!"; return; } QDomDocument doc; doc.appendChild(doc.createProcessingInstruction("xml", "version=\"1.0\"")); doc.appendChild(doc.createElement("messaging-emoticon-map")); QTextStream emoStream(&fp); emoStream.setCodec( "UTF-8" ); emoStream << doc.toString(4); fp.close(); } // kate: space-indent on; indent-width 4; replace-tabs on;