[PATCH 1/2] Print: copy the bundled templates to a safe location

Lubomir I. Ivanov neolit123 at gmail.com
Sun Oct 18 14:25:14 PDT 2015


From: "Lubomir I. Ivanov" <neolit123 at gmail.com>

This patch adds couple of helpers to retrieve the template path in
the application bundle (getPrintingTemplatePathBundle())
and the template path in the user directory
(getPrintingTemplatePathUser()).

Once the print dialog is initiated for the first time the contents
of the bundled template path are copied to the user template path
using copyPath(). No overwriting of files will occur.

The PrintOptions and TemplateLayout classes then only use
the user path for retrieving templates.

Fixes an issue where the bundled templates can be locked as read-only
on OSX and Linux.

Signed-off-by: Lubomir I. Ivanov <neolit123 at gmail.com>
---
Dirk, this is following your original suggestion to simply copy the files.
Granlee's loader makes it complicated for me to figure out how not-to-load
a file which exists in another path with the same name.

I would have appreciated a simple Grantlee::Engine::loadTemplateFromPath()
instead of the Grantlee::FileSystemTemplateLoader::setTemplateDirs() and
Grantlee::Engine::loadByName().

I was thinking of approaching the issue by prefixing the bundled templates
and/or creating a $TEMP/priting_templates and only copying there files
of relevance on runtime but went against that as it's a bit of a mess.

So, yeah - simply copying the files is the cheapest solution, but it
works for the DMG and AppImage as a bugfix!

In any case, we don't support merges if the bundled templates, somehow
change in format and also it becomes a user responsibility to backup his
templates the same way he/she backups his/her default log file in the
user directory.
---
 helpers.h              |  3 +++
 qt-ui/mainwindow.cpp   |  2 ++
 qt-ui/printoptions.cpp |  6 +++---
 qthelper.cpp           | 32 ++++++++++++++++++++++++++++++++
 templatelayout.cpp     | 12 ++++++------
 5 files changed, 46 insertions(+), 9 deletions(-)

diff --git a/helpers.h b/helpers.h
index 6c5c31c..76c6ac9 100644
--- a/helpers.h
+++ b/helpers.h
@@ -27,6 +27,9 @@ void set_default_dive_computer(const char *vendor, const char *product);
 void set_default_dive_computer_device(const char *name);
 void set_default_dive_computer_download_mode(int downloadMode);
 QString getSubsurfaceDataPath(QString folderToFind);
+QString getPrintingTemplatePathUser();
+QString getPrintingTemplatePathBundle();
+void copyPath(QString src, QString dst);
 extern const QString get_dc_nickname(const char *model, uint32_t deviceid);
 int gettimezoneoffset(timestamp_t when = 0);
 int parseTemperatureToMkelvin(const QString &text);
diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp
index d28c1c6..580190c 100644
--- a/qt-ui/mainwindow.cpp
+++ b/qt-ui/mainwindow.cpp
@@ -236,6 +236,8 @@ 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());
 	find_all_templates();
 #endif
 
diff --git a/qt-ui/printoptions.cpp b/qt-ui/printoptions.cpp
index 79bcee1..769c89f 100644
--- a/qt-ui/printoptions.cpp
+++ b/qt-ui/printoptions.cpp
@@ -152,7 +152,7 @@ void PrintOptions::on_importButton_clicked()
 	if (filename.isEmpty())
 		return;
 	QFileInfo fileInfo(filename);
-	QFile::copy(filename, getSubsurfaceDataPath("printing_templates") + QDir::separator() + fileInfo.fileName());
+	QFile::copy(filename, getPrintingTemplatePathUser() + QDir::separator() + fileInfo.fileName());
 	printOptions->p_template = fileInfo.fileName();
 	find_all_templates();
 	setup();
@@ -164,7 +164,7 @@ void PrintOptions::on_exportButton_clicked()
 							tr("HTML files (*.html)"));
 	if (filename.isEmpty())
 		return;
-	QFile::copy(getSubsurfaceDataPath("printing_templates") + QDir::separator() + getSelectedTemplate(), filename);
+	QFile::copy(getPrintingTemplatePathUser() + QDir::separator() + getSelectedTemplate(), filename);
 }
 
 void PrintOptions::on_deleteButton_clicked()
