Home | History | Annotate | Download | only in wml
      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 "WMLTimerElement.h"
     25 
     26 #include "Attribute.h"
     27 #include "HTMLNames.h"
     28 #include "WMLCardElement.h"
     29 #include "WMLDocument.h"
     30 #include "WMLNames.h"
     31 #include "WMLPageState.h"
     32 #include "WMLTemplateElement.h"
     33 #include "WMLVariables.h"
     34 
     35 namespace WebCore {
     36 
     37 using namespace WMLNames;
     38 
     39 WMLTimerElement::WMLTimerElement(const QualifiedName& tagName, Document* doc)
     40     : WMLElement(tagName, doc)
     41     , m_timer(this, &WMLTimerElement::timerFired)
     42 {
     43 }
     44 
     45 PassRefPtr<WMLTimerElement> WMLTimerElement::create(const QualifiedName& tagName, Document* document)
     46 {
     47     return adoptRef(new WMLTimerElement(tagName, document));
     48 }
     49 
     50 void WMLTimerElement::parseMappedAttribute(Attribute* attr)
     51 {
     52     if (attr->name() == HTMLNames::nameAttr)
     53         m_name = parseValueForbiddingVariableReferences(attr->value());
     54     else
     55         WMLElement::parseMappedAttribute(attr);
     56 }
     57 
     58 void WMLTimerElement::insertedIntoDocument()
     59 {
     60     WMLElement::insertedIntoDocument();
     61 
     62     // If the value of timeout is not a positive integer, ignore it
     63     if (value().toInt() <= 0)
     64         return;
     65 
     66     ContainerNode* parent = parentNode();
     67     if (!parent || !parent->isWMLElement())
     68         return;
     69 
     70     if (parent->hasTagName(cardTag)) {
     71         m_card = static_cast<WMLCardElement*>(parent);
     72         m_card->setIntrinsicEventTimer(this);
     73     }
     74 }
     75 
     76 void WMLTimerElement::removedFromDocument()
     77 {
     78     ContainerNode* parent = parentNode();
     79     if (parent && parent->isWMLElement() && parent->hasTagName(cardTag)) {
     80         m_card->setIntrinsicEventTimer(0);
     81         m_card = 0;
     82     }
     83 
     84     WMLElement::removedFromDocument();
     85 }
     86 
     87 void WMLTimerElement::timerFired(Timer<WMLTimerElement>*)
     88 {
     89     if (!m_card)
     90         return;
     91 
     92     WMLPageState* pageState = wmlPageStateForDocument(document());
     93     if (!pageState)
     94         return;
     95 
     96     String value = this->value();
     97 
     98     // When the timer expires, set the name varialbe of timer to '0'
     99     if (!m_name.isEmpty()) {
    100         value = "0";
    101         pageState->storeVariable(m_name, value);
    102     }
    103 
    104     WMLIntrinsicEventType eventType = WMLIntrinsicEventOnTimer;
    105     WMLIntrinsicEventHandler* eventHandler = m_card->eventHandler();
    106 
    107     bool hasIntrinsicEvent = false;
    108     if (eventHandler && eventHandler->hasIntrinsicEvent(eventType))
    109         hasIntrinsicEvent = true;
    110     else if (m_card->templateElement()) {
    111         eventHandler = m_card->templateElement()->eventHandler();
    112         if (eventHandler && eventHandler->hasIntrinsicEvent(eventType))
    113             hasIntrinsicEvent = true;
    114     }
    115 
    116     if (hasIntrinsicEvent)
    117         eventHandler->triggerIntrinsicEvent(eventType);
    118 }
    119 
    120 void WMLTimerElement::start(int interval)
    121 {
    122     if (!m_card || m_timer.isActive())
    123         return;
    124 
    125     if (interval <= 0 && !m_name.isEmpty()) {
    126         if (WMLPageState* pageState = wmlPageStateForDocument(document()))
    127             interval = pageState->getVariable(m_name).toInt();
    128     }
    129 
    130     if (interval <= 0)
    131         interval = value().toInt();
    132 
    133     if (interval > 0)
    134         m_timer.startOneShot(interval / 10.0f);
    135 }
    136 
    137 void WMLTimerElement::stop()
    138 {
    139     if (m_timer.isActive())
    140         m_timer.stop();
    141 }
    142 
    143 void WMLTimerElement::storeIntervalToPageState()
    144 {
    145     if (!m_timer.isActive())
    146         return;
    147 
    148     int interval = static_cast<int>(m_timer.nextFireInterval()) * 10;
    149 
    150     if (WMLPageState* pageState = wmlPageStateForDocument(document()))
    151         pageState->storeVariable(m_name, String::number(interval));
    152 }
    153 
    154 String WMLTimerElement::value() const
    155 {
    156     return parseValueSubstitutingVariableReferences(getAttribute(HTMLNames::valueAttr));
    157 }
    158 
    159 }
    160 
    161 #endif
    162