From 1139e4a16a76d5de64c87b4075ac570914ef192f Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 29 Jul 2023 00:43:28 +0200 Subject: Ctrl+Shift+S: Create a new name for file instead of replacing existing one --- src/QuickMedia.cpp | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 3a42906..86d4f8f 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -7992,6 +7992,33 @@ namespace QuickMedia { } } + static std::string get_unique_filename(Path directory, Path filename) { + Path full_filepath = directory; + full_filepath.join(filename); + if(get_file_type(full_filepath) == FileType::FILE_NOT_FOUND) + return full_filepath.data; + + for(int i = 1; i < 100; ++i) { + std::string filename_unique = filename.filename_no_ext() + " (" + std::to_string(i) + ")" + filename.ext(); + Path full_filepath = directory; + full_filepath.join(filename_unique); + if(get_file_type(full_filepath) == FileType::FILE_NOT_FOUND) + return full_filepath.data; + } + + full_filepath = directory; + char buffer[10]; + if(generate_random_characters(buffer, sizeof(buffer), "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 62)) { + std::string new_filename = filename.filename_no_ext() + " "; + new_filename.append(buffer, sizeof(buffer)); + new_filename += filename.ext(); + full_filepath.join(new_filename); + } else { + full_filepath.join(filename); + } + return full_filepath.data; + } + void Program::download_page(std::string url, std::string download_filename, bool no_dialog) { window.set_title(("QuickMedia - Select where you want to save " + std::string(url)).c_str()); @@ -8087,7 +8114,8 @@ namespace QuickMedia { output_filepath = get_config().download.music_directory; else output_filepath = get_config().download.file_directory; - output_filepath += "/" + filename; + + output_filepath = get_unique_filename(output_filepath, filename); } else { window.set_visible(true); output_filepath = file_save_page(filename); -- cgit v1.2.3