diff --git a/src/plugins/help/helpindexfilter.cpp b/src/plugins/help/helpindexfilter.cpp index 6b0f221a791d5572eee46d09aaeffd2d339422cf..5018748468443bdb35f1edc72779ac11e44299bc 100644 --- a/src/plugins/help/helpindexfilter.cpp +++ b/src/plugins/help/helpindexfilter.cpp @@ -28,43 +28,134 @@ **************************************************************************/ #include "helpindexfilter.h" -#include "helpplugin.h" +#include "helpmanager.h" #include <extensionsystem/pluginmanager.h> #include <coreplugin/icore.h> #include <coreplugin/modemanager.h> +#include <QtGui/QIcon> + #include <QtHelp/QHelpEngine> #include <QtHelp/QHelpIndexModel> +#include <QtSql/QSqlDatabase> +#include <QtSql/QSqlDriver> +#include <QtSql/QSqlError> +#include <QtSql/QSqlQuery> + using namespace Locator; using namespace Help; using namespace Help::Internal; Q_DECLARE_METATYPE(ILocatorFilter*); -HelpIndexFilter::HelpIndexFilter(HelpPlugin *plugin, QHelpEngine *helpEngine): - m_plugin(plugin), - m_helpEngine(helpEngine), - m_icon(QIcon()) // TODO: Put an icon next to the results +// -- HelpIndexFilter::HelpFileReader + +class HelpIndexFilter::HelpFileReader +{ +public: + HelpFileReader(); + ~HelpFileReader(); + +public: + void updateHelpFiles(); + QList<FilterEntry> matchesFor(const QString &entry, ILocatorFilter *locator); + +private: + bool m_initialized; + QStringList m_helpFiles; +}; + +HelpIndexFilter::HelpFileReader::HelpFileReader() + : m_initialized(false) +{ +} + +HelpIndexFilter::HelpFileReader::~HelpFileReader() +{ +} + +void HelpIndexFilter::HelpFileReader::updateHelpFiles() +{ + m_helpFiles.clear(); + const QLatin1String id("HelpIndexFilter::HelpFileReader::helpFiles"); + { + QSqlDatabase db = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"), id); + if (db.driver() + && db.driver()->lastError().type() == QSqlError::NoError) { + db.setDatabaseName(HelpManager::collectionFilePath()); + if (db.open()) { + QSqlQuery query = QSqlQuery(db); + query.exec(QLatin1String("SELECT a.FilePath FROM NamespaceTable a")); + while (query.next()) + m_helpFiles.append(query.value(0).toString()); + } + } + } + QSqlDatabase::removeDatabase(id); +} + +QList<FilterEntry> HelpIndexFilter::HelpFileReader::matchesFor(const QString &id, + ILocatorFilter *locator) +{ + if (!m_initialized) { + updateHelpFiles(); + m_initialized = true; + } + + QList<FilterEntry> entries; + const QLatin1String sqlite("QSQLITE"); + const QLatin1String name("HelpIndexFilter::HelpFileReader::matchesFor"); + foreach(const QString &file, m_helpFiles) { + if (!QFile::exists(file)) + continue; + { + QSqlDatabase db = QSqlDatabase::addDatabase(sqlite, name); + if (db.driver() + && db.driver()->lastError().type() == QSqlError::NoError) { + db.setDatabaseName(file); + if (db.open()) { + QSqlQuery query = QSqlQuery(db); + query.setForwardOnly(true); + query.exec(QString::fromLatin1("SELECT DISTINCT Name FROM " + "IndexTable WHERE Name LIKE '%%1%'").arg(id)); + while (query.next()) { + const QString &key = query.value(0).toString(); + if (!key.isEmpty()) { + // NOTE: do not use an icon since it is really slow + entries.append(FilterEntry(locator, key, QVariant(), + QIcon())); + } + } + } + } + } + QSqlDatabase::removeDatabase(name); + } + return entries; +} + +// -- HelpIndexFilter + +HelpIndexFilter::HelpIndexFilter() + : m_fileReader(new HelpFileReader) { setIncludedByDefault(false); setShortcutString(QString(QLatin1Char('?'))); - connect(m_helpEngine->indexModel(), SIGNAL(indexCreated()), - this, SLOT(updateIndices())); + connect(&HelpManager::helpEngineCore(), SIGNAL(setupFinished()), this, + SLOT(updateHelpFiles())); } -void HelpIndexFilter::updateIndices() +HelpIndexFilter::~HelpIndexFilter() { - const QString currentFilter = m_plugin->indexFilter(); - if (!currentFilter.isEmpty()) - m_plugin->setIndexFilter(QString()); - - m_helpIndex = m_helpEngine->indexModel()->stringList(); + delete m_fileReader; +} - if (!currentFilter.isEmpty()) - m_plugin->setIndexFilter(currentFilter); +void HelpIndexFilter::updateHelpFiles() +{ + m_fileReader->updateHelpFiles(); } QString HelpIndexFilter::displayName() const @@ -84,19 +175,15 @@ ILocatorFilter::Priority HelpIndexFilter::priority() const QList<FilterEntry> HelpIndexFilter::matchesFor(const QString &entry) { - QList<FilterEntry> entries; - foreach (const QString &string, m_helpIndex) { - if (string.contains(entry, Qt::CaseInsensitive)) { - FilterEntry entry(this, string, QVariant(), m_icon); - entries.append(entry); - } - } - return entries; + if (entry.length() < 2) + return QList<FilterEntry>(); + return m_fileReader->matchesFor(entry, this); } void HelpIndexFilter::accept(FilterEntry selection) const { - QMap<QString, QUrl> links = m_helpEngine->indexModel()->linksForKeyword(selection.displayName); + const QHelpEngineCore &engine = HelpManager::helpEngineCore(); + QMap<QString, QUrl> links = engine.linksForIdentifier(selection.displayName); if (links.size() == 1) { emit linkActivated(links.begin().value()); } else if (!links.isEmpty()) { diff --git a/src/plugins/help/helpindexfilter.h b/src/plugins/help/helpindexfilter.h index 8dbf49343975e92f489bbc62ddbffc62526653ad..9c3a6de46599c2b2dc0eaaefec84d0d4a87ecfb0 100644 --- a/src/plugins/help/helpindexfilter.h +++ b/src/plugins/help/helpindexfilter.h @@ -32,13 +32,6 @@ #include <locator/ilocatorfilter.h> -#include <QtGui/QIcon> - -QT_BEGIN_NAMESPACE -class QHelpEngine; -class QUrl; -QT_END_NAMESPACE - namespace Help { namespace Internal { @@ -47,9 +40,11 @@ class HelpPlugin; class HelpIndexFilter : public Locator::ILocatorFilter { Q_OBJECT + class HelpFileReader; public: - HelpIndexFilter(HelpPlugin *plugin, QHelpEngine *helpEngine); + HelpIndexFilter(); + ~HelpIndexFilter(); // ILocatorFilter QString displayName() const; @@ -64,13 +59,10 @@ signals: void linksActivated(const QMap<QString, QUrl> &urls, const QString &keyword) const; private slots: - void updateIndices(); + void updateHelpFiles(); private: - HelpPlugin *m_plugin; - QHelpEngine *m_helpEngine; - QStringList m_helpIndex; - QIcon m_icon; + HelpFileReader *m_fileReader; }; } // namespace Internal diff --git a/src/plugins/help/helpplugin.cpp b/src/plugins/help/helpplugin.cpp index 901085a764d71667658e26ee2cf7e3a87695af61..d51ffbaea2b1226b6405a309d005b1d03c0ec291 100644 --- a/src/plugins/help/helpplugin.cpp +++ b/src/plugins/help/helpplugin.cpp @@ -354,13 +354,6 @@ bool HelpPlugin::initialize(const QStringList &arguments, QString *error) connect(m_indexWidget, SIGNAL(linksActivated(QMap<QString, QUrl>, QString)), m_centralWidget, SLOT(showTopicChooser(QMap<QString, QUrl>, QString))); - HelpIndexFilter *helpIndexFilter = new HelpIndexFilter(this, m_helpEngine); - addAutoReleasedObject(helpIndexFilter); - connect(helpIndexFilter, SIGNAL(linkActivated(QUrl)), this, - SLOT(switchToHelpMode(QUrl))); - connect(helpIndexFilter, SIGNAL(linksActivated(QMap<QString, QUrl>, QString)), - this, SLOT(switchToHelpMode(QMap<QString, QUrl>, QString))); - previousAction->setEnabled(m_centralWidget->isBackwardAvailable()); nextAction->setEnabled(m_centralWidget->isForwardAvailable()); @@ -396,6 +389,13 @@ bool HelpPlugin::initialize(const QStringList &arguments, QString *error) connect(generalSettingsPage, SIGNAL(fontChanged()), this, SLOT(fontChanged())); connect(generalSettingsPage, SIGNAL(dialogAccepted()), this, SLOT(checkForGeneralChanges())); + HelpIndexFilter *helpIndexFilter = new HelpIndexFilter(); + addAutoReleasedObject(helpIndexFilter); + connect(helpIndexFilter, SIGNAL(linkActivated(QUrl)), this, + SLOT(switchToHelpMode(QUrl))); + connect(helpIndexFilter, SIGNAL(linksActivated(QMap<QString, QUrl>, QString)), + this, SLOT(switchToHelpMode(QMap<QString, QUrl>, QString))); + return true; }