1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_package.hxx"
26 #include <ZipPackageEntry.hxx>
27 #include <com/sun/star/packages/zip/ZipConstants.hpp>
28 #include <osl/diagnose.h>
29 
30 #include <ZipPackageFolder.hxx>
31 #include <ZipPackageStream.hxx>
32 #include <ContentInfo.hxx>
33 
34 #include <comphelper/storagehelper.hxx>
35 
36 using namespace rtl;
37 using namespace com::sun::star;
38 using namespace com::sun::star::uno;
39 using namespace com::sun::star::lang;
40 using namespace com::sun::star::container;
41 using namespace com::sun::star::packages::zip;
42 using namespace com::sun::star::packages::zip::ZipConstants;
43 
ZipPackageEntry(bool bNewFolder)44 ZipPackageEntry::ZipPackageEntry ( bool bNewFolder )
45 : mbIsFolder ( bNewFolder )
46 , mbAllowRemoveOnInsert( sal_True )
47 , pParent ( NULL )
48 {
49 }
50 
~ZipPackageEntry()51 ZipPackageEntry::~ZipPackageEntry()
52 {
53 	// When the entry is destroyed it must be already disconnected from the parent
54 	OSL_ENSURE( !pParent, "The parent must be disconnected already! Memory corruption is possible!\n" );
55 }
56 
57 // XChild
getName()58 OUString SAL_CALL ZipPackageEntry::getName(  )
59 	throw(RuntimeException)
60 {
61 	return msName;
62 }
setName(const OUString & aName)63 void SAL_CALL ZipPackageEntry::setName( const OUString& aName )
64 	throw(RuntimeException)
65 {
66 	if ( pParent && msName.getLength() && pParent->hasByName ( msName ) )
67 		pParent->removeByName ( msName );
68 
69     // unfortunately no other exception than RuntimeException can be thrown here
70     // usually the package is used through storage implementation, the problem should be detected there
71     if ( !::comphelper::OStorageHelper::IsValidZipEntryFileName( aName, sal_True ) )
72         throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Unexpected character is used in file name." ) ), uno::Reference< XInterface >() );
73 
74 	msName = aName;
75 
76 	if ( pParent )
77 		pParent->doInsertByName ( this, sal_False );
78 }
getParent()79 uno::Reference< XInterface > SAL_CALL ZipPackageEntry::getParent(  )
80 		throw(RuntimeException)
81 {
82 	// return uno::Reference< XInterface >( xParent, UNO_QUERY );
83 	return uno::Reference< XInterface >( static_cast< ::cppu::OWeakObject* >( pParent ), UNO_QUERY );
84 }
85 
doSetParent(ZipPackageFolder * pNewParent,sal_Bool bInsert)86 void ZipPackageEntry::doSetParent ( ZipPackageFolder * pNewParent, sal_Bool bInsert )
87 {
88 	// xParent = pParent = pNewParent;
89 	pParent = pNewParent;
90 	if ( bInsert && msName.getLength() && !pNewParent->hasByName ( msName ) )
91 		pNewParent->doInsertByName ( this, sal_False );
92 }
93 
setParent(const uno::Reference<XInterface> & xNewParent)94 void SAL_CALL ZipPackageEntry::setParent( const uno::Reference< XInterface >& xNewParent )
95 		throw(NoSupportException, RuntimeException)
96 {
97 	sal_Int64 nTest(0);
98 	uno::Reference < XUnoTunnel > xTunnel ( xNewParent, UNO_QUERY );
99 	if ( !xNewParent.is() || ( nTest = xTunnel->getSomething ( ZipPackageFolder::static_getImplementationId () ) ) == 0 )
100 		throw NoSupportException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
101 
102 	ZipPackageFolder *pNewParent = reinterpret_cast < ZipPackageFolder * > ( nTest );
103 
104 	if ( pNewParent != pParent )
105 	{
106 		if ( pParent && msName.getLength() && pParent->hasByName ( msName ) && mbAllowRemoveOnInsert )
107 			pParent->removeByName( msName );
108 		doSetParent ( pNewParent, sal_True );
109 	}
110 }
111 	//XPropertySet
getPropertySetInfo()112 uno::Reference< beans::XPropertySetInfo > SAL_CALL ZipPackageEntry::getPropertySetInfo(  )
113 		throw(RuntimeException)
114 {
115 	return uno::Reference < beans::XPropertySetInfo > ();
116 }
addPropertyChangeListener(const OUString &,const uno::Reference<beans::XPropertyChangeListener> &)117 void SAL_CALL ZipPackageEntry::addPropertyChangeListener( const OUString& /*aPropertyName*/, const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
118 		throw(beans::UnknownPropertyException, WrappedTargetException, RuntimeException)
119 {
120 }
removePropertyChangeListener(const OUString &,const uno::Reference<beans::XPropertyChangeListener> &)121 void SAL_CALL ZipPackageEntry::removePropertyChangeListener( const OUString& /*aPropertyName*/, const uno::Reference< beans::XPropertyChangeListener >& /*aListener*/ )
122 		throw(beans::UnknownPropertyException, WrappedTargetException, RuntimeException)
123 {
124 }
addVetoableChangeListener(const OUString &,const uno::Reference<beans::XVetoableChangeListener> &)125 void SAL_CALL ZipPackageEntry::addVetoableChangeListener( const OUString& /*PropertyName*/, const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
126 		throw(beans::UnknownPropertyException, WrappedTargetException, RuntimeException)
127 {
128 }
removeVetoableChangeListener(const OUString &,const uno::Reference<beans::XVetoableChangeListener> &)129 void SAL_CALL ZipPackageEntry::removeVetoableChangeListener( const OUString& /*PropertyName*/, const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
130 		throw(beans::UnknownPropertyException, WrappedTargetException, RuntimeException)
131 {
132 }
133