ginebra2/ContentViews/ViewportMetaDataParser.cpp
changeset 6 1c3b8676e58c
child 16 3c88a81ff781
equal deleted inserted replaced
5:0f2326c2a325 6:1c3b8676e58c
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     8 *
       
     9 * This program 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
       
    12 * GNU Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not,
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:
       
    19 *
       
    20 */
       
    21 
       
    22 #include "ViewportMetaDataParser.h"
       
    23 
       
    24 namespace GVA {
       
    25 
       
    26 ViewportMetaDataParser::ViewportMetaDataParser(const QRect& clientRect)
       
    27             : m_clientRect(clientRect)
       
    28 {}
       
    29 
       
    30 ViewportMetaDataParser::~ViewportMetaDataParser()
       
    31 {}
       
    32 
       
    33 ViewportMetaData ViewportMetaDataParser::parse(const QString& viewportParams)
       
    34 {
       
    35     ViewportMetaData viewportMetaData = processArguments(viewportParams);
       
    36     viewportMetaData.adjustViewportData(m_clientRect);
       
    37     return viewportMetaData;
       
    38 }
       
    39 
       
    40 
       
    41 //W A R N I N G
       
    42 //FOLLOWING CODE HAS BEEN TAKEN FROM WebCore/dom/Document.cpp
       
    43 //NEED TO REMOVE WHEN QtWebkit provides viewport param API
       
    44 
       
    45 // Though isspace() considers \t and \v to be whitespace, Win IE doesn't.
       
    46 bool ViewportMetaDataParser::isSeparator(QChar c)
       
    47 {
       
    48     return c == ' '
       
    49             || c == '\t'
       
    50             || c == '\n'
       
    51             || c == '\r'
       
    52             || c == '='
       
    53             || c == ','
       
    54             || c == '\0'
       
    55             || c == ';';
       
    56 }
       
    57 
       
    58 ViewportMetaData ViewportMetaDataParser::processArguments(const QString& features)
       
    59 {
       
    60     int keyBegin, keyEnd;
       
    61     int valueBegin, valueEnd;
       
    62     ViewportMetaData viewportMetaData;
       
    63 
       
    64     int i = 0;
       
    65     int length = features.length();
       
    66     QString buffer = features.toLower();
       
    67 
       
    68     while (i < length) {
       
    69         // skip to first non-separator, but don't skip past the end of the string
       
    70         while (isSeparator(buffer[i])) {
       
    71             if (i >= length)
       
    72                 break;
       
    73             i++;
       
    74         }
       
    75         keyBegin = i;
       
    76 
       
    77         // skip to first separator
       
    78         while (!isSeparator(buffer[i]))
       
    79             i++;
       
    80         keyEnd = i;
       
    81 
       
    82         // skip to first '=', but don't skip past a ',' or the end of the string
       
    83         while (buffer[i] != '=') {
       
    84             if (buffer[i] == ',' || i >= length)
       
    85                 break;
       
    86             i++;
       
    87         }
       
    88         // skip to first non-separator, but don't skip past a ',' or the end of the string
       
    89         while (isSeparator(buffer[i])) {
       
    90             if (buffer[i] == ',' || i >= length)
       
    91                 break;
       
    92             i++;
       
    93         }
       
    94         valueBegin = i;
       
    95 
       
    96         // skip to first separator
       
    97         while (!isSeparator(buffer[i]))
       
    98             i++;
       
    99         valueEnd = i;
       
   100 
       
   101         Q_ASSERT(i <= length);
       
   102 
       
   103         QString keyString = buffer.mid(keyBegin, keyEnd - keyBegin);
       
   104         QString valueString = buffer.mid(valueBegin, valueEnd - valueBegin);
       
   105         setViewportFeature(keyString, valueString, viewportMetaData);
       
   106     }
       
   107     return viewportMetaData;
       
   108 }
       
   109 
       
   110 void ViewportMetaDataParser::setViewportFeature(const QString& keyString, const QString& valueString, ViewportMetaData& viewportMetaData)
       
   111 {
       
   112     float value = ViewportMetaData::ValueUndefined;
       
   113 
       
   114     if (QString::compare(valueString, "device-width", Qt::CaseInsensitive) == 0)
       
   115         value = m_clientRect.width();
       
   116     else if (QString::compare(valueString, "device-height", Qt::CaseInsensitive) == 0)
       
   117         value = m_clientRect.height();
       
   118     else if (QString::compare(valueString, "default", Qt::CaseInsensitive) == 0) {
       
   119         // This allows us to distinguish the omission of a key from asking for the default value.
       
   120         value = -2;
       
   121     }
       
   122     else if (valueString.length()) {
       
   123         // listing a key with no value is shorthand for key=default
       
   124         value = valueString.toDouble();
       
   125     }
       
   126 
       
   127     if (keyString == "initial-scale") {
       
   128         viewportMetaData.m_initialScale = value;
       
   129         viewportMetaData.setFlag(ViewportMetaData::UserDefinedInitialScale,true);
       
   130     }
       
   131     else if (keyString == "minimum-scale") {
       
   132         viewportMetaData.m_minimumScale = value;
       
   133         viewportMetaData.m_specifiedData.m_minScale = value;
       
   134         viewportMetaData.setFlag(ViewportMetaData::UserDefinedMinumumScale,true);
       
   135     }
       
   136     else if (keyString == "maximum-scale") {
       
   137         viewportMetaData.m_maximumScale = value;
       
   138         viewportMetaData.setFlag(ViewportMetaData::UserDefinedMaximumScale,true);
       
   139     }
       
   140     else if (keyString == "user-scalable") {
       
   141         if (QString::compare(valueString, "yes", Qt::CaseInsensitive) == 0)
       
   142             viewportMetaData.m_userScalable = true;
       
   143         else
       
   144             viewportMetaData.m_userScalable = false;
       
   145     }
       
   146     else if (keyString == "width") {
       
   147         viewportMetaData.m_width = value;
       
   148         viewportMetaData.m_specifiedData.m_width = valueString;
       
   149     }
       
   150     else if (keyString == "height") {
       
   151         viewportMetaData.m_height = value;
       
   152         viewportMetaData.m_specifiedData.m_height = valueString;
       
   153     }
       
   154 }
       
   155 
       
   156 } //namespace GVA