messagingapp/msgutils/unidatamodel/unimmsdataplugin/src/UniObjectList.cpp
changeset 25 84d9eb65b26f
parent 23 238255e8b033
child 27 e4592d119491
child 37 518b245aa84c
child 79 2981cb3aa489
equal deleted inserted replaced
23:238255e8b033 25:84d9eb65b26f
     1 /*
       
     2 * Copyright (c) 2005 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: 
       
    15 *       CUniObjectList, List of attachments.
       
    16 *
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // ========== INCLUDE FILES ================================
       
    22 
       
    23 #include <e32std.h>
       
    24 #include <e32def.h>     // for basic types
       
    25 #include <eikenv.h>     // for CBase
       
    26 #include <mtclbase.h>   // for CBaseMtm
       
    27 #include <msvstd.h>     // for TMsvId
       
    28 #include <msvids.h>     // for KMsvTempIndexEntryId
       
    29 #include <msvstore.h>
       
    30 #include <mmsvattachmentmanager.h>
       
    31 #include <mmsvattachmentmanagersync.h>
       
    32 #include <cmsvattachment.h>
       
    33 
       
    34 #include <uriutils.h>   // Uri decoding and encoding
       
    35 
       
    36 #include <MsgMediaInfo.h>
       
    37 
       
    38 #include "UniDataUtils.h"
       
    39 #include "UniModelConst.h"
       
    40 #include "UniMimeInfo.h"
       
    41 #include "UniObject.h"
       
    42 #include "UniObjectList.h"
       
    43 #include "UniSmilUtils.h"
       
    44 
       
    45 // ========== EXTERNAL DATA STRUCTURES =====================
       
    46 
       
    47 // ========== EXTERNAL FUNCTION PROTOTYPES =================
       
    48 
       
    49 // ========== CONSTANTS ====================================
       
    50 
       
    51 const TInt KObjectArrayGranularity = 10;
       
    52 _LIT( KContentIdString, "cid:*" );
       
    53 _LIT8 ( KCidLeftAngle, "<"); // 8 bit angle bracket
       
    54 _LIT8 ( KCidRightAngle, ">"); // 8 bit angle bracket
       
    55 
       
    56 // ========== MACROS =======================================
       
    57 
       
    58 // ========== LOCAL CONSTANTS AND MACROS ===================
       
    59 
       
    60 // ========== MODULE DATA STRUCTURES =======================
       
    61 
       
    62 
       
    63 // ========== LOCAL FUNCTION PROTOTYPES ====================
       
    64 
       
    65 // ========== LOCAL FUNCTIONS ==============================
       
    66 
       
    67 // ========== MEMBER FUNCTIONS =============================
       
    68 
       
    69 // ---------------------------------------------------------
       
    70 // CUniObjectList::NewLC
       
    71 //
       
    72 // Factory method.
       
    73 // ---------------------------------------------------------
       
    74 //
       
    75 CUniObjectList* CUniObjectList::NewL( CBaseMtm& aMtm, CUniDataUtils& aData )
       
    76     {
       
    77     CUniObjectList* self = new(ELeave) CUniObjectList( aMtm, aData );
       
    78     CleanupStack::PushL( self );
       
    79     self->ConstructL();
       
    80     CleanupStack::Pop( self );
       
    81     return self;
       
    82     }
       
    83 
       
    84 
       
    85 // ---------------------------------------------------------
       
    86 // CUniObjectList::CUniObjectList
       
    87 //
       
    88 // Constructor.
       
    89 // ---------------------------------------------------------
       
    90 //
       
    91 CUniObjectList::CUniObjectList( CBaseMtm& aMtm, CUniDataUtils& aData ) :
       
    92     iObjectArray( NULL ),
       
    93     iData( aData ),
       
    94     iMtm( aMtm )
       
    95     {
       
    96     }
       
    97 
       
    98 
       
    99 // ---------------------------------------------------------
       
   100 // CUniObjectList::CUniObjectList
       
   101 //
       
   102 // Destructor.
       
   103 // ---------------------------------------------------------
       
   104 //
       
   105 CUniObjectList::~CUniObjectList()
       
   106     {
       
   107     if ( iObjectArray )
       
   108         {
       
   109         iObjectArray->ResetAndDestroy();
       
   110         }
       
   111     delete iObjectArray;
       
   112     }
       
   113 
       
   114 // ---------------------------------------------------------
       
   115 // CUniObjectList::AddObjectL
       
   116 //
       
   117 // AddObjectL.
       
   118 // ---------------------------------------------------------
       
   119 //
       
   120 void CUniObjectList::AddObjectL( CUniObject* aObject )
       
   121     {
       
   122     iObjectArray->AppendL( aObject );
       
   123 
       
   124     if ( iListObserver )
       
   125         {
       
   126         iListObserver->ObjectAddedL( this, aObject, iObjectArray->Count() - 1 );
       
   127         }
       
   128     }
       
   129 
       
   130 // ---------------------------------------------------------
       
   131 // CUniObjectList::SpaceNeededForSaveAll
       
   132 //
       
   133 // SpaceNeededForSaveAll.
       
   134 // ---------------------------------------------------------
       
   135 //
       
   136 TInt CUniObjectList::SpaceNeededForSaveAll()
       
   137     {
       
   138     TInt spaceNeeded( 0 );
       
   139     /*
       
   140     for (TInt i = iObjectArray->Count(); --i >= 0 ;)
       
   141         {
       
   142         if ( iObjectArray->At( i )->StoreState() == EMmsStoreStateTemporary )
       
   143             {
       
   144             spaceNeeded += iObjectArray->At( i )->Size( EFalse ); // Count headers too.
       
   145             }
       
   146         }
       
   147     */
       
   148     return spaceNeeded;
       
   149     }
       
   150 
       
   151 // ---------------------------------------------------------
       
   152 // CUniObjectList::SaveAllL
       
   153 //
       
   154 // SaveAllL.
       
   155 // ---------------------------------------------------------
       
   156 //
       
   157 void CUniObjectList::SaveAll( MUniObjectSaveObserver& aObserver, CMsvAttachment::TMsvAttachmentType aSaveType )
       
   158     {
       
   159     if ( iObjectArray->Count() )
       
   160         {
       
   161         iSaveObserver = &aObserver;
       
   162         iSaveCount = 0;
       
   163         iSaveType = aSaveType;
       
   164         iObjectArray->At( iSaveCount )->Save( *this, aSaveType );
       
   165         }
       
   166     else
       
   167         {
       
   168         aObserver.ObjectSaveReady( KErrNone );
       
   169         }
       
   170     }
       
   171 
       
   172 // ---------------------------------------------------------
       
   173 // CUniObjectList::RemoveAllObjectsL
       
   174 //
       
   175 // RemoveAllObjectsL
       
   176 // ---------------------------------------------------------
       
   177 //
       
   178 void CUniObjectList::RemoveAllObjectsL()
       
   179     {
       
   180     TInt a = iObjectArray->Count();
       
   181     while ( a-- )
       
   182         {
       
   183         CUniObject* obj = iObjectArray->At( a );
       
   184         obj->RemoveFromStoreL();
       
   185         iObjectArray->Delete( a );
       
   186 
       
   187         if ( iListObserver )
       
   188             {
       
   189             iListObserver->ObjectRemovedL( this, obj, a );
       
   190             }
       
   191 
       
   192         delete obj;
       
   193         }
       
   194     }
       
   195 
       
   196 // ---------------------------------------------------------
       
   197 // CUniObjectList::EnsureAllObjectsHaveContentLocationL
       
   198 //
       
   199 // EnsureAllObjectsHaveContentLocationL
       
   200 // ---------------------------------------------------------
       
   201 //
       
   202 void CUniObjectList::EnsureAllObjectsHaveContentLocationL()
       
   203     {
       
   204     CMsvStore* store = iMtm.Entry().EditStoreL();
       
   205     CleanupStack::PushL( store );
       
   206     MMsvAttachmentManager& manager = store->AttachmentManagerL();
       
   207     MMsvAttachmentManagerSync& managerSync = store->AttachmentManagerExtensionsL();
       
   208     TBool commit( EFalse );
       
   209 	TInt a = iObjectArray->Count();
       
   210     while ( a-- )
       
   211         {
       
   212         CUniObject* obj = iObjectArray->At( a );
       
   213         if ( !obj->MimeInfo()->ContentLocation().Length()
       
   214         	&& obj->AttachmentId( ) )
       
   215             {
       
   216             CMsvAttachment* attachment = manager.GetAttachmentInfoL( obj->AttachmentId() );
       
   217             CleanupStack::PushL( attachment );
       
   218             TParsePtrC parse( obj->MediaInfo()->FullFilePath() );
       
   219             TPtrC nameAndExt( parse.NameAndExt() );
       
   220             obj->MimeInfo()->EnsureContentLocationL(
       
   221                 manager,
       
   222                 *attachment,
       
   223                 nameAndExt );
       
   224             managerSync.ModifyAttachmentInfoL( attachment );
       
   225             CleanupStack::Pop( attachment );
       
   226             commit = ETrue;
       
   227             }
       
   228 		}
       
   229     if ( commit )
       
   230         {
       
   231         store->CommitL();
       
   232         }
       
   233     CleanupStack::PopAndDestroy( store );
       
   234 	}
       
   235 
       
   236 // ---------------------------------------------------------
       
   237 // CUniObjectList::RemoveObjectL
       
   238 //
       
   239 // RemoveObjectL.
       
   240 // ---------------------------------------------------------
       
   241 //
       
   242 void CUniObjectList::RemoveObjectL( CUniObject* aObject, TBool aRemoveFromStore /*= ETrue*/ )
       
   243     {
       
   244     if ( aRemoveFromStore )
       
   245         {
       
   246         aObject->RemoveFromStoreL();
       
   247         }
       
   248     TInt a = iObjectArray->Count();
       
   249     while ( a-- )
       
   250         {
       
   251         if ( iObjectArray->At( a ) == aObject )
       
   252             {
       
   253             iObjectArray->Delete( a );
       
   254 
       
   255             if ( iListObserver )
       
   256                 {
       
   257                 iListObserver->ObjectRemovedL( this, aObject, a );
       
   258                 }
       
   259             break;
       
   260             }
       
   261         }
       
   262     }
       
   263 
       
   264 // ---------------------------------------------------------
       
   265 // CUniObjectList::GetByIndex
       
   266 //
       
   267 // GetByIndex.
       
   268 // ---------------------------------------------------------
       
   269 //
       
   270 CUniObject* CUniObjectList::GetByIndex( TInt aIndex )
       
   271     {
       
   272     if ( aIndex < 0 || aIndex >= iObjectArray->Count() )
       
   273         {
       
   274         return NULL;
       
   275         }
       
   276     return iObjectArray->At( aIndex );
       
   277     }
       
   278 
       
   279 // ---------------------------------------------------------
       
   280 // CUniObjectList::GetByAttachmentId
       
   281 //
       
   282 // GetByAttachmentId.
       
   283 // ---------------------------------------------------------
       
   284 //
       
   285 CUniObject* CUniObjectList::GetByAttachmentId( TMsvAttachmentId aAttachmentId )
       
   286     {
       
   287     TInt a = iObjectArray->Count();
       
   288     while ( a-- )
       
   289         {
       
   290         if ( iObjectArray->At( a )->AttachmentId() == aAttachmentId )
       
   291             {
       
   292             return iObjectArray->At( a );
       
   293             }
       
   294         }
       
   295     return NULL;
       
   296     }
       
   297 
       
   298 // ---------------------------------------------------------
       
   299 // CUniSmilModel::GetByNode
       
   300 //
       
   301 // ---------------------------------------------------------
       
   302 //
       
   303 CUniObject* CUniObjectList::GetByNodeL( CMDXMLNode * aNode )
       
   304     {
       
   305     TPtrC domsrc = UniSmilUtils::GetSrcL( aNode ); // Get path from DOM.
       
   306     return GetByUrlL( domsrc );
       
   307     }
       
   308 
       
   309 // ---------------------------------------------------------
       
   310 // CUniObjectList::GetByUrlL
       
   311 //
       
   312 // GetByUrlL.
       
   313 // ---------------------------------------------------------
       
   314 //
       
   315 CUniObject* CUniObjectList::GetByUrlL( const TDesC& aUrl )
       
   316     {
       
   317     CUniObject* att = NULL;
       
   318     TBool found = EFalse;
       
   319 
       
   320     HBufC8* url8bit = HBufC8::NewLC( aUrl.Length() );
       
   321 
       
   322     CUri16* decodedUri = NULL;
       
   323     TUriParser8 parser;
       
   324 
       
   325     if( aUrl.MatchF( KContentIdString ) == 0 )
       
   326         {
       
   327         //Remove "cid:" from the beginning
       
   328         url8bit->Des().Copy( aUrl.Right( aUrl.Length() - KContentIdString().Length() + 1 ) );
       
   329         parser.Parse( *url8bit );
       
   330         }
       
   331     else
       
   332         {
       
   333         url8bit->Des().Copy( aUrl );
       
   334         parser.Parse( *url8bit );
       
   335         }
       
   336     decodedUri = UriUtils::ConvertToDisplayFormL( parser );
       
   337     CleanupStack::PushL( decodedUri );
       
   338 
       
   339     TInt count = iObjectArray->Count();
       
   340     for ( TInt i = 0; i < count && !found ; i++ )
       
   341         {
       
   342         TPtrC contentLocation = iObjectArray->At( i )->MimeInfo()->ContentLocation();
       
   343         TPtrC8 contentIdPtr = iObjectArray->At( i )->MimeInfo()->ContentId();
       
   344         CUri16* contLocUri = NULL;
       
   345         CUri16* contIdUri = NULL;
       
   346         HBufC8* contentLoc = NULL;
       
   347         HBufC8* contentId = NULL;
       
   348 
       
   349         // Convert content location as well.
       
   350         if ( contentIdPtr.Length() )
       
   351             {
       
   352             //Copy string to 8-bit descriptor
       
   353             contentId = HBufC8::NewLC( contentIdPtr.Length() );
       
   354 
       
   355             if ( contentId->Find( KCidLeftAngle ) == 0 &&
       
   356                  contentId->Find( KCidRightAngle ) == contentId->Length() - 1 )
       
   357                 {
       
   358                 // When comparing against cid, remove "<" and ">"
       
   359                 contentId->Des().Copy( contentIdPtr.Mid( 1, contentIdPtr.Length() - 2 ) );
       
   360                 }
       
   361             else
       
   362                 {
       
   363                 contentId->Des().Copy( contentIdPtr );
       
   364                 }
       
   365 
       
   366             parser.Parse( *contentId );
       
   367             contIdUri = UriUtils::ConvertToDisplayFormL( parser );
       
   368             CleanupStack::PushL( contIdUri );
       
   369             }
       
   370          if ( contentLocation.Length() )
       
   371             {
       
   372             //Copy string to 8-bit descriptor
       
   373             contentLoc = HBufC8::NewLC( contentLocation.Length() );
       
   374             contentLoc->Des().Copy( contentLocation );
       
   375             parser.Parse( *contentLoc );
       
   376             contLocUri = UriUtils::ConvertToDisplayFormL( parser );
       
   377             CleanupStack::PushL( contLocUri );
       
   378             }
       
   379 
       
   380         if (
       
   381             ( ( contentLocation.Length() ) && (decodedUri->Uri().UriDes().CompareF( contLocUri->Uri().UriDes() ) == 0 ) ) ||
       
   382             ( ( contentIdPtr.Length() ) && (decodedUri->Uri().UriDes().CompareF( contIdUri->Uri().UriDes() ) == 0 ) ) )
       
   383             {
       
   384             att = iObjectArray->At( i );
       
   385             }
       
   386 
       
   387         if ( contentLoc )
       
   388             {
       
   389             CleanupStack::PopAndDestroy( 2, contentLoc ); // contentLoc, contLocUri
       
   390             }
       
   391         if ( contentId )
       
   392             {
       
   393             CleanupStack::PopAndDestroy( 2, contentId ); // contentId, contIdUri
       
   394             }
       
   395 
       
   396         }
       
   397     CleanupStack::PopAndDestroy( 2, url8bit ); // decodedUri, url8bit
       
   398     return att;
       
   399     }
       
   400 
       
   401 // ---------------------------------------------------------
       
   402 // CUniObjectList::ObjectByteSize
       
   403 // ---------------------------------------------------------
       
   404 //
       
   405 TInt CUniObjectList::ObjectByteSize() const
       
   406     {
       
   407     TInt byteSize(0);
       
   408     TInt i = iObjectArray->Count();
       
   409     while ( i-- )
       
   410         {
       
   411         byteSize += iObjectArray->At( i )->Size();
       
   412         }
       
   413 
       
   414     return byteSize;
       
   415     }
       
   416 
       
   417 // ---------------------------------------------------------
       
   418 // CUniObjectList::SetListObserver
       
   419 // ---------------------------------------------------------
       
   420 //
       
   421 void CUniObjectList::SetListObserver( MUniObjectListObserver* aObserver )
       
   422     {
       
   423     iListObserver = aObserver;
       
   424     }
       
   425 
       
   426 // ---------------------------------------------------------
       
   427 // CUniObjectList::GetObjectByUniqueHandle
       
   428 // ---------------------------------------------------------
       
   429 //
       
   430 CUniObject* CUniObjectList::GetObjectByUniqueHandle( TInt aUniqueControlHandle )
       
   431     {
       
   432     for ( TInt index = 0; index < iObjectArray->Count(); index++ )
       
   433         {
       
   434         CUniObject* current = iObjectArray->At( index );
       
   435         if( current->UniqueControlHandle() == aUniqueControlHandle )
       
   436             {
       
   437             return current;
       
   438             }
       
   439         }
       
   440     return NULL;
       
   441     }
       
   442 
       
   443 // ---------------------------------------------------------
       
   444 // CUniObjectList::ConstructL
       
   445 //
       
   446 // 2nd phase constructor.
       
   447 // ---------------------------------------------------------
       
   448 //
       
   449 void CUniObjectList::ConstructL()
       
   450     {
       
   451     iObjectArray = new(ELeave)CArrayPtrFlat<CUniObject>( KObjectArrayGranularity );
       
   452     }
       
   453 
       
   454 // ---------------------------------------------------------
       
   455 // CUniObjectList::ObjectSaveReady
       
   456 //
       
   457 // ---------------------------------------------------------
       
   458 //
       
   459 void CUniObjectList::ObjectSaveReady( TInt aError )
       
   460     {
       
   461     if ( aError )
       
   462         {
       
   463         iSaveObserver->ObjectSaveReady( aError );
       
   464         }
       
   465     else
       
   466         {
       
   467         iSaveCount++;
       
   468         if ( iSaveCount < iObjectArray->Count() )
       
   469             {
       
   470             iObjectArray->At( iSaveCount )->Save( *this, iSaveType );
       
   471             }
       
   472         else
       
   473             {
       
   474             iSaveObserver->ObjectSaveReady( KErrNone );
       
   475             }
       
   476         }
       
   477     }
       
   478 
       
   479 // EOF