// ----------------------------------------------------------------------------
// 
// ----------------------------------------------------------------------------
//
TBool CWrtHarvester::CheckNetworkAccessL( TUid& aUid )
    {
    RWidgetRegistryClientSession session;
    CleanupClosePushL( session );
    User::LeaveIfError( session.Connect() );
    TBool networkAccess = *(session.GetWidgetPropertyValueL( aUid, EAllowNetworkAccess ) );
    CleanupStack::PopAndDestroy( &session );
    
    return networkAccess;
    }
 TInt CWidgetRegistrySTIF::ClientSessionDisconnectFirstL( TTestResult& aResult )
 {
 aResult.SetResult( KErrGeneral, KFailed );
 RWidgetRegistryClientSession* registryClient;
 registryClient = new (ELeave) RWidgetRegistryClientSession;
 CleanupStack::PushL( registryClient );
     TInt error = registryClient->Disconnect();
     if ( KErrNone == error )
         {
         aResult.SetResult( KErrNone, KPassed );
         }
 CleanupStack::PopAndDestroy( registryClient );
 return KErrNone;
 }
// ----------------------------------------------------------------------------
// 
// ----------------------------------------------------------------------------
//
void CWrtHarvester::QueueResumeL( TUid& aUid )
    {
    RWidgetRegistryClientSession session;
    CleanupClosePushL( session );
    User::LeaveIfError( session.Connect() );
    if ( session.IsBlanketPermGranted ( aUid ) == EBlanketUnknown && !iDialogShown )
        {
        iDialogShown = ETrue;            
        AllowPlatformAccessL( aUid );
        }
    else if(!iDialogShown)
        {
        QueueOperationL( WidgetResume, aUid );
        }        
    CleanupStack::PopAndDestroy( &session );
    }
TInt CWidgetRegistrySTIF::ClientSessionGetAvailableUidL( TTestResult& aResult )
    {
    aResult.SetResult( KErrGeneral, KFailed );
    RWidgetRegistryClientSession* registryClient;
    registryClient = new (ELeave) RWidgetRegistryClientSession;
    CleanupStack::PushL( registryClient );
    TInt error = registryClient->Connect();
    if ( KErrNone == error )
        {
        TRAP( error,
        TUid uid = registryClient->GetAvailableUidL( 'C' );
        if ( KNullUid != uid )
            {
            aResult.SetResult( KErrNone, KPassed );
            }
        )
// ------------------------------------------------------------------------
// CGlobalQueryHandlerAO::CGlobalQueryHandlerAO
//
// ISet network and platform access permission based on user response.
// ------------------------------------------------------------------------
void CGlobalQueryHandlerAO::RunL()
    {    
    RWidgetRegistryClientSession session;
    CleanupClosePushL( session );
    User::LeaveIfError( session.Connect() );  
    if (iStatus == EAknSoftkeyOk)
        {
        session.SetBlanketPermissionL( iWidgetUid, EBlanketTrue );
        }
    else if ( iStatus == EAknSoftkeyCancel)
        {
        session.SetBlanketPermissionL( iWidgetUid, EBlanketFalse );
        }
    iWrtHarvester->QueueOperationL( WidgetResume, iWidgetUid );
    iWrtHarvester->DialogShown();
    CleanupStack::PopAndDestroy( &session );
    
    delete this;
    }
TInt CWidgetRegistrySTIF::ClientSessionGetWidgetUidForUrlL( TTestResult& aResult )
    {
    aResult.SetResult( KErrGeneral, KFailed );
    RWidgetRegistryClientSession* registryClient;
    registryClient = new (ELeave) RWidgetRegistryClientSession;
    CleanupStack::PushL( registryClient );
    TInt error = registryClient->Connect();
    if ( KErrNone == error )
        {
        TInt uid = registryClient->GetWidgetUidForUrl( KNullDesC );
        if ( KNullUid == TUid::Uid( uid ) )
            {
            aResult.SetResult( KErrNone, KPassed );
            }
        registryClient->Disconnect();
        }
    CleanupStack::PopAndDestroy( registryClient );
    return KErrNone;
    }
 TInt CWidgetRegistrySTIF::ClientSessionIsWidgetRunningInvalidL( TTestResult& aResult )
 {
 aResult.SetResult( KErrGeneral, KFailed );
 RWidgetRegistryClientSession* registryClient;
 registryClient = new (ELeave) RWidgetRegistryClientSession;
 CleanupStack::PushL( registryClient );
 TInt error = registryClient->Connect();
 if ( KErrNone == error )
     {
     TBool b = registryClient->IsWidgetRunning( KInvalidUid );
     if ( b == EFalse )
         {
         aResult.SetResult( KErrNone, KPassed );
         }
     registryClient->Disconnect();
     }
 CleanupStack::PopAndDestroy( registryClient );
 return KErrNone;
 }
 TInt CWidgetRegistrySTIF::ClientSessionWidgetExistsOverflowL( TTestResult& aResult )
 {
 aResult.SetResult( KErrGeneral, KFailed );
 RWidgetRegistryClientSession* registryClient;
 registryClient = new (ELeave) RWidgetRegistryClientSession;
 CleanupStack::PushL( registryClient );
 TInt error = registryClient->Connect();
 if ( KErrNone == error )
     {
     TBool b = registryClient->WidgetExistsL( KLong );
     if ( EFalse == b )
         {
         aResult.SetResult( KErrNone, KPassed );
         }
     registryClient->Disconnect();
     }
 CleanupStack::PopAndDestroy( registryClient );
 return KErrNone;
 }
TInt CWidgetRegistrySTIF::ClientSessionVersionL( TTestResult& aResult )
    {
    aResult.SetResult( KErrGeneral, KFailed );
    RWidgetRegistryClientSession* registryClient;
    registryClient = new (ELeave) RWidgetRegistryClientSession;
    CleanupStack::PushL( registryClient );
    TInt error = registryClient->Connect();
    if ( KErrNone == error )
        {
        TVersion version = registryClient->Version();
        if ( 0 < (version.iMajor + version.iMinor + version.iBuild) )
            {
            aResult.SetResult( KErrNone, KPassed );
            }
        registryClient->Disconnect();
        }
    CleanupStack::PopAndDestroy( registryClient );
    return KErrNone;
    }
// -----------------------------------------------------------------------------
// CWidgetRegistrySTIF::ConstructL
// Symbian 2nd phase constructor can leave.
// Note: If OOM test case uses STIF Logger, then STIF Logger must be created
// with static buffer size parameter (aStaticBufferSize). Otherwise Logger
// allocates memory from heap and therefore causes error situations with OOM
// testing. For more information about STIF Logger construction, see STIF Users
// Guide.
// -----------------------------------------------------------------------------
//
void CWidgetRegistrySTIF::ConstructL()
    {
    iLog = CStifLogger::NewL( KWidgetRegistrySTIFLogPath,
                              KWidgetRegistrySTIFLogFile );

    User::LeaveIfError( iFileSystem.Connect() );
    
    RWidgetRegistryClientSession* registryClient;
    registryClient = new (ELeave) RWidgetRegistryClientSession;
    CleanupStack::PushL( registryClient );
    TInt error = registryClient->Connect();
    if ( KErrNone == error )
        {
        RWidgetInfoArray widgetInfoArray;
        error = registryClient->InstalledWidgetsL( widgetInfoArray );
        CWidgetInfo *myInfo = widgetInfoArray[0];
        iValidUid = myInfo->iUid;
        }
     CleanupStack::PopAndDestroy( registryClient ); 
    }
TInt CWidgetRegistrySTIF::ClientSessionGetWidgetPathL( TTestResult& aResult )
    {
    aResult.SetResult( KErrGeneral, KFailed );
    RWidgetRegistryClientSession* registryClient;
    registryClient = new (ELeave) RWidgetRegistryClientSession;
    CleanupStack::PushL( registryClient );
    TInt error = registryClient->Connect();
    if ( KErrNone == error )
        {
        TFileName fn;
        fn.SetLength( 0 );
        registryClient->GetWidgetPath( iValidUid, fn );
        if ( 0 < fn.Length() )
            {
            aResult.SetResult( KErrNone, KPassed );
            }
        registryClient->Disconnect();
        }
    CleanupStack::PopAndDestroy( registryClient );
    return KErrNone;
    }
TInt CWidgetRegistrySTIF::ClientSessionWidgetCountL( TTestResult& aResult )
    {
    aResult.SetResult( KErrGeneral, KFailed );
    RWidgetRegistryClientSession* registryClient;
    registryClient = new (ELeave) RWidgetRegistryClientSession;
    CleanupStack::PushL( registryClient );
    TInt error = registryClient->Connect();
    if ( KErrNone == error )
        {
        TInt count = -1;
        TInt count2 = count - 1;
        count2 = registryClient->WidgetCount( count );
        if ( -1 < count && count == count2 )
            {
            aResult.SetResult( KErrNone, KPassed );
            }
        registryClient->Disconnect();
        }
    CleanupStack::PopAndDestroy( registryClient );
    return KErrNone;
    }
void WidgetFinalizeInstallation::execute()
{
    QString widgetId = m_WidgetInfo[ EPropertyWidgetId ].toString();
        
    if( (widgetId.isEmpty()) )
    {
        emit aborted( EErrorUpdateGeneral );
        return;
    }
    
    if (WebAppRegistry::instance()->isRegistered(widgetId)) 
    {
        TInt64 isInMiniview;        
        TBuf<120> bundleId(widgetId.utf16());
        TUid aUid;
    
        RWidgetRegistryClientSession clientReg;
        if( (clientReg.Connect() ) != KErrNone )
        {
            emit aborted( EErrorUpdateGeneral );
            return;
        }
        
        aUid.iUid = clientReg.GetWidgetUidL(bundleId);
        isInMiniview = clientReg.IsWidgetInMiniView(aUid);
    
        if (isInMiniview) 
            addSuboperation( new WidgetHomeScreenIntegration( m_WidgetInfo, aUid ) );
    }
    
    addSuboperation( new WidgetSetupInstallationDirectory( m_WidgetInfo ) );
    
    addSuboperation( new WidgetRegistration( m_WidgetInfo ) );
    
    addSuboperation( new WidgetCleanup( m_WidgetInfo ) );
    
    emit completed();
}
bool WidgetRegistrationS60::registerAppL(const QString& appId,
                                         const QString& appTitle,
                                         const QString& appPath,
                                         const QString& dataPath,
                                         const QString& iconPath,
                                         const AttributeMap& attr,
                                         const QString& type,
                                         unsigned long size,
                                         const QString& startPath,
                                         int& widgetUid,
                                         QString& convertedIconPath,
                                         bool hideIcon)
{
    LOG("WidgetRegistrationS60::registerAppL()" << " appId : " << appId << " appTitle : " << 
            appTitle << " appPath : " << appPath << " iconPath : " << iconPath << " startPath : " << 
            startPath);

    if (appId.isEmpty() || appTitle.isEmpty() || appPath.isEmpty())
        return false;

    // S60 requires widgetProps as CWidgetPropertyValue
    RPointerArray<CWidgetPropertyValue> propertyValues(EWidgetPropertyIdCount);
    CleanupStack::PushL(TCleanupItem(PointerArrayCleanup, &propertyValues)); // pushed 1

    RWidgetRegistryClientSession registryClient;
    User::LeaveIfError(registryClient.Connect());
    CleanupClosePushL( registryClient ); // pushed 2

    RFs rfs;
    User::LeaveIfError(rfs.Connect());
    CleanupClosePushL(rfs); // pushed 3
    User::LeaveIfError(rfs.ShareProtected());

    // empty values
    for (TInt i = 0; i < EWidgetPropertyIdCount; ++i) {
        CWidgetPropertyValue* value = CWidgetPropertyValue::NewL();
        CleanupStack::PushL(value); // pushed 4
        propertyValues.AppendL(value);
        CleanupStack::Pop(value); // pushed 3
    }

    *(propertyValues[EWidgetPropertyListVersion]) = KWidgetPropertyListVersion71CWRT;
    *(propertyValues[EFileSize]) = size;

    SwiUI::CWidgetRegistrationS60Apparc* appArc = SwiUI::CWidgetRegistrationS60Apparc::NewL(rfs);
    CleanupStack::PushL(appArc); // pushed 4

    // get drive letter from appPath
    TUint driveLetter = appPath[0].unicode();

    // Generate our UID based on UIDs in the Qt WidgetRegistry
    int iUid = WebAppRegistry::instance()->nextAvailableUid();
    widgetUid = iUid;

    if (iUid == 0) {
        LOG("WidgetRegistrationS60::registerAppL() - registryClient.GetAvailableUidL() failed");
        CleanupStack::PopAndDestroy( 4, &propertyValues );
        return false;
    }

    // convert icon to required format and sizes
    QString newIconPath = "";
    if (!processIconL(iUid, newIconPath, iconPath, QDir::toNativeSeparators(dataPath))) {
        LOG("WidgetRegistrationS60::registerAppL() - processIconL() failed");
        CleanupStack::PopAndDestroy( 4, &propertyValues );
        return false;
        }

    // FIXME this translation doesn't cover all cases, if generalized
    // must cover S60 WRT names and W3C names
    QString appPathNative = QDir::toNativeSeparators(appPath);
    // FIXME enforce canonicalization in caller
    // must end in QDir::separator()
    if (QDir::separator() != appPathNative.at(appPathNative.count()-1)) {
        appPathNative.append(QDir::separator());
    }
    TPtrC16 basePathSymbian(reinterpret_cast<const TUint16*>
                            (appPathNative.constData()));
    *(propertyValues[EBasePath]) = basePathSymbian;

    QString driveName = appPathNative.left(2);
    TPtrC16 driveNameSymbian(reinterpret_cast<const TUint16*>
                             (driveName.constData()));
    *(propertyValues[EDriveName]) = driveNameSymbian;

    TPtrC16 mainHtmlSymbian(reinterpret_cast<const TUint16*>
                            (startPath.constData()));
    *(propertyValues[EMainHTML]) = mainHtmlSymbian;

    TPtrC16 identifierSymbian(reinterpret_cast<const TUint16*>
                              (appId.constData()));
    *(propertyValues[EBundleIdentifier]) = identifierSymbian;

    if (attr.contains(W3CSettingsKey::WIDGET_VERSION)) {
        QString ver = attr.value(W3CSettingsKey::WIDGET_VERSION).toString();
        if(!(ver.isEmpty())) {
            TPtrC16 version(reinterpret_cast<const TUint16*>
                            (attr.value(W3CSettingsKey::WIDGET_VERSION).toString().constData()));
            *(propertyValues[EBundleVersion]) = version;
        }
    }

    if (appTitle.isEmpty()) {
        // FIXME this probably should cause registration failure
        *(propertyValues[EBundleDisplayName]) = identifierSymbian;
    } else {
        TPtrC16 titleSymbian(reinterpret_cast<const TUint16*>
                             (appTitle.constData()));
        *(propertyValues[EBundleDisplayName]) = titleSymbian;
    }

    // TODO: We decided to drop BundleName and just use
    // DisplayName but the registry code has errors in it and uses
    // BundleName when it should use DisplayName so as a workaround,
    // set BundleName to DisplayName.  Should eventually remove
    // BundleName from set of registry values.
    const TDesC& name = *(propertyValues[EBundleDisplayName]);
    *(propertyValues[EBundleName]) = name;

    convertedIconPath = newIconPath;

    if (!newIconPath.isEmpty()) {
        // FIXME enforce canonicalization in caller
        // strangely icon path doesn't include icon file name
        int li = newIconPath.lastIndexOf(QDir::separator());
        if (li > 0) {
            newIconPath = newIconPath.left(li+1);
            TPtrC16 iconPathSymbian(reinterpret_cast<const TUint16*>
                                    (newIconPath.constData()));
            *(propertyValues[EIconPath]) = iconPathSymbian;
        }
    }

    *(propertyValues[EUid]) = iUid;

    *(propertyValues[EMiniViewEnable]) = 0;
    if (attr.contains(W3CSettingsKey::WIDGET_VIEWMODES)) {
        QStringList viewModeList = attr.value(W3CSettingsKey::WIDGET_VIEWMODES).toString().split(" ");
        foreach (const QString &str, viewModeList) {
            if (str.contains("minimized", Qt::CaseInsensitive)) {
                *(propertyValues[EMiniViewEnable]) = 1;
                break;
            }
        }
    }