Move "do you want to overwrite this file?" question inside the FileSelector

Native file dialogs ask this same question too (also they check for
read-only attribute).
This commit is contained in:
David Capello 2015-05-28 16:11:06 -03:00
parent 71efdfdf76
commit 2a5c93e333
9 changed files with 51 additions and 116 deletions

View File

@ -388,22 +388,14 @@ private:
} }
void onExport() { void onExport() {
again: std::string filename = app::show_file_selector(
std::string filename = app::show_file_selector("Export Keyboard Shortcuts", "", "Export Keyboard Shortcuts", "",
KEYBOARD_FILENAME_EXTENSION, FileSelectorType::Save); KEYBOARD_FILENAME_EXTENSION, FileSelectorType::Save);
if (!filename.empty()) { if (filename.empty())
if (base::is_file(filename)) {
int ret = Alert::show("Warning<<File exists, overwrite it?<<%s||&Yes||&No||&Cancel",
base::get_file_name(filename).c_str());
if (ret == 2)
goto again;
else if (ret != 1)
return; return;
}
app::KeyboardShortcuts::instance()->exportFile(filename.c_str()); app::KeyboardShortcuts::instance()->exportFile(filename.c_str());
} }
}
void onReset() { void onReset() {
if (Alert::show("Warning" if (Alert::show("Warning"

View File

@ -134,45 +134,15 @@ void SaveFileBaseCommand::saveAsDialog(const ContextReader& reader, const char*
filename = m_filename; filename = m_filename;
} }
else { else {
std::string exts = get_writable_extensions();
filename = document->filename(); filename = document->filename();
std::string exts = get_writable_extensions();
for (;;) {
std::string newfilename = app::show_file_selector( std::string newfilename = app::show_file_selector(
dlgTitle, filename, exts, FileSelectorType::Save); dlgTitle, filename, exts, FileSelectorType::Save);
if (newfilename.empty()) if (newfilename.empty())
return; return;
filename = newfilename; filename = newfilename;
// Ask if the user wants overwrite the existent file.
int ret = 0;
if (base::is_file(filename)) {
ret = ui::Alert::show("Warning<<The file already exists, overwrite it?<<%s||&Yes||&No||&Cancel",
base::get_file_name(filename).c_str());
// Check for read-only attribute.
if (ret == 1) {
if (!confirmReadonly(filename))
ret = 2; // Select file again.
else
break;
}
}
else
break;
// "yes": we must continue with the operation...
if (ret == 1) {
break;
}
// "cancel" or <esc> per example: we back doing nothing
else if (ret != 2)
return;
// "no": we must back to select other file-name
}
} }
std::string oldFilename; std::string oldFilename;
@ -202,23 +172,6 @@ void SaveFileBaseCommand::saveAsDialog(const ContextReader& reader, const char*
} }
} }
//static
bool SaveFileBaseCommand::confirmReadonly(const std::string& filename)
{
if (!base::has_readonly_attr(filename))
return true;
int ret = ui::Alert::show("Warning<<The file is read-only, do you really want to overwrite it?<<%s||&Yes||&No",
base::get_file_name(filename).c_str());
if (ret == 1) {
base::remove_readonly_attr(filename);
return true;
}
else
return false;
}
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
class SaveFileCommand : public SaveFileBaseCommand { class SaveFileCommand : public SaveFileBaseCommand {
@ -248,9 +201,6 @@ void SaveFileCommand::onExecute(Context* context)
ContextWriter writer(reader); ContextWriter writer(reader);
Document* documentWriter = writer.document(); Document* documentWriter = writer.document();
if (!confirmReadonly(documentWriter->filename()))
return;
save_document_in_background(context, documentWriter, true, save_document_in_background(context, documentWriter, true,
m_filenameFormat.c_str()); m_filenameFormat.c_str());
} }

View File

@ -30,8 +30,6 @@ namespace app {
void saveAsDialog(const ContextReader& reader, const char* dlgTitle, bool markAsSaved); void saveAsDialog(const ContextReader& reader, const char* dlgTitle, bool markAsSaved);
static bool confirmReadonly(const std::string& filename);
std::string m_filename; std::string m_filename;
std::string m_filenameFormat; std::string m_filenameFormat;
std::string m_selectedFilename; std::string m_selectedFilename;

View File

@ -48,33 +48,12 @@ void SaveMaskCommand::onExecute(Context* context)
const ContextReader reader(context); const ContextReader reader(context);
const Document* document(reader.document()); const Document* document(reader.document());
std::string filename = "default.msk"; std::string filename = "default.msk";
int ret;
for (;;) {
filename = app::show_file_selector( filename = app::show_file_selector(
"Save .msk File", filename, "msk", FileSelectorType::Save); "Save .msk File", filename, "msk", FileSelectorType::Save);
if (filename.empty()) if (filename.empty())
return; return;
// Does the file exist?
if (base::is_file(filename.c_str())) {
/* ask if the user wants overwrite the file? */
ret = ui::Alert::show("Warning<<File exists, overwrite it?<<%s||&Yes||&No||&Cancel",
base::get_file_name(filename).c_str());
}
else
break;
/* "yes": we must continue with the operation... */
if (ret == 1)
break;
/* "cancel" or <esc> per example: we back doing nothing */
else if (ret != 2)
return;
/* "no": we must back to select other file-name */
}
if (save_msk_file(document->mask(), filename.c_str()) != 0) if (save_msk_file(document->mask(), filename.c_str()) != 0)
ui::Alert::show("Error<<Error saving .msk file<<%s||&Close", filename.c_str()); ui::Alert::show("Error<<Error saving .msk file<<%s||&Close", filename.c_str());
} }

View File

@ -60,23 +60,10 @@ void SavePaletteCommand::onExecute(Context* context)
} }
else { else {
std::string exts = get_writable_palette_extensions(); std::string exts = get_writable_palette_extensions();
int ret;
again:
filename = app::show_file_selector("Save Palette", "", exts, filename = app::show_file_selector("Save Palette", "", exts,
FileSelectorType::Save); FileSelectorType::Save);
if (filename.empty()) if (filename.empty())
return; return;
if (base::is_file(filename)) {
ret = Alert::show("Warning<<File exists, overwrite it?<<%s||&Yes||&No||&Cancel",
base::get_file_name(filename).c_str());
if (ret == 2)
goto again;
else if (ret != 1)
return;
}
} }
if (!save_palette(filename.c_str(), palette)) if (!save_palette(filename.c_str(), palette))

