videoscheduler/SchedulerServer/src/CCseScheduleDB.cpp
branchRCL_3
changeset 23 13a33d82ad98
equal deleted inserted replaced
22:826cea16efd9 23:13a33d82ad98
       
     1 /*
       
     2 * Copyright (c) 2006 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 the License "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:    Database where to store scheduled events.*
       
    15 */
       
    16 
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CCseScheduleDB.h"         // Header file for this class
       
    22 #include <ipvideo/CCseScheduledProgram.h>   // Represent one schedule in database
       
    23 #include <bautils.h>
       
    24 #include "CseDebug.h"               // Debug macros
       
    25 #include <babackup.h>
       
    26 
       
    27 // EXTERNAL DATA STRUCTURES
       
    28 // None
       
    29 
       
    30 // EXTERNAL FUNCTION PROTOTYPES  
       
    31 // None
       
    32 
       
    33 // CONSTANTS
       
    34 _LIT( KCseCDiskPrefix,  "c:"                         );
       
    35 _LIT( KCseSqlGetAll,    "SELECT * FROM ScheduleEvent");
       
    36 _LIT( KCseSelectFromDb, "SELECT * FROM "             );
       
    37 _LIT( KCseWhere,        " WHERE "                    );
       
    38 _LIT( KCseEqual,        " = "                        );
       
    39 _LIT( KCseOrderBy,      " ORDER BY "                 );
       
    40 _LIT( KCseAnd,          " AND "                      );
       
    41 _LIT( KCseLess,         " < "                        );
       
    42 _LIT( KCseMore,         " > "                        );
       
    43 
       
    44 static TUint KScheduleNameMaxLength = 255;
       
    45 static TUint KCseDatbaseVersionNumber = 1;
       
    46 
       
    47 // MACROS
       
    48 // None
       
    49 
       
    50 // LOCAL CONSTANTS AND MACROS
       
    51 // None
       
    52 
       
    53 // MODULE DATA STRUCTURES
       
    54 // None
       
    55 
       
    56 // LOCAL FUNCTION PROTOTYPES
       
    57 // None
       
    58 
       
    59 // FORWARD DECLARATIONS
       
    60 // None
       
    61 
       
    62 // ============================ MEMBER FUNCTIONS ===============================
       
    63 // ---------------------------------------------------------------------------
       
    64 // CCseScheduleDB::CCseScheduleDB()
       
    65 //
       
    66 // ---------------------------------------------------------------------------
       
    67 CCseScheduleDB::CCseScheduleDB() : iDbAccess( ECseDbOpen )
       
    68 	{
       
    69 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::CCseScheduleDB");
       
    70 	// Default C++ Constructor		
       
    71 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::CCseScheduleDB");
       
    72 	}
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // CCseScheduleDB::~CCseScheduleDB()
       
    76 //
       
    77 // ---------------------------------------------------------------------------	
       
    78 CCseScheduleDB::~CCseScheduleDB()
       
    79 	{
       
    80 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::~CCseScheduleDB");
       
    81 	       	
       
    82 	iScheduleDb.Close();
       
    83 	delete iScheduleFileStore;
       
    84 	iFsSession.Close(); 
       
    85 	
       
    86 	if( iCompactTimer )
       
    87         {
       
    88         iCompactTimer->Cancel();
       
    89         delete iCompactTimer;
       
    90         }
       
    91     delete iBackupWrapper;
       
    92     CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::~CCseScheduleDB");
       
    93 	}
       
    94 
       
    95 // ---------------------------------------------------------------------------
       
    96 // CCseScheduleDB::NewL()
       
    97 //
       
    98 // ---------------------------------------------------------------------------	
       
    99 CCseScheduleDB* CCseScheduleDB::NewL()
       
   100 	{
       
   101 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::NewL");
       
   102     CCseScheduleDB* self = new ( ELeave ) CCseScheduleDB();    
       
   103     CleanupStack::PushL( self );
       
   104     self->ConstructL();
       
   105     CleanupStack::Pop( self );    
       
   106     
       
   107     CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::NewL");
       
   108     
       
   109     return self;
       
   110 	}
       
   111 
       
   112 // ---------------------------------------------------------------------------
       
   113 // CCseScheduleDB::ConstructL()
       
   114 //
       
   115 // ---------------------------------------------------------------------------	
       
   116 void CCseScheduleDB::ConstructL()
       
   117 	{
       
   118 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::ConstructL");
       
   119     
       
   120     User::LeaveIfError( iFsSession.Connect() );
       
   121     
       
   122     iCompactTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
   123             
       
   124     iBackupWrapper = CBaBackupSessionWrapper::NewL();
       
   125 	
       
   126 	TRAPD( err, OpenOrCreateDbL() );
       
   127 
       
   128     if ( err != KErrNone )
       
   129         {
       
   130 		CSELOGSTRING2_HIGH_LEVEL( "CCseScheduleDB::ConstructL - Opening or creating the database FAILED: %d", err );
       
   131 
       
   132         if ( err != KErrNoMemory && 
       
   133              err != KErrLocked && 
       
   134              err != KErrDisMounted &&
       
   135              err != KErrDiskFull &&
       
   136              err != KErrNotReady )
       
   137             {
       
   138             // Delete and recreate database file. Cannot recover other way. 
       
   139     		CSELOGSTRING_HIGH_LEVEL( "CCseScheduleDB::ConstructL - deleting database" );
       
   140             RemoveDbL();
       
   141     		CSELOGSTRING_HIGH_LEVEL( "CCseScheduleDB::ConstructL - recreating database" );
       
   142             TRAPD( err, OpenOrCreateDbL() );
       
   143             if ( err != KErrNone )
       
   144                 {
       
   145                 CSELOGSTRING2_HIGH_LEVEL( "CCseScheduleDB::ConstructL couldnt recreate database (%d), leaving", err );
       
   146                 User::Leave( err );
       
   147                 }
       
   148             }
       
   149         else
       
   150             {
       
   151             CSELOGSTRING2_HIGH_LEVEL( "CCseScheduleDB::ConstructL leaving (%d)", err );
       
   152             User::Leave( err );
       
   153             }
       
   154         }
       
   155 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::ConstructL");	
       
   156 	}
       
   157 
       
   158 // ---------------------------------------------------------------------------
       
   159 // CCseScheduleDB::RemoveDbL()
       
   160 // ---------------------------------------------------------------------------
       
   161 void CCseScheduleDB::RemoveDbL()
       
   162     {
       
   163     delete iBackupWrapper;
       
   164 	iBackupWrapper = NULL;
       
   165     iScheduleDb.Close();
       
   166 	delete iScheduleFileStore;
       
   167 	iScheduleFileStore = NULL;
       
   168     if ( BaflUtils::FileExists( iFsSession, iDbFile ) )
       
   169         {
       
   170         TInt err( iFsSession.Delete( iDbFile ) );
       
   171 		CSELOGSTRING2_HIGH_LEVEL("CCseScheduleDB:: deleting database file, err = %d", err);
       
   172         }
       
   173 	}
       
   174 
       
   175 
       
   176 // ---------------------------------------------------------------------------
       
   177 // CCseScheduleDB::GetApplicationSchedulesL()
       
   178 //
       
   179 // ---------------------------------------------------------------------------	
       
   180 void CCseScheduleDB::GetApplicationSchedulesL( const TInt32 aAppUid,
       
   181 								RPointerArray<CCseScheduledProgram>& aArray )
       
   182 	{
       
   183 	CSELOGSTRING2_HIGH_LEVEL(">>>CCseScheduleDB::GetApplicationSchedulesL - AppUid: %d",
       
   184 	                          aAppUid);
       
   185 	
       
   186 	LeaveIfDbLockedL();
       
   187 	ResetCompactTimer();
       
   188 	
       
   189 	// Gets program occurence(s) that are going to happen next
       
   190 	// from database
       
   191 	TBuf<KCseCustomSqlLength> sqlStatement;
       
   192 	sqlStatement.Append( KCseSelectFromDb );	
       
   193 	sqlStatement.Append( KCseScheduleTable );
       
   194 	sqlStatement.Append( KCseWhere );
       
   195 	sqlStatement.Append( KCseScheduleApplicationUIDCol );
       
   196 	sqlStatement.Append( KCseEqual );
       
   197 	sqlStatement.AppendNum( aAppUid );
       
   198 	sqlStatement.Append( KCseOrderBy );
       
   199 	sqlStatement.Append( KCseScheduleStartTimeCol );
       
   200 
       
   201 	// Order table based on start time
       
   202 	RDbView view;
       
   203 	CleanupClosePushL( view );
       
   204 	User::LeaveIfError( view.Prepare( iScheduleDb, 
       
   205 						TDbQuery( sqlStatement ) ) );
       
   206 	User::LeaveIfError( view.EvaluateAll() );
       
   207 	
       
   208 	view.FirstL();
       
   209 	
       
   210 	CDbColSet* colSetOrder = view.ColSetL();
       
   211 	CleanupStack::PushL( colSetOrder );
       
   212 	
       
   213 	// Get the SQL table indexes.
       
   214 	TDbColNo keyIndex = colSetOrder->ColNo( KCseScheduleDbKeyCol );
       
   215 	TDbColNo nameIndex = colSetOrder->ColNo( KCseScheduleNameCol );
       
   216 	TDbColNo startTimeIndex = colSetOrder->ColNo( KCseScheduleStartTimeCol );
       
   217 	TDbColNo endTimeIndex = colSetOrder->ColNo( KCseScheduleEndTimeCol );
       
   218 	TDbColNo applicationUidIdIndex = colSetOrder->ColNo( KCseScheduleApplicationUIDCol );
       
   219 	TDbColNo plugInUidIndex = colSetOrder->ColNo( KCseSchedulePlugInUIDCol );
       
   220 	TDbColNo applicationSpecificIndex = colSetOrder->ColNo( KCseScheduleApplicationSpecificCol );
       
   221 	TDbColNo scheduleTypeIndex = colSetOrder->ColNo( KCseScheduleTypeCol );
       
   222 	TDbColNo pluginTypeIndex = colSetOrder->ColNo( KCseSchedulePluginTypeCol );
       
   223 	
       
   224 	CleanupStack::PopAndDestroy( colSetOrder );
       
   225 	
       
   226 	// Loop through the tables
       
   227 	while ( view.AtRow() )
       
   228 		{
       
   229 		// Create new CCseSceduledProgram to be added to array
       
   230 		CCseScheduledProgram* prog = CCseScheduledProgram::NewL();
       
   231 		CleanupStack::PushL( prog );			
       
   232 		
       
   233 		// Fill created schedule with DB information
       
   234 		view.GetL();
       
   235 		prog->SetDbIdentifier( view.ColUint32( keyIndex ) );
       
   236 		prog->SetName( view.ColDes8( nameIndex ) );
       
   237 		prog->SetStartTime( view.ColTime( startTimeIndex ) );
       
   238 		prog->SetEndTime( view.ColTime( endTimeIndex ) );
       
   239 		prog->SetAppUid( view.ColInt32( applicationUidIdIndex ) );
       
   240 		prog->SetPluginUid( view.ColInt32( plugInUidIndex ) );
       
   241 		prog->SetScheduleType( view.ColInt32( scheduleTypeIndex ) );
       
   242 		prog->SetPluginType( view.ColInt32( pluginTypeIndex ) );
       
   243 		
       
   244 		TInt streamLen( 0 );
       
   245 		streamLen = view.ColLength( applicationSpecificIndex );
       
   246 	
       
   247 		HBufC8* appDataBuffer = HBufC8::NewL( streamLen );
       
   248 		
       
   249 		CleanupStack::PushL( appDataBuffer );
       
   250 		
       
   251 		TPtr8 buffPtr( appDataBuffer->Des() );
       
   252 		RDbColReadStream readStream;
       
   253 		readStream.OpenLC( view, applicationSpecificIndex );
       
   254 		readStream.ReadL( buffPtr, streamLen );
       
   255 		readStream.Release();
       
   256 		CleanupStack::PopAndDestroy(); // readStream
       
   257 
       
   258 		prog->SetApplicationDataL( *appDataBuffer );
       
   259 		
       
   260 		CleanupStack::PopAndDestroy( appDataBuffer );
       
   261 		User::LeaveIfError( aArray.Append( prog ) );
       
   262 		CleanupStack::Pop( prog );
       
   263 		view.NextL();
       
   264 		}
       
   265 		
       
   266 	CleanupStack::PopAndDestroy( &view ); // Closes view
       
   267 	
       
   268 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::GetApplicationSchedulesL");	
       
   269 	}
       
   270 
       
   271 // ---------------------------------------------------------------------------
       
   272 // CCseScheduleDB::OpenOrCreateDbL()
       
   273 //
       
   274 // Create a new database. 
       
   275 // ---------------------------------------------------------------------------
       
   276 void CCseScheduleDB::OpenOrCreateDbL()
       
   277     {
       
   278     CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::OpenOrCreateDbL");
       
   279 	TBuf<KMaxFileName> path;
       
   280 	
       
   281 	// Get path to CSE's private directory
       
   282 	TInt err( iFsSession.PrivatePath( path ) );
       
   283 	if ( err != KErrNone )
       
   284 		{
       
   285 		CSELOGSTRING2_HIGH_LEVEL("iFsSession.PrivatePath() failed: %d", err );
       
   286 		User::Leave( err );
       
   287 		}
       
   288 
       
   289 	iDbFile.Zero();
       
   290 	iDbFile.Append( KCseCDiskPrefix );
       
   291 	iDbFile.Append( path );
       
   292 	if ( BaflUtils::CheckFolder( iFsSession, iDbFile ) != KErrNone ) 
       
   293 		{
       
   294 		err = iFsSession.CreatePrivatePath( EDriveC );
       
   295 		if ( err != KErrNone )
       
   296 			{
       
   297 			CSELOGSTRING2_HIGH_LEVEL( "CreatePrivatePath failed! %d", err );
       
   298 			User::Leave( err );
       
   299 			}
       
   300 		}
       
   301 	
       
   302 	iDbFile.Append( KCseScheduleDBName() );
       
   303 	
       
   304 	// If file doesn't exist create it
       
   305     if ( !BaflUtils::FileExists( iFsSession, iDbFile ) )
       
   306         {
       
   307         CSELOGSTRING_HIGH_LEVEL("CCseScheduleDB::OpenOrCreateDbL Create New");
       
   308         iScheduleDb.Close();
       
   309                     
       
   310         if( iScheduleFileStore )
       
   311             {
       
   312             delete iScheduleFileStore;
       
   313             iScheduleFileStore = NULL;
       
   314             }
       
   315             
       
   316 		iScheduleFileStore = CPermanentFileStore::ReplaceL( iFsSession,
       
   317                                                             iDbFile,
       
   318                                                             EFileRead|EFileWrite );
       
   319 		// Set file store type
       
   320 		iScheduleFileStore->SetTypeL( iScheduleFileStore->Layout() ); 
       
   321     	// Create stream object
       
   322 	    TStreamId id = iScheduleDb.CreateL( iScheduleFileStore );   
       
   323 		// Keep database id as root of store  
       
   324     	iScheduleFileStore->SetRootL( id ); 
       
   325 		// Complete creation by commiting
       
   326     	iScheduleFileStore->CommitL();                              
       
   327 
       
   328         CreateDatabaseTableL( iScheduleDb );        
       
   329         CreateScheduleTableL( iScheduleDb );
       
   330         SetDatabaseVersionL( );
       
   331         }
       
   332     // Or if it exists then just open it.
       
   333 	else
       
   334 		{
       
   335 		iScheduleDb.Close();
       
   336 	    delete iScheduleFileStore;
       
   337 		iScheduleFileStore = NULL;
       
   338 		iScheduleFileStore = CPermanentFileStore::OpenL( iFsSession, iDbFile,
       
   339 														EFileRead|EFileWrite );
       
   340 		iScheduleFileStore->SetTypeL( iScheduleFileStore->Layout() );
       
   341 		iScheduleDb.OpenL( iScheduleFileStore, iScheduleFileStore->Root() );
       
   342     	CheckDbVersionL();    	
       
   343 		User::LeaveIfError( iScheduleDb.Compact() );
       
   344 		}
       
   345 				
       
   346 	if ( !iBackupWrapper )
       
   347 	    {
       
   348 	    iBackupWrapper = CBaBackupSessionWrapper::NewL();
       
   349 	    }
       
   350 	    
       
   351 	iBackupWrapper->RegisterFileL( iDbFile, *this );
       
   352 
       
   353     CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::OpenOrCreateDbL");
       
   354     }
       
   355     
       
   356 // ---------------------------------------------------------------------------
       
   357 // CCseScheduleDB::CheckDbVersionL
       
   358 //
       
   359 // ---------------------------------------------------------------------------
       
   360 void CCseScheduleDB::CheckDbVersionL( )
       
   361 	{	
       
   362 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::CheckDbVersionL");
       
   363 	
       
   364 	TInt dbVersion( 0 );	
       
   365 	
       
   366 	TRAPD( leave, dbVersion = GetDbVersionL() );
       
   367 	
       
   368 	if ( dbVersion != KCseDatbaseVersionNumber || leave != KErrNone )
       
   369 	    {
       
   370     	CSELOGSTRING3_HIGH_LEVEL(
       
   371     	    "CCseScheduleDB::CheckDbVersionL Not ok, version: %d, error: %d",
       
   372     	    dbVersion, leave );
       
   373     	
       
   374 	    // Destroy the old one
       
   375 	    iScheduleDb.Close();
       
   376 	    delete iScheduleFileStore;
       
   377 	    iScheduleFileStore = NULL;
       
   378 	    
       
   379 	    // Create new one
       
   380 	    iScheduleFileStore = CPermanentFileStore::ReplaceL( iFsSession,
       
   381                                                             iDbFile,
       
   382                                                             EFileRead|EFileWrite );
       
   383                                                         
       
   384 		// Set file store type
       
   385 		iScheduleFileStore->SetTypeL( iScheduleFileStore->Layout() ); 
       
   386     	// Create stream object
       
   387 	    TStreamId id = iScheduleDb.CreateL( iScheduleFileStore );   
       
   388 		// Keep database id as root of store  
       
   389     	iScheduleFileStore->SetRootL( id ); 
       
   390 		// Complete creation by commiting
       
   391     	iScheduleFileStore->CommitL();                              
       
   392 
       
   393     	CreateDatabaseTableL( iScheduleDb );
       
   394         CreateScheduleTableL( iScheduleDb );
       
   395 	    SetDatabaseVersionL( );
       
   396 	    }
       
   397 	
       
   398 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::CheckDbVersionL");		
       
   399 	}
       
   400 
       
   401 // ---------------------------------------------------------------------------
       
   402 // CCseScheduleDB::GetDbVersionL
       
   403 //
       
   404 // ---------------------------------------------------------------------------
       
   405 TInt CCseScheduleDB::GetDbVersionL( )
       
   406 	{	
       
   407 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::GetDbVersionL");
       
   408 	LeaveIfDbLockedL();
       
   409 	ResetCompactTimer();
       
   410 	TInt dbVersion(0);
       
   411 	
       
   412 	TBuf<KCseCustomSqlLength> sqlStatement;
       
   413 	sqlStatement.Append( KCseSelectFromDb );
       
   414 	sqlStatement.Append( KCseDatabaseTable );
       
   415 		
       
   416 	// Order table based on start time
       
   417 	RDbView view;
       
   418 	CleanupClosePushL( view );
       
   419 	User::LeaveIfError( view.Prepare( iScheduleDb, 
       
   420 						TDbQuery( sqlStatement ) ) );
       
   421 	User::LeaveIfError( view.EvaluateAll() );
       
   422 	
       
   423 	// Get the next reminder
       
   424 	view.FirstL();
       
   425 	
       
   426 	CDbColSet* colSetOrder = view.ColSetL();
       
   427 	CleanupStack::PushL( colSetOrder );
       
   428 	
       
   429 	TDbColNo versionIndex = colSetOrder->ColNo( KCseDatabaseVersionCol );
       
   430 	
       
   431 	CleanupStack::PopAndDestroy( colSetOrder );
       
   432 		
       
   433 	if ( view.AtRow() )
       
   434 		{				
       
   435 		view.GetL();
       
   436 		dbVersion = view.ColInt32( versionIndex );		
       
   437 		}
       
   438 	CleanupStack::PopAndDestroy( &view ); // Closes view
       
   439 
       
   440 	CSELOGSTRING2_HIGH_LEVEL(
       
   441 	    "<<<CCseScheduleDB::GetDbVersionL, version: %d",
       
   442 	    dbVersion );
       
   443 	return dbVersion;
       
   444 	}
       
   445 
       
   446 // ---------------------------------------------------------------------------
       
   447 // CCseScheduleDB::CreateScheduleTableL
       
   448 //
       
   449 // Creates Schedule table.
       
   450 // ---------------------------------------------------------------------------
       
   451 void CCseScheduleDB::CreateScheduleTableL( RDbStoreDatabase& aDatabase ) const
       
   452     {
       
   453     CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::CreateScheduleTableL");
       
   454     
       
   455     // Create columns for the database.     
       
   456     TDbCol keyCol( KCseScheduleDbKeyCol, EDbColUint32 );
       
   457     keyCol.iAttributes = TDbCol::EAutoIncrement;
       
   458     
       
   459 	TDbCol nameCol( KCseScheduleNameCol, EDbColText8, KScheduleNameMaxLength );		
       
   460 	TDbCol startTimeCol( KCseScheduleStartTimeCol, EDbColDateTime );
       
   461 	TDbCol endTimeCol( KCseScheduleEndTimeCol, EDbColDateTime );
       
   462     TDbCol applicationUidCol( KCseScheduleApplicationUIDCol, EDbColInt32 );   
       
   463 	TDbCol plugInUidCol( KCseSchedulePlugInUIDCol, EDbColInt32 );    
       
   464 	TDbCol typeCol( KCseScheduleTypeCol, EDbColInt32 );    	
       
   465 	TDbCol applicationSpecificCol( KCseScheduleApplicationSpecificCol, EDbColLongBinary );
       
   466 	TDbCol runCountCol( KCseScheduleRunCountCol, EDbColInt32 );	
       
   467 	TDbCol stateCol( KCseScheduleStateCol, EDbColInt32 );	
       
   468 	TDbCol pluginTypeCol( KCseSchedulePluginTypeCol, EDbColInt32 );	
       
   469 	TDbCol reserved1Col( KCseScheduleReserved1Col, EDbColInt32 );	
       
   470 	TDbCol reserved2Col( KCseScheduleReserved2Col, EDbColInt32 );	
       
   471 
       
   472 	// Create column set and add defined columns in to the set
       
   473     CDbColSet* scheduleColSet = CDbColSet::NewLC();
       
   474     scheduleColSet->AddL( keyCol );
       
   475 	scheduleColSet->AddL( nameCol );
       
   476 	scheduleColSet->AddL( startTimeCol );
       
   477 	scheduleColSet->AddL( endTimeCol );
       
   478     scheduleColSet->AddL( applicationUidCol );
       
   479     scheduleColSet->AddL( plugInUidCol );
       
   480 	scheduleColSet->AddL( typeCol );
       
   481     scheduleColSet->AddL( applicationSpecificCol );	
       
   482     scheduleColSet->AddL( runCountCol );	
       
   483     scheduleColSet->AddL( stateCol );
       
   484     scheduleColSet->AddL( pluginTypeCol );
       
   485     scheduleColSet->AddL( reserved1Col );
       
   486     scheduleColSet->AddL( reserved2Col );
       
   487     
       
   488     // Create new table to the database with created columnset
       
   489     User::LeaveIfError( aDatabase.CreateTable( KCseScheduleTable, 
       
   490 										      *scheduleColSet ) ); 
       
   491     
       
   492     CleanupStack::PopAndDestroy( scheduleColSet );
       
   493     
       
   494     CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::CreateScheduleTableL");
       
   495     }
       
   496 
       
   497 // ---------------------------------------------------------------------------
       
   498 // CCseScheduleDB::CreateDatabaseTableL
       
   499 //
       
   500 // Creates Database table.
       
   501 // ---------------------------------------------------------------------------
       
   502 void CCseScheduleDB::CreateDatabaseTableL( RDbStoreDatabase& aDatabase )
       
   503     {
       
   504     CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::CreateDatabaseTableL");
       
   505 
       
   506     // Create columns for the database.     
       
   507     TDbCol versionCol( KCseDatabaseVersionCol, EDbColInt32 );
       
   508     TDbCol reserved1Col( KCseDatabaseReserved1Col, EDbColInt32 );
       
   509     TDbCol reserved2Col( KCseDatabaseReserved2Col, EDbColInt32 );
       
   510 
       
   511 	// Create column set and add defined columns in to the set
       
   512     CDbColSet* databaseColSet = CDbColSet::NewLC();
       
   513     databaseColSet->AddL( versionCol );
       
   514     databaseColSet->AddL( reserved1Col );
       
   515     databaseColSet->AddL( reserved2Col );
       
   516     
       
   517     // Create new table to the database with created columnset
       
   518     User::LeaveIfError( aDatabase.CreateTable( KCseDatabaseTable, 
       
   519 										      *databaseColSet ) ); 
       
   520     
       
   521     CleanupStack::PopAndDestroy( databaseColSet );
       
   522     
       
   523     CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::CreateDatabaseTableL");
       
   524     }
       
   525 
       
   526 // ---------------------------------------------------------------------------
       
   527 // CCseScheduleDB::SetDatabaseVersionL
       
   528 //
       
   529 // Creates Database table.
       
   530 // ---------------------------------------------------------------------------
       
   531 void CCseScheduleDB::SetDatabaseVersionL( )
       
   532     {
       
   533     CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::SetDatabaseVersionL");
       
   534     
       
   535     iSqlSchedule.Zero(); 
       
   536     iSqlSchedule.Append( KCseSelectFromDb );
       
   537     iSqlSchedule.Append( KCseDatabaseTable );
       
   538     
       
   539     RDbView view;
       
   540     CleanupClosePushL( view );
       
   541     
       
   542     // Prepare DB for update
       
   543     User::LeaveIfError(view.Prepare( iScheduleDb,
       
   544     					TDbQuery( iSqlSchedule ),
       
   545     					TDbWindow::EUnlimited,
       
   546     					RDbView::EInsertOnly ));    
       
   547     view.InsertL();
       
   548     
       
   549     // Create colomn set (row) to be added
       
   550     CDbColSet* databaseColSet = view.ColSetL();
       
   551     CleanupStack::PushL( databaseColSet );
       
   552 
       
   553     // Fill row with Schedule information
       
   554 	view.SetColL( databaseColSet->ColNo( KCseDatabaseVersionCol ),
       
   555 										 KCseDatbaseVersionNumber );
       
   556     CleanupStack::PopAndDestroy( databaseColSet );
       
   557     view.PutL();
       
   558 	CleanupStack::PopAndDestroy( &view ); // closes view
       
   559     
       
   560     CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::SetDatabaseVersionL");
       
   561     }
       
   562 
       
   563 // ---------------------------------------------------------------------------
       
   564 // CCseScheduleDB::AddScheduleL()
       
   565 //
       
   566 // ---------------------------------------------------------------------------
       
   567 void CCseScheduleDB::AddScheduleL( CCseScheduledProgram& aData )
       
   568 	{
       
   569 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::AddScheduleL");
       
   570 	LeaveIfDbLockedL();
       
   571 	ResetCompactTimer();
       
   572 	
       
   573 	iSqlSchedule.Zero();
       
   574 	iSqlSchedule.Append( KCseSqlGetAll );
       
   575     RDbView view;
       
   576     CleanupClosePushL( view );
       
   577 
       
   578     // Prepare DB for update
       
   579     User::LeaveIfError(view.Prepare( iScheduleDb,
       
   580     					TDbQuery( iSqlSchedule ),
       
   581     					TDbWindow::EUnlimited,
       
   582     					RDbView::EInsertOnly ));    
       
   583     view.InsertL();
       
   584     
       
   585     // Create colomn set (row) to be added
       
   586     CDbColSet* scheduleColSet = view.ColSetL();
       
   587     CleanupStack::PushL( scheduleColSet );
       
   588 
       
   589     // Fill row with Schedule information
       
   590 	view.SetColL( scheduleColSet->ColNo( KCseScheduleNameCol ),
       
   591 										aData.Name() );
       
   592 
       
   593 	view.SetColL( scheduleColSet->ColNo( KCseScheduleStartTimeCol ),
       
   594 										aData.StartTime() );
       
   595 
       
   596 	view.SetColL( scheduleColSet->ColNo( KCseScheduleEndTimeCol ),
       
   597 										aData.EndTime() );
       
   598 
       
   599 	view.SetColL( scheduleColSet->ColNo( KCseScheduleApplicationUIDCol ), 
       
   600 										aData.AppUid() );
       
   601 
       
   602 	view.SetColL( scheduleColSet->ColNo( KCseSchedulePlugInUIDCol ), 
       
   603 										 aData.PluginUid() );	
       
   604 
       
   605 	view.SetColL( scheduleColSet->ColNo( KCseScheduleTypeCol ), 
       
   606 										 aData.ScheduleType() );	
       
   607 	
       
   608 	view.SetColL( scheduleColSet->ColNo( KCseScheduleRunCountCol ), 
       
   609 										 0 );
       
   610 										 
       
   611     view.SetColL( scheduleColSet->ColNo( KCseScheduleStateCol ), 
       
   612 										 ECseWaiting );
       
   613 										 
       
   614     view.SetColL( scheduleColSet->ColNo( KCseSchedulePluginTypeCol ), 
       
   615 										 aData.PluginType() );
       
   616 										 
       
   617 	// Description is long one, it needs to be handled by stream
       
   618 	RDbColWriteStream write;
       
   619 	write.OpenLC( view, scheduleColSet->ColNo( KCseScheduleApplicationSpecificCol ) );
       
   620 	write.WriteL( aData.ApplicationData() );
       
   621 	write.CommitL();
       
   622 	CleanupStack::PopAndDestroy(); // write
       
   623 	
       
   624 	TDbColNo keyColumnNo = scheduleColSet->ColNo( KCseScheduleDbKeyCol );
       
   625     CleanupStack::PopAndDestroy( scheduleColSet );
       
   626     view.PutL();
       
   627 
       
   628     // Get new program key
       
   629     aData.SetDbIdentifier( view.ColUint32( keyColumnNo ) );
       
   630     
       
   631     CleanupStack::PopAndDestroy( &view ); // closes view
       
   632     
       
   633     CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::AddScheduleL");
       
   634 	}
       
   635 
       
   636 // ---------------------------------------------------------------------------
       
   637 // CCseScheduleDB::RemoveScheduleL()
       
   638 //
       
   639 // ---------------------------------------------------------------------------
       
   640 void CCseScheduleDB::RemoveScheduleL( const TUint32 aDbIdentifier )
       
   641 	{	
       
   642 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::RemoveScheduleL");
       
   643 	LeaveIfDbLockedL();
       
   644 	ResetCompactTimer();
       
   645 	
       
   646 	// Create SQL string to find given schedule from DB
       
   647 	TBuf<KCseCustomSqlLength> sqlStatement;
       
   648 	sqlStatement.Append( KCseSelectFromDb );
       
   649 	sqlStatement.Append( KCseScheduleTable );
       
   650 	sqlStatement.Append( KCseWhere );
       
   651 	sqlStatement.Append( KCseScheduleDbKeyCol );
       
   652 	sqlStatement.Append( KCseEqual );
       
   653 	sqlStatement.AppendNum( aDbIdentifier );
       
   654 	
       
   655 	User::LeaveIfError( iScheduleDb.Begin() );
       
   656 	
       
   657 	// Prepare DB to update
       
   658 	RDbView view;
       
   659 	CleanupClosePushL( view );
       
   660 	User::LeaveIfError( view.Prepare( iScheduleDb, 
       
   661 									  TDbQuery( sqlStatement ) ) );
       
   662 	User::LeaveIfError( view.EvaluateAll() );
       
   663 	
       
   664 	view.FirstL();
       
   665 
       
   666 	TInt count( 0 );
       
   667 
       
   668     // If schedule found, remove it.
       
   669 	while ( view.AtRow() )
       
   670 		{
       
   671 		view.DeleteL();
       
   672 		count++;
       
   673 		view.NextL();	
       
   674 		}		
       
   675 	
       
   676 	CleanupStack::PopAndDestroy( &view ); // closes view
       
   677 	User::LeaveIfError( iScheduleDb.Commit() );
       
   678 	    	
       
   679     CSELOGSTRING3_HIGH_LEVEL("---- %d Items for identifier %d removed from DB", count, aDbIdentifier);
       
   680 	if ( count == 0 )
       
   681 		{
       
   682 		User::Leave( KErrNotFound );	
       
   683 		}
       
   684 	
       
   685 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::RemoveScheduleL");
       
   686 	}
       
   687 	
       
   688 // ---------------------------------------------------------------------------
       
   689 // CCseScheduleDB::GetNextScheduleTimeL()
       
   690 //
       
   691 // ---------------------------------------------------------------------------	
       
   692 TTime CCseScheduleDB::GetNextScheduleTimeL()
       
   693 	{
       
   694 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::GetNextScheduleTimeL");
       
   695 	LeaveIfDbLockedL();
       
   696 	ResetCompactTimer();
       
   697 	
       
   698 	// Create SQL satring to get schedule occurence(s) that are
       
   699 	// going to happen next from database.
       
   700 	TTime nextScheduleTime( 0 );
       
   701 	TBuf<KCseCustomSqlLength> sqlStatement;
       
   702 	sqlStatement.Append( KCseSelectFromDb );	
       
   703 	sqlStatement.Append( KCseScheduleTable );
       
   704 	sqlStatement.Append( KCseWhere );
       
   705 	sqlStatement.Append( KCseScheduleStateCol );
       
   706 	sqlStatement.Append( KCseEqual );
       
   707 	sqlStatement.AppendNum( ECseWaiting );
       
   708 	sqlStatement.Append( KCseOrderBy );
       
   709 	sqlStatement.Append( KCseScheduleStartTimeCol );
       
   710 		
       
   711 	// Order table based on start time
       
   712 	RDbView view;
       
   713 	CleanupClosePushL( view );
       
   714 	User::LeaveIfError( view.Prepare( iScheduleDb, 
       
   715 						TDbQuery( sqlStatement ) ) );
       
   716 	User::LeaveIfError( view.EvaluateAll() );
       
   717 	
       
   718 	// Get the next schedule
       
   719 	view.FirstL();
       
   720 	
       
   721 	CDbColSet* colSetOrder = view.ColSetL();
       
   722 	CleanupStack::PushL( colSetOrder );
       
   723 	
       
   724 	TDbColNo startTimeIndex = colSetOrder->ColNo( KCseScheduleStartTimeCol );
       
   725 	
       
   726 	CleanupStack::PopAndDestroy( colSetOrder );
       
   727 	
       
   728 	// Schedule found get its start time.
       
   729 	if ( view.AtRow() )	
       
   730 		{		
       
   731 		view.GetL();				
       
   732 		nextScheduleTime = view.ColTime( startTimeIndex );
       
   733 		}
       
   734 		
       
   735 	CleanupStack::PopAndDestroy( &view ); // Closes view
       
   736 	
       
   737 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::GetNextScheduleTimeL");
       
   738 	
       
   739 	return nextScheduleTime;
       
   740 	}
       
   741 	
       
   742 	
       
   743 
       
   744 // ---------------------------------------------------------------------------
       
   745 // CCseScheduleDB::FillScheduleArrayByTimeL()
       
   746 //
       
   747 // ---------------------------------------------------------------------------	
       
   748 void CCseScheduleDB::FillScheduleArrayByTimeL( const TTime& aScheduleTime, 
       
   749 					RPointerArray<CCseScheduledProgram>& aNextScheduleArray )
       
   750 	{
       
   751 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::FillScheduleArrayByTimeL");
       
   752 	LeaveIfDbLockedL();
       
   753 	ResetCompactTimer();
       
   754 	
       
   755 	// Gets program occurence(s) that are going to happen next
       
   756 	// from database
       
   757 	TBuf<100> startTimeBuf;
       
   758 	
       
   759 	_LIT( KDateTimeFormat,"# %1%*D/%2%*M/%3%*Y %H:%T:%S.%C#" ); 
       
   760 
       
   761 	aScheduleTime.FormatL( startTimeBuf, KDateTimeFormat );
       
   762 		
       
   763 	// Create SQL string to get all schedules to be happen at given time
       
   764 	TBuf<KCseCustomSqlLength> sqlStatement;
       
   765 	sqlStatement.Append( KCseSelectFromDb );	
       
   766 	sqlStatement.Append( KCseScheduleTable );
       
   767 	sqlStatement.Append( KCseWhere );
       
   768 	sqlStatement.Append( KCseScheduleStartTimeCol );
       
   769 	sqlStatement.Append( KCseEqual );
       
   770 	sqlStatement.Append( startTimeBuf );
       
   771 		
       
   772 	// Order table based on start time
       
   773 	RDbView view;
       
   774 	CleanupClosePushL( view );
       
   775 	User::LeaveIfError( view.Prepare( iScheduleDb, 
       
   776 						TDbQuery( sqlStatement ) ) );
       
   777 	User::LeaveIfError( view.EvaluateAll() );
       
   778 	
       
   779 	// Get the next reminder
       
   780 	view.FirstL();
       
   781 	
       
   782 	CDbColSet* colSetOrder = view.ColSetL();
       
   783 	CleanupStack::PushL( colSetOrder );
       
   784 	
       
   785 	// Get column indexes
       
   786 	TDbColNo keyIndex = colSetOrder->ColNo( KCseScheduleDbKeyCol );
       
   787 	TDbColNo nameIndex = colSetOrder->ColNo( KCseScheduleNameCol );
       
   788 	TDbColNo startTimeIndex = colSetOrder->ColNo( KCseScheduleStartTimeCol );
       
   789 	TDbColNo endTimeIndex = colSetOrder->ColNo( KCseScheduleEndTimeCol );
       
   790 	TDbColNo applicationUidIdIndex = colSetOrder->ColNo( KCseScheduleApplicationUIDCol );
       
   791 	TDbColNo plugInUidIndex = colSetOrder->ColNo( KCseSchedulePlugInUIDCol );
       
   792 	TDbColNo typeIndex = colSetOrder->ColNo( KCseScheduleTypeCol );
       
   793 	TDbColNo applicationSpecificIndex = colSetOrder->ColNo( KCseScheduleApplicationSpecificCol );
       
   794 	TDbColNo pluginTypeIndex = colSetOrder->ColNo( KCseSchedulePluginTypeCol );
       
   795 	
       
   796 	CleanupStack::PopAndDestroy( colSetOrder );
       
   797 	
       
   798 	// Loop through all found schedules.
       
   799 	while ( view.AtRow() )
       
   800 		{
       
   801 		// Create new schedule to be added to array
       
   802 		CCseScheduledProgram* prog = CCseScheduledProgram::NewL();
       
   803 		CleanupStack::PushL( prog );			
       
   804 		view.GetL();
       
   805 		
       
   806 		// Copy information from DB to schedule.
       
   807 		prog->SetDbIdentifier( view.ColUint32( keyIndex ) );
       
   808 		prog->SetName( view.ColDes8( nameIndex ) );
       
   809 		prog->SetStartTime( view.ColTime( startTimeIndex ) );
       
   810 		prog->SetEndTime( view.ColTime( endTimeIndex ) );
       
   811 		prog->SetAppUid( view.ColInt32( applicationUidIdIndex ) );
       
   812 		prog->SetPluginUid( view.ColInt32( plugInUidIndex ) );
       
   813 		prog->SetScheduleType( CCseScheduledProgram::TCseScheduleType( 
       
   814 		                       view.ColInt32( typeIndex ) ) );
       
   815         prog->SetPluginType( CCseScheduledProgram::TCsePluginType( 
       
   816 							   view.ColInt32( pluginTypeIndex ) ) );
       
   817 		TInt streamLen( view.ColLength( applicationSpecificIndex ) );
       
   818 	
       
   819 		HBufC8* appDataBuffer = HBufC8::NewL( streamLen );
       
   820 		CleanupStack::PushL( appDataBuffer );
       
   821 		
       
   822 		TPtr8 buffPtr( appDataBuffer->Des() );
       
   823 		RDbColReadStream readStream;
       
   824 		readStream.OpenLC( view, applicationSpecificIndex );
       
   825 		readStream.ReadL( buffPtr, streamLen );
       
   826 		readStream.Release();
       
   827 		CleanupStack::PopAndDestroy(); // readStream;
       
   828 
       
   829 		prog->SetApplicationDataL( *appDataBuffer );
       
   830 		
       
   831 		CleanupStack::PopAndDestroy( appDataBuffer );
       
   832 		User::LeaveIfError( aNextScheduleArray.Append( prog ) );
       
   833 		CleanupStack::Pop( prog );
       
   834 		
       
   835 		// And next schedule...
       
   836 		view.NextL();
       
   837 		}
       
   838 		
       
   839 	CleanupStack::PopAndDestroy( &view ); // Closes view
       
   840 	
       
   841 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::FillScheduleArrayByTimeL");
       
   842 	}
       
   843 
       
   844 
       
   845 // ---------------------------------------------------------------------------
       
   846 // CCseScheduleDB::GetScheduleByDbIdentifierL()
       
   847 //
       
   848 // ---------------------------------------------------------------------------	
       
   849 void CCseScheduleDB::GetScheduleByDbIdentifierL( const TUint32 aDbIdentifier, 
       
   850 								           CCseScheduledProgram* aSchedule )
       
   851 	{
       
   852 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::GetScheduleByDbIdentifierL");
       
   853 	LeaveIfDbLockedL();
       
   854 	ResetCompactTimer();
       
   855 	
       
   856 	if ( !aSchedule )
       
   857 		{
       
   858 		return; 
       
   859 		}		
       
   860 	
       
   861 	TBuf<KCseCustomSqlLength> sqlStatement;
       
   862 	sqlStatement.Append( KCseSelectFromDb );	
       
   863 	sqlStatement.Append( KCseScheduleTable );
       
   864 	sqlStatement.Append( KCseWhere );
       
   865 	sqlStatement.Append( KCseScheduleDbKeyCol );
       
   866 	sqlStatement.Append( KCseEqual );
       
   867 	sqlStatement.AppendNum( aDbIdentifier );
       
   868 		
       
   869 	// Order table based on start time
       
   870 	RDbView view;
       
   871 	CleanupClosePushL( view );
       
   872 	User::LeaveIfError( view.Prepare( iScheduleDb, 
       
   873 						TDbQuery( sqlStatement ) ) );
       
   874 	User::LeaveIfError( view.EvaluateAll() );
       
   875 	
       
   876 	// Get the next reminder
       
   877 	view.FirstL();
       
   878 	
       
   879 	CDbColSet* colSetOrder = view.ColSetL();
       
   880 	CleanupStack::PushL( colSetOrder );
       
   881 	
       
   882 	TDbColNo keyIndex = colSetOrder->ColNo( KCseScheduleDbKeyCol );
       
   883 	TDbColNo nameIndex = colSetOrder->ColNo( KCseScheduleNameCol );
       
   884 	TDbColNo startTimeIndex = colSetOrder->ColNo( KCseScheduleStartTimeCol );
       
   885 	TDbColNo endTimeIndex = colSetOrder->ColNo( KCseScheduleEndTimeCol );
       
   886 	TDbColNo applicationUidIdIndex = colSetOrder->ColNo( KCseScheduleApplicationUIDCol );
       
   887 	TDbColNo plugInUidIndex = colSetOrder->ColNo( KCseSchedulePlugInUIDCol );
       
   888 	TDbColNo typeIndex = colSetOrder->ColNo( KCseScheduleTypeCol );
       
   889 	TDbColNo applicationSpecificIndex = colSetOrder->ColNo( KCseScheduleApplicationSpecificCol );
       
   890 	TDbColNo pluginTypeIndex = colSetOrder->ColNo( KCseSchedulePluginTypeCol );
       
   891 	
       
   892 	CleanupStack::PopAndDestroy( colSetOrder );
       
   893 		
       
   894 	while ( view.AtRow() )
       
   895 		{				
       
   896 		view.GetL();
       
   897 		aSchedule->SetDbIdentifier( view.ColUint32( keyIndex ) );
       
   898 		aSchedule->SetName( view.ColDes8( nameIndex ) );
       
   899 		aSchedule->SetStartTime( view.ColTime( startTimeIndex ) );
       
   900 		aSchedule->SetEndTime( view.ColTime( endTimeIndex ) );
       
   901 		aSchedule->SetAppUid( view.ColInt32( applicationUidIdIndex ) );
       
   902 		aSchedule->SetPluginUid( view.ColInt32( plugInUidIndex ) );
       
   903 		
       
   904 		aSchedule->SetScheduleType( CCseScheduledProgram::TCseScheduleType( 
       
   905 							   view.ColInt32( typeIndex ) ) );
       
   906         aSchedule->SetPluginType( CCseScheduledProgram::TCsePluginType( 
       
   907 							   view.ColInt32( pluginTypeIndex ) ) );
       
   908 
       
   909 		TInt streamLen( 0 );
       
   910 		streamLen = view.ColLength( applicationSpecificIndex );
       
   911 	
       
   912 		HBufC8* appDataBuffer = HBufC8::NewL( streamLen );
       
   913 		CleanupStack::PushL( appDataBuffer );
       
   914 		
       
   915 		TPtr8 buffPtr( appDataBuffer->Des() );
       
   916 		RDbColReadStream readStream;
       
   917 		readStream.OpenLC( view, applicationSpecificIndex ); // |->1
       
   918 		readStream.ReadL(buffPtr, streamLen);
       
   919 		readStream.Release();
       
   920 		CleanupStack::PopAndDestroy( ); // 1<-| Popping pointer to Col that we dont have here
       
   921 		TPtrC8 ptr8( appDataBuffer->Des() );
       
   922 		aSchedule->SetApplicationDataL( *appDataBuffer );
       
   923 		CleanupStack::PopAndDestroy( appDataBuffer );
       
   924 		view.NextL();
       
   925 		}
       
   926 		
       
   927 	CleanupStack::PopAndDestroy( &view ); // Closes view
       
   928 	
       
   929 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::GetScheduleByDbIdentifierL");
       
   930 	}
       
   931 
       
   932 
       
   933 // ---------------------------------------------------------------------------
       
   934 // CCseScheduleDB::GetOverlappingSchedulesL()
       
   935 //
       
   936 // ---------------------------------------------------------------------------	
       
   937 void CCseScheduleDB::GetOverlappingSchedulesL( const TInt32 aType,
       
   938                                                const TTime& aStartTime,
       
   939                                                const TTime& aEndTime,
       
   940                                                RPointerArray<CCseScheduledProgram>& aResultArray
       
   941                                                )
       
   942 	{
       
   943 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::GetOverlappingSchedulesL");
       
   944 	LeaveIfDbLockedL();
       
   945 	ResetCompactTimer();
       
   946 	
       
   947 	TBuf<100> startTimeBuf;
       
   948 	_LIT( KDateTimeFormat,"# %1%*D/%2%*M/%3%*Y %H:%T:%S#" ); 
       
   949 	aStartTime.FormatL( startTimeBuf, KDateTimeFormat );
       
   950 	TBuf<100> endTimeBuf;
       
   951 	aEndTime.FormatL( endTimeBuf, KDateTimeFormat );  
       
   952 
       
   953     // Create SQL string to find all overlapping schedules from database between given
       
   954     // timeframe.
       
   955 	TBuf<KCseCustomSqlLength> sqlStatement;
       
   956 	sqlStatement.Append( KCseSelectFromDb );	
       
   957 	sqlStatement.Append( KCseScheduleTable );
       
   958 	sqlStatement.Append( KCseWhere );
       
   959 	sqlStatement.Append( KCseScheduleTypeCol );
       
   960 	sqlStatement.Append( KCseEqual );
       
   961 	sqlStatement.AppendNum( aType );
       
   962 	sqlStatement.Append( KCseAnd );
       
   963 	sqlStatement.Append( KCseScheduleStartTimeCol );
       
   964 	sqlStatement.Append( KCseLess );
       
   965 	sqlStatement.Append( endTimeBuf );
       
   966 	sqlStatement.Append( KCseAnd );
       
   967 	sqlStatement.Append( KCseScheduleEndTimeCol );
       
   968 	sqlStatement.Append( KCseMore );
       
   969 	sqlStatement.Append( startTimeBuf );	
       
   970 	sqlStatement.Append( KCseOrderBy );
       
   971 	sqlStatement.Append( KCseScheduleStartTimeCol );
       
   972 
       
   973     // Order table		
       
   974 	RDbView view;
       
   975 	CleanupClosePushL( view );
       
   976 	User::LeaveIfError( view.Prepare( iScheduleDb, 
       
   977 						TDbQuery( sqlStatement ) ) );
       
   978 	User::LeaveIfError( view.EvaluateAll() );
       
   979 	
       
   980 	// Move to the first row
       
   981 	view.FirstL();
       
   982 	
       
   983 	CDbColSet* colSetOrder = view.ColSetL();
       
   984 	CleanupStack::PushL( colSetOrder );
       
   985 	
       
   986 	// Get colon indexes.
       
   987 	TDbColNo keyIndex = colSetOrder->ColNo( KCseScheduleDbKeyCol );
       
   988 	TDbColNo nameIndex = colSetOrder->ColNo( KCseScheduleNameCol );
       
   989 	TDbColNo startTimeIndex = colSetOrder->ColNo( KCseScheduleStartTimeCol );
       
   990 	TDbColNo endTimeIndex = colSetOrder->ColNo( KCseScheduleEndTimeCol );
       
   991 	TDbColNo applicationUidIdIndex = colSetOrder->ColNo( KCseScheduleApplicationUIDCol );
       
   992 	TDbColNo plugInUidIndex = colSetOrder->ColNo( KCseSchedulePlugInUIDCol );
       
   993 	TDbColNo typeIndex = colSetOrder->ColNo( KCseScheduleTypeCol );
       
   994 	TDbColNo applicationSpecificIndex = colSetOrder->ColNo( KCseScheduleApplicationSpecificCol );
       
   995 	TDbColNo pluginTypeIndex = colSetOrder->ColNo( KCseSchedulePluginTypeCol );
       
   996 	
       
   997 	CleanupStack::PopAndDestroy( colSetOrder );
       
   998 	
       
   999 	// Loop through all found schedules.
       
  1000 	while ( view.AtRow() )
       
  1001 		{
       
  1002 		// Create new schedule to be added to array.
       
  1003 		CCseScheduledProgram* schedule = CCseScheduledProgram::NewL();
       
  1004 		CleanupStack::PushL( schedule );	
       
  1005 		view.GetL();
       
  1006 		
       
  1007 		// Set schedule information.
       
  1008 		schedule->SetDbIdentifier( view.ColUint32( keyIndex ) );
       
  1009 		schedule->SetName( view.ColDes8( nameIndex ) );
       
  1010 		schedule->SetStartTime( view.ColTime( startTimeIndex ) );
       
  1011 		schedule->SetEndTime( view.ColTime( endTimeIndex ) );
       
  1012 		schedule->SetAppUid( view.ColInt32( applicationUidIdIndex ) );
       
  1013 		schedule->SetPluginUid( view.ColInt32( plugInUidIndex ) );
       
  1014 		
       
  1015 		schedule->SetScheduleType( CCseScheduledProgram::TCseScheduleType( 
       
  1016 							   view.ColInt32( typeIndex ) ) );
       
  1017         schedule->SetPluginType( CCseScheduledProgram::TCsePluginType( 
       
  1018 							   view.ColInt32( pluginTypeIndex ) ) );
       
  1019 
       
  1020 		TInt streamLen( 0 );
       
  1021 		streamLen = view.ColLength( applicationSpecificIndex );
       
  1022 	
       
  1023 		HBufC8* appDataBuffer = HBufC8::NewL( streamLen );
       
  1024 		CleanupStack::PushL( appDataBuffer );
       
  1025 		
       
  1026 		TPtr8 buffPtr( appDataBuffer->Des() );
       
  1027 		RDbColReadStream readStream; // |->1
       
  1028 		readStream.OpenLC( view, applicationSpecificIndex );
       
  1029 		readStream.ReadL(buffPtr, streamLen);
       
  1030 		readStream.Release();
       
  1031 		CleanupStack::PopAndDestroy( ); //  1<-| Popping pointer to Col that we dont have here
       
  1032 		TPtrC8 ptr8( appDataBuffer->Des() );
       
  1033 		schedule->SetApplicationDataL( *appDataBuffer );
       
  1034 		CleanupStack::PopAndDestroy( appDataBuffer );
       
  1035 		if ( schedule->StartTime() < aEndTime && schedule->EndTime() > aStartTime )
       
  1036 			{
       
  1037 			aResultArray.AppendL( schedule );
       
  1038 			CleanupStack::Pop( schedule );	
       
  1039 			}
       
  1040 		else
       
  1041 			{
       
  1042 			CleanupStack::PopAndDestroy( schedule );
       
  1043 			schedule = NULL;
       
  1044 			}
       
  1045 		
       
  1046 		// Next shcedule...
       
  1047 		view.NextL();
       
  1048 		}
       
  1049 	CleanupStack::PopAndDestroy( &view ); // Closes view
       
  1050 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::GetOverlappingSchedulesL");
       
  1051 	}
       
  1052 
       
  1053 // ---------------------------------------------------------------------------
       
  1054 // CCseScheduleDB::IncreaseRunCountL()
       
  1055 //
       
  1056 // ---------------------------------------------------------------------------	
       
  1057 void CCseScheduleDB::IncreaseRunCountL( const TUint32 aDbIdentifier )
       
  1058 	{
       
  1059 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::IncreaseRunCountL");
       
  1060 	LeaveIfDbLockedL();
       
  1061 	ResetCompactTimer();
       
  1062 
       
  1063     // Create SQL string to find given schedule
       
  1064 	TBuf<KCseCustomSqlLength> sqlStatement;
       
  1065 	sqlStatement.Append( KCseSelectFromDb );	
       
  1066 	sqlStatement.Append( KCseScheduleTable );
       
  1067 	sqlStatement.Append( KCseWhere );
       
  1068 	sqlStatement.Append( KCseScheduleDbKeyCol );
       
  1069 	sqlStatement.Append( KCseEqual );
       
  1070 	sqlStatement.AppendNum( aDbIdentifier );
       
  1071 		
       
  1072 	// Order table based on DB identifier
       
  1073 	RDbView readView;
       
  1074 	CleanupClosePushL( readView );
       
  1075     
       
  1076     User::LeaveIfError( readView.Prepare( iScheduleDb, 
       
  1077 						TDbQuery( sqlStatement ) ) );
       
  1078 	User::LeaveIfError( readView.EvaluateAll() );
       
  1079     	
       
  1080 	// Get the next reminder
       
  1081 	readView.FirstL();
       
  1082 	
       
  1083 	// First we read existint run count
       
  1084 	CDbColSet* readColSet = readView.ColSetL();
       
  1085 	CleanupStack::PushL( readColSet );		
       
  1086 	TDbColNo runCountCol = readColSet->ColNo( KCseScheduleRunCountCol );	
       
  1087 	CleanupStack::PopAndDestroy( readColSet );
       
  1088 	TInt32 runCount( 0 );
       
  1089 	if ( readView.AtRow() )
       
  1090 		{				
       
  1091 		readView.GetL();
       
  1092 		runCount = readView.ColInt32( runCountCol );
       
  1093 		runCount++;		
       
  1094 		}
       
  1095 	// Close read view
       
  1096 	CleanupStack::PopAndDestroy( &readView );
       
  1097 
       
  1098 	// Update run count
       
  1099 	if ( runCount > 0 )
       
  1100 		{
       
  1101 		// Open write view
       
  1102         RDbView writeView;
       
  1103 		CleanupClosePushL( writeView );    
       
  1104         User::LeaveIfError( writeView.Prepare( iScheduleDb,
       
  1105     				        TDbQuery( sqlStatement ),
       
  1106     					    TDbWindow::EUnlimited,
       
  1107     					    RDbView::EUpdatable ));
       
  1108     	User::LeaveIfError( writeView.EvaluateAll() );
       
  1109     	
       
  1110     	writeView.FirstL();
       
  1111     	CDbColSet* writeColSet = writeView.ColSetL();
       
  1112 		CleanupStack::PushL( writeColSet );		
       
  1113 		TDbColNo runCountColNo = writeColSet->ColNo( KCseScheduleRunCountCol );	
       
  1114 		CleanupStack::PopAndDestroy( writeColSet );
       
  1115 
       
  1116     	writeView.UpdateL();
       
  1117     	
       
  1118     	// If we found (as we should) schedule again we update its value.
       
  1119     	if ( writeView.AtRow() )
       
  1120     	    {    	        
       
  1121 	        writeView.SetColL( runCountColNo, runCount );
       
  1122     	    }
       
  1123 	    
       
  1124 	    writeView.PutL();
       
  1125         
       
  1126         CleanupStack::PopAndDestroy( &writeView );
       
  1127 		}	
       
  1128 	
       
  1129 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::IncreaseRunCountL");
       
  1130 	}
       
  1131 
       
  1132 // ---------------------------------------------------------------------------
       
  1133 // CCseScheduleDB::GetRunCountL()
       
  1134 //
       
  1135 // ---------------------------------------------------------------------------	
       
  1136 TInt32 CCseScheduleDB::GetRunCountL( const TUint32 aDbIdentifier )
       
  1137 	{
       
  1138 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::GetRunCountL");
       
  1139 	LeaveIfDbLockedL();
       
  1140 	ResetCompactTimer();
       
  1141 	
       
  1142 	TInt32 runCount( 0 );
       
  1143 
       
  1144     // Create SQL string to find given schedule	
       
  1145 	TBuf<KCseCustomSqlLength> sqlStatement;
       
  1146 	sqlStatement.Append( KCseSelectFromDb );	
       
  1147 	sqlStatement.Append( KCseScheduleTable );
       
  1148 	sqlStatement.Append( KCseWhere );
       
  1149 	sqlStatement.Append( KCseScheduleDbKeyCol );
       
  1150 	sqlStatement.Append( KCseEqual );
       
  1151 	sqlStatement.AppendNum( aDbIdentifier );
       
  1152 		
       
  1153 	// Order table based on start time
       
  1154 	RDbView view;
       
  1155 	CleanupClosePushL( view );
       
  1156 	User::LeaveIfError( view.Prepare( iScheduleDb, 
       
  1157 						TDbQuery( sqlStatement ) ) );
       
  1158 	User::LeaveIfError( view.EvaluateAll() );
       
  1159 	
       
  1160 	// Get the next reminder
       
  1161 	view.FirstL();
       
  1162 	
       
  1163 	CDbColSet* colSetOrder = view.ColSetL();
       
  1164 	CleanupStack::PushL( colSetOrder );
       
  1165 		
       
  1166 	TDbColNo runCountCol = colSetOrder->ColNo( KCseScheduleRunCountCol );
       
  1167 	
       
  1168 	CleanupStack::PopAndDestroy( colSetOrder );
       
  1169 
       
  1170     // If we found schedule get its run count.
       
  1171 	if ( view.AtRow() )
       
  1172 		{				
       
  1173 		view.GetL();
       
  1174 		
       
  1175 		runCount = view.ColInt32( runCountCol );
       
  1176 		}
       
  1177 		
       
  1178 	CleanupStack::PopAndDestroy( &view ); // Closes view
       
  1179 	
       
  1180 	CSELOGSTRING2_HIGH_LEVEL("<<<CCseScheduleDB::GetRunCountL: runCount=%d", runCount);
       
  1181 	
       
  1182 	return runCount;
       
  1183 	}
       
  1184 	
       
  1185 // ---------------------------------------------------------------------------
       
  1186 // CCseScheduleDB::SetScheduleStateL()
       
  1187 //
       
  1188 // ---------------------------------------------------------------------------	
       
  1189 void CCseScheduleDB::SetScheduleStateL( const TUint32 aDbIdentifier,
       
  1190                                         const TInt32 aState )
       
  1191 	{
       
  1192 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::SetScheduleStateL");
       
  1193 	LeaveIfDbLockedL();
       
  1194 	ResetCompactTimer();
       
  1195 	
       
  1196     // Create SQL string to find given schedule	
       
  1197 	TBuf<KCseCustomSqlLength> sqlStatement;
       
  1198 	sqlStatement.Append( KCseSelectFromDb );	
       
  1199 	sqlStatement.Append( KCseScheduleTable );
       
  1200 	sqlStatement.Append( KCseWhere );
       
  1201 	sqlStatement.Append( KCseScheduleDbKeyCol );
       
  1202 	sqlStatement.Append( KCseEqual );
       
  1203 	sqlStatement.AppendNum( aDbIdentifier );
       
  1204 		
       
  1205 	// Open write view
       
  1206     RDbView writeView;
       
  1207 	CleanupClosePushL( writeView );    
       
  1208     User::LeaveIfError( writeView.Prepare( iScheduleDb,
       
  1209 				        TDbQuery( sqlStatement ),
       
  1210 					    TDbWindow::EUnlimited,
       
  1211 					    RDbView::EUpdatable ));
       
  1212 	User::LeaveIfError( writeView.EvaluateAll() );
       
  1213 	
       
  1214 	writeView.FirstL();
       
  1215 	CDbColSet* writeColSet = writeView.ColSetL();
       
  1216 	CleanupStack::PushL( writeColSet );		
       
  1217 	TDbColNo stateColNo = writeColSet->ColNo( KCseScheduleStateCol );	
       
  1218 	CleanupStack::PopAndDestroy( writeColSet );
       
  1219 
       
  1220 	writeView.UpdateL();
       
  1221 	
       
  1222 	// If we found schedule, we change the column value.
       
  1223 	if ( writeView.AtRow() )
       
  1224 	    {    	        
       
  1225         writeView.SetColL( stateColNo, aState );
       
  1226 	    }
       
  1227     
       
  1228     writeView.PutL();
       
  1229     CleanupStack::PopAndDestroy( &writeView );
       
  1230 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::SetScheduleStateL");
       
  1231 	}
       
  1232 	
       
  1233 // ---------------------------------------------------------------------------
       
  1234 // CCseScheduleDB::GetScheduleStateL()
       
  1235 //
       
  1236 // ---------------------------------------------------------------------------	
       
  1237 TInt32 CCseScheduleDB::GetScheduleStateL( const TUint32 aDbIdentifier )
       
  1238 	{
       
  1239 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::GetScheduleStateL");
       
  1240 	LeaveIfDbLockedL();
       
  1241 	ResetCompactTimer();
       
  1242 	
       
  1243 	// Create SQL string to find given schedule	
       
  1244 	TBuf<KCseCustomSqlLength> sqlStatement;
       
  1245 	sqlStatement.Append( KCseSelectFromDb );	
       
  1246 	sqlStatement.Append( KCseScheduleTable );
       
  1247 	sqlStatement.Append( KCseWhere );
       
  1248 	sqlStatement.Append( KCseScheduleDbKeyCol );
       
  1249 	sqlStatement.Append( KCseEqual );
       
  1250 	sqlStatement.AppendNum( aDbIdentifier );
       
  1251 		
       
  1252 	// Order table based on DB identifier
       
  1253 	RDbView readView;
       
  1254 	CleanupClosePushL( readView );    
       
  1255     User::LeaveIfError( readView.Prepare( iScheduleDb, 
       
  1256 						TDbQuery( sqlStatement ) ) );
       
  1257 	User::LeaveIfError( readView.EvaluateAll() );    		
       
  1258     	
       
  1259 	// Get the reminder
       
  1260 	readView.FirstL();
       
  1261 	
       
  1262 	CDbColSet* readColSet = readView.ColSetL();
       
  1263 	CleanupStack::PushL( readColSet );		
       
  1264 	TDbColNo stateCol = readColSet->ColNo( KCseScheduleStateCol );	
       
  1265 	CleanupStack::PopAndDestroy( readColSet );
       
  1266 	TInt32 state( KErrNotFound );
       
  1267 	
       
  1268     // If we found schedule, get the schedule state.
       
  1269 	if ( readView.AtRow() )
       
  1270 		{				
       
  1271 		readView.GetL();
       
  1272 		state = readView.ColInt32( stateCol );
       
  1273 		}
       
  1274 	// Close read view
       
  1275 	CleanupStack::PopAndDestroy( &readView );
       
  1276 	
       
  1277 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::GetScheduleStateL");
       
  1278 	
       
  1279 	return state;
       
  1280 	}
       
  1281 
       
  1282 // ---------------------------------------------------------------------------
       
  1283 // CCseScheduleDB::StartupSchedulesL()
       
  1284 //
       
  1285 // ---------------------------------------------------------------------------	
       
  1286 void CCseScheduleDB::StartupSchedulesL( RPointerArray<CCseScheduledProgram>& aScheduleArray )
       
  1287 	{
       
  1288 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::StartupSchedulesL");
       
  1289 	LeaveIfDbLockedL();
       
  1290 	ResetCompactTimer();
       
  1291 
       
  1292 	// Create SQL string to find wanted schedules.
       
  1293 	// At startup phase we are first interested for
       
  1294 	// those schedules that were on run when phone was
       
  1295 	// shutdown (eg runcount is bigger than 0).	
       
  1296 	TBuf<KCseCustomSqlLength> sqlStatement;
       
  1297 	sqlStatement.Append( KCseSelectFromDb );	
       
  1298 	sqlStatement.Append( KCseScheduleTable );
       
  1299 	sqlStatement.Append( KCseWhere );
       
  1300 	sqlStatement.Append( KCseScheduleRunCountCol );
       
  1301 	sqlStatement.Append( KCseMore );
       
  1302 	sqlStatement.AppendNum( 0 );
       
  1303 
       
  1304     // Order table
       
  1305 	RDbView view;
       
  1306 	CleanupClosePushL( view );
       
  1307 	User::LeaveIfError( view.Prepare( iScheduleDb, 
       
  1308 						TDbQuery( sqlStatement ) ) );
       
  1309 	User::LeaveIfError( view.EvaluateAll() );
       
  1310 	
       
  1311 	// Get the first reminder
       
  1312 	view.FirstL();
       
  1313 	
       
  1314 	CDbColSet* colSetOrder = view.ColSetL();
       
  1315 	CleanupStack::PushL( colSetOrder );
       
  1316 	
       
  1317 	// Column indexes
       
  1318 	TDbColNo keyIndex = colSetOrder->ColNo( KCseScheduleDbKeyCol );
       
  1319 	TDbColNo nameIndex = colSetOrder->ColNo( KCseScheduleNameCol );
       
  1320 	TDbColNo startTimeIndex = colSetOrder->ColNo( KCseScheduleStartTimeCol );
       
  1321 	TDbColNo endTimeIndex = colSetOrder->ColNo( KCseScheduleEndTimeCol );
       
  1322 	TDbColNo applicationUidIdIndex = colSetOrder->ColNo( KCseScheduleApplicationUIDCol );
       
  1323 	TDbColNo plugInUidIndex = colSetOrder->ColNo( KCseSchedulePlugInUIDCol );
       
  1324 	TDbColNo typeIndex = colSetOrder->ColNo( KCseScheduleTypeCol );
       
  1325 	TDbColNo applicationSpecificIndex = colSetOrder->ColNo( KCseScheduleApplicationSpecificCol );
       
  1326 	TDbColNo pluginTypeIndex = colSetOrder->ColNo( KCseSchedulePluginTypeCol );
       
  1327 	
       
  1328 	CleanupStack::PopAndDestroy( colSetOrder );
       
  1329 
       
  1330     // Loop through the found schedules.		
       
  1331 	while ( view.AtRow() )
       
  1332 		{
       
  1333 		// Create CseScheduledProgram to be added to string.
       
  1334 		CCseScheduledProgram* prog = CCseScheduledProgram::NewL();
       
  1335 		CleanupStack::PushL( prog );			
       
  1336 		view.GetL();
       
  1337 		
       
  1338 		// Set schedule information.
       
  1339 		prog->SetDbIdentifier( view.ColUint32( keyIndex ) );
       
  1340 		prog->SetName( view.ColDes8( nameIndex ) );
       
  1341 		prog->SetStartTime( view.ColTime( startTimeIndex ) );
       
  1342 		prog->SetEndTime( view.ColTime( endTimeIndex ) );
       
  1343 		prog->SetAppUid( view.ColInt32( applicationUidIdIndex ) );
       
  1344 		prog->SetPluginUid( view.ColInt32( plugInUidIndex ) );
       
  1345 		
       
  1346 		prog->SetScheduleType( CCseScheduledProgram::TCseScheduleType( 
       
  1347 							   view.ColInt32( typeIndex ) ) );
       
  1348         prog->SetPluginType( CCseScheduledProgram::TCsePluginType( 
       
  1349 							   view.ColInt32( pluginTypeIndex ) ) );
       
  1350 							   
       
  1351 		TInt streamLen( view.ColLength( applicationSpecificIndex ) );
       
  1352 	
       
  1353 		HBufC8* appDataBuffer = HBufC8::NewL( streamLen );
       
  1354 		CleanupStack::PushL( appDataBuffer );
       
  1355 		
       
  1356 		TPtr8 buffPtr( appDataBuffer->Des() );
       
  1357 		RDbColReadStream readStream;
       
  1358 		readStream.OpenLC( view, applicationSpecificIndex ); // |->1
       
  1359 		readStream.ReadL( buffPtr, streamLen );
       
  1360 		readStream.Release();
       
  1361 		CleanupStack::PopAndDestroy( ); // 1<-| Popping pointer to Col that we dont have here
       
  1362 
       
  1363 		prog->SetApplicationDataL( *appDataBuffer );
       
  1364 		
       
  1365 		CleanupStack::PopAndDestroy( appDataBuffer );
       
  1366 		User::LeaveIfError( aScheduleArray.Append( prog ) );
       
  1367 		CleanupStack::Pop( prog );
       
  1368 		
       
  1369 		// Next shcedule
       
  1370 		view.NextL();
       
  1371 		}
       
  1372 		
       
  1373 	CleanupStack::PopAndDestroy( &view ); // Closes view
       
  1374 	
       
  1375 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::StartupSchedulesL");
       
  1376 	}
       
  1377 
       
  1378 // ---------------------------------------------------------------------------
       
  1379 // CCseScheduleDB::ResetCompactTimer()
       
  1380 //
       
  1381 // ---------------------------------------------------------------------------	
       
  1382 void CCseScheduleDB::ResetCompactTimer()
       
  1383     {
       
  1384     CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::ResetCompactTimer");
       
  1385     
       
  1386     // If iCompatTimer is not running start it.
       
  1387     if( !iCompactTimer )
       
  1388         {
       
  1389         TRAPD(err, iCompactTimer = CPeriodic::NewL( CActive::EPriorityStandard ));
       
  1390         
       
  1391         if(err != KErrNone )
       
  1392             {
       
  1393             CSELOGSTRING2_HIGH_LEVEL("CCseScheduleDB::ResetCompactTimer - Can't create iCompactTimer: %d", err);
       
  1394             }
       
  1395         }
       
  1396         
       
  1397     // If iCompactTimer is running cancel it and start over again.
       
  1398     if( iCompactTimer )
       
  1399         {            
       
  1400         iCompactTimer->Cancel();
       
  1401         
       
  1402         iCompactTimer->Start( KCseDbCompactTimeout,
       
  1403                               KCseDbCompactTimeout, 
       
  1404                               TCallBack( CompactCallback, this ) );
       
  1405         }
       
  1406         
       
  1407     CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::ResetCompactTimer");
       
  1408     }
       
  1409 
       
  1410 // ---------------------------------------------------------------------------
       
  1411 // CCseScheduleDB::CompactCallback()
       
  1412 //
       
  1413 // ---------------------------------------------------------------------------
       
  1414 TInt CCseScheduleDB::CompactCallback( TAny* aThis )
       
  1415     {
       
  1416     CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::CompactCallback");
       
  1417     
       
  1418     // Direct call to class method.
       
  1419     static_cast<CCseScheduleDB*>( aThis )->CompactDb();
       
  1420     
       
  1421     CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::CompactCallback");
       
  1422     return EFalse;    
       
  1423     }
       
  1424 
       
  1425 // ---------------------------------------------------------------------------
       
  1426 // CCseScheduleDB::CompactDb()
       
  1427 //
       
  1428 // ---------------------------------------------------------------------------    
       
  1429 void CCseScheduleDB::CompactDb()
       
  1430     {
       
  1431     CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::CompactDb");
       
  1432     // Compact database
       
  1433 
       
  1434     if( iDbAccess != ECseDbLocked )
       
  1435         {
       
  1436         TInt err( iScheduleDb.Compact() );
       
  1437         if ( err != KErrNone )
       
  1438             {
       
  1439             CSELOGSTRING2_HIGH_LEVEL("iScheduleDb.Compact() failed: %d", err);    
       
  1440             }
       
  1441         }
       
  1442     
       
  1443     // Cancel timer if it is running    
       
  1444 	if ( iCompactTimer )
       
  1445 		{
       
  1446 		iCompactTimer->Cancel();
       
  1447     	delete iCompactTimer;
       
  1448     	iCompactTimer = NULL;    	
       
  1449 		}
       
  1450 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::CompactDb");
       
  1451     }
       
  1452 
       
  1453 // ---------------------------------------------------------------------------
       
  1454 // CCseScheduleDB::ChangeFileLockL()
       
  1455 //
       
  1456 // ---------------------------------------------------------------------------    
       
  1457 //
       
  1458 void CCseScheduleDB::ChangeFileLockL(
       
  1459     const TDesC& /*aFileName*/,
       
  1460     TFileLockFlags aFlags )
       
  1461     {
       
  1462     CSELOGSTRING_HIGH_LEVEL( ">>>CCseScheduleDB::ChangeFileLockL" );
       
  1463     
       
  1464     switch ( aFlags )
       
  1465         {
       
  1466         case MBackupObserver::EReleaseLockReadOnly:
       
  1467         case MBackupObserver::EReleaseLockNoAccess:
       
  1468             {
       
  1469             // Backup and restore starting, close the db
       
  1470             CloseDbFile();
       
  1471             
       
  1472             // Cancel timer if it is running    
       
  1473             if ( iCompactTimer )
       
  1474                 {
       
  1475                 iCompactTimer->Cancel();
       
  1476                 delete iCompactTimer;
       
  1477                 iCompactTimer = NULL;       
       
  1478                 }
       
  1479             }
       
  1480             break;
       
  1481             
       
  1482         default:
       
  1483             {
       
  1484             OpenDbFileL();
       
  1485             }
       
  1486             break;                
       
  1487         }
       
  1488 
       
  1489     CSELOGSTRING_HIGH_LEVEL( "<<<CCseScheduleDB::ChangeFileLockL" );
       
  1490     }
       
  1491 
       
  1492 // ---------------------------------------------------------------------------
       
  1493 // CCseScheduleDB::CloseDbFile()
       
  1494 //
       
  1495 // ---------------------------------------------------------------------------    
       
  1496 void CCseScheduleDB::CloseDbFile()
       
  1497     {
       
  1498     CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::CloseDbFile");
       
  1499     
       
  1500     iDbAccess = ECseDbLocked;
       
  1501     iScheduleDb.Close();
       
  1502     delete iScheduleFileStore;
       
  1503     iScheduleFileStore = NULL;    
       
  1504         
       
  1505     CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::CloseDbFile");
       
  1506     }
       
  1507 	
       
  1508 // ---------------------------------------------------------------------------
       
  1509 // CCseScheduleDB::OpenDbFileL()
       
  1510 //
       
  1511 // ---------------------------------------------------------------------------    
       
  1512 void CCseScheduleDB::OpenDbFileL()
       
  1513     {
       
  1514     CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::OpenDbFileL");
       
  1515     
       
  1516     iScheduleDb.Close();
       
  1517     if( iScheduleFileStore )
       
  1518         {
       
  1519         delete iScheduleFileStore;
       
  1520         iScheduleFileStore = NULL;
       
  1521         }
       
  1522 	iScheduleFileStore = CPermanentFileStore::OpenL( iFsSession, iDbFile,
       
  1523 												     EFileRead|EFileWrite );
       
  1524 	iScheduleFileStore->SetTypeL( iScheduleFileStore->Layout() );	
       
  1525     iScheduleDb.OpenL( iScheduleFileStore, iScheduleFileStore->Root() );
       
  1526 	User::LeaveIfError( iScheduleDb.Compact() );    
       
  1527 	
       
  1528 	iDbAccess = ECseDbOpen;
       
  1529 	
       
  1530 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::OpenDbFileL");
       
  1531     }
       
  1532 
       
  1533 // ---------------------------------------------------------------------------
       
  1534 // CCseScheduleDB::LeaveIfDbLocked()
       
  1535 //
       
  1536 // ---------------------------------------------------------------------------    
       
  1537 void CCseScheduleDB::LeaveIfDbLockedL() const
       
  1538     {
       
  1539     CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::LeaveIfDbLocked");
       
  1540     
       
  1541     if(iDbAccess == ECseDbLocked)
       
  1542         {
       
  1543         CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::LeaveIfDbLocked - Leave, DB locked!!");
       
  1544         User::Leave( KErrLocked );
       
  1545         }
       
  1546         
       
  1547     CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::LeaveIfDbLocked");
       
  1548     }
       
  1549 
       
  1550 // ---------------------------------------------------------------------------
       
  1551 // CCseScheduleDB::GetSchedulesByPluginL()
       
  1552 //
       
  1553 // ---------------------------------------------------------------------------	
       
  1554 void CCseScheduleDB::GetSchedulesByPluginL( const TInt32 aPluginUid,
       
  1555 								            RPointerArray<CCseScheduledProgram>& aArray )
       
  1556 	{
       
  1557 	
       
  1558 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::GetSchedulesByPluginL");
       
  1559 	LeaveIfDbLockedL();
       
  1560 	ResetCompactTimer();
       
  1561 	
       
  1562 	// Gets program occurence(s) that are going to happen next
       
  1563 	// from database
       
  1564 	TBuf<KCseCustomSqlLength> sqlStatement;
       
  1565 	sqlStatement.Append( KCseSelectFromDb );	
       
  1566 	sqlStatement.Append( KCseScheduleTable );
       
  1567 	sqlStatement.Append( KCseWhere );
       
  1568 	sqlStatement.Append( KCseSchedulePlugInUIDCol );
       
  1569 	sqlStatement.Append( KCseEqual );
       
  1570 	sqlStatement.AppendNum( aPluginUid );
       
  1571 	sqlStatement.Append( KCseOrderBy );
       
  1572 	sqlStatement.Append( KCseScheduleStartTimeCol );
       
  1573 
       
  1574 	// Order table based on start time
       
  1575 	RDbView view;
       
  1576 	CleanupClosePushL( view );
       
  1577 	User::LeaveIfError( view.Prepare( iScheduleDb, 
       
  1578 						TDbQuery( sqlStatement ) ) );
       
  1579 	User::LeaveIfError( view.EvaluateAll() );
       
  1580 	
       
  1581 	view.FirstL();
       
  1582 	
       
  1583 	CDbColSet* colSetOrder = view.ColSetL();
       
  1584 	CleanupStack::PushL( colSetOrder );
       
  1585 	
       
  1586 	// Get the SQL table indexes.
       
  1587 	TDbColNo keyIndex = colSetOrder->ColNo( KCseScheduleDbKeyCol );
       
  1588 	TDbColNo nameIndex = colSetOrder->ColNo( KCseScheduleNameCol );
       
  1589 	TDbColNo startTimeIndex = colSetOrder->ColNo( KCseScheduleStartTimeCol );
       
  1590 	TDbColNo endTimeIndex = colSetOrder->ColNo( KCseScheduleEndTimeCol );
       
  1591 	TDbColNo applicationUidIdIndex = colSetOrder->ColNo( KCseScheduleApplicationUIDCol );
       
  1592 	TDbColNo plugInUidIndex = colSetOrder->ColNo( KCseSchedulePlugInUIDCol );
       
  1593 	TDbColNo applicationSpecificIndex = colSetOrder->ColNo( KCseScheduleApplicationSpecificCol );
       
  1594 	TDbColNo scheduleTypeIndex = colSetOrder->ColNo( KCseScheduleTypeCol );
       
  1595 	TDbColNo pluginTypeIndex = colSetOrder->ColNo( KCseSchedulePluginTypeCol );
       
  1596 	
       
  1597 	CleanupStack::PopAndDestroy( colSetOrder );
       
  1598 	
       
  1599 	// Loop through the tables
       
  1600 	while ( view.AtRow() )
       
  1601 		{
       
  1602 		// Create new CCseSceduledProgram to be added to array
       
  1603 		CCseScheduledProgram* prog = CCseScheduledProgram::NewL();
       
  1604 		CleanupStack::PushL( prog );			
       
  1605 		
       
  1606 		// Fill created schedule with DB information
       
  1607 		view.GetL();
       
  1608 		prog->SetDbIdentifier( view.ColUint32( keyIndex ) );
       
  1609 		prog->SetName( view.ColDes8( nameIndex ) );
       
  1610 		prog->SetStartTime( view.ColTime( startTimeIndex ) );
       
  1611 		prog->SetEndTime( view.ColTime( endTimeIndex ) );
       
  1612 		prog->SetAppUid( view.ColInt32( applicationUidIdIndex ) );
       
  1613 		prog->SetPluginUid( view.ColInt32( plugInUidIndex ) );
       
  1614 		prog->SetScheduleType( view.ColInt32( scheduleTypeIndex ) );
       
  1615 		prog->SetPluginType( view.ColInt32( pluginTypeIndex ) );
       
  1616 		
       
  1617 		TInt streamLen( 0 );
       
  1618 		streamLen = view.ColLength( applicationSpecificIndex );
       
  1619 	
       
  1620 		HBufC8* appDataBuffer = HBufC8::NewL( streamLen );
       
  1621 		
       
  1622 		CleanupStack::PushL( appDataBuffer );
       
  1623 		
       
  1624 		TPtr8 buffPtr( appDataBuffer->Des() );
       
  1625 		RDbColReadStream readStream;
       
  1626 		readStream.OpenLC( view, applicationSpecificIndex ); // |->1
       
  1627 		readStream.ReadL( buffPtr, streamLen );
       
  1628 		readStream.Release();
       
  1629 		CleanupStack::PopAndDestroy( ); // 1<-| Popping pointer to Col that we dont have here
       
  1630 
       
  1631 		prog->SetApplicationDataL( *appDataBuffer );
       
  1632 		
       
  1633 		CleanupStack::PopAndDestroy( appDataBuffer );
       
  1634 		User::LeaveIfError( aArray.Append( prog ) );
       
  1635 		CleanupStack::Pop( prog );
       
  1636 		view.NextL();
       
  1637 		}
       
  1638 		
       
  1639 	CleanupStack::PopAndDestroy( &view ); // Closes view
       
  1640 	
       
  1641 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::GetSchedulesByPluginL");	
       
  1642 	}
       
  1643 	
       
  1644 // ---------------------------------------------------------------------------
       
  1645 // CCseScheduleDB::GetSchedulesByTypeL()
       
  1646 //
       
  1647 // ---------------------------------------------------------------------------	
       
  1648 void CCseScheduleDB::GetSchedulesByTypeL( const TInt32 aType,
       
  1649 								          RPointerArray<CCseScheduledProgram>& aArray )
       
  1650 	{	
       
  1651 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::GetSchedulesByPluginL");
       
  1652 	LeaveIfDbLockedL();
       
  1653 	ResetCompactTimer();
       
  1654 	
       
  1655 	// Gets program occurence(s) that are going to happen next
       
  1656 	// from database
       
  1657 	TBuf<KCseCustomSqlLength> sqlStatement;
       
  1658 	sqlStatement.Append( KCseSelectFromDb );	
       
  1659 	sqlStatement.Append( KCseScheduleTable );
       
  1660 	sqlStatement.Append( KCseWhere );
       
  1661 	sqlStatement.Append( KCseScheduleTypeCol );
       
  1662 	sqlStatement.Append( KCseEqual );
       
  1663 	sqlStatement.AppendNum( aType );
       
  1664 	sqlStatement.Append( KCseOrderBy );
       
  1665 	sqlStatement.Append( KCseScheduleStartTimeCol );
       
  1666 
       
  1667 	// Order table based on start time
       
  1668 	RDbView view;
       
  1669 	CleanupClosePushL( view );
       
  1670 	User::LeaveIfError( view.Prepare( iScheduleDb, 
       
  1671 						TDbQuery( sqlStatement ) ) );
       
  1672 	User::LeaveIfError( view.EvaluateAll() );
       
  1673 	
       
  1674 	view.FirstL();
       
  1675 	
       
  1676 	CDbColSet* colSetOrder = view.ColSetL();
       
  1677 	CleanupStack::PushL( colSetOrder );
       
  1678 	
       
  1679 	// Get the SQL table indexes.
       
  1680 	TDbColNo keyIndex = colSetOrder->ColNo( KCseScheduleDbKeyCol );
       
  1681 	TDbColNo nameIndex = colSetOrder->ColNo( KCseScheduleNameCol );
       
  1682 	TDbColNo startTimeIndex = colSetOrder->ColNo( KCseScheduleStartTimeCol );
       
  1683 	TDbColNo endTimeIndex = colSetOrder->ColNo( KCseScheduleEndTimeCol );
       
  1684 	TDbColNo applicationUidIdIndex = colSetOrder->ColNo( KCseScheduleApplicationUIDCol );
       
  1685 	TDbColNo plugInUidIndex = colSetOrder->ColNo( KCseSchedulePlugInUIDCol );
       
  1686 	TDbColNo applicationSpecificIndex = colSetOrder->ColNo( KCseScheduleApplicationSpecificCol );
       
  1687 	TDbColNo scheduleTypeIndex = colSetOrder->ColNo( KCseScheduleTypeCol );
       
  1688 	TDbColNo pluginTypeIndex = colSetOrder->ColNo( KCseSchedulePluginTypeCol );
       
  1689 	
       
  1690 	CleanupStack::PopAndDestroy( colSetOrder );
       
  1691 	
       
  1692 	// Loop through the tables
       
  1693 	while ( view.AtRow() )
       
  1694 		{
       
  1695 		// Create new CCseSceduledProgram to be added to array
       
  1696 		CCseScheduledProgram* prog = CCseScheduledProgram::NewL();
       
  1697 		CleanupStack::PushL( prog );			
       
  1698 		
       
  1699 		// Fill created schedule with DB information
       
  1700 		view.GetL();
       
  1701 		prog->SetDbIdentifier( view.ColUint32( keyIndex ) );
       
  1702 		prog->SetName( view.ColDes8( nameIndex ) );
       
  1703 		prog->SetStartTime( view.ColTime( startTimeIndex ) );
       
  1704 		prog->SetEndTime( view.ColTime( endTimeIndex ) );
       
  1705 		prog->SetAppUid( view.ColInt32( applicationUidIdIndex ) );
       
  1706 		prog->SetPluginUid( view.ColInt32( plugInUidIndex ) );
       
  1707 		prog->SetScheduleType( view.ColInt32( scheduleTypeIndex ) );
       
  1708 		prog->SetPluginType( view.ColInt32( pluginTypeIndex ) );
       
  1709 		
       
  1710 		TInt streamLen( 0 );
       
  1711 		streamLen = view.ColLength( applicationSpecificIndex );
       
  1712 	
       
  1713 		HBufC8* appDataBuffer = HBufC8::NewL( streamLen );
       
  1714 		
       
  1715 		CleanupStack::PushL( appDataBuffer );
       
  1716 		
       
  1717 		TPtr8 buffPtr( appDataBuffer->Des() );
       
  1718 		RDbColReadStream readStream;
       
  1719 		readStream.OpenLC( view, applicationSpecificIndex ); // |-> 1
       
  1720 		readStream.ReadL( buffPtr, streamLen );
       
  1721 		readStream.Release();
       
  1722 		CleanupStack::PopAndDestroy( ); // 1 <-| Popping pointer to Col that we dont have here
       
  1723 
       
  1724 		prog->SetApplicationDataL( *appDataBuffer );
       
  1725 		
       
  1726 		CleanupStack::PopAndDestroy( appDataBuffer );
       
  1727 		User::LeaveIfError( aArray.Append( prog ) );
       
  1728 		CleanupStack::Pop( prog );
       
  1729 		view.NextL();
       
  1730 		}
       
  1731 		
       
  1732 	CleanupStack::PopAndDestroy( &view ); // Closes view
       
  1733 	
       
  1734 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::GetSchedulesByPluginL");	
       
  1735 	}
       
  1736 	
       
  1737 // ---------------------------------------------------------------------------
       
  1738 // CCseScheduleDB::GetSchedulesByTimeframeL()
       
  1739 //
       
  1740 // ---------------------------------------------------------------------------	
       
  1741 void CCseScheduleDB::GetSchedulesByTimeframeL( const TTime& aBeginning,
       
  1742                                                const TTime& aEnd,
       
  1743                                                RPointerArray<CCseScheduledProgram>& aResultArray
       
  1744                                                )
       
  1745 	{
       
  1746 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::GetSchedulesByTimeframeL");
       
  1747 	LeaveIfDbLockedL();
       
  1748 	ResetCompactTimer();
       
  1749 	
       
  1750 	TBuf<100> startTimeBuf;
       
  1751 	_LIT( KDateTimeFormat,"# %1%*D/%2%*M/%3%*Y %H:%T:%S#" ); 
       
  1752 	aBeginning.FormatL( startTimeBuf, KDateTimeFormat );
       
  1753 	TBuf<100> endTimeBuf;
       
  1754 	aEnd.FormatL( endTimeBuf, KDateTimeFormat );  
       
  1755 
       
  1756     // Create SQL string to find all overlapping schedules from database between given
       
  1757     // timeframe.
       
  1758 	TBuf<KCseCustomSqlLength> sqlStatement;
       
  1759 	sqlStatement.Append( KCseSelectFromDb );	
       
  1760 	sqlStatement.Append( KCseScheduleTable );
       
  1761 	sqlStatement.Append( KCseWhere );
       
  1762 	sqlStatement.Append( KCseScheduleStartTimeCol );
       
  1763 	sqlStatement.Append( KCseLess );
       
  1764 	sqlStatement.Append( endTimeBuf );
       
  1765 	sqlStatement.Append( KCseAnd );
       
  1766 	sqlStatement.Append( KCseScheduleEndTimeCol );
       
  1767 	sqlStatement.Append( KCseMore );
       
  1768 	sqlStatement.Append( startTimeBuf );	
       
  1769 	sqlStatement.Append( KCseOrderBy );
       
  1770 	sqlStatement.Append( KCseScheduleStartTimeCol );
       
  1771 
       
  1772     // Order table		
       
  1773 	RDbView view;
       
  1774 	CleanupClosePushL( view );
       
  1775 	User::LeaveIfError( view.Prepare( iScheduleDb, 
       
  1776 						TDbQuery( sqlStatement ) ) );
       
  1777 	User::LeaveIfError( view.EvaluateAll() );
       
  1778 	
       
  1779 	// Move to the first row
       
  1780 	view.FirstL();
       
  1781 	
       
  1782 	CDbColSet* colSetOrder = view.ColSetL();
       
  1783 	CleanupStack::PushL( colSetOrder );
       
  1784 	
       
  1785 	// Get colon indexes.
       
  1786 	TDbColNo keyIndex = colSetOrder->ColNo( KCseScheduleDbKeyCol );
       
  1787 	TDbColNo nameIndex = colSetOrder->ColNo( KCseScheduleNameCol );
       
  1788 	TDbColNo startTimeIndex = colSetOrder->ColNo( KCseScheduleStartTimeCol );
       
  1789 	TDbColNo endTimeIndex = colSetOrder->ColNo( KCseScheduleEndTimeCol );
       
  1790 	TDbColNo applicationUidIdIndex = colSetOrder->ColNo( KCseScheduleApplicationUIDCol );
       
  1791 	TDbColNo plugInUidIndex = colSetOrder->ColNo( KCseSchedulePlugInUIDCol );
       
  1792 	TDbColNo typeIndex = colSetOrder->ColNo( KCseScheduleTypeCol );
       
  1793 	TDbColNo applicationSpecificIndex = colSetOrder->ColNo( KCseScheduleApplicationSpecificCol );
       
  1794 	TDbColNo pluginTypeIndex = colSetOrder->ColNo( KCseSchedulePluginTypeCol );
       
  1795 	
       
  1796 	CleanupStack::PopAndDestroy( colSetOrder );
       
  1797 	
       
  1798 	// Loop through all found schedules.
       
  1799 	while ( view.AtRow() )
       
  1800 		{
       
  1801 		// Create new schedule to be added to array.
       
  1802 		CCseScheduledProgram* schedule = CCseScheduledProgram::NewL();
       
  1803 		CleanupStack::PushL( schedule );	
       
  1804 		view.GetL();
       
  1805 		
       
  1806 		// Set schedule information.
       
  1807 		schedule->SetDbIdentifier( view.ColUint32( keyIndex ) );
       
  1808 		schedule->SetName( view.ColDes8( nameIndex ) );
       
  1809 		schedule->SetStartTime( view.ColTime( startTimeIndex ) );
       
  1810 		schedule->SetEndTime( view.ColTime( endTimeIndex ) );
       
  1811 		schedule->SetAppUid( view.ColInt32( applicationUidIdIndex ) );
       
  1812 		schedule->SetPluginUid( view.ColInt32( plugInUidIndex ) );
       
  1813 		
       
  1814 		schedule->SetScheduleType( CCseScheduledProgram::TCseScheduleType( 
       
  1815 							   view.ColInt32( typeIndex ) ) );
       
  1816         schedule->SetPluginType( CCseScheduledProgram::TCseScheduleType( 
       
  1817 							   view.ColInt32( pluginTypeIndex ) ) );
       
  1818 
       
  1819 		TInt streamLen( 0 );
       
  1820 		streamLen = view.ColLength( applicationSpecificIndex );
       
  1821 	
       
  1822 		HBufC8* appDataBuffer = HBufC8::NewL( streamLen );
       
  1823 		CleanupStack::PushL( appDataBuffer );
       
  1824 		
       
  1825 		TPtr8 buffPtr( appDataBuffer->Des() );
       
  1826 		RDbColReadStream readStream;
       
  1827 		readStream.OpenLC( view, applicationSpecificIndex ); // |-> 1
       
  1828 		readStream.ReadL(buffPtr, streamLen);
       
  1829 		readStream.Release();
       
  1830 		CleanupStack::PopAndDestroy( ); // 1 <-| Popping pointer to Col that we dont have here
       
  1831 		TPtrC8 ptr8( appDataBuffer->Des() );
       
  1832 		schedule->SetApplicationDataL( *appDataBuffer );
       
  1833 		CleanupStack::PopAndDestroy( appDataBuffer );
       
  1834 		
       
  1835 		aResultArray.AppendL( schedule );
       
  1836 		CleanupStack::Pop( schedule );	
       
  1837 	
       
  1838 		// Next shcedule...
       
  1839 		view.NextL();
       
  1840 		}
       
  1841 	CleanupStack::PopAndDestroy( &view ); // Closes view
       
  1842 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::GetSchedulesByTimeframeL");
       
  1843 	}
       
  1844 	
       
  1845 // ---------------------------------------------------------------------------
       
  1846 // CCseScheduleDB::GetPluginsL()
       
  1847 //
       
  1848 // ---------------------------------------------------------------------------	
       
  1849 void CCseScheduleDB::GetPluginsL( RArray<TInt32>& aPlugins )
       
  1850 	{
       
  1851 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::GetPluginsL");
       
  1852 	LeaveIfDbLockedL();
       
  1853 	ResetCompactTimer();	
       
  1854 	
       
  1855 	// Gets program occurence(s) that are going to happen next
       
  1856 	// from database
       
  1857 	TBuf<KCseCustomSqlLength> sqlStatement;
       
  1858 	sqlStatement.Append( KCseSelectFromDb );	
       
  1859 	sqlStatement.Append( KCseScheduleTable );
       
  1860 	sqlStatement.Append( KCseOrderBy );
       
  1861 	sqlStatement.Append( KCseSchedulePlugInUIDCol );
       
  1862 	
       
  1863 	// Order table based on start time
       
  1864 	RDbView view;
       
  1865 	CleanupClosePushL( view );
       
  1866 	User::LeaveIfError( view.Prepare( iScheduleDb, 
       
  1867 						TDbQuery( sqlStatement ) ) );
       
  1868 	User::LeaveIfError( view.EvaluateAll() );
       
  1869 	
       
  1870 	view.FirstL();
       
  1871 	
       
  1872 	CDbColSet* colSetOrder = view.ColSetL();
       
  1873 	CleanupStack::PushL( colSetOrder );
       
  1874 	
       
  1875 	// Get the SQL table indexes.	
       
  1876 	TDbColNo pluginUidIndex = colSetOrder->ColNo( KCseSchedulePlugInUIDCol );
       
  1877 	
       
  1878 	CleanupStack::PopAndDestroy( colSetOrder );
       
  1879 	
       
  1880 	TInt32 currentPluginUid( 0 );
       
  1881 	TInt32 newPluginUid( 0 );
       
  1882 	
       
  1883 	// Loop through the tables
       
  1884 	while ( view.AtRow() )
       
  1885 	    {	    
       
  1886 	    view.GetL();
       
  1887 	    
       
  1888 	    // Get plugin Uid from row	        
       
  1889 		newPluginUid = view.ColInt32( pluginUidIndex );
       
  1890 		
       
  1891 		// If it is different that the current plugin uid add it to
       
  1892 		// array and set new "current" uid.
       
  1893 		if( newPluginUid != currentPluginUid )
       
  1894 		    {
       
  1895 		    // Query results are sorted by plugin uid
       
  1896 		    // so if uid changes we can add it to array
       
  1897 		    // without fearing that we will have
       
  1898 		    // duplicates.		    
       
  1899 		    aPlugins.Append( newPluginUid );
       
  1900 		    currentPluginUid = newPluginUid;
       
  1901 		    }		
       
  1902 		
       
  1903 		view.NextL();
       
  1904 		}
       
  1905 		
       
  1906     view.Close();		
       
  1907 	CleanupStack::PopAndDestroy( &view ); // Closes view	
       
  1908 	
       
  1909 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::GetPluginsL");
       
  1910 	}
       
  1911 
       
  1912 // ---------------------------------------------------------------------------
       
  1913 // CCseScheduleDB::GetNextScheduleTimeByPluginL()
       
  1914 //
       
  1915 // ---------------------------------------------------------------------------	
       
  1916 TTime CCseScheduleDB::GetNextScheduleTimeByPluginL( TInt32 aPluginUid )
       
  1917 	{
       
  1918 	CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::GetNextScheduleTimeByPluginL");
       
  1919 	LeaveIfDbLockedL();
       
  1920 	ResetCompactTimer();
       
  1921 	
       
  1922 	// Create SQL satring to get schedule occurence(s) that are
       
  1923 	// going to happen next from database.
       
  1924 	TTime nextScheduleTime( 0 );
       
  1925 	TBuf<KCseCustomSqlLength> sqlStatement;
       
  1926 	sqlStatement.Append( KCseSelectFromDb );	
       
  1927 	sqlStatement.Append( KCseScheduleTable );
       
  1928 	sqlStatement.Append( KCseWhere );
       
  1929 	sqlStatement.Append( KCseScheduleStateCol );
       
  1930 	sqlStatement.Append( KCseEqual );
       
  1931 	sqlStatement.AppendNum( ECseWaiting );
       
  1932 	sqlStatement.Append( KCseAnd );
       
  1933 	sqlStatement.Append( KCseSchedulePlugInUIDCol );
       
  1934 	sqlStatement.Append( KCseEqual );
       
  1935 	sqlStatement.AppendNum( aPluginUid );
       
  1936 	sqlStatement.Append( KCseOrderBy );
       
  1937 	sqlStatement.Append( KCseScheduleStartTimeCol );
       
  1938 		
       
  1939 	// Order table based on start time
       
  1940 	RDbView view;
       
  1941 	CleanupClosePushL( view );
       
  1942 	User::LeaveIfError( view.Prepare( iScheduleDb, 
       
  1943 						TDbQuery( sqlStatement ) ) );
       
  1944 	User::LeaveIfError( view.EvaluateAll() );
       
  1945 	
       
  1946 	// Get the next schedule
       
  1947 	view.FirstL();
       
  1948 	
       
  1949 	CDbColSet* colSetOrder = view.ColSetL();
       
  1950 	CleanupStack::PushL( colSetOrder );
       
  1951 	
       
  1952 	TDbColNo startTimeIndex = colSetOrder->ColNo( KCseScheduleStartTimeCol );
       
  1953 	
       
  1954 	CleanupStack::PopAndDestroy( colSetOrder );
       
  1955 	
       
  1956 	// Schedule found get its start time.
       
  1957 	if ( view.AtRow() )	
       
  1958 		{		
       
  1959 		view.GetL();				
       
  1960 		nextScheduleTime = view.ColTime( startTimeIndex );
       
  1961 		}
       
  1962 		
       
  1963 	CleanupStack::PopAndDestroy( &view ); // Closes view
       
  1964 	
       
  1965 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::GetNextScheduleTimeByPluginL");
       
  1966 	
       
  1967 	return nextScheduleTime;
       
  1968 	}
       
  1969 
       
  1970 // ---------------------------------------------------------------------------
       
  1971 // CCseScheduleDB::GetSchedulesByTimeAndPluginL()
       
  1972 //
       
  1973 // ---------------------------------------------------------------------------	
       
  1974 void CCseScheduleDB::GetSchedulesByTimeAndPluginL(
       
  1975                                      const TTime& aScheduleTime, 
       
  1976                                      TInt32 aPluginUid,
       
  1977 					                 RPointerArray<CCseScheduledProgram>& aNextScheduleArray )
       
  1978 	{
       
  1979     CSELOGSTRING_HIGH_LEVEL(">>>CCseScheduleDB::GetSchedulesByTimeAndPluginL");
       
  1980 	LeaveIfDbLockedL();
       
  1981 	ResetCompactTimer();
       
  1982 	
       
  1983 	// Gets program occurence(s) that are going to happen next
       
  1984 	// from database
       
  1985 	TBuf<100> startTimeBuf;
       
  1986 	
       
  1987 	_LIT( KDateTimeFormat,"# %1%*D/%2%*M/%3%*Y %H:%T:%S.%C#" ); 
       
  1988 
       
  1989 	aScheduleTime.FormatL( startTimeBuf, KDateTimeFormat );
       
  1990 		
       
  1991 	// Create SQL string to get all schedules to be happen at given time
       
  1992 	TBuf<KCseCustomSqlLength> sqlStatement;
       
  1993 	sqlStatement.Append( KCseSelectFromDb );	
       
  1994 	sqlStatement.Append( KCseScheduleTable );
       
  1995 	sqlStatement.Append( KCseWhere );
       
  1996 	sqlStatement.Append( KCseScheduleStartTimeCol );
       
  1997 	sqlStatement.Append( KCseEqual );
       
  1998 	sqlStatement.Append( startTimeBuf );
       
  1999 	sqlStatement.Append( KCseAnd );
       
  2000 	sqlStatement.Append( KCseSchedulePlugInUIDCol );
       
  2001 	sqlStatement.Append( KCseEqual );
       
  2002 	sqlStatement.AppendNum( aPluginUid );
       
  2003 		
       
  2004 	// Order table based on start time
       
  2005 	RDbView view;
       
  2006 	CleanupClosePushL( view );
       
  2007 	User::LeaveIfError( view.Prepare( iScheduleDb, 
       
  2008 						TDbQuery( sqlStatement ) ) );
       
  2009 	User::LeaveIfError( view.EvaluateAll() );
       
  2010 	
       
  2011 	// Get the next reminder
       
  2012 	view.FirstL();
       
  2013 	
       
  2014 	CDbColSet* colSetOrder = view.ColSetL();
       
  2015 	CleanupStack::PushL( colSetOrder );
       
  2016 	
       
  2017 	// Get column indexes
       
  2018 	TDbColNo keyIndex = colSetOrder->ColNo( KCseScheduleDbKeyCol );
       
  2019 	TDbColNo nameIndex = colSetOrder->ColNo( KCseScheduleNameCol );
       
  2020 	TDbColNo startTimeIndex = colSetOrder->ColNo( KCseScheduleStartTimeCol );
       
  2021 	TDbColNo endTimeIndex = colSetOrder->ColNo( KCseScheduleEndTimeCol );
       
  2022 	TDbColNo applicationUidIdIndex = colSetOrder->ColNo( KCseScheduleApplicationUIDCol );
       
  2023 	TDbColNo plugInUidIndex = colSetOrder->ColNo( KCseSchedulePlugInUIDCol );
       
  2024 	TDbColNo typeIndex = colSetOrder->ColNo( KCseScheduleTypeCol );
       
  2025 	TDbColNo applicationSpecificIndex = colSetOrder->ColNo( KCseScheduleApplicationSpecificCol );
       
  2026 	TDbColNo pluginTypeIndex = colSetOrder->ColNo( KCseSchedulePluginTypeCol );
       
  2027 	
       
  2028 	CleanupStack::PopAndDestroy( colSetOrder );
       
  2029 	
       
  2030 	// Loop through all found schedules.
       
  2031 	while ( view.AtRow() )
       
  2032 		{
       
  2033 		// Create new schedule to be added to array
       
  2034 		CCseScheduledProgram* prog = CCseScheduledProgram::NewL();
       
  2035 		CleanupStack::PushL( prog );			
       
  2036 		view.GetL();
       
  2037 		
       
  2038 		// Copy information from DB to schedule.
       
  2039 		prog->SetDbIdentifier( view.ColUint32( keyIndex ) );
       
  2040 		prog->SetName( view.ColDes8( nameIndex ) );
       
  2041 		prog->SetStartTime( view.ColTime( startTimeIndex ) );
       
  2042 		prog->SetEndTime( view.ColTime( endTimeIndex ) );
       
  2043 		prog->SetAppUid( view.ColInt32( applicationUidIdIndex ) );
       
  2044 		prog->SetPluginUid( view.ColInt32( plugInUidIndex ) );
       
  2045 		
       
  2046 		prog->SetScheduleType( CCseScheduledProgram::TCseScheduleType( 
       
  2047 							   view.ColInt32( typeIndex ) ) );
       
  2048         prog->SetPluginType( view.ColInt32( pluginTypeIndex ) );
       
  2049 		TInt streamLen( view.ColLength( applicationSpecificIndex ) );
       
  2050 	
       
  2051 		HBufC8* appDataBuffer = HBufC8::NewL( streamLen );
       
  2052 		CleanupStack::PushL( appDataBuffer );
       
  2053 		
       
  2054 		TPtr8 buffPtr( appDataBuffer->Des() );
       
  2055 		RDbColReadStream readStream;
       
  2056 		readStream.OpenLC( view, applicationSpecificIndex ); // |-> 1
       
  2057 		readStream.ReadL( buffPtr, streamLen );
       
  2058 		readStream.Release();
       
  2059 		CleanupStack::PopAndDestroy(); // 1 <-|
       
  2060 
       
  2061 		prog->SetApplicationDataL( *appDataBuffer );
       
  2062 		
       
  2063 		CleanupStack::PopAndDestroy( appDataBuffer );
       
  2064 		User::LeaveIfError( aNextScheduleArray.Append( prog ) );
       
  2065 		CleanupStack::Pop( prog );
       
  2066 		
       
  2067 		// And next schedule...
       
  2068 		view.NextL();
       
  2069 		}
       
  2070 		
       
  2071 	CleanupStack::PopAndDestroy( &view ); // Closes view
       
  2072 	
       
  2073 	CSELOGSTRING_HIGH_LEVEL("<<<CCseScheduleDB::GetSchedulesByTimeAndPluginL");
       
  2074 	}
       
  2075 
       
  2076 
       
  2077 // End of file.