Turn providers into plugins courtesy of libpeas

This is an initial, static implementation of plugins.  The
CallsApplication has a plugin name which can be changed with a new
--provider command line option.  This plugin name is used to
instantiate the appropriate plugin when the application is activated.
From then on, the plugin cannot change.

In future, we can expand this support to include loading multiple
plugins at once, configurable through some UI.  This will have
far-reaching implications though, and complicate things like
enumerating the provider hierarchy.  There is also no practical
benefit right now; the mm and ofono plugins can't be used at the same
time because ModemManager and oFono don't play nice together, and the
whole raison d'être of the dummy plugin is undermined if you can make
use of one of the others.  So for now, we just implement one static
plugin.
This commit is contained in:
Bob Ham
2018-11-23 14:34:41 +00:00
parent b6cd5a3fe8
commit 460c0c6c3d
39 changed files with 672 additions and 181 deletions

View File

@@ -25,23 +25,23 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <glib/gi18n.h>
#define HANDY_USE_UNSTABLE_API
#include <handy.h>
#include "config.h"
#include "calls-new-call-header-bar.h"
#include "calls-history-header-bar.h"
#include "calls-history-box.h"
#include "calls-new-call-box.h"
#include "calls-encryption-indicator.h"
#include "calls-mm-provider.h"
#include "calls-ringer.h"
#include "calls-call-window.h"
#include "calls-main-window.h"
#include "calls-application.h"
#define HANDY_USE_UNSTABLE_API
#include <handy.h>
#include <libpeas/peas.h>
#include <glib/gi18n.h>
/**
* SECTION: calls-application
* @title: CallsApplication
@@ -49,10 +49,13 @@
* @include: "calls-application.h"
*/
#define DEFAULT_PROVIDER_PLUGIN "mm"
struct _CallsApplication
{
GtkApplication parent_instance;
GString *provider_name;
CallsProvider *provider;
CallsRinger *ringer;
};
@@ -60,6 +63,217 @@ struct _CallsApplication
G_DEFINE_TYPE (CallsApplication, calls_application, GTK_TYPE_APPLICATION)
static gint
handle_local_options (GApplication *application,
GVariantDict *options)
{
gboolean ok;
g_autoptr(GError) error = NULL;
const gchar *name;
g_debug ("Registering application");
ok = g_application_register (application, NULL, &error);
if (!ok)
{
g_error ("Error registering application: %s",
error->message);
}
ok = g_variant_dict_lookup (options, "provider", "&s", &name);
if (ok)
{
g_action_group_activate_action (G_ACTION_GROUP (application),
"set-provider-name",
g_variant_new_string (name));
}
return -1; // Continue processing signal
}
static void
set_provider_name_action (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
CallsApplication *self = CALLS_APPLICATION (user_data);
const gchar *name;
name = g_variant_get_string (parameter, NULL);
g_return_if_fail (name != NULL);
if (self->provider)
{
g_warning ("Cannot set provider name to `%s'"
" because provider is already created",
name);
return;
}
g_string_assign (self->provider_name, name);
g_debug ("Provider name set to `%s'",
self->provider_name->str);
}
static const GActionEntry actions[] =
{
{ "set-provider-name", set_provider_name_action, "s" },
};
static void
startup (GApplication *application)
{
G_APPLICATION_CLASS (calls_application_parent_class)->startup (application);
g_set_prgname (APP_ID);
g_set_application_name (_("Calls"));
g_action_map_add_action_entries (G_ACTION_MAP (application),
actions,
G_N_ELEMENTS (actions),
application);
}
static void
load_provider_plugin (CallsApplication *self)
{
const gchar * const name = self->provider_name->str;
PeasEngine *plugins;
PeasPluginInfo *info;
PeasExtension *extension;
g_assert (self->provider == NULL);
// Add Calls search path and rescan
plugins = peas_engine_get_default ();
peas_engine_add_search_path (plugins, PLUGIN_LIBDIR, PLUGIN_LIBDIR);
g_debug ("Scanning for plugins in `%s'", PLUGIN_LIBDIR);
// Find the plugin
info = peas_engine_get_plugin_info (plugins, name);
if (!info)
{
g_critical ("Could not find plugin `%s'", name);
return;
}
// Possibly load the plugin
if (!peas_plugin_info_is_loaded (info))
{
g_autoptr(GError) error = NULL;
peas_engine_load_plugin (plugins, info);
if (!peas_plugin_info_is_available (info, &error))
{
if (error)
{
g_critical ("Error loading plugin `%s': %s",
name, error->message);
}
else
{
g_critical ("Could not load plugin `%s'", name);
}
return;
}
g_debug ("Loaded plugin `%s'", name);
}
// Check the plugin provides CallsProvider
if (!peas_engine_provides_extension
(plugins, info, CALLS_TYPE_PROVIDER))
{
g_critical ("Plugin `%s' does not have a provider extension",
name);
return;
}
// Get the extension
extension = peas_engine_create_extensionv
(plugins, info, CALLS_TYPE_PROVIDER, 0, NULL);
if (!extension)
{
g_critical ("Could not create provider from plugin `%s'",
name);
return;
}
g_debug ("Created provider from plugin `%s'", name);
self->provider = CALLS_PROVIDER (extension);
}
static void
activate (GApplication *application)
{
GtkApplication *gtk_app;
GtkWindow *window;
g_assert (GTK_IS_APPLICATION (application));
gtk_app = GTK_APPLICATION (application);
window = gtk_application_get_active_window (gtk_app);
if (window == NULL)
{
CallsApplication *self = CALLS_APPLICATION (application);
// Later we will make provider loading/unloaded a dynamic
// process but that will have far-reaching consequences and is
// of no use immediately so for now, we just load one provider
// at startup. We can't put this in the actual startup() method
// though, because we need to be able to set the provider name
// from the command line and we use actions to do that, which
// depend on the application already being started up.
if (!self->provider)
{
load_provider_plugin (self);
if (!self->provider)
{
g_application_quit (application);
return;
}
self->ringer = calls_ringer_new (self->provider);
g_assert (self->ringer != NULL);
}
/*
* We don't track the memory created. Ideally, we might have to.
* But we assume that the application is closed by closing the
* window. In that case, GTK+ frees the resources right.
*/
window = GTK_WINDOW (calls_main_window_new (gtk_app, self->provider));
calls_call_window_new (gtk_app, self->provider);
}
gtk_window_present (window);
}
static void
constructed (GObject *object)
{
GObjectClass *parent_class = g_type_class_peek (GTK_TYPE_APPLICATION);
CallsApplication *self = CALLS_APPLICATION (object);
GSimpleActionGroup *action_group;
action_group = g_simple_action_group_new ();
g_action_map_add_action_entries (G_ACTION_MAP (action_group),
actions, G_N_ELEMENTS (actions), self);
g_object_unref (action_group);
parent_class->constructed (object);
}
static void
dispose (GObject *object)
{
@@ -68,51 +282,20 @@ dispose (GObject *object)
g_clear_object (&self->ringer);
g_clear_object (&self->provider);
G_OBJECT_CLASS (calls_application_parent_class)->dispose (object);
}
static void
finalize (GObject *object)
{
CallsApplication *self = (CallsApplication *)object;
g_string_free (self->provider_name, TRUE);
G_OBJECT_CLASS (calls_application_parent_class)->finalize (object);
}
static void
startup (GApplication *application)
{
CallsApplication *self = (CallsApplication *)application;
g_autoptr(GError) error = NULL;
G_APPLICATION_CLASS (calls_application_parent_class)->startup (application);
g_set_prgname (APP_ID);
g_set_application_name (_("Calls"));
self->provider = CALLS_PROVIDER (calls_mm_provider_new ());
g_assert (self->provider != NULL);
self->ringer = calls_ringer_new (self->provider);
g_assert (self->ringer != NULL);
}
static void
activate (GApplication *application)
{
CallsApplication *self = (CallsApplication *)application;
GtkApplication *app = (GtkApplication *)application;
GtkWindow *window;
g_assert (GTK_IS_APPLICATION (app));
window = gtk_application_get_active_window (app);
if (window == NULL)
{
/*
* We don't track the memory created. Ideally, we might have to.
* But we assume that the application is closed by closing the
* window. In that case, GTK+ frees the resources right.
*/
window = GTK_WINDOW (calls_main_window_new (app, self->provider));
calls_call_window_new (app, self->provider);
}
gtk_window_present (window);
}
static void
calls_application_class_init (CallsApplicationClass *klass)
@@ -120,8 +303,11 @@ calls_application_class_init (CallsApplicationClass *klass)
GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->constructed = constructed;
object_class->dispose = dispose;
object_class->finalize = finalize;
application_class->handle_local_options = handle_local_options;
application_class->startup = startup;
application_class->activate = activate;
@@ -133,11 +319,28 @@ calls_application_class_init (CallsApplicationClass *klass)
g_type_ensure (HDY_TYPE_DIALER);
}
static void
calls_application_init (CallsApplication *self)
{
const GOptionEntry options[] = {
{
"provider", 'p', G_OPTION_FLAG_NONE,
G_OPTION_ARG_STRING, NULL,
_("The name of the plugin to use for the call Provider"),
_("PLUGIN")
},
{
NULL
}
};
g_application_add_main_option_entries (G_APPLICATION (self), options);
self->provider_name = g_string_new (DEFAULT_PROVIDER_PLUGIN);
}
CallsApplication *
calls_application_new (void)
{