summaryrefslogtreecommitdiffstats
path: root/desktop-widgets/templatelayout.cpp
diff options
context:
space:
mode:
authorGravatar Lubomir I. Ivanov <neolit123@gmail.com>2017-11-24 22:54:54 +0200
committerGravatar Lubomir I. Ivanov <neolit123@gmail.com>2017-11-24 23:06:52 +0200
commit65f0600679a72d13b64b20c575c05b5313a80635 (patch)
tree65af0a1b9bf6f619f252ad756956c3c9bab71078 /desktop-widgets/templatelayout.cpp
parentfc48cde77c98385c69bc4d951de6867cf2f652a0 (diff)
downloadsubsurface-65f0600679a72d13b64b20c575c05b5313a80635.tar.gz
printing: update the coping of bundled templates
This update includes: - Instead of copyPath() use a new specialized function: copy_bundled_templates() - The new function supports overwriting of templates in the user path, but only if a template file is read-only - If the file is RW create a backup of the file in the form of: <file-name>-User.html - Collect backup files and store them in a QStringList which is then shown in a QMessageBox from MainWindow to notifying the user about the backup This change allows moving the maintenance of the bundled templates back to the application developers and contributors as currently the only one who can edit the templates in the user path was the user. Suggested-by: Dirk Hohndel <dirk@hohndel.org> Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Diffstat (limited to 'desktop-widgets/templatelayout.cpp')
-rw-r--r--desktop-widgets/templatelayout.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/desktop-widgets/templatelayout.cpp b/desktop-widgets/templatelayout.cpp
index ab9b3f09a..f3f3c8be2 100644
--- a/desktop-widgets/templatelayout.cpp
+++ b/desktop-widgets/templatelayout.cpp
@@ -62,6 +62,35 @@ void set_bundled_templates_as_read_only()
QFile::setPermissions(pathUser + QDir::separator() + f, QFileDevice::ReadOwner | QFileDevice::ReadUser);
}
+void copy_bundled_templates(QString src, QString dst, QStringList *templateBackupList)
+{
+ QDir dir(src);
+ if (!dir.exists())
+ return;
+ foreach (QString d, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
+ QString dst_path = dst + QDir::separator() + d;
+ dir.mkpath(dst_path);
+ copy_bundled_templates(src + QDir::separator() + d, dst_path, templateBackupList);
+ }
+ foreach (QString f, dir.entryList(QDir::Files)) {
+ QFile fileSrc(src + QDir::separator() + f);
+ QFile fileDest(dst + QDir::separator() + f);
+ if (fileDest.exists()) {
+ // if open() fails the file is either locked or r/o. try to remove it and then overwrite
+ if (!fileDest.open(QFile::ReadWrite | QFile::Text)) {
+ fileDest.setPermissions(QFileDevice::WriteOwner | QFileDevice::WriteUser);
+ fileDest.remove();
+ } else { // if the file is not read-only create a backup
+ fileDest.close();
+ const QString targetFile = fileDest.fileName().replace(".html", "-User.html");
+ fileDest.copy(targetFile);
+ *templateBackupList << targetFile;
+ }
+ }
+ fileSrc.copy(fileDest.fileName()); // in all cases copy the file
+ }
+}
+
TemplateLayout::TemplateLayout(print_options *PrintOptions, template_options *templateOptions) :
m_engine(NULL)
{