contentpublishingsrv/contentpublishingserver/cpserver/src/cpactiondatacache.cpp
branchRCL_3
changeset 14 15e4dd19031c
child 15 a0713522ab97
equal deleted inserted replaced
12:502e5d91ad42 14:15e4dd19031c
       
     1 /*
       
     2 * Copyright (c) 2008 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:  ?Description
       
    15 
       
    16  *
       
    17 */
       
    18 
       
    19 
       
    20 #include <liwcommon.h>
       
    21 #include <liwvariant.h>
       
    22 #include <s32mem.h>
       
    23 
       
    24 #include "cpactiondatacache.h"
       
    25 #include "cpglobals.h"
       
    26 
       
    27 using namespace LIW;
       
    28 
       
    29 _LIT8(KCachedMap, "cached_map");
       
    30 
       
    31 static const int KMaxCacheItems = 6; 
       
    32 
       
    33 
       
    34 // ======== MEMBER FUNCTIONS ========
       
    35 
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // 
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 CCPActionDataCache* CCPActionDataCache::NewL()
       
    42     {
       
    43     CCPActionDataCache* self = CCPActionDataCache::NewLC();
       
    44     CleanupStack::Pop(self);
       
    45     return self;
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // 
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 CCPActionDataCache* CCPActionDataCache::NewLC()
       
    53     {
       
    54     CCPActionDataCache* self = new (ELeave) CCPActionDataCache;
       
    55     CleanupStack::PushL(self);
       
    56     self->ConstructL();
       
    57     return self;
       
    58     }
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // 
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 void CCPActionDataCache::ConstructL( )
       
    65     {
       
    66     iInternalList = CLiwDefaultList::NewL();
       
    67     }
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // 
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 CCPActionDataCache::CCPActionDataCache()
       
    74     {
       
    75     }
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 // 
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 CCPActionDataCache::~CCPActionDataCache()
       
    82     {
       
    83     iInternalList->Close();
       
    84     }
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // 
       
    88 // ---------------------------------------------------------------------------
       
    89 //
       
    90 void CCPActionDataCache::HandleCacheRemoveL(const CLiwMap* aMap)
       
    91     {
       
    92     TInt id = FindL(aMap);
       
    93     if (id != KErrNotFound)
       
    94         {
       
    95         iInternalList->Remove(id);
       
    96         }
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // 
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 void CCPActionDataCache::AppendL( const CLiwGenericParamList* aParamList)
       
   104     {
       
   105     const TLiwGenericParam* param;
       
   106     TInt pos( 0);
       
   107     param = aParamList->FindFirst( pos, KListMap );
       
   108 
       
   109     if (param && param->Value().TypeId() == EVariantTypeMap)
       
   110         {
       
   111         const CLiwMap* inputMap = param->Value().AsMap();
       
   112         CLiwDefaultMap* map = CLiwDefaultMap::NewLC();
       
   113         CopyVariantL(KId, inputMap, map);
       
   114         CopyVariantL(KPublisherId, inputMap, map);
       
   115         CopyVariantL(KContentType, inputMap, map);
       
   116         CopyVariantL(KContentId, inputMap, map);
       
   117         map->InsertL(KCachedMap, TLiwVariant(inputMap));
       
   118         iInternalList->AppendL(TLiwVariant(map));
       
   119         CleanupStack::PopAndDestroy(map);
       
   120     
       
   121         if (iInternalList->Count() > KMaxCacheItems)
       
   122             {
       
   123             iInternalList->Remove(0);
       
   124             }
       
   125         }
       
   126     }
       
   127 
       
   128 // ---------------------------------------------------------------------------
       
   129 // 
       
   130 // ---------------------------------------------------------------------------
       
   131 //
       
   132 TBool CCPActionDataCache::IsCacheableL(const CLiwMap* aMap)
       
   133     {
       
   134     TBool result(EFalse);
       
   135     TLiwVariant value;
       
   136     value.PushL();
       
   137 
       
   138     if (aMap->FindL(KId, value) && value.AsTInt32() > 0)
       
   139         {
       
   140         result = ETrue;
       
   141         }
       
   142     else if (IsSpecifiedL(aMap, KPublisherId) && IsSpecifiedL(aMap,
       
   143             KContentType) && IsSpecifiedL(aMap, KContentId))
       
   144         {
       
   145         result = ETrue;
       
   146         }
       
   147 
       
   148     CleanupStack::PopAndDestroy(&value);
       
   149     return result;
       
   150     }
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // 
       
   154 // ---------------------------------------------------------------------------
       
   155 //
       
   156 TBool CCPActionDataCache::ExistL(const CLiwMap* aMap)
       
   157     {
       
   158     TBool result(EFalse);
       
   159     if (FindL(aMap) != KErrNotFound)
       
   160         {
       
   161         result = ETrue;
       
   162         }
       
   163     return result;
       
   164     }
       
   165 
       
   166 // ---------------------------------------------------------------------------
       
   167 // 
       
   168 // ---------------------------------------------------------------------------
       
   169 //
       
   170 void CCPActionDataCache::GetL(const CLiwMap* aMap,
       
   171         CLiwGenericParamList* aParamList)
       
   172     {
       
   173     TInt id = FindL(aMap);
       
   174     if (id != KErrNotFound)
       
   175         {
       
   176         TLiwVariant value;
       
   177         value.PushL();
       
   178         iInternalList->AtL(id, value);
       
   179         const CLiwMap* map = value.AsMap();
       
   180         if (map->FindL(KCachedMap, value))
       
   181             {
       
   182             CLiwDefaultMap* outMap = CLiwDefaultMap::NewLC();
       
   183             value.Get(*outMap);
       
   184             TLiwGenericParam genericParam(KListMap, TLiwVariant(outMap));
       
   185             aParamList->AppendL(genericParam);
       
   186             CleanupStack::PopAndDestroy(outMap);
       
   187             }
       
   188         CleanupStack::PopAndDestroy(&value);
       
   189         }
       
   190     }
       
   191 
       
   192 // ---------------------------------------------------------------------------
       
   193 // 
       
   194 // ---------------------------------------------------------------------------
       
   195 //
       
   196 TBool CCPActionDataCache::MatchL(const CLiwMap* aCachedMap,
       
   197         const CLiwMap* aInputMap)
       
   198     {
       
   199     TBool idMatch(EFalse);
       
   200     TLiwVariant l, r;
       
   201     l.PushL();
       
   202     r.PushL();
       
   203     if (aCachedMap->FindL(KId, l) && aInputMap->FindL(KId, r) && l.AsTInt32()
       
   204             == r.AsTInt32())
       
   205         {
       
   206         idMatch = ETrue;
       
   207         }
       
   208     if (!idMatch)
       
   209         {
       
   210         if (MatchL(aCachedMap, aInputMap, KPublisherId) && MatchL(aCachedMap,
       
   211                 aInputMap, KContentType) && MatchL(aCachedMap, aInputMap,
       
   212                 KContentId))
       
   213             {
       
   214             idMatch = ETrue;
       
   215             }
       
   216         }
       
   217 
       
   218     CleanupStack::PopAndDestroy(&r);
       
   219     CleanupStack::PopAndDestroy(&l);
       
   220     return idMatch;
       
   221     }
       
   222 
       
   223 // ---------------------------------------------------------------------------
       
   224 // 
       
   225 // ---------------------------------------------------------------------------
       
   226 //
       
   227 TBool CCPActionDataCache::MatchL(const CLiwMap* aLeft, const CLiwMap* aRight,
       
   228         const TDesC8& aKey)
       
   229     {
       
   230     TBool match(EFalse);
       
   231     TLiwVariant l, r;
       
   232     l.PushL();
       
   233     r.PushL();
       
   234     if (aLeft->FindL(aKey, l) && aRight->FindL(aKey, r)
       
   235             && !l.AsDes().Compare(r.AsDes()))
       
   236         {
       
   237         match = ETrue;
       
   238         }
       
   239     CleanupStack::PopAndDestroy(&r);
       
   240     CleanupStack::PopAndDestroy(&l);
       
   241     return match;
       
   242     }
       
   243 
       
   244 // ---------------------------------------------------------------------------
       
   245 // 
       
   246 // ---------------------------------------------------------------------------
       
   247 //
       
   248 TBool CCPActionDataCache::IsSpecifiedL(const CLiwMap* aMap,
       
   249         const TDesC8& aKey)
       
   250     {
       
   251     TBool result(EFalse);
       
   252     TLiwVariant value;
       
   253     value.PushL();
       
   254     if (aMap->FindL(aKey, value) && value.AsDes().Compare(KNullDesC) != 0
       
   255             && value.AsDes().Compare(KAll) != 0)
       
   256         {
       
   257         result = ETrue;
       
   258         }
       
   259     CleanupStack::PopAndDestroy(&value);
       
   260     return result;
       
   261     }
       
   262 
       
   263 // ---------------------------------------------------------------------------
       
   264 // 
       
   265 // ---------------------------------------------------------------------------
       
   266 //
       
   267 TInt CCPActionDataCache::FindL(const CLiwMap* aKey)
       
   268     {
       
   269     TInt result(KErrNotFound);
       
   270     TLiwVariant value;
       
   271     value.PushL();
       
   272     for (TInt i = 0; i < iInternalList->Count(); i++)
       
   273         {
       
   274         iInternalList->AtL(i, value);
       
   275         if (MatchL(value.AsMap(), aKey))
       
   276             {
       
   277             result = i;
       
   278             break;
       
   279             }
       
   280         }
       
   281     CleanupStack::PopAndDestroy(&value);
       
   282     return result;
       
   283     }
       
   284 
       
   285 // -----------------------------------------------------------------------------
       
   286 //
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 void CCPActionDataCache::CopyVariantL(const TDesC8& aKey,
       
   290         const CLiwMap* aInMap, CLiwDefaultMap* aOutMap)
       
   291     {
       
   292     //TODO: method exist also in data manager - should be refactored
       
   293     TLiwVariant variant;
       
   294     variant.PushL();
       
   295     if (aInMap->FindL(aKey, variant))
       
   296         {
       
   297         aOutMap->InsertL(aKey, variant);
       
   298         }
       
   299     CleanupStack::PopAndDestroy(&variant);
       
   300     }