move.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "move.h"
  2. #include <stdlib.h>
  3. #include <lib/toolbox/args.h>
  4. #include "../../../types/token_info.h"
  5. #include "../../../services/config/config.h"
  6. #include "../../cli_helpers.h"
  7. #include "../../../ui/scene_director.h"
  8. #include "../../common_command_arguments.h"
  9. #define TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX "new_index"
  10. void totp_cli_command_move_docopt_commands() {
  11. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_MOVE ", " TOTP_CLI_COMMAND_MOVE_ALT
  12. " Move token\r\n");
  13. }
  14. void totp_cli_command_move_docopt_usage() {
  15. TOTP_CLI_PRINTF(
  16. " " TOTP_CLI_COMMAND_NAME
  17. " " DOCOPT_REQUIRED(TOTP_CLI_COMMAND_MOVE " | " TOTP_CLI_COMMAND_MOVE_ALT) " " DOCOPT_ARGUMENT(
  18. TOTP_CLI_COMMAND_ARG_INDEX) " " DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX) "\r\n");
  19. }
  20. void totp_cli_command_move_docopt_arguments() {
  21. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX
  22. " New token index in the list\r\n");
  23. }
  24. void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  25. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  26. return;
  27. }
  28. int token_number;
  29. TokenInfoIteratorContext* iterator_context =
  30. totp_config_get_token_iterator_context(plugin_state);
  31. size_t total_count = totp_token_info_iterator_get_total_count(iterator_context);
  32. if(!args_read_int_and_trim(args, &token_number) || token_number < 1 ||
  33. (size_t)token_number > total_count) {
  34. totp_cli_print_invalid_arguments();
  35. return;
  36. }
  37. int new_token_number = 0;
  38. if(!args_read_int_and_trim(args, &new_token_number) || new_token_number < 1 ||
  39. (size_t)new_token_number > total_count) {
  40. totp_cli_print_invalid_arguments();
  41. return;
  42. }
  43. if(token_number == new_token_number) {
  44. TOTP_CLI_PRINTF_ERROR("New token number matches current token number\r\n");
  45. return;
  46. }
  47. TOTP_CLI_LOCK_UI(plugin_state);
  48. size_t token_index = token_number - 1;
  49. size_t new_token_index = new_token_number - 1;
  50. size_t original_token_index =
  51. totp_token_info_iterator_get_current_token_index(iterator_context);
  52. totp_cli_print_processing();
  53. if(totp_token_info_iterator_go_to(iterator_context, token_index) &&
  54. totp_token_info_iterator_move_current_token_info(iterator_context, new_token_index)) {
  55. totp_cli_delete_last_line();
  56. TOTP_CLI_PRINTF_SUCCESS(
  57. "Token \"%s\" has been successfully updated\r\n",
  58. furi_string_get_cstr(
  59. totp_token_info_iterator_get_current_token(iterator_context)->name));
  60. } else {
  61. totp_cli_delete_last_line();
  62. totp_cli_print_error_updating_config_file();
  63. }
  64. totp_token_info_iterator_go_to(iterator_context, original_token_index);
  65. TOTP_CLI_UNLOCK_UI(plugin_state);
  66. }