MMSExample.cpp
上传用户:sempras
上传日期:2007-03-04
资源大小:821k
文件大小:19k
- /*
- MMSExample.CPP - source file for MMSExample application
-
- */
- #include <mtclreg.h> // for CClientMtmRegistry
- #include <msvids.h> // for Message type IDs
- #include <mmsclient.h> // for CMmsClientMtm
- #include <AknQueryDialog.h> // for CAknTextQueryDialog
- #include "MMSExample.h" // own definitions
- #include "MMSExample.hrh" // own resource header
- #include <MMSExample.rsg>
- // Own constants
- const TUid KUidMMSExample = { 0x101F402F }; // MMSExample application UID
- const TInt KMaxAddressLength = 80; // maximum length for a recipient address
- const TUid KMMSExampleViewId = { 1 }; // UID of MMSExample view
- //
- // CMMSExampleContainer
- //
- /*
- -------------------------------------------------------------------------------
- CMMSExampleContainer::ConstructL()
- Description: 2nd phase Constructor.
- Return value: N/A
- -------------------------------------------------------------------------------
- */
- void CMMSExampleContainer::ConstructL()
- {
- CreateWindowL();
- }
- /*
- -----------------------------------------------------------------------------
- CMMSExampleContainer::Draw()
- Simple Draw method.
- -----------------------------------------------------------------------------
- */
- void CMMSExampleContainer::Draw(const TRect& /*aRect*/) const
- {
- CWindowGc& gc = SystemGc();
- gc.Clear();
-
- // Draw text "MMS Example for Series 60"
- gc.SetPenColor(KRgbBlack);
- const CFont* fontUsed = iEikonEnv->TitleFont();
- gc.UseFont(fontUsed);
- TInt baseline = (Rect().Height() / 2) - fontUsed->AscentInPixels()*2; // set text 2 * text ascent abowe the centerline
- TInt margin=0; // margin is zero so that the text will be cenetred
- _LIT(K1stLine,"MMS example");
- gc.DrawText(K1stLine,Rect(),baseline,CGraphicsContext::ECenter, margin);
-
- baseline = (Rect().Height() / 2) + fontUsed->AscentInPixels()*2; // 2nd line goes below the centerline
- _LIT(K2ndLine,"for Series 60");
- gc.DrawText(K2ndLine, Rect(), baseline, CGraphicsContext::ECenter, margin);
- }
- //
- // CMMSExampleAppView
- //
- /*
- -----------------------------------------------------------------------------
- CMMSExampleAppView::NewL()
- 2nd phase construction.
- Return values: CMMSExampleAppView*
- -----------------------------------------------------------------------------
- */
- CMMSExampleAppView* CMMSExampleAppView::NewL()
- {
- CMMSExampleAppView* self=NewLC();
- CleanupStack::Pop(); // self
- return self;
- }
- /*
- -----------------------------------------------------------------------------
- CMMSExampleAppView::NewLC()
- 2nd phase construction. Created object is put into CleanupStack
- before calling ConstructL(). Note that object is left in CS so the caller
- must take care of popping it out.
- Return values: CMMSExampleAppView*
- -----------------------------------------------------------------------------
- */
- CMMSExampleAppView* CMMSExampleAppView::NewLC()
- {
- CMMSExampleAppView* self = new(ELeave) CMMSExampleAppView();
- CleanupStack::PushL(self);
- self->ConstructL();
- return self;
- }
- /*
- -----------------------------------------------------------------------------
- CMMSExampleAppView::CMMSExampleAppView()
- C++ constructor
- -----------------------------------------------------------------------------
- */
- CMMSExampleAppView::CMMSExampleAppView()
- {
- }
- /*
- -----------------------------------------------------------------------------
- CMMSExampleAppView::ConstructL()
- 2nd phase constructor.
- Return value: N/A
- -----------------------------------------------------------------------------
- */
- void CMMSExampleAppView::ConstructL()
- {
- }
- /*
- -------------------------------------------------------------------------------
- ~CMMSExampleAppView()
- Description: Destructor.
- Return value: N/A
- -------------------------------------------------------------------------------
- */
- CMMSExampleAppView::~CMMSExampleAppView()
- {
- if(iContainer)
- AppUi()->RemoveFromStack(iContainer);
- }
- /*
- -------------------------------------------------------------------------------
- CMMSExampleAppView::Id()
- Description: Returns the id of the view object.
- Return value: TUid
- -------------------------------------------------------------------------------
- */
- TUid CMMSExampleAppView::Id() const
- {
- return KMMSExampleViewId;
- }
- /*
- -------------------------------------------------------------------------------
- CMMSExampleAppView::DoActivateL()
- Description: Activate this view.
- Return value: N/A
- -------------------------------------------------------------------------------
- */
- void CMMSExampleAppView::DoActivateL(const TVwsViewId& /*aPrevViewId*/, TUid /*aCustomMessageId*/, const TDesC8& /*aCustomMessage*/ )
- {
- if (!iContainer) // container hasn't been created yet
- {
- // Then construct the UI components
- iContainer = new(ELeave) CMMSExampleContainer;
- iContainer->ConstructL(); // Construct a view control
- iContainer->SetRect(ClientRect()); // Sets view control's extent to the space available
- }
- iContainer->ActivateL(); // Activate the view control
- }
- /*
- -------------------------------------------------------------------------------
- CMMSExampleAppView::DoDeactivate()
- Description: Deactivate this view.
- Return value: N/A
- -------------------------------------------------------------------------------
- */
- void CMMSExampleAppView::DoDeactivate()
- {
- if (iContainer)
- {
- delete iContainer;
- iContainer = NULL;
- }
- }
- //
- // CMMSExampleAppUi
- //
- /*
- -----------------------------------------------------------------------------
- CMMSExampleAppUi::ConstructL()
-
- 2nd phase constructor
- -----------------------------------------------------------------------------
- */
- void CMMSExampleAppUi::ConstructL()
- {
- BaseConstructL(); // init this AppUi with standard values
- iRecipient = HBufC::NewL(KMaxAddressLength); // for recipient address (gsm number or E-Mail addr)
- // Create CMsvSession
- iSession = CMsvSession::OpenAsyncL(*this); // new session is opened asynchronously
- // CompleteConstructL() is called when async finishes
- // Series60 view launching
- CMMSExampleAppView* view = CMMSExampleAppView::NewLC();
- AddViewL(view); // add created view to this AppUi
- ActivateLocalViewL( view->Id() ); // activate view
- CleanupStack::Pop(); // view
- }
- /*
- -----------------------------------------------------------------------------
- CMMSExampleAppUi::~CMMSExampleAppUi()
- Destructor.
- -----------------------------------------------------------------------------
- */
- CMMSExampleAppUi::~CMMSExampleAppUi()
- {
- delete iRecipient;
- delete iMmsMtm;
- delete iMtmReg;
- delete iSession; // session must be deleted last (and constructed first)
- }
- /*
- -----------------------------------------------------------------------------
- CMMSExampleAppUi::CompleteConstructL()
- Creates client MTM registry when session is ready for use.
- This completes model construction and is called after 'server
- ready' event is received after async opening of CMsvSession.
- -----------------------------------------------------------------------------
- */
- void CMMSExampleAppUi::CompleteConstructL()
- {
- // We get a MtmClientRegistry from our session
- // this registry is used to instantiate new mtms.
- iMtmReg = CClientMtmRegistry::NewL(*iSession);
- iMmsMtm = (CMmsClientMtm*) iMtmReg->NewMtmL( KUidMsgTypeMultimedia );
- // notify the user with a InfoWin (this will be shown in emulator only)
- iEikonEnv->InfoMsg(_L("Server session opened."));
- }
- /*
- -----------------------------------------------------------------------------
- CMMSExampleAppUi::HandleSessionEventL()
- Receives session events from observer and calls event handling functions.
- Note that if additional session event handlers are defined
- in the session, they are called before this function (as this is the
- main session observer).
- The type of event is indicated by the value of aEvent. The
- interpretation of the TAny arguments depends on this type. For most
- event types, the action that is taken, for example updating the
- display, is client-specific. All clients though should respond to
- EMsvCloseSession and EMsvServerTerminated events.
- -----------------------------------------------------------------------------
- */
- void CMMSExampleAppUi::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* /*aArg1*/, TAny* /*aArg2*/, TAny* /*aArg3*/)
- {
- switch (aEvent)
- {
- // This event tells us that the session has been opened
- case EMsvServerReady:
- CompleteConstructL(); // Construct the mtm registry & MMS mtm
- break;
- default:
- // All other events are ignored
- break;
- }
- }
- /*
- -----------------------------------------------------------------------------
- CMMSExampleAppUi::HandleCommandL(TInt aCommand)
- Handle the commands from CBA and menu items
- -----------------------------------------------------------------------------
- */
- void CMMSExampleAppUi::HandleCommandL(TInt aCommand)
- {
- switch (aCommand)
- {
- case EMMSExampleCmdSend:
- CmdSendL();
- break;
- case EAknSoftkeyExit:
- case EClose:
- CmdExitL();
- break;
- default:
- break;
- }
- }
- /*
- -----------------------------------------------------------------------------
- CMMSExampleAppUi::CmdSendL()
- Handle send command
-
- -----------------------------------------------------------------------------
- */
- void CMMSExampleAppUi::CmdSendL()
- {
- if (!InitializeCommunicationsL())
- {
- // Note that this message will be shown in emulator only!
- iEikonEnv->InfoMsg(_L("Problems in initializingncommunications."));
- return;
- }
- if (!SendMessageL())
- {
- // Note that this message will be shown in emulator only!
- iEikonEnv->InfoMsg(_L("Problems in sendingnmessage."));
- return;
- }
- }
- /*
- -----------------------------------------------------------------------------
- CMMSExampleAppUi::CmdExitL()
-
- Exit application
-
- -----------------------------------------------------------------------------
- */
- void CMMSExampleAppUi::CmdExitL()
- {
- CBaActiveScheduler::Exit(); // Call the CBaActiveScheduler's Exit function
- // that stops the application's thread and destroys it.
- }
- /*
- -----------------------------------------------------------------------------
- CMMSExampleAppUi::InitializeCommunicationsL()
-
- Initialize a new message and ask the user for a recipient address.
- Return values: ETrue or EFalse
- -----------------------------------------------------------------------------
- */
- TBool CMMSExampleAppUi::InitializeCommunicationsL()
- {
- // First the recipients address
- // we get it from a data query dialog.
- TBuf<20> addr = iRecipient->Des();
- CAknTextQueryDialog* telNumDialog = CAknTextQueryDialog::NewL(addr, CAknQueryDialog::ENoTone);
- if (!telNumDialog->ExecuteLD(R_MMSEXAMPLE_TEL_NUMBER_DIALOG))
- return EFalse;
- iRecipient->Des() = addr; // Note that the user can give both numeric and textual data in the query dialog,
- // so the address can be a GSM number or an e-mail address.
- // set up a new message
- CreateNewMessageL();
- return ETrue;
- }
- /*
- -----------------------------------------------------------------------------
- CMMSExampleAppUi::CreateNewMessageL()
- Creates a new message server entry and set up default values.
- Return values: N/A
- -----------------------------------------------------------------------------
- */
- void CMMSExampleAppUi::CreateNewMessageL()
- {
- // - CMsvEntry accesses and acts upon a particular Message Server entry.
- // - NewL() does not create a new entry, but simply a new object to access an existing entry.
- // - It takes in as parameters the client's message server session,
- // ID of the entry to access and initial sorting order of the children of the entry.
- //
- CMsvEntry* entry = CMsvEntry::NewL(*iSession, KMsvGlobalOutBoxIndexEntryId ,TMsvSelectionOrdering());
- CleanupStack::PushL(entry);
- // Set context to the parent folder (Outbox)
- iMmsMtm->SwitchCurrentEntryL( entry->EntryId() );
- // Create new message in the parent folder (Outbox) and set it as the current context.
- // choose the default service settings for Multimedia message sending (this is set in the Messaging
- // applications Settings menu)
- iMmsMtm->CreateMessageL( iMmsMtm->DefaultSettingsL() );
- CleanupStack::PopAndDestroy(); // entry
- }
- /*
- -----------------------------------------------------------------------------
- CMMSExampleAppUi::SendMessageL()
- Prepares the message body and sends the message.
- Return values: ETrue or EFalse
- -----------------------------------------------------------------------------
- */
- TBool CMMSExampleAppUi::SendMessageL()
- {
- // Setting recipients
- //
- // This method has no distinction betweed "To" and "Cc" recipients,
- // use this to add the "To" recipients.
- iMmsMtm->AddAddresseeL( iRecipient->Des() );
- // This example sends the message to only one address but here is the code how to
- // define a "Cc" addressee:
- /*
- _LIT( KAddress2, "name.surname@company.com" );
- TBufC<20> address2( KAddress2 );
- iMmsMtm->AddTypedAddresseeL( address2, EMmsCc ); // typed addressee "Cc"
- */
- // Setting attachments (message parts)
- //
- // Our message consists of one image
- TMsvId attachmentID = KMsvNullIndexEntryId;
- TFileName attachmentFile( _L("c:\system\apps\MMSExample\mmsexample.jpg") );
- iMmsMtm->CreateAttachment2L( attachmentID, attachmentFile );
- // It is possible to give more specific data about the content,
- // for example to define attachment type:
- /*
- TBufC8<20> content = _L8( "image/jpeg" );
- iMmsMtm->SetAttachmentTypeL( attachmentID, content );
- */
- // You can specify the message part that is the root of the presentation. Normally
- // this is SMIL or WML type of entity. If this is omitted then the message does
- // not have presentation part but contains just a bunch of equal media parts.
- /*
- iMmsMtm->SetMessageRoot( attachmentID );
- */
- // Set InPreparation to false
- TMsvEntry ent = iMmsMtm->Entry().Entry();
- ent.SetInPreparation(EFalse);
- ent.SetVisible(ETrue); // mark as visible, after this the message can be seen in Outbox and, after sending, in Sent folder.
- iMmsMtm->Entry().ChangeL(ent); // Commit changes
- // Move message to "Sent" folder after sending
- //iMmsMtm->SetMoveToSent(EFalse); // This method has not been implemented. Therefore it is up to the designer to insure that all messages
- // created by a 3rd party application (especially invisible entries) will be cleaned from the folders.
- // Save changes (If you do not call this method, all changes made will be lost when the context is changed.)
- iMmsMtm->SaveMessageL();
-
- // Start sending the message via the Server MTM to the MMS server
- CMsvOperationWait* wait = CMsvOperationWait::NewLC(); // left in CS
- wait->iStatus = KRequestPending;
- CMsvOperation* op = NULL;
- op = iMmsMtm->SendL( wait->iStatus );
- wait->Start();
- CleanupStack::PushL( op );
- CActiveScheduler::Start();
- // The following is to ignore the completion of other active objects. It is not
- // needed if the app has a command absorbing control.
- while( wait->iStatus.Int() == KRequestPending )
- {
- CActiveScheduler::Start();
- }
- CleanupStack::PopAndDestroy(2); // op, wait
- return ETrue;
- }
- //
- // CMMSExampleDocument
- //
- /*
- -----------------------------------------------------------------------------
- CMMSExampleDocument::NewL(
- 2nd phase construction.
- -----------------------------------------------------------------------------
- */
- CMMSExampleDocument* CMMSExampleDocument::NewL(CEikApplication& aApp)
- {
- CMMSExampleDocument* self = new(ELeave) CMMSExampleDocument(aApp);
- CleanupStack::PushL(self);
- self->ConstructL();
- CleanupStack::Pop(); //self.
- return self;
- }
- /*
- -----------------------------------------------------------------------------
- CMMSExampleDocument::CMMSExampleDocument()
- C++ constructor
- -----------------------------------------------------------------------------
- */
- CMMSExampleDocument::CMMSExampleDocument(CEikApplication& aApp)
- : CEikDocument(aApp)
- {
- }
- /*
- -----------------------------------------------------------------------------
- CMMSExampleDocument::ConstructL()
- 2nd phase constructor.
- -----------------------------------------------------------------------------
- */
- void CMMSExampleDocument::ConstructL()
- {
- }
- /*
- -----------------------------------------------------------------------------
- CMMSExampleDocument::CreateAppUiL()
- Create new CMMSExampleAppUi object
- Return values: CEikAppUi*
- -----------------------------------------------------------------------------
- */
- CEikAppUi* CMMSExampleDocument::CreateAppUiL()
- {
- return (new(ELeave) CMMSExampleAppUi);
- }
- //
- // CMMSExampleApplication
- //
- /*
- -----------------------------------------------------------------------------
- CMMSExampleApplication::AppDllUid()
- Returns application UID of MMSExample application
- -----------------------------------------------------------------------------
- */
- TUid CMMSExampleApplication::AppDllUid() const
- {
- return KUidMMSExample;
- }
- /*
- -----------------------------------------------------------------------------
- CMMSExampleApplication::CreateDocumentL()
- Create new application document
- Return values: CApaDocument*
- -----------------------------------------------------------------------------
- */
- CApaDocument* CMMSExampleApplication::CreateDocumentL()
- {
- return (CMMSExampleDocument::NewL(*this));
- }
- //
- // Functions for Application Architecture
- //
- EXPORT_C CApaApplication* NewApplication()
- {
- return (new CMMSExampleApplication);
- }
- //
- // DLL entry point
- //
- GLDEF_C TInt E32Dll(TDllReason)
- {
- return KErrNone;
- }