archive_scene_rename.c 3.1 KB

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