remotestoragefw/webdavaccessplugin/src/rsfwdavaccess.cpp
branchRCL_3
changeset 15 88ee4cf65e19
parent 12 87c71b25c937
child 16 1aa8c82cb4cb
equal deleted inserted replaced
12:87c71b25c937 15:88ee4cf65e19
     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:  Implements remote access plugin API using WebDAV protocol
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "rsfwdavaccess.h"
       
    21 #include "rsfwdavfileinfo.h"
       
    22 #include "rsfwdavaccesscontext.h"
       
    23 #include "rsfwdavtransaction.h"
       
    24 #include "mdebug.h"
       
    25 
       
    26 
       
    27 // ============================ MEMBER FUNCTIONS ==============================
       
    28 
       
    29 void CRsfwDavAccess::ConstructL()
       
    30     {
       
    31     }
       
    32 
       
    33 CRsfwDavAccess* CRsfwDavAccess::NewL()
       
    34     {
       
    35     CRsfwDavAccess* self = new (ELeave) CRsfwDavAccess;
       
    36     CleanupStack::PushL(self);
       
    37     self->ConstructL();
       
    38     CleanupStack::Pop(self);
       
    39     return self;
       
    40     }
       
    41 
       
    42 CRsfwDavAccess::~CRsfwDavAccess()
       
    43     {
       
    44     delete iWebDavSession;
       
    45     iDavFileInfos.ResetAndDestroy();
       
    46     iDavAccessContexts.ResetAndDestroy(); 
       
    47     }
       
    48 
       
    49 // ----------------------------------------------------------------------------
       
    50 // CRsfwDavAccess::DavFileInfoL
       
    51 // Find the file info for the given file name.
       
    52 // ----------------------------------------------------------------------------
       
    53 //
       
    54 CRsfwDavFileInfo* CRsfwDavAccess::DavFileInfoL(const TDesC& aName)
       
    55     {
       
    56     TInt index = DavFileInfoIndexL(aName);
       
    57     if (index != KErrNotFound)
       
    58         {
       
    59         return iDavFileInfos[index];
       
    60         }
       
    61     return NULL;
       
    62     }
       
    63 
       
    64 // ----------------------------------------------------------------------------
       
    65 // CRsfwDavAccess::AddDavFileInfo
       
    66 // Add a new file info entry.
       
    67 // ----------------------------------------------------------------------------
       
    68 //
       
    69 void CRsfwDavAccess::AddDavFileInfo(CRsfwDavFileInfo* aDavFileInfo)
       
    70     {
       
    71 #ifdef _DEBUG
       
    72     TPtrC namePtr;
       
    73     if (aDavFileInfo->Name())
       
    74         {
       
    75         namePtr.Set(*aDavFileInfo->Name());
       
    76         }
       
    77     TPtrC8 lockPtr;
       
    78     if (aDavFileInfo->LockToken())
       
    79         {
       
    80         lockPtr.Set(*aDavFileInfo->LockToken());
       
    81         }
       
    82     DEBUGSTRING16(("Add file info: name='%S'", &namePtr));
       
    83     DEBUGSTRING8(("               lock='%S', time=%d",
       
    84                   &lockPtr,
       
    85                   aDavFileInfo->Timeout()));
       
    86     
       
    87 #endif // DEBUG
       
    88     iDavFileInfos.Append(aDavFileInfo);
       
    89     }
       
    90 
       
    91 // ----------------------------------------------------------------------------
       
    92 // CRsfwDavAccess::RemoveDavFileInfoL
       
    93 // Remove a file info entry.
       
    94 // ----------------------------------------------------------------------------
       
    95 //
       
    96 void CRsfwDavAccess::RemoveDavFileInfoL(const TDesC& aPath)
       
    97     {
       
    98     TInt index = DavFileInfoIndexL(aPath);
       
    99     if (index != KErrNotFound)
       
   100         {
       
   101         CRsfwDavFileInfo* davFileInfo =  iDavFileInfos[index];
       
   102 #ifdef _DEBUG
       
   103         TPtrC namePtr;
       
   104         if (davFileInfo->Name())
       
   105             {
       
   106             namePtr.Set(*davFileInfo->Name());
       
   107             }
       
   108         DEBUGSTRING16(("Remove file info: name='%S'", &namePtr));
       
   109 #endif // DEBUG
       
   110         iDavFileInfos.Remove(index);
       
   111         delete davFileInfo;
       
   112         }
       
   113     }
       
   114 
       
   115 // ----------------------------------------------------------------------------
       
   116 // CRsfwDavAccess::SetupL
       
   117 // Setup - should be immediately followed by NewL() call.
       
   118 // ----------------------------------------------------------------------------
       
   119 //
       
   120 void CRsfwDavAccess::SetupL(MRsfwRemoteAccessObserver* aRsfwRemoteAccessObserver)
       
   121     {
       
   122     DEBUGSTRING(("DAV: SetupL"));
       
   123     MRsfwConnectionObserver* rsfwConnectionObserver = NULL;
       
   124     if (aRsfwRemoteAccessObserver)
       
   125         {
       
   126         // Cascade remote access observers
       
   127         iRsfwRemoteAccessObserver = aRsfwRemoteAccessObserver;
       
   128         rsfwConnectionObserver = this;
       
   129         }
       
   130     iWebDavSession = CRsfwDavSession::NewL(this, rsfwConnectionObserver);
       
   131     }
       
   132 
       
   133 // ----------------------------------------------------------------------------
       
   134 // CRsfwDavAccess::OpenL
       
   135 // ----------------------------------------------------------------------------
       
   136 //
       
   137 TUint CRsfwDavAccess::OpenL(const TUriC& aUri,
       
   138                         const TDesC& /*aFriendlyName*/,
       
   139                         const TDesC& aUserName,
       
   140                         const TDesC& aPassword,
       
   141                         const TDesC& aAuxData,
       
   142                         MRsfwRemoteAccessResponseHandler* aResponseHandler)
       
   143     {
       
   144     if (!aResponseHandler)
       
   145         {
       
   146         User::Leave(KErrArgument);
       
   147         }
       
   148 
       
   149     HBufC* url = HBufC::NewLC(KHttpsScheme().Length() +
       
   150                               KMaxServerNameLen +
       
   151                               1 +
       
   152                               KMaxPath);
       
   153     TPtr urlPtr = url->Des();
       
   154 
       
   155     TInt portNumber = 0;
       
   156     if (aUri.IsPresent(EUriPort))
       
   157         {
       
   158         TLex portx(aUri.Extract(EUriPort));
       
   159         if (portx.Val(portNumber) != KErrNone)
       
   160             {
       
   161             portNumber = 0;
       
   162             }
       
   163         }
       
   164 
       
   165     // Check scheme and map it to port number or vice versa
       
   166     TPtrC scheme;
       
   167     if (aUri.IsPresent(EUriScheme))
       
   168         {
       
   169         scheme.Set(aUri.Extract(EUriScheme));
       
   170         }
       
   171     if (scheme.Length())
       
   172         {
       
   173         if (portNumber == 0)
       
   174             {
       
   175             if (scheme.CompareF(KHttpsScheme) == 0)
       
   176                 {
       
   177                 portNumber = KHttpsPortNumber;
       
   178                 }
       
   179             else
       
   180                 {
       
   181                 portNumber = KHttpPortNumber;
       
   182                 }
       
   183             }
       
   184         }
       
   185     else
       
   186         {
       
   187         if (portNumber == 0)
       
   188             {
       
   189             portNumber = KHttpPortNumber;
       
   190             }
       
   191         if (portNumber == KHttpPortNumber)
       
   192             {
       
   193             scheme.Set(KHttpScheme);
       
   194             }
       
   195         else if (portNumber == KHttpsPortNumber)
       
   196             {
       
   197             scheme.Set(KHttpsScheme);
       
   198             }
       
   199         else
       
   200             {
       
   201             User::Leave(KErrBadName);
       
   202             }
       
   203         }
       
   204         
       
   205     TPtrC rootDirectory;
       
   206     if (aUri.IsPresent(EUriPath))
       
   207         {
       
   208         rootDirectory.Set(aUri.Extract(EUriPath));
       
   209         }
       
   210     iRootDirectory.Copy(rootDirectory);
       
   211     if (!iRootDirectory.Length() ||
       
   212         iRootDirectory[iRootDirectory.Length() - 1] != '/')
       
   213         {
       
   214         // Append trailing '/'
       
   215         iRootDirectory.Append('/');
       
   216         }
       
   217     
       
   218     urlPtr.Copy(scheme);
       
   219     urlPtr.Append(':');
       
   220     urlPtr.Append('/');
       
   221     urlPtr.Append('/');
       
   222     urlPtr.Append(aUri.Extract(EUriHost));
       
   223     // There needs to be a slash between server name and the root dir
       
   224     // (we assume that there cannot be an excess of slash characters)
       
   225     if (urlPtr[urlPtr.Length() - 1] != '/')
       
   226         {
       
   227         if (!iRootDirectory.Length() || (iRootDirectory[0] != '/'))
       
   228             {
       
   229             urlPtr.Append('/');
       
   230             }
       
   231         }
       
   232     urlPtr.Append(iRootDirectory);
       
   233     
       
   234     DEBUGSTRING16(("DAV: OpenL to URL '%S'", &urlPtr));
       
   235     
       
   236     iWebDavSession->OpenL(urlPtr,
       
   237                           portNumber,
       
   238                           aUserName,
       
   239                           aPassword,
       
   240                           aAuxData);
       
   241     CleanupStack::PopAndDestroy(url);
       
   242     return OptionsL(aResponseHandler);
       
   243     }
       
   244 
       
   245 // ----------------------------------------------------------------------------
       
   246 // CRsfwDavAccess::GetDirectoryL
       
   247 // ----------------------------------------------------------------------------
       
   248 //
       
   249 TUint CRsfwDavAccess::GetDirectoryL(const TDesC& aPathName,
       
   250                                 RPointerArray<CRsfwDirEnt>& aDirEnts,
       
   251                                 MRsfwRemoteAccessResponseHandler* aResponseHandler)
       
   252     {
       
   253     // Get the contents of the directory
       
   254     DEBUGSTRING16(("DAV: GetDirectory '%S'", &aPathName));
       
   255 
       
   256     // check that arguments are sensible
       
   257     // aPathName might here be null (mounted root directory)
       
   258     if (aPathName.Length() > KMaxPath)
       
   259         {
       
   260         User::Leave(KErrBadName);
       
   261         }
       
   262 
       
   263     if (!aResponseHandler)
       
   264         {
       
   265         User::Leave(KErrArgument);
       
   266         }
       
   267 
       
   268     CRsfwDavAccessContextPropFindDir* davAccessContextPropFindDir =
       
   269         CRsfwDavAccessContextPropFindDir::NewL(this,
       
   270                                            aResponseHandler,
       
   271                                            aPathName,
       
   272                                            1,
       
   273                                            NULL,
       
   274                                            &aDirEnts);
       
   275     TUint id = AddAccessContext(davAccessContextPropFindDir);
       
   276     davAccessContextPropFindDir->StartL();
       
   277     return id;
       
   278     }
       
   279 
       
   280 // ----------------------------------------------------------------------------
       
   281 // CRsfwDavAccess::GetFileL
       
   282 // ----------------------------------------------------------------------------
       
   283 //
       
   284 TUint CRsfwDavAccess::GetFileL(const TDesC& aRemotePathName,
       
   285                            const TDesC& aLocalPathName,
       
   286                            TInt aOffset,
       
   287                            TInt* aLength,
       
   288                            TUint aFlags,
       
   289                            MRsfwRemoteAccessResponseHandler* aResponseHandler)
       
   290     {
       
   291 
       
   292 #ifdef _DEBUG
       
   293     {
       
   294     TInt length;
       
   295     if (aLength)
       
   296         {
       
   297         length = *aLength;
       
   298         }
       
   299     else
       
   300         {
       
   301         length = 0;
       
   302         }
       
   303     DEBUGSTRING16(("DAV: GetFile rn='%S', ln='%S' (off=%d, len=%d)",
       
   304                    &aRemotePathName,
       
   305                    &aLocalPathName,
       
   306                    aOffset,
       
   307                    length));
       
   308     }
       
   309 #endif // DEBUG
       
   310 
       
   311     // check that arguments are sensible
       
   312     if (aOffset < 0 ||
       
   313        (aLength && *aLength < 0) ||
       
   314        (!aResponseHandler))
       
   315         {
       
   316         User::Leave(KErrArgument);
       
   317         }
       
   318 
       
   319     if ((aLocalPathName.Length() == 0) ||
       
   320         (aLocalPathName.Length() > KMaxPath) ||
       
   321         (aRemotePathName.Length() == 0) ||
       
   322         (aRemotePathName.Length() > KMaxPath))
       
   323         {
       
   324         User::Leave(KErrBadName);
       
   325         }
       
   326 
       
   327     CRsfwDavAccessContextGet* davAccessContextGet =
       
   328         CRsfwDavAccessContextGet::NewL(this,
       
   329                                    aResponseHandler,
       
   330                                    aRemotePathName,
       
   331                                    aLocalPathName,
       
   332                                    aOffset,
       
   333                                    aLength,
       
   334                                    aFlags);
       
   335     TUint id = AddAccessContext(davAccessContextGet);
       
   336     davAccessContextGet->StartL();
       
   337     return id;
       
   338     }
       
   339 
       
   340 // ----------------------------------------------------------------------------
       
   341 // CRsfwDavAccess::PutFileL
       
   342 // ----------------------------------------------------------------------------
       
   343 //
       
   344 TUint CRsfwDavAccess::PutFileL(const TDesC& aLocalPathName,
       
   345                            const TDesC& aRemotePathName,
       
   346                            const TDesC8& aMimeType,
       
   347                            TInt aOffset,
       
   348                            TInt aLength,
       
   349                            TInt aTotalLength,
       
   350                            MRsfwRemoteAccessResponseHandler* aResponseHandler)
       
   351     {
       
   352     // Copy file to server using PUT,
       
   353     // might require lock token, if the destination file is locked...
       
   354 #ifdef _DEBUG
       
   355     {
       
   356     TInt length;
       
   357     if (aLength)
       
   358         {
       
   359         length = aLength;
       
   360         }
       
   361     else
       
   362         {
       
   363         length = 0;
       
   364         }
       
   365     DEBUGSTRING16(("DAV: PutFile ln='%S', rn='%S' (off=%d, len=%d)",
       
   366                    &aLocalPathName,
       
   367                    &aRemotePathName,
       
   368                    aOffset,
       
   369                    length));
       
   370     }
       
   371 #endif // DEBUG
       
   372 
       
   373     // check that arguments are sensible
       
   374     if (aOffset < 0 ||
       
   375        (aLength < 0) ||
       
   376        (((aOffset + aLength) > aTotalLength) && aTotalLength > 0) ||
       
   377        (!aResponseHandler))
       
   378         {
       
   379         User::Leave(KErrArgument);
       
   380         }
       
   381 
       
   382     // note that aLocalPathName can be undefined
       
   383     // (CreateFile calls with null-ptr)
       
   384     if ((aLocalPathName.Length() > KMaxPath) ||
       
   385         (aRemotePathName.Length() == 0) ||
       
   386         (aRemotePathName.Length() > KMaxPath) ||
       
   387         (aMimeType.Length() > KMaxMimeTypeLength))
       
   388         {
       
   389         User::Leave(KErrBadName);
       
   390         }
       
   391 
       
   392 
       
   393     const HBufC8* lockToken = NULL;
       
   394     CRsfwDavFileInfo *davFileInfo = DavFileInfoL(aRemotePathName);
       
   395     if (davFileInfo)
       
   396         {
       
   397         lockToken = davFileInfo->LockToken();
       
   398         }
       
   399     CRsfwDavAccessContextPut* davAccessContextPut =
       
   400         CRsfwDavAccessContextPut::NewL(this,
       
   401                                    aResponseHandler,
       
   402                                    aLocalPathName,
       
   403                                    aRemotePathName,
       
   404                                    aMimeType,
       
   405                                    aOffset,
       
   406                                    aLength,
       
   407                                    aTotalLength,
       
   408                                    lockToken);
       
   409     TUint id = AddAccessContext(davAccessContextPut);
       
   410     davAccessContextPut->StartL();
       
   411     return id;
       
   412     }
       
   413 
       
   414 // ----------------------------------------------------------------------------
       
   415 // CRsfwDavAccess::PutFileL
       
   416 // ----------------------------------------------------------------------------
       
   417 //
       
   418 TUint CRsfwDavAccess::PutFileL(const TDesC& aLocalPathName,
       
   419                            const TDesC& aRemotePathName,
       
   420                            const TDesC8& aMimeType,
       
   421                            MRsfwRemoteAccessResponseHandler* aResponseHandler)
       
   422     {
       
   423     return PutFileL(aLocalPathName,
       
   424                     aRemotePathName,
       
   425                     aMimeType,
       
   426                     0,
       
   427                     NULL,
       
   428                     0,
       
   429                     aResponseHandler);
       
   430     }
       
   431 
       
   432 // ----------------------------------------------------------------------------
       
   433 // CRsfwDavAccess::CreateFileL
       
   434 // overwriting info not needed in HTTP PUT
       
   435 // ----------------------------------------------------------------------------
       
   436 //
       
   437 TUint CRsfwDavAccess::CreateFileL(const TDesC& aPathName,
       
   438                               TBool /*aOverWriting*/,
       
   439                               MRsfwRemoteAccessResponseHandler* aResponseHandler)
       
   440     {
       
   441     DEBUGSTRING16(("DAV: CreateFile '%S'", &aPathName));
       
   442     // check that arguments are sensible
       
   443     if (!aResponseHandler)
       
   444         {
       
   445         User::Leave(KErrArgument);
       
   446         }
       
   447 
       
   448     if ((aPathName.Length() == 0) ||
       
   449        (aPathName.Length() >  KMaxPath))
       
   450         {
       
   451         User::Leave(KErrBadName);
       
   452         }
       
   453         
       
   454     // Could create the file only from LOCK,
       
   455     // but that seems to cause 500 Internal Error with several
       
   456     // Apache servers...
       
   457     TPtrC null;
       
   458     return PutFileL(null, aPathName, KTextPlain, 0, NULL, 0, aResponseHandler);
       
   459     }
       
   460 
       
   461 // ----------------------------------------------------------------------------
       
   462 // CRsfwDavAccess::MakeDirectoryL
       
   463 // ----------------------------------------------------------------------------
       
   464 //
       
   465 TUint CRsfwDavAccess::MakeDirectoryL(
       
   466     const TDesC& aPathName,
       
   467     MRsfwRemoteAccessResponseHandler* aResponseHandler)
       
   468     {
       
   469     DEBUGSTRING16(("DAV: MakeDirectory", &aPathName));
       
   470 
       
   471     // check that arguments are sensible
       
   472     if (!aResponseHandler)
       
   473         {
       
   474         User::Leave(KErrArgument);
       
   475         }
       
   476 
       
   477     if ((aPathName.Length() == 0) ||
       
   478        (aPathName.Length() >  KMaxPath))
       
   479         {
       
   480         User::Leave(KErrBadName);
       
   481         }
       
   482 
       
   483     CRsfwDavAccessContextMkDir* davAccessContextMkDir =
       
   484         CRsfwDavAccessContextMkDir::NewL(this,
       
   485                                      aResponseHandler,
       
   486                                      aPathName);
       
   487     TUint id = AddAccessContext(davAccessContextMkDir);
       
   488     davAccessContextMkDir->StartL();
       
   489     return id;
       
   490     }
       
   491 
       
   492 // ----------------------------------------------------------------------------
       
   493 // CRsfwDavAccess::DeleteDirectoryL
       
   494 // ----------------------------------------------------------------------------
       
   495 //
       
   496 TUint CRsfwDavAccess::DeleteDirectoryL(
       
   497     const TDesC& aPathName,
       
   498     MRsfwRemoteAccessResponseHandler* aResponseHandler)
       
   499     {
       
   500     DEBUGSTRING16(("DAV: DeleteDirectory '%S'", &aPathName));
       
   501 
       
   502     // check that arguments are sensible
       
   503     if (!aResponseHandler)
       
   504         {
       
   505         User::Leave(KErrArgument);
       
   506         }
       
   507 
       
   508     if ((aPathName.Length() == 0) ||
       
   509        (aPathName.Length() >  KMaxPath))
       
   510         {
       
   511         User::Leave(KErrBadName);
       
   512         }
       
   513 
       
   514     CRsfwDavAccessContextDelete* davAccessContextDelete =
       
   515         CRsfwDavAccessContextDelete::NewL(this,
       
   516                                       aResponseHandler,
       
   517                                       aPathName,
       
   518                                       ETrue,
       
   519                                       NULL);
       
   520     TUint id = AddAccessContext(davAccessContextDelete);
       
   521     davAccessContextDelete->StartL();
       
   522     return id;
       
   523     }
       
   524 
       
   525 // ----------------------------------------------------------------------------
       
   526 // CRsfwDavAccess::DeleteFileL
       
   527 // ----------------------------------------------------------------------------
       
   528 //
       
   529 TUint CRsfwDavAccess::DeleteFileL(const TDesC& aPathName,
       
   530                               MRsfwRemoteAccessResponseHandler* aResponseHandler)
       
   531     {
       
   532     DEBUGSTRING16(("DAV: DeleteFile '%S'", &aPathName));
       
   533 
       
   534     // check that arguments are sensible
       
   535     if (!aResponseHandler)
       
   536         {
       
   537         User::Leave(KErrArgument);
       
   538         }
       
   539 
       
   540     if ((aPathName.Length() == 0) ||
       
   541        (aPathName.Length() >  KMaxPath))
       
   542         {
       
   543         User::Leave(KErrBadName);
       
   544         }
       
   545 
       
   546     const HBufC8* lockToken = NULL;
       
   547     CRsfwDavFileInfo *davFileInfo = DavFileInfoL(aPathName);
       
   548     if (davFileInfo)
       
   549         {
       
   550         lockToken = davFileInfo->LockToken();
       
   551         }
       
   552     CRsfwDavAccessContextDelete* davAccessContextDelete =
       
   553         CRsfwDavAccessContextDelete::NewL(this,
       
   554                                       aResponseHandler,
       
   555                                       aPathName,
       
   556                                       EFalse,
       
   557                                       lockToken);
       
   558     TUint id = AddAccessContext(davAccessContextDelete);
       
   559     davAccessContextDelete->StartL();
       
   560     return id;
       
   561     }
       
   562 
       
   563 // ----------------------------------------------------------------------------
       
   564 // CRsfwDavAccess::RenameL
       
   565 // ----------------------------------------------------------------------------
       
   566 //
       
   567 TUint CRsfwDavAccess::RenameL(const TDesC& aSrcPathName,
       
   568                           const TDesC& aDstPathName,
       
   569                           TBool aOverwrite,
       
   570                           MRsfwRemoteAccessResponseHandler* aResponseHandler)
       
   571     {
       
   572     DEBUGSTRING16(("DAV: Rename '%S' to '%S'", &aSrcPathName, &aDstPathName));
       
   573 
       
   574      // check that arguments are sensible
       
   575     if (!aResponseHandler)
       
   576         {
       
   577         User::Leave(KErrArgument);
       
   578         }
       
   579 
       
   580     if ((aSrcPathName.Length() == 0) ||
       
   581         (aSrcPathName.Length() >  KMaxPath) ||
       
   582         (aDstPathName.Length() == 0) ||
       
   583         (aDstPathName.Length() >  KMaxPath))
       
   584         {
       
   585         User::Leave(KErrBadName);
       
   586         }
       
   587 
       
   588     // lock token for the source file, if the source is locked...
       
   589     const HBufC8* lockToken = NULL;
       
   590     CRsfwDavFileInfo* davFileInfo = DavFileInfoL(aSrcPathName);
       
   591     if (davFileInfo)
       
   592         {
       
   593         lockToken = davFileInfo->LockToken();
       
   594         }
       
   595         
       
   596     
       
   597     // lock token for the destination file, if the destination is locked
       
   598     const HBufC8* destLockToken = NULL;
       
   599     CRsfwDavFileInfo* destDavFileInfo = DavFileInfoL(aDstPathName);
       
   600     if (destDavFileInfo)
       
   601         {
       
   602         destLockToken = destDavFileInfo->LockToken();
       
   603         }
       
   604     
       
   605         
       
   606     CRsfwDavAccessContextMove* davAccessContextMove =
       
   607         CRsfwDavAccessContextMove::NewL(this,
       
   608                                     aResponseHandler,
       
   609                                     aSrcPathName,
       
   610                                     aDstPathName,
       
   611                                     aOverwrite,
       
   612                                     lockToken,
       
   613                                     destLockToken);
       
   614     TUint id = AddAccessContext(davAccessContextMove);
       
   615     davAccessContextMove->StartL();
       
   616     return id;
       
   617     }
       
   618 
       
   619 // ----------------------------------------------------------------------------
       
   620 // CRsfwDavAccess::GetDirectoryAttributesL
       
   621 // ----------------------------------------------------------------------------
       
   622 //
       
   623 TUint CRsfwDavAccess::GetDirectoryAttributesL(
       
   624     const TDesC& aPathName,
       
   625     CRsfwDirEntAttr*& aAttr,
       
   626     MRsfwRemoteAccessResponseHandler* aResponseHandler)
       
   627     {
       
   628     DEBUGSTRING16(("DAV: GetDirectoryAttributes of '%S'",
       
   629                    &aPathName));
       
   630 
       
   631     // check that arguments are sensible
       
   632     // aPathName might here be null (mounted root directory)
       
   633     if (!aResponseHandler)
       
   634         {
       
   635         User::Leave(KErrArgument);
       
   636         }
       
   637 
       
   638     if (aPathName.Length() > KMaxPath)
       
   639         {
       
   640         User::Leave(KErrBadName);
       
   641         }
       
   642 
       
   643     CRsfwDavAccessContextPropFindDir* davAccessContextPropFindDir =
       
   644         CRsfwDavAccessContextPropFindDir::NewL(this,
       
   645                                            aResponseHandler,
       
   646                                            aPathName,
       
   647                                            0,
       
   648                                            &aAttr,
       
   649                                            NULL);
       
   650     TUint id = AddAccessContext(davAccessContextPropFindDir);
       
   651     davAccessContextPropFindDir->StartL();
       
   652     return id;
       
   653     }
       
   654 
       
   655 // ----------------------------------------------------------------------------
       
   656 // CRsfwDavAccess::GetFileAttributesL
       
   657 // ----------------------------------------------------------------------------
       
   658 //
       
   659 TUint
       
   660 CRsfwDavAccess::GetFileAttributesL(const TDesC& aPathName,
       
   661                                CRsfwDirEntAttr*& aAttr,
       
   662                                MRsfwRemoteAccessResponseHandler* aResponseHandler)
       
   663     {
       
   664     DEBUGSTRING16(("DAV: GetFileAttributes of '%S'",
       
   665                    &aPathName));
       
   666 
       
   667     // check that arguments are sensible
       
   668     if (!aResponseHandler)
       
   669         {
       
   670         User::Leave(KErrArgument);
       
   671         }
       
   672 
       
   673     if ((aPathName.Length() == 0) ||
       
   674        (aPathName.Length() >  KMaxPath))
       
   675         {
       
   676         User::Leave(KErrBadName);
       
   677         }
       
   678 
       
   679     CRsfwDavAccessContextPropFindFile* davAccessContextPropFindFile =
       
   680         CRsfwDavAccessContextPropFindFile::NewL(this,
       
   681                                             aResponseHandler,
       
   682                                             aPathName,
       
   683                                             &aAttr);
       
   684     TUint id = AddAccessContext(davAccessContextPropFindFile);
       
   685     davAccessContextPropFindFile->StartL();
       
   686     return id;
       
   687     }
       
   688 
       
   689 // ----------------------------------------------------------------------------
       
   690 // CRsfwDavAccess::SetAttributesL
       
   691 // ----------------------------------------------------------------------------
       
   692 //
       
   693 TUint CRsfwDavAccess::SetAttributesL(
       
   694     const TDesC& /* aPathName */,
       
   695     CRsfwDirEntAttr& /* aAttr */,
       
   696     MRsfwRemoteAccessResponseHandler* /* aResponseHandler */)
       
   697     {
       
   698     DEBUGSTRING(("DAV: SetAttributes"));
       
   699     User::Leave(KErrNotSupported);
       
   700     // Not reached
       
   701     return 0;
       
   702     }
       
   703 
       
   704 // ----------------------------------------------------------------------------
       
   705 // CRsfwDavAccess::ObtainLockL
       
   706 // ----------------------------------------------------------------------------
       
   707 //
       
   708 TUint CRsfwDavAccess::ObtainLockL(const TDesC& aPathName,
       
   709                               TUint aLockFlags,
       
   710                               TUint& aTimeout,
       
   711                               TDesC8*& aLockToken,
       
   712                               MRsfwRemoteAccessResponseHandler* aResponseHandler)
       
   713     {
       
   714     // check that arguments are sensible
       
   715     if ((!aResponseHandler) ||
       
   716         (aTimeout == 0))
       
   717         {
       
   718         User::Leave(KErrArgument);
       
   719         }
       
   720 
       
   721     // CRsfwDavAccess is the place to know about webdab locks
       
   722     // We only try to obtain a lock if the file was opened for writing
       
   723     if ((!(aLockFlags & EFileWrite)) ||
       
   724         (iWebDavSession->WebDavSupportClass() < KDavVersionTwo))
       
   725         {
       
   726         // WebDAV doesn't have read locks
       
   727         aResponseHandler->HandleRemoteAccessResponse(0, KErrNotSupported); 
       
   728         return 0;
       
   729         }
       
   730     
       
   731     if ((aPathName.Length() == 0) ||
       
   732        (aPathName.Length() >  KMaxPath))
       
   733         {
       
   734         User::Leave(KErrBadName);
       
   735         }
       
   736 
       
   737     CRsfwDavAccessContextLock* davAccessContextLock =
       
   738         CRsfwDavAccessContextLock::NewL(this,
       
   739                                     aResponseHandler,
       
   740                                     aPathName,
       
   741                                     aLockFlags,
       
   742                                     aTimeout,
       
   743                                     &aLockToken);
       
   744     TUint id = AddAccessContext(davAccessContextLock);
       
   745     davAccessContextLock->StartL();
       
   746     return id;
       
   747     }
       
   748 
       
   749 // ----------------------------------------------------------------------------
       
   750 // CRsfwDavAccess::ReleaseLockL
       
   751 // ----------------------------------------------------------------------------
       
   752 //
       
   753 TUint CRsfwDavAccess::ReleaseLockL(const TDesC& aPathName,
       
   754                                MRsfwRemoteAccessResponseHandler* aResponseHandler)
       
   755     {
       
   756     /*
       
   757       Precondition:
       
   758       - File has been at least locked so there must be fileInfo and
       
   759       locktoken for it
       
   760     */
       
   761     // check that arguments are sensible
       
   762     if (!aResponseHandler)
       
   763         {
       
   764         User::Leave(KErrArgument);
       
   765         }
       
   766 
       
   767     if ((aPathName.Length() == 0) ||
       
   768        (aPathName.Length() >  KMaxPath))
       
   769         {
       
   770         User::Leave(KErrBadName);
       
   771         }
       
   772            
       
   773     // Must send the Lock Token
       
   774     CRsfwDavFileInfo* davFileInfo = DavFileInfoL(aPathName);
       
   775     if (!davFileInfo)
       
   776         {
       
   777         User::Leave(KErrNotFound);
       
   778         }
       
   779 
       
   780     const HBufC8* lockToken = davFileInfo->LockToken();
       
   781     if (!lockToken)
       
   782         {
       
   783         User::Leave(KErrNotFound);
       
   784         }
       
   785     // Prevent further access to lock token
       
   786     davFileInfo->SetFlag(TRsfwDavFileInfoFlags::EUnlockPending);
       
   787 
       
   788     CRsfwDavAccessContextUnlock* davAccessContextUnlock =
       
   789         CRsfwDavAccessContextUnlock::NewL(this,
       
   790                                       aResponseHandler,
       
   791                                       aPathName,
       
   792                                       lockToken);
       
   793     TUint id = AddAccessContext(davAccessContextUnlock);
       
   794     davAccessContextUnlock->StartL();
       
   795     return id;
       
   796     }
       
   797 
       
   798 // ----------------------------------------------------------------------------
       
   799 // CRsfwDavAccess::RefreshLockL
       
   800 // ----------------------------------------------------------------------------
       
   801 //
       
   802 TUint CRsfwDavAccess::RefreshLockL(const TDesC& aPathName,
       
   803                                TUint& aTimeout,
       
   804                                MRsfwRemoteAccessResponseHandler* aResponseHandler)
       
   805 
       
   806     {
       
   807     /*
       
   808       Precondition:
       
   809       - File has been at least locked
       
   810       so there must be fileInfo and locktoken for it
       
   811     */
       
   812     // check that arguments are sensible
       
   813     if ((!aResponseHandler) ||
       
   814         (aTimeout == 0))
       
   815         {
       
   816         User::Leave(KErrArgument);
       
   817         }
       
   818 
       
   819     if ((aPathName.Length() == 0) ||
       
   820        (aPathName.Length() >  KMaxPath))
       
   821         {
       
   822         User::Leave(KErrBadName);
       
   823         }
       
   824 
       
   825 
       
   826     // Must send the Lock Token
       
   827     CRsfwDavFileInfo* davFileInfo = DavFileInfoL(aPathName);
       
   828     if (!davFileInfo)
       
   829         {
       
   830         User::Leave(KErrNotFound);
       
   831         }
       
   832     const HBufC8* lockToken = davFileInfo->LockToken();
       
   833     if (!lockToken)
       
   834         {
       
   835         User::Leave(KErrNotFound);
       
   836         }
       
   837 
       
   838     CRsfwDavAccessContextRefreshLock* davAccessContextRefreshLock =
       
   839         CRsfwDavAccessContextRefreshLock::NewL(this,
       
   840                                            aResponseHandler,
       
   841                                            aPathName,
       
   842                                            lockToken,
       
   843                                            aTimeout);
       
   844     TUint id = AddAccessContext(davAccessContextRefreshLock);
       
   845     davAccessContextRefreshLock->StartL();
       
   846     return id;
       
   847     }
       
   848 
       
   849 // ----------------------------------------------------------------------------
       
   850 // CRsfwDavAccess::CancelL by ID
       
   851 // ----------------------------------------------------------------------------
       
   852 //
       
   853 void CRsfwDavAccess::Cancel(TUint aId)
       
   854     {
       
   855     TInt i;
       
   856     if (aId)
       
   857         {
       
   858         i = LookupAccessContextByContextId(aId);
       
   859         if (i != KErrNotFound)
       
   860             {
       
   861             CRsfwDavAccessContext* context = iDavAccessContexts[i];
       
   862             // sometimes it may happen that transaction has not been created for 
       
   863             // some context (e.g. if a leave has occured)
       
   864             if (context->WebDavTransaction())
       
   865                 {
       
   866                 context->WebDavTransaction()->Cancel();
       
   867                 }
       
   868             else
       
   869                 {
       
   870                 iDavAccessContexts.Remove(i);
       
   871                 delete context;                
       
   872                 }                        
       
   873             }
       
   874         }
       
   875     else
       
   876         {
       
   877         // Cancel all pending transactions.
       
   878         // Note that cancelling one transaction may result in creating the other
       
   879         // e.g. if you cancel PUT, then RELEASE LOCK is generated.
       
   880         // Anyway the purpose here is to 'kill' all of them, even those generated later
       
   881         DEBUGSTRING(("Cancelling all pending transactions..."));
       
   882         while (iDavAccessContexts.Count() > 0)
       
   883             {
       
   884             CRsfwDavAccessContext* context = iDavAccessContexts[0];
       
   885             TUint id = context->Id();
       
   886             Cancel(id);    
       
   887             }
       
   888         }
       
   889     }
       
   890 
       
   891 // ----------------------------------------------------------------------------
       
   892 // CRsfwDavAccess::CancelL by path
       
   893 // ----------------------------------------------------------------------------
       
   894 //
       
   895 void CRsfwDavAccess::Cancel(TDesC& aTargetPath)
       
   896     {
       
   897     DEBUGSTRING16(("CRsfwDavAccess::Cancel '%S'", &aTargetPath));
       
   898     TInt i;
       
   899     i = LookupAccessContextByPath(aTargetPath);
       
   900     if (i != KErrNotFound)
       
   901         {
       
   902         DEBUGSTRING16(("found transaction....cancelling webdavop %d", 
       
   903                     iDavAccessContexts[i]->WebDavTransaction()->iWebDavOp));
       
   904         iDavAccessContexts[i]->WebDavTransaction()->Cancel();
       
   905         }
       
   906     }
       
   907 
       
   908 
       
   909 
       
   910 // ----------------------------------------------------------------------------
       
   911 // CRsfwDavAccess::SetLockToken
       
   912 // ----------------------------------------------------------------------------
       
   913 //
       
   914 TInt CRsfwDavAccess::SetLockToken(const TDesC& aPathName,
       
   915                               const TDesC8& aLockToken)
       
   916     {
       
   917     TRAPD(err, SetLockTokenL(aPathName, aLockToken));
       
   918     return err;
       
   919     }
       
   920 
       
   921 // ----------------------------------------------------------------------------
       
   922 // CRsfwDavAccess::SetLockTokenL
       
   923 // ----------------------------------------------------------------------------
       
   924 //
       
   925 void CRsfwDavAccess::SetLockTokenL(const TDesC& aPathName,
       
   926                                const TDesC8& aLockToken)
       
   927     {
       
   928     HBufC* path = HBufC::NewLC(iRootDirectory.Length() + KMaxPath + 1);
       
   929     TPtr pathPtr = path->Des();
       
   930     pathPtr.Copy(iRootDirectory);
       
   931     pathPtr.Append(aPathName);
       
   932 
       
   933     DEBUGSTRING16(("DAV: setting lock token: path = '%S'", &pathPtr));
       
   934     DEBUGSTRING8(("                        token = '%S'", &aLockToken));
       
   935     
       
   936     if (aLockToken.Length())
       
   937         {
       
   938         CRsfwDavFileInfo* fileInfo = DavFileInfoL(pathPtr);
       
   939         if (fileInfo)
       
   940             {
       
   941             fileInfo->SetLockTokenL(aLockToken);
       
   942             }
       
   943         else 
       
   944             {
       
   945             // new file
       
   946             fileInfo = CRsfwDavFileInfo::NewL();
       
   947             CleanupStack::PushL(fileInfo);
       
   948             fileInfo->SetNameL(pathPtr);
       
   949             fileInfo->SetLockTokenL(aLockToken);
       
   950             CleanupStack::Pop(fileInfo);
       
   951             // Ownership transferred to file info array
       
   952             AddDavFileInfo(fileInfo);
       
   953             }
       
   954         }
       
   955     else
       
   956         {
       
   957         // Remove lock token
       
   958         RemoveDavFileInfoL(pathPtr);
       
   959         }
       
   960 
       
   961     CleanupStack::PopAndDestroy(path);
       
   962     }
       
   963 
       
   964 // ----------------------------------------------------------------------------
       
   965 // From MRsfwDavResponseObserver
       
   966 // ----------------------------------------------------------------------------
       
   967 
       
   968 void CRsfwDavAccess::RequestCompleteL(TUint aWebDavTransactionId)
       
   969     {
       
   970     TInt i = LookupAccessContextByTransactionId(aWebDavTransactionId);
       
   971     if (i != KErrNotFound)
       
   972         {
       
   973         CRsfwDavAccessContext* context = iDavAccessContexts[i];
       
   974         DEBUGSTRING(("DAV: Request %d complete", aWebDavTransactionId));
       
   975         context->TransactionCompleteL();
       
   976         if (context->Done())
       
   977             {
       
   978             MRsfwRemoteAccessResponseHandler* responseHandler =
       
   979                 context->ResponseHandler();
       
   980             TUint id = context->Id();
       
   981             TInt status = context->Status();
       
   982             iDavAccessContexts.Remove(i);
       
   983             delete context;
       
   984             responseHandler->HandleRemoteAccessResponse(id, status);
       
   985             }
       
   986         }
       
   987     else
       
   988         {
       
   989         DEBUGSTRING(("DAV: Stray request (id=%d) complete",
       
   990                      aWebDavTransactionId));
       
   991         }
       
   992     }
       
   993 
       
   994 void CRsfwDavAccess::RequestError(TUint aWebDavTransactionId, TInt aStatus)
       
   995     {
       
   996     TInt i = LookupAccessContextByTransactionId(aWebDavTransactionId);
       
   997     if (i != KErrNotFound)
       
   998         {
       
   999         CRsfwDavAccessContext* context = iDavAccessContexts[i];
       
  1000         DEBUGSTRING(("DAV: Request %d error %d",
       
  1001                      aWebDavTransactionId,
       
  1002                      aStatus));
       
  1003         context->TransactionError(aStatus);
       
  1004         if (context->Done())
       
  1005             {
       
  1006             MRsfwRemoteAccessResponseHandler* responseHandler =
       
  1007                 context->ResponseHandler();
       
  1008             TUint id = context->Id();
       
  1009             TInt status = context->Status();
       
  1010             iDavAccessContexts.Remove(i);
       
  1011             delete context;
       
  1012             responseHandler->HandleRemoteAccessResponse(id, status);
       
  1013             }
       
  1014         }
       
  1015     else
       
  1016         {
       
  1017         DEBUGSTRING(("DAV: Stray request (id=%d) error",
       
  1018                      aWebDavTransactionId));
       
  1019         }
       
  1020     }
       
  1021 
       
  1022 // ----------------------------------------------------------------------------
       
  1023 // CRsfwDavAccess::AddAccessContext
       
  1024 // Add a context entry in the table of currently active contexts.
       
  1025 // ----------------------------------------------------------------------------
       
  1026 //
       
  1027 TUint CRsfwDavAccess::AddAccessContext(CRsfwDavAccessContext* aDavAccessContext)
       
  1028     {
       
  1029     TUint id = GetNextAccessContextId();
       
  1030     DEBUGSTRING(("DAV: Added transaction %d", id));
       
  1031     aDavAccessContext->SetId(id);
       
  1032     iDavAccessContexts.Append(aDavAccessContext);
       
  1033     return id;
       
  1034     }
       
  1035 
       
  1036 // ----------------------------------------------------------------------------
       
  1037 // CRsfwDavAccess::LookupAccessContextByTransactionId
       
  1038 // Find an access context for the given WebDAV transaction id.
       
  1039 // ----------------------------------------------------------------------------
       
  1040 //
       
  1041 TInt CRsfwDavAccess::LookupAccessContextByTransactionId(TUint aWebDavTransactionId)
       
  1042     {
       
  1043     TInt i;
       
  1044     for (i = 0; i < iDavAccessContexts.Count(); i++)
       
  1045         {
       
  1046         if (iDavAccessContexts[i]->WebDavTransactionId() ==
       
  1047             aWebDavTransactionId)
       
  1048             {
       
  1049             return i;
       
  1050             }
       
  1051         }
       
  1052     DEBUGSTRING(("Access context for id %d is missing !!!",
       
  1053                  aWebDavTransactionId));
       
  1054     return KErrNotFound;
       
  1055     }
       
  1056 
       
  1057 // ----------------------------------------------------------------------------
       
  1058 // CRsfwDavAccess::LookupAccessContext
       
  1059 // Find an access context having the given id.
       
  1060 // ----------------------------------------------------------------------------
       
  1061 //
       
  1062 TInt CRsfwDavAccess::LookupAccessContextByContextId(TUint aId)
       
  1063     {
       
  1064     TInt i;
       
  1065     for (i = 0; i < iDavAccessContexts.Count(); i++)
       
  1066         {
       
  1067         if (iDavAccessContexts[i]->Id() == aId)
       
  1068             {
       
  1069             return i;
       
  1070             }
       
  1071         }
       
  1072     DEBUGSTRING(("Access context for id %d is missing !!!", aId));
       
  1073     return KErrNotFound;
       
  1074     }
       
  1075 
       
  1076 // ----------------------------------------------------------------------------
       
  1077 // CRsfwDavAccess::LookupAccessContextByPath
       
  1078 // Find an access context based on the path of the targer file/directory.
       
  1079 // ----------------------------------------------------------------------------
       
  1080 //
       
  1081 TInt CRsfwDavAccess::LookupAccessContextByPath(TDesC& aTargetPath)
       
  1082     { 
       
  1083     TInt i;
       
  1084     for (i = 0; i < iDavAccessContexts.Count(); i++)
       
  1085         {
       
  1086         if (iDavAccessContexts[i]->TargetPath() == aTargetPath)
       
  1087             {
       
  1088             return i;
       
  1089             }
       
  1090         }
       
  1091     return KErrNotFound;
       
  1092     }
       
  1093 
       
  1094 
       
  1095 // ----------------------------------------------------------------------------
       
  1096 // CRsfwDavAccess::DavFileInfoIndexL
       
  1097 // Find the index to file info array for the given file name.
       
  1098 // ----------------------------------------------------------------------------
       
  1099 //
       
  1100 TInt CRsfwDavAccess::DavFileInfoIndexL(const TDesC& aName)
       
  1101     {
       
  1102     HBufC* path = HBufC::NewLC(iRootDirectory.Length() + KMaxPath + 1);
       
  1103     TPtr pathPtr = path->Des();
       
  1104     pathPtr.Copy(iRootDirectory);
       
  1105     pathPtr.Append(aName);
       
  1106 
       
  1107     DEBUGSTRING16(("Finding file info for '%S'", &pathPtr));
       
  1108     TInt index;
       
  1109     for (index = 0; index < iDavFileInfos.Count(); index++)
       
  1110         {
       
  1111         const HBufC* name = iDavFileInfos[index]->Name();
       
  1112         if (name)
       
  1113             {
       
  1114             if (pathPtr.Compare(*name) == 0)
       
  1115                 {
       
  1116                 DEBUGSTRING(("info found"));
       
  1117                 CleanupStack::PopAndDestroy(path);
       
  1118                 return index;
       
  1119                 }
       
  1120             }
       
  1121         }
       
  1122     CleanupStack::PopAndDestroy(path);
       
  1123     return KErrNotFound;
       
  1124     }
       
  1125 
       
  1126 // ----------------------------------------------------------------------------
       
  1127 // CRsfwDavAccess::OptionsL
       
  1128 // Perform an Options query
       
  1129 // ----------------------------------------------------------------------------
       
  1130 //
       
  1131 TUint CRsfwDavAccess::OptionsL(MRsfwRemoteAccessResponseHandler* aResponseHandler)
       
  1132     {
       
  1133     CRsfwDavAccessContextOptions* davAccessContextOptions =
       
  1134         CRsfwDavAccessContextOptions::NewL(this, aResponseHandler);
       
  1135     TUint id = AddAccessContext(davAccessContextOptions);
       
  1136     davAccessContextOptions->StartL();
       
  1137     return id;
       
  1138     }
       
  1139 
       
  1140 // ----------------------------------------------------------------------------
       
  1141 // From MRsfwConnectionObserver
       
  1142 // ----------------------------------------------------------------------------
       
  1143 
       
  1144 // ----------------------------------------------------------------------------
       
  1145 // CRsfwDavAccess::HandleConnectionEventL
       
  1146 // Perform an Options query
       
  1147 // ----------------------------------------------------------------------------
       
  1148 //
       
  1149 void CRsfwDavAccess::HandleConnectionEventL(TInt aConnectionEvent,
       
  1150                                         TAny* /*aArg*/)
       
  1151 
       
  1152     {
       
  1153     switch (aConnectionEvent)
       
  1154         {
       
  1155     case ERsfwConnectionObserverEventConnectionDisconnected:
       
  1156         if (iRsfwRemoteAccessObserver)
       
  1157             {
       
  1158             iRsfwRemoteAccessObserver->
       
  1159                 HandleRemoteAccessEventL(
       
  1160                     ERsfwRemoteAccessObserverEventConnection,
       
  1161                     ERsfwRemoteAccessObserverEventConnectionDisconnected,
       
  1162                     NULL);
       
  1163             }
       
  1164         break;
       
  1165         
       
  1166     case ERsfwConnectionObserverEventConnectionWeaklyConnected:
       
  1167         // If we were reacting to link up events, we would do OptionsL(this);
       
  1168         break;
       
  1169         
       
  1170     case ERsfwConnectionObserverEventConnectionStronglyConnected:
       
  1171         // If we were reacting to link up events, we would do OptionsL(this);
       
  1172         break;
       
  1173         
       
  1174     default:
       
  1175         break;
       
  1176         }
       
  1177     }
       
  1178 
       
  1179 //  End of File