nfc_playlist_scene_file_rename.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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*)malloc(50 * sizeof(char));
  27. strcpy(nfc_playlist->text_input_output, furi_string_get_cstr(tmp_file_name_furi));
  28. furi_string_free(tmp_file_name_furi);
  29. text_input_set_header_text(nfc_playlist->text_input, "Enter new file name");
  30. text_input_set_minimum_length(nfc_playlist->text_input, 1);
  31. 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);
  32. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_TextInput);
  33. }
  34. bool nfc_playlist_file_rename_scene_on_event(void* context, SceneManagerEvent event) {
  35. UNUSED(context);
  36. UNUSED(event);
  37. return false;
  38. }
  39. void nfc_playlist_file_rename_scene_on_exit(void* context) {
  40. NfcPlaylist* nfc_playlist = context;
  41. text_input_reset(nfc_playlist->text_input);
  42. free(nfc_playlist->text_input_output);
  43. }