examples/Messaging/SendAs2Example/SendAs2Example.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: This is a simple example that demonstrates the use of the SendAs v2 protocol to send messages   
00028 */
00029 
00030 
00031 
00032 
00033 
00037 #include "SendAs2Example.h"
00038 
00039 _LIT(KBodyText, "You are limited only by your fears. Remember it is attitude, more than aptitude,that determines your altitude");
00040 _LIT(KAddress1, "ban-sindhub01@ban-sindhub01.intra");
00041 _LIT(KAddress3, "ban-sindhub01@ban-sindhub01.intra");
00042 _LIT(KAddress4, "test1@nokia.com");
00043 _LIT(KAlias1, "Alias1");
00044 _LIT(KAlias2, "Alias2");
00045 _LIT(KAccount1, " \n SendAs Account1 \n");
00046 _LIT(KAccount2, " \n SendAs Account2 \n");
00047 _LIT(KAccount3, " \n SendAs Account3 \n");
00048 _LIT(KAccountNames,"\n The list of accounts present are: \n");
00049 _LIT(KTextWelcome, " SendAs2 Example \n");
00050 _LIT(KTextStartApp, "\n Starting the SendAs2 Example application .... \n");
00051 _LIT(KTextPressAKey, "\n Press any key to step through the example .... \n");
00052 _LIT(KTextConn, "\n Connecting to sendAs server .... \n");
00053 _LIT(KTextErrInn, "\n Connection is successful\n");
00054 _LIT(KTextErrOut, "\n\n Connection has failed\n");      
00055 _LIT(KTextCreate, "\n Creating a SendAs message .... \n");
00056 _LIT(KTextSend, "\n Sending the sendAs message .... \n");
00057 _LIT(KTextClose, "\n The SendAs message has been sent \n");
00058 _LIT(KPressAnyKey, "\n Press any key.... \n");
00059 _LIT(KExitKey, "\n Press any key to exit the application.... \n");
00060 _LIT(KSmsClient, "SMS Client");
00061 
00066 CSendAs2Example* CSendAs2Example::NewL()
00067         {
00068         CSendAs2Example* self = new (ELeave) CSendAs2Example();
00069         CleanupStack::PushL(self);
00070         self->ConstructL();
00071         CleanupStack::Pop();
00072         return self;
00073         }
00074         
00078 CSendAs2Example::CSendAs2Example()
00079         {       
00080                 
00081         }
00082 
00086 void CSendAs2Example::ConstructL()
00087         {
00088         iConsole = Console::NewL(KTextWelcome,TSize(KConsFullScreen,KConsFullScreen));
00089         iConsole->Printf ( KTextStartApp );
00090         iConsole->Printf ( KTextPressAKey );
00091         iConsole->Getch ();
00092         }       
00093         
00100 CSendAs2Example::~CSendAs2Example()
00101         {       
00102         delete iSelection;
00103         iSelection = NULL;
00104         
00105         delete iEntry;
00106         iEntry = NULL;
00107         
00108         delete iSession;
00109         iSession = NULL;
00110         
00111         delete iObserver;
00112         iObserver = NULL;
00113         
00114         iSendAs.Close();        
00115         delete iConsole;
00116         }
00117         
00124 void CSendAs2Example::StartL()
00125         {
00126         iObserver = new(ELeave) CDummyObserver;
00127         iSession = CMsvSession::OpenSyncL(*iObserver);
00128 
00129         TMsvId aFolderId = KMsvDraftEntryId;
00130         iEntry = CMsvEntry::NewL(*iSession, aFolderId, 
00131                 TMsvSelectionOrdering(KMsvNoGrouping,EMsvSortByNone,ETrue));
00132         
00133         iSelection = iEntry->ChildrenL();
00134 
00135         Connect();
00136         RSendAsMessage message;
00137         CleanMessageFolderL();
00138         CreateL(message);
00139         DisplayAccountL();
00140         CapabilityFilterL();
00141         SendL(message);
00142         }
00143 
00150 void CSendAs2Example::Connect()
00151         {
00152         iConsole->Printf ( KTextConn );
00153         TInt err = iSendAs.Connect(KSendAsDefaultMessageSlots);
00154         if ( err == KErrNone)
00155                 {
00156                 iConsole->Printf ( KTextErrInn );
00157                 }
00158         else
00159                 {
00160                 iConsole->Printf ( KTextErrOut );
00161                 }
00162         }
00163 
00167 void CSendAs2Example::CleanMessageFolderL()
00168         {
00169         TMsvLocalOperationProgress progress;
00170         if (iSelection->Count() > 0)
00171                 {
00172                 iEntry->DeleteL(*iSelection, progress);
00173                 }       
00174         }
00175 
00180 void CSendAs2Example::CreateL( RSendAsMessage& aMessage)
00181         {
00182                 
00183         CSendAsMessageTypes* messageTypes = CSendAsMessageTypes::NewL();
00184         CleanupStack::PushL(messageTypes);      
00185 
00186         // Filter all MTMs that can send messages. 
00187         // This list can be refined by applying filters using FilterAgainstCapability.
00188         iSendAs.FilteredMessageTypesL(*messageTypes);
00189 
00190         TUid sendAsMtmUid;
00191                 
00192         // Returning the message UID based on the message type 
00193         // such as 0 for SMTP Client, 1 for SMS Client
00194         // 2 for Infrared client MTM and 3 for Bluetooth client MTM.
00195         sendAsMtmUid = messageTypes->UidFromNameL(KSmsClient);
00196         
00197         CleanupStack::PopAndDestroy(messageTypes);      
00198         iConsole->Printf ( KTextCreate );
00199         
00200         
00201         iEntry->SetEntryL(KMsvDraftEntryId);
00202         
00203         aMessage.CreateL(iSendAs, sendAsMtmUid);
00204         // Set the body text of this message using a plain descriptor. 
00205         // The object must have an open message to use this method.
00206         aMessage.SetBodyTextL(KBodyText);
00207         // Add recipients to this message.
00208         aMessage.AddRecipientL(KAddress1, KAlias1, RSendAsMessage::ESendAsRecipientTo);
00209         aMessage.AddRecipientL(KAddress3, RSendAsMessage::ESendAsRecipientCc);
00210         aMessage.AddRecipientL(KAddress4, KAlias2, RSendAsMessage::ESendAsRecipientCc);
00211 
00212         CMsvEntry* entry = CMsvEntry::NewL(*iSession, KMsvDraftEntryId, 
00213                 TMsvSelectionOrdering(KMsvNoGrouping,EMsvSortByNone,ETrue));
00214         CleanupStack::PushL(entry);
00215         
00216         // Sets the context to the specified entry.
00217         entry->SetEntryL(KMsvDraftEntryId);
00218         if(iSelection->Count() >= 1)
00219                 {
00220                 entry->SetEntryL((*iSelection)[0]);
00221                 };
00222                 
00223         CleanupStack::PopAndDestroy(entry);
00224         }
00225         
00230 void CSendAs2Example::DisplayAccountL()
00231         {
00232         CSendAsAccounts* accounts = CSendAsAccounts::NewL();
00233         CleanupStack::PushL(accounts);
00234         
00235         // Append 3 new accounts and associated name pair.
00236         if(accounts->Count()==0)
00237                 {
00238                 accounts->AppendAccountL(KAccount1, 0xaaaaaaaa);        
00239                 }
00240         
00241         if(accounts->Count()==1)
00242                 {
00243                 accounts->AppendAccountL(KAccount2, 0x55555555);        
00244                 }
00245                 
00246         if(accounts->Count()==2)
00247                 {
00248                 accounts->AppendAccountL(KAccount3, 0x22222222);        
00249                 }
00250         iConsole->Printf( KAccountNames );
00251         iConsole->Printf ( KPressAnyKey );
00252         iConsole->Getch ();
00253         
00254         // Display the array of names of accounts for this message type
00255         for(TInt i = 0; i < 3; i++)
00256                 {
00257                 TPtrC array = accounts->AccountNames().MdcaPoint(i);
00258                 iConsole->Printf( array );      
00259                 }
00260                 
00261         CleanupStack::PopAndDestroy(accounts);
00262         
00263         }
00264 
00270 void CSendAs2Example::CapabilityFilterL()
00271         {
00272         // Get a list of MTMs available to send the message
00273         // By specifying the features that the MTMs must have.
00274         // Eg: set filter to test that KUidMtmQueryMaxBodySize >= bodySize
00275         User::LeaveIfError(iSendAs.FilterAgainstCapability(KUidMtmQuerySupportAttachments));
00276         User::LeaveIfError(iSendAs.FilterAgainstCapability(KUidMtmQueryMaxBodySize, 0x10, RSendAs::ESendAsGreaterThan));
00277         User::LeaveIfError(iSendAs.FilterAgainstCapability(KUidMtmQuerySupportedBody, KMtm16BitBody, RSendAs::ESendAsBitwiseAnd));
00278         User::LeaveIfError(iSendAs.FilterAgainstCapability(KUidMtmQuerySupportedBody, KMtm16BitBody, RSendAs::ESendAsBitwiseAnd));
00279         User::LeaveIfError(iSendAs.FilterAgainstCapability(KUidMtmQuerySupportSubject));
00280         User::LeaveIfError(iSendAs.FilterAgainstCapability(KUidMtmQuerySupportsFolder));
00281         User::LeaveIfError(iSendAs.FilterAgainstCapability(KUidMtmQueryMaxRecipientCount));
00282         }
00283         
00288 void CSendAs2Example::SendL(RSendAsMessage& aMessage)
00289         {
00290         iConsole->Printf ( KTextSend );
00291         iConsole->Printf ( KPressAnyKey );
00292         iConsole->Getch ();
00293         
00294         CMsvOperationWait* wait = CMsvOperationWait::NewLC();
00295         wait->iStatus = KRequestPending;
00296         wait->Start();
00297         // Asynchronously send the message.
00298         // This will only be allowed to happen if the caller 
00299         // holds sufficient capabilities to perform this action. 
00300         // If the caller does not hold these capabilities, then 
00301         // the message send will be automatically demoted to a confirmed send.
00302         aMessage.SendMessage(wait->iStatus);
00303         
00304         CleanupStack::PopAndDestroy(wait);
00305         iConsole->Printf ( KTextClose );
00306         iConsole->Printf ( KExitKey );
00307         iConsole->Getch ();
00308         }
00309 
00310 LOCAL_C void MainL()
00311         {
00312         // Creates an Active Scheduler to handle asychronous calls.
00313         CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
00314         /*
00315         If an Active Scheduler has been created, installs it .
00316         As it is an asychronous call we need to install it explicitly.
00317         */
00318         CActiveScheduler::Install( scheduler );
00319         
00320         CSendAs2Example* app = CSendAs2Example::NewL();
00321         CleanupStack::PushL(app);
00322         // Calls the main function in which other functions are called.
00323         app->StartL();
00324         CleanupStack::PopAndDestroy(app);
00325         
00326         delete scheduler;
00327         }
00328         
00329 GLDEF_C TInt E32Main()
00330         {
00331     __UHEAP_MARK;
00332     CTrapCleanup* cleanup = CTrapCleanup::New();
00333     if(cleanup == NULL)
00334         {
00335         return KErrNoMemory;
00336         }
00337     TRAPD(err, MainL());
00338         if(err != KErrNone)
00339                 {       
00340                 User::Panic(_L("failed to complete"),err);
00341                 }
00342 
00343     delete cleanup;
00344     __UHEAP_MARKEND;
00345     return KErrNone;
00346         }

Generated by  doxygen 1.6.2