btobexprofiles/obexreceiveservices/btmsgviewer/src/btmsgviewerutils.cpp
branchRCL_3
changeset 23 9386f31cc85b
parent 22 613943a21004
child 24 269724087bed
equal deleted inserted replaced
22:613943a21004 23:9386f31cc85b
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0""
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:  
       
    15  *
       
    16  */
       
    17 
       
    18 #include <mmsvattachmentmanager.h>
       
    19 #include <apgcli.h>
       
    20 #include "btmsgviewerutils.h"
       
    21 
       
    22 const TInt32 KUidMsgTypeBtTInt32 = 0x10009ED5;
       
    23 
       
    24     
       
    25 CBtMsgViewerUtils* CBtMsgViewerUtils::NewL()
       
    26     {
       
    27     CBtMsgViewerUtils* me = new (ELeave) CBtMsgViewerUtils();
       
    28     CleanupStack::PushL(me);
       
    29     me->ConstructL();
       
    30     CleanupStack::Pop(me);
       
    31     return me;
       
    32     }
       
    33 
       
    34 CBtMsgViewerUtils::CBtMsgViewerUtils()
       
    35     {
       
    36     
       
    37     }
       
    38 
       
    39 void CBtMsgViewerUtils::ConstructL()
       
    40     {
       
    41     iMsvSession = CMsvSession::OpenSyncL(*this);
       
    42     }
       
    43 
       
    44 CBtMsgViewerUtils::~CBtMsgViewerUtils()
       
    45     {
       
    46     if ( iMsvSession )
       
    47         {
       
    48         delete iMsvSession;
       
    49         }
       
    50     delete iMimeType;
       
    51     }
       
    52 
       
    53 HBufC* CBtMsgViewerUtils::GetMessagePath(TInt aMessageId, TInt aError)
       
    54     {
       
    55     HBufC* fileName = NULL;
       
    56     TRAP(aError, fileName = HBufC::NewL(KMaxPath));   
       
    57     if(aError < KErrNone)
       
    58         {
       
    59         return fileName;
       
    60         }
       
    61     
       
    62     TRAP(aError, GetMessagePathL(fileName->Des(), aMessageId));
       
    63     return fileName;
       
    64     }
       
    65 
       
    66 void CBtMsgViewerUtils::GetMessagePathL(TPtr aMsgPath, const TInt aMessageId)
       
    67     {
       
    68     CMsvEntry* messageEntry = iMsvSession->GetEntryL(aMessageId);
       
    69     CleanupStack::PushL(messageEntry); //1st push
       
    70     
       
    71     TMsvEntry entry = messageEntry->Entry();
       
    72     if(entry.MtmData1() == KUidMsgTypeBtTInt32)
       
    73         {
       
    74         CMsvStore* store = messageEntry->ReadStoreL();
       
    75         CleanupStack::PushL(store); //2nd push
       
    76 
       
    77         //get file handle for the attachment & the complete path of the file
       
    78         RFile attachmentFile;
       
    79         attachmentFile = store->AttachmentManagerL().GetAttachmentFileL(0);
       
    80         CleanupClosePushL(attachmentFile); //3rd push
       
    81         User::LeaveIfError(attachmentFile.FullName(aMsgPath));
       
    82         CleanupStack::PopAndDestroy(&attachmentFile);
       
    83         StoreMessageMimeTypeL(aMsgPath);
       
    84         
       
    85         //mark attachment as Read
       
    86         TMsvEntry attachEntry = messageEntry->Entry();
       
    87         attachEntry.SetUnread(EFalse);
       
    88         messageEntry->ChangeL(attachEntry);
       
    89         
       
    90         CleanupStack::PopAndDestroy(store);
       
    91         CleanupStack::PopAndDestroy(messageEntry);
       
    92         }
       
    93     else
       
    94         {
       
    95         CMsvEntry* attachmentEntry = iMsvSession->GetEntryL((*messageEntry)[0].Id());
       
    96         CleanupStack::PushL(attachmentEntry); //2nd push
       
    97             
       
    98         CMsvStore* store = attachmentEntry->ReadStoreL();
       
    99         CleanupStack::PushL(store);  //3rd push
       
   100         
       
   101         //get file handle for the attachment & the complete path of the file
       
   102         RFile attachmentFile;
       
   103         attachmentFile = store->AttachmentManagerL().GetAttachmentFileL(0);
       
   104         CleanupClosePushL(attachmentFile);
       
   105         User::LeaveIfError(attachmentFile.FullName(aMsgPath));
       
   106         CleanupStack::PopAndDestroy(&attachmentFile);
       
   107         StoreMessageMimeTypeL(aMsgPath);
       
   108         
       
   109         //mark attachment as Read
       
   110         TMsvEntry attachEntry = attachmentEntry->Entry();
       
   111         attachEntry.SetUnread(EFalse);
       
   112         attachmentEntry->ChangeL(attachEntry);
       
   113         
       
   114         CleanupStack::PopAndDestroy(store);
       
   115         CleanupStack::PopAndDestroy(attachmentEntry);
       
   116         CleanupStack::PopAndDestroy(messageEntry);
       
   117         }
       
   118     }
       
   119 
       
   120 void CBtMsgViewerUtils::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1, 
       
   121                                                     TAny* aArg2, TAny* aArg3)
       
   122     {
       
   123     (void) aEvent;
       
   124     (void) aArg1;
       
   125     (void) aArg2;
       
   126     (void) aArg3;
       
   127     }
       
   128 
       
   129 void CBtMsgViewerUtils::StoreMessageMimeTypeL(TPtr aMsgPath)
       
   130     {
       
   131     RFs rfs;
       
   132     RFile file;
       
   133     
       
   134     User::LeaveIfError(rfs.Connect());
       
   135     
       
   136     User::LeaveIfError(rfs.ShareProtected());
       
   137     
       
   138     User::LeaveIfError(file.Open(rfs, aMsgPath, EFileShareReadersOrWriters | EFileRead));
       
   139     
       
   140     TDataRecognitionResult dataType;
       
   141     RApaLsSession apaSession;
       
   142     
       
   143     if(apaSession.Connect() == KErrNone)
       
   144         {
       
   145         if (apaSession.RecognizeData(file, dataType) == KErrNone)
       
   146             {
       
   147             if(iMimeType)
       
   148                 {
       
   149                 delete iMimeType;
       
   150                 iMimeType = NULL;
       
   151                 }
       
   152         
       
   153             iMimeType = dataType.iDataType.Des8().AllocL();
       
   154             
       
   155             rfs.Close();
       
   156             apaSession.Close();
       
   157             }
       
   158         }
       
   159 
       
   160     rfs.Close();
       
   161     }
       
   162 
       
   163 HBufC8* CBtMsgViewerUtils::GetMimeType()
       
   164     {
       
   165     return iMimeType;
       
   166     }