subbrute_scene_save_name.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "../subbrute_i.h"
  2. #include "subbrute_scene.h"
  3. #include <lib/toolbox/random_name.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. set_random_name(instance->text_store, sizeof(instance->text_store));
  10. text_input_set_header_text(text_input, "Name of file");
  11. text_input_set_result_callback(
  12. text_input,
  13. subbrute_text_input_callback,
  14. instance,
  15. instance->text_store,
  16. SUBBRUTE_MAX_LEN_NAME,
  17. true);
  18. string_set_str(instance->file_path, SUBBRUTE_PATH);
  19. ValidatorIsFile* validator_is_file =
  20. validator_is_file_alloc_init(string_get_cstr(instance->file_path), SUBBRUTE_FILE_EXT, "");
  21. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  22. view_dispatcher_switch_to_view(instance->view_dispatcher, SubBruteViewTextInput);
  23. }
  24. bool subbrute_scene_save_name_on_event(void* context, SceneManagerEvent event) {
  25. SubBruteState* instance = (SubBruteState*)context;
  26. bool consumed = false;
  27. if(event.type == SceneManagerEventTypeBack) {
  28. scene_manager_previous_scene(instance->scene_manager);
  29. return true;
  30. } else if(
  31. event.type == SceneManagerEventTypeCustom &&
  32. event.event == SubBruteCustomEventTypeTextEditDone) {
  33. #ifdef FURI_DEBUG
  34. FURI_LOG_D(TAG, "Saving: %s", instance->text_store);
  35. #endif
  36. bool success = false;
  37. if(strcmp(instance->text_store, "")) {
  38. furi_string_cat_printf(
  39. instance->file_path, "/%s%s", instance->text_store, SUBBRUTE_FILE_EXT);
  40. if(subbrute_device_save_file(instance->device, string_get_cstr(instance->file_path))) {
  41. scene_manager_next_scene(instance->scene_manager, SubBruteSceneSaveSuccess);
  42. success = true;
  43. consumed = true;
  44. }
  45. }
  46. if(!success) {
  47. dialog_message_show_storage_error(instance->dialogs, "Error during saving!");
  48. consumed = scene_manager_search_and_switch_to_previous_scene(
  49. instance->scene_manager, SubBruteSceneSetupAttack);
  50. }
  51. }
  52. return consumed;
  53. }
  54. void subbrute_scene_save_name_on_exit(void* context) {
  55. SubBruteState* instance = (SubBruteState*)context;
  56. // Clear view
  57. void* validator_context = text_input_get_validator_callback_context(instance->text_input);
  58. text_input_set_validator(instance->text_input, NULL, NULL);
  59. validator_is_file_free(validator_context);
  60. text_input_reset(instance->text_input);
  61. string_reset(instance->file_path);
  62. }