xdmprotocols/LocalProtocol/LocalOperations/src/LocalOperationBase.cpp
branchGCC_SURGE
changeset 28 d9861ae9169c
parent 23 77cb48a03620
parent 26 04ca1926b01c
equal deleted inserted replaced
23:77cb48a03620 28:d9861ae9169c
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: CLocalOperationBase
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDES
       
    23 #include "XdmXmlParser.h"
       
    24 #include "LocalProtocol.h"
       
    25 #include "LocalDocument.h"
       
    26 #include "LocalOperationBase.h"
       
    27 
       
    28 // ---------------------------------------------------------
       
    29 // C++ constructor can NOT contain any code, that
       
    30 // might leave.
       
    31 // ---------------------------------------------------------
       
    32 //
       
    33 CLocalOperationBase::CLocalOperationBase( const CLocalDocument& aTargetDoc,
       
    34                                           CLocalOperationFactory& aOperationFactory ) :
       
    35                                           iTargetDoc( CONST_CAST( CLocalDocument&, aTargetDoc ) ),
       
    36                                           iOperationFactory( aOperationFactory ) 
       
    37     {
       
    38     }
       
    39 
       
    40 // ---------------------------------------------------------
       
    41 // Destructor
       
    42 //
       
    43 // ---------------------------------------------------------
       
    44 //
       
    45 CLocalOperationBase::~CLocalOperationBase()
       
    46     {
       
    47     iXmlFile.Close();
       
    48     delete iXmlParser;
       
    49     delete iFileManager;
       
    50     }
       
    51 
       
    52 // ---------------------------------------------------------
       
    53 // CLocalOperationBase::BaseConstructL
       
    54 //
       
    55 // ---------------------------------------------------------
       
    56 //
       
    57 void CLocalOperationBase::BaseConstructL()
       
    58     {
       
    59     iXmlParser = CXdmXmlParser::NewL();
       
    60     iFileManager = CFileMan::NewL( CLocalProtocol::FileSession() );
       
    61     }
       
    62     
       
    63 // ---------------------------------------------------------
       
    64 // CLocalOperationBase::WriteFileL
       
    65 //
       
    66 // ---------------------------------------------------------
       
    67 //
       
    68 void CLocalOperationBase::WriteFileL( const CXdmDocumentNode* aRootNode )
       
    69     {
       
    70     __ASSERT_DEBUG( aRootNode != NULL, User::Panic( _L( "CLocalOperationBase" ), 1 ) );
       
    71     HBufC8* data = iXmlParser->FormatToXmlLC( ETrue, &iTargetDoc, aRootNode );
       
    72     User::LeaveIfError( iXmlFile.Write( data->Des() ) );
       
    73     CleanupStack::PopAndDestroy();  //data
       
    74     GenerateTimeStampL();
       
    75     }
       
    76 
       
    77 // ---------------------------------------------------------
       
    78 // CLocalOperationBase::GenerateTimeStampL
       
    79 //
       
    80 // ---------------------------------------------------------
       
    81 //
       
    82 void CLocalOperationBase::GenerateTimeStampL()
       
    83     {
       
    84     RFile timeStamp;
       
    85     HBufC8* time = TimeLC();
       
    86     HBufC* timeStampName = ConstructTimestampNameL();
       
    87     CleanupStack::PushL( timeStampName );
       
    88     User::LeaveIfError( timeStamp.Replace( CLocalProtocol::FileSession(), timeStampName->Des(), EFileWrite ) );
       
    89     User::LeaveIfError( timeStamp.Write( time->Des() ) );
       
    90     timeStamp.Close();
       
    91     CleanupStack::PopAndDestroy( 2 );  //timeStampName, time
       
    92     }
       
    93 
       
    94 // ---------------------------------------------------------
       
    95 // CLocalOperationBase::ConstructTimestampNameL
       
    96 //
       
    97 // ---------------------------------------------------------
       
    98 //
       
    99 HBufC* CLocalOperationBase::ConstructTimestampNameL()
       
   100     {
       
   101     const TChar backSlash = 92;
       
   102     const TChar idSeparator = 46;
       
   103     TInt index = iFullPath.LocateReverse( backSlash );
       
   104     TPtrC path = iFullPath.Left( index + 1 );
       
   105     HBufC* fileName = HBufC::NewLC( iTargetDoc.Name().Length() + KTimeStampFileExt().Length() );
       
   106     HBufC* fullPath = HBufC::NewLC( path.Length() + fileName->Des().MaxLength() );
       
   107     TPtr ptr( fileName->Des() );
       
   108     ptr.Copy( iTargetDoc.Name() );
       
   109     index = ptr.LocateReverse( idSeparator );
       
   110     TPtrC tmspName = index > 0 ? ptr.Left( index ) : ptr;
       
   111     fullPath->Des().Copy( path );
       
   112     fullPath->Des().Append( tmspName );
       
   113     fullPath->Des().Append( KTimeStampFileExt );
       
   114     CleanupStack::Pop();  //fullPath
       
   115     CleanupStack::PopAndDestroy();  //fileName
       
   116     return fullPath;
       
   117     }
       
   118 
       
   119 // ---------------------------------------------------------
       
   120 // CLocalOperationBase::Time
       
   121 //
       
   122 // ---------------------------------------------------------
       
   123 //
       
   124 HBufC8* CLocalOperationBase::TimeLC()
       
   125     {
       
   126     TTime time;
       
   127     TBuf<KDateBufferMaxSize> dateBuffer;
       
   128 	time.HomeTime();
       
   129     time.FormatL( dateBuffer, KDateFormat );
       
   130     TBuf<KDateBufferMaxSize> timeBuffer;
       
   131 	time.HomeTime();
       
   132 	time.FormatL( timeBuffer, KTimeFormat );
       
   133     TBuf<KDateTimeMaxSize> buffer;
       
   134     buffer.SetLength( 0 );
       
   135     buffer.Copy( dateBuffer );
       
   136     buffer.Append( _L( " " ) );
       
   137     buffer.Append( timeBuffer );
       
   138     HBufC8* ret = HBufC8::NewLC( buffer.Length() );
       
   139     ret->Des().Copy( buffer );
       
   140     return ret;        
       
   141     }
       
   142 
       
   143 // ---------------------------------------------------------
       
   144 // CLocalOperationBase::FetchXmlDataL
       
   145 //
       
   146 // ---------------------------------------------------------
       
   147 //
       
   148 HBufC8* CLocalOperationBase::FetchXmlDataL( TInt aFileSize )
       
   149     {
       
   150     HBufC8* data = HBufC8::NewLC( aFileSize );
       
   151     TPtr8 desc( data->Des() );
       
   152     User::LeaveIfError( iXmlFile.Read( desc ) );
       
   153     CleanupStack::Pop();  //data
       
   154     return data;
       
   155     }
       
   156 
       
   157 // ---------------------------------------------------------
       
   158 // CLocalOperationBase::FetchXmlDataL
       
   159 //
       
   160 // ---------------------------------------------------------
       
   161 //
       
   162 TInt CLocalOperationBase::OpenDataFileL( const TDesC& aFilePath )
       
   163     {
       
   164     TInt length = 0;
       
   165     iFullPath.Set( aFilePath );
       
   166     TInt error = iXmlFile.Open( CLocalProtocol::FileSession(), iFullPath,
       
   167                                 EFileRead | EFileWrite | EFileShareExclusive );
       
   168     if( error != KErrNone )
       
   169         {
       
   170         if( error == KErrPathNotFound )
       
   171             User::LeaveIfError( CLocalProtocol::FileSession().MkDirAll( CLocalProtocol::RootFolder() ) );
       
   172         error = iXmlFile.Create( CLocalProtocol::FileSession(), iFullPath,
       
   173                                  EFileRead | EFileWrite | EFileShareExclusive );                                 
       
   174         }
       
   175     error = error == KErrNone ? iXmlFile.Size( length ) : KErrUnknown;
       
   176     return error == KErrNone ? length : error;
       
   177     }
       
   178 
       
   179 // ---------------------------------------------------------
       
   180 // CLocalOperationBase::ReplaceDataFileL
       
   181 //
       
   182 // ---------------------------------------------------------
       
   183 //
       
   184 void CLocalOperationBase::ReplaceDataFileL()
       
   185     {
       
   186     User::LeaveIfError( iXmlFile.Replace( CLocalProtocol::FileSession(), iFullPath,
       
   187                                           EFileRead | EFileWrite | EFileShareExclusive ) );
       
   188     }
       
   189 
       
   190 // ---------------------------------------------------------
       
   191 // CLocalOperationBase::DeleteDataFileL
       
   192 //
       
   193 // ---------------------------------------------------------
       
   194 //
       
   195 void CLocalOperationBase::DeleteDataFileL()
       
   196     {
       
   197     User::LeaveIfError( CLocalProtocol::FileSession().Delete( iFullPath ) );
       
   198     HBufC* timeStampName = ConstructTimestampNameL();
       
   199     CleanupStack::PushL( timeStampName );
       
   200     User::LeaveIfError( CLocalProtocol::FileSession().Delete( timeStampName->Des() ) );
       
   201     CleanupStack::PopAndDestroy();  //timeStampName
       
   202     }      
       
   203 
       
   204 // ---------------------------------------------------------
       
   205 // CLocalOperationBase::ExecuteL
       
   206 //
       
   207 // ---------------------------------------------------------
       
   208 //
       
   209 void CLocalOperationBase::ExecuteL( TRequestStatus& /*aStatus*/, TUint /*aOptions*/ )
       
   210     {
       
   211     }
       
   212         
       
   213 // ---------------------------------------------------------
       
   214 // CLocalOperationBase::CancelOperation
       
   215 //
       
   216 // ---------------------------------------------------------
       
   217 //
       
   218 void CLocalOperationBase::CancelOperation()
       
   219     {
       
   220     }
       
   221 
       
   222 // ---------------------------------------------------------
       
   223 // CLocalOperationBase::CompletionData
       
   224 //
       
   225 // ---------------------------------------------------------
       
   226 //     
       
   227 const TXdmCompletionData& CLocalOperationBase::CompletionData() const
       
   228     {
       
   229     return iCompletionData;
       
   230     }
       
   231         
       
   232 // ---------------------------------------------------------
       
   233 // CLocalOperationBase::IsCompleted
       
   234 //
       
   235 // ---------------------------------------------------------
       
   236 //        
       
   237 TBool CLocalOperationBase::IsCompleted() const
       
   238     {
       
   239     return ETrue;
       
   240     }
       
   241         
       
   242 // ---------------------------------------------------------
       
   243 // CLocalOperationBase::TargetNode
       
   244 //
       
   245 // ---------------------------------------------------------
       
   246 //      
       
   247 CXdmDocumentNode* CLocalOperationBase::TargetNode() const
       
   248     {
       
   249     return NULL;
       
   250     }                      
       
   251 
       
   252 // ---------------------------------------------------------
       
   253 // CLocalOperationBase::Result
       
   254 //
       
   255 // ---------------------------------------------------------
       
   256 //        
       
   257 TInt CLocalOperationBase::Result() const
       
   258     {
       
   259     return KErrNone;
       
   260     }   
       
   261      
       
   262 //  End of File  
       
   263