archive_scene_rename.c 3.0 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. #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. string_t filename;
  18. string_init(filename);
  19. path_extract_filename(current->path, filename, true);
  20. strlcpy(archive->text_store, 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. string_get_cstr(archive->browser->path), archive->file_extension, NULL);
  32. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  33. string_clear(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("storage");
  42. const char* path_src = archive_get_name(archive->browser);
  43. ArchiveFile_t* file = archive_get_current_file(archive->browser);
  44. string_t path_dst;
  45. string_init(path_dst);
  46. path_extract_dirname(path_src, path_dst);
  47. string_cat_printf(path_dst, "/%s%s", archive->text_store, known_ext[file->type]);
  48. storage_common_rename(fs_api, path_src, string_get_cstr(path_dst));
  49. furi_record_close("storage");
  50. if(file->fav) {
  51. archive_favorites_rename(path_src, string_get_cstr(path_dst));
  52. }
  53. string_clear(path_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. }