Improve export file / dir validation

- Still ask before overwriting files.
 - Error out if file name exists, but is not a file.
 - Ask to create non-existing directories
 - Check if an expected directory either really
   is a directory or does not exist.
This commit is contained in:
Christian Hohnstaedt 2024-09-13 21:40:42 +02:00
parent 09a2924912
commit 7376cce5a7
2 changed files with 44 additions and 10 deletions

View File

@ -115,16 +115,50 @@ void ExportDialog::on_exportFormat_activated(int selected)
on_exportFormat_highlighted(selected);
}
bool ExportDialog::mayWriteFile(const QString &fname)
bool ExportDialog::mayWriteFile(const QString &fname, bool inSeparateFiles)
{
QFileInfo fi(fname);
if (fi.exists() && !fi.isDir()) {
xcaWarningBox msg(NULL,
tr("The file: '%1' already exists!").arg(fname));
msg.addButton(QMessageBox::Ok, tr("Overwrite"));
msg.addButton(QMessageBox::Cancel, tr("Do not overwrite"));
if (msg.exec() != QMessageBox::Ok)
return false;
QString dirname(fname);
if (!inSeparateFiles) {
if (fi.exists()) {
if (fi.isFile()) {
xcaWarningBox msg(NULL,
tr("The file: '%1' already exists!").arg(fname));
msg.addButton(QMessageBox::Ok, tr("Overwrite"));
msg.addButton(QMessageBox::Cancel, tr("Do not overwrite"));
if (msg.exec() != QMessageBox::Ok)
return false;
} else {
XCA_ERROR(tr("The path: '%1' exist, but is not a file")
.arg(nativeSeparator(fname)));
return false;
}
}
dirname = fi.path();
}
QFileInfo dir(dirname);
qDebug() << "Checking" << fname << dirname << "isDir" << dir.isDir() << "exists" << dir.exists();
if (dir.isDir())
return true;
if (dir.exists()) {
XCA_ERROR(tr("The path: '%1' exist, but is not a directory")
.arg(nativeSeparator(fname)));
return false;
}
xcaWarningBox msg(NULL,
tr("The directory: '%1' does not exist. Should it be created?")
.arg(nativeSeparator(dirname)));
msg.addButton(QMessageBox::Ok, tr("Create"));
msg.addButton(QMessageBox::Cancel);
if (msg.exec() != QMessageBox::Ok)
return false;
if (!QDir().mkpath(dirname)) {
xcaWarningBox msg(NULL, tr("Failed to create directory '%1'")
.arg(nativeSeparator(fname)));
msg.exec();
return false;
}
return true;
}
@ -142,7 +176,7 @@ void ExportDialog::accept()
reject();
return;
}
if (mayWriteFile(fn)) {
if (mayWriteFile(fn, separateFiles->isChecked())) {
update_workingdir(fn);
QDialog::accept();
}

View File

@ -29,8 +29,8 @@ class ExportDialog: public QDialog, public Ui::ExportDialog
QList<const pki_export*> types,
const QString &help_ctx = QString());
~ExportDialog();
static bool mayWriteFile(const QString &fname);
const pki_export *export_type(int idx = -1) const;
static bool mayWriteFile(const QString &fname, bool inSeparateFiles);
void setupExportFormat(int disable_flag);
public slots: