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