From 32b70c0edc996c6db01af863d43164405408015e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Fri, 22 Jan 2021 18:17:23 +0100 Subject: [PATCH] device: Add an unique function to check if we can stop the current action --- src/device.c | 85 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 35 deletions(-) diff --git a/src/device.c b/src/device.c index ddef380..83e88c5 100644 --- a/src/device.c +++ b/src/device.c @@ -1157,6 +1157,52 @@ can_start_action (FprintDevice *rdev, GError **error) return FALSE; } +static gboolean +can_stop_action (FprintDevice *rdev, + FprintDeviceAction action, + GError **error) +{ + FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); + + switch (priv->current_action) + { + case ACTION_IDENTIFY: + case ACTION_VERIFY: + if (action == ACTION_IDENTIFY || action == ACTION_VERIFY) + return TRUE; + break; + + default: + if (priv->current_action == action) + return TRUE; + } + + if (priv->current_action != ACTION_NONE) + { + g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_ALREADY_IN_USE, + "Another operation is already in progress"); + return FALSE; + } + + switch (action) + { + case ACTION_ENROLL: + g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_NO_ACTION_IN_PROGRESS, + "No enrollment in progress"); + return FALSE; + + case ACTION_VERIFY: + g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_NO_ACTION_IN_PROGRESS, + "No verification in progress"); + return FALSE; + + default: + g_set_error (error, FPRINT_ERROR, FPRINT_ERROR_NO_ACTION_IN_PROGRESS, + "No action in progress"); + return FALSE; + } +} + static void stoppable_action_completed (FprintDevice *rdev) { @@ -1443,7 +1489,6 @@ fprint_device_verify_stop (FprintDBusDevice *dbus_dev, GDBusMethodInvocation *invocation) { FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); - FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); g_autoptr(GError) error = NULL; @@ -1453,24 +1498,9 @@ fprint_device_verify_stop (FprintDBusDevice *dbus_dev, return TRUE; } - switch (priv->current_action) + if (!can_stop_action (rdev, ACTION_VERIFY, &error)) { - case ACTION_VERIFY: - case ACTION_IDENTIFY: - break; - - case ACTION_NONE: - g_dbus_method_invocation_return_error_literal ( - invocation, FPRINT_ERROR, - FPRINT_ERROR_NO_ACTION_IN_PROGRESS, - "No verification 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"); + g_dbus_method_invocation_return_gerror (invocation, error); return TRUE; } @@ -1756,7 +1786,6 @@ fprint_device_enroll_stop (FprintDBusDevice *dbus_dev, GDBusMethodInvocation *invocation) { FprintDevice *rdev = FPRINT_DEVICE (dbus_dev); - FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev); g_autoptr(GError) error = NULL; @@ -1766,23 +1795,9 @@ fprint_device_enroll_stop (FprintDBusDevice *dbus_dev, return TRUE; } - switch (priv->current_action) + if (!can_stop_action (rdev, ACTION_ENROLL, &error)) { - case ACTION_ENROLL: - break; - - case ACTION_NONE: - g_dbus_method_invocation_return_error_literal ( - invocation, FPRINT_ERROR, - FPRINT_ERROR_NO_ACTION_IN_PROGRESS, - "No enrollment 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"); + g_dbus_method_invocation_return_gerror (invocation, error); return TRUE; }