QtWebApp
templateloader.cpp
Go to the documentation of this file.
1 
6 #include "templateloader.h"
7 #include <QFile>
8 #include <QFileInfo>
9 #include <QStringList>
10 #include <QDir>
11 #include <QSet>
12 #include <QTextStream>
13 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
14  #include <QRegularExpression>
15 #else
16  #include <QRegExp>
17 #endif
18 
19 using namespace stefanfrings;
20 
21 TemplateLoader::TemplateLoader(const QSettings *settings, QObject *parent)
22  : QObject(parent)
23 {
24  templatePath=settings->value("path",".").toString();
25  // Convert relative path to absolute, based on the directory of the config file.
26 #ifdef Q_OS_WIN32
27  if (QDir::isRelativePath(templatePath) && settings->format()!=QSettings::NativeFormat)
28 #else
29  if (QDir::isRelativePath(templatePath))
30 #endif
31  {
32  QFileInfo configFile(settings->fileName());
33  templatePath=QFileInfo(configFile.absolutePath(),templatePath).absoluteFilePath();
34  }
35  fileNameSuffix=settings->value("suffix",".tpl").toString();
36  QString encoding=settings->value("encoding").toString();
37  if (encoding.isEmpty())
38  {
39  textCodec=QTextCodec::codecForLocale();
40  }
41  else
42  {
43  textCodec=QTextCodec::codecForName(encoding.toLocal8Bit());
44  }
45  qDebug("TemplateLoader: path=%s, codec=%s",qPrintable(templatePath),qPrintable(encoding));
46 }
47 
49 {}
50 
51 QString TemplateLoader::tryFile(QString localizedName)
52 {
53  QString fileName=templatePath+"/"+localizedName+fileNameSuffix;
54  qDebug("TemplateCache: trying file %s",qPrintable(fileName));
55  QFile file(fileName);
56  if (file.exists()) {
57  file.open(QIODevice::ReadOnly);
58  QString document=textCodec->toUnicode(file.readAll());
59  file.close();
60  if (file.error())
61  {
62  qCritical("TemplateLoader: cannot load file %s, %s",qPrintable(fileName),qPrintable(file.errorString()));
63  return "";
64  }
65  else
66  {
67  return document;
68  }
69  }
70  return "";
71 }
72 
73 Template TemplateLoader::getTemplate(QString templateName, QString locales)
74 {
75  QSet<QString> tried; // used to suppress duplicate attempts
76 
77  #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
78  QStringList locs=locales.split(',',Qt::SkipEmptyParts);
79  #else
80  QStringList locs=locales.split(',',QString::SkipEmptyParts);
81  #endif
82 
83  // Search for exact match
84  foreach (QString loc,locs)
85  {
86  #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
87  loc.replace(QRegularExpression(";.*"),"");
88  #else
89  loc.replace(QRegExp(";.*"),"");
90  #endif
91  loc.replace('-','_');
92 
93  QString localizedName=templateName+"-"+loc.trimmed();
94  if (!tried.contains(localizedName))
95  {
96  QString document=tryFile(localizedName);
97  if (!document.isEmpty()) {
98  return Template(document,localizedName);
99  }
100  tried.insert(localizedName);
101  }
102  }
103 
104  // Search for correct language but any country
105  foreach (QString loc,locs)
106  {
107  #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
108  loc.replace(QRegularExpression("[;_-].*"),"");
109  #else
110  loc.replace(QRegExp("[;_-].*"),"");
111  #endif
112  QString localizedName=templateName+"-"+loc.trimmed();
113  if (!tried.contains(localizedName))
114  {
115  QString document=tryFile(localizedName);
116  if (!document.isEmpty())
117  {
118  return Template(document,localizedName);
119  }
120  tried.insert(localizedName);
121  }
122  }
123 
124  // Search for default file
125  QString document=tryFile(templateName);
126  if (!document.isEmpty())
127  {
128  return Template(document,templateName);
129  }
130 
131  qCritical("TemplateCache: cannot find template %s",qPrintable(templateName));
132  return Template("",templateName);
133 }
QString fileNameSuffix
Suffix to the filenames.
Template getTemplate(const QString templateName, const QString locales=QString())
Get a template for a given locale.
TemplateLoader(const QSettings *settings, QObject *parent=nullptr)
Constructor.
QTextCodec * textCodec
Codec for decoding the files.
virtual QString tryFile(const QString localizedName)
Try to get a file from cache or filesystem.
virtual ~TemplateLoader()
Destructor.
QString templatePath
Directory where the templates are searched.
Enhanced version of QString for template processing.
Definition: template.h:91