camerauis/cameraxui/cxui/src/cxuisettingxmlreader.cpp
branchRCL_3
changeset 24 bac7acad7cb3
parent 23 61bc0f252b2b
child 25 2c87b2808fd7
equal deleted inserted replaced
23:61bc0f252b2b 24:bac7acad7cb3
     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:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QPair>
       
    19 #include <QtXml>
       
    20 #include <QFile>
       
    21 #include <hbparameterlengthlimiter.h>
       
    22 #include <HbExtendedLocale>
       
    23 
       
    24 
       
    25 #include "cxutils.h"
       
    26 #include "cxuienums.h"
       
    27 #include "cxuisettingxmlreader.h"
       
    28 #include "cxuisettingradiobuttonlist.h"
       
    29 
       
    30 using namespace CxUiSettings;
       
    31 
       
    32 namespace
       
    33 {
       
    34     static const char *TAG_ROOT                           = "cxui";
       
    35     static const char *TAG_SETTINGS                       = "settings";
       
    36     static const char *TAG_SETTING_LIST                   = "setting_list";
       
    37     static const char *TAG_SETTING_SLIDER                 = "setting_slider";
       
    38     static const char *TAG_ITEM                           = "item";
       
    39     static const char *TAG_ITEM_VARIANT_LN                = "lnItem";
       
    40     static const char *TAG_ITEM_VARIANT_L1                = "l1Item";
       
    41     static const char *TAG_SETUP                          = "setup";
       
    42 
       
    43     static const char *ATTRIBUTE_ID                       = "id";
       
    44     static const char *ATTRIBUTE_HEADING                  = "heading";
       
    45     static const char *ATTRIBUTE_PREVIEW                  = "preview";
       
    46     static const char *ATTRIBUTE_LISTBOXTYPE              = "type";
       
    47     static const char *ATTRIBUTE_HEADING_ICON             = "setting_icon";
       
    48     static const char *ATTRIBUTE_ICON                     = "icon";
       
    49 
       
    50     static const char *ATTRIBUTE_VALUE                    = "value";
       
    51     static const char *ATTRIBUTE_LOCALIZATION_ID          = "string";
       
    52     static const char *ATTRIBUTE_LOCALIZATION_ID_LN_VALUE = "lnValue";
       
    53     static const char *ATTRIBUTE_LOCALIZATION_ID_L1_VALUE = "l1Value";
       
    54 
       
    55     static const char *ATTRIBUTE_SLIDER_MIN               = "min";
       
    56     static const char *ATTRIBUTE_SLIDER_MAX               = "max";
       
    57     static const char *ATTRIBUTE_SLIDER_MINOR_STEP        = "minorStep";
       
    58     static const char *ATTRIBUTE_SLIDER_MAJOR_STEP        = "majorStep";
       
    59 
       
    60     // possible values that can be used in xml for "ATTRIBUTE_LISTBOXTYPE"
       
    61     static const char *SINGLE_LINE_LISTBOX                = "SingleLineListBox";
       
    62     static const char *TWO_LINE_LISTBOX                   = "TwoLineListBox";
       
    63 }
       
    64 
       
    65 
       
    66 CxuiSettingXmlReader::CxuiSettingXmlReader()
       
    67   : mXmlUri(),
       
    68     mParsed(false),
       
    69     mListParams(),
       
    70     mSliderParams()
       
    71 {
       
    72 }
       
    73 
       
    74 
       
    75 CxuiSettingXmlReader::~CxuiSettingXmlReader()
       
    76 {
       
    77     clear();
       
    78 }
       
    79 
       
    80 
       
    81 /*!
       
    82 * Get the settings info based on the setting id
       
    83 * @param setting denotes the setting key id.
       
    84 * @param Returns list of settings specific to radiobuttonlist.
       
    85 * @return Were the params found, i.e. is the setting supported.
       
    86 */
       
    87 CxUiSettings::RadioButtonListParams CxuiSettingXmlReader::getListSettingInfo(const QString &setting, bool &found)
       
    88 {
       
    89     CX_DEBUG_ENTER_FUNCTION();
       
    90     RadioButtonListParams p;
       
    91 
       
    92     // Parse current XML file now if not already done.
       
    93     parse();
       
    94 
       
    95     if (mListParams.contains(setting)){
       
    96         CX_ASSERT_ALWAYS(mListParams.value(setting));
       
    97         p = *mListParams.value(setting);
       
    98         found = true;
       
    99     } else {
       
   100         found = false;
       
   101     }
       
   102 
       
   103     CX_DEBUG_EXIT_FUNCTION();
       
   104     return p;
       
   105 }
       
   106 
       
   107 
       
   108 
       
   109 /*!
       
   110 * Get the settings info based on the setting id
       
   111 * @param setting Denotes the setting key id.
       
   112 * @param found Were the params found, i.e. is the setting supported.
       
   113 * @return Returns list of settings specific to slider setting.
       
   114 */
       
   115 CxUiSettings::SliderParams CxuiSettingXmlReader::getSliderSettingInfo(const QString &setting, bool &found)
       
   116 {
       
   117     CX_DEBUG_ENTER_FUNCTION();
       
   118     SliderParams p;
       
   119 
       
   120     // Parse current XML file now if not already done.
       
   121     parse();
       
   122 
       
   123     if (mSliderParams.contains(setting)){
       
   124         CX_ASSERT_ALWAYS(mSliderParams.value(setting));
       
   125         p = *mSliderParams.value(setting);
       
   126         found = true;
       
   127     } else {
       
   128         found = false;
       
   129     }
       
   130 
       
   131     CX_DEBUG_EXIT_FUNCTION();
       
   132     return p;
       
   133 }
       
   134 
       
   135 /*!
       
   136 * Set new source for setting XML.
       
   137 * @param uri URI for the setting XML. Note: Currently only local file path is supported.
       
   138 */
       
   139 void CxuiSettingXmlReader::setXmlSource(const QString& uri)
       
   140 {
       
   141     CX_DEBUG_ENTER_FUNCTION();
       
   142     CX_DEBUG(("Settings mapping XML: XML source set to [%s]", uri.toAscii().constData()));
       
   143 
       
   144     if(mXmlUri.compare(uri) != 0) {
       
   145         // Clear any old content and set flag that parsing needs to be done again.
       
   146         clear();
       
   147         mXmlUri = uri;
       
   148     }
       
   149     CX_DEBUG_EXIT_FUNCTION();
       
   150 }
       
   151 
       
   152 /*!
       
   153 * Clear existing setting info and raise flag for needed parsing.
       
   154 */
       
   155 void CxuiSettingXmlReader::clear()
       
   156 {
       
   157     CX_DEBUG_ENTER_FUNCTION();
       
   158 
       
   159     mParsed = false;
       
   160 
       
   161     qDeleteAll(mListParams.values());
       
   162     mListParams.clear();
       
   163 
       
   164     qDeleteAll(mSliderParams.values());
       
   165     mSliderParams.clear();
       
   166 
       
   167     CX_DEBUG_EXIT_FUNCTION();
       
   168 }
       
   169 
       
   170 /*!
       
   171 * Parses the XML and creates all settings list defined in XML
       
   172 */
       
   173 void CxuiSettingXmlReader::parse()
       
   174 {
       
   175     CX_DEBUG_ENTER_FUNCTION();
       
   176 
       
   177     if (mParsed) {
       
   178         CX_DEBUG(("Settings mapping XML: Already parsed our XML, using cached content") );
       
   179         CX_DEBUG_EXIT_FUNCTION();
       
   180         return;
       
   181     }
       
   182 
       
   183     QDomDocument document("dummy");
       
   184     QFile file(mXmlUri);
       
   185 
       
   186     if (!file.open(QFile::ReadOnly|QFile::Text)) {
       
   187         CX_DEBUG(("Settings mapping XML: could not open file [%s]", mXmlUri.toAscii().constData()));
       
   188         return;
       
   189     }
       
   190 
       
   191     bool ok = document.setContent(&file);
       
   192     file.close();
       
   193 
       
   194     if (!ok) {
       
   195         CX_DEBUG(("Settings mapping XML: could not set content"));
       
   196         return;
       
   197     }
       
   198 
       
   199     QDomElement root = document.documentElement();
       
   200     if (root.tagName() != TAG_ROOT) {
       
   201         CX_DEBUG(("Settings mapping XML: root name wrong %s", root.tagName().toAscii().constData()));
       
   202         return;
       
   203     }
       
   204 
       
   205     QDomElement e = findChildElement(root, TAG_SETTINGS);
       
   206     if (e.isNull()) {
       
   207         CX_DEBUG(("Settings mapping XML: could not find settings tag"));
       
   208         return;
       
   209     }
       
   210 
       
   211     parseSettings(e);
       
   212 
       
   213     // We've done parsing. No need to do it again.
       
   214     mParsed = true;
       
   215 
       
   216     CX_DEBUG_EXIT_FUNCTION();
       
   217 }
       
   218 
       
   219 
       
   220 /*!
       
   221 * Parses the settings in XML and creates settings requested using the DOM element
       
   222 */
       
   223 void CxuiSettingXmlReader::parseSettings(const QDomElement& element) {
       
   224     CX_DEBUG_ENTER_FUNCTION();
       
   225 
       
   226     if (element.isNull()) {
       
   227         //!@todo throw exception
       
   228         return;
       
   229     }
       
   230 
       
   231     QString tagName;
       
   232     QDomElement e;
       
   233     // Go through list of items
       
   234     for(QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling()) {
       
   235         e = n.toElement();
       
   236         tagName = e.tagName();
       
   237 
       
   238         if (tagName == TAG_SETTING_LIST) {
       
   239             // <setting_list id="WHITE_BALANCE" heading="qtn_setting_title_wb">
       
   240             RadioButtonListParams *p(parseSettingList(e));
       
   241             if (p) {
       
   242                 mListParams.insert(p->mSettingId,p);
       
   243             }
       
   244         } else if(tagName == TAG_SETTING_SLIDER) {
       
   245             // <setting_slider id="EXPOSURE_COMPENSATION" heading="qtn_setting_title_ev">
       
   246             SliderParams *p(parseSettingSlider(e));
       
   247             if (p) {
       
   248                 mSliderParams.insert(p->mSettingId,p);
       
   249             }
       
   250         }
       
   251     }
       
   252 
       
   253     CX_DEBUG_EXIT_FUNCTION();
       
   254 }
       
   255 
       
   256 
       
   257 /*!
       
   258 * Creates the settings list based on the DomElement
       
   259 returns radiobutton list data
       
   260 */
       
   261 CxUiSettings::RadioButtonListParams *CxuiSettingXmlReader::parseSettingList(const QDomElement& element)
       
   262 {
       
   263     CX_DEBUG_ENTER_FUNCTION();
       
   264 
       
   265     // Read the heading and id.
       
   266     if (element.isNull()) {
       
   267         return NULL;
       
   268     }
       
   269 
       
   270     RadioButtonListParams *p = new RadioButtonListParams();
       
   271 
       
   272     CX_DEBUG(("Parsing radio button list for setting id [%s]", element.attribute(ATTRIBUTE_ID).toAscii().constData()));
       
   273 
       
   274     // XML something like this
       
   275     // <setting_list id="WHITE_BALANCE" heading="qtn_setting_title_wb">
       
   276     p->mHeading = hbTrId(element.attribute(ATTRIBUTE_HEADING).toAscii().constData());
       
   277     p->mSettingId = element.attribute(ATTRIBUTE_ID);
       
   278     p->mHeadingIcon = element.attribute(ATTRIBUTE_HEADING_ICON);
       
   279 
       
   280     // Set the list box attributes
       
   281 
       
   282     // Example: preview="1"
       
   283     p->mPreview = element.attribute(ATTRIBUTE_PREVIEW).toInt() == 1;
       
   284 
       
   285     // Example: type="SingleLineListBox"
       
   286     if (element.attribute(ATTRIBUTE_LISTBOXTYPE) == SINGLE_LINE_LISTBOX) {
       
   287         p->mListboxType = CxuiSettingRadioButtonList::SingleLine;
       
   288     } else if(element.attribute(ATTRIBUTE_LISTBOXTYPE) == TWO_LINE_LISTBOX) {
       
   289         p->mListboxType = CxuiSettingRadioButtonList::TwoLine;
       
   290     }
       
   291 
       
   292     QDomElement e;
       
   293     p->mSettingPairList.clear();
       
   294 
       
   295     // Go through list of items
       
   296     // XML something like this
       
   297     // <item value="0" string="qtn_whitebalance_auto" />
       
   298     bool ok(false);
       
   299     int intValue(0);
       
   300     QVariant value;
       
   301     QString string;
       
   302     for (QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling()) {
       
   303         e = n.toElement();
       
   304         CX_DEBUG(("tag name <%s>", e.tagName().toAscii().constData()));
       
   305 
       
   306         // Get the item equivalent engine value from xml.
       
   307         // We need to know later if it is a string or int type, hence the extra effort.
       
   308         // QVariant has auto-conversion even from string to int, so can't use QVariant::canConvert<T>().
       
   309         string = e.attribute(ATTRIBUTE_VALUE);
       
   310         intValue = string.toInt(&ok);
       
   311         if (ok) {
       
   312             value.setValue(intValue);
       
   313         } else {
       
   314             value.setValue(string);
       
   315         }
       
   316 
       
   317         // get the string text id from the xml, and format based on type.
       
   318         string = e.attribute(ATTRIBUTE_LOCALIZATION_ID);
       
   319         if (e.tagName() == TAG_ITEM) {
       
   320             // string without parameters: get localised string
       
   321             string = hbTrId(string.toAscii().constData());
       
   322             CX_DEBUG(("tag name <%s>", e.tagName().toAscii().constData()));
       
   323         } else if (e.tagName() == TAG_ITEM_VARIANT_L1) {
       
   324             // string with numeric parameter: get localised string and add numeric parameter
       
   325             QString l1Value = e.attribute(ATTRIBUTE_LOCALIZATION_ID_L1_VALUE);
       
   326             string = hbTrId(string.toAscii().constData()).arg(l1Value.toInt());
       
   327             CX_DEBUG(("tag name <%s>", e.tagName().toAscii().constData()));
       
   328             CX_DEBUG(("attribute [%s] value[%s]", ATTRIBUTE_LOCALIZATION_ID_L1_VALUE,
       
   329                      string.toAscii().constData()));
       
   330         } else if (e.tagName() == TAG_ITEM_VARIANT_LN) {
       
   331             QString lnValue = e.attribute(ATTRIBUTE_LOCALIZATION_ID_LN_VALUE);
       
   332             // string with count parameter: get localised string and add plural form numeric parameter
       
   333             string = HbParameterLengthLimiter(string.toAscii().constData(), lnValue.toInt());
       
   334             CX_DEBUG(("tag name <%s>", e.tagName().toAscii().constData()));
       
   335             CX_DEBUG(("attribute [%s] value[%s]", ATTRIBUTE_LOCALIZATION_ID_LN_VALUE, string.toAscii().constData()));
       
   336         }
       
   337 
       
   338         if (!string.isNull()) {
       
   339             CxUiSettings::SettingItem setting;
       
   340             setting.mItem = string;
       
   341             setting.mValue = value;
       
   342             setting.mIcon = e.attribute(ATTRIBUTE_ICON);
       
   343             p->mSettingPairList.append(setting);
       
   344         }
       
   345     }
       
   346 
       
   347     CX_DEBUG_EXIT_FUNCTION();
       
   348     return p;
       
   349 }
       
   350 
       
   351 
       
   352 
       
   353 /*!
       
   354 * Creates the slider settings based on the DomElement
       
   355 @param element specifies the dom element
       
   356 returns slider parameters
       
   357 */
       
   358 CxUiSettings::SliderParams *CxuiSettingXmlReader::parseSettingSlider(const QDomElement& element)
       
   359 {
       
   360     const double SLIDER_ZERO = 0.0;
       
   361 
       
   362     CX_DEBUG_ENTER_FUNCTION();
       
   363 
       
   364     // Read the heading and id.
       
   365     if (element.isNull()) {
       
   366         return NULL;
       
   367     }
       
   368 
       
   369     SliderParams *p = new SliderParams();
       
   370 
       
   371     CX_DEBUG(("Parsing slider for setting id [%s]", element.attribute(ATTRIBUTE_ID).toAscii().constData()));
       
   372 
       
   373     // XML something like this
       
   374     // <setting_list id="WHITE_BALANCE" heading="qtn_setting_title_wb">
       
   375     p->mHeading = hbTrId(element.attribute(ATTRIBUTE_HEADING).toAscii().constData());
       
   376     p->mSettingId = element.attribute(ATTRIBUTE_ID);
       
   377     p->mHeadingIcon = element.attribute(ATTRIBUTE_HEADING_ICON);
       
   378 
       
   379     QDomNode n = element.firstChild();
       
   380     QDomElement e = n.toElement();
       
   381 
       
   382     p->mSettingStrings.clear();
       
   383 
       
   384     CX_DEBUG(("tag name <%s>", e.tagName().toAscii().constData()));
       
   385 
       
   386     if (e.tagName() == TAG_SETUP) {
       
   387         // get the min text id from the xml
       
   388         QString minString = e.attribute(ATTRIBUTE_SLIDER_MIN);
       
   389 
       
   390         // get the max text id from the xml
       
   391         QString maxString = e.attribute(ATTRIBUTE_SLIDER_MAX);
       
   392 
       
   393         // get the step text id from the xml
       
   394         QVariant stepString = e.attribute(ATTRIBUTE_SLIDER_MINOR_STEP);
       
   395         p->mMinorStep = stepString.toReal();
       
   396         stepString = e.attribute(ATTRIBUTE_SLIDER_MAJOR_STEP);
       
   397         p->mMajorStep = stepString.toReal();
       
   398 
       
   399         // get the l1 value from xml to generate strings for slider
       
   400         QString l1Value = e.attribute(ATTRIBUTE_LOCALIZATION_ID_L1_VALUE);
       
   401 
       
   402         // Don't create labels for slider if no strings are defined in settinsg xml
       
   403         // Create labels only for major ticks
       
   404         if (!minString.isEmpty() && !maxString.isEmpty()) {
       
   405             qreal step = p->mMajorStep;
       
   406             qreal value = l1Value.toDouble();
       
   407             // generating all negative valued strings for slider setting
       
   408             while (value > 0) {
       
   409                 // format the setting string
       
   410                 QString str =  hbTrId(minString.toAscii().constData()).arg(value,0,'f',1);
       
   411                 p->mSettingStrings.append(str);
       
   412                 value = value - step;
       
   413             }
       
   414 
       
   415             HbExtendedLocale locale = HbExtendedLocale::system();
       
   416             QString zeroString = locale.toString(SLIDER_ZERO, 'f', 1);
       
   417             p->mSettingStrings.append(zeroString);
       
   418 
       
   419             // generating all non-negative valued strings for slider setting
       
   420             step = p->mMajorStep;
       
   421             value = step;
       
   422             while(value <= l1Value.toInt()) {
       
   423                 // format the setting string
       
   424                 QString str = hbTrId(maxString.toAscii().constData()).arg(value,0,'f',1);
       
   425                 p->mSettingStrings.append(str);
       
   426                 value = value + step;
       
   427             }
       
   428         }
       
   429 
       
   430         int min = -l1Value.toInt();
       
   431         int max = l1Value.toInt();
       
   432 
       
   433         p->mRange = qMakePair(min, max);
       
   434     }
       
   435 
       
   436     CX_DEBUG_EXIT_FUNCTION();
       
   437     return p;
       
   438 }
       
   439 
       
   440 
       
   441 
       
   442 /*!
       
   443 * CxuiSettingXmlReader::findChildElement
       
   444 */
       
   445 QDomElement CxuiSettingXmlReader::findChildElement(const QDomNode& parent, const QString& name) {
       
   446     CX_DEBUG(("findChildElement name[%s]", name.toAscii().constData()));
       
   447 
       
   448     for (QDomNode n = parent.firstChild(); !n.isNull(); n = n.nextSibling()) {
       
   449         QDomElement e = n.toElement();
       
   450         if (e.tagName() == name) {
       
   451             return e;
       
   452         }
       
   453     }
       
   454 
       
   455     return QDomElement();
       
   456 }