move.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "move.h"
  2. #include <stdlib.h>
  3. #include <lib/toolbox/args.h>
  4. #include <linked_list.h>
  5. #include "../../../types/token_info.h"
  6. #include "../../../services/config/config.h"
  7. #include "../../cli_helpers.h"
  8. #include "../../../ui/scene_director.h"
  9. #include "../../common_command_arguments.h"
  10. #define TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX "new_index"
  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. void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  26. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  27. return;
  28. }
  29. int token_index;
  30. if(!args_read_int_and_trim(args, &token_index) || token_index < 1 ||
  31. token_index > plugin_state->tokens_count) {
  32. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  33. return;
  34. }
  35. int new_token_index = 0;
  36. if(!args_read_int_and_trim(args, &new_token_index) || new_token_index < 1 ||
  37. new_token_index > plugin_state->tokens_count) {
  38. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  39. return;
  40. }
  41. bool activate_generate_token_scene = false;
  42. if(plugin_state->current_scene != TotpSceneAuthentication) {
  43. totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
  44. activate_generate_token_scene = true;
  45. }
  46. TokenInfo* token_info = NULL;
  47. plugin_state->tokens_list =
  48. list_remove_at(plugin_state->tokens_list, token_index - 1, (void**)&token_info);
  49. furi_check(token_info != NULL);
  50. plugin_state->tokens_list =
  51. list_insert_at(plugin_state->tokens_list, new_token_index - 1, token_info);
  52. if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) {
  53. TOTP_CLI_PRINTF_SUCCESS(
  54. "Token \"%s\" has been successfully updated\r\n", token_info->name);
  55. } else {
  56. TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
  57. }
  58. if(activate_generate_token_scene) {
  59. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  60. }
  61. }