GrantleeTheme

qtresourcetemplateloader.cpp
1 /*
2  * SPDX-FileCopyrightText: 2016-2022 Laurent Montel <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 
8 #include "qtresourcetemplateloader.h"
9 
10 #include <QFile>
11 #include <QTextStream>
12 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
13 #include <grantlee/engine.h>
14 #else
15 #include <KTextTemplate/engine.h>
16 #endif
17 // TODO: remove this class when Grantlee support it
18 using namespace GrantleeTheme;
19 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
20 QtResourceTemplateLoader::QtResourceTemplateLoader(const QSharedPointer<Grantlee::AbstractLocalizer> localizer)
21  : Grantlee::FileSystemTemplateLoader(localizer)
22 #else
23 QtResourceTemplateLoader::QtResourceTemplateLoader(const QSharedPointer<KTextTemplate::AbstractLocalizer> localizer)
24  : KTextTemplate::FileSystemTemplateLoader(localizer)
25 #endif
26 {
27 }
28 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
29 Grantlee::Template QtResourceTemplateLoader::loadByName(const QString &fileName, const Grantlee::Engine *engine) const
30 #else
31 KTextTemplate::Template QtResourceTemplateLoader::loadByName(const QString &fileName, const KTextTemplate::Engine *engine) const
32 #endif
33 {
34  // Qt resource file
35  if (fileName.startsWith(QLatin1String(":/"))) {
36  QFile file;
37  file.setFileName(fileName);
38  if (!file.exists() || !file.open(QIODevice::ReadOnly | QIODevice::Text)) {
39  return {};
40  }
41 
42  QTextStream fstream(&file);
43 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
44  fstream.setCodec("UTF-8");
45 #endif
46  const auto fileContent = fstream.readAll();
47 
48  return engine->newTemplate(fileContent, fileName);
49  } else {
50 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
51  return Grantlee::FileSystemTemplateLoader::loadByName(fileName, engine);
52 #else
53  return KTextTemplate::FileSystemTemplateLoader::loadByName(fileName, engine);
54 #endif
55  }
56 }
57 
58 bool QtResourceTemplateLoader::canLoadTemplate(const QString &name) const
59 {
60  // Qt resource file
61  if (name.startsWith(QLatin1String(":/"))) {
62  QFile file;
63  file.setFileName(name);
64 
65  if (!file.exists() || !file.open(QIODevice::ReadOnly | QIODevice::Text)) {
66  return false;
67  }
68  file.close();
69  return true;
70  } else {
71 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
72  return Grantlee::FileSystemTemplateLoader::canLoadTemplate(name);
73 #else
74  return KTextTemplate::FileSystemTemplateLoader::canLoadTemplate(name);
75 #endif
76  }
77 }
virtual bool open(QIODevice::OpenMode mode) override
bool exists() const const
void setFileName(const QString &name)
virtual void close() override
The QtResourceTemplateLoader class.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Wed Sep 28 2022 04:06:59 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.