Code cleanup, only allow one registered callback in PluginManager and PluginPage
This commit is contained in:
parent
2643ed0d8f
commit
759aa341c2
5 changed files with 77 additions and 23 deletions
|
|
@ -3,19 +3,26 @@
|
||||||
|
|
||||||
PluginManager::PluginManager(bool dark_theme_val)
|
PluginManager::PluginManager(bool dark_theme_val)
|
||||||
{
|
{
|
||||||
dark_theme = dark_theme_val;
|
/*---------------------------------------------------------*\
|
||||||
|
| Initialize plugin manager class variables |
|
||||||
|
\*---------------------------------------------------------*/
|
||||||
|
dark_theme = dark_theme_val;
|
||||||
|
AddPluginTabCallbackVal = nullptr;
|
||||||
|
AddPluginTabCallbackArg = nullptr;
|
||||||
|
RemovePluginTabCallbackVal = nullptr;
|
||||||
|
RemovePluginTabCallbackArg = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PluginManager::RegisterAddPluginTabCallback(AddPluginTabCallback new_callback, void * new_callback_arg)
|
void PluginManager::RegisterAddPluginTabCallback(AddPluginTabCallback new_callback, void * new_callback_arg)
|
||||||
{
|
{
|
||||||
AddPluginTabCallbacks.push_back(new_callback);
|
AddPluginTabCallbackVal = new_callback;
|
||||||
AddPluginTabCallbackArgs.push_back(new_callback_arg);
|
AddPluginTabCallbackArg = new_callback_arg;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PluginManager::RegisterRemovePluginTabCallback(RemovePluginTabCallback new_callback, void * new_callback_arg)
|
void PluginManager::RegisterRemovePluginTabCallback(RemovePluginTabCallback new_callback, void * new_callback_arg)
|
||||||
{
|
{
|
||||||
RemovePluginTabCallbacks.push_back(new_callback);
|
RemovePluginTabCallbackVal = new_callback;
|
||||||
RemovePluginTabCallbackArgs.push_back(new_callback_arg);
|
RemovePluginTabCallbackArg = new_callback_arg;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PluginManager::ScanAndLoadPlugins()
|
void PluginManager::ScanAndLoadPlugins()
|
||||||
|
|
@ -57,6 +64,9 @@ void PluginManager::AddPlugin(std::string path)
|
||||||
|
|
||||||
unsigned int plugin_idx;
|
unsigned int plugin_idx;
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------*\
|
||||||
|
| Search active plugins to see if this path already exists |
|
||||||
|
\*---------------------------------------------------------------------*/
|
||||||
for(plugin_idx = 0; plugin_idx < ActivePlugins.size(); plugin_idx++)
|
for(plugin_idx = 0; plugin_idx < ActivePlugins.size(); plugin_idx++)
|
||||||
{
|
{
|
||||||
if(path == ActivePlugins[plugin_idx].path)
|
if(path == ActivePlugins[plugin_idx].path)
|
||||||
|
|
@ -183,6 +193,9 @@ void PluginManager::RemovePlugin(std::string path)
|
||||||
{
|
{
|
||||||
unsigned int plugin_idx;
|
unsigned int plugin_idx;
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------*\
|
||||||
|
| Search active plugins to see if this path already exists |
|
||||||
|
\*---------------------------------------------------------------------*/
|
||||||
for(plugin_idx = 0; plugin_idx < ActivePlugins.size(); plugin_idx++)
|
for(plugin_idx = 0; plugin_idx < ActivePlugins.size(); plugin_idx++)
|
||||||
{
|
{
|
||||||
if(path == ActivePlugins[plugin_idx].path)
|
if(path == ActivePlugins[plugin_idx].path)
|
||||||
|
|
@ -191,16 +204,25 @@ void PluginManager::RemovePlugin(std::string path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------*\
|
||||||
|
| If the plugin path does not exist in the active plugins list, return |
|
||||||
|
\*---------------------------------------------------------------------*/
|
||||||
if(plugin_idx == ActivePlugins.size())
|
if(plugin_idx == ActivePlugins.size())
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------*\
|
||||||
|
| If the selected plugin is in the list and loaded, unload it |
|
||||||
|
\*---------------------------------------------------------------------*/
|
||||||
if(ActivePlugins[plugin_idx].loaded)
|
if(ActivePlugins[plugin_idx].loaded)
|
||||||
{
|
{
|
||||||
UnloadPlugin(path);
|
UnloadPlugin(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------*\
|
||||||
|
| Remove the plugin from the active plugins list |
|
||||||
|
\*---------------------------------------------------------------------*/
|
||||||
ActivePlugins.erase(ActivePlugins.begin() + plugin_idx);
|
ActivePlugins.erase(ActivePlugins.begin() + plugin_idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -208,6 +230,9 @@ void PluginManager::LoadPlugin(std::string path)
|
||||||
{
|
{
|
||||||
unsigned int plugin_idx;
|
unsigned int plugin_idx;
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------*\
|
||||||
|
| Search active plugins to see if this path already exists |
|
||||||
|
\*---------------------------------------------------------------------*/
|
||||||
for(plugin_idx = 0; plugin_idx < ActivePlugins.size(); plugin_idx++)
|
for(plugin_idx = 0; plugin_idx < ActivePlugins.size(); plugin_idx++)
|
||||||
{
|
{
|
||||||
if(path == ActivePlugins[plugin_idx].path)
|
if(path == ActivePlugins[plugin_idx].path)
|
||||||
|
|
@ -216,11 +241,17 @@ void PluginManager::LoadPlugin(std::string path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------*\
|
||||||
|
| If the plugin path does not exist in the active plugins list, return |
|
||||||
|
\*---------------------------------------------------------------------*/
|
||||||
if(plugin_idx == ActivePlugins.size())
|
if(plugin_idx == ActivePlugins.size())
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------*\
|
||||||
|
| If the selected plugin is in the list but not loaded, load it |
|
||||||
|
\*---------------------------------------------------------------------*/
|
||||||
if(!ActivePlugins[plugin_idx].loaded)
|
if(!ActivePlugins[plugin_idx].loaded)
|
||||||
{
|
{
|
||||||
ActivePlugins[plugin_idx].loader->load();
|
ActivePlugins[plugin_idx].loader->load();
|
||||||
|
|
@ -241,11 +272,11 @@ void PluginManager::LoadPlugin(std::string path)
|
||||||
plugin->Initialize(dark_theme, ResourceManager::get());
|
plugin->Initialize(dark_theme, ResourceManager::get());
|
||||||
|
|
||||||
/*-------------------------------------------------*\
|
/*-------------------------------------------------*\
|
||||||
| Call the callbacks |
|
| Call the Add Plugin Tab callback |
|
||||||
\*-------------------------------------------------*/
|
\*-------------------------------------------------*/
|
||||||
for(unsigned int callback_idx = 0; callback_idx < AddPluginTabCallbacks.size(); callback_idx++)
|
if(AddPluginTabCallbackArg != nullptr)
|
||||||
{
|
{
|
||||||
AddPluginTabCallbacks[callback_idx](AddPluginTabCallbackArgs[callback_idx], &ActivePlugins[plugin_idx]);
|
AddPluginTabCallbackVal(AddPluginTabCallbackArg, &ActivePlugins[plugin_idx]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -257,6 +288,9 @@ void PluginManager::UnloadPlugin(std::string path)
|
||||||
{
|
{
|
||||||
unsigned int plugin_idx;
|
unsigned int plugin_idx;
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------*\
|
||||||
|
| Search active plugins to see if this path already exists |
|
||||||
|
\*---------------------------------------------------------------------*/
|
||||||
for(plugin_idx = 0; plugin_idx < ActivePlugins.size(); plugin_idx++)
|
for(plugin_idx = 0; plugin_idx < ActivePlugins.size(); plugin_idx++)
|
||||||
{
|
{
|
||||||
if(path == ActivePlugins[plugin_idx].path)
|
if(path == ActivePlugins[plugin_idx].path)
|
||||||
|
|
@ -265,11 +299,17 @@ void PluginManager::UnloadPlugin(std::string path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------*\
|
||||||
|
| If the plugin path does not exist in the active plugins list, return |
|
||||||
|
\*---------------------------------------------------------------------*/
|
||||||
if(plugin_idx == ActivePlugins.size())
|
if(plugin_idx == ActivePlugins.size())
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------*\
|
||||||
|
| If the selected plugin is in the list and loaded, unload it |
|
||||||
|
\*---------------------------------------------------------------------*/
|
||||||
if(ActivePlugins[plugin_idx].loaded)
|
if(ActivePlugins[plugin_idx].loaded)
|
||||||
{
|
{
|
||||||
/*-------------------------------------------------*\
|
/*-------------------------------------------------*\
|
||||||
|
|
@ -278,11 +318,11 @@ void PluginManager::UnloadPlugin(std::string path)
|
||||||
ActivePlugins[plugin_idx].plugin->Unload();
|
ActivePlugins[plugin_idx].plugin->Unload();
|
||||||
|
|
||||||
/*-------------------------------------------------*\
|
/*-------------------------------------------------*\
|
||||||
| Call the callbacks |
|
| Call the Remove Plugin Tab callback |
|
||||||
\*-------------------------------------------------*/
|
\*-------------------------------------------------*/
|
||||||
for(unsigned int callback_idx = 0; callback_idx < RemovePluginTabCallbacks.size(); callback_idx++)
|
if(RemovePluginTabCallbackVal != nullptr)
|
||||||
{
|
{
|
||||||
RemovePluginTabCallbacks[callback_idx](RemovePluginTabCallbackArgs[callback_idx], &ActivePlugins[plugin_idx]);
|
RemovePluginTabCallbackVal(RemovePluginTabCallbackArg, &ActivePlugins[plugin_idx]);
|
||||||
}
|
}
|
||||||
|
|
||||||
ActivePlugins[plugin_idx].loader->unload();
|
ActivePlugins[plugin_idx].loader->unload();
|
||||||
|
|
|
||||||
|
|
@ -46,9 +46,9 @@ public:
|
||||||
private:
|
private:
|
||||||
bool dark_theme;
|
bool dark_theme;
|
||||||
|
|
||||||
std::vector<AddPluginTabCallback> AddPluginTabCallbacks;
|
AddPluginTabCallback AddPluginTabCallbackVal;
|
||||||
std::vector<void *> AddPluginTabCallbackArgs;
|
void * AddPluginTabCallbackArg;
|
||||||
|
|
||||||
std::vector<RemovePluginTabCallback> RemovePluginTabCallbacks;
|
RemovePluginTabCallback RemovePluginTabCallbackVal;
|
||||||
std::vector<void *> RemovePluginTabCallbackArgs;
|
void * RemovePluginTabCallbackArg;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,9 @@ OpenRGBPluginsEntry::OpenRGBPluginsEntry(QWidget *parent) :
|
||||||
ui(new Ui::OpenRGBPluginsEntryUi)
|
ui(new Ui::OpenRGBPluginsEntryUi)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
|
EnableClickCallbackVal = nullptr;
|
||||||
|
EnableClickCallbackArg = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
OpenRGBPluginsEntry::~OpenRGBPluginsEntry()
|
OpenRGBPluginsEntry::~OpenRGBPluginsEntry()
|
||||||
|
|
@ -17,8 +20,8 @@ OpenRGBPluginsEntry::~OpenRGBPluginsEntry()
|
||||||
|
|
||||||
void OpenRGBPluginsEntry::RegisterEnableClickCallback(EnableClickCallback new_callback, void * new_callback_arg)
|
void OpenRGBPluginsEntry::RegisterEnableClickCallback(EnableClickCallback new_callback, void * new_callback_arg)
|
||||||
{
|
{
|
||||||
EnableClickCallbacks.push_back(new_callback);
|
EnableClickCallbackVal = new_callback;
|
||||||
EnableClickCallbackArgs.push_back(new_callback_arg);
|
EnableClickCallbackArg = new_callback_arg;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ui::OpenRGBPluginsEntry::on_EnabledCheckBox_stateChanged(int /*checked*/)
|
void Ui::OpenRGBPluginsEntry::on_EnabledCheckBox_stateChanged(int /*checked*/)
|
||||||
|
|
@ -26,9 +29,9 @@ void Ui::OpenRGBPluginsEntry::on_EnabledCheckBox_stateChanged(int /*checked*/)
|
||||||
/*-------------------------------------------------*\
|
/*-------------------------------------------------*\
|
||||||
| Call the callbacks |
|
| Call the callbacks |
|
||||||
\*-------------------------------------------------*/
|
\*-------------------------------------------------*/
|
||||||
for(unsigned int callback_idx = 0; callback_idx < EnableClickCallbacks.size(); callback_idx++)
|
if(EnableClickCallbackVal != nullptr)
|
||||||
{
|
{
|
||||||
EnableClickCallbacks[callback_idx](EnableClickCallbackArgs[callback_idx], this);
|
EnableClickCallbackVal(EnableClickCallbackArg, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,8 +26,8 @@ private slots:
|
||||||
void on_EnabledCheckBox_stateChanged(int checked);
|
void on_EnabledCheckBox_stateChanged(int checked);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<EnableClickCallback> EnableClickCallbacks;
|
EnableClickCallback EnableClickCallbackVal;
|
||||||
std::vector<void *> EnableClickCallbackArgs;
|
void * EnableClickCallbackArg;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // OPENRGBPLUGINSENTRY_H
|
#endif // OPENRGBPLUGINSENTRY_H
|
||||||
|
|
|
||||||
|
|
@ -83,6 +83,9 @@ void Ui::OpenRGBPluginsPage::RefreshList()
|
||||||
|
|
||||||
void Ui::OpenRGBPluginsPage::on_InstallPluginButton_clicked()
|
void Ui::OpenRGBPluginsPage::on_InstallPluginButton_clicked()
|
||||||
{
|
{
|
||||||
|
/*-----------------------------------------------------*\
|
||||||
|
| Open a file selection prompt to choose the plugin file|
|
||||||
|
\*-----------------------------------------------------*/
|
||||||
QString install_file = QFileDialog::getOpenFileName(this, "Install OpenRGB Plugin", "", "DLL Files (*.dll; *.dylib; *.so; *.so.*)");
|
QString install_file = QFileDialog::getOpenFileName(this, "Install OpenRGB Plugin", "", "DLL Files (*.dll; *.dylib; *.so; *.so.*)");
|
||||||
|
|
||||||
std::string from_path = install_file.toStdString();
|
std::string from_path = install_file.toStdString();
|
||||||
|
|
@ -90,6 +93,9 @@ void Ui::OpenRGBPluginsPage::on_InstallPluginButton_clicked()
|
||||||
std::string to_file = to_path + filesystem::path(from_path).filename().string();
|
std::string to_file = to_path + filesystem::path(from_path).filename().string();
|
||||||
bool match = false;
|
bool match = false;
|
||||||
|
|
||||||
|
/*-----------------------------------------------------*\
|
||||||
|
| Check if a plugin with this path already exists |
|
||||||
|
\*-----------------------------------------------------*/
|
||||||
for(unsigned int plugin_idx = 0; plugin_idx < plugin_manager->ActivePlugins.size(); plugin_idx++)
|
for(unsigned int plugin_idx = 0; plugin_idx < plugin_manager->ActivePlugins.size(); plugin_idx++)
|
||||||
{
|
{
|
||||||
if(to_file == plugin_manager->ActivePlugins[plugin_idx].path)
|
if(to_file == plugin_manager->ActivePlugins[plugin_idx].path)
|
||||||
|
|
@ -99,8 +105,9 @@ void Ui::OpenRGBPluginsPage::on_InstallPluginButton_clicked()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*-----------------------------------------------------*\
|
||||||
|
| If this plugin already exists, prompt to replace it |
|
||||||
|
\*-----------------------------------------------------*/
|
||||||
if(match == true)
|
if(match == true)
|
||||||
{
|
{
|
||||||
QMessageBox::StandardButton reply;
|
QMessageBox::StandardButton reply;
|
||||||
|
|
@ -113,6 +120,10 @@ void Ui::OpenRGBPluginsPage::on_InstallPluginButton_clicked()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*-----------------------------------------------------*\
|
||||||
|
| When replacing, remove the existing plugin before |
|
||||||
|
| copying the file and adding the new one |
|
||||||
|
\*-----------------------------------------------------*/
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
plugin_manager->RemovePlugin(to_file);
|
plugin_manager->RemovePlugin(to_file);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue