// ----------------------------------------------------------------------------
// WidgetPreferences::SetPreferenceL
// set Preference for a key
//
//
// ----------------------------------------------------------------------------
void WidgetPreferences::setPreferenceL( const TDesC& akey, const TDesC& avalue)
{
    if ( !m_basepath || (m_basepath->Length() <= 0) )
        return;

    if ( akey.Length() <= KMaxKeyValueSize ) {

        HBufC* k = HBufC::NewLC( akey.Length() + KMaxIntLength + 1 );
        k->Des().Format( KKeyFormat, m_widgetid, &akey );

        // if hash has the key and its value
        // delete the old value later when the new value was successfully updated
        PrefElement* prefExisting = NULL;
        prefExisting = m_preferences->Find( *k );

        if ( avalue.Length() <= KMaxKeyValueSize ) {
            PrefElement* pref = new (ELeave) PrefElement;
            CleanupStack::PushL( pref );
            pref->setValueL( avalue );
            pref->setValueSize( avalue.Length() );
            m_preferences->InsertL( k, pref );
            CleanupStack::Pop(); //pref
        }
        else {
            // create a temp file and save the value in temp file.
            // m_value member of PrefElement contains the temp file name.
            RFs fs;
            RFile file;
            if ( fs.Connect() == KErrNone ) {
                CleanupClosePushL( fs );

                // create and write to file
                TFileName tempFileName;
                file.Temp( fs, *m_basepath, tempFileName, EFileWrite|EFileShareExclusive );
                CleanupClosePushL( file );
                HBufC* filePath = HBufC::NewLC( tempFileName.Length() );
                TPtr fName( filePath->Des() );
                fName.Append( tempFileName );
                RFileWriteStream writeStream( file );
                CleanupClosePushL( writeStream );
                TRAPD( err,
                       writeStream.WriteInt32L( avalue.Length() );
                       writeStream.WriteL( avalue );
                       writeStream.CommitL(); );
                // If an error occured while writing to the file, delete the temp file
                // This should be the case when disk is full
                if ( err != KErrNone )
                {
                    CleanupStack::PopAndDestroy( ); //writeStream
                    file.Close();
                    fs.Delete( *filePath );
                    User::Leave( err );
                }

                // create new preference element
                PrefElement* pref = new ( ELeave ) PrefElement;
                CleanupStack::PushL( pref );
                pref->setValueSize( avalue.Length() );
                pref->setValueL( *filePath );
                // update new preference element
                m_preferences->InsertL( k, pref );

                CleanupStack::Pop( pref );
                CleanupStack::PopAndDestroy( ); //writeStream
                CleanupStack::PopAndDestroy( 3 ); //filePath,file,fs
            }
//
// Opens client-side sub-session for a registered script. The script session is modelled as a
// client side sub-session with a peer server side sub-session.
// 
TInt RSecMgrSubSession::Open(const RSessionBase& aSession,
		CScript& aScriptInfo, TPolicyID aPolicyID, const TDesC& aHashValue)
	{
	TIpcArgs args(aScriptInfo.ScriptID (), aPolicyID);

	TInt errCode(KErrNone);
	errCode = iFs.Connect();
	if(errCode == KErrNone)
    	{
    	if ( KAnonymousScript==aScriptInfo.ScriptID ())
    		errCode = CreateSubSession (aSession, EGetTrustedUnRegScriptSession,
    				args);
    	else
    		errCode = CreateSubSession (aSession, EGetScriptSession, args);
    
    	if ( errCode==KErrNone)
    		{
    		// Retrieve the RFs and RFile handles from the server
    		TPckgBuf<TInt> fh; // sub-session (RFile) handle
    		TIpcArgs args(&fh);
    
    		RFile file;
    		CleanupClosePushL(file);
    
    		if ( KErrNone==errCode)
    			{
    			iFs.ShareProtected ();
    
    			TFileName tempDirPath;
    			TFileName tempPath;
    
    			iFs.PrivatePath (tempDirPath);
    			BaflUtils::EnsurePathExistsL (iFs, tempDirPath);
    
    			errCode = file.Temp (iFs, tempDirPath, tempPath, EFileWrite);
    
    			if ( KErrNone==errCode)
    				{
    				file.TransferToServer (args, EMsgArgOne, EMsgArgTwo);
    				errCode = SendReceive (EGetScriptFile, args);
    
    				if ( KErrNone==errCode)
    					{
    					RFileReadStream rfs(file);
    					CleanupClosePushL(rfs);
    					aScriptInfo.InternalizeL (rfs);
    					TBufC<KMaxPath> hashValue(aScriptInfo.Hash());
    					if(0 != hashValue.Compare(KNullDesC))
    						{
    						if(!aScriptInfo.HashMatch(aHashValue))
    							{
    							//hash check failed
    							errCode = KErrNotFound;
    							}						
    						}
    					
    					CleanupStack::PopAndDestroy(&rfs);
    					}
    				}
    			iFs.Delete (tempPath);
    			}
    
    		CleanupStack::PopAndDestroy(&file);
    		}
    	}
	return errCode;
	}