examples/ForumNokia/ThreadAndActiveObjectsEx/src/threadaoappui.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 // INCLUDES
00031 #include <avkon.hrh>
00032 #include <aknnotewrappers.h> 
00033 #include <ThreadAO.rsg>         // R_SET_REFRESH_TIME_DIALOG
00034 
00035 #include <btnotifierapi.h>
00036 
00037 #include "ThreadAO.pan"
00038 #include "ThreadAOAppUi.h"
00039 #include "ThreadAO.hrh"
00040 #include "threadAOengine.h"
00041 #include "DevicelistContainer.h"
00042 #include "SharedIntermediator.h"
00043 #include "BluetoothRefreshTimer.h"
00044 
00045 #include "ListboxRefreshTimer.h"
00046 
00047 // ----------------------------------------------------------------------------
00048 // CThreadAOAppUi::ConstructL()
00049 //
00050 // Standard Symbian OS 2nd phase constructor. 
00051 // ----------------------------------------------------------------------------
00052 void CThreadAOAppUi::ConstructL()
00053     {
00054     BaseConstructL( CAknAppUi::EAknEnableSkin );
00055 
00056         // Create listbox container
00057     iContainer = CDeviceListContainer::NewL(ClientRect());
00058     iContainer->SetMopParent(this);
00059 
00060         // Add view to control stack
00061         AddToStackL(iContainer);
00062         iSMediator = CSharedIntermediator::NewL(iContainer);
00063 
00064         // Doesn't take ownership
00065         iContainer->SetSMediator(iSMediator);
00066 
00067         // Listbox refresh timer. Needed because calling HandleItemAdditionL
00068         // from thread1 doesn't seem to work.  
00069         iTimer = CListboxRefreshTimer::NewL( iContainer );
00070 
00071         // Start refreshing the the listbox.
00072         iTimer->StartL();
00073 
00074         // Create thread engine which creates thread1.
00075         iThreadEngine = CThreadAOEngine::NewL(iSMediator);
00076         }
00077 
00078 TBool CThreadAOAppUi::TurnBtOnL()
00079     {
00080     RNotifier notifier;
00081     User::LeaveIfError( notifier.Connect() );
00082     TPckgBuf<TBool> dummy(ETrue);
00083     TPckgBuf<TBool> reply(ETrue);
00084     TRequestStatus stat;
00085 
00086     notifier.StartNotifierAndGetResponse(stat, KPowerModeSettingNotifierUid, dummy, reply);
00087     User::WaitForRequest(stat);
00088 
00089     notifier.CancelNotifier(KPowerModeSettingNotifierUid);
00090     notifier.Close();
00091     return reply();
00092     }
00093 
00094 
00095 // ----------------------------------------------------------------------------
00096 // CThreadAOAppUi::CThreadAOAppUi()  
00097 //
00098 // Constructor.
00099 // ----------------------------------------------------------------------------
00100 CThreadAOAppUi::CThreadAOAppUi() : iThreadStarted(EFalse)                          
00101     {
00102     }
00103 
00104 // ----------------------------------------------------------------------------
00105 // CThreadAOAppUi::~CThreadAOAppUi()
00106 //
00107 // Destructor.
00108 // ----------------------------------------------------------------------------
00109 CThreadAOAppUi::~CThreadAOAppUi()
00110     {
00111         delete iThreadEngine;
00112 
00113     if ( iContainer )
00114         {
00115         iEikonEnv->RemoveFromStack(iContainer);
00116         delete iContainer;
00117         }
00118         
00119         delete iTimer;
00120 
00121         delete iSMediator;
00122         }
00123 
00124 // ----------------------------------------------------------------------------
00125 // CThreadAOAppUi::HandleCommandL(TInt aCommand)
00126 //
00127 // Takes care of command handling.
00128 // ----------------------------------------------------------------------------
00129 void CThreadAOAppUi::HandleCommandL(TInt aCommand)
00130     {
00131     switch(aCommand)
00132         {
00133         case EEikCmdExit:
00134         case EAknSoftkeyExit:
00135                         {
00136                         if (iThreadStarted)
00137                                 {
00138                                 // CThreadListener is responsible for exiting the program.
00139                                 // The object is created in ThreadEngine CreateThreadsL().
00140                                 // This is done because thread1 needs to cleanup before killing
00141                                 // the thread.
00142                                 iThreadEngine->Stop();
00143                                 }
00144                         else 
00145                                 {
00146                                 Exit();
00147                                 }
00148                         }
00149             break;
00150 
00151                 case EThreadStartCommand:
00152             {
00153             if (!TurnBtOnL())
00154                 {
00155                 return;
00156                 }
00157 
00158                         // start thread1 ( bluetooth discovery )
00159                     if (!iThreadStarted)
00160                                 {
00161                                 iThreadEngine->StartL();
00162                                 iThreadStarted = ETrue;
00163                                 }
00164             }
00165             break;
00166                 
00167                 case EThreadSetRefreshTimeCommand:
00168             {
00169                         // Query a new refresh time and set the new value.
00170                         if ( iThreadStarted )
00171                                 {
00172                                 TInt newRefreshTime = 0;
00173                             CAknNumberQueryDialog* dlg = new (ELeave) 
00174                            CAknNumberQueryDialog(newRefreshTime, CAknQueryDialog::ENoTone);
00175                                 if ( dlg->ExecuteLD( R_SET_REFRESH_TIME_DIALOG ) )
00176                                         {
00177                                         iSMediator->BTRefreshTimerPtr()->SetRefreshTime(
00178                                                                                   newRefreshTime );
00179                                         }
00180                                 }
00181                         else 
00182                                 // If threadOne hasn't been started, query a new refresh time
00183                                 // and set refresh timer's initial value.
00184                                 {
00185                                 TInt newRefreshTime = 0;
00186                             CAknNumberQueryDialog* dlg = new (ELeave) 
00187                            CAknNumberQueryDialog(newRefreshTime, CAknQueryDialog::ENoTone);
00188                                 if (dlg->ExecuteLD(R_SET_REFRESH_TIME_DIALOG))
00189                                         {
00190                                         iSMediator->SetRefreshTimerInitlVal( newRefreshTime );
00191                                         }
00192                                 }
00193             }
00194             break;
00195 
00196         default:
00197             Panic(EThreadUi);
00198             break;
00199         }
00200     }
00201 
00202 // ---------------------------------------------------------
00203 // CThreadAOAppUi::HandleStatusPaneSizeChange()
00204 // Called by framework when resource is changed.
00205 // ---------------------------------------------------------
00206 //
00207 void CThreadAOAppUi::HandleStatusPaneSizeChange()
00208         {
00209         CAknAppUi::HandleStatusPaneSizeChange(); //call to upper class
00210         iContainer->SetRect( ClientRect());
00211         }
00212 
00213 

Generated by  doxygen 1.6.2