archive_scene_rename.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "../archive_i.h"
  2. #include "../helpers/archive_favorites.h"
  3. #include "../helpers/archive_files.h"
  4. #include "../helpers/archive_browser.h"
  5. #define SCENE_RENAME_CUSTOM_EVENT (0UL)
  6. #define MAX_TEXT_INPUT_LEN 22
  7. void archive_scene_rename_text_input_callback(void* context) {
  8. ArchiveApp* archive = (ArchiveApp*)context;
  9. view_dispatcher_send_custom_event(archive->view_dispatcher, SCENE_RENAME_CUSTOM_EVENT);
  10. }
  11. void archive_scene_rename_on_enter(void* context) {
  12. ArchiveApp* archive = (ArchiveApp*)context;
  13. TextInput* text_input = archive->text_input;
  14. ArchiveFile_t* current = archive_get_current_file(archive->browser);
  15. strlcpy(archive->text_store, string_get_cstr(current->name), MAX_NAME_LEN);
  16. archive_get_file_extension(archive->text_store, archive->file_extension);
  17. archive_trim_file_path(archive->text_store, true);
  18. text_input_set_header_text(text_input, "Rename:");
  19. text_input_set_result_callback(
  20. text_input,
  21. archive_scene_rename_text_input_callback,
  22. archive,
  23. archive->text_store,
  24. MAX_TEXT_INPUT_LEN,
  25. false);
  26. ValidatorIsFile* validator_is_file =
  27. validator_is_file_alloc_init(archive_get_path(archive->browser), archive->file_extension);
  28. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  29. view_dispatcher_switch_to_view(archive->view_dispatcher, ArchiveViewTextInput);
  30. }
  31. bool archive_scene_rename_on_event(void* context, SceneManagerEvent event) {
  32. ArchiveApp* archive = (ArchiveApp*)context;
  33. bool consumed = false;
  34. if(event.type == SceneManagerEventTypeCustom) {
  35. if(event.event == SCENE_RENAME_CUSTOM_EVENT) {
  36. Storage* fs_api = furi_record_open("storage");
  37. string_t buffer_src;
  38. string_t buffer_dst;
  39. const char* path = archive_get_path(archive->browser);
  40. const char* name = archive_get_name(archive->browser);
  41. string_init_printf(buffer_src, "%s/%s", path, name);
  42. string_init_printf(buffer_dst, "%s/%s", path, archive->text_store);
  43. // append extension
  44. ArchiveFile_t* file = archive_get_current_file(archive->browser);
  45. string_cat(buffer_dst, known_ext[file->type]);
  46. storage_common_rename(
  47. fs_api, string_get_cstr(buffer_src), string_get_cstr(buffer_dst));
  48. furi_record_close("storage");
  49. if(file->fav) {
  50. archive_favorites_rename(path, name, string_get_cstr(buffer_dst));
  51. }
  52. string_clear(buffer_src);
  53. string_clear(buffer_dst);
  54. scene_manager_next_scene(archive->scene_manager, ArchiveAppSceneBrowser);
  55. consumed = true;
  56. }
  57. }
  58. return consumed;
  59. }
  60. void archive_scene_rename_on_exit(void* context) {
  61. ArchiveApp* archive = (ArchiveApp*)context;
  62. // Clear view
  63. void* validator_context = text_input_get_validator_callback_context(archive->text_input);
  64. text_input_set_validator(archive->text_input, NULL, NULL);
  65. validator_is_file_free(validator_context);
  66. text_input_reset(archive->text_input);
  67. }