From 894e7d5d39b8e926d06a10e7eb70300cb93bd374 Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Mon, 19 Oct 2015 00:25:14 +0300 Subject: 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 Signed-off-by: Dirk Hohndel --- 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 6c5c31c7d..76c6ac9cd 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 d28c1c699..580190caa 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 79bcee158..769c89ff4 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 c74dba647..1b61d4a26 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 161200d62..a376459a6 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 m_templateLoader = QSharedPointer(new Grantlee::FileSystemTemplateLoader()); - m_templateLoader->setTemplateDirs(QStringList() << getSubsurfaceDataPath("printing_templates")); + m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser()); m_engine->addTemplateLoader(m_templateLoader); Grantlee::registerMetaType(); @@ -113,7 +113,7 @@ QString TemplateLayout::generateStatistics() QSharedPointer m_templateLoader = QSharedPointer(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(); @@ -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()); -- cgit v1.2.3-70-g09d2