examples/Messaging/smsexample/src/smsexample.cpp

00001 // Copyright (c) 2005-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 //
00015 
00016 #include "smsexample.h"
00017 #include <msvuids.h>
00018 #include <csmsaccount.h>
00019 
00023 CExampleActive::CExampleActive()
00024 : CActive(0)
00025     {
00026     CActiveScheduler::Add(this);
00027     }
00031 CExampleActive::~CExampleActive()
00032     {
00033     Cancel();
00034     }
00035 
00039 void CExampleActive::DoCancel()
00040     {
00041     TRequestStatus* s=&iStatus;
00042     User::RequestComplete(s, KErrCancel);
00043     }
00044 
00048 void CExampleActive::StartL()
00049     {
00050     SetActive();
00051     }
00052 
00056 void CExampleActive::RunL() 
00057     {
00058     CActiveScheduler::Stop();
00059     }
00060 
00064 CSmsExample* CSmsExample::NewLC()
00065     {
00066     CSmsExample* self=new(ELeave)CSmsExample();
00067     CleanupStack::PushL(self);
00068     self->ConstructL();
00069     return self;
00070     }
00071 
00075 void CSmsExample::ConstructL()
00076         {
00077         iParaFormat = CParaFormatLayer::NewL();
00078         iCharFormat = CCharFormatLayer::NewL();
00079         iRichText = CRichText::NewL(iParaFormat, iCharFormat);
00080         }  
00081 
00085 CSmsExample::CSmsExample()
00086     {
00087     iSmsServiceId = KMsvNullIndexEntryId;
00088     }
00089 
00093 CSmsExample::~CSmsExample()
00094     {
00095     delete iServiceSettings;
00096     delete iRichText;
00097     delete iCharFormat;
00098     delete iParaFormat;
00099     delete iMsvEntry;
00100     delete iSessionObserver;
00101     delete iSession;
00102     delete iOperation;
00103     delete iActive;
00104     delete iServerEntry;
00105     delete ircpt;
00106     delete iConsole;
00107     }
00111 TBuf<220> CSmsExample::GetUserInput()
00112     {
00113     TBuf<220> buf;
00114     TKeyCode ch = iConsole->Getch();
00115     while(ch != EKeyEnter)
00116         {
00117         _LIT(KChar, "%c");
00118         iConsole->Printf(KChar,ch);
00119         if(ch!= EKeyBackspace)
00120             {
00121             buf.Append(ch);
00122             }
00123         ch=iConsole->Getch();               
00124         }
00125     return buf;
00126     }
00127 
00131 void CSmsExample::ServiceIdL(TUid aMtm, TMsvId& aFirstId, CMsvEntrySelection* aServiceIds)
00132     {
00133     //Returns the Service IDs of MTM aMtm
00134     TMsvSelectionOrdering ordering;  
00135     iSessionObserver = new (ELeave) CSessionObserver();
00136     iSession = CMsvSession::OpenSyncL(*iSessionObserver); 
00137     iMsvEntry = CMsvEntry::NewL(*iSession, KMsvGlobalInBoxIndexEntryIdValue,ordering);
00138     aFirstId = KMsvNullIndexEntryId;
00139     iMsvEntry->SetEntryL(KMsvRootIndexEntryId);
00140 
00141     TMsvSelectionOrdering order;
00142     order.SetShowInvisibleEntries(ETrue);
00143     iMsvEntry->SetSortTypeL(order);
00144     
00145     //Get the children on the Root Index Entry
00146     CMsvEntrySelection* selection = iMsvEntry->ChildrenWithTypeL(KUidMsvServiceEntry);
00147     CleanupStack::PushL(selection);
00148     
00149     TInt count = selection->Count();
00150     
00151     //Find an entry for MTM aMtm
00152     for (TInt curChild = 0; curChild < count && (aFirstId == KMsvNullIndexEntryId || aServiceIds); curChild++)
00153         {
00154         iMsvEntry->SetEntryL(selection->At(curChild));
00155     
00156         if (iMsvEntry->Entry().iMtm == aMtm)
00157             {
00158             TMsvId id = iMsvEntry->Entry().Id();
00159     
00160             if (aFirstId == KMsvNullIndexEntryId)
00161                 {
00162                 aFirstId = id;
00163                 }
00164     
00165             if (aServiceIds)
00166                 {
00167                 aServiceIds->AppendL(id);
00168                 }
00169             }
00170         }
00171 
00172     //Leave if no Service Entry found for MTM aMtm
00173     if (aFirstId == KMsvNullIndexEntryId)
00174         {
00175         CleanupStack::PopAndDestroy(); //selection
00176         User::Leave(KErrNotFound);
00177         }
00178     CleanupStack::PopAndDestroy(selection); //selection
00179     }
00180 
00185 void  CSmsExample::CreateSmsServiceL()
00186     {
00187     // create service in root
00188     TInt err = KErrNone;
00189     iSmsServiceId = 0;
00190     TRAP(err, ServiceIdL(KUidMsgTypeSMS, iSmsServiceId));
00191     TMsvSelectionOrdering ordering;   
00192     // service settings.
00193 
00194     CSmsAccount* account = CSmsAccount::NewLC();
00195     iServiceSettings = CSmsSettings::NewL();
00196     account->LoadSettingsL(*iServiceSettings);
00197                                 
00198     // Remove the default service centres from the service settings
00199     TInt count = iServiceSettings->ServiceCenterCount();
00200     while( count-- )
00201         {
00202         iServiceSettings->RemoveServiceCenter(count);
00203         }
00204     
00205     _LIT(KTxtServiceCenter,"Add Service Center\n");
00206     _LIT(KTxtServiceCenterLimit,"Service Center Name and Number should not be more than 15 characters\n");
00207     _LIT(KTxtServiceCenterName,"Name is : ");
00208     _LIT(KTxtServiceCenterNumber,"\nNumber : ");
00209     iConsole->Printf(KTxtServiceCenterLimit);
00210     iConsole->Printf(KTxtServiceCenter);
00211     iConsole->Printf(KTxtServiceCenterName);  
00212     TBuf<15> name = GetUserInput();
00213     iConsole->Printf(KTxtServiceCenterNumber);
00214     TBuf<15> number = GetUserInput();
00215     // Add the Vodafone service centre - store the settings.
00216     iServiceSettings->AddServiceCenterL(name, number);
00217 
00218     // Set delivery options - do matching and make reports visible. 
00219     iServiceSettings->SetDeliveryReport(ETrue);
00220     iServiceSettings->SetStatusReportHandling(CSmsSettings::EMoveReportToInboxVisibleAndMatch);
00221     account->SaveSettingsL(*iServiceSettings);
00222     
00223     //Get the service centre information.
00224     CSmsServiceCenter* serviceCenter = CSmsServiceCenter::NewL(iServiceSettings->GetServiceCenter(0));
00225     TPtrC name1 = serviceCenter->Name(); //Name of the service center
00226     TPtrC number1 = serviceCenter->Address(); //Address details of the service center
00227     _LIT(KTxtDisplayServiceCenterDetails,"\nService centre name is %S \n Number is %S \n");
00228     iConsole->Printf(KTxtDisplayServiceCenterDetails,&name1,&number1);
00229     CleanupStack::PopAndDestroy(account);
00230     
00231     //used for asynchronous calls
00232     iActive=new(ELeave) CExampleActive;
00233     delete serviceCenter;
00234     }
00235 
00239 void CSmsExample::SetRecipientsL(CSmsHeader& aHeader)
00240     {
00241     _LIT(KTxtSetReceipent,"\nGive the receipent number\n");
00242     iConsole->Printf(KTxtSetReceipent);   
00243     CSmsNumber* ircpt = CSmsNumber::NewL();
00244     _LIT(KTxtReceipentNumberLimit,"Receipent Number should not be more than 15 characters :\n");
00245     iConsole->Printf(KTxtReceipentNumberLimit);
00246     TBuf<15> receipent = GetUserInput();
00247     ircpt->SetAddressL(receipent);
00248     aHeader.Recipients().AppendL(ircpt);
00249     }
00250 
00254 void CSmsExample::CreateMessageL()
00255     {  
00256     _LIT(KTxtCreateMessage,"Creating message...\n");
00257     iConsole->Printf(KTxtCreateMessage);
00258     TMsvEntry entry;
00259     entry.SetVisible(ETrue); 
00260     entry.SetInPreparation(ETrue); 
00261     entry.iServiceId = iSmsServiceId; //iSmsServiceId
00262     entry.iType = KUidMsvMessageEntry; 
00263     entry.iMtm = KUidMsgTypeSMS; 
00264     entry.iDate.HomeTime(); 
00265     entry.iSize = 0; 
00266     entry.iDescription.Set(KNullDesC); 
00267     entry.iDetails.Set(KNullDesC); 
00268     entry.SetSendingState(KMsvSendStateScheduled);
00269    
00270     // Create the SMS header object...
00271     CSmsHeader* header = CSmsHeader::NewL(CSmsPDU::ESmsSubmit, *iRichText);
00272 
00273     // Set the body text...
00274     iRichText->Reset();
00275     _LIT(KMessageData, "Write the Message description\n");
00276     iConsole->Printf(KMessageData);
00277     
00278     _LIT(KTxtMessageLimit,"Total number of characters in the message should not be more than 220 :\n");
00279     iConsole->Printf(KTxtMessageLimit);
00280     TBuf<220> message = GetUserInput();
00281     iRichText->InsertL(0, message);
00282     
00283     // Copy the message settings...
00284     header->SetSmsSettingsL(*iServiceSettings); 
00285     
00286     // Set the service centre
00287     TInt defaultIndex = iServiceSettings->DefaultServiceCenter();
00288     header->SetServiceCenterAddressL(iServiceSettings->GetServiceCenter(defaultIndex).Address());
00289 
00290     // Set recipient - ask derived class
00291     SetRecipientsL(*header);
00292     
00293     // Update entry description and details...
00294     CArrayPtrFlat<CSmsNumber>& recipient = header->Recipients();
00295     entry.iDetails.Set(recipient[0]->Address());
00296     entry.iDescription.Set(iRichText->Read(0, iServiceSettings->DescriptionLength()));
00297     entry.SetInPreparation(EFalse);
00298     
00299     // Create the entry - set context to the global outbox.
00300     iMsvEntry->SetEntryL(KMsvGlobalOutBoxIndexEntryId);
00301     iMsvEntry->CreateL(entry);
00302     
00303     // Create new store and save header information 
00304     iMsvEntry->SetEntryL(entry.Id()); 
00305     CMsvStore* store = iMsvEntry->EditStoreL(); 
00306     CleanupStack::PushL(store); 
00307     header->StoreL(*store);
00308     store->StoreBodyTextL(*iRichText);
00309     store->CommitL(); 
00310     delete header;
00311     CleanupStack::PopAndDestroy(store); 
00312     iMessageId = entry.Id();
00313     }
00314 
00318 void CSmsExample::DisplayMessagesL(TMsvId aFolder)
00319     {
00320     iMsvEntry->SetEntryL(aFolder);
00321     CMsvEntrySelection* sel = iMsvEntry->ChildrenL();
00322     TInt count = sel->Count();
00323     for(TInt i=0;i<count;i++)
00324           {
00325           iMsvEntry->SetEntryL(sel->At(i));
00326           TMsvEntry sel = iMsvEntry->Entry();
00327           _LIT(KTxtMessageDescription,"\nMessage Description is : %S\n");
00328           iConsole->Printf(KTxtMessageDescription,&sel.iDescription);
00329           _LIT(KTxtMessageDetails,"\nMessage Details are :  %S\n");
00330           iConsole->Printf(KTxtMessageDetails,&sel.iDetails);
00331           }
00332     delete sel;
00333     } 
00334 
00342 void CSmsExample::SendMessageL()
00343     {
00344     _LIT(KTxtSendingMessage,"\nSending message...\n");
00345     iConsole->Printf(KTxtSendingMessage);
00346     delete iOperation;
00347     iOperation = NULL;
00348     CMsvEntrySelection* sel = new (ELeave) CMsvEntrySelection();
00349     CleanupStack::PushL(sel);
00350     sel->AppendL(iMessageId);
00351     iMsvEntry->SetEntryL(KMsvGlobalOutBoxIndexEntryId);
00357     iOperation = iMsvEntry->CopyL(*sel, KMsvSentEntryId,iActive->iStatus);  
00358     iActive->StartL();
00359     CActiveScheduler::Start();  
00360     CleanupStack::PopAndDestroy(sel);
00361     }
00362 
00366 void CSmsExample::DeleteMessagesL(TMsvId aFolder)
00367     {
00368     iMsvEntry->SetEntryL(aFolder);
00369     CMsvEntrySelection* sel = iMsvEntry->ChildrenL();
00370     TInt count = sel->Count();
00371     while (count--)
00372         {
00373         iMsvEntry->DeleteL(sel->At(count));
00374         }
00375    delete sel; //delete selection
00376     }
00377 
00381 void CSmsExample::ReceiveMessagesL()
00382     {
00383     _LIT(KTxtReceivingMessage,"Receiving message...\n");   
00384     /*
00385      * Messaging framework receives the messages from the Watchers. The watchers place them in the
00386      * INBOX. For this example, as the network simulation is not present, we are moving the message from 
00387      * OutBox to INBOX and reading it. 
00388      */
00389     iConsole->Printf(KTxtReceivingMessage);
00390     delete iOperation;
00391     iOperation = NULL;
00392     CMsvEntrySelection* sel = new (ELeave) CMsvEntrySelection();
00393     CleanupStack::PushL(sel);
00394     sel->AppendL(iMessageId);
00395     iMsvEntry->SetEntryL(KMsvGlobalOutBoxIndexEntryId);
00396     iOperation = iMsvEntry->CopyL(*sel, KMsvGlobalInBoxIndexEntryId,iActive->iStatus);  
00397     iActive->StartL();
00398     CActiveScheduler::Start();  
00399     DisplayMessagesL(KMsvGlobalInBoxIndexEntryId);
00400     CleanupStack::PopAndDestroy(sel);
00401     }
00402 
00406 void CSmsExample::SendReceiveSmsL()
00407     {   
00408     // Create and install the active scheduler
00409     CActiveScheduler* scheduler= new (ELeave) CActiveScheduler();
00410     CleanupStack::PushL(scheduler);
00411     CActiveScheduler::Install(scheduler); 
00412     _LIT(KTxtExampleCode,"SMS Example \n");
00413     iConsole= Console::NewL(KTxtExampleCode,TSize(KConsFullScreen,KConsFullScreen));
00414     iConsole->Printf(KTxtExampleCode);
00415     _LIT(KTxtEsc,"Press ESC to exit the example\n");
00416     _LIT(KTxtPressAnyKey,"Press any key to continue\n");
00417     _LIT(KFormat,"************************************************************************************\n");   
00418     _LIT(KTxtSmsExampleInfo1,"Create an SMS service by giving the service center details like name and number.\n \
00419             \nCreate a SMS message, with message details and recipient number...\n \
00420             \nOnce you have added a service center, you will use this to send messages.\n");    
00421     _LIT(KTxtSmsExampleInfo2,"\nAll options will allow you to create a service ID if required. \n \
00422          The send and receive options allow you to create a message if required.\n");
00423     _LIT(KTxtSmsExampleInfo3,"\nSo the options do not need to be selected sequentially.\n");  
00424     iConsole->Printf(KFormat);
00425     iConsole->Printf(KTxtSmsExampleInfo1);
00426     iConsole->Printf(KTxtSmsExampleInfo2);
00427     iConsole->Printf(KTxtSmsExampleInfo3);
00428     iConsole->Printf(KFormat);
00429     iConsole->Printf(KTxtPressAnyKey);
00430     iConsole->Printf(KTxtEsc);
00431     TBool serviceCreated = EFalse;
00432     TBool messageCreated = EFalse;
00433     TBool messageSent = EFalse;
00434     TChar tchar = iConsole->Getch();
00435     iConsole->ClearScreen();
00436     //make a choice
00437     while (tchar != EKeyEscape)
00438         {
00439         _LIT(KCreateSmsService,"Option 1 : Create an SMS Service \n");
00440         _LIT(KCreateSms,"Option 2 : Create an SMS message \n");
00441         _LIT(KCreateSendSMS,"Option 3 : Send the message \n");
00442         _LIT(KCreateReceiveSMS,"Option 4 : Receive the message \n");
00443         _LIT(KTxtOptionNotSupported," Option is not supported \n");
00444         iConsole->Printf(KFormat);
00445         iConsole->Printf(KCreateSmsService);
00446         iConsole->Printf(KCreateSms);
00447         iConsole->Printf(KCreateSendSMS);
00448         iConsole->Printf(KCreateReceiveSMS);
00449         iConsole->Printf(KFormat);
00450         TChar option = iConsole->Getch(); 
00451         switch(option)
00452             {
00453             case '1':
00454                 {
00455                 if(!serviceCreated)
00456                     {
00457                     CreateSmsServiceL();
00458                     serviceCreated = ETrue;
00459                     }
00460                 else
00461                     {
00462                     _LIT(KTxtServiceCenterAlreadyExists,"Service center already exists\n");
00463                     iConsole->Printf(KTxtServiceCenterAlreadyExists);
00464                     }
00465                 break;
00466                 }
00467             case '2':
00468                 {
00469                 if(!serviceCreated)
00470                     {
00471                     serviceCreated = ETrue;
00472                     CreateSmsServiceL();
00473                     }
00474                 messageCreated = ETrue;
00475                 DeleteMessagesL(KMsvGlobalOutBoxIndexEntryId);
00476                 DeleteMessagesL(KMsvSentEntryId);
00477                 CreateMessageL();
00478                 DisplayMessagesL(KMsvGlobalOutBoxIndexEntryId);
00479                 break;
00480                 }
00481             case '3':
00482                 {
00483                 if(!serviceCreated)
00484                     {
00485                     serviceCreated = ETrue;
00486                     CreateSmsServiceL();
00487                     }
00488                 if(!messageCreated)
00489                     {
00490                     messageCreated = ETrue;
00491                     DeleteMessagesL(KMsvGlobalOutBoxIndexEntryId);
00492                     DeleteMessagesL(KMsvSentEntryId);         
00493                     CreateMessageL();
00494                     DisplayMessagesL(KMsvGlobalOutBoxIndexEntryId);
00495                     }
00496                 messageSent = ETrue;
00497                 SendMessageL();
00498                 DisplayMessagesL(KMsvSentEntryId);
00499                 break;
00500                 }
00501             case '4':
00502                 {
00503                 if(!serviceCreated)
00504                     {
00505                     serviceCreated = ETrue;
00506                     CreateSmsServiceL();
00507                     }
00508                 if(!messageCreated)
00509                     {
00510                     messageCreated = ETrue;
00511                     DeleteMessagesL(KMsvGlobalOutBoxIndexEntryId);
00512                     DeleteMessagesL(KMsvSentEntryId);         
00513                     CreateMessageL();
00514                     DisplayMessagesL(KMsvGlobalOutBoxIndexEntryId);
00515                     }
00516                 if(!messageSent)
00517                     {
00518                     messageSent = ETrue;
00519                     SendMessageL();
00520                     DisplayMessagesL(KMsvSentEntryId);
00521                     }
00522                 DeleteMessagesL(KMsvGlobalInBoxIndexEntryId);
00523                 ReceiveMessagesL();
00524                 break;
00525                 }
00526             default:
00527                 {
00528                 iConsole->Printf(KTxtOptionNotSupported);
00529                 break;
00530                 }
00531             }
00532         iConsole->Printf(KTxtPressAnyKey);
00533         iConsole->Printf(KTxtEsc);
00534         tchar = iConsole->Getch();
00535         }
00536     CleanupStack::PopAndDestroy(scheduler);
00537     }
00538 
00542 void callExampleL() 
00543     {
00544     CSmsExample* sendreceive = CSmsExample::NewLC();
00545     sendreceive->SendReceiveSmsL();
00546     CleanupStack::PopAndDestroy(sendreceive);
00547     }
00548 
00549 extern TInt E32Main()
00550     {
00551     __UHEAP_MARK;
00552     CTrapCleanup* cleanup=CTrapCleanup::New(); // Create clean-up stack.
00553     if(cleanup!=NULL)
00554         {
00555         TRAPD (error,callExampleL());
00556         _LIT(KTxtSmsExample,"SMS Example");
00557         __ASSERT_ALWAYS(!error,User::Panic(KTxtSmsExample,error));
00558         }
00559     delete cleanup; // Delete clean-up stack.
00560     __UHEAP_MARKEND;
00561     return KErrNone;
00562     }

Generated on Thu Jan 21 10:32:59 2010 for TB10.1 Example Applications by  doxygen 1.5.3