securitydialogs/lockclient/src/lockaccessextension.cpp
branchRCL_3
changeset 21 09b1ac925e3f
parent 0 164170e6151a
child 22 03674e5abf46
--- a/securitydialogs/lockclient/src/lockaccessextension.cpp	Thu Aug 19 10:45:23 2010 +0300
+++ b/securitydialogs/lockclient/src/lockaccessextension.cpp	Tue Aug 31 16:04:40 2010 +0300
@@ -15,17 +15,20 @@
  *
 */
 
-
 #include "lockaccessextension.h"
 #include <lockappclientserver.h>
 #include <e32property.h> // P&S API
 #include <apgtask.h> // TApaTask, TApaTaskList
 #include <coemain.h> // CCoeEnv
-
+#include "../../Autolock/PubSub/securityuisprivatepskeys.h"
+#include <apgcli.h>
+#include <apacmdln.h>
 
-// Constants
-const TInt KTimesToConnectServer( 2);
-const TInt KTimeoutBeforeRetrying( 50000);
+#include <xqservicerequest.h>
+#include <xqserviceutil.h>
+#include <xqrequestinfo.h>
+#include <xqaiwrequest.h>
+#include <xqappmgr.h>
 
 // ---------------------------------------------------------------------------
 // Gets server version, needed for connection
@@ -41,6 +44,8 @@
 TInt RLockAccessExtension::TryConnect( RWsSession& aWsSession )
 	{
 	TInt ret(KErrNone);
+	/*
+	this is the old methd. Now we use QtHighway
 	TApaTaskList list(aWsSession);
 	// check that lockapp is running
 	TApaTask task = list.FindApp( KLockAppUid );
@@ -62,6 +67,8 @@
 		RDebug::Printf( "%s %s (%u) ???? LockApp task not found=%x", __FILE__, __PRETTY_FUNCTION__, __LINE__, KLockAppUid );
 		ret = KErrNotReady;
 		}
+	*/
+	RDEBUG("ret", ret);
 	return ret;
 	}
 
@@ -71,7 +78,70 @@
 TInt RLockAccessExtension::EnsureConnected( )
 	{
 	TInt ret(KErrNone);
+
+	// Now we use QtHighway, but nevertheless need to be sure that only 1 process is running
+	// This is done because Autolock.exe should start at the beginning, but it might not be ready yet.
+	// As Qthighway will start it, it's better to give time for the first one to prepare itself.
+	TInt err = KErrNone;
+	TInt numAttempts = 0;
+	TInt numberOfInstances = 0;
+	do
+		{
+		numberOfInstances=0;
+		TFullName processName;
+		TFindThread find(_L("*utolock*"));	// first letter can can be uppercase or lowercase
+		while( find.Next( processName ) == KErrNone )
+		    {
+				// Autolock[100059b5]0002::Autolock		in device
+				// autolock.exe[100059b5]0002::Main		in emulator
+				RDEBUG("found process", 1);
+        numberOfInstances++;
+		    }	// end while
+		RDEBUG("numberOfInstances", numberOfInstances);
+		if(numberOfInstances<=0)
+			{
+			RDEBUG("Autolock.exe not running already. Starting.", 0 );
+	    RApaLsSession ls;
+	    User::LeaveIfError(ls.Connect());
+	    CleanupClosePushL(ls);
+	    RDEBUG("commandLine", 0); 
+	    CApaCommandLine* commandLine = CApaCommandLine::NewLC();
+	    commandLine->SetExecutableNameL(_L("autolock.exe"));
+	    commandLine->SetCommandL(EApaCommandRun);
+	    // Try to launch the application.
+	    RDEBUG("StartApp", 0); 
+	    TInt err = ls.StartApp(*commandLine); // this migh fail
+    	CleanupStack::PopAndDestroy(2); // commandLine, ls
+
+			RDEBUG("Autolock.exe launched. Waiting a bit. err", err );
+			User::After(1000*1000);
+			RDEBUG("re-verifying Autolock.exe process.", 1 );
+			}
+		} while (numAttempts++ <3 && numberOfInstances<=0);
+		
+
+
+	TInt value = -1;
+	err = KErrNone;
+	numAttempts = 0;
+	while( value<1 && numAttempts++ <10 )	// wait max 5 seconds
+		{
+		// process was started, but still not fully running. Give a bit more time
+		err = RProperty::Get(KPSUidSecurityUIs, KSecurityUIsLockInitiatorUID, value);
+		RDEBUG("err", err);
+		RDEBUG("value", value);
+		if(value<1)
+			{
+			RDEBUG("Autolock.exe has started but it's not fully running", value);
+			User::After(5*100*1000);	// half a second
+			}
+		}
+	RDEBUG("numAttempts", numAttempts);
+	/*
+	this is the old method. 
 	// we need CCoeEnv because of window group list
+	const TInt KTimesToConnectServer( 2);
+	const TInt KTimeoutBeforeRetrying( 50000);
 	CCoeEnv* coeEnv = CCoeEnv::Static( );
 	if ( coeEnv )
 		{
@@ -90,6 +160,8 @@
 		// No CCoeEnv
 		ret = KErrNotSupported;
 		}
+	*/
+	RDEBUG("ret", ret);
 	return ret;
 	}
 
