1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_package.hxx"
30 #include <ZipPackageEntry.hxx>
31 #include <com/sun/star/packages/zip/ZipConstants.hpp>
32 #include <osl/diagnose.h>
33 
34 #include <ZipPackageFolder.hxx>
35 #include <ZipPackageStream.hxx>
36 #include <ContentInfo.hxx>
37 
38 #include <comphelper/storagehelper.hxx>
39 
40 using namespace rtl;
41 using namespace com::sun::star;
42 using namespace com::sun::star::uno;
43 using namespace com::sun::star::lang;
44 using namespace com::sun::star::container;
45 using namespace com::sun::star::packages::zip;
46 using namespace com::sun::star::packages::zip::ZipConstants;
47 
48 ZipPackageEntry::ZipPackageEntry ( bool bNewFolder )
49 : mbIsFolder ( bNewFolder )
50 , mbAllowRemoveOnInsert( sal_True )
51 , pParent ( NULL )
52 {
53 }
54 
55 ZipPackageEntry::~ZipPackageEntry()
56 {
57 	// When the entry is destroyed it must be already disconnected from the parent
58 	OSL_ENSURE( !pParent, "The parent must be disconnected already! Memory corruption is possible!\n" );
59 }
60 
61 // XChild
62 OUString SAL_CALL ZipPackageEntry::getName(  )
63 	throw(RuntimeException)
64 {
65 	return msName;
66 }
67 void SAL_CALL ZipPackageEntry::setName( const OUString& aName )
68 	throw(RuntimeException)
69 {
70 	if ( pParent && msName.getLength() && pParent->hasByName ( msName ) )
71 		pParent->removeByName ( msName );
72 
73     // unfortunately no other exception than RuntimeException can be thrown here
74     // usually the package is used through storage implementation, the problem should be detected there
75     if ( !::comphelper::OStorageHelper::IsValidZipEntryFileName( aName, sal_True ) )
76         throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Unexpected character is used in file name." ) ), uno::Reference< XInterface >() );
77 
78 	msName = aName;
79 
80 	if ( pParent )
81 		pParent->doInsertByName ( this, sal_False );
82 }
83 uno::Reference< XInterface > SAL_CALL ZipPackageEntry::getParent(  )
84 		throw(RuntimeException)
85 {
86 	// return uno::Reference< XInterface >( xParent, UNO_QUERY );
87 	return uno::Reference< XInterface >( static_cast< ::cppu::OWeakObject* >( pParent ), UNO_QUERY );
88 }
89 
90 void ZipPackageEntry::doSetParent ( ZipPackageFolder * pNewParent, sal_Bool bInsert )
91 {
92 	// xParent = pParent = pNewParent;
93 	pParent = pNewParent;
94 	if ( bInsert && msName.getLength() && !pNewParent->hasByName ( msName ) )
95 		pNewParent->doInsertByName ( this, sal_False );
96 }
97 
98 void SAL_CALL ZipPackageEntry::setParent( const uno::Reference< XInterface >& xNewParent )
99 		throw(NoSupportException, RuntimeException)
100 {
101 	sal_Int64 nTest(0);
102 	uno::Reference < XUnoTunnel > xTunnel ( xNewParent, UNO_QUERY );
103 	if ( !xNewParent.is() || ( nTest = xTunnel->getSomething ( ZipPackageFolder::static_getImplementationId () ) ) == 0 )
104 		throw NoSupportException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
105 
106 	ZipPackageFolder *pNewParent = reinterpret_cast < ZipPackageFolder * > ( nTest );
107 
108 	if ( pNewParent != pParent )
109 	{
110 		if ( pParent && msName.getLength() && pParent->hasByName ( msName ) && mbAllowRemoveOnInsert )
111 			pParent->removeByName( msName );
112 		doSetParent ( pNewParent, sal_True );
113 	}
114 }
115 	//XPropertySet
116 uno::Reference< beans::XPropertySetInfo > SAL_CALL ZipPackageEntry::getPropertySetInfo(  )
117 		throw(RuntimeException)
118 {
119 	return uno::Reference < beans::XPropertySetInfo > ();
120 }
121 void SAL_CALL ZipPackageEntry::addPropertyChangeListener( const OUString& /*aPropertyName*/, const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
122 		throw(beans::UnknownPropertyException, WrappedTargetException, RuntimeException)
123 {
124 }
125 void SAL_CALL ZipPackageEntry::removePropertyChangeListener( const OUString& /*aPropertyName*/, const uno::Reference< beans::XPropertyChangeListener >& /*aListener*/ )
126 		throw(beans::UnknownPropertyException, WrappedTargetException, RuntimeException)
127 {
128 }
129 void SAL_CALL ZipPackageEntry::addVetoableChangeListener( const OUString& /*PropertyName*/, const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
130 		throw(beans::UnknownPropertyException, WrappedTargetException, RuntimeException)
131 {
132 }
133 void SAL_CALL ZipPackageEntry::removeVetoableChangeListener( const OUString& /*PropertyName*/, const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
134 		throw(beans::UnknownPropertyException, WrappedTargetException, RuntimeException)
135 {
136 }
137