nfc_playlist_scene_file_rename.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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* tmp_old_file_path = furi_string_alloc_set_str(old_file_path);
  8. furi_string_replace(tmp_old_file_path, old_file_name, "");
  9. FuriString* tmp_new_file_path = furi_string_alloc();
  10. furi_string_printf(tmp_new_file_path, "%s%s.txt", furi_string_get_cstr(tmp_old_file_path), nfc_playlist->text_input_output);
  11. if(!storage_file_exists(storage, furi_string_get_cstr(tmp_new_file_path))) {
  12. storage_common_rename_safe(storage, furi_string_get_cstr(nfc_playlist->settings.file_path), furi_string_get_cstr(tmp_new_file_path));
  13. nfc_playlist->settings.file_path = furi_string_alloc_set_str(furi_string_get_cstr(tmp_new_file_path));
  14. }
  15. furi_record_close(RECORD_STORAGE);
  16. furi_string_free(tmp_new_file_path);
  17. furi_string_free(tmp_old_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 = (char*)furi_string_get_cstr(tmp_file_name_furi);
  27. text_input_set_header_text(nfc_playlist->text_input, "Enter new file name");
  28. text_input_set_minimum_length(nfc_playlist->text_input, 1);
  29. text_input_set_result_callback(nfc_playlist->text_input, nfc_playlist_file_rename_menu_callback, nfc_playlist, nfc_playlist->text_input_output, 50, false);
  30. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_TextInput);
  31. }
  32. bool nfc_playlist_file_rename_scene_on_event(void* context, SceneManagerEvent event) {
  33. UNUSED(context);
  34. UNUSED(event);
  35. return false;
  36. }
  37. void nfc_playlist_file_rename_scene_on_exit(void* context) {
  38. NfcPlaylist* nfc_playlist = context;
  39. text_input_reset(nfc_playlist->text_input);
  40. free(nfc_playlist->text_input_output);
  41. }