From ba7f2a399b6c0df6805d25b37bcab266d2020acc Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Thu, 23 Nov 2017 18:03:46 +0200 Subject: 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 --- desktop-widgets/printoptions.cpp | 46 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) (limited to 'desktop-widgets/printoptions.cpp') 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(); -- cgit v1.2.3-70-g09d2