Skip to content

Commit

Permalink
added checks to prevent a save as or export overwriting the currently…
Browse files Browse the repository at this point in the history
… open document (as t as unsupported (#2504)
  • Loading branch information
giuspen committed May 10, 2024
1 parent 79cb886 commit 9226688
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
31 changes: 19 additions & 12 deletions src/ct/ct_actions_export.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,26 @@ void CtActions::export_to_ct()
fileSelArgs.curr_folder = currDocFilepath.parent_path();
}
std::string new_filepath;
if (CtDocType::MultiFile == storageSelArgs.ctDocType) {
new_filepath = CtDialogs::folder_save_as_dialog(_pCtMainWin, fileSelArgs);
}
else {
fileSelArgs.filter_name = _("CherryTree File");
fileSelArgs.filter_pattern.push_back(std::string{CtConst::CHAR_STAR}+fileExtension);
fileSelArgs.overwrite_confirmation = false; // as not supported for the multifile, we do in both cases elsewhere
new_filepath = CtDialogs::file_save_as_dialog(_pCtMainWin, fileSelArgs);
}
if (new_filepath.empty()) {
return;
do {
if (CtDocType::MultiFile == storageSelArgs.ctDocType) {
new_filepath = CtDialogs::folder_save_as_dialog(_pCtMainWin, fileSelArgs);
}
else {
fileSelArgs.filter_name = _("CherryTree File");
fileSelArgs.filter_pattern.push_back(std::string{CtConst::CHAR_STAR}+fileExtension);
fileSelArgs.overwrite_confirmation = false; // as not supported for the multifile, we do in both cases elsewhere
new_filepath = CtDialogs::file_save_as_dialog(_pCtMainWin, fileSelArgs);
}
if (new_filepath.empty()) {
return;
}
CtMiscUtil::filepath_extension_fix(storageSelArgs.ctDocType, storageSelArgs.ctDocEncrypt, new_filepath);
if (currDocFilepath == new_filepath) {
CtDialogs::warning_dialog(_("The file/folder in use cannot be overwritten!\nPlease use a different name or location."), *_pCtMainWin);
}
}
CtMiscUtil::filepath_extension_fix(storageSelArgs.ctDocType, storageSelArgs.ctDocEncrypt, new_filepath);
while (currDocFilepath == new_filepath);

if (Glib::file_test(new_filepath, Glib::FILE_TEST_EXISTS)) {
// overwrite confirmation here
std::string message;
Expand Down
34 changes: 20 additions & 14 deletions src/ct/ct_actions_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void CtActions::file_save_as()
}
CtDialogs::CtStorageSelectArgs storageSelArgs{};
storageSelArgs.showAutosaveOptions = true;
fs::path currDocFilepath = _pCtMainWin->get_ct_storage()->get_file_path();
const fs::path currDocFilepath = _pCtMainWin->get_ct_storage()->get_file_path();
if (not currDocFilepath.empty()) {
storageSelArgs.ctDocType = fs::get_doc_type_from_file_ext(currDocFilepath);
storageSelArgs.ctDocEncrypt = fs::get_doc_encrypt_from_file_ext(currDocFilepath);
Expand All @@ -75,20 +75,26 @@ void CtActions::file_save_as()
fileSelArgs.curr_file_name = suggested_basename.stem() + CtMiscUtil::get_doc_extension(storageSelArgs.ctDocType, storageSelArgs.ctDocEncrypt);
}
std::string filepath;
if (CtDocType::MultiFile == storageSelArgs.ctDocType) {
filepath = CtDialogs::folder_save_as_dialog(_pCtMainWin, fileSelArgs);
}
else {
fileSelArgs.filter_name = _("CherryTree File");
std::string fileExtension = CtMiscUtil::get_doc_extension(storageSelArgs.ctDocType, storageSelArgs.ctDocEncrypt);
fileSelArgs.filter_pattern.push_back(std::string{CtConst::CHAR_STAR}+fileExtension);
fileSelArgs.overwrite_confirmation = false; // as not supported for the multifile, we do in both cases elsewhere
filepath = CtDialogs::file_save_as_dialog(_pCtMainWin, fileSelArgs);
}
if (filepath.empty()) {
return;
do {
if (CtDocType::MultiFile == storageSelArgs.ctDocType) {
filepath = CtDialogs::folder_save_as_dialog(_pCtMainWin, fileSelArgs);
}
else {
fileSelArgs.filter_name = _("CherryTree File");
std::string fileExtension = CtMiscUtil::get_doc_extension(storageSelArgs.ctDocType, storageSelArgs.ctDocEncrypt);
fileSelArgs.filter_pattern.push_back(std::string{CtConst::CHAR_STAR}+fileExtension);
fileSelArgs.overwrite_confirmation = false; // as not supported for the multifile, we do in both cases elsewhere
filepath = CtDialogs::file_save_as_dialog(_pCtMainWin, fileSelArgs);
}
if (filepath.empty()) {
return;
}
CtMiscUtil::filepath_extension_fix(storageSelArgs.ctDocType, storageSelArgs.ctDocEncrypt, filepath);
if (currDocFilepath == filepath) {
CtDialogs::warning_dialog(_("The file/folder in use cannot be overwritten!\nPlease use a different name or location."), *_pCtMainWin);
}
}
CtMiscUtil::filepath_extension_fix(storageSelArgs.ctDocType, storageSelArgs.ctDocEncrypt, filepath);
while (currDocFilepath == filepath);
if (Glib::file_test(filepath, Glib::FILE_TEST_EXISTS)) {
// overwrite confirmation here
std::string message;
Expand Down

0 comments on commit 9226688

Please sign in to comment.