• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KUtils

  • kutils
  • kemoticons
  • providers
  • pidgin
pidgin_emoticons.cpp
Go to the documentation of this file.
1/**********************************************************************************
2 * Copyright (C) 2008 by Carlo Segato <brandon.ml@gmail.com> *
3 * *
4 * This library is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * This library is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.*
16 * *
17 **********************************************************************************/
18
19#include "pidgin_emoticons.h"
20
21#include <QtCore/QFile>
22#include <QtCore/QFileInfo>
23
24#include <kpluginfactory.h>
25#include <kdebug.h>
26#include <kstandarddirs.h>
27
28K_PLUGIN_FACTORY(PidginEmoticonsFactory, registerPlugin<PidginEmoticons>();)
29K_EXPORT_PLUGIN(PidginEmoticonsFactory("PidginEmoticons"))
30
31PidginEmoticons::PidginEmoticons(QObject *parent, const QVariantList &args)
32 : KEmoticonsProvider(parent)
33{
34 Q_UNUSED(args);
35}
36
37bool PidginEmoticons::removeEmoticon(const QString &emo)
38{
39 QString emoticon = QFileInfo(emoticonsMap().key(emo.split(' '))).fileName();
40
41 bool start = false;
42 for (int i = 0; i < m_text.size(); ++i) {
43 QString line = m_text.at(i);
44
45 if (line.startsWith('#') || line.isEmpty()) {
46 continue;
47 }
48
49 QRegExp re("^\\[(.*)\\]$");
50 int pos = re.indexIn(line.trimmed());
51 if (pos > -1) {
52 if (!re.cap(1).compare("default", Qt::CaseInsensitive)) {
53 start = true;
54 } else {
55 start = false;
56 }
57 continue;
58 }
59
60 if (!start) {
61 continue;
62 }
63
64 QStringList splitted = line.split(' ');
65 QString emoName;
66
67 if (splitted.at(0) == "!") {
68 emoName = splitted.at(1);
69 } else {
70 emoName = splitted.at(0);
71 }
72
73 if (emoName == emoticon) {
74 m_text.removeAt(i);
75 removeEmoticonIndex(emoticon, emo.split(' '));
76 return true;
77 }
78 }
79
80 return false;
81}
82
83bool PidginEmoticons::addEmoticon(const QString &emo, const QString &text, AddEmoticonOption option)
84{
85 KEmoticonsProvider::addEmoticon(emo, text, option);
86
87 const QStringList splitted = text.split(' ');
88 int i = m_text.indexOf(QRegExp("^\\[default\\]$", Qt::CaseInsensitive));
89
90 if (i == -1) {
91 return false;
92 }
93
94 QString emoticon = QString("%1 %2").arg(QFileInfo(emo).fileName()).arg(text);
95 m_text.insert(i + 1, emoticon);
96
97 addEmoticonIndex(emo, splitted);
98 addEmoticonsMap(emo, splitted);
99 return true;
100}
101
102void PidginEmoticons::save()
103{
104 QFile fp(themePath() + '/' + fileName());
105
106 if (!fp.exists()) {
107 kWarning() << fp.fileName() << "doesn't exist!";
108 return;
109 }
110
111 if (!fp.open(QIODevice::WriteOnly)) {
112 kWarning() << fp.fileName() << "can't open WriteOnly!";
113 return;
114 }
115
116 QTextStream emoStream(&fp);
117
118 if (m_text.indexOf(QRegExp("^Icon=.*", Qt::CaseInsensitive)) == -1) {
119 int i = m_text.indexOf(QRegExp("^Description=.*", Qt::CaseInsensitive));
120 QString file = QFileInfo(emoticonsMap().keys().value(0)).fileName();
121 m_text.insert(i + 1, "Icon=" + file);
122 }
123
124 emoStream << m_text.join("\n");
125 fp.close();
126}
127
128bool PidginEmoticons::loadTheme(const QString &path)
129{
130 KEmoticonsProvider::loadTheme(path);
131
132 QFile fp(path);
133
134 if (!fp.exists()) {
135 kWarning() << path << "doesn't exist!";
136 return false;
137 }
138
139 if (!fp.open(QIODevice::ReadOnly)) {
140 kWarning() << fp.fileName() << "can't open ReadOnly!";
141 return false;
142 }
143
144 QTextStream str(&fp);
145 bool start = false;
146 m_text.clear();
147 while (!str.atEnd()) {
148 QString line = str.readLine();
149 m_text << line;
150
151 if (line.startsWith('#') || line.isEmpty()) {
152 continue;
153 }
154
155 QRegExp re("^\\[(.*)\\]$");
156 int pos = re.indexIn(line.trimmed());
157 if (pos > -1) {
158 if (!re.cap(1).compare("default", Qt::CaseInsensitive)) {
159 start = true;
160 } else {
161 start = false;
162 }
163 continue;
164 }
165
166 if (!start) {
167 continue;
168 }
169
170 QStringList splitted = line.split(QRegExp("\\s+"));
171 QString emo;
172 int i = 1;
173 if (splitted.at(0) == "!") {
174 i = 2;
175 emo = KGlobal::dirs()->findResource("emoticons", themeName() + '/' + splitted.at(1));
176 } else {
177 emo = KGlobal::dirs()->findResource("emoticons", themeName() + '/' + splitted.at(0));
178 }
179
180 QStringList sl;
181 for (; i < splitted.size(); ++i) {
182 if (!splitted.at(i).isEmpty() && splitted.at(i) != " ") {
183 sl << splitted.at(i);
184 }
185 }
186
187 addEmoticonIndex(emo, sl);
188 addEmoticonsMap(emo, sl);
189 }
190
191 fp.close();
192
193 return true;
194}
195
196void PidginEmoticons::createNew()
197{
198 QString path = KGlobal::dirs()->saveLocation("emoticons", themeName());
199
200 QFile fp(path + '/' + "theme");
201
202 if (!fp.open(QIODevice::WriteOnly)) {
203 kWarning() << fp.fileName() << "can't open WriteOnly!";
204 return;
205 }
206
207 QTextStream out(&fp);
208 out.setCodec( "UTF-8" );
209
210 out << "Name=" + themeName() << endl;
211 out << "Description=" + themeName() << endl;
212 out << "Author=" << endl;
213 out << endl;
214 out << "[default]" << endl;
215
216 fp.close();
217}
218
219// kate: space-indent on; indent-width 4; replace-tabs on;
KEmoticonsProvider
This is the base class for the emoticons provider plugins.
Definition: kemoticonsprovider.h:36
KEmoticonsProvider::AddEmoticonOption
AddEmoticonOption
Options to pass to addEmoticon.
Definition: kemoticonsprovider.h:53
KEmoticonsProvider::themePath
QString themePath() const
Returns the theme path.
Definition: kemoticonsprovider.cpp:94
KEmoticonsProvider::addEmoticonsMap
void addEmoticonsMap(QString key, QStringList value)
Insert a new item in the emoticons map.
Definition: kemoticonsprovider.cpp:109
KEmoticonsProvider::addEmoticonIndex
void addEmoticonIndex(const QString &path, const QStringList &emoList)
Add an emoticon to the index.
Definition: kemoticonsprovider.cpp:135
KEmoticonsProvider::fileName
QString fileName() const
Returns the file name of the theme.
Definition: kemoticonsprovider.cpp:99
KEmoticonsProvider::themeName
QString themeName() const
Returns the theme name.
Definition: kemoticonsprovider.cpp:84
KEmoticonsProvider::removeEmoticonIndex
void removeEmoticonIndex(const QString &path, const QStringList &emoList)
Remove an emoticon from the index.
Definition: kemoticonsprovider.cpp:158
KEmoticonsProvider::addEmoticon
virtual bool addEmoticon(const QString &emo, const QString &text, AddEmoticonOption option=DoNotCopy)
Add the emoticon emo with text text.
Definition: kemoticonsprovider.cpp:70
KEmoticonsProvider::emoticonsMap
QHash< QString, QStringList > emoticonsMap() const
Returns a QHash that contains the emoticons path as keys and the text as values.
Definition: kemoticonsprovider.cpp:121
KEmoticonsProvider::loadTheme
virtual bool loadTheme(const QString &path)
Load the theme inside the directory path.
Definition: kemoticonsprovider.cpp:55
K_EXPORT_PLUGIN
#define K_EXPORT_PLUGIN(factory)
KStandardDirs::saveLocation
QString saveLocation(const char *type, const QString &suffix=QString(), bool create=true) const
KStandardDirs::findResource
QString findResource(const char *type, const QString &filename) const
PidginEmoticons
Definition: pidgin_emoticons.h:25
PidginEmoticons::createNew
void createNew()
Create a new theme.
Definition: pidgin_emoticons.cpp:196
PidginEmoticons::addEmoticon
bool addEmoticon(const QString &emo, const QString &text, AddEmoticonOption option=DoNotCopy)
Add the emoticon emo with text text.
Definition: pidgin_emoticons.cpp:83
PidginEmoticons::removeEmoticon
bool removeEmoticon(const QString &emo)
Remove the emoticon emo, this will not delete the image file too.
Definition: pidgin_emoticons.cpp:37
PidginEmoticons::save
void save()
Save the emoticon theme.
Definition: pidgin_emoticons.cpp:102
PidginEmoticons::loadTheme
bool loadTheme(const QString &path)
Load the theme inside the directory path.
Definition: pidgin_emoticons.cpp:128
QObject
kWarning
#define kWarning
kdebug.h
kpluginfactory.h
kstandarddirs.h
KGlobal::dirs
KStandardDirs * dirs()
pidgin_emoticons.h
K_PLUGIN_FACTORY
K_PLUGIN_FACTORY(ProxyScoutFactory, registerPlugin< KPAC::ProxyScout >();) namespace KPAC
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KUtils

Skip menu "KUtils"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal