// ---------------------------------------------------------------------------------
// CUpnpTmServerDeviceXmlParser::StartL
// Method which inputs xml formatted buffer content to the XML Parser 
// and invokes parsing
// ---------------------------------------------------------------------------------
// 
void CUpnpTmServerDeviceXmlParser::StartL()
	{
    OstTraceFunctionEntry0( CUPNPTMSERVERDEVICEXMLPARSER_STARTL_ENTRY );
    TDriveNumber drive = RFs::GetSystemDrive();   //Find system's drive
    TBuf<UpnpString::KMaxFilenameLength> privatePath;  
    TInt err = iFs.PrivatePath(privatePath); //Find the application's private path
    // a) If the Private Path is not found (for whatever reasons),
    //      Create a private path in the System Drive
    if( err == KErrNotFound )
        {
        User::LeaveIfError( iFs.CreatePrivatePath(drive) );
        }
    // b) If Private Path is found but is a read-only or non-persistent drive
    //      Create a private path in the system drive else use it
    else if( err == KErrNone )
        {
        TDriveInfo driveInfo;
        User::LeaveIfError( iFs.Drive(driveInfo));
        TUint driveAttr = driveInfo.iDriveAtt;
        if ( driveAttr == KDriveAttRom )
            {
            User::LeaveIfError( iFs.CreatePrivatePath(drive) );
            }
        }
    else
        {
        OstTrace1( TRACE_ERROR, CUPNPTMSERVERDEVICEXMLPARSER_STARTL, "CUpnpTmServerDeviceXmlParser::StartL;err=%d", err );     
        User::Leave(err);
        }
    privatePath.Append(KPublicDevicePath());
    err = iFs.MkDirAll(privatePath);
    if( err && err != KErrAlreadyExists )
        {
        OstTrace1( TRACE_ERROR, DUP1_CUPNPTMSERVERDEVICEXMLPARSER_STARTL, "CUpnpTmServerDeviceXmlParser::StartL;err=%d", err );
        User::LeaveIfError(err);
        }
    iDeviceDir.CreateL(privatePath);
    RBuf8 rootDeviceBuf;
    CleanupClosePushL(rootDeviceBuf);
    rootDeviceBuf.CreateL(KBufSize);
    rootDeviceBuf.Append(KRootDeviceXmlLead());
    // Appends the device icon list to the device xml buffer
    const RPointerArray<CUpnpTerminalModeIcon>& deviceList = iDeviceInfo.DeviceIconList();
    TInt iconCount = deviceList.Count();
    if ( iconCount > KErrNone )
        {
        privatePath.Append(KIconDirectory());
        err = iFs.MkDir(privatePath);  // Creates icon directory 
        if( err && err != KErrAlreadyExists )
            {
            OstTrace1( TRACE_ERROR, DUP2_CUPNPTMSERVERDEVICEXMLPARSER_STARTL, "CUpnpTmServerDeviceXmlParser::StartL;err=%d", err );
            User::LeaveIfError(err);
            }
        iIconDirectory.CreateL(privatePath);
        OstTrace1( TRACE_ERROR, DUP3_CUPNPTMSERVERDEVICEXMLPARSER_STARTL, "CUpnpTmServerDeviceXmlParser::StartL;iconCount=%d", iconCount );
        rootDeviceBuf.Append(KStartIconList);
        for ( TInt i(0); i < iconCount; i++ )
            {
            rootDeviceBuf.Append(KStartIcon);
            rootDeviceBuf.Append(KStartMimeType);
            rootDeviceBuf.Append(deviceList[i]->MimeType());
            rootDeviceBuf.Append(KEndMimeType);
            rootDeviceBuf.Append(KStartWidth);
            rootDeviceBuf.AppendNum(deviceList[i]->Width());
            rootDeviceBuf.Append(KEndWidth);
            rootDeviceBuf.Append(KStartHeight);
            rootDeviceBuf.AppendNum(deviceList[i]->Height());
            rootDeviceBuf.Append(KEndHeight);
            rootDeviceBuf.Append(KStartDepth);
            rootDeviceBuf.AppendNum(deviceList[i]->Depth());
            rootDeviceBuf.Append(KEndDepth);
            rootDeviceBuf.Append(KStartUrl);
            
            TBuf8<KMaxPath> iconBuf;
            const TDesC& fileName = deviceList[i]->IconFilename();
            iconBuf.Copy(fileName);
            TBuf8<UpnpString::KDefaultStringLength> iconRelativeUrl(KScpdUrl());
            // Extracts the actual input filepath and creates the relative url for the icon
            // to be provided in the device xml file
            iconRelativeUrl.Append(KIconPath());
            // Extracts only filename and appends the same to url
            iconRelativeUrl.Append(iconBuf.Mid((iconBuf.LocateReverse(KDirectorySeparator))+1));
            rootDeviceBuf.Append(iconRelativeUrl);
            delete iFileMan;
            iFileMan = NULL;
            iFileMan = CFileMan::NewL(iFs) ;
            // copies icon files to the private device directory
            User::LeaveIfError(iFileMan->Copy(fileName,iIconDirectory));  
            rootDeviceBuf.Append(KEndUrl);
            rootDeviceBuf.Append(KEndIcon);
            }
        rootDeviceBuf.Append(KEndIconList);
        }
    rootDeviceBuf.Append( iDeviceInfo.DeviceInfo());
    rootDeviceBuf.Append( KRootDeviceXmlTrail() );
       
	ParseL(*iParser, rootDeviceBuf );
	CleanupStack::PopAndDestroy(&rootDeviceBuf);

	OstTraceFunctionExit0( CUPNPTMSERVERDEVICEXMLPARSER_STARTL_EXIT );
	}