nfc_playlist_scene_playlist_rename.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "../nfc_playlist.h"
  2. void nfc_playlist_playlist_rename_menu_callback(void* context) {
  3. NfcPlaylist* nfc_playlist = context;
  4. char const* old_file_path = furi_string_get_cstr(nfc_playlist->settings.playlist_path);
  5. FuriString* new_file_path = furi_string_alloc_set(nfc_playlist->settings.playlist_path);
  6. furi_string_replace(new_file_path, strchr(old_file_path, '/') != NULL ? &strrchr(old_file_path, '/')[1] : old_file_path, nfc_playlist->text_input_output);
  7. furi_string_cat_str(new_file_path, ".txt");
  8. Storage* storage = furi_record_open(RECORD_STORAGE);
  9. if (!storage_file_exists(storage, furi_string_get_cstr(new_file_path))) {
  10. storage_common_rename(storage, old_file_path, furi_string_get_cstr(new_file_path));
  11. furi_string_swap(nfc_playlist->settings.playlist_path, new_file_path);
  12. }
  13. furi_record_close(RECORD_STORAGE);
  14. furi_string_free(new_file_path);
  15. scene_manager_search_and_switch_to_previous_scene(nfc_playlist->scene_manager, NfcPlaylistScene_MainMenu);
  16. }
  17. void nfc_playlist_playlist_rename_scene_on_enter(void* context) {
  18. NfcPlaylist* nfc_playlist = context;
  19. char const* tmp_file_path = furi_string_get_cstr(nfc_playlist->settings.playlist_path);
  20. FuriString* tmp_file_name = furi_string_alloc_set_str(strchr(tmp_file_path, '/') != NULL ? &strrchr(tmp_file_path, '/')[1] : tmp_file_path);
  21. furi_string_replace(tmp_file_name, ".txt", "");
  22. nfc_playlist->text_input_output = malloc(PLAYLIST_NAME_LEN);
  23. strcpy(nfc_playlist->text_input_output, furi_string_get_cstr(tmp_file_name));
  24. furi_string_free(tmp_file_name);
  25. text_input_set_header_text(nfc_playlist->text_input, "Enter new file name");
  26. text_input_set_minimum_length(nfc_playlist->text_input, 1);
  27. text_input_set_result_callback(nfc_playlist->text_input, nfc_playlist_playlist_rename_menu_callback, nfc_playlist, nfc_playlist->text_input_output, PLAYLIST_NAME_LEN, false);
  28. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_TextInput);
  29. }
  30. bool nfc_playlist_playlist_rename_scene_on_event(void* context, SceneManagerEvent event) {
  31. UNUSED(context);
  32. UNUSED(event);
  33. return false;
  34. }
  35. void nfc_playlist_playlist_rename_scene_on_exit(void* context) {
  36. NfcPlaylist* nfc_playlist = context;
  37. free(nfc_playlist->text_input_output);
  38. text_input_reset(nfc_playlist->text_input);
  39. }