File indexing completed on 2024-04-28 04:36:30

0001 /*
0002     SPDX-FileCopyrightText: 2007 Alexander Dymo <adymo@kdevelop.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ipartcontroller.h"
0008 
0009 #include <KParts/Part>
0010 #include <KParts/PartLoader>
0011 #include <KParts/ReadOnlyPart>
0012 #include <KPluginFactory>
0013 
0014 #include <algorithm>
0015 #include <array>
0016 
0017 namespace KDevelop {
0018 
0019 namespace {
0020 KPluginFactory* findPartFactory(const QString& mimetype, const QString& preferredName)
0021 {
0022     const auto parts = KParts::PartLoader::partsForMimeType(mimetype);
0023     if (parts.isEmpty()) {
0024         return nullptr;
0025     }
0026 
0027     auto it = parts.end();
0028 
0029     // if there is a preferred plugin we'll take it
0030     if (!preferredName.isEmpty()) {
0031         it = std::find_if(parts.begin(), parts.end(), [&preferredName](const KPluginMetaData& part) {
0032             return part.pluginId() == preferredName;
0033         });
0034     }
0035 
0036     // otherwise use the first available part by default
0037     if (it == parts.end()) {
0038         it = parts.begin();
0039     }
0040 
0041     return KPluginFactory::loadFactory(*it).plugin;
0042 }
0043 }
0044 
0045 IPartController::IPartController(QWidget* toplevel)
0046     : KParts::PartManager(toplevel, nullptr)
0047 {
0048 }
0049 
0050 KParts::Part* IPartController::createPart ( const QString& mimetype, const QString& prefName )
0051 {
0052     auto* editorFactory = findPartFactory(mimetype, prefName);
0053     if (!editorFactory) {
0054         return nullptr;
0055     }
0056 
0057     auto ret = editorFactory->create<KParts::ReadOnlyPart>(nullptr, this);
0058     if (!ret) {
0059         qWarning() << "failed to create ReadOnlyPart" << editorFactory->metaData().name() << mimetype << prefName;
0060     }
0061 
0062     return ret;
0063 }
0064 }
0065 
0066 #include "moc_ipartcontroller.cpp"