@@ -176,7 +176,7 @@ void PrintOptions::on_deleteButton_clicked()
 	msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
 	msgBox.setDefaultButton(QMessageBox::Cancel);
 	if (msgBox.exec() == QMessageBox::Ok) {
-		QFile f(getSubsurfaceDataPath("printing_templates") + QDir::separator() + templateName);
+		QFile f(getPrintingTemplatePathUser() + QDir::separator() + templateName);
 		f.remove();
 		find_all_templates();
 		setup();
diff --git a/qthelper.cpp b/qthelper.cpp
index c74dba6..1b61d4a 100644
--- a/qthelper.cpp
+++ b/qthelper.cpp
@@ -942,6 +942,38 @@ QString getSubsurfaceDataPath(QString folderToFind)
 	return QString("");
 }
 
+static const char *printing_templates = "printing_templates";
+
+QString getPrintingTemplatePathUser()
+{
+	static QString path = QString();
+	if (path.isEmpty())
+		path = QString(system_default_directory()) + QDir::separator() + QString(printing_templates);
+	return path;
+}
+
+QString getPrintingTemplatePathBundle()
+{
+	static QString path = QString();
+	if (path.isEmpty())
+		path = getSubsurfaceDataPath(printing_templates);
+	return path;
+}
+
+void copyPath(QString src, QString dst)
+{
+	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);
+		copyPath(src + QDir::separator() + d, dst_path);
+	}
+	foreach (QString f, dir.entryList(QDir::Files))
+		QFile::copy(src + QDir::separator() + f, dst + QDir::separator() + f);
+}
+
 int gettimezoneoffset(timestamp_t when)
 {
 	QDateTime dt1, dt2;
diff --git a/templatelayout.cpp b/templatelayout.cpp
index 161200d..a376459 100644
--- a/templatelayout.cpp
+++ b/templatelayout.cpp
@@ -25,7 +25,7 @@ void find_all_templates()
 {
 	grantlee_templates.clear();
 	grantlee_statistics_templates.clear();
-	QDir dir(getSubsurfaceDataPath("printing_templates"));
+	QDir dir(getPrintingTemplatePathUser());
 	QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
 	foreach (QFileInfo finfo, list) {
 		QString filename = finfo.fileName();
@@ -34,7 +34,7 @@ void find_all_templates()
 		}
 	}
 	// find statistics templates
-	dir.setPath(getSubsurfaceDataPath("printing_templates") + QDir::separator() + "statistics");
+	dir.setPath(getPrintingTemplatePathUser() + QDir::separator() + "statistics");
 	list = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
 	foreach (QFileInfo finfo, list) {
 		QString filename = finfo.fileName();
@@ -66,7 +66,7 @@ QString TemplateLayout::generate()
 
 	QSharedPointer<Grantlee::FileSystemTemplateLoader> m_templateLoader =
 		QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader());
-	m_templateLoader->setTemplateDirs(QStringList() << getSubsurfaceDataPath("printing_templates"));
+	m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser());
 	m_engine->addTemplateLoader(m_templateLoader);
 
 	Grantlee::registerMetaType<Dive>();
@@ -113,7 +113,7 @@ QString TemplateLayout::generateStatistics()
 
 	QSharedPointer<Grantlee::FileSystemTemplateLoader> m_templateLoader =
 		QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader());
-	m_templateLoader->setTemplateDirs(QStringList() << getSubsurfaceDataPath("printing_templates/statistics"));
+	m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser() + QDir::separator() + QString("statistics"));
 	m_engine->addTemplateLoader(m_templateLoader);
 
 	Grantlee::registerMetaType<YearInfo>();
@@ -153,7 +153,7 @@ QString TemplateLayout::generateStatistics()
 
 QString TemplateLayout::readTemplate(QString template_name)
 {
-	QFile qfile(getSubsurfaceDataPath("printing_templates") + QDir::separator() + template_name);
+	QFile qfile(getPrintingTemplatePathUser() + QDir::separator() + template_name);
 	if (qfile.open(QFile::ReadOnly | QFile::Text)) {
 		QTextStream in(&qfile);
 		return in.readAll();
@@ -163,7 +163,7 @@ QString TemplateLayout::readTemplate(QString template_name)
 
 void TemplateLayout::writeTemplate(QString template_name, QString grantlee_template)
 {
-	QFile qfile(getSubsurfaceDataPath("printing_templates") + QDir::separator() + template_name);
+	QFile qfile(getPrintingTemplatePathUser() + QDir::separator() + template_name);
 	if (qfile.open(QFile::ReadWrite | QFile::Text)) {
 		qfile.write(grantlee_template.toUtf8().data());
 		qfile.resize(qfile.pos());
-- 
1.7.11.msysgit.0



More information about the subsurface mailing list