mirror of
https://invent.kde.org/network/falkon.git
synced 2024-11-11 09:32:12 +01:00
OpenSearchEngine: Use Json class instead QScriptEngine to parse suggestions
This commit is contained in:
parent
476bd93f55
commit
addd5d1f43
|
@ -37,6 +37,7 @@
|
||||||
#include "opensearchengine.h"
|
#include "opensearchengine.h"
|
||||||
#include "qzregexp.h"
|
#include "qzregexp.h"
|
||||||
#include "opensearchenginedelegate.h"
|
#include "opensearchenginedelegate.h"
|
||||||
|
#include "json.h"
|
||||||
|
|
||||||
#include <qbuffer.h>
|
#include <qbuffer.h>
|
||||||
#include <qcoreapplication.h>
|
#include <qcoreapplication.h>
|
||||||
|
@ -44,8 +45,6 @@
|
||||||
#include <qnetworkrequest.h>
|
#include <qnetworkrequest.h>
|
||||||
#include <qnetworkreply.h>
|
#include <qnetworkreply.h>
|
||||||
#include <qregexp.h>
|
#include <qregexp.h>
|
||||||
#include <qscriptengine.h>
|
|
||||||
#include <qscriptvalue.h>
|
|
||||||
#include <qstringlist.h>
|
#include <qstringlist.h>
|
||||||
|
|
||||||
#if QT_VERSION >= 0x050000
|
#if QT_VERSION >= 0x050000
|
||||||
|
@ -109,7 +108,6 @@ OpenSearchEngine::OpenSearchEngine(QObject* parent)
|
||||||
, m_suggestionsMethod(QLatin1String("get"))
|
, m_suggestionsMethod(QLatin1String("get"))
|
||||||
, m_networkAccessManager(0)
|
, m_networkAccessManager(0)
|
||||||
, m_suggestionsReply(0)
|
, m_suggestionsReply(0)
|
||||||
, m_scriptEngine(0)
|
|
||||||
, m_delegate(0)
|
, m_delegate(0)
|
||||||
{
|
{
|
||||||
m_requestMethods.insert(QLatin1String("get"), QNetworkAccessManager::GetOperation);
|
m_requestMethods.insert(QLatin1String("get"), QNetworkAccessManager::GetOperation);
|
||||||
|
@ -121,9 +119,6 @@ OpenSearchEngine::OpenSearchEngine(QObject* parent)
|
||||||
*/
|
*/
|
||||||
OpenSearchEngine::~OpenSearchEngine()
|
OpenSearchEngine::~OpenSearchEngine()
|
||||||
{
|
{
|
||||||
if (m_scriptEngine) {
|
|
||||||
m_scriptEngine->deleteLater();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString OpenSearchEngine::parseTemplate(const QString &searchTerm, const QString &searchTemplate)
|
QString OpenSearchEngine::parseTemplate(const QString &searchTerm, const QString &searchTemplate)
|
||||||
|
@ -655,33 +650,23 @@ void OpenSearchEngine::suggestionsObtained()
|
||||||
m_suggestionsReply->deleteLater();
|
m_suggestionsReply->deleteLater();
|
||||||
m_suggestionsReply = 0;
|
m_suggestionsReply = 0;
|
||||||
|
|
||||||
if (response.isEmpty()) {
|
Json json;
|
||||||
|
const QVariant res = json.parse(response);
|
||||||
|
|
||||||
|
if (!json.ok() || res.type() != QVariant::Map)
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
if (!response.startsWith(QLatin1Char('[')) || !response.endsWith(QLatin1Char(']'))) {
|
const QVariantList list = res.toMap().value(QSL("1")).toList();
|
||||||
|
|
||||||
|
if (list.isEmpty())
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
if (!m_scriptEngine) {
|
QStringList out;
|
||||||
m_scriptEngine = new QScriptEngine();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Evaluate the JSON response using QtScript.
|
foreach (const QVariant &v, list)
|
||||||
if (!m_scriptEngine->canEvaluate(response)) {
|
out.append(v.toString());
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QScriptValue responseParts = m_scriptEngine->evaluate(response);
|
emit suggestions(out);
|
||||||
|
|
||||||
if (!responseParts.property(1).isArray()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QStringList suggestionsList;
|
|
||||||
qScriptValueToSequence(responseParts.property(1), suggestionsList);
|
|
||||||
|
|
||||||
emit suggestions(suggestionsList);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|
|
@ -47,7 +47,6 @@
|
||||||
#include <qurl.h>
|
#include <qurl.h>
|
||||||
|
|
||||||
class QNetworkReply;
|
class QNetworkReply;
|
||||||
class QScriptEngine;
|
|
||||||
|
|
||||||
class OpenSearchEngineDelegate;
|
class OpenSearchEngineDelegate;
|
||||||
class QUPZILLA_EXPORT OpenSearchEngine : public QObject
|
class QUPZILLA_EXPORT OpenSearchEngine : public QObject
|
||||||
|
@ -164,8 +163,6 @@ private:
|
||||||
QNetworkAccessManager* m_networkAccessManager;
|
QNetworkAccessManager* m_networkAccessManager;
|
||||||
QNetworkReply* m_suggestionsReply;
|
QNetworkReply* m_suggestionsReply;
|
||||||
|
|
||||||
QScriptEngine* m_scriptEngine;
|
|
||||||
|
|
||||||
OpenSearchEngineDelegate* m_delegate;
|
OpenSearchEngineDelegate* m_delegate;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user