Skip to content

Commit

Permalink
refactor: inherit from ValentResource
Browse files Browse the repository at this point in the history
Refactor a number of classes, namely extensions and adapters, to
inherit from `ValentResource`.
  • Loading branch information
andyholmes committed Dec 6, 2024
1 parent d0c32f7 commit 699ad7c
Show file tree
Hide file tree
Showing 55 changed files with 376 additions and 564 deletions.
4 changes: 2 additions & 2 deletions src/libvalent/contacts/valent-contact-list.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

struct _ValentContactList
{
ValentObject parent_instance;
ValentResource parent_instance;

TrackerSparqlConnection *connection;
TrackerNotifier *notifier;
Expand All @@ -54,7 +54,7 @@ static void valent_contact_list_load (ValentContactList *self);
static void valent_contact_list_load_contact (ValentContactList *self,
const char *iri);

G_DEFINE_FINAL_TYPE_WITH_CODE (ValentContactList, valent_contact_list, VALENT_TYPE_OBJECT,
G_DEFINE_FINAL_TYPE_WITH_CODE (ValentContactList, valent_contact_list, VALENT_TYPE_RESOURCE,
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, g_list_model_iface_init))


Expand Down
4 changes: 2 additions & 2 deletions src/libvalent/contacts/valent-contact-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

#pragma once

#include "../core/valent-object.h"
#include "../core/valent-resource.h"

G_BEGIN_DECLS

#define VALENT_TYPE_CONTACT_LIST (valent_contact_list_get_type())

G_DECLARE_FINAL_TYPE (ValentContactList, valent_contact_list, VALENT, CONTACT_LIST, ValentObject)
G_DECLARE_FINAL_TYPE (ValentContactList, valent_contact_list, VALENT, CONTACT_LIST, ValentResource)

G_END_DECLS
20 changes: 17 additions & 3 deletions src/libvalent/core/valent-application.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "config.h"

#include <gio/gio.h>
#include <libtracker-sparql/tracker-sparql.h>

#include "valent-application.h"
#include "valent-application-plugin.h"
Expand Down Expand Up @@ -42,12 +43,25 @@ static inline void
valent_application_enable_plugin (ValentApplication *self,
ValentPlugin *plugin)
{
g_autofree char *urn = NULL;
const char *title = NULL;
const char *description = NULL;
const char *module = NULL;

g_assert (VALENT_IS_APPLICATION (self));

title = peas_plugin_info_get_name (plugin->info);
description = peas_plugin_info_get_description (plugin->info);
module = peas_plugin_info_get_module_name (plugin->info);
urn = tracker_sparql_escape_uri_printf ("urn:valent:application:%s", module);
plugin->extension = peas_engine_create_extension (valent_get_plugin_engine (),
plugin->info,
VALENT_TYPE_APPLICATION_PLUGIN,
"object", self,
"iri", urn,
// FIXME: root source
"source", NULL,
"title", title,
"description", description,
NULL);
g_return_if_fail (G_IS_OBJECT (plugin->extension));
}
Expand Down Expand Up @@ -309,6 +323,8 @@ valent_application_constructed (GObject *object)
PeasEngine *engine = NULL;
unsigned int n_plugins = 0;

G_OBJECT_CLASS (valent_application_parent_class)->constructed (object);

self->plugins = g_hash_table_new_full (NULL, NULL, NULL, valent_plugin_free);
self->plugins_context = valent_context_new (NULL, "application", NULL);

Expand Down Expand Up @@ -336,8 +352,6 @@ valent_application_constructed (GObject *object)
G_CALLBACK (on_unload_plugin),
self,
0);

G_OBJECT_CLASS (valent_application_parent_class)->constructed (object);
}

static void
Expand Down
12 changes: 9 additions & 3 deletions src/libvalent/core/valent-component.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ typedef struct
GObject *primary_adapter;
} ValentComponentPrivate;

G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ValentComponent, valent_component, VALENT_TYPE_OBJECT);
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ValentComponent, valent_component, VALENT_TYPE_RESOURCE);

