GrantleeTheme

qtresourcetemplateloader.cpp
1 /*
2  * Copyright (C) 2016-2020 Laurent Montel <[email protected]>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  */
19 
20 #include "qtresourcetemplateloader.h"
21 
22 #include <QFile>
23 #include <QTextStream>
24 #include <grantlee/engine.h>
25 //TODO: remove this class when Grantlee support it
26 using namespace GrantleeTheme;
27 
28 QtResourceTemplateLoader::QtResourceTemplateLoader(const QSharedPointer<Grantlee::AbstractLocalizer> localizer)
29  : Grantlee::FileSystemTemplateLoader(localizer)
30 {
31 }
32 
33 Grantlee::Template QtResourceTemplateLoader::loadByName(const QString &fileName, const Grantlee::Engine *engine) const
34 {
35  // Qt resource file
36  if (fileName.startsWith(QLatin1String(":/"))) {
37  QFile file;
38  file.setFileName(fileName);
39  if (!file.exists() || !file.open(QIODevice::ReadOnly | QIODevice::Text)) {
40  return Grantlee::Template();
41  }
42 
43  QTextStream fstream(&file);
44  fstream.setCodec("UTF-8");
45  const auto fileContent = fstream.readAll();
46 
47  return engine->newTemplate(fileContent, fileName);
48  } else {
49  return Grantlee::FileSystemTemplateLoader::loadByName(fileName, engine);
50  }
51 }
52 
53 bool QtResourceTemplateLoader::canLoadTemplate(const QString &name) const
54 {
55  // Qt resource file
56  if (name.startsWith(QLatin1String(":/"))) {
57  QFile file;
58  file.setFileName(name);
59 
60  if (!file.exists() || !file.open(QIODevice::ReadOnly | QIODevice::Text)) {
61  return false;
62  }
63  file.close();
64  return true;
65  } else {
66  return Grantlee::FileSystemTemplateLoader::canLoadTemplate(name);
67  }
68 }
void setFileName(const QString &name)
bool exists() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
virtual bool open(QIODevice::OpenMode mode) override
virtual void close() override
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed May 27 2020 22:46:09 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.