File indexing completed on 2025-02-09 04:28:36

0001 /*
0002   This file is part of the KTextTemplate library
0003 
0004   SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com>
0005 
0006   SPDX-License-Identifier: LGPL-2.0-or-later
0007 
0008 */
0009 
0010 #include "l10n_money.h"
0011 
0012 #include "abstractlocalizer.h"
0013 #include "exception.h"
0014 #include "parser.h"
0015 #include "util.h"
0016 
0017 L10nMoneyNodeFactory::L10nMoneyNodeFactory() = default;
0018 
0019 Node *L10nMoneyNodeFactory::getNode(const QString &tagContent, Parser *p) const
0020 {
0021     auto expr = smartSplit(tagContent);
0022 
0023     if (expr.size() < 2)
0024         throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: l10n_money tag takes at least one argument"));
0025 
0026     const FilterExpression value(expr.at(1), p);
0027 
0028     FilterExpression currency;
0029 
0030     if (expr.size() == 3)
0031         currency = FilterExpression(expr.at(2), p);
0032 
0033     return new L10nMoneyNode(value, currency);
0034 }
0035 
0036 L10nMoneyVarNodeFactory::L10nMoneyVarNodeFactory() = default;
0037 
0038 KTextTemplate::Node *L10nMoneyVarNodeFactory::getNode(const QString &tagContent, Parser *p) const
0039 {
0040     auto expr = smartSplit(tagContent);
0041 
0042     if (expr.size() < 4)
0043         throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: l10n_money tag takes at least three arguments"));
0044 
0045     FilterExpression value(expr.at(1), p);
0046 
0047     FilterExpression currency;
0048 
0049     if (expr.size() == 3)
0050         currency = FilterExpression(expr.at(2), p);
0051 
0052     const auto resultName = expr.last();
0053 
0054     return new L10nMoneyVarNode(value, currency, resultName);
0055 }
0056 
0057 L10nMoneyNode::L10nMoneyNode(const FilterExpression &value, const FilterExpression &currency, QObject *parent)
0058     : Node(parent)
0059     , m_value(value)
0060     , m_currency(currency)
0061 {
0062 }
0063 
0064 void L10nMoneyNode::render(OutputStream *stream, Context *c) const
0065 {
0066     auto resultString = c->localizer()->localizeMonetaryValue(m_value.resolve(c).value<double>(), getSafeString(m_currency.resolve(c)).get());
0067 
0068     streamValueInContext(stream, resultString, c);
0069 }
0070 
0071 L10nMoneyVarNode::L10nMoneyVarNode(const FilterExpression &value, const FilterExpression &currency, const QString &resultName, QObject *parent)
0072     : Node(parent)
0073     , m_value(value)
0074     , m_currency(currency)
0075     , m_resultName(resultName)
0076 {
0077 }
0078 
0079 void L10nMoneyVarNode::render(OutputStream *stream, Context *c) const
0080 {
0081     Q_UNUSED(stream)
0082     const auto resultString = c->localizer()->localizeMonetaryValue(m_value.resolve(c).value<double>(), getSafeString(m_currency.resolve(c)).get());
0083 
0084     c->insert(m_resultName, resultString);
0085 }
0086 
0087 #include "moc_l10n_money.cpp"