1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
/*
SPDX-FileCopyrightText: 2009 Aleix Pol <aleixpol@kde.org>
SPDX-FileCopyrightText: 2010 Benjamin Port <port.benjamin@gmail.com>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "qthelpplugin.h"
#include "qthelpprovider.h"
#include "qthelpqtdoc.h"
#include "qthelp_config_shared.h"
#include "debug.h"
#include "qthelpconfig.h"
#include <interfaces/icore.h>
#include <interfaces/idocumentationcontroller.h>
#include <util/algorithm.h>
#include <util/owningrawpointercontainer.h>
#include <KPluginFactory>
#include <QDirIterator>
#include <QFile>
#include <QFileInfo>
#include <QHash>
#include <QSet>
K_PLUGIN_FACTORY_WITH_JSON(QtHelpPluginFactory, "kdevqthelp.json", registerPlugin<QtHelpPlugin>(); )
QtHelpPlugin::QtHelpPlugin(QObject* parent, const KPluginMetaData& metaData, const QVariantList& args)
: KDevelop::IPlugin(QStringLiteral("kdevqthelp"), parent, metaData)
, m_qtHelpProviders()
, m_qtDoc(new QtHelpQtDoc(this, QtHelpQtDoc::qmakeCandidates().value(0), QStringLiteral("qthelpcollection.qhc")))
, m_loadSystemQtDoc(false)
{
Q_UNUSED(args);
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;
QString searchDir;
qtHelpReadConfig(iconList, nameList, pathList, ghnsList, searchDir, m_loadSystemQtDoc);
// Make all .qch file paths read from config absolute as required by loadQtHelpProvider().
for (auto& path : pathList) {
path = QFileInfo{path}.absoluteFilePath();
}
// Pass an absolute path searchDir to searchHelpDirectory() so that it appends absolute paths to pathList.
searchDir = QFileInfo{searchDir}.absoluteFilePath();
searchHelpDirectory(pathList, nameList, iconList, searchDir);
loadQtHelpProvider(pathList, nameList, iconList);
removeUnusedHelpCollectionFiles();
loadQtDocumentation(m_loadSystemQtDoc);
emit changedProvidersList();
}
void QtHelpPlugin::loadQtDocumentation(bool loadQtDoc)
{
if (!m_qtDoc->isInitialized()) {
// defer loading until initialization has finished
connect(m_qtDoc, &QtHelpQtDoc::isInitializedChanged, this, [this, loadQtDoc]() {
loadQtDocumentation(loadQtDoc);
});
return;
}
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() << QStringLiteral("*.qch"), QDir::Files, QDirIterator::Subdirectories);
const QString logo(QStringLiteral("qtlogo"));
while(dirIt.hasNext() == true)
{
const auto& fileInfo = dirIt.nextFileInfo();
pathList.append(fileInfo.filePath());
nameList.append(fileInfo.baseName());
iconList.append(logo);
qCDebug(QTHELP) << "qch found:" << pathList.constLast();
}
}
void QtHelpPlugin::loadQtHelpProvider(const QStringList& pathList, const QStringList& nameList, const QStringList& iconList)
{
if (pathList.empty()) {
qDeleteAll(m_qtHelpProviders);
m_qtHelpProviders = {}; // shed unneeded capacity
return;
}
// Most or all new providers will likely be the same as old ones. Put the old providers in a hash table
// and attempt to reuse them instead of destroying and creating the same providers again, which is slow.
KDevelop::OwningRawPointerContainer<QHash<QString, QtHelpProvider*>> oldProviders;
oldProviders->reserve(m_qtHelpProviders.size());
for (auto* const provider : std::as_const(m_qtHelpProviders)) {
oldProviders->insert(provider->namespaceName(), provider);
}
m_qtHelpProviders.clear(); // keep the likely needed capacity
QSet<QString> registeredNamespaceNames;
registeredNamespaceNames.reserve(pathList.size());
for(int i=0; i < pathList.length(); i++) {
QString fileName = pathList.at(i);
QString name = nameList.at(i);
QString iconName = iconList.at(i);
// NOTE: fileName must be an absolute file path, because QHelpEngineCore::documentationFileName()
// returns an absolute file path, and this return value is compared to fileName.
Q_ASSERT(QFileInfo{fileName}.isAbsolute());
const auto namespaceName = QHelpEngineCore::namespaceName(fileName);
if (namespaceName.isEmpty()) {
qCWarning(QTHELP) << "skipping documentation file with an empty namespace name:" << fileName;
continue;
}
if (!Algorithm::insert(registeredNamespaceNames, namespaceName).inserted) {
qCWarning(QTHELP) << "skipping documentation file with a duplicate namespace name" << namespaceName << ':'
<< fileName;
continue;
}
QtHelpProvider *provider = nullptr;
if (const auto it = oldProviders->constFind(namespaceName); it != oldProviders->cend()) {
if ((*it)->fileName() == fileName) {
provider = *it; // reuse this matching old provider
} else {
// The namespace name determines the .qhc file name. Destroy the nonmatching old provider
// with the same namespace to prevent .qhc file conflict with the new provider created below.
delete *it;
}
oldProviders->erase(it);
}
if(!provider){
provider = new QtHelpProvider(this, fileName, namespaceName, name, iconName);
}else{
// The file name and the namespace name already match. Update the name and the icon name.
provider->setName(name);
provider->setIconName(iconName);
}
m_qtHelpProviders.append(provider);
}
}
void QtHelpPlugin::removeUnusedHelpCollectionFiles() const
{
QSet<QString> usedFileNames;
usedFileNames.reserve(m_qtHelpProviders.size() + 1);
for (const auto* const provider : std::as_const(m_qtHelpProviders)) {
usedFileNames.insert(provider->engine()->collectionFile());
}
usedFileNames.insert(m_qtDoc->engine()->collectionFile()); // this file is used even if m_loadSystemQtDoc is false
// Collect a list of files and then remove them all in a separate loop, because removing files
// while iterating over a directory might be slower in case some file system cache is invalidated.
QList<QString> filesToRemove;
QDirIterator it(QtHelpProviderAbstract::collectionFileLocation(), {QStringLiteral("*.qhc")},
QDir::Files | QDir::Hidden);
while (it.hasNext()) {
auto filePath = it.next();
if (!usedFileNames.contains(filePath)) {
filesToRemove.push_back(std::move(filePath));
}
}
if (filesToRemove.empty()) {
return;
}
QFile file; // reuse this file object to optimize
for (const auto& filePath : std::as_const(filesToRemove)) {
file.setFileName(filePath);
if (file.remove(filePath)) {
qCDebug(QTHELP) << "removed unused file" << filePath;
} else {
qCWarning(QTHELP) << "could not remove unused file" << filePath << ':' << file.error()
<< file.errorString();
}
}
}
QList<KDevelop::IDocumentationProvider*> QtHelpPlugin::providers()
{
QList<KDevelop::IDocumentationProvider*> list;
list.reserve(m_qtHelpProviders.size() + (m_loadSystemQtDoc?1:0));
for (QtHelpProvider* provider : std::as_const(m_qtHelpProviders)) {
list.append(provider);
}
if(m_loadSystemQtDoc){
list.append(m_qtDoc);
}
return list;
}
QList<QtHelpProvider*> QtHelpPlugin::qtHelpProviderLoaded()
{
return m_qtHelpProviders;
}
bool QtHelpPlugin::isQtHelpQtDocLoaded() const
{
return m_loadSystemQtDoc && m_qtDoc->isInitialized();
}
bool QtHelpPlugin::isQtHelpAvailable() const
{
return m_qtDoc->isQtHelpAvailable();
}
bool QtHelpPlugin::isInitialized() const
{
return m_qtDoc->isInitialized();
}
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"
#include "moc_qthelpplugin.cpp"
|