summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/opensearch/opensearchengine.cpp44
-rw-r--r--src/opensearch/opensearchengine.h14
-rw-r--r--src/opensearch/opensearchmanager.cpp26
3 files changed, 70 insertions, 14 deletions
diff --git a/src/opensearch/opensearchengine.cpp b/src/opensearch/opensearchengine.cpp
index ef0f3f74..15228455 100644
--- a/src/opensearch/opensearchengine.cpp
+++ b/src/opensearch/opensearchengine.cpp
@@ -34,6 +34,10 @@
// Qt Includes
#include <QtCore/QRegExp>
+#include <QtCore/QFile>
+
+// KDE Includes
+#include <KStandardDirs>
OpenSearchEngine::OpenSearchEngine(QObject *parent)
@@ -246,6 +250,19 @@ bool OpenSearchEngine::operator<(const OpenSearchEngine &other) const
}
+ResponseList OpenSearchEngine::parseSuggestion(const QString &searchTerm, const QByteArray &resp)
+{
+ QFile file(suggestionPathFor(searchTerm));
+ if (file.open(QIODevice::WriteOnly | QIODevice::Text))
+ {
+ file.write(resp, resp.size());
+ file.close();
+ }
+
+ return parseSuggestion(resp);
+}
+
+
ResponseList OpenSearchEngine::parseSuggestion(const QByteArray &resp)
{
if (!m_parser)
@@ -259,3 +276,30 @@ QString OpenSearchEngine::type()
{
return m_parser->type();
}
+
+
+QString OpenSearchEngine::suggestionPathFor(const QString &searchTerm)
+{
+ return KStandardDirs::locateLocal("cache", QString("opensearch/") + m_name + QString("/") + searchTerm, true);
+}
+
+
+bool OpenSearchEngine::hasCachedSuggestionsFor(const QString &searchTerm)
+{
+ return QFile::exists(suggestionPathFor(searchTerm));
+}
+
+
+ResponseList OpenSearchEngine::cachedSuggestionsFor(const QString &searchTerm)
+{
+ QFile file(suggestionPathFor(searchTerm));
+ if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
+ return ResponseList();
+
+ QByteArray resp;
+ while (!file.atEnd())
+ {
+ resp += file.readLine();
+ }
+ return parseSuggestion(resp);
+}
diff --git a/src/opensearch/opensearchengine.h b/src/opensearch/opensearchengine.h
index c05501fb..deb2000a 100644
--- a/src/opensearch/opensearchengine.h
+++ b/src/opensearch/opensearchengine.h
@@ -52,7 +52,7 @@ class OpenSearchEngine : public QObject
public:
typedef QPair<QString, QString> Parameter;
-
+
OpenSearchEngine(QObject *parent = 0);
~OpenSearchEngine();
@@ -79,7 +79,7 @@ public:
void setSuggestionsParameters(const QList<Parameter> &suggestionsParameters);
void setSuggestionParser(SuggestionParser *parser);
-
+
QString imageUrl() const;
void setImageUrl(const QString &url);
@@ -91,12 +91,16 @@ public:
bool operator==(const OpenSearchEngine &other) const;
bool operator<(const OpenSearchEngine &other) const;
- ResponseList parseSuggestion(const QByteArray &response);
+ ResponseList parseSuggestion(const QString &searchTerm, const QByteArray &response);
static QString parseTemplate(const QString &searchTerm, const QString &searchTemplate);
QString type();
+ bool hasCachedSuggestionsFor(const QString &searchTerm);
+
+ ResponseList cachedSuggestionsFor(const QString &searchTerm);
+
private:
QString m_name;
QString m_description;
@@ -110,6 +114,10 @@ private:
QList<Parameter> m_suggestionsParameters;
SuggestionParser *m_parser;
+
+ QString suggestionPathFor(const QString &searchTerm);
+
+ ResponseList parseSuggestion(const QByteArray &resp);
};
#endif // OPENSEARCHENGINE_H
diff --git a/src/opensearch/opensearchmanager.cpp b/src/opensearch/opensearchmanager.cpp
index 41e46ec6..eedc8208 100644
--- a/src/opensearch/opensearchmanager.cpp
+++ b/src/opensearch/opensearchmanager.cpp
@@ -147,15 +147,20 @@ void OpenSearchManager::requestSuggestion(const QString &searchText)
idleJob();
}
- _typedText = searchText;
-
- KUrl url = m_activeEngine->suggestionsUrl(searchText);
- kDebug() << "Requesting for suggestions: " << url.url();
-
- m_currentJob = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo);
- m_state = REQ_SUGGESTION;
- connect(m_currentJob, SIGNAL(data(KIO::Job *, const QByteArray &)), this, SLOT(dataReceived(KIO::Job *, const QByteArray &)));
- connect(m_currentJob, SIGNAL(result(KJob *)), this, SLOT(jobFinished(KJob *)));
+ if (m_activeEngine->hasCachedSuggestionsFor(searchText))
+ {
+ emit suggestionsReceived(searchText, m_activeEngine->cachedSuggestionsFor(searchText));
+ }
+ else
+ {
+ KUrl url = m_activeEngine->suggestionsUrl(searchText);
+ kDebug() << "Requesting for suggestions: " << url.url();
+ _typedText = searchText;
+ m_currentJob = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo);
+ m_state = REQ_SUGGESTION;
+ connect(m_currentJob, SIGNAL(data(KIO::Job *, const QByteArray &)), this, SLOT(dataReceived(KIO::Job *, const QByteArray &)));
+ connect(m_currentJob, SIGNAL(result(KJob *)), this, SLOT(jobFinished(KJob *)));
+ }
}
@@ -177,13 +182,12 @@ void OpenSearchManager::jobFinished(KJob *job)
if (m_state == REQ_SUGGESTION)
{
- const ResponseList suggestionsList = m_activeEngine->parseSuggestion(m_jobData);
+ const ResponseList suggestionsList = m_activeEngine->parseSuggestion(_typedText, m_jobData);
kDebug() << "Received suggestions in "<< _typedText << " from " << m_activeEngine->name() << ": ";
Q_FOREACH(const Response &r, suggestionsList)
{
kDebug() << r.title;
}
-
emit suggestionsReceived(_typedText, suggestionsList);
idleJob();
return;