subbrute_scene_save_name.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "../subbrute_i.h"
  2. #include "subbrute_scene.h"
  3. #include <toolbox/name_generator.h>
  4. #define TAG "SubBruteSceneSaveFile"
  5. void subbrute_scene_save_name_on_enter(void* context) {
  6. SubBruteState* instance = (SubBruteState*)context;
  7. // Setup view
  8. TextInput* text_input = instance->text_input;
  9. if(instance->device->attack == SubBruteAttackLoadFile) {
  10. name_generator_make_auto(
  11. instance->text_store,
  12. sizeof(instance->text_store),
  13. subbrute_protocol_file(instance->device->file_protocol_info->file));
  14. } else {
  15. name_generator_make_auto(
  16. instance->text_store,
  17. sizeof(instance->text_store),
  18. subbrute_protocol_file(instance->device->protocol_info->file));
  19. }
  20. text_input_set_header_text(text_input, "Name of file");
  21. text_input_set_result_callback(
  22. text_input,
  23. subbrute_text_input_callback,
  24. instance,
  25. instance->text_store,
  26. SUBBRUTE_MAX_LEN_NAME,
  27. true);
  28. furi_string_reset(instance->file_path);
  29. furi_string_set_str(instance->file_path, SUBBRUTE_PATH);
  30. ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
  31. furi_string_get_cstr(instance->file_path), SUBBRUTE_FILE_EXT, "");
  32. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  33. view_dispatcher_switch_to_view(instance->view_dispatcher, SubBruteViewTextInput);
  34. }
  35. bool subbrute_scene_save_name_on_event(void* context, SceneManagerEvent event) {
  36. SubBruteState* instance = (SubBruteState*)context;
  37. bool consumed = false;
  38. if(event.type == SceneManagerEventTypeBack) {
  39. scene_manager_previous_scene(instance->scene_manager);
  40. return true;
  41. } else if(
  42. event.type == SceneManagerEventTypeCustom &&
  43. event.event == SubBruteCustomEventTypeTextEditDone) {
  44. #ifdef FURI_DEBUG
  45. FURI_LOG_D(TAG, "Saving: %s", instance->text_store);
  46. #endif
  47. bool success = false;
  48. if(strcmp(instance->text_store, "") != 0) {
  49. furi_string_reset(instance->file_path);
  50. furi_string_cat_printf(
  51. instance->file_path,
  52. "%s/%s%s",
  53. SUBBRUTE_PATH,
  54. instance->text_store,
  55. SUBBRUTE_FILE_EXT);
  56. if(subbrute_device_save_file(
  57. instance->device, furi_string_get_cstr(instance->file_path))) {
  58. scene_manager_next_scene(instance->scene_manager, SubBruteSceneSaveSuccess);
  59. success = true;
  60. consumed = true;
  61. }
  62. }
  63. if(!success) {
  64. dialog_message_show_storage_error(instance->dialogs, "Error during saving!");
  65. consumed = scene_manager_search_and_switch_to_previous_scene(
  66. instance->scene_manager, SubBruteSceneSetupAttack);
  67. }
  68. }
  69. return consumed;
  70. }
  71. void subbrute_scene_save_name_on_exit(void* context) {
  72. SubBruteState* instance = (SubBruteState*)context;
  73. // Clear view
  74. void* validator_context = text_input_get_validator_callback_context(instance->text_input);
  75. text_input_set_validator(instance->text_input, NULL, NULL);
  76. validator_is_file_free(validator_context);
  77. text_input_reset(instance->text_input);
  78. furi_string_reset(instance->file_path);
  79. }