nfc_playlist_scene_file_rename.c 2.3 KB

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