WebCore/wml/WMLTimerElement.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 void WMLTimerElement::parseMappedAttribute(Attribute* attr)
       
    46 {
       
    47     if (attr->name() == HTMLNames::nameAttr)
       
    48         m_name = parseValueForbiddingVariableReferences(attr->value());
       
    49     else
       
    50         WMLElement::parseMappedAttribute(attr);
       
    51 }
       
    52 
       
    53 void WMLTimerElement::insertedIntoDocument()
       
    54 {
       
    55     WMLElement::insertedIntoDocument();
       
    56 
       
    57     // If the value of timeout is not a positive integer, ignore it
       
    58     if (value().toInt() <= 0)
       
    59         return;
       
    60 
       
    61     Node* parent = parentNode();
       
    62     if (!parent || !parent->isWMLElement())
       
    63         return;
       
    64 
       
    65     if (parent->hasTagName(cardTag)) {
       
    66         m_card = static_cast<WMLCardElement*>(parent);
       
    67         m_card->setIntrinsicEventTimer(this);
       
    68     }
       
    69 }
       
    70 
       
    71 void WMLTimerElement::removedFromDocument()
       
    72 {
       
    73     Node* parent = parentNode();
       
    74     if (parent && parent->isWMLElement() && parent->hasTagName(cardTag)) {
       
    75         m_card->setIntrinsicEventTimer(0);
       
    76         m_card = 0;
       
    77     }
       
    78 
       
    79     WMLElement::removedFromDocument();
       
    80 }
       
    81 
       
    82 void WMLTimerElement::timerFired(Timer<WMLTimerElement>*)
       
    83 {
       
    84     if (!m_card)
       
    85         return;
       
    86 
       
    87     WMLPageState* pageState = wmlPageStateForDocument(document());
       
    88     if (!pageState)
       
    89         return;
       
    90 
       
    91     String value = this->value();
       
    92 
       
    93     // When the timer expires, set the name varialbe of timer to '0'
       
    94     if (!m_name.isEmpty()) {
       
    95         value = "0";
       
    96         pageState->storeVariable(m_name, value);
       
    97     }
       
    98 
       
    99     WMLIntrinsicEventType eventType = WMLIntrinsicEventOnTimer;
       
   100     WMLIntrinsicEventHandler* eventHandler = m_card->eventHandler();
       
   101 
       
   102     bool hasIntrinsicEvent = false;
       
   103     if (eventHandler && eventHandler->hasIntrinsicEvent(eventType))
       
   104         hasIntrinsicEvent = true;
       
   105     else if (m_card->templateElement()) {
       
   106         eventHandler = m_card->templateElement()->eventHandler();
       
   107         if (eventHandler && eventHandler->hasIntrinsicEvent(eventType))
       
   108             hasIntrinsicEvent = true;
       
   109     }
       
   110 
       
   111     if (hasIntrinsicEvent)
       
   112         eventHandler->triggerIntrinsicEvent(eventType);
       
   113 }
       
   114 
       
   115 void WMLTimerElement::start(int interval)
       
   116 {
       
   117     if (!m_card || m_timer.isActive())
       
   118         return;
       
   119 
       
   120     if (interval <= 0 && !m_name.isEmpty()) {
       
   121         if (WMLPageState* pageState = wmlPageStateForDocument(document()))
       
   122             interval = pageState->getVariable(m_name).toInt();
       
   123     }
       
   124 
       
   125     if (interval <= 0)
       
   126         interval = value().toInt();
       
   127 
       
   128     if (interval > 0)
       
   129         m_timer.startOneShot(interval / 10.0f);
       
   130 }
       
   131 
       
   132 void WMLTimerElement::stop()
       
   133 {
       
   134     if (m_timer.isActive())
       
   135         m_timer.stop();
       
   136 }
       
   137 
       
   138 void WMLTimerElement::storeIntervalToPageState()
       
   139 {
       
   140     if (!m_timer.isActive())
       
   141         return;
       
   142 
       
   143     int interval = static_cast<int>(m_timer.nextFireInterval()) * 10;
       
   144 
       
   145     if (WMLPageState* pageState = wmlPageStateForDocument(document()))
       
   146         pageState->storeVariable(m_name, String::number(interval));
       
   147 }
       
   148 
       
   149 String WMLTimerElement::value() const
       
   150 {
       
   151     return parseValueSubstitutingVariableReferences(getAttribute(HTMLNames::valueAttr));
       
   152 }
       
   153 
       
   154 }
       
   155 
       
   156 #endif