messagingapp/msgutils/unieditorutils/unieditorplugins/unieditormmsplugin/tsrc/testmmsgpluginapp/mmstestbed/src/mmsteststaticutils.cpp
changeset 52 12db4185673b
equal deleted inserted replaced
44:36f374c67aa8 52:12db4185673b
       
     1 /*
       
     2  * Copyright (c) 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 "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 #include <f32file.h>
       
    18 #include <centralrepository.h>
       
    19 #include <apparc.h>
       
    20 #include <msvapi.h>
       
    21 #include <msvids.h>
       
    22 
       
    23 #include "mmsteststaticutils.h"
       
    24 #include "mmsconst.h"
       
    25 #include "MmsEnginePrivateCRKeys.h"
       
    26 #include "mmssettings.h"
       
    27 #include "mmsheaders.h"
       
    28 #include "mmsservercommon.h"
       
    29 #include "mmsencode.h"
       
    30 
       
    31 
       
    32 // ---------------------------------------------------------
       
    33 //
       
    34 // ---------------------------------------------------------
       
    35 //
       
    36 TMmsJoinedArrays::TMmsJoinedArrays(CDesCArray* const & aArray1, CDesCArray* const & aArray2)
       
    37 : iArray1(aArray1), iArray2(aArray2)
       
    38     {
       
    39     }
       
    40 
       
    41 TInt TMmsJoinedArrays::MdcaCount() const
       
    42     {
       
    43     TInt count=0;
       
    44     if (iArray1)
       
    45         count += iArray1->MdcaCount();
       
    46     if (iArray2)
       
    47         count += iArray2->MdcaCount();
       
    48     return count;
       
    49     }
       
    50 
       
    51 TPtrC16 TMmsJoinedArrays::MdcaPoint(TInt aIndex) const
       
    52     {
       
    53     if (iArray1)
       
    54         {
       
    55         if (aIndex < iArray1->MdcaCount())
       
    56             return iArray1->MdcaPoint(aIndex);
       
    57         else
       
    58             aIndex -= iArray1->MdcaCount();
       
    59         }
       
    60     return iArray2->MdcaPoint(aIndex);
       
    61     }
       
    62 
       
    63 
       
    64 // ---------------------------------------------------------
       
    65 //
       
    66 // ---------------------------------------------------------
       
    67 //
       
    68 TMmsTestUtils::TMmsTestUtils()
       
    69     {
       
    70 
       
    71     }
       
    72 
       
    73 // ---------------------------------------------------------
       
    74 //
       
    75 // ---------------------------------------------------------
       
    76 //
       
    77 HBufC* TMmsTestUtils::ReadContactFromFileL( TDesC& aFileName, RFs& aFs )
       
    78     {
       
    79     RFile file;
       
    80     TInt error = KErrNone;
       
    81 
       
    82     TInt textBufferSize = 256;
       
    83     HBufC* textBuffer = HBufC::NewL( textBufferSize );
       
    84     TPtr textPtr = textBuffer->Des();
       
    85     TFileText textFile;
       
    86 
       
    87     error = file.Open( aFs, aFileName,
       
    88             EFileRead|EFileShareReadersOnly );
       
    89 
       
    90     if ( error == KErrNone )
       
    91         {
       
    92         textFile.Set( file );
       
    93         error = textFile.Seek( ESeekStart );
       
    94         }
       
    95 
       
    96     if ( error == KErrNone )
       
    97         {
       
    98         error = textFile.Read( textPtr );
       
    99         }
       
   100 
       
   101     file.Close();
       
   102 
       
   103     if ( textBuffer->Des().Find( &KMmsByteOrderMark, 1 ) == 0 )
       
   104         {
       
   105         textBuffer->Des().Copy( textBuffer->Des().Mid( 1 ) );
       
   106         }
       
   107 
       
   108     return textBuffer;
       
   109     }
       
   110 
       
   111 //----------------------------------------------------------------------------------------
       
   112 // turn on detailed logging while decoding a message
       
   113 //----------------------------------------------------------------------------------------
       
   114 //
       
   115 void TMmsTestUtils::DecodeLoggingOnL()
       
   116     {
       
   117     // CenRep for decodelogging
       
   118     CRepository* repository = NULL;
       
   119     TInt retval = KErrNone;
       
   120     TRAP( retval, repository = CRepository::NewL( KUidMmsServerMtm ) );
       
   121     if( retval == KErrNone )
       
   122         {
       
   123         // Best effort - if cannot access repository, no can do
       
   124         TInt temp = 1;
       
   125         repository->Set( KMmsEngineDecodeLog, temp );
       
   126         delete repository;
       
   127         }
       
   128     }
       
   129 
       
   130 //----------------------------------------------------------------------------------------
       
   131 // turn off detailed logging while decoding a message
       
   132 //----------------------------------------------------------------------------------------
       
   133 //
       
   134 void TMmsTestUtils::DecodeLoggingOffL()
       
   135     {
       
   136     // CenRep for decodelogging
       
   137     CRepository* repository = NULL;
       
   138     TInt retval = KErrNone;
       
   139     TRAP( retval, repository = CRepository::NewL( KUidMmsServerMtm ) );
       
   140     if( retval == KErrNone )
       
   141         {
       
   142         // Best effort - if cannot access repository, no can do
       
   143         TInt temp = 0;
       
   144         repository->Set( KMmsEngineDecodeLog, temp );
       
   145         delete repository;
       
   146         }
       
   147 
       
   148     }
       
   149 
       
   150 //----------------------------------------------------------------------------------------
       
   151 // turn on binary dump of incoming messages
       
   152 //----------------------------------------------------------------------------------------
       
   153 //
       
   154 void TMmsTestUtils::BinaryDumpOnL()
       
   155     {
       
   156     // CenRep for binarydump setting
       
   157     CRepository* repository = NULL;
       
   158     TInt retval = KErrNone;
       
   159     TRAP( retval, repository = CRepository::NewL( KUidMmsServerMtm ) );
       
   160     if( retval == KErrNone )
       
   161         {
       
   162         // Best effort - if cannot access repository, no can do
       
   163         TInt temp = 1;
       
   164         repository->Set( KMmsEngineBinaryDump, temp );
       
   165         delete repository;
       
   166         }
       
   167 
       
   168     }
       
   169 
       
   170 //----------------------------------------------------------------------------------------
       
   171 // turn off binary dump of incoming messages
       
   172 //----------------------------------------------------------------------------------------
       
   173 //
       
   174 void TMmsTestUtils::BinaryDumpOffL()
       
   175     {
       
   176     // CenRep for binarydump setting
       
   177     CRepository* repository = NULL;
       
   178     TInt retval = KErrNone;
       
   179     TRAP( retval, repository = CRepository::NewL( KUidMmsServerMtm ) );
       
   180     if( retval == KErrNone )
       
   181         {
       
   182         // Best effort - if cannot access repository, no can do
       
   183         TInt temp = 0;
       
   184         repository->Set( KMmsEngineBinaryDump, temp );
       
   185         delete repository;
       
   186         }
       
   187     }
       
   188 
       
   189 // ---------------------------------------------------------
       
   190 //
       
   191 // ---------------------------------------------------------
       
   192 //
       
   193 void TMmsTestUtils::CleanDirectoryL( RFs& aFs )
       
   194     {
       
   195     // Delete the files in the directory
       
   196     CFileMan* fileMan = CFileMan::NewL( aFs );
       
   197     CleanupStack::PushL( fileMan );
       
   198     fileMan->RmDir( KMmsDefaultLocalModeDir );
       
   199     fileMan->RmDir( KMmsMMBoxDirectory );
       
   200     fileMan->RmDir( KMmsMMBoxDescriptionDirectory );
       
   201     CleanupStack::PopAndDestroy(); // fileManager
       
   202     // we don't want to throw these away!
       
   203     aFs.MkDirAll( KMmsDefaultLocalModeDir );
       
   204     aFs.MkDirAll( KMmsMMBoxDirectory );
       
   205     aFs.MkDirAll( KMmsMMBoxDescriptionDirectory );
       
   206     }
       
   207 
       
   208 // ---------------------------------------------------------
       
   209 //
       
   210 // ---------------------------------------------------------
       
   211 //
       
   212 void TMmsTestUtils::Dump( CBufFlat& aBuffer, TFileName& aFilename, TParse& aParse, RFs& aFs )
       
   213     {
       
   214     TInt error = KErrNone;
       
   215     aFs.MkDirAll( aFilename );
       
   216     TUint att;
       
   217     if ( aFs.Att( aFilename, att ) == KErrNone )
       
   218         {
       
   219         _LIT( KRelated, "dump.mms");
       
   220         aParse.Set( aFilename, &KRelated, NULL );
       
   221         aFilename = aParse.FullName();
       
   222         error = CApaApplication::GenerateFileName( aFs, aFilename );
       
   223         if ( error == KErrNone )
       
   224             {
       
   225             RFile file;
       
   226             error = file.Create( aFs, aFilename, EFileWrite | EFileShareExclusive );
       
   227             // for message id generation
       
   228             aParse.Set( aFilename, NULL, NULL );
       
   229             if ( error == KErrNone )
       
   230                 {
       
   231                 // the data is supposed to be in the encode buffer
       
   232                 TPtr8 ptr = aBuffer.Ptr( 0 );
       
   233                 file.Write( ptr );
       
   234                 file.Flush();
       
   235                 }
       
   236 
       
   237             // done - close files
       
   238             file.Close();
       
   239             }
       
   240         }
       
   241     }
       
   242 
       
   243 // ---------------------------------------------------------
       
   244 //
       
   245 // ---------------------------------------------------------
       
   246 //
       
   247 void TMmsTestUtils::CleanBoxL(TMsvId aBoxId, CMsvSession& aSession)
       
   248     {
       
   249     CMsvEntry* cEntry = NULL;
       
   250     // delete all messages from the specified box
       
   251     cEntry = aSession.GetEntryL(KMsvRootIndexEntryId);
       
   252     CleanupStack::PushL(cEntry);
       
   253     cEntry->SetEntryL(aBoxId);
       
   254     // show invisible entries
       
   255     cEntry->SetSortTypeL( TMsvSelectionOrdering( KMsvNoGrouping, EMsvSortByNone, ETrue ) );
       
   256     CMsvEntrySelection* msvEntrySelection = cEntry->ChildrenWithMtmL(KUidMsgTypeMultimedia);
       
   257     CleanupStack::PushL(msvEntrySelection);
       
   258 
       
   259     CMsvEntrySelection* selection = NULL;
       
   260     cEntry->SetSortTypeL( TMsvSelectionOrdering( KMsvNoGrouping, EMsvSortByNone, ETrue ) );
       
   261     selection = cEntry->ChildrenWithMtmL(KUidMsgMMSNotification);
       
   262     CleanupStack::PushL( selection );
       
   263     if ( selection->Count() > 0 )
       
   264         {
       
   265         msvEntrySelection->AppendL( selection->Back( 0 ), selection->Count() );
       
   266         }
       
   267     CleanupStack::PopAndDestroy(); // selection
       
   268 
       
   269     int i;
       
   270     for (i = 0; i < msvEntrySelection->Count(); ++i)
       
   271         {
       
   272         CMsvEntry* entry = aSession.GetEntryL( msvEntrySelection->At(i) );
       
   273         CleanupStack::PushL( entry );
       
   274         TMsvEntry tEntry = entry->Entry();
       
   275         tEntry.SetReadOnly(EFalse);
       
   276         entry->ChangeL(tEntry);
       
   277         cEntry->DeleteL( msvEntrySelection->At(i) );
       
   278         CleanupStack::PopAndDestroy( entry );
       
   279         }
       
   280 
       
   281     CleanupStack::PopAndDestroy(msvEntrySelection);
       
   282     CleanupStack::PopAndDestroy(cEntry);
       
   283     }
       
   284 
       
   285 //----------------------------------------------------------------------------------------
       
   286 // turn on logging email recipients
       
   287 //----------------------------------------------------------------------------------------
       
   288 void TMmsTestUtils::EmailLoggingOnL()
       
   289     {
       
   290     // CenRep for binarydump setting
       
   291     CRepository* repository = NULL;
       
   292     TInt retval = KErrNone;
       
   293     TRAP( retval, repository = CRepository::NewL( KUidMmsServerMtm ) ); // ***
       
   294     if( retval == KErrNone )
       
   295         {
       
   296         TInt temp = 1;
       
   297         repository->Set( KMmsEngineLogEmailRecipients, temp );
       
   298         delete repository;
       
   299         }
       
   300 
       
   301     }
       
   302 
       
   303 //----------------------------------------------------------------------------------------
       
   304 // turn off logging email recipients
       
   305 //----------------------------------------------------------------------------------------
       
   306 void TMmsTestUtils::EmailLoggingOffL()
       
   307     {
       
   308     // CenRep for binarydump setting
       
   309     CRepository* repository = NULL;
       
   310     TInt retval = KErrNone;
       
   311     TRAP( retval, repository = CRepository::NewL( KUidMmsServerMtm ) ); // ***
       
   312     if( retval == KErrNone )
       
   313         {
       
   314         TInt temp = 0;
       
   315         repository->Set( KMmsEngineLogEmailRecipients, temp );
       
   316         delete repository;
       
   317         }
       
   318 
       
   319     }
       
   320 
       
   321 //----------------------------------------------------------------------------------------
       
   322 //
       
   323 //----------------------------------------------------------------------------------------
       
   324 TInt TMmsTestUtils::CountChildrenL(TMsvId aBoxId, CMsvEntrySelection*& aMsvEntrySelection, CMsvSession& aSession, TUid aMessageType )
       
   325     {
       
   326     CMsvEntry* cEntry = aSession.GetEntryL(KMsvRootIndexEntryId);
       
   327     CleanupStack::PushL(cEntry);
       
   328     cEntry->SetEntryL(aBoxId);
       
   329     if (aMsvEntrySelection != NULL)
       
   330         {
       
   331         delete aMsvEntrySelection;
       
   332         aMsvEntrySelection = NULL;
       
   333         }
       
   334     aMsvEntrySelection = cEntry->ChildrenWithMtmL(aMessageType);
       
   335     CleanupStack::PopAndDestroy(); // cEntry
       
   336     return aMsvEntrySelection->Count();
       
   337     }
       
   338 
       
   339 
       
   340 // -----------------------------------------------------------------------------
       
   341 // CreateFolderEntryL
       
   342 //
       
   343 // -----------------------------------------------------------------------------
       
   344 //
       
   345 void TMmsTestUtils::CreateFolderEntryL(
       
   346     CMsvSession& aSession,
       
   347     TMsvId aParentFolder,
       
   348     const TDesC& aFolderName,
       
   349     TMsvId& aFolderId )
       
   350     {
       
   351     aFolderId = KMsvNullIndexEntryId;
       
   352     CMsvEntry* cEntry = aSession.GetEntryL( aParentFolder );
       
   353     CleanupStack::PushL( cEntry );
       
   354 
       
   355     // Create a new folder.
       
   356 
       
   357     TMsvEntry entry;
       
   358     entry.iType = KUidMsvFolderEntry;
       
   359     entry.iMtm = KUidMsvLocalServiceMtm;
       
   360     entry.iDetails.Set( aFolderName );
       
   361     entry.SetVisible( EFalse );
       
   362     entry.SetInPreparation( EFalse );
       
   363     entry.iServiceId = KMsvLocalServiceIndexEntryId;
       
   364     cEntry->CreateL( entry );
       
   365     aFolderId = entry.Id();
       
   366     CleanupStack::PopAndDestroy( cEntry );
       
   367 
       
   368     }
       
   369 
       
   370 
       
   371 //----------------------------------------------------------------------------------------
       
   372 //
       
   373 //----------------------------------------------------------------------------------------
       
   374 TMsvId TMmsTestUtils::CreateNotificationEntryL(
       
   375         TMsvId aNotificationFolder,
       
   376         TMsvId aServiceId,
       
   377         CBufFlat* aEncodeBuffer,
       
   378         CMsvSession& aSession )
       
   379     {
       
   380     TMsvId entryId = KMsvNullIndexEntryId;
       
   381     if ( aNotificationFolder == KMsvNullIndexEntryId )
       
   382         {
       
   383         // no folder no entry
       
   384         return entryId;
       
   385         }
       
   386 
       
   387     CMsvEntry* cEntry = aSession.GetEntryL( aNotificationFolder );
       
   388     CleanupStack::PushL(cEntry);
       
   389 
       
   390     TMsvEntry entry;
       
   391     entry.iType = KUidMsvMessageEntry;
       
   392     entry.iMtm = KUidMsgTypeMultimedia;
       
   393     entry.SetVisible( ETrue );
       
   394     // If we want to put data here, InPreparation must be set to true first
       
   395     entry.SetInPreparation( EFalse );
       
   396     entry.iServiceId = KMsvLocalServiceIndexEntryId;
       
   397     entry.iRelatedId = aServiceId;
       
   398     entry.iMtmData2 = KMmsNotificationBinary;
       
   399     cEntry->CreateL( entry );
       
   400     entryId = entry.Id();
       
   401 
       
   402     //
       
   403     // Stream
       
   404     // 1) length of the data as 32 bit integer
       
   405     // 2) pushed message data
       
   406     // into created entry's stream
       
   407     //
       
   408     cEntry->SetEntryL( entryId );
       
   409     CMsvStore* store = cEntry->EditStoreL();
       
   410     CleanupStack::PushL( store );   // ***
       
   411     RMsvWriteStream outs;
       
   412     outs.AssignLC( *store, KUidBinaryNotificationStream ); // ***
       
   413     TPtrC8 ptr = aEncodeBuffer->Ptr( 0 );
       
   414     outs.WriteUint32L( ptr.Length() );
       
   415     outs.WriteL( ptr );
       
   416     outs.CommitL();
       
   417     outs.Close();
       
   418     store->CommitL();
       
   419 
       
   420     CleanupStack::PopAndDestroy( &outs ); // close outs
       
   421     CleanupStack::PopAndDestroy( store );
       
   422     CleanupStack::PopAndDestroy( cEntry );
       
   423 
       
   424     return entryId;
       
   425 
       
   426     }
       
   427 
       
   428 //----------------------------------------------------------------------------------------
       
   429 //
       
   430 //----------------------------------------------------------------------------------------
       
   431 void TMmsTestUtils::FormNotification(
       
   432         TDesC8& aUrl,
       
   433         TInt aSize,
       
   434         CMmsHeaders& aMmsHeaders,
       
   435         CMmsEncode& aMmsEncoder,
       
   436         CBufFlat* aEncodeBuffer )
       
   437     {
       
   438     // for test purposes aUrl will contain the filename.
       
   439 
       
   440     // Reset sets the default encapsulation version
       
   441     // The default version has been set from MmsSettings in NewL
       
   442     aMmsHeaders.Reset();
       
   443 
       
   444     // construct the notification into iMmsHeaders, and call encode
       
   445 
       
   446     aMmsHeaders.SetMessageType( KMmsMessageTypeMNotificationInd );
       
   447 
       
   448     TTime currentTime;
       
   449     currentTime.UniversalTime();
       
   450     currentTime.Int64();
       
   451 
       
   452     TPtrC8 tid;
       
   453     TBufC8<KMMSMAXTIDLENGTH> target;
       
   454     TInt random = 0;
       
   455 
       
   456     // we don't generate a true random TID: We generate the
       
   457     // TID from the URL so that if we generate a notification
       
   458     // twice from the same file, we get the same TID and the
       
   459     // same URL. This way we can test the pruning function in
       
   460     // server MTM
       
   461 
       
   462     TInt i;
       
   463     for ( i = 0; i < aUrl.Length(); ++i )
       
   464         {
       
   465         random += aUrl[ i ];
       
   466         }
       
   467 
       
   468     target.Des().Num( random );
       
   469     tid.Set( target.Des() );
       
   470     aMmsHeaders.SetTidL( tid );
       
   471 
       
   472     aMmsHeaders.SetMessageClass( EMmsClassPersonal );
       
   473     aMmsHeaders.SetMessageSize( aSize );
       
   474     const TInt KTenHours = 10 * 60 * 60; // 10 hours relative expiry
       
   475     aMmsHeaders.SetExpiryInterval( KTenHours );
       
   476     aMmsHeaders.SetContentLocationL( aUrl );
       
   477 
       
   478     aMmsEncoder.EncodeHeadersL( aMmsHeaders, *aEncodeBuffer );
       
   479 
       
   480     }
       
   481 
       
   482 
       
   483 //----------------------------------------------------------------------------------------
       
   484 //
       
   485 //----------------------------------------------------------------------------------------
       
   486 TBool TMmsTestUtils::IsFile(const TDesC& aFileName, RFs& aFs)
       
   487     {
       
   488     //Is the name a file?
       
   489     if (IsDrive(aFileName))
       
   490         return EFalse;
       
   491     return !(IsDir(aFileName, aFs));
       
   492     }
       
   493 
       
   494 //----------------------------------------------------------------------------------------
       
   495 //
       
   496 //----------------------------------------------------------------------------------------
       
   497 TBool TMmsTestUtils::IsDir(const TDesC& aFileName, RFs& aFs)
       
   498     {
       
   499     //Is the name a directory?
       
   500     TEntry entry;
       
   501     TInt err = aFs.Entry(aFileName, entry);
       
   502     if (err)
       
   503         return EFalse;
       
   504     else
       
   505         return entry.IsDir();
       
   506     }
       
   507 
       
   508 //----------------------------------------------------------------------------------------
       
   509 //
       
   510 //----------------------------------------------------------------------------------------
       
   511 TBool TMmsTestUtils::IsDrive(const TDesC& aFileName)
       
   512     {
       
   513     //horrible little function to figure if the path is just a drive
       
   514     TBool retVal = EFalse;
       
   515     if (aFileName.Length()==3) //eg "c:\"
       
   516         {
       
   517         if ((aFileName[1] == ':')  && (aFileName[2] == '\\'))
       
   518             retVal=ETrue;
       
   519         }
       
   520     else if (aFileName.Length()==2) //eg "c:"
       
   521         {
       
   522         if (aFileName[1] == ':')
       
   523             retVal=ETrue;
       
   524         }
       
   525     return retVal;
       
   526     }
       
   527 
       
   528 // end of file