1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/* ============================================================
*
* This file is a part of the rekonq project
*
* Copyright (C) 2009 by Fredy Yanardi <fyanardi@gmail.com>
* Copyright (C) 2010-2011 by Lionel Chauvin <megabigbug@yahoo.fr>
* Copyright (C) 2010-2012 by Andrea Diamantini <adjam7 at gmail dot com>
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ============================================================ */
// Self Includes
#include "opensearchmanager.h"
#include "opensearchmanager.moc"
// Local Includes
#include "application.h"
#include "opensearchengine.h"
#include "opensearchreader.h"
#include "opensearchwriter.h"
// KDE Includes
#include <KGlobal>
#include <KStandardDirs>
#include <KUrl>
#include <kio/scheduler.h>
#include <KService>
#include <KDE/KMessageBox>
#include <KUriFilterData>
#include <KConfigGroup>
// Qt Includes
#include <QFile>
#include <QFileInfo>
#include <QDBusMessage>
#include <QDBusConnection>
OpenSearchManager::OpenSearchManager(QObject *parent)
: QObject(parent)
, m_activeEngine(0)
, m_currentJob(0)
{
m_state = IDLE;
loadEngines();
}
OpenSearchManager::~OpenSearchManager()
{
qDeleteAll(m_engineCache);
m_engineCache.clear();
m_engines.clear();
}
void OpenSearchManager::setSearchProvider(const QString &searchProvider)
{
m_activeEngine = 0;
if (!m_engineCache.contains(searchProvider))
{
const QString fileName = KGlobal::dirs()->findResource("data", "rekonq/opensearch/" + trimmedEngineName(searchProvider) + ".xml");
kDebug() << searchProvider << " trimmed name: " << trimmedEngineName(searchProvider) << " file name path: " << fileName;
if (fileName.isEmpty())
{
return;
}
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return;
}
OpenSearchReader reader;
OpenSearchEngine *engine = reader.read(&file);
if (engine)
{
m_engineCache.insert(searchProvider, engine);
}
else
{
return;
}
}
m_activeEngine = m_engineCache.value(searchProvider);
}
bool OpenSearchManager::isSuggestionAvailable()
{
return m_activeEngine != 0;
}
void OpenSearchManager::addOpenSearchEngine(const KUrl &url, const QString &title, const QString &shortcut)
{
m_shortcut = shortcut;
m_title = trimmedEngineName(title);
if (m_state != IDLE)
{
idleJob();
}
m_currentJob = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo);
m_jobUrl = url;
m_state = REQ_DESCRIPTION;
connect(m_currentJob, SIGNAL(data(KIO::Job*,QByteArray)), this, SLOT(dataReceived(KIO::Job*,QByteArray)));
connect(m_currentJob, SIGNAL(result(KJob*)), this, SLOT(jobFinished(KJob*)));
}
void OpenSearchManager::requestSuggestion(const QString &searchText)
{
if (!m_activeEngine)
return;
if (m_state != IDLE)
{
// NOTE:
// changing OpenSearchManager behavior
// using idleJob here lets opensearchmanager to start another search, while
// if we want in any case lets it finish its previous job we can just return here.
idleJob();
}
if (m_activeEngine->hasCachedSuggestionsFor(searchText))
{
emit suggestionsReceived(searchText, m_activeEngine->cachedSuggestionsFor(searchText));
}
else
{
KUrl url = m_activeEngine->suggestionsUrl(searchText);
_typedText = searchText;
m_currentJob = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo);
m_state = REQ_SUGGESTION;
connect(m_currentJob, SIGNAL(data(KIO::Job*,QByteArray)), this, SLOT(dataReceived(KIO::Job*,QByteArray)));
connect(m_currentJob, SIGNAL(result(KJob*)), this, SLOT(jobFinished(KJob*)));
}
}
void OpenSearchManager::dataReceived(KIO::Job *job, const QByteArray &data)
{
Q_UNUSED(job);
m_jobData.append(data);
}
void OpenSearchManager::jobFinished(KJob *job)
{
if (!job->error() && m_state == REQ_DESCRIPTION)
{
OpenSearchReader reader;
OpenSearchEngine *engine = reader.read(m_jobData);
if (engine)
{
m_engineCache.insert(m_title, engine);
m_engines.insert(m_jobUrl, m_shortcut);
saveEngines();
QString path;
if (engine->providesSuggestions())
{
// save opensearch description only if it provides suggestions
OpenSearchWriter writer;
path = KGlobal::dirs()->findResource("data", "rekonq/opensearch/");
QFile file(path + trimmedEngineName(engine->name()) + ".xml");
writer.write(&file, engine);
// save desktop file here
QString searchUrl = OpenSearchEngine::parseTemplate("\\{@}", engine->searchUrlTemplate());
m_currentJob = NULL;
path = KGlobal::mainComponent().dirs()->saveLocation("services", "searchproviders/");
KConfig _service(path + m_title + ".desktop", KConfig::SimpleConfig);
KConfigGroup service(&_service, "Desktop Entry");
service.writeEntry("Type", "Service");
service.writeEntry("ServiceTypes", "SearchProvider");
service.writeEntry("Name", m_title);
service.writeEntry("Query", searchUrl);
service.writeEntry("Keys", m_shortcut);
// TODO charset
service.writeEntry("Charset", "" /* provider->charset() */);
// we might be overwriting a hidden entry
service.writeEntry("Hidden", false);
service.sync();
// Update filters in running applications...
QDBusMessage msg = QDBusMessage::createSignal("/", "org.kde.KUriFilterPlugin", "configure");
QDBusConnection::sessionBus().send(msg);
emit openSearchEngineAdded(engine->name());
}
}
else
{
kFatal() << "Error while adding new open search engine";
}
idleJob();
return;
}
// Do NOT parse if job had same errors or the typed string is empty
if (job->error() || _typedText.isEmpty())
{
emit suggestionsReceived(_typedText, ResponseList());
m_state = IDLE;
return; // just silently return
}
if (m_state == REQ_SUGGESTION)
{
ResponseList suggestionsList;
if (isSuggestionAvailable())
{
suggestionsList = m_activeEngine->parseSuggestion(_typedText, m_jobData);
}
emit suggestionsReceived(_typedText, suggestionsList);
idleJob();
return;
}
}
void OpenSearchManager::loadEngines()
{
QFile file(KStandardDirs::locate("appdata", "opensearch/db_opensearch.json"));
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return;
}
QString fileContent = QString::fromUtf8(file.readAll());
QScriptEngine reader;
if (!reader.canEvaluate(fileContent))
{
return;
}
QScriptValue responseParts = reader.evaluate(fileContent);
QVariantList list;
qScriptValueToSequence(responseParts, list);
QStringList l;
Q_FOREACH(const QVariant & e, list)
{
l = e.toStringList();
m_engines.insert(KUrl(l.first()), l.last());
}
file.close();
}
void OpenSearchManager::saveEngines()
{
QFile file(KStandardDirs::locateLocal("appdata", "opensearch/db_opensearch.json"));
if (!file.open(QIODevice::WriteOnly))
{
return;
}
QTextStream out(&file);
out << "[";
int i = 0;
QList<KUrl> urls = m_engines.keys();
Q_FOREACH(const KUrl & url, urls)
{
out << "[\"" << url.url() << "\",\"" << m_engines.value(url) << "\"]";
i++;
if (i != urls.size())
{
out << ",\n";
}
}
out << "]\n";
file.close();
}
void OpenSearchManager::removeDeletedEngines()
{
KService::Ptr service;
Q_FOREACH(const KUrl & url, m_engines.keys())
{
service = KService::serviceByDesktopPath(QString("searchproviders/%1.desktop").arg(m_engines.value(url)));
if (!service)
{
QString path = KStandardDirs::locateLocal("appdata", "opensearch/" + trimmedEngineName(m_engines.value(url)) + ".xml");
QFile::remove(path + trimmedEngineName(m_engines.value(url)) + ".xml");
m_engines.remove(url);
}
}
saveEngines();
}
bool OpenSearchManager::engineExists(const KUrl &url)
{
return m_engines.contains(url);
}
QString OpenSearchManager::trimmedEngineName(const QString &engineName) const
{
QString trimmed;
QString::ConstIterator constIter = engineName.constBegin();
while (constIter != engineName.constEnd())
{
if (constIter->isSpace())
{
trimmed.append('_');
}
else
{
if (*constIter != '.')
{
trimmed.append(constIter->toLower());
}
}
constIter++;
}
return trimmed;
}
void OpenSearchManager::idleJob()
{
if (m_currentJob)
{
disconnect(m_currentJob);
m_currentJob->kill();
}
m_jobData.clear();
m_state = IDLE;
}
|