diff options
Diffstat (limited to 'src/designer/src/lib/shared/pluginmanager.cpp')
-rw-r--r-- | src/designer/src/lib/shared/pluginmanager.cpp | 187 |
1 files changed, 76 insertions, 111 deletions
diff --git a/src/designer/src/lib/shared/pluginmanager.cpp b/src/designer/src/lib/shared/pluginmanager.cpp index 88b581862..cc520997c 100644 --- a/src/designer/src/lib/shared/pluginmanager.cpp +++ b/src/designer/src/lib/shared/pluginmanager.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the Qt Designer of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 #include "pluginmanager_p.h" #include "qdesigner_utils_p.h" @@ -50,21 +25,23 @@ #include <QtCore/qxmlstream.h> -static const char *uiElementC = "ui"; -static const char *languageAttributeC = "language"; -static const char *widgetElementC = "widget"; -static const char *displayNameAttributeC = "displayname"; -static const char *classAttributeC = "class"; -static const char *customwidgetElementC = "customwidget"; -static const char *extendsElementC = "extends"; -static const char *addPageMethodC = "addpagemethod"; -static const char *propertySpecsC = "propertyspecifications"; -static const char *stringPropertySpecC = "stringpropertyspecification"; -static const char propertyToolTipC[] = "tooltip"; -static const char *stringPropertyNameAttrC = "name"; -static const char *stringPropertyTypeAttrC = "type"; -static const char *stringPropertyNoTrAttrC = "notr"; -static const char *jambiLanguageC = "jambi"; +using namespace Qt::StringLiterals; + +static constexpr auto uiElementC = "ui"_L1; +static constexpr auto languageAttributeC = "language"_L1; +static constexpr auto widgetElementC = "widget"_L1; +static constexpr auto displayNameAttributeC = "displayname"_L1; +static constexpr auto classAttributeC = "class"_L1; +static constexpr auto customwidgetElementC = "customwidget"_L1; +static constexpr auto extendsElementC = "extends"_L1; +static constexpr auto addPageMethodC = "addpagemethod"_L1; +static constexpr auto propertySpecsC = "propertyspecifications"_L1; +static constexpr auto stringPropertySpecC = "stringpropertyspecification"_L1; +static constexpr auto propertyToolTipC = "tooltip"_L1; +static constexpr auto stringPropertyNameAttrC = "name"_L1; +static constexpr auto stringPropertyTypeAttrC = "type"_L1; +static constexpr auto stringPropertyNoTrAttrC = "notr"_L1; +static constexpr auto jambiLanguageC = "jambi"_L1; enum { debugPluginManager = 0 }; @@ -98,21 +75,10 @@ QStringList QDesignerPluginManager::defaultPluginPaths() const QStringList path_list = QCoreApplication::libraryPaths(); - const QString designer = QStringLiteral("designer"); - for (const QString &path : path_list) { - QString libPath = path; - libPath += QDir::separator(); - libPath += designer; - result.append(libPath); - } - - QString homeLibPath = QDir::homePath(); - homeLibPath += QDir::separator(); - homeLibPath += QStringLiteral(".designer"); - homeLibPath += QDir::separator(); - homeLibPath += QStringLiteral("plugins"); + for (const QString &path : path_list) + result.append(path + "/designer"_L1); - result.append(homeLibPath); + result.append(qdesigner_internal::dataDirectory() + "/plugins"_L1); return result; } @@ -122,11 +88,11 @@ QStringList QDesignerPluginManager::defaultPluginPaths() static inline QString getDesignerLanguage(QDesignerFormEditorInterface *core) { if (QDesignerLanguageExtension *lang = qt_extension<QDesignerLanguageExtension *>(core->extensionManager(), core)) { - if (lang->uiExtension() == QStringLiteral("jui")) - return QLatin1String(jambiLanguageC); - return QStringLiteral("unknown"); + if (lang->uiExtension() == "jui"_L1) + return jambiLanguageC; + return u"unknown"_s; } - return QStringLiteral("c++"); + return u"c++"_s; } // ---------------- QDesignerCustomWidgetSharedData @@ -134,9 +100,7 @@ static inline QString getDesignerLanguage(QDesignerFormEditorInterface *core) class QDesignerCustomWidgetSharedData : public QSharedData { public: // Type of a string property - using StringPropertyType = QPair<qdesigner_internal::TextPropertyValidationMode, bool>; - using StringPropertyTypeMap = QHash<QString, StringPropertyType>; - using PropertyToolTipMap = QHash<QString, QString>; + using StringPropertyType = std::pair<qdesigner_internal::TextPropertyValidationMode, bool>; explicit QDesignerCustomWidgetSharedData(const QString &thePluginPath) : pluginPath(thePluginPath) {} void clearXML(); @@ -149,8 +113,8 @@ public: QString xmlAddPageMethod; QString xmlExtends; - StringPropertyTypeMap xmlStringPropertyTypeMap; - PropertyToolTipMap propertyToolTipMap; + QHash<QString, StringPropertyType> xmlStringPropertyTypeMap; + QHash<QString, QString> propertyToolTipMap; }; void QDesignerCustomWidgetSharedData::clearXML() @@ -222,7 +186,7 @@ QString QDesignerCustomWidgetData::pluginPath() const bool QDesignerCustomWidgetData::xmlStringPropertyType(const QString &name, StringPropertyType *type) const { - QDesignerCustomWidgetSharedData::StringPropertyTypeMap::const_iterator it = m_d->xmlStringPropertyTypeMap.constFind(name); + const auto it = m_d->xmlStringPropertyTypeMap.constFind(name); if (it == m_d->xmlStringPropertyTypeMap.constEnd()) { *type = StringPropertyType(qdesigner_internal::ValidationRichText, true); return false; @@ -273,19 +237,19 @@ static inline QString msgAttributeMissing(const QString &name) static qdesigner_internal::TextPropertyValidationMode typeStringToType(const QString &v, bool *ok) { *ok = true; - if (v == QStringLiteral("multiline")) + if (v == "multiline"_L1) return qdesigner_internal::ValidationMultiLine; - if (v == QStringLiteral("richtext")) + if (v == "richtext"_L1) return qdesigner_internal::ValidationRichText; - if (v == QStringLiteral("stylesheet")) + if (v == "stylesheet"_L1) return qdesigner_internal::ValidationStyleSheet; - if (v == QStringLiteral("singleline")) + if (v == "singleline"_L1) return qdesigner_internal::ValidationSingleLine; - if (v == QStringLiteral("objectname")) + if (v == "objectname"_L1) return qdesigner_internal::ValidationObjectName; - if (v == QStringLiteral("objectnamescope")) + if (v == "objectnamescope"_L1) return qdesigner_internal::ValidationObjectNameScope; - if (v == QStringLiteral("url")) + if (v == "url"_L1) return qdesigner_internal::ValidationURL; *ok = false; return qdesigner_internal::ValidationRichText; @@ -295,12 +259,12 @@ static bool parsePropertySpecs(QXmlStreamReader &sr, QDesignerCustomWidgetSharedData *data, QString *errorMessage) { - const QString propertySpecs = QLatin1String(propertySpecsC); - const QString stringPropertySpec = QLatin1String(stringPropertySpecC); - const QString propertyToolTip = QLatin1String(propertyToolTipC); - const QString stringPropertyTypeAttr = QLatin1String(stringPropertyTypeAttrC); - const QString stringPropertyNoTrAttr = QLatin1String(stringPropertyNoTrAttrC); - const QString stringPropertyNameAttr = QLatin1String(stringPropertyNameAttrC); + const QString propertySpecs = propertySpecsC; + const QString stringPropertySpec = stringPropertySpecC; + const QString propertyToolTip = propertyToolTipC; + const QString stringPropertyTypeAttr = stringPropertyTypeAttrC; + const QString stringPropertyNoTrAttr = stringPropertyNoTrAttrC; + const QString stringPropertyNameAttr = stringPropertyNameAttrC; while (!sr.atEnd()) { switch(sr.readNext()) { @@ -320,7 +284,7 @@ static bool parsePropertySpecs(QXmlStreamReader &sr, return false; } bool typeOk; - const bool noTr = notrS == QStringLiteral("true") || notrS == QStringLiteral("1"); + const bool noTr = notrS == "true"_L1 || notrS == "1"_L1; QDesignerCustomWidgetSharedData::StringPropertyType v(typeStringToType(type, &typeOk), !noTr); if (!typeOk) { *errorMessage = QDesignerPluginManager::tr("'%1' is not a valid string property specification.").arg(type); @@ -343,6 +307,7 @@ static bool parsePropertySpecs(QXmlStreamReader &sr, case QXmlStreamReader::EndElement: // Outer </stringproperties> if (sr.name() == propertySpecs) return true; + break; default: break; } @@ -366,8 +331,8 @@ QDesignerCustomWidgetData::ParseResult ParseResult rc = ParseOk; // Parse for the (optional) <ui> or the first <widget> element QStringList elements; - elements.push_back(QLatin1String(uiElementC)); - elements.push_back(QLatin1String(widgetElementC)); + elements.push_back(uiElementC); + elements.push_back(widgetElementC); for (int i = 0; i < 2 && !foundWidget; i++) { switch (findElement(elements, sr)) { case FindError: @@ -378,13 +343,13 @@ QDesignerCustomWidgetData::ParseResult return ParseError; case 0: { // <ui> const QXmlStreamAttributes attributes = sr.attributes(); - data.xmlLanguage = attributes.value(QLatin1String(languageAttributeC)).toString(); - data.xmlDisplayName = attributes.value(QLatin1String(displayNameAttributeC)).toString(); + data.xmlLanguage = attributes.value(languageAttributeC).toString(); + data.xmlDisplayName = attributes.value(displayNameAttributeC).toString(); foundUI = true; } break; case 1: // <widget>: Do some sanity checks - data.xmlClassName = sr.attributes().value(QLatin1String(classAttributeC)).toString(); + data.xmlClassName = sr.attributes().value(classAttributeC).toString(); if (data.xmlClassName.isEmpty()) { *errorMessage = QDesignerPluginManager::tr("The class attribute for the class %1 is missing.").arg(name); rc = ParseWarning; @@ -402,7 +367,7 @@ QDesignerCustomWidgetData::ParseResult if (!foundUI) return rc; elements.clear(); - elements.push_back(QLatin1String(customwidgetElementC)); + elements.push_back(customwidgetElementC); switch (findElement(elements, sr)) { case FindError: *errorMessage = msgXmlError(name, sr.errorString()); @@ -413,10 +378,7 @@ QDesignerCustomWidgetData::ParseResult break; } // Find <extends>, <addPageMethod>, <stringproperties> - elements.clear(); - elements.push_back(QLatin1String(extendsElementC)); - elements.push_back(QLatin1String(addPageMethodC)); - elements.push_back(QLatin1String(propertySpecsC)); + elements = {extendsElementC, addPageMethodC, propertySpecsC}; while (true) { switch (findElement(elements, sr)) { case FindError: @@ -453,7 +415,7 @@ QDesignerCustomWidgetData::ParseResult class QDesignerPluginManagerPrivate { public: - using ClassNamePropertyNameKey = QPair<QString, QString>; + using ClassNamePropertyNameKey = std::pair<QString, QString>; QDesignerPluginManagerPrivate(QDesignerFormEditorInterface *core); @@ -461,7 +423,7 @@ class QDesignerPluginManagerPrivate { bool addCustomWidget(QDesignerCustomWidgetInterface *c, const QString &pluginPath, const QString &designerLanguage); - void addCustomWidgets(const QObject *o, + void addCustomWidgets(QObject *o, const QString &pluginPath, const QString &designerLanguage); @@ -471,8 +433,7 @@ class QDesignerPluginManagerPrivate { // TODO: QPluginLoader also caches invalid plugins -> This seems to be dead code QStringList m_disabledPlugins; - typedef QMap<QString, QString> FailedPluginMap; - FailedPluginMap m_failedPlugins; + QMap<QString, QString> m_failedPlugins; // Synced lists of custom widgets and their data. Note that the list // must be ordered for collections to appear in order. @@ -535,7 +496,7 @@ bool QDesignerPluginManagerPrivate::addCustomWidget(QDesignerCustomWidgetInterfa // Check the plugin interface for either a custom widget or a collection and // add all contained custom widgets. -void QDesignerPluginManagerPrivate::addCustomWidgets(const QObject *o, +void QDesignerPluginManagerPrivate::addCustomWidgets(QObject *o, const QString &pluginPath, const QString &designerLanguage) { @@ -543,7 +504,7 @@ void QDesignerPluginManagerPrivate::addCustomWidgets(const QObject *o, addCustomWidget(c, pluginPath, designerLanguage); return; } - if (const QDesignerCustomWidgetCollectionInterface *coll = qobject_cast<QDesignerCustomWidgetCollectionInterface*>(o)) { + if (QDesignerCustomWidgetCollectionInterface *coll = qobject_cast<QDesignerCustomWidgetCollectionInterface*>(o)) { const auto &collCustomWidgets = coll->customWidgets(); for (QDesignerCustomWidgetInterface *c : collCustomWidgets) addCustomWidget(c, pluginPath, designerLanguage); @@ -555,12 +516,18 @@ void QDesignerPluginManagerPrivate::addCustomWidgets(const QObject *o, // As of 4.4, the header will be distributed with the Eclipse plugin. QDesignerPluginManager::QDesignerPluginManager(QDesignerFormEditorInterface *core) : + QDesignerPluginManager(QStringList{}, core) +{ +} + +QDesignerPluginManager::QDesignerPluginManager(const QStringList &pluginPaths, + QDesignerFormEditorInterface *core) : QObject(core), m_d(new QDesignerPluginManagerPrivate(core)) { - m_d->m_pluginPaths = defaultPluginPaths(); + m_d->m_pluginPaths = pluginPaths.isEmpty() ? defaultPluginPaths() : pluginPaths; const QSettings settings(qApp->organizationName(), QDesignerQSettings::settingsApplicationName()); - m_d->m_disabledPlugins = unique(settings.value(QStringLiteral("PluginManager/DisabledPlugins")).toStringList()); + m_d->m_disabledPlugins = unique(settings.value("PluginManager/DisabledPlugins").toStringList()); // Register plugins updateRegisteredPlugins(); @@ -595,15 +562,14 @@ QStringList QDesignerPluginManager::findPlugins(const QString &path) // Load symbolic links but make sure all file names are unique as not // to fall for something like 'libplugin.so.1 -> libplugin.so' QStringList result; - const QFileInfoList::const_iterator icend = infoList.constEnd(); - for (QFileInfoList::const_iterator it = infoList.constBegin(); it != icend; ++it) { + for (const auto &fi : infoList) { QString fileName; - if (it->isSymLink()) { - const QFileInfo linkTarget = QFileInfo(it->symLinkTarget()); + if (fi.isSymLink()) { + const QFileInfo linkTarget = QFileInfo(fi.symLinkTarget()); if (linkTarget.exists() && linkTarget.isFile()) fileName = linkTarget.absoluteFilePath(); } else { - fileName = it->absoluteFilePath(); + fileName = fi.absoluteFilePath(); } if (!fileName.isEmpty() && QLibrary::isLibrary(fileName) && !result.contains(fileName)) result += fileName; @@ -662,7 +628,7 @@ void QDesignerPluginManager::updateRegisteredPlugins() if (debugPluginManager) qDebug() << Q_FUNC_INFO; m_d->m_registeredPlugins.clear(); - for (const QString &path : qAsConst(m_d->m_pluginPaths)) + for (const QString &path : std::as_const(m_d->m_pluginPaths)) registerPath(path); } @@ -672,7 +638,7 @@ bool QDesignerPluginManager::registerNewPlugins() qDebug() << Q_FUNC_INFO; const int before = m_d->m_registeredPlugins.size(); - for (const QString &path : qAsConst(m_d->m_pluginPaths)) + for (const QString &path : std::as_const(m_d->m_pluginPaths)) registerPath(path); const bool newPluginsFound = m_d->m_registeredPlugins.size() > before; // We force a re-initialize as Jambi collection might return @@ -704,7 +670,7 @@ void QDesignerPluginManager::registerPlugin(const QString &plugin) QPluginLoader loader(plugin); if (loader.isLoaded() || loader.load()) { m_d->m_registeredPlugins += plugin; - QDesignerPluginManagerPrivate::FailedPluginMap::iterator fit = m_d->m_failedPlugins.find(plugin); + const auto fit = m_d->m_failedPlugins.find(plugin); if (fit != m_d->m_failedPlugins.end()) m_d->m_failedPlugins.erase(fit); return; @@ -719,8 +685,8 @@ void QDesignerPluginManager::registerPlugin(const QString &plugin) bool QDesignerPluginManager::syncSettings() { QSettings settings(qApp->organizationName(), QDesignerQSettings::settingsApplicationName()); - settings.beginGroup(QStringLiteral("PluginManager")); - settings.setValue(QStringLiteral("DisabledPlugins"), m_d->m_disabledPlugins); + settings.beginGroup("PluginManager"); + settings.setValue("DisabledPlugins", m_d->m_disabledPlugins); settings.endGroup(); return settings.status() == QSettings::NoError; } @@ -743,7 +709,7 @@ void QDesignerPluginManager::ensureInitialized() for (QObject *o : staticPluginObjects) m_d->addCustomWidgets(o, staticPluginPath, designerLanguage); } - for (const QString &plugin : qAsConst(m_d->m_registeredPlugins)) { + for (const QString &plugin : std::as_const(m_d->m_registeredPlugins)) { if (QObject *o = instance(plugin)) m_d->addCustomWidgets(o, plugin, designerLanguage); } @@ -767,8 +733,7 @@ QDesignerCustomWidgetData QDesignerPluginManager::customWidgetData(QDesignerCust QDesignerCustomWidgetData QDesignerPluginManager::customWidgetData(const QString &name) const { - const int count = m_d->m_customWidgets.size(); - for (int i = 0; i < count; i++) + for (qsizetype i = 0, count = m_d->m_customWidgets.size(); i < count; ++i) if (m_d->m_customWidgets.at(i)->name() == name) return m_d->m_customWidgetData.at(i); return QDesignerCustomWidgetData(); |