contextengine/plugins/locationplugin/src/locationcontextplugin.cpp
changeset 0 c53acadfccc6
child 1 acef663c1218
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2007-2009 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:  A Context Engine Plug-in to get location context information.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32std.h>
       
    20 #include <uri16.h>
       
    21 #include <e32math.h>
       
    22 #include <LbsPosition.h> 
       
    23 #include <etel3rdparty.h>
       
    24 
       
    25 #include "locationcontextplugin.h"
       
    26 #include "mdenamespacedef.h"
       
    27 #include "mdeobject.h"
       
    28 #include "mdeharvestersession.h"
       
    29 #include "mdeproperty.h"
       
    30 #include "harvesterlog.h"
       
    31 #include "mdsutils.h"
       
    32 #include "mdeconstants.h"
       
    33 #include "mdeobjectdef.h"
       
    34 #include "harvesterdata.h"
       
    35 
       
    36 using namespace MdeConstants;
       
    37 
       
    38 // ======== MEMBER FUNCTIONS ========
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // Default constructor for first phase construction.
       
    42 // Don't export these - used only by functions in this DLL, eg our NewL().
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 CLocationContextPlugin::CLocationContextPlugin() :
       
    46     iObserver( NULL ), iMdeSession( NULL ), iLocationTrailConnected( EFalse )
       
    47     {
       
    48     }
       
    49 
       
    50 
       
    51 // ---------------------------------------------------------------------------
       
    52 // Second phase construction
       
    53 // ---------------------------------------------------------------------------
       
    54 //
       
    55 void CLocationContextPlugin::ConstructL() // second-phase constructor
       
    56     {
       
    57     // No implementation required
       
    58     }
       
    59 
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // The usual NewL()
       
    63 // ---------------------------------------------------------------------------
       
    64 //
       
    65 CLocationContextPlugin* CLocationContextPlugin::NewL()
       
    66     {
       
    67     CLocationContextPlugin* self = new (ELeave) CLocationContextPlugin();
       
    68     CleanupStack::PushL( self );
       
    69     self->ConstructL();
       
    70     CleanupStack::Pop( self );
       
    71     return self;
       
    72     }
       
    73 
       
    74     
       
    75 // ---------------------------------------------------------------------------
       
    76 // Destructor
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 CLocationContextPlugin::~CLocationContextPlugin()
       
    80     {
       
    81     iLocationTrail.Close();
       
    82     iManipulator.Close();
       
    83     }
       
    84 
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // From CContextPlugin.
       
    88 // Initialization of Location Context Plugin.
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 void CLocationContextPlugin::Init( MContextPluginObserver& aObserver )
       
    92     {
       
    93     WRITELOG("CLocationContextPlugin::Init()");
       
    94     
       
    95     TInt err = iLocationTrail.Connect();
       
    96     if ( err == KErrNone )
       
    97     	{
       
    98     	err = iManipulator.Connect();
       
    99     	if (err == KErrNone)
       
   100     		{
       
   101     		iLocationTrailConnected = ETrue;
       
   102     		}
       
   103     	}
       
   104     else
       
   105     	{
       
   106     	iLocationTrailConnected = EFalse;
       
   107     	}
       
   108     aObserver.PluginInitializationStatus( KErrNone ); // notify the context framework
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // Set a pointer to MdESession.
       
   113 // Session is used to store harvested context data.
       
   114 // ---------------------------------------------------------------------------
       
   115 //
       
   116 void CLocationContextPlugin::SetMdeSession( CMdESession& aSession )
       
   117     {
       
   118     iMdeSession = &aSession;
       
   119     }
       
   120 
       
   121 // ---------------------------------------------------------------------------
       
   122 // From CContextPlugin.
       
   123 // This method takes a location context snapshot for one metadata object
       
   124 // and writes the information to the object received as parameter.
       
   125 // The observer will be informed about the status of the operation.
       
   126 // ---------------------------------------------------------------------------
       
   127 //
       
   128 void CLocationContextPlugin::ContextSnapshot( MContextPluginObserver& aObserver,
       
   129 	CHarvesterData& aHD )
       
   130     {
       
   131     WRITELOG( "CLocationContextPlugin::ContextSnapshot()" );
       
   132     
       
   133     if( !aHD.AddLocation() )
       
   134     	{
       
   135         WRITELOG( "CLocationContextPlugin::ContextSnapshot() - aHd.AddLocation returned false" );
       
   136     	aHD.SetErrorCode( KErrNone );
       
   137         aObserver.PluginSnapshotStatus( &aHD );
       
   138     	return;
       
   139     	}
       
   140     
       
   141     if ( !iLocationTrailConnected ) // no trail, no snapshot
       
   142     	{
       
   143     	aHD.SetErrorCode( KErrDisconnected );
       
   144         aObserver.PluginSnapshotStatus( &aHD );
       
   145     	return;
       
   146     	}
       
   147     
       
   148     TInt ret = KErrNone;
       
   149     
       
   150     ret = iManipulator.LocationSnapshot( aHD.MdeObject().Id() );
       
   151     
       
   152     aHD.SetErrorCode( ret );
       
   153     aObserver.PluginSnapshotStatus( &aHD );
       
   154     }
       
   155 
       
   156 // ---------------------------------------------------------------------------
       
   157 // From CContextPlugin.
       
   158 // This method takes a location context snapshot for several metadata objects
       
   159 // and writes the information to the all the objects received as parameter.
       
   160 // The observer will be informed about the status of the operation.
       
   161 // ---------------------------------------------------------------------------
       
   162 //
       
   163 void CLocationContextPlugin::ContextSnapshot( MContextPluginObserver& aObserver,
       
   164     RPointerArray<CHarvesterData>& aHDArray )
       
   165     {
       
   166 	const TInt count( aHDArray.Count() );
       
   167     WRITELOG( "CLocationContextPlugin::ContextSnapshot()" );
       
   168 
       
   169     if ( !iLocationTrailConnected ) // no trail, no snapshot
       
   170     	{
       
   171     	for(TInt i = 0; i < count; i++)
       
   172     		{
       
   173     		CHarvesterData* hd = aHDArray[i];
       
   174     		hd->SetErrorCode( KErrDisconnected );
       
   175     		aObserver.PluginSnapshotStatus( hd );
       
   176     		}
       
   177     	return;
       
   178     	}
       
   179 
       
   180     TLocationData data;
       
   181     RLocationTrail::TTrailState state;
       
   182     CMdEProperty* timeProp = NULL;
       
   183     TInt ret = KErrNone;
       
   184     iLocationTrail.GetLocationTrailState( state );
       
   185 
       
   186     if ( state != RLocationTrail::ETrailStopped && state != RLocationTrail::ETrailStopping )
       
   187     	{
       
   188     	for (TInt i = 0; i < aHDArray.Count(); ++i)
       
   189     		{
       
   190     		CMdEObject& mdeObject = ((CHarvesterData*)aHDArray[i])->MdeObject();
       
   191     		TRAP( ret, GetMdeObjectCreationTimeL( mdeObject, timeProp ) );
       
   192     		if ( ret != KErrNone )
       
   193     			{
       
   194     			continue;
       
   195     			}
       
   196 #ifdef _DEBUG
       
   197     		TRAP_IGNORE(
       
   198     		WRITELOG1( "CLocationContextPlugin::ContextSnapshot() - timeProp: %Ld", 
       
   199     				timeProp->TimeValueL().Int64() ) );
       
   200     		WRITELOG1( "CLocationContextPlugin::ContextSnapshot() - Location trail state: %d", 
       
   201     				state );
       
   202 #endif
       
   203     				
       
   204     		TRAP_IGNORE(ret = iLocationTrail.RetrieveLocation( timeProp->TimeValueL(), data, state ));
       
   205 
       
   206 #ifdef _DEBUG
       
   207     		WRITELOG1( "CLocationContextPlugin::ContextSnapshot() - Retrieve location returned: %d", ret );
       
   208     		WRITELOG1( "CLocationContextPlugin::ContextSnapshot() - Latitude was: %f", data.iPosition.Latitude() );
       
   209     		WRITELOG1( "CLocationContextPlugin::ContextSnapshot() - Longitude was: %f", data.iPosition.Longitude() );
       
   210     		WRITELOG1( "CLocationContextPlugin::ContextSnapshot() - Altitude was: %f", data.iPosition.Altitude() );
       
   211 #endif
       
   212         
       
   213     		if (ret == KErrNone)
       
   214     			{
       
   215     			if ( !Math::IsNaN(data.iPosition.Latitude() ) ||
       
   216    	        		 !Math::IsNaN(data.iPosition.Longitude() ) || 
       
   217    	        		 !Math::IsNaN(data.iPosition.Altitude() ) ||
       
   218    	        		 data.iNetworkInfo.iCellId > 0 || data.iNetworkInfo.iLocationAreaCode > 0 ||
       
   219    	        		 data.iNetworkInfo.iCountryCode.Length() > 0 ||
       
   220    	        		 data.iCountry.Length() > 0 || data.iNetworkInfo.iNetworkId.Length() > 0 )
       
   221    	        		{
       
   222 #ifdef _DEBUG
       
   223 	    			WRITELOG1( "CLocationContextPlugin::ContextSnapshot() Create location object for id: %d", mdeObject.Id());
       
   224 #endif
       
   225 	    			ret = iManipulator.CreateLocationObject(data, mdeObject.Id());
       
   226 	    			WRITELOG1( "CLocationContextPlugin::ContextSnapshot() Create location object returned: %d", ret);
       
   227    	        		}
       
   228     			}
       
   229 	        else
       
   230 	        	{
       
   231 	        	ret = KErrNone;
       
   232 	        	}
       
   233 
       
   234     		}
       
   235         }
       
   236 
       
   237     // Notify to context framework
       
   238     for(TInt i = 0; i < count; i++)
       
   239         {
       
   240         CHarvesterData* hd = aHDArray[i];
       
   241         hd->SetErrorCode( KErrNone );
       
   242         aObserver.PluginSnapshotStatus( hd );
       
   243         }
       
   244     }
       
   245 
       
   246 // ---------------------------------------------------------------------------
       
   247 // Get mde object creation date/time.
       
   248 // ---------------------------------------------------------------------------
       
   249 //
       
   250 void CLocationContextPlugin::GetMdeObjectCreationTimeL( CMdEObject& aObject, 
       
   251 	CMdEProperty*& aProperty )
       
   252     {
       
   253     CMdENamespaceDef& namespaceDef = iMdeSession->GetDefaultNamespaceDefL();
       
   254 
       
   255     CMdEObjectDef& objectDef = namespaceDef.GetObjectDefL( Object::KBaseObject );
       
   256     CMdEPropertyDef& creationDef = objectDef.GetPropertyDefL( Object::KLastModifiedDateProperty );
       
   257 
       
   258     CMdEProperty* property = NULL;
       
   259     aObject.Property( creationDef, property, 0 );
       
   260     if ( !property )
       
   261         {
       
   262         User::Leave( KErrNotFound );
       
   263         }
       
   264     aProperty = property;
       
   265     }