/* * (c) Copyright Ascensio System SIA 2010-2014 * * This program is a free software product. You can redistribute it and/or * modify it under the terms of the GNU Affero General Public License (AGPL) * version 3 as published by the Free Software Foundation. In accordance with * Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect * that Ascensio System SIA expressly excludes the warranty of non-infringement * of any third-party rights. * * This program is distributed WITHOUT ANY WARRANTY; without even the implied * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For * details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html * * You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, * EU, LV-1021. * * The interactive user interfaces in modified source and object code versions * of the Program must display Appropriate Legal Notices, as required under * Section 5 of the GNU AGPL version 3. * * Pursuant to Section 7(b) of the License you must retain the original Product * logo when distributing the program. Pursuant to Section 7(e) we decline to * grant you any rights under trademark law for use of our trademarks. * * All the Product's GUI elements, including illustrations and icon sets, as * well as technical writing content are licensed under the terms of the * Creative Commons Attribution-ShareAlike 4.0 International. See the License * terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode * */ #include "CommonInclude.h" #include "IFileContainer_Spreadsheet.h" #include "FileFactory_Spreadsheet.h" #include "../DocxFormat/Rels.h" #include "../DocxFormat/ContentTypes.h" #include "../DocxFormat/FileType.h" #include "../DocxFormat/External/External.h" #include "../DocxFormat/External/HyperLink.h" namespace OOX { namespace Spreadsheet { CAtlMap IFileContainer::m_mapEnumeratedGlobal; UnknowTypeFile IFileContainer::Unknown; void IFileContainer::Read (const OOX::CPath& oPath) { RELEASEOBJECT(m_pCurRels); m_pCurRels = new OOX::CRels(oPath); Read( *m_pCurRels, oPath.GetDirectory() ); } void IFileContainer::Read (const OOX::CRels& oRels, const OOX::CPath& oPath) { int nCount = oRels.m_arrRelations.GetSize(); for ( int nIndex = 0; nIndex < nCount; ++nIndex ) { smart_ptr& pFile = OOX::Spreadsheet::CreateFile( oPath, oRels.m_arrRelations[nIndex] ); Add( oRels.m_arrRelations[nIndex].rId(), pFile ); } } void IFileContainer::Write(const OOX::CPath& oFileName, const OOX::CPath& oDirectory, OOX::CContentTypes& oContent) const { OOX::CPath oCurrent = oFileName.GetDirectory(); OOX::CRels oRels; Write( oRels, oCurrent, oDirectory, oContent ); oRels.Write( oFileName ); } void IFileContainer::Write(OOX::CRels& oRels, const OOX::CPath& oCurrent, const OOX::CPath& oDir, OOX::CContentTypes& oContent) const { CAtlMap mNamePair; POSITION pos = m_mContainer.GetStartPosition(); while ( NULL != pos ) { const CAtlMap>::CPair* pPair = m_mContainer.GetNext( pos ); smart_ptr pFile = pPair->m_value; smart_ptr pExt = pFile.smart_dynamic_cast(); if ( !pExt.IsInit() ) { OOX::CPath oDefDir = pFile->DefaultDirectory(); OOX::CPath oName = pFile->DefaultFileName(); if(false == pFile->m_sFilename.IsEmpty()) oName.SetName(pFile->m_sFilename, false); OOX::CSystemUtility::CreateDirectories( oCurrent / oDefDir ); pFile->write( oCurrent / oDefDir / oName, oDir / oDefDir, oContent ); if(true != pFile->m_bDoNotAddRels) oRels.Registration( pPair->m_key, pFile->type(), oDefDir / oName ); } else { oRels.Registration( pPair->m_key, pExt ); } } } void IFileContainer::Commit (const OOX::CPath& oPath) { CAtlMap mNamepair; POSITION pos = m_mContainer.GetStartPosition(); while ( NULL != pos ) { CAtlMap>::CPair* pPair = m_mContainer.GetNext( pos ); smart_ptr pFile = pPair->m_value; smart_ptr pExt = pFile.smart_dynamic_cast(); if (!pExt.IsInit()) { OOX::CPath oDefDir = pFile->DefaultDirectory(); OOX::CPath oName = pFile->DefaultFileName(); CAtlMap::CPair* pNamePair = mNamepair.Lookup( oName.m_strFilename ); if (NULL == pNamePair) mNamepair.SetAt( oName.m_strFilename, 1 ); else oName = oName + pNamePair->m_key; OOX::CSystemUtility::CreateDirectories( oPath / oDefDir ); smart_ptr pFileBuilder = pPair->m_value.smart_dynamic_cast(); if ( pFileBuilder.is_init() ) pFileBuilder->Commit( oPath / oDefDir / oName ); } } } void IFileContainer::Finalize(const OOX::CPath& oFileName, const OOX::CPath& oDirectory, OOX::CContentTypes& oContent) { OOX::CPath oCurrent = oFileName.GetDirectory(); OOX::CRels oRels; Finalize( oRels, oCurrent, oDirectory, oContent ); oRels.Write( oFileName ); } void IFileContainer::Finalize(OOX::CRels& oRels, const OOX::CPath& oCurrent, const OOX::CPath& oDir, OOX::CContentTypes& oContent) { CAtlMap mNamepair; POSITION pos = m_mContainer.GetStartPosition(); while ( NULL != pos ) { CAtlMap>::CPair* pPair = m_mContainer.GetNext( pos ); smart_ptr pFile = pPair->m_value; smart_ptr pExt = pFile.smart_dynamic_cast(); if ( !pExt.IsInit() ) { OOX::CPath oDefDir = pFile->DefaultDirectory(); OOX::CPath oName = pFile->DefaultFileName(); CAtlMap::CPair* pNamePair = mNamepair.Lookup( oName.m_strFilename ); if ( NULL == pNamePair ) mNamepair.SetAt( oName.m_strFilename, 1 ); else oName = oName + pNamePair->m_key; OOX::CSystemUtility::CreateDirectories( oCurrent / oDefDir ); smart_ptr pFileBuilder = pFile.smart_dynamic_cast(); if ( pFileBuilder.is_init() ) { pFileBuilder->Finalize( oCurrent / oDefDir / oName, oDir / oDefDir, oContent ); } else { pFile->write( oCurrent / oDefDir / oName, oDir / oDefDir, oContent ); } oRels.Registration( pPair->m_key, pFile->type(), oDefDir / oName ); } else { oRels.Registration( pPair->m_key, pExt ); } } } const bool IFileContainer::IsExist(const FileType& oType) const { POSITION pos = m_mContainer.GetStartPosition(); while ( NULL != pos ) { const CAtlMap>::CPair* pPair = m_mContainer.GetNext( pos ); if ( oType == pPair->m_value->type() ) return true; } return false; } const bool IFileContainer::IsExist(const RId& rId) const { const CAtlMap>::CPair* pPair = m_mContainer.Lookup(rId.get()); return ( NULL != pPair ); } template const bool IFileContainer::IsExist() const { T oFile; return IsExist( oFile.type() ); } CString IFileContainer::IsExistHyperlink(smart_ptr& pHyperLink) { POSITION pos = m_mContainer.GetStartPosition(); while ( NULL != pos ) { const CAtlMap>::CPair* pPair = m_mContainer.GetNext( pos ); if(OOX::FileTypes::HyperLink == pPair->m_value->type()) { smart_ptr pCurHyperlink = pPair->m_value.smart_dynamic_cast(); if(pCurHyperlink->Uri().GetPath() == pHyperLink->Uri().GetPath()) return pPair->m_key; } } return CString(); } const bool IFileContainer::IsExternal(const OOX::RId& rId) const { const CAtlMap>::CPair* pPair = m_mContainer.Lookup(rId.get()); if ( NULL != pPair ) { CString sType = pPair->m_value->type().RelationType(); CString sName = pPair->m_value->type().DefaultFileName().m_strFilename; return (( ( sType == OOX::FileTypes::ExternalAudio.RelationType() ) || ( sType == OOX::FileTypes::ExternalImage.RelationType() ) || ( sType == OOX::FileTypes::ExternalVideo.RelationType() ) ) && ( sName == _T("") ) ); } return true; } smart_ptr IFileContainer::Get(const FileType& oType) { POSITION pos = m_mContainer.GetStartPosition(); while ( NULL != pos ) { CAtlMap>::CPair* pPair = m_mContainer.GetNext( pos ); if ( oType == pPair->m_value->type() ) return pPair->m_value; } return smart_ptr(new UnknowTypeFile( Unknown )); } const RId IFileContainer::Add(smart_ptr& pFile) { const RId rId = GetMaxRId().next(); Add( rId, pFile ); return rId; } void IFileContainer::Add(const OOX::RId& rId, smart_ptr& pFile) { bool bEnumerated = pFile->type().Enumerated(); bool bEnumeratedGlobal = pFile->type().EnumeratedGlobal(); if(true == bEnumeratedGlobal || true == bEnumerated) { CAtlMap::CPair* pNamePair = NULL; if(true == bEnumeratedGlobal) pNamePair = m_mapEnumeratedGlobal.Lookup( pFile->type().OverrideType() ); else pNamePair = m_mapAddNamePair.Lookup( pFile->type().OverrideType() ); int nIndex = 0; if(NULL != pNamePair) nIndex = pNamePair->m_value; nIndex++; if(true == bEnumeratedGlobal) { if(pFile.is()) { OOX::FileGlobalEnumerated& oFileGlobalEnumerated = pFile.as(); oFileGlobalEnumerated.SetGlobalNumber(nIndex); } } CString sPath = pFile->DefaultFileName().GetPath(); int nDotIndex = sPath.ReverseFind('.'); if(-1 != nDotIndex && nDotIndex > 0) { CString sDigit;sDigit.Format(_T("%d"), nIndex); sPath.Insert(nDotIndex, sDigit); } pFile->m_sFilename = sPath; if(true == bEnumeratedGlobal) m_mapEnumeratedGlobal.SetAt( pFile->type().OverrideType(), nIndex ); else m_mapAddNamePair.SetAt( pFile->type().OverrideType(), nIndex ); } m_lMaxRid = max( m_lMaxRid, rId.getNumber() ); m_mContainer.SetAt( rId.get(), pFile ); } smart_ptr IFileContainer::Find(const FileType& oType) const { POSITION pos = m_mContainer.GetStartPosition(); while ( NULL != pos ) { const CAtlMap>::CPair* pPair = m_mContainer.GetNext( pos ); if ( oType == pPair->m_value->type() ) return pPair->m_value; } return smart_ptr( (OOX::File*)new UnknowTypeFile() ); } void IFileContainer::FindAllByType(const FileType& oType, CAtlMap>& aOutput) const { POSITION pos = m_mContainer.GetStartPosition(); while ( NULL != pos ) { const CAtlMap>::CPair* pPair = m_mContainer.GetNext( pos ); if ( oType == pPair->m_value->type() ) { aOutput.SetAt(pPair->m_key, pPair->m_value); } } } smart_ptr IFileContainer::Find(const OOX::RId& rId) const { const CAtlMap>::CPair* pPair = m_mContainer.Lookup(rId.get()); if ( NULL != pPair ) return pPair->m_value; return smart_ptr( (OOX::File*)new UnknowTypeFile() ); } template T& IFileContainer::Find() { T oFile; return dynamic_cast( Find( oFile.type() ) ); } smart_ptr IFileContainer::operator [](const OOX::RId rId) { CAtlMap>::CPair* pPair = m_mContainer.Lookup(rId.get()); if ( NULL != pPair ) return pPair->m_value; return smart_ptr( (OOX::File*)new UnknowTypeFile() ); } smart_ptr IFileContainer::operator [](const FileType& oType) { return Find( oType ); } const RId IFileContainer::GetMaxRId() { return RId( m_lMaxRid ); } void IFileContainer::SetGlobalNumberByType(const CString& sOverrideType, int val) { m_mapEnumeratedGlobal.SetAt( sOverrideType, val ); } int IFileContainer::GetGlobalNumberByType(const CString& sOverrideType) { CAtlMap::CPair* pNamePair = m_mapEnumeratedGlobal.Lookup( sOverrideType ); int nRes = 0; if(NULL != pNamePair) nRes = pNamePair->m_value; return nRes; } } } // namespace OOX