diff --git a/documentation/qthelp/qthelpplugin.cpp b/documentation/qthelp/qthelpplugin.cpp index fd4218f65b..fd3d1dd1e8 100644 --- a/documentation/qthelp/qthelpplugin.cpp +++ b/documentation/qthelp/qthelpplugin.cpp @@ -1,178 +1,177 @@ /* This file is part of KDevelop Copyright 2009 Aleix Pol Copyright 2010 Benjamin Port This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library 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 "qthelpplugin.h" #include #include #include #include #include #include "qthelpprovider.h" #include "qthelpqtdoc.h" #include "qthelp_config_shared.h" #include "debug.h" #include "qthelpconfig.h" QtHelpPlugin *QtHelpPlugin::s_plugin = 0; K_PLUGIN_FACTORY_WITH_JSON(QtHelpPluginFactory, "kdevqthelp.json", registerPlugin(); ) QtHelpPlugin::QtHelpPlugin(QObject* parent, const QVariantList& args) : KDevelop::IPlugin("kdevqthelp", parent) , m_qtHelpProviders() - , m_qtDoc(0) + , m_qtDoc(new QtHelpQtDoc(this, QVariantList())) + , m_loadSystemQtDoc(false) { KDEV_USE_EXTENSION_INTERFACE( KDevelop::IDocumentationProviderProvider ) Q_UNUSED(args); s_plugin = this; connect(this, &QtHelpPlugin::changedProvidersList, KDevelop::ICore::self()->documentationController(), &KDevelop::IDocumentationController::changedDocumentationProviders); QMetaObject::invokeMethod(this, "readConfig", Qt::QueuedConnection); } QtHelpPlugin::~QtHelpPlugin() { } void QtHelpPlugin::readConfig() { QStringList iconList, nameList, pathList, ghnsList; - bool loadQtDoc; QString searchDir; - qtHelpReadConfig(iconList, nameList, pathList, ghnsList, searchDir, loadQtDoc); + qtHelpReadConfig(iconList, nameList, pathList, ghnsList, searchDir, m_loadSystemQtDoc); searchHelpDirectory(pathList, nameList, iconList, searchDir); loadQtHelpProvider(pathList, nameList, iconList); - loadQtDocumentation(loadQtDoc); + loadQtDocumentation(m_loadSystemQtDoc); emit changedProvidersList(); } void QtHelpPlugin::loadQtDocumentation(bool loadQtDoc) { - if(m_qtDoc && !loadQtDoc){ - delete m_qtDoc; - m_qtDoc = 0; - } else if(!m_qtDoc && loadQtDoc) { - m_qtDoc = new QtHelpQtDoc(this, QVariantList()); + if(!loadQtDoc){ + m_qtDoc->unloadDocumentation(); + } else if(loadQtDoc) { + m_qtDoc->loadDocumentation(); } } void QtHelpPlugin::searchHelpDirectory(QStringList& pathList, QStringList& nameList, QStringList& iconList, const QString& searchDir) { if (searchDir.isEmpty()) { return; } qCDebug(QTHELP) << "Searching qch files in: " << searchDir; QDirIterator dirIt(searchDir, QStringList() << "*.qch", QDir::Files, QDirIterator::Subdirectories); const QString logo("qtlogo"); while(dirIt.hasNext() == true) { dirIt.next(); qCDebug(QTHELP) << "qch found: " << dirIt.filePath(); pathList.append(dirIt.filePath()); nameList.append(dirIt.fileInfo().baseName()); iconList.append(logo); } } void QtHelpPlugin::loadQtHelpProvider(QStringList pathList, QStringList nameList, QStringList iconList) { QList oldList(m_qtHelpProviders); m_qtHelpProviders.clear(); for(int i=0; i < pathList.length(); i++) { // check if provider already exist QString fileName = pathList.at(i); QString name = nameList.at(i); QString iconName = iconList.at(i); QString nameSpace = QHelpEngineCore::namespaceName(fileName); if(!nameSpace.isEmpty()){ QtHelpProvider *provider = 0; foreach(QtHelpProvider* oldProvider, oldList){ if(QHelpEngineCore::namespaceName(oldProvider->fileName()) == nameSpace){ provider = oldProvider; oldList.removeAll(provider); break; } } if(!provider){ provider = new QtHelpProvider(this, fileName, name, iconName, QVariantList()); }else{ provider->setName(name); provider->setIconName(iconName); } bool exist = false; foreach(QtHelpProvider* existingProvider, m_qtHelpProviders){ if(QHelpEngineCore::namespaceName(existingProvider->fileName()) == nameSpace){ exist = true; break; } } if(!exist){ m_qtHelpProviders.append(provider); } } } // delete unused providers qDeleteAll(oldList); } QList QtHelpPlugin::providers() { QList list; foreach(QtHelpProvider* provider, m_qtHelpProviders) { list.append(provider); } - if(m_qtDoc){ + if(m_loadSystemQtDoc){ list.append(m_qtDoc); } return list; } QList QtHelpPlugin::qtHelpProviderLoaded() { return m_qtHelpProviders; } bool QtHelpPlugin::qtHelpQtDocLoaded(){ - return m_qtDoc; + return m_loadSystemQtDoc; } KDevelop::ConfigPage* QtHelpPlugin::configPage(int number, QWidget* parent) { if (number == 0) { return new QtHelpConfig(this, parent); } return nullptr; } int QtHelpPlugin::configPages() const { return 1; } #include "qthelpplugin.moc" diff --git a/documentation/qthelp/qthelpplugin.h b/documentation/qthelp/qthelpplugin.h index 65aa701c6e..e268cc5865 100644 --- a/documentation/qthelp/qthelpplugin.h +++ b/documentation/qthelp/qthelpplugin.h @@ -1,62 +1,63 @@ /* This file is part of KDevelop Copyright 2009 Aleix Pol Copyright 2010 Benjamin Port This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library 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. */ #ifndef QTHELPPLUGIN_H #define QTHELPPLUGIN_H #include #include class QtHelpProvider; class QtHelpQtDoc; class QtHelpDocumentation; class QtHelpPlugin : public KDevelop::IPlugin, public KDevelop::IDocumentationProviderProvider { Q_OBJECT Q_INTERFACES( KDevelop::IDocumentationProviderProvider ) public: QtHelpPlugin(QObject *parent, const QVariantList & args); ~QtHelpPlugin() override; static QtHelpPlugin *self() { return s_plugin; } QList providers() override; QList qtHelpProviderLoaded(); bool qtHelpQtDocLoaded(); int configPages() const override; KDevelop::ConfigPage* configPage(int number, QWidget* parent) override; public slots: void readConfig(); signals: void changedProvidersList() const override; private: void loadQtDocumentation(bool loadQtDoc); void searchHelpDirectory(QStringList& pathList, QStringList& nameList, QStringList& iconList, const QString& searchDir); void loadQtHelpProvider(QStringList pathList, QStringList nameList, QStringList iconList); static QtHelpPlugin *s_plugin; QList m_qtHelpProviders; QtHelpQtDoc* m_qtDoc; + bool m_loadSystemQtDoc; }; #endif // QTHELPPLUGIN_H diff --git a/documentation/qthelp/qthelpqtdoc.cpp b/documentation/qthelp/qthelpqtdoc.cpp index 817da643e7..fa698ec64b 100644 --- a/documentation/qthelp/qthelpqtdoc.cpp +++ b/documentation/qthelp/qthelpqtdoc.cpp @@ -1,112 +1,136 @@ /* This file is part of KDevelop Copyright 2009 Aleix Pol Copyright 2009 David Nolden Copyright 2010 Benjamin Port + Copyright 2016 Andreas Cord-Landwehr This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library 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 "qthelpqtdoc.h" #include "debug.h" #include #include #include #include #include #include namespace { QString qmakeCandidate() { // return the first qmake executable we can find const QStringList candidates = {"qmake", "qmake-qt4", "qmake-qt5"}; auto it = std::find_if(candidates.constBegin(), candidates.constEnd(), [](const QString& candidate) { return !QStandardPaths::findExecutable(candidate).isEmpty(); }); return it != candidates.constEnd() ? *it : QString(); } } QtHelpQtDoc::QtHelpQtDoc(QObject *parent, const QVariantList &args) : QtHelpProviderAbstract(parent, "qthelpcollection.qhc", args) + , m_path(QString()) { Q_UNUSED(args); registerDocumentations(); } void QtHelpQtDoc::registerDocumentations() { const QString qmake = qmakeCandidate(); if (!qmake.isEmpty()) { KProcess *p = new KProcess; p->setOutputChannelMode(KProcess::MergedChannels); p->setProgram(qmake, QStringList("-query") << "QT_INSTALL_DOCS"); p->start(); connect(p, static_cast(&KProcess::finished), this, &QtHelpQtDoc::lookupDone); } } void QtHelpQtDoc::lookupDone(int code) { if(code==0) { KProcess* p = qobject_cast(sender()); - QString path = QDir::fromNativeSeparators(QString::fromLatin1(p->readAllStandardOutput().trimmed())); - qCDebug(QTHELP) << "Detected doc path:" << path; - - if (!path.isEmpty()) { - loadDirectory(path); - loadDirectory(path+"/qch/"); - } + m_path = QDir::fromNativeSeparators(QString::fromLatin1(p->readAllStandardOutput().trimmed())); + qCDebug(QTHELP) << "Detected doc path:" << m_path; } sender()->deleteLater(); } -void QtHelpQtDoc::loadDirectory(const QString& path) +void QtHelpQtDoc::loadDocumentation() { - QDir d(path); - if(path.isEmpty() || !d.exists()) { - qCDebug(QTHELP) << "no QtHelp found at all"; + if(m_path.isEmpty()) { return; } - foreach(const QString& file, d.entryList(QDir::Files)) { - QString fileName=path+'/'+file; - QString fileNamespace = QHelpEngineCore::namespaceName(fileName); + QString fileName = qchFile(); + if(fileName.isEmpty()) { + qCWarning(QTHELP) << "could not find QCH file in directory" << m_path; + return; + } + + QString fileNamespace = QHelpEngineCore::namespaceName(fileName); + if (!fileNamespace.isEmpty() && !m_engine.registeredDocumentations().contains(fileNamespace)) { + qCDebug(QTHELP) << "loading doc" << fileName << fileNamespace; + if(!m_engine.registerDocumentation(fileName)) + qCCritical(QTHELP) << "error >> " << fileName << m_engine.error(); + } +} + +void QtHelpQtDoc::unloadDocumentation() +{ + QString fileName = qchFile(); + QString fileNamespace = QHelpEngineCore::namespaceName(fileName); + if(!fileName.isEmpty() && !m_engine.registeredDocumentations().contains(fileNamespace)) { + m_engine.unregisterDocumentation(fileName); + } +} + +QString QtHelpQtDoc::qchFile() const +{ + QVector paths; + paths << m_path << m_path+"/qch/"; + foreach (const auto &path, paths) { + QDir d(path); + if(path.isEmpty() || !d.exists()) { + continue; + } - if (!fileNamespace.isEmpty() && !m_engine.registeredDocumentations().contains(fileNamespace)) { - qCDebug(QTHELP) << "loading doc" << fileName << fileNamespace; - if(!m_engine.registerDocumentation(fileName)) - qCDebug(QTHELP) << "error >> " << fileName << m_engine.error(); + foreach(const QString& file, d.entryList(QDir::Files)) { + QString fileName=path+'/'+file; + return fileName; } } - qCDebug(QTHELP) << "registered" << m_engine.error() << m_engine.registeredDocumentations(); + qCDebug(QTHELP) << "no QCH file found at all"; + return QString(); } QIcon QtHelpQtDoc::icon() const { return QIcon::fromTheme("qtlogo"); } QString QtHelpQtDoc::name() const { return i18n("QtHelp"); } diff --git a/documentation/qthelp/qthelpqtdoc.h b/documentation/qthelp/qthelpqtdoc.h index 294313eeb5..a5d53a43e7 100644 --- a/documentation/qthelp/qthelpqtdoc.h +++ b/documentation/qthelp/qthelpqtdoc.h @@ -1,42 +1,47 @@ /* This file is part of KDevelop Copyright 2009 Aleix Pol Copyright 2010 Benjamin Port + Copyright 2016 Andreas Cord-Landwehr This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library 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. */ #ifndef QTHELPQTDOC_H #define QTHELPQTDOC_H #include "qthelpproviderabstract.h" class QtHelpQtDoc : public QtHelpProviderAbstract { Q_OBJECT - public: + public: QtHelpQtDoc(QObject *parent, const QVariantList &args); QIcon icon() const override; QString name() const override; void registerDocumentations(); + void loadDocumentation(); + void unloadDocumentation(); + /** @return local path to QCH file if it exists, otherwise returns an empty string **/ + QString qchFile() const; private: - void loadDirectory(const QString& path); + QString m_path; private slots: void lookupDone(int code); }; #endif // QTHELPQTDOC_H