contentstorage/castorage/src/casqlitestorage.cpp
changeset 127 7b66bc3c6dc9
parent 107 b34d53f6acdf
equal deleted inserted replaced
126:efda7c0771b9 127:7b66bc3c6dc9
    42 //
    42 //
    43 void CCaSqLiteStorage::ConstructL()
    43 void CCaSqLiteStorage::ConstructL()
    44     {
    44     {
    45     User::LeaveIfError( iRfs.Connect() );
    45     User::LeaveIfError( iRfs.Connect() );
    46 
    46 
    47     User::LeaveIfError( CreatePrivateDirPath( iPrivatePathCDriveDb, KCDrive,
    47     User::LeaveIfError(
    48             KDbName ) );
    48             CreatePrivateDirPath( iPrivatePathCDriveDb, KCDrive, KDbName ) );
    49 
    49     User::LeaveIfError(
    50     User::LeaveIfError( CreatePrivateDirPath( iPrivatePathZDriveDb, KZDrive,
    50             CreatePrivateDirPath( iPrivatePathZDriveDb, KZDrive, KDbName ) );
    51             KDbName ) );
    51     User::LeaveIfError(
    52 
    52             CreatePrivateDirPath( iPrivatePathCDrive, KCDrive, KNullDesC ) );
    53     User::LeaveIfError( CreatePrivateDirPath( iPrivatePathCDrive, KCDrive,
    53     User::LeaveIfError(
    54             KNullDesC ) );
    54             CreatePrivateDirPath(
    55     
    55                     iPrivatePathCDriveDbBackup, KCDrive, KDbNameBackup ) );
    56     User::LeaveIfError( CreatePrivateDirPath( iPrivatePathCDriveDbBackup, KCDrive,
       
    57     		KDbNameBackup ) );
       
    58     
       
    59 
    56 
    60     if( iSqlDb.Open( iPrivatePathCDriveDb, &KSqlDbConfig ) )
    57     if( iSqlDb.Open( iPrivatePathCDriveDb, &KSqlDbConfig ) )
    61         {
    58         {
    62         //we could not load data base from C-drive lets try Rom
    59         //we could not load data base from C-drive lets try Rom
    63         LoadDataBaseFromRomL();
    60         LoadDataBaseFromRomL();
    64         }
    61         }
    65     else
    62     else
    66         {
    63         {
    67         TBuf<KCaMaxAttrNameLen> versionValue;
    64         TBuf<KCaMaxAttrNameLen> versionValue;
    68         DbPropertyL(KCaDbPropVersion, versionValue);
    65         DbPropertyL( KCaDbPropVersion, versionValue );
    69         ASSERT(versionValue.Length()>0);
    66         ASSERT( versionValue.Length() > 0 );
    70         if( versionValue.CompareC( KCaDbVersion ) )
    67         if( versionValue.CompareC( KCaDbVersion ) )
    71             {
    68             {
    72             // database loaded from C: is obsolete, load from Z:
    69             // database loaded from C: is obsolete, load from Z:
    73             iSqlDb.Close();
    70             iSqlDb.Close();
    74             LoadDataBaseFromRomL();
    71             LoadDataBaseFromRomL();
    75             }
    72             }
    76         }
    73         }
       
    74     TBuf<KCaMaxAttrNameLen> restoreValue;
       
    75     DbPropertyL( KCaDbPropRestore, restoreValue );
       
    76     if( restoreValue.CompareC( KCaDbPropRestoreVal ) == KErrNone )
       
    77         {
       
    78         RestoreDatabaseL();
       
    79         }
       
    80     }
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 // CCASqLiteStorage::GetDownloadedApplicationsArrayL()
       
    84 //
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 void CCaSqLiteStorage::GetDownloadedApplicationsArrayL(
       
    88         RPointerArray<CCaInnerEntry>& aResultArray )
       
    89 {
       
    90     iCollectionDownloadId = GetCollectionDownloadIdL();
       
    91     CCaInnerQuery* downloadedQuery = CCaInnerQuery::NewLC();
       
    92     downloadedQuery->SetParentId( iCollectionDownloadId );
       
    93     GetEntriesL( downloadedQuery, aResultArray );
       
    94     CleanupStack::PopAndDestroy( downloadedQuery );
       
    95 }
       
    96 
       
    97 // ---------------------------------------------------------------------------
       
    98 // CCASqLiteStorage::GetDownloadedApplicationsArrayL()
       
    99 //
       
   100 // ---------------------------------------------------------------------------
       
   101 //
       
   102 void CCaSqLiteStorage::GetDownloadedApplicationsArrayL(
       
   103         RArray<TInt>& aResultArray )
       
   104 {
       
   105     iCollectionDownloadId = GetCollectionDownloadIdL();
       
   106     CCaInnerQuery* downloadedQuery = CCaInnerQuery::NewLC();
       
   107     downloadedQuery->SetParentId( iCollectionDownloadId );
       
   108     GetEntriesIdsL( downloadedQuery, aResultArray );
       
   109     CleanupStack::PopAndDestroy( downloadedQuery );
       
   110 }
       
   111 
       
   112 // ---------------------------------------------------------------------------
       
   113 // CCASqLiteStorage::RestoreDownloadedApplications()
       
   114 //
       
   115 // ---------------------------------------------------------------------------
       
   116 //
       
   117 void CCaSqLiteStorage::SetDownloadedApplicationsArrayL(
       
   118         RPointerArray<CCaInnerEntry>& aResultArray )
       
   119 {
       
   120     //remove all entries from downloaded collection
       
   121     RArray<TInt> entryIds;
       
   122     CleanupClosePushL( entryIds );
       
   123     GetDownloadedApplicationsArrayL( entryIds );
       
   124 
       
   125     TCaOperationParams params;
       
   126     params.iOperationType = TCaOperationParams::ERemove;
       
   127     params.iGroupId = iCollectionDownloadId;
       
   128     params.iBeforeEntryId = 0; // Not Used
       
   129     ExecuteOrganizeL( entryIds, params );
       
   130     CleanupStack::PopAndDestroy( &entryIds );
       
   131 
       
   132     //get current downloaded applications list
       
   133     for( TInt i = aResultArray.Count() - 1; i >= 0; i-- )
       
   134         {
       
   135         CCaInnerQuery* downloadedQuery = CCaInnerQuery::NewLC();
       
   136         RArray<TInt> entryIds;
       
   137         CleanupClosePushL( entryIds );
       
   138         entryIds.AppendL( aResultArray[i]->GetId() );
       
   139         downloadedQuery->SetIdsL( entryIds );
       
   140 
       
   141         RArray<TInt> resultEntryIds;
       
   142         CleanupClosePushL( resultEntryIds );
       
   143         GetEntriesIdsL( downloadedQuery, resultEntryIds );
       
   144         if( !resultEntryIds.Count() )
       
   145             {
       
   146             //its in case of application that was installed after backup
       
   147             aResultArray[i]->SetId(0);
       
   148             }
       
   149         //there could be some icon added to db aster backup
       
   150         //( after update or installation of applicatiotion )
       
   151         aResultArray[i]->SetIconId(0);
       
   152 
       
   153         CleanupStack::PopAndDestroy( &resultEntryIds );
       
   154         CleanupStack::PopAndDestroy( &entryIds );
       
   155         CleanupStack::PopAndDestroy( downloadedQuery );
       
   156         }
       
   157 
       
   158     //add applications to downloaded collection
       
   159     for( TInt i = 0; i < aResultArray.Count(); i++ )
       
   160         {
       
   161         AddL( aResultArray[i] );
       
   162         }
       
   163     params.iOperationType = TCaOperationParams::EAppend;
       
   164     ExecuteOrganizeL( aResultArray, params );
       
   165 }
       
   166 
       
   167 // ---------------------------------------------------------
       
   168 //
       
   169 // ---------------------------------------------------------
       
   170 //
       
   171 void CCaSqLiteStorage::ExecuteOrganizeL(
       
   172         const RPointerArray<CCaInnerEntry> & aResultArray,
       
   173         TCaOperationParams params)
       
   174 {
       
   175     RArray<TInt> restoredEntryIds;
       
   176     CleanupClosePushL(restoredEntryIds);
       
   177     for( TInt i = 0; i < aResultArray.Count(); i++ )
       
   178         {
       
   179         restoredEntryIds.AppendL( aResultArray[i]->GetId() );
       
   180         }
       
   181     ExecuteOrganizeL( restoredEntryIds, params );
       
   182     CleanupStack::PopAndDestroy( &restoredEntryIds );
       
   183 }
       
   184 
       
   185 // ---------------------------------------------------------
       
   186 //
       
   187 // ---------------------------------------------------------
       
   188 //
       
   189 TInt CCaSqLiteStorage::GetCollectionDownloadIdL()
       
   190     {
       
   191     TInt downloadId( KErrNotFound );
       
   192     RPointerArray<CCaInnerEntry> resultArray;
       
   193     CleanupResetAndDestroyPushL( resultArray );
       
   194     CCaInnerQuery* allAppQuery = CCaInnerQuery::NewLC();
       
   195     CDesC16ArrayFlat* appType =
       
   196             new ( ELeave ) CDesC16ArrayFlat( KGranularityOne );
       
   197     CleanupStack::PushL( appType );
       
   198     appType->AppendL( KCaTypeCollectionDownload );
       
   199     allAppQuery->SetEntryTypeNames( appType );
       
   200     CleanupStack::Pop( appType );
       
   201     GetEntriesL( allAppQuery, resultArray );
       
   202     CleanupStack::PopAndDestroy( allAppQuery );
       
   203     if( resultArray.Count() )
       
   204         {
       
   205         downloadId = resultArray[0]->GetId();
       
   206         }
       
   207     CleanupStack::PopAndDestroy( &resultArray );
       
   208     return downloadId;
    77     }
   209     }
    78 
   210 
    79 // ---------------------------------------------------------------------------
   211 // ---------------------------------------------------------------------------
    80 // CCASqLiteStorage::NewL()
   212 // CCASqLiteStorage::NewL()
    81 //
   213 //
   109                 &KSqlDbConfig ) );
   241                 &KSqlDbConfig ) );
   110         }
   242         }
   111     }
   243     }
   112 
   244 
   113 // ---------------------------------------------------------------------------
   245 // ---------------------------------------------------------------------------
   114 // 
   246 //
   115 //
   247 //
   116 // ---------------------------------------------------------------------------
   248 // ---------------------------------------------------------------------------
   117 //
   249 //
   118 void CCaSqLiteStorage::SaveDatabaseL()
   250 void CCaSqLiteStorage::SaveDatabaseL()
   119     {
   251     {
   120     if( ( BaflUtils::FileExists( iRfs, iPrivatePathCDriveDb ) ) )
   252     if( ( BaflUtils::FileExists( iRfs, iPrivatePathCDriveDb ) ) )
   121         {
   253         {
   122         iSqlDb.Close();
   254         iSqlDb.Close();
   123         User::LeaveIfError( BaflUtils::CopyFile( iRfs,
   255         User::LeaveIfError( BaflUtils::CopyFile(
   124                 iPrivatePathCDriveDb, iPrivatePathCDriveDbBackup ) );
   256                 iRfs, iPrivatePathCDriveDb, iPrivatePathCDriveDbBackup ) );
   125         User::LeaveIfError( iSqlDb.Open( iPrivatePathCDriveDb,
   257         User::LeaveIfError( iSqlDb.Open(
   126                 &KSqlDbConfig ) );
   258                 iPrivatePathCDriveDb, &KSqlDbConfig ) );
   127         }
   259         }
   128     }
   260     }
   129 
   261 
   130 // ---------------------------------------------------------------------------
   262 // ---------------------------------------------------------------------------
   131 // 
   263 //
   132 //
   264 //
   133 // ---------------------------------------------------------------------------
   265 // ---------------------------------------------------------------------------
   134 //
   266 //
   135 void CCaSqLiteStorage::RestoreDatabaseL()
   267 void CCaSqLiteStorage::RestoreDatabaseL()
   136     {
   268     {
   137     if( ( BaflUtils::FileExists( iRfs, iPrivatePathCDriveDbBackup ) ) )
   269     if( BaflUtils::FileExists( iRfs, iPrivatePathCDriveDbBackup ) )
   138         {
   270         {
       
   271         RPointerArray<CCaInnerEntry> resultArray;
       
   272         CleanupResetAndDestroyPushL( resultArray );
       
   273         //get current downloaded applications list
       
   274         GetDownloadedApplicationsArrayL( resultArray );
       
   275 
   139         iSqlDb.Close();
   276         iSqlDb.Close();
   140         User::LeaveIfError( BaflUtils::CopyFile( iRfs,
   277         User::LeaveIfError( BaflUtils::CopyFile(
   141         		iPrivatePathCDriveDbBackup, iPrivatePathCDriveDb ) );
   278                 iRfs, iPrivatePathCDriveDbBackup, iPrivatePathCDriveDb ) );
   142         User::LeaveIfError( iSqlDb.Open( iPrivatePathCDriveDb,
   279         User::LeaveIfError( iSqlDb.Open(
   143                 &KSqlDbConfig ) );
   280                 iPrivatePathCDriveDb, &KSqlDbConfig ) );
   144         }
   281 
       
   282         //set current downloaded applications list to backuped
       
   283         //downloaded collection
       
   284         SetDownloadedApplicationsArrayL( resultArray );
       
   285         CleanupStack::PopAndDestroy( &resultArray );
       
   286         }
       
   287     SetDBPropertyL( KCaDbPropRestore, KCaDbPropNoRestoreVal );
   145     }
   288     }
   146 
   289 
   147 // ---------------------------------------------------------------------------
   290 // ---------------------------------------------------------------------------
   148 // CCASqLiteStorage::NewL()
   291 // CCASqLiteStorage::NewL()
   149 //
   292 //
   234         sqlGetAttributesQuery->BindEntryIdsL( aResultContainer );
   377         sqlGetAttributesQuery->BindEntryIdsL( aResultContainer );
   235         sqlGetAttributesQuery->ExecuteL( aResultContainer,
   378         sqlGetAttributesQuery->ExecuteL( aResultContainer,
   236                 CCaSqlQuery::EAttribute );
   379                 CCaSqlQuery::EAttribute );
   237         CleanupStack::PopAndDestroy( sqlGetAttributesQuery );
   380         CleanupStack::PopAndDestroy( sqlGetAttributesQuery );
   238         }
   381         }
   239     
   382 
   240     //  set entries if proper order if they were fetched by ids
   383     //  set entries if proper order if they were fetched by ids
   241     if( aQuery->GetIds().Count() > 0 )
   384     if( aQuery->GetIds().Count() > 0 )
   242         {
   385         {
   243         SetEntriesInProperOrderL( aQuery->GetIds(), aResultContainer );
   386         SetEntriesInProperOrderL( aQuery->GetIds(), aResultContainer );
   244         }
   387         }
   390 // ---------------------------------------------------------------------------
   533 // ---------------------------------------------------------------------------
   391 // CCASqLiteStorage::AddL
   534 // CCASqLiteStorage::AddL
   392 //
   535 //
   393 // ---------------------------------------------------------------------------
   536 // ---------------------------------------------------------------------------
   394 //
   537 //
   395 void CCaSqLiteStorage::AddL( CCaInnerEntry* aEntry, TBool aUpdate)
   538 void CCaSqLiteStorage::AddL( CCaInnerEntry* aEntry, TBool aUpdate )
   396     {
   539     {
   397     RPointerArray<CCaSqlQuery> sqlQueries;
   540     RPointerArray<CCaSqlQuery> sqlQueries;
   398     CleanupResetAndDestroyPushL( sqlQueries );
   541     CleanupResetAndDestroyPushL( sqlQueries );
   399 
   542 
   400     CaSqlQueryCreator::CreateAddQueryL( aEntry, sqlQueries, iSqlDb );
   543     CaSqlQueryCreator::CreateAddQueryL( aEntry, sqlQueries, iSqlDb );
   534     CleanupClosePushL( entryIds );
   677     CleanupClosePushL( entryIds );
   535     for( TInt j=0; j<aEntryIds.Count(); j++ )
   678     for( TInt j=0; j<aEntryIds.Count(); j++ )
   536         {
   679         {
   537         entryIds.AppendL(aEntryIds[j]);
   680         entryIds.AppendL(aEntryIds[j]);
   538         }
   681         }
   539     
   682 
   540     RArray<TInt> oldIds;
   683     RArray<TInt> oldIds;
   541     CleanupClosePushL( oldIds );
   684     CleanupClosePushL( oldIds );
   542     CCaInnerQuery* innerQuery = CCaInnerQuery::NewLC();
   685     CCaInnerQuery* innerQuery = CCaInnerQuery::NewLC();
   543     innerQuery->SetParentId( aGroupId );
   686     innerQuery->SetParentId( aGroupId );
   544     
   687 
   545     GetEntriesIdsL( innerQuery, oldIds );
   688     GetEntriesIdsL( innerQuery, oldIds );
   546     
   689 
   547     if( oldIds.Count() != entryIds.Count() )
   690     if( oldIds.Count() != entryIds.Count() )
   548         {
   691         {
   549         for( TInt i=0; i<oldIds.Count(); i++ )
   692         for( TInt i=0; i<oldIds.Count(); i++ )
   550             {
   693             {
   551             TInt oldId = oldIds[i];
   694             TInt oldId = oldIds[i];
   554                 // instert apps with visible set on false on old positions
   697                 // instert apps with visible set on false on old positions
   555                 entryIds.InsertL( oldId, i );
   698                 entryIds.InsertL( oldId, i );
   556                 }
   699                 }
   557             }
   700             }
   558         }
   701         }
   559     
   702 
   560     const RArray<TInt> constEntryIds( entryIds );
   703     const RArray<TInt> constEntryIds( entryIds );
   561     
   704 
   562     RPointerArray<CCaSqlQuery> sqlQueries;
   705     RPointerArray<CCaSqlQuery> sqlQueries;
   563     CleanupResetAndDestroyPushL( sqlQueries ); 
   706     CleanupResetAndDestroyPushL( sqlQueries );
   564 
   707 
   565     CaSqlQueryCreator::CreateCustomSortQueryL(
   708     CaSqlQueryCreator::CreateCustomSortQueryL(
   566             constEntryIds, sqlQueries, iSqlDb );
   709             constEntryIds, sqlQueries, iSqlDb );
   567 
   710 
   568     ExecuteStatementL( KSqlStatementBegin );
   711     ExecuteStatementL( KSqlStatementBegin );
   693             aSqlQuery[i]->BindEntryIdL( aEntry->GetId() );
   836             aSqlQuery[i]->BindEntryIdL( aEntry->GetId() );
   694             aSqlQuery[i]->ExecuteL();
   837             aSqlQuery[i]->ExecuteL();
   695             aSqlQuery[i]->CloseStatement();
   838             aSqlQuery[i]->CloseStatement();
   696             isAttributeDeleted = ETrue;
   839             isAttributeDeleted = ETrue;
   697             }
   840             }
   698         else if( aSqlQuery[i]->Type()==CCaSqlQuery::EAttributeTable )
   841         else if( aSqlQuery[i]->Type() == CCaSqlQuery::EAttributeTable )
   699             {
   842             {
   700             // add new attribute(s) if it's neccesery
   843             // add new attribute(s) if it's neccesery
   701             aSqlQuery[i]->PrepareL();
   844             aSqlQuery[i]->PrepareL();
   702             for( TInt k(0); k < aEntry->GetAttributes().Count(); k++ )
   845             for( TInt k(0); k < aEntry->GetAttributes().Count(); k++ )
   703                 {
   846                 {
   832     RPointerArray<CCaSqlQuery> sqlQuery;
   975     RPointerArray<CCaSqlQuery> sqlQuery;
   833     CleanupResetAndDestroyPushL( sqlQuery );
   976     CleanupResetAndDestroyPushL( sqlQuery );
   834 
   977 
   835     CaSqlQueryCreator::CreateTouchQueryL( sqlQuery, iSqlDb, aRemovable );
   978     CaSqlQueryCreator::CreateTouchQueryL( sqlQuery, iSqlDb, aRemovable );
   836 
   979 
   837     TTime time;
       
   838     time.UniversalTime();
       
   839 
       
   840     for( TInt i = 0; i < sqlQuery.Count(); i++ )
   980     for( TInt i = 0; i < sqlQuery.Count(); i++ )
   841         {
   981         {
   842         sqlQuery[i]->PrepareL();
   982         sqlQuery[i]->PrepareL();
   843         sqlQuery[i]->BindValuesForLaunchL( aEntryId, time.Int64() );
   983         sqlQuery[i]->BindValuesForTouchL( aEntryId );
   844         sqlQuery[i]->ExecuteL();
   984         sqlQuery[i]->ExecuteL();
   845         sqlQuery[i]->CloseStatement();
   985         sqlQuery[i]->CloseStatement();
   846         }
   986         }
   847 
   987 
   848     CleanupStack::PopAndDestroy( &sqlQuery );
   988     CleanupStack::PopAndDestroy( &sqlQuery );
   880     sqlQuery->ExecuteL();
  1020     sqlQuery->ExecuteL();
   881     sqlQuery->CloseStatement();
  1021     sqlQuery->CloseStatement();
   882     CleanupStack::PopAndDestroy( sqlQuery );
  1022     CleanupStack::PopAndDestroy( sqlQuery );
   883     }
  1023     }
   884 
  1024 
   885 
       
   886 // ---------------------------------------------------------------------------
       
   887 // CCASqLiteStorage::RemoveOldEntriesFromLaunchTableL( TInt aDays )
       
   888 //
       
   889 // ---------------------------------------------------------------------------
       
   890 //
       
   891 void CCaSqLiteStorage::RemoveOldEntriesFromLaunchTableL( TInt aDays )
       
   892     {
       
   893     TTime presentTime;
       
   894     presentTime.UniversalTime();
       
   895     TTime borderTime = presentTime - ( TTimeIntervalDays( aDays ) );
       
   896 
       
   897     CCaSqlQuery* sqlQuery = CCaSqlQuery::NewLC( iSqlDb );
       
   898     sqlQuery->SetQueryL( KSQLDeleteOldFromLaunch );
       
   899     sqlQuery->PrepareL();
       
   900     sqlQuery->BindValuesForLaunchL( 0, borderTime.Int64() );
       
   901     sqlQuery->ExecuteL();
       
   902 
       
   903     CleanupStack::PopAndDestroy( sqlQuery );
       
   904     }
       
   905 
       
   906 // ---------------------------------------------------------------------------
  1025 // ---------------------------------------------------------------------------
   907 // CCASqLiteStorage::VerifyOrganizeParamsL( const RArray<TInt>& aEntryIds,
  1026 // CCASqLiteStorage::VerifyOrganizeParamsL( const RArray<TInt>& aEntryIds,
   908 //      TCaOperationParams aParams);
  1027 //      TCaOperationParams aParams);
   909 //
  1028 //
   910 // ---------------------------------------------------------------------------
  1029 // ---------------------------------------------------------------------------
   923             query->PrepareL();
  1042             query->PrepareL();
   924             query->BindValuesForOrganizeL( aEntryIds, aParams );
  1043             query->BindValuesForOrganizeL( aEntryIds, aParams );
   925             TInt dbEntryCount;
  1044             TInt dbEntryCount;
   926             query->ExecuteL( dbEntryCount );
  1045             query->ExecuteL( dbEntryCount );
   927             query->CloseStatement();
  1046             query->CloseStatement();
       
  1047 
   928             if( dbEntryCount < aEntryIds.Count() )
  1048             if( dbEntryCount < aEntryIds.Count() )
   929                 {
  1049                 {
   930                 User::Leave( KErrArgument );
  1050                 User::Leave( KErrArgument );
   931                 }
  1051                 }
   932 
  1052