1
0
Fork 0
mirror of https://gitlab.gnome.org/GNOME/calls.git synced 2024-10-01 00:05:25 +00:00

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

@ -79,67 +79,6 @@ set_state (CallsManager *self, CallsManagerState state)
g_object_notify_by_pspec (G_OBJECT (self), props[PROP_STATE]); g_object_notify_by_pspec (G_OBJECT (self), props[PROP_STATE]);
} }
static CallsProvider *
load_provider (const gchar* 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);
}
static void static void
add_call (CallsManager *self, CallsCall *call, CallsOrigin *origin) add_call (CallsManager *self, CallsCall *call, CallsOrigin *origin)
{ {
@ -249,8 +188,6 @@ remove_origin (CallsManager *self, CallsOrigin *origin, CallsProvider *provider)
static void static void
remove_provider (CallsManager *self) remove_provider (CallsManager *self)
{ {
PeasEngine *engine = peas_engine_get_default ();
PeasPluginInfo *plugin = peas_engine_get_plugin_info (engine, self->provider_name);
GListModel *origins; GListModel *origins;
guint n_items; guint n_items;
@ -269,8 +206,9 @@ remove_provider (CallsManager *self)
remove_origin (self, origin, self->provider); remove_origin (self, origin, self->provider);
} }
calls_provider_unload_plugin (self->provider_name);
g_clear_pointer (&self->provider_name, g_free); g_clear_pointer (&self->provider_name, g_free);
peas_engine_unload_plugin (engine, plugin);
g_clear_object (&self->provider); g_clear_object (&self->provider);
set_state (self, CALLS_MANAGER_STATE_NO_PROVIDER); set_state (self, CALLS_MANAGER_STATE_NO_PROVIDER);
} }
@ -313,7 +251,7 @@ add_provider (CallsManager *self, const gchar *name)
if (name == NULL) if (name == NULL)
return; return;
self->provider = load_provider (name); self->provider = calls_provider_load_plugin (name);
if (self->provider == NULL) { if (self->provider == NULL) {
set_state (self, CALLS_MANAGER_STATE_NO_PLUGIN); set_state (self, CALLS_MANAGER_STATE_NO_PLUGIN);

View file

@ -25,9 +25,11 @@
#include "calls-provider.h" #include "calls-provider.h"
#include "calls-origin.h" #include "calls-origin.h"
#include "calls-message-source.h" #include "calls-message-source.h"
#include "config.h"
#include "util.h" #include "util.h"
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <libpeas/peas.h>
/** /**
* SECTION:calls-provider * SECTION:calls-provider
@ -156,3 +158,84 @@ calls_provider_get_origins (CallsProvider *self)
return CALLS_PROVIDER_GET_CLASS (self)->get_origins (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);
}

View file

@ -50,9 +50,11 @@ struct _CallsProviderClass
}; };
const char *calls_provider_get_name (CallsProvider *self); const char *calls_provider_get_name (CallsProvider *self);
const char *calls_provider_get_status (CallsProvider *self); const char *calls_provider_get_status (CallsProvider *self);
GListModel *calls_provider_get_origins (CallsProvider *self); GListModel *calls_provider_get_origins (CallsProvider *self);
CallsProvider *calls_provider_load_plugin (const char *name);
void calls_provider_unload_plugin (const char *name);
G_END_DECLS G_END_DECLS