From 9209382c1868045baf49da42c5700da43556a49f Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Thu, 23 Nov 2017 01:59:26 +0200 Subject: printing: add set_bundled_templates_as_read_only() Add the function set_bundled_templates_as_read_only() in templatelayout.cpp/h. The function is used to mark the bundled template files as read-only in the user folder. It is called in mainwindow.cpp, after the files are copied from the bundle. Signed-off-by: Lubomir I. Ivanov --- desktop-widgets/mainwindow.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'desktop-widgets/mainwindow.cpp') diff --git a/desktop-widgets/mainwindow.cpp b/desktop-widgets/mainwindow.cpp index f685a2b74..05314af1d 100644 --- a/desktop-widgets/mainwindow.cpp +++ b/desktop-widgets/mainwindow.cpp @@ -258,6 +258,7 @@ MainWindow::MainWindow() : QMainWindow(), #ifndef NO_PRINTING // copy the bundled print templates to the user path; no overwriting occurs! copyPath(getPrintingTemplatePathBundle(), getPrintingTemplatePathUser()); + set_bundled_templates_as_read_only(); find_all_templates(); #endif -- cgit v1.2.3-70-g09d2 From 65f0600679a72d13b64b20c575c05b5313a80635 Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Fri, 24 Nov 2017 22:54:54 +0200 Subject: 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: -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 Signed-off-by: Lubomir I. Ivanov --- desktop-widgets/mainwindow.cpp | 19 +++++++++++++++++-- desktop-widgets/templatelayout.cpp | 29 +++++++++++++++++++++++++++++ desktop-widgets/templatelayout.h | 2 ++ 3 files changed, 48 insertions(+), 2 deletions(-) (limited to 'desktop-widgets/mainwindow.cpp') diff --git a/desktop-widgets/mainwindow.cpp b/desktop-widgets/mainwindow.cpp index 05314af1d..e5739bd47 100644 --- a/desktop-widgets/mainwindow.cpp +++ b/desktop-widgets/mainwindow.cpp @@ -256,8 +256,23 @@ MainWindow::MainWindow() : QMainWindow(), connect(geoLookup, SIGNAL(started()),information(), SLOT(disableGeoLookupEdition())); connect(geoLookup, SIGNAL(finished()), information(), SLOT(enableGeoLookupEdition())); #ifndef NO_PRINTING - // copy the bundled print templates to the user path; no overwriting occurs! - copyPath(getPrintingTemplatePathBundle(), getPrintingTemplatePathUser()); + // copy the bundled print templates to the user path + QStringList templateBackupList; + QString templatePathUser(getPrintingTemplatePathUser()); + copy_bundled_templates(getPrintingTemplatePathBundle(), templatePathUser, &templateBackupList); + if (templateBackupList.length()) { + QMessageBox msgBox(this); + templatePathUser.replace("\\", "/"); + templateBackupList.replaceInStrings(templatePathUser + "/", ""); + msgBox.setWindowTitle(tr("Template backup created")); + msgBox.setText(tr("The following backup printing templates were created:\n\n%1\n\n" + "Location:\n%2\n\n" + "Please note that as of this version of Subsurface the default templates\n" + "are read-only and should not be edited directly, since the application\n" + "can overwrite them on startup.").arg(templateBackupList.join("\n")).arg(templatePathUser)); + msgBox.setStandardButtons(QMessageBox::Ok); + msgBox.exec(); + } set_bundled_templates_as_read_only(); find_all_templates(); #endif 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) { diff --git a/desktop-widgets/templatelayout.h b/desktop-widgets/templatelayout.h index 8ec4eadc7..cb60cc03d 100644 --- a/desktop-widgets/templatelayout.h +++ b/desktop-widgets/templatelayout.h @@ -2,6 +2,7 @@ #ifndef TEMPLATELAYOUT_H #define TEMPLATELAYOUT_H +#include #include #include "mainwindow.h" #include "printoptions.h" @@ -13,6 +14,7 @@ int getTotalWork(print_options *printOptions); void find_all_templates(); void set_bundled_templates_as_read_only(); +void copy_bundled_templates(QString src, QString dst, QStringList *templateBackupList); extern QList grantlee_templates, grantlee_statistics_templates; -- cgit v1.2.3-70-g09d2