2011-10-21 23:26:34 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* Copyright (C) 2010-2012 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"
|
|
|
|
#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"
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
#define ENSURE_LOADED if (!m_settingsLoaded) loadSettings();
|
|
|
|
|
|
|
|
QIcon SearchEnginesManager::iconForSearchEngine(const QUrl &url)
|
|
|
|
{
|
2012-01-17 19:27:24 +01:00
|
|
|
QIcon ic = IconProvider::iconFromImage(mApp->iconProvider()->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();
|
|
|
|
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();
|
|
|
|
en.icon = IconProvider::iconFromBase64(query.value(1).toByteArray());
|
|
|
|
en.url = query.value(2).toString();
|
|
|
|
en.shortcut = query.value(3).toString();
|
|
|
|
en.suggestionsUrl = query.value(4).toString();
|
|
|
|
|
|
|
|
m_allEngines.append(en);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
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
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
foreach(Engine en, m_allEngines) {
|
|
|
|
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();
|
|
|
|
url.replace("%s", QUrl::toPercentEncoding(string));
|
|
|
|
return QUrl::fromEncoded(url);
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QUrl SearchEnginesManager::searchUrl(const QString &string)
|
|
|
|
{
|
2012-01-21 00:20:30 +01:00
|
|
|
return searchUrl(m_activeEngine, string);
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesManager::restoreDefaults()
|
|
|
|
{
|
|
|
|
Engine google;
|
|
|
|
google.name = "Google";
|
|
|
|
google.icon = QIcon(":icons/menu/google.png");
|
|
|
|
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)";
|
|
|
|
wiki.icon = QIcon(":/icons/menu/wikipedia.png");
|
|
|
|
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";
|
|
|
|
yt.icon = QIcon(":/icons/menu/youtube.png");
|
|
|
|
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";
|
|
|
|
duck.icon = QIcon(":/icons/menu/duck.png");
|
|
|
|
duck.url = "https://duckduckgo.com/?q=%s";
|
|
|
|
duck.shortcut = "d";
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
addEngine(google, false);
|
|
|
|
addEngine(wiki, false);
|
|
|
|
addEngine(yt, false);
|
2012-01-19 18:05:21 +01:00
|
|
|
addEngine(duck, false);
|
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()) {
|
|
|
|
e.icon = QIcon(QPixmap::fromImage(engine->image()));
|
|
|
|
|
|
|
|
m_allEngines.removeOne(e);
|
|
|
|
m_allEngines.append(e);
|
|
|
|
|
|
|
|
emit enginesChanged();
|
|
|
|
|
|
|
|
delete engine;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesManager::editEngine(const Engine &before, const Engine &after)
|
|
|
|
{
|
|
|
|
removeEngine(before);
|
|
|
|
addEngine(after);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesManager::addEngine(const Engine &engine, bool emitSignal)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (emitSignal) {
|
2011-10-21 23:26:34 +02:00
|
|
|
emit enginesChanged();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesManager::addEngine(OpenSearchEngine* engine)
|
|
|
|
{
|
|
|
|
ENSURE_LOADED;
|
|
|
|
|
|
|
|
Engine en;
|
|
|
|
en.name = engine->name();
|
|
|
|
en.url = engine->searchUrl("searchstring").toString().replace("searchstring", "%s");
|
2011-11-06 17:01:23 +01:00
|
|
|
if (engine->image().isNull()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
en.icon = iconForSearchEngine(engine->searchUrl(""));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesManager::removeEngine(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
|
|
|
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare("DELETE FROM search_engines WHERE name=? AND url=?");
|
|
|
|
query.bindValue(0, engine.name);
|
|
|
|
query.bindValue(1, engine.url);
|
|
|
|
query.exec();
|
|
|
|
|
|
|
|
m_allEngines.removeOne(engine);
|
|
|
|
emit enginesChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesManager::setAllEngines(const QList<Engine> &engines)
|
|
|
|
{
|
|
|
|
ENSURE_LOADED;
|
|
|
|
|
|
|
|
m_allEngines = engines;
|
|
|
|
emit enginesChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<SearchEngine> SearchEnginesManager::allEngines()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
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");
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
foreach(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);
|
|
|
|
query.bindValue(1, IconProvider::iconToBase64(en.icon));
|
|
|
|
query.bindValue(2, en.url);
|
|
|
|
query.bindValue(3, en.shortcut);
|
|
|
|
query.bindValue(4, en.suggestionsUrl);
|
|
|
|
query.bindValue(5, en.suggestionsParameters);
|
|
|
|
|
|
|
|
query.exec();
|
|
|
|
}
|
|
|
|
}
|