File indexing completed on 2024-05-19 05:42:14
0001 // ct_lvtplg_abstractlibrarydispatcher.h -*-C++-*- 0002 0003 /* 0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk> 0005 // SPDX-License-Identifier: Apache-2.0 0006 // 0007 // Licensed under the Apache License, Version 2.0 (the "License"); 0008 // you may not use this file except in compliance with the License. 0009 // You may obtain a copy of the License at 0010 // 0011 // http://www.apache.org/licenses/LICENSE-2.0 0012 // 0013 // Unless required by applicable law or agreed to in writing, software 0014 // distributed under the License is distributed on an "AS IS" BASIS, 0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0016 // See the License for the specific language governing permissions and 0017 // limitations under the License. 0018 */ 0019 0020 #ifndef DIAGRAM_SERVER_CT_LVTPLG_ABSTRACTLIBRARYDISPATCHER_H 0021 #define DIAGRAM_SERVER_CT_LVTPLG_ABSTRACTLIBRARYDISPATCHER_H 0022 0023 #include <KPluginMetaData> 0024 #include <QFile> 0025 #include <QJsonDocument> 0026 0027 #include <filesystem> 0028 #include <iostream> 0029 #include <memory> 0030 #include <string> 0031 0032 typedef void (*functionPointer)(); 0033 0034 namespace Codethink::lvtplg { 0035 0036 class AbstractLibraryDispatcher { 0037 public: 0038 struct ResolveContext { 0039 ResolveContext(functionPointer const& hook): hook(hook) 0040 { 0041 } 0042 0043 virtual ~ResolveContext() 0044 { 0045 } 0046 0047 functionPointer hook = nullptr; 0048 }; 0049 0050 virtual void unload() = 0; 0051 virtual void reload() = 0; 0052 virtual ~AbstractLibraryDispatcher() = 0; 0053 virtual std::unique_ptr<ResolveContext> resolve(std::string const& functionName) = 0; 0054 virtual std::string fileName() = 0; 0055 0056 virtual std::string pluginId() 0057 { 0058 return metadata().pluginId().toStdString(); 0059 } 0060 0061 virtual std::string metadataFilePath() 0062 { 0063 return (std::filesystem::path{this->fileName()}.parent_path() / "metadata.json").string(); 0064 } 0065 0066 virtual void setEnabled(bool enabled) 0067 { 0068 auto rawData = metadata().rawData(); 0069 auto obj = rawData["KPlugin"].toObject(); 0070 obj["EnabledByDefault"] = enabled; 0071 rawData["KPlugin"] = obj; 0072 auto file = QFile{QString::fromStdString(this->metadataFilePath())}; 0073 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { 0074 return; 0075 } 0076 file.write(QJsonDocument(rawData).toJson()); 0077 file.close(); 0078 } 0079 0080 virtual bool isEnabled() 0081 { 0082 auto rawData = metadata().rawData(); 0083 auto obj = rawData["KPlugin"].toObject(); 0084 return obj["EnabledByDefault"].toBool(); 0085 } 0086 0087 protected: 0088 KPluginMetaData metadata() 0089 { 0090 #ifdef KDE_FRAMEWORKS_IS_OLD 0091 return {}; 0092 #else 0093 return KPluginMetaData::fromJsonFile(QString::fromStdString(this->metadataFilePath())); 0094 #endif 0095 } 0096 }; 0097 0098 inline AbstractLibraryDispatcher::~AbstractLibraryDispatcher() = default; 0099 0100 } // namespace Codethink::lvtplg 0101 0102 #endif // DIAGRAM_SERVER_CT_LVTPLG_ABSTRACTLIBRARYDISPATCHER_H