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

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

Generated by  doxygen 1.6.2