move.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 = totp_config_get_token_iterator_context(plugin_state);
  30. size_t total_count = totp_token_info_iterator_get_total_count(iterator_context);
  31. if(!args_read_int_and_trim(args, &token_number) || token_number < 1 ||
  32. (size_t)token_number > total_count) {
  33. totp_cli_print_invalid_arguments();
  34. return;
  35. }
  36. int new_token_number = 0;
  37. if(!args_read_int_and_trim(args, &new_token_number) || new_token_number < 1 ||
  38. (size_t)new_token_number > total_count) {
  39. totp_cli_print_invalid_arguments();
  40. return;
  41. }
  42. if(token_number == new_token_number) {
  43. TOTP_CLI_PRINTF_ERROR("New token number matches current token number\r\n");
  44. return;
  45. }
  46. TOTP_CLI_LOCK_UI(plugin_state);
  47. size_t token_index = token_number - 1;
  48. size_t new_token_index = new_token_number - 1;
  49. size_t original_token_index = totp_token_info_iterator_get_current_token_index(iterator_context);
  50. totp_cli_print_processing();
  51. if(totp_token_info_iterator_go_to(iterator_context, token_index) &&
  52. totp_token_info_iterator_move_current_token_info(iterator_context, new_token_index)) {
  53. totp_cli_delete_last_line();
  54. TOTP_CLI_PRINTF_SUCCESS(
  55. "Token \"%s\" has been successfully updated\r\n",
  56. furi_string_get_cstr(totp_token_info_iterator_get_current_token(iterator_context)->name));
  57. } else {
  58. totp_cli_delete_last_line();
  59. totp_cli_print_error_updating_config_file();
  60. }
  61. totp_token_info_iterator_go_to(iterator_context, original_token_index);
  62. TOTP_CLI_UNLOCK_UI(plugin_state);
  63. }