Add Plugins tab to settings for installing, enabling, disabling, and removing plugins
* Rework Plugin Manager so that plugins can be loaded after initialization * Use a callback function to add plugin tabs to the dialog * Install button lets you choose plugin file, copies it to plugins directory, and immediately loads it * Remove button deletes selected plugin file from plugins directory - need to add a means to unload it first
This commit is contained in:
parent
a28badafb9
commit
a20405a6ef
11 changed files with 557 additions and 70 deletions
|
|
@ -1,57 +1,134 @@
|
|||
#include "LogManager.h"
|
||||
#include "PluginManager.h"
|
||||
|
||||
void PluginManager::ScanAndLoadPlugins(bool dark_theme)
|
||||
PluginManager::PluginManager(bool dark_theme_val)
|
||||
{
|
||||
dark_theme = dark_theme_val;
|
||||
}
|
||||
|
||||
void PluginManager::RegisterAddPluginTabCallback(AddPluginTabCallback new_callback, void * new_callback_arg)
|
||||
{
|
||||
AddPluginTabCallbacks.push_back(new_callback);
|
||||
AddPluginTabCallbackArgs.push_back(new_callback_arg);
|
||||
}
|
||||
|
||||
void PluginManager::ScanAndLoadPlugins()
|
||||
{
|
||||
LOG_INFO("Loading plugins");
|
||||
|
||||
std::string OpenRGBConfigDir = ResourceManager::get()->GetConfigurationDirectory();
|
||||
|
||||
std::string PluginPath = OpenRGBConfigDir + "/Plugins";
|
||||
|
||||
/*--------------------------------------------------------------------------------------*\
|
||||
| I used https://github.com/krf/cmake-qtqml-plugin-example to figure out how to do this |
|
||||
| So BIG credit to krf |
|
||||
\*--------------------------------------------------------------------------------------*/
|
||||
OpenRGBPluginInterface *OpenRGBPlugin = nullptr;
|
||||
|
||||
const QDir pluginsDir = QString().fromStdString(ResourceManager::get()->GetConfigurationDirectory()) + "plugins/";
|
||||
const QDir plugins_dir = QString().fromStdString(ResourceManager::get()->GetConfigurationDirectory()) + "plugins/";
|
||||
|
||||
std::vector<std::string> FileList;
|
||||
|
||||
for(int i = 0; i < QDir(pluginsDir).entryList(QDir::Files).size(); i++)
|
||||
for(int i = 0; i < QDir(plugins_dir).entryList(QDir::Files).size(); i++)
|
||||
{
|
||||
/*--------------------------------------*\
|
||||
| Add all of the Plugin Files to a list |
|
||||
\*--------------------------------------*/
|
||||
FileList.push_back(QDir(pluginsDir).entryList(QDir::Files)[i].toStdString());
|
||||
FileList.push_back(QDir(plugins_dir).entryList(QDir::Files)[i].toStdString());
|
||||
}
|
||||
|
||||
for(const std::string &fileName : FileList)
|
||||
for(const std::string &plugin_name : FileList)
|
||||
{
|
||||
const std::string filePath = pluginsDir.absoluteFilePath(QString().fromStdString(fileName)).toStdString();
|
||||
const std::string plugin_path = plugins_dir.absoluteFilePath(QString().fromStdString(plugin_name)).toStdString();
|
||||
|
||||
LOG_VERBOSE("Attempting to load: %s", filePath.c_str());
|
||||
|
||||
QPluginLoader loader(pluginsDir.absoluteFilePath(QString().fromStdString(fileName)));
|
||||
|
||||
if (QObject *instance = loader.instance())
|
||||
{
|
||||
if ((OpenRGBPlugin = qobject_cast<OpenRGBPluginInterface*>(instance)))
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Initialize the plugin |
|
||||
\*-----------------------------------------------------*/
|
||||
OpenRGBPlugin->info = OpenRGBPlugin->Initialize(dark_theme, ResourceManager::get());
|
||||
|
||||
LOG_VERBOSE("Loaded plugin %s", OpenRGBPlugin->info.PluginName.c_str());
|
||||
|
||||
PluginManager::ActivePlugins.push_back(OpenRGBPlugin);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << loader.errorString().toStdString() << std::endl;
|
||||
}
|
||||
LoadPlugin(plugin_path);
|
||||
}
|
||||
}
|
||||
|
||||
void PluginManager::LoadPlugin(std::string path)
|
||||
{
|
||||
OpenRGBPluginInterface *OpenRGBPlugin = nullptr;
|
||||
|
||||
LOG_VERBOSE("Attempting to load: %s", path.c_str());
|
||||
|
||||
QPluginLoader loader(QString().fromStdString(path));
|
||||
|
||||
if (QObject *instance = loader.instance())
|
||||
{
|
||||
if ((OpenRGBPlugin = qobject_cast<OpenRGBPluginInterface*>(instance)))
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Initialize the plugin |
|
||||
\*-----------------------------------------------------*/
|
||||
OpenRGBPlugin->info = OpenRGBPlugin->Initialize(dark_theme, ResourceManager::get());
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Search the settings to see if it is enabled |
|
||||
\*-----------------------------------------------------*/
|
||||
std::string name = "";
|
||||
std::string description = "";
|
||||
bool enabled = true;
|
||||
bool found = false;
|
||||
unsigned int plugin_ct = 0;
|
||||
|
||||
/*-------------------------------------------------*\
|
||||
| Open device disable list and read in disabled |
|
||||
| device strings |
|
||||
\*-------------------------------------------------*/
|
||||
json plugin_settings = ResourceManager::get()->GetSettingsManager()->GetSettings("Plugins");
|
||||
|
||||
if(plugin_settings.contains("plugins"))
|
||||
{
|
||||
plugin_ct = plugin_settings["plugins"].size();
|
||||
|
||||
for(unsigned int plugin_idx = 0; plugin_idx < plugin_settings["plugins"].size(); plugin_idx++)
|
||||
{
|
||||
if(plugin_settings["plugins"][plugin_idx].contains("name"))
|
||||
{
|
||||
name = plugin_settings["plugins"][plugin_idx]["name"];
|
||||
}
|
||||
|
||||
if(plugin_settings["plugins"][plugin_idx].contains("description"))
|
||||
{
|
||||
description = plugin_settings["plugins"][plugin_idx]["description"];
|
||||
}
|
||||
|
||||
if(plugin_settings["plugins"][plugin_idx].contains("enabled"))
|
||||
{
|
||||
enabled = plugin_settings["plugins"][plugin_idx]["enabled"];
|
||||
}
|
||||
|
||||
if((OpenRGBPlugin->info.PluginName == name)
|
||||
&&(OpenRGBPlugin->info.PluginDescription == description))
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!found)
|
||||
{
|
||||
plugin_settings["plugins"][plugin_ct]["name"] = OpenRGBPlugin->info.PluginName;
|
||||
plugin_settings["plugins"][plugin_ct]["description"] = OpenRGBPlugin->info.PluginDescription;
|
||||
plugin_settings["plugins"][plugin_ct]["enabled"] = enabled;
|
||||
|
||||
ResourceManager::get()->GetSettingsManager()->SetSettings("Plugins", plugin_settings);
|
||||
ResourceManager::get()->GetSettingsManager()->SaveSettings();
|
||||
}
|
||||
|
||||
LOG_VERBOSE("Loaded plugin %s", OpenRGBPlugin->info.PluginName.c_str());
|
||||
|
||||
OpenRGBPluginEntry entry;
|
||||
|
||||
entry.plugin = OpenRGBPlugin;
|
||||
entry.path = path;
|
||||
entry.enabled = enabled;
|
||||
|
||||
PluginManager::ActivePlugins.push_back(entry);
|
||||
|
||||
/*-------------------------------------------------*\
|
||||
| Call the callbacks |
|
||||
\*-------------------------------------------------*/
|
||||
for(unsigned int callback_idx = 0; callback_idx < AddPluginTabCallbacks.size(); callback_idx++)
|
||||
{
|
||||
AddPluginTabCallbacks[callback_idx](AddPluginTabCallbackArgs[callback_idx], OpenRGBPlugin);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << loader.errorString().toStdString() << std::endl;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue