landmarksui/engine/src/CLmkEncoder.cpp
branchRCL_3
changeset 17 1fc85118c3ae
parent 16 8173571d354e
child 18 870918037e16
equal deleted inserted replaced
16:8173571d354e 17:1fc85118c3ae
     1 /*
       
     2 * Copyright (c) 2002-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:   This file implements the methods which wrap functionality related
       
    15 *                to landmark encoding into a simpler interface.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 // INCLUDE FILES
       
    26 
       
    27 #include <e32base.h>
       
    28 #include <e32std.h>
       
    29 #include <AknQueryDialog.h>
       
    30 #include <StringLoader.h>
       
    31 #include <lmkui.rsg>
       
    32 #include <EPos_CPosLandmarkDatabase.h>
       
    33 #include <EPos_CPosLandmarkEncoder.h>
       
    34 #include "LmkConsts.h"
       
    35 #include "Debug.h"
       
    36 #include "LmkFileUtils.h"
       
    37 #include "CLmkAOOperation.h"
       
    38 #include "CLmkEncoder.h"
       
    39 #include "CLmkEncodeUnsavedLandmarksOp.h"
       
    40 #include <lmkerrors.h>
       
    41 
       
    42 // CONSTANTS
       
    43 /// Unnamed namespace for local definitions
       
    44 namespace {
       
    45 
       
    46 _LIT8( KLandmarkMimeType, "application/vnd.nokia.landmarkcollection+xml" );
       
    47 // We wrap two async. tasks (export and write to file) into one
       
    48 // using estimated 0.8/0.2 progress distribution:
       
    49 const TReal32 KLmkExportMaxProgress( TReal32( 0.8 ) );
       
    50 
       
    51 #if defined(_DEBUG)
       
    52 _LIT( KPanicMsg, "CLmkEncoder" );
       
    53 
       
    54 void Panic( TPanicCode aReason )
       
    55     {
       
    56     User::Panic( KPanicMsg, aReason );
       
    57     }
       
    58 #endif
       
    59 }  // namespace
       
    60 
       
    61 // ============================ MEMBER FUNCTIONS ===============================
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // CLmkEncoder::CLmkEncoder
       
    65 // C++ default constructor can NOT contain any code, that
       
    66 // might leave.
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 CLmkEncoder::CLmkEncoder(
       
    70 	CPosLandmarkDatabase& aDb,
       
    71 	MLmkAOOperationObserver& aObserver,
       
    72 	TBool aProgressNote )
       
    73 	: iDb( aDb ),
       
    74 	iObserver( aObserver ),
       
    75 	iProgressNote( aProgressNote )
       
    76 	{
       
    77 	}
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CLmkEncoder::NewL()
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 EXPORT_C CLmkEncoder* CLmkEncoder::NewL(
       
    84 	CPosLandmarkDatabase& aDb,
       
    85 	MLmkAOOperationObserver& aObserver,
       
    86 	TBool aProgressNote )
       
    87 	{
       
    88 	CLmkEncoder* self =
       
    89 	new ( ELeave ) CLmkEncoder( aDb, aObserver, aProgressNote );
       
    90 	CleanupStack::PushL( self );
       
    91 	self->ConstructL();
       
    92 	CleanupStack::Pop(); //self
       
    93 	return self;
       
    94 	}
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CLmkEncoder::ConstructL()
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 void CLmkEncoder::ConstructL()
       
   101 	{
       
   102 	}
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // CLmkEncoder::~CLmkEncoder()
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 CLmkEncoder::~CLmkEncoder()
       
   109 	{
       
   110 	delete iAOOperation;
       
   111 	delete iEncoder;
       
   112 	}
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CLmkEncoder::HandleOperationL
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 void CLmkEncoder::HandleOperationL(
       
   119 	TOperationTypes aType,
       
   120 	TReal32 aProgress,
       
   121 	TInt aStatus )
       
   122 	{
       
   123 	DEBUG2( CLmkEncoder::HandleOperationL entered: progress=%d status=%d,
       
   124 	aProgress, aStatus );
       
   125 
       
   126 	__ASSERT_DEBUG( aType == EEncode, Panic( KLmkPanicUnknownOperation ) );
       
   127 
       
   128 	if ( aStatus != KPosLmOperationNotComplete )
       
   129 		{
       
   130 		__ASSERT_DEBUG( iAOOperation, Panic( KLmkPanicNullMember ) );
       
   131 
       
   132 		delete iAOOperation;
       
   133 		iAOOperation = NULL;
       
   134 
       
   135 		if ( iExporting && aStatus == KErrNone )
       
   136 			{ // start second part of asynchronous task
       
   137 			FinalizeEncodingL();
       
   138 			aStatus = KPosLmOperationNotComplete;
       
   139 			}
       
   140 		}
       
   141 
       
   142 	// Modify progress information because we have wrapped two tasks into one:
       
   143 	if ( iExporting && aStatus == KPosLmOperationNotComplete )
       
   144 		{
       
   145 		aProgress = aProgress * KLmkExportMaxProgress;
       
   146 		}
       
   147 	else if ( !iExporting && aStatus == KPosLmOperationNotComplete )
       
   148 		{ // Progress of writing to file is between KLmkExportMaxProgress...1
       
   149 		aProgress = KLmkExportMaxProgress +
       
   150 		aProgress * ( 1 - KLmkExportMaxProgress );
       
   151 		}
       
   152 
       
   153 	if( iProgressNote || aStatus != KPosLmOperationNotComplete )
       
   154 		{ // Caller either requested to receive all events, or we
       
   155 		// have completed entirely and inform that to the caller.
       
   156 		iObserver.HandleOperationL( aType, aProgress, aStatus );
       
   157 		}
       
   158 }
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // CLmkEncoder::SetPackageNameL
       
   162 // -----------------------------------------------------------------------------
       
   163 //
       
   164 //EXPORT_C void CLmkEncoder::SetPackageNameL( const TDesC& aName )
       
   165 void CLmkEncoder::SetPackageNameL( const TDesC& aName )
       
   166 	{
       
   167 	iEncoder->AddCollectionDataL( EPosLmCollDataCollectionName, aName );
       
   168 	}
       
   169 
       
   170 // -----------------------------------------------------------------------------
       
   171 // CLmkEncoder::StartEncodingL
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 EXPORT_C void CLmkEncoder::StartEncodingL(
       
   175 	const TDesC& aFileName,
       
   176 	const RArray<TPosLmItemId>& aLandmarkIdArray )
       
   177 	{
       
   178 	DEBUG( CLmkEncoder::StartEncodingL entered );
       
   179 	LeaveIfInUseL();
       
   180     PrepareEncoderL(aFileName);
       
   181 	if (aLandmarkIdArray.Count() > 1) // Only if multiple Landmarks Are selected
       
   182 		{
       
   183 	    // Set the package name here
       
   184 		SetPackageNameL(aFileName);
       
   185 		}
       
   186 
       
   187 	CPosLmOperation* operation =
       
   188 	iDb.ExportLandmarksL( *iEncoder,
       
   189 	                      aLandmarkIdArray,
       
   190 	                      CPosLandmarkDatabase::EIncludeCategories );
       
   191 	CleanupStack::PushL( operation );
       
   192 	iAOOperation = CLmkAOOperation::NewL( operation,
       
   193 	                                  *this,
       
   194 	                                  EEncode,
       
   195 	                                  iProgressNote );
       
   196 	CleanupStack::Pop( operation ); // ownership transferred
       
   197 
       
   198 	iExporting = ETrue; // exporting part of encoding is being done
       
   199 
       
   200 	DEBUG( CLmkEncoder::StartEncodingL: starting operation );
       
   201 	iAOOperation->StartOperation();
       
   202 	}
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 // CLmkEncoder::CancelEncodeOperationL
       
   206 // -----------------------------------------------------------------------------
       
   207 //
       
   208 EXPORT_C void CLmkEncoder::CancelEncodeOperationL()
       
   209 	{
       
   210 	if ( iAOOperation )
       
   211 		{
       
   212 		iAOOperation->Cancel();
       
   213 		delete iAOOperation;
       
   214 		iAOOperation = NULL;
       
   215 		}
       
   216 	}
       
   217 
       
   218 // -----------------------------------------------------------------------------
       
   219 // CLmkEncoder::FinalizeEncodingL
       
   220 // -----------------------------------------------------------------------------
       
   221 //
       
   222 void CLmkEncoder::FinalizeEncodingL()
       
   223     {
       
   224     DEBUG( CLmkEncoder::FinalizeEncodingL entered );
       
   225 
       
   226     LeaveIfInUseL();
       
   227 
       
   228     CPosLmOperation* operation = iEncoder->FinalizeEncodingL();
       
   229     CleanupStack::PushL( operation );
       
   230     iAOOperation = CLmkAOOperation::NewL( operation,
       
   231                                           *this,
       
   232                                           EEncode,
       
   233                                           iProgressNote );
       
   234     CleanupStack::Pop( operation ); // ownership transferred
       
   235 
       
   236     iExporting = EFalse; // write to file -part of encoding is being done
       
   237 
       
   238     DEBUG( CLmkEncoder::FinalizeEncodingL: starting operation );
       
   239     iAOOperation->StartOperation();
       
   240     }
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // CLmkEncoder::LeaveIfInUseL
       
   244 // -----------------------------------------------------------------------------
       
   245 //
       
   246 void CLmkEncoder::LeaveIfInUseL()
       
   247     {
       
   248     if ( iAOOperation )
       
   249         {
       
   250         User::Leave( KErrInUse );
       
   251         }
       
   252     }
       
   253 
       
   254 // -----------------------------------------------------------------------------
       
   255 // CLmkEncoder::PrepareEncoderL
       
   256 // -----------------------------------------------------------------------------
       
   257 //
       
   258 void CLmkEncoder::PrepareEncoderL(const TDesC& aFileName)
       
   259     {
       
   260     // Full path name (Private path c:\\Provate\\UID3 + pacakge File name)
       
   261     HBufC* nameBuf = HBufC::NewLC( 2 * KLmkPackageNameMaxLen +  KLmkEncodedDataFileExtension().Length() );
       
   262 
       
   263 	// For getting private path
       
   264 	TPtr ptr = 	nameBuf->Des();
       
   265 	RFs fs;
       
   266     User::LeaveIfError(fs.Connect());
       
   267     CleanupClosePushL(fs);
       
   268     fs.SessionPath(ptr);
       
   269     fs.MkDirAll(ptr);
       
   270     CleanupStack::PopAndDestroy(); // fs
       
   271 
       
   272 	ptr.Append(aFileName);
       
   273 	ptr.Append(KLmkEncodedDataFileExtension);
       
   274 	TInt err = KErrNone;
       
   275 
       
   276 	if ( !iEncoder )
       
   277 		{
       
   278 		DEBUG( CLmkEncoder::StartEncodingL: creating encoder );
       
   279 		TRAP(err, iEncoder = CPosLandmarkEncoder::NewL( KLandmarkMimeType ));
       
   280 		DEBUG( CLmkEncoder::StartEncodingL: encoder created);
       
   281 		}
       
   282     if(err != KErrNone)
       
   283 		{
       
   284 		LmkFileUtils::DeleteFileL( ptr ); // Delete old file if it exists
       
   285 	    CleanupStack::PopAndDestroy(nameBuf); //nameBuf
       
   286 	    User::LeaveIfError( err );
       
   287 		}
       
   288     LmkFileUtils::DeleteFileL( ptr ); // Delete old file if it exists
       
   289 	iEncoder->SetOutputFileL( ptr );
       
   290     CleanupStack::PopAndDestroy(nameBuf); //nameBuf
       
   291     }
       
   292 
       
   293 // -----------------------------------------------------------------------------
       
   294 // CLmkEncoder::StartEncodingL
       
   295 // -----------------------------------------------------------------------------
       
   296 //
       
   297 EXPORT_C void CLmkEncoder::StartEncodingL(
       
   298 	const TDesC& aFileName,
       
   299 	const RPointerArray<CLmkLandmark>& aLandmarks )
       
   300 	{
       
   301 	DEBUG( CLmkEncoder::StartEncodingL entered );
       
   302 	LeaveIfInUseL();
       
   303     PrepareEncoderL(aFileName);
       
   304 	if (aLandmarks.Count() > 1) // Only if multiple Landmarks Are selected
       
   305 		{
       
   306 	    // Set the package name here
       
   307 		SetPackageNameL(aFileName);
       
   308 		}
       
   309 
       
   310     CPosLmOperation* operation =
       
   311             CLmkEncodeUnsavedLandmarksOp::NewL( *iEncoder,
       
   312                                                 aLandmarks,
       
   313                                                     CPosLandmarkDatabase::EIncludeCategories);
       
   314 	CleanupStack::PushL( operation );
       
   315 	iAOOperation = CLmkAOOperation::NewL( operation,
       
   316 	                                  *this,
       
   317 	                                  EEncode,
       
   318 	                                  iProgressNote );
       
   319 	CleanupStack::Pop( operation ); // ownership transferred
       
   320 
       
   321 	iExporting = ETrue; // exporting part of encoding is being done
       
   322 
       
   323 	DEBUG( CLmkEncoder::StartEncodingL: starting operation );
       
   324 	iAOOperation->StartOperation();
       
   325 	}
       
   326 //  End of File