diff options
author | Lubomir I. Ivanov <neolit123@gmail.com> | 2017-11-23 18:03:46 +0200 |
---|---|---|
committer | Lubomir I. Ivanov <neolit123@gmail.com> | 2017-11-24 22:27:30 +0200 |
commit | ba7f2a399b6c0df6805d25b37bcab266d2020acc (patch) | |
tree | f0f060ab712b71a1105d327730170cdfe443ab1a /desktop-widgets/printoptions.cpp | |
parent | c57df085a4dc9e4b679dfa5f2d834c3ecf5f8f63 (diff) | |
download | subsurface-ba7f2a399b6c0df6805d25b37bcab266d2020acc.tar.gz |
printing: handle overwriting in import / export
Show an error message if trying to:
1) Import over an existing read-only template with the same name
2) Export to a read-only file with the same name
3) Delete a read-only template
Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Diffstat (limited to 'desktop-widgets/printoptions.cpp')
-rw-r--r-- | desktop-widgets/printoptions.cpp | 46 |
1 files changed, 43 insertions, 3 deletions
diff --git a/desktop-widgets/printoptions.cpp b/desktop-widgets/printoptions.cpp index 4b1deabca..6e45bc5e1 100644 --- a/desktop-widgets/printoptions.cpp +++ b/desktop-widgets/printoptions.cpp @@ -149,7 +149,23 @@ void PrintOptions::on_importButton_clicked() if (filename.isEmpty()) return; QFileInfo fileInfo(filename); - QFile::copy(filename, pathUser + QDir::separator() + fileInfo.fileName()); + + const QString dest = pathUser + QDir::separator() + fileInfo.fileName(); + QFile f(dest); + if (!f.open(QFile::ReadWrite | QFile::Text)) { + QMessageBox msgBox(this); + msgBox.setWindowTitle(tr("Read-only template!")); + msgBox.setText(tr("The destination template '%1' is read-only and cannot be overwritten.").arg(fileInfo.fileName())); + msgBox.setStandardButtons(QMessageBox::Ok); + msgBox.exec(); + return; + } else { + f.close(); + if (filename != dest) + f.remove(); + } + + QFile::copy(filename, dest); printOptions->p_template = fileInfo.fileName(); lastImportExportTemplate = fileInfo.fileName(); find_all_templates(); @@ -168,13 +184,28 @@ void PrintOptions::on_exportButton_clicked() filename += "l"; else if (!filename.endsWith(ext, Qt::CaseInsensitive)) filename += ext; - QFile::copy(pathUser + QDir::separator() + getSelectedTemplate(), filename); + QFileInfo fileInfo(filename); + const QString dest = pathUser + QDir::separator() + getSelectedTemplate(); + QFile f(filename); + if (!f.open(QFile::ReadWrite | QFile::Text)) { + QMessageBox msgBox(this); + msgBox.setWindowTitle(tr("Read-only template!")); + msgBox.setText(tr("The destination template '%1' is read-only and cannot be overwritten.").arg(fileInfo.fileName())); + msgBox.setStandardButtons(QMessageBox::Ok); + msgBox.exec(); + return; + } else { + f.close(); + if (dest != filename) + f.remove(); + } + + QFile::copy(dest, filename); if (!f.open(QFile::ReadWrite | QFile::Text)) f.setPermissions(QFileDevice::ReadUser | QFileDevice::ReadOwner | QFileDevice::WriteUser | QFileDevice::WriteOwner); else f.close(); - QFileInfo fileInfo(filename); lastImportExportTemplate = fileInfo.fileName(); find_all_templates(); setup(); @@ -190,6 +221,15 @@ void PrintOptions::on_deleteButton_clicked() msgBox.setDefaultButton(QMessageBox::Cancel); if (msgBox.exec() == QMessageBox::Ok) { QFile f(getPrintingTemplatePathUser() + QDir::separator() + templateName); + if (!f.open(QFile::ReadWrite | QFile::Text)) { + msgBox.setWindowTitle(tr("Read-only template!")); + msgBox.setText(tr("The template '%1' is read-only and cannot be deleted.").arg(templateName)); + msgBox.setStandardButtons(QMessageBox::Ok); + msgBox.exec(); + return; + } else { + f.close(); + } f.remove(); find_all_templates(); setup(); |