contentmgmt/referencedrmagent/contentiterator/embeddedcontentiteratorbase.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 #include <f32file.h>
       
    20 #include <apmstd.h>
       
    21 
       
    22 #include "contentIterator.h"
       
    23 #include "virtualpath.h"
       
    24 #include "content.h"
       
    25 #include "EmbeddedcontentIteratorBase.h"
       
    26 #include "embeddedobject.h"
       
    27 
       
    28 using namespace ContentAccess;
       
    29 
       
    30 
       
    31 CEmbeddedContentIteratorBase* CEmbeddedContentIteratorBase ::NewL(CContent& aContent, TBool aRecursive, const TDesC8& aMimeType)
       
    32 	{
       
    33 	CEmbeddedContentIteratorBase* self = new (ELeave) CEmbeddedContentIteratorBase(aContent, aRecursive, aMimeType);
       
    34 	CleanupStack::PushL(self);
       
    35 	self->ConstructL();
       
    36 	CleanupStack::Pop(self);
       
    37 	return self;
       
    38 	}
       
    39 
       
    40 void CEmbeddedContentIteratorBase::ConstructL()
       
    41 	{
       
    42 	// populate the array with the items at this level of the content file
       
    43 	iContent.GetEmbeddedObjectsL(iEmbeddedContentObjects, EContentObject);
       
    44 	iContent.GetEmbeddedObjectsL(iEmbeddedContainerObjects, EContainerObject);
       
    45 
       
    46 	// Find the Next,  (ie. first content object)
       
    47 	// This will leave if there is no object
       
    48 	User::LeaveIfError(Next());
       
    49 	}
       
    50 
       
    51 CEmbeddedContentIteratorBase::CEmbeddedContentIteratorBase(CContent& aContent, TBool aRecursive, const TDesC8& aMimeType) 
       
    52 	: iContent(aContent), iRecursive(aRecursive), iMimeType(aMimeType)
       
    53 	{
       
    54 	iContentIndex = 0;
       
    55 	iContainerIndex = 0;
       
    56 	}
       
    57 
       
    58 CEmbeddedContentIteratorBase::~CEmbeddedContentIteratorBase()
       
    59 	{
       
    60 	delete iSubIterator;
       
    61 
       
    62 	// Clear array 
       
    63 	iEmbeddedContainerObjects.Close();
       
    64 	iEmbeddedContentObjects.Close();
       
    65 	}	
       
    66 
       
    67 CEmbeddedObject& CEmbeddedContentIteratorBase::EmbeddedObject()
       
    68 	{
       
    69 	// The next pointer is currently on an object inside the subiterator
       
    70 	if(iSubIterator)
       
    71 		{
       
    72 		return iSubIterator->EmbeddedObject();
       
    73 		}
       
    74 	else
       
    75 		{	
       
    76 		return *iEmbeddedContentObjects[iContentIndex - 1];
       
    77 		}
       
    78 	}
       
    79 		
       
    80 TInt CEmbeddedContentIteratorBase::Next()
       
    81 	{
       
    82 	// Must figure out if there's a constant for this
       
    83 	TBuf8 <KMaxDataTypeLength> mimeType;
       
    84 	TInt ret = KErrNotFound;
       
    85 
       
    86 	// If we are already looking into a container, try finding the next in there
       
    87 	if(iSubIterator)
       
    88 		{
       
    89 		ret = iSubIterator->Next();
       
    90 		if(ret == KErrNone)
       
    91 			{
       
    92 			return ret;
       
    93 			}
       
    94 		else 
       
    95 			{
       
    96 			// come back to our level
       
    97 			iContent.CloseContainer();
       
    98 
       
    99 			// Delete sub-iterator
       
   100 			delete iSubIterator;
       
   101 			iSubIterator = NULL;
       
   102 			}
       
   103 		}
       
   104 	
       
   105 
       
   106 	// Find content objects inside this container
       
   107 	for( ; iContentIndex < iEmbeddedContentObjects.Count(); iContentIndex++)
       
   108 		{
       
   109 		// if we don't care about mime type, this content object will do
       
   110 		if(iMimeType.Length() == 0)
       
   111 			{
       
   112 			// make sure next time we look at the next item in our array
       
   113 			iContentIndex++;
       
   114 			return KErrNone;
       
   115 			}
       
   116 
       
   117 		// See if the content object has the right mime type that we are looking for
       
   118 		mimeType.Copy(iEmbeddedContentObjects[iContentIndex]->MimeType());
       
   119 		if(iMimeType == mimeType)
       
   120 			{
       
   121 			// make sure next time we look at the next item in our array
       
   122 			iContentIndex++;
       
   123 			return KErrNone;
       
   124 			}
       
   125 		// otherwise continue to next iteration, for loop incrementes iContentIndex		
       
   126 		}		
       
   127 	
       
   128 	// Free memory allocated for content objects
       
   129 	if(iContentIndex)
       
   130 		{
       
   131 		iEmbeddedContentObjects.ResetAndDestroy();
       
   132 		iContentIndex = 0;
       
   133 		}
       
   134 
       
   135 	// Find content objects within nested containers
       
   136 	for( ; iContainerIndex < iEmbeddedContainerObjects.Count(); iContainerIndex++)
       
   137 		{
       
   138 		// If it's a container look inside 
       
   139 		iContent.OpenContainer(iEmbeddedContainerObjects[iContainerIndex]->UniqueId());
       
   140 		TRAPD(err, iSubIterator = CEmbeddedContentIteratorBase::NewL(iContent, iRecursive, iMimeType));
       
   141 		if(err == KErrNone)
       
   142 			{
       
   143 			// must have found something inside
       
   144 			// make sure next time we search at the next item in our array
       
   145 			iContainerIndex++;
       
   146 			return KErrNone;
       
   147 			}
       
   148 		// otherwise continue to next iteration
       
   149 		}
       
   150 
       
   151 	// must be at the end of the array, ie. can't find any more content objects
       
   152 	return KErrNotFound;
       
   153 	}
       
   154 
       
   155