@@ -98,10 +170,12 @@
 // ---------------------------------------------------------------------------
 TInt RLockAccessExtension::SendMessage( TInt aMessage )
 	{
-	TInt ret = EnsureConnected( );
+	RDEBUG("0", 0);
+	TInt ret = KErrNone;
 	if ( ret == KErrNone )
 		{
-		ret = SendReceive( aMessage );
+		// ret = SendReceive( aMessage );
+		ret = SendMessage( aMessage, -1, -1 );
 		}
 	return ret;
 	}
@@ -111,12 +185,14 @@
 // ---------------------------------------------------------------------------
 TInt RLockAccessExtension::SendMessage( TInt aMessage, TInt aParam1 )
 	{
-	TInt ret = EnsureConnected( );
+	RDEBUG("0", 0);
+	TInt ret = KErrNone;
 	if ( ret == KErrNone )
 		{
 		// assign parameters to IPC argument
 		TIpcArgs args(aParam1);
-		ret = SendReceive( aMessage, args );
+		// ret = SendReceive( aMessage, args );
+		ret = SendMessage( aMessage, aParam1, -1 );
 		}
 	return ret;
 	}
@@ -126,13 +202,82 @@
 // ---------------------------------------------------------------------------
 TInt RLockAccessExtension::SendMessage( TInt aMessage, TInt aParam1, TInt aParam2 )
 	{
+	RDEBUG("0", 0);
 	TInt ret = EnsureConnected( );
+	RDEBUG("ret", ret);
 	if ( ret == KErrNone )
 		{
 		// assign parameters to IPC argument
-		TIpcArgs args( aParam1, aParam2);
-		ret = SendReceive( aMessage, args );
+		// TIpcArgs args( aParam1, aParam2);
+		// this is the old methd. Now we use QtHighway
+		// ret = SendReceive( aMessage, args );
+    RDEBUG("aMessage", aMessage);
+    RDEBUG("aParam1", aParam1);
+    RDEBUG("aParam2", aParam2);
+
+		
+			{	// old method. Not used any more. Kept as reference
+			/*
+	    XQServiceRequest* mServiceRequest;
+      RDEBUG("XQServiceRequest", 0);
+	    mServiceRequest = new XQServiceRequest("com.nokia.services.Autolock.Autolock","service(QString,QString,QString)");// use   , false    to make async
+      RDEBUG("aMessage", 0);
+	    QString label;
+	    label = "" + QString("%1").arg(aMessage);
+	    *mServiceRequest << QString(label);
+      RDEBUG("aParam1", 0);
+	    label = "" + QString("%1").arg(aParam1);
+	    *mServiceRequest << QString(label);
+      RDEBUG("aParam2", 0);
+	    label = "" + QString("%1").arg(aParam2);
+	    *mServiceRequest << QString(label);
+	    int returnvalue;
+      RDEBUG("send", 0);
+	    bool ret = mServiceRequest->send(returnvalue);
+      RDEBUG("ret", ret);
+      RDEBUG("returnvalue", returnvalue);
+      */
+			}
+
+			RDEBUG("args", 0);
+			QList<QVariant> args;
+	    args << QVariant(QString(QString::number(aMessage)));
+	    args << QVariant(QString(QString::number(aParam1)));
+	    args << QVariant(QString(QString::number(aParam2)));
+
+	    XQApplicationManager mAppManager;
+	    XQAiwRequest *request;
+	    RDEBUG("create", 0);
+			request = mAppManager.create("com.nokia.services.Autolock", "Autolock", "service(QString,QString,QString)", false);
+			// also works with		create("Autolock", "service(QString,QString,QString)", false);
+			if(request)
+				{
+	    	RDEBUG("got request", 0);
+	    	}
+	    else
+	    	{
+	 	    RDEBUG("not got request", 0);
+	 	    RDebug::Printf( "%s %s (%u) not got request=%x", __FILE__, __PRETTY_FUNCTION__, __LINE__, 0 );
+	 	    return KErrAbort;
+	 	  	}
+				
+			RDEBUG("setArguments", 0);
+			request->setArguments(args);
+			RDEBUG("args", 0);
+	    int returnvalue=0;
+	    QVariant var = QVariant(returnvalue);
+			RDEBUG("send", 0);
+			bool retSend = request->send(var);
+			returnvalue = var.toInt();
+			RDEBUG("retSend", retSend);
+			RDEBUG("returnvalue", returnvalue);
+	    int error = request->lastError();
+			RDEBUG("error", error);
+	    ret = returnvalue;
+	
+	    delete request;
 		}
+  RDEBUG("ret", ret);
 	return ret;
 	}