Commit ab6e8da2 authored by Laurent Montel's avatar Laurent Montel 😁

Add QtResourceTemplateLoader

parent f224b82d
...@@ -19,7 +19,7 @@ include(KDEFrameworkCompilerSettings NO_POLICY_SCOPE) ...@@ -19,7 +19,7 @@ include(KDEFrameworkCompilerSettings NO_POLICY_SCOPE)
include(ECMQtDeclareLoggingCategory) include(ECMQtDeclareLoggingCategory)
include(ECMAddTests) include(ECMAddTests)
set(PIM_VERSION "5.2.40") set(PIM_VERSION "5.2.41")
set(GRANTLEETHEME_LIB_VERSION ${PIM_VERSION}) set(GRANTLEETHEME_LIB_VERSION ${PIM_VERSION})
set(QT_REQUIRED_VERSION "5.4.0") set(QT_REQUIRED_VERSION "5.4.0")
......
...@@ -8,6 +8,7 @@ set(libgrantleetheme_SRCS ...@@ -8,6 +8,7 @@ set(libgrantleetheme_SRCS
grantleetheme.cpp grantleetheme.cpp
grantleethememanager.cpp grantleethememanager.cpp
grantleeki18nlocalizer.cpp grantleeki18nlocalizer.cpp
qtresourcetemplateloader.cpp
) )
configure_file(config-grantleetheme.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-grantleetheme.h) configure_file(config-grantleetheme.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-grantleetheme.h)
...@@ -45,6 +46,7 @@ ecm_generate_headers(GrantleeTheme_CamelCase_HEADERS ...@@ -45,6 +46,7 @@ ecm_generate_headers(GrantleeTheme_CamelCase_HEADERS
HEADER_NAMES HEADER_NAMES
GrantleeThemeManager GrantleeThemeManager
GrantleeTheme GrantleeTheme
QtResourceTemplateLoader
REQUIRED_HEADERS GrantleeTheme_HEADERS REQUIRED_HEADERS GrantleeTheme_HEADERS
PREFIX GrantleeTheme PREFIX GrantleeTheme
) )
......
/*
* Copyright (C) 2016 Laurent Montel <montel@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "qtresourcetemplateloader.h"
#include <QFile>
#include <QTextStream>
#include <grantlee/engine.h>
//TODO: remove this class when Grantlee support it
using namespace GrantleeTheme;
QtResourceTemplateLoader::QtResourceTemplateLoader(const QSharedPointer<Grantlee::AbstractLocalizer> localizer)
: Grantlee::FileSystemTemplateLoader(localizer)
{
}
Grantlee::Template QtResourceTemplateLoader::loadByName(const QString &fileName, const Grantlee::Engine *engine) const
{
// Qt resource file
if (fileName.startsWith(QLatin1String(":/"))) {
QFile file;
file.setFileName(fileName);
if (!file.exists() || !file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return Grantlee::Template();
}
QTextStream fstream(&file);
fstream.setCodec("UTF-8");
const auto fileContent = fstream.readAll();
return engine->newTemplate(fileContent, fileName);
} else {
return Grantlee::FileSystemTemplateLoader::loadByName(fileName, engine);
}
}
bool QtResourceTemplateLoader::canLoadTemplate(const QString &name) const
{
// Qt resource file
if (name.startsWith(QLatin1String(":/"))) {
QFile file;
file.setFileName(name);
if (!file.exists() || !file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return false;
}
file.close();
return true;
} else {
return Grantlee::FileSystemTemplateLoader::canLoadTemplate(name);
}
}
/*
* Copyright (C) 2016 Laurent Montel <montel@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef QTRESOURCETEMPLATELOADER_H
#define QTRESOURCETEMPLATELOADER_H
#include "grantlee/templateloader.h"
#include "grantleetheme_export.h"
namespace GrantleeTheme
{
class GRANTLEETHEME_EXPORT QtResourceTemplateLoader : public Grantlee::FileSystemTemplateLoader
{
public:
QtResourceTemplateLoader(const QSharedPointer<Grantlee::AbstractLocalizer> localizer = QSharedPointer<Grantlee::AbstractLocalizer>());
Grantlee::Template loadByName(const QString &fileName, const Grantlee::Engine *engine) const Q_DECL_OVERRIDE;
bool canLoadTemplate(const QString &name) const Q_DECL_OVERRIDE;
};
}
#endif // QTRESOURCETEMPLATELOADER_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment