nfc_playlist_scene_playlist_rename.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "../nfc_playlist.h"
  2. void nfc_playlist_playlist_rename_menu_callback(void* context) {
  3. NfcPlaylist* nfc_playlist = context;
  4. FuriString* old_file_path = furi_string_alloc();
  5. path_extract_dirname(
  6. furi_string_get_cstr(nfc_playlist->settings.playlist_path), old_file_path);
  7. FuriString* new_file_path = furi_string_alloc_set(old_file_path);
  8. path_concat(
  9. furi_string_get_cstr(old_file_path), nfc_playlist->text_input_output, new_file_path);
  10. furi_string_free(old_file_path);
  11. furi_string_cat_str(new_file_path, ".txt");
  12. Storage* storage = furi_record_open(RECORD_STORAGE);
  13. bool playlist_exist_already = false;
  14. if(!storage_file_exists(storage, furi_string_get_cstr(new_file_path))) {
  15. if(storage_common_rename(
  16. storage,
  17. furi_string_get_cstr(nfc_playlist->settings.playlist_path),
  18. furi_string_get_cstr(new_file_path)) == FSE_OK) {
  19. furi_string_swap(nfc_playlist->settings.playlist_path, new_file_path);
  20. }
  21. } else {
  22. if(furi_string_cmp(nfc_playlist->settings.playlist_path, new_file_path) != 0) {
  23. playlist_exist_already = true;
  24. }
  25. }
  26. furi_string_free(new_file_path);
  27. furi_record_close(RECORD_STORAGE);
  28. view_dispatcher_send_custom_event(nfc_playlist->view_dispatcher, playlist_exist_already);
  29. }
  30. void nfc_playlist_playlist_rename_scene_on_enter(void* context) {
  31. NfcPlaylist* nfc_playlist = context;
  32. FuriString* tmp_file_name = furi_string_alloc();
  33. path_extract_filename_no_ext(
  34. furi_string_get_cstr(nfc_playlist->settings.playlist_path), tmp_file_name);
  35. nfc_playlist->text_input_output = malloc(MAX_PLAYLIST_NAME_LEN + 1);
  36. strcpy(nfc_playlist->text_input_output, furi_string_get_cstr(tmp_file_name));
  37. furi_string_free(tmp_file_name);
  38. text_input_set_header_text(nfc_playlist->text_input, "Enter new file name");
  39. text_input_set_minimum_length(nfc_playlist->text_input, 1);
  40. text_input_set_result_callback(
  41. nfc_playlist->text_input,
  42. nfc_playlist_playlist_rename_menu_callback,
  43. nfc_playlist,
  44. nfc_playlist->text_input_output,
  45. MAX_PLAYLIST_NAME_LEN,
  46. false);
  47. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_TextInput);
  48. }
  49. bool nfc_playlist_playlist_rename_scene_on_event(void* context, SceneManagerEvent event) {
  50. NfcPlaylist* nfc_playlist = context;
  51. if(event.type == SceneManagerEventTypeCustom) {
  52. bool playlist_exist_already = event.event;
  53. if(playlist_exist_already) {
  54. scene_manager_next_scene(
  55. nfc_playlist->scene_manager, NfcPlaylistScene_ErrorPlaylistAlreadyExists);
  56. } else {
  57. scene_manager_search_and_switch_to_previous_scene(
  58. nfc_playlist->scene_manager, NfcPlaylistScene_MainMenu);
  59. }
  60. return true;
  61. }
  62. return false;
  63. }
  64. void nfc_playlist_playlist_rename_scene_on_exit(void* context) {
  65. NfcPlaylist* nfc_playlist = context;
  66. free(nfc_playlist->text_input_output);
  67. text_input_reset(nfc_playlist->text_input);
  68. }