2011-10-21 23:26:34 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2013-02-08 12:10:12 +01:00
|
|
|
* Copyright (C) 2010-2013 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"
|
2011-10-21 23:26:34 +02:00
|
|
|
#include "iconprovider.h"
|
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "networkmanager.h"
|
|
|
|
#include "opensearchreader.h"
|
|
|
|
#include "opensearchengine.h"
|
2011-12-08 21:52:03 +01:00
|
|
|
#include "databasewriter.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>
|
2012-03-08 13:05:57 +01:00
|
|
|
#include <QWebElement>
|
2012-02-29 18:33:50 +01:00
|
|
|
|
2012-12-20 14:45:35 +01:00
|
|
|
#if QT_VERSION >= 0x050000
|
|
|
|
#include <QUrlQuery>
|
|
|
|
#endif
|
|
|
|
|
2011-10-21 23:26:34 +02:00
|
|
|
#define ENSURE_LOADED if (!m_settingsLoaded) loadSettings();
|
|
|
|
|
|
|
|
QIcon SearchEnginesManager::iconForSearchEngine(const QUrl &url)
|
|
|
|
{
|
2012-04-22 20:51:28 +02:00
|
|
|
QIcon ic = qIconProvider->iconFromImage(qIconProvider->iconForDomain(url));
|
2011-11-06 17:01:23 +01:00
|
|
|
if (ic.isNull()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
ic = QIcon(":icons/menu/search-icon.png");
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
return ic;
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchEnginesManager::SearchEnginesManager()
|
|
|
|
: QObject()
|
|
|
|
, 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");
|
|
|
|
m_startingEngineName = settings.value("activeEngine", "Google").toString();
|
2012-09-27 11:59:09 +02:00
|
|
|
m_defaultEngineName = settings.value("DefaultEngine", "Google").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;
|
|
|
|
query.exec("SELECT name, icon, url, shortcut, suggestionsUrl FROM search_engines");
|
|
|
|
while (query.next()) {
|
|
|
|
Engine en;
|
|
|
|
en.name = query.value(0).toString();
|
2012-04-22 20:51:28 +02:00
|
|
|
en.icon = qIconProvider->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();
|
|
|
|
en.suggestionsUrl = query.value(4).toString();
|
|
|
|
|
|
|
|
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;
|
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
|
|
|
|
2012-01-24 19:12:31 +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
|
|
|
}
|
|
|
|
|
|
|
|
QUrl SearchEnginesManager::searchUrl(const Engine &engine, const QString &string)
|
|
|
|
{
|
|
|
|
ENSURE_LOADED;
|
|
|
|
|
2012-01-21 00:20:30 +01:00
|
|
|
QByteArray url = engine.url.toUtf8();
|
2012-09-04 12:42:45 +02:00
|
|
|
url.replace(QLatin1String("%s"), QUrl::toPercentEncoding(string));
|
2012-01-21 00:20:30 +01:00
|
|
|
return QUrl::fromEncoded(url);
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QUrl SearchEnginesManager::searchUrl(const QString &string)
|
|
|
|
{
|
2013-02-08 12:10:12 +01:00
|
|
|
const Engine en = qzSettings->searchWithDefaultEngine ? m_defaultEngine : m_activeEngine;
|
2012-09-27 11:59:09 +02:00
|
|
|
return searchUrl(en, string);
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesManager::restoreDefaults()
|
|
|
|
{
|
|
|
|
Engine google;
|
|
|
|
google.name = "Google";
|
2012-06-28 17:16:45 +02:00
|
|
|
google.icon = QIcon(":icons/sites/google.png");
|
2011-10-21 23:26:34 +02:00
|
|
|
google.url = "http://www.google.com/search?client=qupzilla&q=%s";
|
|
|
|
google.shortcut = "g";
|
2012-01-07 12:09:39 +01:00
|
|
|
google.suggestionsUrl = "http://suggestqueries.google.com/complete/search?output=firefox&q=%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");
|
2011-10-21 23:26:34 +02:00
|
|
|
wiki.url = "http://en.wikipedia.org/wiki/Special:Search?search=%s&fulltext=Search";
|
|
|
|
wiki.shortcut = "w";
|
|
|
|
wiki.suggestionsUrl = "http://en.wikipedia.org/w/api.php?action=opensearch&search=%s&namespace=0";
|
|
|
|
|
|
|
|
Engine yt;
|
|
|
|
yt.name = "YouTube";
|
2012-06-28 17:16:45 +02:00
|
|
|
yt.icon = QIcon(":/icons/sites/youtube.png");
|
2011-10-21 23:26:34 +02:00
|
|
|
yt.url = "http://www.youtube.com/results?search_query=%s&search=Search";
|
|
|
|
yt.shortcut = "yt";
|
2012-01-07 12:19:47 +01:00
|
|
|
yt.suggestionsUrl = "http://suggestqueries.google.com/complete/search?ds=yt&output=firefox&q=%s";
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2012-01-19 18:05:21 +01:00
|
|
|
Engine duck;
|
|
|
|
duck.name = "DuckDuckGo";
|
2012-06-28 17:16:45 +02:00
|
|
|
duck.icon = QIcon(":/icons/sites/duck.png");
|
2012-02-23 21:53:22 +01:00
|
|
|
duck.url = "https://duckduckgo.com/?q=%s&t=qupzilla";
|
2012-01-19 18:05:21 +01:00
|
|
|
duck.shortcut = "d";
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2012-03-08 13:05:57 +01:00
|
|
|
addEngine(google);
|
|
|
|
addEngine(wiki);
|
|
|
|
addEngine(yt);
|
|
|
|
addEngine(duck);
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2012-09-27 11:59:09 +02:00
|
|
|
m_defaultEngine = google;
|
|
|
|
|
2011-10-21 23:26:34 +02:00
|
|
|
emit enginesChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
foreach(Engine e, m_allEngines) {
|
2011-10-21 23:26:34 +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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesManager::addEngineFromForm(const QWebElement &element, WebView* view)
|
|
|
|
{
|
|
|
|
QWebElement formElement = element.parent();
|
|
|
|
|
|
|
|
while (!formElement.isNull()) {
|
2012-09-04 12:42:45 +02:00
|
|
|
if (formElement.tagName().toLower() == QLatin1String("form")) {
|
2012-03-08 13:05:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
formElement = formElement.parent();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (formElement.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl actionUrl = QUrl::fromEncoded(formElement.attribute("action").toUtf8());
|
2012-03-08 17:17:41 +01:00
|
|
|
|
2012-03-08 13:05:57 +01:00
|
|
|
if (actionUrl.isRelative()) {
|
|
|
|
actionUrl = view->url().resolved(actionUrl);
|
|
|
|
}
|
2012-12-20 14:45:35 +01:00
|
|
|
#if QT_VERSION >= 0x050000
|
|
|
|
QUrlQuery query(actionUrl);
|
|
|
|
query.addQueryItem(element.attribute("name"), "%s");
|
|
|
|
|
|
|
|
QWebElementCollection allInputs = formElement.findAll("input");
|
|
|
|
foreach(QWebElement e, allInputs) {
|
|
|
|
if (element == e || !e.hasAttribute("name")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
query.addQueryItem(e.attribute("name"), e.evaluateJavaScript("this.value").toString());
|
|
|
|
}
|
2012-03-08 13:05:57 +01:00
|
|
|
|
2012-12-20 14:45:35 +01:00
|
|
|
actionUrl.setQuery(query);
|
|
|
|
#else
|
2012-03-08 13:05:57 +01:00
|
|
|
actionUrl.addQueryItem(element.attribute("name"), "%s");
|
|
|
|
|
|
|
|
QList<QPair<QByteArray, QByteArray> > queryItems;
|
|
|
|
QWebElementCollection allInputs = formElement.findAll("input");
|
2012-03-10 13:57:50 +01:00
|
|
|
foreach(QWebElement e, allInputs) {
|
2012-03-08 13:05:57 +01:00
|
|
|
if (element == e || !e.hasAttribute("name")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPair<QByteArray, QByteArray> item;
|
|
|
|
item.first = QUrl::toPercentEncoding(e.attribute("name").toUtf8());
|
|
|
|
item.second = QUrl::toPercentEncoding(e.evaluateJavaScript("this.value").toByteArray());
|
|
|
|
|
|
|
|
queryItems.append(item);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-03-08 13:05:57 +01:00
|
|
|
|
|
|
|
actionUrl.setEncodedQueryItems(queryItems + actionUrl.encodedQueryItems());
|
2012-12-20 14:45:35 +01:00
|
|
|
#endif
|
2012-03-08 13:05:57 +01:00
|
|
|
|
|
|
|
SearchEngine engine;
|
|
|
|
engine.name = view->title();
|
|
|
|
engine.icon = view->icon();
|
|
|
|
engine.url = actionUrl.toString();
|
|
|
|
|
|
|
|
EditSearchEngine dialog(SearchEnginesDialog::tr("Add Search Engine"), view);
|
|
|
|
dialog.setName(engine.name);
|
|
|
|
dialog.setIcon(engine.icon);
|
|
|
|
dialog.setUrl(engine.url);
|
|
|
|
|
|
|
|
if (dialog.exec() != QDialog::Accepted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
engine.name = dialog.name();
|
|
|
|
engine.icon = dialog.icon();
|
|
|
|
engine.url = dialog.url();
|
|
|
|
engine.shortcut = dialog.shortcut();
|
|
|
|
|
|
|
|
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"));
|
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
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
en.suggestionsUrl = engine->getSuggestionsUrl();
|
|
|
|
en.suggestionsParameters = engine->getSuggestionsParameters();
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
2012-01-24 19:12:31 +01:00
|
|
|
foreach(const Engine & en, m_allEngines) {
|
2011-10-21 23:26:34 +02:00
|
|
|
query.prepare("INSERT INTO search_engines (name, icon, url, shortcut, suggestionsUrl, suggestionsParameters) VALUES (?, ?, ?, ?, ?, ?)");
|
|
|
|
query.bindValue(0, en.name);
|
2012-04-22 20:51:28 +02:00
|
|
|
query.bindValue(1, qIconProvider->iconToBase64(en.icon));
|
2011-10-21 23:26:34 +02:00
|
|
|
query.bindValue(2, en.url);
|
|
|
|
query.bindValue(3, en.shortcut);
|
|
|
|
query.bindValue(4, en.suggestionsUrl);
|
|
|
|
query.bindValue(5, en.suggestionsParameters);
|
|
|
|
|
|
|
|
query.exec();
|
|
|
|
}
|
|
|
|
}
|