examples/Messaging/SearchSortExample/example.cpp

00001 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // License: "Symbian Foundation License v1.0" to Symbian Foundation
00004 // members and "Symbian Foundation End User License Agreement v1.0"
00005 // to non-members at the URL
00006 // "http://www.symbianfoundation.org/legal/licencesv10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 //
00015 
00016 #include <e32base.h>
00017 #include <e32cons.h>
00018 #include "example.h"
00019 #include "searchsortexample.h"
00020 
00021 
00022 _LIT(KTxtEPOC32EX,"EXAMPLES");
00023 _LIT(KTxtExampleCode,"Symbian platform Example Code");
00024 _LIT(KFormatFailed,"failed: leave code=%d");
00025 _LIT(KTxtOK,"ok");
00026 _LIT(KTxtPressAnyKey," [press any key to exit]");
00027 
00028 
00029 
00030 //constants for MENU:
00031 
00032 _LIT(KMenuRow0,"******************\n");
00033 _LIT(KMenuRow1,"* Press a key\n");
00034 _LIT(KMenuRow2,"* 1    Display The MessageEntry Header\n");
00035 _LIT(KMenuRow3,"* 2    Simple Search Sort without Iterator\n");
00036 _LIT(KMenuRow4,"* 3    Combined Search Sort without Iterator\n");
00037 _LIT(KMenuRow5,"* 4    Search-Sort with Iterator\n");
00038 _LIT(KMenuRow6,"* 5    Search-Sort with Query ID\n");
00039 _LIT(KMenuRow7,"* Press Escape Key to Exit\n");
00040 _LIT(KMenuRow8,"********************\n");
00041 
00042 
00043 //Welcome Message.
00044 _LIT(KMessageRow0,"************************************\n");
00045 _LIT(KWelcomeMessage,"\n*Welcome To Search-Sort Example\n");
00046 _LIT(KMessageRow2,"\n************************************\n");
00047 
00048 _LIT(KNewLine,"\n");
00049 _LIT(KSingleTab,"\t");
00050 _LIT(KDoubleTab,"\t\t");
00051 _LIT(KSpace,"   ");
00052 _LIT(KFromFormat," From :");
00053 _LIT(KToFormat," To :");
00054 _LIT(KSubjectFormat," Subject :");
00055 _LIT(KSizeFormat," Size :");
00056 _LIT(KDateFormat," Date :");
00057 _LIT(KTimeFormat," Time :");
00058 _LIT(KSimpleSearchSort,"Simple Search Sort \n");
00059 _LIT(KSearchSortSettings,"Provide inputs for the setting\n");
00060 _LIT(KCombineSearchSort,"Combined Search Sort \n");
00061 _LIT(KInvalidEntry,"Invalid Entry !!!!!! Enter either y/n\n");
00062 _LIT(KWaitMessage,"Wait till the search is done.................\n");
00063 _LIT(KReturnToMainMenu,"\nReturn to main menu by pressing ENTER key\nFor Exit press ESCAPE key\n");
00064 _LIT(KMessageSummary,"********** Message Summary *****************\n");
00065 _LIT(KTextFormat,"%c\n");
00066 _LIT(KTextFormat1,"%c");
00067 _LIT(KResultFormat,"The ResultCount =%d\n");
00068 _LIT(KNoEntiresFound,"No Entries Found\n");
00069 _LIT(KNoMenuOption,"Not a valid menu option\n");
00070 
00071 // public.
00072 static CConsoleBase* console; // write all your messages to this.
00073 // private.
00074 static void callExampleL(); // initialize with cleanup stack, then do example.
00075 
00076 static void DisplayMenu();
00077 
00078 //Constructor.
00079 
00080 CKeyReader::CKeyReader(CConsoleBase* aConsole,CSearchsortExample* aExample)
00081     :CActive(EPriorityStandard),
00082     iConsole(aConsole),
00083     iSearchSortExample(aExample)
00084 
00085     {
00086         CActiveScheduler::Add(this);
00087         iCaseSensitivityON = EFalse;
00088         iWholeWordSearch = EFalse;
00089         iCombinedSearch= EFalse;
00090     }
00091 
00092 
00093 //Destructor.
00094 CKeyReader::~CKeyReader()
00095     {
00096     Cancel();
00097     }
00098 
00099  //Requesting Function, takes key inputs and calls RunL for handling the event generated.
00100 void CKeyReader::StartL()
00101     {
00102         
00103     iConsole->ClearScreen();
00104     console->Write(KMessageRow0);
00105         console->Write(KWelcomeMessage);
00106         console->Write(KMessageRow2);
00107 
00108     DisplayMenu();
00109     
00110     iStatus = KRequestPending;
00111     SetActive();
00112 
00113     iConsole->Read(iStatus);
00114     CActiveScheduler::Start();
00115     // continues from this point when CActiveScheduler::Stop is called.
00116     User::LeaveIfError(iErrorCondition);
00117     }
00118 
00119 void CKeyReader::RunL()
00120     {
00121     TBool exit = EFalse;
00122     // Get the key code.
00123         TUint8 option = iConsole->KeyCode();
00124         // Print the selected option.
00125         iConsole->Printf(KTextFormat,option);
00126 
00127         // Get the numeric value for key pressed.
00128         TInt number = option - (TUint)'0';
00129 
00130         if(number>0 && number<10)
00131                 {
00132                 switch(number)
00133                         {
00134                         case 1:
00135                                 {
00136                         _LIT(KDisplayMessage,"Displaying header info for Message Entries \n");
00137                         iConsole->Printf(KDisplayMessage);
00138                                 
00139                         for(TInt index=0;index<KMaxEntryCount;index++)
00140                                         {
00141                                 TBuf<128> displaymessage(KFromFormat);
00142                                 displaymessage.Append(iFrominfo[index]);
00143                                 displaymessage.Append(KToFormat);
00144                                 displaymessage.Append(iToinfo[index]);
00145                                 displaymessage.Append(KSubjectFormat);
00146                                 displaymessage.Append(iSubjectinfo[index]);
00147                                 displaymessage.Append(KNewLine);
00148                                 iConsole->Printf(displaymessage);
00149 
00150                                         }
00151                                 iConsole->Printf(KReturnToMainMenu);
00152                                 }
00153                         break;
00154                         case 2:
00155                                 {
00156                         ResetSettings();
00157                         iConsole->Printf(KSimpleSearchSort);
00158                         iConsole->Printf(KSearchSortSettings);
00159                         if(!SearchSortSettings())
00160                             {
00161                             iConsole->Printf(KReturnToMainMenu);
00162                             break;              
00163                             }
00164                         iConsole->Printf(KWaitMessage);
00165 
00166                         iSearchSortExample->SearchSortRequestWithoutIteratorL(iSender,iCaseSensitivityON,iWholeWordSearch,iCombinedSearch,iResultCount);
00167 
00168                         iConsole->Printf(KResultFormat,iResultCount);
00169                         if(iResultCount)
00170                                         DisplayMessageSummary();
00171                         else
00172                                         iConsole->Printf(KNoEntiresFound);
00173 
00174                         iConsole->Printf(KReturnToMainMenu);
00175                                 }
00176                         break;
00177                         case 3:
00178                                 {
00179                                 _LIT(KInfoMessage1,"\nSearch value is set on \"To\", \"Subject\" and \"Size\" fields \n\"To\" field is provided through user input\n\"Size\" part is hard coded to 100 and \"Subject\" to GoodEmail\n");
00180                         ResetSettings();
00181                         iCombinedSearch=ETrue;
00182                         iConsole->Printf(KCombineSearchSort);
00183                         iConsole->Printf(KInfoMessage1);
00184                         iConsole->Printf(KNewLine);
00185                         iConsole->Printf(KSearchSortSettings);
00186                         if(!SearchSortSettings())
00187                             {
00188                             iConsole->Printf(KReturnToMainMenu);
00189                             break;              
00190                             }
00191 
00192                                 iConsole->Printf(KWaitMessage);
00193                         iSearchSortExample->SearchSortRequestWithoutIteratorL(iSender,iCaseSensitivityON,iWholeWordSearch,iCombinedSearch,iResultCount);
00194 
00195                         iConsole->Printf(KResultFormat,iResultCount);
00196                         if(iResultCount)
00197                                         DisplayMessageSummary();
00198                         else
00199                                         iConsole->Printf(KNoEntiresFound);
00200 
00201                         iConsole->Printf(KReturnToMainMenu);
00202                                 }
00203                         break;
00204                         case 4:
00205                                 {
00206                         _LIT(KSearchSortWithIterator,"Search-Sort with Iterator \n");
00207                         _LIT(KInfoMessage2,"The Search is made on Size, hard coded to value 100\n");
00208                         _LIT(KResultMessage1,"Searching By Iterator done, ResultCount=%d\n");
00209 
00210                         iConsole->Printf(KSearchSortWithIterator);
00211                         iConsole->Printf(KInfoMessage2);
00212                         iSearchSortExample->SearchSortRequestWithIteratorL(iResultCount);
00213                         iConsole->Printf(KResultMessage1,iResultCount);
00214                         iConsole->Printf(KReturnToMainMenu);
00215                                 }
00216                         break;
00217                         case 5:
00218                                 {
00219                         _LIT(KSearchSortQueryID,"Search-Sort using Query ID \n");
00220                         _LIT(KResultMessage2,"Searching By QueryID done, ResultCount=%d\n");
00221 
00222                         iConsole->Printf(KSearchSortQueryID);
00223                         iSearchSortExample->SearchSortRequestByQueryIdL(iResultCount);
00224                         iConsole->Printf(KResultMessage2,iResultCount);
00225                         if(iResultCount)
00226                                         DisplayMessageSummary();
00227                         else
00228                                         iConsole->Printf(KNoEntiresFound);
00229 
00230                         iConsole->Printf(KReturnToMainMenu);
00231                                 }
00232                         break;
00233                         default:
00234                                 {
00235                                 iConsole->Printf(KNoMenuOption);
00236                                 exit=ETrue;
00237                         break;  
00238                                 }
00239                         }
00240                 }
00241         else if(option==EKeyEscape)
00242                 {
00243                 exit=ETrue;
00244                 }
00245         else if(option==EKeyEnter)
00246                 {
00247                 DisplayMenu();
00248                 exit=EFalse;
00249                 }
00250         else
00251                 {
00252                 iConsole->Printf(KNoMenuOption);
00253                 }
00254         if (exit)
00255                 {
00256                 Cancel();
00257                 CActiveScheduler::Stop();
00258                 }
00259         else
00260                 {
00261                 iStatus = KRequestPending;
00262                 SetActive();
00263                 iConsole->Read(iStatus);
00264                 }
00265         }
00266 
00267 void CKeyReader::DoCancel()
00268     {
00269 
00270     }
00271 
00272 TInt CKeyReader::RunError(TInt aError)
00273     {
00274     iErrorCondition = aError;
00275     CActiveScheduler::Stop();
00276     return KErrNone;
00277     }
00278 
00279 //Function for creating message entries.
00280 void CKeyReader::CreateMessageEntriesL()
00281         {
00282         //Making settings before creating the message.
00283         iSearchSortExample->CreateClientRegistryL();
00284         iSearchSortExample->CreatePopAndSmtpAccountL();
00285 
00286         for(TInt index=0;index<KMaxEntryCount;index++)
00287                 {
00288                 iSearchSortExample->CreateSmtpMessageL(index,iFrominfo[index],iToinfo[index],iSubjectinfo[index]);
00289                 }
00290         }
00291 
00292 //Function for Displaying message summary.
00293 void CKeyReader::DisplayMessageSummary()
00294         {
00295         iConsole->Printf(KNewLine);
00296         iConsole->Printf(KMessageSummary);
00297         TBuf<200> messageSummaryTable;
00298         messageSummaryTable.Append(KToFormat);
00299         messageSummaryTable.Append(KDoubleTab);
00300         messageSummaryTable.Append(KSingleTab);
00301         messageSummaryTable.Append(KSizeFormat);
00302         messageSummaryTable.Append(KDoubleTab);
00303         messageSummaryTable.Append(KDateFormat);
00304         messageSummaryTable.Append(KDoubleTab);
00305         messageSummaryTable.Append(KTimeFormat);
00306         messageSummaryTable.Append(KDoubleTab);
00307         messageSummaryTable.Append(KSubjectFormat);
00308         messageSummaryTable.Append(KDoubleTab);
00309         messageSummaryTable.Append(KNewLine);
00310         iConsole->Printf(messageSummaryTable);
00311         
00312         for(TInt index=0;index<iResultCount;index++)
00313                 {
00314                 TResultSummary tempsummary=iSearchSortExample->iTResultSummary[index];
00315                 TBuf<200> displaymessageSummary;
00316                 displaymessageSummary.Append(tempsummary.iToText);
00317                 displaymessageSummary.Append(KSpace);
00318                 displaymessageSummary.AppendNum(tempsummary.iSize);
00319                 displaymessageSummary.Append(KDoubleTab);
00320                 displaymessageSummary.Append(tempsummary.iDatestring);
00321                 displaymessageSummary.Append(KSingleTab);
00322                 displaymessageSummary.Append(tempsummary.iTimestring);
00323                 displaymessageSummary.Append(KDoubleTab);
00324                 displaymessageSummary.Append(tempsummary.iSubjectText);
00325                 displaymessageSummary.Append(KNewLine);
00326                 iConsole->Printf(displaymessageSummary);
00327                 }
00328         }
00329 
00330 //Settings done for search-sort depending on the user input.
00331 TBool CKeyReader::SearchSortSettings()
00332         {
00333         _LIT(KText,"Enter the To field to be searched on \n e.g exampleUser,exampleUser2@nokia.com etc.....\n");
00334         iConsole->Printf(KText);
00335         iSender.Delete(0,iSender.Length());
00336         TKeyCode string;
00337 
00338         while((string=iConsole->Getch())!=EKeyEnter)
00339                 {
00340                 iConsole->Printf(KTextFormat1,string);
00341                 iSender.Append(string);
00342                 }
00343 
00344         iConsole->Printf(KNewLine);
00345         iConsole->Printf(iSender);
00346         iConsole->Printf(KNewLine);
00347         iSender.Trim();
00348         if(iSender.Length()<= 0)
00349                 {
00350                 _LIT(KInValidMsg,"Enter a valid field to be searched\n");
00351                 iConsole->Printf(KInValidMsg);
00352                 return EFalse;
00353                 }
00354         _LIT(KText1,"Case Sensitivity Search ON? y/n\n");
00355         iConsole->Printf(KText1);
00356 
00357         while(ETrue)
00358                 {
00359                 string=iConsole->Getch();
00360                 if(string=='y')
00361                         {
00362                         iConsole->Printf(KTextFormat,string);
00363                         iCaseSensitivityON = ETrue;
00364                         break;
00365                         }
00366                 else if(string=='n')
00367                         {
00368                         iConsole->Printf(KTextFormat,string);
00369                         iCaseSensitivityON = EFalse;
00370                         break;
00371                         }
00372                 else
00373                         {
00374                         iConsole->Printf(KInvalidEntry);
00375                         }
00376                 }
00377 
00378         _LIT(KText2,"Whole Word Search ON? y/n\n");
00379         iConsole->Printf(KText2);
00380 
00381         while(ETrue)
00382                 {
00383                 string=iConsole->Getch();
00384 
00385                 if(string=='y')
00386                         {
00387                         iConsole->Printf(KTextFormat,string);
00388                         iWholeWordSearch = ETrue;
00389                         break;
00390                         }
00391                 else if(string=='n')
00392                         {
00393                         iConsole->Printf(KTextFormat,string);
00394                         iWholeWordSearch = EFalse;
00395                         break;
00396                         }
00397                 else
00398                         {
00399                         iConsole->Printf(KInvalidEntry);
00400                         }
00401                 }
00402         return ETrue;
00403         }
00404 
00405 //Reseting the search-sort settings made.
00406 void CKeyReader::ResetSettings()
00407         {
00408         iCaseSensitivityON =EFalse;
00409         iWholeWordSearch = EFalse;
00410         iCombinedSearch=EFalse;
00411         }
00412 
00413 
00414 static void DisplayMenu()
00415     {
00416     console->Printf(KMenuRow0);
00417     console->Printf(KMenuRow1);
00418     console->Printf(KMenuRow2);
00419     console->Printf(KMenuRow3);
00420     console->Printf(KMenuRow4);
00421     console->Printf(KMenuRow5);
00422     console->Printf(KMenuRow6);
00423     console->Printf(KMenuRow7);
00424     console->Printf(KMenuRow8);
00425         
00426     }
00427 
00428 static void MainL()
00429     {
00430 
00431         CSearchsortExample* example=CSearchsortExample::NewL();
00432         CleanupStack::PushL(example);
00433 
00434         CKeyReader* reader = new (ELeave) CKeyReader(console,example);
00435     CleanupStack::PushL(reader);
00436 
00437     reader->CreateMessageEntriesL();
00438     reader->StartL();
00439     example->DeleteAccountsL(); 
00440 
00441     CleanupStack::PopAndDestroy(reader);
00442     CleanupStack::PopAndDestroy(example);
00443     }
00444 
00445 // do the example.
00446 static void DoStartL()
00447     {
00448         // Create active scheduler (to run active objects).
00449         CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
00450         CleanupStack::PushL(scheduler);
00451         CActiveScheduler::Install(scheduler);
00452 
00453         MainL();
00454 
00455         // Delete active scheduler.
00456         CleanupStack::PopAndDestroy(scheduler);
00457         }
00458 
00459 static void callExampleL() // initialize and call example code under cleanup stack.
00460     {
00461         console=Console::NewL(KTxtExampleCode,TSize(KConsFullScreen,KConsFullScreen));
00462         CleanupStack::PushL(console);
00463         TRAPD(error,DoStartL()); // perform example function.
00464         if (error)
00465                 console->Printf(KFormatFailed, error);
00466         else
00467                 console->Printf(KTxtOK);
00468         console->Printf(KTxtPressAnyKey);
00469         console->Getch(); // get and ignore character.
00470         CleanupStack::PopAndDestroy(); // close console.
00471     }
00472 
00473 extern TInt E32Main() // main function called by E32.
00474     {
00475         __UHEAP_MARK;
00476         CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack.
00477 
00478         if(cleanup == NULL)
00479         {
00480         return KErrNoMemory;
00481         }
00482 
00483         TRAPD(error,callExampleL()); // more initialization, then do example.
00484         __ASSERT_ALWAYS(!error,User::Panic(KTxtEPOC32EX,error));
00485         delete cleanup; // destroy clean-up stack.
00486         __UHEAP_MARKEND;
00487         return 0; // and return.
00488     }
00489 
00490 

Generated by  doxygen 1.6.2