From 4e707f0d310baf825b1e5ebccd950c5ba8e21459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Fri, 6 Nov 2020 19:19:24 +0100 Subject: [PATCH] manager: Use GEnum to register DBus errors from FprintError Given that mk_genenum already parses FprintError, add the nick metadata to the errors so that it matches the wanted DBus error and automatically generate the errors list. In this way we'll have to only touch one definition to get everything updated --- src/device.c | 1 - src/fprintd.h | 26 +++++++++++++++++--------- src/manager.c | 35 ++++++++++++++++------------------- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/device.c b/src/device.c index 0c8d3ca..089003c 100644 --- a/src/device.c +++ b/src/device.c @@ -30,7 +30,6 @@ #include #include "fprintd.h" -#include "fprintd-enums.h" #include "storage.h" static const char *FINGERS_NAMES[] = { diff --git a/src/fprintd.h b/src/fprintd.h index 26755c1..eef6644 100644 --- a/src/fprintd.h +++ b/src/fprintd.h @@ -22,6 +22,7 @@ #include #include #include +#include "fprintd-enums.h" #include "fprintd-dbus.h" /* General */ @@ -33,16 +34,23 @@ GQuark fprint_error_quark(void); #define FPRINT_ERROR fprint_error_quark() -#define FPRINT_ERROR_DBUS_INTERFACE "net.reactivated.Fprint.Error" typedef enum { - FPRINT_ERROR_CLAIM_DEVICE, /* developer didn't claim the device */ - FPRINT_ERROR_ALREADY_IN_USE, /* device is already claimed by somebody else */ - FPRINT_ERROR_INTERNAL, /* internal error occurred */ - FPRINT_ERROR_PERMISSION_DENIED, /* PolicyKit refused the action */ - FPRINT_ERROR_NO_ENROLLED_PRINTS, /* No prints are enrolled */ - FPRINT_ERROR_NO_ACTION_IN_PROGRESS, /* No actions currently in progress */ - FPRINT_ERROR_INVALID_FINGERNAME, /* the finger name passed was invalid */ - FPRINT_ERROR_NO_SUCH_DEVICE, /* device does not exist */ + /* developer didn't claim the device */ + FPRINT_ERROR_CLAIM_DEVICE, /*< nick=net.reactivated.Fprint.Error.ClaimDevice >*/ + /* device is already claimed by somebody else */ + FPRINT_ERROR_ALREADY_IN_USE, /*< nick=net.reactivated.Fprint.Error.AlreadyInUse >*/ + /* internal error occurred */ + FPRINT_ERROR_INTERNAL, /*< nick=net.reactivated.Fprint.Error.Internal >*/ + /* PolicyKit refused the action */ + FPRINT_ERROR_PERMISSION_DENIED, /*< nick=net.reactivated.Fprint.Error.PermissionDenied >*/ + /* No prints are enrolled */ + FPRINT_ERROR_NO_ENROLLED_PRINTS, /*< nick=net.reactivated.Fprint.Error.NoEnrolledPrints >*/ + /* No actions currently in progress */ + FPRINT_ERROR_NO_ACTION_IN_PROGRESS, /*< nick=net.reactivated.Fprint.Error.NoActionInProgress >*/ + /* the finger name passed was invalid */ + FPRINT_ERROR_INVALID_FINGERNAME, /*< nick=net.reactivated.Fprint.Error.InvalidFingername >*/ + /* device does not exist */ + FPRINT_ERROR_NO_SUCH_DEVICE, /*< nick=net.reactivated.Fprint.Error.NoSuchDevice >*/ } FprintError; typedef enum { diff --git a/src/manager.c b/src/manager.c index f97fa94..2a9abdb 100644 --- a/src/manager.c +++ b/src/manager.c @@ -394,28 +394,25 @@ static gboolean fprint_manager_get_default_device(FprintManager *manager, } } -#define ERROR_ENTRY(name, dbus_name) \ - { FPRINT_ERROR_ ## name, FPRINT_ERROR_DBUS_INTERFACE "." dbus_name } -GDBusErrorEntry fprint_error_entries[] = -{ - ERROR_ENTRY (CLAIM_DEVICE, "ClaimDevice"), - ERROR_ENTRY (ALREADY_IN_USE, "AlreadyInUse"), - ERROR_ENTRY (INTERNAL, "Internal"), - ERROR_ENTRY (PERMISSION_DENIED, "PermissionDenied"), - ERROR_ENTRY (NO_ENROLLED_PRINTS, "NoEnrolledPrints"), - ERROR_ENTRY (NO_ACTION_IN_PROGRESS, "NoActionInProgress"), - ERROR_ENTRY (INVALID_FINGERNAME, "InvalidFingername"), - ERROR_ENTRY (NO_SUCH_DEVICE, "NoSuchDevice"), -}; - GQuark fprint_error_quark (void) { static volatile gsize quark = 0; - if (!quark) { - g_dbus_error_register_error_domain ("fprintd-error-quark", - &quark, - fprint_error_entries, - G_N_ELEMENTS (fprint_error_entries)); + if (g_once_init_enter (&quark)) { + g_autoptr(GEnumClass) errors_enum = NULL; + GQuark domain; + unsigned i; + + domain = g_quark_from_static_string ("fprintd-error-quark"); + errors_enum = g_type_class_ref (FPRINT_TYPE_ERROR); + + for (i = 0; i < errors_enum->n_values; ++i) { + GEnumValue *value = &errors_enum->values[i]; + + g_dbus_error_register_error (domain, value->value, + value->value_nick); + } + + g_once_init_leave (&quark, domain); } return (GQuark) quark; }