javaextensions/wma/mms/src.s60/cmmsplatformservices60impl.cpp
changeset 21 2a9601315dfc
child 78 71ad690e91f5
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2008 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:  ?Description
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <cmsvattachment.h>
       
    21 #include <cmsvmimeheaders.h>
       
    22 #include <mmsvattachmentmanager.h>
       
    23 
       
    24 #include "logger.h"
       
    25 #include "fs_methodcall.h"
       
    26 #include "s60commonutils.h"
       
    27 #include "cmmsmessageattachment.h"
       
    28 #include "cmmsplatformservices60impl.h"
       
    29 
       
    30 //constants
       
    31 const TInt KMaxAppIDLength = 34;
       
    32 
       
    33 using namespace java::push;
       
    34 using namespace java::util;
       
    35 
       
    36 namespace java
       
    37 {
       
    38 namespace wma
       
    39 {
       
    40 /* ================= MEMBER FUNCTIONS =======================
       
    41  * constructor
       
    42  *------------------------------------------------------------------------------
       
    43  * private CMMSPlatformServiceS60Impl
       
    44  *
       
    45  *------------------------------------------------------------------------------
       
    46  */
       
    47 CMMSPlatformServiceS60Impl::CMMSPlatformServiceS60Impl(
       
    48     const bool aServerConnection,
       
    49     HBufC* aServerApplicationId,
       
    50     ConnectionListener* aListener)
       
    51         :CActive(EPriorityNormal), java::util::FunctionServer("CMMSPlatformService"),
       
    52         mIsSending(false), mServerConnection(aServerConnection), mNewMessages(0),
       
    53         mServerApplicationId(aServerApplicationId)
       
    54 {
       
    55     JELOG2(EWMA);
       
    56     //Creates a new thread and starts active scheduler
       
    57     //This results in call to doServerSideInit()
       
    58     createServerToNewThread();
       
    59     mSendMsgArray = NULL;
       
    60     mMessageListener = aListener;
       
    61 }
       
    62 /**
       
    63  * Gets the instance of OS Specific class which implements the interface
       
    64  * CMMSPlatformService.
       
    65  * @param aJni - jni interface pointer
       
    66  * @param aPeer - java side peer reference.
       
    67  * @param aServerConnection - boolean value to identify client/server
       
    68  *                            connection.
       
    69  * @param aUri - uri on which the connection to be opened.
       
    70  * @param aListener - listener to be notified about incoming messages.
       
    71  */
       
    72 MMSPlatformService* MMSPlatformService::getInstance(JNIEnv& aJni,
       
    73         jobject aPeer,const bool aServerConnection,const std::wstring& aUri,
       
    74         ConnectionListener* aListener)
       
    75 {
       
    76     return CMMSPlatformServiceS60Impl::New(aJni,aPeer,aServerConnection,aUri,
       
    77                                            aListener);
       
    78 }
       
    79 
       
    80 
       
    81 CMMSPlatformServiceS60Impl* CMMSPlatformServiceS60Impl::New(JNIEnv& aJni,
       
    82         jobject aPeer,const bool aServerConnection,const std::wstring& aUri,
       
    83         ConnectionListener* aListener)
       
    84 {
       
    85     JELOG2(EWMA);
       
    86     HBufC* serverApplicationId = NULL;
       
    87     int error = KErrNone;
       
    88     if (aServerConnection)
       
    89     {
       
    90         serverApplicationId = S60CommonUtils::wstringToDes(
       
    91                                   (aUri.substr(7)).c_str());
       
    92     }
       
    93     CMMSPlatformServiceS60Impl * self = new CMMSPlatformServiceS60Impl(
       
    94         aServerConnection, serverApplicationId, aListener);
       
    95     error = self->getError();
       
    96     if (KErrNone == error)
       
    97     {
       
    98         //Attaches the function server thread to VM, to make callbacks to java
       
    99         //This results in call to vmAttached()
       
   100         self->attachToVm(aJni, aPeer);
       
   101         LOG(EWMA, EInfo, "CMMSPlatformServiceS60Impl Successfully attached to VM");
       
   102         TRAP(error, self->ConstructL());
       
   103     }
       
   104     if (error != KErrNone)
       
   105     {
       
   106         ELOG1(EWMA, "CMMSPlatformServiceS60Impl : Error while opening %d", error);
       
   107         // If there is any error while opening close the connection
       
   108         self->closeConnection(aJni,aPeer);
       
   109         delete self;
       
   110         throw error;
       
   111     }
       
   112     return self;
       
   113 }
       
   114 
       
   115 /**------------------------------------------------------------------------------
       
   116  * EPOC default constructor can leave.
       
   117  * private ConstructL
       
   118  *------------------------------------------------------------------------------
       
   119  */
       
   120 void CMMSPlatformServiceS60Impl::ConstructL()
       
   121 {
       
   122     JELOG2(EWMA);
       
   123     User::LeaveIfError(mRFs.Connect());
       
   124     User::LeaveIfError(mRFs.ShareProtected());
       
   125     CreateSendMessageArrayL(); // create send array
       
   126     mMessageDeliveryDate = TTime(0);
       
   127     mMmsMessagePriority = EMmsPriorityHigh;
       
   128     mMessageHandler = CJavaMMSMessageHandler::NewL(mRFs);
       
   129     // If server connection then check for messages already available in MMS
       
   130     // message store.
       
   131     if (mServerConnection)
       
   132     {
       
   133         LOG(EWMA, EInfo , "CMMSPlatformServiceS60Impl::ConstructL() Check for"
       
   134             "any messages availble in message store");
       
   135         CallMethodL(this, &CMMSPlatformServiceS60Impl::NotifyReceivedMessageL,
       
   136                     this);
       
   137     }
       
   138 }
       
   139 
       
   140 
       
   141 void CMMSPlatformServiceS60Impl::vmAttached()
       
   142 {
       
   143     JELOG2(EWMA);
       
   144     jclass sessionClass = mJniEnv->FindClass(
       
   145                               "com/nokia/mj/impl/mms/MMSConnectionImpl");
       
   146     mSendCallBackMethodId = mJniEnv->GetMethodID(sessionClass,
       
   147                             "messageSentCallback", "(I)V");
       
   148 
       
   149 }
       
   150 /**
       
   151  * Returns total number of messages that are arrived
       
   152  * from opening the connection
       
   153  */
       
   154 int CMMSPlatformServiceS60Impl::getNumberOfMessages()
       
   155 {
       
   156     JELOG2(EWMA);
       
   157     return mNewMessages;
       
   158 }
       
   159 /**
       
   160  * Does the MMS Engine side initializations .Registers with MMS Engine
       
   161  * for notification about incoming message in case of server connection.
       
   162  */
       
   163 void CMMSPlatformServiceS60Impl::doServerSideInit()
       
   164 {
       
   165     JELOG2(EWMA);
       
   166     FunctionServer::doServerSideInit();
       
   167     CActiveScheduler::Add(this);
       
   168     TRAPD(error,
       
   169     {
       
   170         mSession = CMsvSession::OpenSyncL(*this);
       
   171         mClientMtmRegistry = CClientMtmRegistry::NewL(*mSession);
       
   172         mMmsClient = (CMmsClientMtm *) mClientMtmRegistry->NewMtmL(
       
   173                          KUidMsgTypeMultimedia);
       
   174         mSendTMsvIdArray = RArray<TMsvId>(KArrayGranularity);
       
   175         mMmsApplicationAdapter = CMmsApplicationAdapter::NewL();
       
   176         if (mServerConnection)
       
   177         {
       
   178             mServerMmsMtm = (CMmsClientMtm *) mClientMtmRegistry->NewMtmL(
       
   179                                 KUidMsgTypeMultimedia);
       
   180             mReceivedMsgArray = new(ELeave) CArrayPtrSeg
       
   181             <CMMSMessageInformation>(KArrayGranularity);
       
   182             TBool isRegister = EFalse;
       
   183             // If launched by push the application would have already registered
       
   184             // with mms engine . check before registering.
       
   185             LOG(EWMA, EInfo , "Checking whether application is already"
       
   186                 "registered or not");
       
   187             isRegister = mMmsApplicationAdapter->RegisteredL
       
   188                          (*mServerApplicationId);
       
   189             // If not registered , register now.
       
   190             if (!isRegister)
       
   191             {
       
   192                 TMsvId folderId;
       
   193                 LOG(EWMA, EInfo , "Registering the AppId with MMS engine");
       
   194                 mMmsApplicationAdapter->RegisterL(*mServerApplicationId,folderId);
       
   195                 isRegister = true;
       
   196             }
       
   197 
       
   198             TMsvId tmsvId = mMmsApplicationAdapter->FolderIdL(
       
   199                                 *mServerApplicationId);
       
   200             if (0 != tmsvId)
       
   201             {
       
   202                 mRegisterCMsvEntry = mSession->GetEntryL(tmsvId);
       
   203                 // Add listener to the created entry for that application Id
       
   204                 mRegisterCMsvEntry->AddObserverL(*this);
       
   205             }
       
   206 
       
   207         }
       
   208     });
       
   209     mError = error;
       
   210 }
       
   211 
       
   212 /* -----------------------------------------------------------------------------
       
   213  * CMMSPlatformServiceS60Impl::Send
       
   214  * Sends a multimedia message to its recipients.
       
   215  * -----------------------------------------------------------------------------
       
   216  */
       
   217 int CMMSPlatformServiceS60Impl::send(JNIEnv& aJniEnv,
       
   218                                      jbyteArray aBytes, int aOffset, int aLength, jstring /*aDestination*/)
       
   219 {
       
   220     JELOG2(EWMA);
       
   221     // put the jbyteArray into the mSendBuffer
       
   222     delete mSendBuf; // avoid the memory leak
       
   223     mSendBuf = NULL;
       
   224     TRAPD(error,
       
   225     {
       
   226         mSendBuf = CBufFlat::NewL(aLength);
       
   227 
       
   228         // Extends the size of the buffer by the required size
       
   229         mSendBuf->ExpandL(aOffset, aLength);
       
   230 
       
   231         // Transfer the data from java
       
   232         TPtr8 sendBufPtr = mSendBuf->Ptr(0);
       
   233         S60CommonUtils::CopyToNative(aJniEnv, aBytes, aOffset, aLength,sendBufPtr);
       
   234         // If the object is active then it is currently sending a message
       
   235         // it will also send the newly added message.
       
   236         iStatus = KRequestPending;
       
   237         SetActive();
       
   238         CallMethodL(this, &CMMSPlatformServiceS60Impl::mmsRequestComplete,
       
   239                     KErrNone,this);
       
   240     });
       
   241     ELOG1(EWMA, "CMMSPlatformServiceS60Impl : status of send %d",error);
       
   242     return error;
       
   243 }
       
   244 
       
   245 /*------------------------------------------------------------------------------
       
   246  * protected SetObjectActive locally
       
   247  * .
       
   248  *------------------------------------------------------------------------------
       
   249  */
       
   250 void CMMSPlatformServiceS60Impl::mmsRequestComplete(int result)
       
   251 {
       
   252     JELOG2(EWMA);
       
   253     TRequestStatus *status = &iStatus;
       
   254     User::RequestComplete(status, result);
       
   255 }
       
   256 
       
   257 /*------------------------------------------------------------------------------
       
   258  * Notifies java side about success or failure of sending MMS message
       
   259  * .
       
   260  *------------------------------------------------------------------------------
       
   261  */
       
   262 void CMMSPlatformServiceS60Impl::NotifySend(JNIEnv& aJni,jobject aPeer,
       
   263         int aError)
       
   264 {
       
   265     JELOG2(EWMA);
       
   266     aJni.CallVoidMethod(aPeer, mSendCallBackMethodId, aError);
       
   267     mIsSending = false;
       
   268 }
       
   269 
       
   270 /*------------------------------------------------------------------------------
       
   271  * RunL from CActive
       
   272  * .
       
   273  *------------------------------------------------------------------------------
       
   274  */
       
   275 void CMMSPlatformServiceS60Impl::RunL()
       
   276 {
       
   277     JELOG2(EWMA);
       
   278     if (iStatus.Int() ==KErrNone)
       
   279     {
       
   280         mIsSending = true;
       
   281         SendMultiPartMessageL();
       
   282     }
       
   283 }
       
   284 
       
   285 /*------------------------------------------------------------------------------
       
   286  * protected DoCancel
       
   287  * Cancel the sending of the message. If the message is in the creation phase
       
   288  * we can cancel the sending of the message. If the deviec is actually sending
       
   289  * the message we cannot cancel it.
       
   290  *------------------------------------------------------------------------------
       
   291  */
       
   292 void CMMSPlatformServiceS60Impl::DoCancel()
       
   293 {
       
   294     // clean up the entry
       
   295     JELOG2(EWMA);
       
   296 }
       
   297 /*------------------------------------------------------------------------------
       
   298  * protected RunError
       
   299  * .
       
   300  *------------------------------------------------------------------------------
       
   301  */
       
   302 TInt CMMSPlatformServiceS60Impl::RunError(TInt aError)
       
   303 {
       
   304     JELOG2(EWMA);
       
   305     ELOG1(EWMA, "+ In runerror %d this %d", aError);
       
   306     NotifySend(*mJniEnv, mJavaPeerObject, aError);
       
   307     return KErrNone;
       
   308 }
       
   309 /* -----------------------------------------------------------------------------
       
   310  * CMMSPlatformServiceS60Impl::RetrieveMessageL
       
   311  * Retrieves the first received message from the queue. After delivering the
       
   312  * message to the caller, the message is removed from the queue.
       
   313  * -----------------------------------------------------------------------------
       
   314  */
       
   315 jbyteArray CMMSPlatformServiceS60Impl::retrieveMessage(JNIEnv& aJni)
       
   316 {
       
   317     JELOG2(EWMA);
       
   318     // check if there is something in the queue
       
   319     if (mReceivedMMSIDs.Count() <= 0)
       
   320     {
       
   321         return NULL;
       
   322     }
       
   323     // retrieve the first message whose ID is in the queue
       
   324     TMsvId id = mReceivedMMSIDs[0];
       
   325     mReceivedMMSIDs.Remove(0);
       
   326 
       
   327     CMMSMessageInformation* mms = ReceivedMessage(id);
       
   328     if (!mms)
       
   329     {
       
   330         return NULL;
       
   331     }
       
   332     CleanupStack::PushL(mms);
       
   333 
       
   334     // serialize the received MMS
       
   335     LOG(EWMA, EInfo , "Serializing the message before giving to java");
       
   336     CBufFlat& receiveBuffer = mMessageHandler->SerializeLC(*mms);
       
   337     // receiveBuffer is on the cleanup stack
       
   338     TPtr8 receiveBufferPtr = receiveBuffer.Ptr(0);
       
   339     //get the jbyteArray
       
   340     TInt dataLength = receiveBuffer.Capacity();
       
   341     jbyteArray array = aJni.NewByteArray(dataLength);
       
   342     S60CommonUtils::CopyToJava(aJni, receiveBufferPtr, array, 0,dataLength);
       
   343     CleanupStack::PopAndDestroy(&receiveBuffer); // receiveBuffer
       
   344     CleanupStack::PopAndDestroy(mms);
       
   345     return array;
       
   346 }
       
   347 
       
   348 /*------------------------------------------------------------------------------
       
   349  * public CreateMessageL
       
   350  * create an empty messsage in draft folder. Later we will add the information
       
   351  * (attachments addresses, subject etc)
       
   352  *------------------------------------------------------------------------------
       
   353  */
       
   354 TMsvId CMMSPlatformServiceS60Impl::CreateMessageL()
       
   355 {
       
   356     JELOG2(EWMA);
       
   357     TMsvId serviceId = mMmsClient->DefaultServiceL();
       
   358     // Context must be set to parent folder for CreateMessageL
       
   359     mMmsClient->SwitchCurrentEntryL(KMsvDraftEntryId);
       
   360     mMmsClient->CreateMessageL(serviceId);
       
   361     CMsvEntry& cmsvEntry = mMmsClient->Entry();
       
   362     TMsvId msvId = cmsvEntry.EntryId();
       
   363     return msvId;
       
   364 }
       
   365 
       
   366 /*------------------------------------------------------------------------------
       
   367  * private DeleteSentMessage
       
   368  * When the message is gone from the device. Delete the message from
       
   369  * the sent folder.
       
   370  *------------------------------------------------------------------------------
       
   371  */
       
   372 void CMMSPlatformServiceS60Impl::DeleteSentMessageL(TMsvId aMsvId)
       
   373 {
       
   374     JELOG2(EWMA);
       
   375     TInt index = mSendTMsvIdArray.Find(aMsvId);
       
   376     if (KErrNotFound == index)
       
   377     {
       
   378         return;
       
   379     }
       
   380     mSendTMsvIdArray.Remove(index);
       
   381     DeleteMmsMessageL(aMsvId);
       
   382 }
       
   383 
       
   384 /*------------------------------------------------------------------------------
       
   385  * public DeleteMessage
       
   386  * Delete the message from message store. We are deleting two type of
       
   387  * multimedia messages.Messages that we send from Java side. When they
       
   388  * are gone from the device. We delete the message from the sent folder.
       
   389  * Second is when we receive the message we delete it after
       
   390  * reading it from Java side.
       
   391  *------------------------------------------------------------------------------
       
   392  */
       
   393 void CMMSPlatformServiceS60Impl::DeleteMmsMessageL(TMsvId aMsvId)
       
   394 {
       
   395     JELOG2(EWMA);
       
   396     LoadMessageL(aMsvId);
       
   397     CMsvEntry& cmsvEntry = mMmsClient->Entry();
       
   398     TMsvEntry tmsvEntry(cmsvEntry.Entry());
       
   399 
       
   400     if ((tmsvEntry.iMtm != KUidMsgTypeMultimedia)||(tmsvEntry.InPreparation()))
       
   401     {
       
   402         // It is not MMS  or the entry is not ready
       
   403         return;
       
   404     }
       
   405     //Taking a handle to the Sent folder...
       
   406     TMsvSelectionOrdering sort = cmsvEntry.SortType();
       
   407     sort.SetShowInvisibleEntries(ETrue);
       
   408     // we want to handle also the invisible entries
       
   409     // Take a handle to the parent entry
       
   410     CMsvEntry* parentEntry = CMsvEntry::NewL(mMmsClient->Session(),
       
   411                              tmsvEntry.Parent(), sort);
       
   412     CleanupStack::PushL(parentEntry); //
       
   413     TRAPD(err, parentEntry->DeleteL(aMsvId));
       
   414     if (err != KErrNone)
       
   415     {
       
   416         mSession->RemoveEntry(aMsvId);
       
   417     }
       
   418     CleanupStack::PopAndDestroy(parentEntry);
       
   419 }
       
   420 /*------------------------------------------------------------------------------
       
   421  * public LoadMessage
       
   422  * laod the message  from the given TMsvId,
       
   423  * After this call message will be current message
       
   424  *------------------------------------------------------------------------------
       
   425  */
       
   426 void CMMSPlatformServiceS60Impl::LoadMessageL(TMsvId aMessageEntry)
       
   427 {
       
   428     JELOG2(EWMA);
       
   429     mMmsClient->SwitchCurrentEntryL(aMessageEntry);
       
   430     mMmsClient->LoadMessageL();
       
   431 }
       
   432 /*------------------------------------------------------------------------------
       
   433  * AddAttachments
       
   434  * add attachment, location, contentId, and set the message root here
       
   435  *
       
   436  *------------------------------------------------------------------------------
       
   437  */
       
   438 void CMMSPlatformServiceS60Impl::AddAttachmentsL(
       
   439     CMMSMessageInformation* aMidMessageInformation)
       
   440 {
       
   441     JELOG2(EWMA);
       
   442     // Add attachments also set the started ID
       
   443     CMsvStore* store = mMmsClient->Entry().EditStoreL();
       
   444     CleanupStack::PushL(store);
       
   445     TInt count = aMidMessageInformation->AttachmentCount();
       
   446     for (int i = 0; i < count; i++)
       
   447     {
       
   448         CMMSMessageAttachment& messageAttachment =
       
   449             aMidMessageInformation->Attachment(i);
       
   450         TDesC& fileName = messageAttachment.AttachmentName();
       
   451         TDesC8& mimeType8 = messageAttachment.AttachmentType();
       
   452         TDesC8& contentLocation8 = messageAttachment.ContentLocationL();
       
   453         TDesC8& contentId8 = messageAttachment.ContentIdL();
       
   454         TInt charEncoding = messageAttachment.Encoding();
       
   455         TDesC* contentLocation = NULL;
       
   456         S60CommonUtils::ConvertNarrowToWiderLC(contentLocation8,contentLocation);
       
   457 
       
   458         CMsvMimeHeaders* mimeHeaders = CMsvMimeHeaders::NewLC();
       
   459         // set the attachment mime type in the CMsvMimeHeaders
       
   460         if (&mimeType8)
       
   461         {
       
   462             TInt position = mimeType8.Find(KSlash);
       
   463             if (position > 0 && position < mimeType8.Length() - 1)
       
   464             {
       
   465                 mimeHeaders->SetContentTypeL(mimeType8.Left(position));
       
   466                 mimeHeaders->SetContentSubTypeL(mimeType8.Mid(position + 1));
       
   467             }
       
   468             else
       
   469             {
       
   470                 mimeHeaders->SetContentTypeL(mimeType8);
       
   471             }
       
   472         }
       
   473         CMsvAttachment* attachment = CMsvAttachment::NewL(
       
   474                                          CMsvAttachment::EMsvFile);
       
   475         if (contentLocation)
       
   476         {
       
   477             mimeHeaders->SetContentLocationL(*contentLocation);
       
   478         }
       
   479         // contentId8 can be NULL
       
   480         if (&contentId8)
       
   481         {
       
   482             mimeHeaders->SetContentIdL(contentId8);
       
   483         }
       
   484         // set Charset in attachment
       
   485         if (charEncoding >= 0)
       
   486         {
       
   487             mimeHeaders->SetMimeCharset(charEncoding);
       
   488         }
       
   489         RFile attaFile;
       
   490         User::LeaveIfError(attaFile.Open(mRFs, fileName,
       
   491                                          EFileShareReadersOnly | EFileRead));
       
   492         CleanupClosePushL(attaFile);
       
   493         TMsvAttachmentId attaId = 0;
       
   494         // attachment ownerhip will be transferred to Attachment Manager
       
   495         // It must not be pushed onto the cleanupStack before calling
       
   496         // CreateAttachment2L
       
   497         HBufC8* emptyMime = NULL;
       
   498         if (&mimeType8)
       
   499         {
       
   500             mMmsClient->CreateAttachment2L(*store, // edit store
       
   501                                            attaFile, // open file handle
       
   502                                            mimeType8,
       
   503                                            // combination type like image/jpeg
       
   504                                            *mimeHeaders, attachment, attaId);
       
   505         }
       
   506         else
       
   507         {
       
   508             emptyMime = HBufC8::NewLC(0);
       
   509             mMmsClient->CreateAttachment2L(*store, // edit store
       
   510                                            attaFile, // open file handle
       
   511                                            *emptyMime,
       
   512                                            // combination type like image/jpeg
       
   513                                            *mimeHeaders, attachment, attaId);
       
   514         }
       
   515         // Now Attachment Manager owns the attachment object, so assign it NULL.
       
   516         attachment = NULL;
       
   517         // check also isStarted Id
       
   518         if (messageAttachment.IsStarterContentId())
       
   519         {
       
   520             mMmsClient->SetMessageRootL(attaId);
       
   521         }
       
   522         if (emptyMime)
       
   523         {
       
   524             CleanupStack::PopAndDestroy(emptyMime);
       
   525         }
       
   526         CleanupStack::PopAndDestroy(&attaFile); // attaFile.Close()
       
   527         CleanupStack::PopAndDestroy(mimeHeaders); // mimeHeaders
       
   528         CleanupStack::PopAndDestroy(contentLocation); // contentLocation
       
   529     } // end of add attachment for loop
       
   530 
       
   531     store->CommitL();
       
   532     CleanupStack::PopAndDestroy(store); // store
       
   533 }
       
   534 /*------------------------------------------------------------------------------
       
   535  * public SetSubject
       
   536  * LoadMessage is called before this method call other wise subject can be set
       
   537  * to wrong TMsvId
       
   538  *------------------------------------------------------------------------------
       
   539  */
       
   540 void CMMSPlatformServiceS60Impl::SetSubjectL(const TDesC& aSubject)
       
   541 {
       
   542     JELOG2(EWMA);
       
   543     mMmsClient->SetSubjectL(aSubject);
       
   544 }
       
   545 /*------------------------------------------------------------------------------
       
   546  * public AddRecipient
       
   547  * LoadMessage is called before this method call other wise address can be set
       
   548  * to wrong TMsvId
       
   549  *------------------------------------------------------------------------------
       
   550  */
       
   551 void CMMSPlatformServiceS60Impl::AddRecipientL(const TDesC& aAddress,
       
   552         TMmsRecipients aAddressType)
       
   553 {
       
   554     JELOG2(EWMA);
       
   555     mMmsClient->AddAddresseeL(aAddressType, aAddress);
       
   556 }
       
   557 /*------------------------------------------------------------------------------
       
   558  * public SetApplicationId
       
   559  * LoadMessage is called before this method call, other wise applicationId can
       
   560  * be set to wrong TMsvId.
       
   561  *------------------------------------------------------------------------------
       
   562  */
       
   563 void CMMSPlatformServiceS60Impl::SetApplicationIdL(const TDesC& aApplicationId)
       
   564 {
       
   565     JELOG2(EWMA);
       
   566     if (aApplicationId.Length() > 0)
       
   567     {
       
   568         TMsvId mvsEntry = mMmsClient->Entry().EntryId();
       
   569         mMmsApplicationAdapter->SetApplicationIdL(mvsEntry,aApplicationId);
       
   570     }
       
   571 }
       
   572 /*------------------------------------------------------------------------------
       
   573  * SetReplyToApplicationId
       
   574  * LoadMessage is called before this method call, other wise
       
   575  * ReplyToApplicationId can be set to wrong TMsvId.
       
   576  *------------------------------------------------------------------------------
       
   577  */
       
   578 void CMMSPlatformServiceS60Impl::SetReplyToApplicationIdL(
       
   579     const TDesC& aReplyToApplicationId)
       
   580 {
       
   581     JELOG2(EWMA);
       
   582     if (aReplyToApplicationId.Length() > 0)
       
   583     {
       
   584         TMsvId mvsEntry = mMmsClient->Entry().EntryId();
       
   585         mMmsApplicationAdapter->SetReplyToApplicationIdL(mvsEntry,
       
   586                 aReplyToApplicationId);
       
   587     }
       
   588 }
       
   589 /*------------------------------------------------------------------------------
       
   590  * public SendMultiPartMessageL
       
   591  * ASyn method call to send the message.
       
   592  *------------------------------------------------------------------------------
       
   593  */
       
   594 void CMMSPlatformServiceS60Impl::SendMultiPartMessageL()
       
   595 {
       
   596     JELOG2(EWMA);
       
   597     // convert the byte array into a native multimedia message representation
       
   598     LOG(EWMA, EInfo , "Converting the byte array into a native multimedia"
       
   599         "message representation");
       
   600     CMMSMessageInformation& mms = mMessageHandler->DeSerializeL(*mSendBuf);
       
   601     // send it towards the MMS engine
       
   602     mSendMsgArray->AppendL(&mms);
       
   603     while (mSendMsgArray->Count() > 0)
       
   604     {
       
   605         CMMSMessageInformation* midMessageInformation =
       
   606             mSendMsgArray->At(0);
       
   607         CleanupStack::PushL(midMessageInformation);
       
   608         SendMmsMessageL(midMessageInformation);
       
   609         mSendMsgArray->Delete(0);
       
   610         CleanupStack::Pop(midMessageInformation); // midMessageInformation
       
   611         delete midMessageInformation;
       
   612         midMessageInformation = NULL;
       
   613     }
       
   614 }
       
   615 /*------------------------------------------------------------------------------
       
   616  * public SendMultiMediaMessageL
       
   617  * Message is send in the following steps
       
   618  * 1) Create an empty message in the draft folder
       
   619  * 2) Get the multimeida messsage information from the CMMSMessageInformation
       
   620  *    object and set that information to the newly created multimedia message
       
   621  *    in the following order
       
   622  *        i) Set the subject of the message (Optional)
       
   623  *        ii)Add the destination addresses (to, cc, bcc)
       
   624  *        iii)Add attachments to the message. Attachments are saved in the
       
   625  *          message store.
       
   626  *        vi)Set message setting (Date, time, expiry time, priority, visibility
       
   627  *         of the sender)
       
   628  * 3) Save the formed message in the message store
       
   629  * 4) Send the message by using mmsMTMClient.
       
   630  * 5) When the message is gone from the device delete the message from the sent
       
   631  *    folder.
       
   632  *-----------------------------------------------------------------------------
       
   633  */
       
   634 void CMMSPlatformServiceS60Impl::SendMmsMessageL(
       
   635     CMMSMessageInformation* aMidMessageInformation)
       
   636 {
       
   637     JELOG2(EWMA);
       
   638     TMsvId msvId = CreateMessageL();
       
   639     // Add subject
       
   640     TDesC8& str8 = aMidMessageInformation->MessageSubjectL();
       
   641     TDesC* str = NULL;
       
   642     if (NULL != &str8)
       
   643     {
       
   644         S60CommonUtils::ConvertNarrowToWiderL(str8, str);
       
   645         SetSubjectL(*str);
       
   646         delete str;
       
   647         str = NULL;
       
   648     }
       
   649     // Add EMsvRecipientTo addresses
       
   650     TInt i = 0;
       
   651     TInt count = aMidMessageInformation->AddressesCount(EMsvRecipientTo);
       
   652     for (i = 0; i < count; i++)
       
   653     {
       
   654         AddRecipientL(aMidMessageInformation->MessageAddressL(
       
   655                           EMsvRecipientTo, i), EMsvRecipientTo);
       
   656     }
       
   657     // Add EMsvRecipientCc addresses
       
   658     count = aMidMessageInformation->AddressesCount(EMsvRecipientCc);
       
   659     for (i = 0; i < count; i++)
       
   660     {
       
   661         AddRecipientL(aMidMessageInformation->MessageAddressL(
       
   662                           EMsvRecipientCc, i), EMsvRecipientCc);
       
   663     }
       
   664     // Add EMsvRecipientBcc addresses
       
   665     count = aMidMessageInformation->AddressesCount(EMsvRecipientBcc);
       
   666     for (i = 0; i < count; i++)
       
   667     {
       
   668         AddRecipientL(aMidMessageInformation->MessageAddressL(
       
   669                           EMsvRecipientBcc, i), EMsvRecipientBcc);
       
   670     }
       
   671     // Add attachments also set the started ID
       
   672     AddAttachmentsL(aMidMessageInformation);
       
   673     // set date and time
       
   674     TInt64 time = aMidMessageInformation->MessageTime();
       
   675     SetMessageSendDate(time);
       
   676     mMmsMessagePriority = aMidMessageInformation->MessagePriority();
       
   677     // set priorty, delivery time, message class, ExpiryInterval,
       
   678     // sender visibility and delivery report
       
   679     SetMessageSettingL();
       
   680 
       
   681     // set application-id and reply-to-application-id
       
   682     TDesC8& id8 = aMidMessageInformation->ApplicationIdL();
       
   683     TDesC* id = NULL;
       
   684     if (NULL != &id8)
       
   685     {
       
   686         S60CommonUtils::ConvertNarrowToWiderL(id8, id);
       
   687         SetApplicationIdL(*id);
       
   688         delete id;
       
   689         id = NULL;
       
   690     }
       
   691     // Set reply-to-application-Id
       
   692     TDesC8& rId8 = aMidMessageInformation->ReplyToApplicationIdL();
       
   693     if (NULL != &rId8)
       
   694     { // add reply-to-application-id to currently loaded message
       
   695         S60CommonUtils::ConvertNarrowToWiderL(rId8, id);
       
   696         SetReplyToApplicationIdL(*id);
       
   697         delete id;
       
   698         id = NULL;
       
   699     }
       
   700     SendMmsMessageL(msvId); // send it now
       
   701     LOG(EWMA, EInfo, "CMMSPlatformServiceS60Impl::SendMmsMessageL Notifying"
       
   702         "java about the message send status");
       
   703     NotifySend(*mJniEnv, mJavaPeerObject, KErrNone);
       
   704 }
       
   705 /*-----------------------------------------------------------------------------
       
   706  * private SendMmsMessageL
       
   707  * Send the message from the device. This method is called when the
       
   708  * message is ready to send. All the necessary fields are filled.
       
   709  * message is send by using mmsClient mtm.
       
   710  *-----------------------------------------------------------------------------
       
   711  */
       
   712 void CMMSPlatformServiceS60Impl::SendMmsMessageL(TMsvId aSendMmsMsgId)
       
   713 {
       
   714     JELOG2(EWMA);
       
   715     TMsvEntry tmvsEntry = mMmsClient->Entry().Entry();
       
   716     tmvsEntry.SetInPreparation(EFalse);
       
   717     tmvsEntry.SetVisible(ETrue);
       
   718     mMmsClient->Entry().ChangeL(tmvsEntry);
       
   719     // Save the message here to save the last changes made above.
       
   720     mMmsClient->SaveMessageL();
       
   721     // left on CleanupStack
       
   722     CMsvOperationWait* wait = CMsvOperationWait::NewLC();
       
   723     wait->iStatus = KRequestPending;
       
   724     CMsvOperation* op = NULL;
       
   725     op = mMmsClient->SendL(wait->iStatus);
       
   726     wait->Start();
       
   727     CleanupStack::PushL(op);
       
   728     CActiveScheduler::Start();
       
   729     mSendTMsvIdArray.AppendL(aSendMmsMsgId);
       
   730     // The following is to ignore the completion of other active objects.
       
   731     // It is not needed if the app has a command absorbing control.
       
   732     while (wait->iStatus.Int() == KRequestPending)
       
   733     {
       
   734         CActiveScheduler::Start();
       
   735     }
       
   736     CleanupStack::PopAndDestroy(op);
       
   737     CleanupStack::PopAndDestroy(wait);
       
   738 }
       
   739 /*------------------------------------------------------------------------------
       
   740  * protected Set Message Setting
       
   741  * Set the class, ExpiryInterval, delivery time interval, priority and
       
   742  * visibility of the sender.
       
   743  *------------------------------------------------------------------------------
       
   744  */
       
   745 void CMMSPlatformServiceS60Impl::SetMessageSettingL()
       
   746 {
       
   747     JELOG2(EWMA);
       
   748     mMmsClient->RestoreSettingsL();
       
   749 
       
   750     mMmsClient->SetMessageClass(EMmsClassPersonal);
       
   751     // X-Mms-Delivery-Time
       
   752     mMmsClient->SetDeliveryDate(mMessageDeliveryDate);
       
   753 
       
   754     // X-Mms-Priority
       
   755     mMmsClient->SetMessagePriority(mMmsMessagePriority);
       
   756     mMmsClient->SetSenderVisibility(EMmsSenderVisibilityShow);
       
   757     mMmsClient->SetDeliveryReport(EMmsDeliveryReportYes);
       
   758 }
       
   759 /*------------------------------------------------------------------------------
       
   760  * public SetMessageSendDate
       
   761  * Set message time in Java format
       
   762  *------------------------------------------------------------------------------
       
   763  */
       
   764 void CMMSPlatformServiceS60Impl::SetMessageSendDate(TInt64& aLong)
       
   765 {
       
   766     JELOG2(EWMA);
       
   767     if (aLong != 0)
       
   768     {
       
   769         mMessageDeliveryDate = S60CommonUtils::JavaTimeToTTime(aLong);
       
   770     }
       
   771     else
       
   772     {
       
   773         mMessageDeliveryDate = TTime(0);
       
   774     }
       
   775 }
       
   776 /*------------------------------------------------------------------------------
       
   777  * public HandleSessionEventL
       
   778  * EMsvEntriesMoved,
       
   779  *
       
   780  *    One or more entries have been moved.
       
   781  *
       
   782  *    aArg1 is a CMsvEntrySelection containing the IDs of the moved entries.
       
   783  *    aArg2 is the TMsvId of the new parent.
       
   784  *    aArg3 is the TMsvId of the old parent entry.
       
   785  *------------------------------------------------------------------------------
       
   786  */
       
   787 void CMMSPlatformServiceS60Impl::HandleSessionEventL(
       
   788     TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* /*aArg3*/)
       
   789 {
       
   790     JELOG2(EWMA);
       
   791     switch (aEvent)
       
   792     {
       
   793     case EMsvEntriesMoved:
       
   794     {
       
   795         TMsvId parentId = KMsvNullIndexEntryId;
       
   796         parentId = *(TMsvId*) aArg2;
       
   797 
       
   798         if (parentId == KMsvSentEntryId)
       
   799         {
       
   800             // We take the created entries into a selection
       
   801             CMsvEntrySelection* entries =
       
   802                 static_cast<CMsvEntrySelection*>(aArg1);
       
   803             TInt count = entries->Count();
       
   804             LOG1(EWMA, EInfo, "CMMSPlatformServiceS60Impl::HandleSessionEventL"
       
   805                  "Count = %d ", count);
       
   806             for (TInt i = 0; i < count; i++)
       
   807             {
       
   808                 LOG1(EWMA, EInfo, "CMMSPlatformServiceS60Impl::"
       
   809                      "HandleSessionEventL number = %d ", i);
       
   810                 TRAP_IGNORE(DeleteSentMessageL(entries->At(i)));
       
   811             }// end of for loop
       
   812         } // end of parentid if
       
   813         break;
       
   814     }
       
   815     default:
       
   816         break;
       
   817     }
       
   818 }
       
   819 /*------------------------------------------------------------------------------
       
   820  * private CreateSendMessageArray
       
   821  * Create the send message TMsvId array. It will be later used to delete the
       
   822  * messages. We should NOT be deleting the messages send by other application.
       
   823  *------------------------------------------------------------------------------
       
   824  */
       
   825 void CMMSPlatformServiceS60Impl::CreateSendMessageArrayL()
       
   826 {
       
   827     JELOG2(EWMA);
       
   828     ClearAndDestroySendMessageArray();
       
   829     mSendMsgArray = new(ELeave) CArrayPtrSeg<CMMSMessageInformation>
       
   830     (KArrayGranularity);
       
   831 }
       
   832 /*------------------------------------------------------------------------------
       
   833  * private ClearAndDestroySendMessageArray
       
   834  *
       
   835  *------------------------------------------------------------------------------
       
   836  */
       
   837 void CMMSPlatformServiceS60Impl::ClearAndDestroySendMessageArray()
       
   838 {
       
   839     JELOG2(EWMA);
       
   840     if (mSendMsgArray)
       
   841     {
       
   842         mSendMsgArray->ResetAndDestroy();
       
   843     }
       
   844     delete mSendMsgArray;
       
   845     mSendMsgArray = NULL;
       
   846 }
       
   847 
       
   848 /*------------------------------------------------------------------------------
       
   849  * public ClearSendMessageArray
       
   850  *------------------------------------------------------------------------------
       
   851  */
       
   852 void CMMSPlatformServiceS60Impl::ClearSendMessageArray()
       
   853 {
       
   854     JELOG2(EWMA);
       
   855     if (mSendMsgArray)
       
   856     {
       
   857         mSendMsgArray->Reset();
       
   858     }
       
   859 }
       
   860 
       
   861 /*------------------------------------------------------------------------------
       
   862  * public NotifyReceivedMessage
       
   863  * Message arrive in the device. User was prompted for the MMS message.
       
   864  * But user do not want to start the midlet. Later (after some time)
       
   865  * user start the midlet.
       
   866  * MIDlet will receive all the messages. Following method will notify
       
   867  * the listener for the received messages.
       
   868  *------------------------------------------------------------------------------
       
   869  */
       
   870 void CMMSPlatformServiceS60Impl::NotifyReceivedMessageL()
       
   871 {
       
   872     JELOG2(EWMA);
       
   873     TMsvId folderId = KMsvNullIndexEntryId;
       
   874     folderId = mMmsApplicationAdapter->FolderIdL(*mServerApplicationId);
       
   875 
       
   876     if (KMsvNullIndexEntryId != folderId)
       
   877     {
       
   878         CMsvEntry* cEntry = mSession->GetEntryL(folderId);
       
   879         // Show invisible entries
       
   880         // Sort the entries according to date;
       
   881         cEntry->SetSortTypeL(TMsvSelectionOrdering(KMsvNoGrouping,
       
   882                              EMsvSortByDate, ETrue));
       
   883         //get the selection array of the messages in sorted order by date
       
   884         CMsvEntrySelection* msvEntrySelection =
       
   885             cEntry->ChildrenWithTypeL(KUidMsvMessageEntry);
       
   886 
       
   887         //Process each entry, one at a time.
       
   888         TInt count = msvEntrySelection->Count();
       
   889         for (TInt i = 0; i < count; i++)
       
   890         {
       
   891             // this checks the entry and handles it if it is targeted
       
   892             // to midp app we are in startup we do not want to leave here
       
   893             TRAP_IGNORE(DoReceiveMessageL(msvEntrySelection->At(i)));
       
   894         }// end of for loop
       
   895         delete msvEntrySelection;
       
   896         msvEntrySelection = NULL;
       
   897         delete cEntry;
       
   898         cEntry = NULL;
       
   899     }
       
   900 } // end of the method NotifyReceivedMessageL
       
   901 
       
   902 /*------------------------------------------------------------------------------
       
   903  * public HandleEntryEventL
       
   904  * observer the entry for the specific application Id.
       
   905  * if a new message is arrived in this folder notify the listener
       
   906  *------------------------------------------------------------------------------
       
   907  */
       
   908 void CMMSPlatformServiceS60Impl::HandleEntryEventL(
       
   909     TMsvEntryEvent aEvent, TAny* aArg1, TAny*, TAny*)
       
   910 {
       
   911     JELOG2(EWMA);
       
   912     switch (aEvent)
       
   913     {
       
   914     case MMsvSessionObserver::EMsvEntriesChanged:
       
   915     {
       
   916         // We take the created entries into a selection
       
   917         CMsvEntrySelection* entries =
       
   918             static_cast<CMsvEntrySelection*>(aArg1);
       
   919         //Process each created entry, one at a time.
       
   920         for (TInt i = 0; i < entries->Count(); i++)
       
   921         { // this checks the entry and handles it if it is targeted
       
   922             // to midp app
       
   923             TRAP_IGNORE(DoReceiveMessageL(entries->At(i)));
       
   924         } // end of for loop
       
   925     }
       
   926     break;
       
   927     }
       
   928 }
       
   929 
       
   930 /*------------------------------------------------------------------------------
       
   931  * private DoReceiveMessageL
       
   932  * ReceiveMessageL MUST be call from DoReceiveMessageL
       
   933  *------------------------------------------------------------------------------
       
   934  */
       
   935 void CMMSPlatformServiceS60Impl::DoReceiveMessageL(TMsvId aMsvId)
       
   936 {
       
   937     JELOG2(EWMA);
       
   938     ReceiveMessageL(aMsvId);
       
   939 }
       
   940 /*------------------------------------------------------------------------------
       
   941  * protected ReceiveMessageL
       
   942  *
       
   943  *------------------------------------------------------------------------------
       
   944  */
       
   945 void CMMSPlatformServiceS60Impl::ReceiveMessageL(TMsvId aMsvId)
       
   946 {
       
   947     JELOG2(EWMA);
       
   948     CMsvEntry* csvEntry = mSession->GetEntryL(aMsvId);
       
   949     TMsvEntry msvEntry = csvEntry->Entry();
       
   950 
       
   951     if (msvEntry.iMtm != KUidMsgTypeMultimedia)
       
   952     { // It is not MMS, free up the memory of the CMsvEntry
       
   953         delete csvEntry;
       
   954         csvEntry = NULL;
       
   955         return;
       
   956     }
       
   957 
       
   958     if (msvEntry.InPreparation() > 0)
       
   959     { // message is not ready free up the memory of the CMsvEntry
       
   960         delete csvEntry;
       
   961         csvEntry = NULL;
       
   962         return; // entry is not ready to read
       
   963     }
       
   964 
       
   965     mServerMmsMtm->SwitchCurrentEntryL(aMsvId);
       
   966     mServerMmsMtm->LoadMessageL();
       
   967 
       
   968     // Check application ID
       
   969     TPtrC applicationId = mMmsApplicationAdapter->ApplicationIdL(aMsvId);
       
   970     // Local Copy of the Application ID
       
   971     TDesC *localCopy_AppId = NULL;
       
   972     S60CommonUtils::CopyWiderL(applicationId, (TDesC16*&) localCopy_AppId);
       
   973     // AppId and replyToAppId is not working yet so cannot be tested.
       
   974     if (mServerApplicationId->Compare(*localCopy_AppId) != 0)
       
   975     { // received message is not for this connector
       
   976         delete csvEntry;
       
   977         csvEntry = NULL; // free the memory
       
   978         return;
       
   979     }
       
   980     TPtrC replyToappId = mMmsApplicationAdapter->ReplyToApplicationIdL(aMsvId);
       
   981     // Local Copy of reply-to-Application-Id  //TDesC16*&
       
   982     TDesC *localCopy_Reply_To_AppId = NULL;
       
   983     S60CommonUtils::CopyWiderL(replyToappId,(TDesC16*&)localCopy_Reply_To_AppId);
       
   984 
       
   985     // receive message date
       
   986     TInt64 dateTime;
       
   987     if (mServerMmsMtm->SendingDate() != TTime(0))
       
   988     {
       
   989         dateTime = S60CommonUtils::TTimeToJavaTime(
       
   990                        mServerMmsMtm->SendingDate());
       
   991     }
       
   992     else
       
   993     {
       
   994         dateTime = S60CommonUtils::TTimeToJavaTime(msvEntry.iDate);
       
   995     }
       
   996     TDesC *localCopySender = NULL;
       
   997     // only for emulator in local mode to pass the test on the emulator
       
   998 #if(defined (__WINS__) || defined(__WINSCW__))
       
   999     {
       
  1000         const CMsvRecipientList& recipientList =mServerMmsMtm->AddresseeList();
       
  1001         TInt recipientCount = recipientList.Count();
       
  1002         for (TInt j=0; j < recipientCount; j++)
       
  1003         {
       
  1004             const TDesC& address = recipientList[j];
       
  1005             TMsvRecipientType addressType = recipientList.Type(j);
       
  1006             if (EMsvRecipientTo == addressType)
       
  1007             {
       
  1008                 TInt len = S60CommonUtils::CopyWiderL(address,(TDesC16*&)
       
  1009                                                       localCopySender);
       
  1010                 break;
       
  1011             }
       
  1012         }
       
  1013     }
       
  1014 #else
       
  1015     {
       
  1016         const TPtrC sender = mServerMmsMtm->Sender();
       
  1017         S60CommonUtils::CopyWiderL(sender, (TDesC16*&) localCopySender);
       
  1018     }
       
  1019 #endif
       
  1020 
       
  1021     TInt size = mServerMmsMtm->MessageSize();
       
  1022     TDesC8* applicationId8 = NULL;
       
  1023     CMMSMessageInformation* receiveMsgInformation =
       
  1024         CMMSMessageInformation::NewL(*localCopySender, dateTime,
       
  1025                                      *applicationId8, aMsvId);
       
  1026     CleanupStack::PushL(receiveMsgInformation);
       
  1027 
       
  1028     const CMsvRecipientList& recipientList = mServerMmsMtm->AddresseeList();
       
  1029     TInt recipientCount = recipientList.Count();
       
  1030     TDesC16* messageAddress;
       
  1031     for (TInt j = 0; j < recipientCount; j++)
       
  1032     {
       
  1033         messageAddress = NULL;
       
  1034         const TDesC& address = recipientList[j];
       
  1035         TMsvRecipientType addressType = recipientList.Type(j);
       
  1036         S60CommonUtils::CopyWiderL(address, messageAddress);
       
  1037         receiveMsgInformation->AddAddressL((TMsvRecipientTypeValues)addressType,
       
  1038                                            messageAddress);
       
  1039     }
       
  1040 
       
  1041     receiveMsgInformation->SetApplicationIdL(*localCopy_AppId);
       
  1042     receiveMsgInformation->SetReplyToApplicationIdL(*localCopy_Reply_To_AppId);
       
  1043     receiveMsgInformation->SetMessageSize(size);
       
  1044     TPtrC subject = mServerMmsMtm->SubjectL();
       
  1045     receiveMsgInformation->SetMessageSubjectL(subject);
       
  1046     TMmsMessagePriority priority =
       
  1047         (TMmsMessagePriority) mServerMmsMtm->MessagePriority();
       
  1048     receiveMsgInformation->SetMessagePriority(priority);
       
  1049     ReadAttachmentsL(*receiveMsgInformation);
       
  1050 
       
  1051     mReceivedMsgArray->AppendL(receiveMsgInformation);
       
  1052     CleanupStack::Pop(receiveMsgInformation);
       
  1053 
       
  1054     delete csvEntry;
       
  1055     csvEntry = NULL;
       
  1056     delete localCopy_Reply_To_AppId;
       
  1057     localCopy_Reply_To_AppId = NULL;
       
  1058     delete localCopy_AppId;
       
  1059     localCopy_AppId = NULL;
       
  1060     delete localCopySender;
       
  1061     localCopySender = NULL;
       
  1062 
       
  1063     HBufC* buf = HBufC::NewLC(KMaxAppIDLength);
       
  1064     TPtr ptr = buf->Des();
       
  1065     ptr.SetLength(0); // make sure that there is no garbage
       
  1066     ptr.AppendNum(aMsvId);
       
  1067     NotifyJava(*buf, 1);
       
  1068     CleanupStack::PopAndDestroy(buf); //buf
       
  1069     TRAP_IGNORE(DeleteMmsMessageL(aMsvId));
       
  1070 }
       
  1071 
       
  1072 /*------------------------------------------------------------------------------
       
  1073  * Notifies the message listener about the incoming message
       
  1074  * .
       
  1075  *------------------------------------------------------------------------------
       
  1076  */
       
  1077 void CMMSPlatformServiceS60Impl::NotifyJava(const TDesC& aDescription,
       
  1078         int aMessages)
       
  1079 {
       
  1080     JELOG2(EWMA);
       
  1081     TLex lex(aDescription);
       
  1082     TMsvId ID;
       
  1083     lex.Val(ID);
       
  1084     TRAP_IGNORE(mReceivedMMSIDs.AppendL(ID));
       
  1085     mNewMessages += aMessages;
       
  1086     mMessageListener->msgArrived();
       
  1087 }
       
  1088 /*-------------------------------------------------------------------------------
       
  1089  * void ReadAttachmentsL(CMMSMessageInformation& aMidMessageInformation)
       
  1090  *
       
  1091  *-------------------------------------------------------------------------------
       
  1092  */
       
  1093 void CMMSPlatformServiceS60Impl::ReadAttachmentsL(
       
  1094     CMMSMessageInformation& aMidMessageInformation)
       
  1095 {
       
  1096     JELOG2(EWMA);
       
  1097     // Find root attachment
       
  1098     TMsvAttachmentId rootId = mServerMmsMtm->MessageRootAttachment();
       
  1099     CMsvStore* store = mServerMmsMtm->Entry().ReadStoreL();
       
  1100     CleanupStack::PushL(store);
       
  1101     MMsvAttachmentManager& attaMan = store->AttachmentManagerL();
       
  1102     CMsvAttachment* msvAttachment = NULL;
       
  1103     TInt attaCount = attaMan.AttachmentCount();
       
  1104 
       
  1105     for (TInt i = 0; i < attaCount; i++)
       
  1106     {
       
  1107         msvAttachment = attaMan.GetAttachmentInfoL(i);
       
  1108         CleanupStack::PushL(msvAttachment);
       
  1109         // open other message part
       
  1110         // get mime header
       
  1111         CMsvMimeHeaders* mimeHeaders = CMsvMimeHeaders::NewLC();
       
  1112         mimeHeaders->RestoreL(*msvAttachment);
       
  1113 
       
  1114         TMsvAttachmentId attachmentId = msvAttachment->Id();
       
  1115         RFile attaFile = attaMan.GetAttachmentFileL(attachmentId);
       
  1116         TDesC8* dataArray8 = NULL;
       
  1117         ReadDataFromAttachFileL(attaFile, dataArray8);
       
  1118         CleanupStack::PushL(dataArray8);
       
  1119         attaFile.Close();
       
  1120         // mimetype and encoding from mime header
       
  1121         const TDesC8& attachmentType8 = mimeHeaders->ContentType();
       
  1122         const TDesC8& attachmentSubType8 = mimeHeaders->ContentSubType();
       
  1123         HBufC8* attachmentTypeBuf8 = HBufC8::NewLC(attachmentType8.Length() +
       
  1124                                      attachmentSubType8.Length()
       
  1125                                      + KSlashLength +
       
  1126                                      KSlashLength);
       
  1127         TPtr8 ptr = attachmentTypeBuf8->Des();
       
  1128         ptr.SetLength(0);
       
  1129         ptr.Append(attachmentType8);
       
  1130         ptr.Append(KSlash);
       
  1131         ptr.Append(attachmentSubType8);
       
  1132         // Get the values of content-id, content-location
       
  1133         const TDesC8& contentId8 = mimeHeaders->ContentId();
       
  1134         const TDesC& location = mimeHeaders->ContentLocation();
       
  1135         TDesC8* location8 = NULL;
       
  1136         // Check for the content location if we have the same string that
       
  1137         // we use for sending remove the content location.
       
  1138         if ((&location) && location.Find(KTempMmsAttachmentName)== KErrNotFound)
       
  1139         {
       
  1140             S60CommonUtils::ConvertWiderToNarrowL(location, location8);
       
  1141         }
       
  1142         TInt size = msvAttachment->Size();
       
  1143 
       
  1144         CMMSMessageAttachment* midAttachment =
       
  1145             CMMSMessageAttachment::NewL(mRFs, *dataArray8,
       
  1146                                         *attachmentTypeBuf8, contentId8, *location8,
       
  1147                                         false);
       
  1148 
       
  1149         CleanupStack::PushL(midAttachment);
       
  1150 
       
  1151         TUint charEncoding = mimeHeaders->MimeCharset();
       
  1152         midAttachment->SetEncoding(charEncoding);
       
  1153 
       
  1154         if (attachmentId == rootId)
       
  1155         {
       
  1156             midAttachment->SetStarterContentId(true);
       
  1157         }
       
  1158         aMidMessageInformation.InsertAttachmentL(midAttachment);
       
  1159         CleanupStack::Pop(midAttachment); // midAttachment
       
  1160 
       
  1161         delete location8;
       
  1162         location8 = NULL; // location8
       
  1163         // attachmentTypeBuf8
       
  1164         CleanupStack::PopAndDestroy(attachmentTypeBuf8);
       
  1165         CleanupStack::Pop(dataArray8); // dataArray8
       
  1166         // mimeHeaders, attaInfo
       
  1167         CleanupStack::PopAndDestroy(mimeHeaders);
       
  1168         // mimeHeaders, attaInfo
       
  1169         CleanupStack::PopAndDestroy(msvAttachment);
       
  1170     }
       
  1171     CleanupStack::PopAndDestroy(store); // store
       
  1172 }
       
  1173 /*-------------------------------------------------------------------------------
       
  1174  *
       
  1175  *-------------------------------------------------------------------------------
       
  1176  */
       
  1177 void CMMSPlatformServiceS60Impl::ReadDataFromAttachFileL(RFile& aRFile,
       
  1178         TDesC8*& aDataArray)
       
  1179 {
       
  1180     JELOG2(EWMA);
       
  1181     TInt size = 0;
       
  1182     TInt error = KErrNotFound;
       
  1183     error = aRFile.Size(size);
       
  1184     if (error == KErrNone)
       
  1185     {
       
  1186         HBufC8* buffer = HBufC8::NewL(size);
       
  1187         if (buffer)
       
  1188         {
       
  1189             TPtr8 ptr = buffer->Des();
       
  1190             error = aRFile.Read(ptr, size); //read the data in the file
       
  1191         }
       
  1192         aDataArray = buffer;
       
  1193     }
       
  1194 }
       
  1195 
       
  1196 /*------------------------------------------------------------------------------
       
  1197  * public ReceivedMessage(TMsvId aMessageId)
       
  1198  *
       
  1199  *------------------------------------------------------------------------------
       
  1200  */
       
  1201 CMMSMessageInformation* CMMSPlatformServiceS60Impl::ReceivedMessage(
       
  1202     TMsvId aMessageId)
       
  1203 {
       
  1204     JELOG2(EWMA);
       
  1205     CMMSMessageInformation* msgInfo = NULL;
       
  1206     TInt count = mReceivedMsgArray->Count();
       
  1207     for (TInt i = 0; i < count; i++)
       
  1208     {
       
  1209         CMMSMessageInformation* mi =(CMMSMessageInformation*)
       
  1210                                     mReceivedMsgArray->At(i);
       
  1211         if (aMessageId == mi->MessageMsvId()) //check for the message Id
       
  1212         {
       
  1213             msgInfo = mi; // that is the message we are looking for.
       
  1214             mReceivedMsgArray->Delete(i);
       
  1215             break;
       
  1216         }
       
  1217     }
       
  1218     return msgInfo;
       
  1219 }
       
  1220 
       
  1221 /*------------------------------------------------------------------------------
       
  1222  * public destructor
       
  1223  *
       
  1224  *------------------------------------------------------------------------------
       
  1225  */
       
  1226 CMMSPlatformServiceS60Impl::~CMMSPlatformServiceS60Impl()
       
  1227 {
       
  1228     JELOG2(EWMA);
       
  1229     detachFromVm();
       
  1230     stopServer();
       
  1231 }
       
  1232 void CMMSPlatformServiceS60Impl::closeConnection(JNIEnv& aJni, jobject aPeer)
       
  1233 {
       
  1234     JELOG2(EWMA);
       
  1235     // If sending of a message is being carried out unblock the java side with
       
  1236     // KErrCancel.
       
  1237     if (mIsSending)
       
  1238     {
       
  1239         NotifySend(aJni, aPeer, KErrCancel);
       
  1240     }
       
  1241     // Close() Needs to be called in function server thread
       
  1242     TRAP_IGNORE(CallMethodL(this, &CMMSPlatformServiceS60Impl::Close, this));
       
  1243 }
       
  1244 /*------------------------------------------------------------------------------
       
  1245  * private close
       
  1246  *
       
  1247  *------------------------------------------------------------------------------
       
  1248  */
       
  1249 void CMMSPlatformServiceS60Impl::Close()
       
  1250 {
       
  1251     JELOG2(EWMA);
       
  1252     if (mServerConnection)
       
  1253     {
       
  1254         mReceivedMMSIDs.Close();
       
  1255         if (mRegisterCMsvEntry)
       
  1256         {
       
  1257             mRegisterCMsvEntry->RemoveObserver(*this);
       
  1258             delete mRegisterCMsvEntry;
       
  1259             mRegisterCMsvEntry = NULL;
       
  1260         }
       
  1261         if (mServerMmsMtm)
       
  1262         {
       
  1263             delete mServerMmsMtm;
       
  1264             mServerMmsMtm = NULL;
       
  1265         }
       
  1266         if (mReceivedMsgArray)
       
  1267         {
       
  1268             mReceivedMsgArray->ResetAndDestroy();
       
  1269             delete mReceivedMsgArray;
       
  1270             mReceivedMsgArray = NULL;
       
  1271         }
       
  1272         // Unregistering from MMS Engine.
       
  1273         LOG(EWMA, EInfo , "CMMSPlatformServiceS60Impl::Close() Unregistering"
       
  1274             "from MMS Engine");
       
  1275         mMmsApplicationAdapter->UnregisterL(*mServerApplicationId);
       
  1276         if (mServerApplicationId)
       
  1277         {
       
  1278             delete mServerApplicationId;
       
  1279             mServerApplicationId = NULL;
       
  1280         }
       
  1281     }
       
  1282     if (mMmsClient)
       
  1283     {
       
  1284         delete mMmsClient;
       
  1285         mMmsClient = NULL;
       
  1286     }
       
  1287 
       
  1288     if (mClientMtmRegistry)
       
  1289     {
       
  1290         delete mClientMtmRegistry;
       
  1291         mClientMtmRegistry = NULL;
       
  1292     }
       
  1293 
       
  1294     if (mSession)
       
  1295     {
       
  1296         delete mSession;
       
  1297         mSession = NULL;
       
  1298     }
       
  1299 
       
  1300     if (mMmsApplicationAdapter)
       
  1301     {
       
  1302         delete mMmsApplicationAdapter;
       
  1303         mMmsApplicationAdapter = NULL;
       
  1304     }
       
  1305     mSendTMsvIdArray.Close();
       
  1306 
       
  1307     if (mSendMsgArray)
       
  1308     {
       
  1309         mSendMsgArray->ResetAndDestroy();
       
  1310         delete mSendMsgArray;
       
  1311         mSendMsgArray = NULL;
       
  1312     }
       
  1313     if (mMessageHandler)
       
  1314     {
       
  1315         delete mMessageHandler;
       
  1316         mMessageHandler = NULL;
       
  1317     }
       
  1318 }
       
  1319 // End of File
       
  1320 } //namespace wma
       
  1321 } //namespace java
       
  1322