device: Use a common error function if an action is ongoing

There is no need to dupliate the code. Just create one function that
sets an error and returns FALSE if action is not ACTION_NONE.
This commit is contained in:
Benjamin Berg
2020-12-03 22:30:22 +01:00
parent 2ca2d5e62c
commit 583cd870d8

View File

@ -955,6 +955,48 @@ static void report_verify_status (FprintDevice *rdev,
session->verify_status_reported = TRUE;
}
static gboolean can_start_action(FprintDevice *rdev, GError **error) {
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev);
switch (priv->current_action) {
case ACTION_NONE:
return TRUE;
case ACTION_ENROLL:
g_set_error (error,
FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE,
"Enrollment already in progress");
break;
case ACTION_IDENTIFY:
case ACTION_VERIFY:
g_set_error (error,
FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE,
"Enrollment already in progress");
break;
case ACTION_OPEN:
g_set_error (error,
FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE,
"Claim already in progress");
break;
case ACTION_CLOSE:
g_set_error (error,
FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE,
"Release already in progress");
break;
case ACTION_DELETE:
g_set_error (error,
FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE,
"Delete already in progress");
break;
default: /* Fallback only. */
g_assert_not_reached();
g_set_error (error,
FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE,
"Another operation is already in progress");
}
return FALSE;
}
static void match_cb (FpDevice *device,
FpPrint *match,
FpPrint *print,
@ -1101,19 +1143,8 @@ static gboolean fprint_device_verify_start (FprintDBusDevice *dbus_dev,
session = session_data_get (priv);
switch (priv->current_action) {
case ACTION_NONE:
break;
case ACTION_VERIFY:
case ACTION_IDENTIFY:
g_dbus_method_invocation_return_error_literal (
invocation, FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE,
"Verification already in progress");
return TRUE;
default:
g_dbus_method_invocation_return_error_literal (
invocation, FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE,
"Another operation is already in progress");
if (!can_start_action (rdev, &error)) {
g_dbus_method_invocation_return_gerror (invocation, error);
return TRUE;
}
@ -1431,18 +1462,8 @@ static gboolean fprint_device_enroll_start (FprintDBusDevice *dbus_dev,
return TRUE;
}
switch (priv->current_action) {
case ACTION_NONE:
break;
case ACTION_ENROLL:
g_dbus_method_invocation_return_error_literal (
invocation, FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE,
"Enrollment already in progress");
return TRUE;
default:
g_dbus_method_invocation_return_error_literal (
invocation, FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE,
"Another operation is already in progress");
if (!can_start_action (rdev, &error)) {
g_dbus_method_invocation_return_gerror (invocation, error);
return TRUE;
}
@ -1644,18 +1665,8 @@ static gboolean fprint_device_delete_enrolled_fingers (FprintDBusDevice *dbus_de
log_offending_client (invocation);
#endif
switch (priv->current_action) {
case ACTION_NONE:
break;
case ACTION_DELETE:
g_dbus_method_invocation_return_error_literal (
invocation, FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE,
"Deletion already in progress");
return TRUE;
default:
g_dbus_method_invocation_return_error_literal (
invocation, FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE,
"Another operation is already in progress");
if (!can_start_action(rdev, &error)) {
g_dbus_method_invocation_return_gerror (invocation, error);
return TRUE;
}
@ -1709,18 +1720,8 @@ static gboolean fprint_device_delete_enrolled_fingers2 (FprintDBusDevice *dbus_d
return TRUE;
}
switch (priv->current_action) {
case ACTION_NONE:
break;
case ACTION_DELETE:
g_dbus_method_invocation_return_error_literal (
invocation, FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE,
"Deletion already in progress");
return TRUE;
default:
g_dbus_method_invocation_return_error_literal (
invocation, FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE,
"Another operation is already in progress");
if (!can_start_action(rdev, &error)) {
g_dbus_method_invocation_return_gerror (invocation, error);
return TRUE;
}