summaryrefslogtreecommitdiffstats
path: root/desktop-widgets/templatelayout.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'desktop-widgets/templatelayout.cpp')
-rw-r--r--desktop-widgets/templatelayout.cpp58
1 files changed, 55 insertions, 3 deletions
diff --git a/desktop-widgets/templatelayout.cpp b/desktop-widgets/templatelayout.cpp
index 9f933e91c..f3f3c8be2 100644
--- a/desktop-widgets/templatelayout.cpp
+++ b/desktop-widgets/templatelayout.cpp
@@ -1,4 +1,5 @@
// SPDX-License-Identifier: GPL-2.0
+#include <QFileDevice>
#include <string>
#include "templatelayout.h"
@@ -19,23 +20,74 @@ int getTotalWork(print_options *printOptions)
void find_all_templates()
{
+ const QString ext(".html");
grantlee_templates.clear();
grantlee_statistics_templates.clear();
QDir dir(getPrintingTemplatePathUser());
QStringList list = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
foreach (const QString& filename, list) {
- if (filename.at(filename.size() - 1) != '~') {
+ if (filename.at(filename.size() - 1) != '~' && filename.endsWith(ext))
grantlee_templates.append(filename);
- }
}
// find statistics templates
dir.setPath(getPrintingTemplatePathUser() + QDir::separator() + "statistics");
list = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
foreach (const QString& filename, list) {
- if (filename.at(filename.size() - 1) != '~') {
+ if (filename.at(filename.size() - 1) != '~' && filename.endsWith(ext))
grantlee_statistics_templates.append(filename);
+ }
+}
+
+/* find templates which are part of the bundle in the user path
+ * and set them as read only.
+ */
+void set_bundled_templates_as_read_only()
+{
+ QDir dir;
+ const QString stats("statistics");
+ QStringList list, listStats;
+ QString pathBundle = getPrintingTemplatePathBundle();
+ QString pathUser = getPrintingTemplatePathUser();
+
+ dir.setPath(pathBundle);
+ list = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
+ dir.setPath(pathBundle + QDir::separator() + stats);
+ listStats = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
+ for (int i = 0; i < listStats.length(); i++)
+ listStats[i] = stats + QDir::separator() + listStats.at(i);
+ list += listStats;
+
+ foreach (const QString& f, list)
+ 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
}
}