KUtils
pidgin_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 "pidgin_emoticons.h" 00020 00021 #include <QtCore/QFile> 00022 #include <QtCore/QFileInfo> 00023 00024 #include <kpluginfactory.h> 00025 #include <kdebug.h> 00026 #include <kstandarddirs.h> 00027 00028 K_PLUGIN_FACTORY(PidginEmoticonsFactory, registerPlugin<PidginEmoticons>();) 00029 K_EXPORT_PLUGIN(PidginEmoticonsFactory("PidginEmoticons")) 00030 00031 PidginEmoticons::PidginEmoticons(QObject *parent, const QVariantList &args) 00032 : KEmoticonsProvider(parent) 00033 { 00034 Q_UNUSED(args); 00035 } 00036 00037 bool PidginEmoticons::removeEmoticon(const QString &emo) 00038 { 00039 QString emoticon = QFileInfo(emoticonsMap().key(emo.split(' '))).fileName(); 00040 00041 bool start = false; 00042 for (int i = 0; i < m_text.size(); ++i) { 00043 QString line = m_text.at(i); 00044 00045 if (line.startsWith('#') || line.isEmpty()) { 00046 continue; 00047 } 00048 00049 QRegExp re("^\\[(.*)\\]$"); 00050 int pos = re.indexIn(line.trimmed()); 00051 if (pos > -1) { 00052 if (!re.cap(1).compare("default", Qt::CaseInsensitive)) { 00053 start = true; 00054 } else { 00055 start = false; 00056 } 00057 continue; 00058 } 00059 00060 if (!start) { 00061 continue; 00062 } 00063 00064 QStringList splitted = line.split(' '); 00065 QString emoName; 00066 00067 if (splitted.at(0) == "!") { 00068 emoName = splitted.at(1); 00069 } else { 00070 emoName = splitted.at(0); 00071 } 00072 00073 if (emoName == emoticon) { 00074 m_text.removeAt(i); 00075 removeEmoticonIndex(emoticon, emo.split(' ')); 00076 return true; 00077 } 00078 } 00079 00080 return false; 00081 } 00082 00083 bool PidginEmoticons::addEmoticon(const QString &emo, const QString &text, AddEmoticonOption option) 00084 { 00085 KEmoticonsProvider::addEmoticon(emo, text, option); 00086 00087 const QStringList splitted = text.split(' '); 00088 int i = m_text.indexOf(QRegExp("^\\[default\\]$", Qt::CaseInsensitive)); 00089 00090 if (i == -1) { 00091 return false; 00092 } 00093 00094 QString emoticon = QString("%1 %2").arg(QFileInfo(emo).fileName()).arg(text); 00095 m_text.insert(i + 1, emoticon); 00096 00097 addEmoticonIndex(emo, splitted); 00098 addEmoticonsMap(emo, splitted); 00099 return true; 00100 } 00101 00102 void PidginEmoticons::save() 00103 { 00104 QFile fp(themePath() + '/' + fileName()); 00105 00106 if (!fp.exists()) { 00107 kWarning() << fp.fileName() << "doesn't exist!"; 00108 return; 00109 } 00110 00111 if (!fp.open(QIODevice::WriteOnly)) { 00112 kWarning() << fp.fileName() << "can't open WriteOnly!"; 00113 return; 00114 } 00115 00116 QTextStream emoStream(&fp); 00117 00118 if (m_text.indexOf(QRegExp("^Icon=.*", Qt::CaseInsensitive)) == -1) { 00119 int i = m_text.indexOf(QRegExp("^Description=.*", Qt::CaseInsensitive)); 00120 QString file = QFileInfo(emoticonsMap().keys().value(0)).fileName(); 00121 m_text.insert(i + 1, "Icon=" + file); 00122 } 00123 00124 emoStream << m_text.join("\n"); 00125 fp.close(); 00126 } 00127 00128 bool PidginEmoticons::loadTheme(const QString &path) 00129 { 00130 KEmoticonsProvider::loadTheme(path); 00131 00132 QFile fp(path); 00133 00134 if (!fp.exists()) { 00135 kWarning() << path << "doesn't exist!"; 00136 return false; 00137 } 00138 00139 if (!fp.open(QIODevice::ReadOnly)) { 00140 kWarning() << fp.fileName() << "can't open ReadOnly!"; 00141 return false; 00142 } 00143 00144 QTextStream str(&fp); 00145 bool start = false; 00146 m_text.clear(); 00147 while (!str.atEnd()) { 00148 QString line = str.readLine(); 00149 m_text << line; 00150 00151 if (line.startsWith('#') || line.isEmpty()) { 00152 continue; 00153 } 00154 00155 QRegExp re("^\\[(.*)\\]$"); 00156 int pos = re.indexIn(line.trimmed()); 00157 if (pos > -1) { 00158 if (!re.cap(1).compare("default", Qt::CaseInsensitive)) { 00159 start = true; 00160 } else { 00161 start = false; 00162 } 00163 continue; 00164 } 00165 00166 if (!start) { 00167 continue; 00168 } 00169 00170 QStringList splitted = line.split(QRegExp("\\s+")); 00171 QString emo; 00172 int i = 1; 00173 if (splitted.at(0) == "!") { 00174 i = 2; 00175 emo = KGlobal::dirs()->findResource("emoticons", themeName() + '/' + splitted.at(1)); 00176 } else { 00177 emo = KGlobal::dirs()->findResource("emoticons", themeName() + '/' + splitted.at(0)); 00178 } 00179 00180 QStringList sl; 00181 for (; i < splitted.size(); ++i) { 00182 if (!splitted.at(i).isEmpty() && splitted.at(i) != " ") { 00183 sl << splitted.at(i); 00184 } 00185 } 00186 00187 addEmoticonIndex(emo, sl); 00188 addEmoticonsMap(emo, sl); 00189 } 00190 00191 fp.close(); 00192 00193 return true; 00194 } 00195 00196 void PidginEmoticons::createNew() 00197 { 00198 QString path = KGlobal::dirs()->saveLocation("emoticons", themeName()); 00199 00200 QFile fp(path + '/' + "theme"); 00201 00202 if (!fp.open(QIODevice::WriteOnly)) { 00203 kWarning() << fp.fileName() << "can't open WriteOnly!"; 00204 return; 00205 } 00206 00207 QTextStream out(&fp); 00208 out.setCodec( "UTF-8" ); 00209 00210 out << "Name=" + themeName() << endl; 00211 out << "Description=" + themeName() << endl; 00212 out << "Author=" << endl; 00213 out << endl; 00214 out << "[default]" << endl; 00215 00216 fp.close(); 00217 } 00218 00219 // kate: space-indent on; indent-width 4; replace-tabs on;
KDE 4.6 API Reference