provider: Add API for loading and unloading provider plugins

This is also used for our tests. In order to avoid code duplication
this is being moved to calls-provider.{c,h}.
This commit is contained in:
Evangelos Ribeiro Tzaras
2021-04-02 16:04:07 +02:00
parent c30a41ffa9
commit 9e92fb88af
3 changed files with 91 additions and 68 deletions

View File

@@ -25,9 +25,11 @@
#include "calls-provider.h"
#include "calls-origin.h"
#include "calls-message-source.h"
#include "config.h"
#include "util.h"
#include <glib/gi18n.h>
#include <libpeas/peas.h>
/**
* SECTION:calls-provider
@@ -156,3 +158,84 @@ calls_provider_get_origins (CallsProvider *self)
return CALLS_PROVIDER_GET_CLASS (self)->get_origins (self);
}
/**
* calls_provider_load_plugin:
* @name: The name of the provider plugin to load
*
* Get a #CallsProvider plugin by name
*
* Returns: (transfer full): A #CallsProvider
*/
CallsProvider *
calls_provider_load_plugin (const char *name)
{
g_autoptr (GError) error = NULL;
PeasEngine *plugins;
PeasPluginInfo *info;
PeasExtension *extension;
const gchar *dir;
// Add Calls search path and rescan
plugins = peas_engine_get_default ();
peas_engine_add_search_path (plugins, PLUGIN_LIBDIR, NULL);
g_debug ("Scanning for plugins in `%s'", PLUGIN_LIBDIR);
dir = g_getenv ("CALLS_PLUGIN_DIR");
if (dir && dir[0] != '\0') {
g_debug ("Adding %s to plugin search path", dir);
peas_engine_prepend_search_path (plugins, dir, NULL);
}
// Find the plugin
info = peas_engine_get_plugin_info (plugins, name);
if (!info)
{
g_debug ("Could not find plugin `%s'", name);
return NULL;
}
// Possibly load the plugin
if (!peas_plugin_info_is_loaded (info))
{
peas_engine_load_plugin (plugins, info);
if (!peas_plugin_info_is_available (info, &error))
{
g_debug ("Error loading plugin `%s': %s", name, error->message);
return NULL;
}
g_debug ("Loaded plugin `%s'", name);
}
// Check the plugin provides CallsProvider
if (!peas_engine_provides_extension (plugins, info, CALLS_TYPE_PROVIDER))
{
g_debug ("Plugin `%s' does not have a provider extension", name);
return NULL;
}
// Get the extension
extension = peas_engine_create_extensionv (plugins, info, CALLS_TYPE_PROVIDER, 0, NULL);
if (!extension)
{
g_debug ("Could not create provider from plugin `%s'", name);
return NULL;
}
g_debug ("Created provider from plugin `%s'", name);
return CALLS_PROVIDER (extension);
}
void
calls_provider_unload_plugin (const char *name)
{
PeasEngine *engine = peas_engine_get_default ();
PeasPluginInfo *plugin = peas_engine_get_plugin_info (engine, name);
if (plugin)
peas_engine_unload_plugin (engine, plugin);
else
g_warning ("Can't unload plugin: No plugin with name %s found", name);
}