appinstaller/AppinstUi/Plugin/TaskManager/Src/TaskManagerImpl.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2002-2004 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   This module contains the implementation of CTaskManagerImpl 
       
    15 *                class member functions.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 
       
    22 #include <s32file.h>
       
    23 #include <pathinfo.h>
       
    24 
       
    25 #include "TaskManagerImpl.h"
       
    26 #include "SWInstTask.h"
       
    27 
       
    28 using namespace SwiUI;
       
    29 
       
    30 _LIT( KTaskDir, "Tasks\\" );
       
    31 _LIT( KTaskListMatch, "*" );
       
    32 
       
    33 const TUint KMaxNumLength = 20;
       
    34 
       
    35 
       
    36 // ============================ MEMBER FUNCTIONS ===============================
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CTaskManagerImpl::CTaskManagerImpl
       
    40 // C++ default constructor can NOT contain any code, that
       
    41 // might leave.
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 CTaskManagerImpl::CTaskManagerImpl()
       
    45     {
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CTaskManagerImpl::ConstructL
       
    50 // Symbian 2nd phase constructor can leave.
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 void CTaskManagerImpl::ConstructL()
       
    54     {
       
    55     User::LeaveIfError( iRFs.Connect() );    
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CTaskManagerImpl::NewL
       
    60 // Two-phased constructor.
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 CTaskManagerImpl* CTaskManagerImpl::NewL()
       
    64     {
       
    65     CTaskManagerImpl* self = new( ELeave ) CTaskManagerImpl();
       
    66     CleanupStack::PushL( self );
       
    67     self->ConstructL();
       
    68     CleanupStack::Pop( self );
       
    69     return self; 
       
    70     }
       
    71 
       
    72 // Destructor
       
    73 CTaskManagerImpl::~CTaskManagerImpl()
       
    74     {
       
    75     iTaskList.ResetAndDestroy();    
       
    76     iRFs.Close();    
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // TaskManagerImpl::AddTaskL
       
    81 // Adds a task to the task list.
       
    82 // (other items were commented in a header).
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 void CTaskManagerImpl::AddTaskL( CTask* aTask )
       
    86     {
       
    87     iTaskList.Append( aTask );    
       
    88     }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // TaskManagerImpl::RemoveTaskL
       
    92 // Removes a task from the task list.
       
    93 // (other items were commented in a header).
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 void CTaskManagerImpl::RemoveTaskL( CTask* aTask )
       
    97     {
       
    98     TInt index = iTaskList.Find( aTask );
       
    99     
       
   100     if ( aTask->IsPersistent() )
       
   101         {
       
   102         RemovePersistentTask( *aTask );        
       
   103         }    
       
   104 
       
   105     iTaskList.Remove( index );
       
   106     delete aTask;    
       
   107     }
       
   108      
       
   109 // -----------------------------------------------------------------------------
       
   110 // TaskManagerImpl::FlushTasks
       
   111 // Removes tasks from task list without executing them.
       
   112 // (other items were commented in a header).
       
   113 // -----------------------------------------------------------------------------
       
   114 //             
       
   115 void CTaskManagerImpl::FlushTasks()
       
   116     {
       
   117     while ( iTaskList.Count() )
       
   118         {
       
   119         CTask* task = iTaskList[0];        
       
   120         iTaskList.Remove( 0 );
       
   121 
       
   122         if ( task->IsPersistent() )
       
   123             {
       
   124             RemovePersistentTask( *task );            
       
   125             }
       
   126         
       
   127         delete task;       
       
   128         }
       
   129     
       
   130     iTaskList.Reset();    
       
   131     }
       
   132           
       
   133 // -----------------------------------------------------------------------------
       
   134 // TaskManagerImpl::ExecutePendingTasksL
       
   135 // Executes all pending tasks that are added to the task list within the 
       
   136 // lifetime of this thread.
       
   137 // (other items were commented in a header).
       
   138 // -----------------------------------------------------------------------------
       
   139 //
       
   140 void CTaskManagerImpl::ExecutePendingTasksL()
       
   141     {
       
   142     TInt error( KErrNone );
       
   143 
       
   144     while ( iTaskList.Count() )
       
   145         {
       
   146         CTask* task = iTaskList[0];        
       
   147         TRAP( error, task->ExecuteL() );
       
   148         iTaskList.Remove( 0 );
       
   149 
       
   150         if ( error == KErrNone && task->IsPersistent() )
       
   151             {
       
   152             RemovePersistentTask( *task );            
       
   153             }
       
   154         
       
   155         delete task;       
       
   156         }   
       
   157 
       
   158     iTaskList.Reset();    
       
   159 
       
   160     if ( error != KErrNone )
       
   161         {
       
   162         User::Leave( error );        
       
   163         }    
       
   164     }
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // TaskManagerImpl::ExecuteRecoveryTasksL
       
   168 // Executes all tasks from non persistent and persistent memory.
       
   169 // (other items were commented in a header).
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 void CTaskManagerImpl::ExecuteRecoveryTasksL()
       
   173     {
       
   174     TInt error( KErrNone );
       
   175  
       
   176     PopulatePersistentTasksL();
       
   177     
       
   178     while ( iTaskList.Count() )
       
   179         {
       
   180         CTask* task = iTaskList[0];        
       
   181         TRAP( error, task->ExecuteL() );
       
   182         iTaskList.Remove( 0 );
       
   183         
       
   184         if ( task->IsPersistent() )
       
   185             {
       
   186             RemovePersistentTask( *task );            
       
   187             }
       
   188  
       
   189         delete task;       
       
   190         }             
       
   191 
       
   192     iTaskList.Reset();    
       
   193     DeleteTaskFilesL();
       
   194 
       
   195     if ( error != KErrNone )
       
   196         {
       
   197         User::Leave( error );        
       
   198         }    
       
   199     }
       
   200 
       
   201 // -----------------------------------------------------------------------------
       
   202 // TaskManagerImpl::CommitL
       
   203 // Writes all persistent tasks from the task list to a persistent storage.
       
   204 // (other items were commented in a header).
       
   205 // -----------------------------------------------------------------------------
       
   206 //
       
   207 void CTaskManagerImpl::CommitL()
       
   208     {
       
   209     CreateTaskDirL();
       
   210     
       
   211     for ( TInt index = 0; index < iTaskList.Count(); index++ )
       
   212         {
       
   213         CTask* task = iTaskList[index];
       
   214         
       
   215         // Write to store only if this task is persistent and it has no id
       
   216         // (already written)
       
   217         if ( task->IsPersistent() && !task->Id() )
       
   218             {
       
   219             // Get an id for the task
       
   220             TInt id = GetFreeTaskIdL();            
       
   221             task->SetId( id );
       
   222           
       
   223             // Create stream
       
   224             RFileWriteStream stream;
       
   225             stream.PushL();
       
   226             TFileName filePath;                
       
   227             GetTaskFilePath( filePath, id );
       
   228             User::LeaveIfError( stream.Create( iRFs, filePath, EFileWrite ) );
       
   229     
       
   230             // Externalize task
       
   231             stream.WriteInt32L( task->ImplementationUid().iUid );            
       
   232             stream << *task;
       
   233             stream.CommitL();
       
   234             CleanupStack::PopAndDestroy(); // stream            
       
   235             }        
       
   236         }
       
   237     }
       
   238 
       
   239 // -----------------------------------------------------------------------------
       
   240 // TaskManagerImpl::IncreaseClientCount
       
   241 // Increase the count of clients.
       
   242 // (other items were commented in a header).
       
   243 // -----------------------------------------------------------------------------
       
   244 //
       
   245 void CTaskManagerImpl::IncreaseClientCount()
       
   246     {
       
   247     ++iClientCount;
       
   248     }
       
   249 
       
   250 // -----------------------------------------------------------------------------
       
   251 // TaskManagerImpl::DecreaseClientCount
       
   252 // Decrease the count of clients.
       
   253 // (other items were commented in a header).
       
   254 // -----------------------------------------------------------------------------
       
   255 //
       
   256 void CTaskManagerImpl::DecreaseClientCount()
       
   257     {
       
   258     --iClientCount;
       
   259     }
       
   260 
       
   261 // -----------------------------------------------------------------------------
       
   262 // TaskManagerImpl::CanBeFreed
       
   263 // Indicates if no more clients have open instances and memory can be freed.
       
   264 // (other items were commented in a header).
       
   265 // -----------------------------------------------------------------------------
       
   266 //
       
   267 TBool CTaskManagerImpl::CanBeFreed() 
       
   268     {
       
   269     if ( iClientCount > 0 )
       
   270 	{
       
   271         return EFalse;
       
   272 	}
       
   273     else
       
   274 	{
       
   275         return ETrue;
       
   276 	}
       
   277     }
       
   278 
       
   279 // -----------------------------------------------------------------------------
       
   280 // TaskManagerImpl::RemovePersistentTask
       
   281 // Removes task from persistent storage.
       
   282 // (other items were commented in a header).
       
   283 // -----------------------------------------------------------------------------
       
   284 //    
       
   285 void CTaskManagerImpl::RemovePersistentTask( const CTask& aTask )
       
   286     {
       
   287     // Remove only if task is committed, i.e. it has an id.
       
   288     if ( aTask.Id() )
       
   289         {        
       
   290         TFileName fileName;    
       
   291         GetTaskFilePath( fileName, aTask.Id() );
       
   292         iRFs.Delete( fileName );    
       
   293         }    
       
   294     }
       
   295 
       
   296 // -----------------------------------------------------------------------------
       
   297 // TaskManagerImpl::DeleteTaskFilesL
       
   298 // Deletes all tasks from persistent storage.
       
   299 // (other items were commented in a header).
       
   300 // -----------------------------------------------------------------------------
       
   301 //  
       
   302 void CTaskManagerImpl::DeleteTaskFilesL()
       
   303     {
       
   304     TFileName fileMatch;
       
   305     GetTaskDir( fileMatch );
       
   306     fileMatch.Append( KTaskListMatch );
       
   307 
       
   308     CFileMan* fileMan = CFileMan::NewL( iRFs );
       
   309     fileMan->Delete( fileMatch );
       
   310     delete fileMan;    
       
   311     }
       
   312 
       
   313 // -----------------------------------------------------------------------------
       
   314 // TaskManagerImpl::CreateTaskDirL
       
   315 // Creates the directory where persistent tasks are stored.
       
   316 // (other items were commented in a header).
       
   317 // -----------------------------------------------------------------------------
       
   318 //  
       
   319 void CTaskManagerImpl::CreateTaskDirL()
       
   320     {
       
   321     TInt drive = 0;
       
   322     iRFs.CharToDrive( TParsePtrC( PathInfo::PhoneMemoryRootPath() ).Drive()[0], drive );
       
   323     iRFs.CreatePrivatePath( drive );
       
   324     
       
   325     TFileName taskDir;    
       
   326     GetTaskDir( taskDir );    
       
   327     iRFs.MkDir( taskDir );    
       
   328     }
       
   329 
       
   330 // -----------------------------------------------------------------------------
       
   331 // TaskManagerImpl::GetTaskDir
       
   332 // Gets the directory where persistent tasks are stored.
       
   333 // (other items were commented in a header).
       
   334 // -----------------------------------------------------------------------------
       
   335 //  
       
   336 void CTaskManagerImpl::GetTaskDir( TDes& aFilePath )
       
   337     {
       
   338     iRFs.PrivatePath( aFilePath );
       
   339     aFilePath.Insert( 0, TParsePtrC( PathInfo::PhoneMemoryRootPath() ).Drive() );
       
   340     aFilePath.Append( KTaskDir );    
       
   341     }
       
   342 
       
   343 // -----------------------------------------------------------------------------
       
   344 // TaskManagerImpl::GetTaskFilePath
       
   345 // Gets the full path to the persistent task file.
       
   346 // (other items were commented in a header).
       
   347 // -----------------------------------------------------------------------------
       
   348 //  
       
   349 void CTaskManagerImpl::GetTaskFilePath( TDes& aFilePath, TInt aTaskId )
       
   350     {
       
   351     GetTaskDir( aFilePath );    
       
   352     aFilePath.AppendNum( aTaskId ); 
       
   353     }
       
   354 
       
   355 // -----------------------------------------------------------------------------
       
   356 // TaskManagerImpl::PopulatePersistentTasksL
       
   357 // Reads all persistent task from storage and populates the task list.
       
   358 // (other items were commented in a header).
       
   359 // -----------------------------------------------------------------------------
       
   360 //  
       
   361 void CTaskManagerImpl::PopulatePersistentTasksL()
       
   362     {
       
   363     // Get task dir matcher
       
   364     HBufC* taskDir = HBufC::NewLC( KMaxFileName );
       
   365     TPtr dirPtr( taskDir->Des() );    
       
   366     GetTaskDir( dirPtr );
       
   367     dirPtr.Append( KTaskListMatch );
       
   368     
       
   369     // Get dir contents
       
   370     CDir* dirList;    
       
   371     iRFs.GetDir( dirPtr, KEntryAttMaskSupported, ESortByDate, dirList );
       
   372     
       
   373     if ( dirList )
       
   374         {        
       
   375         CleanupStack::PushL( dirList );    
       
   376     
       
   377         HBufC* taskPath = HBufC::NewLC( KMaxFileName );
       
   378         TPtr pathPtr( taskPath->Des() );
       
   379         GetTaskDir( dirPtr );
       
   380         
       
   381         // Go through the list and internalize tasks
       
   382         for ( TInt index = 0; index < dirList->Count(); index++ )
       
   383             {
       
   384             pathPtr.Copy( dirPtr );            
       
   385             pathPtr.Append( (*dirList)[index].iName );
       
   386             
       
   387             // Create stream
       
   388             RFileReadStream stream;
       
   389             stream.PushL();    
       
   390             User::LeaveIfError( stream.Open( iRFs, pathPtr, EFileRead ) );
       
   391 
       
   392             // Internalize task
       
   393             TUid uid;
       
   394             uid.iUid = stream.ReadInt32L();            
       
   395             CTask* task = CTask::NewL( uid, ETrue );
       
   396             stream >> *task;
       
   397             iTaskList.Append( task );
       
   398             CleanupStack::PopAndDestroy(); // stream        
       
   399             }
       
   400 
       
   401         CleanupStack::PopAndDestroy( 2, dirList );        
       
   402         }
       
   403     CleanupStack::PopAndDestroy( taskDir );    
       
   404     }
       
   405 
       
   406 // -----------------------------------------------------------------------------
       
   407 // TaskManagerImpl::GetFreeTaskIdL
       
   408 // Gets the next free id that can be assigned to a task.
       
   409 // (other items were commented in a header).
       
   410 // -----------------------------------------------------------------------------
       
   411 //  
       
   412 TInt CTaskManagerImpl::GetFreeTaskIdL()
       
   413     {
       
   414     TInt result( 0 );
       
   415     
       
   416     // Get task dir matcher
       
   417     HBufC* taskDir = HBufC::NewL( KMaxFileName );
       
   418     TPtr dirPtr( taskDir->Des() );    
       
   419     GetTaskDir( dirPtr );
       
   420     dirPtr.Append( KTaskListMatch );
       
   421     
       
   422     // Get dir contents
       
   423     CDir* dirList;    
       
   424     iRFs.GetDir( dirPtr, KEntryAttMaskSupported, ESortByDate, dirList );
       
   425     delete taskDir;
       
   426     
       
   427     if ( dirList )
       
   428         {        
       
   429         // Find the first free slot
       
   430         TInt index( 0 );        
       
   431         while ( index < dirList->Count() )
       
   432             {
       
   433             TBuf<KMaxNumLength> temp;
       
   434             // Make sure that first index is 1
       
   435             temp.Num( index + 1 );            
       
   436             if ( TParsePtrC( (*dirList)[index].iName ).Name() != temp )
       
   437                 {
       
   438                 break;
       
   439                 } 
       
   440             ++index;            
       
   441             }   
       
   442 
       
   443         delete dirList;   
       
   444         // Make sure that the first index is 1        
       
   445         result = index + 1;        
       
   446         }
       
   447 
       
   448     return result;    
       
   449     }
       
   450 
       
   451 //  End of File