examples/ForumNokia/Symbian_OS_End-to-End_Sockets_API_Example/SocketTaskManager_S60/src/TaskManagerAppView.cpp

00001 /*
00002 * ============================================================================
00003 *  Name     : CTaskManagerAppView from TaskManagerAppView.cpp
00004 *  Part of  : TaskManager
00005 *  Created  : 15/03/2006 by Forum Nokia
00006 *  Version  : 1.2
00007 *  Copyright: Nokia Corporation
00008 * ============================================================================
00009 */
00010 
00011 // INCLUDE FILES
00012 #include "TaskManager.hrh"
00013 #include "TaskManagerAppView.h"
00014 #include "TaskManagerAppUi.h"
00015 #include "Response.h"
00016 
00017 #include <coemain.h>
00018 #include <aknlists.h>                   // CAknSingleStyleListBox
00019 #include <barsread.h>                   // TResourceReader
00020 #include <aknnotewrappers.h>    // CAknInformationNote
00021 #include <SocketTaskManager.rsg>
00022 #include <stringloader.h>
00023 #include <e32std.h>
00024 #include <aknquerydialog.h>
00025 
00026 // CONSTANTS
00027 #define KListPosition TPoint(0,0)
00028 _LIT(KTab, "\t");
00029 _LIT(KError, "Error: %d");
00030 _LIT(KNoTasks, "No Tasks!");
00031 _LIT(KLoadingTasks, "Loading tasks...");
00032 _LIT(KCompletingTask, "Completing task...");
00033 _LIT(KTaskCompleted, "\n\nTask completed?");
00034 _LIT(KInvalidTask, "Invalid task. Cannot complete.");
00035 _LIT(KTaskFormat, "%d\t%S");
00036 _LIT(KOpeningConnection, "Opening connection...");
00037 const TInt KMaxErrorLength = 30;
00038 
00039 
00040 // ================= MEMBER FUNCTIONS =======================
00041 
00042 // constructor
00043 CTaskManagerAppView::CTaskManagerAppView(CTaskManagerAppUi& aAppUi)
00044         :       iAppUi(aAppUi)
00045         {
00046         iStatusText = KNoTasks;
00047         }
00048 
00049 // destructor
00050 CTaskManagerAppView::~CTaskManagerAppView()
00051         {
00052         delete iTaskList;
00053         }
00054 
00055 // ----------------------------------------------------
00056 // CTaskManagerAppView::NewL()
00057 // Two-phased constructor.
00058 // ----------------------------------------------------
00059 //
00060 CTaskManagerAppView *CTaskManagerAppView::NewL(const TRect& aRect, CTaskManagerAppUi& aAppUi)
00061         {
00062     CTaskManagerAppView *self = new(ELeave) CTaskManagerAppView(aAppUi);
00063     CleanupStack::PushL(self);
00064     self->ConstructL(aRect);
00065     CleanupStack::Pop(self);
00066     return self;
00067         }
00068 
00069 // ----------------------------------------------------
00070 // CTaskManagerAppView::ConstructL()
00071 // Symbian OS default constructor can leave.
00072 // ----------------------------------------------------
00073 //      
00074 void CTaskManagerAppView::ConstructL(const TRect& aRect)
00075         {
00076     // Create a window for this application view
00077     CreateWindowL();
00078         
00079         CreateListL();
00080         
00081     // Set the windows size
00082     SetRect(aRect);
00083 
00084     // Activate the window, which makes it ready to be drawn
00085     ActivateL();
00086         }
00087 
00088 // ----------------------------------------------------
00089 // CTaskManagerAppView::ConstructL()
00090 // Creates a listbox that is used for showing the tasks
00091 // to the user.
00092 // ----------------------------------------------------
00093 //
00094 void CTaskManagerAppView::CreateListL()
00095         {
00096         iTaskList = new (ELeave) CAknSingleStyleListBox;
00097         iTaskList->SetContainerWindowL( *this );
00098         
00099         iTaskList->SetListBoxObserver( this );
00100         
00101         TResourceReader reader;
00102         iEikonEnv->CreateResourceReaderLC( reader, R_TASKMANAGER_TASKLIST );
00103         iTaskList->ConstructFromResourceL( reader );
00104 
00105         iTaskList->MakeVisible( EFalse );
00106         
00107         CleanupStack::PopAndDestroy(); // ResourceReader
00108         }
00109 
00110 
00111 // ----------------------------------------------------
00112 // CTaskManagerAppView::Draw()
00113 // This function is used for window server-initiated 
00114 // redrawing of controls, and for some 
00115 // application-initiated drawing. Here we show the 
00116 // status text to the user.
00117 // ----------------------------------------------------
00118 //
00119 void CTaskManagerAppView::Draw(const TRect& /*aRect*/) const
00120         {
00121     // Get the standard graphics context 
00122     CWindowGc &gc = SystemGc();
00123     
00124     // Gets the control's extent
00125     TRect rect = Rect();
00126     
00127     // Clears the screen
00128     gc.Clear(rect);
00129     
00130     // status text is showed only if iTaskList isn't visible
00131     if( !iTaskList->IsVisible() )
00132         {
00133                 gc.UseFont( iCoeEnv->NormalFont() );
00134                 
00135                 // This is done to center the text
00136                 TInt pointX = rect.Width() / 2 -
00137                                           iCoeEnv->NormalFont()->TextWidthInPixels( iStatusText ) / 2;
00138 
00139                 TInt pointY = rect.Height() / 2 -
00140                                           iCoeEnv->NormalFont()->HeightInPixels() / 2;
00141                                           
00142                 gc.DrawText( iStatusText, TPoint( pointX, pointY ) );
00143         }
00144 
00145         }
00146 
00147 // ----------------------------------------------------
00148 // CTaskManagerAppView::HandleListBoxEventL()
00149 // Handles list box events. When enter key is pressed, 
00150 // the selected task is marked completed.
00151 // ----------------------------------------------------
00152 //
00153 void CTaskManagerAppView::HandleListBoxEventL(CEikListBox* aListBox, 
00154                                               TListBoxEvent aListBoxEvent)
00155         {
00156         if( aListBoxEvent == MEikListBoxObserver::EEventEnterKeyPressed )
00157                 {
00158 
00159                 const MDesCArray* items = aListBox->Model()->MatchableTextArray();
00160                 
00161                 // We get the currently selected item's text to print it in a query
00162                 TPtrC pointer = items->MdcaPoint( aListBox->CurrentItemIndex() );
00163                 
00164                 TInt tabOffSet = pointer.Find(KTab);
00165                 // id of the task was not found.
00166                 if (tabOffSet == KErrNotFound || tabOffSet == 0)
00167                         {
00168                         CAknInformationNote *informationNote = new(ELeave) CAknInformationNote;
00169                         informationNote->ExecuteLD(KInvalidTask);
00170                         return;
00171                         }
00172                 
00173                 TLex lex(pointer.Left(tabOffSet));
00174                 TInt taskId;
00175                 User::LeaveIfError(lex.Val(taskId));
00176                 
00177                 // (KMaxTaskLength +20) space for task and KTaskCompleted
00178                 TBuf<KMaxTaskLength + 20> message = pointer.Mid(tabOffSet+1);
00179                 message.Append( KTaskCompleted ); 
00180                 
00181                 // A waiting confirmation note
00182                 CAknQueryDialog* note = CAknQueryDialog::NewL(); 
00183                 CleanupStack::PushL(note);
00184                 note->SetPromptL( message );
00185                 CleanupStack::Pop(note);
00186                 
00187                 iAppUi.SetViewBusyL(ETrue);
00188                 
00189                 // The query is shown
00190                 if( note->ExecuteLD( R_TASKMANAGER_TASK_CONFIRMATION_QUERY ) )
00191                         {
00192                         // show 'Completing task' to the user.
00193                         ShowStatus(KCompletingTask);
00194                         
00195                         iAppUi.ShowConnectingCbaL(ETrue);
00196                         iAppUi.Model().MarkTaskDoneL(taskId);
00197                         iTransactionStatus = EMarkingTaskDone;
00198                         }               
00199                 
00200                 iAppUi.SetViewBusyL(EFalse);
00201                 }       
00202         }
00203 
00204 // ----------------------------------------------------
00205 // CTaskManagerAppView::DeleteSelectedTaskL()
00206 // Removes the selected task from the listbox.
00207 // ----------------------------------------------------
00208 //
00209 void CTaskManagerAppView::DeleteSelectedTaskL()
00210         {
00211         CTextListBoxModel* model = iTaskList->Model();
00212         CDesCArray* itemArray = static_cast<CDesCArray*>( model->ItemTextArray() );
00213         
00214         TInt currentItem = iTaskList->CurrentItemIndex();
00215         
00216         itemArray->Delete( currentItem );
00217         
00218         AknListBoxUtils::HandleItemRemovalAndPositionHighlightL( iTaskList,
00219                                                                                                                          currentItem,
00220                                                                                                                          ETrue );
00221 
00222         iTaskList->DrawNow();           
00223         
00224         // no more tasks, show 'No tasks' to user.                                                                                                       
00225         if( model->NumberOfItems() == 0 )
00226                 {
00227                 iTaskList->MakeVisible( EFalse );
00228                 }
00229         }
00230 
00231 // ----------------------------------------------------
00232 // CTaskManagerAppView::ReadTasksL()
00233 // Reads tasks from aResponse and adds them to the listbox.
00234 // ----------------------------------------------------
00235 //
00236 void CTaskManagerAppView::ReadTasksL( const CResponse& aResponse )
00237         {
00238         CTextListBoxModel* model = iTaskList->Model();
00239         
00240         model->SetOwnershipType( ELbmOwnsItemArray ); // Just to underline the relation
00241         CDesCArray* itemArray = static_cast<CDesCArray*>( model->ItemTextArray() );
00242                                                                                  
00243         itemArray->Reset();
00244         
00245         TInt taskCount = aResponse.TaskCount();
00246         // reserve space for task description and for taskid
00247         TBuf<KMaxTaskLength+10> taskDesc;
00248         for (TInt i = 0; i < taskCount; i++)
00249                 {
00250                 TBuf<KMaxTaskLength> desc = aResponse.TaskDescription(i);
00251                 taskDesc.Format(KTaskFormat, aResponse.TaskId(i), &desc);
00252                 itemArray->AppendL(taskDesc);
00253                 }
00254         
00255         // If there are items in the listbox, make the listbox visible.
00256         if( model->NumberOfItems() > 0 )
00257                 {
00258                 iTaskList->HandleItemAdditionL();
00259                 iTaskList->DrawNow();
00260                 iTaskList->MakeVisible( ETrue );
00261                 }
00262         }
00263 
00264 // ----------------------------------------------------
00265 // CTaskManagerAppView::CountComponentControls()
00266 // Gets the number of controls contained in a compound 
00267 // control. 
00268 // ----------------------------------------------------
00269 //
00270 TInt CTaskManagerAppView::CountComponentControls() const
00271         {
00272         TInt count = 0;
00273         if (iTaskList)
00274                 {
00275                 count++;
00276                 }
00277                 
00278         return count;
00279         }
00280 
00281 // ----------------------------------------------------
00282 // CTaskManagerAppView::ComponentControl()
00283 // Gets the specified component of a compound control.
00284 // ----------------------------------------------------
00285 //
00286 CCoeControl* CTaskManagerAppView::ComponentControl( TInt aIndex ) const
00287         {
00288         switch( aIndex )
00289                 {
00290                 case 0:
00291                         return iTaskList;
00292                 default:
00293                         return 0;
00294                 };
00295         }
00296 
00297 // ----------------------------------------------------
00298 // CTaskManagerAppView::SizeChanged()
00299 // Responds to size changes to sets the size and 
00300 // position of the contents of this control. 
00301 // ----------------------------------------------------
00302 //      
00303 void CTaskManagerAppView::SizeChanged()
00304         {
00305         iTaskList->SetExtent( KListPosition, iTaskList->MinimumSize() );        
00306         }
00307 
00308 // ----------------------------------------------------
00309 // CTaskManagerAppView::OfferKeyEventL()
00310 // When a key event occurs, the control framework calls 
00311 // this function for each control on the control stack, 
00312 // until one of them can process the key event 
00313 // (and returns EKeyWasConsumed).
00314 // ----------------------------------------------------
00315 //      
00316 TKeyResponse CTaskManagerAppView::OfferKeyEventL( const TKeyEvent& aKeyEvent,
00317                                                                                                   TEventCode aType )
00318         {
00319         if( iTaskList && iTaskList->IsVisible() )
00320                 {
00321                 return iTaskList->OfferKeyEventL( aKeyEvent, aType );
00322                 }
00323         else
00324                 {
00325                 return EKeyWasNotConsumed;
00326                 }
00327         }
00328 
00329 // ----------------------------------------------------
00330 // CTaskManagerAppView::OpeningConnectionL()
00331 // Called when a GPRS connection is opened.
00332 // ----------------------------------------------------
00333 //      
00334 void CTaskManagerAppView::OpeningConnectionL()
00335         {
00336         ShowStatus(KOpeningConnection);
00337         iAppUi.ShowConnectingCbaL(ETrue);
00338         }
00339         
00340 // ----------------------------------------------------
00341 // CTaskManagerAppView::ConnectingL()
00342 // Called when a transaction is initiated. 
00343 // ----------------------------------------------------
00344 //
00345 void CTaskManagerAppView::ConnectingToServerL(const TBool& aLoadingTasks)
00346         {
00347         if (aLoadingTasks)
00348                 {
00349                 ShowStatus(KLoadingTasks);
00350                 }
00351         else
00352                 {
00353                 ShowStatus(KCompletingTask);
00354                 }
00355 
00356         // show cancel button.
00357         iAppUi.ShowConnectingCbaL(ETrue);
00358         }
00359 
00360 // ----------------------------------------------------
00361 // CTaskManagerAppView::SuccessL()
00362 // Called when the transaction has successfully 
00363 // finished. 
00364 // ----------------------------------------------------
00365 //      
00366 void CTaskManagerAppView::SuccessL(const CResponse& aResponse)
00367         {
00368         // even though the transaction was successful, errors might have occurred
00369         // in the server database.
00370         if (aResponse.HasError())
00371                 {
00372                 CAknInformationNote *informationNote = new(ELeave) CAknInformationNote;
00373                 informationNote->ExecuteLD(aResponse.Error());
00374                 }
00375         // no errors.
00376         else
00377                 {
00378                 // we were completing a task, remove it from the listbox.
00379                 if (iTransactionStatus == EMarkingTaskDone)
00380                         {
00381                         iTransactionStatus = EFetchingTasks;
00382                         DeleteSelectedTaskL();
00383                         }
00384                 // we were loading tasks, show them in the listbox.
00385                 else
00386                         {
00387                         ReadTasksL(aResponse);
00388                         }
00389                 }
00390         
00391         // will show 'No tasks' or if tasks exist, a list of tasks is shown.
00392         ShowStatus(KNoTasks);
00393         
00394         iAppUi.ShowConnectingCbaL(EFalse);
00395         }
00396 
00397 // ----------------------------------------------------
00398 // CTaskManagerAppView::FailedL()
00399 // Called when the transaction has failed. 
00400 // ----------------------------------------------------
00401 //      
00402 void CTaskManagerAppView::FailedL(const TInt& aError)
00403         {
00404         TBuf<KMaxErrorLength> error;
00405         error.Format(KError, aError);
00406         CAknInformationNote *informationNote = new(ELeave) CAknInformationNote;
00407         informationNote->ExecuteLD(error);
00408         
00409         // will show 'No tasks' or if tasks exist, a list of tasks is shown.
00410         ShowStatus(KNoTasks);
00411 
00412         iAppUi.ShowConnectingCbaL(EFalse);
00413         }
00414         
00415 // ----------------------------------------------------
00416 // CTaskManagerAppView::CancelledL()
00417 // Called when the transaction was cancelled by 
00418 // the user. 
00419 // ----------------------------------------------------
00420 //      
00421 void CTaskManagerAppView::CancelledL()
00422         {
00423         // will show 'No tasks' or if tasks exist, a list of tasks is shown.
00424         ShowStatus(KNoTasks);
00425 
00426         iAppUi.ShowConnectingCbaL(EFalse);
00427         }
00428 
00429 // ----------------------------------------------------
00430 // CTaskManagerAppView::ErrorL()
00431 // Called when connection settings are invalid and the 
00432 // transaction cannot be initiated. This occurs 
00433 // e.g. when username and/or password is not set.
00434 // ----------------------------------------------------
00435 //      
00436 void CTaskManagerAppView::ErrorL(const TDesC& aErrorMsg)
00437         {
00438         CAknInformationNote *informationNote = new(ELeave) CAknInformationNote;
00439         informationNote->ExecuteLD(aErrorMsg);
00440         
00441         // will show 'No tasks' or if tasks exist, a list of tasks is shown.
00442         ShowStatus(KNoTasks);
00443 
00444         iAppUi.ShowConnectingCbaL(EFalse);
00445         }
00446 
00447 // ----------------------------------------------------
00448 // CTaskManagerAppView::QueryIapL()
00449 // Opens up a querylist dialog containing all IAPs. User 
00450 // selects from the list the IAP that he/she wants to use.
00451 // ----------------------------------------------------
00452 //              
00453 TBool CTaskManagerAppView::QueryIapL(TUint32& aId, const TUint32& aDefaultId)
00454         {
00455         TBool retval = EFalse;
00456         RArray<TIap>& iaps = iAppUi.Model().Iaps();
00457         TInt iapCount = iaps.Count();
00458         
00459         CDesCArrayFlat* iapArray = new (ELeave) CDesCArrayFlat(iapCount);
00460         CleanupStack::PushL(iapArray);
00461         
00462         TInt selectedIndex = 0;
00463         
00464         // Load all IAPs to the list.
00465         for (TInt i = 0; i < iapCount; i++)
00466                 {
00467                 if (iaps[i].iId == aDefaultId)
00468                         {
00469                         selectedIndex = i;
00470                         }
00471                 iapArray->AppendL(iaps[i].iName);
00472                 }
00473                 
00474         TInt index(0);
00475         CAknListQueryDialog* query = new (ELeave) CAknListQueryDialog(&index);
00476         query->PrepareLC(R_TASKMANAGER_IAP_LIST_QUERY);
00477         query->SetItemTextArray(iapArray);
00478         query->SetOwnershipType(ELbmDoesNotOwnItemArray);
00479         query->ListBox()->SetCurrentItemIndex(selectedIndex);
00480         if (query->RunLD())
00481                 {
00482                 aId = iaps[index].iId;
00483                 retval = ETrue;
00484                 }
00485                 
00486         CleanupStack::PopAndDestroy(iapArray);  
00487         return retval;
00488         }
00489 
00490 // ----------------------------------------------------
00491 // CTaskManagerAppView::ShowStatus()
00492 // Shows proper status text to the user.
00493 // ----------------------------------------------------
00494 //      
00495 void CTaskManagerAppView::ShowStatus(const TDesC& aStatus)
00496         {
00497         iStatusText = aStatus;
00498         
00499         // Show 'No tasks' or if tasks exist, show list of tasks.
00500         if (aStatus == KNoTasks)
00501                 {
00502                 if (iTaskList->Model()->NumberOfItems() > 0)
00503                         {
00504                         iTaskList->MakeVisible(ETrue);
00505                         }
00506                 }
00507         // Show only status text.
00508         else
00509                 {
00510                 iTaskList->MakeVisible(EFalse);
00511                 }
00512                 
00513         DrawNow();
00514         }
00515         
00516 // End of file

Generated by  doxygen 1.6.2