1/**
2 * Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 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 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21#include "config.h"
22
23#if ENABLE(WML)
24#include "WMLTaskElement.h"
25
26#include "WMLAnchorElement.h"
27#include "WMLDoElement.h"
28#include "WMLNames.h"
29#include "WMLOnEventElement.h"
30#include "WMLPageState.h"
31#include "WMLSetvarElement.h"
32
33namespace WebCore {
34
35using namespace WMLNames;
36
37WMLTaskElement::WMLTaskElement(const QualifiedName& tagName, Document* doc)
38    : WMLElement(tagName, doc)
39{
40}
41
42WMLTaskElement::~WMLTaskElement()
43{
44}
45
46void WMLTaskElement::insertedIntoDocument()
47{
48    WMLElement::insertedIntoDocument();
49
50    ContainerNode* parent = parentNode();
51    if (!parent || !parent->isWMLElement())
52        return;
53
54    if (parent->hasTagName(anchorTag))
55        static_cast<WMLAnchorElement*>(parent)->registerTask(this);
56    else if (parent->hasTagName(doTag))
57        static_cast<WMLDoElement*>(parent)->registerTask(this);
58    else if (parent->hasTagName(oneventTag))
59        static_cast<WMLOnEventElement*>(parent)->registerTask(this);
60}
61
62void WMLTaskElement::removedFromDocument()
63{
64    ContainerNode* parent = parentNode();
65    if (parent && parent->isWMLElement()) {
66        if (parent->hasTagName(anchorTag))
67            static_cast<WMLAnchorElement*>(parent)->deregisterTask(this);
68        else if (parent->hasTagName(doTag))
69            static_cast<WMLDoElement*>(parent)->deregisterTask(this);
70        else if (parent->hasTagName(oneventTag))
71            static_cast<WMLOnEventElement*>(parent)->deregisterTask(this);
72    }
73
74    WMLElement::removedFromDocument();
75}
76
77void WMLTaskElement::registerVariableSetter(WMLSetvarElement* element)
78{
79    ASSERT(m_variableSetterElements.find(element) == WTF::notFound);
80    m_variableSetterElements.append(element);
81}
82
83void WMLTaskElement::deregisterVariableSetter(WMLSetvarElement* element)
84{
85    size_t position = m_variableSetterElements.find(element);
86    ASSERT(position != WTF::notFound);
87    m_variableSetterElements.remove(position);
88}
89
90void WMLTaskElement::storeVariableState(WMLPageState* pageState)
91{
92    if (!pageState || m_variableSetterElements.isEmpty())
93        return;
94
95    WMLVariableMap variables;
96    Vector<WMLSetvarElement*>::iterator it = m_variableSetterElements.begin();
97    Vector<WMLSetvarElement*>::iterator end = m_variableSetterElements.end();
98
99    for (; it != end; ++it) {
100        WMLSetvarElement* setterElement = (*it);
101
102        String name = setterElement->name();
103        if (name.isEmpty())
104            continue;
105
106        String value = setterElement->value();
107        variables.set(name, value);
108
109        // The next setvar element may depend on this variable value. It's safe to store the current
110        // name value pair in the page state, as the whole page state is replaced soon by this new map
111        pageState->storeVariable(name, value);
112    }
113
114    pageState->storeVariables(variables);
115}
116
117}
118
119#endif
120