camappengine/asynchfilesavequeue/src/asynchfsq.cpp
branchRCL_3
changeset 34 27fe719c32e6
parent 0 9b3e960ffc8a
equal deleted inserted replaced
33:e3cdd00b5ae3 34:27fe719c32e6
       
     1 /*
       
     2 * Copyright (c) 2002-2007 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:  Asynchronous File Saving Queue
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <asynchfsq.h>
       
    22 #include <bautils.h> // for deleting files
       
    23 #include "asynchatom.h"
       
    24 
       
    25 // CONSTANTS
       
    26 
       
    27 // ============================ MEMBER FUNCTIONS ===============================
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // CAsynchFSQ::CAsynchFSQ
       
    31 // C++ default constructor can NOT contain any code, that
       
    32 // might leave.
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 CAsynchFSQ::CAsynchFSQ()
       
    36     {
       
    37     LOGTEXT( _L( "CAsynchFSQ::CAsynchFSQ() entering" ) );
       
    38     }
       
    39 
       
    40     
       
    41 // -----------------------------------------------------------------------------
       
    42 // CAsynchFSQ::ConstructL
       
    43 // Symbian 2nd phase constructor can leave.
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 void CAsynchFSQ::ConstructL()
       
    47     {
       
    48     LOGTEXT( _L( "CAsynchFSQ::ConstructL() entering" ) );
       
    49     iPriority = EPriorityNormal;
       
    50     iManualStart = EFalse;
       
    51     LOGTEXT( _L( "CAsynchFSQ::ConstructL() exiting" ) );
       
    52     }
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // CAsynchFSQ::NewL
       
    56 // Two-phased constructor.
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 EXPORT_C CAsynchFSQ* CAsynchFSQ::NewL()
       
    60     {
       
    61     LOGTEXT( _L( "CAsynchFSQ::NewL() entering" ) );
       
    62     CAsynchFSQ* self = new( ELeave ) CAsynchFSQ();   
       
    63     CleanupStack::PushL( self );
       
    64     self->ConstructL();
       
    65     CleanupStack::Pop();
       
    66     return self;
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CAsynchFSQ::~CAsynchFSQ   
       
    71 // Destructor
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 CAsynchFSQ::~CAsynchFSQ()
       
    75     {
       
    76     LOGTEXT( _L( "CAsynchFSQ::~CAsynchFSQ() entering" ) );
       
    77 
       
    78     // clean up queues
       
    79     iQueue.ResetAndDestroy();
       
    80     
       
    81     LOGTEXT( _L( "CAsynchFSQ::~CAsynchFSQ() exiting" ) );
       
    82     }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // CAsynchFSQ::SaveAndDestroy
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 EXPORT_C TInt CAsynchFSQ::SaveAndDestroy( TDesC8& aData, const TDesC& aPath, TBool aOverwrite )
       
    89     {
       
    90     LOGTEXT( _L( "CAsynchFSQ::SaveAndDestroy() entering" ) );
       
    91     
       
    92     TInt err = KErrNone;
       
    93     
       
    94     // return if NULL descriptor.
       
    95     if( !&aData )
       
    96         {
       
    97         return KErrArgument;
       
    98         }
       
    99     
       
   100     iOverwrite = aOverwrite;
       
   101     
       
   102     LOGTEXT2( _L( "CAsynchFSQ::SaveAndDestroy() iQueue.Count()=%d" ), iQueue.Count() );
       
   103     LOGTEXT2( _L( "CAsynchFSQ::SaveAndDestroy() ActionsLeft()=%d" ), ActionsLeft() );
       
   104     
       
   105     // add data to our queue
       
   106     err = Enqueue( EFileSave, aData, aPath, KNullDesC, ENullSchemaType, KNullDesC, KNullDesC );
       
   107     
       
   108     LOGTEXT2( _L( "CAsynchFSQ::SaveAndDestroy() iManualStart=%d" ), iManualStart );
       
   109 
       
   110     if ( !err && !iManualStart )
       
   111         {
       
   112         err = Go();	
       
   113         }
       
   114         
       
   115     LOGTEXT2( _L( "CAsynchFSQ::SaveAndDestroy() returning err=%d" ), err );
       
   116     return err;
       
   117     }
       
   118         
       
   119 // -----------------------------------------------------------------------------
       
   120 // CAsynchFSQ::Go
       
   121 // -----------------------------------------------------------------------------
       
   122 //
       
   123 EXPORT_C TInt CAsynchFSQ::Go()
       
   124     {
       
   125     LOGTEXT( _L( "CAsynchFSQ::Go() entering" ) );
       
   126     TInt err = KErrNone;
       
   127     
       
   128     // make sure the queue has at least one object
       
   129     if( iQueue.Count() == 0 )
       
   130         {
       
   131         err = KErrNotReady;	
       
   132         }
       
   133     
       
   134     // activate AO's
       
   135     
       
   136     for( TInt i = 0; i < iQueue.Count(); i++ )
       
   137         {
       
   138         if(( !err )&&( iQueue[i]->GetState() == EPending ))
       
   139             {
       
   140             err = iQueue[i]->Go();
       
   141             }
       
   142         }
       
   143     
       
   144     LOGTEXT2( _L( "CAsynchFSQ::Go() returning err=%d" ), err );
       
   145     return err;
       
   146     }
       
   147         
       
   148 // -----------------------------------------------------------------------------
       
   149 // CAsynchFSQ::SetManualStart
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 EXPORT_C void CAsynchFSQ::SetManualStart( TBool aManualStart )
       
   153     {
       
   154     LOGTEXT( _L( "CAsynchFSQ::ManualStart() entering" ) );
       
   155     iManualStart = aManualStart;
       
   156     LOGTEXT( _L( "CAsynchFSQ::ManualStart() returning" ) );
       
   157     }
       
   158 
       
   159 // -----------------------------------------------------------------------------
       
   160 // CAsynchFSQ::Delete
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 EXPORT_C TInt CAsynchFSQ::Delete( const TDesC& aPath )
       
   164     {
       
   165     LOGTEXT( _L( "CAsynchFSQ::Delete() entering" ) );
       
   166 	TInt err = KErrNotFound;
       
   167 
       
   168     // search for path name and delete if found
       
   169     for( TInt i = 0; i < iQueue.Count(); i++ )
       
   170         {
       
   171         if ( iQueue[i]->DoesLocalSave() )
       
   172             {
       
   173             if ( (iQueue[i]->GetPath()).Compare( aPath ) == 0 )
       
   174                 {
       
   175                 LOGTEXT( _L( "CAsynchFSQ::Delete() file found, deleting" ) );
       
   176                 err = iQueue[i]->DeleteLocal();
       
   177                 }
       
   178             }
       
   179         }
       
   180         
       
   181     // if still not found,
       
   182     // attempt to delete from file system
       
   183     if ( err == KErrNotFound )
       
   184         {
       
   185         LOGTEXT( _L( "CAsynchFSQ::Delete() not in queues, trying file system" ) );
       
   186         RFs fs;
       
   187         TInt connectError = fs.Connect();
       
   188         BaflUtils ba;
       
   189         if( !connectError && ba.FileExists( fs, aPath ) )
       
   190             {
       
   191             LOGTEXT( _L( "CAsynchFSQ::Delete() exists, deleting from file system" ) );
       
   192             err = KErrNone;
       
   193             ba.DeleteFile( fs, aPath );
       
   194             }
       
   195         fs.Close();
       
   196         }
       
   197        
       
   198     LOGTEXT2( _L( "CAsynchFSQ::Delete() returning err=%d" ), err );
       
   199     return err;
       
   200     }
       
   201     
       
   202 // -----------------------------------------------------------------------------
       
   203 // CAsynchFSQ::Rename
       
   204 // -----------------------------------------------------------------------------
       
   205 //
       
   206 EXPORT_C TInt CAsynchFSQ::Rename( const TDesC& aOld, const TDesC& aNew )
       
   207     {
       
   208     LOGTEXT( _L( "CAsynchFSQ::Rename() entering" ) );
       
   209   
       
   210 	TInt err = KErrNotFound;
       
   211 
       
   212     // search for path name and delete if found
       
   213     for( TInt i = 0; i < iQueue.Count(); i++ )
       
   214         {
       
   215         if ( iQueue[i]->DoesLocalSave() )
       
   216             {
       
   217             if ( (iQueue[i]->GetPath()).Compare( aOld ) == 0 )
       
   218                 {
       
   219                 LOGTEXT( _L( "CAsynchFSQ::Delete() file found, renaming" ) );
       
   220                 err = iQueue[i]->RenameLocal( aNew );
       
   221                 }
       
   222             }
       
   223         }       
       
   224     
       
   225     // if still not found,
       
   226     // attempt to rename from file system
       
   227     if ( err == KErrNotFound )
       
   228         {
       
   229         LOGTEXT( _L( "CAsynchFSQ::Rename() not in queues, trying file system" ) );
       
   230         RFs fs;
       
   231         TInt connectError = fs.Connect();
       
   232         BaflUtils ba;
       
   233         if( !connectError && ba.FileExists( fs, aOld ) )
       
   234             {
       
   235             LOGTEXT( _L( "CAsynchFSQ::Rename() exists, renaming in file system" ) );
       
   236             err = KErrNone;
       
   237             ba.RenameFile( fs, aOld, aNew );
       
   238             }
       
   239         fs.Close();
       
   240         }
       
   241 
       
   242     LOGTEXT2( _L( "CAsynchFSQ::Rename() returning err=%d" ), err );
       
   243     return err;
       
   244     }
       
   245 
       
   246 
       
   247     
       
   248 // -----------------------------------------------------------------------------
       
   249 // CAsynchFSQ::SetThreadPriority
       
   250 // -----------------------------------------------------------------------------
       
   251 //
       
   252 EXPORT_C void CAsynchFSQ::SetPriority( TInt aPriority ) 
       
   253     {
       
   254     LOGTEXT2( _L( "CAsynchFSQ::SetThreadPriority() entering aPriority=%d" ), aPriority );
       
   255     iPriority = aPriority;
       
   256     LOGTEXT( _L( "CAsynchFSQ::SetThreadPriority() exiting" ) );
       
   257     }
       
   258     
       
   259 // -----------------------------------------------------------------------------
       
   260 // CAsynchFSQ::SetCallback
       
   261 // -----------------------------------------------------------------------------
       
   262 //
       
   263 EXPORT_C void CAsynchFSQ::SetCallback( MAsynchFSQObserver* aCallback )
       
   264     {
       
   265     LOGTEXT( _L( "CAsynchFSQ::SetCallback() entering" ) );
       
   266     iCallback = aCallback;
       
   267     LOGTEXT( _L( "CAsynchFSQ::SetCallback() exiting" ) );
       
   268     }
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // CAsynchFSQ::ActionsLeft
       
   272 // -----------------------------------------------------------------------------
       
   273 //
       
   274 EXPORT_C TInt CAsynchFSQ::ActionsLeft()
       
   275     {
       
   276     TInt actionsLeft = 0;
       
   277     
       
   278     for( TInt i = 0; i < iQueue.Count(); i++ )
       
   279         {
       
   280         actionsLeft += iQueue[i]->ActionsLeft();
       
   281         }
       
   282         
       
   283     LOGTEXT2( _L( "CAsynchFSQ::ActionsLeft() actionsLeft=%d" ), actionsLeft );
       
   284     return actionsLeft;
       
   285     }
       
   286     
       
   287 // -----------------------------------------------------------------------------
       
   288 // CAsynchFSQ::Cancel
       
   289 // -----------------------------------------------------------------------------
       
   290 //
       
   291 EXPORT_C void CAsynchFSQ::Cancel()
       
   292     {
       
   293     LOGTEXT( _L( "CAsynchFSQ::Cancel() entering" ) );
       
   294     
       
   295     while( iQueue.Count() )
       
   296         {
       
   297         delete iQueue[0];
       
   298         iQueue[0] = NULL;
       
   299         iQueue.Remove( 0 ); // Data MUST be deleted first
       
   300         }
       
   301         
       
   302     LOGTEXT( _L( "CAsynchFSQ::Cancel() exiting" ) );
       
   303     }
       
   304     
       
   305 // -----------------------------------------------------------------------------
       
   306 // CAsynchFSQ::Notify
       
   307 // -----------------------------------------------------------------------------
       
   308 //
       
   309 void CAsynchFSQ::Notify( TInt aError )
       
   310     {
       
   311     LOGTEXT( _L( "CAsynchFSQ::Notify() entering" ) );
       
   312     iCallback->MAsynchFSQEvent( aError );
       
   313     
       
   314     // Scan and delete completed atoms
       
   315     for( TInt i = 0; i < iQueue.Count(); i++ )
       
   316         {
       
   317         if( iQueue[i]->ActionsLeft() == 0 )
       
   318             {
       
   319             delete iQueue[i];
       
   320             iQueue[i] = NULL;
       
   321             iQueue.Remove( i ); // Data MUST be deleted first
       
   322             i--;
       
   323             }
       
   324         }
       
   325         
       
   326     LOGTEXT( _L( "CAsynchFSQ::Notify() exiting" ) );
       
   327     }
       
   328         
       
   329 // -----------------------------------------------------------------------------
       
   330 // CAsynchFSQ::Enqueue
       
   331 // -----------------------------------------------------------------------------
       
   332 //
       
   333 TInt CAsynchFSQ::Enqueue( TFSQActionType aActionType, TDesC8& aData, 
       
   334                           const TDesC& aPath, const TDesC& aURL, 
       
   335                           TFSQSchemaType aSchema, const TDesC& aUserName, 
       
   336                           const TDesC& aPassword )
       
   337     {
       
   338     LOGTEXT( _L( "CAsynchFSQ::Enqueue() entering" ) );
       
   339     
       
   340     TInt err = KErrNone;
       
   341     
       
   342     TInt queueError = KErrNone; 
       
   343     TRAP( err,
       
   344         // create atom
       
   345         CAsynchFSQAtom* atom = 
       
   346             CAsynchFSQAtom::NewL( this, 
       
   347                                   iPriority,
       
   348                                   aActionType,
       
   349                                   aData,
       
   350                                   aPath,
       
   351                                   aURL,
       
   352                                   aSchema,
       
   353                                   aUserName,
       
   354                                   aPassword );
       
   355         
       
   356         //append to queue
       
   357         iQueue.Append( atom );
       
   358         );
       
   359 
       
   360     if ( !err && queueError )
       
   361         {
       
   362         err = queueError;
       
   363         }
       
   364 
       
   365     LOGTEXT2( _L( "CAsynchFSQ::Enqueue() returning err=%d" ), err );
       
   366     return err;
       
   367     }
       
   368     
       
   369 //  End of File