javauis/mmapi_qt/baseline/src/cmmaplayerproperties.cpp
changeset 23 98ccebc37403
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
       
     1 /*
       
     2 * Copyright (c) 2002 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 is used for storing and parsing properties
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 #include <logger.h>
       
    21 #include "cmmaplayerproperties.h"
       
    22 
       
    23 const TInt KPropertyNameEndChar = '=';
       
    24 const TInt KPropertyEndChar = '&';
       
    25 const TInt KDefaultGranularity = 8;
       
    26 
       
    27 CMMAPlayerProperties* CMMAPlayerProperties::NewL(const TDesC& aProperties,
       
    28         MMMAParameterRule& aRule)
       
    29 {
       
    30     CMMAPlayerProperties* props = new(ELeave) CMMAPlayerProperties(aProperties, aRule);
       
    31     return props;
       
    32 }
       
    33 
       
    34 CMMAPlayerProperties::~CMMAPlayerProperties()
       
    35 {
       
    36 }
       
    37 
       
    38 
       
    39 CMMAPlayerProperties::CMMAPlayerProperties(const TDesC& aProperties, MMMAParameterRule& aRule) :
       
    40         iProperties(aProperties),
       
    41         iRule(aRule)
       
    42 {
       
    43 }
       
    44 
       
    45 TBool CMMAPlayerProperties::GetProperty(const TDesC& aKey,
       
    46                                         TInt& aValue) const
       
    47 {
       
    48     return iRule.FindProperty(aKey, aValue);
       
    49 }
       
    50 
       
    51 TBool CMMAPlayerProperties::GetProperty(const TDesC& aKey, TPtrC& aValue) const
       
    52 {
       
    53     return iRule.FindProperty(aKey, aValue);
       
    54 }
       
    55 
       
    56 TBool CMMAPlayerProperties::Compare(const TDesC& aKey, const TDesC& aValue) const
       
    57 {
       
    58     return iRule.Compare(aKey, aValue);
       
    59 }
       
    60 
       
    61 TBool CMMAPlayerProperties::Compare(const TDesC& aKey, const TInt& aValue) const
       
    62 {
       
    63     return iRule.Compare(aKey, aValue);
       
    64 }
       
    65 
       
    66 // Parse property values: "key=value&key2=value2&..."
       
    67 void CMMAPlayerProperties::ValidateL() const
       
    68 {
       
    69     // Empty properties are valid
       
    70     if (iProperties.Length() == 0)
       
    71     {
       
    72         return;
       
    73     }
       
    74     TInt pos = 0;
       
    75     while (pos != KErrNotFound)
       
    76     {
       
    77         // Getting everything from 'pos' to end and finding '&'
       
    78         TPtrC property = iProperties.Mid(pos);
       
    79         TInt valueEndPos = property.Locate(KPropertyEndChar);
       
    80 
       
    81         if (valueEndPos != KErrNotFound)
       
    82         {
       
    83             // This is not last property, clipping remaining chars
       
    84             property.Set(iProperties.Mid(pos, valueEndPos));
       
    85         }
       
    86 
       
    87         TInt keyEndPos = property.Locate(KPropertyNameEndChar);
       
    88         // if we are getting KErrNotFound then properties is not valid
       
    89         if (keyEndPos == KErrNotFound)
       
    90         {
       
    91             User::Leave(KErrArgument);
       
    92         }
       
    93 
       
    94         // "key=value"
       
    95         TPtrC propertyKey(property.Left(keyEndPos));
       
    96         TPtrC propertyValue(property.Mid(keyEndPos + 1));
       
    97 
       
    98         // check is the pair valid, leave if not
       
    99         // check also that this key was expected, leave if not
       
   100         TPtrC tmp;
       
   101         TInt tmpInt;
       
   102         if (!iRule.ValidateAndStoreL(propertyKey, propertyValue) ||
       
   103                 !(iRule.FindProperty(propertyKey, tmp) ||
       
   104                   iRule.FindProperty(propertyKey, tmpInt)))
       
   105         {
       
   106             User::Leave(KErrArgument);
       
   107         }
       
   108 
       
   109         // prepare for next token or mark end
       
   110         if (valueEndPos != KErrNotFound)
       
   111         {
       
   112             pos += valueEndPos + 1;
       
   113         }
       
   114         else
       
   115         {
       
   116             pos = valueEndPos;
       
   117         }
       
   118     }
       
   119 }
       
   120 
       
   121 
       
   122 CMMAParameterRuleSet* CMMAParameterRuleSet::NewLC()
       
   123 {
       
   124     CMMAParameterRuleSet* self = new(ELeave)CMMAParameterRuleSet();
       
   125     CleanupStack::PushL(self);
       
   126     self->ConstructL();
       
   127     return self;
       
   128 }
       
   129 
       
   130 CMMAParameterRuleSet::~CMMAParameterRuleSet()
       
   131 {
       
   132     if (iRules)
       
   133     {
       
   134         iRules->Reset();
       
   135         delete iRules;
       
   136     }
       
   137 }
       
   138 
       
   139 CMMAParameterRuleSet::CMMAParameterRuleSet()
       
   140 {
       
   141 }
       
   142 
       
   143 void CMMAParameterRuleSet::ConstructL()
       
   144 {
       
   145     iRules = new(ELeave)CArrayPtrSeg< MMMAParameterRule >(KDefaultGranularity);
       
   146 }
       
   147 
       
   148 #define LOOPUNTILRULE( endRule ) \
       
   149     TInt rules = iRules->Count(); \
       
   150     TInt i( 0 ); \
       
   151     while( ( i < rules ) && \
       
   152            endRule ) \
       
   153         { \
       
   154         i++; \
       
   155         }
       
   156 
       
   157 TBool CMMAParameterRuleSet::ValidateAndStoreL(const TDesC& aKey, const TDesC& aValue)
       
   158 {
       
   159     LOOPUNTILRULE(iRules->At(i)->ValidateAndStoreL(aKey, aValue));
       
   160     // if loop has ended before end then some validation has failed.
       
   161     return (i == rules);
       
   162 }
       
   163 
       
   164 TBool CMMAParameterRuleSet::Compare(const TDesC& aKey, const TDesC& aValue)
       
   165 {
       
   166     LOOPUNTILRULE(!iRules->At(i)->Compare(aKey, aValue));
       
   167     // if loop has ended before end then key-value pair has been found
       
   168     return (i != rules);
       
   169 }
       
   170 
       
   171 TBool CMMAParameterRuleSet::Compare(const TDesC& aKey, const TInt aValue)
       
   172 {
       
   173     LOOPUNTILRULE(!iRules->At(i)->Compare(aKey, aValue));
       
   174     // if loop has ended before end then key-value pair has been found
       
   175     return (i != rules);
       
   176 }
       
   177 
       
   178 TBool CMMAParameterRuleSet::FindProperty(const TDesC& aKey, TPtrC& aValue)
       
   179 {
       
   180     LOOPUNTILRULE(!iRules->At(i)->FindProperty(aKey, aValue));
       
   181     // if loop has ended before end then key has been found
       
   182     return (i != rules);
       
   183 }
       
   184 
       
   185 TBool CMMAParameterRuleSet::FindProperty(const TDesC& aKey, TInt& aValue)
       
   186 {
       
   187     LOOPUNTILRULE(!iRules->At(i)->FindProperty(aKey, aValue));
       
   188     // if loop has ended before end then key has been found
       
   189     return (i != rules);
       
   190 }
       
   191 
       
   192 void CMMAParameterRuleSet::AppendRuleL(MMMAParameterRule* aRule)
       
   193 {
       
   194     iRules->AppendL(aRule);
       
   195 }
       
   196 
       
   197 TMMAParameterRuleBase::TMMAParameterRuleBase(const TDesC& aKey) :
       
   198         iKey(aKey),
       
   199         iAssigned(EFalse)
       
   200 {}
       
   201 
       
   202 TBool TMMAParameterRuleBase::ValidateAndStoreL(const TDesC& aKey, const TDesC& aValue)
       
   203 {
       
   204     if ((aKey.Length() == 0) ||
       
   205             (aValue.Length() == 0))
       
   206     {
       
   207         // key or value length is zero, fail
       
   208         User::Leave(KErrArgument);
       
   209     }
       
   210 
       
   211     TBool isValid = ETrue;
       
   212     // this is same key as in rule
       
   213     if (iKey.Compare(aKey) == 0)
       
   214     {
       
   215         //if this key is already assigned then there is same key more than once
       
   216         if (iAssigned)
       
   217         {
       
   218             User::Leave(KErrArgument);
       
   219         }
       
   220         isValid = ValidateValueL(aValue);
       
   221 
       
   222         // if it was valid we mark this key as assigned
       
   223         if (isValid)
       
   224         {
       
   225             iAssigned = ETrue;
       
   226         }
       
   227     }
       
   228     return isValid;
       
   229 }
       
   230 
       
   231 TBool TMMAParameterRuleBase::Compare(const TDesC& /*aKey*/, const TDesC& /*aValue*/)
       
   232 {
       
   233     // by default we do not found this key
       
   234     return EFalse;
       
   235 }
       
   236 
       
   237 TBool TMMAParameterRuleBase::Compare(const TDesC& /*aKey*/, const TInt /*aValue*/)
       
   238 {
       
   239     // by default we do not found this key
       
   240     return EFalse;
       
   241 }
       
   242 
       
   243 TBool TMMAParameterRuleBase::FindProperty(const TDesC& /*aKey*/, TPtrC& /*aValue*/)
       
   244 {
       
   245     // by default we do not found this key
       
   246     return EFalse;
       
   247 }
       
   248 
       
   249 TBool TMMAParameterRuleBase::FindProperty(const TDesC& /*aKey*/, TInt& /*aValue*/)
       
   250 {
       
   251     // by default we do not found this key
       
   252     return EFalse;
       
   253 }
       
   254 
       
   255 TMMAParameterRuleInt::TMMAParameterRuleInt(const TDesC& aKey) :
       
   256         TMMAParameterRuleBase(aKey),
       
   257         iUpperLimit(KMaxTInt),
       
   258         iLowerLimit(KMinTInt)
       
   259 {}
       
   260 
       
   261 TMMAParameterRuleInt::TMMAParameterRuleInt(const TDesC& aKey,
       
   262         const TInt aLowerLimit) :
       
   263         TMMAParameterRuleBase(aKey),
       
   264         iUpperLimit(KMaxTInt),
       
   265         iLowerLimit(aLowerLimit)
       
   266 {}
       
   267 
       
   268 TMMAParameterRuleInt::TMMAParameterRuleInt(const TDesC& aKey,
       
   269         const TInt aLowerLimit,
       
   270         const TInt aUpperLimit) :
       
   271         TMMAParameterRuleBase(aKey),
       
   272         iUpperLimit(aUpperLimit),
       
   273         iLowerLimit(aLowerLimit)
       
   274 {}
       
   275 
       
   276 TBool TMMAParameterRuleInt::ValidateValueL(const TDesC& aValue)
       
   277 {
       
   278     TLex lex(aValue);
       
   279     TInt valueInt = 0;
       
   280     if ((lex.Val(valueInt) != KErrNone) ||
       
   281             !lex.Eos())
       
   282     {
       
   283         User::Leave(KErrArgument);
       
   284     }
       
   285     if ((valueInt < iLowerLimit) ||
       
   286             (valueInt > iUpperLimit))
       
   287     {
       
   288         // value is not valid
       
   289         return EFalse;
       
   290     }
       
   291     else
       
   292     {
       
   293         // value is valid, store it
       
   294         iValue = valueInt;
       
   295         return ETrue;
       
   296     }
       
   297 }
       
   298 
       
   299 TBool TMMAParameterRuleInt::Compare(const TDesC& aKey, const TInt aValue)
       
   300 {
       
   301     TBool match = EFalse;
       
   302     if (iAssigned &&
       
   303             (iKey.Compare(aKey) == 0) &&
       
   304             (aValue == iValue))
       
   305     {
       
   306         match = ETrue;
       
   307     }
       
   308     return match;
       
   309 }
       
   310 
       
   311 TBool TMMAParameterRuleInt::FindProperty(const TDesC& aKey, TInt& aValue)
       
   312 {
       
   313     TBool match = EFalse;
       
   314     if (iAssigned &&
       
   315             iKey.Compare(aKey) == 0)
       
   316     {
       
   317         aValue = iValue;
       
   318         match = ETrue;
       
   319     }
       
   320     return match;
       
   321 }
       
   322 
       
   323 TMMAParameterRuleDes::TMMAParameterRuleDes(const TDesC& aKey) :
       
   324         TMMAParameterRuleBase(aKey),
       
   325         iValidValues(NULL),
       
   326         iArraySize(0)
       
   327 {}
       
   328 
       
   329 TMMAParameterRuleDes::TMMAParameterRuleDes(const TDesC& aKey,
       
   330         const TMMAStaticStrArray* aValidValues,
       
   331         const TInt aArraySize) :
       
   332         TMMAParameterRuleBase(aKey),
       
   333         iValidValues(aValidValues),
       
   334         iArraySize(aArraySize)
       
   335 {}
       
   336 
       
   337 TBool TMMAParameterRuleDes::ValidateValueL(const TDesC& aValue)
       
   338 {
       
   339     TInt i = 0;
       
   340     while ((i < iArraySize) &&
       
   341             aValue.Compare(iValidValues[i]()) != 0)
       
   342     {
       
   343         i++;
       
   344     }
       
   345     // if there is not valid values then we treat every value as valid
       
   346     if (iValidValues &&
       
   347             (i == iArraySize))
       
   348     {
       
   349         // there was no hit
       
   350         return EFalse;
       
   351     }
       
   352 
       
   353     // find match or every value is valid, storing
       
   354     iValue.Set(aValue);
       
   355     return ETrue;
       
   356 }
       
   357 
       
   358 TBool TMMAParameterRuleDes::Compare(const TDesC& aKey, const TDesC& aValue)
       
   359 {
       
   360     TBool match = EFalse;
       
   361     if (iAssigned &&
       
   362             (iKey.Compare(aKey) == 0) &&
       
   363             (iValue.Compare(aValue) == 0))
       
   364     {
       
   365         match = ETrue;
       
   366     }
       
   367     return match;
       
   368 }
       
   369 
       
   370 TBool TMMAParameterRuleDes::FindProperty(const TDesC& aKey, TPtrC& aValue)
       
   371 {
       
   372     TBool match = EFalse;
       
   373     if (iAssigned &&
       
   374             iKey.Compare(aKey) == 0)
       
   375     {
       
   376         aValue.Set(iValue);
       
   377         match = ETrue;
       
   378     }
       
   379     return match;
       
   380 }
       
   381 //  END OF FILE