examples/Messaging/SendAs2Example/SendAs2Example.cpp

Go to the documentation of this file.
00001 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // This is a simple example that demonstrates the use of the SendAs v2 protocol to send messages 
00015 //
00016 
00017 
00018 
00022 #include "SendAs2Example.h"
00023 
00024 _LIT(KBodyText, "You are limited only by your fears. Remember it is attitude, more than aptitude,that determines your altitude");
00025 _LIT(KAddress1, "[email protected]");
00026 _LIT(KAddress3, "[email protected]");
00027 _LIT(KAddress4, "[email protected]");
00028 _LIT(KAlias1, "Alias1");
00029 _LIT(KAlias2, "Alias2");
00030 _LIT(KAccount1, " \n SendAs Account1 \n");
00031 _LIT(KAccount2, " \n SendAs Account2 \n");
00032 _LIT(KAccount3, " \n SendAs Account3 \n");
00033 _LIT(KAccountNames,"\n The list of accounts present are: \n");
00034 _LIT(KTextWelcome, " SendAs2 Example \n");
00035 _LIT(KTextStartApp, "\n Starting the SendAs2 Example application .... \n");
00036 _LIT(KTextPressAKey, "\n Press any key to step through the example .... \n");
00037 _LIT(KTextConn, "\n Connecting to sendAs server .... \n");
00038 _LIT(KTextErrInn, "\n Connection is successful\n");
00039 _LIT(KTextErrOut, "\n\n Connection has failed\n");      
00040 _LIT(KTextCreate, "\n Creating a SendAs message .... \n");
00041 _LIT(KTextSend, "\n Sending the sendAs message .... \n");
00042 _LIT(KTextClose, "\n The SendAs message has been sent \n");
00043 _LIT(KPressAnyKey, "\n Press any key.... \n");
00044 _LIT(KExitKey, "\n Press any key to exit the application.... \n");
00045 _LIT(KSmsClient, "SMS Client");
00046 
00051 CSendAs2Example* CSendAs2Example::NewL()
00052         {
00053         CSendAs2Example* self = new (ELeave) CSendAs2Example();
00054         CleanupStack::PushL(self);
00055         self->ConstructL();
00056         CleanupStack::Pop();
00057         return self;
00058         }
00059         
00063 CSendAs2Example::CSendAs2Example()
00064         {       
00065                 
00066         }
00067 
00071 void CSendAs2Example::ConstructL()
00072         {
00073         iConsole = Console::NewL(KTextWelcome,TSize(KConsFullScreen,KConsFullScreen));
00074         iConsole->Printf ( KTextStartApp );
00075         iConsole->Printf ( KTextPressAKey );
00076         iConsole->Getch ();
00077         }       
00078         
00085 CSendAs2Example::~CSendAs2Example()
00086         {       
00087         delete iSelection;
00088         iSelection = NULL;
00089         
00090         delete iEntry;
00091         iEntry = NULL;
00092         
00093         delete iSession;
00094         iSession = NULL;
00095         
00096         delete iObserver;
00097         iObserver = NULL;
00098         
00099         iSendAs.Close();        
00100         delete iConsole;
00101         }
00102         
00109 void CSendAs2Example::StartL()
00110         {
00111         iObserver = new(ELeave) CDummyObserver;
00112         iSession = CMsvSession::OpenSyncL(*iObserver);
00113 
00114         TMsvId aFolderId = KMsvDraftEntryId;
00115         iEntry = CMsvEntry::NewL(*iSession, aFolderId, 
00116                 TMsvSelectionOrdering(KMsvNoGrouping,EMsvSortByNone,ETrue));
00117         
00118         iSelection = iEntry->ChildrenL();
00119 
00120         Connect();
00121         RSendAsMessage message;
00122         CleanMessageFolderL();
00123         CreateL(message);
00124         DisplayAccountL();
00125         CapabilityFilterL();
00126         SendL(message);
00127         }
00128 
00135 void CSendAs2Example::Connect()
00136         {
00137         iConsole->Printf ( KTextConn );
00138         TInt err = iSendAs.Connect(KSendAsDefaultMessageSlots);
00139         if ( err == KErrNone)
00140                 {
00141                 iConsole->Printf ( KTextErrInn );
00142                 }
00143         else
00144                 {
00145                 iConsole->Printf ( KTextErrOut );
00146                 }
00147         }
00148 
00152 void CSendAs2Example::CleanMessageFolderL()
00153         {
00154         TMsvLocalOperationProgress progress;
00155         if (iSelection->Count() > 0)
00156                 {
00157                 iEntry->DeleteL(*iSelection, progress);
00158                 }       
00159         }
00160 
00165 void CSendAs2Example::CreateL( RSendAsMessage& aMessage)
00166         {
00167                 
00168         CSendAsMessageTypes* messageTypes = CSendAsMessageTypes::NewL();
00169         CleanupStack::PushL(messageTypes);      
00170 
00171         // Filter all MTMs that can send messages. 
00172         // This list can be refined by applying filters using FilterAgainstCapability.
00173         iSendAs.FilteredMessageTypesL(*messageTypes);
00174 
00175         TUid sendAsMtmUid;
00176                 
00177         // Returning the message UID based on the message type 
00178         // such as 0 for SMTP Client, 1 for SMS Client
00179         // 2 for Infrared client MTM and 3 for Bluetooth client MTM.
00180         sendAsMtmUid = messageTypes->UidFromNameL(KSmsClient);
00181         
00182         CleanupStack::PopAndDestroy(messageTypes);      
00183         iConsole->Printf ( KTextCreate );
00184         
00185         
00186         iEntry->SetEntryL(KMsvDraftEntryId);
00187         
00188         aMessage.CreateL(iSendAs, sendAsMtmUid);
00189         // Set the body text of this message using a plain descriptor. 
00190         // The object must have an open message to use this method.
00191         aMessage.SetBodyTextL(KBodyText);
00192         // Add recipients to this message.
00193         aMessage.AddRecipientL(KAddress1, KAlias1, RSendAsMessage::ESendAsRecipientTo);
00194         aMessage.AddRecipientL(KAddress3, RSendAsMessage::ESendAsRecipientCc);
00195         aMessage.AddRecipientL(KAddress4, KAlias2, RSendAsMessage::ESendAsRecipientCc);
00196 
00197         CMsvEntry* entry = CMsvEntry::NewL(*iSession, KMsvDraftEntryId, 
00198                 TMsvSelectionOrdering(KMsvNoGrouping,EMsvSortByNone,ETrue));
00199         CleanupStack::PushL(entry);
00200         
00201         // Sets the context to the specified entry.
00202         entry->SetEntryL(KMsvDraftEntryId);
00203         if(iSelection->Count() >= 1)
00204                 {
00205                 entry->SetEntryL((*iSelection)[0]);
00206                 };
00207                 
00208         CleanupStack::PopAndDestroy(entry);
00209         }
00210         
00215 void CSendAs2Example::DisplayAccountL()
00216         {
00217         CSendAsAccounts* accounts = CSendAsAccounts::NewL();
00218         CleanupStack::PushL(accounts);
00219         
00220         // Append 3 new accounts and associated name pair.
00221         if(accounts->Count()==0)
00222                 {
00223                 accounts->AppendAccountL(KAccount1, 0xaaaaaaaa);        
00224                 }
00225         
00226         if(accounts->Count()==1)
00227                 {
00228                 accounts->AppendAccountL(KAccount2, 0x55555555);        
00229                 }
00230                 
00231         if(accounts->Count()==2)
00232                 {
00233                 accounts->AppendAccountL(KAccount3, 0x22222222);        
00234                 }
00235         iConsole->Printf( KAccountNames );
00236         iConsole->Printf ( KPressAnyKey );
00237         iConsole->Getch ();
00238         
00239         // Display the array of names of accounts for this message type
00240         for(TInt i = 0; i < 3; i++)
00241                 {
00242                 TPtrC array = accounts->AccountNames().MdcaPoint(i);
00243                 iConsole->Printf( array );      
00244                 }
00245                 
00246         CleanupStack::PopAndDestroy(accounts);
00247         
00248         }
00249 
00255 void CSendAs2Example::CapabilityFilterL()
00256         {
00257         // Get a list of MTMs available to send the message
00258         // By specifying the features that the MTMs must have.
00259         // Eg: set filter to test that KUidMtmQueryMaxBodySize >= bodySize
00260         User::LeaveIfError(iSendAs.FilterAgainstCapability(KUidMtmQuerySupportAttachments));
00261         User::LeaveIfError(iSendAs.FilterAgainstCapability(KUidMtmQueryMaxBodySize, 0x10, RSendAs::ESendAsGreaterThan));
00262         User::LeaveIfError(iSendAs.FilterAgainstCapability(KUidMtmQuerySupportedBody, KMtm16BitBody, RSendAs::ESendAsBitwiseAnd));
00263         User::LeaveIfError(iSendAs.FilterAgainstCapability(KUidMtmQuerySupportedBody, KMtm16BitBody, RSendAs::ESendAsBitwiseAnd));
00264         User::LeaveIfError(iSendAs.FilterAgainstCapability(KUidMtmQuerySupportSubject));
00265         User::LeaveIfError(iSendAs.FilterAgainstCapability(KUidMtmQuerySupportsFolder));
00266         User::LeaveIfError(iSendAs.FilterAgainstCapability(KUidMtmQueryMaxRecipientCount));
00267         }
00268         
00273 void CSendAs2Example::SendL(RSendAsMessage& aMessage)
00274         {
00275         iConsole->Printf ( KTextSend );
00276         iConsole->Printf ( KPressAnyKey );
00277         iConsole->Getch ();
00278         
00279         CMsvOperationWait* wait = CMsvOperationWait::NewLC();
00280         wait->iStatus = KRequestPending;
00281         wait->Start();
00282         // Asynchronously send the message.
00283         // This will only be allowed to happen if the caller 
00284         // holds sufficient capabilities to perform this action. 
00285         // If the caller does not hold these capabilities, then 
00286         // the message send will be automatically demoted to a confirmed send.
00287         aMessage.SendMessage(wait->iStatus);
00288         
00289         CleanupStack::PopAndDestroy(wait);
00290         iConsole->Printf ( KTextClose );
00291         iConsole->Printf ( KExitKey );
00292         iConsole->Getch ();
00293         }
00294 
00295 LOCAL_C void MainL()
00296         {
00297         // Creates an Active Scheduler to handle asychronous calls.
00298         CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
00299         /*
00300         If an Active Scheduler has been created, installs it .
00301         As it is an asychronous call we need to install it explicitly.
00302         */
00303         CActiveScheduler::Install( scheduler );
00304         
00305         CSendAs2Example* app = CSendAs2Example::NewL();
00306         CleanupStack::PushL(app);
00307         // Calls the main function in which other functions are called.
00308         app->StartL();
00309         CleanupStack::PopAndDestroy(app);
00310         
00311         delete scheduler;
00312         }
00313         
00314 GLDEF_C TInt E32Main()
00315         {
00316     __UHEAP_MARK;
00317     CTrapCleanup* cleanup = CTrapCleanup::New();
00318     if(cleanup == NULL)
00319         {
00320         return KErrNoMemory;
00321         }
00322     TRAPD(err, MainL());
00323         if(err != KErrNone)
00324                 {       
00325                 User::Panic(_L("failed to complete"),err);
00326                 }
00327 
00328     delete cleanup;
00329     __UHEAP_MARKEND;
00330     return KErrNone;
00331         }

Generated by  doxygen 1.6.2