Cutelee  6.1.0
mediafinder.cpp
1 /*
2  This file is part of the Cutelee template system.
3 
4  Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either version
9  2.1 of the Licence, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 
19 */
20 
21 #include "mediafinder.h"
22 
23 #include "engine.h"
24 #include "exception.h"
25 #include "parser.h"
26 #include "util.h"
27 
28 #include <QtCore/QUrl>
29 
30 MediaFinderNodeFactory::MediaFinderNodeFactory() {}
31 
33  Parser *p) const
34 {
35  auto expr = smartSplit(tagContent);
36 
37  if (expr.size() <= 1) {
38  throw Cutelee::Exception(
39  TagSyntaxError,
40  QStringLiteral("'media_finder' tag requires at least one argument"));
41  }
42  expr.takeAt(0);
43 
44  return new MediaFinderNode(getFilterExpressionList(expr, p), p);
45 }
46 
47 MediaFinderNode::MediaFinderNode(
48  const QList<FilterExpression> &mediaExpressionList, QObject *parent)
49  : Node(parent), m_mediaExpressionList(mediaExpressionList)
50 {
51 }
52 
54 {
55  auto t = containerTemplate();
56  auto engine = t->engine();
57 
58  for (const FilterExpression &fe : m_mediaExpressionList) {
59  if (fe.isTrue(c)) {
60  auto fileUrl = engine->mediaUri(getSafeString(fe.resolve(c)));
61  if (fileUrl.second.isEmpty())
62  continue;
63  auto uri = QUrl::fromLocalFile(fileUrl.first).toString();
64  c->addExternalMedia(uri, fileUrl.second);
65  if (c->urlType() == Context::AbsoluteUrls)
66  streamValueInContext(stream, uri, c);
67  else if (!c->relativeMediaPath().isEmpty())
69  stream, QVariant(c->relativeMediaPath() + QLatin1Char('/')), c);
70  streamValueInContext(stream, fileUrl.second, c);
71  return;
72  }
73  }
74 }
QList< FilterExpression > getFilterExpressionList(const QStringList &list, Parser *p) const
Definition: node.cpp:192
Q_INVOKABLE QStringList smartSplit(const QString &str) const
Definition: node.cpp:202
The Context class holds the context to render a Template with.
Definition: context.h:119
UrlType urlType() const
Definition: context.cpp:196
QString relativeMediaPath() const
Definition: context.cpp:208
An exception for use when implementing template tags.
Definition: exception.h:85
A FilterExpression object represents a filter expression in a template.
Base class for all nodes.
Definition: node.h:78
void streamValueInContext(OutputStream *stream, const QVariant &input, Cutelee::Context *c) const
Definition: node.cpp:88
TemplateImpl * containerTemplate() const
Definition: node.cpp:107
The OutputStream class is used to render templates to a QTextStream.
Definition: outputstream.h:81
The Parser class processes a string template into a tree of nodes.
Definition: parser.h:49
Node * getNode(const QString &tagContent, Parser *p) const override
Definition: mediafinder.cpp:32
void render(OutputStream *stream, Context *c) const override
Definition: mediafinder.cpp:53
Cutelee::SafeString getSafeString(const QVariant &input)
Definition: util.cpp:108
Utility functions used throughout Cutelee.