File indexing completed on 2024-05-05 04:40:50

0001 /*
0002     SPDX-FileCopyrightText: 2009 Aleix Pol <aleixpol@kde.org>
0003     SPDX-FileCopyrightText: 2010 Benjamin Port <port.benjamin@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "qthelpplugin.h"
0009 
0010 #include <interfaces/icore.h>
0011 #include <interfaces/idocumentationcontroller.h>
0012 #include "qthelpprovider.h"
0013 #include "qthelpqtdoc.h"
0014 #include "qthelp_config_shared.h"
0015 #include "debug.h"
0016 #include "qthelpconfig.h"
0017 
0018 #include <KPluginFactory>
0019 
0020 #include <QDirIterator>
0021 
0022 QtHelpPlugin *QtHelpPlugin::s_plugin = nullptr;
0023 
0024 K_PLUGIN_FACTORY_WITH_JSON(QtHelpPluginFactory, "kdevqthelp.json", registerPlugin<QtHelpPlugin>(); )
0025 
0026 QtHelpPlugin::QtHelpPlugin(QObject* parent, const QVariantList& args)
0027     : KDevelop::IPlugin(QStringLiteral("kdevqthelp"), parent)
0028     , m_qtHelpProviders()
0029     , m_qtDoc(new QtHelpQtDoc(this, QVariantList()))
0030     , m_loadSystemQtDoc(false)
0031 {
0032     Q_UNUSED(args);
0033     s_plugin = this;
0034     connect(this, &QtHelpPlugin::changedProvidersList, KDevelop::ICore::self()->documentationController(), &KDevelop::IDocumentationController::changedDocumentationProviders);
0035     QMetaObject::invokeMethod(this, "readConfig", Qt::QueuedConnection);
0036 }
0037 
0038 QtHelpPlugin::~QtHelpPlugin()
0039 {
0040 }
0041 
0042 
0043 void QtHelpPlugin::readConfig()
0044 {
0045     QStringList iconList, nameList, pathList, ghnsList;
0046     QString searchDir;
0047     qtHelpReadConfig(iconList, nameList, pathList, ghnsList, searchDir, m_loadSystemQtDoc);
0048 
0049     searchHelpDirectory(pathList, nameList, iconList, searchDir);
0050     loadQtHelpProvider(pathList, nameList, iconList);
0051     loadQtDocumentation(m_loadSystemQtDoc);
0052 
0053     emit changedProvidersList();
0054 }
0055 
0056 void QtHelpPlugin::loadQtDocumentation(bool loadQtDoc)
0057 {
0058     if(!loadQtDoc){
0059         m_qtDoc->unloadDocumentation();
0060     } else if(loadQtDoc) {
0061         m_qtDoc->loadDocumentation();
0062     }
0063 }
0064 
0065 void QtHelpPlugin::searchHelpDirectory(QStringList& pathList, QStringList& nameList, QStringList& iconList, const QString& searchDir)
0066 {
0067     if (searchDir.isEmpty()) {
0068         return;
0069     }
0070 
0071     qCDebug(QTHELP) << "Searching qch files in: " << searchDir;
0072     QDirIterator dirIt(searchDir, QStringList() << QStringLiteral("*.qch"), QDir::Files, QDirIterator::Subdirectories);
0073     const QString logo(QStringLiteral("qtlogo"));
0074     while(dirIt.hasNext() == true)
0075     {
0076         dirIt.next();
0077         qCDebug(QTHELP) << "qch found: " << dirIt.filePath();
0078         pathList.append(dirIt.filePath());
0079         nameList.append(dirIt.fileInfo().baseName());
0080         iconList.append(logo);
0081     }
0082 }
0083 
0084 
0085 void QtHelpPlugin::loadQtHelpProvider(const QStringList& pathList, const QStringList& nameList, const QStringList& iconList)
0086 {
0087     QList<QtHelpProvider*> oldList(m_qtHelpProviders);
0088     m_qtHelpProviders.clear();
0089     for(int i=0; i < pathList.length(); i++) {
0090         // check if provider already exist
0091         QString fileName = pathList.at(i);
0092         QString name = nameList.at(i);
0093         QString iconName = iconList.at(i);
0094         QString nameSpace = QHelpEngineCore::namespaceName(fileName);
0095         if(!nameSpace.isEmpty()){
0096             QtHelpProvider *provider = nullptr;
0097             for (QtHelpProvider* oldProvider : qAsConst(oldList)) {
0098                 if(QHelpEngineCore::namespaceName(oldProvider->fileName()) == nameSpace){
0099                     provider = oldProvider;
0100                     oldList.removeAll(provider);
0101                     break;
0102                 }
0103             }
0104             if(!provider){
0105                 provider = new QtHelpProvider(this, fileName, name, iconName, QVariantList());
0106             }else{
0107                 provider->setName(name);
0108                 provider->setIconName(iconName);
0109             }
0110 
0111             bool exist = false;
0112             for (QtHelpProvider* existingProvider : qAsConst(m_qtHelpProviders)) {
0113                 if(QHelpEngineCore::namespaceName(existingProvider->fileName()) ==  nameSpace){
0114                     exist = true;
0115                     break;
0116                 }
0117             }
0118 
0119             if(!exist){
0120                 m_qtHelpProviders.append(provider);
0121             }
0122         }
0123     }
0124 
0125     // delete unused providers
0126     qDeleteAll(oldList);
0127 }
0128 
0129 QList<KDevelop::IDocumentationProvider*> QtHelpPlugin::providers()
0130 {
0131     QList<KDevelop::IDocumentationProvider*> list;
0132     list.reserve(m_qtHelpProviders.size() + (m_loadSystemQtDoc?1:0));
0133     for (QtHelpProvider* provider : qAsConst(m_qtHelpProviders)) {
0134         list.append(provider);
0135     }
0136     if(m_loadSystemQtDoc){
0137         list.append(m_qtDoc);
0138     }
0139     return list;
0140 }
0141 
0142 QList<QtHelpProvider*> QtHelpPlugin::qtHelpProviderLoaded()
0143 {
0144     return m_qtHelpProviders;
0145 }
0146 
0147 bool QtHelpPlugin::isQtHelpQtDocLoaded() const
0148 {
0149     return m_loadSystemQtDoc && m_qtDoc->isInitialized();
0150 }
0151 
0152 bool QtHelpPlugin::isQtHelpAvailable() const
0153 {
0154     return !m_qtDoc->qchFiles().isEmpty();
0155 }
0156 
0157 KDevelop::ConfigPage* QtHelpPlugin::configPage(int number, QWidget* parent)
0158 {
0159     if (number == 0) {
0160         return new QtHelpConfig(this, parent);
0161     }
0162     return nullptr;
0163 }
0164 
0165 int QtHelpPlugin::configPages() const
0166 {
0167     return 1;
0168 }
0169 
0170 #include "qthelpplugin.moc"
0171 #include "moc_qthelpplugin.cpp"