mmfenh/progressivedownload/ProgressiveDownloadSource/src/fileaccess.cpp
changeset 0 71ca22bcf22a
equal deleted inserted replaced
-1:000000000000 0:71ca22bcf22a
       
     1 /*
       
     2 * Copyright (c) 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:  Progressive Download Utility
       
    15 *
       
    16 */
       
    17 
       
    18 #include <f32file.h>
       
    19 #include <e32std.h>
       
    20 #include <caf/attribute.h>
       
    21 #include <caf/bitset.h>
       
    22 #include <caf/content.h>
       
    23 #include <caf/data.h>
       
    24 using namespace ContentAccess;
       
    25 #include <f32file.h>
       
    26 
       
    27 #include "FileAccess.h"
       
    28 
       
    29 CF32File::~CF32File()
       
    30 	{
       
    31 	if (!iFileHandle)
       
    32 		iFile.Close();
       
    33 	delete iFilePath;
       
    34 	}
       
    35 
       
    36 CF32File* CF32File::NewL(RFs& aSession, TDesC& aFilePath, TUint aMode)
       
    37 	{
       
    38 	CF32File* self = new (ELeave) CF32File;
       
    39 	CleanupStack::PushL(self);
       
    40 	self->ConstructL(aSession, aFilePath, aMode);
       
    41 	CleanupStack::Pop(self);
       
    42 	return self;
       
    43 	}
       
    44 
       
    45 
       
    46 void CF32File::ConstructL(RFs& aSession, TDesC& aPath, TUint aFileMode)
       
    47 	{
       
    48 	iSession = &aSession;
       
    49 	iFilePath = aPath.AllocL();
       
    50 	TUint attributes = 0;
       
    51 	TInt err = iSession->Att(*iFilePath, attributes);
       
    52 	if (err == KErrNone)
       
    53 		{
       
    54 		if ( attributes & KEntryAttReadOnly )
       
    55 			User::LeaveIfError(iFile.Open(*iSession, *iFilePath, EFileShareAny )) ;
       
    56 		else //if ( !( attributes & KEntryAttReadOnly ) )
       
    57 			User::LeaveIfError(iFile.Open(*iSession, *iFilePath, EFileShareAny)) ;
       
    58 		}
       
    59 	else if ((err == KErrNotFound) && (aFileMode & EFileWrite))
       
    60 		{
       
    61 		User::LeaveIfError(iFile.Create(*iSession, *iFilePath, EFileShareAny )) ;
       
    62 		}
       
    63 	else
       
    64 		{
       
    65 		User::Leave(err);
       
    66 		}
       
    67 	}
       
    68 
       
    69 
       
    70 RFile& CF32File::FileL()
       
    71 	{
       
    72 	return iFile;
       
    73 	}
       
    74 
       
    75 TInt CF32File::Seek(TSeek aSeekMode, TInt aPos)
       
    76 	{
       
    77 	return iFile.Seek(aSeekMode, aPos);
       
    78 	}
       
    79 
       
    80 TInt CF32File::Read(TDes8& aDes,TInt aLength)
       
    81 	{
       
    82 	return iFile.Read(aDes, aLength);
       
    83 	}
       
    84 
       
    85 void CF32File::Read(TDes8& aDes, TInt aLength, TRequestStatus& aStatus)
       
    86 	{
       
    87 	iFile.Read(aDes, aLength, aStatus);
       
    88 	}
       
    89 
       
    90 TInt CF32File::Write(const TDesC8& aDes,TInt aLength)
       
    91 	{
       
    92 	return iFile.Write(aDes, aLength);
       
    93 	}
       
    94 
       
    95 void CF32File::Write(const TDesC8& aDes, TInt aLength, TRequestStatus& aStatus)
       
    96 	{
       
    97 	iFile.Write(aDes, aLength, aStatus);
       
    98 	}
       
    99 
       
   100 TInt CF32File::Size(TInt& aSize)
       
   101 	{
       
   102     #if _DEBUG
       
   103 	  RDebug::Print(_L("[%x]CF32File::Size to %d\n"),this,aSize);
       
   104     #endif
       
   105 
       
   106 	return iFile.Size(aSize);
       
   107 	}
       
   108 
       
   109 TInt CF32File::SetSize(TInt aSize)
       
   110 	{
       
   111 	#if _DEBUG
       
   112 	  RDebug::Print(_L("[%x]CF32File::SetSize to %d\n"),this,aSize);
       
   113     #endif
       
   114 
       
   115 
       
   116 	TInt err =  iFile.SetSize(aSize);
       
   117 	if(err == KErrNone)
       
   118 		iFileSize = aSize;
       
   119 	else
       
   120 		iFileSize = -1;
       
   121 
       
   122 	return err;
       
   123 	}
       
   124 
       
   125 TInt CF32File::EvaluateIntent(TIntent /*aIntent*/) const
       
   126 	{
       
   127 	return KErrNone;
       
   128 	}
       
   129 
       
   130 
       
   131 TInt CF32File::ExecuteIntent(TIntent /*aIntent*/)
       
   132 	{
       
   133 	return KErrNone;
       
   134 	}
       
   135 
       
   136 TBool CF32File::IsProtected() const
       
   137 	{
       
   138 	return EFalse;
       
   139 	}
       
   140 
       
   141 
       
   142 TInt CF32File::SetAgentProperty(TAgentProperty /*aProperty*/, TInt /*aValue*/)
       
   143 	{
       
   144 	// not an error to set this if not supported, just wont do anything
       
   145 	return KErrNone;
       
   146 	}
       
   147 
       
   148 
       
   149 CContentFile::~CContentFile()
       
   150 	{
       
   151 	delete iData;
       
   152 	delete iContent;
       
   153 
       
   154 	if (iLegacyFileOpen)
       
   155 		iLegacyFile.Close();
       
   156 	delete iFilePath;
       
   157 	}
       
   158 
       
   159 
       
   160 
       
   161 CContentFile* CContentFile::NewL(RFs& aSession, const TDesC& aFilePath, const TDesC& aUniqueId, TUint aMode)
       
   162 	{
       
   163 	CContentFile* self = new (ELeave) CContentFile;
       
   164 	CleanupStack::PushL(self);
       
   165 	self->ConstructL(aSession, aFilePath, aUniqueId, aMode);
       
   166 	CleanupStack::Pop(self);
       
   167 	return self;
       
   168 	}
       
   169 
       
   170 void CContentFile::ConstructL(RFs& aSession, const TDesC& aPath, const TDesC& aUniqueId, TUint /*aMode*/)
       
   171 	{
       
   172 	iSession = &aSession;
       
   173 	iFilePath = aPath.AllocL();
       
   174 
       
   175 	iContent = CContent::NewL(*iFilePath,EContentShareReadWrite);
       
   176 	if (aUniqueId.Length()>0)
       
   177 		{
       
   178 		iData = iContent->OpenContentL(EPeek, aUniqueId);//rj need mode for this one.
       
   179 		}
       
   180 	else
       
   181 		{
       
   182 		iData = iContent->OpenContentL(EPeek,EContentShareReadWrite);
       
   183 		}
       
   184 	}
       
   185 
       
   186 CContentFile* CContentFile::NewL(RFs& aSession, const TDesC& aFilePath, TUint aMode)
       
   187 	{
       
   188 	CContentFile* self = new (ELeave) CContentFile;
       
   189 	CleanupStack::PushL(self);
       
   190 	self->ConstructL(aSession, aFilePath, aMode);
       
   191 	CleanupStack::Pop(self);
       
   192 	return self;
       
   193 	}
       
   194 
       
   195 void CContentFile::ConstructL(RFs& aSession, const TDesC& aPath, TUint /*aMode*/)
       
   196 	{
       
   197 	iSession = &aSession;
       
   198 	iFilePath = aPath.AllocL();
       
   199 
       
   200 	iContent = CContent::NewL(*iFilePath, EContentShareReadWrite) ;
       
   201 	iData = iContent->OpenContentL(EPeek,EContentShareReadWrite);
       
   202 	}
       
   203 
       
   204 
       
   205 
       
   206 
       
   207 
       
   208 TInt CContentFile::EvaluateIntent(TIntent aIntent) const
       
   209 	{
       
   210 	ASSERT(iData);
       
   211 	return iData->EvaluateIntent(aIntent);
       
   212 	}
       
   213 
       
   214 
       
   215 TInt CContentFile::ExecuteIntent(TIntent aIntent)
       
   216 	{
       
   217 	ASSERT(iData);
       
   218 	return iData->ExecuteIntent(aIntent);
       
   219 	}
       
   220 
       
   221 TBool CContentFile::IsProtected() const
       
   222 	{
       
   223 	ASSERT(iContent && iData);
       
   224 	TInt value = 0;
       
   225 	TInt err =iData->GetAttribute(EIsProtected, value);
       
   226 	return (err == KErrNone && value);
       
   227 	}
       
   228 
       
   229 
       
   230 TInt CContentFile::SetAgentProperty(ContentAccess::TAgentProperty aProperty, TInt aValue)
       
   231 	{
       
   232 	ASSERT(iContent);
       
   233 	return iContent->SetProperty(aProperty, aValue);
       
   234 	}
       
   235 
       
   236 
       
   237 TInt CContentFile::Seek(TSeek aSeekMode, TInt aPos)
       
   238 	{
       
   239 	ASSERT(iData);
       
   240 	return iData->Seek(aSeekMode, aPos);
       
   241 	}
       
   242 
       
   243 TInt CContentFile::Read(TDes8& aDes,TInt aLength)
       
   244 	{
       
   245 	ASSERT(iData);
       
   246 	return iData->Read(aDes,aLength);
       
   247 	}
       
   248 
       
   249 void CContentFile::Read(TDes8& aDes, TInt aLength, TRequestStatus& aStatus)
       
   250 	{
       
   251 	ASSERT(iData);
       
   252 	iData->Read(aDes, aLength, aStatus);
       
   253 	}
       
   254 
       
   255 TInt CContentFile::Write(const TDesC8& /*aDes*/,TInt /*aLength*/)
       
   256 	{
       
   257 	return KErrAccessDenied;
       
   258 	}
       
   259 void CContentFile::Write(const TDesC8& /*aDes*/, TInt /*aLength*/, TRequestStatus& aStatus)
       
   260 	{
       
   261 	TRequestStatus* status = &aStatus;
       
   262 	User::RequestComplete(status, KErrAccessDenied);
       
   263 	}
       
   264 
       
   265 TInt CContentFile::SetSize(TInt /*aSize*/)
       
   266 	{
       
   267 	// Only a source is implemented, hence this cannot be allowed
       
   268 	return KErrAccessDenied;
       
   269 	}
       
   270 
       
   271 
       
   272 // Get the size of file
       
   273 // this method opens a new, read-only, RFile the first time this method is called
       
   274 
       
   275 TInt CContentFile::Size(TInt& aSize)
       
   276 	{
       
   277 	ASSERT(iData);
       
   278 	TRAPD(err, iData->DataSizeL(aSize));
       
   279 	return err;
       
   280 	}
       
   281 
       
   282 /**
       
   283  * return a RFile for the legacy RFile method
       
   284  * this method opens a new, read-only, RFile the first time this method is called
       
   285  * @internalTechnology
       
   286  * @return Reference to RFile handle to current file
       
   287  */
       
   288 RFile& CContentFile::FileL()
       
   289 	{
       
   290 	if (!iLegacyFileOpen)
       
   291 		{
       
   292 		User::LeaveIfError(iLegacyFile.Open(*iSession, *iFilePath, EFileRead | EFileStream | EFileShareAny));
       
   293 		iLegacyFileOpen = ETrue;
       
   294 		}
       
   295 	return iLegacyFile;
       
   296 	}
       
   297 
       
   298 
       
   299 CF32File* CF32File::NewL(RFile& aFile)
       
   300 	{
       
   301 	CF32File* self = new (ELeave) CF32File;
       
   302 	CleanupStack::PushL(self);
       
   303 	self->ConstructL(aFile);
       
   304 	CleanupStack::Pop(self);
       
   305 	return self;
       
   306 	}
       
   307 
       
   308 void CF32File::ConstructL(RFile& aFile)
       
   309 	{
       
   310 	iFile = aFile;
       
   311 	iFileHandle = ETrue;
       
   312 	}
       
   313 
       
   314 CContentFile* CContentFile::NewL(RFile& aFile, const TDesC& aUniqueId)
       
   315 	{
       
   316 	CContentFile* self = new (ELeave) CContentFile;
       
   317 	CleanupStack::PushL(self);
       
   318 	self->ConstructL(aFile, aUniqueId);
       
   319 	CleanupStack::Pop(self);
       
   320 	return self;
       
   321 	}
       
   322 
       
   323 void CContentFile::ConstructL(RFile& aFile, const TDesC& aUniqueId)
       
   324 	{
       
   325 	iContent = CContent::NewL(aFile) ;
       
   326 	if (aUniqueId.Length()>0)
       
   327 		iData = iContent->OpenContentL(EPeek, aUniqueId); //need mode
       
   328 	else
       
   329 		iData = iContent->OpenContentL(EPeek, aUniqueId);
       
   330 	}
       
   331