localconnectivityservice/obexserviceman/utils/src/obexutilsmessagehandler.cpp
changeset 0 c3e98f10fcf4
child 1 388a17646e40
equal deleted inserted replaced
-1:000000000000 0:c3e98f10fcf4
       
     1 /*
       
     2 * Copyright (c) 2002 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 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "obexutilsmessagehandler.h"
       
    21 #include "obexutilsuilayer.h"
       
    22 #include "obexutilsdebug.h"
       
    23 #include "obexutilsentryhandler.h"
       
    24 
       
    25 #include <msvids.h>
       
    26 #include <msvuids.h>
       
    27 #include <apgcli.h>
       
    28 #include <txtrich.h>
       
    29 
       
    30 #include <biouids.h>
       
    31 #include <obexutils.rsg>
       
    32 #include <bautils.h>
       
    33 
       
    34 #include <e32property.h>
       
    35 #include <UikonInternalPSKeys.h>
       
    36 
       
    37 #include <mmsvattachmentmanagersync.h>
       
    38 #include <mmsvattachmentmanager.h>
       
    39 
       
    40 #include <centralrepository.h>
       
    41 #include <btengdomaincrkeys.h>
       
    42 #include "updatemusiccollection.h"
       
    43 
       
    44 #include <pathinfo.h>          // provides interface for quering system paths 
       
    45 
       
    46 #ifdef __BT_SAP
       
    47  #include "vMessageHandler.h"
       
    48 #endif // __BT_SAP
       
    49 
       
    50 // CONSTANT
       
    51 const TInt KFFSBelowCritical = -2;
       
    52 const TInt KRenameOffSet = 4 ;
       
    53 // ============================= LOCAL FUNCTIONS ===============================
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // RemoveObexBuffer  Removes Obex buffer object.
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 void TObexUtilsMessageHandler::RemoveObexBuffer(CObexBufObject* &aBufObject)
       
    60     {
       
    61     if (aBufObject)
       
    62         {
       
    63         delete aBufObject;
       
    64         aBufObject = NULL;
       
    65         }
       
    66     }
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // CreateMsvSessionLC  Creates MSV session
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 void TObexUtilsMessageHandler::CreateMsvSessionLC(
       
    73     CDummySessionObserver* &aSessionObs,
       
    74     CMsvSession* &aMsvSession)
       
    75     {
       
    76     aSessionObs = new( ELeave )CDummySessionObserver;
       
    77     CleanupStack::PushL( aSessionObs );  //1st push
       
    78     aMsvSession = CMsvSession::OpenSyncL( *aSessionObs );
       
    79     CleanupStack::PushL( aMsvSession );  //2nd push
       
    80     }
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CreateMsvSessionLC  Creates MSV session to selected destination
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 void TObexUtilsMessageHandler::CreateMsvSessionLC(
       
    87     CDummySessionObserver* &aSessionObs,
       
    88     CMsvSession* &aMsvSession,
       
    89     CMsvEntry* &aDestination,
       
    90     TMsvId aMsvId)
       
    91     {
       
    92     CreateMsvSessionLC(aSessionObs, aMsvSession);
       
    93     aDestination = aMsvSession->GetEntryL( aMsvId );
       
    94     CleanupStack::PushL( aDestination );  //3rd push
       
    95     }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // GetCenRepKeyIntValueL Gets integer repository value
       
    99 // Returns: Error
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 EXPORT_C TInt TObexUtilsMessageHandler::GetCenRepKeyIntValueL(
       
   103     TUid aRepositoryUid,  // aRepositoryUid Central repository's UID value
       
   104     TUint32 aId,          // Central repository's ID value
       
   105     TInt& aVal)           // Returned integer value
       
   106     {
       
   107     CRepository* storage = CRepository::NewLC( aRepositoryUid );
       
   108     TInt retval = storage->Get( aId, aVal );
       
   109     if (retval != KErrNone)
       
   110         {
       
   111         FLOG(_L("[OBEXUTILS]\t GetCenRepKeyIntValueL() Get failed"));
       
   112         }
       
   113     CleanupStack::PopAndDestroy( storage );
       
   114     return retval;
       
   115     }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // GetCurrentTime Gets current time
       
   119 // Returns: Current time
       
   120 // -----------------------------------------------------------------------------
       
   121 //
       
   122 TTime TObexUtilsMessageHandler::GetCurrentTime()
       
   123     {
       
   124     TTime time;
       
   125     time.UniversalTime();
       
   126     return time;
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // StoreAsRichTextL Change message content to Richtext
       
   131 // Returns: None.
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 void TObexUtilsMessageHandler::StoreAsRichTextL( 
       
   135     CMsvEntry* aParentEntry,  // A pointer to new message server entry.
       
   136     RFile& aFile)             // New file to store
       
   137     {
       
   138     FLOG(_L("[OBEXUTILS]\t StoreAsRichTextL()"));
       
   139 
       
   140     // Read the file into buffer
       
   141     
       
   142     CBufFlat* buffer = CBufFlat::NewL( 16 );
       
   143     CleanupStack::PushL(buffer);  // 1st push
       
   144 
       
   145     TInt fileLength = 0;
       
   146     User::LeaveIfError( aFile.Size( fileLength ) );
       
   147     
       
   148     buffer->ResizeL( fileLength );
       
   149     TPtr8 bufferPtr = buffer->Ptr(0);
       
   150     
       
   151     User::LeaveIfError( aFile.Read( bufferPtr ) );
       
   152     
       
   153     // In order for Bio to understand 8-bit binary data, 
       
   154     // it needs to be stored in wide rich text object
       
   155     // in the low byte of each word with the upper byte as 0.
       
   156     // Do not use UTF8, just convert 8-bit to 16-bit.
       
   157 
       
   158     CRichText* richText = ConstructRichTextLC(); // 2nd, 3rd, 4th push
       
   159     HBufC* convert8BitTo16Bit = HBufC::NewLC(fileLength);  // 5th push
       
   160     convert8BitTo16Bit->Des().Copy( bufferPtr );
       
   161     buffer->Reset();  // free unused memory before InsertL()
       
   162     
       
   163     richText->InsertL(0, *convert8BitTo16Bit);
       
   164     CleanupStack::PopAndDestroy(convert8BitTo16Bit);
       
   165     // -1 pop: free unused memory before StoreBodyTextL()
       
   166 
       
   167     CMsvStore* parentStore = aParentEntry->EditStoreL();
       
   168     CleanupStack::PushL(parentStore);   // 5th push
       
   169     parentStore->StoreBodyTextL(*richText);
       
   170     parentStore->CommitL();
       
   171 
       
   172     CleanupStack::PopAndDestroy(5);  // parentStore, richText, richParaFormatLayer,
       
   173                                      // richCharFormatLayer, buffer
       
   174 
       
   175     FLOG(_L("[OBEXUTILS]\t StoreAsRichTextL() completed"));
       
   176     }
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // UpdateAttachmentInformationL Add the obex object as a file attachment.
       
   180 // Returns: None.
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 void TObexUtilsMessageHandler::UpdateAttachmentInformationL(
       
   184     TReceivedData& aRecData,      // The obex received data
       
   185     CMsvEntry* aAttachEntry,      // Attachment entry
       
   186     CMsvAttachment* aAttachInfo,  // Attachment information
       
   187     CMsvStore* aStore)            // Message store (attachment)
       
   188     {
       
   189     FLOG(_L("[OBEXUTILS]\t UpdateAttachmentInformationL()"));
       
   190 
       
   191     TMsvEntry attachTEntry = aAttachEntry->Entry();
       
   192 
       
   193     SetDetailsLC(attachTEntry.iDescription, aAttachInfo->MimeType());
       
   194     attachTEntry.iSize = aRecData.bytesReceived;
       
   195     attachTEntry.iDate = aRecData.recTime;
       
   196     aAttachEntry->ChangeL(attachTEntry);  // Save properties.
       
   197     CleanupStack::PopAndDestroy(); // unicode in SetDetailsLC
       
   198 
       
   199     aAttachInfo->SetSize(aRecData.bytesReceived);
       
   200     aAttachInfo->SetAttachmentNameL(aRecData.msgName);
       
   201     aAttachInfo->SetComplete(ETrue);
       
   202 
       
   203     aStore->AttachmentManagerExtensionsL().ModifyAttachmentInfoL(aAttachInfo);
       
   204     
       
   205     FLOG(_L("[OBEXUTILS]\t UpdateAttachmentInformation() completed"));
       
   206     }
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // FinaliseMessageL Save the size of all the attachments and make visible.
       
   210 // Returns: None.
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 void TObexUtilsMessageHandler::FinaliseMessageL(
       
   214     CMsvEntry* aParentEntry,  // Parent entry
       
   215     TReceivedData& aRecData,  // The obex received data
       
   216     CMsvStore* aStore,        // Attachment's store
       
   217     TBool aIsBioMsg)          // Is BIO message?
       
   218     {
       
   219     FLOG(_L("[OBEXUTILS]\t FinaliseMessageL()"));
       
   220     // Save the size of all the attachment & make visible.   
       
   221     TMsvEntry parentTEntry = aParentEntry->Entry();
       
   222     parentTEntry.iDate = GetCurrentTime();
       
   223     parentTEntry.iSize = aRecData.bytesReceived;
       
   224     parentTEntry.SetUnread(ETrue);
       
   225     
       
   226     // Saved OK. Make the entry visible and flag it as complete.
       
   227     parentTEntry.SetVisible(ETrue);                   // visible
       
   228     parentTEntry.SetInPreparation(EFalse);            // complete
       
   229     if (!aIsBioMsg)
       
   230         {
       
   231         parentTEntry.iDescription.Set(aRecData.msgName);  // "Subject"
       
   232         }
       
   233     aParentEntry->ChangeL(parentTEntry);              // commit changes
       
   234 
       
   235     aStore->CommitL();
       
   236     
       
   237     FLOG(_L("[OBEXUTILS]\t FinaliseMessageL() completed"));
       
   238     }
       
   239 
       
   240 // -----------------------------------------------------------------------------
       
   241 // FinaliseMessageL Save the size of all the attachments and make visible.
       
   242 // for Saving file received via BT/IR directly to file system.
       
   243 // Returns: None.
       
   244 // -----------------------------------------------------------------------------
       
   245 //
       
   246 void TObexUtilsMessageHandler::FinaliseMessageL(
       
   247     CMsvEntry* aParentEntry,
       
   248     CMsvStore* aStore,
       
   249     TEntry& aFileEntry,
       
   250     TParse& aFileNameParser)
       
   251     {
       
   252     FLOG(_L("[OBEXUTILS]\t FinaliseMessageL() new "));
       
   253     
       
   254     // Save the size of all the attachment & make visible.
       
   255     TMsvEntry parentTEntry = aParentEntry->Entry();
       
   256     parentTEntry.iDate = GetCurrentTime();
       
   257     parentTEntry.iSize = aFileEntry.iSize; 
       
   258     parentTEntry.SetUnread(EFalse);    
       
   259     // Saved OK. Make the entry visible and flag it as complete.
       
   260     parentTEntry.SetVisible(ETrue);                   // visible
       
   261     parentTEntry.SetInPreparation(EFalse);            // complete
       
   262     parentTEntry.iDescription.Set(aFileNameParser.Name());  // "Subject"
       
   263     aParentEntry->ChangeL(parentTEntry);              // commit changes
       
   264     aStore->CommitL();
       
   265     
       
   266     FLOG(_L("[OBEXUTILS]\t FinaliseMessageL() new completed "));
       
   267     }
       
   268 
       
   269 // -----------------------------------------------------------------------------
       
   270 // StoreAndUpdateBioMessageL Stores and updates a BIO message
       
   271 // Returns: None.
       
   272 // -----------------------------------------------------------------------------
       
   273 //
       
   274 void TObexUtilsMessageHandler::StoreAndUpdateBioMessageL(
       
   275     CBIODatabase* aBioDB,     // Bio database
       
   276     TUid aBioMsgId,           // Bio message ID
       
   277     CMsvEntry* parentEntry,   // Parent message entry
       
   278     RFile& aFile)             // File to attachment
       
   279     {
       
   280     FLOG(_L("[OBEXUTILS]\t StoreAndUpdateBioMessageL()"));
       
   281     
       
   282     TInt index;
       
   283     aBioDB->GetBioIndexWithMsgIDL(aBioMsgId, index);
       
   284     
       
   285     StoreAsRichTextL( parentEntry, aFile );
       
   286     
       
   287     TMsvEntry parentTEntry = parentEntry->Entry();
       
   288     parentTEntry.iMtm = KUidBIOMessageTypeMtm;
       
   289     parentTEntry.iServiceId = KMsvLocalServiceIndexEntryId;  
       
   290     parentTEntry.iBioType = aBioMsgId.iUid;
       
   291     parentTEntry.iDescription.Set(aBioDB->BifReader(index).Description());
       
   292     parentEntry->ChangeL(parentTEntry);
       
   293     
       
   294     FLOG(_L("[OBEXUTILS]\t StoreAndUpdateBioMessageL() completed"));
       
   295     }
       
   296 
       
   297 // -----------------------------------------------------------------------------
       
   298 // RecogniseObjectsL Recognised objects if no type field has been given.
       
   299 // Returns: None.
       
   300 // -----------------------------------------------------------------------------
       
   301 //
       
   302 void TObexUtilsMessageHandler::RecogniseObjectsL(
       
   303     TFileName& aFileName,
       
   304     CMsvAttachment* aAttachInfo)  // Attachment information
       
   305     {
       
   306     FLOG(_L("[OBEXUTILS]\t RecogniseObjectsL()"));
       
   307 
       
   308     TPtrC8 mimeType;
       
   309     RApaLsSession lsSess;
       
   310     User::LeaveIfError( lsSess.Connect() );
       
   311     CleanupClosePushL(lsSess);
       
   312     TDataType appDataType;
       
   313     TUid appUid;
       
   314     if (lsSess.AppForDocument(aFileName, appUid, appDataType) == KErrNone)
       
   315         {
       
   316         mimeType.Set(appDataType.Des8());
       
   317         }
       
   318     else
       
   319         {
       
   320         // Don't trust on sender's MIME type here. Just set to empty.
       
   321         mimeType.Set(KNullDesC8);
       
   322         FLOG(_L("[OBEXUTILS]\t RecogniseObjectsL() MIME check failed"));
       
   323         }
       
   324     CleanupStack::PopAndDestroy();  // lsSess
       
   325     HBufC* buf16 = HBufC::NewLC(mimeType.Length());
       
   326     buf16->Des().Copy(mimeType);
       
   327     TPtrC mimeType16(buf16->Des());
       
   328     CleanupStack::PopAndDestroy();   //   buf16
       
   329     
       
   330     CUpdateMusicCollection* updateMusiccollection =CUpdateMusicCollection::NewL() ;
       
   331     if (updateMusiccollection->isSupported(mimeType16))
       
   332         {
       
   333         updateMusiccollection->addToCollectionL(aFileName);
       
   334         }
       
   335     
       
   336     aAttachInfo->SetMimeTypeL( mimeType );
       
   337     
       
   338     FLOG(_L("[OBEXUTILS]\t RecogniseObjectsL() completed"));
       
   339     }
       
   340 
       
   341 // -----------------------------------------------------------------------------
       
   342 // SaveMimeAndGetBioLC Saves MIME type and gets BIO database for that MIME
       
   343 // Returns: BIO database if BIO message
       
   344 // -----------------------------------------------------------------------------
       
   345 //
       
   346 CBIODatabase* TObexUtilsMessageHandler::SaveMimeAndGetBioLC(
       
   347     TFileName& aFileName,
       
   348     RFs& aFsSess,                 // File session
       
   349     CMsvAttachment* aAttachInfo,  // Attachment information
       
   350     TUid &aBioMsgId)              // BIO message ID for data
       
   351     {
       
   352     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::SaveMimeAndGetBioLC()"));
       
   353     
       
   354     RecogniseObjectsL( aFileName, aAttachInfo );
       
   355 
       
   356     // For attachments / BIO check.
       
   357     CBIODatabase* bioDB = CBIODatabase::NewLC( aFsSess );  // 1st push
       
   358     
       
   359     // Run through the data objects, seeing if they can be accepted by BIO.
       
   360     // Check if they are bio objects
       
   361 
       
   362     TBioMsgIdText bioMessageIdText;
       
   363     SetBioDataTextL( bioMessageIdText, aAttachInfo->MimeType() );
       
   364 
       
   365     // Remove any zero termination on mime type
       
   366     //
       
   367     TInt zeroTermination = bioMessageIdText.Locate( TChar(0) ); 
       
   368     
       
   369     if ( zeroTermination != KErrNotFound )
       
   370         {
       
   371         bioMessageIdText.SetLength( zeroTermination );
       
   372         }
       
   373 
       
   374     TInt bioMsgErr = bioDB->IsBioMessageL( EBioMsgIdIana, bioMessageIdText, NULL, aBioMsgId );
       
   375     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::SaveMimeAndGetBioLC() completed"));
       
   376     if (bioMsgErr != KErrNone)
       
   377         {
       
   378         return NULL;
       
   379         }
       
   380     return bioDB;
       
   381     }
       
   382 
       
   383 // ============================ MEMBER FUNCTIONS ===============================
       
   384 
       
   385 // -----------------------------------------------------------------------------
       
   386 // CreateInboxAttachmentL Creates inbox entry and attachment for it
       
   387 // If this leaves, it means the entry and obex object have already been deleted.
       
   388 // In this case aMsvIdParent has been set to zero.
       
   389 // Returns: None.
       
   390 // -----------------------------------------------------------------------------
       
   391 //
       
   392 EXPORT_C void TObexUtilsMessageHandler::CreateInboxAttachmentL(
       
   393     CObexBufObject* &aBufObject,  // Obex object
       
   394     const TUid aMtmId,            // Mtm ID of the message
       
   395     TMsvId &aMsvIdParent,         // Message ID of the parent message entry
       
   396     RFile &aFile)                 // File to the attachment
       
   397     {
       
   398     TMsvId msvId = 0;
       
   399     TRAPD(retTrap, DoCreateInboxAttachmentL(aMtmId, msvId, aFile,KNullDesC))
       
   400     if (retTrap != KErrNone)
       
   401         {
       
   402         aMsvIdParent = 0;
       
   403         if (msvId)
       
   404             {
       
   405             RemoveInboxEntriesL(aBufObject, msvId);
       
   406             }
       
   407         User::Leave(retTrap);
       
   408         }
       
   409     aMsvIdParent = msvId;
       
   410     }
       
   411 
       
   412 // -----------------------------------------------------------------------------
       
   413 // CreateInboxAttachmentL Creates inbox entry and attachment for it
       
   414 // If this leaves, it means the entry and obex object have already been deleted.
       
   415 // In this case aMsvIdParent has been set to zero.
       
   416 // Returns: None.
       
   417 // -----------------------------------------------------------------------------
       
   418 //
       
   419 EXPORT_C void TObexUtilsMessageHandler::CreateInboxAttachmentL(
       
   420     CObexBufObject* &aBufObject,  // Obex object
       
   421     const TUid aMtmId,            // Mtm ID of the message
       
   422     TMsvId &aMsvIdParent,         // Message ID of the parent message entry
       
   423     RFile &aFile,                 // File to the attachment
       
   424     const TDesC& aRemoteBluetoothName)  // bluetooth name of remote device
       
   425     {
       
   426     TMsvId msvId = 0;
       
   427     TRAPD(retTrap, DoCreateInboxAttachmentL(aMtmId, msvId, aFile,aRemoteBluetoothName))
       
   428     if (retTrap != KErrNone)
       
   429         {
       
   430         aMsvIdParent = 0;
       
   431         if (msvId)
       
   432             {
       
   433             RemoveInboxEntriesL(aBufObject, msvId);
       
   434             }
       
   435         User::Leave(retTrap);
       
   436         }
       
   437     aMsvIdParent = msvId;
       
   438     }
       
   439 
       
   440 // -----------------------------------------------------------------------------
       
   441 // DoCreateInboxAttachmentL Creates inbox entry and attachment for it (private)
       
   442 // Returns: None.
       
   443 // -----------------------------------------------------------------------------
       
   444 //
       
   445 void TObexUtilsMessageHandler::DoCreateInboxAttachmentL(
       
   446     const TUid aMtmId,     // Mtm ID of the message
       
   447     TMsvId &aMsvIdParent,  // Message ID of the parent message entry
       
   448     RFile &aFile,          // File to the attachment
       
   449     const TDesC& aRemoteBluetoothName)  // bluetooth name of remote device
       
   450     {
       
   451     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::CreateInboxAttachmentL()"));
       
   452     
       
   453     CDummySessionObserver* sessionObs;
       
   454     CMsvSession* msvSession;
       
   455     CMsvEntry* inbox;
       
   456     CreateMsvSessionLC(sessionObs, msvSession, inbox, KMsvGlobalInBoxIndexEntryId);
       
   457     // 1st, 2nd, 3rd push
       
   458 
       
   459     TBuf<KObexUtilsMaxCharToFromField> toFrom;
       
   460     TInt resourceId;
       
   461 
       
   462     if (aMtmId == KUidMsgTypeBt)
       
   463         {
       
   464         resourceId = R_BT_TO_FROM_IN_INBOX;
       
   465         }
       
   466     else
       
   467         {
       
   468         resourceId = R_IR_TO_FROM_IN_INBOX;
       
   469         }
       
   470    
       
   471     TObexUtilsUiLayer::ReadResourceL( toFrom, resourceId );
       
   472     
       
   473     TMsvEntry parentTEntry;
       
   474     parentTEntry.iMtm = aMtmId;
       
   475     parentTEntry.SetVisible(EFalse); // Make invisible..
       
   476     parentTEntry.SetUnread(ETrue);
       
   477     
       
   478     // ...and in preparation to make sure it gets cleaned up on errors.
       
   479     //
       
   480     parentTEntry.SetInPreparation(ETrue); 
       
   481     parentTEntry.iType = KUidMsvMessageEntry;
       
   482     parentTEntry.iServiceId = KMsvUnknownServiceIndexEntryId;
       
   483     // iDate & iSize will be set later
       
   484     if ( aRemoteBluetoothName.Length() > 0 )
       
   485         {
       
   486         parentTEntry.iDetails.Set(aRemoteBluetoothName);
       
   487         }
       
   488     else
       
   489         {
       
   490         parentTEntry.iDetails.Set(toFrom);
       
   491         }
       
   492     inbox->CreateL(parentTEntry);
       
   493     
       
   494     aMsvIdParent = parentTEntry.Id();
       
   495     
       
   496     // Create attachment for the inbox entry
       
   497     
       
   498     TMsvEntry attachTEntry;
       
   499     attachTEntry.iType      = KUidMsvAttachmentEntry;
       
   500     attachTEntry.iServiceId = KMsvUnknownServiceIndexEntryId;   
       
   501     attachTEntry.iMtm       = aMtmId;
       
   502     
       
   503     CMsvEntry* parentEntry = msvSession->GetEntryL(aMsvIdParent);
       
   504     CleanupStack::PushL(parentEntry);  // 4th push
       
   505     parentEntry->CreateL(attachTEntry);  // create attachment as child of parent
       
   506     
       
   507     // create the v2 attachment, save RFile
       
   508     
       
   509     CMsvEntry* attachEntry = msvSession->GetEntryL(attachTEntry.Id());
       
   510     CleanupStack::PushL(attachEntry);  // 5th push
       
   511     
       
   512     CMsvStore* store = attachEntry->EditStoreL();
       
   513     CleanupStack::PushL(store);  // 6th push
       
   514     CMsvAttachment* attachment = CMsvAttachment::NewL(CMsvAttachment::EMsvFile);
       
   515     CleanupStack::PushL(attachment);  // 7th push
       
   516      
       
   517     store->AttachmentManagerExtensionsL().CreateAttachmentL(_L("Unnamed"), aFile, attachment);
       
   518     CleanupStack::Pop(attachment);
       
   519     store->CommitL();
       
   520     // CommitL() required here otherwise EditStoreL() will fail in SaveObjToInbox()
       
   521     
       
   522     CleanupStack::PopAndDestroy(6);
       
   523     // store, attachEntry, parentEntry, inbox, msvSession, sessionObs
       
   524 
       
   525     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::CreateInboxAttachmentL() completed"));
       
   526     }
       
   527 
       
   528 // -----------------------------------------------------------------------------
       
   529 // CObexUtilsMessageHandler::RemoveInboxEntriesL
       
   530 // -----------------------------------------------------------------------------
       
   531 EXPORT_C void TObexUtilsMessageHandler::RemoveInboxEntriesL(
       
   532     CObexBufObject* &aBufObject, // Obex object
       
   533     TMsvId aMsvIdParent)         // Message ID of the parent message entry
       
   534     {
       
   535     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::RemoveInboxEntriesL()"));
       
   536     
       
   537     CDummySessionObserver* sessionObs;
       
   538     CMsvSession* msvSession;
       
   539     CMsvEntry* inbox;
       
   540     CreateMsvSessionLC(sessionObs, msvSession, inbox, KMsvGlobalInBoxIndexEntryId);
       
   541     // 1st, 2nd, 3rd push
       
   542     
       
   543     // Close file
       
   544     
       
   545     RemoveObexBuffer(aBufObject);
       
   546     
       
   547     // Delete parent and descendants
       
   548     if ( aMsvIdParent )
       
   549         {
       
   550         inbox->DeleteL(aMsvIdParent);
       
   551         }
       
   552     
       
   553     CleanupStack::PopAndDestroy(3);
       
   554     
       
   555     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::RemoveInboxEntriesL() completed"));
       
   556     }
       
   557 
       
   558 // -----------------------------------------------------------------------------
       
   559 // CObexUtilsMessageHandler::SaveObjToInboxL
       
   560 // -----------------------------------------------------------------------------
       
   561 //
       
   562 EXPORT_C void TObexUtilsMessageHandler::SaveObjToInboxL( 
       
   563     CObexBufObject* /*aReceivedObject*/, const TDesC16& /*aPath*/, const TUid /*aMtmID*/ )
       
   564     {
       
   565     }
       
   566 
       
   567 // -----------------------------------------------------------------------------
       
   568 // CObexUtilsMessageHandler::SaveObjToInboxL
       
   569 // -----------------------------------------------------------------------------
       
   570 //
       
   571 EXPORT_C void TObexUtilsMessageHandler::SaveObjToInboxL(
       
   572     CObexBufObject* &aReceivedObject, // Obex object
       
   573     RFile  &aFile,         
       
   574     TMsvId aMsvIdParent)              // Message ID of the parent message entry
       
   575     {
       
   576     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::SaveObjToInboxL()"));
       
   577 
       
   578     // Open session. 
       
   579     CDummySessionObserver* sessionObs;
       
   580     CMsvSession* msvSession;
       
   581     CreateMsvSessionLC(sessionObs, msvSession);  // 1st, 2nd push
       
   582 
       
   583     CMsvEntry* parentEntry = msvSession->GetEntryL(aMsvIdParent);
       
   584     CleanupStack::PushL( parentEntry );  // 3rd push
       
   585 
       
   586 #ifdef __BT_SAP
       
   587     if(CSapVMessageParser::HandleMessageL(aReceivedObject, parentEntry->Entry().iMtm,
       
   588         aFile, msvSession, GetCurrentTime()))
       
   589         {
       
   590         RemoveObexBuffer(aReceivedObject);
       
   591         CMsvEntry* inbox = msvSession->GetEntryL( KMsvGlobalInBoxIndexEntryId );
       
   592         CleanupStack::PushL( inbox );         //4th push
       
   593         inbox->DeleteL(aMsvIdParent);
       
   594         // Close session etc.
       
   595         CleanupStack::PopAndDestroy(4, sessionObs);  // inbox, parentEntry, session, sessionObs
       
   596         FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::SaveObjToInboxL() completed"));
       
   597         return;
       
   598         }
       
   599 #endif // __BT_SAP
       
   600     CMsvEntry* attachEntry = msvSession->GetEntryL(((*parentEntry)[0]).Id());
       
   601     CleanupStack::PushL(attachEntry); // 4th push
       
   602 
       
   603     CMsvStore* store = attachEntry->EditStoreL();
       
   604     CleanupStack::PushL( store );  // 5th push
       
   605 
       
   606     // Collect obex received data before obex buffer deletion
       
   607 
       
   608     TReceivedData receivedData;
       
   609     receivedData.bytesReceived = aReceivedObject->BytesReceived();
       
   610     receivedData.recTime = aReceivedObject->Time();
       
   611 
       
   612     RFs& fsSess = msvSession->FileSession();
       
   613     receivedData.msgName = aReceivedObject->Name();
       
   614     MakeValidFileName(fsSess, receivedData.msgName);
       
   615 
       
   616     // Rename attachment for MIME type checking
       
   617     
       
   618     RemoveObexBuffer(aReceivedObject);
       
   619     store->AttachmentManagerExtensionsL().RenameAttachmentL(0, receivedData.msgName);
       
   620     RFile newFile = store->AttachmentManagerL().GetAttachmentFileL(0);
       
   621     CleanupClosePushL(newFile);  // 6th push
       
   622     
       
   623     // RenameAttachmentL() modified attachment info, so read it now
       
   624     
       
   625     CMsvAttachment* attachInfo = store->AttachmentManagerL().GetAttachmentInfoL(0);
       
   626     CleanupStack::PushL( attachInfo ); // 7th push
       
   627 
       
   628     TUid bioMsgId;  // SaveMimeAndGetBioLC modifies this
       
   629     TFileName fileName;
       
   630     newFile.FullName(fileName);
       
   631     CBIODatabase* bioDB = SaveMimeAndGetBioLC( fileName, fsSess, attachInfo, bioMsgId );
       
   632     // 8th push
       
   633     if (bioDB)  // bio message
       
   634         {
       
   635         FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::SaveObjToInboxL() BIO"));
       
   636         StoreAndUpdateBioMessageL( bioDB, bioMsgId, parentEntry, newFile );
       
   637         FinaliseMessageL(parentEntry, receivedData, store, ETrue);
       
   638         TMsvId attachId = attachEntry->EntryId();
       
   639         CleanupStack::PopAndDestroy(5);  // bioDB, attachInfo, newFile (close),
       
   640                                          // store, attachEntry
       
   641         // PopAndDestroying here because open store locks the entry,
       
   642         // it can be deleted only after destroying the store pointer
       
   643         parentEntry->DeleteL(attachId);  // invalidate unused attachment
       
   644         CleanupStack::PopAndDestroy(3);  // parentEntry, msvSession, sessionObs
       
   645         FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::SaveObjToInboxL() BIO completed"));
       
   646         }
       
   647     else  // attachment message
       
   648         {
       
   649         FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::SaveObjToInboxL() Attachment"));
       
   650         CleanupStack::PopAndDestroy();   // bioDB
       
   651         UpdateAttachmentInformationL(receivedData, attachEntry, attachInfo, store);
       
   652         CleanupStack::Pop(attachInfo);   // ownership taken by ModifyAttachmentInfoL()
       
   653         FinaliseMessageL(parentEntry, receivedData, store, EFalse);
       
   654         CleanupStack::PopAndDestroy(6);  // newFile (close), store, attachEntry,
       
   655                                          // parentEntry, msvSession, sessionObs
       
   656         FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::SaveObjToInboxL() Attachment completed"));
       
   657         }
       
   658 
       
   659     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::SaveObjToInboxL() completed"));
       
   660     }
       
   661 
       
   662 
       
   663 // -----------------------------------------------------------------------------
       
   664 // GetCenRepKeyStringValueL Gets integer repository value
       
   665 // Returns: Error
       
   666 // -----------------------------------------------------------------------------
       
   667 //
       
   668 EXPORT_C TInt TObexUtilsMessageHandler::GetCenRepKeyStringValueL(
       
   669     TUid aRepositoryUid,  // aRepositoryUid Central repository's UID value
       
   670     TUint32 aId,          // Central repository's ID value
       
   671     TDes& aVal)           // Returned String
       
   672     {
       
   673     CRepository* storage = CRepository::NewLC( aRepositoryUid );
       
   674     TInt retval = storage->Get( aId, aVal );
       
   675     if (retval != KErrNone)
       
   676         {
       
   677         FLOG(_L("[OBEXUTILS]\t GetCenRepKeyStringValueL() Get failed"));
       
   678         }
       
   679     CleanupStack::PopAndDestroy( storage );
       
   680     return retval;
       
   681     }
       
   682 
       
   683 // -----------------------------------------------------------------------------
       
   684 // CreateReceiveBufferL Creates an Obex receive buffer object and RFile object.
       
   685 // Returns: Symbian error code
       
   686 // -----------------------------------------------------------------------------
       
   687 //
       
   688 
       
   689 EXPORT_C TInt TObexUtilsMessageHandler::CreateReceiveBufferAndRFileL(
       
   690     RFile& aFile,
       
   691     const TDesC& aPath,
       
   692     TFileName& aFullName, 
       
   693     CBufFlat* &aReceiveObject, 
       
   694     const TInt aBufSize )  
       
   695     {
       
   696     
       
   697     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::CreateReceiveBufferAndRFileL()"));
       
   698     
       
   699     aReceiveObject = CBufFlat::NewL(aBufSize); 
       
   700     aReceiveObject->ResizeL(aBufSize);
       
   701     
       
   702     
       
   703     RFs rfs;
       
   704     User::LeaveIfError( rfs.Connect() );
       
   705     if ( !BaflUtils::FolderExists( rfs, aPath ) )
       
   706         {
       
   707         User::LeaveIfError(rfs.MkDirAll(aPath));
       
   708         }
       
   709     User::LeaveIfError( aFile.Temp( rfs, aPath, aFullName, EFileWrite) );
       
   710     // Set temp file  hidden file.
       
   711     //
       
   712     TUint setAttMask(0);
       
   713     TUint clearAttMask(0);
       
   714     aFile.SetAtt(setAttMask | KEntryAttHidden, clearAttMask);
       
   715     aFile.Flush();
       
   716     aFile.Close();
       
   717     
       
   718     RFile logFile;
       
   719     TFileName logFilePath;
       
   720     TFileName cenRepFolder;
       
   721     GetCenRepKeyStringValueL(KCRUidBluetoothEngine, KLCReceiveFolder,cenRepFolder);
       
   722     
       
   723     logFilePath.Zero();
       
   724     logFilePath.Append(PathInfo::PhoneMemoryRootPath());
       
   725     logFilePath.Append(cenRepFolder);
       
   726     
       
   727     if ( !BaflUtils::FolderExists(rfs , logFilePath) )
       
   728         {
       
   729         User::LeaveIfError(rfs.MkDirAll(logFilePath));
       
   730         }
       
   731     logFilePath.Append(_L("log.txt"));
       
   732     if ( !BaflUtils::FileExists( rfs, logFilePath ))
       
   733         {
       
   734         logFile.Create(rfs, logFilePath, EFileWrite);
       
   735         }
       
   736     else
       
   737         {
       
   738         User::LeaveIfError(logFile.Open(rfs, logFilePath, EFileWrite));
       
   739         }
       
   740     
       
   741     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::CreateReceiveBufferAndRFileL()    log.txt is created"));
       
   742     TFileText ft;              
       
   743     ft.Set(logFile);
       
   744     ft.Write(aFullName);    // restore tempfilename in logfile
       
   745 
       
   746     // Set log file 
       
   747     logFile.SetAtt(setAttMask | KEntryAttHidden, clearAttMask);
       
   748     logFile.Flush();
       
   749     logFile.Close();
       
   750     rfs.Close();
       
   751     
       
   752     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::CreateReceiveBufferAndRFileL() completed"));
       
   753     
       
   754     return KErrNone;
       
   755     }
       
   756 
       
   757 // -----------------------------------------------------------------------------
       
   758 // CObexUtilsMessageHandler::RemoveTemporaryRFileL
       
   759 // -----------------------------------------------------------------------------
       
   760 //
       
   761 EXPORT_C void TObexUtilsMessageHandler::RemoveTemporaryRFileL ( const TFileName& aFullName )
       
   762     {
       
   763     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::RemoveTemporaryRFileL()"));
       
   764     RFs rfs;
       
   765     User::LeaveIfError(rfs.Connect());
       
   766     
       
   767     TParse fileNameParser;
       
   768     User::LeaveIfError(fileNameParser.Set(aFullName, NULL, NULL));
       
   769     
       
   770     TFileName logFilePath;
       
   771     TFileName cenRepFolder;
       
   772     GetCenRepKeyStringValueL(KCRUidBluetoothEngine, KLCReceiveFolder,cenRepFolder);
       
   773     logFilePath.Zero();
       
   774     logFilePath.Append(PathInfo::PhoneMemoryRootPath());
       
   775     logFilePath.Append(cenRepFolder);
       
   776     logFilePath.Append(_L("log.txt"));
       
   777             
       
   778     if ( fileNameParser.Ext().Compare(_L(".$$$")) == 0  )                                                                           
       
   779         {
       
   780         // aFullName is a temp file name, we need to remove it.        
       
   781         rfs.Delete( aFullName );
       
   782         FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::RemoveTemporaryRFileL() temp file is removed (bt off)"));
       
   783         }
       
   784     else if ( (aFullName.Length() == 0) && BaflUtils::FileExists(rfs, logFilePath))
       
   785         {
       
   786         RFile logFile;
       
   787         User::LeaveIfError(logFile.Open(rfs, logFilePath, EFileRead));
       
   788         TFileText ft;
       
   789         ft.Set(logFile);
       
   790         TFileName tempFileName;
       
   791         ft.Read(tempFileName);  // read tempfile name stored in logfile.
       
   792         
       
   793         if ( tempFileName.Length() > 0 )
       
   794             {
       
   795             rfs.Delete(tempFileName);
       
   796             }
       
   797         logFile.Close();   
       
   798         FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::RemoveTemporaryRFileL() temp files are removed (reboot)"));
       
   799         }              
       
   800     rfs.Delete( logFilePath );
       
   801     rfs.Close();
       
   802     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::RemoveTemporaryRFileL() completed"));
       
   803     }
       
   804 // -----------------------------------------------------------------------------
       
   805 // CObexUtilsMessageHandler::SaveFileToFileSystemL
       
   806 // -----------------------------------------------------------------------------
       
   807 //
       
   808 EXPORT_C void TObexUtilsMessageHandler::SaveFileToFileSystemL(
       
   809     CObexBufObject* &aReceivedObject,
       
   810     const TUid aMtmId,
       
   811     TMsvId& aMsvIdParent,
       
   812     TFileName& aFullName, 
       
   813     RFile& aFile,
       
   814     const TDesC& aSenderBluetoothName)
       
   815     {
       
   816    
       
   817     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::SaveFileToFileSystemL()"));
       
   818     // Create inbox entry ( this was done in CreateAttachment() before )
       
   819     //
       
   820     CDummySessionObserver* sessionObs;
       
   821     CMsvSession* msvSession;
       
   822     CMsvEntry* inbox;
       
   823     CreateMsvSessionLC(sessionObs, msvSession, inbox, KMsvGlobalInBoxIndexEntryId);
       
   824     // 1st, 2nd, 3rd push
       
   825 
       
   826     TBuf<KObexUtilsMaxCharToFromField> toFrom;
       
   827     TInt resourceId;
       
   828 
       
   829     if (aMtmId == KUidMsgTypeBt)
       
   830         {
       
   831         resourceId = R_BT_TO_FROM_IN_INBOX;
       
   832         }
       
   833     else
       
   834         {
       
   835         resourceId = R_IR_TO_FROM_IN_INBOX;
       
   836         }
       
   837     // NOTE: toFrom will be updated to Bluetooth sender's name
       
   838     //
       
   839     TObexUtilsUiLayer::ReadResourceL( toFrom, resourceId );  // read the toForm ( will be updated to sender's bt name in the future ) 
       
   840 
       
   841     TMsvEntry parentTEntry;   
       
   842     parentTEntry.iMtm = aMtmId;
       
   843     parentTEntry.SetVisible(EFalse); // Make invisible..
       
   844     parentTEntry.SetUnread(EFalse);  // Msgs received via bluetooth will always be set to READ.
       
   845         
       
   846     // ...and in preparation to make sure it gets cleaned up on errors.
       
   847     //
       
   848     parentTEntry.SetInPreparation(ETrue); 
       
   849     parentTEntry.iType = KUidMsvMessageEntry;
       
   850     parentTEntry.iServiceId = KMsvUnknownServiceIndexEntryId;
       
   851     // iDate & iSize will be set later
       
   852     if ( aSenderBluetoothName.Length() > 0 )
       
   853         {
       
   854         parentTEntry.iDetails.Set(aSenderBluetoothName);
       
   855         }
       
   856     else
       
   857         {
       
   858         parentTEntry.iDetails.Set(toFrom);
       
   859         }
       
   860     inbox->CreateL(parentTEntry);
       
   861         
       
   862     aMsvIdParent = parentTEntry.Id();
       
   863         
       
   864     // Create attachment for the inbox entry
       
   865     
       
   866     TMsvEntry attachTEntry;
       
   867     attachTEntry.iType      = KUidMsvAttachmentEntry;
       
   868     attachTEntry.iServiceId = KMsvUnknownServiceIndexEntryId;   
       
   869     attachTEntry.iMtm       = aMtmId;
       
   870      
       
   871     CMsvEntry* parentEntry = msvSession->GetEntryL(aMsvIdParent);
       
   872     CleanupStack::PushL(parentEntry);  // 4th push
       
   873     parentEntry->CreateL(attachTEntry);  // create attachment as child of parent
       
   874   
       
   875     // Check if message is sap message
       
   876     // If we delete parent entry in sap, we have to delete the RFile object ourself as well )
       
   877     //
       
   878     RFs& fsSess = msvSession->FileSession();
       
   879     TFileName tempFullName;
       
   880     aFile.FullName(tempFullName);
       
   881     
       
   882 #ifdef __BT_SAP   
       
   883     if(CSapVMessageParser::HandleMessageL(aReceivedObject, parentEntry->Entry().iMtm,
       
   884             aFile, msvSession, GetCurrentTime()))
       
   885         {
       
   886         RemoveObexBuffer(aReceivedObject);
       
   887         CMsvEntry* inbox = msvSession->GetEntryL( KMsvGlobalInBoxIndexEntryId );
       
   888         CleanupStack::PushL( inbox );         //5th push
       
   889         inbox->DeleteL(aMsvIdParent);
       
   890         fsSess.Delete(tempFullName);  // we have to use fullname to delete the file object in file system 
       
   891         // Close session etc.
       
   892         CleanupStack::PopAndDestroy(5, sessionObs);  // inbox, parentEntry, session, sessionObs
       
   893         FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::SaveFileToFileSystemL() completed"));
       
   894         return;
       
   895         }
       
   896 #endif // __BT_SAP
       
   897 
       
   898     // Collect obex received data before obex buffer deletion
       
   899     TReceivedData receivedData;
       
   900     receivedData.bytesReceived = aReceivedObject->BytesReceived();
       
   901     receivedData.recTime = aReceivedObject->Time();
       
   902     receivedData.msgName = aReceivedObject->Name();
       
   903     
       
   904     TFileName filename;
       
   905     TParse parse;
       
   906     parse.Set(aFullName, NULL, NULL);
       
   907     filename.Copy(receivedData.msgName);
       
   908     FixExtremeLongFileName(filename, parse.DriveAndPath());
       
   909     MakeValidFileName( fsSess, filename);     
       
   910     RemoveObexBuffer(aReceivedObject);
       
   911 
       
   912     // We set file to visible before renaming 
       
   913     // so that photo/music app can scan the media files we recevie. 
       
   914     //
       
   915     // Set file back to visible
       
   916     //
       
   917     TUint setAttMask(0);
       
   918     TUint clearAttMask(0);
       
   919     User::LeaveIfError(aFile.Open(fsSess,tempFullName,EFileWrite));
       
   920     User::LeaveIfError(aFile.SetAtt(setAttMask , clearAttMask | KEntryAttHidden));
       
   921     aFile.Close();
       
   922     
       
   923     
       
   924     // Move file to the actual drive and directory
       
   925     // as well rename the file with the correct file name instead of temporary name 
       
   926     // tempFullName will be updated in RenameFileL()    
       
   927     TInt error = RenameFileL(tempFullName, filename, parse.DriveAndPath(), fsSess);
       
   928     
       
   929     
       
   930     
       
   931     if ( error != KErrNone )
       
   932         {
       
   933         fsSess.Delete(tempFullName);   // If rename fails, we remove the temp RFile object.
       
   934         }
       
   935     aFullName = tempFullName; // assign new full Name back to aFullName,which goes back to caller
       
   936     
       
   937     CleanupStack::PopAndDestroy(4);         // parentEntry, inbox, msvSession, sessionObs
       
   938      
       
   939     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::SaveFileToFileSystemL() completed"));
       
   940     }
       
   941 
       
   942 // -----------------------------------------------------------------------------
       
   943 // CObexUtilsMessageHandler::AddEntryToInboxL
       
   944 // -----------------------------------------------------------------------------
       
   945 //
       
   946 EXPORT_C void TObexUtilsMessageHandler::AddEntryToInboxL( 
       
   947     TMsvId& aMsvIdParent,
       
   948     TFileName& aFullName,
       
   949     RArray<TMsvId>* aMsvIdArray)
       
   950     {
       
   951     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::AddEntryToInboxL() "));
       
   952     
       
   953     CDummySessionObserver* sessionObs;
       
   954     CMsvSession* msvSession;
       
   955     CreateMsvSessionLC(sessionObs, msvSession);
       
   956     // 1st, 2nd push
       
   957     
       
   958     CMsvEntry* parentEntry = msvSession->GetEntryL(aMsvIdParent);
       
   959     CleanupStack::PushL(parentEntry);  // 3th push
       
   960     
       
   961     CMsvEntry* attachEntry = msvSession->GetEntryL(((*parentEntry)[0]).Id());
       
   962     CleanupStack::PushL(attachEntry); // 4th push
       
   963         
       
   964     CMsvStore* store = attachEntry->EditStoreL();
       
   965     CleanupStack::PushL( store );  // 5th push
       
   966     
       
   967     CObexutilsEntryhandler* entryHandler = CObexutilsEntryhandler::NewL();
       
   968     CleanupStack::PushL(entryHandler);  // 6th push  
       
   969             
       
   970     CMsvAttachment* attachInfo = CMsvAttachment::NewL(CMsvAttachment::EMsvLinkedFile);
       
   971     CleanupStack::PushL(attachInfo);  // 7th push
       
   972        
       
   973     // Create attachment Info
       
   974     //
       
   975     RFs& fsSess = msvSession->FileSession();
       
   976     
       
   977     TParse fileNameParser;
       
   978     User::LeaveIfError(fileNameParser.Set(aFullName, NULL, NULL));
       
   979     attachInfo->SetAttachmentNameL(fileNameParser.NameAndExt());
       
   980              
       
   981     TEntry fileEntry;
       
   982     User::LeaveIfError(fsSess.Entry( fileNameParser.FullName(), fileEntry) );
       
   983     attachInfo->SetSize(fileEntry.iSize);
       
   984         
       
   985     // check if it's the bio message
       
   986        
       
   987     TUid bioMsgId;  // SaveMimeAndGetBioLC modifies this
       
   988     CBIODatabase* bioDB = SaveMimeAndGetBioLC( aFullName, fsSess, attachInfo, bioMsgId );
       
   989     // 8th push
       
   990     if (bioDB)  // bio message
       
   991         {
       
   992         FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::AddEntryToInboxL() BIO"));
       
   993         
       
   994         RFile file;
       
   995         User::LeaveIfError(file.Open(fsSess,aFullName,EFileRead));
       
   996         TReceivedData receivedData;
       
   997         receivedData.bytesReceived = fileEntry.iSize;
       
   998         receivedData.recTime = fileEntry.iModified;
       
   999         receivedData.msgName = fileNameParser.Name();
       
  1000                 
       
  1001         StoreAndUpdateBioMessageL( bioDB, bioMsgId, parentEntry, file );
       
  1002         FinaliseMessageL(parentEntry, receivedData, store, ETrue);
       
  1003         TMsvId attachId = attachEntry->EntryId();
       
  1004         file.Close();
       
  1005         fsSess.Delete(aFullName);
       
  1006         CleanupStack::PopAndDestroy(5);  // bioDB, attachInfo, linkHandler,
       
  1007                                          // store, attachEntry
       
  1008         
       
  1009         // PopAndDestroying here because open store locks the entry,
       
  1010         // it can be deleted only after destroying the store pointer
       
  1011         parentEntry->DeleteL(attachId);  // invalidate unused attachment
       
  1012         CleanupStack::PopAndDestroy(3);  // parentEntry, msvSession, sessionObs
       
  1013         FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::AddEntryToInboxL() BIO completed"));
       
  1014         }
       
  1015     else
       
  1016         {
       
  1017 
       
  1018         CleanupStack::PopAndDestroy();  // bioDB
       
  1019         // Add entry to Inbox
       
  1020         entryHandler->AddEntryAttachment(aFullName, attachInfo, store);
       
  1021         CleanupStack::Pop(attachInfo);   // attachInfo, Pass ownership to store
       
  1022         CleanupStack::PopAndDestroy(entryHandler);  // entryHandler
       
  1023         FinaliseMessageL(parentEntry, store,fileEntry, fileNameParser);
       
  1024         CleanupStack::PopAndDestroy(5);         // linkHandler, attachInfo, store, attachEntry,
       
  1025                                                 // parentEntry, msvSession, sessionObs
       
  1026         
       
  1027         // File Link is created in INBOX successfully,
       
  1028         // We add MsvId to array.
       
  1029         //
       
  1030         aMsvIdArray->Append(aMsvIdParent);
       
  1031         
       
  1032         FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::AddEntryToInboxL() completed "));
       
  1033         }
       
  1034        
       
  1035     }
       
  1036 
       
  1037 // -----------------------------------------------------------------------------
       
  1038 // CObexUtilsMessageHandler::UpdateEntryAttachmentL
       
  1039 // -----------------------------------------------------------------------------
       
  1040 //
       
  1041 EXPORT_C void TObexUtilsMessageHandler::UpdateEntryAttachmentL ( 
       
  1042     TFileName& aFullName,
       
  1043     CMsvEntry* aParentEntry)
       
  1044     {
       
  1045     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::UpdateEntryAttachmentL() "));
       
  1046     CDummySessionObserver* sessionObs;
       
  1047     CMsvSession* msvSession;
       
  1048     CreateMsvSessionLC(sessionObs, msvSession);
       
  1049     // 1st, 2nd push
       
  1050       
       
  1051     CMsvEntry* attachEntry = msvSession->GetEntryL(((*aParentEntry)[0]).Id());
       
  1052     CleanupStack::PushL(attachEntry); // 3th push
       
  1053             
       
  1054     CMsvStore* store = attachEntry->EditStoreL();
       
  1055     CleanupStack::PushL( store );  // 4th push
       
  1056   
       
  1057     CObexutilsEntryhandler* entryHandler = CObexutilsEntryhandler::NewL();
       
  1058     CleanupStack::PushL(entryHandler);  // 5th push  
       
  1059     
       
  1060     // Note:
       
  1061     // Because setFilePath() in CMsvAttachment is not implementated by Symbian yet, 
       
  1062     // we have to delete the original attachment and add another new one to fix the broken link.
       
  1063     //
       
  1064     
       
  1065     // remove the old attachment first.
       
  1066     //
       
  1067     store->AttachmentManagerExtensionsL().RemoveAttachmentL(0);
       
  1068     // Create a new attachment.
       
  1069     //
       
  1070     CMsvAttachment* attachInfo = CMsvAttachment::NewL(CMsvAttachment::EMsvLinkedFile);
       
  1071     CleanupStack::PushL(attachInfo);  // 6th  push
       
  1072     
       
  1073     // Get mime type
       
  1074     //
       
  1075     RecogniseObjectsL(aFullName, attachInfo);
       
  1076     RFs& fsSess = msvSession->FileSession();
       
  1077     
       
  1078     TParse fileNameParser;
       
  1079     User::LeaveIfError(fileNameParser.Set(aFullName, NULL, NULL));
       
  1080     attachInfo->SetAttachmentNameL(fileNameParser.NameAndExt());
       
  1081     TEntry fileEntry;
       
  1082     User::LeaveIfError(fsSess.Entry( fileNameParser.FullName(), fileEntry) );
       
  1083     attachInfo->SetSize(fileEntry.iSize);
       
  1084 
       
  1085     entryHandler->AddEntryAttachment(aFullName,attachInfo, store);
       
  1086     CleanupStack::Pop(attachInfo);   // attachInfo, Pass ownership to store
       
  1087     CleanupStack::PopAndDestroy(entryHandler);  // entryHandler
       
  1088     FinaliseMessageL(aParentEntry, store,fileEntry, fileNameParser);
       
  1089     CleanupStack::PopAndDestroy(4);   // store, 
       
  1090                                       // attachEntry, msvSession, sessionObs
       
  1091     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::UpdateEntryAttachmentL() completed "));
       
  1092     }
       
  1093 
       
  1094 
       
  1095 // Common methods
       
  1096 
       
  1097 // -----------------------------------------------------------------------------
       
  1098 // ConstructRichTextLC Handles rich text initialisation.
       
  1099 // Returns: CRichText* A pointer to the rich text.
       
  1100 // -----------------------------------------------------------------------------
       
  1101 //
       
  1102 CRichText* TObexUtilsMessageHandler::ConstructRichTextLC()
       
  1103     {
       
  1104     FLOG(_L("[OBEXUTILS]\t ConstructRichTextLC()"));
       
  1105 
       
  1106     CParaFormatLayer* richParaFormatLayer = CParaFormatLayer::NewL();
       
  1107     CleanupStack::PushL( richParaFormatLayer );
       
  1108 
       
  1109     CCharFormatLayer* richCharFormatLayer = CCharFormatLayer::NewL(); 
       
  1110     CleanupStack::PushL( richCharFormatLayer );
       
  1111     CRichText* richText = CRichText::NewL( richParaFormatLayer, 
       
  1112                                            richCharFormatLayer );
       
  1113     CleanupStack::PushL(richText);
       
  1114 
       
  1115     FLOG(_L("[OBEXUTILS]\t ConstructRichTextLC() completed"));
       
  1116 
       
  1117     return richText;
       
  1118     }
       
  1119 
       
  1120 // -----------------------------------------------------------------------------
       
  1121 // MakeValidFileName Rename the given filename to a valid filename.
       
  1122 // Removes invalid characters from a filename; if no characters left, filename
       
  1123 // is set to R_NAME_FOR_INVALID_FILE
       
  1124 // Returns: None.
       
  1125 // -----------------------------------------------------------------------------
       
  1126 //
       
  1127 void TObexUtilsMessageHandler::MakeValidFileName( 
       
  1128     RFs& aFileSession, // A reference to file server session.
       
  1129     TDes& aFileName )  // The filename to modify.
       
  1130     {
       
  1131     FLOG(_L("[OBEXUTILS]\t MakeValidFileName()"));
       
  1132 
       
  1133     TParse fileParse;
       
  1134     fileParse.Set(aFileName, NULL, NULL);
       
  1135     
       
  1136     TBool nameempty = ETrue;
       
  1137     if (fileParse.NameOrExtPresent())
       
  1138         {        
       
  1139         aFileName = fileParse.NameAndExt();
       
  1140         nameempty=EFalse;
       
  1141         TText badChar;    
       
  1142         TInt nameLength = aFileName.Length();
       
  1143     
       
  1144         while ( !aFileSession.IsValidName(aFileName, badChar) )
       
  1145             {
       
  1146             // Locate and delete all instances of the bad char
       
  1147             FOREVER
       
  1148                 {
       
  1149                 TInt index = aFileName.Locate(TChar(badChar)); 
       
  1150                 nameLength--;
       
  1151                 if (index == KErrNotFound)
       
  1152                     {
       
  1153                     break;
       
  1154                     }             
       
  1155                 aFileName.Delete(index,1);               
       
  1156                 }
       
  1157                 if ( nameLength <= 0 )
       
  1158                     {
       
  1159                     break;
       
  1160                     }
       
  1161             }    
       
  1162         }    
       
  1163     
       
  1164     // Did we delete all the characters? If so then we must have some filename
       
  1165     if ( aFileName.Length() == 0 || nameempty )
       
  1166         {
       
  1167         TInt resourceId = R_NAME_FOR_INVALID_FILE;
       
  1168         TRAPD( retVal, TObexUtilsUiLayer::ReadResourceL( aFileName, resourceId ) );
       
  1169         if (retVal != KErrNone)
       
  1170             {
       
  1171             FLOG(_L("TObexUtilsMessageHandler::MakeValidFileName failed."));
       
  1172             }
       
  1173         }
       
  1174 
       
  1175     FLOG(_L("[OBEXUTILS]\t MakeValidFileName() completed"));
       
  1176     }
       
  1177 
       
  1178 // -----------------------------------------------------------------------------
       
  1179 // TObexUtilsMessageHandler::CreateOutboxEntryL
       
  1180 // -----------------------------------------------------------------------------
       
  1181 //
       
  1182 EXPORT_C TMsvId TObexUtilsMessageHandler::CreateOutboxEntryL(
       
  1183     const TUid& aMtm,          // Message type id
       
  1184     const TInt& aResourceID )  // Resource id for the message entry text
       
  1185     {
       
  1186     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::CreateOutboxEntryL()"));
       
  1187 
       
  1188     CDummySessionObserver* sessionObs;
       
  1189     CMsvSession* msvSession;
       
  1190     CMsvEntry* outBoxEntry;
       
  1191     CreateMsvSessionLC(sessionObs, msvSession, outBoxEntry, KMsvGlobalOutBoxIndexEntryId);
       
  1192     // 1st, 2nd, 3rd push
       
  1193 
       
  1194     TBuf<KObexUtilsMaxCharToFromField> toFrom;
       
  1195     TObexUtilsUiLayer::ReadResourceL( toFrom, aResourceID );
       
  1196 
       
  1197     // Message entry fields
       
  1198     TMsvEntry newTEntry;
       
  1199     newTEntry.SetVisible( ETrue );          // Make visible
       
  1200     newTEntry.SetInPreparation( ETrue );    // and in preparation to make sure gets cleaned up on errors.
       
  1201     newTEntry.iDetails.Set( toFrom );       // To/From field on Outbox
       
  1202     newTEntry.SetSendingState( KMsvSendStateSending );
       
  1203     newTEntry.iType = KUidMsvMessageEntry;      
       
  1204     newTEntry.iMtm = aMtm;                  // message type id 
       
  1205     newTEntry.iServiceId = KMsvGlobalOutBoxIndexEntryId;
       
  1206     outBoxEntry->CreateL( newTEntry );
       
  1207     
       
  1208     CleanupStack::PopAndDestroy(3);         //outBoxEntry, msvSession, sessionObs
       
  1209     return newTEntry.Id();
       
  1210     }
       
  1211 
       
  1212 // -----------------------------------------------------------------------------
       
  1213 // TObexUtilsMessageHandler::DeleteOutboxEntryL
       
  1214 // -----------------------------------------------------------------------------
       
  1215 //
       
  1216 EXPORT_C void TObexUtilsMessageHandler::DeleteOutboxEntryL( const TMsvId& aMessageServerIndex )
       
  1217     {
       
  1218     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::DeleteOutboxEntryL()"));
       
  1219 
       
  1220     CDummySessionObserver* sessionObs;
       
  1221     CMsvSession* msvSession;
       
  1222     CMsvEntry* outBoxEntry;
       
  1223     CreateMsvSessionLC(sessionObs, msvSession, outBoxEntry, KMsvGlobalOutBoxIndexEntryId);
       
  1224     // 1st, 2nd, 3rd push
       
  1225 
       
  1226     outBoxEntry->DeleteL( aMessageServerIndex );
       
  1227 
       
  1228     CleanupStack::PopAndDestroy(3);         //outBoxEntry, msvSession, sessionObs
       
  1229     }
       
  1230 
       
  1231 // -----------------------------------------------------------------------------
       
  1232 // TObexUtilsMessageHandler::GetFileSystemStatus
       
  1233 // -----------------------------------------------------------------------------
       
  1234 //
       
  1235 EXPORT_C TInt TObexUtilsMessageHandler::GetFileSystemStatus()
       
  1236     {
       
  1237     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::GetFileSystemStatus()"));
       
  1238 
       
  1239     /**
       
  1240     *  Free space level of FFS file system (drive C:)
       
  1241     *  The integer value may be:
       
  1242     *       0: Free space OK
       
  1243     *      -1: Free space at or below warning level
       
  1244     *      -2: Free space at or below critical level
       
  1245     */
       
  1246     
       
  1247     TInt status = KErrNone;
       
  1248     TInt retVal = KErrNone;
       
  1249     
       
  1250     retVal = GetPubSubKeyIntValue(KPSUidUikon, KUikFFSFreeLevel, status);
       
  1251     if ( ( retVal == KErrNone ) && ( status == KFFSBelowCritical ) )
       
  1252         {
       
  1253         retVal = KErrDiskFull;
       
  1254         }
       
  1255      
       
  1256     FTRACE(FPrint(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::GetFileSystemStatus completed with %d."), retVal));
       
  1257     return retVal;
       
  1258     }
       
  1259 
       
  1260 // -----------------------------------------------------------------------------
       
  1261 // TObexUtilsMessageHandler::GetMmcFileSystemStatus
       
  1262 // -----------------------------------------------------------------------------
       
  1263 //
       
  1264 EXPORT_C TInt TObexUtilsMessageHandler::GetMmcFileSystemStatus()
       
  1265     {
       
  1266     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::GetMmcFileSystemStatus()"));
       
  1267 
       
  1268     /**
       
  1269     *  Free space level of FFS file system (MMC)
       
  1270     *  The integer value may be:
       
  1271     *       0: Free space OK
       
  1272     *      -1: Free space at or below warning level
       
  1273     *      -2: Free space at or below critical level
       
  1274     */
       
  1275    
       
  1276     TInt retVal = KErrNone;
       
  1277     TInt status = KErrNone;
       
  1278     
       
  1279     retVal = GetPubSubKeyIntValue(KPSUidUikon, KUikMmcFFSFreeLevel, status);
       
  1280     if ( ( retVal == KErrNone ) && ( status == KFFSBelowCritical ) )
       
  1281         {
       
  1282         retVal = KErrDiskFull;
       
  1283         }
       
  1284     
       
  1285     FTRACE(FPrint(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::GetMmcFileSystemStatus completed with %d."), retVal));
       
  1286     return retVal;
       
  1287     }
       
  1288 
       
  1289 // -----------------------------------------------------------------------------
       
  1290 // TObexUtilsMessageHandler::GetMessageCentreDriveL
       
  1291 // -----------------------------------------------------------------------------
       
  1292 //
       
  1293 EXPORT_C TInt TObexUtilsMessageHandler::GetMessageCentreDriveL()
       
  1294     {
       
  1295     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::GetMessageCentreDriveL()"));
       
  1296     
       
  1297     CDummySessionObserver* sessionObs;
       
  1298     CMsvSession* msvSession;
       
  1299     CreateMsvSessionLC(sessionObs, msvSession);  // 1st, 2nd push
       
  1300     
       
  1301     TDriveUnit driveUnit = msvSession->CurrentDriveL();
       
  1302     CleanupStack::PopAndDestroy(2);  // msvSession, sessionObs
       
  1303     
       
  1304     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::GetMessageCentreDriveL() Completed"));
       
  1305     return driveUnit;
       
  1306     }
       
  1307 
       
  1308 // -----------------------------------------------------------------------------
       
  1309 // TObexUtilsMessageHandler::CreateDefaultMtmServiceL
       
  1310 // -----------------------------------------------------------------------------
       
  1311 //
       
  1312 EXPORT_C void TObexUtilsMessageHandler::CreateDefaultMtmServiceL(TUid aMtm)
       
  1313     {
       
  1314     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::CreateDefaultMtmServiceL()"));
       
  1315     
       
  1316     CDummySessionObserver* sessionObs;
       
  1317     CMsvSession* msvSession;
       
  1318     CMsvEntry* root;
       
  1319     CreateMsvSessionLC(sessionObs, msvSession, root, KMsvRootIndexEntryId);
       
  1320     // 1st, 2nd, 3rd push
       
  1321     
       
  1322     TMsvSelectionOrdering msvSelectionOrdering = root->SortType();
       
  1323     msvSelectionOrdering.SetShowInvisibleEntries( ETrue );
       
  1324     root->SetSortTypeL( msvSelectionOrdering );
       
  1325     
       
  1326     CMsvEntrySelection* sel = root->ChildrenWithMtmL( aMtm );
       
  1327     CleanupStack::PushL( sel );  // 4th push
       
  1328     if (sel->Count() == 0)
       
  1329         {
       
  1330         TMsvEntry serviceEntry;
       
  1331         serviceEntry.iMtm = aMtm;
       
  1332         serviceEntry.iType = KUidMsvServiceEntry;
       
  1333         serviceEntry.SetVisible(EFalse);
       
  1334         serviceEntry.SetReadOnly(ETrue);
       
  1335         serviceEntry.SetInPreparation(EFalse);
       
  1336         root->CreateL(serviceEntry);
       
  1337         }
       
  1338     CleanupStack::PopAndDestroy(4);  // sel, root, msvSession, sessionObs
       
  1339     
       
  1340     FLOG(_L("[OBEXUTILS]\t TObexUtilsMessageHandler::CreateDefaultMtmServiceL() Completed"));
       
  1341     }
       
  1342 
       
  1343 // -----------------------------------------------------------------------------
       
  1344 // GetCenRepKeyIntValue Gets integer value for a specified CenRep key
       
  1345 // Returns: CenRep key's value
       
  1346 // -----------------------------------------------------------------------------
       
  1347 //
       
  1348 EXPORT_C TInt TObexUtilsMessageHandler::GetPubSubKeyIntValue(
       
  1349     TUid aCategory,  // Category of the key
       
  1350     TUint aKey,      // PUBSUB key
       
  1351     TInt& value)     // Returned value
       
  1352     {
       
  1353     TInt retVal;
       
  1354     RProperty property;
       
  1355     retVal = property.Attach(aCategory, aKey);
       
  1356     if ( retVal == KErrNone )
       
  1357         {
       
  1358         retVal = property.Get(value);
       
  1359         if (retVal != KErrNone)
       
  1360             {
       
  1361             FLOG(_L("[OBEXUTILS]\t GetPubSubKeyIntValue() Get failed for CenRep key"));
       
  1362             }
       
  1363         }
       
  1364     else
       
  1365         {
       
  1366         FLOG(_L("[OBEXUTILS]\t GetPubSubKeyIntValue() Attach failed for CenRep key"));
       
  1367         }
       
  1368     property.Close();
       
  1369     return retVal;
       
  1370     }
       
  1371 
       
  1372 // -----------------------------------------------------------------------------
       
  1373 // SetBioDataTextL Converts message type to bio message id.
       
  1374 // Returns: None.
       
  1375 // -----------------------------------------------------------------------------
       
  1376 //
       
  1377 void TObexUtilsMessageHandler::SetBioDataTextL(
       
  1378     TBioMsgIdText& aText, // A bio message id text.
       
  1379     const TDesC8& aType ) // The type of the content. 
       
  1380     {
       
  1381     FLOG(_L("[OBEXUTILS]\t SetBioDataTextL()"));
       
  1382 
       
  1383     HBufC16* unicode = CnvUtfConverter::ConvertToUnicodeFromUtf8L( aType );
       
  1384     CleanupStack::PushL( unicode );
       
  1385     
       
  1386     if (unicode->Length() > KMaxBioIdText )
       
  1387         {
       
  1388         User::Leave( KErrOverflow );
       
  1389         }
       
  1390     
       
  1391     aText = unicode->Des();
       
  1392     CleanupStack::PopAndDestroy( unicode );
       
  1393     
       
  1394     FLOG(_L("[OBEXUTILS]\t SetBioDataTextL() completed"));
       
  1395     }
       
  1396 
       
  1397 // -----------------------------------------------------------------------------
       
  1398 // SetDetailsLC Converts message type to unicode.
       
  1399 // Returns: None.
       
  1400 // -----------------------------------------------------------------------------
       
  1401 //
       
  1402 void TObexUtilsMessageHandler::SetDetailsLC(
       
  1403     TPtrC& aDetails,      // A message text.
       
  1404     const TDesC8& aType ) // The type of the content.
       
  1405     {
       
  1406     FLOG(_L("[OBEXUTILS]\t SetDetailsLC()"));
       
  1407 
       
  1408     HBufC16* unicode = CnvUtfConverter::ConvertToUnicodeFromUtf8L( aType );
       
  1409     CleanupStack::PushL( unicode );
       
  1410     aDetails.Set(unicode->Des());
       
  1411 
       
  1412     FLOG(_L("[OBEXUTILS]\t SetDetailsLC() completed"));
       
  1413     }
       
  1414 
       
  1415 //-------------------------------------------------------------------------------
       
  1416 // Rename a file and move file if needed 
       
  1417 // Return: None.
       
  1418 //-------------------------------------------------------------------------------
       
  1419 //
       
  1420 TInt TObexUtilsMessageHandler::RenameFileL(
       
  1421     TFileName& aFileName, 
       
  1422     TFileName& aNewFileName,
       
  1423     const TDesC& aNewPath, 
       
  1424     RFs& aFileSession)
       
  1425     {
       
  1426     // We move the file to the final location
       
  1427     //
       
  1428     CFileMan* fileMan= CFileMan::NewL(aFileSession);
       
  1429     CleanupStack::PushL(fileMan);       
       
  1430 
       
  1431     fileMan->Move(aFileName,aNewPath,CFileMan::ERecurse );
       
  1432     CleanupStack::PopAndDestroy(fileMan); 
       
  1433     
       
  1434     TParse fileParse;
       
  1435     fileParse.Set(aFileName, NULL, NULL);
       
  1436     
       
  1437     TFileName tempFile;
       
  1438     tempFile.Append(aNewPath);
       
  1439     tempFile.Append(fileParse.NameAndExt());
       
  1440     aFileName = tempFile;
       
  1441     
       
  1442     // check the duplicated file name. if so, we renamed in a different way. 
       
  1443     TInt segmentNum = 0;
       
  1444     TBuf<64> segmentString;
       
  1445     
       
  1446     TFileName newFullName;
       
  1447     newFullName.Zero();
       
  1448     newFullName.Append(aNewPath);
       
  1449     newFullName.Append(aNewFileName);
       
  1450     
       
  1451     aFileSession.SetSessionPath(aNewPath);
       
  1452     
       
  1453     while ( BaflUtils::FileExists(aFileSession, newFullName) )    
       
  1454         {
       
  1455         segmentNum++;
       
  1456         User::LeaveIfError( RenameFileWithSegmentNumL(aNewFileName, segmentNum, segmentString) );
       
  1457         newFullName.Zero();
       
  1458         newFullName.Append(aNewPath);
       
  1459         newFullName.Append(aNewFileName);
       
  1460         }
       
  1461     // rename the file.
       
  1462     //
       
  1463     TInt error = aFileSession.Rename(aFileName, newFullName);
       
  1464     aFileName = newFullName;
       
  1465     
       
  1466     return error;
       
  1467     }
       
  1468 
       
  1469 //-------------------------------------------------------------------------------
       
  1470 // Rename a file by adding a additional segment string
       
  1471 // Return: None.
       
  1472 //-------------------------------------------------------------------------------
       
  1473 //
       
  1474 TInt TObexUtilsMessageHandler::RenameFileWithSegmentNumL(
       
  1475     TDes &aFileName,        // name for the file 
       
  1476     TInt aSegmentNum,       // segment number
       
  1477     TDes &aSegmentString)   // segment String with parenthesis
       
  1478     {
       
  1479     
       
  1480     FLOG(_L("[OBEXUTILS]\t RenameFileWithSegmentNumL()"));
       
  1481 
       
  1482     const char KDot = '.';
       
  1483     const char KLeftParenthesis = '(';
       
  1484     const char KRightParenthesis = ')';
       
  1485     
       
  1486     // Convert Integer to String
       
  1487     // 
       
  1488     TBuf<64> buf;
       
  1489     buf.Zero();
       
  1490     _LIT(KNum, "%d" );
       
  1491     buf.AppendFormat(KNum, aSegmentNum);
       
  1492     
       
  1493     // Rules to modify the duplicated filenames.
       
  1494     // case 1: No extention in filename,  we append aSegmentString directly.
       
  1495     // case 2: Filename with extention, we insert aSegmentString right before dot (.)
       
  1496     // case 3: Filename with aSegmentString is still duplicated, 
       
  1497     //         we remove the old aSegmentString from aFileName and add a new one and add a new aSegmentString.
       
  1498     
       
  1499     TInt posOfDot = aFileName.LocateReverse(KDot);
       
  1500     
       
  1501     if ( aSegmentNum > 1 )   // filename with aSegementString is still duplicated
       
  1502         {
       
  1503         // In case the original file name contains the same string as our aSegmentString
       
  1504         // eg 1 with ext. original filename is file(1).txt, after renaming, it is changed to file(1)(1).txt.
       
  1505         // eg 2 without ext. original filename is file(1), after renaming, it is changed to file(1)(1).
       
  1506         TInt posDeleteStarts = posOfDot == KErrNotFound ? 
       
  1507                                 aFileName.Length() - aSegmentString.Length() :
       
  1508                                 posOfDot - aSegmentString.Length();
       
  1509                                 
       
  1510         aFileName.Delete(posDeleteStarts, aSegmentString.Length());
       
  1511         }
       
  1512     
       
  1513     // Construct (New) aSegmentString
       
  1514     aSegmentString.Zero();
       
  1515     aSegmentString.Append(KLeftParenthesis);
       
  1516     aSegmentString.Append(buf);
       
  1517     aSegmentString.Append(KRightParenthesis);
       
  1518     
       
  1519     // 
       
  1520     // If the length of new filename will be larger than KMaxFileName, we remove few chars from the end of aFilename 
       
  1521     // in order to add aSegmentString without panic.
       
  1522     // 
       
  1523     if ( KMaxFileName < (aFileName.Length() + aSegmentString.Length()))
       
  1524         {
       
  1525         aFileName.Delete(KMaxFileName - aSegmentString.Length(), KMaxFileName - aFileName.Length());
       
  1526         }
       
  1527     
       
  1528     posOfDot = aFileName.LocateReverse(KDot);
       
  1529     if ( posOfDot == KErrNotFound )   
       
  1530         {
       
  1531         // aFileName has no extension
       
  1532         // we append the modification to aFileName
       
  1533         //
       
  1534         aFileName.Append(aSegmentString);
       
  1535         }
       
  1536     else
       
  1537         {
       
  1538         // Dot (.) is found, we insert the modifications before dot.
       
  1539         //
       
  1540         aFileName.Insert(posOfDot,aSegmentString);
       
  1541         
       
  1542         }
       
  1543     
       
  1544     FLOG(_L("[OBEXUTILS]\t RenameFileWithSegmentNumL() completed"));
       
  1545     return KErrNone;
       
  1546     }
       
  1547 
       
  1548 //-------------------------------------------------------------------------------
       
  1549 // Shorten an extreme long file name
       
  1550 // Return: None.
       
  1551 //-------------------------------------------------------------------------------
       
  1552 //
       
  1553 void TObexUtilsMessageHandler::FixExtremeLongFileName(TFileName& aExtremeLongFileName, const TDesC& aDriveAndPath)
       
  1554      {
       
  1555      FLOG( _L( "[OBEXUTILS]\t FixExtremeLongFileName() ") );
       
  1556      const char KDot = '.';
       
  1557      TInt combinedLength = aExtremeLongFileName.Length() + aDriveAndPath.Length();
       
  1558      
       
  1559      if ( combinedLength > aExtremeLongFileName.MaxLength() )
       
  1560         {
       
  1561         FLOG( _L( "[OBEXUTILS]\t FixExtremeLongFileName: combinedLenght > aExtremeLongFileName.MaxLength()\t") );
       
  1562          //KDefaultFileExtensionSeparator
       
  1563          TFileName tempFileName;
       
  1564          TInt dotPos = aExtremeLongFileName.LocateReverse( KDot );    
       
  1565          TInt overLap = combinedLength - aExtremeLongFileName.MaxLength() + KRenameOffSet;  
       
  1566  
       
  1567          if ( dotPos > 0 )
       
  1568              {
       
  1569              tempFileName.Copy( aExtremeLongFileName.Left( dotPos - overLap ) );
       
  1570              tempFileName.Append( aExtremeLongFileName.Right( aExtremeLongFileName.Length() - dotPos ) );  
       
  1571              }
       
  1572          else
       
  1573              {
       
  1574              tempFileName.Copy( aExtremeLongFileName.Left(aExtremeLongFileName.Length() - overLap ) );
       
  1575              }
       
  1576          aExtremeLongFileName.Copy( tempFileName );
       
  1577          }
       
  1578      FLOG( _L( "[OBEXUTILS]\t FixExtremeLongFileName() completed") );
       
  1579      }
       
  1580 
       
  1581 //  End of File  
       
  1582