• Main Page
  • Classes
  • Files
  • File List
  • File Members

lib/bfTemplateEngine/src/templateloader.cpp

Go to the documentation of this file.
00001 
00006 #include "templateloader.h"
00007 #include <QFile>
00008 #include <QFileInfo>
00009 #include <QStringList>
00010 #include <QDir>
00011 #include <QSet>
00012 
00013 TemplateLoader::TemplateLoader(QSettings* settings, QObject* parent)
00014     : QObject(parent)
00015 {
00016     templatePath=settings->value("path",".").toString();
00017     // Convert relative path to absolute, based on the directory of the config file.
00018 #ifdef Q_OS_WIN32
00019     if (QDir::isRelativePath(templatePath) && settings->format()!=QSettings::NativeFormat)
00020 #else
00021     if (QDir::isRelativePath(templatePath))
00022 #endif
00023     {
00024         QFileInfo configFile(settings->fileName());
00025         templatePath=QFileInfo(configFile.absolutePath(),templatePath).absoluteFilePath();
00026     }
00027     fileNameSuffix=settings->value("suffix",".tpl").toString();
00028     QString encoding=settings->value("encoding").toString();
00029     if (encoding.isEmpty()) {
00030         textCodec=QTextCodec::codecForLocale();
00031     }
00032     else {
00033        textCodec=QTextCodec::codecForName(encoding.toAscii());
00034    }
00035    qDebug("TemplateLoader: path=%s, codec=%s",qPrintable(templatePath),textCodec->name().data());
00036 }
00037 
00038 TemplateLoader::~TemplateLoader() {}
00039 
00040 QString TemplateLoader::tryFile(QString localizedName) {
00041     QString fileName=templatePath+"/"+localizedName+fileNameSuffix;
00042     qDebug("TemplateCache: trying file %s",qPrintable(fileName));
00043     QFile file(fileName);
00044     if (file.exists()) {
00045         file.open(QIODevice::ReadOnly);
00046         QString document=textCodec->toUnicode(file.readAll());
00047         file.close();
00048         if (file.error()) {
00049             qCritical("TemplateLoader: cannot load file %s, %s",qPrintable(fileName),qPrintable(file.errorString()));
00050             return "";
00051         }
00052         else {
00053             return document;
00054         }
00055     }
00056     return "";
00057 }
00058 
00059 Template TemplateLoader::getTemplate(QString templateName, QString locales) {
00060     mutex.lock();
00061     QSet<QString> tried; // used to suppress duplicate attempts
00062     QStringList locs=locales.split(',',QString::SkipEmptyParts);
00063 
00064     // Search for exact match
00065     foreach (QString loc,locs) {
00066         loc.replace(QRegExp(";.*"),"");
00067         loc.replace('-','_');
00068         QString localizedName=templateName+"-"+loc.trimmed();
00069         if (!tried.contains(localizedName)) {
00070             QString document=tryFile(localizedName);
00071             if (!document.isEmpty()) {
00072                 mutex.unlock();
00073                 return Template(document,localizedName);
00074             }
00075             tried.insert(localizedName);
00076         }
00077     }
00078 
00079     // Search for correct language but any country
00080     foreach (QString loc,locs) {
00081         loc.replace(QRegExp("[;_-].*"),"");
00082         QString localizedName=templateName+"-"+loc.trimmed();
00083         if (!tried.contains(localizedName)) {
00084             QString document=tryFile(localizedName);
00085             if (!document.isEmpty()) {
00086                 mutex.unlock();
00087                 return Template(document,localizedName);
00088             }
00089             tried.insert(localizedName);
00090         }
00091     }
00092 
00093     // Search for default file
00094     QString document=tryFile(templateName);
00095     if (!document.isEmpty()) {
00096         mutex.unlock();
00097         return Template(document,templateName);
00098     }
00099 
00100     qCritical("TemplateCache: cannot find template %s",qPrintable(templateName));
00101     mutex.unlock();
00102     return Template("",templateName);
00103 }

Generated on Mon Dec 26 2011 12:09:22 for QtWebApp by  doxygen 1.7.1