bluetoothengine/btui/devmodel/src/btregistryobserver.cpp
changeset 0 f63038272f30
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2006-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Monitors BTRegistry device changes and sends the 
       
    15 *				 new status to listener
       
    16 *
       
    17 */
       
    18 
       
    19 #include "btregistryobserver.h"
       
    20 #include "debug.h"
       
    21 
       
    22 #include <bt_subscribe.h>
       
    23 const TInt KDeviceArrayDefaultSize=10;
       
    24 // ---------------------------------------------------------------------
       
    25 // Constructor
       
    26 // ---------------------------------------------------------------------
       
    27 CBTRegistryObserver::CBTRegistryObserver(MBTRegistryObserver* aObserver, 
       
    28     const TBTRegistrySearch aPattern) 
       
    29     	: CActive(CActive::EPriorityHigh), 
       
    30       	  iObserver(aObserver),   iSearchPattern(aPattern)
       
    31     {    
       
    32     TRACE_FUNC_ENTRY
       
    33     CActiveScheduler::Add(this);
       
    34     // The priority will be EPriorityHigh only initially.
       
    35     // after the first successfull retrieval of the devicelist
       
    36     // the priority will be changed to EPriorityLow.
       
    37     
       
    38     // This will make the 1st devicelist drawn as quickly as possible,
       
    39     // but the subsequent refrehes will not decrease the responsiveness
       
    40     // of the Btui.
       
    41     iAnotherEventPending=EFalse;
       
    42     
       
    43     TRACE_FUNC_EXIT
       
    44     }
       
    45 // ---------------------------------------------------------------------
       
    46 // CBTRegistryObserver::NewL
       
    47 // ---------------------------------------------------------------------
       
    48 CBTRegistryObserver* CBTRegistryObserver::NewL(MBTRegistryObserver* aObserver,
       
    49 	  const TBTRegistrySearch aPattern)
       
    50     {
       
    51     CBTRegistryObserver* self = new (ELeave) CBTRegistryObserver(aObserver, aPattern);
       
    52     CleanupStack::PushL(self);
       
    53     self->ConstructL();
       
    54     CleanupStack::Pop(self);
       
    55     return self;
       
    56     }
       
    57 // ---------------------------------------------------------------------
       
    58 // Destructor
       
    59 // ---------------------------------------------------------------------    
       
    60 CBTRegistryObserver::~CBTRegistryObserver()
       
    61     {
       
    62     TRACE_FUNC_ENTRY
       
    63     Cancel();
       
    64     iProperty.Close();
       
    65     delete iDevMan;
       
    66     
       
    67     DeleteAllocatedDevices();
       
    68     
       
    69     delete iRegDeviceArray;
       
    70     iRegDeviceArray=NULL;
       
    71     
       
    72     TRACE_FUNC_EXIT
       
    73     }
       
    74 // ---------------------------------------------------------------------
       
    75 // CBTRegistryObserver::ConstructL()
       
    76 // ---------------------------------------------------------------------
       
    77 void CBTRegistryObserver::ConstructL()
       
    78     {
       
    79     TRACE_FUNC_ENTRY
       
    80 	iRegDeviceArray = new (ELeave) CBTDeviceArray(KDeviceArrayDefaultSize);    
       
    81     iDevMan =  CBTEngDevMan::NewL(this);
       
    82     iDevMan->SetPriority(CActive::EPriorityHigh);
       
    83     
       
    84     iProperty.Attach(KPropertyUidBluetoothCategory, KPropertyKeyBluetoothGetRegistryTableChange);
       
    85     StartIfNotRunning();
       
    86     TRACE_FUNC_EXIT
       
    87     }
       
    88 // ---------------------------------------------------------------------
       
    89 // CBTRegistryObserver::Refresh
       
    90 // This will simulate registry change event.
       
    91 // ---------------------------------------------------------------------    
       
    92 void CBTRegistryObserver::Refresh()
       
    93 	{
       
    94 	TRACE_FUNC_ENTRY
       
    95 	
       
    96 	__ASSERT_DEBUG(iDevMan, User::Invariant());
       
    97 	
       
    98     if (iDevMan->IsActive())
       
    99     	{
       
   100     	// An attempt to start a new search, during a prior
       
   101     	// one. 
       
   102     	iAnotherEventPending=ETrue;
       
   103     	return;
       
   104     	}   
       
   105 	Cancel();
       
   106 
       
   107     DeleteAllocatedDevices();
       
   108     iDevMan->GetDevices(iSearchPattern, iRegDeviceArray);
       
   109     
       
   110 	StartIfNotRunning();    
       
   111     
       
   112     TRACE_FUNC_EXIT    
       
   113 	}
       
   114 	
       
   115 // ---------------------------------------------------------------------
       
   116 // CBTRegistryObserver::StartIfNotRunning
       
   117 // this have to be called again each time there has been registry event
       
   118 // ---------------------------------------------------------------------
       
   119 void CBTRegistryObserver::StartIfNotRunning()
       
   120     {
       
   121     TRACE_FUNC_ENTRY
       
   122     if (!IsActive())
       
   123         {
       
   124         TRACE_INFO(_L("Start()"))
       
   125         iIsStopped = EFalse;
       
   126         iProperty.Subscribe(iStatus);
       
   127         SetActive();
       
   128         }
       
   129     TRACE_FUNC_EXIT        
       
   130     }
       
   131 // ---------------------------------------------------------------------
       
   132 // CBTRegistryObserver::RunL
       
   133 // From CAtive
       
   134 // This is called when registry has changed. This will not
       
   135 // be called when BTRegistry date retrieval is complete, since
       
   136 // HandleGetDevicesComplete is called when RBTregistry data retrieval
       
   137 // is complete
       
   138 // ----------------------------------------------------------------------
       
   139 
       
   140 void CBTRegistryObserver::RunL()
       
   141     {
       
   142     TRACE_FUNC_ENTRY
       
   143     if(iDevMan && iDevMan->IsActive())
       
   144     	{
       
   145     	// An attempt to start a new search, during a prior
       
   146     	// one. We will not start a new search, but we
       
   147     	// will do it after the current search is complete
       
   148     	iAnotherEventPending=ETrue;
       
   149     	StartIfNotRunning();
       
   150 		TRACE_FUNC_EXIT    	
       
   151     	return;
       
   152     	}
       
   153     if(!IsActive())
       
   154     	SetPriority(CActive::EPriorityLow);
       
   155     
       
   156     
       
   157 
       
   158     TInt registryTable;
       
   159     iProperty.Get(registryTable);
       
   160     iAnotherEventPending=EFalse;
       
   161     
       
   162     if(iDevMan && iStatus.Int() == KErrNone && registryTable == KRegistryChangeRemoteTable)
       
   163         {
       
   164         TRACE_INFO(_L("Remote table change"))
       
   165         
       
   166         DeleteAllocatedDevices();
       
   167         iDevMan->GetDevices(iSearchPattern, iRegDeviceArray);
       
   168         }
       
   169     StartIfNotRunning();
       
   170     TRACE_FUNC_EXIT
       
   171     }
       
   172 
       
   173 // ---------------------------------------------------------------------
       
   174 // CBTRegistryObserver::HandleGetDevicesComplete
       
   175 // From MBTEngDevManObserver
       
   176 //
       
   177 // This function will not filter out refreshes with no changes,
       
   178 // since the connect status may have been changed in those cases, and
       
   179 // this class does not know about that
       
   180 //
       
   181 // It is worth to mention that CBTDeviceContainer will not refresh parts
       
   182 // of the screen that are not changed.
       
   183 // ---------------------------------------------------------------------
       
   184 void  CBTRegistryObserver::HandleGetDevicesComplete(TInt aErr, CBTDeviceArray* aDeviceArray)
       
   185     {
       
   186     TRACE_FUNC_ENTRY
       
   187     TRACE_INFO((_L("aErr = %d"), aErr))
       
   188     
       
   189     // the initial view refresh is done. We will decrese
       
   190     // the priority of this component to quarantee UI
       
   191     // responsiveness
       
   192     if(!iDevMan->IsActive())
       
   193     	iDevMan->SetPriority(CActive::EPriorityLow);
       
   194     
       
   195     TRAP_IGNORE(
       
   196 		if( aErr != KErrNotFound)
       
   197 			iObserver->RegistryChangedL(aDeviceArray );
       
   198 		else
       
   199 			{
       
   200 			// handle the empty response case. The DevMan may issue
       
   201 			// NULL in that case 
       
   202 			iObserver->RegistryChangedL(iRegDeviceArray );	
       
   203 			}
       
   204 	);	
       
   205 
       
   206     DeleteAllocatedDevices();
       
   207 
       
   208 	if(iAnotherEventPending)	
       
   209 		{
       
   210 		// Current results are not sent further, since
       
   211 		// there has been changes, witch may have rendered the current
       
   212 		// results allready obsolate. Therefore a refresh is made instead
       
   213     
       
   214 		iAnotherEventPending=EFalse;
       
   215         iDevMan->GetDevices(iSearchPattern, iRegDeviceArray);
       
   216 		}
       
   217     TRACE_FUNC_EXIT		
       
   218     }
       
   219 
       
   220 // ---------------------------------------------------------------------
       
   221 // CBTRegistryObserver::DeleteAllocatedDevices
       
   222 // ---------------------------------------------------------------------
       
   223 void CBTRegistryObserver::DeleteAllocatedDevices()
       
   224 	{
       
   225 	TRACE_FUNC_ENTRY
       
   226 	if(iRegDeviceArray)
       
   227 		{
       
   228         iRegDeviceArray->ResetAndDestroy();
       
   229         }
       
   230     TRACE_FUNC_EXIT		
       
   231 	}
       
   232 // ---------------------------------------------------------------------
       
   233 // CBTRegistryObserver::Cancel
       
   234 // From CActive
       
   235 // ---------------------------------------------------------------------
       
   236     
       
   237 void CBTRegistryObserver::Cancel()
       
   238     {
       
   239     TRACE_FUNC_ENTRY
       
   240     iIsStopped = ETrue;
       
   241     iAnotherEventPending=EFalse;    
       
   242     CActive::Cancel();
       
   243     if (iDevMan) iDevMan->Cancel();
       
   244     DeleteAllocatedDevices();
       
   245     TRACE_FUNC_EXIT    
       
   246     }
       
   247 // ---------------------------------------------------------------------
       
   248 // CBTRegistryObserver::DoCancel
       
   249 // From CActive
       
   250 // ---------------------------------------------------------------------
       
   251 void CBTRegistryObserver::DoCancel()
       
   252 	{
       
   253 	TRACE_FUNC_ENTRY
       
   254     iProperty.Cancel();	
       
   255     TRACE_FUNC_EXIT
       
   256 	}