archive_scene_rename.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 = validator_is_file_alloc_init(
  27. archive_get_path(archive->browser), archive->file_extension, NULL);
  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", name);
  42. //TODO: take path from src name
  43. string_init_printf(buffer_dst, "%s/%s", path, archive->text_store);
  44. // append extension
  45. ArchiveFile_t* file = archive_get_current_file(archive->browser);
  46. string_cat(buffer_dst, known_ext[file->type]);
  47. storage_common_rename(
  48. fs_api, string_get_cstr(buffer_src), string_get_cstr(buffer_dst));
  49. furi_record_close("storage");
  50. if(file->fav) {
  51. archive_favorites_rename(name, string_get_cstr(buffer_dst));
  52. }
  53. string_clear(buffer_src);
  54. string_clear(buffer_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. }