From e9d099f4c5efa41fafd16408f13400cb37616f18 Mon Sep 17 00:00:00 2001 From: lionelc Date: Thu, 12 Aug 2010 18:31:16 +0200 Subject: introduce a new SuggestionListItem introduce an opensearch engine --- src/search/opensearchmanager.cpp | 175 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 src/search/opensearchmanager.cpp (limited to 'src/search/opensearchmanager.cpp') diff --git a/src/search/opensearchmanager.cpp b/src/search/opensearchmanager.cpp new file mode 100644 index 00000000..6e37db77 --- /dev/null +++ b/src/search/opensearchmanager.cpp @@ -0,0 +1,175 @@ +/* This file is part of the KDE project + * Copyright (C) 2009 Fredy Yanardi + * + * This library 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 library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "opensearchmanager.h" + +#include + +#include +#include +#include +#include +#include + +#include "opensearchengine.h" +#include "opensearchreader.h" +#include "opensearchwriter.h" + +OpenSearchManager::OpenSearchManager(QObject *parent) + : QObject(parent) + , m_activeEngine(0) +{ + m_state = IDLE; +} + +OpenSearchManager::~OpenSearchManager() { + qDeleteAll(m_enginesMap.values()); + m_enginesMap.clear(); +} + +void OpenSearchManager::setSearchProvider(const QString &searchProvider) +{ + m_activeEngine = 0; + + if (!m_enginesMap.contains(searchProvider)) { + const QString fileName = KGlobal::dirs()->findResource("data", "konqueror/opensearch/" + searchProvider + ".xml"); + if (fileName.isEmpty()) { + return; + } + QFile file(fileName); + + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + kWarning(1202) << "Cannot open opensearch description file: " + fileName; + return; + } + + OpenSearchReader reader; + OpenSearchEngine *engine = reader.read(&file); + + if (engine) { + m_enginesMap.insert(searchProvider, engine); + } + else { + return; + } + } + + m_activeEngine = m_enginesMap.value(searchProvider); +} + +bool OpenSearchManager::isSuggestionAvailable() +{ + return m_activeEngine != 0; +} + +void OpenSearchManager::addOpenSearchEngine(const KUrl &url, const QString &title) +{ + Q_UNUSED(title); + + m_jobData.clear(); + + if (m_state != IDLE) { + // TODO: cancel job + } + + m_state = REQ_DESCRIPTION; + KIO::TransferJob *job = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo); + connect(job, SIGNAL(data(KIO::Job *, const QByteArray &)), + this, SLOT(dataReceived(KIO::Job *, const QByteArray &))); + connect(job, SIGNAL(result(KJob *)), SLOT(jobFinished(KJob *))); +} + +void OpenSearchManager::requestSuggestion(const QString &searchText) +{ + if (!m_activeEngine) { + return; + } + + if (m_state != IDLE) { + // TODO: cancel job + } + m_state = REQ_SUGGESTION; + + KUrl url = m_activeEngine->suggestionsUrl(searchText); + kDebug(1202) << "Requesting for suggestions: " << url.url(); + m_jobData.clear(); + KIO::TransferJob *job = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo); + connect(job, SIGNAL(data(KIO::Job *, const QByteArray &)), + this, SLOT(dataReceived(KIO::Job *, const QByteArray &))); + connect(job, SIGNAL(result(KJob *)), SLOT(jobFinished(KJob *))); +} + +void OpenSearchManager::dataReceived(KIO::Job *job, const QByteArray &data) +{ + Q_UNUSED(job); + m_jobData.append(data); +} + +void OpenSearchManager::jobFinished(KJob *job) +{ + if (job->error()) { + return; // just silently return + } + + if (m_state == REQ_SUGGESTION) { + const QStringList suggestionsList = m_activeEngine->parseSuggestion(m_jobData); + kDebug(1202) << "Received suggestion from " << m_activeEngine->name() << ": " << suggestionsList; + + emit suggestionReceived(suggestionsList); + } + else if (m_state == REQ_DESCRIPTION) { + OpenSearchReader reader; + OpenSearchEngine *engine = reader.read(m_jobData); + if (engine) { + m_enginesMap.insert(engine->name(), engine); + QString path = KGlobal::dirs()->findResource("data", "konqueror/opensearch/"); + QString fileName = trimmedEngineName(engine->name()); + QFile file(path + fileName + ".xml"); + OpenSearchWriter writer; + writer.write(&file, engine); + + QString searchUrl = OpenSearchEngine::parseTemplate("\\{@}", engine->searchUrlTemplate()); + emit openSearchEngineAdded(engine->name(), searchUrl, fileName); + } + else { + kFatal() << "Error while adding new open search engine"; + } + } +} + +QString OpenSearchManager::trimmedEngineName(const QString &engineName) const +{ + QString trimmed; + QString::ConstIterator constIter = engineName.constBegin(); + while (constIter != engineName.constEnd()) { + if (constIter->isSpace()) { + trimmed.append('-'); + } + else if (*constIter != '.') { + trimmed.append(constIter->toLower()); + } + constIter++; + } + + return trimmed; +} + +#include "opensearchmanager.moc" + + -- cgit v1.2.1 From 7888d6166a34cdae0c88956208d9269fe448d3e6 Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Mon, 16 Aug 2010 11:52:08 +0200 Subject: OpenSearch review - file headers fix - clean up code - import engine files --- src/search/opensearchmanager.cpp | 154 ++++++++++++++++++++++++--------------- 1 file changed, 96 insertions(+), 58 deletions(-) (limited to 'src/search/opensearchmanager.cpp') diff --git a/src/search/opensearchmanager.cpp b/src/search/opensearchmanager.cpp index 6e37db77..463c0a11 100644 --- a/src/search/opensearchmanager.cpp +++ b/src/search/opensearchmanager.cpp @@ -1,35 +1,50 @@ -/* This file is part of the KDE project - * Copyright (C) 2009 Fredy Yanardi - * - * This library 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 library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public License - * along with this library; see the file COPYING.LIB. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - */ - +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2009 by Fredy Yanardi +* Copyright (C) 2010 by Lionel Chauvin +* Copyright (C) 2010 by Andrea Diamantini +* +* +* 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) version 3 or any later version +* accepted by the membership of KDE e.V. (or its successor approved +* by the membership of KDE e.V.), which shall act as a proxy +* defined in Section 14 of version 3 of the license. +* +* 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 . +* +* ============================================================ */ + + +// Self Includes #include "opensearchmanager.h" +#include "opensearchmanager.moc" -#include +// Local Includes +#include "opensearchengine.h" +#include "opensearchreader.h" +#include "opensearchwriter.h" +// KDE Includes #include #include #include #include #include -#include "opensearchengine.h" -#include "opensearchreader.h" -#include "opensearchwriter.h" +// Qt Includes +#include + OpenSearchManager::OpenSearchManager(QObject *parent) : QObject(parent) @@ -38,34 +53,44 @@ OpenSearchManager::OpenSearchManager(QObject *parent) m_state = IDLE; } -OpenSearchManager::~OpenSearchManager() { + +OpenSearchManager::~OpenSearchManager() +{ qDeleteAll(m_enginesMap.values()); m_enginesMap.clear(); } + void OpenSearchManager::setSearchProvider(const QString &searchProvider) { m_activeEngine = 0; - if (!m_enginesMap.contains(searchProvider)) { - const QString fileName = KGlobal::dirs()->findResource("data", "konqueror/opensearch/" + searchProvider + ".xml"); - if (fileName.isEmpty()) { + if (!m_enginesMap.contains(searchProvider)) + { + const QString fileName = KGlobal::dirs()->findResource("data", "rekonq/opensearch/" + searchProvider + ".xml"); + kDebug() << searchProvider << " file name path: " << fileName; + if (fileName.isEmpty()) + { + kDebug() << "OpenSearch file name empty"; return; } QFile file(fileName); - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { - kWarning(1202) << "Cannot open opensearch description file: " + fileName; + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) + { + kDebug() << "Cannot open opensearch description file: " + fileName; return; } OpenSearchReader reader; OpenSearchEngine *engine = reader.read(&file); - if (engine) { + if (engine) + { m_enginesMap.insert(searchProvider, engine); } - else { + else + { return; } } @@ -73,72 +98,81 @@ void OpenSearchManager::setSearchProvider(const QString &searchProvider) m_activeEngine = m_enginesMap.value(searchProvider); } + bool OpenSearchManager::isSuggestionAvailable() { return m_activeEngine != 0; } + void OpenSearchManager::addOpenSearchEngine(const KUrl &url, const QString &title) { Q_UNUSED(title); m_jobData.clear(); - if (m_state != IDLE) { + if (m_state != IDLE) + { // TODO: cancel job } m_state = REQ_DESCRIPTION; KIO::TransferJob *job = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo); - connect(job, SIGNAL(data(KIO::Job *, const QByteArray &)), - this, SLOT(dataReceived(KIO::Job *, const QByteArray &))); - connect(job, SIGNAL(result(KJob *)), SLOT(jobFinished(KJob *))); + connect(job, SIGNAL(data(KIO::Job *, const QByteArray &)), this, SLOT(dataReceived(KIO::Job *, const QByteArray &))); + connect(job, SIGNAL(result(KJob *)), this, SLOT(jobFinished(KJob *))); } + void OpenSearchManager::requestSuggestion(const QString &searchText) { - if (!m_activeEngine) { + if (!m_activeEngine) return; - } - if (m_state != IDLE) { + if (m_state != IDLE) + { // TODO: cancel job } m_state = REQ_SUGGESTION; KUrl url = m_activeEngine->suggestionsUrl(searchText); - kDebug(1202) << "Requesting for suggestions: " << url.url(); + kDebug() << "Requesting for suggestions: " << url.url(); m_jobData.clear(); + KIO::TransferJob *job = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo); - connect(job, SIGNAL(data(KIO::Job *, const QByteArray &)), - this, SLOT(dataReceived(KIO::Job *, const QByteArray &))); - connect(job, SIGNAL(result(KJob *)), SLOT(jobFinished(KJob *))); + connect(job, SIGNAL(data(KIO::Job *, const QByteArray &)), this, SLOT(dataReceived(KIO::Job *, const QByteArray &))); + connect(job, SIGNAL(result(KJob *)), this, SLOT(jobFinished(KJob *))); } + void OpenSearchManager::dataReceived(KIO::Job *job, const QByteArray &data) { Q_UNUSED(job); m_jobData.append(data); } + void OpenSearchManager::jobFinished(KJob *job) { - if (job->error()) { + if (job->error()) return; // just silently return - } - if (m_state == REQ_SUGGESTION) { + if (m_state == REQ_SUGGESTION) + { const QStringList suggestionsList = m_activeEngine->parseSuggestion(m_jobData); - kDebug(1202) << "Received suggestion from " << m_activeEngine->name() << ": " << suggestionsList; + kDebug() << "Received suggestion from " << m_activeEngine->name() << ": " << suggestionsList; emit suggestionReceived(suggestionsList); + return; } - else if (m_state == REQ_DESCRIPTION) { + + if (m_state == REQ_DESCRIPTION) + { OpenSearchReader reader; OpenSearchEngine *engine = reader.read(m_jobData); - if (engine) { + if (engine) + { m_enginesMap.insert(engine->name(), engine); - QString path = KGlobal::dirs()->findResource("data", "konqueror/opensearch/"); + QString path = KGlobal::dirs()->findResource("data", "rekonq/opensearch/"); QString fileName = trimmedEngineName(engine->name()); QFile file(path + fileName + ".xml"); OpenSearchWriter writer; @@ -147,29 +181,33 @@ void OpenSearchManager::jobFinished(KJob *job) QString searchUrl = OpenSearchEngine::parseTemplate("\\{@}", engine->searchUrlTemplate()); emit openSearchEngineAdded(engine->name(), searchUrl, fileName); } - else { + else + { kFatal() << "Error while adding new open search engine"; } } } + QString OpenSearchManager::trimmedEngineName(const QString &engineName) const { QString trimmed; QString::ConstIterator constIter = engineName.constBegin(); - while (constIter != engineName.constEnd()) { - if (constIter->isSpace()) { + while (constIter != engineName.constEnd()) + { + if (constIter->isSpace()) + { trimmed.append('-'); } - else if (*constIter != '.') { - trimmed.append(constIter->toLower()); + else + { + if (*constIter != '.') + { + trimmed.append(constIter->toLower()); + } } constIter++; } return trimmed; } - -#include "opensearchmanager.moc" - - -- cgit v1.2.1