View File

@ -64,7 +64,10 @@ std::string show_file_selector(const std::string& title,
} }
FileSelector fileSelector; FileSelector fileSelector;
return fileSelector.show(title, initialPath, showExtensions); return fileSelector.show(
title, initialPath, showExtensions,
(type == FileSelectorType::Open ? FileSelector::Open:
FileSelector::Save));
} }
} // namespace app } // namespace app

View File

@ -328,9 +328,11 @@ void FileSelector::goInsideFolder()
} }
} }
std::string FileSelector::show(const std::string& title, std::string FileSelector::show(
const std::string& title,
const std::string& initialPath, const std::string& initialPath,
const std::string& showExtensions) const std::string& showExtensions,
Type type)
{ {
std::string result; std::string result;
@ -515,6 +517,29 @@ again:
buf += m_fileType->getItemText(m_fileType->getSelectedItemIndex()); buf += m_fileType->getItemText(m_fileType->getSelectedItemIndex());
} }
if (type == Save && base::is_file(buf)) {
int ret = Alert::show("Warning<<File exists, overwrite it?<<%s||&Yes||&No||&Cancel",
base::get_file_name(buf).c_str());
if (ret == 2) {
setVisible(true);
goto again;
}
else if (ret == 1) {
// Check for read-only attribute
if (base::has_readonly_attr(buf)) {
ui::Alert::show(
"Problem<<The selected file is read-only. Try with other file.||&Go back");
setVisible(true);
goto again;
}
}
// Cancel
else if (ret != 1) {
return "";
}
}
// duplicate the buffer to return a new string // duplicate the buffer to return a new string
result = buf; result = buf;

View File

@ -27,6 +27,8 @@ namespace app {
class FileSelector : public ui::Window { class FileSelector : public ui::Window {
public: public:
enum Type { Open, Save };
FileSelector(); FileSelector();
void goBack(); void goBack();
@ -37,7 +39,8 @@ namespace app {
// Shows the dialog to select a file in the program. // Shows the dialog to select a file in the program.
std::string show(const std::string& title, std::string show(const std::string& title,
const std::string& initialPath, const std::string& initialPath,
const std::string& showExtensions); const std::string& showExtensions,
Type type);
private: private:
void updateLocation(); void updateLocation();

View File

@ -93,10 +93,8 @@ public:
if (!m_save) if (!m_save)
ofn.Flags |= OFN_FILEMUSTEXIST; ofn.Flags |= OFN_FILEMUSTEXIST;
#if 0 // This is asked in custom UI
else else
ofn.Flags |= OFN_OVERWRITEPROMPT; ofn.Flags |= OFN_OVERWRITEPROMPT;
#endif
BOOL res; BOOL res;
if (m_save) if (m_save)