typedef enum {
PROP_PLUGIN_DOMAIN = 1,
Expand Down Expand Up @@ -184,6 +184,8 @@ valent_component_enable_plugin (ValentComponent *self,
{
ValentComponentPrivate *priv = valent_component_get_instance_private (self);
g_autofree char *urn = NULL;
const char *title = NULL;
const char *description = NULL;
const char *domain = NULL;
const char *module = NULL;

Expand All @@ -192,14 +194,18 @@ valent_component_enable_plugin (ValentComponent *self,
g_assert (VALENT_IS_COMPONENT (self));
g_assert (plugin != NULL);

title = peas_plugin_info_get_name (plugin->info);
description = peas_plugin_info_get_description (plugin->info);
domain = valent_context_get_domain (priv->context);
module = peas_plugin_info_get_module_name (plugin->info);
urn = tracker_sparql_escape_uri_printf ("urn:valent:%s:%s", domain, module);
plugin->extension = peas_engine_create_extension (priv->engine,
plugin->info,
priv->plugin_type,
"iri", urn,
"object", self,
"iri", urn,
"source", self,
"title", title,
"description", description,
NULL);
g_return_if_fail (G_IS_OBJECT (plugin->extension));

Expand Down
24 changes: 12 additions & 12 deletions src/libvalent/core/valent-component.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ G_BEGIN_DECLS
#define VALENT_TYPE_COMPONENT (valent_component_get_type())

VALENT_AVAILABLE_IN_1_0
G_DECLARE_DERIVABLE_TYPE (ValentComponent, valent_component, VALENT, COMPONENT, ValentObject)
G_DECLARE_DERIVABLE_TYPE (ValentComponent, valent_component, VALENT, COMPONENT, ValentResource)

struct _ValentComponentClass
{
ValentObjectClass parent_class;
ValentResourceClass parent_class;

/* virtual functions */
void (*bind_extension) (ValentComponent *component,
GObject *extension);
void (*unbind_extension) (ValentComponent *component,
GObject *extension);
void (*bind_preferred) (ValentComponent *component,
GObject *extension);
void (*export_adapter) (ValentComponent *component,
ValentExtension *extension);
void (*unexport_adapter) (ValentComponent *component,
ValentExtension *extension);
void (*bind_extension) (ValentComponent *component,
GObject *extension);
void (*unbind_extension) (ValentComponent *component,
GObject *extension);
void (*bind_preferred) (ValentComponent *component,
GObject *extension);
void (*export_adapter) (ValentComponent *component,
ValentExtension *extension);
void (*unexport_adapter) (ValentComponent *component,
ValentExtension *extension);

/*< private >*/
gpointer padding[8];
Expand Down
72 changes: 4 additions & 68 deletions src/libvalent/core/valent-extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

#include "valent-context.h"
#include "valent-core-enums.h"
#include "valent-extension.h"
#include "valent-object.h"
#include "valent-resource.h"

#include "valent-extension.h"


/**
Expand Down Expand Up @@ -60,8 +62,6 @@

typedef struct
{
GObject *object;

PeasPluginInfo *plugin_info;
ValentPluginState plugin_state;
GError *plugin_error;
Expand All @@ -74,15 +74,14 @@ typedef struct
static void g_action_group_iface_init (GActionGroupInterface *iface);
static void g_action_map_iface_init (GActionMapInterface *iface);

G_DEFINE_ABSTRACT_TYPE_WITH_CODE (ValentExtension, valent_extension, VALENT_TYPE_OBJECT,
G_DEFINE_ABSTRACT_TYPE_WITH_CODE (ValentExtension, valent_extension, VALENT_TYPE_RESOURCE,
G_ADD_PRIVATE (ValentExtension)
G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, g_action_group_iface_init)
G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP, g_action_map_iface_init))

enum {
PROP_0,
PROP_CONTEXT,
PROP_OBJECT,
PROP_PLUGIN_INFO,
PROP_PLUGIN_STATE,
PROP_SETTINGS,
Expand Down Expand Up @@ -289,25 +288,6 @@ g_action_map_iface_init (GActionMapInterface *iface)
iface->remove_action = valent_extension_remove_action;
}

/*
* ValentExtension
*/
static void
valent_extension_set_object (ValentExtension *self,
gpointer object)
{
ValentExtensionPrivate *priv = valent_extension_get_instance_private (self);

g_assert (VALENT_IS_EXTENSION (self));
g_assert (object == NULL || G_IS_OBJECT (object));

if (priv->object == object)
return;

priv->object = object;
g_object_add_weak_pointer (G_OBJECT (priv->object), (gpointer *)&priv->object);
}

/*
* ValentObject
*/
Expand Down Expand Up @@ -343,7 +323,6 @@ valent_extension_finalize (GObject *object)
ValentExtension *self = VALENT_EXTENSION (object);
ValentExtensionPrivate *priv = valent_extension_get_instance_private (self);

g_clear_weak_pointer (&priv->object);
g_clear_error (&priv->plugin_error);
g_clear_pointer (&priv->actions, g_hash_table_unref);
g_clear_object (&priv->context);
Expand All @@ -368,10 +347,6 @@ valent_extension_get_property (GObject *object,
g_value_set_object (value, valent_extension_get_context (self));
break;

case PROP_OBJECT:
g_value_set_object (value, priv->object);
break;

case PROP_PLUGIN_INFO:
g_value_set_object (value, priv->plugin_info);
break;
Expand Down Expand Up @@ -404,10 +379,6 @@ valent_extension_set_property (GObject *object,
priv->context = g_value_dup_object (value);
break;

case PROP_OBJECT:
valent_extension_set_object (self, g_value_get_object (value));
break;

case PROP_PLUGIN_INFO:
priv->plugin_info = g_value_dup_object (value);
break;
Expand Down Expand Up @@ -444,21 +415,6 @@ valent_extension_class_init (ValentExtensionClass *klass)
G_PARAM_EXPLICIT_NOTIFY |
G_PARAM_STATIC_STRINGS));

/**
* ValentExtension:object: (getter get_object)
*
* The [[email protected]] this plugin is bound to.
*
* Since: 1.0
*/
properties [PROP_OBJECT] =
g_param_spec_object ("object", NULL, NULL,
G_TYPE_OBJECT,
(G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_EXPLICIT_NOTIFY |
G_PARAM_STATIC_STRINGS));

/**
* ValentExtension:plugin-info:
*
Expand Down Expand Up @@ -547,26 +503,6 @@ valent_extension_get_context (ValentExtension *extension)
return priv->context;
}

/**
* valent_extension_get_object: (get-property object)
* @extension: a `ValentExtension`
*
* Get the object this plugin is bound to.
*
* Returns: (type GObject.Object) (transfer none) (nullable): a `GObject`
*
* Since: 1.0
*/
gpointer
valent_extension_get_object (ValentExtension *extension)
{
ValentExtensionPrivate *priv = valent_extension_get_instance_private (extension);

g_return_val_if_fail (VALENT_IS_EXTENSION (extension), NULL);

return priv->object;
}

/**
* valent_extension_get_settings: (get-property settings)
* @extension: a `ValentExtension`
Expand Down
9 changes: 4 additions & 5 deletions src/libvalent/core/valent-extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#endif

#include "valent-context.h"
#include "valent-resource.h"

G_BEGIN_DECLS

Expand All @@ -29,23 +30,21 @@ typedef enum
#define VALENT_TYPE_EXTENSION (valent_extension_get_type ())

VALENT_AVAILABLE_IN_1_0
G_DECLARE_DERIVABLE_TYPE (ValentExtension, valent_extension, VALENT, EXTENSION, ValentObject)
G_DECLARE_DERIVABLE_TYPE (ValentExtension, valent_extension, VALENT, EXTENSION, ValentResource)

struct _ValentExtensionClass
{
ValentObjectClass parent_class;
ValentResourceClass parent_class;

/* virtual functions */

/*< private >*/
gpointer padding[8];
gpointer padding[8];
};

VALENT_AVAILABLE_IN_1_0
ValentContext * valent_extension_get_context (ValentExtension *extension);
VALENT_AVAILABLE_IN_1_0
gpointer valent_extension_get_object (ValentExtension *extension);
VALENT_AVAILABLE_IN_1_0
GSettings * valent_extension_get_settings (ValentExtension *extension);
VALENT_AVAILABLE_IN_1_0
ValentPluginState valent_extension_plugin_state_check (ValentExtension *extension,
Expand Down
16 changes: 7 additions & 9 deletions src/libvalent/device/valent-device-plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
#include "valent-device-plugin.h"
#include "valent-packet.h"

#define PLUGIN_SETTINGS_KEY "X-DevicePluginSettings"


/**
* ValentDevicePlugin:
*
Expand Down Expand Up @@ -151,8 +148,9 @@ valent_device_plugin_init (ValentDevicePlugin *self)
*
* Queue a KDE Connect packet to be sent to the device this plugin is bound to.
*
* For notification of success call [[email protected]_object] and
* then [[email protected]_packet].
* For notification of success, you may call [[email protected]_source]
* and then [[email protected]_packet], but note that there can be no
* guarantee the remote device has received the packet.
*
* Since: 1.0
*/
Expand All @@ -166,7 +164,7 @@ valent_device_plugin_queue_packet (ValentDevicePlugin *plugin,
g_return_if_fail (VALENT_IS_DEVICE_PLUGIN (plugin));
g_return_if_fail (VALENT_IS_PACKET (packet));

if ((device = valent_extension_get_object (VALENT_EXTENSION (plugin))) == NULL)
if ((device = valent_resource_get_source (VALENT_RESOURCE (plugin))) == NULL)
return;

destroy = valent_object_ref_cancellable (VALENT_OBJECT (plugin));
Expand Down Expand Up @@ -211,8 +209,8 @@ valent_device_plugin_show_notification (ValentDevicePlugin *plugin,
return;

g_object_get (plugin,
"object", &device,
"plugin-info", &plugin_info,
"source", &device,
NULL);
notification_id = g_strdup_printf ("%s::%s::%s",
valent_device_get_id (device),
Expand Down Expand Up @@ -249,8 +247,8 @@ valent_device_plugin_hide_notification (ValentDevicePlugin *plugin,
return;

g_object_get (plugin,
"object", &device,
"plugin-info", &plugin_info,
"source", &device,
NULL);
notification_id = g_strdup_printf ("%s::%s::%s",
valent_device_get_id (device),
Expand Down Expand Up @@ -411,7 +409,7 @@ valent_device_plugin_set_menu_item (ValentDevicePlugin *plugin,
g_return_if_fail (item == NULL || G_IS_MENU_ITEM (item));

/* NOTE: this method may be called by plugins in their `dispose()` */
if ((device = valent_extension_get_object (VALENT_EXTENSION (plugin))) == NULL)
if ((device = valent_resource_get_source (VALENT_RESOURCE (plugin))) == NULL)
return;

menu = valent_device_get_menu (device);
Expand Down
Loading

0 comments on commit 699ad7c

Please sign in to comment.