securitydialogs/AutolockSrv/src/AutolockSrv.cpp
branchGCC_SURGE
changeset 40 604cd42065d1
parent 29 b63e8c2d8cff
parent 38 e0432375ea67
equal deleted inserted replaced
29:b63e8c2d8cff 40:604cd42065d1
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     8 * 
       
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not, 
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:
       
    19 *
       
    20 */
       
    21 
       
    22 // #include "xqservicelog.h"
       
    23 
       
    24 #include <QApplication>
       
    25 #include <QKeyEvent>
       
    26 #include <QEvent>
       
    27 #include <QLabel>
       
    28 #include <QVBoxLayout>
       
    29 #include <QStackedWidget>
       
    30 #include <QImageReader>
       
    31 #include <QDebug>
       
    32 #include <QTimer>
       
    33 #include <QPushButton>
       
    34 #include <QLineEdit>
       
    35 #include <QListView>
       
    36 #include <QMessageBox>
       
    37 #include <QToolButton>
       
    38 #include <QSymbianEvent>
       
    39 #include <QMainWindow>
       
    40 #include <qvaluespacesubscriber.h>
       
    41 #include <qvaluespacepublisher.h>
       
    42 
       
    43 QTM_USE_NAMESPACE
       
    44 
       
    45 #include <HbIndicator>
       
    46 #include <hbdevicemessagebox.h>
       
    47 
       
    48 #include <lockappclientserver.h>
       
    49 
       
    50 #include <xqsharablefile.h>
       
    51 
       
    52 #include <QGraphicsLinearLayout>
       
    53 
       
    54 #include <hblabel.h>
       
    55 
       
    56 #include "AutolockSrv.h"
       
    57 #include <xqserviceutil.h>
       
    58 
       
    59 #include "autolockuseractivityservice.h"
       
    60 
       
    61 #include <settingsinternalcrkeys.h>		// CenRep keys
       
    62 #include <W32STD.H>
       
    63 #include <eikenv.h>
       
    64 
       
    65 #include <qapplication.h>
       
    66 
       
    67 #include <secuisecuritysettings.h>
       
    68 #include <secui.h>
       
    69 #include <secuisecurityhandler.h>
       
    70 #include <etelmm.h>
       
    71 #include <rmmcustomapi.h>
       
    72 
       
    73 #include <qvaluespacesubscriber.h>
       
    74 
       
    75 #include <hwrmlightdomaincrkeys.h>
       
    76 #include <ProfileEngineSDKCRKeys.h>
       
    77 #include <e32property.h>
       
    78 #include <coreapplicationuisdomainpskeys.h>
       
    79 #include "../../../inc/securityuisprivatepskeys.h"
       
    80 #include <avkondomainpskeys.h>
       
    81 
       
    82 #include <hbdevicedialog.h>
       
    83 
       
    84 const TInt KPhoneIndex(0);
       
    85 const TInt KTriesToConnectServer(2);
       
    86 const TInt KTimeBeforeRetryingServerConnection(50000);
       
    87 
       
    88 #define ESecUiTypeLock					0x00100000
       
    89 
       
    90 _LIT( KMmTsyModuleName, "PhoneTsy");
       
    91 
       
    92 #define XQSERVICE_DEBUG_PRINT(a) qDebug() << (a)
       
    93 
       
    94 AutolockSrv::AutolockSrv(QWidget *parent, Qt::WFlags f) :
       
    95     QWidget(parent, f), mService(NULL)
       
    96     {
       
    97     XQSERVICE_DEBUG_PRINT("AutolockSrv::AutolockSrv");
       
    98     RDEBUG("start", 0);
       
    99     mService = new AutolockSrvService(this);
       
   100 
       
   101     /* Adjust the palette */
       
   102 #if defined(Q_WS_S60)
       
   103     RDEBUG( "Q_WS_S60", 1 );
       
   104     QPalette p = qApp->palette();
       
   105     QColor color(192,192,192);
       
   106     QColor bg(201,250,250);
       
   107     p.setColor(QPalette::Highlight, color.lighter(200));
       
   108     p.setColor(QPalette::Text, Qt::black);
       
   109     p.setColor(QPalette::Base, bg);
       
   110     p.setColor(QPalette::WindowText, Qt::black);
       
   111     p.setColor(QPalette::Window, bg);
       
   112     p.setColor(QPalette::ButtonText, Qt::black);
       
   113     p.setColor(QPalette::Button, color.lighter(150));
       
   114     p.setColor(QPalette::Link, QColor(240,40,40));
       
   115 
       
   116     qApp->setPalette(p);
       
   117 #endif
       
   118 
       
   119     QPushButton *unlockButton = new QPushButton(tr("Unlock"));
       
   120     QPushButton *unguardButton = new QPushButton(tr("Unguard"));
       
   121     QPushButton *lockButton = new QPushButton(tr("Lock"));
       
   122     QPushButton *quitButton = new QPushButton(tr("Quit"));
       
   123     QPushButton *test1Button = new QPushButton(tr("Autolock 10 seconds"));
       
   124     QPushButton *test2Button = new QPushButton(tr("Autolock never"));
       
   125     connect(quitButton, SIGNAL(clicked()), this, SLOT(quit()));
       
   126     connect(lockButton, SIGNAL(clicked()), this, SLOT(lockAction()));
       
   127     connect(unlockButton, SIGNAL(clicked()), this, SLOT(unlockAction()));
       
   128     connect(unguardButton, SIGNAL(clicked()), this, SLOT(unguardAction()));
       
   129     connect(test1Button, SIGNAL(clicked()), this, SLOT(test1Action()));
       
   130     connect(test2Button, SIGNAL(clicked()), this, SLOT(test2Action()));
       
   131     RDEBUG("connect", 1);
       
   132 
       
   133     /* there's no use for this */
       
   134     /*
       
   135      bool isService = XQServiceUtil::isService();
       
   136      
       
   137      QString t = "SERVICEAPP:\n";
       
   138      t = t + (isService ?  "    LAUNCHED AS SERVICE\n" : "    LAUNCHED NORMALLY\n");
       
   139      t = t + (XQServiceUtil::isEmbedded() ? "    EMBEDDED\n" : "    NOT EMBEDDED\n");
       
   140      
       
   141      QStringList args = QApplication::arguments();
       
   142      foreach (QString arg, args)
       
   143      {
       
   144      t += "cmdline arg=" + arg + "\n";
       
   145      }
       
   146      
       
   147      QLabel *title = new QLabel(t);
       
   148      */
       
   149 
       
   150     mLabel = new QLabel("");
       
   151     mNumberEdit = new QLineEdit("");
       
   152 
       
   153     QVBoxLayout *vl = new QVBoxLayout;
       
   154     RDEBUG("QVBoxLayout", 1);
       
   155 
       
   156     vl->setMargin(0);
       
   157     vl->setSpacing(0);
       
   158     RDEBUG("setSpacing", 1);
       
   159 
       
   160     vl->addWidget(lockButton);
       
   161     vl->addWidget(unlockButton);
       
   162     vl->addWidget(unguardButton);
       
   163     vl->addWidget(quitButton);
       
   164     vl->addWidget(test1Button);
       
   165     vl->addWidget(test2Button);
       
   166     RDEBUG("added test2Button", 1);
       
   167 
       
   168     mLabelIcon = new QToolButton;
       
   169     mLabelIcon->setIcon(QIcon(
       
   170             ":/AutolockSrv_hbicon/qtg_large_device_lock.svg"));
       
   171     mLabelIcon->setIconSize(QSize(300, 300));
       
   172 
       
   173     vl->addWidget(mLabelIcon);
       
   174     RDEBUG("added mLabelIcon", 1);
       
   175 
       
   176     /*    vl->addItem(title);
       
   177      vl->addWidget(mLabel);
       
   178      vl->addWidget(mNumberEdit);
       
   179      */
       
   180     setLayout(vl);
       
   181 
       
   182 #if defined(Q_WS_X11) || defined(Q_WS_WIN)
       
   183     setFixedSize(QSize(360,640)); // nHD
       
   184 #elif defined(Q_WS_S60)
       
   185     // showMaximized();
       
   186     showFullScreen();
       
   187 #endif
       
   188     mLabelIcon_visible = 1;
       
   189     serviceKeyguard = new AutolockUserActivityService();
       
   190     serviceDevicelock = new AutolockUserActivityService();
       
   191 
       
   192     TInt lockValue = 0;
       
   193     TInt lightsTimeout = 0;
       
   194     CRepository* repository;
       
   195     TInt cRresult = 0;
       
   196 
       
   197     iLockStatusPrev = ELockNotActive;
       
   198     iLockStatus = ELockNotActive;
       
   199     repository = CRepository::NewL(KCRUidSecuritySettings);
       
   200     cRresult = repository->Get(KSettingsAutolockStatus, lockValue);
       
   201     RDEBUG("KSettingsAutolockStatus", KSettingsAutolockStatus);
       
   202     RDEBUG("cRresult", cRresult);
       
   203     RDEBUG("lockValue", lockValue);
       
   204     iLockStatus = lockValue;
       
   205     // the settings says to lock
       
   206     delete repository;
       
   207 
       
   208     adjustInactivityTimers(0);
       
   209 
       
   210     repository = CRepository::NewL(KCRUidProfileEngine);
       
   211     cRresult = repository->Get(KProEngActiveProfile, lightsTimeout);
       
   212     delete repository;
       
   213 
       
   214     repository = CRepository::NewL(KCRUidLightSettings);
       
   215     cRresult = repository->Get(KDisplayLightsTimeout, lightsTimeout);
       
   216     delete repository;
       
   217 
       
   218     // TODO flip
       
   219 
       
   220     subscriberKSettingsAutolockStatus = new QValueSpaceSubscriber(
       
   221             "/KCRUidSecuritySettings/KSettingsAutolockStatus", this);
       
   222     connect(subscriberKSettingsAutolockStatus, SIGNAL(contentsChanged()),
       
   223             this, SLOT(subscriberKSettingsAutolockStatusChanged()));
       
   224     subscriberKSettingsAutoLockTime = new QValueSpaceSubscriber(
       
   225             "/KCRUidSecuritySettings/KSettingsAutoLockTime", this);
       
   226     connect(subscriberKSettingsAutoLockTime, SIGNAL(contentsChanged()), this,
       
   227             SLOT(subscriberKSettingsAutoLockTimeChanged()));
       
   228     subscriberKSettingsAutomaticKeyguardTime = new QValueSpaceSubscriber(
       
   229             "/KCRUidSecuritySettings/KSettingsAutomaticKeyguardTime", this);
       
   230     connect(subscriberKSettingsAutomaticKeyguardTime, SIGNAL(
       
   231             contentsChanged()), this, SLOT(
       
   232             subscriberKSettingsAutomaticKeyguardTimeChanged()));
       
   233     subscriberKDisplayLightsTimeout = new QValueSpaceSubscriber(
       
   234             "/KCRUidLightSettings/KDisplayLightsTimeout", this);
       
   235     connect(subscriberKDisplayLightsTimeout, SIGNAL(contentsChanged()), this,
       
   236             SLOT(subscriberKDisplayLightsTimeoutChanged()));
       
   237     subscriberKProEngActiveProfile = new QValueSpaceSubscriber(
       
   238             "/KCRUidProfileEngine/KProEngActiveProfile", this);
       
   239     connect(subscriberKProEngActiveProfile, SIGNAL(contentsChanged()), this,
       
   240             SLOT(subscriberKProEngActiveProfileChanged()));
       
   241 
       
   242     TSecurityPolicy readPolicy(ECapabilityReadDeviceData);
       
   243     TSecurityPolicy writePolicy(ECapabilityWriteDeviceData);
       
   244     TInt ret = RProperty::Define(KPSUidSecurityUIs,
       
   245             KSecurityUIsSecUIOriginatedQuery, RProperty::EInt, readPolicy,
       
   246             writePolicy);
       
   247     RDEBUG("defined KSecurityUIsSecUIOriginatedQuery", ret);
       
   248     ret = RProperty::Define(KPSUidSecurityUIs,
       
   249             KSecurityUIsQueryRequestCancel, RProperty::EInt, readPolicy,
       
   250             writePolicy);
       
   251     RDEBUG("defined KSecurityUIsQueryRequestCancel", ret);
       
   252 
       
   253     _LIT_SECURITY_POLICY_PASS( KReadPolicy);
       
   254     _LIT_SECURITY_POLICY_C1(KWritePolicy, ECapabilityWriteDeviceData);
       
   255     ret = RProperty::Define(KPSUidCoreApplicationUIs,
       
   256             KCoreAppUIsAutolockStatus, RProperty::EInt, KReadPolicy,
       
   257             KWritePolicy);
       
   258     RDEBUG("defined KCoreAppUIsAutolockStatus", ret);
       
   259 
       
   260     ret = RProperty::Define(KPSUidAvkonDomain, KAknKeyguardStatus,
       
   261             RProperty::EInt, TSecurityPolicy(TSecurityPolicy::EAlwaysPass),
       
   262             KWritePolicy);
       
   263     RDEBUG("defined KAknKeyguardStatus", ret);
       
   264 
       
   265     ret = RProperty::Define(KPSUidSecurityUIs,
       
   266             KSecurityUIsLockInitiatorUID, RProperty::EInt, readPolicy,
       
   267             writePolicy);
       
   268     RDEBUG("defined KSecurityUIsLockInitiatorUID", ret);
       
   269 
       
   270     ret = RProperty::Define(KPSUidSecurityUIs,
       
   271             KSecurityUIsLockInitiatorTimeHigh, RProperty::EInt, readPolicy,
       
   272             writePolicy);
       
   273     RDEBUG("defined KSecurityUIsLockInitiatorTimeHigh", ret);
       
   274     ret = RProperty::Define(KPSUidSecurityUIs,
       
   275             KSecurityUIsLockInitiatorTimeLow, RProperty::EInt, readPolicy,
       
   276             writePolicy);
       
   277     RDEBUG("defined KSecurityUIsLockInitiatorTimeLow", ret);
       
   278 
       
   279     ret = RProperty::Define(KPSUidSecurityUIs, KSecurityUIsLights,
       
   280             RProperty::EInt, TSecurityPolicy(TSecurityPolicy::EAlwaysPass),
       
   281             TSecurityPolicy(TSecurityPolicy::EAlwaysPass));
       
   282     RDEBUG("defined KAknKeyguardStatus", ret);
       
   283 
       
   284     // inactivity
       
   285     connect(serviceKeyguard, SIGNAL(active()), this, SLOT(activeKeyguard()));
       
   286     connect(serviceKeyguard, SIGNAL(notActive()), this, SLOT(
       
   287             notActiveKeyguard()));
       
   288     connect(serviceDevicelock, SIGNAL(active()), this, SLOT(
       
   289             activeDevicelock()));
       
   290     connect(serviceDevicelock, SIGNAL(notActive()), this, SLOT(
       
   291             notActiveDevicelock()));
       
   292 
       
   293     RWindowGroup& groupWin = CEikonEnv::Static()->RootWin();
       
   294     RDEBUG("got groupWin", 1);
       
   295     // TODO if I want to release, I should do:   mKeyCaptureHandle = env->RootWin().CaptureKey(EKeyBackspace, 0, 0);
       
   296     groupWin.CaptureKey(EKeyBackspace, 0, 0);
       
   297     groupWin.CaptureKey(EKeyDeviceF, 0, 0);
       
   298     groupWin.CaptureKey(EKeyBell, 0, 0);
       
   299     groupWin.CaptureKey(EKeyTab, 0, 0);
       
   300     groupWin.CaptureKey(EKeyInsert, 0, 0);
       
   301     RDEBUG("got mKeyCaptureHandle", 1);
       
   302 
       
   303     iSecQueryUiCreated = -1;
       
   304     iDeviceDialogCreated = -1;
       
   305     // TODO for now, always starts unlocked
       
   306     // TryChangeStatus(iLockStatus);
       
   307     TryChangeStatus( ELockNotActive);
       
   308     lower();
       
   309     hide();
       
   310     // not needed:   new AutolockSrvService(this);
       
   311     }
       
   312 
       
   313 AutolockSrv::~AutolockSrv()
       
   314     {
       
   315     RDEBUG("0", 0);
       
   316     delete mService;
       
   317     }
       
   318 
       
   319 void AutolockSrv::adjustInactivityTimers(int aReason)
       
   320     {
       
   321     RDEBUG("aReason", aReason);
       
   322     TInt keyguardTime = 0;
       
   323     TInt lockTime = 0;
       
   324     CRepository* repository;
       
   325     TInt cRresult = 0;
       
   326 
       
   327     repository = CRepository::NewL(KCRUidSecuritySettings);
       
   328     cRresult = repository->Get(KSettingsAutoLockTime, lockTime);	// in minutes, handled internally as seconds
       
   329     lockTime *= 60;
       
   330     RDEBUG("KSettingsAutoLockTime", KSettingsAutoLockTime);
       
   331     RDEBUG("cRresult", cRresult);
       
   332     RDEBUG("lockTime", lockTime);
       
   333     if (lockTime > 60 && lockTime < 24*60*60)	// lock timer can't be bigger than 1 day
       
   334         {
       
   335         serviceDevicelock->setInactivityPeriod(lockTime);
       
   336         }
       
   337     else
       
   338         {
       
   339         serviceDevicelock->setInactivityPeriod(12 * 31 * 24 * 60 * 60);	// 0x1ea6e00
       
   340         }
       
   341 
       
   342     cRresult = repository->Get(KSettingsAutomaticKeyguardTime, keyguardTime);	// in seconds
       
   343     RDEBUG("KSettingsAutomaticKeyguardTime", KSettingsAutomaticKeyguardTime);
       
   344     RDEBUG("cRresult", cRresult);
       
   345     RDEBUG("keyguardTime", keyguardTime);
       
   346     if (keyguardTime > 0 && keyguardTime < 1000)
       
   347         {
       
   348         serviceKeyguard->setInactivityPeriod(keyguardTime);
       
   349         }
       
   350     else
       
   351         {
       
   352         serviceKeyguard->setInactivityPeriod(12 * 31 * 24 * 60 * 60);
       
   353         }
       
   354     delete repository;
       
   355     }
       
   356 void AutolockSrv::quit()
       
   357     {
       
   358     RDEBUG("0", 0);
       
   359     qApp->quit();
       
   360     }
       
   361 
       
   362 // from the button
       
   363 void AutolockSrv::unlockAction()
       
   364     {
       
   365     RDEBUG("0", 0);
       
   366     TryChangeStatus( ELockAppDisableDevicelock);
       
   367     }
       
   368 
       
   369 void AutolockSrv::unguardAction()
       
   370     {
       
   371     RDEBUG("0", 0);
       
   372     TryChangeStatus( ELockAppDisableKeyguard);
       
   373     }
       
   374 
       
   375 void AutolockSrv::test1Action()
       
   376     {
       
   377     RDEBUG("Set(KSettingsAutoLockTime, 2)", 2);
       
   378 
       
   379     CRepository* repositorySet = CRepository::NewL(KCRUidSecuritySettings);
       
   380     repositorySet->Set(KSettingsAutoLockTime, 2);	// in minutes
       
   381     delete repositorySet;
       
   382     }
       
   383 
       
   384 void AutolockSrv::test2Action()
       
   385     {
       
   386     RDEBUG("Set(KSettingsAutoLockTime, 0)", 0);
       
   387 
       
   388     CRepository* repositorySet = CRepository::NewL(KCRUidSecuritySettings);
       
   389     repositorySet->Set(KSettingsAutoLockTime, 0);	// minutes
       
   390     delete repositorySet;
       
   391     }
       
   392 
       
   393 int AutolockSrv::AskValidSecCode(int aReason)
       
   394     {
       
   395     RDEBUG("aReason", aReason);
       
   396     RMobilePhone iPhone; // NULL in emulator
       
   397 
       
   398 #ifdef __WINS__1
       
   399     return KErrNone;
       
   400 #endif
       
   401 
       
   402 
       
   403     TInt err(KErrGeneral);
       
   404     TBool validCode(EFalse);
       
   405     TInt thisTry(0);
       
   406     RTelServer iTelServer;
       
   407 
       
   408     err = RProperty::Set(KPSUidSecurityUIs,
       
   409             KSecurityUIsLights, ESecurityUIsLightsQueryOnRequest);
       
   410 		RDEBUG("KSecurityUIsLights err", err);
       
   411 
       
   412     RMmCustomAPI iCustomPhone;
       
   413     while ((err = iTelServer.Connect()) != KErrNone && (thisTry++)
       
   414             <= KTriesToConnectServer)
       
   415         {
       
   416         User::After( KTimeBeforeRetryingServerConnection);
       
   417         }
       
   418     err = iTelServer.LoadPhoneModule(KMmTsyModuleName);
       
   419     RTelServer::TPhoneInfo PhoneInfo;
       
   420     RDEBUG("err", err);
       
   421     err = iTelServer.SetExtendedErrorGranularity(RTelServer::EErrorExtended);
       
   422     RDEBUG("err", err);
       
   423     err = iTelServer.GetPhoneInfo(KPhoneIndex, PhoneInfo);
       
   424     RDEBUG("err", err);
       
   425     err = iPhone.Open(iTelServer, PhoneInfo.iName);
       
   426     RDEBUG("err", err);
       
   427     err = iCustomPhone.Open(iPhone);
       
   428     RDEBUG("err", err);
       
   429 
       
   430     RDEBUG("CSecurityHandler", 0);
       
   431     CSecurityHandler* handler = new (ELeave) CSecurityHandler(iPhone);
       
   432     if (aReason == ELockAppDisableDevicelock)
       
   433         {
       
   434         RDEBUG("calling AskSecCodeInAutoLockL", 0);
       
   435         validCode = handler->AskSecCodeInAutoLockL(); // this returns true/false
       
   436         // TODO should this also do iPhone.SetLockSetting(status, lockType, lockChange); ???
       
   437         }
       
   438     else if (aReason == ELockAppEnableDevicelock)
       
   439         {
       
   440         // check whether code is really needed
       
   441         RMobilePhone::TMobilePhoneLock lockType =
       
   442                 RMobilePhone::ELockPhoneDevice;
       
   443         RMobilePhone::TMobilePhoneLockInfoV1 lockInfo;
       
   444         RMobilePhone::TMobilePhoneLockInfoV1Pckg lockInfoPkg(lockInfo);
       
   445         RMobilePhone::TMobilePhoneLockSetting lockChange(
       
   446                 RMobilePhone::ELockSetDisabled);
       
   447         TRequestStatus status = KRequestPending;
       
   448         TInt ret = KErrNone;
       
   449         RDEBUG("GetLockInfo", 0);
       
   450         iPhone.GetLockInfo(status, lockType, lockInfoPkg);
       
   451         RDEBUG("WaitForRequest", 0);
       
   452         User::WaitForRequest(status);
       
   453         ret = status.Int();
       
   454         RDEBUG("WaitForRequest ret", ret);
       
   455 #ifdef __WINS__
       
   456         if(ret==KErrTimedOut)
       
   457             {
       
   458             ret = KErrNone;
       
   459             lockInfo.iSetting = RMobilePhone::ELockSetDisabled; // ask password only if there's no timeout.
       
   460             }
       
   461 #endif
       
   462         if (ret == KErrNone)
       
   463             {
       
   464             RDEBUG("lockInfo.iSetting", lockInfo.iSetting);
       
   465             RDEBUG("RMobilePhone::ELockSetDisabled",
       
   466                     RMobilePhone::ELockSetDisabled);
       
   467             if (lockInfo.iSetting == RMobilePhone::ELockSetDisabled) // ask password only if there's no timeout
       
   468                 {
       
   469                 lockChange = RMobilePhone::ELockSetEnabled;
       
   470                 RDEBUG("SetLockSetting lockChange", lockChange);
       
   471 #define OLD_METHODx
       
   472 #ifdef OLD_METHOD
       
   473                 status = KRequestPending;
       
   474                 RDEBUG( "SetLockSetting", 0 );
       
   475                 iPhone.SetLockSetting(status, lockType, lockChange); // ask for PassPhraseRequiredL
       
   476                 RDEBUG( "WaitForRequestL", 0 );
       
   477                 User::WaitForRequest( status ); // TODO this waits 33 seconds in WINS and returns ffffffdf = KErrTimedOut
       
   478                 ret = status.Int();
       
   479                 RDEBUG( "WaitForRequestL ret", ret );
       
   480 #else
       
   481                 RDEBUG("! OLD_METHOD", 0);
       
   482                 CWait *iWait = NULL;
       
   483                 iWait = CWait::NewL();
       
   484                 iWait->SetRequestType(EMobilePhoneSetLockSetting);
       
   485                 iPhone.SetLockSetting(iWait->iStatus, lockType, lockChange); // ask for PassPhraseRequiredL
       
   486                 RDEBUG("WaitForRequestL", 0);
       
   487                 ret = iWait->WaitForRequestL();
       
   488                 RDEBUG("WaitForRequestL ret", ret);
       
   489                 if (iWait)
       
   490                     {
       
   491                     RDEBUG("IsActive", 0);
       
   492                     if (iWait->IsActive())
       
   493                         {
       
   494                         RDEBUG("CancelAsyncRequest", 0);
       
   495                         iPhone.CancelAsyncRequest(iWait->GetRequestType());
       
   496                         RDEBUG("cancelled", 0);
       
   497                         }
       
   498                     }
       
   499                 delete iWait;
       
   500 #endif
       
   501 
       
   502                 RDEBUG("WaitForRequestL ret", ret);
       
   503 #ifdef __WINS__
       
   504                 if(ret==KErrTimedOut)
       
   505                 ret = KErrNone;
       
   506 #endif
       
   507                 if (ret == KErrNone)
       
   508                     validCode = 1;
       
   509                 else
       
   510                     validCode = 0;
       
   511                 }
       
   512             else
       
   513                 validCode = 0x20;
       
   514             }
       
   515         else
       
   516             validCode = 0x21;
       
   517 
       
   518         /* Alternative way to ask for password
       
   519          RMobilePhone::TMobilePhoneSecurityEvent iEvent;
       
   520          TInt result = KErrNone;
       
   521          iEvent = RMobilePhone::EPhonePasswordRequired;
       
   522          RDEBUG( "calling HandleEventL", 0 );
       
   523          handler->HandleEventL(iEvent, result);	// this is supposed to wait
       
   524          validCode = false;
       
   525          if(result)
       
   526          validCode = true;
       
   527          RDEBUG( "result", result );
       
   528          */
       
   529         }
       
   530     // TODO this doesn't wait on WINS , so how do I get the Acceptation?
       
   531     RDEBUG("validCode (true/false)", validCode);
       
   532     if (validCode)
       
   533         return KErrNone;
       
   534 
       
   535 		// no valid code -> switch off the lights
       
   536     err = RProperty::Set(KPSUidSecurityUIs,
       
   537             KSecurityUIsLights, ESecurityUIsLightsLockOffRequest);
       
   538 		RDEBUG("KSecurityUIsLights err", err);
       
   539     return KErrCancel;
       
   540     }
       
   541 
       
   542 void AutolockSrv::lockAction()
       
   543     {
       
   544     RDEBUG("0", 0);
       
   545 
       
   546     XQSERVICE_DEBUG_PRINT("AutolockSrv::lockAction");
       
   547     TryChangeStatus( ELockAppOfferDevicelock);
       
   548 
       
   549     }
       
   550 
       
   551 void AutolockSrv::handleAnswerDelivered()
       
   552     {
       
   553     RDEBUG("0", 0);
       
   554     // quit();
       
   555 
       
   556     }
       
   557 
       
   558 void AutolockSrv::endCall()
       
   559     {
       
   560     RDEBUG("This should never be called", 0);
       
   561     //QVBoxLayout *vl = qobject_cast<QVBoxLayout *>(layout()) ;
       
   562     //vl->removeWidget(mEndCallButton);
       
   563 
       
   564     //XQServiceUtil::toBackground(true);
       
   565     }
       
   566 
       
   567 void AutolockSrv::setLabelNumber(QString label, QString number)
       
   568     {
       
   569     RDEBUG("0", 0);
       
   570     mLabel->setText("mLabel=" + label);
       
   571     mNumberEdit->setText("mNumberEdit=" + number);
       
   572     }
       
   573 
       
   574 void AutolockSrv::DebugRequest(int aReason)
       
   575     {
       
   576     switch (aReason)
       
   577         {
       
   578         case ELockAppEnableKeyguard:
       
   579             RDEBUG("ELockAppEnableKeyguard", aReason);
       
   580             break;
       
   581         case ELockAppDisableKeyguard:
       
   582             RDEBUG("ELockAppDisableKeyguard", aReason);
       
   583             break;
       
   584         case ELockAppEnableDevicelock:
       
   585             RDEBUG("ELockAppEnableDevicelock", aReason);
       
   586             break;
       
   587         case ELockAppDisableDevicelock:
       
   588             RDEBUG("ELockAppDisableDevicelock", aReason);
       
   589             break;
       
   590         case ELockAppOfferKeyguard:
       
   591             RDEBUG("ELockAppOfferKeyguard", aReason);
       
   592             break;
       
   593         case ELockAppOfferDevicelock:
       
   594             RDEBUG("ELockAppOfferDevicelock", aReason);
       
   595             break;
       
   596         case ELockAppShowKeysLockedNote:
       
   597             RDEBUG("ELockAppShowKeysLockedNote", aReason);
       
   598             break;
       
   599         default:
       
   600             RDEBUG("default", aReason);
       
   601             break;
       
   602         }
       
   603     }
       
   604 void AutolockSrv::DebugStatus(int aReason)
       
   605     {
       
   606     switch (aReason)
       
   607         {
       
   608         case ELockNotActive:
       
   609             RDEBUG("ELockNotActive", aReason);
       
   610             break;
       
   611         case EKeyguardActive:
       
   612             RDEBUG("EKeyguardActive", aReason);
       
   613             break;
       
   614         case EDevicelockActive:
       
   615             RDEBUG("EDevicelockActive", aReason);
       
   616             break;
       
   617         default:
       
   618             RDEBUG("default", aReason);
       
   619             break;
       
   620         }
       
   621     }
       
   622 void AutolockSrv::DebugError(int aReason)
       
   623     {
       
   624     switch (aReason)
       
   625         {
       
   626         case KErrNone:
       
   627             RDEBUG("KErrNone", aReason);
       
   628             break;
       
   629         case KErrPermissionDenied:	// caller doesn't have enough capabilities
       
   630             RDEBUG("KErrPermissionDenied", aReason);
       
   631             break;
       
   632         case KErrAlreadyExists:	// this particular lock is already enabled
       
   633             RDEBUG("KErrAlreadyExists", aReason);
       
   634             break;
       
   635         case KErrInUse:	// the dialog is already shown
       
   636             RDEBUG("KErrInUse", aReason);
       
   637             break;
       
   638         default:
       
   639             RDEBUG("default", aReason);
       
   640             break;
       
   641         }
       
   642     }
       
   643 
       
   644 int AutolockSrv::CheckIfLegal(int aReason)
       
   645     {
       
   646     RDEBUG("aReason", aReason);
       
   647     // check whether a dialog is already displayed. This is to prevent timeout-lock and timeout-keyguard for activated on top of a PIN query, in particular at boot-up (TODO Starter)
       
   648     TInt secUiOriginatedQuery(ESecurityUIsSecUIOriginatedUninitialized);
       
   649     TInt err = RProperty::Get(KPSUidSecurityUIs,
       
   650             KSecurityUIsSecUIOriginatedQuery, secUiOriginatedQuery);
       
   651     RDEBUG("err", err);
       
   652     RDEBUG("secUiOriginatedQuery", secUiOriginatedQuery);
       
   653     switch (aReason)
       
   654         {
       
   655         case ELockAppEnableKeyguard:
       
   656             {
       
   657             RDEBUG("ELockAppEnableKeyguard iLockStatus", iLockStatus);
       
   658             if (secUiOriginatedQuery
       
   659                     != ESecurityUIsSecUIOriginatedUninitialized)
       
   660                 return KErrInUse; // PIN on top. Don't keyguard
       
   661             switch (iLockStatus)
       
   662                 {
       
   663                 case ELockNotActive:
       
   664                     if (1 == 0) // !CKeyLockPolicyApi::KeyguardAllowed() )
       
   665                         return KErrPermissionDenied;
       
   666                     else
       
   667                         return KErrNone;
       
   668                 case EKeyguardActive:
       
   669                     return KErrAlreadyExists;
       
   670                 case EDevicelockActive:
       
   671                     return KErrPermissionDenied;
       
   672                 }
       
   673             }
       
   674             break;
       
   675         case ELockAppDisableKeyguard:
       
   676             {
       
   677             RDEBUG("ELockAppDisableKeyguard iLockStatus", iLockStatus);
       
   678             // no matter whether PIN is displayed
       
   679             switch (iLockStatus)
       
   680                 {
       
   681                 case ELockNotActive:
       
   682                     return KErrAlreadyExists;
       
   683                 case EKeyguardActive:
       
   684                     return KErrNone;
       
   685                 case EDevicelockActive:
       
   686                     return KErrPermissionDenied;
       
   687                 }
       
   688             }
       
   689             break;
       
   690         case ELockAppEnableDevicelock:
       
   691             {
       
   692             RDEBUG("ELockAppEnableDevicelock iLockStatus", iLockStatus);
       
   693             if (secUiOriginatedQuery
       
   694                     != ESecurityUIsSecUIOriginatedUninitialized
       
   695                     && secUiOriginatedQuery
       
   696                             != ESecurityUIsSystemLockOriginated)
       
   697                 return KErrInUse; // PIN on top. Don't devicelock
       
   698             switch (iLockStatus)
       
   699                 {
       
   700                 case ELockNotActive:
       
   701                     return KErrNone;
       
   702                 case EKeyguardActive:
       
   703                     return KErrNone;
       
   704                 case EDevicelockActive:
       
   705                     return KErrAlreadyExists;
       
   706                 }
       
   707             }
       
   708             break;
       
   709         case ELockAppDisableDevicelock:
       
   710             {
       
   711             RDEBUG("ELockAppDisableDevicelock iLockStatus", iLockStatus);
       
   712             if (secUiOriginatedQuery
       
   713                     != ESecurityUIsSecUIOriginatedUninitialized
       
   714                     && secUiOriginatedQuery
       
   715                             != ESecurityUIsSystemLockOriginated)
       
   716                 {
       
   717                 // PIN on top and trying to display unlock-code. This is valid
       
   718                 }
       
   719             switch (iLockStatus)
       
   720                 {
       
   721                 case ELockNotActive:
       
   722                     return KErrAlreadyExists;
       
   723                 case EKeyguardActive:
       
   724                     return KErrPermissionDenied;
       
   725                 case EDevicelockActive:
       
   726                     return KErrNone;
       
   727                 }
       
   728             }
       
   729             break;
       
   730         case ELockAppOfferKeyguard:
       
   731             {
       
   732             RDEBUG("ELockAppOfferKeyguard iLockStatus", iLockStatus);
       
   733             if (secUiOriginatedQuery
       
   734                     != ESecurityUIsSecUIOriginatedUninitialized
       
   735                     && secUiOriginatedQuery
       
   736                             != ESecurityUIsSystemLockOriginated)
       
   737                 return KErrInUse; // PIN on top. Don't offer
       
   738             switch (iLockStatus)
       
   739                 {
       
   740                 case ELockNotActive:
       
   741                     return KErrNone;
       
   742                 case EKeyguardActive:
       
   743                     return KErrPermissionDenied;
       
   744                 case EDevicelockActive:
       
   745                     return KErrPermissionDenied;
       
   746                 }
       
   747             }
       
   748             break;
       
   749         case ELockAppOfferDevicelock:
       
   750             {
       
   751             RDEBUG("ELockAppOfferDevicelock iLockStatus", iLockStatus);
       
   752             if (secUiOriginatedQuery
       
   753                     != ESecurityUIsSecUIOriginatedUninitialized
       
   754                     && secUiOriginatedQuery
       
   755                             != ESecurityUIsSystemLockOriginated)
       
   756                 return KErrInUse; // PIN on top. Don't offer
       
   757             switch (iLockStatus)
       
   758                 {
       
   759                 case ELockNotActive:
       
   760                     return KErrNone;
       
   761                 case EKeyguardActive:
       
   762                     return KErrNone;
       
   763                 case EDevicelockActive:
       
   764                     return KErrAlreadyExists;
       
   765                 }
       
   766             }
       
   767             break;
       
   768         case ELockAppShowKeysLockedNote:
       
   769             {
       
   770             RDEBUG("ELockAppShowKeysLockedNote iLockStatus", iLockStatus);
       
   771             if (secUiOriginatedQuery
       
   772                     != ESecurityUIsSecUIOriginatedUninitialized
       
   773                     && secUiOriginatedQuery
       
   774                             != ESecurityUIsSystemLockOriginated)
       
   775                 return KErrInUse; // PIN on top. Don't display
       
   776             switch (iLockStatus)
       
   777                 {
       
   778                 case ELockNotActive:
       
   779                     return KErrPermissionDenied;
       
   780                 case EKeyguardActive:
       
   781                     return KErrNone;
       
   782                 case EDevicelockActive:
       
   783                     return KErrPermissionDenied;
       
   784                 }
       
   785             }
       
   786             break;
       
   787         default:
       
   788             {
       
   789             RDEBUG("default iLockStatus", iLockStatus);
       
   790             return KErrPermissionDenied;
       
   791             }
       
   792             // break;
       
   793         } // switch
       
   794     return KErrPermissionDenied;
       
   795     }
       
   796 
       
   797 int AutolockSrv::publishStatus(int aReason)
       
   798     {
       
   799     RDEBUG("aReason", aReason);
       
   800     TInt err;
       
   801     const TUid KCRUidCoreApplicationUIsSysAp = { 0x101F8765 };
       
   802     const TUint32 KSysApKeyguardActive = 0x00000001;
       
   803     CRepository* repositoryDevicelock;
       
   804     CRepository* repositoryKeyguard;
       
   805     repositoryDevicelock = CRepository::NewL(KCRUidSecuritySettings);
       
   806     repositoryKeyguard = CRepository::NewL(KCRUidCoreApplicationUIsSysAp);
       
   807     TInt cRresult = KErrNone;
       
   808     if (1 == 1) // this is a quick way to disable this functionality, for testing
       
   809         {
       
   810         RDEBUG("publishing", aReason);
       
   811         if (aReason == ELockNotActive)
       
   812             {
       
   813             err = RProperty::Set(KPSUidAvkonDomain, KAknKeyguardStatus,
       
   814                     EKeyguardNotActive);
       
   815             RDEBUG("err", err);
       
   816             err = RProperty::Set(KPSUidCoreApplicationUIs,
       
   817                     KCoreAppUIsAutolockStatus, EAutolockOff);
       
   818             RDEBUG("err", err);
       
   819             // lights are required ?
       
   820 
       
   821             // cRresult = repositoryDevicelock->Set(KSettingsAutolockStatus, 0);	// the settings remains. Only ISA changes, as well as the P&S
       
   822             cRresult = repositoryKeyguard->Set(KSysApKeyguardActive, 0);
       
   823             RDEBUG("cRresult", cRresult);
       
   824             }
       
   825         if (1 == 1) // this is a quick way to disable this functionality, for testing
       
   826             {
       
   827             if (aReason == EKeyguardActive)
       
   828                 {
       
   829                 err = RProperty::Set(KPSUidAvkonDomain, KAknKeyguardStatus,
       
   830                         EKeyguardLocked);
       
   831             		RDEBUG("KAknKeyguardStatus err", err);
       
   832                 err = RProperty::Set(KPSUidCoreApplicationUIs,
       
   833                         KCoreAppUIsAutolockStatus, EAutolockOff);
       
   834             		RDEBUG("KCoreAppUIsAutolockStatus err", err);
       
   835                 err = RProperty::Set(KPSUidSecurityUIs,
       
   836                         KSecurityUIsLights, ESecurityUIsLightsLockOffRequest);	// same for keyguard and devicelock
       
   837             		RDEBUG("KSecurityUIsLights err", err);
       
   838                 // cRresult = repositoryDevicelock->Set(KSettingsAutolockStatus, 0);
       
   839                 cRresult = repositoryKeyguard->Set(KSysApKeyguardActive, 1);
       
   840                 RDEBUG("cRresult", cRresult);
       
   841                 }
       
   842             else if (aReason >= EDevicelockActive)
       
   843                 {
       
   844                 err = RProperty::Set(KPSUidAvkonDomain, KAknKeyguardStatus,
       
   845                         EKeyguardAutolockEmulation); // not EKeyguardLocked, not EKeyguardNotActive
       
   846             		RDEBUG("KAknKeyguardStatus err", err);
       
   847                 err = RProperty::Set(KPSUidCoreApplicationUIs,
       
   848                         KCoreAppUIsAutolockStatus, EManualLocked);
       
   849             		RDEBUG("KCoreAppUIsAutolockStatus err", err);
       
   850                 err = RProperty::Set(KPSUidSecurityUIs,
       
   851                         KSecurityUIsLights, ESecurityUIsLightsLockOffRequest);
       
   852             		RDEBUG(" KSecurityUIsLights err", err);
       
   853                 // cRresult = repositoryDevicelock->Set(KSettingsAutolockStatus, 1);
       
   854                 cRresult = repositoryKeyguard->Set(KSysApKeyguardActive, 0); // keyguard disabled, so that user can type and DeviceF can be captured
       
   855                 RDEBUG("cRresult", cRresult);
       
   856                 }
       
   857             }
       
   858         }
       
   859     delete repositoryDevicelock;
       
   860     delete repositoryKeyguard;
       
   861     // this is the real point where everything is done.
       
   862     iLockStatusPrev = iLockStatus;
       
   863     iLockStatus = aReason;
       
   864     RDEBUG("setting iLockStatus", iLockStatus);
       
   865     return KErrNone;
       
   866     }
       
   867 /********************/
       
   868 int AutolockSrv::showNoteIfRequested(int aReason)
       
   869     {
       
   870     RDEBUG("aReason", aReason);
       
   871 
       
   872     if (aReason == ELockNotActive)
       
   873         {
       
   874         if (iShowKeyguardNote == 1)
       
   875             {
       
   876             HbDeviceMessageBox::information("Keyguard deactivated");
       
   877             }
       
   878         }
       
   879     else if (aReason == EKeyguardActive)
       
   880         {
       
   881         if (iShowKeyguardNote == 1)
       
   882             {
       
   883             HbDeviceMessageBox::information("Keyguard activated");
       
   884             }
       
   885         }
       
   886     else if (aReason >= EDevicelockActive) // this doesn't happen, but we are prepared nevertheless
       
   887         {
       
   888         if (iShowKeyguardNote == 1)
       
   889             {
       
   890             HbDeviceMessageBox::information("Devicelock activated");
       
   891             }
       
   892         }
       
   893     return KErrNone;
       
   894     }
       
   895 /***********************/
       
   896 int AutolockSrv::TryChangeStatus(int aReason)
       
   897     {
       
   898     RDEBUG("aReason", aReason);
       
   899     int ret = aReason;
       
   900     int errorInProcess = KErrNone;
       
   901     DebugRequest(ret);
       
   902 
       
   903     switch (ret)
       
   904         {
       
   905         case ELockAppEnableKeyguard: // 1
       
   906             {
       
   907             errorInProcess = CheckIfLegal(ret);
       
   908             DebugError(errorInProcess);
       
   909             if (errorInProcess == KErrNone)
       
   910                 {
       
   911                 setLabelIcon( EKeyguardActive);
       
   912                 updateIndicator(EKeyguardActive);
       
   913                 publishStatus(EKeyguardActive);
       
   914                 showNoteIfRequested(EKeyguardActive);
       
   915                 }
       
   916             }
       
   917             break;
       
   918         case ELockAppDisableKeyguard: // 2
       
   919             {
       
   920             errorInProcess = CheckIfLegal(ret);
       
   921             DebugError(errorInProcess);
       
   922             if (errorInProcess == KErrNone)
       
   923                 {
       
   924                 setLabelIcon( ELockNotActive);
       
   925                 updateIndicator(ELockNotActive);
       
   926                 publishStatus(ELockNotActive);
       
   927                 showNoteIfRequested(ELockNotActive);
       
   928                 }
       
   929             }
       
   930             break;
       
   931         case ELockAppEnableDevicelock: // 3
       
   932             {
       
   933             errorInProcess = CheckIfLegal(ret);
       
   934             DebugError(errorInProcess);
       
   935             if (errorInProcess == KErrNone)
       
   936                 {
       
   937                 if (!callerHasECapabilityWriteDeviceData) // check permissions for calling process, because doesn't AskValidSecCode
       
   938                     errorInProcess = KErrPermissionDenied;
       
   939                 DebugError(errorInProcess);
       
   940                 }
       
   941             if (errorInProcess == KErrNone)
       
   942                 {
       
   943                 updateIndicator( EDevicelockActive);
       
   944                 publishStatus(EDevicelockActive);
       
   945                 setLabelIcon(EDevicelockActive);
       
   946                 setLockDialog(aReason, 1);
       
   947                 }
       
   948             // aParam1 is aReason : EDevicelockManual, EDevicelockRemote
       
   949             // this never shows a note
       
   950             }
       
   951             break;
       
   952         case ELockAppDisableDevicelock: // 4
       
   953             {
       
   954             errorInProcess = CheckIfLegal(ret);
       
   955             DebugError(errorInProcess);
       
   956             if (errorInProcess == KErrNone)
       
   957                 {
       
   958                 if (!callerHasECapabilityWriteDeviceData) // check permissions for calling process, because doesn't AskValidSecCode
       
   959                     errorInProcess = KErrPermissionDenied;
       
   960                 DebugError(errorInProcess);
       
   961                 }
       
   962             if (errorInProcess == KErrNone)
       
   963                 {
       
   964                 RDEBUG(" calling HbDeviceMessageBox::question", 0);
       
   965                 bool value = HbDeviceMessageBox::question("Disable Lock?");
       
   966                 RDEBUG("value", value);
       
   967                 if (!value)
       
   968                     errorInProcess = KErrCancel;
       
   969                 }
       
   970             if (errorInProcess == KErrNone)
       
   971                 {
       
   972                 setLockDialog(aReason, 0); // hide temporarilly because AskValidSecCode doesn't get in top of the Lock-icon. Thus, dismiss it.
       
   973                 RDEBUG("calling AskValidSecCode", 0);
       
   974                 errorInProcess = AskValidSecCode(ELockAppDisableDevicelock);
       
   975                 RDEBUG("errorInProcess", errorInProcess);
       
   976                 }
       
   977             if (errorInProcess == KErrNone)
       
   978                 {
       
   979                 setLabelIcon( ELockNotActive);
       
   980                 updateIndicator(ELockNotActive);
       
   981                 publishStatus(ELockNotActive);
       
   982                 }
       
   983             if (errorInProcess != KErrNone)
       
   984                 { // re-lock. For example, if password is wrong
       
   985                 setLockDialog(aReason, 1);
       
   986                 }
       
   987             // this never shows a note
       
   988             }
       
   989             break;
       
   990         case ELockAppOfferKeyguard: // 5
       
   991             {
       
   992             errorInProcess = CheckIfLegal(ret);
       
   993             DebugError(errorInProcess);
       
   994             if (errorInProcess == KErrNone)
       
   995                 {
       
   996                 bool value = HbDeviceMessageBox::question("Enable Keyguard?");
       
   997                 // TODO what about a nice icon?
       
   998                 RDEBUG("value", value);
       
   999                 if (!value)
       
  1000                     errorInProcess = KErrCancel;
       
  1001                 }
       
  1002             if (errorInProcess == KErrNone)
       
  1003                 {
       
  1004                 errorInProcess = TryChangeStatus(ELockAppEnableKeyguard);
       
  1005                 }
       
  1006             // this never shows a note
       
  1007             }
       
  1008             break;
       
  1009         case ELockAppOfferDevicelock: // 6
       
  1010             {
       
  1011             errorInProcess = CheckIfLegal(ret);
       
  1012             DebugError(errorInProcess);
       
  1013             setLockDialog(aReason, 0); // hide temporarilly because AskValidSecCode doesn't get in top of the Lock-icon. Thus, dismiss it.
       
  1014             errorInProcess = AskValidSecCode(ELockAppEnableDevicelock);
       
  1015             if (errorInProcess == KErrNone)
       
  1016                 {
       
  1017                 errorInProcess = TryChangeStatus(ELockAppEnableDevicelock);
       
  1018                 }
       
  1019             // this never shows a note. Perhaps ELockAppEnableDevicelock does.
       
  1020             }
       
  1021             break;
       
  1022         case ELockAppShowKeysLockedNote:
       
  1023             {
       
  1024             iShowKeyguardNote = 1; // this is not sent as parameter, so we need to fake it: ON
       
  1025             showNoteIfRequested( EKeyguardActive);
       
  1026             }
       
  1027             break;
       
  1028         default:
       
  1029             RDEBUG("default", ret);
       
  1030             errorInProcess = KErrNotSupported;
       
  1031             break;
       
  1032 
       
  1033         }
       
  1034     return errorInProcess;
       
  1035     }
       
  1036 /**************************/
       
  1037 int AutolockSrv::setLockDialog(int aReason, int status)
       
  1038     {
       
  1039     RDEBUG("aReason", aReason);
       
  1040     RDEBUG("status", status);
       
  1041     RDEBUG("iDeviceDialogCreated", iDeviceDialogCreated);
       
  1042     TInt secUiOriginatedQuery(ESecurityUIsSecUIOriginatedUninitialized);
       
  1043     TInt err = RProperty::Get(KPSUidSecurityUIs,
       
  1044             KSecurityUIsSecUIOriginatedQuery, secUiOriginatedQuery);
       
  1045     RDEBUG("err", err);
       
  1046     RDEBUG("secUiOriginatedQuery", secUiOriginatedQuery);
       
  1047 
       
  1048     if (status == 0) // hide
       
  1049         {
       
  1050         // secUiOriginatedQuery should be ESecurityUIsSystemLockOriginated . If not, this is not correctly setting it
       
  1051         if (iDeviceDialogCreated > 0)
       
  1052             {
       
  1053             iDeviceDialogCreated = 0;
       
  1054             iDeviceDialog->cancel();
       
  1055             err = iDeviceDialog->error();
       
  1056             RDEBUG("err", err);
       
  1057             TInt err = RProperty::Set(KPSUidSecurityUIs,
       
  1058                     KSecurityUIsSecUIOriginatedQuery,
       
  1059                     ESecurityUIsSecUIOriginatedUninitialized);
       
  1060             RDEBUG("err", err);
       
  1061             }
       
  1062         }
       
  1063     else if (status == 1) // show
       
  1064         {
       
  1065         // secUiOriginatedQuery should be ESecurityUIsSecUIOriginatedUninitialized . If not, the validation is not correctly filtering it
       
  1066         QVariantMap params;
       
  1067         TBool err;
       
  1068 #define ESecUiTypeDeviceLock		0x00100000
       
  1069 #define ESecUiTypeKeyguard			0x00200000
       
  1070 
       
  1071         if (aReason == EKeyguardActive)
       
  1072             params.insert("type", ESecUiTypeKeyguard);
       
  1073         else if (aReason >= EDevicelockActive)
       
  1074             params.insert("type", ESecUiTypeDeviceLock);
       
  1075         else
       
  1076             {
       
  1077             RDEBUG("error. status=1 but aReason", aReason);
       
  1078             }
       
  1079         // no need for title. Icon should be explicit enough
       
  1080         // params.insert("title", "Locked");
       
  1081         if (iDeviceDialogCreated <= 0)
       
  1082             {
       
  1083             RDEBUG("creating iDeviceDialog", 0);
       
  1084             iDeviceDialog = new HbDeviceDialog(HbDeviceDialog::NoFlags, this);
       
  1085             iDeviceDialogCreated = 1;
       
  1086             }
       
  1087         else
       
  1088             {
       
  1089             RDEBUG("raising iDeviceDialog", 0);
       
  1090             // confirm that dialog is present
       
  1091             err = iDeviceDialog->error();
       
  1092             RDEBUG("err", err);
       
  1093             iDeviceDialogCreated = 2;
       
  1094             }
       
  1095         const QString KSecQueryUiDeviceDialog(
       
  1096                 "com.nokia.secuinotificationdialog/1.0");
       
  1097         RDEBUG("pre show", aReason);
       
  1098         err = iDeviceDialog->show(KSecQueryUiDeviceDialog, params); // and continue processing
       
  1099         RDEBUG("post show. err", err);
       
  1100         err = iDeviceDialog->error();
       
  1101         RDEBUG("err", err);
       
  1102         // This won't be needed when screensaver is in place, as the dialogs will be different, and therefore both can be present
       
  1103         // Somehow this should be handled by Orbit, but unfortunatelly they don't allow the same dialog twice
       
  1104         err = RProperty::Set(KPSUidSecurityUIs,
       
  1105                 KSecurityUIsSecUIOriginatedQuery,
       
  1106                 ESecurityUIsSecUIOriginatedUninitialized);
       
  1107         RDEBUG("err", err);
       
  1108 
       
  1109         }
       
  1110     else
       
  1111         {
       
  1112         RDEBUG("unknown status", status);
       
  1113         return KErrNotSupported;
       
  1114         }
       
  1115     return KErrNone;
       
  1116     }
       
  1117 void AutolockSrv::setLabelIcon(int aReason)
       
  1118     {
       
  1119     RDEBUG("aReason", aReason);
       
  1120 
       
  1121     if (aReason == ELockNotActive)
       
  1122         {
       
  1123         mLabelIcon->setVisible(false);
       
  1124         setLockDialog(aReason, 0); // TODO isn't this done already at TryChangeStatus ???
       
  1125         lower();
       
  1126         hide();
       
  1127         }
       
  1128     else if (aReason == EKeyguardActive)
       
  1129         {
       
  1130         mLabelIcon->setVisible(true);
       
  1131         setLockDialog(aReason, 1);
       
  1132         raise(); // not repaint(), not show() , not setVisible(true), not showFullScreen() , not ???
       
  1133         show();
       
  1134         }
       
  1135     else if (aReason == EDevicelockActive)
       
  1136         {
       
  1137         mLabelIcon->setVisible(true);
       
  1138         raise();
       
  1139         show();
       
  1140         }
       
  1141     else
       
  1142         {
       
  1143         RDEBUG("error: aReason", aReason);
       
  1144         }
       
  1145     }
       
  1146 
       
  1147 int AutolockSrv::updateIndicator(int aReason)
       
  1148     {
       
  1149     RDEBUG("aReason", aReason);
       
  1150     QList<QVariant> list;
       
  1151     list.insert(0, 1);
       
  1152     list.insert(1, "aaa");
       
  1153     list.insert(2, 2);
       
  1154 
       
  1155     HbIndicator indicator;
       
  1156     bool success;
       
  1157     if (aReason == ELockNotActive)
       
  1158         success = indicator.deactivate(
       
  1159                 "com.nokia.hb.indicator.autolocksrv.autolocksrv_8/1.0"); // maybe it's already deactivated. Not a problem
       
  1160     else if (aReason == EKeyguardActive)
       
  1161         success = indicator.activate(
       
  1162                 "com.nokia.hb.indicator.autolocksrv.autolocksrv_8/1.0");
       
  1163     else if (aReason == EDevicelockActive)
       
  1164         success = indicator.activate(
       
  1165                 "com.nokia.hb.indicator.autolocksrv.autolocksrv_8/1.0"); // same. We have just 1 indicator
       
  1166     else
       
  1167         success = 0;
       
  1168     RDEBUG("success", success);
       
  1169     return success;
       
  1170     }
       
  1171 
       
  1172 void AutolockSrv::activeKeyguard()
       
  1173     {
       
  1174     // activity while keyguarded. Nothing to do
       
  1175     RDEBUG("0", 0);
       
  1176     }
       
  1177 
       
  1178 void AutolockSrv::notActiveKeyguard()
       
  1179     {
       
  1180     // inactivity. Keyguard should be activated
       
  1181     RDEBUG("0", 0);
       
  1182     int ret = KErrNone;
       
  1183     DebugStatus( iLockStatus);
       
  1184     if (iLockStatus == ELockNotActive) // not possible if it's keyguarded, or locked
       
  1185         {
       
  1186         ret = TryChangeStatus(ELockAppEnableKeyguard);
       
  1187         }
       
  1188     RDEBUG("ret", ret);
       
  1189     }
       
  1190 /* Some activity detected while the deviceLock is enabled */
       
  1191 void AutolockSrv::activeDevicelock()
       
  1192     {
       
  1193     // nothing to do
       
  1194     RDEBUG("0", 0);
       
  1195     }
       
  1196 
       
  1197 void AutolockSrv::notActiveDevicelock()
       
  1198     {
       
  1199     // inactivity. Devicelock should be activated
       
  1200     RDEBUG("0", 0);
       
  1201     int ret = KErrNone;
       
  1202     DebugStatus( iLockStatus);
       
  1203     if (iLockStatus == ELockNotActive || iLockStatus == EKeyguardActive) // not possible if it's locked
       
  1204         {
       
  1205         ret = TryChangeStatus(ELockAppEnableDevicelock);
       
  1206         }
       
  1207     RDEBUG("ret", ret);
       
  1208 
       
  1209     }
       
  1210 
       
  1211 // some key is pressed
       
  1212 bool AutolockSrv::event(QEvent *ev)
       
  1213     {
       
  1214     if (ev)
       
  1215         {
       
  1216         int isSwitchKey = 0;
       
  1217         // on device, this doesn't seem to get the EKeyDeviceF key: only 1ffffff
       
  1218         if (ev->type() == QEvent::KeyPress)
       
  1219             {
       
  1220             QKeyEvent *keyEvent = static_cast<QKeyEvent *> (ev);
       
  1221             qDebug() << QString("KeyPress:%1\n").arg(keyEvent->key(), 0, 16);
       
  1222             qDebug() << keyEvent->key();
       
  1223             qDebug() << EKeyInsert;
       
  1224             qDebug() << (keyEvent->key() & 0xFF);
       
  1225             qDebug() << (EKeyInsert & 0xFF);
       
  1226             if ((keyEvent->key() & 0xFF) == (EKeyInsert & 0xFF))
       
  1227                 {
       
  1228                 qDebug() << "pressed EKeyInsert";
       
  1229                 // only reacts on release, not on press
       
  1230                 isSwitchKey = 1;
       
  1231                 }
       
  1232             if ((keyEvent->key() & 0xFF) == (EKeyTab & 0xFF))
       
  1233                 {
       
  1234                 qDebug() << "pressed EKeyTab";
       
  1235                 }
       
  1236             if ((keyEvent->key() & 0xFF) == (EKeyDeviceF & 0xFF))
       
  1237                 {
       
  1238                 qDebug() << "pressed EKeyDeviceF";
       
  1239                 }
       
  1240             if (keyEvent->key() == 0x1ffffff)
       
  1241                 {
       
  1242                 qDebug() << "pressed EKeyDeviceF-1ffffff";
       
  1243                 isSwitchKey = 1;
       
  1244                 }
       
  1245             }
       
  1246         else if (ev->type() == QEvent::KeyRelease)
       
  1247             {
       
  1248             QKeyEvent *keyEvent = static_cast<QKeyEvent *> (ev);
       
  1249             qDebug()
       
  1250                     << QString("KeyRelease:%1\n").arg(keyEvent->key(), 0, 16);
       
  1251             if (keyEvent->key() == 0x1ffffff)
       
  1252                 {
       
  1253                 RDEBUG("released EKeyDeviceF-1ffffff", 1);
       
  1254                 // isSwitchKey=1; this should happen is   pressed  was not processed (hint: if it is/was in background)
       
  1255                 }
       
  1256             }
       
  1257         RDEBUG("isSwitchKey", isSwitchKey);
       
  1258         if (isSwitchKey)
       
  1259             {
       
  1260             int ret = KErrNone;
       
  1261             DebugStatus( iLockStatus);
       
  1262             if (iLockStatus == ELockNotActive)
       
  1263                 {
       
  1264                 iShowKeyguardNote = 1; // note on enable keyguard
       
  1265                 ret = TryChangeStatus(ELockAppEnableKeyguard); // this should not ask confirmation
       
  1266                 }
       
  1267             else if (iLockStatus == EKeyguardActive)
       
  1268                 {
       
  1269                 iShowKeyguardNote = 1; // note on disable keyguard
       
  1270                 ret = TryChangeStatus(ELockAppDisableKeyguard);
       
  1271                 }
       
  1272             else if (iLockStatus == EDevicelockActive)
       
  1273                 {
       
  1274                 ret = TryChangeStatus(ELockAppDisableDevicelock);
       
  1275                 }
       
  1276             else
       
  1277                 {
       
  1278                 RDEBUG("unknown iLockStatus", iLockStatus);
       
  1279                 }
       
  1280             RDEBUG("ret", ret);
       
  1281             } // isSwitchKey
       
  1282         } // ev
       
  1283     // Process if not done before. For example, redraw or quit
       
  1284     return QWidget::event(ev);
       
  1285     }
       
  1286 
       
  1287 bool AutolockSrv::eventFilter(QObject *o, QEvent *ev)
       
  1288     {
       
  1289     // this never happens
       
  1290     RDEBUG("0", 0);
       
  1291     return QWidget::eventFilter(o, ev);
       
  1292 
       
  1293     }
       
  1294 
       
  1295 void AutolockSrv::subscriberKSettingsAutolockStatusChanged()
       
  1296     {
       
  1297     RDEBUG("0", 0);
       
  1298     QVariant v = subscriberKSettingsAutolockStatus->value(
       
  1299             "/KCRUidSecuritySettings/KSettingsAutolockStatus");
       
  1300     adjustInactivityTimers( KSettingsAutolockStatus);
       
  1301     qDebug() << "AutolockSrv::subscriberKSettingsAutolockStatusChanged" << v;
       
  1302     }
       
  1303 void AutolockSrv::subscriberKSettingsAutoLockTimeChanged()
       
  1304     {
       
  1305     RDEBUG("0", 0);
       
  1306     QVariant v = subscriberKSettingsAutoLockTime->value(
       
  1307             "/KCRUidSecuritySettings/KSettingsAutoLockTime");
       
  1308     adjustInactivityTimers( KSettingsAutoLockTime);
       
  1309     qDebug() << "AutolockSrv::subscriberKSettingsAutoLockTimeChanged" << v;
       
  1310     }
       
  1311 void AutolockSrv::subscriberKSettingsAutomaticKeyguardTimeChanged()
       
  1312     {
       
  1313     RDEBUG("0", 0);
       
  1314     QVariant v = subscriberKSettingsAutomaticKeyguardTime->value(
       
  1315             "/KCRUidSecuritySettings/KSettingsAutomaticKeyguardTime");
       
  1316     adjustInactivityTimers( KSettingsAutoLockTime);
       
  1317     qDebug()
       
  1318             << "AutolockSrv::subscriberKSettingsAutomaticKeyguardTimeChanged"
       
  1319             << v;
       
  1320     }
       
  1321 void AutolockSrv::subscriberKDisplayLightsTimeoutChanged()
       
  1322     {
       
  1323     RDEBUG("0", 0);
       
  1324     QVariant v = subscriberKDisplayLightsTimeout->value(
       
  1325             "/KCRUidLightSettings/KDisplayLightsTimeout");
       
  1326     // nothing to do
       
  1327     qDebug() << "AutolockSrv::subscriberKDisplayLightsTimeoutChanged" << v;
       
  1328     }
       
  1329 void AutolockSrv::subscriberKProEngActiveProfileChanged()
       
  1330     {
       
  1331     RDEBUG("0", 0);
       
  1332     QVariant v = subscriberKProEngActiveProfile->value(
       
  1333             "/KCRUidProfileEngine/KProEngActiveProfile");
       
  1334     // nothing to do
       
  1335     qDebug() << "AutolockSrv::subscriberKProEngActiveProfileChanged" << v;
       
  1336     }
       
  1337 
       
  1338 // ----------AutolockSrvService---------------
       
  1339 
       
  1340 AutolockSrvService::AutolockSrvService(AutolockSrv* parent) :
       
  1341     XQServiceProvider(QLatin1String(
       
  1342             "com.nokia.services.AutolockSrv.AutolockSrv"), parent),
       
  1343             mAutolockSrv(parent), mAsyncReqId(-1), mAsyncAnswer(false)
       
  1344     {
       
  1345     RDEBUG("0", 0);
       
  1346     publishAll();
       
  1347     connect(this, SIGNAL(returnValueDelivered()), parent, SLOT(
       
  1348             handleAnswerDelivered()));
       
  1349     }
       
  1350 
       
  1351 AutolockSrvService::~AutolockSrvService()
       
  1352     {
       
  1353     RDEBUG("0", 0);
       
  1354     }
       
  1355 
       
  1356 void AutolockSrvService::complete(QString number)
       
  1357     {
       
  1358     RDEBUG("0", 0);
       
  1359     if (mAsyncReqId == -1)
       
  1360         return;
       
  1361     XQSERVICE_DEBUG_PRINT("AutolockSrvService::complete");
       
  1362     completeRequest(mAsyncReqId, number.toInt());
       
  1363     }
       
  1364 
       
  1365 // gor API request
       
  1366 int AutolockSrvService::service(const QString& number,
       
  1367         const QString& aParam1, const QString& aParam2)
       
  1368     {
       
  1369     RDEBUG("0", 0);
       
  1370     TInt err = KErrNone;
       
  1371     qDebug() << "number=" << number;
       
  1372     qDebug() << "aParam1=" << aParam1;
       
  1373     qDebug() << "aParam2=" << aParam2;
       
  1374     mAsyncAnswer = false;
       
  1375     XQRequestInfo info = requestInfo();
       
  1376     QSet<int> caps = info.clientCapabilities();
       
  1377 
       
  1378     mAutolockSrv->callerHasECapabilityWriteDeviceData = 0;
       
  1379     if (caps.contains(ECapabilityWriteDeviceData))
       
  1380         {
       
  1381         RDEBUG("callerHasECapabilityWriteDeviceData",
       
  1382                 ECapabilityWriteDeviceData);
       
  1383         mAutolockSrv->callerHasECapabilityWriteDeviceData = 1;
       
  1384         }
       
  1385 
       
  1386     QString label = "AutolockSrv::service:\n";
       
  1387     label += QString("number=%1\n").arg(number);
       
  1388 
       
  1389     mNumber = number;
       
  1390     mAutolockSrv->setLabelNumber(label, number);
       
  1391     int ret = 0;
       
  1392     ret = number.toInt();
       
  1393 
       
  1394     mAutolockSrv->mParam1 = aParam1.toInt();
       
  1395     mAutolockSrv->iShowKeyguardNote = aParam1.toInt();
       
  1396     mAutolockSrv->mParam2 = aParam2.toInt();
       
  1397     TTime myTime;
       
  1398     myTime.HomeTime();
       
  1399     TInt myTimeHigh = 0;
       
  1400     TInt myTimeLow = 0;
       
  1401     err = RProperty::Get(KPSUidSecurityUIs, KSecurityUIsLockInitiatorTimeHigh, myTimeHigh );
       
  1402     RDEBUG("err", err);
       
  1403     RDEBUG("myTimeHigh", myTimeHigh);
       
  1404     err = RProperty::Get(KPSUidSecurityUIs, KSecurityUIsLockInitiatorTimeLow, myTimeLow );
       
  1405     RDEBUG("err", err);
       
  1406     RDEBUG("myTimeLow", myTimeLow);
       
  1407     
       
  1408     myTimeHigh = myTime.Int64() >> 16;
       
  1409     myTimeLow = myTime.Int64() & 0xFFFFFFFF ;
       
  1410     RDEBUG("myTimeHigh", myTimeHigh);
       
  1411     RDEBUG("myTimeLow", myTimeLow);
       
  1412     err = RProperty::Set(KPSUidSecurityUIs, KSecurityUIsLockInitiatorTimeHigh, myTimeHigh );
       
  1413     err = RProperty::Set(KPSUidSecurityUIs, KSecurityUIsLockInitiatorTimeLow, myTimeLow );
       
  1414 
       
  1415 
       
  1416     ret = mAutolockSrv->TryChangeStatus(ret);
       
  1417     RDEBUG("ret", ret);
       
  1418     return ret;
       
  1419     }
       
  1420 
       
  1421 void AutolockSrvService::handleClientDisconnect()
       
  1422     {
       
  1423     RDEBUG("0", 0);
       
  1424     // Just quit service application if client ends
       
  1425     mAsyncAnswer = false;
       
  1426     RDEBUG("0", 0);
       
  1427     // mAutolockSrv->quit();
       
  1428     }
       
  1429 
       
  1430 /****************/
       
  1431 CWait* CWait::NewL()
       
  1432     {
       
  1433     CWait* self = new (ELeave) CWait();
       
  1434     CleanupStack::PushL(self);
       
  1435     self->ConstructL();
       
  1436     CleanupStack::Pop();
       
  1437     return self;
       
  1438     }
       
  1439 void CWait::ConstructL()
       
  1440     {
       
  1441     CActiveScheduler::Add(this);
       
  1442     }
       
  1443 CWait::CWait() :
       
  1444     CActive(0)
       
  1445     {
       
  1446     }
       
  1447 CWait::~CWait()
       
  1448     {
       
  1449     Cancel();
       
  1450     }
       
  1451 TInt CWait::WaitForRequestL()
       
  1452     {
       
  1453     SetActive();
       
  1454     iWait.Start();
       
  1455     return iStatus.Int();
       
  1456     }
       
  1457 void CWait::RunL()
       
  1458     {
       
  1459     if (iWait.IsStarted())
       
  1460         iWait.AsyncStop();
       
  1461     }
       
  1462 void CWait::DoCancel()
       
  1463     {
       
  1464     if (iWait.IsStarted())
       
  1465         iWait.AsyncStop();
       
  1466     }
       
  1467 void CWait::SetRequestType(TInt aRequestType)
       
  1468     {
       
  1469     iRequestType = aRequestType;
       
  1470     }
       
  1471 TInt CWait::GetRequestType()
       
  1472     {
       
  1473     return iRequestType;
       
  1474     }
       
  1475