fotaapplication/fotaserver/FotaServer/src/FotaDB.cpp
changeset 44 39aa16f3fdc2
parent 43 c8e5c3d81b42
child 53 b3a7d8e28262
child 57 f5b689a4f7a2
equal deleted inserted replaced
43:c8e5c3d81b42 44:39aa16f3fdc2
     1 /*
       
     2 * Copyright (c) 2005 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:   Fota package state preservation 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "FotaDB.h"
       
    21 #include "FotaSrvDebug.h"
       
    22 #include <centralrepository.h>
       
    23 #include "fotaserverPrivateCRKeys.h"
       
    24 
       
    25 #define __LEAVE_IF_ERROR(x) if(KErrNone!=x) {FLOG(_L("LEAVE in %s: %d"), __FILE__, __LINE__); User::Leave(x); }
       
    26 
       
    27 // ====================== MEMBER FUNCTIONS ===================================
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // CFotaDB::CFotaDB()
       
    31 // ---------------------------------------------------------------------------
       
    32 CFotaDB::CFotaDB() : iIsOpen( EFalse )
       
    33 	,iTableAltercheck(ETrue)
       
    34     {
       
    35     }
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // CFotaDB::~CFotaDB()
       
    39 // ---------------------------------------------------------------------------
       
    40 CFotaDB::~CFotaDB()
       
    41     {
       
    42     //Delete columns set
       
    43     if (iColSet)
       
    44     	{
       
    45     	delete iColSet;
       
    46     	iColSet = NULL;
       
    47     	}
       
    48     //Close table
       
    49    	iTable.Close();
       
    50    	
       
    51    	//Close database
       
    52    	iStateDB.Close();
       
    53    	
       
    54    	//Close file server session
       
    55     if (iFSSession.Handle())
       
    56     	iFSSession.Close();
       
    57     }
       
    58 
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // CFotaDB::NewL()
       
    62 // ---------------------------------------------------------------------------
       
    63 CFotaDB* CFotaDB::NewL()
       
    64     { 
       
    65     CFotaDB* self=new(ELeave) CFotaDB();
       
    66     CleanupStack::PushL(self);
       
    67     self->ConstructL();
       
    68     CleanupStack::Pop();
       
    69     return self;
       
    70     }
       
    71 
       
    72 
       
    73 // ---------------------------------------------------------------------------
       
    74 // CFotaDB::ConstructL()
       
    75 // ---------------------------------------------------------------------------
       
    76 void CFotaDB::ConstructL()
       
    77     {
       
    78     TInt err;
       
    79     TBuf<KMaxPath>    tmp;
       
    80     User::LeaveIfError( iFSSession.Connect() );
       
    81     err = iFSSession.CreatePrivatePath( EDriveC );
       
    82     if ( err != KErrNone && err != KErrAlreadyExists ) User::Leave( err );
       
    83     User::LeaveIfError( iFSSession.SetSessionToPrivate( EDriveC ) );
       
    84     User::LeaveIfError( iFSSession.SessionPath (tmp) );
       
    85     }
       
    86 
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // CFotaDB::CreateDatabaseL()
       
    90 // Creates db in private dir of fotaserver
       
    91 // ---------------------------------------------------------------------------
       
    92 void CFotaDB::CreateDatabaseL()
       
    93     {
       
    94     FLOG(_L("CFotaDB::CreateDatabaseL()  >>"));
       
    95     TInt        err;
       
    96     User::LeaveIfError( iStateDB.Create( iFSSession ,KDatabaseName ) );
       
    97     User::LeaveIfError(iStateDB.Begin());
       
    98     User::LeaveIfError(iStateDB.Execute(KCreateTable));
       
    99     err = iStateDB.Commit();
       
   100     if(err) 
       
   101         {
       
   102         FLOG(_L("      cdb err %d"),err);
       
   103         iStateDB.Rollback();
       
   104         User::Leave(err);
       
   105         }
       
   106     User::LeaveIfError( iStateDB.Compact() );
       
   107     FLOG(_L("CFotaDB::CreateDatabaseL()  <<"));
       
   108     }
       
   109 
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // CFotaDB::AddPackageStateL
       
   113 // Adds state to db
       
   114 // ---------------------------------------------------------------------------
       
   115 void CFotaDB::AddPackageStateL(const TPackageState& aState
       
   116                                 ,const TDesC8& aPkgURL)
       
   117     {
       
   118     FLOG(_L("  CFotaDB::AddPackageStateL   >>"));
       
   119     TInt            err;
       
   120     RDbView         view;
       
   121 	CleanupClosePushL( view );
       
   122     TPackageState   pkgstate(aState); 
       
   123     pkgstate.iResult = -1; // result should be -1 if no Execs have been done
       
   124 	err = view.Prepare( iStateDB, TDbQuery(KSelectAll), RDbView::EInsertOnly);
       
   125     __LEAVE_IF_ERROR(err);
       
   126     FLOG(_L("  CFotaDB::AddPackageStateL   inserting. pkgid:%d result:%d \
       
   127             state:%d"), pkgstate.iPkgId,pkgstate.iResult,pkgstate.iState);
       
   128 	view.InsertL();
       
   129     StateToRowL( pkgstate,aPkgURL,view );
       
   130 	view.PutL();
       
   131 	CleanupStack::PopAndDestroy(); //view
       
   132     FLOG(_L("  CFotaDB::AddPackageStateL   <<"));
       
   133     }
       
   134 
       
   135 
       
   136 // ---------------------------------------------------------------------------
       
   137 // CFotaDB::GetAllL
       
   138 // Get all states
       
   139 // ---------------------------------------------------------------------------
       
   140 void CFotaDB::GetAllL( RArray<TInt>& aStates)
       
   141 	{
       
   142 	RDbView             view;
       
   143 	CleanupClosePushL( view );
       
   144     
       
   145 	TInt err = view.Prepare( iStateDB, TDbQuery( KSelectAll ) );
       
   146 	__LEAVE_IF_ERROR(err);
       
   147 	view.EvaluateAll();
       
   148 
       
   149 	view.FirstL();
       
   150     FLOG(_L("[fota DB] --- rows --------------------------------------------\
       
   151         ----------------------- v"));		
       
   152 	while ( view.AtRow() )
       
   153 		{
       
   154         view.GetL();
       
   155         HBufC8*         url;
       
   156         TPackageState   s = RowToStateL(url,view);
       
   157         aStates.Append( s.iPkgId );
       
   158         CleanupStack::PushL ( url );       
       
   159         FLOG(_L("[fotaDB] pkgid: %d profid:%d state:%d  result:%d \
       
   160         url: %d chars sessiontype:%d IapId:%d Pkgsize:%d UpdateLtr:%d"), s.iPkgId, s.iProfileId, s.iState,s.iResult
       
   161         ,url->Des().Length(), s.iSessionType, s.iIapId, s.iPkgSize, s.iUpdateLtr );
       
   162 	    CleanupStack::PopAndDestroy(); // url
       
   163 		view.NextL();
       
   164 		}	
       
   165     FLOG(_L("[fota DB] --- rows --------------------------------------------\
       
   166         ----------------------- ^"));		
       
   167     view.Close();
       
   168 	CleanupStack::PopAndDestroy(); //view
       
   169 	}
       
   170 
       
   171 
       
   172 // ---------------------------------------------------------------------------
       
   173 // CFotaDB::OpenDBL()
       
   174 // Opens database
       
   175 // ---------------------------------------------------------------------------
       
   176 void CFotaDB::OpenDBL()
       
   177     {
       
   178     FLOG(_L("CFotaDB::OpenDBL()"));
       
   179     if (!iIsOpen)	//Prevents accidental opening of database twice
       
   180     	{
       
   181 	    TInt        err;
       
   182 	    err     = iStateDB.Open ( iFSSession ,KDatabaseName);
       
   183 	    
       
   184 	    if ( err == KErrNotFound )
       
   185 	        {
       
   186 	        CreateDatabaseL();
       
   187 	        iTableAltercheck = EFalse; //Table created newly, hence no alteration required.
       
   188 	        }
       
   189 	    else if ( err != KErrNone )
       
   190 	        {
       
   191 	        FLOG(_L("[fota DB openDB]\t db open error: %d"), err);
       
   192 	        FLOG(_L("deleting fota DB and creating it again..."));
       
   193 	        err = iFSSession.Delete(KDatabaseName);
       
   194 	        CreateDatabaseL();
       
   195 	        User::LeaveIfError(err);
       
   196 	        }
       
   197 	    User::LeaveIfError( iTable.Open (iStateDB, KTblState) );
       
   198 	    iColSet = iTable.ColSetL();
       
   199 	    
       
   200 	   	//Check & correct if the table has wrong attributes - this is for data compatibility
       
   201 	    if (iTableAltercheck)
       
   202 	    	AlterTableIfRequiredL();
       
   203 	
       
   204 	    iIsOpen = ETrue;
       
   205     	}
       
   206     }
       
   207 
       
   208 // ---------------------------------------------------------------------------
       
   209 // CFotaDB::CloseAndCommitDB
       
   210 // Closes and commits DB
       
   211 // ---------------------------------------------------------------------------
       
   212 void CFotaDB::CloseAndCommitDB()
       
   213     {
       
   214     if ( iColSet )
       
   215         {
       
   216         delete iColSet;
       
   217         iColSet = NULL;
       
   218         }
       
   219     iTable.Close();
       
   220     iStateDB.Close();
       
   221     iIsOpen = EFalse;
       
   222     }
       
   223 
       
   224 // ---------------------------------------------------------------------------
       
   225 // CFotaDB::IsOpen()
       
   226 // Chekcs if db is open
       
   227 // ---------------------------------------------------------------------------
       
   228 TBool CFotaDB::IsOpen()
       
   229     {
       
   230     return iIsOpen;
       
   231     }
       
   232 
       
   233 
       
   234 // ---------------------------------------------------------------------------
       
   235 // CFotaDB::GetStateL
       
   236 // Gets pkg state from db
       
   237 // ---------------------------------------------------------------------------
       
   238 TPackageState CFotaDB::GetStateL(const TInt aPkgId)
       
   239     {   
       
   240 	RDbView             view;
       
   241     TPackageState       s;
       
   242 	CleanupClosePushL( view ); 
       
   243 
       
   244     s.iState = RFotaEngineSession::EIdle; // default state is idle
       
   245 
       
   246     HBufC* select  = HBufC::NewLC( KSelect_where_packageid().Length() + 10); 
       
   247     select->Des().Format (KSelect_where_packageid, aPkgId);
       
   248 
       
   249     TInt err = view.Prepare( iStateDB, TDbQuery(*select) );
       
   250 	__LEAVE_IF_ERROR(err);
       
   251 	view.EvaluateAll();
       
   252 	view.FirstL();
       
   253 
       
   254     while ( view.AtRow() )
       
   255 		{
       
   256         view.GetL();
       
   257         HBufC8* url;
       
   258         s = RowToStateL( url,view ); 
       
   259         CleanupStack::PushL( url );     
       
   260         CleanupStack::PopAndDestroy(); // url
       
   261 		view.NextL();
       
   262 		}	
       
   263 	CleanupStack::PopAndDestroy(); //select
       
   264 	CleanupStack::PopAndDestroy(); //view
       
   265     return s;
       
   266     }
       
   267 
       
   268 
       
   269 // ---------------------------------------------------------------------------
       
   270 // CFotaDB::SetStateL
       
   271 // Writes package state to DB. 
       
   272 // ---------------------------------------------------------------------------
       
   273 void CFotaDB::SetStateL( TPackageState& aState, const TDesC8& aPkgURL
       
   274                         , TUint aChangedFields )
       
   275     {
       
   276     FLOG(_L("  CFotaDB::SetStateL  >>  id %d result %d  state %d sessiontype %d iapid %d pkgsize %d updateltr %d")
       
   277                     ,aState.iPkgId, aState.iResult, aState.iState);
       
   278     __ASSERT_ALWAYS( aChangedFields!=0, User::Panic(KFotaPanic, KErrArgument) );
       
   279 
       
   280     TPackageState found = GetStateL( aState.iPkgId );
       
   281     if ( found.iPkgId == KErrNotFound )
       
   282         {
       
   283         AddPackageStateL( aState, aPkgURL);
       
   284         }
       
   285     else
       
   286         {
       
   287         // sml try count must be reset, if state is set
       
   288         if (aChangedFields & EFDBState )
       
   289             {
       
   290             aChangedFields = aChangedFields | EFDBSmlTryCount;
       
   291             SetRetryCount(aState);
       
   292             }
       
   293 
       
   294         // Construct a SQL string for update. 
       
   295         // Example: UPDATE State SET Result=40,State=4 WHERE pkgID=5
       
   296         // 
       
   297         TInt        sqlsize = 0;
       
   298         _LIT8( KSqlbegin, "UPDATE State SET " );
       
   299         TBuf<21>    sqlEnd;
       
   300         HBufC8*     sql( NULL );
       
   301         // determine characters needed 
       
   302         sqlsize = DetermineCharNeeded(aChangedFields,aState,aPkgURL);
       
   303         sqlEnd.AppendFormat( _L(" WHERE pkgID=%d"),aState.iPkgId );
       
   304         
       
   305         sql = HBufC8::NewLC( ((TDesC8)KSqlbegin).Length() + sqlsize 
       
   306                                         + sqlEnd.Length() );
       
   307 
       
   308         sql->Des().Append ( KSqlbegin );
       
   309 
       
   310         if (aChangedFields & EFDBResult )   
       
   311             {
       
   312             // check FUMO compability
       
   313  			__ASSERT_ALWAYS( aState.iResult>=KErrNotFound 
       
   314  			&& aState.iResult<=600, User::Panic(KFotaPanic, KErrArgument) );
       
   315             sql->Des().AppendFormat(_L8("Result=%d,"),aState.iResult);
       
   316             }
       
   317         if (aChangedFields & EFDBState )    
       
   318             {
       
   319             // check FUMO compability
       
   320  			__ASSERT_ALWAYS( aState.iState>=0 && aState.iState<=100
       
   321  				, User::Panic(KFotaPanic, KErrArgument) );
       
   322             sql->Des().AppendFormat(_L8("State=%d,"),aState.iState);
       
   323             }
       
   324         if (aChangedFields & EFDBProfileId) 
       
   325             {
       
   326             sql->Des().AppendFormat(_L8("profileid=%d,"),aState.iProfileId);
       
   327             }
       
   328         if (aChangedFields & EFDBPkgUrl )   
       
   329             {
       
   330             sql->Des().AppendFormat(_L8("pkgurl='%S',"), &aPkgURL );
       
   331             }
       
   332         if (aChangedFields & EFDBPkgName )  
       
   333             {
       
   334             sql->Des().AppendFormat(_L8("pkgname='%S',") 
       
   335                 ,&(aState.iPkgName)) ;
       
   336             }
       
   337         if (aChangedFields & EFDBVersion )  
       
   338             {
       
   339             sql->Des().AppendFormat(_L8("Version='%S',")
       
   340                     ,&(aState.iPkgVersion));
       
   341             }
       
   342         if ( aChangedFields & EFDBSmlTryCount )
       
   343             {
       
   344  			__ASSERT_ALWAYS( aState.iSmlTryCount>=0
       
   345  				, User::Panic(KFotaPanic, KErrArgument) );            
       
   346             sql->Des().AppendFormat(_L8("SmlTryCount=%d,")
       
   347                 , aState.iSmlTryCount );
       
   348                     
       
   349             }
       
   350         if (aChangedFields & EFDBSessionType )    
       
   351             {
       
   352             sql->Des().AppendFormat(_L8("SessionType=%d,"),aState.iSessionType);
       
   353             }
       
   354         if (aChangedFields & EFDBIapId )    
       
   355             {
       
   356             // validate IAP ID
       
   357  			__ASSERT_ALWAYS( aState.iIapId>=-1 ,User::Panic(KFotaPanic, KErrArgument) );
       
   358             sql->Des().AppendFormat(_L8("IapId=%d,"),aState.iIapId);
       
   359             }
       
   360         if (aChangedFields & EFDBPkgSize )    
       
   361             {
       
   362             // validate size
       
   363  //			__ASSERT_ALWAYS( aState.iPkgSize>=0 ,User::Panic(KFotaPanic, KErrArgument) ); // to remove compiler warning
       
   364             sql->Des().AppendFormat(_L8("PkgSize=%d,"),aState.iPkgSize);
       
   365             }
       
   366         if (aChangedFields & EFDBUpdateLtr )    
       
   367             {
       
   368             // validate bit
       
   369             sql->Des().AppendFormat(_L8("UpdateLtr=%d,"),aState.iUpdateLtr);
       
   370             }
       
   371         // remove trailing ,
       
   372         if ( aChangedFields )
       
   373             {
       
   374             sql->Des().SetLength ( sql->Des().Length()-1 );
       
   375             }
       
   376         sql->Des().Append ( sqlEnd );
       
   377         HBufC*  sql2 = HBufC::NewLC( sql->Length() ); // to cleanupstack
       
   378         sql2->Des().Copy(sql->Des());
       
   379 		FLOG(_L("  sql:%S"),sql2);
       
   380         
       
   381         User::LeaveIfError( iStateDB.Begin() );
       
   382         User::LeaveIfError( iStateDB.Execute(*sql2) );
       
   383         User::LeaveIfError( iStateDB.Commit() );
       
   384         User::LeaveIfError( iStateDB.Compact() );
       
   385 
       
   386         CleanupStack::PopAndDestroy( sql2 );  //sql2
       
   387         CleanupStack::PopAndDestroy( sql );  //sql
       
   388         }
       
   389     FLOG(_L("  CFotaDB::SetStateL  <<"));
       
   390     }
       
   391 
       
   392 // ---------------------------------------------------------------------------
       
   393 // CFotaDB::DetermineCharNeeded 
       
   394 // Returns the char needed fro the query 
       
   395 // ---------------------------------------------------------------------------
       
   396 
       
   397 TInt CFotaDB::DetermineCharNeeded(TInt aChangedFields,TPackageState& aState,const TDesC8& aPkgURL)
       
   398 
       
   399 {	 
       
   400 	
       
   401 	      TInt sqlsize=0;
       
   402 	      if (aChangedFields & EFDBResult )       sqlsize += 4 + 7 + 4; 
       
   403         if (aChangedFields & EFDBState )        sqlsize += 4 + 5 + 4;
       
   404         if (aChangedFields & EFDBProfileId )    sqlsize += 4 + 9 + 4;
       
   405         if (aChangedFields & EFDBPkgUrl )       sqlsize += aPkgURL.Length() 
       
   406                                                            + 6 + 4;
       
   407         if (aChangedFields & EFDBPkgName )      sqlsize += 
       
   408                                                    aState.iPkgName.Length() 
       
   409                                                    + 7 + 4;
       
   410         if (aChangedFields & EFDBVersion )      sqlsize += 
       
   411                                                    aState.iPkgVersion.Length() 
       
   412                                                    + 7 + 4;
       
   413         if (aChangedFields & EFDBSmlTryCount )  sqlsize += 4 + 11 + 4;
       
   414         
       
   415         if (aChangedFields & EFDBSessionType )    sqlsize += 4 + 11 + 4;
       
   416         if (aChangedFields & EFDBIapId )    sqlsize += 4 + 5 + 4;
       
   417         if (aChangedFields & EFDBPkgSize )    sqlsize += 4 + 7 + 10;
       
   418         if (aChangedFields & EFDBUpdateLtr ) sqlsize += 4 + 11 + 4;
       
   419         
       
   420         return sqlsize;
       
   421 	
       
   422 }
       
   423 
       
   424 // ---------------------------------------------------------------------------
       
   425 // CFotaDB::SetRetryCount
       
   426 // Sets the retry count
       
   427 // ---------------------------------------------------------------------------
       
   428 void CFotaDB::SetRetryCount(TPackageState& aState)
       
   429 {
       
   430    CRepository* centrep( NULL);
       
   431    TInt err = KErrNone;
       
   432    TInt retry = 0;
       
   433   
       
   434    TRAP(err, centrep = CRepository::NewL( KCRUidFotaServer ) );
       
   435    if ( centrep ) 
       
   436      	   {
       
   437         	err = centrep->Get( KGenericAlertRetries, retry );
       
   438         	delete centrep; centrep = NULL;
       
   439         	}
       
   440         	if(err == KErrNone )
       
   441         	    {
       
   442             	    if(retry < 0 )
       
   443             	    {
       
   444             	    aState.iSmlTryCount = KDefaultSmlTryCount;
       
   445             	    }
       
   446                   	else if( retry == 0 )
       
   447                	    {
       
   448                	     aState.iSmlTryCount = 2 ;
       
   449                	    }
       
   450                 	else if (retry > KMaximumSmlTryCount )
       
   451                	    {
       
   452                	     aState.iSmlTryCount = KMaximumSmlTryCount + 1;
       
   453                	    }
       
   454                    	else
       
   455                     {
       
   456                 	  aState.iSmlTryCount = retry + 1;
       
   457                     }
       
   458         	    }
       
   459         	 else
       
   460         	    {
       
   461         	    aState.iSmlTryCount = KDefaultSmlTryCount;
       
   462         	    }
       
   463 }        	    
       
   464         	    
       
   465 
       
   466 // ---------------------------------------------------------------------------
       
   467 // CFotaDB::DeleteStateL
       
   468 // Delete state
       
   469 // ---------------------------------------------------------------------------
       
   470 TInt CFotaDB::DeleteStateL(const TInt aPkgId)
       
   471     {
       
   472 	FLOG(_L("[fota DB Delete]\tdeleting %d"),aPkgId);
       
   473 	TInt err( KErrNone );
       
   474 
       
   475     if ( iStateDB.InTransaction() )
       
   476     	{
       
   477 	    return ( KErrAccessDenied );
       
   478 	    }
       
   479 
       
   480 	_LIT( KSQLDeleteState,"DELETE FROM State WHERE PkgId = %d" );
       
   481 
       
   482     HBufC*  del  = HBufC::NewLC( KSQLDeleteState().Length() + 10);
       
   483     del->Des().Format (KSQLDeleteState, aPkgId);
       
   484 
       
   485 	iStateDB.Begin();
       
   486 
       
   487 	err = iStateDB.Execute( *del );
       
   488 	CleanupStack::PopAndDestroy( del );
       
   489 	if ( err < KErrNone )
       
   490 		{
       
   491 		iStateDB.Rollback();
       
   492 		return err;
       
   493 		}
       
   494     FLOG(_L("[fota DB Delete]\tdeleted %d"),aPkgId);
       
   495 	return KErrNone;
       
   496     }
       
   497 
       
   498 // ---------------------------------------------------------------------------
       
   499 // CFotaDB::RowToStateL
       
   500 // Extracts db row contents to package state object and aPkgUrl
       
   501 // Returns url in aPkgURL parameter
       
   502 // ---------------------------------------------------------------------------
       
   503 TPackageState CFotaDB::RowToStateL(HBufC8*& aPkgUrl,const RDbView& aView)
       
   504     {
       
   505     TPackageState   s;
       
   506     TInt            pkgid   = aView.ColInt( iColSet->ColNo(KColPkgId)  );
       
   507 	TInt            state   = aView.ColInt( iColSet->ColNo(KColState)  );
       
   508 	TInt            result  = aView.ColInt( iColSet->ColNo(KColResult)  );
       
   509     TSmlProfileId   profileid(aView.ColInt( iColSet->ColNo(KColProfileId)));
       
   510     TPtrC           pkgname = aView.ColDes( iColSet->ColNo(KColPkgName)  );
       
   511     TPtrC           version = aView.ColDes( iColSet->ColNo(KColVersion)  );
       
   512     TInt            smltrycount 
       
   513                         = aView.ColInt( iColSet->ColNo(KColSmlTryCount) );
       
   514     TInt            sessiontype = aView.ColInt( iColSet->ColNo(KColSessionType)  );
       
   515     TInt            iapid = aView.ColInt( iColSet->ColNo(KColIapId)  );
       
   516     TUint           pkgsize = aView.ColUint( iColSet->ColNo(KColPkgSize)  );
       
   517     TBool			updateltr = aView.ColUint8( iColSet->ColNo(KColUpdateLtr));
       
   518     
       
   519     s.iPkgId            = pkgid;
       
   520     s.iPkgName.Copy     (pkgname);
       
   521     s.iPkgVersion.Copy  (version);
       
   522     s.iProfileId        = profileid;
       
   523     s.iResult           = result;
       
   524     s.iState            = RFotaEngineSession::TState (state);
       
   525     s.iSmlTryCount      = smltrycount;
       
   526     s.iSessionType 		= sessiontype;
       
   527     s.iIapId 			= iapid;
       
   528     s.iPkgSize 			= pkgsize;
       
   529     s.iUpdateLtr		= updateltr;
       
   530 
       
   531     RDbColReadStream    rstream;
       
   532     TInt                len = aView.ColLength(iColSet->ColNo(KColPkgUrl));
       
   533     rstream.OpenLC(aView,iColSet->ColNo(KColPkgUrl) );
       
   534     HBufC*             pkgurl         = HBufC::NewLC( len );
       
   535     TPtr               ptr            = pkgurl->Des();
       
   536     rstream.ReadL( ptr, len );
       
   537 
       
   538     HBufC8*             tmp = HBufC8::NewL( pkgurl->Des().Length() );
       
   539     tmp->Des().Copy ( pkgurl->Des() );
       
   540     aPkgUrl = tmp;
       
   541 
       
   542     CleanupStack::PopAndDestroy( pkgurl ); 
       
   543     CleanupStack::PopAndDestroy( &rstream );
       
   544     return s;
       
   545     }
       
   546 
       
   547 
       
   548 // ---------------------------------------------------------------------------
       
   549 // CFotaDB::StateToRowL
       
   550 // Converts state object to database row (into view object)
       
   551 // ---------------------------------------------------------------------------
       
   552 void CFotaDB::StateToRowL (const TPackageState& aPkg, const TDesC8& aPkgURL
       
   553                                 ,RDbView& aView)
       
   554     {
       
   555     HBufC*  pkgname = HBufC::NewLC(aPkg.iPkgName.Length());
       
   556     HBufC*  version = HBufC::NewLC(aPkg.iPkgVersion.Length());
       
   557 
       
   558     pkgname->Des().Copy(aPkg.iPkgName);
       
   559     version->Des().Copy(aPkg.iPkgVersion);
       
   560 
       
   561     aView.SetColL( iColSet->ColNo( KColPkgId ),      aPkg.iPkgId);
       
   562 	aView.SetColL( iColSet->ColNo( KColResult ),     aPkg.iResult);
       
   563 	aView.SetColL( iColSet->ColNo( KColState ),      aPkg.iState);
       
   564 	aView.SetColL( iColSet->ColNo( KColProfileId ),  aPkg.iProfileId);
       
   565 	aView.SetColL( iColSet->ColNo( KColPkgName ),    *pkgname );
       
   566 	aView.SetColL( iColSet->ColNo( KColVersion ),    *version );
       
   567 	aView.SetColL( iColSet->ColNo( KColSmlTryCount ), aPkg.iSmlTryCount );
       
   568 	aView.SetColL( iColSet->ColNo( KColSessionType ), aPkg.iSessionType );
       
   569 	aView.SetColL( iColSet->ColNo( KColIapId ), 	  aPkg.iIapId  );
       
   570 	aView.SetColL( iColSet->ColNo( KColPkgSize ), 	  aPkg.iPkgSize );
       
   571 	aView.SetColL( iColSet->ColNo( KColUpdateLtr ),	  aPkg.iUpdateLtr );
       
   572 
       
   573     RDbColWriteStream wstream;
       
   574     CleanupClosePushL( wstream );
       
   575     wstream.OpenL( aView,iColSet->ColNo(KColPkgUrl) );
       
   576     // Cannot write 8 bit descriptors to databae
       
   577     HBufC* buf = HBufC::NewLC( aPkgURL.Length() );
       
   578     buf->Des().Copy( aPkgURL );
       
   579     wstream.WriteL( buf->Des() );
       
   580 
       
   581     FLOG(_L("CFotaDB::StateToRowL  id:%d result:%d state:%d profileid:%d \
       
   582     		name:%d chars version: %d chars url: %d chars sessiontype:%d iapid:%d pkgsize:%d updateltr = %d")
       
   583     		,aPkg.iPkgId, aPkg.iResult, aPkg.iState, aPkg.iProfileId
       
   584     		, pkgname->Des().Length(), version->Des().Length()
       
   585     		, buf->Des().Length(),aPkg.iSessionType,aPkg.iIapId,aPkg.iPkgSize, aPkg.iUpdateLtr);
       
   586 
       
   587     CleanupStack::PopAndDestroy( buf );
       
   588     CleanupStack::PopAndDestroy( &wstream );
       
   589     CleanupStack::PopAndDestroy( version );
       
   590     CleanupStack::PopAndDestroy( pkgname );
       
   591     }
       
   592 
       
   593 
       
   594 // ---------------------------------------------------------------------------
       
   595 // CFotaDB::AlterTableIfRequiredL()
       
   596 // Adds the new attributes when fota state table is old.
       
   597 // ---------------------------------------------------------------------------
       
   598 void CFotaDB::AlterTableIfRequiredL()
       
   599 	{
       
   600     FLOG(_L("CFotaDB::AlterTableIfRequired >>"));
       
   601 	TInt noofcol = iTable.ColCount();
       
   602 	//noofcol = 9 means old database; alteration is needed
       
   603 	if (noofcol!=KNoOfDBFields)
       
   604 		{
       
   605 		FLOG(_L("FotaState table is incompatible; needs alteration!"));
       
   606 	    User::LeaveIfError(iStateDB.Begin());
       
   607 
       
   608 
       
   609 	    TInt err (KErrNone);
       
   610 	    err = iStateDB.Execute(KAlterTable);
       
   611 	    if (!err)
       
   612 	    	{
       
   613 	    	err = iStateDB.Commit();
       
   614 		    if(err) 
       
   615 		        {
       
   616 		        FLOG(_L(" FotaState table alteration err2 %d, deleting it and recreating it again"),err);
       
   617 		    	CloseAndCommitDB();
       
   618 		        User::LeaveIfError (iFSSession.Delete(KDatabaseName));
       
   619 		        }
       
   620 	    	}
       
   621 	    else
       
   622 	    	{
       
   623 	    	FLOG(_L(" FotaState table alteration err1 %d, deleting it and recreating it again"),err);
       
   624 	    	CloseAndCommitDB();
       
   625 	    	User::LeaveIfError (iFSSession.Delete(KDatabaseName));
       
   626 	    	}
       
   627 	    if (!err)
       
   628 	    	{
       
   629 	    	User::LeaveIfError( iStateDB.Compact() );
       
   630 	    	CloseAndCommitDB();
       
   631 		    FLOG(_L("FotaState table altered successfully, reopening it again."));
       
   632 			iTableAltercheck = EFalse;
       
   633 	    	}
       
   634 	    OpenDBL();
       
   635 		}
       
   636 	else
       
   637 		iTableAltercheck = EFalse;
       
   638 
       
   639     FLOG(_L("CFotaDB::AlterTableIfRequired <<"));
       
   640 	}