nfc_playlist_scene_file_rename.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. char const* new_file_path_cstr = furi_string_get_cstr(new_file_path);
  11. if(!storage_file_exists(storage, new_file_path_cstr)) {
  12. storage_common_rename(storage, furi_string_get_cstr(nfc_playlist->settings.file_path), new_file_path_cstr);
  13. furi_string_free(nfc_playlist->settings.file_path);
  14. nfc_playlist->settings.file_path = furi_string_alloc_set_str(new_file_path_cstr);
  15. }
  16. furi_record_close(RECORD_STORAGE);
  17. furi_string_free(new_file_path);
  18. scene_manager_previous_scene(nfc_playlist->scene_manager);
  19. }
  20. void nfc_playlist_file_rename_scene_on_enter(void* context) {
  21. NfcPlaylist* nfc_playlist = context;
  22. char const* tmp_file_path = (char*)furi_string_get_cstr(nfc_playlist->settings.file_path);
  23. char const* tmp_file_name = strchr(tmp_file_path, '/') != NULL ? &strrchr(tmp_file_path, '/')[1] : tmp_file_path;
  24. FuriString* tmp_file_name_furi = furi_string_alloc_set_str(tmp_file_name);
  25. furi_string_replace(tmp_file_name_furi, ".txt", "");
  26. nfc_playlist->text_input_output = strdup(furi_string_get_cstr(tmp_file_name_furi));
  27. furi_string_free(tmp_file_name_furi);
  28. text_input_set_header_text(nfc_playlist->text_input, "Enter new file name");
  29. text_input_set_minimum_length(nfc_playlist->text_input, 1);
  30. text_input_set_result_callback(nfc_playlist->text_input, nfc_playlist_file_rename_menu_callback, nfc_playlist, nfc_playlist->text_input_output, (50 * sizeof(char)), false);
  31. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_TextInput);
  32. }
  33. bool nfc_playlist_file_rename_scene_on_event(void* context, SceneManagerEvent event) {
  34. UNUSED(context);
  35. UNUSED(event);
  36. return false;
  37. }
  38. void nfc_playlist_file_rename_scene_on_exit(void* context) {
  39. NfcPlaylist* nfc_playlist = context;
  40. text_input_reset(nfc_playlist->text_input);
  41. free(nfc_playlist->text_input_output);
  42. }