file_rename.c 2.1 KB

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