phoneuis/bubblemanager2/tsrc/bubbletest2/bubbletester/bubbledata.cpp
branchGCC_SURGE
changeset 51 f39ed5e045e0
parent 40 bab96b7ed1a4
parent 46 bc5a64e5bc3c
equal deleted inserted replaced
40:bab96b7ed1a4 51:f39ed5e045e0
     1 /*!
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Animated icon.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QFile>
       
    19 #include <QMessageBox>
       
    20 #include <QDomDocument>
       
    21 #include "bubbledata.h"
       
    22 
       
    23 BubbleData::BubbleData( QObject *parent ) : QObject(parent)
       
    24 {
       
    25     QFile file(":/testdata.xml");
       
    26 
       
    27     if (!file.open(QFile::ReadOnly | QFile::Text)) {
       
    28         QMessageBox::critical((QWidget*)parent, tr("Bubble tester"),
       
    29                               tr("Cannot read testdata.xml") );
       
    30         return;
       
    31     }
       
    32 
       
    33     createDomFromFile(&file);
       
    34 }
       
    35 
       
    36 void BubbleData::createDomFromFile(QIODevice *device)
       
    37 {
       
    38     QString errorStr;
       
    39     int errorLine;
       
    40     int errorColumn;
       
    41 
       
    42     if (!mDomDocument.setContent(device, true, &errorStr, &errorLine,
       
    43                                 &errorColumn)) {
       
    44         QMessageBox::information((QWidget*)parent(), tr("Bubble tester"),
       
    45                                  tr("Parse error at line %1, column %2:\n%3")
       
    46                                  .arg(errorLine)
       
    47                                  .arg(errorColumn)
       
    48                                  .arg(errorStr));
       
    49     }
       
    50 }
       
    51 
       
    52 QString BubbleData::dataField( const QString&  bubble, const QString& fieldName )
       
    53 {
       
    54     QString text("");
       
    55     QDomElement elem = bubbleElement(bubble);
       
    56 
       
    57     if (!elem.isNull()) {
       
    58         QDomNodeList list = elem.elementsByTagName(fieldName);
       
    59 
       
    60         if (list.count()) {
       
    61             QDomNode node = list.at(0);
       
    62             if (node.isElement()) {
       
    63                 QDomElement e = node.toElement();
       
    64                 text = e.text();
       
    65             }
       
    66         }
       
    67     }
       
    68 
       
    69     return text;
       
    70 }
       
    71 
       
    72 QDomElement BubbleData::bubbleElement(const QString&  bubble)
       
    73 {
       
    74     QDomNodeList list = mDomDocument.elementsByTagName("bubble");
       
    75 
       
    76     QDomElement elem;
       
    77 
       
    78     for (int i=0; i<list.count(); i++ ) {
       
    79         QDomNode node = list.at(i);
       
    80         if (node.isElement()) {
       
    81             QDomElement e = node.toElement();
       
    82             QString id = e.attribute("id");
       
    83             if (id == bubble) {
       
    84                 elem = e;
       
    85                 break;
       
    86             }
       
    87         }
       
    88     }
       
    89 
       
    90     return elem;
       
    91 }
       
    92 
       
    93 void BubbleData::setDataField( const QString&  bubble, const QString& fieldName, const QString& value )
       
    94 {
       
    95     QDomElement elem = bubbleElement(bubble);
       
    96     if (elem.isNull()) {
       
    97         return;
       
    98     }
       
    99 
       
   100     QDomText domText = mDomDocument.createTextNode(value);
       
   101     QDomElement newElem = mDomDocument.createElement(fieldName);
       
   102     newElem.appendChild(domText);
       
   103 
       
   104     QDomNodeList list = elem.elementsByTagName(fieldName);
       
   105 
       
   106     if (list.count()) {
       
   107         QDomNode node = list.at(0);
       
   108         if (node.isElement()) {
       
   109             QDomNode parent = node.parentNode();
       
   110             parent.replaceChild(newElem,node);
       
   111         }
       
   112     }
       
   113 }
       
   114 
       
   115 QString BubbleData::dataField(const QString& fieldName)
       
   116 {
       
   117     QString text("");
       
   118     QDomNodeList list = mDomDocument.elementsByTagName(fieldName);
       
   119 
       
   120     if (list.count()) {
       
   121         QDomNode node = list.at(0);
       
   122         if (node.isElement()) {
       
   123             QDomElement e = node.toElement();
       
   124             text = e.text();
       
   125         }
       
   126     }
       
   127 
       
   128     return text;
       
   129 }
       
   130 
       
   131 void BubbleData::setDataField(const QString& fieldName, const QString& value)
       
   132 {
       
   133     QDomText domText = mDomDocument.createTextNode(value);
       
   134     QDomElement newElem = mDomDocument.createElement(fieldName);
       
   135     newElem.appendChild(domText);
       
   136 
       
   137     QDomNodeList list = mDomDocument.elementsByTagName(fieldName);
       
   138 
       
   139     if (list.count()) {
       
   140         QDomNode node = list.at(0);
       
   141         if (node.isElement()) {
       
   142             QDomNode parent = node.parentNode();
       
   143             parent.replaceChild(newElem,node);
       
   144         }
       
   145     }
       
   146 
       
   147 }
       
   148 
       
   149 const QDomDocument& BubbleData::document() const
       
   150 {
       
   151     return mDomDocument;
       
   152 }
       
   153