file_rename.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "nfc_playlist.h"
  2. #include "scences/file_rename.h"
  3. void nfc_playlist_file_rename_menu_callback(void* context) {
  4. NfcPlaylist* nfc_playlist = context;
  5. Storage* storage = furi_record_open(RECORD_STORAGE);
  6. FuriString* 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. int file_path_size = (strlen(old_file_path) - strlen(old_file_name) + 1);
  10. char* file_path = (char*)malloc(file_path_size);
  11. snprintf(file_path, file_path_size, "%s", old_file_path);
  12. furi_string_printf(new_file_path, "%s%s.txt", file_path, nfc_playlist->playlist_name);
  13. if (!storage_file_exists(storage, furi_string_get_cstr(new_file_path))) {
  14. storage_common_rename_safe(storage, furi_string_get_cstr(nfc_playlist->settings.file_path), furi_string_get_cstr(new_file_path));
  15. nfc_playlist->settings.file_path = new_file_path;
  16. }
  17. free(file_path);
  18. free(nfc_playlist->playlist_name);
  19. furi_record_close(RECORD_STORAGE);
  20. furi_string_free(new_file_path);
  21. scene_manager_previous_scene(nfc_playlist->scene_manager);
  22. }
  23. void nfc_playlist_file_rename_scene_on_enter(void* context) {
  24. NfcPlaylist* nfc_playlist = context;
  25. nfc_playlist->playlist_name = (char*)malloc(50);
  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->playlist_name, 50, true);
  29. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_FileRename);
  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. }