stifui/qt/src/uisetting.cpp
changeset 38 aca9d8bb5fbc
parent 31 e7a04a6385be
child 39 a5449825eaf3
equal deleted inserted replaced
31:e7a04a6385be 38:aca9d8bb5fbc
     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: QT C++ based Class.
       
    15  *
       
    16  */
       
    17 
       
    18 #include <QFile>
       
    19 #include <QTextStream>
       
    20 #include <QDateTime>
       
    21 #include "uisetting.h"
       
    22 const QString SETTINGFILE = "c:\\TestFramework\\StfQtUISetting.ini";
       
    23 
       
    24 UiSetting::UiSetting()
       
    25     {
       
    26     if(!load())
       
    27         {
       
    28         loadDefault();
       
    29         }
       
    30     }
       
    31 
       
    32 UiSetting::~UiSetting()
       
    33     {
       
    34     }
       
    35 
       
    36 QString UiSetting::ReadSetting(const QString& item)
       
    37     {
       
    38     QString value = "";
       
    39     if(settingList.contains(item))
       
    40         {
       
    41         value = settingList.value(item);
       
    42         }
       
    43     else
       
    44         {
       
    45         value = getDefaultValue(item);
       
    46         settingList.insert(item, value);
       
    47         }
       
    48     return value;
       
    49     }
       
    50 
       
    51 
       
    52 void UiSetting::SetSetting(const QString& item, const QString& value)
       
    53     {
       
    54     if(settingList.contains(item))
       
    55         {
       
    56         settingList.remove(item);
       
    57         }
       
    58     settingList.insert(item, value);
       
    59     save();
       
    60     }
       
    61 
       
    62 
       
    63 void UiSetting::loadDefault()
       
    64     {
       
    65     settingList.clear();
       
    66     settingList.insert(KShowOutput, getDefaultValue(KShowOutput));
       
    67     settingList.insert(KStyleSheet, getDefaultValue(KStyleSheet));
       
    68     settingList.insert(KFilter, getDefaultValue(KFilter));
       
    69     settingList.insert(KFilterCaseSens, getDefaultValue(KFilterCaseSens));
       
    70     //add mor default setting here.
       
    71     }
       
    72 
       
    73 QString UiSetting::getDefaultValue(const QString& item)
       
    74     {
       
    75     QString result = "";
       
    76     if(item == KShowOutput)
       
    77         {
       
    78         result = "true";
       
    79         }
       
    80     else if(item == KStyleSheet)
       
    81         {
       
    82         result = ":/qss/coffee.qss";
       
    83         }
       
    84     else if(item == KFilter)
       
    85         {
       
    86         result = "";
       
    87         }
       
    88     else if(item == KFilterCaseSens)
       
    89         {
       
    90         result = "false";
       
    91         }
       
    92     return result;
       
    93     }
       
    94 
       
    95 bool UiSetting::load()
       
    96     {
       
    97     QFile file(SETTINGFILE);
       
    98     if(!file.open(QIODevice::ReadOnly))
       
    99         {
       
   100         return false;
       
   101         }
       
   102     QTextStream in(&file);
       
   103     QString line, item, value;
       
   104     int index;
       
   105     while(!in.atEnd())
       
   106         {
       
   107         line = in.readLine().trimmed();
       
   108         if(!line.startsWith("//"))
       
   109             {
       
   110             index = line.indexOf("=");
       
   111             if(index > 0 && index < line.length() - 1)
       
   112                 {
       
   113                 item = line.left(index).trimmed();
       
   114                 value = line.right(line.length() - index  -1);
       
   115                 if(item == KFilter) //For filter do not care about stored value
       
   116                     value = "";
       
   117                 settingList.insert(item, value);
       
   118                 }
       
   119             }
       
   120         //end while.
       
   121         }
       
   122     return true;
       
   123     }
       
   124 
       
   125 bool UiSetting::save()
       
   126     {
       
   127     QFile file(SETTINGFILE);
       
   128     if(!file.open(QIODevice::WriteOnly))
       
   129         {
       
   130         return false;
       
   131         }
       
   132     QTextStream in(&file);
       
   133     in << "//STFQtUI Setting.\r\n";
       
   134     in << "//Created at: " + QDateTime::currentDateTime().toString("yyyy.mm.dd hh:mm::ss");
       
   135     in << "\r\n";
       
   136     for(int i=0;i< settingList.size();i++)
       
   137         {
       
   138         in << settingList.keys()[i];
       
   139         in << "=";
       
   140         in << settingList.value(settingList.keys()[i]);
       
   141         in << "\r\n";
       
   142         }
       
   143     return true;    
       
   144     }
       
   145 
       
   146 // End of File