2011-10-21 23:26:34 +02:00
|
|
|
/* ============================================================
|
2017-08-25 17:11:29 +02:00
|
|
|
* Falkon - Qt web browser
|
2014-01-08 10:39:41 +01:00
|
|
|
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
|
2011-10-21 23:26:34 +02:00
|
|
|
*
|
|
|
|
* 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"
|
2012-03-08 13:05:57 +01:00
|
|
|
#include "searchenginesdialog.h"
|
|
|
|
#include "editsearchengine.h"
|
2015-09-29 11:45:39 +02:00
|
|
|
#include "networkmanager.h"
|
2011-10-21 23:26:34 +02:00
|
|
|
#include "iconprovider.h"
|
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "opensearchreader.h"
|
|
|
|
#include "opensearchengine.h"
|
2012-01-11 21:58:25 +01:00
|
|
|
#include "settings.h"
|
2012-09-27 11:59:09 +02:00
|
|
|
#include "qzsettings.h"
|
2012-03-08 13:05:57 +01:00
|
|
|
#include "webview.h"
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QMessageBox>
|
2014-03-15 01:14:06 +01:00
|
|
|
#include <QSqlQuery>
|
2014-03-07 18:03:42 +01:00
|
|
|
#include <QBuffer>
|
2012-02-29 18:33:50 +01:00
|
|
|
|
2012-12-20 14:45:35 +01:00
|
|
|
#include <QUrlQuery>
|
|
|
|
|
2011-10-21 23:26:34 +02:00
|
|
|
#define ENSURE_LOADED if (!m_settingsLoaded) loadSettings();
|
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
static QIcon iconFromBase64(const QByteArray &data)
|
2011-10-21 23:26:34 +02:00
|
|
|
{
|
2014-03-07 18:03:42 +01:00
|
|
|
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;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
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();
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
2014-03-10 00:47:07 +01:00
|
|
|
SearchEnginesManager::SearchEnginesManager(QObject* parent)
|
|
|
|
: QObject(parent)
|
2011-10-21 23:26:34 +02:00
|
|
|
, m_settingsLoaded(false)
|
|
|
|
, m_saveScheduled(false)
|
|
|
|
{
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2011-10-21 23:26:34 +02:00
|
|
|
settings.beginGroup("SearchEngines");
|
2014-02-11 14:17:21 +01:00
|
|
|
m_startingEngineName = settings.value("activeEngine", "DuckDuckGo").toString();
|
|
|
|
m_defaultEngineName = settings.value("DefaultEngine", "DuckDuckGo").toString();
|
2011-10-21 23:26:34 +02:00
|
|
|
settings.endGroup();
|
|
|
|
|
|
|
|
connect(this, SIGNAL(enginesChanged()), this, SLOT(scheduleSave()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesManager::loadSettings()
|
|
|
|
{
|
|
|
|
m_settingsLoaded = true;
|
|
|
|
|
|
|
|
QSqlQuery query;
|
2013-11-09 13:32:03 +01:00
|
|
|
query.exec("SELECT name, icon, url, shortcut, suggestionsUrl, suggestionsParameters, postData FROM search_engines");
|
2014-03-15 01:14:06 +01:00
|
|
|
|
2011-10-21 23:26:34 +02:00
|
|
|
while (query.next()) {
|
|
|
|
Engine en;
|
|
|
|
en.name = query.value(0).toString();
|
2014-03-07 18:03:42 +01:00
|
|
|
en.icon = iconFromBase64(query.value(1).toByteArray());
|
2011-10-21 23:26:34 +02:00
|
|
|
en.url = query.value(2).toString();
|
|
|
|
en.shortcut = query.value(3).toString();
|
2013-11-09 13:32:03 +01:00
|
|
|
en.suggestionsUrl = query.value(4).toString();
|
|
|
|
en.suggestionsParameters = query.value(5).toByteArray();
|
|
|
|
en.postData = query.value(6).toByteArray();
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
m_allEngines.append(en);
|
2012-09-27 11:59:09 +02:00
|
|
|
|
|
|
|
if (en.name == m_defaultEngineName) {
|
|
|
|
m_defaultEngine = en;
|
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (m_allEngines.isEmpty()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
restoreDefaults();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-09-27 11:59:09 +02:00
|
|
|
|
|
|
|
if (m_defaultEngine.name.isEmpty()) {
|
|
|
|
m_defaultEngine = m_allEngines[0];
|
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SearchEngine SearchEnginesManager::engineForShortcut(const QString &shortcut)
|
|
|
|
{
|
2011-11-14 16:00:54 +01:00
|
|
|
Engine returnEngine;
|
2014-01-08 10:39:41 +01:00
|
|
|
|
2011-11-16 16:47:08 +01:00
|
|
|
if (shortcut.isEmpty()) {
|
2011-11-14 16:00:54 +01:00
|
|
|
return returnEngine;
|
2011-11-16 16:47:08 +01:00
|
|
|
}
|
2011-11-14 16:00:54 +01:00
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const Engine &en, m_allEngines) {
|
2011-11-06 17:01:23 +01:00
|
|
|
if (en.shortcut == shortcut) {
|
2011-11-14 16:00:54 +01:00
|
|
|
returnEngine = en;
|
|
|
|
break;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
2011-11-14 16:00:54 +01:00
|
|
|
return returnEngine;
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
2014-03-19 21:27:43 +01:00
|
|
|
LoadRequest SearchEnginesManager::searchResult(const Engine &engine, const QString &string)
|
2011-10-21 23:26:34 +02:00
|
|
|
{
|
|
|
|
ENSURE_LOADED;
|
|
|
|
|
2014-03-19 21:27:43 +01:00
|
|
|
// GET search engine
|
2013-11-09 15:48:24 +01:00
|
|
|
if (engine.postData.isEmpty()) {
|
|
|
|
QByteArray url = engine.url.toUtf8();
|
2017-09-11 10:05:02 +02:00
|
|
|
url.replace("%s", QUrl::toPercentEncoding(string));
|
2013-11-09 15:48:24 +01:00
|
|
|
|
2014-03-19 21:27:43 +01:00
|
|
|
return LoadRequest(QUrl::fromEncoded(url));
|
2013-11-09 15:48:24 +01:00
|
|
|
}
|
2013-11-09 13:32:03 +01:00
|
|
|
|
2014-03-19 21:27:43 +01:00
|
|
|
// POST search engine
|
|
|
|
QByteArray data = engine.postData;
|
|
|
|
data.replace("%s", QUrl::toPercentEncoding(string));
|
|
|
|
|
2015-10-06 09:46:23 +02:00
|
|
|
return LoadRequest(QUrl::fromEncoded(engine.url.toUtf8()), LoadRequest::PostOperation, data);
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
2014-03-19 21:27:43 +01:00
|
|
|
LoadRequest SearchEnginesManager::searchResult(const QString &string)
|
2011-10-21 23:26:34 +02:00
|
|
|
{
|
2013-11-09 13:32:03 +01:00
|
|
|
ENSURE_LOADED;
|
|
|
|
|
2013-02-08 12:10:12 +01:00
|
|
|
const Engine en = qzSettings->searchWithDefaultEngine ? m_defaultEngine : m_activeEngine;
|
2013-11-09 13:32:03 +01:00
|
|
|
return searchResult(en, string);
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesManager::restoreDefaults()
|
|
|
|
{
|
2013-08-12 10:46:24 +02:00
|
|
|
Engine duck;
|
|
|
|
duck.name = "DuckDuckGo";
|
|
|
|
duck.icon = QIcon(":/icons/sites/duck.png");
|
2017-08-25 17:11:29 +02:00
|
|
|
duck.url = "https://duckduckgo.com/?q=%s&t=falkon";
|
2013-08-12 10:46:24 +02:00
|
|
|
duck.shortcut = "d";
|
2015-12-18 11:41:43 +01:00
|
|
|
duck.suggestionsUrl = "https://ac.duckduckgo.com/ac/?q=%s&type=list";
|
2013-08-12 10:46:24 +02:00
|
|
|
|
2013-11-09 15:59:35 +01:00
|
|
|
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";
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
Engine wiki;
|
|
|
|
wiki.name = "Wikipedia (en)";
|
2012-06-28 17:16:45 +02:00
|
|
|
wiki.icon = QIcon(":/icons/sites/wikipedia.png");
|
2015-12-18 11:41:43 +01:00
|
|
|
wiki.url = "https://en.wikipedia.org/wiki/Special:Search?search=%s&fulltext=Search";
|
2011-10-21 23:26:34 +02:00
|
|
|
wiki.shortcut = "w";
|
2015-12-18 11:41:43 +01:00
|
|
|
wiki.suggestionsUrl = "https://en.wikipedia.org/w/api.php?action=opensearch&search=%s&namespace=0";
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2013-11-09 15:59:35 +01:00
|
|
|
Engine google;
|
|
|
|
google.name = "Google";
|
|
|
|
google.icon = QIcon(":icons/sites/google.png");
|
2017-08-25 17:11:29 +02:00
|
|
|
google.url = "https://www.google.com/search?client=falkon&q=%s";
|
2013-11-09 15:59:35 +01:00
|
|
|
google.shortcut = "g";
|
2015-12-18 11:41:43 +01:00
|
|
|
google.suggestionsUrl = "https://suggestqueries.google.com/complete/search?output=firefox&q=%s";
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2013-08-12 10:46:24 +02:00
|
|
|
addEngine(duck);
|
2013-11-09 15:59:35 +01:00
|
|
|
addEngine(sp);
|
2012-03-08 13:05:57 +01:00
|
|
|
addEngine(wiki);
|
2013-11-09 15:59:35 +01:00
|
|
|
addEngine(google);
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2013-08-12 10:46:24 +02:00
|
|
|
m_defaultEngine = duck;
|
2012-09-27 11:59:09 +02:00
|
|
|
|
2011-10-21 23:26:34 +02:00
|
|
|
emit enginesChanged();
|
|
|
|
}
|
|
|
|
|
2014-03-07 18:03:42 +01:00
|
|
|
// static
|
|
|
|
QIcon SearchEnginesManager::iconForSearchEngine(const QUrl &url)
|
|
|
|
{
|
|
|
|
QIcon ic = IconProvider::iconForDomain(url);
|
|
|
|
|
|
|
|
if (ic.isNull()) {
|
2016-12-29 15:07:34 +01:00
|
|
|
ic = QIcon::fromTheme(QSL("edit-find"), QIcon(QSL(":icons/menu/search-icon.svg")));
|
2014-03-07 18:03:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ic;
|
|
|
|
}
|
|
|
|
|
2011-10-21 23:26:34 +02:00
|
|
|
void SearchEnginesManager::engineChangedImage()
|
|
|
|
{
|
|
|
|
OpenSearchEngine* engine = qobject_cast<OpenSearchEngine*>(sender());
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!engine) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (Engine e, m_allEngines) {
|
2014-04-05 14:42:19 +02:00
|
|
|
if (e.name == engine->name() &&
|
|
|
|
e.url.contains(engine->searchUrl("%s").toString()) &&
|
|
|
|
!engine->image().isNull()
|
|
|
|
) {
|
2012-03-07 12:19:54 +01:00
|
|
|
int index = m_allEngines.indexOf(e);
|
|
|
|
if (index != -1) {
|
|
|
|
m_allEngines[index].icon = QIcon(QPixmap::fromImage(engine->image()));
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2012-03-07 12:19:54 +01:00
|
|
|
emit enginesChanged();
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2012-03-07 12:19:54 +01:00
|
|
|
delete engine;
|
|
|
|
break;
|
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesManager::editEngine(const Engine &before, const Engine &after)
|
|
|
|
{
|
|
|
|
removeEngine(before);
|
|
|
|
addEngine(after);
|
|
|
|
}
|
|
|
|
|
2012-03-08 13:05:57 +01:00
|
|
|
void SearchEnginesManager::addEngine(const Engine &engine)
|
2011-10-21 23:26:34 +02:00
|
|
|
{
|
|
|
|
ENSURE_LOADED;
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (m_allEngines.contains(engine)) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
m_allEngines.append(engine);
|
|
|
|
|
2012-03-08 13:05:57 +01:00
|
|
|
emit enginesChanged();
|
|
|
|
}
|
|
|
|
|
2015-09-30 14:57:08 +02:00
|
|
|
void SearchEnginesManager::addEngineFromForm(const QVariantMap &formData, WebView *view)
|
2012-03-08 13:05:57 +01:00
|
|
|
{
|
2015-09-30 14:57:08 +02:00
|
|
|
if (formData.isEmpty())
|
2012-03-08 13:05:57 +01:00
|
|
|
return;
|
|
|
|
|
2015-09-30 14:57:08 +02:00
|
|
|
const QString method = formData.value(QSL("method")).toString();
|
|
|
|
bool isPost = method == QL1S("post");
|
2013-11-09 15:48:24 +01:00
|
|
|
|
2015-09-30 14:57:08 +02:00
|
|
|
QUrl actionUrl = formData.value(QSL("action")).toUrl();
|
2012-03-08 17:17:41 +01:00
|
|
|
|
2012-03-08 13:05:57 +01:00
|
|
|
if (actionUrl.isRelative()) {
|
|
|
|
actionUrl = view->url().resolved(actionUrl);
|
|
|
|
}
|
2013-11-09 15:48:24 +01:00
|
|
|
|
|
|
|
QUrl parameterUrl = actionUrl;
|
|
|
|
|
|
|
|
if (isPost) {
|
|
|
|
parameterUrl = QUrl("http://foo.bar");
|
|
|
|
}
|
|
|
|
|
2015-09-30 14:57:08 +02:00
|
|
|
const QString &inputName = formData.value(QSL("inputName")).toString();
|
|
|
|
|
2013-11-09 15:48:24 +01:00
|
|
|
QUrlQuery query(parameterUrl);
|
2015-09-30 14:57:08 +02:00
|
|
|
query.addQueryItem(inputName, QSL("SEARCH"));
|
2012-12-20 14:45:35 +01:00
|
|
|
|
2015-09-30 14:57:08 +02:00
|
|
|
const QVariantList &inputs = formData.value(QSL("inputs")).toList();
|
|
|
|
foreach (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())
|
2012-12-20 14:45:35 +01:00
|
|
|
continue;
|
|
|
|
|
2015-09-30 14:57:08 +02:00
|
|
|
query.addQueryItem(name, value);
|
2012-12-20 14:45:35 +01:00
|
|
|
}
|
2012-03-08 13:05:57 +01:00
|
|
|
|
2013-11-09 15:48:24 +01:00
|
|
|
parameterUrl.setQuery(query);
|
2012-03-08 13:05:57 +01:00
|
|
|
|
2013-11-09 15:48:24 +01:00
|
|
|
if (!isPost) {
|
|
|
|
actionUrl = parameterUrl;
|
|
|
|
}
|
|
|
|
|
2012-03-08 13:05:57 +01:00
|
|
|
SearchEngine engine;
|
|
|
|
engine.name = view->title();
|
|
|
|
engine.icon = view->icon();
|
2013-11-09 16:14:54 +01:00
|
|
|
engine.url = actionUrl.toEncoded();
|
2012-03-08 13:05:57 +01:00
|
|
|
|
2013-11-09 15:48:24 +01:00
|
|
|
if (isPost) {
|
|
|
|
QByteArray data = parameterUrl.toEncoded(QUrl::RemoveScheme);
|
|
|
|
engine.postData = data.contains('?') ? data.mid(data.lastIndexOf('?') + 1) : QByteArray();
|
2015-09-30 14:57:08 +02:00
|
|
|
engine.postData.replace((inputName + QL1S("=SEARCH")).toUtf8(), (inputName + QL1S("=%s")).toUtf8());
|
|
|
|
} else {
|
|
|
|
engine.url.replace(inputName + QL1S("=SEARCH"), inputName + QL1S("=%s"));
|
2013-11-09 15:48:24 +01:00
|
|
|
}
|
|
|
|
|
2012-03-08 13:05:57 +01:00
|
|
|
EditSearchEngine dialog(SearchEnginesDialog::tr("Add Search Engine"), view);
|
|
|
|
dialog.setName(engine.name);
|
|
|
|
dialog.setIcon(engine.icon);
|
|
|
|
dialog.setUrl(engine.url);
|
2013-11-09 15:48:24 +01:00
|
|
|
dialog.setPostData(engine.postData);
|
2012-03-08 13:05:57 +01:00
|
|
|
|
|
|
|
if (dialog.exec() != QDialog::Accepted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
engine.name = dialog.name();
|
|
|
|
engine.icon = dialog.icon();
|
|
|
|
engine.url = dialog.url();
|
|
|
|
engine.shortcut = dialog.shortcut();
|
2013-11-09 15:48:24 +01:00
|
|
|
engine.postData = dialog.postData().toUtf8();
|
2012-03-08 13:05:57 +01:00
|
|
|
|
|
|
|
if (engine.name.isEmpty() || engine.url.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
addEngine(engine);
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesManager::addEngine(OpenSearchEngine* engine)
|
|
|
|
{
|
|
|
|
ENSURE_LOADED;
|
|
|
|
|
|
|
|
Engine en;
|
|
|
|
en.name = engine->name();
|
2012-09-04 12:42:45 +02:00
|
|
|
en.url = engine->searchUrl("searchstring").toString().replace(QLatin1String("searchstring"), QLatin1String("%s"));
|
2013-11-09 15:48:24 +01:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (engine->image().isNull()) {
|
2012-09-02 11:42:41 +02:00
|
|
|
en.icon = iconForSearchEngine(engine->searchUrl(QString()));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-10-21 23:26:34 +02:00
|
|
|
en.icon = QIcon(QPixmap::fromImage(engine->image()));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2013-11-09 15:48:24 +01:00
|
|
|
|
2011-10-21 23:26:34 +02:00
|
|
|
en.suggestionsUrl = engine->getSuggestionsUrl();
|
|
|
|
en.suggestionsParameters = engine->getSuggestionsParameters();
|
2013-11-09 15:48:24 +01:00
|
|
|
en.postData = engine->getPostData("searchstring").replace("searchstring", "%s");
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
addEngine(en);
|
|
|
|
|
|
|
|
connect(engine, SIGNAL(imageChanged()), this, SLOT(engineChangedImage()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesManager::addEngine(const QUrl &url)
|
|
|
|
{
|
|
|
|
ENSURE_LOADED;
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!url.isValid()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
qApp->setOverrideCursor(Qt::WaitCursor);
|
|
|
|
|
|
|
|
QNetworkReply* reply = mApp->networkManager()->get(QNetworkRequest(url));
|
|
|
|
reply->setParent(this);
|
|
|
|
connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesManager::replyFinished()
|
|
|
|
{
|
|
|
|
qApp->restoreOverrideCursor();
|
|
|
|
|
|
|
|
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!reply) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
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);
|
2011-11-01 12:30:48 +01:00
|
|
|
QMessageBox::information(0, tr("Search Engine Added"), tr("Search Engine \"%1\" has been successfully added.").arg(engine->name()));
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!m_allEngines.contains(engine)) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
m_activeEngine = engine;
|
2011-11-06 12:05:16 +01:00
|
|
|
emit activeEngineChanged();
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
2012-09-27 11:59:09 +02:00
|
|
|
void SearchEnginesManager::setDefaultEngine(const SearchEnginesManager::Engine &engine)
|
|
|
|
{
|
|
|
|
ENSURE_LOADED;
|
|
|
|
|
|
|
|
if (!m_allEngines.contains(engine)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_defaultEngine = engine;
|
|
|
|
emit defaultEngineChanged();
|
|
|
|
}
|
|
|
|
|
2011-10-21 23:26:34 +02:00
|
|
|
void SearchEnginesManager::removeEngine(const Engine &engine)
|
|
|
|
{
|
|
|
|
ENSURE_LOADED;
|
|
|
|
|
2013-02-26 12:56:11 +01:00
|
|
|
int index = m_allEngines.indexOf(engine);
|
|
|
|
|
|
|
|
if (index < 0) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("DELETE FROM search_engines WHERE name=? AND url=?");
|
|
|
|
query.bindValue(0, engine.name);
|
|
|
|
query.bindValue(1, engine.url);
|
|
|
|
query.exec();
|
|
|
|
|
2013-02-26 12:56:11 +01:00
|
|
|
m_allEngines.remove(index);
|
2011-10-21 23:26:34 +02:00
|
|
|
emit enginesChanged();
|
|
|
|
}
|
|
|
|
|
2013-02-26 12:56:11 +01:00
|
|
|
void SearchEnginesManager::setAllEngines(const QVector<Engine> &engines)
|
2011-10-21 23:26:34 +02:00
|
|
|
{
|
|
|
|
ENSURE_LOADED;
|
|
|
|
|
|
|
|
m_allEngines = engines;
|
|
|
|
emit enginesChanged();
|
|
|
|
}
|
|
|
|
|
2013-02-26 12:56:11 +01:00
|
|
|
QVector<SearchEngine> SearchEnginesManager::allEngines()
|
2011-10-21 23:26:34 +02:00
|
|
|
{
|
|
|
|
ENSURE_LOADED;
|
|
|
|
|
|
|
|
return m_allEngines;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesManager::saveSettings()
|
|
|
|
{
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2011-10-21 23:26:34 +02:00
|
|
|
settings.beginGroup("SearchEngines");
|
|
|
|
settings.setValue("activeEngine", m_activeEngine.name);
|
2012-09-27 11:59:09 +02:00
|
|
|
settings.setValue("DefaultEngine", m_defaultEngine.name);
|
2011-10-21 23:26:34 +02:00
|
|
|
settings.endGroup();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!m_saveScheduled) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
query.exec("DELETE FROM search_engines");
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const Engine &en, m_allEngines) {
|
2013-11-09 15:48:24 +01:00
|
|
|
query.prepare("INSERT INTO search_engines (name, icon, url, shortcut, suggestionsUrl, suggestionsParameters, postData) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
2013-11-08 22:17:27 +01:00
|
|
|
query.addBindValue(en.name);
|
2014-03-07 18:03:42 +01:00
|
|
|
query.addBindValue(iconToBase64(en.icon));
|
2013-11-08 22:17:27 +01:00
|
|
|
query.addBindValue(en.url);
|
|
|
|
query.addBindValue(en.shortcut);
|
|
|
|
query.addBindValue(en.suggestionsUrl);
|
|
|
|
query.addBindValue(en.suggestionsParameters);
|
2013-11-09 13:32:03 +01:00
|
|
|
query.addBindValue(en.postData);
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
query.exec();
|
|
|
|
}
|
|
|
|
}
|