nfc_playlist_scene_file_rename.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. FuriString* tmp_old_file_path = furi_string_alloc();
  6. FuriString* tmp_new_file_path = furi_string_alloc();
  7. char const* old_file_path = (char*)furi_string_get_cstr(nfc_playlist->settings.file_path);
  8. char const* old_file_name = strchr(old_file_path, '/') != NULL ? &strrchr(old_file_path, '/')[1] : old_file_path;
  9. furi_string_printf(tmp_old_file_path, "%s", old_file_path);
  10. furi_string_replace(tmp_old_file_path, old_file_name, "");
  11. furi_string_printf(tmp_new_file_path, "%s%s.txt", furi_string_get_cstr(tmp_old_file_path), nfc_playlist->text_input_output);
  12. if(!storage_file_exists(storage, furi_string_get_cstr(tmp_new_file_path))) {
  13. storage_common_rename_safe(storage, furi_string_get_cstr(nfc_playlist->settings.file_path), furi_string_get_cstr(tmp_new_file_path));
  14. nfc_playlist->settings.file_path = furi_string_alloc_set_str(furi_string_get_cstr(tmp_new_file_path));
  15. }
  16. furi_record_close(RECORD_STORAGE);
  17. furi_string_free(tmp_new_file_path);
  18. furi_string_free(tmp_old_file_path);
  19. scene_manager_previous_scene(nfc_playlist->scene_manager);
  20. }
  21. void nfc_playlist_file_rename_scene_on_enter(void* context) {
  22. NfcPlaylist* nfc_playlist = context;
  23. nfc_playlist->text_input_output = (char*)malloc(50);
  24. text_input_set_header_text(nfc_playlist->text_input, "Enter new file name");
  25. text_input_set_minimum_length(nfc_playlist->text_input, 1);
  26. text_input_set_result_callback(nfc_playlist->text_input, nfc_playlist_file_rename_menu_callback, nfc_playlist, nfc_playlist->text_input_output, 50, true);
  27. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistScene_FileRename);
  28. }
  29. bool nfc_playlist_file_rename_scene_on_event(void* context, SceneManagerEvent event) {
  30. UNUSED(context);
  31. UNUSED(event);
  32. return false;
  33. }
  34. void nfc_playlist_file_rename_scene_on_exit(void* context) {
  35. NfcPlaylist* nfc_playlist = context;
  36. text_input_reset(nfc_playlist->text_input);
  37. free(nfc_playlist->text_input_output);
  38. }