videoscheduler/SchedulerServer/inc/CCseScheduleDB.h
branchRCL_3
changeset 47 826cea16efd9
parent 45 798ee5f1972c
child 48 13a33d82ad98
equal deleted inserted replaced
45:798ee5f1972c 47:826cea16efd9
     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:    header file for CCseScheduleDB class*
       
    15 */
       
    16 
       
    17 
       
    18 
       
    19 #ifndef _CCSESCHEDULEDB_H
       
    20 #define _CCSESCHEDULEDB_H
       
    21 
       
    22 #include <e32base.h>
       
    23 #include <d32dbms.h>    // RDbStoreDatabase
       
    24 #include <f32file.h>    // RFs
       
    25 #include <s32file.h>
       
    26 #include <babackup.h>
       
    27 
       
    28 // CONSTANTS
       
    29 const int KCseCustomSqlLength               = 256;
       
    30 const int KCseDbCompactTimeout              = 30000000;
       
    31 
       
    32 // Constant for filename definition
       
    33 _LIT( KCseScheduleDBName, "CseSchedules.db" );
       
    34 // Database table
       
    35 _LIT( KCseDatabaseTable,                    "CseDatabase" );
       
    36 _LIT( KCseDatabaseVersionCol,               "DbVersion" );
       
    37 _LIT( KCseDatabaseReserved1Col,             "DbReserved1" );
       
    38 _LIT( KCseDatabaseReserved2Col,             "DbReserved2" );
       
    39 
       
    40 // Program table
       
    41 _LIT( KCseScheduleTable, 					"ScheduleEvent" );
       
    42 _LIT( KCseScheduleDbKeyCol, 				"Key" );
       
    43 _LIT( KCseScheduleNameCol,					"Name" );
       
    44 _LIT( KCseScheduleStartTimeCol,				"StartTime" );
       
    45 _LIT( KCseScheduleEndTimeCol,				"EndTime" );
       
    46 _LIT( KCseScheduleApplicationUIDCol, 		"ApplicationUID" );
       
    47 _LIT( KCseSchedulePlugInUIDCol,    			"PlugInUID" );
       
    48 _LIT( KCseScheduleTypeCol,					"Type" );
       
    49 _LIT( KCseScheduleApplicationSpecificCol,	"ApplicationSpecificData" );
       
    50 _LIT( KCseScheduleRunCountCol,              "ScheduleRunCount" );
       
    51 _LIT( KCseScheduleStateCol,                 "ScheduleState" );
       
    52 _LIT( KCseSchedulePluginTypeCol,            "PluginType" );
       
    53 _LIT( KCseScheduleReserved1Col,             "ScheduleReserved1" );
       
    54 _LIT( KCseScheduleReserved2Col,             "ScheduleReserved2" );
       
    55 
       
    56 class CCseScheduledProgram;
       
    57 
       
    58 /**  
       
    59 * Version of CleanupXxxxPushL() that can be used to 'ResetAndDestroy'
       
    60 * the supplied object if the clean-up stack unwinds.
       
    61 */
       
    62 template <class T>
       
    63 class CleanupResetAndDestroy
       
    64     {
       
    65     public:
       
    66         inline static void PushL( T& aRef );
       
    67     private:
       
    68         static void ResetAndDestroy( TAny *aPtr );
       
    69     };
       
    70     
       
    71 template <class T>
       
    72     inline void CleanupResetAndDestroyPushL( T& aRef );
       
    73 
       
    74 template <class T>
       
    75 inline void CleanupResetAndDestroy<T>::PushL( T& aRef )
       
    76      {
       
    77      CleanupStack::PushL( TCleanupItem( &ResetAndDestroy, &aRef ) );
       
    78      }
       
    79 
       
    80 template <class T>
       
    81 void CleanupResetAndDestroy<T>::ResetAndDestroy( TAny *aPtr )
       
    82     {
       
    83     ( static_cast<T*>( aPtr ) )->ResetAndDestroy();
       
    84     }
       
    85     
       
    86 template <class T>
       
    87 inline void CleanupResetAndDestroyPushL( T& aRef )
       
    88     {
       
    89     CleanupResetAndDestroy<T>::PushL( aRef );
       
    90     }
       
    91     
       
    92     
       
    93 /**
       
    94 * Schedule database.
       
    95 */
       
    96 class CCseScheduleDB : public CBase,
       
    97                        public MBackupObserver
       
    98 {
       
    99 	public:	// Constructors and destructors	
       
   100 		virtual ~CCseScheduleDB();
       
   101 		
       
   102 		/**
       
   103         * Two-phased constructor.
       
   104         */
       
   105 		IMPORT_C static CCseScheduleDB* NewL();
       
   106 		
       
   107 		/**
       
   108         * Enum for schedule state
       
   109         */
       
   110 		enum TCseScheduleState
       
   111             {
       
   112             ECseWaiting = 0,
       
   113             ECseRunning            
       
   114             };
       
   115         
       
   116         /**
       
   117         * Enum for database access
       
   118         */    
       
   119         enum TCseDatabaseAccess
       
   120             {
       
   121             ECseDbOpen = 0,
       
   122             ECseDbLocked            
       
   123             };
       
   124             
       
   125     public: // Methods from MBackupObserver
       
   126         void ChangeFileLockL(const TDesC &aFileName, TFileLockFlags aFlags);
       
   127 		
       
   128 	public: // New methods
       
   129 		/**
       
   130 		* Adds given schedule to database. Method will leave with
       
   131 		* KErrLocked -error code if Backup/Restore is going on.
       
   132 		* @param	aData schedule to be added to database.
       
   133 		* @return	None
       
   134 		*/
       
   135 		void AddScheduleL( CCseScheduledProgram& aData );
       
   136 		
       
   137 		/**
       
   138 		* Removes schedule from database. Method will leave with
       
   139 		* KErrLocked -error code if Backup/Restore is going on.
       
   140 		* @param	aData schedule to be removed from database.
       
   141 		* @return	None
       
   142 		*/
       
   143 		void RemoveScheduleL( const TUint32 aDbIdentifier );
       
   144 		
       
   145 		/**
       
   146 		* Finds start time of the schedule that is happening next from the database.
       
   147 		* Method will leave with KErrLocked -error code if Backup/Restore is going on.
       
   148 		* @return	Start time of the next schedule.
       
   149 		*/
       
   150 		TTime GetNextScheduleTimeL();
       
   151 		
       
   152 		/**
       
   153 		* Fills parameter array with schedules that have same starting time as
       
   154 		* paramter time. Method will leave with KErrLocked -error code if
       
   155 		* Backup/Restore is going on.
       
   156 		* @param	aSchdeduleTime      Start time of the schedules fetched.
       
   157 		* @param	aNextScheduleArray  Array containing schedules after call
       
   158 		*                               is completed.
       
   159 		* @return	None
       
   160 		*/
       
   161 		void FillScheduleArrayByTimeL( const TTime& aScheduleTime, 
       
   162 			 		RPointerArray<CCseScheduledProgram>& aNextScheduleArray );
       
   163 		
       
   164 		/**
       
   165 		* Gets the given schedule from database. Method will leave with
       
   166 		* KErrLocked -error code if Backup/Restore is going on.
       
   167 		* @param	aDbIdentifier       Database identifier of the schedule fetched.
       
   168 		* @param	aSchedule           Pointer where schedule is stored when call
       
   169 		*                               is completed.
       
   170 		* @return	None
       
   171 		*/	 						 
       
   172 		void GetScheduleByDbIdentifierL( const TUint32 aDbIdentifier, 
       
   173                                          CCseScheduledProgram* aSchedule );
       
   174 	
       
   175 		/**
       
   176 		* Get application specific schedules from db. Method will leave with
       
   177 		* KErrLocked -error code if Backup/Restore is going on.
       
   178 		* @param    aAppUid         Application UID.
       
   179 		* @param    aScheduleArray  Array where to put scheduled events.
       
   180 		* @return	None
       
   181 		*/
       
   182 		void GetApplicationSchedulesL( const TInt32 aAppUid, 
       
   183                                        RPointerArray<CCseScheduledProgram>& aArray );
       
   184 
       
   185 		/**
       
   186 		* Get overlapping schedules. Method will leave with
       
   187 		* KErrLocked -error code if Backup/Restore is going on.
       
   188 		* @param    aType           Schedule type (one from 
       
   189 		* 		                    CCseScheduledProgram::TCseScheduleType)
       
   190 		* @param    aStartTime      Start time
       
   191 		* @param    aEndTime        End Time
       
   192 		* @param    aResultArray    On return, contains pointers to overlapping schedules
       
   193 		*               		    empty if none found.
       
   194 		* @return   None.
       
   195 		*/
       
   196 		void GetOverlappingSchedulesL( const TInt32 aType, 
       
   197 			                           const TTime& aStartTime,
       
   198 			                           const TTime& aEndTime,
       
   199 			                           RPointerArray<CCseScheduledProgram>& aResultArray );
       
   200 			
       
   201 		/**
       
   202 		* Increases runcount of given schedule in DB. Method will leave with
       
   203 		* KErrLocked -error code if Backup/Restore is going on.
       
   204 		* @param    aDbIdentifier   Database identifier of the schedule thats
       
   205 		*                           runcount is to be incremented		
       
   206 		*/
       
   207 		void IncreaseRunCountL( const TUint32 aDbIdentifier );
       
   208 		
       
   209 		/**
       
   210 		* Gets the runcount of give schedule. Method will leave with
       
   211 		* KErrLocked -error code if Backup/Restore is going on.
       
   212 		* @param    aDbIdentifier   DbIdentifier of the schedule whos runcount is fetched.
       
   213 		* @return   Run count of the schedule.
       
   214 		*/
       
   215 		TInt32 GetRunCountL( const TUint32 aDbIdentifier );
       
   216 		
       
   217 		/**
       
   218 		* Gets the state of give schedule. Method will leave with
       
   219 		* KErrLocked -error code if Backup/Restore is going on.
       
   220 		* @param    aDbIdentifier   DbIdentifier of the schedule whos state is fetched.
       
   221 		* @return   Schedule state (one of the CCseScheduledDB::TCseScheduleState)
       
   222 		*/
       
   223 		TInt32 GetScheduleStateL( const TUint32 aDbIdentifier );
       
   224 		
       
   225 		/**
       
   226 		* Sets the state of give schedule. Method will leave with
       
   227 		* KErrLocked -error code if Backup/Restore is going on.
       
   228 		* @param    aDbIdentifier   DbIdentifier of the schedule whos state is fetched.
       
   229 		* @param    aState          State to be set.
       
   230 		* @return   None
       
   231 		*/
       
   232 		void SetScheduleStateL( const TUint32 aDbIdentifier,
       
   233                                 const TInt32 aState );
       
   234         
       
   235         /**
       
   236 		* Gets the startup schedules from the DB. When phone is set on, we try to run all
       
   237 		* schdeulus that were on run when the phone was set off (run count is bigger than 0).
       
   238 		* Method will leave with KErrLocked -error code if Backup/Restore is going on.
       
   239 		* @param    aResultArray   Array where schedules that were on run are stored.
       
   240 		* @return   None
       
   241 		*/
       
   242         void StartupSchedulesL( RPointerArray<CCseScheduledProgram>& aResultArray );
       
   243         
       
   244         /**
       
   245 		* Get plugin specific schedules from db. Method will leave with
       
   246 		* KErrLocked -error code if Backup/Restore is going on.
       
   247 		* @param    aPluginUid      Application UID.
       
   248 		* @param    aScheduleArray  Array where to put scheduled events.
       
   249 		* @return	None
       
   250 		*/
       
   251         void GetSchedulesByPluginL( const TInt32 aPluginUid,
       
   252 						            RPointerArray<CCseScheduledProgram>& aArray );
       
   253 
       
   254         /**
       
   255 		* Get plugin specific schedules of specific type from db. Method will leave with
       
   256 		* KErrLocked -error code if Backup/Restore is going on.
       
   257 		* @param    aType           Schedule type
       
   258 		* @param    aScheduleArray  Array where to put scheduled events.
       
   259 		* @return	None
       
   260 		*/
       
   261         void GetSchedulesByTypeL( const TInt32 aType,
       
   262 						          RPointerArray<CCseScheduledProgram>& aArray );
       
   263 						          
       
   264         /**
       
   265 		* Get schedules in given timeframe. Method will leave with
       
   266 		* KErrLocked -error code if Backup/Restore is going on.	
       
   267 		* @param    aBeginning      Start time of timeframe
       
   268 		* @param    aEnd            End Time of timeframe
       
   269 		* @param    aResultArray    On return, contains pointers to overlapping schedules
       
   270 		*               		    empty if none found.
       
   271 		* @return   None.
       
   272 		*/
       
   273 		void GetSchedulesByTimeframeL( const TTime& aStartTime,
       
   274 			                           const TTime& aEndTime,
       
   275 			                           RPointerArray<CCseScheduledProgram>& aResultArray );
       
   276 			                           
       
   277         /**
       
   278 		* Gets all the uid's of different plugins that are in DB.
       
   279 		* KErrLocked -error code if Backup/Restore is going on.	
       
   280 		* @param    aPlugins    On return, contains all the different plugin uid's in
       
   281 		*               		db. Empty if none found.
       
   282 		*/
       
   283 		void GetPluginsL( RArray<TInt32>& aPlugins );
       
   284 		
       
   285 		/**
       
   286 		* Get the next happening time of schedule from database based on plugin uid.
       
   287 		* KErrLocked -error code if Backup/Restore is going on.	
       
   288 		* @param    aPluginUid  Plugin uid in which to base the search
       
   289 	    * @return   Next start time of the schedule of given plugin Uid.
       
   290 		*/
       
   291 		TTime GetNextScheduleTimeByPluginL( TInt32 aPluginUid );
       
   292 		
       
   293 		/**
       
   294 		* Gets all the schedules that are going to happen at the given time based on plugin uid.
       
   295 		* KErrLocked -error code if Backup/Restore is going on.	
       
   296 		* @param    aScheduleTime       Start time in which to base the search
       
   297 		* @param    aPluginUid          Plugin uid in which to base the search
       
   298 		* @param    aNextScheduleArray  On return, contains pointers to schedules.
       
   299 		*/
       
   300 		void GetSchedulesByTimeAndPluginL( 
       
   301 		                         const TTime& aScheduleTime, 
       
   302                                  TInt32 aPluginUid,
       
   303 					             RPointerArray<CCseScheduledProgram>& aNextScheduleArray );
       
   304 
       
   305 
       
   306 	private:
       
   307 	    /**
       
   308         * C++ default constructor.        
       
   309         */
       
   310 		CCseScheduleDB();
       
   311 	
       
   312 		/**
       
   313         * By default Symbian 2nd phase constructor is private.
       
   314         */
       
   315 		void ConstructL();
       
   316 
       
   317 	private: // Methods        
       
   318         /**
       
   319         * Checks the db version from db. If it is older than current one, destroy it
       
   320         * and create new one.
       
   321         * @return	None
       
   322         */
       
   323         void CheckDbVersionL();
       
   324         
       
   325         /**        
       
   326         * Open DB or create new database if DB file is not found. 
       
   327         * @return	None
       
   328         */
       
   329         void OpenOrCreateDbL();
       
   330             	
       
   331     	/**
       
   332 		* Create schedule table.
       
   333 		* @param	aDatabase where to create schedule table.
       
   334 		* @return	None
       
   335 		*/
       
   336     	void CreateScheduleTableL( RDbStoreDatabase& aDatabase ) const;
       
   337     	
       
   338     	/**
       
   339  		* Create database table.
       
   340  		* @param	aDatabase where to create database table.
       
   341  		* @return	None
       
   342  		*/
       
   343      	void CreateDatabaseTableL( RDbStoreDatabase& aDatabase );
       
   344      	
       
   345      	/**
       
   346   		* Gets the database version number
       
   347   		* @param	None
       
   348   		* @return	None
       
   349   		*/
       
   350       	TInt GetDbVersionL(  );
       
   351       	
       
   352      	/**
       
   353   		* Set hardcoded database version to database. Used after creating new database.
       
   354   		* @param	None
       
   355   		* @return	None
       
   356   		*/
       
   357       	void SetDatabaseVersionL();
       
   358 
       
   359         /**
       
   360         * Removes existing database.
       
   361         * @return   None
       
   362         */
       
   363 		void RemoveDbL();
       
   364 		
       
   365 		/**
       
   366         * Static call back for CPeriodic.
       
   367         * @param    aThis   Pointer to CCseScheduleDB so that we can
       
   368         *                   direct call back to class. 
       
   369         * @return   Always TFalse
       
   370         */
       
   371 		static TInt CompactCallback( TAny* aThis );
       
   372 		
       
   373 		/**
       
   374         * We want to compact database 30 seconds after last database operation.
       
   375         * this resets 30 sec counter.
       
   376         * @return   None
       
   377         */
       
   378 		void ResetCompactTimer( );
       
   379 		
       
   380 		/**
       
   381         * Compacts database.
       
   382         * @return   None
       
   383         */
       
   384 		void CompactDb();
       
   385 		
       
   386 		/**
       
   387         * Closed Db file and sets iDbAccess to ECseDbLocked.
       
   388         * @return   None
       
   389         */
       
   390 		void CloseDbFile();
       
   391 		
       
   392 		/**
       
   393         * Closed Db file and sets iDbAccess to ECseDbOpen.
       
   394         * @return   None
       
   395         */
       
   396 		void OpenDbFileL();
       
   397 		
       
   398 		/**
       
   399         * Leaves with KErrLocked if iDbAccess is ECseDbLocked.
       
   400         * @return   None
       
   401         */
       
   402 		void LeaveIfDbLockedL() const;
       
   403 
       
   404 	private: // Data
       
   405 		/**
       
   406 		* File server session is connected in construction and closed in
       
   407 		* destruction
       
   408 		*/
       
   409     	RFs iFsSession;
       
   410     	
       
   411     	/**
       
   412     	* File name of the database which to handle
       
   413     	*/    	
       
   414 		TFileName iDbFile;
       
   415 		
       
   416 		/**
       
   417 		* For database operations
       
   418 		*/
       
   419         RDbStoreDatabase iScheduleDb;
       
   420         
       
   421         /**
       
   422         * For creating and opening services database file. Own.
       
   423         */
       
   424         CFileStore* iScheduleFileStore;
       
   425 
       
   426         /**
       
   427         * Timer used in compacting database. Own.
       
   428         */
       
   429 		CPeriodic* iCompactTimer;  
       
   430         
       
   431         /**
       
   432         * SQL string for SQL calls
       
   433         */
       
   434         TBuf<KCseCustomSqlLength> iSqlSchedule;
       
   435         
       
   436         /**
       
   437         * Status is database accessible
       
   438         */
       
   439         TCseDatabaseAccess iDbAccess;
       
   440         
       
   441         /**
       
   442         * Wrapper to get notification if backup/restore is
       
   443         * about to happen. Own.
       
   444         */
       
   445         CBaBackupSessionWrapper* iBackupWrapper;
       
   446 };
       
   447 #endif _CCSESCHEDULEDB_H