lfb-event: Add getter and setter for app-id

For applications this is figured out automatically but notification
daemons and portal implementations might want to override it to have the
per-app feedback profile picked up automatically.
This commit is contained in:
Guido Günther
2023-04-17 13:32:21 +02:00
parent a58b47e1eb
commit 2de190fb09
3 changed files with 85 additions and 2 deletions
+75 -2
View File
@@ -79,6 +79,7 @@ enum {
PROP_STATE, PROP_STATE,
PROP_END_REASON, PROP_END_REASON,
PROP_FEEDBACK_PROFILE, PROP_FEEDBACK_PROFILE,
PROP_APP_ID,
PROP_LAST_PROP, PROP_LAST_PROP,
}; };
static GParamSpec *props[PROP_LAST_PROP]; static GParamSpec *props[PROP_LAST_PROP];
@@ -95,6 +96,7 @@ typedef struct _LfbEvent {
char *event; char *event;
gint timeout; gint timeout;
gchar *profile; gchar *profile;
char *app_id;
guint id; guint id;
LfbEventState state; LfbEventState state;
@@ -221,6 +223,9 @@ lfb_event_set_property (GObject *object,
case PROP_FEEDBACK_PROFILE: case PROP_FEEDBACK_PROFILE:
lfb_event_set_feedback_profile (self, g_value_get_string (value)); lfb_event_set_feedback_profile (self, g_value_get_string (value));
break; break;
case PROP_APP_ID:
lfb_event_set_app_id (self, g_value_get_string (value));
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break; break;
@@ -246,6 +251,9 @@ lfb_event_get_property (GObject *object,
case PROP_FEEDBACK_PROFILE: case PROP_FEEDBACK_PROFILE:
g_value_set_string (value, lfb_event_get_feedback_profile (self)); g_value_set_string (value, lfb_event_get_feedback_profile (self));
break; break;
case PROP_APP_ID:
g_value_set_string (value, lfb_event_get_app_id (self));
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break; break;
@@ -262,6 +270,7 @@ lfb_event_finalize (GObject *object)
g_clear_pointer (&self->event, g_free); g_clear_pointer (&self->event, g_free);
g_clear_pointer (&self->profile, g_free); g_clear_pointer (&self->profile, g_free);
g_clear_pointer (&self->app_id, g_free);
G_OBJECT_CLASS (lfb_event_parent_class)->finalize (object); G_OBJECT_CLASS (lfb_event_parent_class)->finalize (object);
} }
@@ -336,6 +345,20 @@ lfb_event_class_init (LfbEventClass *klass)
NULL, NULL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY); G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
/**
* LfbEvent:app-id:
*
* The application id to use for the event.
* [method@LfbEvent.set_feedback_profile]() for details.
*/
props[PROP_APP_ID] =
g_param_spec_string (
"app-id",
"Application Id",
"The Application id to use for this event",
NULL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
g_object_class_install_properties (object_class, PROP_LAST_PROP, props); g_object_class_install_properties (object_class, PROP_LAST_PROP, props);
/** /**
@@ -411,6 +434,7 @@ lfb_event_trigger_feedback (LfbEvent *self, GError **error)
{ {
LfbGdbusFeedback *proxy; LfbGdbusFeedback *proxy;
gboolean success; gboolean success;
const char *app_id;
g_return_val_if_fail (LFB_IS_EVENT (self), FALSE); g_return_val_if_fail (LFB_IS_EVENT (self), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
@@ -429,8 +453,9 @@ lfb_event_trigger_feedback (LfbEvent *self, GError **error)
G_CONNECT_SWAPPED); G_CONNECT_SWAPPED);
} }
app_id = self->app_id ?: lfb_get_app_id ();
success = lfb_gdbus_feedback_call_trigger_feedback_sync (proxy, success = lfb_gdbus_feedback_call_trigger_feedback_sync (proxy,
lfb_get_app_id (), app_id,
self->event, self->event,
build_hints (self), build_hints (self),
self->timeout, self->timeout,
@@ -462,6 +487,7 @@ lfb_event_trigger_feedback_async (LfbEvent *self,
{ {
LfbAsyncData *data; LfbAsyncData *data;
LfbGdbusFeedback *proxy; LfbGdbusFeedback *proxy;
const char *app_id;
g_return_if_fail (LFB_IS_EVENT (self)); g_return_if_fail (LFB_IS_EVENT (self));
if (!lfb_is_initted ()) if (!lfb_is_initted ())
@@ -481,8 +507,10 @@ lfb_event_trigger_feedback_async (LfbEvent *self,
data = g_new0 (LfbAsyncData, 1); data = g_new0 (LfbAsyncData, 1);
data->task = g_task_new (self, cancellable, callback, user_data); data->task = g_task_new (self, cancellable, callback, user_data);
data->event = g_object_ref (self); data->event = g_object_ref (self);
app_id = self->app_id ?: lfb_get_app_id ();
lfb_gdbus_feedback_call_trigger_feedback (proxy, lfb_gdbus_feedback_call_trigger_feedback (proxy,
lfb_get_app_id (), app_id,
self->event, self->event,
build_hints (self), build_hints (self),
self->timeout, self->timeout,
@@ -728,3 +756,48 @@ lfb_event_get_feedback_profile (LfbEvent *self)
return self->profile; return self->profile;
} }
/**
* lfb_event_set_app_id:
* @self: The event
* @app_id: The application id to use
*
* Tells the feedback server to use the given application id for
* this event when it is submitted. The server might ignore this
* request. This can be used by notification daemons to honor per
* application settings automatically.
*
* The functions is usually not used by applications.
*
* A value of %NULL (the default) lets the server pick the profile.
*/
void
lfb_event_set_app_id (LfbEvent *self, const gchar *app_id)
{
g_return_if_fail (LFB_IS_EVENT (self));
if (!g_strcmp0 (self->app_id, app_id))
return;
g_free (self->app_id);
self->app_id = g_strdup (app_id);
g_object_notify_by_pspec (G_OBJECT (self), props[PROP_APP_ID]);
}
/**
* lfb_event_get_app_id:
* @self: The event
*
* Returns the app-id for this event. If no app-id has been explicitly
* set, %NULL is returned. The event uses the app-id returns by
* [func@lfb_get_app_id] in this case.
*
* Returns:(transfer none): The set app-id for this event or %NULL.
*/
const char *
lfb_event_get_app_id (LfbEvent *self)
{
g_return_val_if_fail (LFB_IS_EVENT (self), NULL);
return self->app_id;
}
+2
View File
@@ -75,6 +75,8 @@ void lfb_event_set_timeout (LfbEvent *self, gint timeout);
gint lfb_event_get_timeout (LfbEvent *self); gint lfb_event_get_timeout (LfbEvent *self);
void lfb_event_set_feedback_profile (LfbEvent *self, const char *profile); void lfb_event_set_feedback_profile (LfbEvent *self, const char *profile);
const char *lfb_event_get_feedback_profile (LfbEvent *self); const char *lfb_event_get_feedback_profile (LfbEvent *self);
void lfb_event_set_app_id (LfbEvent *self, const char *app_id);
const char *lfb_event_get_app_id (LfbEvent *self);
const char *lfb_event_get_event (LfbEvent *self); const char *lfb_event_get_event (LfbEvent *self);
LfbEventState lfb_event_get_state (LfbEvent *self); LfbEventState lfb_event_get_state (LfbEvent *self);
LfbEventEndReason lfb_event_get_end_reason (LfbEvent *self); LfbEventEndReason lfb_event_get_end_reason (LfbEvent *self);
+8
View File
@@ -14,6 +14,7 @@ test_lfb_event_props (void)
g_autoptr(LfbEvent) event = NULL; g_autoptr(LfbEvent) event = NULL;
g_autofree gchar *evname = NULL; g_autofree gchar *evname = NULL;
g_autofree gchar *profile = NULL; g_autofree gchar *profile = NULL;
g_autofree gchar *app_id = NULL;
gint timeout; gint timeout;
g_assert_true (lfb_init (TEST_APP_ID, NULL)); g_assert_true (lfb_init (TEST_APP_ID, NULL));
@@ -25,10 +26,12 @@ test_lfb_event_props (void)
"event", &evname, "event", &evname,
"timeout", &timeout, "timeout", &timeout,
"feedback-profile", &profile, "feedback-profile", &profile,
"app-id", &app_id,
NULL); NULL);
g_assert_cmpstr (evname, ==, "window-close"); g_assert_cmpstr (evname, ==, "window-close");
g_assert_cmpint (timeout, ==, -1); g_assert_cmpint (timeout, ==, -1);
g_assert_null (profile); g_assert_null (profile);
g_assert_null (app_id);
g_assert_cmpint (lfb_event_get_end_reason (event), ==, LFB_EVENT_END_REASON_NATURAL); g_assert_cmpint (lfb_event_get_end_reason (event), ==, LFB_EVENT_END_REASON_NATURAL);
g_assert_cmpint (lfb_event_get_state (event), ==, LFB_EVENT_STATE_NONE); g_assert_cmpint (lfb_event_get_state (event), ==, LFB_EVENT_STATE_NONE);
@@ -38,6 +41,11 @@ test_lfb_event_props (void)
g_assert_cmpstr (profile, ==, "full"); g_assert_cmpstr (profile, ==, "full");
g_assert_cmpstr (lfb_event_get_feedback_profile (event), ==, "full"); g_assert_cmpstr (lfb_event_get_feedback_profile (event), ==, "full");
g_object_set (event, "app-id", "com.example.foo", NULL);
g_object_get (event, "app-id", &app_id, NULL);
g_assert_cmpstr (app_id, ==, "com.example.foo");
g_assert_cmpstr (lfb_event_get_app_id (event), ==, "com.example.foo");
lfb_uninit (); lfb_uninit ();
} }