How do I add an attachment to the message
2011-07-21 10:07
441 查看
Question:
How do I add an attachment to the message ?
Answer:
The code snippet below allows you to create and insert a BT/IR message with attachment in the Inbox of the messaging app. You can use this code in your application to help you for example to debug your recognizer for handling your MIME type (instead of testing it on the device by beaming your file over BT or IR), notify a user of an event, deliver a message to the user etc. The code has been compiled against Series60 1.2 and 2.1.
You will need these libraries...
msgs.lib // main messaging library
efsrv.lib
Estor.lib
Include the following header files...
#include
#include OR
#include
// Use these files if you are creating a BT message
//
#include // Series60
#include // UIQ
Step 1
In one of your classes in your code sub-class from MMsvSessionObserver (defined in mtclreg.h) for use when opening a session
with the message server.
class CMyClass : public CBase, public MMsvSessionObserver
{
...
/**
* From MMsvSessionObserver.
*/ void HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* aArg3);
/**
* Creates a message in the Inbox.
*/
void CreateInboxMessageL();
...
...};
Step 2
Use these methods and invoke CreateInboxMessageL() at the appropriate point.
void ExternaliseAndCommit1FileNameL(CMsvEntry* aEntry, const TUid aMsgTypeUid, const TDesC16* aFileName)
{
CMsvStore* messageStore = aEntry->EditStoreL();
CleanupStack::PushL(messageStore);
RMsvWriteStream messageWriteStream;
messageWriteStream.AssignL(*messageStore, aMsgTypeUid);
messageWriteStream.WriteInt32L(aFileName->Length());
messageWriteStream.WriteL(*aFileName);
messageWriteStream.CommitL();
messageWriteStream.Close();
messageStore->CommitL();
CleanupStack::PopAndDestroy(); //messageStore
}
struct TEntryInfoHolder
{
CMsvSession* messageSession;
TMsvId iEntryId;
};
LOCAL_C void DeleteEntry(TAny* aObject)
{
TEntryInfoHolder* object = reinterpret_cast (aObject);
object->messageSession->RemoveEntry(object->iEntryId);
}
void CMyClass::CreateInboxMessageL()
{
CMsvSession* messageSession;
CClientMtmRegistry* registry;
TFileName details;
TFileName description;
TFileName fileName;
TEntryInfoHolder infoHolder;
messageSession = CMsvSession::OpenSyncL(*this);
CleanupStack::PushL(messageSession);
registry = CClientMtmRegistry::NewL(*messageSession);
CleanupStack::PushL(registry);
infoHolder.messageSession=messageSession;
infoHolder.iEntryId=0; // No entry yet !
CleanupStack::PushL(TCleanupItem(DeleteEntry, &infoHolder));
// Subject
_LIT(KSub, "Test Hello");
details.Copy(KSub());
_LIT(KFile, "c:\\testSIS.sis");
_LIT(KFileName, "testSIS.sis");
description.Copy(KFileName());
CMsvEntry* inbox=messageSession->GetEntryL(KMsvGlobalInBoxIndexEntryIdValue);
CleanupStack::PushL(inbox);
TMsvEntry newEntry;
// If using BT uncomment line
// newEntry.iMtm = TUid::Uid(KUidMsgTypeBT);
newEntry.iMtm = TUid::Uid(KUidMsgTypeIr);
newEntry.SetVisible(EFalse);
newEntry.SetInPreparation(ETrue);
newEntry.iType = KUidMsvMessageEntry;
newEntry.iServiceId = KMsvUnknownServiceIndexEntryId;
newEntry.iDate.HomeTime();
newEntry.iSize = 0;//todo
newEntry.iDetails.Set(details);
newEntry.iDescription.Set(description);
inbox->CreateL(newEntry);
infoHolder.iEntryId=newEntry.Id();
CClientMtmRegistry* mtmReg = CClientMtmRegistry::NewL(*messageSession);
CleanupStack::PushL(mtmReg);
CMsvEntry* entry=messageSession->GetEntryL(newEntry.Id());
CleanupStack::PushL(entry);
CBaseMtm *mtm = mtmReg->NewMtmL(entry->Entry().iMtm);
mtm->SetCurrentEntryL(entry); // takes ownership
CleanupStack::Pop(); // entry
CleanupStack::PushL(mtm);
// Create an empty attachment
TFileName tempfileName;
TMsvId attachmentId;
mtm->CreateAttachmentL(attachmentId, tempfileName);
// Construct the path of the file to be created as attachment
HBufC* completeFilePath = HBufC::NewLC(500);
TPtr completeFilePathPtr = completeFilePath->Des();
completeFilePathPtr.Append(tempfileName);
completeFilePathPtr.Append(description);
RFs session;
session.Connect();
CleanupClosePushL(session);
CFileMan* fman = CFileMan::NewL(session);
TInt err =fman->Copy(KFileName(),*completeFilePath);
delete fman;
CleanupStack::PopAndDestroy(); // completeFilePath
CleanupStack::PopAndDestroy(); // session
CMsvEntry* attachment=messageSession->GetEntryL(attachmentId);
TMsvEntry attachmentEntry=attachment->Entry();
CleanupStack::PushL(attachment);
fileName.Append(description);
TEntry fileEntry;
messageSession->FileSession().Entry(fileName, fileEntry);
attachmentEntry.iDetails.Set(description);
attachmentEntry.iDate.HomeTime();
attachmentEntry.iSize = fileEntry.iSize;
// When using BT uncomment below
//
// ExternaliseAndCommit1FileNameL(attachment, TUid::Uid( KUidMsgTypeBT ), &description); // Note: IR does not require the BT UID param.
ExternaliseAndCommit1FileNameL(attachment, TUid::Uid( KUidMsgTypeIr) , &description); // Note: IR does not require the BT UID param.
attachment->ChangeL(attachmentEntry);
CleanupStack::PopAndDestroy(); // attachment
newEntry = entry->Entry();
newEntry.iSize = fileEntry.iSize;
newEntry.SetVisible(ETrue);
newEntry.SetInPreparation(EFalse);
newEntry.SetUnread(ETrue);
entry->ChangeL(newEntry);
CleanupStack::PopAndDestroy(3); // inbox, mtmReg and mtm
CleanupStack::Pop(); // TCleanupItem(DeleteEntry, &infoHolder
CleanupStack::PopAndDestroy(2); // messageSession, registry
}
Caveats
1. Code might compile but not work depending on if BT or IR is supported for a given device.
2. On the Emulator the user is NOT notified after the message has been added in the Inbox. This means that the user has to start the messaging app manually.
3. Refer to FAQ-1138 for limitations on creating user defined mailboxes
How do I add an attachment to the message ?
Answer:
The code snippet below allows you to create and insert a BT/IR message with attachment in the Inbox of the messaging app. You can use this code in your application to help you for example to debug your recognizer for handling your MIME type (instead of testing it on the device by beaming your file over BT or IR), notify a user of an event, deliver a message to the user etc. The code has been compiled against Series60 1.2 and 2.1.
You will need these libraries...
msgs.lib // main messaging library
efsrv.lib
Estor.lib
Include the following header files...
#include
#include OR
#include
// Use these files if you are creating a BT message
//
#include // Series60
#include // UIQ
Step 1
In one of your classes in your code sub-class from MMsvSessionObserver (defined in mtclreg.h) for use when opening a session
with the message server.
class CMyClass : public CBase, public MMsvSessionObserver
{
...
/**
* From MMsvSessionObserver.
*/ void HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* aArg3);
/**
* Creates a message in the Inbox.
*/
void CreateInboxMessageL();
...
...};
Step 2
Use these methods and invoke CreateInboxMessageL() at the appropriate point.
void ExternaliseAndCommit1FileNameL(CMsvEntry* aEntry, const TUid aMsgTypeUid, const TDesC16* aFileName)
{
CMsvStore* messageStore = aEntry->EditStoreL();
CleanupStack::PushL(messageStore);
RMsvWriteStream messageWriteStream;
messageWriteStream.AssignL(*messageStore, aMsgTypeUid);
messageWriteStream.WriteInt32L(aFileName->Length());
messageWriteStream.WriteL(*aFileName);
messageWriteStream.CommitL();
messageWriteStream.Close();
messageStore->CommitL();
CleanupStack::PopAndDestroy(); //messageStore
}
struct TEntryInfoHolder
{
CMsvSession* messageSession;
TMsvId iEntryId;
};
LOCAL_C void DeleteEntry(TAny* aObject)
{
TEntryInfoHolder* object = reinterpret_cast (aObject);
object->messageSession->RemoveEntry(object->iEntryId);
}
void CMyClass::CreateInboxMessageL()
{
CMsvSession* messageSession;
CClientMtmRegistry* registry;
TFileName details;
TFileName description;
TFileName fileName;
TEntryInfoHolder infoHolder;
messageSession = CMsvSession::OpenSyncL(*this);
CleanupStack::PushL(messageSession);
registry = CClientMtmRegistry::NewL(*messageSession);
CleanupStack::PushL(registry);
infoHolder.messageSession=messageSession;
infoHolder.iEntryId=0; // No entry yet !
CleanupStack::PushL(TCleanupItem(DeleteEntry, &infoHolder));
// Subject
_LIT(KSub, "Test Hello");
details.Copy(KSub());
_LIT(KFile, "c:\\testSIS.sis");
_LIT(KFileName, "testSIS.sis");
description.Copy(KFileName());
CMsvEntry* inbox=messageSession->GetEntryL(KMsvGlobalInBoxIndexEntryIdValue);
CleanupStack::PushL(inbox);
TMsvEntry newEntry;
// If using BT uncomment line
// newEntry.iMtm = TUid::Uid(KUidMsgTypeBT);
newEntry.iMtm = TUid::Uid(KUidMsgTypeIr);
newEntry.SetVisible(EFalse);
newEntry.SetInPreparation(ETrue);
newEntry.iType = KUidMsvMessageEntry;
newEntry.iServiceId = KMsvUnknownServiceIndexEntryId;
newEntry.iDate.HomeTime();
newEntry.iSize = 0;//todo
newEntry.iDetails.Set(details);
newEntry.iDescription.Set(description);
inbox->CreateL(newEntry);
infoHolder.iEntryId=newEntry.Id();
CClientMtmRegistry* mtmReg = CClientMtmRegistry::NewL(*messageSession);
CleanupStack::PushL(mtmReg);
CMsvEntry* entry=messageSession->GetEntryL(newEntry.Id());
CleanupStack::PushL(entry);
CBaseMtm *mtm = mtmReg->NewMtmL(entry->Entry().iMtm);
mtm->SetCurrentEntryL(entry); // takes ownership
CleanupStack::Pop(); // entry
CleanupStack::PushL(mtm);
// Create an empty attachment
TFileName tempfileName;
TMsvId attachmentId;
mtm->CreateAttachmentL(attachmentId, tempfileName);
// Construct the path of the file to be created as attachment
HBufC* completeFilePath = HBufC::NewLC(500);
TPtr completeFilePathPtr = completeFilePath->Des();
completeFilePathPtr.Append(tempfileName);
completeFilePathPtr.Append(description);
RFs session;
session.Connect();
CleanupClosePushL(session);
CFileMan* fman = CFileMan::NewL(session);
TInt err =fman->Copy(KFileName(),*completeFilePath);
delete fman;
CleanupStack::PopAndDestroy(); // completeFilePath
CleanupStack::PopAndDestroy(); // session
CMsvEntry* attachment=messageSession->GetEntryL(attachmentId);
TMsvEntry attachmentEntry=attachment->Entry();
CleanupStack::PushL(attachment);
fileName.Append(description);
TEntry fileEntry;
messageSession->FileSession().Entry(fileName, fileEntry);
attachmentEntry.iDetails.Set(description);
attachmentEntry.iDate.HomeTime();
attachmentEntry.iSize = fileEntry.iSize;
// When using BT uncomment below
//
// ExternaliseAndCommit1FileNameL(attachment, TUid::Uid( KUidMsgTypeBT ), &description); // Note: IR does not require the BT UID param.
ExternaliseAndCommit1FileNameL(attachment, TUid::Uid( KUidMsgTypeIr) , &description); // Note: IR does not require the BT UID param.
attachment->ChangeL(attachmentEntry);
CleanupStack::PopAndDestroy(); // attachment
newEntry = entry->Entry();
newEntry.iSize = fileEntry.iSize;
newEntry.SetVisible(ETrue);
newEntry.SetInPreparation(EFalse);
newEntry.SetUnread(ETrue);
entry->ChangeL(newEntry);
CleanupStack::PopAndDestroy(3); // inbox, mtmReg and mtm
CleanupStack::Pop(); // TCleanupItem(DeleteEntry, &infoHolder
CleanupStack::PopAndDestroy(2); // messageSession, registry
}
Caveats
1. Code might compile but not work depending on if BT or IR is supported for a given device.
2. On the Emulator the user is NOT notified after the message has been added in the Inbox. This means that the user has to start the messaging app manually.
3. Refer to FAQ-1138 for limitations on creating user defined mailboxes
相关文章推荐
- How to hide an entry in the Add/Remove Programs applet?
- How do I bind the values of an enum to a ComboBox?
- How to add icon for the item of ListCtrl using C++ (Mail Attachment ListCtrl) - 如何用C++为ListCtrl的Item添加图标(邮件附件ListCtrl)
- How do I add a library project to the Android Studio?
- How do I add an integer value with javascript (jquery) to a value that's returning a string?
- How to Add the JVM Component to an Existing Oracle Database (Doc ID 1461562.1)
- In an iOS 5 Storyboard, how do you push a new scene to the original view controller from a Popover?
- How do I use a modem to send an audio message?
- i.MX Yocto Project: How do I add an existing package to an image?
- How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)
- Windows Phone 7 Beta: How do I save an image to the Pictures Hub
- How do I add a ProgressBar to a .NET StatusBar
- How to Add An Automatically Executable Program in YOCTO?
- How do I check if a type is a subtype OR the type of an object?
- How to add the selinux policy access for the new created linux file node.
- How to make an SDL window the child of another window in Win32
- How to get the xpath by clicking an html element
- SiteMap Controls: How to add a duplicate link in the web.sitemap
- How to Distinguish a Physical Disk Device from an Event Message
- [Maven]How do I tell Maven to use the latest version of a dependency?