remotestoragefw/remotefileengine/src/rsfwclosestatemachine.cpp
branchRCL_3
changeset 15 88ee4cf65e19
parent 12 87c71b25c937
child 16 1aa8c82cb4cb
equal deleted inserted replaced
12:87c71b25c937 15:88ee4cf65e19
     1 /*
       
     2 * Copyright (c) 2005-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 "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:  State machine for closing a file
       
    15 *
       
    16 */
       
    17 
       
    18 #include <apgcli.h>
       
    19 
       
    20 #include "rsfwclosestatemachine.h"
       
    21 #include "rsfwfileentry.h"
       
    22 #include "rsfwfiletable.h"
       
    23 #include "rsfwvolumetable.h"
       
    24 #include "rsfwvolume.h"
       
    25 #include "rsfwfileengine.h"
       
    26 #include "rsfwrfeserver.h"
       
    27 #include "rsfwwaitnotemanager.h"
       
    28 #include "rsfwlockmanager.h"
       
    29 #include "mdebug.h"
       
    30 
       
    31 
       
    32 // CRsfwCloseStateMachine
       
    33 
       
    34 // ----------------------------------------------------------------------------
       
    35 // CRsfwCloseStateMachine::CRsfwCloseStateMachine
       
    36 // ----------------------------------------------------------------------------
       
    37 //
       
    38 CRsfwCloseStateMachine::CRsfwCloseStateMachine()
       
    39     {
       
    40     }
       
    41 
       
    42 // ----------------------------------------------------------------------------
       
    43 // CRsfwCloseStateMachine::~CRsfwCloseStateMachine
       
    44 // ----------------------------------------------------------------------------
       
    45 //
       
    46 CRsfwCloseStateMachine::~CRsfwCloseStateMachine()
       
    47     {
       
    48     }
       
    49 
       
    50 // ----------------------------------------------------------------------------
       
    51 // CRsfwCloseStateMachine::CompleteRequestL
       
    52 // ----------------------------------------------------------------------------
       
    53 //
       
    54 CRsfwCloseStateMachine::TState* CRsfwCloseStateMachine::CompleteRequestL(TInt aError)
       
    55     {
       
    56     DEBUGSTRING(("CRsfwCloseStateMachine::CompleteRequestL::ErrorL %d", aError));
       
    57 
       
    58     // decrease count of files opened for writing
       
    59     Node()->iFileTable->UpdateOpenFileCount(-1);
       
    60  
       
    61     if (iFlags != ECloseLastFlushFailed) 
       
    62         {
       
    63         // file was closed successfully (not saved locally and deleted from filetable)
       
    64         DEBUGSTRING(("file was closed successfully (not saved locally and deleted from filetable)"));
       
    65         
       
    66         // If we just wrote the file to the server set attributes from the cache
       
    67         // file's attributes.
       
    68         if (Node()->CacheFileName() && Node()->IsOpenedForWriting())
       
    69                 {
       
    70                 Node()->SetCached(ETrue);
       
    71                 FileEngine()->SetupAttributes(*Node());
       
    72                 }
       
    73            
       
    74         // Add new cached and closed file to LRU cache managemet list.
       
    75         if ((Node()->Type() == KNodeTypeFile) &&
       
    76             (Node()->iCachedSize >0))
       
    77             {
       
    78             Volumes()->AddToLRUPriorityListL(Node(), Node()->CachePriority());
       
    79             }
       
    80     
       
    81         // If no content is in the cache then add closed file to the metadata LRU list.
       
    82         if ((Node()->Type() == KNodeTypeFile) &&
       
    83             (Node()->iCachedSize == 0))
       
    84             {
       
    85             Volumes()->AddToMetadataLRUPriorityListL(Node(), Node()->CachePriority());
       
    86             }  
       
    87 
       
    88         // uncommitted modifications have been resolved
       
    89         Node()->SetOpenedForWriting(EFalse);
       
    90         }
       
    91      else 
       
    92         {
       
    93         // remove the file from filetable as it was saved locally as a result of error
       
    94         // (either ECloseLastFlushFailed was set or we attempted to write the data and
       
    95         // there was error)
       
    96         Node()->iFileTable->RemoveL(Node());
       
    97         delete Node();
       
    98         }
       
    99 
       
   100     CompleteAndDestroyState()->SetErrorCode(aError);
       
   101     // remove the wait note
       
   102     DeleteWaitNoteL(ETrue);
       
   103     return CompleteAndDestroyState();
       
   104     }
       
   105 
       
   106 // ----------------------------------------------------------------------------
       
   107 // CRsfwCloseStateMachine::ErrorOnStateEntry
       
   108 // ----------------------------------------------------------------------------
       
   109 // 
       
   110 CRsfwCloseStateMachine::TState* CRsfwCloseStateMachine::ErrorOnStateEntry(TInt aError) 
       
   111     {
       
   112     DEBUGSTRING16(("CRsfwCloseStateMachine::ErrorOnStateEntry %d", aError));
       
   113 
       
   114     if (aError == KErrNotFound) 
       
   115         {
       
   116         // the node was not found, do not try to clos it
       
   117         return CRsfwRfeStateMachine::ErrorOnStateEntry(aError);
       
   118         }
       
   119     else 
       
   120         {    
       
   121         // don't show 'save as' note if transfer was cancelled explicitily by the user
       
   122         if (iFlags == ECloseLastFlushFailed && !Node()->IsCancelled()) 
       
   123             {
       
   124             // modified file, last flush failed so let user to save the file locally
       
   125             return new CRsfwCloseStateMachine::TSaveLocallyState(this);
       
   126             }
       
   127         else 
       
   128             {
       
   129             // in any case, we mark this file as closed
       
   130             CRsfwCloseStateMachine::TState* nextstate = NULL;
       
   131             TRAP_IGNORE(nextstate = CompleteRequestL(KErrNone));
       
   132             return nextstate;
       
   133             }
       
   134         }
       
   135 
       
   136     }
       
   137 
       
   138 // Release lock
       
   139 
       
   140 // ----------------------------------------------------------------------------
       
   141 // CRsfwCloseStateMachine::TReleaseLockState::TReleaseLockState
       
   142 // ----------------------------------------------------------------------------
       
   143 //
       
   144 CRsfwCloseStateMachine::
       
   145 TReleaseLockState::TReleaseLockState(CRsfwCloseStateMachine* aParent)
       
   146     : iOperation(aParent)
       
   147     {
       
   148     }
       
   149 
       
   150 // ----------------------------------------------------------------------------
       
   151 // CRsfwCloseStateMachine::TReleaseLockState::EnterL
       
   152 // ----------------------------------------------------------------------------
       
   153 //
       
   154 void CRsfwCloseStateMachine::TReleaseLockState::EnterL()
       
   155     {
       
   156     DEBUGSTRING(("CRsfwCloseStateMachine::TReleaseLockState::EnterL"));
       
   157     
       
   158     if (!iOperation->Node())
       
   159         {
       
   160         User::Leave(KErrNotFound);
       
   161         }
       
   162 
       
   163     DEBUGSTRING16(("closing fid %d (%S)", 
       
   164                         iOperation->Node()->Fid().iNodeId,
       
   165                         iOperation->Node()->Name()));
       
   166                     
       
   167 
       
   168     if (iOperation->Node()->Type() != KNodeTypeFile)
       
   169         {
       
   170         // Sanity
       
   171         DEBUGSTRING(("closing something else than a file!!!"));
       
   172         User::Leave(KErrArgument);
       
   173         }
       
   174 
       
   175     TRfeCloseInArgs* inArgs =
       
   176         static_cast<TRfeCloseInArgs*>(iOperation->iInArgs);
       
   177         
       
   178    DEBUGSTRING(("flags %d", inArgs->iFlags));  
       
   179    
       
   180    iOperation->iFlags = inArgs->iFlags;  
       
   181     
       
   182    if (iOperation->Node()->IsLocked())
       
   183         {
       
   184         // always attempt to unlock the file if it was locked
       
   185         iOperation->FileEngine()->LockManager()->ReleaseLockL(iOperation->Node(),
       
   186                                                           iOperation);
       
   187         }
       
   188     else
       
   189         {
       
   190         // no need to release the lock
       
   191         iOperation->HandleRemoteAccessResponse(0, KErrNone);
       
   192         } 
       
   193     
       
   194     }
       
   195 
       
   196 // ----------------------------------------------------------------------------
       
   197 // CRsfwCloseStateMachine::TReleaseLockState::CompleteL
       
   198 // ----------------------------------------------------------------------------
       
   199 //
       
   200 CRsfwCloseStateMachine::TState* CRsfwCloseStateMachine::TReleaseLockState::CompleteL()
       
   201     {
       
   202     DEBUGSTRING(("CRsfwCloseStateMachine::TReleaseLockState::CompleteL"));
       
   203     iOperation->Node()->RemoveLocked();
       
   204     
       
   205     // don't show 'save as' note if transfer was cancelled explicitily by the user
       
   206     if (iOperation->iFlags == ECloseLastFlushFailed && !iOperation->Node()->IsCancelled()) 
       
   207         {
       
   208         // modified file, last flush failed so let user to save the file locally
       
   209         return new CRsfwCloseStateMachine::TSaveLocallyState(iOperation);
       
   210         }
       
   211     else 
       
   212         {
       
   213         return iOperation->CompleteRequestL(KErrNone);
       
   214         }
       
   215    
       
   216     }
       
   217 
       
   218 // ----------------------------------------------------------------------------
       
   219 // CRsfwCloseStateMachine::TReleaseLockState::ErrorL
       
   220 // ----------------------------------------------------------------------------
       
   221 //
       
   222 CRsfwCloseStateMachine::TState*
       
   223 CRsfwCloseStateMachine::TReleaseLockState::ErrorL(TInt /* aCode */)
       
   224     {
       
   225     DEBUGSTRING(("CRsfwCloseStateMachine::TReleaseLockState::ErrorL"));
       
   226     // Probably not really an error as  according to the RFC locks 
       
   227     // can disappear anytime anyway, lets just run the logic in CompleteL
       
   228     return CompleteL();
       
   229     }
       
   230 
       
   231 
       
   232 // save as
       
   233 
       
   234 // ----------------------------------------------------------------------------
       
   235 // CRsfwCloseStateMachine::TSaveLocallyState::TSaveLocallyState
       
   236 // ----------------------------------------------------------------------------
       
   237 //
       
   238 CRsfwCloseStateMachine::
       
   239 TSaveLocallyState::TSaveLocallyState(CRsfwCloseStateMachine* aParent)
       
   240    : iOperation(aParent)
       
   241     {
       
   242     }
       
   243 
       
   244 // ----------------------------------------------------------------------------
       
   245 // CRsfwCloseStateMachine::TSaveLocallyState::EnterL
       
   246 // ----------------------------------------------------------------------------
       
   247 //
       
   248 void CRsfwCloseStateMachine::TSaveLocallyState::EnterL()
       
   249     {
       
   250     DEBUGSTRING(("CRsfwCloseStateMachine::TSaveLocallyState::EnterL"));
       
   251     TEntry fEntry;
       
   252     CRsfwRfeServer::Env()->iFs.Entry((*(iOperation->Node()->CacheFileName())), fEntry);
       
   253     iFileSizeString.Num(fEntry.iSize);
       
   254     TPtrC cacheDriveLetter = iOperation->Node()->CacheFileName()->Left(1);
       
   255     
       
   256     iSaveToRequest.iMethod = TRsfwNotPluginRequest::ESaveToDlg;
       
   257     iSaveToRequest.iDriveName = iOperation->Node()->iFileTable->Volume()->MountInfo()
       
   258     									->iMountConfig.iName;
       
   259     									
       
   260     iSaveToRequest.iFileName = *(iOperation->Node()->Name());  
       
   261     iSaveToRequest.iCacheDrive = cacheDriveLetter;
       
   262     iSaveToRequest.iFileSize = iFileSizeString;
       
   263     
       
   264     				
       
   265     iOperation->Volumes()->WaitNoteManager()->SetSaveToDialogRequestL(iSaveToRequest);
       
   266     
       
   267     iOperation->Volumes()->WaitNoteManager()
       
   268             ->StartWaitNoteL(ERemoteSaveToLocal, iOperation);  
       
   269     }
       
   270    
       
   271 
       
   272 // ----------------------------------------------------------------------------
       
   273 // CRsfwCloseStateMachine::TSaveLocallyState::CompleteL
       
   274 // ----------------------------------------------------------------------------
       
   275 //
       
   276 CRsfwCloseStateMachine::TState* CRsfwCloseStateMachine::TSaveLocallyState::CompleteL()
       
   277     {  
       
   278     DEBUGSTRING(("CRsfwCloseStateMachine::TSaveLocallyState::CompleteL"));
       
   279     TInt err;
       
   280      
       
   281     // move the file from cache to the new location
       
   282     HBufC* newName = HBufC::NewMaxLC(KMaxPath);
       
   283     TPtr pathPtr = newName->Des();
       
   284     pathPtr = iSaveToRequest.iFileName;
       
   285     CFileMan* fman = CFileMan::NewL(CRsfwRfeServer::Env()->iFs);
       
   286     // we assume that this is local-to-local move, and can be synch. call
       
   287     err = fman->Move((*(iOperation->Node()->CacheFileName())), 
       
   288                    pathPtr, CFileMan::EOverWrite);
       
   289     delete fman;
       
   290     if (err == KErrNone) 
       
   291         {
       
   292         iOperation->Volumes()->WaitNoteManager()->ShowFileSavedToDialogL(pathPtr);
       
   293         }
       
   294     else 
       
   295         {
       
   296         iOperation->Volumes()->WaitNoteManager()->ShowFailedSaveNoteL();
       
   297         }
       
   298       
       
   299     CleanupStack::PopAndDestroy(newName);     
       
   300           
       
   301     return iOperation->CompleteRequestL(KErrNone);
       
   302     }
       
   303 
       
   304 // ----------------------------------------------------------------------------
       
   305 // CRsfwCloseStateMachine::TSaveLocallyState::ErrorL
       
   306 // ----------------------------------------------------------------------------
       
   307 //
       
   308 CRsfwCloseStateMachine::TState*
       
   309 CRsfwCloseStateMachine::TSaveLocallyState::ErrorL(TInt aCode)
       
   310     {
       
   311     DEBUGSTRING(("CRsfwCloseStateMachine::TSaveLocallyState::ErrorL %d", aCode));
       
   312     return iOperation->CompleteRequestL(KErrNone);
       
   313     }
       
   314 
       
   315 
       
   316 
       
   317