serviceproviders/sapi_calendar/calendarservice/src/calendarimport.cpp
changeset 5 989d2f495d90
child 39 1aa6688bfd6b
equal deleted inserted replaced
1:a36b1e19a461 5:989d2f495d90
       
     1 /*
       
     2 * Copyright (c) 2009 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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //System Includes
       
    20 #include <e32cmn.h> 			//for RPointerArray
       
    21 #include <calsession.h>			//For CCalSession
       
    22 #include <calentryview.h>
       
    23 #include <CalenImporter.h>
       
    24 #include <CalenInterimUtils2.h>
       
    25 #include <s32mem.h>
       
    26 #include <s32file.h>
       
    27 
       
    28 //User Include
       
    29 #include "calendarheader.h"
       
    30 #include "calendarconstants.h"
       
    31 #include "asyncreqobserver.h"
       
    32 #include "calendarimport.h"
       
    33 
       
    34 
       
    35 
       
    36 void CleanupCCalEntryArray(TAny* aPointer);
       
    37 void CleanupCCalInstanceArray(TAny* aPointer);
       
    38 
       
    39 // --------------------------------------------------------------------------------------------------------
       
    40 // Static Method which either returns the Two Phase constructed Object or Leave 
       
    41 // NOTE: Ownership of any of the parameters is not taken through this function call
       
    42 // NOTE: Therefore the User of this function needs to take care of neccessary cleanup of passed parameters
       
    43 // --------------------------------------------------------------------------------------------------------
       
    44 //
       
    45 CCalendarImport* CCalendarImport::NewL ( CCalendarSessionInfo* aCalSessionInfo, 
       
    46 											const TDesC8& aFormat, 
       
    47 											const TDesC8& aInputBuffer, 
       
    48 											CAsyncRequestObserver* aAsyncRequestObserver,
       
    49 											MCalCallbackBase* aCallBack )
       
    50 	{
       
    51 	CCalendarImport* self = new (ELeave) CCalendarImport( aCalSessionInfo, aAsyncRequestObserver, aCallBack );
       
    52 	
       
    53     CleanupStack::PushL(self);
       
    54     
       
    55     self->ConstructL( aFormat, aInputBuffer );
       
    56     
       
    57     CleanupStack::Pop(self); 
       
    58     
       
    59     return self;
       
    60 	}	
       
    61 
       
    62 // --------------------------------------------------------------------------------------------------------
       
    63 // Static Method which either returns the Two Phase constructed Object or Leave 
       
    64 // NOTE: Ownership of any of the parameters is not taken through this function call
       
    65 // NOTE: Therefore the User of this function needs to take care of neccessary cleanup of passed parameters
       
    66 // --------------------------------------------------------------------------------------------------------
       
    67 //
       
    68 CCalendarImport* CCalendarImport::NewL ( CCalendarSessionInfo* aCalSessionInfo, 
       
    69 											const TDesC8& aFormat, 
       
    70 											const TDesC& aImportFile, 
       
    71 											CAsyncRequestObserver* aAsyncRequestObserver,
       
    72 											MCalCallbackBase* aCallBack )
       
    73 	{
       
    74 	CCalendarImport* self = new (ELeave) CCalendarImport( aCalSessionInfo, aAsyncRequestObserver, aCallBack );
       
    75 	
       
    76     CleanupStack::PushL(self);
       
    77     
       
    78     self->ConstructL( aFormat, aImportFile );
       
    79     
       
    80     CleanupStack::Pop(self); 
       
    81     
       
    82     return self;
       
    83 	}	
       
    84 
       
    85 	
       
    86 // --------------------------------------------------------------------------------------------------------
       
    87 // Destructor.
       
    88 // --------------------------------------------------------------------------------------------------------
       
    89 //
       
    90 CCalendarImport::~CCalendarImport()
       
    91 	{
       
    92 	Cancel();	
       
    93 	
       
    94 	delete iCalenImporter;
       
    95 	
       
    96 	delete iFormat;
       
    97 	
       
    98 	delete iInputBuffer;
       
    99 	
       
   100 	delete iImportFile;
       
   101 	
       
   102 	iOutputUIDArray.ResetAndDestroy();
       
   103 	}
       
   104 	
       
   105 // --------------------------------------------------------------------------------------------------------
       
   106 // Synchronous Version of Import which takes InputBuffer set at the time of Construction of this object
       
   107 // and Imports them to the Calender opened in CalSession of CalendarSessionInfoL object passed to this object
       
   108 // It sets the LUids and GUids in the aOutputLocalUidArray, this object is not the owner of aOutputLocalUidArray
       
   109 // --------------------------------------------------------------------------------------------------------
       
   110 //
       
   111 void CCalendarImport::ImportL( RPointerArray<TUIDSet>& aOutputUIDArray )
       
   112 	{
       
   113 	if ( iInputBuffer )
       
   114 		{
       
   115 		RDesReadStream rStream( *iInputBuffer );
       
   116 		
       
   117 	    CleanupClosePushL( rStream );
       
   118 	    
       
   119 	    ImportFromStreamL(rStream, aOutputUIDArray);
       
   120 
       
   121 		CleanupStack::PopAndDestroy( &rStream );
       
   122 		}
       
   123 	else
       
   124 		{
       
   125 		RFs rfs;
       
   126 		User::LeaveIfError( rfs.Connect() );
       
   127 		CleanupClosePushL( rfs );
       
   128 		
       
   129 		RFile file;
       
   130 		User::LeaveIfError( file.Open( rfs, *iImportFile, EFileRead ));
       
   131 		CleanupClosePushL( rfs );
       
   132 		
       
   133 		RFileReadStream rStream( file );
       
   134 		CleanupClosePushL( rStream );
       
   135 
       
   136 	    ImportFromStreamL(rStream, aOutputUIDArray);
       
   137 		
       
   138 		CleanupStack::PopAndDestroy( 3 , &rfs );
       
   139 		}
       
   140 	}
       
   141 
       
   142 // --------------------------------------------------------------------------------------------------------
       
   143 // Synchronous Version of Import which takes InputStream set at the time of Construction of this object
       
   144 // and Imports them to the Calender opened in CalSession of CalendarSessionInfoL object passed to this object
       
   145 // It sets the LUids and GUids in the aOutputLocalUidArray, this object is not the owner of aOutputLocalUidArray
       
   146 // --------------------------------------------------------------------------------------------------------
       
   147 //	
       
   148 void CCalendarImport::ImportFromStreamL(RReadStream& aStream,  RPointerArray<TUIDSet>& aOutputUIDArray )
       
   149 	{
       
   150 	RPointerArray<CCalEntry> outputCalEntryArray;
       
   151 
       
   152 	CleanupStack::PushL( TCleanupItem(CleanupCCalEntryArray, &outputCalEntryArray) );
       
   153 	
       
   154 	if ( iFormat->CompareF(KCalFmtVCal) == 0 )
       
   155 		{
       
   156 		iCalenImporter->ImportVCalendarL( aStream, outputCalEntryArray );
       
   157 		}
       
   158 	else if ( iFormat->CompareF(KCalFmtICal) == 0 )
       
   159 		{
       
   160 		iCalenImporter->ImportICalendarL( aStream, outputCalEntryArray );
       
   161 		}
       
   162 	else
       
   163 		User::Leave( KErrNotSupported );	
       
   164 	
       
   165 	TInt count = outputCalEntryArray.Count();
       
   166 	
       
   167 	for ( TInt index = 0; index < count; index ++ )
       
   168 		{
       
   169 		CCalenInterimUtils2::StoreL( *iCalSessionInfo->EntryView() , *outputCalEntryArray[index] );
       
   170 		
       
   171 		TUIDSet* newUid = new(ELeave) TUIDSet;
       
   172 		
       
   173 		CleanupStack::PushL( newUid );
       
   174 		
       
   175 		newUid->iLocalUID = outputCalEntryArray[index]->LocalUidL();
       
   176 		
       
   177 		newUid->iGlobalUID = outputCalEntryArray[index]->UidL().AllocL();
       
   178 		
       
   179 		aOutputUIDArray.AppendL( newUid );
       
   180 		
       
   181 		CleanupStack::Pop( newUid );
       
   182 		}
       
   183 	
       
   184 	CleanupStack::PopAndDestroy( &outputCalEntryArray );
       
   185 	}
       
   186 	
       
   187 
       
   188 
       
   189 // --------------------------------------------------------------------------------------------------------
       
   190 // ASynchronous Version of Import which takes InputBuffer set at the time of Construction of this object through NewL
       
   191 //                     and Imports to the Calender(outputis passed through callback)
       
   192 // --------------------------------------------------------------------------------------------------------	
       
   193 //
       
   194 void CCalendarImport::ImportL()
       
   195 	{
       
   196 	if( !iCallBack || !iAsyncRequestObserver ) //if any of the async handlers are not set then leave
       
   197 		User::Leave( KErrArgument );
       
   198 	
       
   199 	CActiveScheduler::Add ( this );
       
   200 	
       
   201 	ActivateRequest( KErrNone );
       
   202 	}
       
   203 
       
   204 
       
   205 	
       
   206 // --------------------------------------------------------------------------------------------------------
       
   207 // Constructor.
       
   208 // --------------------------------------------------------------------------------------------------------
       
   209 //
       
   210 CCalendarImport::CCalendarImport( CCalendarSessionInfo* aCalSessionInfo, 
       
   211 									CAsyncRequestObserver* aAsyncRequestObserver,
       
   212 									MCalCallbackBase* aCallBack ): 
       
   213 															//CActive( EPriorityStandard ),
       
   214 														  	iCalSessionInfo(aCalSessionInfo),
       
   215 														  	iCallBack( aCallBack ),
       
   216 														  	iAsyncRequestObserver(aAsyncRequestObserver)
       
   217 	{
       
   218 
       
   219 	}
       
   220 	
       
   221 	
       
   222 // --------------------------------------------------------------------------------------------------------
       
   223 // 2nd-phased constructor of two phase construction
       
   224 // --------------------------------------------------------------------------------------------------------
       
   225 //
       
   226 void CCalendarImport::ConstructL( const TDesC8& aFormat, const TDesC8& aInputBuffer )
       
   227 	{	
       
   228 	//instantiating the iCalDataExchange handle
       
   229 	iCalenImporter=CCalenImporter::NewL(*(iCalSessionInfo->Session()));
       
   230 
       
   231 	iFormat = aFormat.AllocL();
       
   232 	
       
   233 	iInputBuffer = aInputBuffer.AllocL();
       
   234 	
       
   235 	}
       
   236 
       
   237 void CCalendarImport::ConstructL( const TDesC8& aFormat, const TDesC& aImportFile )
       
   238 	{	
       
   239 	//instantiating the iCalDataExchange handle
       
   240 	iCalenImporter=CCalenImporter::NewL(*(iCalSessionInfo->Session()));
       
   241 
       
   242 	iFormat = aFormat.AllocL();
       
   243 	
       
   244 	iImportFile = aImportFile.AllocL();
       
   245 	
       
   246 	}
       
   247 
       
   248 
       
   249 // ---------------------------------------------------------------------------
       
   250 // Inherited from CActive class 
       
   251 // ---------------------------------------------------------------------------
       
   252 //
       
   253 void CCalendarImport::DoCancel()
       
   254 	{
       
   255 	NotifyRequestResult( KErrCancel );
       
   256 	}
       
   257 
       
   258 // ---------------------------------------------------------------------------
       
   259 // Inherited from CActive class 
       
   260 // ---------------------------------------------------------------------------
       
   261 //
       
   262 void CCalendarImport::RunL()
       
   263 	{
       
   264 	TInt err = iStatus.Int();
       
   265 
       
   266 	if ( err == KErrNone )
       
   267 		{
       
   268 		TRAP( err, ImportL( iOutputUIDArray ));
       
   269 		}
       
   270 
       
   271 	NotifyRequestResult( err );		
       
   272 	}
       
   273 
       
   274 	
       
   275 // ---------------------------------------------------------------------------
       
   276 // Activates the asynchronous request
       
   277 // ---------------------------------------------------------------------------
       
   278 //
       
   279 void CCalendarImport::ActivateRequest( TInt aReason )
       
   280 	{
       
   281 	iStatus = KRequestPending;
       
   282 	
       
   283 	SetActive();
       
   284 	
       
   285 	TRequestStatus* temp = &iStatus;
       
   286 	
       
   287 	User::RequestComplete( temp, aReason );
       
   288 	}
       
   289 
       
   290 
       
   291 // ---------------------------------------------------------------------------
       
   292 // Notifies callback the result for asynchronous request.
       
   293 // ---------------------------------------------------------------------------
       
   294 //
       
   295 void CCalendarImport::NotifyRequestResult( TInt aReason )
       
   296 	{
       
   297 	if ( iCallBack )
       
   298 		{
       
   299 		iAsyncRequestObserver->RequestComplete( iCallBack->iTransactionId );
       
   300 		
       
   301 		TRAPD( err, iCallBack->NotifyResultL( aReason, ( TAny * )( & iOutputUIDArray )));
       
   302 
       
   303 		}
       
   304 	
       
   305 	// caller will delete the object in case of cancel
       
   306 	if ( aReason != KErrCancel )
       
   307 		delete this;
       
   308 	}