contentstorage/casrv/cawidgetscanner/src/cawidgetstoragehandler.cpp
changeset 60 f62f87b200ec
child 61 8e5041d13c84
equal deleted inserted replaced
4:1a2a00e78665 60:f62f87b200ec
       
     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: 
       
    15  *
       
    16  */
       
    17 // INCLUDE FILES
       
    18 
       
    19 #include <badesca.h>
       
    20 #include "widgetscannerutils.h"
       
    21 #include "cawidgetstoragehandler.h"
       
    22 #include "cadef.h"
       
    23 #include "cainnerentry.h"
       
    24 #include "cainnerquery.h"
       
    25 #include "castorageproxy.h"
       
    26 #include "caarraycleanup.inl"
       
    27 
       
    28 // ============================ MEMBER FUNCTIONS ===============================
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // CCaWidgetStorageHandler::CCaWidgetStorageHandler
       
    32 // C++ default constructor can NOT contain any code, that
       
    33 // might leave.
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 CCaWidgetStorageHandler::CCaWidgetStorageHandler(
       
    37         CCaStorageProxy* aStorage, RFs& aFs )
       
    38     {
       
    39     iStorage = aStorage;
       
    40     iFs = aFs;
       
    41     }
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CCaWidgetStorageHandler::ConstructL
       
    45 // Symbian 2nd phase constructor can leave.
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 void CCaWidgetStorageHandler::ConstructL()
       
    49     {
       
    50     }
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CCaWidgetStorageHandler::NewL
       
    54 // Two-phased constructor.
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 CCaWidgetStorageHandler* CCaWidgetStorageHandler::NewL(
       
    58         CCaStorageProxy* aStorage, RFs& aFs )
       
    59     {
       
    60     CCaWidgetStorageHandler* self = NewLC( aStorage, aFs );
       
    61     CleanupStack::Pop( self );
       
    62     return self;
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CCaWidgetStorageHandler::NewLC
       
    67 // Two-phased constructor.
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 CCaWidgetStorageHandler* CCaWidgetStorageHandler::NewLC(
       
    71         CCaStorageProxy* aStorage, RFs& aFs )
       
    72     {
       
    73     CCaWidgetStorageHandler* self = new ( ELeave ) CCaWidgetStorageHandler(
       
    74             aStorage, aFs );
       
    75     CleanupStack::PushL( self );
       
    76     self->ConstructL();
       
    77     return self;
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // Destructor
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 CCaWidgetStorageHandler::~CCaWidgetStorageHandler()
       
    85     {
       
    86     iWidgets.ResetAndDestroy();
       
    87     iUpdatedIndexes.Close();
       
    88     }
       
    89 
       
    90 // ----------------------------------------------------------------------------
       
    91 // 
       
    92 // ----------------------------------------------------------------------------
       
    93 //  
       
    94 void CCaWidgetStorageHandler::SynchronizeL( const RWidgetArray& aWidgets )
       
    95     {
       
    96     FetchWidgetsL();
       
    97     AddWidgetsL( aWidgets );
       
    98     RemoveWidgetsL();
       
    99     }
       
   100 
       
   101 // ----------------------------------------------------------------------------
       
   102 // 
       
   103 // ----------------------------------------------------------------------------
       
   104 //  
       
   105 void CCaWidgetStorageHandler::AddL( const CCaWidgetDescription* aWidget )
       
   106     {
       
   107     CCaInnerEntry* entry = aWidget->GetEntryLC();
       
   108     iStorage->AddL( entry );
       
   109     if( ( entry->GetFlags() & ERemovable ) != 0 )
       
   110         {
       
   111         AddWidgetToDownloadCollectionL( entry );
       
   112         }
       
   113     CleanupStack::PopAndDestroy( entry );
       
   114     }
       
   115 
       
   116 // ----------------------------------------------------------------------------
       
   117 // 
       
   118 // ----------------------------------------------------------------------------
       
   119 //  
       
   120 void CCaWidgetStorageHandler::UpdateL( const CCaWidgetDescription* aWidget,
       
   121         TUint aEntryId )
       
   122     {
       
   123     CCaInnerEntry* entry = aWidget->GetEntryLC();
       
   124     entry->SetId( aEntryId );
       
   125     if( !aWidget->IsMissing() && ( aWidget->GetLibrary() != KNoLibrary ) )
       
   126         {
       
   127         entry->SetFlags( entry->GetFlags() & ~EUsed );
       
   128         }
       
   129     else if(aWidget->IsUsed())
       
   130         {
       
   131         entry->SetFlags( entry->GetFlags() | EUsed );
       
   132         }
       
   133     entry->SetFlags( entry->GetFlags() & ~EMissing );
       
   134     iStorage->AddL( entry );
       
   135     if( !aWidget->IsMissing() )
       
   136         {
       
   137         AddWidgetToDownloadCollectionL( entry );
       
   138         }
       
   139     CleanupStack::PopAndDestroy( entry );
       
   140     }
       
   141 
       
   142 // ----------------------------------------------------------------------------
       
   143 // 
       
   144 // ----------------------------------------------------------------------------
       
   145 //  
       
   146 void CCaWidgetStorageHandler::AddWidgetsL( const RWidgetArray& aWidgets )
       
   147     {
       
   148     iUpdatedIndexes.Reset();
       
   149     for( TInt i = 0; i < aWidgets.Count(); i++ )
       
   150         {
       
   151         TInt index = iWidgets.Find( 
       
   152                 aWidgets[i], CCaWidgetDescription::Compare );
       
   153         if( index != KErrNotFound )
       
   154             {
       
   155             if( !iWidgets[index]->Compare( *aWidgets[i] ) || 
       
   156                     iWidgets[index]->IsMissing() )
       
   157                 {
       
   158                 aWidgets[i]->SetMissing( iWidgets[index]->IsMissing() );
       
   159                 aWidgets[i]->SetUsed( iWidgets[index]->IsUsed() );
       
   160                 UpdateL( aWidgets[i], iWidgets[index]->GetEntryId() );
       
   161                 }
       
   162             iUpdatedIndexes.AppendL( index );
       
   163             }
       
   164         else
       
   165             {
       
   166             AddL( aWidgets[i] );
       
   167             }
       
   168         }
       
   169     }
       
   170 
       
   171 // ----------------------------------------------------------------------------
       
   172 // 
       
   173 // ----------------------------------------------------------------------------
       
   174 //  
       
   175 void CCaWidgetStorageHandler::RemoveWidgetsL()
       
   176     {
       
   177     RArray<TInt> widgetsToRemove;
       
   178     CleanupClosePushL( widgetsToRemove );
       
   179     for( TInt i = 0; i < iWidgets.Count(); i++ )
       
   180         {
       
   181         if( iUpdatedIndexes.Find( i ) == KErrNotFound )
       
   182             {
       
   183             if( iWidgets[i]->GetMmcId() && 
       
   184                     ( iWidgets[i]->GetMmcId() != 
       
   185                     WidgetScannerUtils::CurrentMmcId( iFs ) ) )
       
   186                 {
       
   187                 SetMissingFlagL( iWidgets[i] );
       
   188                 }
       
   189             else
       
   190                 {
       
   191                 widgetsToRemove.AppendL( iWidgets[i]->GetEntryId() );
       
   192                 }
       
   193             }
       
   194         }
       
   195     if( widgetsToRemove.Count() > 0 )
       
   196         {
       
   197         iStorage->RemoveL( widgetsToRemove );
       
   198         }
       
   199     CleanupStack::PopAndDestroy( &widgetsToRemove );
       
   200     }
       
   201 
       
   202 // ----------------------------------------------------------------------------
       
   203 // 
       
   204 // ----------------------------------------------------------------------------
       
   205 // 
       
   206 void CCaWidgetStorageHandler::AddWidgetToDownloadCollectionL(
       
   207         const CCaInnerEntry* aEntry )
       
   208     {
       
   209     CCaInnerQuery* queryDownload = CCaInnerQuery::NewLC();
       
   210     CDesC16ArrayFlat* downloadType = new ( ELeave ) CDesC16ArrayFlat(
       
   211             KGranularityOne );
       
   212     CleanupStack::PushL( downloadType );
       
   213     downloadType->AppendL( KCaTypeCollectionDownload );
       
   214     queryDownload->SetEntryTypeNames( downloadType );//query takes ownership
       
   215     CleanupStack::Pop( downloadType );
       
   216     queryDownload->SetRole( EGroupEntryRole );
       
   217 
       
   218     RArray<TInt> idsGroup;
       
   219     CleanupClosePushL( idsGroup );
       
   220     iStorage->GetEntriesIdsL( queryDownload, idsGroup );
       
   221 
       
   222     TCaOperationParams operParams;
       
   223     operParams.iOperationType = TCaOperationParams::EPrepend;
       
   224     operParams.iGroupId = idsGroup[0];
       
   225     operParams.iBeforeEntryId = 0;
       
   226     CleanupStack::PopAndDestroy( &idsGroup );
       
   227 
       
   228     RArray<TInt> idsEntry;
       
   229     CleanupClosePushL( idsEntry );
       
   230     idsEntry.Append( aEntry->GetId() );
       
   231     iStorage->OrganizeL( idsEntry, operParams );
       
   232 
       
   233     CleanupStack::PopAndDestroy( &idsEntry );
       
   234     CleanupStack::PopAndDestroy( queryDownload );
       
   235     }
       
   236 
       
   237 // ----------------------------------------------------------------------------
       
   238 // 
       
   239 // ----------------------------------------------------------------------------
       
   240 //  
       
   241 void CCaWidgetStorageHandler::FetchWidgetsL()
       
   242     {
       
   243     CCaInnerQuery* query = CCaInnerQuery::NewLC();
       
   244     CDesC16ArrayFlat* widgetType = new ( ELeave ) CDesC16ArrayFlat(
       
   245             KGranularityOne );
       
   246     CleanupStack::PushL( widgetType );
       
   247     widgetType->AppendL( KCaTypeWidget );
       
   248     query->SetEntryTypeNames( widgetType );//transfers ownership to query
       
   249     CleanupStack::Pop( widgetType );
       
   250 
       
   251     RPointerArray<CCaInnerEntry> entries;
       
   252     CleanupResetAndDestroyPushL( entries );
       
   253     iStorage->GetEntriesL( query, entries );
       
   254     iWidgets.ResetAndDestroy();
       
   255     for( TInt i = 0; i < entries.Count(); i++ )
       
   256         {
       
   257         CCaWidgetDescription* widget = CCaWidgetDescription::NewLC(
       
   258                 entries[i] );
       
   259         iWidgets.AppendL( widget ); //iWidgets takes ownership
       
   260         CleanupStack::Pop( widget );
       
   261         }
       
   262     CleanupStack::PopAndDestroy( &entries );
       
   263     CleanupStack::PopAndDestroy( query );
       
   264     }
       
   265 
       
   266 // ----------------------------------------------------------------------------
       
   267 // 
       
   268 // ----------------------------------------------------------------------------
       
   269 //  
       
   270 void CCaWidgetStorageHandler::SetMissingFlagL(
       
   271         const CCaWidgetDescription* aWidget )
       
   272     {
       
   273     CCaInnerEntry* entry = aWidget->GetEntryLC();
       
   274     entry->SetFlags( entry->GetFlags() | EMissing );
       
   275     if(aWidget->IsUsed())
       
   276         {
       
   277         entry->SetFlags( entry->GetFlags() | EUsed );
       
   278         }
       
   279     iStorage->AddL( entry );
       
   280     CleanupStack::PopAndDestroy( entry );
       
   281     }
       
   282 
       
   283 //  End of File