contentmgmt/referencedrmagent/contentiterator/FileContentIteratorbase.cpp
changeset 15 da2ae96f639b
equal deleted inserted replaced
10:afc583cfa176 15:da2ae96f639b
       
     1 /*
       
     2 * Copyright (c) 2004-2009 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 the License "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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <f32file.h>
       
    21 #include <apmstd.h>
       
    22 
       
    23 #include "contentIterator.h"
       
    24 #include "virtualpath.h"
       
    25 #include "content.h"
       
    26 #include "manager.h"
       
    27 #include "embeddedobject.h"
       
    28 #include "FileContentIteratorBase.h"
       
    29 #include "EmbeddedcontentIterator.h"
       
    30 
       
    31 using namespace ContentAccess;
       
    32 
       
    33 _LIT(KCAFBackSlashCharacter, "\\");
       
    34 
       
    35 
       
    36 CFileContentIteratorBase* CFileContentIteratorBase::NewL(CManager &aManager, const TDesC& aPath, TBool aRecursive, const TDesC8& aMimeType)
       
    37 	{
       
    38 	CFileContentIteratorBase* self = new (ELeave) CFileContentIteratorBase(aManager, aPath, aRecursive, aMimeType);
       
    39 	CleanupStack::PushL(self);
       
    40 	self->ConstructL();
       
    41 	CleanupStack::Pop(self);
       
    42 	return self;
       
    43 	}
       
    44 
       
    45 CFileContentIteratorBase::CFileContentIteratorBase(CManager& aManager, const TDesC& aPath, TBool aRecursive, const TDesC8& aMimeType) :
       
    46 		iManager(aManager), iPath(aPath), iRecursive(aRecursive), iMimeType(aMimeType)
       
    47 	{
       
    48 	iDirIndex = 0;
       
    49 	iFileIndex = 0;
       
    50 	}
       
    51 
       
    52 void CFileContentIteratorBase::ConstructL()
       
    53 	{
       
    54 	// Get our list of files and directories
       
    55 	User::LeaveIfError(iManager.GetDir(iPath, ESortByName, KEntryAttNormal, iCurrentFileList, iCurrentDirectoryList));
       
    56 
       
    57 	if(!iRecursive)
       
    58 		{
       
    59 		// Won't be needing the list of directories
       
    60 		delete iCurrentDirectoryList;
       
    61 		iCurrentDirectoryList = NULL;
       
    62 		}
       
    63 
       
    64 	// Find the Next,  (ie. first content object) inside this directory
       
    65 	// This will leave if nothing was found
       
    66 	User::LeaveIfError(Next());
       
    67 	}
       
    68 
       
    69 CFileContentIteratorBase::~CFileContentIteratorBase()
       
    70 	{
       
    71 	delete iSubIterator;
       
    72 	delete iCurrentDirectoryList;
       
    73 	delete iCurrentFileList;
       
    74 	delete iNewPath;
       
    75 	}
       
    76 
       
    77 const TDesC& CFileContentIteratorBase::FileName() const
       
    78 	{
       
    79 	return iSubIterator->FileName();
       
    80 	}
       
    81 
       
    82 const TDesC& CFileContentIteratorBase::UniqueId() const
       
    83 	{
       
    84 	return iSubIterator->UniqueId();
       
    85 	}
       
    86 
       
    87 const TDesC& CFileContentIteratorBase::Name() const
       
    88 	{
       
    89 	return iSubIterator->Name();
       
    90 	}
       
    91 	
       
    92 const TDesC8& CFileContentIteratorBase::MimeType() const
       
    93 	{
       
    94 	return iSubIterator->MimeType();
       
    95 	}
       
    96 		
       
    97 TInt CFileContentIteratorBase::Next()
       
    98 	{	
       
    99 	_LIT(KSysDirEntry, "sys");
       
   100 	
       
   101 	TInt ret = KErrNotFound;
       
   102 
       
   103 	// If we are already looking into some sub container, try finding the next in there
       
   104 	if(iSubIterator)
       
   105 		{
       
   106 		ret = iSubIterator->Next();
       
   107 		if(ret == KErrNone)
       
   108 			{
       
   109 			return ret;
       
   110 			}
       
   111 		else 
       
   112 			{
       
   113 			// Delete sub-iterator
       
   114 			delete iSubIterator;
       
   115 			iSubIterator = NULL;
       
   116 			delete iNewPath;
       
   117 			iNewPath = NULL;
       
   118 			}
       
   119 		}
       
   120 
       
   121 
       
   122 	// Search files in our directory first
       
   123 	// this allows us to free memory used by iCurrentFileList
       
   124 	// before recursing into sub-directories
       
   125 	if(iCurrentFileList)
       
   126 		{
       
   127 		for ( ; iFileIndex < iCurrentFileList->Count(); iFileIndex++)
       
   128 			{
       
   129 			// Get a reference to the file we are interested in
       
   130 			const TEntry &entry = (*iCurrentFileList)[iFileIndex];
       
   131 		
       
   132 			// create a new path string for the sub directory
       
   133 			TRAP(ret, iNewPath = HBufC::NewL(iPath.Length() + entry.iName.Length()));
       
   134 			if(ret != KErrNone)
       
   135 				{
       
   136 				return ret;
       
   137 				}
       
   138 			iNewPath->Des().Append(iPath);
       
   139 			iNewPath->Des().Append(entry.iName);
       
   140 		
       
   141 			// Look inside the file
       
   142 			TRAP(ret, iSubIterator = CEmbeddedContentIterator::NewL(TVirtualPathPtr(*iNewPath, KNullDesC()), iRecursive, iMimeType));
       
   143 		
       
   144 			if(ret == KErrNone)
       
   145 				{	
       
   146 				// must have found something in the file
       
   147 				// make sure next time we look at the next item in our list
       
   148 				iFileIndex++;
       
   149 				return KErrNone;
       
   150 				}
       
   151 			// next iteration
       
   152 			delete iNewPath;
       
   153 			iNewPath = NULL;
       
   154 			}
       
   155 		delete iCurrentFileList;
       
   156 		iCurrentFileList = NULL;
       
   157 		}
       
   158 
       
   159 
       
   160 	if(iRecursive)
       
   161 		{
       
   162 		// Search sub directories
       
   163 		for ( ; iDirIndex < iCurrentDirectoryList->Count(); iDirIndex++)
       
   164 			{
       
   165 			// Get a reference to the directory we are interested in
       
   166 			const TEntry &entry = (*iCurrentDirectoryList)[iDirIndex];
       
   167 
       
   168 			// make sure it's not the sys directory
       
   169 			if(KSysDirEntry().CompareF(entry.iName) == 0 && iPath.Length() == 3)
       
   170 				{
       
   171 				// go to the next iteration, skip the sys directory
       
   172 				continue;
       
   173 				}
       
   174 
       
   175 			// create a new path string for the sub directory
       
   176 			TRAP(ret, iNewPath = HBufC::NewL(iPath.Length() + 1 + entry.iName.Length()));
       
   177 			if(ret != KErrNone)
       
   178 				{
       
   179 				return ret;
       
   180 				}
       
   181 			iNewPath->Des().Append(iPath);
       
   182 			iNewPath->Des().Append(entry.iName);
       
   183 			iNewPath->Des().Append(KCAFBackSlashCharacter);
       
   184 		
       
   185 
       
   186 			// lets look inside the directory
       
   187 			TRAP(ret, iSubIterator = CFileContentIteratorBase::NewL(iManager, *iNewPath, iRecursive, iMimeType));
       
   188 
       
   189 			if(ret == KErrNone)
       
   190 				{	
       
   191 				// must have found something in the sub-iterator
       
   192 				// make sure next time we look at the next item in our list
       
   193 				iDirIndex++;
       
   194 				return KErrNone;
       
   195 				}
       
   196 			// next iteration
       
   197 			delete iNewPath;
       
   198 			iNewPath = NULL;
       
   199 			}
       
   200 		}
       
   201 	
       
   202 	// reached the end of our list of directories and list of files
       
   203 	return KErrNotFound;	
       
   204 	}