2011-10-21 23:26:34 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2009 Jakub Wieczorek <faw217@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 2 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2011-10-29 12:24:12 +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-29 12:24:12 +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/>.
|
|
|
|
* ============================================================ */
|
2011-10-21 23:26:34 +02:00
|
|
|
#include "opensearchreader.h"
|
|
|
|
|
|
|
|
#include "opensearchengine.h"
|
|
|
|
|
|
|
|
#include <qiodevice.h>
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\class OpenSearchReader
|
|
|
|
\brief A class reading a search engine description from an external source
|
|
|
|
|
|
|
|
OpenSearchReader is a class that can be used to read search engine descriptions
|
|
|
|
formed using the OpenSearch format.
|
|
|
|
|
|
|
|
It inherits QXmlStreamReader and thus provides additional functions, such as
|
|
|
|
QXmlStreamReader::error(), QXmlStreamReader::hasError() that can be used to make sure
|
|
|
|
the reading procedure succeeded.
|
|
|
|
|
|
|
|
For more information see:
|
|
|
|
http://www.opensearch.org/Specifications/OpenSearch/1.1/Draft_4#OpenSearch_description_document
|
|
|
|
|
|
|
|
\sa OpenSearchEngine, OpenSearchWriter
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Constructs a new reader.
|
|
|
|
|
|
|
|
\note One instance can be used to read multiple files, one by one.
|
|
|
|
*/
|
|
|
|
OpenSearchReader::OpenSearchReader()
|
|
|
|
: QXmlStreamReader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Reads an OpenSearch engine from the \a device and returns an OpenSearchEngine object,
|
|
|
|
filled in with all the data that has been retrieved from the document.
|
|
|
|
|
|
|
|
If the \a device is closed, it will be opened.
|
|
|
|
|
|
|
|
To make sure if the procedure succeeded, check QXmlStreamReader::error().
|
|
|
|
|
|
|
|
\return a new constructed OpenSearchEngine object
|
|
|
|
|
|
|
|
\note The function returns an object of the OpenSearchEngine class even if the document
|
|
|
|
is bad formed or doesn't conform to the specification. It needs to be manually
|
|
|
|
deleted afterwards, if intended.
|
|
|
|
\note The lifetime of the returned OpenSearchEngine object is up to the user.
|
|
|
|
The object should be deleted once it is not used anymore to avoid memory leaks.
|
|
|
|
*/
|
2011-11-06 17:01:23 +01:00
|
|
|
OpenSearchEngine* OpenSearchReader::read(QIODevice* device)
|
2011-10-21 23:26:34 +02:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!device->isOpen()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
device->open(QIODevice::ReadOnly);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
setDevice(device);
|
|
|
|
return read();
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
OpenSearchEngine* OpenSearchReader::read()
|
2011-10-21 23:26:34 +02:00
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
OpenSearchEngine* engine = new OpenSearchEngine();
|
|
|
|
m_searchXml = device()->peek(1024 * 5);
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
while (!isStartElement() && !atEnd()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
readNext();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2012-03-08 13:05:57 +01:00
|
|
|
if (!m_searchXml.contains(QLatin1String("http://a9.com/-/spec/opensearch/1.1/")) &&
|
2012-03-10 13:57:50 +01:00
|
|
|
!m_searchXml.contains(QLatin1String("http://www.mozilla.org/2006/browser/search/"))) {
|
2011-11-06 17:01:23 +01:00
|
|
|
raiseError(QObject::tr("The file is not an OpenSearch 1.1 file."));
|
|
|
|
return engine;
|
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
while (!atEnd()) {
|
|
|
|
readNext();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!isStartElement()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
continue;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2011-10-29 12:24:12 +02:00
|
|
|
if (name() == QLatin1String("ShortName") || name() == QLatin1String("os:ShortName")) {
|
2011-10-21 23:26:34 +02:00
|
|
|
engine->setName(readElementText());
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else if (name() == QLatin1String("Description") || name() == QLatin1String("os:Description")) {
|
2011-10-21 23:26:34 +02:00
|
|
|
engine->setDescription(readElementText());
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else if (name() == QLatin1String("Url") || name() == QLatin1String("os:Url")) {
|
2011-10-21 23:26:34 +02:00
|
|
|
QString type = attributes().value(QLatin1String("type")).toString();
|
|
|
|
QString url = attributes().value(QLatin1String("template")).toString();
|
|
|
|
QString method = attributes().value(QLatin1String("method")).toString();
|
|
|
|
|
|
|
|
if (type == QLatin1String("application/x-suggestions+json")
|
2011-11-06 17:01:23 +01:00
|
|
|
&& !engine->suggestionsUrlTemplate().isEmpty()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
continue;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
if ((type.isEmpty()
|
2011-11-06 17:01:23 +01:00
|
|
|
|| type == QLatin1String("text/html")
|
|
|
|
|| type == QLatin1String("application/xhtml+xml"))
|
|
|
|
&& !engine->searchUrlTemplate().isEmpty()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
continue;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (url.isEmpty()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
continue;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
QList<OpenSearchEngine::Parameter> parameters;
|
|
|
|
|
|
|
|
readNext();
|
|
|
|
|
2011-10-29 12:24:12 +02:00
|
|
|
while (!isEndElement() || (name() != QLatin1String("Url") && name() != QLatin1String("os:Url"))) {
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!isStartElement() || (name() != QLatin1String("Param") && name() != QLatin1String("Parameter")
|
|
|
|
&& name() != QLatin1String("os:Param") && name() != QLatin1String("os:Parameter"))) {
|
2011-10-21 23:26:34 +02:00
|
|
|
readNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString key = attributes().value(QLatin1String("name")).toString();
|
|
|
|
QString value = attributes().value(QLatin1String("value")).toString();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!key.isEmpty() && !value.isEmpty()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
parameters.append(OpenSearchEngine::Parameter(key, value));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
while (!isEndElement()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
readNext();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type == QLatin1String("application/x-suggestions+json")) {
|
|
|
|
engine->setSuggestionsUrlTemplate(url);
|
|
|
|
engine->setSuggestionsParameters(parameters);
|
|
|
|
engine->setSuggestionsMethod(method);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else if (type.isEmpty() || type == QLatin1String("text/html") || type == QLatin1String("application/xhtml+xml")) {
|
2011-10-21 23:26:34 +02:00
|
|
|
engine->setSearchUrlTemplate(url);
|
|
|
|
engine->setSearchParameters(parameters);
|
|
|
|
engine->setSearchMethod(method);
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else if (name() == QLatin1String("Image") || name() == QLatin1String("os:Image")) {
|
|
|
|
engine->setImageUrl(readElementText());
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!engine->name().isEmpty()
|
2011-11-06 17:01:23 +01:00
|
|
|
&& !engine->description().isEmpty()
|
|
|
|
&& !engine->suggestionsUrlTemplate().isEmpty()
|
|
|
|
&& !engine->searchUrlTemplate().isEmpty()
|
|
|
|
&& !engine->imageUrl().isEmpty()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
break;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
|