move.c 2.8 KB

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