phonebookengines/contactsmodel/cntplsql/src/cntpplviewsession.cpp
branchRCL_3
changeset 26 0d28c1c5b6dd
parent 23 5586b4d2ec3e
child 64 c1e8ba0c2b16
equal deleted inserted replaced
23:5586b4d2ec3e 26:0d28c1c5b6dd
    45 :  	CTimer(CActive::EPriorityIdle),
    45 :  	CTimer(CActive::EPriorityIdle),
    46     iViewId(aViewId),
    46     iViewId(aViewId),
    47 	iContactProperties(aContactProperties),
    47 	iContactProperties(aContactProperties),
    48     iContactsFile(aContactsFile),
    48     iContactsFile(aContactsFile),
    49 	iSqlSmtSelectAllFieldsById(aSelectAllFields),
    49 	iSqlSmtSelectAllFieldsById(aSelectAllFields),
    50 	iViewPrefs(aViewPrefs)
    50 	iViewPrefs(aViewPrefs),
       
    51 	iRSqlStatementReady(EFalse),
       
    52 	iCachedSqlStatementReady(EFalse)
    51 	{
    53 	{
    52 	CActiveScheduler::Add(this);
    54 	CActiveScheduler::Add(this);
    53 	}
    55 	}
    54 
    56 
    55 	
    57 	
    57 CCntPplViewSession destructor.
    59 CCntPplViewSession destructor.
    58 */
    60 */
    59 CCntPplViewSession::~CCntPplViewSession()
    61 CCntPplViewSession::~CCntPplViewSession()
    60 	{
    62 	{
    61 	Deque();
    63 	Deque();
    62 	
    64 	iContactsFile.RemoveSqlDBObserverL( *this );
    63 	if(iRSqlStatement)
    65     if ( iRSqlStatementReady )
    64 		{
    66         {
    65 		iRSqlStatement->Close();
    67         iRSqlStatement.Close();
    66 		delete iRSqlStatement;
    68         iRSqlStatementReady = EFalse;
    67 		}
    69         }
    68 	
    70 
    69 	if(iCachedSqlStatement)
    71     if ( iCachedSqlStatementReady )
    70 		{
    72         {
    71 		iCachedSqlStatement->Close();
    73         iCachedSqlStatement.Close();
    72 		delete iCachedSqlStatement;
    74         iCachedSqlStatementReady = EFalse;
    73 		}
    75         }
    74 	
    76 	
    75 	delete iCntSqlStatement;
    77 	delete iCntSqlStatement;
    76 	delete iTextDef;
    78 	delete iTextDef;
    77 	}
    79 	}
    78 
    80 
    83 void CCntPplViewSession::ConstructL(const CContactTextDef& aTextDef)
    85 void CCntPplViewSession::ConstructL(const CContactTextDef& aTextDef)
    84 	{
    86 	{
    85 	//Constructing iCntSqlStatement, iTextDef, and iIsFastAccessFieldsOnly
    87 	//Constructing iCntSqlStatement, iTextDef, and iIsFastAccessFieldsOnly
    86 	//by simply call ChangeSortOrderL
    88 	//by simply call ChangeSortOrderL
    87 	CTimer::ConstructL();
    89 	CTimer::ConstructL();
    88 	ChangeSortOrderL(aTextDef);
    90     iContactsFile.AddSqlDBObserverL( *this );
       
    91     ChangeSortOrderL( aTextDef );
    89 	}	
    92 	}	
    90 
    93 
    91 	
    94 	
    92 /**
    95 /**
    93 Change the sorting order/text definition. It should be always called when 
    96 Change the sorting order/text definition. It should be always called when 
   163 	if (! iContactsFile.IsOpened())
   166 	if (! iContactsFile.IsOpened())
   164 		{
   167 		{
   165 		User::Leave(KErrInUse);
   168 		User::Leave(KErrInUse);
   166 		}
   169 		}
   167 	
   170 	
   168 	if(iRSqlStatement)
   171 	if ( iRSqlStatementReady )
   169 		{
   172         {
   170 		iRSqlStatement->Close();
   173         iRSqlStatement.Close();
   171 		delete iRSqlStatement;
   174         iRSqlStatementReady = EFalse;
   172 		iRSqlStatement = NULL;
   175         }
   173 		}
   176 		
   174 	
       
   175 	//Create a new sqlstatment to start a iteration	
       
   176 	iRSqlStatement = new (ELeave) RSqlStatement();
       
   177 	
       
   178 	// we don't need condition for iterating database.
   177 	// we don't need condition for iterating database.
   179 	iCntSqlStatement->ClearCondition();
   178 	iCntSqlStatement->ClearCondition();
   180 	
   179 	
   181 	iRSqlStatement->PrepareL(iContactsFile.NamedDatabase(), iCntSqlStatement->SqlStringL());
   180 	iRSqlStatement.PrepareL(iContactsFile.NamedDatabase(), iCntSqlStatement->SqlStringL());
       
   181 	iRSqlStatementReady = ETrue;
   182 	}
   182 	}
   183 
   183 
   184 	
   184 	
   185 /**
   185 /**
   186 Stop iteration by releasing iteration sql statement.
   186 Stop iteration by releasing iteration sql statement.
   187 */
   187 */
   188 void CCntPplViewSession::EndIterate()
   188 void CCntPplViewSession::EndIterate()
   189 	{
   189     {
   190 	if(iRSqlStatement)
   190     if ( iRSqlStatementReady )
   191 		{
   191         {
   192 		iRSqlStatement->Close();
   192         iRSqlStatement.Close();
   193 		delete iRSqlStatement;
   193         iRSqlStatementReady = EFalse;
   194 		iRSqlStatement = NULL;
   194         }
   195 		}
   195     }
   196 	}
       
   197 
   196 
   198 	
   197 	
   199 /**
   198 /**
   200 Get next view contact object from iterating sql statement.
   199 Get next view contact object from iterating sql statement.
   201 
   200 
   202 @return CViewContact object or NULL if iteration reachs end of rows.
   201 @return CViewContact object or NULL if iteration reachs end of rows.
   203 */
   202 */
   204 CViewContact* CCntPplViewSession::NextItemL(TContactViewPreferences aViewPrefs)
   203 CViewContact* CCntPplViewSession::NextItemL(TContactViewPreferences aViewPrefs)
   205 	{
   204 	{
   206 	if(!iRSqlStatement)
   205 	if(!iRSqlStatementReady)
   207 		{
   206 		{
   208 		//Iteration has not started
   207 		//Iteration has not started
   209 		BeginIterateL();	
   208 		BeginIterateL();	
   210 		}
   209 		}
   211 	
   210 	
   212 	return CreateViewItemL(*iRSqlStatement, *iCntSqlStatement, aViewPrefs);
   211 	return CreateViewItemL(iRSqlStatement, *iCntSqlStatement, aViewPrefs);
   213 	}
   212 	}
   214 
   213 
   215 	
   214 	
   216 /**
   215 /**
   217 Get view contact object based on given contact id.
   216 Get view contact object based on given contact id.
   248     if (! iContactsFile.IsOpened())
   247     if (! iContactsFile.IsOpened())
   249     	{
   248     	{
   250     	User::Leave(KErrInUse);
   249     	User::Leave(KErrInUse);
   251     	}
   250     	}
   252     
   251     
   253 	if(!iCachedSqlStatement)
   252 	if ( !iCachedSqlStatementReady )
   254 		{
   253 		{
   255 		//iCacheSqlStatement is just for ItemAt while iRSqlStatement is used for iteration 
   254 		//iCacheSqlStatement is just for ItemAt while iRSqlStatement is used for iteration 
   256 		//and maintained by BeginIterateL and EndIterateL.
   255 		//and maintained by BeginIterateL and EndIterateL.
   257     	iCntSqlStatement->SetConditionL(KSelectCondition_SearchForContactId()); 
   256     	iCntSqlStatement->SetConditionL(KSelectCondition_SearchForContactId()); 
   258 		
   257 		
   259 		//Create a new sqlstatment and cache its Prepare statement.	
   258 		//Create a new sqlstatment and cache its Prepare statement.	
   260 		iCachedSqlStatement = new (ELeave) RSqlStatement();
   259 		iCachedSqlStatement.PrepareL(iContactsFile.NamedDatabase(),  iCntSqlStatement->SqlStringL());
   261 		iCachedSqlStatement->PrepareL(iContactsFile.NamedDatabase(),  iCntSqlStatement->SqlStringL());
   260 		iCachedSqlStatementReady = ETrue;
   262 		}
   261 		}
   263 		
   262 		
   264 	User::LeaveIfError(iCachedSqlStatement->BindInt(KFirstIndex, aContactId)); //Bind item id into the condition.	
   263 	User::LeaveIfError(iCachedSqlStatement.BindInt(KFirstIndex, aContactId)); //Bind item id into the condition.	
   265 
   264 
   266 	CViewContact* viewContact = CreateViewItemL(*iCachedSqlStatement, *iCntSqlStatement, iViewPrefs);
   265 	CViewContact* viewContact = CreateViewItemL(iCachedSqlStatement, *iCntSqlStatement, iViewPrefs);
   267 	
   266 	
   268 	// Adding this to the cleanup stack, because coverity recons that "iCachedSqlStatement->Reset();" can leave, and it's probably right
   267 	// Adding this to the cleanup stack, because coverity recons that "iCachedSqlStatement->Reset();" can leave, and it's probably right
   269 	CleanupStack::PushL(viewContact);
   268 	CleanupStack::PushL(viewContact);
   270 	iCachedSqlStatement->Reset();
   269 	iCachedSqlStatement.Reset();
   271 
   270 
   272 	// Pop it back off the cleanup stack, and hand over ownership
   271 	// Pop it back off the cleanup stack, and hand over ownership
   273 	CleanupStack::Pop(viewContact);
   272 	CleanupStack::Pop(viewContact);
   274 	return viewContact;
   273 	return viewContact;
   275     }
   274     }
   278 /**
   277 /**
   279 Clean up the cached prepare statement.
   278 Clean up the cached prepare statement.
   280 */
   279 */
   281 void CCntPplViewSession::CleanupCachedPrepareStatement()
   280 void CCntPplViewSession::CleanupCachedPrepareStatement()
   282     {
   281     {
   283 	if(iCachedSqlStatement)
   282     if ( iCachedSqlStatementReady )
   284 		{
   283         {
   285 		iCachedSqlStatement->Close();
   284         iCachedSqlStatement.Close();
   286 		delete iCachedSqlStatement;
   285         iCachedSqlStatementReady = EFalse;
   287 		iCachedSqlStatement = NULL;
   286         }
   288 		}
       
   289     }
   287     }
   290 
   288 
   291 /**
   289 /**
   292 CTimer callback funtion, the cached Prepare statement would be de-cached if ItemAtL is not called
   290 CTimer callback funtion, the cached Prepare statement would be de-cached if ItemAtL is not called
   293 during the time interval defined by KCachedPrepareTimeOut.
   291 during the time interval defined by KCachedPrepareTimeOut.
   294 */
   292 */
   295 void CCntPplViewSession::RunL()
   293 void CCntPplViewSession::RunL()
   296 	{
   294     {
   297 	CleanupCachedPrepareStatement();
   295     CleanupCachedPrepareStatement();
   298 	}
   296     }
   299 	
   297 	
   300 	
   298 	
   301 /**
   299 /**
   302 Create view contact object based on given sql statement.
   300 Create view contact object based on given sql statement.
   303 
   301 
   725 
   723 
   726     return correctType;
   724     return correctType;
   727     }
   725     }
   728 
   726 
   729 	
   727 	
       
   728 /**
       
   729  * Close the resource which is running and depends on RSqlDatabase 
       
   730  * that will be closed in CPplContactsFile.
       
   731  */
       
   732 void CCntPplViewSession::OnCloseL()
       
   733     {
       
   734 
       
   735     if ( iRSqlStatementReady )
       
   736         {
       
   737         iRSqlStatement.Close();
       
   738         iRSqlStatementReady = EFalse;
       
   739         }
       
   740 
       
   741     if ( iCachedSqlStatementReady )
       
   742         {
       
   743         iCachedSqlStatement.Close();
       
   744         iCachedSqlStatementReady = EFalse;
       
   745         }
       
   746 
       
   747     }
       
   748 
       
   749