codhandler/codeng/src/DdEng.cpp
changeset 0 dd21522fd290
child 26 cb62a4f66ebe
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2002 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 *      Implementation of class CDdEng.   
       
    16 *      
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 
       
    23 #include "DdEng.h"
       
    24 #include "CodData.h"
       
    25 #include "CodError.h"
       
    26 #include "CodPanic.h"
       
    27 #include "CodStatus.h"
       
    28 #include "DdParser.h"
       
    29 #include "CodLogger.h"
       
    30 #include "CodSaver.h"
       
    31 #include "CodUtil.h"
       
    32 #include "CodDefs.h"
       
    33 #include <DocumentHandler.h>
       
    34 #include <e32math.h>
       
    35 #include <RoapDef.h>
       
    36 
       
    37 // CONSTANTS
       
    38 
       
    39 _LIT8( KLicenseStartTag, "<license" );
       
    40 _LIT8( KLicenseEndTag, "</license>" );
       
    41 
       
    42 const TInt KElementEnd( '>' );
       
    43 
       
    44 /// Supported DD major version (only major number needs to be checked, 1.0)
       
    45 LOCAL_D const TUint32 KDdSupportedMajorVersion = 2;
       
    46 
       
    47 // ================= MEMBER FUNCTIONS =======================
       
    48 
       
    49 // ---------------------------------------------------------
       
    50 // CDdEng::NewL()
       
    51 // ---------------------------------------------------------
       
    52 //
       
    53 EXPORT_C CDdEng* CDdEng::NewL
       
    54 ( CEikProcess* aProcess, MCodLoadObserver* aObserver )
       
    55     {
       
    56     CDdEng* model = new (ELeave) CDdEng( aObserver );
       
    57     CleanupStack::PushL( model );
       
    58     model->ConstructL( aProcess );
       
    59     CleanupStack::Pop( model );
       
    60     return model;
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------
       
    64 // CDdEng::~CDdEng()
       
    65 // ---------------------------------------------------------
       
    66 //
       
    67 EXPORT_C CDdEng::~CDdEng()
       
    68     {
       
    69     CLOG(( ECodEng, 2, _L("CDdEng::~CDdEng") ));
       
    70     }
       
    71 
       
    72 // ---------------------------------------------------------
       
    73 // CDdEng::CDdEng()
       
    74 // ---------------------------------------------------------
       
    75 //
       
    76 CDdEng::CDdEng( MCodLoadObserver* aObserver ): CCodEngBase( aObserver )
       
    77     {
       
    78     CLOG(( ECodEng, 2, _L("CDdEng::CDdEng") ));
       
    79     }
       
    80 
       
    81 // ---------------------------------------------------------
       
    82 // CDdEng::CheckDataL()
       
    83 // ---------------------------------------------------------
       
    84 //
       
    85 void CDdEng::CheckDataL()
       
    86     {
       
    87     CLOG(( ECodEng, 2, _L("-> CDdEng::CheckDataL") ));
       
    88     if( !iData->IsValid() )
       
    89         {
       
    90         // Syntactically OK, but mandatory attributes are missing.
       
    91         User::Leave( KErrCodInvalidDescriptor );
       
    92         }
       
    93     CLOG(( ECodEng, 4, _L("CDdEng::CheckDataL: data valid") ));
       
    94     CheckVersionL( iData->Version() );
       
    95     CLOG(( ECodEng, 4, _L("CDdEng::CheckDataL: version OK") ));
       
    96     CLOG(( ECodEng, 2, _L("<- CDdEng::CheckDataL") ));
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------
       
   100 // CDdEng::ParseDataL()
       
   101 // ---------------------------------------------------------
       
   102 //
       
   103 void CDdEng::ParseDataL()
       
   104     {
       
   105     CLOG(( ECodEng, 2, _L("-> CDdEng::ParseDataL") ));
       
   106 
       
   107     __ASSERT_DEBUG( iCodBuf, CodPanic( ECodInternal ) );
       
   108     __ASSERT_DEBUG( iData, CodPanic( ECodInternal ) );
       
   109 
       
   110     TDdParser parser;
       
   111     TBool isLicenseTag (EFalse);
       
   112     parser.ParseL( *iCodBuf, *iData, iIsDd2, isLicenseTag );
       
   113     if (isLicenseTag && iIsDd2)
       
   114 	{	
       
   115 	  iIsLicense = ETrue;
       
   116 	  CreateSaverL(KDd2DataType);
       
   117 	  ParseLicenseL();
       
   118 	}
       
   119     CLOG(( ECodEng, 4, _L("CDdEng::SetL: parsed OK") ));
       
   120     CLOG(( ECodEng, 2, _L("<- CDdEng::ParseDataL") ));
       
   121     }
       
   122 
       
   123 // ---------------------------------------------------------
       
   124 // CDdEng::StatusCode
       
   125 // ---------------------------------------------------------
       
   126 //
       
   127 TInt CDdEng::StatusCode( TInt aError, TState aState ) const
       
   128     {
       
   129     // Meaningless to fiddle with statuses after we are done.
       
   130     __ASSERT_DEBUG( aState <= ENotify, CodPanic( ECodInternal ) );
       
   131 
       
   132     TInt code( KHttp954LoaderError );
       
   133 
       
   134     switch( aError )
       
   135         {
       
   136         case KErrNone:
       
   137             {
       
   138             code = KHttp900Success;
       
   139             break;
       
   140             }
       
   141        
       
   142         case KErrNoMemory:
       
   143         case KErrDiskFull:
       
   144             {
       
   145             code = KHttp910NoMemory;
       
   146             break;
       
   147             }
       
   148             
       
   149         case KErrCodInsufficientSpace:
       
   150             {
       
   151             code = KHttp901InsufficientMemory;
       
   152             break;
       
   153             }
       
   154 
       
   155         case KErrCancel:
       
   156         case KErrAbort:
       
   157             {
       
   158             code = KHttp902UserCancelled;
       
   159             break;
       
   160             }
       
   161 
       
   162         case KErrCodInvalidDescriptor:
       
   163             {
       
   164             code = KHttp906InvalidDescriptor;
       
   165             break;
       
   166             }
       
   167 
       
   168         case KErrCodInvalidType:
       
   169             {
       
   170             code = KHttp907InvalidType;
       
   171             break;
       
   172             }
       
   173 
       
   174         case KErrCodUnsupportedVersion:
       
   175             {
       
   176             code = KHttp951InvalidDdVersion;
       
   177             break;
       
   178             }
       
   179 
       
   180         case KErrCodAttributeMismatch:
       
   181             {
       
   182             code = KHttp905AttributeMismatch;
       
   183             break;
       
   184             }
       
   185 
       
   186         case KBadMimeType:
       
   187         case KMimeNotSupported:
       
   188             {
       
   189             code = aState == ESet ?
       
   190                 KHttp952DeviceAborted :      // ESet -> COD MIME bad.
       
   191                 KHttp953NonAcceptableContent;// EFetch -> content MIME bad.
       
   192             break;
       
   193             }
       
   194         case KErrCodHttpLicenseFailed:
       
   195         case KErrRoapGeneral:
       
   196         case KErrRoapUnsupported:
       
   197         case KErrRoapServer:
       
   198         case KErrRoapServerFatal:
       
   199         case KErrRoapDevice:
       
   200         case KErrRoapInvalidDomain:
       
   201         case KErrRoapDomainFull:
       
   202         case KErrRoapNotRegistered:
       
   203             {
       
   204             code = KHttp957LicenseFailed;
       
   205             break;
       
   206             }
       
   207 
       
   208         case KErrTimedOut:
       
   209         case KErrCodHttpCommsFailed:
       
   210         case KErrCodHttpNoResponse:
       
   211             {
       
   212             code = KHttp903LossOfService;
       
   213             break;
       
   214             }
       
   215 
       
   216         case KErrCodHttpRequestedRangeNotSatisfiable:
       
   217             {
       
   218             code = KHttp909RequestedRangeNotSatisfiable;
       
   219             break;
       
   220             }
       
   221  
       
   222         case KErrCodHttpPreconditionFailed:
       
   223             {
       
   224             code = KHttp955PreconditionFailed;
       
   225             break;
       
   226             }
       
   227 
       
   228        	case KErrCodHttpBadUrl:
       
   229         default:
       
   230             {
       
   231             code = KHttp954LoaderError;
       
   232             break;
       
   233             }
       
   234         }
       
   235     return code;
       
   236     }
       
   237 
       
   238 // ---------------------------------------------------------
       
   239 // CDdEng::StatusText()
       
   240 // ---------------------------------------------------------
       
   241 //
       
   242 const TDesC8& CDdEng::StatusText( TInt aCode ) const
       
   243     {
       
   244     switch ( aCode )
       
   245         {
       
   246         case KHttp900Success:              return KText900Success;
       
   247         case KHttp901InsufficientMemory:   return KText901InsufficientMemory;
       
   248         case KHttp902UserCancelled:        return KText902UserCancelled;
       
   249         case KHttp903LossOfService:        return KText903LossOfService;
       
   250         case KHttp905AttributeMismatch:    return KText905AttributeMismatch;
       
   251         case KHttp906InvalidDescriptor:    return KText906InvalidDescriptor;
       
   252         case KHttp907InvalidType:          return KText907InvalidType;
       
   253         case KHttp951InvalidDdVersion:     return KText951InvalidDdVersion;
       
   254         case KHttp952DeviceAborted:        return KText952DeviceAborted;
       
   255         case KHttp953NonAcceptableContent: return KText953NonAcceptableContent;
       
   256         case KHttp954LoaderError:          return KText954LoaderError;
       
   257         case KHttp956LicenseSuccess:       return KText956LicenseSuccess;
       
   258         case KHttp957LicenseFailed:        return KText957LicenseFailed;
       
   259         case KHttp910NoMemory:             return KText910NoMemory;
       
   260         case KHttp955PreconditionFailed:   return KText955MediaObjectUpdated;
       
   261         case KHttp970MixedStatus:          return KText970MixedStatus; 
       
   262         case KHttp981DownloadCompletion:   return KText981DownloadCompletion; 
       
   263         default:                           CodPanic( ECodInternal );
       
   264         }
       
   265     return KNullDesC8;  /* NOTREACHED */
       
   266     }
       
   267 
       
   268 // ---------------------------------------------------------
       
   269 // CDdEng::CheckVersionL()
       
   270 // ---------------------------------------------------------
       
   271 //
       
   272 void CDdEng::CheckVersionL( const TDesC& aVersion ) const
       
   273     {
       
   274     TUint32 majorVer = 1;   // If version is not specified, it defaults to 1.0.
       
   275 
       
   276     if( aVersion.Length() )
       
   277         {
       
   278         // Version is specified, parse it to get get major version.
       
   279         TLex lex( aVersion );
       
   280         TReal realVer;
       
   281         TInt32 intVer;
       
   282         if( lex.Val( realVer ) != KErrNone )
       
   283             {
       
   284             // Could not parse version as a real number, this indicates that
       
   285             // the DD-Version is some rubbish string.
       
   286             User::Leave( KErrCodInvalidDescriptor );
       
   287             }
       
   288         User::LeaveIfError( Math::Int( intVer, realVer ) );
       
   289         if( intVer < 0 )
       
   290             {
       
   291             // Negative DD-Version?
       
   292             User::Leave( KErrCodInvalidDescriptor );
       
   293             }
       
   294         majorVer = STATIC_CAST( TUint32, intVer );
       
   295         }
       
   296 
       
   297     // For code clarity, we always check major version (even if not given and
       
   298     // defaults to supported 1.0).
       
   299     if( majorVer > KDdSupportedMajorVersion )
       
   300         {
       
   301         User::Leave( KErrCodUnsupportedVersion );
       
   302         }
       
   303     }
       
   304 
       
   305 // ---------------------------------------------------------
       
   306 // CDdEng::ParseLicenseL()
       
   307 // ---------------------------------------------------------
       
   308 //
       
   309 void CDdEng::ParseLicenseL()
       
   310 {
       
   311 	HBufC8* descriptorBuf = CodUtil::ConvertLC( iCodBuf->Des());
       
   312 	TPtrC8 license;
       
   313 	TPtrC8 descriptor (descriptorBuf->Ptr());
       
   314 	TInt startTag = descriptor.Find(KLicenseStartTag); // "<license"
       
   315 	if (startTag != KErrNotFound)
       
   316 	{
       
   317 		descriptor.Set(descriptor.Right(descriptor.Length()- startTag -1));
       
   318 		TInt endTag = descriptor.Locate(KElementEnd); //'>'
       
   319 		if (endTag != KErrNotFound)
       
   320 		{
       
   321 			license.Set(descriptor.Right(descriptor.Length()-endTag-1));
       
   322 			TInt licenseTagEnd = license.Find(KLicenseEndTag); // "</license"
       
   323 			if (licenseTagEnd != KErrNotFound)
       
   324 			{
       
   325 				license.Set(license.Left(licenseTagEnd));
       
   326 			}
       
   327 		}
       
   328 	}
       
   329 	iSaver->AppendData( license );
       
   330 	CleanupStack::PopAndDestroy( descriptorBuf );
       
   331 }