-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathsearchenginesmanager.cpp
511 lines (403 loc) · 14 KB
/
searchenginesmanager.cpp
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.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 3 of the License, or
* (at your option) any later version.
*
* 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/>.
* ============================================================ */
#include "searchenginesmanager.h"
#include "searchenginesdialog.h"
#include "editsearchengine.h"
#include "networkmanager.h"
#include "iconprovider.h"
#include "mainapplication.h"
#include "opensearchreader.h"
#include "opensearchengine.h"
#include "settings.h"
#include "qzsettings.h"
#include "webview.h"
#include "sqldatabase.h"
#include <QNetworkReply>
#include <QMessageBox>
#include <QBuffer>
#include <QUrlQuery>
#define ENSURE_LOADED if (!m_settingsLoaded) loadSettings();
static QIcon iconFromBase64(const QByteArray &data)
{
QIcon image;
QByteArray bArray = QByteArray::fromBase64(data);
QBuffer buffer(&bArray);
buffer.open(QIODevice::ReadOnly);
QDataStream in(&buffer);
in >> image;
buffer.close();
if (!image.isNull()) {
return image;
}
return IconProvider::emptyWebIcon();
}
static QByteArray iconToBase64(const QIcon &icon)
{
QByteArray bArray;
QBuffer buffer(&bArray);
buffer.open(QIODevice::WriteOnly);
QDataStream out(&buffer);
out << icon;
buffer.close();
return bArray.toBase64();
}
SearchEnginesManager::SearchEnginesManager(QObject* parent)
: QObject(parent)
, m_settingsLoaded(false)
, m_saveScheduled(false)
{
Settings settings;
settings.beginGroup("SearchEngines");
m_startingEngineName = settings.value("activeEngine", "DuckDuckGo").toString();
m_defaultEngineName = settings.value("DefaultEngine", "DuckDuckGo").toString();
settings.endGroup();
connect(this, &SearchEnginesManager::enginesChanged, this, &SearchEnginesManager::scheduleSave);
}
void SearchEnginesManager::loadSettings()
{
m_settingsLoaded = true;
QSqlQuery query(SqlDatabase::instance()->database());
query.exec("SELECT name, icon, url, shortcut, suggestionsUrl, suggestionsParameters, postData FROM search_engines");
while (query.next()) {
Engine en;
en.name = query.value(0).toString();
en.icon = iconFromBase64(query.value(1).toByteArray());
en.url = query.value(2).toString();
en.shortcut = query.value(3).toString();
en.suggestionsUrl = query.value(4).toString();
en.suggestionsParameters = query.value(5).toByteArray();
en.postData = query.value(6).toByteArray();
m_allEngines.append(en);
if (en.name == m_defaultEngineName) {
m_defaultEngine = en;
}
}
if (m_allEngines.isEmpty()) {
restoreDefaults();
}
if (m_defaultEngine.name.isEmpty()) {
m_defaultEngine = m_allEngines[0];
}
}
SearchEngine SearchEnginesManager::engineForShortcut(const QString &shortcut)
{
Engine returnEngine;
if (shortcut.isEmpty()) {
return returnEngine;
}
for (const Engine &en : qAsConst(m_allEngines)) {
if (en.shortcut == shortcut) {
returnEngine = en;
break;
}
}
return returnEngine;
}
LoadRequest SearchEnginesManager::searchResult(const Engine &engine, const QString &string)
{
ENSURE_LOADED;
// GET search engine
if (engine.postData.isEmpty()) {
QByteArray url = engine.url.toUtf8();
url.replace("%s", QUrl::toPercentEncoding(string));
return LoadRequest(QUrl::fromEncoded(url));
}
// POST search engine
QByteArray data = engine.postData;
data.replace("%s", QUrl::toPercentEncoding(string));
return LoadRequest(QUrl::fromEncoded(engine.url.toUtf8()), LoadRequest::PostOperation, data);
}
LoadRequest SearchEnginesManager::searchResult(const QString &string)
{
ENSURE_LOADED;
const Engine en = qzSettings->searchWithDefaultEngine ? m_defaultEngine : m_activeEngine;
return searchResult(en, string);
}
void SearchEnginesManager::restoreDefaults()
{
Engine duck;
duck.name = "DuckDuckGo";
duck.icon = QIcon(":/icons/sites/duck.png");
duck.url = "https://duckduckgo.com/?q=%s&t=qupzilla";
duck.shortcut = "d";
duck.suggestionsUrl = "https://ac.duckduckgo.com/ac/?q=%s&type=list";
Engine sp;
sp.name = "StartPage";
sp.icon = QIcon(":/icons/sites/startpage.png");
sp.url = "https://startpage.com/do/search";
sp.postData = "query=%s&cat=web&language=english";
sp.shortcut = "sp";
sp.suggestionsUrl = "https://startpage.com/cgi-bin/csuggest?output=json&lang=english&query=%s";
Engine wiki;
wiki.name = "Wikipedia (en)";
wiki.icon = QIcon(":/icons/sites/wikipedia.png");
wiki.url = "https://en.wikipedia.org/wiki/Special:Search?search=%s&fulltext=Search";
wiki.shortcut = "w";
wiki.suggestionsUrl = "https://en.wikipedia.org/w/api.php?action=opensearch&search=%s&namespace=0";
Engine google;
google.name = "Google";
google.icon = QIcon(":icons/sites/google.png");
google.url = "https://www.google.com/search?client=falkon&q=%s";
google.shortcut = "g";
google.suggestionsUrl = "https://suggestqueries.google.com/complete/search?output=firefox&q=%s";
addEngine(duck);
addEngine(sp);
addEngine(wiki);
addEngine(google);
m_defaultEngine = duck;
emit enginesChanged();
}
// static
QIcon SearchEnginesManager::iconForSearchEngine(const QUrl &url)
{
QIcon ic = IconProvider::iconForDomain(url);
if (ic.isNull()) {
ic = QIcon::fromTheme(QSL("edit-find"), QIcon(QSL(":icons/menu/search-icon.svg")));
}
return ic;
}
void SearchEnginesManager::engineChangedImage()
{
OpenSearchEngine* engine = qobject_cast<OpenSearchEngine*>(sender());
if (!engine) {
return;
}
for (Engine e : qAsConst(m_allEngines)) {
if (e.name == engine->name() &&
e.url.contains(engine->searchUrl("%s").toString()) &&
!engine->image().isNull()
) {
int index = m_allEngines.indexOf(e);
if (index != -1) {
m_allEngines[index].icon = QIcon(QPixmap::fromImage(engine->image()));
emit enginesChanged();
delete engine;
break;
}
}
}
}
void SearchEnginesManager::editEngine(const Engine &before, const Engine &after)
{
removeEngine(before);
addEngine(after);
}
void SearchEnginesManager::addEngine(const Engine &engine)
{
ENSURE_LOADED;
if (m_allEngines.contains(engine)) {
return;
}
m_allEngines.append(engine);
emit enginesChanged();
}
void SearchEnginesManager::addEngineFromForm(const QVariantMap &formData, WebView *view)
{
if (formData.isEmpty())
return;
const QString method = formData.value(QSL("method")).toString();
bool isPost = method == QL1S("post");
QUrl actionUrl = formData.value(QSL("action")).toUrl();
if (actionUrl.isRelative()) {
actionUrl = view->url().resolved(actionUrl);
}
QUrl parameterUrl = actionUrl;
if (isPost) {
parameterUrl = QUrl("http://foo.bar");
}
const QString &inputName = formData.value(QSL("inputName")).toString();
QUrlQuery query(parameterUrl);
query.addQueryItem(inputName, QSL("SEARCH"));
const QVariantList &inputs = formData.value(QSL("inputs")).toList();
for (const QVariant &pair : inputs) {
const QVariantList &list = pair.toList();
if (list.size() != 2)
continue;
const QString &name = list.at(0).toString();
const QString &value = list.at(1).toString();
if (name == inputName || name.isEmpty() || value.isEmpty())
continue;
query.addQueryItem(name, value);
}
parameterUrl.setQuery(query);
if (!isPost) {
actionUrl = parameterUrl;
}
SearchEngine engine;
engine.name = view->title();
engine.icon = view->icon();
engine.url = actionUrl.toEncoded();
if (isPost) {
QByteArray data = parameterUrl.toEncoded(QUrl::RemoveScheme);
engine.postData = data.contains('?') ? data.mid(data.lastIndexOf('?') + 1) : QByteArray();
engine.postData.replace((inputName + QL1S("=SEARCH")).toUtf8(), (inputName + QL1S("=%s")).toUtf8());
} else {
engine.url.replace(inputName + QL1S("=SEARCH"), inputName + QL1S("=%s"));
}
EditSearchEngine dialog(SearchEnginesDialog::tr("Add Search Engine"), view);
dialog.setName(engine.name);
dialog.setIcon(engine.icon);
dialog.setUrl(engine.url);
dialog.setPostData(engine.postData);
if (dialog.exec() != QDialog::Accepted) {
return;
}
engine.name = dialog.name();
engine.icon = dialog.icon();
engine.url = dialog.url();
engine.shortcut = dialog.shortcut();
engine.postData = dialog.postData().toUtf8();
if (engine.name.isEmpty() || engine.url.isEmpty()) {
return;
}
addEngine(engine);
}
void SearchEnginesManager::addEngine(OpenSearchEngine* engine)
{
ENSURE_LOADED;
Engine en;
en.name = engine->name();
en.url = engine->searchUrl("searchstring").toString().replace(QLatin1String("searchstring"), QLatin1String("%s"));
if (engine->image().isNull()) {
en.icon = iconForSearchEngine(engine->searchUrl(QString()));
}
else {
en.icon = QIcon(QPixmap::fromImage(engine->image()));
}
en.suggestionsUrl = engine->getSuggestionsUrl();
en.suggestionsParameters = engine->getSuggestionsParameters();
en.postData = engine->getPostData("searchstring").replace("searchstring", "%s");
addEngine(en);
connect(engine, &OpenSearchEngine::imageChanged, this, &SearchEnginesManager::engineChangedImage);
}
void SearchEnginesManager::addEngine(const QUrl &url)
{
ENSURE_LOADED;
if (!url.isValid()) {
return;
}
qApp->setOverrideCursor(Qt::WaitCursor);
QNetworkReply* reply = mApp->networkManager()->get(QNetworkRequest(url));
reply->setParent(this);
connect(reply, &QNetworkReply::finished, this, &SearchEnginesManager::replyFinished);
}
void SearchEnginesManager::replyFinished()
{
qApp->restoreOverrideCursor();
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
if (!reply) {
return;
}
if (reply->error() != QNetworkReply::NoError) {
reply->close();
reply->deleteLater();
return;
}
OpenSearchReader reader;
OpenSearchEngine* engine = reader.read(reply);
engine->setNetworkAccessManager(mApp->networkManager());
reply->close();
reply->deleteLater();
if (checkEngine(engine)) {
addEngine(engine);
QMessageBox::information(0, tr("Search Engine Added"), tr("Search Engine \"%1\" has been successfully added.").arg(engine->name()));
}
}
bool SearchEnginesManager::checkEngine(OpenSearchEngine* engine)
{
if (!engine->isValid()) {
QString errorString = tr("Search Engine is not valid!");
QMessageBox::warning(0, tr("Error"), tr("Error while adding Search Engine <br><b>Error Message: </b> %1").arg(errorString));
return false;
}
return true;
}
void SearchEnginesManager::setActiveEngine(const Engine &engine)
{
ENSURE_LOADED;
if (!m_allEngines.contains(engine)) {
return;
}
m_activeEngine = engine;
emit activeEngineChanged();
}
void SearchEnginesManager::setDefaultEngine(const SearchEnginesManager::Engine &engine)
{
ENSURE_LOADED;
if (!m_allEngines.contains(engine)) {
return;
}
m_defaultEngine = engine;
emit defaultEngineChanged();
}
void SearchEnginesManager::removeEngine(const Engine &engine)
{
ENSURE_LOADED;
int index = m_allEngines.indexOf(engine);
if (index < 0) {
return;
}
QSqlQuery query(SqlDatabase::instance()->database());
query.prepare("DELETE FROM search_engines WHERE name=? AND url=?");
query.bindValue(0, engine.name);
query.bindValue(1, engine.url);
query.exec();
m_allEngines.remove(index);
emit enginesChanged();
}
void SearchEnginesManager::setAllEngines(const QVector<Engine> &engines)
{
ENSURE_LOADED;
m_allEngines = engines;
emit enginesChanged();
}
QVector<SearchEngine> SearchEnginesManager::allEngines()
{
ENSURE_LOADED;
return m_allEngines;
}
void SearchEnginesManager::saveSettings()
{
Settings settings;
settings.beginGroup("SearchEngines");
settings.setValue("activeEngine", m_activeEngine.name);
settings.setValue("DefaultEngine", m_defaultEngine.name);
settings.endGroup();
if (!m_saveScheduled) {
return;
}
// Well, this is not the best implementation to do as this is taking some time.
// Actually, it is delaying the quit of app for about a 1 sec on my machine with only
// 5 engines. Another problem is that deleting rows without VACUUM isn't actually freeing
// space in database.
//
// But as long as user is not playing with search engines every run it is acceptable.
QSqlQuery query(SqlDatabase::instance()->database());
query.exec("DELETE FROM search_engines");
for (const Engine &en : qAsConst(m_allEngines)) {
query.prepare("INSERT INTO search_engines (name, icon, url, shortcut, suggestionsUrl, suggestionsParameters, postData) VALUES (?, ?, ?, ?, ?, ?, ?)");
query.addBindValue(en.name);
query.addBindValue(iconToBase64(en.icon));
query.addBindValue(en.url);
query.addBindValue(en.shortcut);
query.addBindValue(en.suggestionsUrl);
query.addBindValue(en.suggestionsParameters);
query.addBindValue(en.postData);
query.exec();
}
}