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