qtmobileextensions/src/settingsmanager/settingshandlerbase.cpp
branchRCL_3
changeset 9 5d007b20cfd0
equal deleted inserted replaced
8:885c2596c964 9:5d007b20cfd0
       
     1 /*
       
     2 * Copyright (c) 2009 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 "settingshandlerbase.h"
       
    23 
       
    24 #include "xqsettingskey.h"
       
    25 
       
    26 QVariant SettingsHandlerBase::handleReadItemValue(const XQSettingsKey& key, XQSettingsManager::Type type, TInt& error)
       
    27 {
       
    28     const TInt KRBufDefaultLength = 32;
       
    29     switch(type)
       
    30     {
       
    31         case XQSettingsManager::TypeVariant:
       
    32         {
       
    33             //Try to read TInt
       
    34             TInt intValue;
       
    35             error = getValue(key.key(), intValue);
       
    36             if (error == KErrNone)
       
    37             {
       
    38                 return QVariant(intValue);
       
    39             }
       
    40 
       
    41             //Try to read TReal
       
    42             TReal realValue;
       
    43             error = getValue(key.key(), realValue);
       
    44             if (error == KErrNone)
       
    45             {
       
    46                 return QVariant(realValue);
       
    47             }
       
    48 
       
    49             //Try to read RBuf8
       
    50             QVariant byteArrayVariant;
       
    51             TRAP(error,
       
    52                 RBuf8 tdes8Value;
       
    53                 tdes8Value.CreateL(KRBufDefaultLength);
       
    54                 CleanupClosePushL(tdes8Value);
       
    55                 getValueL(key.key(), tdes8Value);
       
    56                 byteArrayVariant.setValue(QByteArray((const char*)tdes8Value.Ptr(), tdes8Value.Length()));
       
    57                 CleanupStack::PopAndDestroy(&tdes8Value);
       
    58             )
       
    59             if (error == KErrNone)
       
    60             {
       
    61                 return byteArrayVariant;
       
    62             }
       
    63             break;
       
    64         }
       
    65         case XQSettingsManager::TypeInt:
       
    66         {
       
    67             //Try to read TInt
       
    68             TInt intValue;
       
    69             error = getValue(key.key(), intValue);
       
    70             if (error == KErrNone)
       
    71             {
       
    72                 return QVariant(intValue);
       
    73             }
       
    74             break;
       
    75         }
       
    76         case XQSettingsManager::TypeDouble:
       
    77         {
       
    78             //Try to read TReal
       
    79             TReal realValue;
       
    80             error = getValue(key.key(), realValue);
       
    81             if (error == KErrNone)
       
    82             {
       
    83                 return QVariant(realValue);
       
    84             }
       
    85             break;
       
    86         }
       
    87         case XQSettingsManager::TypeString:
       
    88         {
       
    89             //Try to read RBuf8
       
    90             QVariant stringVariant;
       
    91             TRAP(error,
       
    92                 RBuf16 tdes16Value;
       
    93                 tdes16Value.CreateL(KRBufDefaultLength);
       
    94                 CleanupClosePushL(tdes16Value);
       
    95                 getValueL(key.key(), tdes16Value);
       
    96                 stringVariant.setValue(QString::fromUtf16(tdes16Value.Ptr(), tdes16Value.Length()));
       
    97                 CleanupStack::PopAndDestroy(&tdes16Value);
       
    98             )
       
    99             if (error == KErrNone)
       
   100             {
       
   101                 return stringVariant;
       
   102             }
       
   103             break;
       
   104         }
       
   105         case XQSettingsManager::TypeByteArray:
       
   106         {
       
   107             //Try to read RBuf8
       
   108             QVariant byteArrayVariant;
       
   109             TRAP(error,
       
   110                 RBuf8 tdes8Value;
       
   111                 tdes8Value.CreateL(KRBufDefaultLength);
       
   112                 CleanupClosePushL(tdes8Value);
       
   113                 getValueL(key.key(), tdes8Value);
       
   114                 byteArrayVariant.setValue(QByteArray((const char*)tdes8Value.Ptr(), tdes8Value.Length()));
       
   115                 CleanupStack::PopAndDestroy(&tdes8Value);
       
   116             )
       
   117             if (error == KErrNone)
       
   118             {
       
   119                 return byteArrayVariant;
       
   120             }
       
   121             break;
       
   122         }
       
   123         default:
       
   124         {
       
   125             break;
       
   126         }
       
   127     };
       
   128 
       
   129     return QVariant();
       
   130 }
       
   131 
       
   132 bool SettingsHandlerBase::handleWriteItemValue(const XQSettingsKey& key, const QVariant& value, TInt& error)
       
   133 {
       
   134     switch (value.type())
       
   135     {
       
   136         case QVariant::Int:
       
   137         {
       
   138             error = setValue(key.key(), value.toInt());
       
   139             break;
       
   140         }
       
   141         case QVariant::Double:
       
   142         {
       
   143             error = setValue(key.key(), value.toDouble());
       
   144             break;
       
   145         }
       
   146         case QVariant::String:
       
   147         {
       
   148             error = setValue(key.key(), TPtrC16(reinterpret_cast<const TUint16*>(value.toString().utf16())));
       
   149             break;
       
   150         }
       
   151         case QVariant::ByteArray:
       
   152         {
       
   153             QByteArray byteArray(value.toByteArray());
       
   154             error = setValue(key.key(), TPtrC8((TUint8*)(byteArray.constData()), byteArray.size()));
       
   155             break;
       
   156         }
       
   157         default:
       
   158         {
       
   159             error = KErrArgument;
       
   160             break;
       
   161         }
       
   162     }
       
   163     return error == KErrNone;
       
   164 }