controlpanelui/src/cpcategorymodel/src/cppluginconfigreader.cpp
branchRCL_3
changeset 14 5f281e37a2f5
parent 13 90fe62538f66
equal deleted inserted replaced
13:90fe62538f66 14:5f281e37a2f5
     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:  This class reads cpcfg files.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "cppluginconfigreader.h"
       
    19 #include <QFile>
       
    20 #include <QXmlStreamReader>
       
    21 #include <cplogger.h>
       
    22 
       
    23 const QString CHILD_PLUGINS_TAG         = "childplugins";
       
    24 const QString PLUGIN_TAG                = "plugin";
       
    25 const QString PLUGIN_ID_ATTR            = "id";
       
    26 const QString PLUGIN_DLL_ATTR           = "dll";
       
    27 const QString PLUGIN_DISPALYNAME_ATTR   = "displayname";
       
    28 const QString DESC_TAG                  = "desc";
       
    29 
       
    30 /*
       
    31  * Constructor.
       
    32  * @configPath : the full path of the config file.
       
    33  */
       
    34 CpPluginConfigReader::CpPluginConfigReader(const QString &configPath)
       
    35 : mConfigPath (configPath)
       
    36 {
       
    37 }
       
    38 
       
    39 /*
       
    40  * Desctructor
       
    41  */
       
    42 CpPluginConfigReader::~CpPluginConfigReader()
       
    43 {
       
    44 }
       
    45 
       
    46 /*
       
    47  * Reads a cpcfg file, returns a list of CpPluginConfig.
       
    48  */
       
    49 QList<CpPluginConfig> CpPluginConfigReader::readCpPluginConfigs()
       
    50 { 
       
    51     CPFW_LOG(QLatin1String("reading cpcfg file:") + mConfigPath);
       
    52     
       
    53     // Empty config file
       
    54     if (mConfigPath.isNull() || mConfigPath.isEmpty()) {
       
    55         CPFW_LOG("CpPluginConfigReader::readCpPluginConfigs() mConfigPath is empty.");
       
    56         return QList<CpPluginConfig> ();
       
    57     }
       
    58 
       
    59     QFile file(mConfigPath);
       
    60     
       
    61     // Config file doesn't exist
       
    62     if (!file.exists()) {
       
    63         CPFW_LOG( mConfigPath  + " does not exist.");
       
    64         return QList<CpPluginConfig> ();
       
    65     }
       
    66     
       
    67     // Open config file failed
       
    68     if (!file.open(QFile::ReadOnly | QFile::Text)) {
       
    69         CPFW_LOG(QString("CpPluginConfigReader::readCpPluginConfigs() open file failed. Error:%1")
       
    70             .arg(static_cast<int>(file.error()),0,10));
       
    71         return QList<CpPluginConfig> ();
       
    72     }
       
    73 
       
    74     QXmlStreamReader reader(&file);
       
    75 
       
    76     QList<CpPluginConfig> cpPluginConfigList;
       
    77 
       
    78     readCpPluginConfigs(reader, cpPluginConfigList);
       
    79 
       
    80     file.close();
       
    81 
       
    82     return cpPluginConfigList;
       
    83 }
       
    84 
       
    85 /*
       
    86  * Read a list of CpPluginConfig from a xml stream.
       
    87  */
       
    88 void CpPluginConfigReader::readCpPluginConfigs(QXmlStreamReader &xmlReader,QList<CpPluginConfig> &cpPluginConfigList)
       
    89 {
       
    90     xmlReader.readNext();
       
    91 
       
    92     while (!xmlReader.atEnd()) {
       
    93         
       
    94         if (xmlReader.isStartElement()) {
       
    95             // Read <childplugins> node
       
    96             if (xmlReader.name() == CHILD_PLUGINS_TAG) {
       
    97                 readChildPluginsElement(xmlReader, cpPluginConfigList);
       
    98             }
       
    99             else {
       
   100                 xmlReader.raiseError("Not a valid file with the right format.");
       
   101             }
       
   102         }
       
   103         
       
   104         else {
       
   105             xmlReader.readNext();
       
   106         }
       
   107     }
       
   108 }
       
   109 
       
   110 /*
       
   111  * Read <childplugins> node.
       
   112  */
       
   113 void CpPluginConfigReader::readChildPluginsElement(QXmlStreamReader &xmlReader,QList<CpPluginConfig> &cpPluginConfigList)
       
   114 {
       
   115     xmlReader.readNext();
       
   116 
       
   117     while (!xmlReader.atEnd()) {
       
   118         
       
   119         if (xmlReader.isEndElement()) {
       
   120             xmlReader.readNext();
       
   121             break;
       
   122         }
       
   123 
       
   124         if (xmlReader.isStartElement()) {
       
   125             // Read <plugin> node
       
   126             if (xmlReader.name() == PLUGIN_TAG) {
       
   127                 readPluginElement(xmlReader, cpPluginConfigList);
       
   128             }
       
   129             // Skip invalid node
       
   130             else {
       
   131                 skipUnknownElement(xmlReader);
       
   132             }
       
   133         }
       
   134         
       
   135         else {
       
   136             xmlReader.readNext();
       
   137         }
       
   138     }
       
   139 }
       
   140 
       
   141 /*
       
   142  * Read <plugin> node.
       
   143  */
       
   144 void CpPluginConfigReader::readPluginElement(QXmlStreamReader &xmlReader,QList<CpPluginConfig> &cpPluginConfigList)
       
   145 { 
       
   146     CpPluginConfig cpPluginConfig;
       
   147   
       
   148     QXmlStreamAttributes xmlAttributes = xmlReader.attributes();
       
   149   
       
   150     // Read <id> attribute
       
   151     if (xmlAttributes.hasAttribute(PLUGIN_ID_ATTR))  {
       
   152         cpPluginConfig.mUid 
       
   153             = (xmlAttributes.value(PLUGIN_ID_ATTR)).toString().toUInt(0,16);
       
   154     }
       
   155   
       
   156     // Read <dll> attribute
       
   157     if (xmlAttributes.hasAttribute(PLUGIN_DLL_ATTR)) {
       
   158         cpPluginConfig.mPluginFile 
       
   159             = (xmlAttributes.value(PLUGIN_DLL_ATTR)).toString();
       
   160     }
       
   161   
       
   162     // Read <displayname> attribute
       
   163     if (xmlAttributes.hasAttribute(PLUGIN_DISPALYNAME_ATTR)) {
       
   164         cpPluginConfig.mDisplayName 
       
   165             = (xmlAttributes.value(PLUGIN_DISPALYNAME_ATTR)).toString();
       
   166     }
       
   167   
       
   168     // Read <description> node
       
   169     readDescElement(xmlReader,cpPluginConfig);
       
   170   
       
   171 #ifdef ENABLE_CPFW_LOG
       
   172     cpPluginConfig.dump();
       
   173 #endif
       
   174   
       
   175     cpPluginConfigList.append(cpPluginConfig);
       
   176 }
       
   177 
       
   178 /*
       
   179  * Read <description> node.
       
   180  */
       
   181 void CpPluginConfigReader::readDescElement(QXmlStreamReader &xmlReader,CpPluginConfig &cpPluginConfig)
       
   182 {
       
   183     xmlReader.readNext();
       
   184 
       
   185     while (!xmlReader.atEnd()) {
       
   186         
       
   187         if (xmlReader.isEndElement()) {
       
   188             xmlReader.readNext();
       
   189             break;
       
   190         }
       
   191 
       
   192         if (xmlReader.isStartElement()) {
       
   193             // valid description node
       
   194             if (xmlReader.name() == DESC_TAG) {
       
   195                 cpPluginConfig.mDescription = xmlReader.readElementText();
       
   196                 if (xmlReader.isEndElement()) {
       
   197                     xmlReader.readNext();
       
   198                 }
       
   199             }
       
   200             // invalid node, skip it
       
   201             else {
       
   202                 skipUnknownElement(xmlReader);
       
   203             }
       
   204         }
       
   205         
       
   206         else {
       
   207             xmlReader.readNext();
       
   208         }
       
   209     }
       
   210 }
       
   211 
       
   212 /*
       
   213  * ignore invalid node.
       
   214  */
       
   215 void CpPluginConfigReader::skipUnknownElement(QXmlStreamReader &xmlReader)
       
   216 {
       
   217     xmlReader.readNext();
       
   218 
       
   219     while (!xmlReader.atEnd()) {
       
   220         
       
   221         if (xmlReader.isEndElement()) {
       
   222             xmlReader.readNext();
       
   223             break;
       
   224         }
       
   225 
       
   226         if (xmlReader.isStartElement()) {
       
   227             skipUnknownElement(xmlReader);
       
   228         }
       
   229         else {
       
   230             xmlReader.readNext();
       
   231         }
       
   232     }
       
   233 }