serviceproviders/sapi_calendar/calendarservice/src/calendarexport.cpp
changeset 19 989d2f495d90
child 33 50974a8b132e
equal deleted inserted replaced
14:a36b1e19a461 19:989d2f495d90
       
     1 /*
       
     2 * Copyright (c) 2007 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 the License "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:  Interface to Exporting the contents of a Calender
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //System Includes
       
    20 #include <e32cmn.h> 		//for RPointerArray
       
    21 #include <s32file.h>
       
    22 #include <calsession.h>
       
    23 #include <calentryview.h>
       
    24 #include <CalenExporter.h>
       
    25 #include <calentryview.h>
       
    26 #include <caliterator.h>
       
    27 
       
    28 //User Include
       
    29 #include "calendarheader.h"
       
    30 #include "calendarconstants.h"
       
    31 #include "asyncreqobserver.h"
       
    32 #include "CalendarExport.h"
       
    33 
       
    34 
       
    35 void CleanupCCalEntryArray(TAny* aPointer);
       
    36 void CleanupCCalInstanceArray(TAny* aPointer);
       
    37 
       
    38 
       
    39 //used in case of writing the temporary output while exporting the entries
       
    40 _LIT(KTempFilePath,"C:\\");
       
    41 
       
    42 //--------------------------------------------------------------------------------------------------------
       
    43 // Static Method which either returns the Two Phase constructed Object or Leave 
       
    44 // Used to set local copies of some of the parameters like aFormat to iFormat
       
    45 //--------------------------------------------------------------------------------------------------------
       
    46 //
       
    47 CCalendarExport* CCalendarExport::NewL ( CCalendarSessionInfo* aCalSessionInfo, 
       
    48 										 const TDesC8& aFormat, 
       
    49 										 CCalendarExportParams* aParams, 
       
    50 										 CAsyncRequestObserver* aAsyncRequestObserver,
       
    51 										 MCalCallbackBase* aCallBack )
       
    52 	{
       
    53 	CCalendarExport* self = new (ELeave) CCalendarExport( aCalSessionInfo, aAsyncRequestObserver, aCallBack );
       
    54 	
       
    55     CleanupStack::PushL(self);
       
    56     
       
    57     self->ConstructL( aFormat, aParams );
       
    58     
       
    59     CleanupStack::Pop( self );
       
    60     
       
    61     return self;
       
    62 	}
       
    63 
       
    64 
       
    65 // --------------------------------------------------------------------------------------------------------
       
    66 // Destructor.
       
    67 // --------------------------------------------------------------------------------------------------------
       
    68 //
       
    69 CCalendarExport::~CCalendarExport()
       
    70 	{
       
    71 	Cancel();
       
    72 	
       
    73 	delete iCalenExporter;
       
    74 	
       
    75 	delete iFormat;
       
    76 	
       
    77 	delete iOutputBuffer;
       
    78 	
       
    79 	if ( iAsyncRequestObserver )
       
    80 		delete iParams;	
       
    81 	}
       
    82 
       
    83 // --------------------------------------------------------------------------------------------------------
       
    84 // Synchronous Export Function which decides the flow basing on the inputs set at the time of Construction 
       
    85 // of this object through NewL and generates corresponding entries in an Ouput Buffer
       
    86 // --------------------------------------------------------------------------------------------------------
       
    87 //
       
    88 void CCalendarExport::ExportL( HBufC8*& aOutputBuffer )
       
    89 	{
       
    90 	if( !iParams )
       
    91 			User::Leave( KErrArgument );
       
    92 	
       
    93 	if( iParams->Params() & CCalendarExportParams::EParamsGUid && 
       
    94 			iParams->Params() & CCalendarExportParams::EParamsLUid)
       
    95 		{
       
    96 		User::Leave( KErrArgument );		
       
    97 		}
       
    98 	else if( iParams->Params() & CCalendarExportParams::EParamsGUid )
       
    99 		{
       
   100 		ConvertGUIDAndExportL( aOutputBuffer );
       
   101 		}
       
   102 	else if( iParams->Params() & CCalendarExportParams::EParamsLUid )
       
   103 		{
       
   104 		ConvertLUIDAndExportL( aOutputBuffer );
       
   105 		}
       
   106 	else
       
   107 		{
       
   108 		GetCalEntriesAndExportL( aOutputBuffer );
       
   109 		}
       
   110 	}
       
   111 
       
   112 
       
   113 // --------------------------------------------------------------------------------------------------------
       
   114 // ASynchronous Export Function which actually sets the ActiveObject up in the Active Schedular
       
   115 // 				Exports entries basing on the input set at the time of construction of this object througn NewL
       
   116 //						all the entries of the Calender(outputis passed through callback)
       
   117 // --------------------------------------------------------------------------------------------------------
       
   118 //
       
   119 void CCalendarExport::ExportL()
       
   120 	{
       
   121 	if( !iAsyncRequestObserver || !iCallBack )
       
   122 		User::Leave( KErrArgument );
       
   123 		
       
   124 	CActiveScheduler::Add ( this );
       
   125 	
       
   126 	ActivateRequest( KErrNone );
       
   127 	}
       
   128 	
       
   129 	
       
   130 // --------------------------------------------------------------------------------------------------------
       
   131 // Constructor
       
   132 // --------------------------------------------------------------------------------------------------------
       
   133 //
       
   134 CCalendarExport::CCalendarExport( CCalendarSessionInfo* aCalSessionInfo, CAsyncRequestObserver* aAsyncRequestObserver, 
       
   135 												MCalCallbackBase* aCallBack ): 
       
   136 								  					//CActive( EPriorityStandard ),
       
   137 								  					iCalSessionInfo( aCalSessionInfo ),
       
   138 								  					iCallBack( aCallBack ),
       
   139 								  					iAsyncRequestObserver( aAsyncRequestObserver )
       
   140 	{
       
   141 	}
       
   142 	
       
   143 	
       
   144 // --------------------------------------------------------------------------------------------------------
       
   145 // 2nd-phased constructor of two phase construction
       
   146 // NOTE: Ownership of any of the parameters is not taken through this function call
       
   147 // --------------------------------------------------------------------------------------------------------
       
   148 //	
       
   149 void CCalendarExport::ConstructL( const TDesC8& aFormat, CCalendarExportParams* aParams )
       
   150 	{	
       
   151 	if ( iAsyncRequestObserver )
       
   152 		{
       
   153 		iParams = aParams->CloneL();
       
   154 		}
       
   155 	else	
       
   156 		{
       
   157 		iParams = aParams;
       
   158 		}
       
   159 	
       
   160 	iFormat = aFormat.AllocL();
       
   161 	
       
   162 	iCalenExporter = CCalenExporter::NewL( * ( iCalSessionInfo->Session() ) );
       
   163 	}
       
   164 
       
   165 // --------------------------------------------------------------------------------------------------------
       
   166 // Private Export Function which is internally called (by both Overloaded 
       
   167 // synchronous functions) to export the ccalentries to OutputBuffer
       
   168 // --------------------------------------------------------------------------------------------------------
       
   169 //
       
   170 void CCalendarExport::ExportToBufferL( const RPointerArray<CCalEntry>& aCalEntryArray , HBufC8*& aOutputBuffer )
       
   171 	{
       
   172 	TFileName tempFileName;
       
   173 	
       
   174 	TPtrC filePath( KTempFilePath );
       
   175 	
       
   176 	RFs fileServer;
       
   177     
       
   178     User::LeaveIfError( fileServer.Connect() );
       
   179     
       
   180     CleanupClosePushL( fileServer );
       
   181 	
       
   182 	//writeStream for the Export to write to
       
   183 	RFileWriteStream wFileStream;
       
   184 	
       
   185 	wFileStream.PushL();
       
   186 	
       
   187 	if ( iParams->Params() & CCalendarExportParams::EParamsFileName )
       
   188 		{
       
   189 		User::LeaveIfError( wFileStream.Replace(fileServer, iParams->ExportFileName(), EFileWrite ));
       
   190 		}
       
   191 	else
       
   192 		{
       
   193 		//opens the temp file for reading and writing and returns its name in tempFileName
       
   194 		User::LeaveIfError( wFileStream.Temp( fileServer, filePath , tempFileName , EFileWrite ));
       
   195 		}
       
   196 	
       
   197 	TInt calEntryArrayCount = aCalEntryArray.Count();	
       
   198 	
       
   199 	//Step2: Exporting the Entries to wFileStream(Temporary file) 
       
   200 	if ( iFormat->CompareF(KCalFmtVCal) == 0 )
       
   201 		{
       
   202 		for(TInt j = 0 ; j < calEntryArrayCount ; ++j )
       
   203 			{			
       
   204 			//As the current Implementation of ExportVCal does not support taking an array of CCalEntries at a time and 
       
   205 			//returning the result in the writeStream....Only one entry is supported at a time.
       
   206 			//Though it provides the overloaded functions one taking an entry at a time and the other taking array at a time
       
   207 			//the One that take an array at a time Leaves if the array length is greater than 1
       
   208 			
       
   209 			iCalenExporter->ExportVCalL( *aCalEntryArray[j], wFileStream );
       
   210 			}
       
   211 		}
       
   212 	else if ( iFormat->CompareF(KCalFmtICal) == 0 )
       
   213 		{
       
   214 		for(TInt j = 0 ; j < calEntryArrayCount ;++j )
       
   215 			{			
       
   216 			//As the current Implementation of ExportVCal does not support taking an array of CCalEntries at a time and 
       
   217 			//returning the result in the writeStream....Only one entry is supported at a time.
       
   218 			//Though it provides the overloaded functions one taking an entry at a time and the other taking array at a time
       
   219 			//the One that take an array at a time Leaves if the array length is greater than 1
       
   220 			
       
   221 			iCalenExporter->ExportICalL( *aCalEntryArray[j], wFileStream );
       
   222 			}
       
   223 		}
       
   224 	else
       
   225 		{
       
   226 		User::Leave( KErrNotSupported );
       
   227 		}	
       
   228 	
       
   229 	wFileStream.CommitL();
       
   230 	
       
   231 	wFileStream.Close();
       
   232 	
       
   233 	wFileStream.Pop();
       
   234 	
       
   235 	wFileStream.Release();
       
   236 	
       
   237 	//wStream( temporary file ) contains the entries exported from the calender...
       
   238 	//Coping the content to the aOutputBuffer
       
   239 
       
   240 	if ( !( iParams->Params() & CCalendarExportParams::EParamsFileName ))
       
   241 		{
       
   242 		RFile file;
       
   243 		
       
   244 		TInt fileSize;
       
   245 		
       
   246 		User::LeaveIfError( file.Open( fileServer, tempFileName, EFileRead ));
       
   247 		
       
   248 	    CleanupClosePushL( file );
       
   249 		
       
   250 		file.Size( fileSize );
       
   251 
       
   252 		aOutputBuffer = HBufC8::NewL( fileSize );
       
   253 		
       
   254 		TPtr8 temp = aOutputBuffer->Des();
       
   255 		
       
   256 		//reads all the contents of the temporary file into the descriptor and its length is set accoordingly
       
   257 		file.Read( temp );
       
   258 		
       
   259 		CleanupStack::PopAndDestroy( &file );//file
       
   260 		
       
   261 		//deleting the temporary file using the RFs Object 
       
   262 		fileServer.Delete( tempFileName );
       
   263 		}
       
   264 	
       
   265 	CleanupStack::PopAndDestroy( &fileServer );//fileServer
       
   266 	}
       
   267 
       
   268 
       
   269 // --------------------------------------------------------------------------------------------------------
       
   270 // Internal Function For Conversion of LocalUIDS to CCalEntry and call ExportToBuffer
       
   271 // (this is called in case of Asynchronous from RUNL)
       
   272 // --------------------------------------------------------------------------------------------------------
       
   273 //          
       
   274 void CCalendarExport::GetCalEntriesAndExportL( HBufC8*& aOutputBuffer )
       
   275 	{
       
   276 	
       
   277 	RPointerArray<CCalEntry> calEntryArray;
       
   278 	 //to store all the calendery entries correspongind to the Input LocalUID Array
       
   279 
       
   280 	CleanupStack::PushL( TCleanupItem(CleanupCCalEntryArray, &calEntryArray) );
       
   281 	
       
   282 	//Step1:Retrieving all the CCalEntryArray from the Calendar
       
   283 
       
   284 	CCalIter* calIter = CCalIter::NewL( *iCalSessionInfo->Session() );
       
   285 	
       
   286 	CleanupStack::PushL( calIter );
       
   287 	
       
   288 	TPtrC8 calEntryUID(calIter->FirstL());
       
   289 	
       
   290 	while( calEntryUID != KNullDesC8 )
       
   291 			{
       
   292 			//fetch the total array of entries at once for the first uid
       
   293 			iCalSessionInfo->EntryView()->FetchL( calEntryUID , calEntryArray );
       
   294 			
       
   295 			calEntryUID.Set(calIter->NextL());
       
   296 			}
       
   297 			
       
   298 	//Step2: Exporting the Entries to writeStream(Temporary file) and copying the contents to the aOutputBuffer
       
   299 	
       
   300 	ExportToBufferL( calEntryArray, aOutputBuffer );
       
   301 	
       
   302 	CleanupStack::PopAndDestroy( 2, &calEntryArray );
       
   303 	}
       
   304 	
       
   305 	
       
   306 // --------------------------------------------------------------------------------------------------------
       
   307 // Internal Function For Conversion of GlobalUIDS to CCalEntry and call ExportToBuffer
       
   308 // (this is called in case of Asynchronous from RUNL)
       
   309 // --------------------------------------------------------------------------------------------------------
       
   310 //	
       
   311 void CCalendarExport::ConvertGUIDAndExportL( HBufC8*& aOutputBuffer )
       
   312 	{
       
   313 	RPointerArray< CCalEntry > calEntryArray; 
       
   314 	//to store all the calendery entries correspongind to the Input GUID Array
       
   315 	CleanupStack::PushL( TCleanupItem(CleanupCCalEntryArray, &calEntryArray) );
       
   316 	
       
   317 	//Step1: Generating the CCalEntryArray corresponding to input GUID(Descriptor..alpha numberic) Array
       
   318 	
       
   319 	TInt inputArrayCount = iParams->GuidArray()->Count();//MdcaCount(); this can also be used
       
   320 	
       
   321 	for ( TInt i = 0; i < inputArrayCount; ++i ) 
       
   322 		{	
       
   323 		iCalSessionInfo->EntryView()->FetchL( (*iParams->GuidArray())[i] , calEntryArray );
       
   324 		}
       
   325 		
       
   326 	
       
   327 	//Step2: Exporting the Entries to writeStream(Temporary file) and copying the contents to the aOutputBuffer
       
   328 	
       
   329 	ExportToBufferL( calEntryArray , aOutputBuffer );
       
   330 	
       
   331 	CleanupStack::PopAndDestroy( &calEntryArray );
       
   332 	}
       
   333 		
       
   334 
       
   335 // --------------------------------------------------------------------------------------------------------
       
   336 // Internal Function For Conversion of LocalUIDS to CCalEntry and call ExportToBuffer
       
   337 // (this is called in case of Asynchronous from RUNL)
       
   338 // --------------------------------------------------------------------------------------------------------
       
   339 //          
       
   340 void CCalendarExport::ConvertLUIDAndExportL( HBufC8*& aOutputBuffer )
       
   341 	{
       
   342 	
       
   343 	RPointerArray<CCalEntry> calEntryArray; //needs this to be pushed onto the stack But little support to do this
       
   344 	//to store all the calendery entries correspongind to the Input LocalUID Array
       
   345 	CleanupStack::PushL( TCleanupItem(CleanupCCalEntryArray, &calEntryArray) );
       
   346 	
       
   347 	//Step1:Generating the CCalEntryArray corresponding to input LocalUID(unsigned int) Array
       
   348 	
       
   349 	TInt inputArrayCount = iParams->LocalUidArray().Count();
       
   350 	
       
   351 	for(TInt i = 0; i < inputArrayCount ; ++i) 
       
   352 		{
       
   353 		CCalEntry* entry = iCalSessionInfo->EntryView()->FetchL( iParams->LocalUidArray()[i] );
       
   354 		if ( entry )
       
   355 			{
       
   356 			CleanupStack::PushL( entry );
       
   357 			
       
   358 			calEntryArray.AppendL( entry );
       
   359 			
       
   360 			CleanupStack::Pop( entry );
       
   361 			}
       
   362 		}
       
   363 		
       
   364 		
       
   365 	//Step2: Exporting the Entries to writeStream(Temporary file) and copying the contents to the aOutputBuffer
       
   366 	
       
   367 	ExportToBufferL( calEntryArray, aOutputBuffer );
       
   368 	
       
   369 	CleanupStack::PopAndDestroy( &calEntryArray );
       
   370 	}
       
   371 			
       
   372 // --------------------------------------------------------------------------------------------------------
       
   373 // Inherited from CActive class 
       
   374 // --------------------------------------------------------------------------------------------------------
       
   375 //
       
   376 void CCalendarExport::DoCancel()
       
   377 	{
       
   378 	NotifyRequestResult( KErrCancel );
       
   379 	}
       
   380 
       
   381 
       
   382 // --------------------------------------------------------------------------------------------------------
       
   383 // Inherited from CActive class 
       
   384 // --------------------------------------------------------------------------------------------------------
       
   385 //
       
   386 void CCalendarExport::RunL()
       
   387 	{
       
   388 	TInt err = iStatus.Int();
       
   389 
       
   390 	if ( err == KErrNone )//ExportL(const TDesC8 aFormat, RArray<TUint>& aInputLocalUidArray , HBufC8*& aOutputBuffer )
       
   391 		{
       
   392 		TRAP( err, ExportL( iOutputBuffer ));
       
   393 		}		
       
   394 	NotifyRequestResult( err );		
       
   395 	}
       
   396 	
       
   397 	
       
   398 // --------------------------------------------------------------------------------------------------------
       
   399 // Activates the asynchronous request
       
   400 // --------------------------------------------------------------------------------------------------------
       
   401 //
       
   402 void CCalendarExport::ActivateRequest( TInt aReason )
       
   403 	{
       
   404 	iStatus = KRequestPending;
       
   405 	
       
   406 	SetActive();
       
   407 	
       
   408 	TRequestStatus* temp = &iStatus;
       
   409 	
       
   410 	User::RequestComplete( temp, aReason );
       
   411 	}
       
   412 
       
   413 
       
   414 // --------------------------------------------------------------------------------------------------------
       
   415 // Notifies callback the result for asynchronous request.
       
   416 // --------------------------------------------------------------------------------------------------------
       
   417 //
       
   418 void CCalendarExport::NotifyRequestResult( TInt aReason )
       
   419 	{
       
   420 	if ( iCallBack )
       
   421 		{
       
   422 		iAsyncRequestObserver->RequestComplete( iCallBack->iTransactionId );
       
   423 		
       
   424 		TRAPD( err, iCallBack->NotifyResultL( aReason, ( TAny * )( iOutputBuffer )));
       
   425 		}
       
   426 	
       
   427 	// caller will delete the object in case of cancel
       
   428 	if ( aReason != KErrCancel )
       
   429 		delete this;
       
   430 	}