1 /*
2 SPDX-FileCopyrightText: 2008 Carlo Segato <brandon.ml@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6
7 #include "pidgin_emoticons.h"
8 #include "kemoticonsprovider.h"
9
10 #include <QFile>
11 #include <QDir>
12 #include "kemoticons_plugin_pidgin_debug.h"
13 #include <QFileInfo>
14 #include <QStandardPaths>
15
16 #include <KPluginFactory>
17
18 K_PLUGIN_CLASS_WITH_JSON(PidginEmoticons, "emoticonstheme_pidgin.json")
19
PidginEmoticons(QObject * parent,const QVariantList & args)20 PidginEmoticons::PidginEmoticons(QObject *parent, const QVariantList &args)
21 : KEmoticonsProvider(parent)
22 {
23 Q_UNUSED(args);
24 }
25
removeEmoticon(const QString & emo)26 bool PidginEmoticons::removeEmoticon(const QString &emo)
27 {
28 QString emoticon = QFileInfo(emoticonsMap().key(emo.split(QLatin1Char(' ')))).fileName();
29
30 bool start = false;
31 for (int i = 0; i < m_text.size(); ++i) {
32 QString line = m_text.at(i);
33
34 if (line.startsWith(QLatin1Char('#')) || line.isEmpty()) {
35 continue;
36 }
37
38 QRegExp re(QStringLiteral("^\\[(.*)\\]$"));
39 int pos = re.indexIn(line.trimmed());
40 if (pos > -1) {
41 if (!re.cap(1).compare(QStringLiteral("default"), Qt::CaseInsensitive)) {
42 start = true;
43 } else {
44 start = false;
45 }
46 continue;
47 }
48
49 if (!start) {
50 continue;
51 }
52
53 const QStringList splitted = line.split(QLatin1Char(' '));
54 QString emoName;
55
56 if (splitted.at(0) == QLatin1Char('!')) {
57 emoName = splitted.at(1);
58 } else {
59 emoName = splitted.at(0);
60 }
61
62 if (emoName == emoticon) {
63 m_text.removeAt(i);
64 removeIndexItem(emoticon, emo.split(QLatin1Char(' ')));
65 return true;
66 }
67 }
68
69 return false;
70 }
71
addEmoticon(const QString & emo,const QString & text,AddEmoticonOption option)72 bool PidginEmoticons::addEmoticon(const QString &emo, const QString &text, AddEmoticonOption option)
73 {
74 if (option == Copy) {
75 bool result = copyEmoticon(emo);
76 if (!result) {
77 qCWarning(KEMOTICONS_PLUGIN_PIDGIN) << "There was a problem copying the emoticon";
78 return false;
79 }
80 }
81
82 const QStringList splitted = text.split(QLatin1Char(' '));
83 int i = m_text.indexOf(QRegExp(QStringLiteral("^\\[default\\]$"), Qt::CaseInsensitive));
84
85 if (i == -1) {
86 return false;
87 }
88
89 const QString emoticon = QStringLiteral("%1 %2").arg(QFileInfo(emo).fileName(),
90 text);
91 m_text.insert(i + 1, emoticon);
92
93 addIndexItem(emo, splitted);
94 addMapItem(emo, splitted);
95 return true;
96 }
97
saveTheme()98 void PidginEmoticons::saveTheme()
99 {
100 QFile fp(themePath() + QLatin1Char('/') + fileName());
101
102 if (!fp.exists()) {
103 qCWarning(KEMOTICONS_PLUGIN_PIDGIN) << fp.fileName() << "doesn't exist!";
104 return;
105 }
106
107 if (!fp.open(QIODevice::WriteOnly)) {
108 qCWarning(KEMOTICONS_PLUGIN_PIDGIN) << fp.fileName() << "can't open WriteOnly!";
109 return;
110 }
111
112 QTextStream emoStream(&fp);
113
114 if (m_text.indexOf(QRegExp(QStringLiteral("^Icon=.*"), Qt::CaseInsensitive)) == -1) {
115 int i = m_text.indexOf(QRegExp(QStringLiteral("^Description=.*"), Qt::CaseInsensitive));
116 QString file = QFileInfo(emoticonsMap().keys().value(0)).fileName();
117 m_text.insert(i + 1, QStringLiteral("Icon=") + file);
118 }
119
120 emoStream << m_text.join(QLatin1Char('\n'));
121 fp.close();
122 }
123
loadTheme(const QString & path)124 bool PidginEmoticons::loadTheme(const QString &path)
125 {
126 QFile file(path);
127
128 if (!file.exists()) {
129 qCWarning(KEMOTICONS_PLUGIN_PIDGIN) << path << "doesn't exist!";
130 return false;
131 }
132
133 setThemePath(path);
134 if (!file.open(QIODevice::ReadOnly)) {
135 qCWarning(KEMOTICONS_PLUGIN_PIDGIN) << file.fileName() << "can't be open ReadOnly!";
136 return false;
137 }
138
139 QTextStream str(&file);
140 bool start = false;
141 m_text.clear();
142 while (!str.atEnd()) {
143 QString line = str.readLine();
144 m_text << line;
145
146 if (line.startsWith(QLatin1Char('#')) || line.isEmpty()) {
147 continue;
148 }
149
150 QRegExp re(QStringLiteral("^\\[(.*)\\]$"));
151 int pos = re.indexIn(line.trimmed());
152 if (pos > -1) {
153 if (!re.cap(1).compare(QStringLiteral("default"), Qt::CaseInsensitive)) {
154 start = true;
155 } else {
156 start = false;
157 }
158 continue;
159 }
160
161 if (!start) {
162 continue;
163 }
164
165 QStringList splitted = line.split(QRegExp(QStringLiteral("\\s+")));
166 QString emo;
167 int i = 1;
168 if (splitted.at(0) == QLatin1Char('!')) {
169 i = 2;
170 emo = splitted.at(1);
171 } else {
172 emo = splitted.at(0);
173 }
174 emo = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QLatin1String("emoticons/") + themeName() + QLatin1Char('/') + emo);
175
176 QStringList sl;
177 for (; i < splitted.size(); ++i) {
178 if (!splitted.at(i).isEmpty() && splitted.at(i) != QLatin1String(" ")) {
179 sl << splitted.at(i);
180 }
181 }
182
183 addIndexItem(emo, sl);
184 addMapItem(emo, sl);
185 }
186
187 file.close();
188
189 return true;
190 }
191
newTheme()192 void PidginEmoticons::newTheme()
193 {
194 QString path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/emoticons/") + themeName();
195 QDir().mkpath(path);
196
197 QFile fp(path + QLatin1Char('/') + QStringLiteral("theme"));
198
199 if (!fp.open(QIODevice::WriteOnly)) {
200 qCWarning(KEMOTICONS_PLUGIN_PIDGIN) << fp.fileName() << "can't open WriteOnly!";
201 return;
202 }
203
204 QTextStream out(&fp);
205 out.setCodec("UTF-8");
206
207 out << QStringLiteral("Name=") + themeName() << "\n";
208 out << QStringLiteral("Description=") + themeName() << "\n";
209 out << "Author=\n";
210 out << "\n";
211 out << "[default]\n";
212
213 fp.close();
214 }
215
216 #include "pidgin_emoticons.moc"
217
218