basiclocationinfodisplay/blid/engine/src/CBlidEng.cpp
branchRCL_3
changeset 17 1fc85118c3ae
parent 16 8173571d354e
child 18 870918037e16
equal deleted inserted replaced
16:8173571d354e 17:1fc85118c3ae
     1 /*
       
     2 * Copyright (c) 2005 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: 
       
    15 *     Implements interface for Blid event
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <sysutil.h>
       
    22 #include <f32file.h>
       
    23 #include <EPos_CPosLandmark.h>
       
    24 #include <driveinfo.h>
       
    25 
       
    26 #include "CBlidEng.h"
       
    27 #include "BlidModelFactory.h"
       
    28 #include "DllMain.h" // panic codes
       
    29 #include "MBlidSettings.h" // MBlidSettings
       
    30 #include "MBlidLocation.h" //MBlidLocation
       
    31 #include "MBlidRouter.h" //MBlidRouter
       
    32 #include "lbsposition.h"
       
    33 #include "TNamedCoordinate.h"
       
    34 #include "blidengconsts.h"
       
    35 
       
    36 // ============================ MEMBER FUNCTIONS ==============================
       
    37 // ----------------------------------------------------------------------------
       
    38 // CBlidEng::NewL
       
    39 // Two-phased constructor.
       
    40 // ----------------------------------------------------------------------------
       
    41 //
       
    42 EXPORT_C CBlidEng* CBlidEng::NewL()
       
    43     {
       
    44     CBlidEng* self = new( ELeave ) CBlidEng;
       
    45     CleanupStack::PushL( self );
       
    46     self->ConstructL();
       
    47     CleanupStack::Pop(self); // self
       
    48     return self;
       
    49     }
       
    50 
       
    51 // ---------------------------------------------------------
       
    52 // CBlidEng::ConstructL
       
    53 // ---------------------------------------------------------
       
    54 //
       
    55 void CBlidEng::ConstructL()
       
    56     {
       
    57     User::LeaveIfError(iServer.Connect());
       
    58     CreateModelL( EBlidSettings );
       
    59     CreateModelL( EBlidRouter );
       
    60     iAppMode = EAppForeground;
       
    61     iHWRMLight = CHWRMLight::NewL();    
       
    62     }
       
    63 
       
    64 // ----------------------------------------------------------------------------
       
    65 // CBlidEng::CBlidEng
       
    66 // C++ default constructor can NOT contain any code, that
       
    67 // might leave.
       
    68 // ----------------------------------------------------------------------------
       
    69 //
       
    70 CBlidEng::CBlidEng()
       
    71     {
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------
       
    75 // CBlidEng::~CBlidEng
       
    76 // ---------------------------------------------------------
       
    77 //
       
    78 CBlidEng::~CBlidEng()
       
    79     {
       
    80     delete iSettings;
       
    81     delete iLocation;
       
    82     delete iRouter;
       
    83     delete iHWRMLight;
       
    84     iServer.Close();    
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------
       
    88 // CBlidEng::StoreL
       
    89 // ---------------------------------------------------------
       
    90 //
       
    91 EXPORT_C TStreamId CBlidEng::StoreL( CStreamStore& aStore,
       
    92                                      CStreamStore& aRestore )
       
    93     {
       
    94     RFs fs;
       
    95     User::LeaveIfError(fs.Connect()); 
       
    96     CleanupClosePushL( fs ); 
       
    97 
       
    98     if ( SysUtil::DiskSpaceBelowCriticalLevelL( &fs, 0, DriveInfo::EDefaultPhoneMemory ) )
       
    99         {
       
   100         const_cast<CBlidEng*>(this)->RestoreL( aRestore, iStreamId );
       
   101         User::Leave( KErrDiskFull );
       
   102 		}
       
   103 	CleanupStack::PopAndDestroy(); // fs
       
   104     
       
   105     RStoreWriteStream stream;
       
   106     iStreamId = stream.CreateLC( aStore );
       
   107     ExternalizeL( stream );
       
   108     stream.CommitL();
       
   109     CleanupStack::PopAndDestroy(); //stream
       
   110     return iStreamId;
       
   111     }
       
   112 
       
   113 // ---------------------------------------------------------
       
   114 // CBlidEng::RestoreL
       
   115 // ---------------------------------------------------------
       
   116 //
       
   117 EXPORT_C void CBlidEng::RestoreL( const CStreamStore& aStore,
       
   118                 TStreamId aStreamId )
       
   119     {
       
   120     iStreamId = aStreamId;
       
   121     RStoreReadStream stream;
       
   122     stream.OpenLC(aStore,iStreamId);
       
   123     InternalizeL(stream);
       
   124     CleanupStack::PopAndDestroy(); //stream
       
   125     }
       
   126 
       
   127 // ----------------------------------------------------------------------------
       
   128 // CBlidEng::SettingsModel()
       
   129 // Settings model getter.
       
   130 // ----------------------------------------------------------------------------
       
   131 //
       
   132 EXPORT_C MBlidSettings* CBlidEng::SettingsModel() const
       
   133     {
       
   134     return iSettings;
       
   135     }
       
   136 
       
   137 // ----------------------------------------------------------------------------
       
   138 // CBlidEng::LocationModel()
       
   139 // Location model getter.
       
   140 // ----------------------------------------------------------------------------
       
   141 //
       
   142 EXPORT_C MBlidLocation* CBlidEng::LocationModel() const
       
   143     {
       
   144     return iLocation;
       
   145     }
       
   146 
       
   147 // ----------------------------------------------------------------------------
       
   148 // CBlidEng::RouterModel()
       
   149 // Router model getter.
       
   150 // ----------------------------------------------------------------------------
       
   151 //
       
   152 EXPORT_C MBlidRouter* CBlidEng::RouterModel() const
       
   153     {
       
   154     return iRouter;
       
   155     }
       
   156 
       
   157 // ----------------------------------------------------------------------------
       
   158 // CBlidEng::CreateModelL()
       
   159 // Creates a given model type.
       
   160 // ----------------------------------------------------------------------------
       
   161 //
       
   162 EXPORT_C void CBlidEng::CreateModelL( TBlidModelType aModel )
       
   163     {
       
   164     switch( aModel )
       
   165         {
       
   166         case EBlidSettings:
       
   167             {
       
   168             if ( !iSettings )
       
   169                 {
       
   170                 iSettings = BlidModelFactory::SettingsL();
       
   171                 }
       
   172             break;
       
   173             }
       
   174         case EBlidLocation:
       
   175             {
       
   176             if ( !iLocation )
       
   177                 {
       
   178                 iLocation = BlidModelFactory::LocationL(iServer, *this);                
       
   179                 }
       
   180             break;
       
   181             }
       
   182         case EBlidRouter:
       
   183             {
       
   184             if ( !iRouter )
       
   185             	{
       
   186                 iRouter = BlidModelFactory::RouterL();
       
   187                 }
       
   188             break;
       
   189             }
       
   190         default:
       
   191             {
       
   192             // Not supported model type
       
   193             __ASSERT_DEBUG(EFalse, BlidEngPanic(ELCEModelTypeNotSupported));
       
   194             break;
       
   195             }            
       
   196         }
       
   197     }
       
   198     
       
   199 // ---------------------------------------------------------
       
   200 // CBlidEng::GetDistanceToDestination
       
   201 // ---------------------------------------------------------
       
   202 //
       
   203 EXPORT_C TInt CBlidEng::GetDistanceToDestination(TReal32& aDistance)
       
   204     {
       
   205     if(iRouter->IsAnyPointSet())
       
   206         {
       
   207         TInt retVal;
       
   208         TPosition& currentPosition = iLocation->GetCurrentPosition();
       
   209         if(iRouter->IsWaypointSet())
       
   210             {
       
   211             TNamedCoordinate* destination = iRouter->Waypoint();
       
   212             retVal = destination->Distance(currentPosition, aDistance);
       
   213             }
       
   214         else
       
   215             {
       
   216             CPosLandmark* landmark = iRouter->Landmark();
       
   217             TPosition destination;
       
   218             landmark->GetPosition(destination);
       
   219             retVal = currentPosition.Distance(destination, aDistance);            
       
   220             }
       
   221         
       
   222         return retVal;
       
   223         }
       
   224     else
       
   225         {
       
   226         return KErrNotFound;
       
   227         }        
       
   228     }
       
   229 
       
   230 // ---------------------------------------------------------
       
   231 // CBlidEng::HasArrivedToDestination
       
   232 // ---------------------------------------------------------
       
   233 //
       
   234 EXPORT_C TBool CBlidEng::HasArrivedToDestination()
       
   235 	{
       
   236 	if(iRouter->IsAnyPointSet())
       
   237 		{
       
   238 		TPosition& currentPosition = iLocation->GetCurrentPosition();
       
   239 		TReal32 distanceToDestination(0);
       
   240 		
       
   241 		if(iRouter->IsWaypointSet())
       
   242 	        {
       
   243 	        TNamedCoordinate* destination = iRouter->Waypoint();
       
   244 	        if(destination->Distance(currentPosition, distanceToDestination) != KErrNone)
       
   245 	        	{
       
   246 	        	return EFalse;
       
   247 	        	}
       
   248 	        }
       
   249 	    else
       
   250 	        {
       
   251 	        TPosition destination;
       
   252 	        CPosLandmark* landmark = iRouter->Landmark();	        
       
   253 	        landmark->GetPosition(destination);
       
   254 	        if(currentPosition.Distance(destination, distanceToDestination) != KErrNone)
       
   255 	        	{
       
   256 	        	return EFalse;
       
   257 	        	}
       
   258 	        }	    
       
   259 	    
       
   260 	    if(distanceToDestination <= KMinArrivalDist)
       
   261 	    	{
       
   262 	    	iSettings->SetArrivedDistance( KMinArrivalDist );
       
   263 	    	return ETrue;
       
   264 	    	}
       
   265 	    
       
   266 	    TReal tempHorizontalAccuracy = currentPosition.HorizontalAccuracy();
       
   267 	    
       
   268 	    if(tempHorizontalAccuracy > KMaxHorAccLimit)
       
   269 	    	{
       
   270 	    	tempHorizontalAccuracy = KMaxHorAccLimit;
       
   271 	    	}
       
   272 	    
       
   273 	    if(distanceToDestination <= tempHorizontalAccuracy)
       
   274 	    	{
       
   275 	    	iSettings->SetArrivedDistance( tempHorizontalAccuracy );
       
   276 	    	return ETrue;
       
   277 	    	}
       
   278 		}	
       
   279 	return EFalse;		
       
   280 	}
       
   281 // ----------------------------------------------------------------------------
       
   282 // CBlidEng::SetAppMode
       
   283 // ----------------------------------------------------------------------------
       
   284 //
       
   285 EXPORT_C void CBlidEng::SetAppMode(TAppMode aMode)
       
   286     {
       
   287     iAppMode = aMode;
       
   288     }
       
   289 
       
   290 // ----------------------------------------------------------------------------
       
   291 // CBlidEng::SetBackLightStateL
       
   292 // It's allocate the Backlight resource for the client and deallocate also depends
       
   293 // Upon the choise of backlight option from the settings view
       
   294 // ----------------------------------------------------------------------------
       
   295 //
       
   296 EXPORT_C void CBlidEng::SetBackLightStateL(TBacklightState aState)
       
   297 	{
       
   298 	if(aState == EBlidBacklightAlwaysOn)
       
   299 		{
       
   300 		TRAPD( error, iHWRMLight->ReserveLightL(CHWRMLight::EPrimaryDisplay, EFalse, EFalse); 
       
   301 		              iHWRMLight->LightOnL(CHWRMLight::EPrimaryDisplay); );
       
   302         if( error != KErrNone )
       
   303             {
       
   304             // do something if necessary.
       
   305             }
       
   306 		}
       
   307 	else
       
   308 		{
       
   309 		iHWRMLight->ReleaseLight(CHWRMLight::EPrimaryDisplay);
       
   310 		}		
       
   311 	}	
       
   312 
       
   313 // ---------------------------------------------------------
       
   314 // CBlidEng::ExternalizeL
       
   315 // ---------------------------------------------------------
       
   316 //
       
   317 void CBlidEng::ExternalizeL( RWriteStream& aStream ) const
       
   318     {
       
   319     aStream.WriteInt32L( KBlidDocVersion1 );
       
   320 	iRouter->ExternalizeL( aStream );
       
   321     }
       
   322 
       
   323 // ---------------------------------------------------------
       
   324 // CBlidEng::InternalizeL
       
   325 // ---------------------------------------------------------
       
   326 //
       
   327 void CBlidEng::InternalizeL( RReadStream& aStream )
       
   328     {
       
   329     switch( aStream.ReadInt32L() )
       
   330         {
       
   331         case KBlidDocVersion1:        
       
   332             {
       
   333             InternalizeV1L( aStream );
       
   334             break;
       
   335             }
       
   336         default:
       
   337             {
       
   338             User::Leave( KErrNotSupported );
       
   339             break;
       
   340             }
       
   341         }
       
   342     }
       
   343 
       
   344 // ---------------------------------------------------------
       
   345 // CBlidEng::InternalizeLV1
       
   346 // ---------------------------------------------------------
       
   347 //
       
   348 void CBlidEng::InternalizeV1L( RReadStream& aStream )
       
   349     {    
       
   350     if ( iRouter->Count() )
       
   351         {
       
   352         iRouter->ResetAndDestroy();
       
   353         }
       
   354     iRouter->InternalizeV1L( aStream );
       
   355     }
       
   356 
       
   357 // ----------------------------------------------------------------------------
       
   358 // CBlidEng::AppMode
       
   359 // ----------------------------------------------------------------------------
       
   360 //
       
   361 EXPORT_C CBlidEng::TAppMode CBlidEng::AppMode()
       
   362     {
       
   363     return iAppMode;
       
   364     }
       
   365 // End of File