Print: copy the bundled templates to a safe location

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@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Lubomir I. Ivanov 2015-10-19 00:25:14 +03:00 committed by Dirk Hohndel
parent dbd07af59f
commit 894e7d5d39
5 changed files with 46 additions and 9 deletions

View file

@ -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);

View file

@ -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

View file

@ -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();

View file

@ -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;

View file

@ -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());