xref: /AOO41X/main/sfx2/source/doc/docfile.cxx (revision d119d52d53d0b2180f2ae51341d882123be2af2b)
1*d119d52dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d119d52dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d119d52dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d119d52dSAndrew Rist  * distributed with this work for additional information
6*d119d52dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d119d52dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d119d52dSAndrew Rist  * "License"); you may not use this file except in compliance
9*d119d52dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*d119d52dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*d119d52dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d119d52dSAndrew Rist  * software distributed under the License is distributed on an
15*d119d52dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d119d52dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*d119d52dSAndrew Rist  * specific language governing permissions and limitations
18*d119d52dSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*d119d52dSAndrew Rist  *************************************************************/
21*d119d52dSAndrew Rist 
22*d119d52dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
26cdf0e10cSrcweir #include <sfx2/docfile.hxx>
27cdf0e10cSrcweir #include "sfx2/signaturestate.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <uno/mapping.hxx>
30cdf0e10cSrcweir #include <com/sun/star/task/XInteractionHandler.hpp>
31cdf0e10cSrcweir #include <com/sun/star/uno/Reference.h>
32cdf0e10cSrcweir #include <com/sun/star/ucb/XContent.hpp>
33cdf0e10cSrcweir #include <com/sun/star/document/XDocumentRevisionListPersistence.hpp>
34cdf0e10cSrcweir #include <com/sun/star/document/LockedDocumentRequest.hpp>
35cdf0e10cSrcweir #include <com/sun/star/document/OwnLockOnDocumentRequest.hpp>
36cdf0e10cSrcweir #include <com/sun/star/document/LockedOnSavingRequest.hpp>
37cdf0e10cSrcweir #include <com/sun/star/document/LockFileIgnoreRequest.hpp>
38cdf0e10cSrcweir #include <com/sun/star/document/ChangedByOthersRequest.hpp>
39cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
40cdf0e10cSrcweir #include <com/sun/star/embed/XTransactedObject.hpp>
41cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
42cdf0e10cSrcweir #include <com/sun/star/embed/UseBackupException.hpp>
43cdf0e10cSrcweir #include <com/sun/star/embed/XOptimizedStorage.hpp>
44cdf0e10cSrcweir #include <com/sun/star/ucb/InteractiveIOException.hpp>
45cdf0e10cSrcweir #include <com/sun/star/ucb/UnsupportedDataSinkException.hpp>
46cdf0e10cSrcweir #include <com/sun/star/ucb/CommandFailedException.hpp>
47cdf0e10cSrcweir #include <com/sun/star/ucb/CommandAbortedException.hpp>
48cdf0e10cSrcweir #include <com/sun/star/ucb/XCommandEnvironment.hpp>
49cdf0e10cSrcweir #include <com/sun/star/ucb/XContentIdentifierFactory.hpp>
50cdf0e10cSrcweir #include <com/sun/star/ucb/XContentProvider.hpp>
51cdf0e10cSrcweir #include <com/sun/star/ucb/XProgressHandler.hpp>
52cdf0e10cSrcweir #include <com/sun/star/ucb/XCommandInfo.hpp>
53cdf0e10cSrcweir #include <com/sun/star/util/XArchiver.hpp>
54cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
55cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
56cdf0e10cSrcweir #include <com/sun/star/io/XTruncate.hpp>
57cdf0e10cSrcweir #include <com/sun/star/io/XStreamListener.hpp>
58cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp>
59cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
60cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
61cdf0e10cSrcweir #include <com/sun/star/ucb/InsertCommandArgument.hpp>
62cdf0e10cSrcweir #include <com/sun/star/ucb/NameClash.hpp>
63cdf0e10cSrcweir #include <com/sun/star/ucb/TransferInfo.hpp>
64cdf0e10cSrcweir #include <com/sun/star/ucb/OpenCommandArgument2.hpp>
65cdf0e10cSrcweir #include <com/sun/star/ucb/OpenMode.hpp>
66cdf0e10cSrcweir #include <com/sun/star/ucb/NameClashException.hpp>
67cdf0e10cSrcweir #include <com/sun/star/logging/XSimpleLogRing.hpp>
68cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
69cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
70cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SECURITY_DOCUMENTSIGNATURESINFORMATION_HPP_
71cdf0e10cSrcweir #include <com/sun/star/security/DocumentSignatureInformation.hpp>
72cdf0e10cSrcweir #endif
73cdf0e10cSrcweir #include <com/sun/star/security/XDocumentDigitalSignatures.hpp>
74cdf0e10cSrcweir #include <tools/zcodec.hxx>
75cdf0e10cSrcweir #include <tools/cachestr.hxx>
76cdf0e10cSrcweir #include <tools/urlobj.hxx>
77cdf0e10cSrcweir #include <unotools/tempfile.hxx>
78cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
79cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
80cdf0e10cSrcweir #include <comphelper/interaction.hxx>
81cdf0e10cSrcweir #include <framework/interaction.hxx>
82cdf0e10cSrcweir #include <unotools/streamhelper.hxx>
83cdf0e10cSrcweir #include <unotools/localedatawrapper.hxx>
84cdf0e10cSrcweir #ifndef _MSGBOX_HXX //autogen
85cdf0e10cSrcweir #include <vcl/msgbox.hxx>
86cdf0e10cSrcweir #endif
87cdf0e10cSrcweir #include <svl/stritem.hxx>
88cdf0e10cSrcweir #include <svl/eitem.hxx>
89cdf0e10cSrcweir #include <svl/lckbitem.hxx>
90cdf0e10cSrcweir #include <svtools/sfxecode.hxx>
91cdf0e10cSrcweir #include <svl/itemset.hxx>
92cdf0e10cSrcweir #include <svl/intitem.hxx>
93cdf0e10cSrcweir #include <svtools/svparser.hxx> // SvKeyValue
94cdf0e10cSrcweir #include <cppuhelper/weakref.hxx>
95cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
96cdf0e10cSrcweir 
97cdf0e10cSrcweir #define _SVSTDARR_ULONGS
98cdf0e10cSrcweir #define _SVSTDARR_STRINGSDTOR
99cdf0e10cSrcweir #include <svl/svstdarr.hxx>
100cdf0e10cSrcweir 
101cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
102cdf0e10cSrcweir 
103cdf0e10cSrcweir #include <rtl/logfile.hxx>
104cdf0e10cSrcweir #include <osl/file.hxx>
105cdf0e10cSrcweir 
106cdf0e10cSrcweir using namespace ::com::sun::star;
107cdf0e10cSrcweir using namespace ::com::sun::star::uno;
108cdf0e10cSrcweir using namespace ::com::sun::star::ucb;
109cdf0e10cSrcweir using namespace ::com::sun::star::beans;
110cdf0e10cSrcweir using namespace ::com::sun::star::io;
111cdf0e10cSrcweir 
112cdf0e10cSrcweir #include <comphelper/storagehelper.hxx>
113cdf0e10cSrcweir #include <comphelper/mediadescriptor.hxx>
114cdf0e10cSrcweir #include <comphelper/configurationhelper.hxx>
115cdf0e10cSrcweir #include <comphelper/docpasswordhelper.hxx>
116cdf0e10cSrcweir #include <tools/urlobj.hxx>
117cdf0e10cSrcweir #include <tools/inetmime.hxx>
118cdf0e10cSrcweir #include <unotools/ucblockbytes.hxx>
119cdf0e10cSrcweir #include <unotools/pathoptions.hxx>
120cdf0e10cSrcweir #include <svtools/asynclink.hxx>
121cdf0e10cSrcweir #include <svl/inettype.hxx>
122cdf0e10cSrcweir #include <ucbhelper/contentbroker.hxx>
123cdf0e10cSrcweir #include <ucbhelper/commandenvironment.hxx>
124cdf0e10cSrcweir #include <unotools/localfilehelper.hxx>
125cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
126cdf0e10cSrcweir #include <unotools/ucbhelper.hxx>
127cdf0e10cSrcweir #include <unotools/progresshandlerwrap.hxx>
128cdf0e10cSrcweir #include <ucbhelper/content.hxx>
129cdf0e10cSrcweir #include <ucbhelper/interactionrequest.hxx>
130cdf0e10cSrcweir #include <sot/stg.hxx>
131cdf0e10cSrcweir #include <unotools/saveopt.hxx>
132cdf0e10cSrcweir #include <svl/documentlockfile.hxx>
133cdf0e10cSrcweir 
134cdf0e10cSrcweir #include "helper.hxx"
135cdf0e10cSrcweir #include <sfx2/request.hxx>      // SFX_ITEMSET_SET
136cdf0e10cSrcweir #include <sfx2/app.hxx>          // GetFilterMatcher
137cdf0e10cSrcweir #include <sfx2/frame.hxx>        // LoadTargetFrame
138cdf0e10cSrcweir #include "fltfnc.hxx"       // SfxFilterMatcher
139cdf0e10cSrcweir #include <sfx2/docfilt.hxx>      // SfxFilter
140cdf0e10cSrcweir #include <sfx2/objsh.hxx>        // CheckOpenMode
141cdf0e10cSrcweir #include <sfx2/docfac.hxx>       // GetFilterContainer
142cdf0e10cSrcweir #include "doc.hrc"
143cdf0e10cSrcweir #include "openflag.hxx"     // SFX_STREAM_READONLY etc.
144cdf0e10cSrcweir #include "sfx2/sfxresid.hxx"
145cdf0e10cSrcweir #include <sfx2/appuno.hxx>
146cdf0e10cSrcweir 
147cdf0e10cSrcweir //#include "xmlversion.hxx"
148cdf0e10cSrcweir 
149cdf0e10cSrcweir #define MAX_REDIRECT 5
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 
152cdf0e10cSrcweir sal_Bool IsReadonlyAccordingACL( const sal_Unicode* pFilePath );
153cdf0e10cSrcweir 
154cdf0e10cSrcweir //==========================================================
155cdf0e10cSrcweir namespace {
156cdf0e10cSrcweir 
157cdf0e10cSrcweir static const sal_Int8 LOCK_UI_NOLOCK = 0;
158cdf0e10cSrcweir static const sal_Int8 LOCK_UI_SUCCEEDED = 1;
159cdf0e10cSrcweir static const sal_Int8 LOCK_UI_TRY = 2;
160cdf0e10cSrcweir 
161cdf0e10cSrcweir //----------------------------------------------------------------
162cdf0e10cSrcweir sal_Bool IsSystemFileLockingUsed()
163cdf0e10cSrcweir {
164cdf0e10cSrcweir     // check whether system file locking has been used, the default value is false
165cdf0e10cSrcweir     sal_Bool bUseSystemLock = sal_False;
166cdf0e10cSrcweir     try
167cdf0e10cSrcweir     {
168cdf0e10cSrcweir 
169cdf0e10cSrcweir         uno::Reference< uno::XInterface > xCommonConfig = ::comphelper::ConfigurationHelper::openConfig(
170cdf0e10cSrcweir                             ::comphelper::getProcessServiceFactory(),
171cdf0e10cSrcweir                             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.Common" ) ),
172cdf0e10cSrcweir                             ::comphelper::ConfigurationHelper::E_STANDARD );
173cdf0e10cSrcweir         if ( !xCommonConfig.is() )
174cdf0e10cSrcweir             throw uno::RuntimeException();
175cdf0e10cSrcweir 
176cdf0e10cSrcweir         ::comphelper::ConfigurationHelper::readRelativeKey(
177cdf0e10cSrcweir                 xCommonConfig,
178cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Misc/" ) ),
179cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "UseDocumentSystemFileLocking" ) ) ) >>= bUseSystemLock;
180cdf0e10cSrcweir     }
181cdf0e10cSrcweir     catch( const uno::Exception& )
182cdf0e10cSrcweir     {
183cdf0e10cSrcweir     }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir     return bUseSystemLock;
186cdf0e10cSrcweir }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir //----------------------------------------------------------------
189cdf0e10cSrcweir sal_Bool IsOOoLockFileUsed()
190cdf0e10cSrcweir {
191cdf0e10cSrcweir     // check whether system file locking has been used, the default value is false
192cdf0e10cSrcweir     sal_Bool bOOoLockFileUsed = sal_False;
193cdf0e10cSrcweir     try
194cdf0e10cSrcweir     {
195cdf0e10cSrcweir 
196cdf0e10cSrcweir         uno::Reference< uno::XInterface > xCommonConfig = ::comphelper::ConfigurationHelper::openConfig(
197cdf0e10cSrcweir                             ::comphelper::getProcessServiceFactory(),
198cdf0e10cSrcweir                             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.Common" ) ),
199cdf0e10cSrcweir                             ::comphelper::ConfigurationHelper::E_STANDARD );
200cdf0e10cSrcweir         if ( !xCommonConfig.is() )
201cdf0e10cSrcweir             throw uno::RuntimeException();
202cdf0e10cSrcweir 
203cdf0e10cSrcweir         ::comphelper::ConfigurationHelper::readRelativeKey(
204cdf0e10cSrcweir                 xCommonConfig,
205cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Misc/" ) ),
206cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "UseDocumentOOoLockFile" ) ) ) >>= bOOoLockFileUsed;
207cdf0e10cSrcweir     }
208cdf0e10cSrcweir     catch( const uno::Exception& )
209cdf0e10cSrcweir     {
210cdf0e10cSrcweir     }
211cdf0e10cSrcweir 
212cdf0e10cSrcweir     return bOOoLockFileUsed;
213cdf0e10cSrcweir }
214cdf0e10cSrcweir 
215cdf0e10cSrcweir } // anonymous namespace
216cdf0e10cSrcweir //==========================================================
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 
219cdf0e10cSrcweir //----------------------------------------------------------------
220cdf0e10cSrcweir class SfxMediumHandler_Impl : public ::cppu::WeakImplHelper1< com::sun::star::task::XInteractionHandler >
221cdf0e10cSrcweir {
222cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::task::XInteractionHandler > m_xInter;
223cdf0e10cSrcweir 
224cdf0e10cSrcweir public:
225cdf0e10cSrcweir     virtual void SAL_CALL handle( const com::sun::star::uno::Reference< com::sun::star::task::XInteractionRequest >& xRequest )
226cdf0e10cSrcweir             throw( com::sun::star::uno::RuntimeException );
227cdf0e10cSrcweir 
228cdf0e10cSrcweir     SfxMediumHandler_Impl( com::sun::star::uno::Reference< com::sun::star::task::XInteractionHandler > xInteraction )
229cdf0e10cSrcweir         : m_xInter( xInteraction )
230cdf0e10cSrcweir         {}
231cdf0e10cSrcweir 
232cdf0e10cSrcweir     ~SfxMediumHandler_Impl();
233cdf0e10cSrcweir };
234cdf0e10cSrcweir 
235cdf0e10cSrcweir //----------------------------------------------------------------
236cdf0e10cSrcweir SfxMediumHandler_Impl::~SfxMediumHandler_Impl()
237cdf0e10cSrcweir {
238cdf0e10cSrcweir }
239cdf0e10cSrcweir 
240cdf0e10cSrcweir //----------------------------------------------------------------
241cdf0e10cSrcweir void SAL_CALL SfxMediumHandler_Impl::handle( const com::sun::star::uno::Reference< com::sun::star::task::XInteractionRequest >& xRequest )
242cdf0e10cSrcweir         throw( com::sun::star::uno::RuntimeException )
243cdf0e10cSrcweir {
244cdf0e10cSrcweir 	if( !m_xInter.is() )
245cdf0e10cSrcweir 		return;
246cdf0e10cSrcweir 
247cdf0e10cSrcweir     com::sun::star::uno::Any aRequest = xRequest->getRequest();
248cdf0e10cSrcweir     com::sun::star::ucb::InteractiveIOException aIoException;
249cdf0e10cSrcweir     com::sun::star::ucb::UnsupportedDataSinkException aSinkException;
250cdf0e10cSrcweir     if ( (aRequest >>= aIoException) && ( aIoException.Code == IOErrorCode_ACCESS_DENIED || aIoException.Code == IOErrorCode_LOCKING_VIOLATION ) )
251cdf0e10cSrcweir         return;
252cdf0e10cSrcweir     else
253cdf0e10cSrcweir     if ( aRequest >>= aSinkException )
254cdf0e10cSrcweir         return;
255cdf0e10cSrcweir     else
256cdf0e10cSrcweir         m_xInter->handle( xRequest );
257cdf0e10cSrcweir }
258cdf0e10cSrcweir 
259cdf0e10cSrcweir //----------------------------------------------------------------
260cdf0e10cSrcweir class SfxMedium_Impl : public SvCompatWeakBase
261cdf0e10cSrcweir {
262cdf0e10cSrcweir public:
263cdf0e10cSrcweir     ::ucbhelper::Content aContent;
264cdf0e10cSrcweir     sal_Bool bUpdatePickList : 1;
265cdf0e10cSrcweir     sal_Bool bIsTemp        : 1;
266cdf0e10cSrcweir     sal_Bool bForceSynchron : 1;
267cdf0e10cSrcweir     sal_Bool bDownloadDone          : 1;
268cdf0e10cSrcweir     sal_Bool bDontCallDoneLinkOnSharingError : 1;
269cdf0e10cSrcweir     sal_Bool bIsStorage: 1;
270cdf0e10cSrcweir     sal_Bool bUseInteractionHandler: 1;
271cdf0e10cSrcweir     sal_Bool bAllowDefaultIntHdl: 1;
272cdf0e10cSrcweir     sal_Bool bIsCharsetInitialized: 1;
273cdf0e10cSrcweir     sal_Bool bDisposeStorage: 1;
274cdf0e10cSrcweir     sal_Bool bStorageBasedOnInStream: 1;
275cdf0e10cSrcweir     sal_Bool m_bSalvageMode: 1;
276cdf0e10cSrcweir     sal_Bool m_bVersionsAlreadyLoaded: 1;
277cdf0e10cSrcweir     sal_Bool m_bLocked: 1;
278cdf0e10cSrcweir     sal_Bool m_bGotDateTime: 1;
279cdf0e10cSrcweir 
280cdf0e10cSrcweir     uno::Reference < embed::XStorage > xStorage;
281cdf0e10cSrcweir 
282cdf0e10cSrcweir     SfxMedium*       pAntiImpl;
283cdf0e10cSrcweir 
284cdf0e10cSrcweir     long             nFileVersion;
285cdf0e10cSrcweir 
286cdf0e10cSrcweir     const SfxFilter* pOrigFilter;
287cdf0e10cSrcweir     String           aOrigURL;
288cdf0e10cSrcweir     String           aPreRedirectionURL;
289cdf0e10cSrcweir     String           aReferer;
290cdf0e10cSrcweir     DateTime         aExpireTime;
291cdf0e10cSrcweir     SfxFrameWeak     wLoadTargetFrame;
292cdf0e10cSrcweir     SvKeyValueIteratorRef xAttributes;
293cdf0e10cSrcweir 
294cdf0e10cSrcweir     svtools::AsynchronLink  aDoneLink;
295cdf0e10cSrcweir     svtools::AsynchronLink  aAvailableLink;
296cdf0e10cSrcweir 
297cdf0e10cSrcweir     uno::Sequence < util::RevisionTag > aVersions;
298cdf0e10cSrcweir 
299cdf0e10cSrcweir     ::utl::TempFile*           pTempFile;
300cdf0e10cSrcweir 
301cdf0e10cSrcweir     uno::Reference < embed::XStorage > m_xZipStorage;
302cdf0e10cSrcweir     Reference < XInputStream > xInputStream;
303cdf0e10cSrcweir     Reference < XStream > xStream;
304cdf0e10cSrcweir 
305cdf0e10cSrcweir     uno::Reference< io::XStream > m_xLockingStream;
306cdf0e10cSrcweir 
307cdf0e10cSrcweir 	sal_uInt32					nLastStorageError;
308cdf0e10cSrcweir 	::rtl::OUString				aCharset;
309cdf0e10cSrcweir 
310cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler > xInteraction;
311cdf0e10cSrcweir 
312cdf0e10cSrcweir 	sal_Bool 		m_bRemoveBackup;
313cdf0e10cSrcweir 	::rtl::OUString m_aBackupURL;
314cdf0e10cSrcweir 
315cdf0e10cSrcweir 	// the following member is changed and makes sence only during saving
316cdf0e10cSrcweir 	// TODO/LATER: in future the signature state should be controlled by the medium not by the document
317cdf0e10cSrcweir 	//             in this case the member will hold this information
318cdf0e10cSrcweir 	sal_uInt16		m_nSignatureState;
319cdf0e10cSrcweir 
320cdf0e10cSrcweir     util::DateTime m_aDateTime;
321cdf0e10cSrcweir 
322cdf0e10cSrcweir     uno::Reference< logging::XSimpleLogRing > m_xLogRing;
323cdf0e10cSrcweir 
324cdf0e10cSrcweir     SfxMedium_Impl( SfxMedium* pAntiImplP );
325cdf0e10cSrcweir     ~SfxMedium_Impl();
326cdf0e10cSrcweir };
327cdf0e10cSrcweir 
328cdf0e10cSrcweir void SfxMedium::DataAvailable_Impl()
329cdf0e10cSrcweir {
330cdf0e10cSrcweir     pImp->aAvailableLink.ClearPendingCall();
331cdf0e10cSrcweir     pImp->aAvailableLink.Call( NULL );
332cdf0e10cSrcweir }
333cdf0e10cSrcweir 
334cdf0e10cSrcweir void SfxMedium::Cancel_Impl()
335cdf0e10cSrcweir {
336cdf0e10cSrcweir     SetError( ERRCODE_IO_GENERAL, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
337cdf0e10cSrcweir }
338cdf0e10cSrcweir 
339cdf0e10cSrcweir //------------------------------------------------------------------
340cdf0e10cSrcweir SfxMedium_Impl::SfxMedium_Impl( SfxMedium* pAntiImplP )
341cdf0e10cSrcweir  :  SvCompatWeakBase( pAntiImplP ),
342cdf0e10cSrcweir     bUpdatePickList(sal_True),
343cdf0e10cSrcweir     bIsTemp( sal_False ),
344cdf0e10cSrcweir     bForceSynchron( sal_False ),
345cdf0e10cSrcweir     bDownloadDone( sal_True ),
346cdf0e10cSrcweir     bDontCallDoneLinkOnSharingError( sal_False ),
347cdf0e10cSrcweir     bIsStorage( sal_False ),
348cdf0e10cSrcweir     bUseInteractionHandler( sal_True ),
349cdf0e10cSrcweir     bAllowDefaultIntHdl( sal_False ),
350cdf0e10cSrcweir     bIsCharsetInitialized( sal_False ),
351cdf0e10cSrcweir     bStorageBasedOnInStream( sal_False ),
352cdf0e10cSrcweir     m_bSalvageMode( sal_False ),
353cdf0e10cSrcweir     m_bVersionsAlreadyLoaded( sal_False ),
354cdf0e10cSrcweir     m_bLocked( sal_False ),
355cdf0e10cSrcweir     m_bGotDateTime( sal_False ),
356cdf0e10cSrcweir     pAntiImpl( pAntiImplP ),
357cdf0e10cSrcweir     nFileVersion( 0 ),
358cdf0e10cSrcweir     pOrigFilter( 0 ),
359cdf0e10cSrcweir     aExpireTime( Date() + 10, Time() ),
360cdf0e10cSrcweir     pTempFile( NULL ),
361cdf0e10cSrcweir     nLastStorageError( 0 ),
362cdf0e10cSrcweir     m_bRemoveBackup( sal_False ),
363cdf0e10cSrcweir     m_nSignatureState( SIGNATURESTATE_NOSIGNATURES )
364cdf0e10cSrcweir {
365cdf0e10cSrcweir     aDoneLink.CreateMutex();
366cdf0e10cSrcweir }
367cdf0e10cSrcweir 
368cdf0e10cSrcweir //------------------------------------------------------------------
369cdf0e10cSrcweir SfxMedium_Impl::~SfxMedium_Impl()
370cdf0e10cSrcweir {
371cdf0e10cSrcweir 
372cdf0e10cSrcweir     aDoneLink.ClearPendingCall();
373cdf0e10cSrcweir     aAvailableLink.ClearPendingCall();
374cdf0e10cSrcweir 
375cdf0e10cSrcweir     if ( pTempFile )
376cdf0e10cSrcweir         delete pTempFile;
377cdf0e10cSrcweir }
378cdf0e10cSrcweir 
379cdf0e10cSrcweir //================================================================
380cdf0e10cSrcweir 
381cdf0e10cSrcweir #define IMPL_CTOR(rootVal,URLVal)			\
382cdf0e10cSrcweir      eError( SVSTREAM_OK ),                 \
383cdf0e10cSrcweir                                             \
384cdf0e10cSrcweir      bDirect( sal_False ),                  \
385cdf0e10cSrcweir      bRoot( rootVal ),						\
386cdf0e10cSrcweir      bSetFilter( sal_False ),               \
387cdf0e10cSrcweir      bTriedStorage( sal_False ),            \
388cdf0e10cSrcweir                                             \
389cdf0e10cSrcweir      nStorOpenMode( SFX_STREAM_READWRITE ), \
390cdf0e10cSrcweir      pURLObj( URLVal ),						\
391cdf0e10cSrcweir      pInStream(0),                          \
392cdf0e10cSrcweir      pOutStream( 0 )
393cdf0e10cSrcweir 
394cdf0e10cSrcweir //------------------------------------------------------------------
395cdf0e10cSrcweir void SfxMedium::ResetError()
396cdf0e10cSrcweir {
397cdf0e10cSrcweir     eError = SVSTREAM_OK;
398cdf0e10cSrcweir     if( pInStream )
399cdf0e10cSrcweir         pInStream->ResetError();
400cdf0e10cSrcweir     if( pOutStream )
401cdf0e10cSrcweir         pOutStream->ResetError();
402cdf0e10cSrcweir }
403cdf0e10cSrcweir 
404cdf0e10cSrcweir //------------------------------------------------------------------
405cdf0e10cSrcweir sal_uInt32 SfxMedium::GetLastStorageCreationState()
406cdf0e10cSrcweir {
407cdf0e10cSrcweir 	return pImp->nLastStorageError;
408cdf0e10cSrcweir }
409cdf0e10cSrcweir 
410cdf0e10cSrcweir //------------------------------------------------------------------
411cdf0e10cSrcweir void SfxMedium::AddLog( const ::rtl::OUString& aMessage )
412cdf0e10cSrcweir {
413cdf0e10cSrcweir     if ( !pImp->m_xLogRing.is() )
414cdf0e10cSrcweir     {
415cdf0e10cSrcweir         try
416cdf0e10cSrcweir         {
417cdf0e10cSrcweir             ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
418cdf0e10cSrcweir             if ( aContext.is() )
419cdf0e10cSrcweir                 pImp->m_xLogRing.set( aContext.getSingleton( "com.sun.star.logging.DocumentIOLogRing" ), UNO_QUERY_THROW );
420cdf0e10cSrcweir         }
421cdf0e10cSrcweir         catch( uno::Exception& )
422cdf0e10cSrcweir         {}
423cdf0e10cSrcweir     }
424cdf0e10cSrcweir 
425cdf0e10cSrcweir     if ( pImp->m_xLogRing.is() )
426cdf0e10cSrcweir         pImp->m_xLogRing->logString( aMessage );
427cdf0e10cSrcweir }
428cdf0e10cSrcweir 
429cdf0e10cSrcweir //------------------------------------------------------------------
430cdf0e10cSrcweir void SfxMedium::SetError( sal_uInt32 nError, const ::rtl::OUString& aLogMessage )
431cdf0e10cSrcweir {
432cdf0e10cSrcweir     eError = nError;
433cdf0e10cSrcweir     if ( eError != ERRCODE_NONE && aLogMessage.getLength() )
434cdf0e10cSrcweir         AddLog( aLogMessage );
435cdf0e10cSrcweir }
436cdf0e10cSrcweir 
437cdf0e10cSrcweir //------------------------------------------------------------------
438cdf0e10cSrcweir sal_uInt32 SfxMedium::GetErrorCode() const
439cdf0e10cSrcweir {
440cdf0e10cSrcweir     sal_uInt32 lError=eError;
441cdf0e10cSrcweir     if(!lError && pInStream)
442cdf0e10cSrcweir         lError=pInStream->GetErrorCode();
443cdf0e10cSrcweir     if(!lError && pOutStream)
444cdf0e10cSrcweir         lError=pOutStream->GetErrorCode();
445cdf0e10cSrcweir     return lError;
446cdf0e10cSrcweir }
447cdf0e10cSrcweir 
448cdf0e10cSrcweir //------------------------------------------------------------------
449cdf0e10cSrcweir void SfxMedium::CheckFileDate( const util::DateTime& aInitDate )
450cdf0e10cSrcweir {
451cdf0e10cSrcweir     GetInitFileDate( sal_True );
452cdf0e10cSrcweir     if ( pImp->m_aDateTime.Seconds != aInitDate.Seconds
453cdf0e10cSrcweir       || pImp->m_aDateTime.Minutes != aInitDate.Minutes
454cdf0e10cSrcweir       || pImp->m_aDateTime.Hours != aInitDate.Hours
455cdf0e10cSrcweir       || pImp->m_aDateTime.Day != aInitDate.Day
456cdf0e10cSrcweir       || pImp->m_aDateTime.Month != aInitDate.Month
457cdf0e10cSrcweir       || pImp->m_aDateTime.Year != aInitDate.Year )
458cdf0e10cSrcweir     {
459cdf0e10cSrcweir         uno::Reference< task::XInteractionHandler > xHandler = GetInteractionHandler();
460cdf0e10cSrcweir 
461cdf0e10cSrcweir         if ( xHandler.is() )
462cdf0e10cSrcweir         {
463cdf0e10cSrcweir             try
464cdf0e10cSrcweir             {
465cdf0e10cSrcweir                 ::rtl::Reference< ::ucbhelper::InteractionRequest > xInteractionRequestImpl = new ::ucbhelper::InteractionRequest( uno::makeAny(
466cdf0e10cSrcweir                     document::ChangedByOthersRequest() ) );
467cdf0e10cSrcweir                 uno::Sequence< uno::Reference< task::XInteractionContinuation > > aContinuations( 3 );
468cdf0e10cSrcweir                 aContinuations[0] = new ::ucbhelper::InteractionAbort( xInteractionRequestImpl.get() );
469cdf0e10cSrcweir                 aContinuations[1] = new ::ucbhelper::InteractionApprove( xInteractionRequestImpl.get() );
470cdf0e10cSrcweir                 xInteractionRequestImpl->setContinuations( aContinuations );
471cdf0e10cSrcweir 
472cdf0e10cSrcweir                 xHandler->handle( xInteractionRequestImpl.get() );
473cdf0e10cSrcweir 
474cdf0e10cSrcweir                 ::rtl::Reference< ::ucbhelper::InteractionContinuation > xSelected = xInteractionRequestImpl->getSelection();
475cdf0e10cSrcweir                 if ( uno::Reference< task::XInteractionAbort >( xSelected.get(), uno::UNO_QUERY ).is() )
476cdf0e10cSrcweir                 {
477cdf0e10cSrcweir                     SetError( ERRCODE_ABORT, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
478cdf0e10cSrcweir                 }
479cdf0e10cSrcweir             }
480cdf0e10cSrcweir             catch ( uno::Exception& )
481cdf0e10cSrcweir             {}
482cdf0e10cSrcweir         }
483cdf0e10cSrcweir     }
484cdf0e10cSrcweir }
485cdf0e10cSrcweir 
486cdf0e10cSrcweir //------------------------------------------------------------------
487cdf0e10cSrcweir sal_Bool SfxMedium::DocNeedsFileDateCheck()
488cdf0e10cSrcweir {
489cdf0e10cSrcweir     return ( !IsReadOnly() && ::utl::LocalFileHelper::IsLocalFile( GetURLObject().GetMainURL( INetURLObject::NO_DECODE ) ) );
490cdf0e10cSrcweir }
491cdf0e10cSrcweir 
492cdf0e10cSrcweir //------------------------------------------------------------------
493cdf0e10cSrcweir util::DateTime SfxMedium::GetInitFileDate( sal_Bool bIgnoreOldValue )
494cdf0e10cSrcweir {
495cdf0e10cSrcweir     if ( ( bIgnoreOldValue || !pImp->m_bGotDateTime ) && aLogicName.Len() )
496cdf0e10cSrcweir     {
497cdf0e10cSrcweir         try
498cdf0e10cSrcweir         {
499cdf0e10cSrcweir             uno::Reference< ::com::sun::star::ucb::XCommandEnvironment > xDummyEnv;
500cdf0e10cSrcweir             ::ucbhelper::Content aContent( GetURLObject().GetMainURL( INetURLObject::NO_DECODE ), xDummyEnv );
501cdf0e10cSrcweir 
502cdf0e10cSrcweir             aContent.getPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "DateModified" )) ) >>= pImp->m_aDateTime;
503cdf0e10cSrcweir             pImp->m_bGotDateTime = sal_True;
504cdf0e10cSrcweir         }
505cdf0e10cSrcweir         catch ( ::com::sun::star::uno::Exception& )
506cdf0e10cSrcweir         {
507cdf0e10cSrcweir         }
508cdf0e10cSrcweir     }
509cdf0e10cSrcweir 
510cdf0e10cSrcweir     return pImp->m_aDateTime;
511cdf0e10cSrcweir }
512cdf0e10cSrcweir 
513cdf0e10cSrcweir //------------------------------------------------------------------
514cdf0e10cSrcweir Reference < XContent > SfxMedium::GetContent() const
515cdf0e10cSrcweir {
516cdf0e10cSrcweir     if ( !pImp->aContent.get().is() )
517cdf0e10cSrcweir     {
518cdf0e10cSrcweir 		Reference < ::com::sun::star::ucb::XContent > xContent;
519cdf0e10cSrcweir 	    Reference < ::com::sun::star::ucb::XCommandEnvironment > xEnv;
520cdf0e10cSrcweir 
521cdf0e10cSrcweir         SFX_ITEMSET_ARG( pSet, pItem, SfxUnoAnyItem, SID_CONTENT, sal_False);
522cdf0e10cSrcweir         if ( pItem )
523cdf0e10cSrcweir             pItem->GetValue() >>= xContent;
524cdf0e10cSrcweir 
525cdf0e10cSrcweir 		if ( xContent.is() )
526cdf0e10cSrcweir 		{
527cdf0e10cSrcweir 			try
528cdf0e10cSrcweir 			{
529cdf0e10cSrcweir 				pImp->aContent = ::ucbhelper::Content( xContent, xEnv );
530cdf0e10cSrcweir 			}
531cdf0e10cSrcweir 			catch ( Exception& )
532cdf0e10cSrcweir 			{
533cdf0e10cSrcweir 			}
534cdf0e10cSrcweir 		}
535cdf0e10cSrcweir 		else
536cdf0e10cSrcweir 		{
537cdf0e10cSrcweir 			// TODO: DBG_ERROR("SfxMedium::GetContent()\nCreate Content? This code exists as fallback only. Please clarify, why its used.");
538cdf0e10cSrcweir 			String aURL;
539cdf0e10cSrcweir 	        if ( aName.Len() )
540cdf0e10cSrcweir 	        	::utl::LocalFileHelper::ConvertPhysicalNameToURL( aName, aURL );
541cdf0e10cSrcweir 	        else if ( aLogicName.Len() )
542cdf0e10cSrcweir 	            aURL = GetURLObject().GetMainURL( INetURLObject::NO_DECODE );
543cdf0e10cSrcweir 			if ( aURL.Len() )
544cdf0e10cSrcweir 	            ::ucbhelper::Content::create( aURL, xEnv, pImp->aContent );
545cdf0e10cSrcweir 		}
546cdf0e10cSrcweir     }
547cdf0e10cSrcweir 
548cdf0e10cSrcweir     return pImp->aContent.get();
549cdf0e10cSrcweir }
550cdf0e10cSrcweir 
551cdf0e10cSrcweir //------------------------------------------------------------------
552cdf0e10cSrcweir ::rtl::OUString SfxMedium::GetBaseURL( bool bForSaving )
553cdf0e10cSrcweir {
554cdf0e10cSrcweir     ::rtl::OUString aBaseURL;
555cdf0e10cSrcweir     const SfxStringItem* pBaseURLItem = static_cast<const SfxStringItem*>( GetItemSet()->GetItem(SID_DOC_BASEURL) );
556cdf0e10cSrcweir     if ( pBaseURLItem )
557cdf0e10cSrcweir         aBaseURL = pBaseURLItem->GetValue();
558cdf0e10cSrcweir     else if ( GetContent().is() )
559cdf0e10cSrcweir     {
560cdf0e10cSrcweir         try
561cdf0e10cSrcweir         {
562cdf0e10cSrcweir             Any aAny = pImp->aContent.getPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("BaseURI" )) );
563cdf0e10cSrcweir             aAny >>= aBaseURL;
564cdf0e10cSrcweir         }
565cdf0e10cSrcweir         catch ( ::com::sun::star::uno::Exception& )
566cdf0e10cSrcweir         {
567cdf0e10cSrcweir         }
568cdf0e10cSrcweir 
569cdf0e10cSrcweir         if ( !aBaseURL.getLength() )
570cdf0e10cSrcweir             aBaseURL = GetURLObject().GetMainURL( INetURLObject::NO_DECODE );
571cdf0e10cSrcweir     }
572cdf0e10cSrcweir 
573cdf0e10cSrcweir     if ( bForSaving )
574cdf0e10cSrcweir     {
575cdf0e10cSrcweir         SvtSaveOptions aOpt;
576cdf0e10cSrcweir         sal_Bool bIsRemote = IsRemote();
577cdf0e10cSrcweir         if( (bIsRemote && !aOpt.IsSaveRelINet()) || (!bRemote && !aOpt.IsSaveRelFSys()) )
578cdf0e10cSrcweir             return ::rtl::OUString();
579cdf0e10cSrcweir     }
580cdf0e10cSrcweir 
581cdf0e10cSrcweir     return aBaseURL;
582cdf0e10cSrcweir }
583cdf0e10cSrcweir 
584cdf0e10cSrcweir //------------------------------------------------------------------
585cdf0e10cSrcweir SvStream* SfxMedium::GetInStream()
586cdf0e10cSrcweir {
587cdf0e10cSrcweir     if ( pInStream )
588cdf0e10cSrcweir         return pInStream;
589cdf0e10cSrcweir 
590cdf0e10cSrcweir     if ( pImp->pTempFile )
591cdf0e10cSrcweir     {
592cdf0e10cSrcweir         pInStream = new SvFileStream( aName, nStorOpenMode );
593cdf0e10cSrcweir 
594cdf0e10cSrcweir         eError = pInStream->GetError();
595cdf0e10cSrcweir 
596cdf0e10cSrcweir         if( !eError && (nStorOpenMode & STREAM_WRITE)
597cdf0e10cSrcweir                     && ! pInStream->IsWritable() )
598cdf0e10cSrcweir         {
599cdf0e10cSrcweir             eError = ERRCODE_IO_ACCESSDENIED;
600cdf0e10cSrcweir             delete pInStream;
601cdf0e10cSrcweir             pInStream = NULL;
602cdf0e10cSrcweir         }
603cdf0e10cSrcweir         else
604cdf0e10cSrcweir             return pInStream;
605cdf0e10cSrcweir     }
606cdf0e10cSrcweir 
607cdf0e10cSrcweir     GetMedium_Impl();
608cdf0e10cSrcweir 
609cdf0e10cSrcweir     if ( GetError() )
610cdf0e10cSrcweir         return NULL;
611cdf0e10cSrcweir 
612cdf0e10cSrcweir     return pInStream;
613cdf0e10cSrcweir }
614cdf0e10cSrcweir 
615cdf0e10cSrcweir //------------------------------------------------------------------
616cdf0e10cSrcweir void SfxMedium::CloseInStream()
617cdf0e10cSrcweir {
618cdf0e10cSrcweir     CloseInStream_Impl();
619cdf0e10cSrcweir }
620cdf0e10cSrcweir 
621cdf0e10cSrcweir void SfxMedium::CloseInStream_Impl()
622cdf0e10cSrcweir {
623cdf0e10cSrcweir     // if there is a storage based on the InStream, we have to
624cdf0e10cSrcweir     // close the storage, too, because otherwise the storage
625cdf0e10cSrcweir     // would use an invalid ( deleted ) stream.
626cdf0e10cSrcweir     if ( pInStream && pImp->xStorage.is() )
627cdf0e10cSrcweir     {
628cdf0e10cSrcweir     	if ( pImp->bStorageBasedOnInStream )
629cdf0e10cSrcweir             CloseStorage();
630cdf0e10cSrcweir     }
631cdf0e10cSrcweir 
632cdf0e10cSrcweir     if ( pInStream && !GetContent().is() )
633cdf0e10cSrcweir     {
634cdf0e10cSrcweir         CreateTempFile( sal_True );
635cdf0e10cSrcweir         return;
636cdf0e10cSrcweir     }
637cdf0e10cSrcweir 
638cdf0e10cSrcweir     DELETEZ( pInStream );
639cdf0e10cSrcweir     if ( pSet )
640cdf0e10cSrcweir         pSet->ClearItem( SID_INPUTSTREAM );
641cdf0e10cSrcweir 
642cdf0e10cSrcweir 	CloseZipStorage_Impl();
643cdf0e10cSrcweir 	pImp->xInputStream = uno::Reference< io::XInputStream >();
644cdf0e10cSrcweir 
645cdf0e10cSrcweir 	if ( !pOutStream )
646cdf0e10cSrcweir 	{
647cdf0e10cSrcweir 		// output part of the stream is not used so the whole stream can be closed
648cdf0e10cSrcweir         // TODO/LATER: is it correct?
649cdf0e10cSrcweir 		pImp->xStream = uno::Reference< io::XStream >();
650cdf0e10cSrcweir 		if ( pSet )
651cdf0e10cSrcweir 			pSet->ClearItem( SID_STREAM );
652cdf0e10cSrcweir 	}
653cdf0e10cSrcweir }
654cdf0e10cSrcweir 
655cdf0e10cSrcweir //------------------------------------------------------------------
656cdf0e10cSrcweir SvStream* SfxMedium::GetOutStream()
657cdf0e10cSrcweir {
658cdf0e10cSrcweir     if ( !pOutStream )
659cdf0e10cSrcweir     {
660cdf0e10cSrcweir         // Create a temp. file if there is none because we always
661cdf0e10cSrcweir         // need one.
662cdf0e10cSrcweir         CreateTempFile( sal_False );
663cdf0e10cSrcweir 
664cdf0e10cSrcweir         if ( pImp->pTempFile )
665cdf0e10cSrcweir         {
666cdf0e10cSrcweir             pOutStream = new SvFileStream( aName, STREAM_STD_READWRITE );
667cdf0e10cSrcweir             CloseStorage();
668cdf0e10cSrcweir         }
669cdf0e10cSrcweir     }
670cdf0e10cSrcweir 
671cdf0e10cSrcweir     return pOutStream;
672cdf0e10cSrcweir }
673cdf0e10cSrcweir 
674cdf0e10cSrcweir //------------------------------------------------------------------
675cdf0e10cSrcweir sal_Bool SfxMedium::CloseOutStream()
676cdf0e10cSrcweir {
677cdf0e10cSrcweir     CloseOutStream_Impl();
678cdf0e10cSrcweir     return sal_True;
679cdf0e10cSrcweir }
680cdf0e10cSrcweir 
681cdf0e10cSrcweir sal_Bool SfxMedium::CloseOutStream_Impl()
682cdf0e10cSrcweir {
683cdf0e10cSrcweir     if ( pOutStream )
684cdf0e10cSrcweir     {
685cdf0e10cSrcweir         // if there is a storage based on the OutStream, we have to
686cdf0e10cSrcweir         // close the storage, too, because otherwise the storage
687cdf0e10cSrcweir         // would use an invalid ( deleted ) stream.
688cdf0e10cSrcweir         //TODO/MBA: how to deal with this?!
689cdf0e10cSrcweir         //maybe we need a new flag when the storage was created from the outstream
690cdf0e10cSrcweir         if ( pImp->xStorage.is() )
691cdf0e10cSrcweir         {
692cdf0e10cSrcweir             //const SvStream *pStorage = aStorage->GetSvStream();
693cdf0e10cSrcweir             //if ( pStorage == pOutStream )
694cdf0e10cSrcweir                 CloseStorage();
695cdf0e10cSrcweir         }
696cdf0e10cSrcweir 
697cdf0e10cSrcweir         delete pOutStream;
698cdf0e10cSrcweir         pOutStream = NULL;
699cdf0e10cSrcweir     }
700cdf0e10cSrcweir 
701cdf0e10cSrcweir 	if ( !pInStream )
702cdf0e10cSrcweir 	{
703cdf0e10cSrcweir 		// input part of the stream is not used so the whole stream can be closed
704cdf0e10cSrcweir         // TODO/LATER: is it correct?
705cdf0e10cSrcweir 		pImp->xStream = uno::Reference< io::XStream >();
706cdf0e10cSrcweir     	if ( pSet )
707cdf0e10cSrcweir 			pSet->ClearItem( SID_STREAM );
708cdf0e10cSrcweir 	}
709cdf0e10cSrcweir 
710cdf0e10cSrcweir     return sal_True;
711cdf0e10cSrcweir }
712cdf0e10cSrcweir 
713cdf0e10cSrcweir //------------------------------------------------------------------
714cdf0e10cSrcweir const String& SfxMedium::GetPhysicalName() const
715cdf0e10cSrcweir {
716cdf0e10cSrcweir     if ( !aName.Len() && aLogicName.Len() )
717cdf0e10cSrcweir         (( SfxMedium*)this)->CreateFileStream();
718cdf0e10cSrcweir 
719cdf0e10cSrcweir     // return the name then
720cdf0e10cSrcweir     return aName;
721cdf0e10cSrcweir }
722cdf0e10cSrcweir 
723cdf0e10cSrcweir //------------------------------------------------------------------
724cdf0e10cSrcweir void SfxMedium::CreateFileStream()
725cdf0e10cSrcweir {
726cdf0e10cSrcweir     ForceSynchronStream_Impl( sal_True );
727cdf0e10cSrcweir     GetInStream();
728cdf0e10cSrcweir     if( pInStream )
729cdf0e10cSrcweir     {
730cdf0e10cSrcweir         CreateTempFile( sal_False );
731cdf0e10cSrcweir         pImp->bIsTemp = sal_True;
732cdf0e10cSrcweir         CloseInStream_Impl();
733cdf0e10cSrcweir     }
734cdf0e10cSrcweir }
735cdf0e10cSrcweir 
736cdf0e10cSrcweir //------------------------------------------------------------------
737cdf0e10cSrcweir sal_Bool SfxMedium::Commit()
738cdf0e10cSrcweir {
739cdf0e10cSrcweir     if( pImp->xStorage.is() )
740cdf0e10cSrcweir 		StorageCommit_Impl();
741cdf0e10cSrcweir     else if( pOutStream  )
742cdf0e10cSrcweir         pOutStream->Flush();
743cdf0e10cSrcweir     else if( pInStream  )
744cdf0e10cSrcweir         pInStream->Flush();
745cdf0e10cSrcweir 
746cdf0e10cSrcweir     if ( GetError() == SVSTREAM_OK )
747cdf0e10cSrcweir 	{
748cdf0e10cSrcweir 		// does something only in case there is a temporary file ( means aName points to different location than aLogicName )
749cdf0e10cSrcweir         Transfer_Impl();
750cdf0e10cSrcweir 	}
751cdf0e10cSrcweir 
752cdf0e10cSrcweir     sal_Bool bResult = ( GetError() == SVSTREAM_OK );
753cdf0e10cSrcweir 
754cdf0e10cSrcweir     if ( bResult && DocNeedsFileDateCheck() )
755cdf0e10cSrcweir         GetInitFileDate( sal_True );
756cdf0e10cSrcweir 
757cdf0e10cSrcweir     // remove truncation mode from the flags
758cdf0e10cSrcweir     nStorOpenMode &= (~STREAM_TRUNC);
759cdf0e10cSrcweir     return bResult;
760cdf0e10cSrcweir }
761cdf0e10cSrcweir 
762cdf0e10cSrcweir //------------------------------------------------------------------
763cdf0e10cSrcweir sal_Bool SfxMedium::IsStorage()
764cdf0e10cSrcweir {
765cdf0e10cSrcweir     if ( pImp->xStorage.is() )
766cdf0e10cSrcweir         return sal_True;
767cdf0e10cSrcweir 
768cdf0e10cSrcweir     if ( bTriedStorage )
769cdf0e10cSrcweir         return pImp->bIsStorage;
770cdf0e10cSrcweir 
771cdf0e10cSrcweir     if ( pImp->pTempFile )
772cdf0e10cSrcweir     {
773cdf0e10cSrcweir 		String aURL;
774cdf0e10cSrcweir 		if ( !::utl::LocalFileHelper::ConvertPhysicalNameToURL( aName, aURL ) )
775cdf0e10cSrcweir         {
776cdf0e10cSrcweir 			DBG_ERROR("Physical name not convertable!");
777cdf0e10cSrcweir         }
778cdf0e10cSrcweir         pImp->bIsStorage = SotStorage::IsStorageFile( aURL ) && !SotStorage::IsOLEStorage( aURL);
779cdf0e10cSrcweir         if ( !pImp->bIsStorage )
780cdf0e10cSrcweir             bTriedStorage = sal_True;
781cdf0e10cSrcweir     }
782cdf0e10cSrcweir     else if ( GetInStream() )
783cdf0e10cSrcweir     {
784cdf0e10cSrcweir         pImp->bIsStorage = SotStorage::IsStorageFile( pInStream ) && !SotStorage::IsOLEStorage( pInStream );
785cdf0e10cSrcweir         if ( !pInStream->GetError() && !pImp->bIsStorage )
786cdf0e10cSrcweir             bTriedStorage = sal_True;
787cdf0e10cSrcweir     }
788cdf0e10cSrcweir 
789cdf0e10cSrcweir     return pImp->bIsStorage;
790cdf0e10cSrcweir }
791cdf0e10cSrcweir 
792cdf0e10cSrcweir //------------------------------------------------------------------
793cdf0e10cSrcweir Link SfxMedium::GetDataAvailableLink() const
794cdf0e10cSrcweir {
795cdf0e10cSrcweir     return pImp->aAvailableLink.GetLink();
796cdf0e10cSrcweir }
797cdf0e10cSrcweir 
798cdf0e10cSrcweir //------------------------------------------------------------------
799cdf0e10cSrcweir Link SfxMedium::GetDoneLink() const
800cdf0e10cSrcweir {
801cdf0e10cSrcweir     return pImp->aDoneLink.GetLink();
802cdf0e10cSrcweir }
803cdf0e10cSrcweir 
804cdf0e10cSrcweir //------------------------------------------------------------------
805cdf0e10cSrcweir sal_Bool SfxMedium::IsPreview_Impl()
806cdf0e10cSrcweir {
807cdf0e10cSrcweir     sal_Bool bPreview = sal_False;
808cdf0e10cSrcweir     SFX_ITEMSET_ARG( GetItemSet(), pPreview, SfxBoolItem, SID_PREVIEW, sal_False);
809cdf0e10cSrcweir     if ( pPreview )
810cdf0e10cSrcweir         bPreview = pPreview->GetValue();
811cdf0e10cSrcweir     else
812cdf0e10cSrcweir     {
813cdf0e10cSrcweir         SFX_ITEMSET_ARG( GetItemSet(), pFlags, SfxStringItem, SID_OPTIONS, sal_False);
814cdf0e10cSrcweir         if ( pFlags )
815cdf0e10cSrcweir         {
816cdf0e10cSrcweir             String aFileFlags = pFlags->GetValue();
817cdf0e10cSrcweir             aFileFlags.ToUpperAscii();
818cdf0e10cSrcweir             if ( STRING_NOTFOUND != aFileFlags.Search( 'B' ) )
819cdf0e10cSrcweir                 bPreview = sal_True;
820cdf0e10cSrcweir         }
821cdf0e10cSrcweir     }
822cdf0e10cSrcweir 
823cdf0e10cSrcweir     return bPreview;
824cdf0e10cSrcweir }
825cdf0e10cSrcweir 
826cdf0e10cSrcweir //------------------------------------------------------------------
827cdf0e10cSrcweir void SfxMedium::StorageBackup_Impl()
828cdf0e10cSrcweir {
829cdf0e10cSrcweir 	::ucbhelper::Content aOriginalContent;
830cdf0e10cSrcweir 	Reference< ::com::sun::star::ucb::XCommandEnvironment > xDummyEnv;
831cdf0e10cSrcweir 
832cdf0e10cSrcweir     sal_Bool bBasedOnOriginalFile = ( !pImp->pTempFile && !( aLogicName.Len() && pImp->m_bSalvageMode )
833cdf0e10cSrcweir         && GetURLObject().GetMainURL( INetURLObject::NO_DECODE ).getLength()
834cdf0e10cSrcweir         && ::utl::LocalFileHelper::IsLocalFile( GetURLObject().GetMainURL( INetURLObject::NO_DECODE ) )
835cdf0e10cSrcweir         && ::utl::UCBContentHelper::IsDocument( GetURLObject().GetMainURL( INetURLObject::NO_DECODE ) ) );
836cdf0e10cSrcweir 
837cdf0e10cSrcweir 	if ( bBasedOnOriginalFile && !pImp->m_aBackupURL.getLength()
838cdf0e10cSrcweir 	  && ::ucbhelper::Content::create( GetURLObject().GetMainURL( INetURLObject::NO_DECODE ), xDummyEnv, aOriginalContent ) )
839cdf0e10cSrcweir 	{
840cdf0e10cSrcweir 		DoInternalBackup_Impl( aOriginalContent );
841cdf0e10cSrcweir 		if( !pImp->m_aBackupURL.getLength() )
842cdf0e10cSrcweir 			SetError( ERRCODE_SFX_CANTCREATEBACKUP, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
843cdf0e10cSrcweir 	}
844cdf0e10cSrcweir }
845cdf0e10cSrcweir 
846cdf0e10cSrcweir //------------------------------------------------------------------
847cdf0e10cSrcweir ::rtl::OUString SfxMedium::GetBackup_Impl()
848cdf0e10cSrcweir {
849cdf0e10cSrcweir 	if ( !pImp->m_aBackupURL.getLength() )
850cdf0e10cSrcweir 		StorageBackup_Impl();
851cdf0e10cSrcweir 
852cdf0e10cSrcweir 	return pImp->m_aBackupURL;
853cdf0e10cSrcweir }
854cdf0e10cSrcweir 
855cdf0e10cSrcweir //------------------------------------------------------------------
856cdf0e10cSrcweir uno::Reference < embed::XStorage > SfxMedium::GetOutputStorage()
857cdf0e10cSrcweir {
858cdf0e10cSrcweir 	if ( GetError() )
859cdf0e10cSrcweir 		return uno::Reference< embed::XStorage >();
860cdf0e10cSrcweir 
861cdf0e10cSrcweir     // if the medium was constructed with a Storage: use this one, not a temp. storage
862cdf0e10cSrcweir     // if a temporary storage already exists: use it
863cdf0e10cSrcweir     if ( pImp->xStorage.is() && ( !aLogicName.Len() || pImp->pTempFile ) )
864cdf0e10cSrcweir         return pImp->xStorage;
865cdf0e10cSrcweir 
866cdf0e10cSrcweir     // if necessary close stream that was used for reading
867cdf0e10cSrcweir     if ( pInStream && !pInStream->IsWritable() )
868cdf0e10cSrcweir         CloseInStream();
869cdf0e10cSrcweir 
870cdf0e10cSrcweir     DBG_ASSERT( !pOutStream, "OutStream in a readonly Medium?!" );
871cdf0e10cSrcweir 
872cdf0e10cSrcweir     // TODO/LATER: The current solution is to store the document temporary and then copy it to the target location;
873cdf0e10cSrcweir     // in future it should be stored directly and then copied to the temporary location, since in this case no
874cdf0e10cSrcweir     // file attributes have to be preserved and system copying mechanics could be used instead of streaming.
875cdf0e10cSrcweir     CreateTempFileNoCopy();
876cdf0e10cSrcweir 
877cdf0e10cSrcweir     return GetStorage();
878cdf0e10cSrcweir }
879cdf0e10cSrcweir 
880cdf0e10cSrcweir //------------------------------------------------------------------
881cdf0e10cSrcweir void SfxMedium::SetEncryptionDataToStorage_Impl()
882cdf0e10cSrcweir {
883cdf0e10cSrcweir 	// in case media-descriptor contains password it should be used on opening
884cdf0e10cSrcweir 	if ( pImp->xStorage.is() && pSet )
885cdf0e10cSrcweir 	{
886cdf0e10cSrcweir         uno::Sequence< beans::NamedValue > aEncryptionData;
887cdf0e10cSrcweir 		if ( GetEncryptionData_Impl( pSet, aEncryptionData ) )
888cdf0e10cSrcweir 		{
889cdf0e10cSrcweir             // replace the password with encryption data
890cdf0e10cSrcweir             pSet->ClearItem( SID_PASSWORD );
891cdf0e10cSrcweir             pSet->Put( SfxUnoAnyItem( SID_ENCRYPTIONDATA, uno::makeAny( aEncryptionData ) ) );
892cdf0e10cSrcweir 
893cdf0e10cSrcweir 			try
894cdf0e10cSrcweir 			{
895cdf0e10cSrcweir 				::comphelper::OStorageHelper::SetCommonStorageEncryptionData( pImp->xStorage, aEncryptionData );
896cdf0e10cSrcweir 			}
897cdf0e10cSrcweir 			catch( uno::Exception& )
898cdf0e10cSrcweir 			{
899cdf0e10cSrcweir 				OSL_ENSURE( sal_False, "It must be possible to set a common password for the storage" );
900cdf0e10cSrcweir 				// TODO/LATER: set the error code in case of problem
901cdf0e10cSrcweir 				// SetError( ERRCODE_IO_GENERAL, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
902cdf0e10cSrcweir 			}
903cdf0e10cSrcweir 		}
904cdf0e10cSrcweir 	}
905cdf0e10cSrcweir }
906cdf0e10cSrcweir 
907cdf0e10cSrcweir //------------------------------------------------------------------
908cdf0e10cSrcweir sal_Int8 SfxMedium::ShowLockedDocumentDialog( const uno::Sequence< ::rtl::OUString >& aData, sal_Bool bIsLoading, sal_Bool bOwnLock )
909cdf0e10cSrcweir {
910cdf0e10cSrcweir     sal_Int8 nResult = LOCK_UI_NOLOCK;
911cdf0e10cSrcweir 
912cdf0e10cSrcweir     // show the interaction regarding the document opening
913cdf0e10cSrcweir     uno::Reference< task::XInteractionHandler > xHandler = GetInteractionHandler();
914cdf0e10cSrcweir 
915cdf0e10cSrcweir     if ( ::svt::DocumentLockFile::IsInteractionAllowed() && xHandler.is() && ( bIsLoading || bOwnLock ) )
916cdf0e10cSrcweir     {
917cdf0e10cSrcweir         ::rtl::OUString aDocumentURL = GetURLObject().GetLastName();
918cdf0e10cSrcweir         ::rtl::OUString aInfo;
919cdf0e10cSrcweir         ::rtl::Reference< ::ucbhelper::InteractionRequest > xInteractionRequestImpl;
920cdf0e10cSrcweir 
921cdf0e10cSrcweir         if ( bOwnLock )
922cdf0e10cSrcweir         {
923cdf0e10cSrcweir             if ( aData.getLength() > LOCKFILE_EDITTIME_ID )
924cdf0e10cSrcweir                 aInfo = aData[LOCKFILE_EDITTIME_ID];
925cdf0e10cSrcweir 
926cdf0e10cSrcweir             xInteractionRequestImpl = new ::ucbhelper::InteractionRequest( uno::makeAny(
927cdf0e10cSrcweir                 document::OwnLockOnDocumentRequest( ::rtl::OUString(), uno::Reference< uno::XInterface >(), aDocumentURL, aInfo, !bIsLoading ) ) );
928cdf0e10cSrcweir         }
929cdf0e10cSrcweir         else
930cdf0e10cSrcweir         {
931cdf0e10cSrcweir             if ( aData.getLength() > LOCKFILE_EDITTIME_ID )
932cdf0e10cSrcweir             {
933cdf0e10cSrcweir                 if ( aData[LOCKFILE_OOOUSERNAME_ID].getLength() )
934cdf0e10cSrcweir                     aInfo = aData[LOCKFILE_OOOUSERNAME_ID];
935cdf0e10cSrcweir                 else
936cdf0e10cSrcweir                     aInfo = aData[LOCKFILE_SYSUSERNAME_ID];
937cdf0e10cSrcweir 
938cdf0e10cSrcweir                 if ( aInfo.getLength() && aData[LOCKFILE_EDITTIME_ID].getLength() )
939cdf0e10cSrcweir                 {
940cdf0e10cSrcweir                     aInfo += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( " ( " ) );
941cdf0e10cSrcweir                     aInfo += aData[LOCKFILE_EDITTIME_ID];
942cdf0e10cSrcweir                     aInfo += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( " )" ) );
943cdf0e10cSrcweir                 }
944cdf0e10cSrcweir             }
945cdf0e10cSrcweir 
946cdf0e10cSrcweir             if ( bIsLoading )
947cdf0e10cSrcweir             {
948cdf0e10cSrcweir                 xInteractionRequestImpl = new ::ucbhelper::InteractionRequest( uno::makeAny(
949cdf0e10cSrcweir                     document::LockedDocumentRequest( ::rtl::OUString(), uno::Reference< uno::XInterface >(), aDocumentURL, aInfo ) ) );
950cdf0e10cSrcweir             }
951cdf0e10cSrcweir             else
952cdf0e10cSrcweir             {
953cdf0e10cSrcweir                 xInteractionRequestImpl = new ::ucbhelper::InteractionRequest( uno::makeAny(
954cdf0e10cSrcweir                     document::LockedOnSavingRequest( ::rtl::OUString(), uno::Reference< uno::XInterface >(), aDocumentURL, aInfo ) ) );
955cdf0e10cSrcweir 
956cdf0e10cSrcweir             }
957cdf0e10cSrcweir         }
958cdf0e10cSrcweir 
959cdf0e10cSrcweir         uno::Sequence< uno::Reference< task::XInteractionContinuation > > aContinuations( 3 );
960cdf0e10cSrcweir         aContinuations[0] = new ::ucbhelper::InteractionAbort( xInteractionRequestImpl.get() );
961cdf0e10cSrcweir         aContinuations[1] = new ::ucbhelper::InteractionApprove( xInteractionRequestImpl.get() );
962cdf0e10cSrcweir         aContinuations[2] = new ::ucbhelper::InteractionDisapprove( xInteractionRequestImpl.get() );
963cdf0e10cSrcweir         xInteractionRequestImpl->setContinuations( aContinuations );
964cdf0e10cSrcweir 
965cdf0e10cSrcweir         xHandler->handle( xInteractionRequestImpl.get() );
966cdf0e10cSrcweir 
967cdf0e10cSrcweir         ::rtl::Reference< ::ucbhelper::InteractionContinuation > xSelected = xInteractionRequestImpl->getSelection();
968cdf0e10cSrcweir         if ( uno::Reference< task::XInteractionAbort >( xSelected.get(), uno::UNO_QUERY ).is() )
969cdf0e10cSrcweir         {
970cdf0e10cSrcweir             SetError( ERRCODE_ABORT, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
971cdf0e10cSrcweir         }
972cdf0e10cSrcweir         else if ( uno::Reference< task::XInteractionDisapprove >( xSelected.get(), uno::UNO_QUERY ).is() )
973cdf0e10cSrcweir         {
974cdf0e10cSrcweir             // own lock on loading, user has selected to ignore the lock
975cdf0e10cSrcweir             // own lock on saving, user has selected to ignore the lock
976cdf0e10cSrcweir             // alien lock on loading, user has selected to edit a copy of document
977cdf0e10cSrcweir             // TODO/LATER: alien lock on saving, user has selected to do SaveAs to different location
978cdf0e10cSrcweir             if ( bIsLoading && !bOwnLock )
979cdf0e10cSrcweir             {
980cdf0e10cSrcweir                 // means that a copy of the document should be opened
981cdf0e10cSrcweir                 GetItemSet()->Put( SfxBoolItem( SID_TEMPLATE, sal_True ) );
982cdf0e10cSrcweir             }
983cdf0e10cSrcweir             else if ( bOwnLock )
984cdf0e10cSrcweir                 nResult = LOCK_UI_SUCCEEDED;
985cdf0e10cSrcweir         }
986cdf0e10cSrcweir         else // if ( XSelected == aContinuations[1] )
987cdf0e10cSrcweir         {
988cdf0e10cSrcweir             // own lock on loading, user has selected to open readonly
989cdf0e10cSrcweir             // own lock on saving, user has selected to open readonly
990cdf0e10cSrcweir             // alien lock on loading, user has selected to retry saving
991cdf0e10cSrcweir             // TODO/LATER: alien lock on saving, user has selected to retry saving
992cdf0e10cSrcweir 
993cdf0e10cSrcweir             if ( bIsLoading )
994cdf0e10cSrcweir                 GetItemSet()->Put( SfxBoolItem( SID_DOC_READONLY, sal_True ) );
995cdf0e10cSrcweir             else
996cdf0e10cSrcweir                 nResult = LOCK_UI_TRY;
997cdf0e10cSrcweir         }
998cdf0e10cSrcweir     }
999cdf0e10cSrcweir     else
1000cdf0e10cSrcweir     {
1001cdf0e10cSrcweir         if ( bIsLoading )
1002cdf0e10cSrcweir         {
1003cdf0e10cSrcweir             // if no interaction handler is provided the default answer is open readonly
1004cdf0e10cSrcweir             // that usually happens in case the document is loaded per API
1005cdf0e10cSrcweir             // so the document must be opened readonly for backward compatibility
1006cdf0e10cSrcweir             GetItemSet()->Put( SfxBoolItem( SID_DOC_READONLY, sal_True ) );
1007cdf0e10cSrcweir         }
1008cdf0e10cSrcweir         else
1009cdf0e10cSrcweir             SetError( ERRCODE_IO_ACCESSDENIED, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
1010cdf0e10cSrcweir 
1011cdf0e10cSrcweir     }
1012cdf0e10cSrcweir 
1013cdf0e10cSrcweir     return nResult;
1014cdf0e10cSrcweir }
1015cdf0e10cSrcweir 
1016cdf0e10cSrcweir //------------------------------------------------------------------
1017cdf0e10cSrcweir sal_Bool SfxMedium::LockOrigFileOnDemand( sal_Bool bLoading, sal_Bool bNoUI )
1018cdf0e10cSrcweir {
1019cdf0e10cSrcweir     // returns true if the document can be opened for editing ( even if it should be a copy )
1020cdf0e10cSrcweir     // otherwise the document should be opened readonly
1021cdf0e10cSrcweir     // if user cancel the loading the ERROR_ABORT is set
1022cdf0e10cSrcweir 
1023cdf0e10cSrcweir     if ( pImp->m_bLocked && bLoading && ::utl::LocalFileHelper::IsLocalFile( GetURLObject().GetMainURL( INetURLObject::NO_DECODE ) ) )
1024cdf0e10cSrcweir     {
1025cdf0e10cSrcweir         // if the document is already locked the system locking might be temporarely off after storing
1026cdf0e10cSrcweir         // check whether the system file locking should be taken again
1027cdf0e10cSrcweir         GetLockingStream_Impl();
1028cdf0e10cSrcweir     }
1029cdf0e10cSrcweir 
1030cdf0e10cSrcweir     sal_Bool bResult = pImp->m_bLocked;
1031cdf0e10cSrcweir 
1032cdf0e10cSrcweir     if ( !bResult )
1033cdf0e10cSrcweir     {
1034cdf0e10cSrcweir         // no read-write access is necessary on loading if the document is explicitly opened as copy
1035cdf0e10cSrcweir         SFX_ITEMSET_ARG( GetItemSet(), pTemplateItem, SfxBoolItem, SID_TEMPLATE, sal_False);
1036cdf0e10cSrcweir         bResult = ( bLoading && pTemplateItem && pTemplateItem->GetValue() );
1037cdf0e10cSrcweir     }
1038cdf0e10cSrcweir 
1039cdf0e10cSrcweir     if ( !bResult && !IsReadOnly() )
1040cdf0e10cSrcweir     {
1041cdf0e10cSrcweir         sal_Bool bContentReadonly = sal_False;
1042cdf0e10cSrcweir         if ( bLoading && ::utl::LocalFileHelper::IsLocalFile( GetURLObject().GetMainURL( INetURLObject::NO_DECODE ) ) )
1043cdf0e10cSrcweir         {
1044cdf0e10cSrcweir             // let the original document be opened to check the possibility to open it for editing
1045cdf0e10cSrcweir             // and to let the writable stream stay open to hold the lock on the document
1046cdf0e10cSrcweir             GetLockingStream_Impl();
1047cdf0e10cSrcweir         }
1048cdf0e10cSrcweir 
1049cdf0e10cSrcweir         // "IsReadOnly" property does not allow to detect whether the file is readonly always
1050cdf0e10cSrcweir         // so we try always to open the file for editing
1051cdf0e10cSrcweir         // the file is readonly only in case the read-write stream can not be opened
1052cdf0e10cSrcweir         if ( bLoading && !pImp->m_xLockingStream.is() )
1053cdf0e10cSrcweir         {
1054cdf0e10cSrcweir             try
1055cdf0e10cSrcweir             {
1056cdf0e10cSrcweir                 // MediaDescriptor does this check also, the duplication should be avoided in future
1057cdf0e10cSrcweir                 Reference< ::com::sun::star::ucb::XCommandEnvironment > xDummyEnv;
1058cdf0e10cSrcweir                 ::ucbhelper::Content aContent( GetURLObject().GetMainURL( INetURLObject::NO_DECODE ), xDummyEnv );
1059cdf0e10cSrcweir                 aContent.getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsReadOnly" ) ) ) >>= bContentReadonly;
1060cdf0e10cSrcweir             }
1061cdf0e10cSrcweir             catch( uno::Exception )
1062cdf0e10cSrcweir             {}
1063cdf0e10cSrcweir 
1064cdf0e10cSrcweir             if ( !bContentReadonly )
1065cdf0e10cSrcweir             {
1066cdf0e10cSrcweir                 // the file is not readonly, check the ACL
1067cdf0e10cSrcweir 
1068cdf0e10cSrcweir                 String aPhysPath;
1069cdf0e10cSrcweir                 if ( ::utl::LocalFileHelper::ConvertURLToPhysicalName( GetURLObject().GetMainURL( INetURLObject::NO_DECODE ), aPhysPath ) )
1070cdf0e10cSrcweir                     bContentReadonly = IsReadonlyAccordingACL( aPhysPath.GetBuffer() );
1071cdf0e10cSrcweir             }
1072cdf0e10cSrcweir         }
1073cdf0e10cSrcweir 
1074cdf0e10cSrcweir         // do further checks only if the file not readonly in fs
1075cdf0e10cSrcweir         if ( !bContentReadonly )
1076cdf0e10cSrcweir         {
1077cdf0e10cSrcweir             // the special file locking should be used only for file URLs
1078cdf0e10cSrcweir             if ( ::utl::LocalFileHelper::IsLocalFile( aLogicName ) )
1079cdf0e10cSrcweir             {
1080cdf0e10cSrcweir 
1081cdf0e10cSrcweir                 // in case of storing the document should request the output before locking
1082cdf0e10cSrcweir                 if ( bLoading )
1083cdf0e10cSrcweir                 {
1084cdf0e10cSrcweir                     // let the stream be opened to check the system file locking
1085cdf0e10cSrcweir                     GetMedium_Impl();
1086cdf0e10cSrcweir                 }
1087cdf0e10cSrcweir 
1088cdf0e10cSrcweir                 sal_Int8 bUIStatus = LOCK_UI_NOLOCK;
1089cdf0e10cSrcweir 
1090cdf0e10cSrcweir                 // check whether system file locking has been used, the default value is false
1091cdf0e10cSrcweir                 sal_Bool bUseSystemLock = IsSystemFileLockingUsed();
1092cdf0e10cSrcweir 
1093cdf0e10cSrcweir                 // TODO/LATER: This implementation does not allow to detect the system lock on saving here, actually this is no big problem
1094cdf0e10cSrcweir                 // if system lock is used the writeable stream should be available
1095cdf0e10cSrcweir                 sal_Bool bHandleSysLocked = ( bLoading && bUseSystemLock && !pImp->xStream.is() && !pOutStream );
1096cdf0e10cSrcweir 
1097cdf0e10cSrcweir                 do
1098cdf0e10cSrcweir                 {
1099cdf0e10cSrcweir                     try
1100cdf0e10cSrcweir                     {
1101cdf0e10cSrcweir                         ::svt::DocumentLockFile aLockFile( aLogicName );
1102cdf0e10cSrcweir                         if ( !bHandleSysLocked )
1103cdf0e10cSrcweir                         {
1104cdf0e10cSrcweir                             try
1105cdf0e10cSrcweir                             {
1106cdf0e10cSrcweir                                 bResult = aLockFile.CreateOwnLockFile();
1107cdf0e10cSrcweir                             }
1108cdf0e10cSrcweir                             catch ( ucb::InteractiveIOException& e )
1109cdf0e10cSrcweir                             {
1110cdf0e10cSrcweir                                 // exception means that the lock file can not be successfuly accessed
1111cdf0e10cSrcweir                                 // in this case it should be ignored if system file locking is anyway active
1112cdf0e10cSrcweir                                 if ( bUseSystemLock || !IsOOoLockFileUsed() )
1113cdf0e10cSrcweir                                 {
1114cdf0e10cSrcweir                                     bResult = sal_True;
1115cdf0e10cSrcweir                                     // take the ownership over the lock file
1116cdf0e10cSrcweir                                     aLockFile.OverwriteOwnLockFile();
1117cdf0e10cSrcweir                                 }
1118cdf0e10cSrcweir                                 else if ( e.Code == IOErrorCode_INVALID_PARAMETER )
1119cdf0e10cSrcweir                                 {
1120cdf0e10cSrcweir                                     // system file locking is not active, ask user whether he wants to open the document without any locking
1121cdf0e10cSrcweir                                     uno::Reference< task::XInteractionHandler > xHandler = GetInteractionHandler();
1122cdf0e10cSrcweir 
1123cdf0e10cSrcweir                                     if ( xHandler.is() )
1124cdf0e10cSrcweir                                     {
1125cdf0e10cSrcweir                                         ::rtl::Reference< ::ucbhelper::InteractionRequest > xIgnoreRequestImpl
1126cdf0e10cSrcweir                                             = new ::ucbhelper::InteractionRequest( uno::makeAny( document::LockFileIgnoreRequest() ) );
1127cdf0e10cSrcweir 
1128cdf0e10cSrcweir                                         uno::Sequence< uno::Reference< task::XInteractionContinuation > > aContinuations( 2 );
1129cdf0e10cSrcweir                                         aContinuations[0] = new ::ucbhelper::InteractionAbort( xIgnoreRequestImpl.get() );
1130cdf0e10cSrcweir                                         aContinuations[1] = new ::ucbhelper::InteractionApprove( xIgnoreRequestImpl.get() );
1131cdf0e10cSrcweir                                         xIgnoreRequestImpl->setContinuations( aContinuations );
1132cdf0e10cSrcweir 
1133cdf0e10cSrcweir                                         xHandler->handle( xIgnoreRequestImpl.get() );
1134cdf0e10cSrcweir 
1135cdf0e10cSrcweir                                         ::rtl::Reference< ::ucbhelper::InteractionContinuation > xSelected = xIgnoreRequestImpl->getSelection();
1136cdf0e10cSrcweir                                         bResult = (  uno::Reference< task::XInteractionApprove >( xSelected.get(), uno::UNO_QUERY ).is() );
1137cdf0e10cSrcweir                                     }
1138cdf0e10cSrcweir                                 }
1139cdf0e10cSrcweir                             }
1140cdf0e10cSrcweir                             catch ( uno::Exception& )
1141cdf0e10cSrcweir                             {
1142cdf0e10cSrcweir                                 // exception means that the lock file can not be successfuly accessed
1143cdf0e10cSrcweir                                 // in this case it should be ignored if system file locking is anyway active
1144cdf0e10cSrcweir                                 if ( bUseSystemLock || !IsOOoLockFileUsed() )
1145cdf0e10cSrcweir                                 {
1146cdf0e10cSrcweir                                     bResult = sal_True;
1147cdf0e10cSrcweir                                     // take the ownership over the lock file
1148cdf0e10cSrcweir                                     aLockFile.OverwriteOwnLockFile();
1149cdf0e10cSrcweir                                 }
1150cdf0e10cSrcweir                             }
1151cdf0e10cSrcweir 
1152cdf0e10cSrcweir                             // in case OOo locking is turned off the lock file is still written if possible
1153cdf0e10cSrcweir                             // but it is ignored while deciding whether the document should be opened for editing or not
1154cdf0e10cSrcweir                             if ( !bResult && !IsOOoLockFileUsed() )
1155cdf0e10cSrcweir                             {
1156cdf0e10cSrcweir                                 bResult = sal_True;
1157cdf0e10cSrcweir                                 // take the ownership over the lock file
1158cdf0e10cSrcweir                                 aLockFile.OverwriteOwnLockFile();
1159cdf0e10cSrcweir                             }
1160cdf0e10cSrcweir                         }
1161cdf0e10cSrcweir 
1162cdf0e10cSrcweir 
1163cdf0e10cSrcweir                         if ( !bResult )
1164cdf0e10cSrcweir                         {
1165cdf0e10cSrcweir                             uno::Sequence< ::rtl::OUString > aData;
1166cdf0e10cSrcweir                             try
1167cdf0e10cSrcweir                             {
1168cdf0e10cSrcweir                                 // impossibility to get data is no real problem
1169cdf0e10cSrcweir                                 aData = aLockFile.GetLockData();
1170cdf0e10cSrcweir                             }
1171cdf0e10cSrcweir                             catch( uno::Exception ) {}
1172cdf0e10cSrcweir 
1173cdf0e10cSrcweir                             sal_Bool bOwnLock = sal_False;
1174cdf0e10cSrcweir 
1175cdf0e10cSrcweir                             if ( !bHandleSysLocked )
1176cdf0e10cSrcweir                             {
1177cdf0e10cSrcweir                                 uno::Sequence< ::rtl::OUString > aOwnData = aLockFile.GenerateOwnEntry();
1178cdf0e10cSrcweir                                 bOwnLock = ( aData.getLength() > LOCKFILE_USERURL_ID
1179cdf0e10cSrcweir                                           && aOwnData.getLength() > LOCKFILE_USERURL_ID
1180cdf0e10cSrcweir                                           && aOwnData[LOCKFILE_SYSUSERNAME_ID].equals( aData[LOCKFILE_SYSUSERNAME_ID] ) );
1181cdf0e10cSrcweir 
1182cdf0e10cSrcweir                                 if ( bOwnLock
1183cdf0e10cSrcweir                                   && aOwnData[LOCKFILE_LOCALHOST_ID].equals( aData[LOCKFILE_LOCALHOST_ID] )
1184cdf0e10cSrcweir                                   && aOwnData[LOCKFILE_USERURL_ID].equals( aData[LOCKFILE_USERURL_ID] ) )
1185cdf0e10cSrcweir                                 {
1186cdf0e10cSrcweir                                     // this is own lock from the same installation, it could remain because of crash
1187cdf0e10cSrcweir                                     bResult = sal_True;
1188cdf0e10cSrcweir                                 }
1189cdf0e10cSrcweir                             }
1190cdf0e10cSrcweir 
1191cdf0e10cSrcweir                             if ( !bResult && !bNoUI )
1192cdf0e10cSrcweir                             {
1193cdf0e10cSrcweir                                 bUIStatus = ShowLockedDocumentDialog( aData, bLoading, bOwnLock );
1194cdf0e10cSrcweir                                 if ( bUIStatus == LOCK_UI_SUCCEEDED )
1195cdf0e10cSrcweir                                 {
1196cdf0e10cSrcweir                                     // take the ownership over the lock file
1197cdf0e10cSrcweir                                     bResult = aLockFile.OverwriteOwnLockFile();
1198cdf0e10cSrcweir                                 }
1199cdf0e10cSrcweir                             }
1200cdf0e10cSrcweir 
1201cdf0e10cSrcweir                             bHandleSysLocked = sal_False;
1202cdf0e10cSrcweir                         }
1203cdf0e10cSrcweir                     }
1204cdf0e10cSrcweir                     catch( uno::Exception& )
1205cdf0e10cSrcweir                     {
1206cdf0e10cSrcweir                     }
1207cdf0e10cSrcweir                 } while( !bResult && bUIStatus == LOCK_UI_TRY );
1208cdf0e10cSrcweir 
1209cdf0e10cSrcweir                 pImp->m_bLocked = bResult;
1210cdf0e10cSrcweir             }
1211cdf0e10cSrcweir             else
1212cdf0e10cSrcweir             {
1213cdf0e10cSrcweir                 // this is no file URL, check whether the file is readonly
1214cdf0e10cSrcweir                 bResult = !bContentReadonly;
1215cdf0e10cSrcweir             }
1216cdf0e10cSrcweir         }
1217cdf0e10cSrcweir     }
1218cdf0e10cSrcweir 
1219cdf0e10cSrcweir     if ( !bResult && GetError() == ERRCODE_NONE )
1220cdf0e10cSrcweir     {
1221cdf0e10cSrcweir         // the error should be set in case it is storing process
1222cdf0e10cSrcweir         // or the document has been opened for editing explicitly
1223cdf0e10cSrcweir 
1224cdf0e10cSrcweir         SFX_ITEMSET_ARG( pSet, pReadOnlyItem, SfxBoolItem, SID_DOC_READONLY, sal_False );
1225cdf0e10cSrcweir         if ( !bLoading || (pReadOnlyItem && !pReadOnlyItem->GetValue()) )
1226cdf0e10cSrcweir             SetError( ERRCODE_IO_ACCESSDENIED, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
1227cdf0e10cSrcweir         else
1228cdf0e10cSrcweir             GetItemSet()->Put( SfxBoolItem( SID_DOC_READONLY, sal_True ) );
1229cdf0e10cSrcweir     }
1230cdf0e10cSrcweir 
1231cdf0e10cSrcweir     // when the file is locked, get the current file date
1232cdf0e10cSrcweir     if ( bResult && DocNeedsFileDateCheck() )
1233cdf0e10cSrcweir         GetInitFileDate( sal_True );
1234cdf0e10cSrcweir 
1235cdf0e10cSrcweir     return bResult;
1236cdf0e10cSrcweir }
1237cdf0e10cSrcweir 
1238cdf0e10cSrcweir //------------------------------------------------------------------
1239cdf0e10cSrcweir uno::Reference < embed::XStorage > SfxMedium::GetStorage( sal_Bool bCreateTempIfNo )
1240cdf0e10cSrcweir {
1241cdf0e10cSrcweir     if ( pImp->xStorage.is() || bTriedStorage )
1242cdf0e10cSrcweir         return pImp->xStorage;
1243cdf0e10cSrcweir 
1244cdf0e10cSrcweir     uno::Sequence< uno::Any > aArgs( 2 );
1245cdf0e10cSrcweir 
1246cdf0e10cSrcweir     // the medium should be retrieved before temporary file creation
1247cdf0e10cSrcweir     // to let the MediaDescriptor be filled with the streams
1248cdf0e10cSrcweir     GetMedium_Impl();
1249cdf0e10cSrcweir 
1250cdf0e10cSrcweir     if ( bCreateTempIfNo )
1251cdf0e10cSrcweir         CreateTempFile( sal_False );
1252cdf0e10cSrcweir 
1253cdf0e10cSrcweir     GetMedium_Impl();
1254cdf0e10cSrcweir 
1255cdf0e10cSrcweir     if ( GetError() )
1256cdf0e10cSrcweir         return pImp->xStorage;
1257cdf0e10cSrcweir 
1258cdf0e10cSrcweir     SFX_ITEMSET_ARG( GetItemSet(), pRepairItem, SfxBoolItem, SID_REPAIRPACKAGE, sal_False);
1259cdf0e10cSrcweir     if ( pRepairItem && pRepairItem->GetValue() )
1260cdf0e10cSrcweir     {
1261cdf0e10cSrcweir         // the storage should be created for repairing mode
1262cdf0e10cSrcweir         CreateTempFile( sal_False );
1263cdf0e10cSrcweir         GetMedium_Impl();
1264cdf0e10cSrcweir 
1265cdf0e10cSrcweir         Reference< ::com::sun::star::ucb::XProgressHandler > xProgressHandler;
1266cdf0e10cSrcweir         Reference< ::com::sun::star::task::XStatusIndicator > xStatusIndicator;
1267cdf0e10cSrcweir 
1268cdf0e10cSrcweir         SFX_ITEMSET_ARG( GetItemSet(), pxProgressItem, SfxUnoAnyItem, SID_PROGRESS_STATUSBAR_CONTROL, sal_False );
1269cdf0e10cSrcweir         if( pxProgressItem && ( pxProgressItem->GetValue() >>= xStatusIndicator ) )
1270cdf0e10cSrcweir             xProgressHandler = Reference< ::com::sun::star::ucb::XProgressHandler >(
1271cdf0e10cSrcweir                                     new utl::ProgressHandlerWrap( xStatusIndicator ) );
1272cdf0e10cSrcweir 
1273cdf0e10cSrcweir         uno::Sequence< beans::PropertyValue > aAddProps( 2 );
1274cdf0e10cSrcweir         aAddProps[0].Name = ::rtl::OUString::createFromAscii( "RepairPackage" );
1275cdf0e10cSrcweir         aAddProps[0].Value <<= (sal_Bool)sal_True;
1276cdf0e10cSrcweir         aAddProps[1].Name = ::rtl::OUString::createFromAscii( "StatusIndicator" );
1277cdf0e10cSrcweir         aAddProps[1].Value <<= xProgressHandler;
1278cdf0e10cSrcweir 
1279cdf0e10cSrcweir         // the first arguments will be filled later
1280cdf0e10cSrcweir         aArgs.realloc( 3 );
1281cdf0e10cSrcweir         aArgs[2] <<= aAddProps;
1282cdf0e10cSrcweir     }
1283cdf0e10cSrcweir 
1284cdf0e10cSrcweir     if ( pImp->xStream.is() )
1285cdf0e10cSrcweir     {
1286cdf0e10cSrcweir         // since the storage is based on temporary stream we open it always read-write
1287cdf0e10cSrcweir         aArgs[0] <<= pImp->xStream;
1288cdf0e10cSrcweir         aArgs[1] <<= embed::ElementModes::READWRITE;
1289cdf0e10cSrcweir         pImp->bStorageBasedOnInStream = sal_True;
1290cdf0e10cSrcweir     }
1291cdf0e10cSrcweir     else if ( pImp->xInputStream.is() )
1292cdf0e10cSrcweir     {
1293cdf0e10cSrcweir         // since the storage is based on temporary stream we open it always read-write
1294cdf0e10cSrcweir         aArgs[0] <<= pImp->xInputStream;
1295cdf0e10cSrcweir         aArgs[1] <<= embed::ElementModes::READ;
1296cdf0e10cSrcweir         pImp->bStorageBasedOnInStream = sal_True;
1297cdf0e10cSrcweir     }
1298cdf0e10cSrcweir     else
1299cdf0e10cSrcweir     {
1300cdf0e10cSrcweir         CloseStreams_Impl();
1301cdf0e10cSrcweir         aArgs[0] <<= ::rtl::OUString( aName );
1302cdf0e10cSrcweir         aArgs[1] <<= embed::ElementModes::READ;
1303cdf0e10cSrcweir         pImp->bStorageBasedOnInStream = sal_False;
1304cdf0e10cSrcweir     }
1305cdf0e10cSrcweir 
1306cdf0e10cSrcweir     try
1307cdf0e10cSrcweir     {
1308cdf0e10cSrcweir         pImp->xStorage = uno::Reference< embed::XStorage >(
1309cdf0e10cSrcweir                             ::comphelper::OStorageHelper::GetStorageFactory()->createInstanceWithArguments( aArgs ),
1310cdf0e10cSrcweir                             uno::UNO_QUERY );
1311cdf0e10cSrcweir     }
1312cdf0e10cSrcweir     catch( uno::Exception& )
1313cdf0e10cSrcweir     {
1314cdf0e10cSrcweir         // impossibility to create the storage is no error
1315cdf0e10cSrcweir     }
1316cdf0e10cSrcweir 
1317cdf0e10cSrcweir     if( ( pImp->nLastStorageError = GetError() ) != SVSTREAM_OK )
1318cdf0e10cSrcweir     {
1319cdf0e10cSrcweir         pImp->xStorage = 0;
1320cdf0e10cSrcweir         if ( pInStream )
1321cdf0e10cSrcweir             pInStream->Seek(0);
1322cdf0e10cSrcweir         return uno::Reference< embed::XStorage >();
1323cdf0e10cSrcweir     }
1324cdf0e10cSrcweir 
1325cdf0e10cSrcweir     bTriedStorage = sal_True;
1326cdf0e10cSrcweir 
1327cdf0e10cSrcweir     // TODO/LATER: Get versionlist on demand
1328cdf0e10cSrcweir     if ( pImp->xStorage.is() )
1329cdf0e10cSrcweir 	{
1330cdf0e10cSrcweir         SetEncryptionDataToStorage_Impl();
1331cdf0e10cSrcweir         GetVersionList();
1332cdf0e10cSrcweir 	}
1333cdf0e10cSrcweir 
1334cdf0e10cSrcweir     SFX_ITEMSET_ARG( pSet, pVersion, SfxInt16Item, SID_VERSION, sal_False);
1335cdf0e10cSrcweir 
1336cdf0e10cSrcweir     sal_Bool bResetStorage = sal_False;
1337cdf0e10cSrcweir     if ( pVersion && pVersion->GetValue() )
1338cdf0e10cSrcweir     {
1339cdf0e10cSrcweir         // Alle verf"ugbaren Versionen einlesen
1340cdf0e10cSrcweir         if ( pImp->aVersions.getLength() )
1341cdf0e10cSrcweir         {
1342cdf0e10cSrcweir             // Die zum Kommentar passende Version suchen
1343cdf0e10cSrcweir             // Die Versionen sind von 1 an durchnumeriert, mit negativen
1344cdf0e10cSrcweir             // Versionsnummern werden die Versionen von der aktuellen aus
1345cdf0e10cSrcweir             // r"uckw"arts gez"ahlt
1346cdf0e10cSrcweir             short nVersion = pVersion ? pVersion->GetValue() : 0;
1347cdf0e10cSrcweir             if ( nVersion<0 )
1348cdf0e10cSrcweir                 nVersion = ( (short) pImp->aVersions.getLength() ) + nVersion;
1349cdf0e10cSrcweir             else if ( nVersion )
1350cdf0e10cSrcweir                 nVersion--;
1351cdf0e10cSrcweir 
1352cdf0e10cSrcweir             util::RevisionTag& rTag = pImp->aVersions[nVersion];
1353cdf0e10cSrcweir             {
1354cdf0e10cSrcweir                 // SubStorage f"ur alle Versionen "offnen
1355cdf0e10cSrcweir                 uno::Reference < embed::XStorage > xSub = pImp->xStorage->openStorageElement( DEFINE_CONST_UNICODE( "Versions" ),
1356cdf0e10cSrcweir                         embed::ElementModes::READ );
1357cdf0e10cSrcweir 
1358cdf0e10cSrcweir                 DBG_ASSERT( xSub.is(), "Versionsliste, aber keine Versionen!" );
1359cdf0e10cSrcweir 
1360cdf0e10cSrcweir                 // Dort ist die Version als gepackter Stream gespeichert
1361cdf0e10cSrcweir                 uno::Reference < io::XStream > xStr = xSub->openStreamElement( rTag.Identifier, embed::ElementModes::READ );
1362cdf0e10cSrcweir                 SvStream* pStream = utl::UcbStreamHelper::CreateStream( xStr );
1363cdf0e10cSrcweir                 if ( pStream && pStream->GetError() == SVSTREAM_OK )
1364cdf0e10cSrcweir                 {
1365cdf0e10cSrcweir                     // Stream ins TempDir auspacken
1366cdf0e10cSrcweir                     ::utl::TempFile aTempFile;
1367cdf0e10cSrcweir                     String          aTmpName = aTempFile.GetURL();
1368cdf0e10cSrcweir                     SvFileStream    aTmpStream( aTmpName, SFX_STREAM_READWRITE );
1369cdf0e10cSrcweir 
1370cdf0e10cSrcweir                     *pStream >> aTmpStream;
1371cdf0e10cSrcweir                     aTmpStream.Close();
1372cdf0e10cSrcweir 
1373cdf0e10cSrcweir                     // Datei als Storage "offnen
1374cdf0e10cSrcweir                     nStorOpenMode = SFX_STREAM_READONLY;
1375cdf0e10cSrcweir                     pImp->xStorage = comphelper::OStorageHelper::GetStorageFromURL( aTmpName, embed::ElementModes::READ );
1376cdf0e10cSrcweir                     pImp->bStorageBasedOnInStream = sal_False;
1377cdf0e10cSrcweir                     String aTemp;
1378cdf0e10cSrcweir                     ::utl::LocalFileHelper::ConvertURLToPhysicalName( aTmpName, aTemp );
1379cdf0e10cSrcweir                     SetPhysicalName_Impl( aTemp );
1380cdf0e10cSrcweir 
1381cdf0e10cSrcweir                     pImp->bIsTemp = sal_True;
1382cdf0e10cSrcweir                     GetItemSet()->Put( SfxBoolItem( SID_DOC_READONLY, sal_True ) );
1383cdf0e10cSrcweir                     // TODO/MBA
1384cdf0e10cSrcweir                     pImp->aVersions.realloc(0);
1385cdf0e10cSrcweir                 }
1386cdf0e10cSrcweir                 else
1387cdf0e10cSrcweir                     bResetStorage = sal_True;
1388cdf0e10cSrcweir             }
1389cdf0e10cSrcweir         }
1390cdf0e10cSrcweir         else
1391cdf0e10cSrcweir             bResetStorage = sal_True;
1392cdf0e10cSrcweir     }
1393cdf0e10cSrcweir 
1394cdf0e10cSrcweir     if ( bResetStorage )
1395cdf0e10cSrcweir     {
1396cdf0e10cSrcweir         pImp->xStorage = 0;
1397cdf0e10cSrcweir         if ( pInStream )
1398cdf0e10cSrcweir             pInStream->Seek( 0L );
1399cdf0e10cSrcweir     }
1400cdf0e10cSrcweir 
1401cdf0e10cSrcweir     pImp->bIsStorage = pImp->xStorage.is();
1402cdf0e10cSrcweir     return pImp->xStorage;
1403cdf0e10cSrcweir }
1404cdf0e10cSrcweir 
1405cdf0e10cSrcweir //------------------------------------------------------------------
1406cdf0e10cSrcweir uno::Reference< embed::XStorage > SfxMedium::GetZipStorageToSign_Impl( sal_Bool bReadOnly )
1407cdf0e10cSrcweir {
1408cdf0e10cSrcweir     if ( !GetError() && !pImp->m_xZipStorage.is() )
1409cdf0e10cSrcweir     {
1410cdf0e10cSrcweir         // very careful!!!
1411cdf0e10cSrcweir         // if bReadOnly == sal_False and there is no temporary file the original file might be used
1412cdf0e10cSrcweir         GetMedium_Impl();
1413cdf0e10cSrcweir 
1414cdf0e10cSrcweir         try
1415cdf0e10cSrcweir         {
1416cdf0e10cSrcweir             // we can not sign document if there is no stream
1417cdf0e10cSrcweir             // should it be possible at all?
1418cdf0e10cSrcweir             if ( !bReadOnly && pImp->xStream.is() )
1419cdf0e10cSrcweir             {
1420cdf0e10cSrcweir                 pImp->m_xZipStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromStream( ZIP_STORAGE_FORMAT_STRING, pImp->xStream, embed::ElementModes::READWRITE );
1421cdf0e10cSrcweir             }
1422cdf0e10cSrcweir             else if ( pImp->xInputStream.is() )
1423cdf0e10cSrcweir             {
1424cdf0e10cSrcweir                 pImp->m_xZipStorage = ::comphelper::OStorageHelper::GetStorageOfFormatFromInputStream( ZIP_STORAGE_FORMAT_STRING, pImp->xInputStream );
1425cdf0e10cSrcweir             }
1426cdf0e10cSrcweir         }
1427cdf0e10cSrcweir         catch( uno::Exception& )
1428cdf0e10cSrcweir         {
1429cdf0e10cSrcweir             OSL_ENSURE( sal_False, "No possibility to get readonly version of storage from medium!\n" );
1430cdf0e10cSrcweir         }
1431cdf0e10cSrcweir 
1432cdf0e10cSrcweir         if ( GetError() ) // do not remove warnings
1433cdf0e10cSrcweir             ResetError();
1434cdf0e10cSrcweir     }
1435cdf0e10cSrcweir 
1436cdf0e10cSrcweir     return pImp->m_xZipStorage;
1437cdf0e10cSrcweir }
1438cdf0e10cSrcweir 
1439cdf0e10cSrcweir //------------------------------------------------------------------
1440cdf0e10cSrcweir void SfxMedium::CloseZipStorage_Impl()
1441cdf0e10cSrcweir {
1442cdf0e10cSrcweir 	if ( pImp->m_xZipStorage.is() )
1443cdf0e10cSrcweir 	{
1444cdf0e10cSrcweir 		try {
1445cdf0e10cSrcweir 			pImp->m_xZipStorage->dispose();
1446cdf0e10cSrcweir 		} catch( uno::Exception& )
1447cdf0e10cSrcweir 		{}
1448cdf0e10cSrcweir 
1449cdf0e10cSrcweir 		pImp->m_xZipStorage = uno::Reference< embed::XStorage >();
1450cdf0e10cSrcweir 	}
1451cdf0e10cSrcweir }
1452cdf0e10cSrcweir 
1453cdf0e10cSrcweir //------------------------------------------------------------------
1454cdf0e10cSrcweir void SfxMedium::CloseStorage()
1455cdf0e10cSrcweir {
1456cdf0e10cSrcweir     if ( pImp->xStorage.is() )
1457cdf0e10cSrcweir     {
1458cdf0e10cSrcweir         uno::Reference < lang::XComponent > xComp( pImp->xStorage, uno::UNO_QUERY );
1459cdf0e10cSrcweir 		// in the salvage mode the medium does not own the storage
1460cdf0e10cSrcweir         if ( pImp->bDisposeStorage && !pImp->m_bSalvageMode )
1461cdf0e10cSrcweir 		{
1462cdf0e10cSrcweir 			try {
1463cdf0e10cSrcweir             	xComp->dispose();
1464cdf0e10cSrcweir 			} catch( uno::Exception& )
1465cdf0e10cSrcweir 			{
1466cdf0e10cSrcweir 				OSL_ENSURE( sal_False, "Medium's storage is already disposed!\n" );
1467cdf0e10cSrcweir 			}
1468cdf0e10cSrcweir 		}
1469cdf0e10cSrcweir 
1470cdf0e10cSrcweir         pImp->xStorage = 0;
1471cdf0e10cSrcweir     	pImp->bStorageBasedOnInStream = sal_False;
1472cdf0e10cSrcweir     }
1473cdf0e10cSrcweir 
1474cdf0e10cSrcweir     bTriedStorage = sal_False;
1475cdf0e10cSrcweir     pImp->bIsStorage = sal_False;
1476cdf0e10cSrcweir }
1477cdf0e10cSrcweir 
1478cdf0e10cSrcweir void SfxMedium::CanDisposeStorage_Impl( sal_Bool bDisposeStorage )
1479cdf0e10cSrcweir {
1480cdf0e10cSrcweir     pImp->bDisposeStorage = bDisposeStorage;
1481cdf0e10cSrcweir }
1482cdf0e10cSrcweir 
1483cdf0e10cSrcweir sal_Bool SfxMedium::WillDisposeStorageOnClose_Impl()
1484cdf0e10cSrcweir {
1485cdf0e10cSrcweir 	return pImp->bDisposeStorage;
1486cdf0e10cSrcweir }
1487cdf0e10cSrcweir 
1488cdf0e10cSrcweir //------------------------------------------------------------------
1489cdf0e10cSrcweir void SfxMedium::SetOpenMode( StreamMode nStorOpen,
1490cdf0e10cSrcweir                              sal_Bool bDirectP,
1491cdf0e10cSrcweir                              sal_Bool bDontClose )
1492cdf0e10cSrcweir {
1493cdf0e10cSrcweir     if ( nStorOpenMode != nStorOpen )
1494cdf0e10cSrcweir     {
1495cdf0e10cSrcweir         nStorOpenMode = nStorOpen;
1496cdf0e10cSrcweir 
1497cdf0e10cSrcweir         if( !bDontClose )
1498cdf0e10cSrcweir         {
1499cdf0e10cSrcweir             if ( pImp->xStorage.is() )
1500cdf0e10cSrcweir                 CloseStorage();
1501cdf0e10cSrcweir 
1502cdf0e10cSrcweir             CloseStreams_Impl();
1503cdf0e10cSrcweir         }
1504cdf0e10cSrcweir     }
1505cdf0e10cSrcweir 
1506cdf0e10cSrcweir     bDirect     = bDirectP;
1507cdf0e10cSrcweir     bSetFilter  = sal_False;
1508cdf0e10cSrcweir }
1509cdf0e10cSrcweir 
1510cdf0e10cSrcweir //------------------------------------------------------------------
1511cdf0e10cSrcweir sal_Bool SfxMedium::UseBackupToRestore_Impl( ::ucbhelper::Content& aOriginalContent,
1512cdf0e10cSrcweir 											const Reference< ::com::sun::star::ucb::XCommandEnvironment >& xComEnv )
1513cdf0e10cSrcweir {
1514cdf0e10cSrcweir 	try
1515cdf0e10cSrcweir 	{
1516cdf0e10cSrcweir 		::ucbhelper::Content aTransactCont( pImp->m_aBackupURL, xComEnv );
1517cdf0e10cSrcweir 
1518cdf0e10cSrcweir 		Reference< XInputStream > aOrigInput = aTransactCont.openStream();
1519cdf0e10cSrcweir 		aOriginalContent.writeStream( aOrigInput, sal_True );
1520cdf0e10cSrcweir 		return sal_True;
1521cdf0e10cSrcweir 	}
1522cdf0e10cSrcweir 	catch( Exception& )
1523cdf0e10cSrcweir 	{
1524cdf0e10cSrcweir 		// in case of failure here the backup file should not be removed
1525cdf0e10cSrcweir 		// TODO/LATER: a message should be used to let user know about the backup
1526cdf0e10cSrcweir 		pImp->m_bRemoveBackup = sal_False;
1527cdf0e10cSrcweir 		// TODO/LATER: needs a specific error code
1528cdf0e10cSrcweir 		eError = ERRCODE_IO_GENERAL;
1529cdf0e10cSrcweir 	}
1530cdf0e10cSrcweir 
1531cdf0e10cSrcweir 	return sal_False;
1532cdf0e10cSrcweir }
1533cdf0e10cSrcweir 
1534cdf0e10cSrcweir //------------------------------------------------------------------
1535cdf0e10cSrcweir sal_Bool SfxMedium::StorageCommit_Impl()
1536cdf0e10cSrcweir {
1537cdf0e10cSrcweir 	sal_Bool bResult = sal_False;
1538cdf0e10cSrcweir 	Reference< ::com::sun::star::ucb::XCommandEnvironment > xDummyEnv;
1539cdf0e10cSrcweir 	::ucbhelper::Content aOriginalContent;
1540cdf0e10cSrcweir 
1541cdf0e10cSrcweir 	if ( pImp->xStorage.is() )
1542cdf0e10cSrcweir 	{
1543cdf0e10cSrcweir 		if ( !GetError() )
1544cdf0e10cSrcweir 		{
1545cdf0e10cSrcweir         	uno::Reference < embed::XTransactedObject > xTrans( pImp->xStorage, uno::UNO_QUERY );
1546cdf0e10cSrcweir         	if ( xTrans.is() )
1547cdf0e10cSrcweir         	{
1548cdf0e10cSrcweir             	try
1549cdf0e10cSrcweir             	{
1550cdf0e10cSrcweir                 	xTrans->commit();
1551cdf0e10cSrcweir 					CloseZipStorage_Impl();
1552cdf0e10cSrcweir 					bResult = sal_True;
1553cdf0e10cSrcweir             	}
1554cdf0e10cSrcweir 				catch ( embed::UseBackupException& aBackupExc )
1555cdf0e10cSrcweir 				{
1556cdf0e10cSrcweir                     // since the temporary file is created always now, the scenario is close to be impossible
1557cdf0e10cSrcweir 					if ( !pImp->pTempFile )
1558cdf0e10cSrcweir 					{
1559cdf0e10cSrcweir 						OSL_ENSURE( pImp->m_aBackupURL.getLength(), "No backup on storage commit!\n" );
1560cdf0e10cSrcweir 						if ( pImp->m_aBackupURL.getLength()
1561cdf0e10cSrcweir 							&& ::ucbhelper::Content::create( GetURLObject().GetMainURL( INetURLObject::NO_DECODE ),
1562cdf0e10cSrcweir 														xDummyEnv,
1563cdf0e10cSrcweir 														aOriginalContent ) )
1564cdf0e10cSrcweir 						{
1565cdf0e10cSrcweir 							// use backup to restore the file
1566cdf0e10cSrcweir 							// the storage has already disconnected from original location
1567cdf0e10cSrcweir 							CloseAndReleaseStreams_Impl();
1568cdf0e10cSrcweir 							if ( !UseBackupToRestore_Impl( aOriginalContent, xDummyEnv ) )
1569cdf0e10cSrcweir 							{
1570cdf0e10cSrcweir 								// connect the medium to the temporary file of the storage
1571cdf0e10cSrcweir             					pImp->aContent = ::ucbhelper::Content();
1572cdf0e10cSrcweir         						aName = aBackupExc.TemporaryFileURL;
1573cdf0e10cSrcweir 								OSL_ENSURE( aName.Len(), "The exception _must_ contain the temporary URL!\n" );
1574cdf0e10cSrcweir 							}
1575cdf0e10cSrcweir 						}
1576cdf0e10cSrcweir 
1577cdf0e10cSrcweir 						if ( !GetError() )
1578cdf0e10cSrcweir         					SetError( ERRCODE_IO_GENERAL, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
1579cdf0e10cSrcweir 					}
1580cdf0e10cSrcweir 				}
1581cdf0e10cSrcweir             	catch ( uno::Exception& )
1582cdf0e10cSrcweir             	{
1583cdf0e10cSrcweir                 	//TODO/LATER: improve error handling
1584cdf0e10cSrcweir         			SetError( ERRCODE_IO_GENERAL, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
1585cdf0e10cSrcweir             	}
1586cdf0e10cSrcweir         	}
1587cdf0e10cSrcweir 		}
1588cdf0e10cSrcweir 	}
1589cdf0e10cSrcweir 
1590cdf0e10cSrcweir 	return bResult;
1591cdf0e10cSrcweir }
1592cdf0e10cSrcweir 
1593cdf0e10cSrcweir //------------------------------------------------------------------
1594cdf0e10cSrcweir sal_Bool SfxMedium::TransactedTransferForFS_Impl( const INetURLObject& aSource,
1595cdf0e10cSrcweir 											 	const INetURLObject& aDest,
1596cdf0e10cSrcweir 											 	const Reference< ::com::sun::star::ucb::XCommandEnvironment >& xComEnv )
1597cdf0e10cSrcweir {
1598cdf0e10cSrcweir 	sal_Bool bResult = sal_False;
1599cdf0e10cSrcweir 	Reference< ::com::sun::star::ucb::XCommandEnvironment > xDummyEnv;
1600cdf0e10cSrcweir 	Reference< XOutputStream > aDestStream;
1601cdf0e10cSrcweir 	::ucbhelper::Content aOriginalContent;
1602cdf0e10cSrcweir 
1603cdf0e10cSrcweir     try
1604cdf0e10cSrcweir     {
1605cdf0e10cSrcweir         aOriginalContent = ::ucbhelper::Content( aDest.GetMainURL( INetURLObject::NO_DECODE ), xComEnv );
1606cdf0e10cSrcweir     }
1607cdf0e10cSrcweir     catch ( ::com::sun::star::ucb::CommandAbortedException& )
1608cdf0e10cSrcweir     {
1609cdf0e10cSrcweir         eError = ERRCODE_ABORT;
1610cdf0e10cSrcweir     }
1611cdf0e10cSrcweir     catch ( ::com::sun::star::ucb::CommandFailedException& )
1612cdf0e10cSrcweir     {
1613cdf0e10cSrcweir         eError = ERRCODE_ABORT;
1614cdf0e10cSrcweir     }
1615cdf0e10cSrcweir     catch (const ::com::sun::star::ucb::ContentCreationException& ex)
1616cdf0e10cSrcweir     {
1617cdf0e10cSrcweir         eError = ERRCODE_IO_GENERAL;
1618cdf0e10cSrcweir         if (
1619cdf0e10cSrcweir             (ex.eError == ::com::sun::star::ucb::ContentCreationError_NO_CONTENT_PROVIDER    ) ||
1620cdf0e10cSrcweir             (ex.eError == ::com::sun::star::ucb::ContentCreationError_CONTENT_CREATION_FAILED)
1621cdf0e10cSrcweir            )
1622cdf0e10cSrcweir         {
1623cdf0e10cSrcweir             eError = ERRCODE_IO_NOTEXISTSPATH;
1624cdf0e10cSrcweir         }
1625cdf0e10cSrcweir     }
1626cdf0e10cSrcweir     catch (const ::com::sun::star::uno::Exception&)
1627cdf0e10cSrcweir     {
1628cdf0e10cSrcweir        eError = ERRCODE_IO_GENERAL;
1629cdf0e10cSrcweir     }
1630cdf0e10cSrcweir 
1631cdf0e10cSrcweir     if( !eError || (eError & ERRCODE_WARNING_MASK) )
1632cdf0e10cSrcweir 	{
1633cdf0e10cSrcweir         if ( pImp->xStorage.is() )
1634cdf0e10cSrcweir             CloseStorage();
1635cdf0e10cSrcweir 
1636cdf0e10cSrcweir         CloseStreams_Impl();
1637cdf0e10cSrcweir 
1638cdf0e10cSrcweir 		::ucbhelper::Content aTempCont;
1639cdf0e10cSrcweir 		if( ::ucbhelper::Content::create( aSource.GetMainURL( INetURLObject::NO_DECODE ), xDummyEnv, aTempCont ) )
1640cdf0e10cSrcweir 		{
1641cdf0e10cSrcweir 			sal_Bool bTransactStarted = sal_False;
1642cdf0e10cSrcweir 			SFX_ITEMSET_ARG( GetItemSet(), pOverWrite, SfxBoolItem, SID_OVERWRITE, sal_False );
1643cdf0e10cSrcweir    			SFX_ITEMSET_ARG( GetItemSet(), pRename, SfxBoolItem, SID_RENAME, sal_False );
1644cdf0e10cSrcweir 			sal_Bool bRename = pRename ? pRename->GetValue() : sal_False;
1645cdf0e10cSrcweir 			sal_Bool bOverWrite = pOverWrite ? pOverWrite->GetValue() : !bRename;
1646cdf0e10cSrcweir 
1647cdf0e10cSrcweir 			try
1648cdf0e10cSrcweir 			{
1649cdf0e10cSrcweir 				if( bOverWrite && ::utl::UCBContentHelper::IsDocument( aDest.GetMainURL( INetURLObject::NO_DECODE ) ) )
1650cdf0e10cSrcweir 				{
1651cdf0e10cSrcweir 					if( ! pImp->m_aBackupURL.getLength() )
1652cdf0e10cSrcweir 						DoInternalBackup_Impl( aOriginalContent );
1653cdf0e10cSrcweir 
1654cdf0e10cSrcweir 					if( pImp->m_aBackupURL.getLength() )
1655cdf0e10cSrcweir 					{
1656cdf0e10cSrcweir 						Reference< XInputStream > aTempInput = aTempCont.openStream();
1657cdf0e10cSrcweir 						bTransactStarted = sal_True;
1658cdf0e10cSrcweir 						aOriginalContent.setPropertyValue( ::rtl::OUString::createFromAscii( "Size" ),
1659cdf0e10cSrcweir 															uno::makeAny( (sal_Int64)0 ) );
1660cdf0e10cSrcweir 						aOriginalContent.writeStream( aTempInput, bOverWrite );
1661cdf0e10cSrcweir 						bResult = sal_True;
1662cdf0e10cSrcweir 					}
1663cdf0e10cSrcweir 					else
1664cdf0e10cSrcweir 					{
1665cdf0e10cSrcweir 						eError = ERRCODE_SFX_CANTCREATEBACKUP;
1666cdf0e10cSrcweir 					}
1667cdf0e10cSrcweir 				}
1668cdf0e10cSrcweir 				else
1669cdf0e10cSrcweir 				{
1670cdf0e10cSrcweir 					Reference< XInputStream > aTempInput = aTempCont.openStream();
1671cdf0e10cSrcweir 					aOriginalContent.writeStream( aTempInput, bOverWrite );
1672cdf0e10cSrcweir 					bResult = sal_True;
1673cdf0e10cSrcweir 				}
1674cdf0e10cSrcweir 			}
1675cdf0e10cSrcweir 			catch ( ::com::sun::star::ucb::CommandAbortedException& )
1676cdf0e10cSrcweir 			{
1677cdf0e10cSrcweir 				eError = ERRCODE_ABORT;
1678cdf0e10cSrcweir 			}
1679cdf0e10cSrcweir 			catch ( ::com::sun::star::ucb::CommandFailedException& )
1680cdf0e10cSrcweir 			{
1681cdf0e10cSrcweir 				eError = ERRCODE_ABORT;
1682cdf0e10cSrcweir 			}
1683cdf0e10cSrcweir 			catch ( ::com::sun::star::ucb::InteractiveIOException& r )
1684cdf0e10cSrcweir 			{
1685cdf0e10cSrcweir 				if ( r.Code == IOErrorCode_ACCESS_DENIED )
1686cdf0e10cSrcweir 					eError = ERRCODE_IO_ACCESSDENIED;
1687cdf0e10cSrcweir 				else if ( r.Code == IOErrorCode_NOT_EXISTING )
1688cdf0e10cSrcweir 					eError = ERRCODE_IO_NOTEXISTS;
1689cdf0e10cSrcweir 				else if ( r.Code == IOErrorCode_CANT_READ )
1690cdf0e10cSrcweir 					eError = ERRCODE_IO_CANTREAD;
1691cdf0e10cSrcweir 				else
1692cdf0e10cSrcweir 					eError = ERRCODE_IO_GENERAL;
1693cdf0e10cSrcweir 			}
1694cdf0e10cSrcweir             catch ( ::com::sun::star::uno::Exception& )
1695cdf0e10cSrcweir 			{
1696cdf0e10cSrcweir 				eError = ERRCODE_IO_GENERAL;
1697cdf0e10cSrcweir 			}
1698cdf0e10cSrcweir 
1699cdf0e10cSrcweir    			if ( bResult )
1700cdf0e10cSrcweir    			{
1701cdf0e10cSrcweir 				if ( pImp->pTempFile )
1702cdf0e10cSrcweir 				{
1703cdf0e10cSrcweir 					pImp->pTempFile->EnableKillingFile( sal_True );
1704cdf0e10cSrcweir    					delete pImp->pTempFile;
1705cdf0e10cSrcweir    					pImp->pTempFile = NULL;
1706cdf0e10cSrcweir 				}
1707cdf0e10cSrcweir    			}
1708cdf0e10cSrcweir 			else if ( bTransactStarted )
1709cdf0e10cSrcweir 			{
1710cdf0e10cSrcweir 				UseBackupToRestore_Impl( aOriginalContent, xDummyEnv );
1711cdf0e10cSrcweir 			}
1712cdf0e10cSrcweir 		}
1713cdf0e10cSrcweir 		else
1714cdf0e10cSrcweir 			eError = ERRCODE_IO_CANTREAD;
1715cdf0e10cSrcweir 	}
1716cdf0e10cSrcweir 
1717cdf0e10cSrcweir 	return bResult;
1718cdf0e10cSrcweir }
1719cdf0e10cSrcweir 
1720cdf0e10cSrcweir //------------------------------------------------------------------
1721cdf0e10cSrcweir sal_Bool SfxMedium::TryDirectTransfer( const ::rtl::OUString& aURL, SfxItemSet& aTargetSet )
1722cdf0e10cSrcweir {
1723cdf0e10cSrcweir     if ( GetError() )
1724cdf0e10cSrcweir         return sal_False;
1725cdf0e10cSrcweir 
1726cdf0e10cSrcweir 	// if the document had no password it should be stored without password
1727cdf0e10cSrcweir 	// if the document had password it should be stored with the same password
1728cdf0e10cSrcweir 	// otherwise the stream copying can not be done
1729cdf0e10cSrcweir     SFX_ITEMSET_ARG( &aTargetSet, pNewPassItem, SfxStringItem, SID_PASSWORD, sal_False );
1730cdf0e10cSrcweir     SFX_ITEMSET_ARG( GetItemSet(), pOldPassItem, SfxStringItem, SID_PASSWORD, sal_False );
1731cdf0e10cSrcweir 	if ( ( !pNewPassItem && !pOldPassItem )
1732cdf0e10cSrcweir 	  || ( pNewPassItem && pOldPassItem && pNewPassItem->GetValue().Equals( pOldPassItem->GetValue() ) ) )
1733cdf0e10cSrcweir 	{
1734cdf0e10cSrcweir 		// the filter must be the same
1735cdf0e10cSrcweir 		SFX_ITEMSET_ARG( &aTargetSet, pNewFilterItem, SfxStringItem, SID_FILTER_NAME, sal_False );
1736cdf0e10cSrcweir     	SFX_ITEMSET_ARG( GetItemSet(), pOldFilterItem, SfxStringItem, SID_FILTER_NAME, sal_False );
1737cdf0e10cSrcweir 		if ( pNewFilterItem && pOldFilterItem && pNewFilterItem->GetValue().Equals( pOldFilterItem->GetValue() ) )
1738cdf0e10cSrcweir 		{
1739cdf0e10cSrcweir 			// get the input stream and copy it
1740cdf0e10cSrcweir 			// in case of success return true
1741cdf0e10cSrcweir 			uno::Reference< io::XInputStream > xInStream = GetInputStream();
1742cdf0e10cSrcweir 
1743cdf0e10cSrcweir             ResetError();
1744cdf0e10cSrcweir 			if ( xInStream.is() )
1745cdf0e10cSrcweir 			{
1746cdf0e10cSrcweir 				try
1747cdf0e10cSrcweir 				{
1748cdf0e10cSrcweir 					uno::Reference< io::XSeekable > xSeek( xInStream, uno::UNO_QUERY );
1749cdf0e10cSrcweir 					sal_Int64 nPos = 0;
1750cdf0e10cSrcweir 					if ( xSeek.is() )
1751cdf0e10cSrcweir 					{
1752cdf0e10cSrcweir 						nPos = xSeek->getPosition();
1753cdf0e10cSrcweir 						xSeek->seek( 0 );
1754cdf0e10cSrcweir 					}
1755cdf0e10cSrcweir 
1756cdf0e10cSrcweir         			uno::Reference < ::com::sun::star::ucb::XCommandEnvironment > xEnv;
1757cdf0e10cSrcweir 					::ucbhelper::Content aTargetContent( aURL, xEnv );
1758cdf0e10cSrcweir 
1759cdf0e10cSrcweir             		InsertCommandArgument aInsertArg;
1760cdf0e10cSrcweir             		aInsertArg.Data = xInStream;
1761cdf0e10cSrcweir                		SFX_ITEMSET_ARG( &aTargetSet, pRename, SfxBoolItem, SID_RENAME, sal_False );
1762cdf0e10cSrcweir                		SFX_ITEMSET_ARG( &aTargetSet, pOverWrite, SfxBoolItem, SID_OVERWRITE, sal_False );
1763cdf0e10cSrcweir                		if ( (pOverWrite && !pOverWrite->GetValue()) // argument says: never overwrite
1764cdf0e10cSrcweir                		  || (pRename && pRename->GetValue()) ) // argument says: rename file
1765cdf0e10cSrcweir             			aInsertArg.ReplaceExisting = sal_False;
1766cdf0e10cSrcweir                		else
1767cdf0e10cSrcweir             			aInsertArg.ReplaceExisting = sal_True; // default is overwrite existing files
1768cdf0e10cSrcweir 
1769cdf0e10cSrcweir     				Any aCmdArg;
1770cdf0e10cSrcweir             		aCmdArg <<= aInsertArg;
1771cdf0e10cSrcweir 					aTargetContent.executeCommand( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "insert" ) ),
1772cdf0e10cSrcweir 													aCmdArg );
1773cdf0e10cSrcweir 
1774cdf0e10cSrcweir 					if ( xSeek.is() )
1775cdf0e10cSrcweir 						xSeek->seek( nPos );
1776cdf0e10cSrcweir 
1777cdf0e10cSrcweir 					return sal_True;
1778cdf0e10cSrcweir 				}
1779cdf0e10cSrcweir 				catch( uno::Exception& )
1780cdf0e10cSrcweir 				{}
1781cdf0e10cSrcweir 			}
1782cdf0e10cSrcweir 		}
1783cdf0e10cSrcweir 	}
1784cdf0e10cSrcweir 
1785cdf0e10cSrcweir 	return sal_False;
1786cdf0e10cSrcweir }
1787cdf0e10cSrcweir 
1788cdf0e10cSrcweir //------------------------------------------------------------------
1789cdf0e10cSrcweir void SfxMedium::Transfer_Impl()
1790cdf0e10cSrcweir {
1791cdf0e10cSrcweir 	// The transfer is required only in two cases: either if there is a temporary file or if there is a salvage item
1792cdf0e10cSrcweir 	String aNameURL;
1793cdf0e10cSrcweir 	if ( pImp->pTempFile )
1794cdf0e10cSrcweir 		aNameURL = pImp->pTempFile->GetURL();
1795cdf0e10cSrcweir 	else if ( aLogicName.Len() && pImp->m_bSalvageMode )
1796cdf0e10cSrcweir 	{
1797cdf0e10cSrcweir 		// makes sence only in case logic name is set
1798cdf0e10cSrcweir 		if ( !::utl::LocalFileHelper::ConvertPhysicalNameToURL( aName, aNameURL ) )
1799cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "The medium name is not convertable!\n" );
1800cdf0e10cSrcweir 	}
1801cdf0e10cSrcweir 
1802cdf0e10cSrcweir     if ( aNameURL.Len() && ( !eError || (eError & ERRCODE_WARNING_MASK) ) )
1803cdf0e10cSrcweir     {
1804cdf0e10cSrcweir 		RTL_LOGFILE_CONTEXT( aLog, "sfx2 (mv76033) SfxMedium::Transfer_Impl, copying to target" );
1805cdf0e10cSrcweir 
1806cdf0e10cSrcweir         Reference < ::com::sun::star::ucb::XCommandEnvironment > xEnv;
1807cdf0e10cSrcweir 		Reference< XOutputStream > rOutStream;
1808cdf0e10cSrcweir 
1809cdf0e10cSrcweir 		// in case an output stream is provided from outside and the URL is correct
1810cdf0e10cSrcweir 		// commit to the stream
1811cdf0e10cSrcweir         if( aLogicName.CompareToAscii( "private:stream", 14 ) == COMPARE_EQUAL )
1812cdf0e10cSrcweir 		{
1813cdf0e10cSrcweir 			// TODO/LATER: support storing to SID_STREAM
1814cdf0e10cSrcweir 	   		SFX_ITEMSET_ARG( pSet, pOutStreamItem, SfxUnoAnyItem, SID_OUTPUTSTREAM, sal_False);
1815cdf0e10cSrcweir 	 		if( pOutStreamItem && ( pOutStreamItem->GetValue() >>= rOutStream ) )
1816cdf0e10cSrcweir 			{
1817cdf0e10cSrcweir                 if ( pImp->xStorage.is() )
1818cdf0e10cSrcweir                     CloseStorage();
1819cdf0e10cSrcweir 
1820cdf0e10cSrcweir                 CloseStreams_Impl();
1821cdf0e10cSrcweir 
1822cdf0e10cSrcweir     		    INetURLObject aSource( aNameURL );
1823cdf0e10cSrcweir 				::ucbhelper::Content aTempCont;
1824cdf0e10cSrcweir 				if( ::ucbhelper::Content::create( aSource.GetMainURL( INetURLObject::NO_DECODE ), xEnv, aTempCont ) )
1825cdf0e10cSrcweir 				{
1826cdf0e10cSrcweir 					try
1827cdf0e10cSrcweir 					{
1828cdf0e10cSrcweir 						sal_Int32 nRead;
1829cdf0e10cSrcweir 						sal_Int32 nBufferSize = 32767;
1830cdf0e10cSrcweir 						Sequence < sal_Int8 > aSequence ( nBufferSize );
1831cdf0e10cSrcweir 						Reference< XInputStream > aTempInput = aTempCont.openStream();
1832cdf0e10cSrcweir 
1833cdf0e10cSrcweir 						do
1834cdf0e10cSrcweir 						{
1835cdf0e10cSrcweir 							nRead = aTempInput->readBytes ( aSequence, nBufferSize );
1836cdf0e10cSrcweir 							if ( nRead < nBufferSize )
1837cdf0e10cSrcweir 							{
1838cdf0e10cSrcweir 								Sequence < sal_Int8 > aTempBuf ( aSequence.getConstArray(), nRead );
1839cdf0e10cSrcweir 								rOutStream->writeBytes ( aTempBuf );
1840cdf0e10cSrcweir 							}
1841cdf0e10cSrcweir 							else
1842cdf0e10cSrcweir 								rOutStream->writeBytes ( aSequence );
1843cdf0e10cSrcweir 						}
1844cdf0e10cSrcweir 						while ( nRead == nBufferSize );
1845cdf0e10cSrcweir 
1846cdf0e10cSrcweir 						// remove temporary file
1847cdf0e10cSrcweir 						if ( pImp->pTempFile )
1848cdf0e10cSrcweir 						{
1849cdf0e10cSrcweir             				pImp->pTempFile->EnableKillingFile( sal_True );
1850cdf0e10cSrcweir             				delete pImp->pTempFile;
1851cdf0e10cSrcweir             				pImp->pTempFile = NULL;
1852cdf0e10cSrcweir 						}
1853cdf0e10cSrcweir 					}
1854cdf0e10cSrcweir 					catch( Exception& )
1855cdf0e10cSrcweir 					{}
1856cdf0e10cSrcweir 				}
1857cdf0e10cSrcweir        		}
1858cdf0e10cSrcweir 			else
1859cdf0e10cSrcweir 			{
1860cdf0e10cSrcweir 				DBG_ERROR( "Illegal Output stream parameter!\n" );
1861cdf0e10cSrcweir         		SetError( ERRCODE_IO_GENERAL, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
1862cdf0e10cSrcweir 			}
1863cdf0e10cSrcweir 
1864cdf0e10cSrcweir 			// free the reference
1865cdf0e10cSrcweir     		if ( pSet )
1866cdf0e10cSrcweir 				pSet->ClearItem( SID_OUTPUTSTREAM );
1867cdf0e10cSrcweir 
1868cdf0e10cSrcweir 			return;
1869cdf0e10cSrcweir 		}
1870cdf0e10cSrcweir 
1871cdf0e10cSrcweir         GetContent();
1872cdf0e10cSrcweir         if ( !pImp->aContent.get().is() )
1873cdf0e10cSrcweir         {
1874cdf0e10cSrcweir             eError = ERRCODE_IO_NOTEXISTS;
1875cdf0e10cSrcweir             return;
1876cdf0e10cSrcweir         }
1877cdf0e10cSrcweir 
1878cdf0e10cSrcweir         SFX_ITEMSET_ARG( GetItemSet(), pSegmentSize, SfxInt32Item, SID_SEGMENTSIZE, sal_False);
1879cdf0e10cSrcweir         if ( pSegmentSize )
1880cdf0e10cSrcweir         {
1881cdf0e10cSrcweir             // this file must be stored into a disk spanned package
1882cdf0e10cSrcweir             try
1883cdf0e10cSrcweir             {
1884cdf0e10cSrcweir                 uno::Reference < embed::XStorage > xStor = comphelper::OStorageHelper::GetStorageFromURL( GetName(),
1885cdf0e10cSrcweir                         embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
1886cdf0e10cSrcweir 
1887cdf0e10cSrcweir                 // set segment size property; package will automatically be divided in pieces fitting
1888cdf0e10cSrcweir                 // into this size
1889cdf0e10cSrcweir                 ::com::sun::star::uno::Any aAny;
1890cdf0e10cSrcweir                 aAny <<= pSegmentSize->GetValue();
1891cdf0e10cSrcweir 
1892cdf0e10cSrcweir                 uno::Reference < beans::XPropertySet > xSet( pImp->xStorage, uno::UNO_QUERY );
1893cdf0e10cSrcweir                 xSet->setPropertyValue( String::CreateFromAscii("SegmentSize"), aAny );
1894cdf0e10cSrcweir 
1895cdf0e10cSrcweir                 // copy the temporary storage into the disk spanned package
1896cdf0e10cSrcweir                 GetStorage()->copyToStorage( xStor );
1897cdf0e10cSrcweir                 uno::Reference < embed::XTransactedObject > xTrans( pImp->xStorage, uno::UNO_QUERY );
1898cdf0e10cSrcweir                 if ( xTrans.is() )
1899cdf0e10cSrcweir                     xTrans->commit();
1900cdf0e10cSrcweir 
1901cdf0e10cSrcweir             }
1902cdf0e10cSrcweir             catch ( uno::Exception& )
1903cdf0e10cSrcweir             {
1904cdf0e10cSrcweir                 //TODO/MBA: error handling
1905cdf0e10cSrcweir             }
1906cdf0e10cSrcweir             return;
1907cdf0e10cSrcweir         }
1908cdf0e10cSrcweir 
1909cdf0e10cSrcweir         INetURLObject aDest( GetURLObject() );
1910cdf0e10cSrcweir 
1911cdf0e10cSrcweir         // source is the temp file written so far
1912cdf0e10cSrcweir         INetURLObject aSource( aNameURL );
1913cdf0e10cSrcweir 
1914cdf0e10cSrcweir 		// a special case, an interaction handler should be used for
1915cdf0e10cSrcweir 		// authentication in case it is available
1916cdf0e10cSrcweir 		Reference< ::com::sun::star::ucb::XCommandEnvironment > xComEnv;
1917cdf0e10cSrcweir        	Reference< ::com::sun::star::task::XInteractionHandler > xInteractionHandler = GetInteractionHandler();
1918cdf0e10cSrcweir         if (xInteractionHandler.is())
1919cdf0e10cSrcweir 			xComEnv = new ::ucbhelper::CommandEnvironment( xInteractionHandler,
1920cdf0e10cSrcweir 													  Reference< ::com::sun::star::ucb::XProgressHandler >() );
1921cdf0e10cSrcweir 
1922cdf0e10cSrcweir         if ( ::utl::LocalFileHelper::IsLocalFile( aDest.GetMainURL( INetURLObject::NO_DECODE ) ) || !aDest.removeSegment() )
1923cdf0e10cSrcweir 		{
1924cdf0e10cSrcweir 			TransactedTransferForFS_Impl( aSource, aDest, xComEnv );
1925cdf0e10cSrcweir 		}
1926cdf0e10cSrcweir 		else
1927cdf0e10cSrcweir         {
1928cdf0e10cSrcweir             // create content for the parent folder and call transfer on that content with the source content
1929cdf0e10cSrcweir             // and the destination file name as parameters
1930cdf0e10cSrcweir             ::ucbhelper::Content aSourceContent;
1931cdf0e10cSrcweir             ::ucbhelper::Content aTransferContent;
1932cdf0e10cSrcweir 
1933cdf0e10cSrcweir             String aFileName = GetLongName();
1934cdf0e10cSrcweir             if ( !aFileName.Len() )
1935cdf0e10cSrcweir                 aFileName = GetURLObject().getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
1936cdf0e10cSrcweir 
1937cdf0e10cSrcweir 			try
1938cdf0e10cSrcweir 			{
1939cdf0e10cSrcweir 				aTransferContent = ::ucbhelper::Content( aDest.GetMainURL( INetURLObject::NO_DECODE ), xComEnv );
1940cdf0e10cSrcweir 			}
1941cdf0e10cSrcweir 			catch (const ::com::sun::star::ucb::ContentCreationException& ex)
1942cdf0e10cSrcweir             {
1943cdf0e10cSrcweir 				eError = ERRCODE_IO_GENERAL;
1944cdf0e10cSrcweir 				if (
1945cdf0e10cSrcweir 					(ex.eError == ::com::sun::star::ucb::ContentCreationError_NO_CONTENT_PROVIDER    ) ||
1946cdf0e10cSrcweir 					(ex.eError == ::com::sun::star::ucb::ContentCreationError_CONTENT_CREATION_FAILED)
1947cdf0e10cSrcweir 				   )
1948cdf0e10cSrcweir 				{
1949cdf0e10cSrcweir 					eError = ERRCODE_IO_NOTEXISTSPATH;
1950cdf0e10cSrcweir 				}
1951cdf0e10cSrcweir             }
1952cdf0e10cSrcweir             catch (const ::com::sun::star::uno::Exception&)
1953cdf0e10cSrcweir             {
1954cdf0e10cSrcweir                 eError = ERRCODE_IO_GENERAL;
1955cdf0e10cSrcweir             }
1956cdf0e10cSrcweir 
1957cdf0e10cSrcweir             if ( !eError || (eError & ERRCODE_WARNING_MASK) )
1958cdf0e10cSrcweir             {
1959cdf0e10cSrcweir                 // free resources, otherwise the transfer may fail
1960cdf0e10cSrcweir                 if ( pImp->xStorage.is() )
1961cdf0e10cSrcweir                     CloseStorage();
1962cdf0e10cSrcweir 
1963cdf0e10cSrcweir                 CloseStreams_Impl();
1964cdf0e10cSrcweir 
1965cdf0e10cSrcweir                 ::ucbhelper::Content::create( aSource.GetMainURL( INetURLObject::NO_DECODE ), xEnv, aSourceContent );
1966cdf0e10cSrcweir 
1967cdf0e10cSrcweir                 // check for external parameters that may customize the handling of NameClash situations
1968cdf0e10cSrcweir                 SFX_ITEMSET_ARG( GetItemSet(), pRename, SfxBoolItem, SID_RENAME, sal_False );
1969cdf0e10cSrcweir                 SFX_ITEMSET_ARG( GetItemSet(), pOverWrite, SfxBoolItem, SID_OVERWRITE, sal_False );
1970cdf0e10cSrcweir                 sal_Int32 nNameClash;
1971cdf0e10cSrcweir                 if ( pOverWrite && !pOverWrite->GetValue() )
1972cdf0e10cSrcweir                     // argument says: never overwrite
1973cdf0e10cSrcweir                     nNameClash = NameClash::ERROR;
1974cdf0e10cSrcweir                 else if ( pRename && pRename->GetValue() )
1975cdf0e10cSrcweir                     // argument says: rename file
1976cdf0e10cSrcweir                     nNameClash = NameClash::RENAME;
1977cdf0e10cSrcweir                 else
1978cdf0e10cSrcweir                     // default is overwrite existing files
1979cdf0e10cSrcweir                     nNameClash = NameClash::OVERWRITE;
1980cdf0e10cSrcweir 
1981cdf0e10cSrcweir                 try
1982cdf0e10cSrcweir                 {
1983cdf0e10cSrcweir                     if (!aTransferContent.transferContent( aSourceContent, ::ucbhelper::InsertOperation_COPY, aFileName, nNameClash ))
1984cdf0e10cSrcweir 						eError = ERRCODE_IO_GENERAL;
1985cdf0e10cSrcweir                 }
1986cdf0e10cSrcweir                 catch ( ::com::sun::star::ucb::CommandAbortedException& )
1987cdf0e10cSrcweir                 {
1988cdf0e10cSrcweir                     eError = ERRCODE_ABORT;
1989cdf0e10cSrcweir                 }
1990cdf0e10cSrcweir                 catch ( ::com::sun::star::ucb::CommandFailedException& )
1991cdf0e10cSrcweir                 {
1992cdf0e10cSrcweir                     eError = ERRCODE_ABORT;
1993cdf0e10cSrcweir                 }
1994cdf0e10cSrcweir                 catch ( ::com::sun::star::ucb::InteractiveIOException& r )
1995cdf0e10cSrcweir                 {
1996cdf0e10cSrcweir                     if ( r.Code == IOErrorCode_ACCESS_DENIED )
1997cdf0e10cSrcweir                         eError = ERRCODE_IO_ACCESSDENIED;
1998cdf0e10cSrcweir                     else if ( r.Code == IOErrorCode_NOT_EXISTING )
1999cdf0e10cSrcweir                         eError = ERRCODE_IO_NOTEXISTS;
2000cdf0e10cSrcweir                     else if ( r.Code == IOErrorCode_CANT_READ )
2001cdf0e10cSrcweir                         eError = ERRCODE_IO_CANTREAD;
2002cdf0e10cSrcweir                     else
2003cdf0e10cSrcweir                         eError = ERRCODE_IO_GENERAL;
2004cdf0e10cSrcweir                 }
2005cdf0e10cSrcweir                 catch ( ::com::sun::star::uno::Exception& )
2006cdf0e10cSrcweir                 {
2007cdf0e10cSrcweir                     eError = ERRCODE_IO_GENERAL;
2008cdf0e10cSrcweir                 }
2009cdf0e10cSrcweir 
2010cdf0e10cSrcweir 				// do not switch from temporary file in case of nonfile protocol
2011cdf0e10cSrcweir 	    	}
2012cdf0e10cSrcweir 		}
2013cdf0e10cSrcweir 
2014cdf0e10cSrcweir         if ( ( !eError || (eError & ERRCODE_WARNING_MASK) ) && !pImp->pTempFile )
2015cdf0e10cSrcweir 		{
2016cdf0e10cSrcweir 			// without a TempFile the physical and logical name should be the same after successful transfer
2017cdf0e10cSrcweir   			::utl::LocalFileHelper::ConvertURLToPhysicalName( GetURLObject().GetMainURL( INetURLObject::NO_DECODE ),
2018cdf0e10cSrcweir 														  	aName );
2019cdf0e10cSrcweir 			pImp->m_bSalvageMode = sal_False;
2020cdf0e10cSrcweir 		}
2021cdf0e10cSrcweir     }
2022cdf0e10cSrcweir }
2023cdf0e10cSrcweir 
2024cdf0e10cSrcweir //------------------------------------------------------------------
2025cdf0e10cSrcweir void SfxMedium::DoInternalBackup_Impl( const ::ucbhelper::Content& aOriginalContent,
2026cdf0e10cSrcweir 									   const String& aPrefix,
2027cdf0e10cSrcweir 									   const String& aExtension,
2028cdf0e10cSrcweir 									   const String& aDestDir )
2029cdf0e10cSrcweir {
2030cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT( aLog, "sfx2 (mv76033) SfxMedium::DoInternalBackup_Impl( with destdir )" );
2031cdf0e10cSrcweir 
2032cdf0e10cSrcweir 	if ( pImp->m_aBackupURL.getLength() )
2033cdf0e10cSrcweir 		return; // the backup was done already
2034cdf0e10cSrcweir 
2035cdf0e10cSrcweir 	::utl::TempFile aTransactTemp( aPrefix, &aExtension, &aDestDir );
2036cdf0e10cSrcweir 	aTransactTemp.EnableKillingFile( sal_False );
2037cdf0e10cSrcweir 
2038cdf0e10cSrcweir 	INetURLObject aBackObj( aTransactTemp.GetURL() );
2039cdf0e10cSrcweir 	::rtl::OUString aBackupName = aBackObj.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
2040cdf0e10cSrcweir 
2041cdf0e10cSrcweir 	Reference < ::com::sun::star::ucb::XCommandEnvironment > xDummyEnv;
2042cdf0e10cSrcweir 	::ucbhelper::Content aBackupCont;
2043cdf0e10cSrcweir 	if( ::ucbhelper::Content::create( aDestDir, xDummyEnv, aBackupCont ) )
2044cdf0e10cSrcweir 	{
2045cdf0e10cSrcweir 		try
2046cdf0e10cSrcweir 		{
2047cdf0e10cSrcweir 			if( aBackupCont.transferContent( aOriginalContent,
2048cdf0e10cSrcweir 											::ucbhelper::InsertOperation_COPY,
2049cdf0e10cSrcweir 											aBackupName,
2050cdf0e10cSrcweir 											NameClash::OVERWRITE ) )
2051cdf0e10cSrcweir 			{
2052cdf0e10cSrcweir 				pImp->m_aBackupURL = aBackObj.GetMainURL( INetURLObject::NO_DECODE );
2053cdf0e10cSrcweir 				pImp->m_bRemoveBackup = sal_True;
2054cdf0e10cSrcweir 			}
2055cdf0e10cSrcweir 		}
2056cdf0e10cSrcweir 		catch( Exception& )
2057cdf0e10cSrcweir 		{}
2058cdf0e10cSrcweir 	}
2059cdf0e10cSrcweir 
2060cdf0e10cSrcweir 	if ( !pImp->m_aBackupURL.getLength() )
2061cdf0e10cSrcweir 		aTransactTemp.EnableKillingFile( sal_True );
2062cdf0e10cSrcweir }
2063cdf0e10cSrcweir 
2064cdf0e10cSrcweir //------------------------------------------------------------------
2065cdf0e10cSrcweir void SfxMedium::DoInternalBackup_Impl( const ::ucbhelper::Content& aOriginalContent )
2066cdf0e10cSrcweir {
2067cdf0e10cSrcweir 	if ( pImp->m_aBackupURL.getLength() )
2068cdf0e10cSrcweir 		return; // the backup was done already
2069cdf0e10cSrcweir 
2070cdf0e10cSrcweir 	::rtl::OUString aFileName =  GetURLObject().getName( INetURLObject::LAST_SEGMENT,
2071cdf0e10cSrcweir 														true,
2072cdf0e10cSrcweir 														INetURLObject::NO_DECODE );
2073cdf0e10cSrcweir 
2074cdf0e10cSrcweir 	sal_Int32 nPrefixLen = aFileName.lastIndexOf( '.' );
2075cdf0e10cSrcweir 	String aPrefix = ( nPrefixLen == -1 ) ? aFileName : aFileName.copy( 0, nPrefixLen );
2076cdf0e10cSrcweir 	String aExtension = ( nPrefixLen == -1 ) ? String() : String(aFileName.copy( nPrefixLen ));
2077cdf0e10cSrcweir    	String aBakDir = SvtPathOptions().GetBackupPath();
2078cdf0e10cSrcweir 
2079cdf0e10cSrcweir 	DoInternalBackup_Impl( aOriginalContent, aPrefix, aExtension, aBakDir );
2080cdf0e10cSrcweir 
2081cdf0e10cSrcweir 	if ( !pImp->m_aBackupURL.getLength() )
2082cdf0e10cSrcweir 	{
2083cdf0e10cSrcweir 		// the copiing to the backup catalog failed ( for example because
2084cdf0e10cSrcweir 		// of using an encrypted partition as target catalog )
2085cdf0e10cSrcweir 		// since the user did not specify to make backup explicitly
2086cdf0e10cSrcweir 		// office should try to make backup in another place,
2087cdf0e10cSrcweir 		// target catalog does not look bad for this case ( and looks
2088cdf0e10cSrcweir 		// to be the only way for encrypted partitions )
2089cdf0e10cSrcweir 
2090cdf0e10cSrcweir 		INetURLObject aDest = GetURLObject();
2091cdf0e10cSrcweir 		if ( aDest.removeSegment() )
2092cdf0e10cSrcweir 			DoInternalBackup_Impl( aOriginalContent, aPrefix, aExtension, aDest.GetMainURL( INetURLObject::NO_DECODE ) );
2093cdf0e10cSrcweir 	}
2094cdf0e10cSrcweir }
2095cdf0e10cSrcweir 
2096cdf0e10cSrcweir 
2097cdf0e10cSrcweir //------------------------------------------------------------------
2098cdf0e10cSrcweir void SfxMedium::DoBackup_Impl()
2099cdf0e10cSrcweir {
2100cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT( aLog, "sfx2 (mv76033) SfxMedium::DoBackup_Impl" );
2101cdf0e10cSrcweir 
2102cdf0e10cSrcweir    	// source file name is the logical name of this medium
2103cdf0e10cSrcweir     INetURLObject aSource( GetURLObject() );
2104cdf0e10cSrcweir 
2105cdf0e10cSrcweir 	// there is nothing to backup in case source file does not exist
2106cdf0e10cSrcweir 	if ( !::utl::UCBContentHelper::IsDocument( aSource.GetMainURL( INetURLObject::NO_DECODE ) ) )
2107cdf0e10cSrcweir 		return;
2108cdf0e10cSrcweir 
2109cdf0e10cSrcweir     sal_Bool        bSuccess = sal_False;
2110cdf0e10cSrcweir 
2111cdf0e10cSrcweir     // get path for backups
2112cdf0e10cSrcweir     String aBakDir = SvtPathOptions().GetBackupPath();
2113cdf0e10cSrcweir     if( aBakDir.Len() )
2114cdf0e10cSrcweir     {
2115cdf0e10cSrcweir         // create content for the parent folder ( = backup folder )
2116cdf0e10cSrcweir         ::ucbhelper::Content  aContent;
2117cdf0e10cSrcweir         Reference < ::com::sun::star::ucb::XCommandEnvironment > xEnv;
2118cdf0e10cSrcweir         if( ::ucbhelper::Content::create( aBakDir, xEnv, aContent ) )
2119cdf0e10cSrcweir 		{
2120cdf0e10cSrcweir         	// save as ".bak" file
2121cdf0e10cSrcweir         	INetURLObject aDest( aBakDir );
2122cdf0e10cSrcweir         	aDest.insertName( aSource.getName() );
2123cdf0e10cSrcweir         	aDest.setExtension( DEFINE_CONST_UNICODE( "bak" ) );
2124cdf0e10cSrcweir         	String aFileName = aDest.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
2125cdf0e10cSrcweir 
2126cdf0e10cSrcweir         	// create a content for the source file
2127cdf0e10cSrcweir         	::ucbhelper::Content aSourceContent;
2128cdf0e10cSrcweir         	if ( ::ucbhelper::Content::create( aSource.GetMainURL( INetURLObject::NO_DECODE ), xEnv, aSourceContent ) )
2129cdf0e10cSrcweir         	{
2130cdf0e10cSrcweir             	try
2131cdf0e10cSrcweir             	{
2132cdf0e10cSrcweir                 	// do the transfer ( copy source file to backup dir )
2133cdf0e10cSrcweir                 	bSuccess = aContent.transferContent( aSourceContent,
2134cdf0e10cSrcweir 														::ucbhelper::InsertOperation_COPY,
2135cdf0e10cSrcweir 														aFileName,
2136cdf0e10cSrcweir 														NameClash::OVERWRITE );
2137cdf0e10cSrcweir 					if( bSuccess )
2138cdf0e10cSrcweir 					{
2139cdf0e10cSrcweir 						pImp->m_aBackupURL = aDest.GetMainURL( INetURLObject::NO_DECODE );
2140cdf0e10cSrcweir 						pImp->m_bRemoveBackup = sal_False;
2141cdf0e10cSrcweir 					}
2142cdf0e10cSrcweir             	}
2143cdf0e10cSrcweir             	catch ( ::com::sun::star::uno::Exception& )
2144cdf0e10cSrcweir             	{
2145cdf0e10cSrcweir             	}
2146cdf0e10cSrcweir         	}
2147cdf0e10cSrcweir     	}
2148cdf0e10cSrcweir 	}
2149cdf0e10cSrcweir 
2150cdf0e10cSrcweir     if ( !bSuccess )
2151cdf0e10cSrcweir 	{
2152cdf0e10cSrcweir 		eError = ERRCODE_SFX_CANTCREATEBACKUP;
2153cdf0e10cSrcweir 	}
2154cdf0e10cSrcweir }
2155cdf0e10cSrcweir 
2156cdf0e10cSrcweir //------------------------------------------------------------------
2157cdf0e10cSrcweir void SfxMedium::ClearBackup_Impl()
2158cdf0e10cSrcweir {
2159cdf0e10cSrcweir 	if( pImp->m_bRemoveBackup )
2160cdf0e10cSrcweir 	{
2161cdf0e10cSrcweir 		// currently a document is always stored in a new medium,
2162cdf0e10cSrcweir 		// thus if a backup can not be removed the backup URL should not be cleaned
2163cdf0e10cSrcweir 		if ( pImp->m_aBackupURL.getLength() )
2164cdf0e10cSrcweir 		{
2165cdf0e10cSrcweir 			if ( ::utl::UCBContentHelper::Kill( pImp->m_aBackupURL ) )
2166cdf0e10cSrcweir 			  // || !::utl::UCBContentHelper::IsDocument( pImp->m_aBackupURL ) );
2167cdf0e10cSrcweir 			{
2168cdf0e10cSrcweir 				pImp->m_bRemoveBackup = sal_False;
2169cdf0e10cSrcweir 				pImp->m_aBackupURL = ::rtl::OUString();
2170cdf0e10cSrcweir 			}
2171cdf0e10cSrcweir 			else
2172cdf0e10cSrcweir 			{
2173cdf0e10cSrcweir 
2174cdf0e10cSrcweir 				DBG_ERROR("Couldn't remove backup file!");
2175cdf0e10cSrcweir 			}
2176cdf0e10cSrcweir 		}
2177cdf0e10cSrcweir 	}
2178cdf0e10cSrcweir 	else
2179cdf0e10cSrcweir 		pImp->m_aBackupURL = ::rtl::OUString();
2180cdf0e10cSrcweir }
2181cdf0e10cSrcweir 
2182cdf0e10cSrcweir //----------------------------------------------------------------
2183cdf0e10cSrcweir void SfxMedium::GetLockingStream_Impl()
2184cdf0e10cSrcweir {
2185cdf0e10cSrcweir     if ( ::utl::LocalFileHelper::IsLocalFile( GetURLObject().GetMainURL( INetURLObject::NO_DECODE ) )
2186cdf0e10cSrcweir       && !pImp->m_xLockingStream.is() )
2187cdf0e10cSrcweir     {
2188cdf0e10cSrcweir         SFX_ITEMSET_ARG( pSet, pWriteStreamItem, SfxUnoAnyItem, SID_STREAM, sal_False);
2189cdf0e10cSrcweir         if ( pWriteStreamItem )
2190cdf0e10cSrcweir             pWriteStreamItem->GetValue() >>= pImp->m_xLockingStream;
2191cdf0e10cSrcweir 
2192cdf0e10cSrcweir         if ( !pImp->m_xLockingStream.is() )
2193cdf0e10cSrcweir         {
2194cdf0e10cSrcweir             // open the original document
2195cdf0e10cSrcweir             uno::Sequence< beans::PropertyValue > xProps;
2196cdf0e10cSrcweir             TransformItems( SID_OPENDOC, *GetItemSet(), xProps );
2197cdf0e10cSrcweir             comphelper::MediaDescriptor aMedium( xProps );
2198cdf0e10cSrcweir 
2199cdf0e10cSrcweir             aMedium.addInputStreamOwnLock();
2200cdf0e10cSrcweir 
2201cdf0e10cSrcweir             uno::Reference< io::XInputStream > xInputStream;
2202cdf0e10cSrcweir             aMedium[comphelper::MediaDescriptor::PROP_STREAM()] >>= pImp->m_xLockingStream;
2203cdf0e10cSrcweir             aMedium[comphelper::MediaDescriptor::PROP_INPUTSTREAM()] >>= xInputStream;
2204cdf0e10cSrcweir 
2205cdf0e10cSrcweir             if ( !pImp->pTempFile && !aName.Len() )
2206cdf0e10cSrcweir             {
2207cdf0e10cSrcweir                 // the medium is still based on the original file, it makes sence to initialize the streams
2208cdf0e10cSrcweir                 if ( pImp->m_xLockingStream.is() )
2209cdf0e10cSrcweir                     pImp->xStream = pImp->m_xLockingStream;
2210cdf0e10cSrcweir 
2211cdf0e10cSrcweir                 if ( xInputStream.is() )
2212cdf0e10cSrcweir                     pImp->xInputStream = xInputStream;
2213cdf0e10cSrcweir 
2214cdf0e10cSrcweir                 if ( !pImp->xInputStream.is() && pImp->xStream.is() )
2215cdf0e10cSrcweir                     pImp->xInputStream = pImp->xStream->getInputStream();
2216cdf0e10cSrcweir             }
2217cdf0e10cSrcweir         }
2218cdf0e10cSrcweir     }
2219cdf0e10cSrcweir }
2220cdf0e10cSrcweir 
2221cdf0e10cSrcweir //----------------------------------------------------------------
2222cdf0e10cSrcweir void SfxMedium::GetMedium_Impl()
2223cdf0e10cSrcweir {
2224cdf0e10cSrcweir     if ( !pInStream )
2225cdf0e10cSrcweir     {
2226cdf0e10cSrcweir         pImp->bDownloadDone = sal_False;
2227cdf0e10cSrcweir         Reference< ::com::sun::star::task::XInteractionHandler > xInteractionHandler = GetInteractionHandler();
2228cdf0e10cSrcweir 
2229cdf0e10cSrcweir         //TODO/MBA: need support for SID_STREAM
2230cdf0e10cSrcweir         SFX_ITEMSET_ARG( pSet, pWriteStreamItem, SfxUnoAnyItem, SID_STREAM, sal_False);
2231cdf0e10cSrcweir         SFX_ITEMSET_ARG( pSet, pInStreamItem, SfxUnoAnyItem, SID_INPUTSTREAM, sal_False);
2232cdf0e10cSrcweir         if ( pWriteStreamItem )
2233cdf0e10cSrcweir         {
2234cdf0e10cSrcweir             pWriteStreamItem->GetValue() >>= pImp->xStream;
2235cdf0e10cSrcweir 
2236cdf0e10cSrcweir             if ( pInStreamItem )
2237cdf0e10cSrcweir                 pInStreamItem->GetValue() >>= pImp->xInputStream;
2238cdf0e10cSrcweir 
2239cdf0e10cSrcweir             if ( !pImp->xInputStream.is() && pImp->xStream.is() )
2240cdf0e10cSrcweir                 pImp->xInputStream = pImp->xStream->getInputStream();
2241cdf0e10cSrcweir         }
2242cdf0e10cSrcweir         else if ( pInStreamItem )
2243cdf0e10cSrcweir         {
2244cdf0e10cSrcweir             pInStreamItem->GetValue() >>= pImp->xInputStream;
2245cdf0e10cSrcweir         }
2246cdf0e10cSrcweir         else
2247cdf0e10cSrcweir         {
2248cdf0e10cSrcweir             uno::Sequence < beans::PropertyValue > xProps;
2249cdf0e10cSrcweir             String aFileName;
2250cdf0e10cSrcweir             if ( aName.Len() )
2251cdf0e10cSrcweir             {
2252cdf0e10cSrcweir                 if ( !::utl::LocalFileHelper::ConvertPhysicalNameToURL( aName, aFileName ) )
2253cdf0e10cSrcweir                 {
2254cdf0e10cSrcweir                     DBG_ERROR("Physical name not convertable!");
2255cdf0e10cSrcweir                 }
2256cdf0e10cSrcweir             }
2257cdf0e10cSrcweir             else
2258cdf0e10cSrcweir                 aFileName = GetName();
2259cdf0e10cSrcweir 
2260cdf0e10cSrcweir             // in case the temporary file exists the streams should be initialized from it,
2261cdf0e10cSrcweir             // but the original MediaDescriptor should not be changed
2262cdf0e10cSrcweir             sal_Bool bFromTempFile = ( pImp->pTempFile != NULL );
2263cdf0e10cSrcweir 
2264cdf0e10cSrcweir             if ( !bFromTempFile )
2265cdf0e10cSrcweir             {
2266cdf0e10cSrcweir                 GetItemSet()->Put( SfxStringItem( SID_FILE_NAME, aFileName ) );
2267cdf0e10cSrcweir                 if( !(nStorOpenMode & STREAM_WRITE ) )
2268cdf0e10cSrcweir                     GetItemSet()->Put( SfxBoolItem( SID_DOC_READONLY, sal_True ) );
2269cdf0e10cSrcweir                 if (xInteractionHandler.is())
2270cdf0e10cSrcweir                     GetItemSet()->Put( SfxUnoAnyItem( SID_INTERACTIONHANDLER, makeAny(xInteractionHandler) ) );
2271cdf0e10cSrcweir             }
2272cdf0e10cSrcweir 
2273cdf0e10cSrcweir             if ( m_xInputStreamToLoadFrom.is() )
2274cdf0e10cSrcweir             {
2275cdf0e10cSrcweir                 pImp->xInputStream = m_xInputStreamToLoadFrom;
2276cdf0e10cSrcweir                 pImp->xInputStream->skipBytes(0);
2277cdf0e10cSrcweir                 if(m_bIsReadOnly)
2278cdf0e10cSrcweir                     GetItemSet()->Put( SfxBoolItem( SID_DOC_READONLY, sal_True ) );
2279cdf0e10cSrcweir 
2280cdf0e10cSrcweir                 // m_xInputStreamToLoadFrom = 0;
2281cdf0e10cSrcweir             }
2282cdf0e10cSrcweir             else
2283cdf0e10cSrcweir             {
2284cdf0e10cSrcweir                 TransformItems( SID_OPENDOC, *GetItemSet(), xProps );
2285cdf0e10cSrcweir                 comphelper::MediaDescriptor aMedium( xProps );
2286cdf0e10cSrcweir 
2287cdf0e10cSrcweir                 if ( pImp->m_xLockingStream.is() && !bFromTempFile )
2288cdf0e10cSrcweir                 {
2289cdf0e10cSrcweir                     // the medium is not based on the temporary file, so the original stream can be used
2290cdf0e10cSrcweir                     pImp->xStream = pImp->m_xLockingStream;
2291cdf0e10cSrcweir                 }
2292cdf0e10cSrcweir                 else
2293cdf0e10cSrcweir                 {
2294cdf0e10cSrcweir                     if ( bFromTempFile )
2295cdf0e10cSrcweir                     {
2296cdf0e10cSrcweir                         aMedium[comphelper::MediaDescriptor::PROP_URL()] <<= ::rtl::OUString( aFileName );
2297cdf0e10cSrcweir                         aMedium.erase( comphelper::MediaDescriptor::PROP_READONLY() );
2298cdf0e10cSrcweir                         aMedium.addInputStream();
2299cdf0e10cSrcweir                     }
2300cdf0e10cSrcweir                     else if ( ::utl::LocalFileHelper::IsLocalFile( GetURLObject().GetMainURL( INetURLObject::NO_DECODE ) ) )
2301cdf0e10cSrcweir                     {
2302cdf0e10cSrcweir                         // use the special locking approach only for file URLs
2303cdf0e10cSrcweir                         aMedium.addInputStreamOwnLock();
2304cdf0e10cSrcweir                     }
2305cdf0e10cSrcweir                     else
2306cdf0e10cSrcweir                         aMedium.addInputStream();
2307cdf0e10cSrcweir 
2308cdf0e10cSrcweir                     // the ReadOnly property set in aMedium is ignored
2309cdf0e10cSrcweir                     // the check is done in LockOrigFileOnDemand() for file and non-file URLs
2310cdf0e10cSrcweir 
2311cdf0e10cSrcweir                     //TODO/MBA: what happens if property is not there?!
2312cdf0e10cSrcweir                     aMedium[comphelper::MediaDescriptor::PROP_STREAM()] >>= pImp->xStream;
2313cdf0e10cSrcweir                     aMedium[comphelper::MediaDescriptor::PROP_INPUTSTREAM()] >>= pImp->xInputStream;
2314cdf0e10cSrcweir                 }
2315cdf0e10cSrcweir 
2316cdf0e10cSrcweir                 GetContent();
2317cdf0e10cSrcweir                 if ( !pImp->xInputStream.is() && pImp->xStream.is() )
2318cdf0e10cSrcweir                     pImp->xInputStream = pImp->xStream->getInputStream();
2319cdf0e10cSrcweir             }
2320cdf0e10cSrcweir 
2321cdf0e10cSrcweir             if ( !bFromTempFile )
2322cdf0e10cSrcweir             {
2323cdf0e10cSrcweir                 //TODO/MBA: need support for SID_STREAM
2324cdf0e10cSrcweir                 if ( pImp->xStream.is() )
2325cdf0e10cSrcweir                     GetItemSet()->Put( SfxUsrAnyItem( SID_STREAM, makeAny( pImp->xStream ) ) );
2326cdf0e10cSrcweir 
2327cdf0e10cSrcweir                 GetItemSet()->Put( SfxUsrAnyItem( SID_INPUTSTREAM, makeAny( pImp->xInputStream ) ) );
2328cdf0e10cSrcweir             }
2329cdf0e10cSrcweir         }
2330cdf0e10cSrcweir 
2331cdf0e10cSrcweir         //TODO/MBA: ErrorHandling - how to transport error from MediaDescriptor
2332cdf0e10cSrcweir         if ( !GetError() && !pImp->xStream.is() && !pImp->xInputStream.is() )
2333cdf0e10cSrcweir             SetError( ERRCODE_IO_ACCESSDENIED, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
2334cdf0e10cSrcweir 
2335cdf0e10cSrcweir         if ( !GetError() )
2336cdf0e10cSrcweir         {
2337cdf0e10cSrcweir             if ( pImp->xStream.is() )
2338cdf0e10cSrcweir                 pInStream = utl::UcbStreamHelper::CreateStream( pImp->xStream );
2339cdf0e10cSrcweir             else if ( pImp->xInputStream.is() )
2340cdf0e10cSrcweir                 pInStream = utl::UcbStreamHelper::CreateStream( pImp->xInputStream );
2341cdf0e10cSrcweir         }
2342cdf0e10cSrcweir 
2343cdf0e10cSrcweir         pImp->bDownloadDone = sal_True;
2344cdf0e10cSrcweir         pImp->aDoneLink.ClearPendingCall();
2345cdf0e10cSrcweir         pImp->aDoneLink.Call( (void*) GetError() );
2346cdf0e10cSrcweir     }
2347cdf0e10cSrcweir }
2348cdf0e10cSrcweir 
2349cdf0e10cSrcweir //----------------------------------------------------------------
2350cdf0e10cSrcweir sal_Bool SfxMedium::IsRemote()
2351cdf0e10cSrcweir {
2352cdf0e10cSrcweir     return bRemote;
2353cdf0e10cSrcweir }
2354cdf0e10cSrcweir 
2355cdf0e10cSrcweir //------------------------------------------------------------------
2356cdf0e10cSrcweir 
2357cdf0e10cSrcweir void SfxMedium::SetUpdatePickList(sal_Bool bVal)
2358cdf0e10cSrcweir {
2359cdf0e10cSrcweir     if(!pImp)
2360cdf0e10cSrcweir         pImp = new SfxMedium_Impl( this );
2361cdf0e10cSrcweir     pImp->bUpdatePickList = bVal;
2362cdf0e10cSrcweir }
2363cdf0e10cSrcweir //------------------------------------------------------------------
2364cdf0e10cSrcweir 
2365cdf0e10cSrcweir sal_Bool SfxMedium::IsUpdatePickList() const
2366cdf0e10cSrcweir {
2367cdf0e10cSrcweir     return pImp? pImp->bUpdatePickList: sal_True;
2368cdf0e10cSrcweir }
2369cdf0e10cSrcweir //----------------------------------------------------------------
2370cdf0e10cSrcweir 
2371cdf0e10cSrcweir void SfxMedium::SetDoneLink( const Link& rLink )
2372cdf0e10cSrcweir {
2373cdf0e10cSrcweir     pImp->aDoneLink = rLink;
2374cdf0e10cSrcweir }
2375cdf0e10cSrcweir 
2376cdf0e10cSrcweir //----------------------------------------------------------------
2377cdf0e10cSrcweir 
2378cdf0e10cSrcweir void SfxMedium::SetDataAvailableLink( const Link& rLink )
2379cdf0e10cSrcweir {
2380cdf0e10cSrcweir     pImp->aAvailableLink = rLink;
2381cdf0e10cSrcweir }
2382cdf0e10cSrcweir 
2383cdf0e10cSrcweir //----------------------------------------------------------------
2384cdf0e10cSrcweir void SfxMedium::StartDownload()
2385cdf0e10cSrcweir {
2386cdf0e10cSrcweir     GetInStream();
2387cdf0e10cSrcweir }
2388cdf0e10cSrcweir 
2389cdf0e10cSrcweir void SfxMedium::DownLoad( const Link& aLink )
2390cdf0e10cSrcweir {
2391cdf0e10cSrcweir     SetDoneLink( aLink );
2392cdf0e10cSrcweir     GetInStream();
2393cdf0e10cSrcweir     if ( pInStream && !aLink.IsSet() )
2394cdf0e10cSrcweir     {
2395cdf0e10cSrcweir         while( !pImp->bDownloadDone )
2396cdf0e10cSrcweir             Application::Yield();
2397cdf0e10cSrcweir     }
2398cdf0e10cSrcweir }
2399cdf0e10cSrcweir 
2400cdf0e10cSrcweir //------------------------------------------------------------------
2401cdf0e10cSrcweir void SfxMedium::Init_Impl()
2402cdf0e10cSrcweir /*  [Beschreibung]
2403cdf0e10cSrcweir     Setzt in den Logischen Namen eine gueltige ::com::sun::star::util::URL (Falls zuvor ein Filename
2404cdf0e10cSrcweir     drin war) und setzt den physikalschen Namen auf den Filenamen, falls
2405cdf0e10cSrcweir     vorhanden.
2406cdf0e10cSrcweir  */
2407cdf0e10cSrcweir 
2408cdf0e10cSrcweir {
2409cdf0e10cSrcweir 	Reference< XOutputStream > rOutStream;
2410cdf0e10cSrcweir 
2411cdf0e10cSrcweir     // TODO/LATER: handle lifetime of storages
2412cdf0e10cSrcweir     pImp->bDisposeStorage = sal_False;
2413cdf0e10cSrcweir 
2414cdf0e10cSrcweir     SFX_ITEMSET_ARG( pSet, pSalvageItem, SfxStringItem, SID_DOC_SALVAGE, sal_False);
2415cdf0e10cSrcweir     if ( pSalvageItem && !pSalvageItem->GetValue().Len() )
2416cdf0e10cSrcweir 	{
2417cdf0e10cSrcweir 		pSalvageItem = NULL;
2418cdf0e10cSrcweir 		pSet->ClearItem( SID_DOC_SALVAGE );
2419cdf0e10cSrcweir 	}
2420cdf0e10cSrcweir 
2421cdf0e10cSrcweir     if( aLogicName.Len() )
2422cdf0e10cSrcweir     {
2423cdf0e10cSrcweir         INetURLObject aUrl( aLogicName );
2424cdf0e10cSrcweir         INetProtocol eProt = aUrl.GetProtocol();
2425cdf0e10cSrcweir         if ( eProt == INET_PROT_NOT_VALID )
2426cdf0e10cSrcweir         {
2427cdf0e10cSrcweir             DBG_ERROR ( "Unknown protocol!" );
2428cdf0e10cSrcweir         }
2429cdf0e10cSrcweir         else
2430cdf0e10cSrcweir         {
2431cdf0e10cSrcweir             if ( aUrl.HasMark() )
2432cdf0e10cSrcweir             {
2433cdf0e10cSrcweir                 aLogicName = aUrl.GetURLNoMark( INetURLObject::NO_DECODE );
2434cdf0e10cSrcweir                 GetItemSet()->Put( SfxStringItem( SID_JUMPMARK, aUrl.GetMark() ) );
2435cdf0e10cSrcweir             }
2436cdf0e10cSrcweir 
2437cdf0e10cSrcweir             // try to convert the URL into a physical name - but never change a physical name
2438cdf0e10cSrcweir             // physical name may be set if the logical name is changed after construction
2439cdf0e10cSrcweir             if ( !aName.Len() )
2440cdf0e10cSrcweir                 ::utl::LocalFileHelper::ConvertURLToPhysicalName( GetURLObject().GetMainURL( INetURLObject::NO_DECODE ), aName );
2441cdf0e10cSrcweir             else {
2442cdf0e10cSrcweir                 DBG_ASSERT( pSalvageItem, "Suspicious change of logical name!" );
2443cdf0e10cSrcweir             }
2444cdf0e10cSrcweir         }
2445cdf0e10cSrcweir     }
2446cdf0e10cSrcweir 
2447cdf0e10cSrcweir     if ( pSalvageItem && pSalvageItem->GetValue().Len() )
2448cdf0e10cSrcweir 	{
2449cdf0e10cSrcweir         aLogicName = pSalvageItem->GetValue();
2450cdf0e10cSrcweir 		DELETEZ( pURLObj );
2451cdf0e10cSrcweir 		pImp->m_bSalvageMode = sal_True;
2452cdf0e10cSrcweir 	}
2453cdf0e10cSrcweir 
2454cdf0e10cSrcweir 	// in case output stream is by mistake here
2455cdf0e10cSrcweir 	// clear the reference
2456cdf0e10cSrcweir     SFX_ITEMSET_ARG( pSet, pOutStreamItem, SfxUnoAnyItem, SID_OUTPUTSTREAM, sal_False);
2457cdf0e10cSrcweir 	if( pOutStreamItem
2458cdf0e10cSrcweir 	 && ( !( pOutStreamItem->GetValue() >>= rOutStream )
2459cdf0e10cSrcweir           || !aLogicName.CompareToAscii( "private:stream", 14 ) == COMPARE_EQUAL ) )
2460cdf0e10cSrcweir 	{
2461cdf0e10cSrcweir 		pSet->ClearItem( SID_OUTPUTSTREAM );
2462cdf0e10cSrcweir 		DBG_ERROR( "Unexpected Output stream parameter!\n" );
2463cdf0e10cSrcweir 	}
2464cdf0e10cSrcweir 
2465cdf0e10cSrcweir     if ( aLogicName.Len() )
2466cdf0e10cSrcweir     {
2467cdf0e10cSrcweir         // if the logic name is set it should be set in MediaDescriptor as well
2468cdf0e10cSrcweir         SFX_ITEMSET_ARG( pSet, pFileNameItem, SfxStringItem, SID_FILE_NAME, sal_False );
2469cdf0e10cSrcweir         if ( !pFileNameItem )
2470cdf0e10cSrcweir         {
2471cdf0e10cSrcweir             // let the ItemSet be created if necessary
2472cdf0e10cSrcweir             GetItemSet()->Put( SfxStringItem( SID_FILE_NAME, INetURLObject( aLogicName ).GetMainURL( INetURLObject::NO_DECODE ) ) );
2473cdf0e10cSrcweir         }
2474cdf0e10cSrcweir     }
2475cdf0e10cSrcweir 
2476cdf0e10cSrcweir     SetIsRemote_Impl();
2477cdf0e10cSrcweir }
2478cdf0e10cSrcweir 
2479cdf0e10cSrcweir //------------------------------------------------------------------
2480cdf0e10cSrcweir SfxMedium::SfxMedium()
2481cdf0e10cSrcweir :   IMPL_CTOR( sal_False, 0 ),	// bRoot, pURLObj
2482cdf0e10cSrcweir 
2483cdf0e10cSrcweir     pFilter(0),
2484cdf0e10cSrcweir     pSet(0),
2485cdf0e10cSrcweir     pImp(new SfxMedium_Impl( this ))
2486cdf0e10cSrcweir {
2487cdf0e10cSrcweir     Init_Impl();
2488cdf0e10cSrcweir }
2489cdf0e10cSrcweir //------------------------------------------------------------------
2490cdf0e10cSrcweir 
2491cdf0e10cSrcweir SfxMedium::SfxMedium( const SfxMedium& rMedium, sal_Bool bTemporary )
2492cdf0e10cSrcweir :   SvRefBase(),
2493cdf0e10cSrcweir     IMPL_CTOR( sal_True,	// bRoot, pURLObj
2494cdf0e10cSrcweir         rMedium.pURLObj ? new INetURLObject(*rMedium.pURLObj) : 0 ),
2495cdf0e10cSrcweir     pImp(new SfxMedium_Impl( this ))
2496cdf0e10cSrcweir {
2497cdf0e10cSrcweir     bDirect       = rMedium.IsDirect();
2498cdf0e10cSrcweir     nStorOpenMode = rMedium.GetOpenMode();
2499cdf0e10cSrcweir     if ( !bTemporary )
2500cdf0e10cSrcweir         aName = rMedium.aName;
2501cdf0e10cSrcweir 
2502cdf0e10cSrcweir     pImp->bIsTemp = bTemporary;
2503cdf0e10cSrcweir     DBG_ASSERT( ! rMedium.pImp->bIsTemp, "Temporaeres Medium darf nicht kopiert werden" );
2504cdf0e10cSrcweir     aLogicName = rMedium.aLogicName;
2505cdf0e10cSrcweir     pSet =  rMedium.GetItemSet() ? new SfxItemSet(*rMedium.GetItemSet()) : 0;
2506cdf0e10cSrcweir     pFilter = rMedium.pFilter;
2507cdf0e10cSrcweir     Init_Impl();
2508cdf0e10cSrcweir     if( bTemporary )
2509cdf0e10cSrcweir         CreateTempFile( sal_True );
2510cdf0e10cSrcweir }
2511cdf0e10cSrcweir 
2512cdf0e10cSrcweir //------------------------------------------------------------------
2513cdf0e10cSrcweir 
2514cdf0e10cSrcweir void SfxMedium::UseInteractionHandler( sal_Bool bUse )
2515cdf0e10cSrcweir {
2516cdf0e10cSrcweir     pImp->bAllowDefaultIntHdl = bUse;
2517cdf0e10cSrcweir }
2518cdf0e10cSrcweir 
2519cdf0e10cSrcweir //------------------------------------------------------------------
2520cdf0e10cSrcweir 
2521cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >
2522cdf0e10cSrcweir SfxMedium::GetInteractionHandler()
2523cdf0e10cSrcweir {
2524cdf0e10cSrcweir     // if interaction isnt allowed explicitly ... return empty reference!
2525cdf0e10cSrcweir     if ( !pImp->bUseInteractionHandler )
2526cdf0e10cSrcweir         return ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >();
2527cdf0e10cSrcweir 
2528cdf0e10cSrcweir     // search a possible existing handler inside cached item set
2529cdf0e10cSrcweir     if ( pSet )
2530cdf0e10cSrcweir     {
2531cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler > xHandler;
2532cdf0e10cSrcweir         SFX_ITEMSET_ARG( pSet, pHandler, SfxUnoAnyItem, SID_INTERACTIONHANDLER, sal_False);
2533cdf0e10cSrcweir         if ( pHandler && (pHandler->GetValue() >>= xHandler) && xHandler.is() )
2534cdf0e10cSrcweir             return xHandler;
2535cdf0e10cSrcweir     }
2536cdf0e10cSrcweir 
2537cdf0e10cSrcweir     // if default interaction isnt allowed explicitly ... return empty reference!
2538cdf0e10cSrcweir     if ( !pImp->bAllowDefaultIntHdl )
2539cdf0e10cSrcweir         return ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >();
2540cdf0e10cSrcweir 
2541cdf0e10cSrcweir     // otherwhise return cached default handler ... if it exist.
2542cdf0e10cSrcweir     if ( pImp->xInteraction.is() )
2543cdf0e10cSrcweir         return pImp->xInteraction;
2544cdf0e10cSrcweir 
2545cdf0e10cSrcweir     // create default handler and cache it!
2546cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
2547cdf0e10cSrcweir     if ( xFactory.is() )
2548cdf0e10cSrcweir     {
2549cdf0e10cSrcweir         pImp->xInteraction = ::com::sun::star::uno::Reference< com::sun::star::task::XInteractionHandler >( xFactory->createInstance( DEFINE_CONST_UNICODE("com.sun.star.task.InteractionHandler") ), ::com::sun::star::uno::UNO_QUERY );
2550cdf0e10cSrcweir         return pImp->xInteraction;
2551cdf0e10cSrcweir     }
2552cdf0e10cSrcweir 
2553cdf0e10cSrcweir 	return ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >();
2554cdf0e10cSrcweir }
2555cdf0e10cSrcweir 
2556cdf0e10cSrcweir //----------------------------------------------------------------
2557cdf0e10cSrcweir 
2558cdf0e10cSrcweir void SfxMedium::SetFilter( const SfxFilter* pFilterP, sal_Bool /*bResetOrig*/ )
2559cdf0e10cSrcweir {
2560cdf0e10cSrcweir     pFilter = pFilterP;
2561cdf0e10cSrcweir     pImp->nFileVersion = 0;
2562cdf0e10cSrcweir }
2563cdf0e10cSrcweir 
2564cdf0e10cSrcweir //----------------------------------------------------------------
2565cdf0e10cSrcweir 
2566cdf0e10cSrcweir const SfxFilter* SfxMedium::GetOrigFilter( sal_Bool bNotCurrent ) const
2567cdf0e10cSrcweir {
2568cdf0e10cSrcweir     return ( pImp->pOrigFilter || bNotCurrent ) ? pImp->pOrigFilter : pFilter;
2569cdf0e10cSrcweir }
2570cdf0e10cSrcweir 
2571cdf0e10cSrcweir //----------------------------------------------------------------
2572cdf0e10cSrcweir 
2573cdf0e10cSrcweir void SfxMedium::SetOrigFilter_Impl( const SfxFilter* pOrigFilter )
2574cdf0e10cSrcweir {
2575cdf0e10cSrcweir     pImp->pOrigFilter = pOrigFilter;
2576cdf0e10cSrcweir }
2577cdf0e10cSrcweir 
2578cdf0e10cSrcweir //------------------------------------------------------------------
2579cdf0e10cSrcweir 
2580cdf0e10cSrcweir sal_uInt32 SfxMedium::CreatePasswordToModifyHash( const ::rtl::OUString& aPasswd, sal_Bool bWriter )
2581cdf0e10cSrcweir {
2582cdf0e10cSrcweir     sal_uInt32 nHash = 0;
2583cdf0e10cSrcweir 
2584cdf0e10cSrcweir     if ( aPasswd.getLength() )
2585cdf0e10cSrcweir     {
2586cdf0e10cSrcweir         if ( bWriter )
2587cdf0e10cSrcweir         {
2588cdf0e10cSrcweir             nHash = ::comphelper::DocPasswordHelper::GetWordHashAsUINT32( aPasswd );
2589cdf0e10cSrcweir         }
2590cdf0e10cSrcweir         else
2591cdf0e10cSrcweir         {
2592cdf0e10cSrcweir             rtl_TextEncoding nEncoding = RTL_TEXTENCODING_UTF8;
2593cdf0e10cSrcweir 
2594cdf0e10cSrcweir             // if the MS-filter should be used
2595cdf0e10cSrcweir             // use the inconsistent algorithm to find the encoding specified by MS
2596cdf0e10cSrcweir             nEncoding = osl_getThreadTextEncoding();
2597cdf0e10cSrcweir             switch( nEncoding )
2598cdf0e10cSrcweir             {
2599cdf0e10cSrcweir                 case RTL_TEXTENCODING_ISO_8859_15:
2600cdf0e10cSrcweir                 case RTL_TEXTENCODING_MS_874:
2601cdf0e10cSrcweir                 case RTL_TEXTENCODING_MS_1250:
2602cdf0e10cSrcweir                 case RTL_TEXTENCODING_MS_1251:
2603cdf0e10cSrcweir                 case RTL_TEXTENCODING_MS_1252:
2604cdf0e10cSrcweir                 case RTL_TEXTENCODING_MS_1253:
2605cdf0e10cSrcweir                 case RTL_TEXTENCODING_MS_1254:
2606cdf0e10cSrcweir                 case RTL_TEXTENCODING_MS_1255:
2607cdf0e10cSrcweir                 case RTL_TEXTENCODING_MS_1256:
2608cdf0e10cSrcweir                 case RTL_TEXTENCODING_MS_1257:
2609cdf0e10cSrcweir                 case RTL_TEXTENCODING_MS_1258:
2610cdf0e10cSrcweir                 case RTL_TEXTENCODING_SHIFT_JIS:
2611cdf0e10cSrcweir                 case RTL_TEXTENCODING_GB_2312:
2612cdf0e10cSrcweir                 case RTL_TEXTENCODING_BIG5:
2613cdf0e10cSrcweir                     // in case the system uses an encoding from the list above, it should be used
2614cdf0e10cSrcweir                     break;
2615cdf0e10cSrcweir 
2616cdf0e10cSrcweir                 default:
2617cdf0e10cSrcweir                     // in case other encoding is used, use one of the encodings from the list
2618cdf0e10cSrcweir                     nEncoding = RTL_TEXTENCODING_MS_1250;
2619cdf0e10cSrcweir                     break;
2620cdf0e10cSrcweir             }
2621cdf0e10cSrcweir 
2622cdf0e10cSrcweir             nHash = ::comphelper::DocPasswordHelper::GetXLHashAsUINT16( aPasswd, nEncoding );
2623cdf0e10cSrcweir         }
2624cdf0e10cSrcweir     }
2625cdf0e10cSrcweir 
2626cdf0e10cSrcweir     return nHash;
2627cdf0e10cSrcweir }
2628cdf0e10cSrcweir 
2629cdf0e10cSrcweir //------------------------------------------------------------------
2630cdf0e10cSrcweir 
2631cdf0e10cSrcweir void SfxMedium::Close()
2632cdf0e10cSrcweir {
2633cdf0e10cSrcweir     if ( pImp->xStorage.is() )
2634cdf0e10cSrcweir     {
2635cdf0e10cSrcweir         // don't close the streams if they belong to the
2636cdf0e10cSrcweir         // storage
2637cdf0e10cSrcweir         //TODO/MBA: how to?! Do we need the flag?!
2638cdf0e10cSrcweir         /*
2639cdf0e10cSrcweir         const SvStream *pStream = aStorage->GetSvStream();
2640cdf0e10cSrcweir         if ( pStream && pStream == pInStream )
2641cdf0e10cSrcweir         {
2642cdf0e10cSrcweir 			CloseZipStorage_Impl();
2643cdf0e10cSrcweir             pInStream = NULL;
2644cdf0e10cSrcweir             pImp->xInputStream = Reference < XInputStream >();
2645cdf0e10cSrcweir             pImp->xLockBytes.Clear();
2646cdf0e10cSrcweir             if ( pSet )
2647cdf0e10cSrcweir                 pSet->ClearItem( SID_INPUTSTREAM );
2648cdf0e10cSrcweir             aStorage->SetDeleteStream( sal_True );
2649cdf0e10cSrcweir         }
2650cdf0e10cSrcweir         else if ( pStream && pStream == pOutStream )
2651cdf0e10cSrcweir         {
2652cdf0e10cSrcweir             pOutStream = NULL;
2653cdf0e10cSrcweir             aStorage->SetDeleteStream( sal_True );
2654cdf0e10cSrcweir         } */
2655cdf0e10cSrcweir 
2656cdf0e10cSrcweir         CloseStorage();
2657cdf0e10cSrcweir     }
2658cdf0e10cSrcweir 
2659cdf0e10cSrcweir 	CloseStreams_Impl();
2660cdf0e10cSrcweir 
2661cdf0e10cSrcweir     UnlockFile( sal_False );
2662cdf0e10cSrcweir }
2663cdf0e10cSrcweir 
2664cdf0e10cSrcweir void SfxMedium::CloseAndRelease()
2665cdf0e10cSrcweir {
2666cdf0e10cSrcweir     if ( pImp->xStorage.is() )
2667cdf0e10cSrcweir     {
2668cdf0e10cSrcweir         // don't close the streams if they belong to the
2669cdf0e10cSrcweir         // storage
2670cdf0e10cSrcweir         //TODO/MBA: how to?! Do we need the flag?!
2671cdf0e10cSrcweir         /*
2672cdf0e10cSrcweir         const SvStream *pStream = aStorage->GetSvStream();
2673cdf0e10cSrcweir         if ( pStream && pStream == pInStream )
2674cdf0e10cSrcweir         {
2675cdf0e10cSrcweir 			CloseZipStorage_Impl();
2676cdf0e10cSrcweir             pInStream = NULL;
2677cdf0e10cSrcweir             pImp->xInputStream = Reference < XInputStream >();
2678cdf0e10cSrcweir             pImp->xLockBytes.Clear();
2679cdf0e10cSrcweir             if ( pSet )
2680cdf0e10cSrcweir                 pSet->ClearItem( SID_INPUTSTREAM );
2681cdf0e10cSrcweir             aStorage->SetDeleteStream( sal_True );
2682cdf0e10cSrcweir         }
2683cdf0e10cSrcweir         else if ( pStream && pStream == pOutStream )
2684cdf0e10cSrcweir         {
2685cdf0e10cSrcweir             pOutStream = NULL;
2686cdf0e10cSrcweir             aStorage->SetDeleteStream( sal_True );
2687cdf0e10cSrcweir         } */
2688cdf0e10cSrcweir 
2689cdf0e10cSrcweir         CloseStorage();
2690cdf0e10cSrcweir     }
2691cdf0e10cSrcweir 
2692cdf0e10cSrcweir 	CloseAndReleaseStreams_Impl();
2693cdf0e10cSrcweir 
2694cdf0e10cSrcweir     UnlockFile( sal_True );
2695cdf0e10cSrcweir }
2696cdf0e10cSrcweir 
2697cdf0e10cSrcweir void SfxMedium::UnlockFile( sal_Bool bReleaseLockStream )
2698cdf0e10cSrcweir {
2699cdf0e10cSrcweir     if ( pImp->m_xLockingStream.is() )
2700cdf0e10cSrcweir     {
2701cdf0e10cSrcweir         if ( bReleaseLockStream )
2702cdf0e10cSrcweir         {
2703cdf0e10cSrcweir             try
2704cdf0e10cSrcweir             {
2705cdf0e10cSrcweir                 uno::Reference< io::XInputStream > xInStream = pImp->m_xLockingStream->getInputStream();
2706cdf0e10cSrcweir                 uno::Reference< io::XOutputStream > xOutStream = pImp->m_xLockingStream->getOutputStream();
2707cdf0e10cSrcweir                 if ( xInStream.is() )
2708cdf0e10cSrcweir                     xInStream->closeInput();
2709cdf0e10cSrcweir                 if ( xOutStream.is() )
2710cdf0e10cSrcweir                     xOutStream->closeOutput();
2711cdf0e10cSrcweir             }
2712cdf0e10cSrcweir             catch( uno::Exception& )
2713cdf0e10cSrcweir             {}
2714cdf0e10cSrcweir         }
2715cdf0e10cSrcweir 
2716cdf0e10cSrcweir         pImp->m_xLockingStream = uno::Reference< io::XStream >();
2717cdf0e10cSrcweir     }
2718cdf0e10cSrcweir 
2719cdf0e10cSrcweir     if ( pImp->m_bLocked )
2720cdf0e10cSrcweir     {
2721cdf0e10cSrcweir         try
2722cdf0e10cSrcweir         {
2723cdf0e10cSrcweir             pImp->m_bLocked = sal_False;
2724cdf0e10cSrcweir             ::svt::DocumentLockFile aLockFile( aLogicName );
2725cdf0e10cSrcweir             // TODO/LATER: A warning could be shown in case the file is not the own one
2726cdf0e10cSrcweir             aLockFile.RemoveFile();
2727cdf0e10cSrcweir         }
2728cdf0e10cSrcweir         catch( uno::Exception& )
2729cdf0e10cSrcweir         {}
2730cdf0e10cSrcweir     }
2731cdf0e10cSrcweir }
2732cdf0e10cSrcweir 
2733cdf0e10cSrcweir void SfxMedium::CloseAndReleaseStreams_Impl()
2734cdf0e10cSrcweir {
2735cdf0e10cSrcweir 	CloseZipStorage_Impl();
2736cdf0e10cSrcweir 
2737cdf0e10cSrcweir 	uno::Reference< io::XInputStream > xInToClose = pImp->xInputStream;
2738cdf0e10cSrcweir 	uno::Reference< io::XOutputStream > xOutToClose;
2739cdf0e10cSrcweir 	if ( pImp->xStream.is() )
2740cdf0e10cSrcweir     {
2741cdf0e10cSrcweir 		xOutToClose = pImp->xStream->getOutputStream();
2742cdf0e10cSrcweir 
2743cdf0e10cSrcweir         // if the locking stream is closed here the related member should be cleaned
2744cdf0e10cSrcweir         if ( pImp->xStream == pImp->m_xLockingStream )
2745cdf0e10cSrcweir             pImp->m_xLockingStream = uno::Reference< io::XStream >();
2746cdf0e10cSrcweir     }
2747cdf0e10cSrcweir 
2748cdf0e10cSrcweir 	// The probably exsisting SvStream wrappers should be closed first
2749cdf0e10cSrcweir     CloseStreams_Impl();
2750cdf0e10cSrcweir 
2751cdf0e10cSrcweir     // in case of salvage mode the storage is based on the streams
2752cdf0e10cSrcweir     if ( !pImp->m_bSalvageMode )
2753cdf0e10cSrcweir     {
2754cdf0e10cSrcweir         try
2755cdf0e10cSrcweir         {
2756cdf0e10cSrcweir             if ( xInToClose.is() )
2757cdf0e10cSrcweir                 xInToClose->closeInput();
2758cdf0e10cSrcweir             if ( xOutToClose.is() )
2759cdf0e10cSrcweir                 xOutToClose->closeOutput();
2760cdf0e10cSrcweir         }
2761cdf0e10cSrcweir         catch ( uno::Exception& )
2762cdf0e10cSrcweir         {
2763cdf0e10cSrcweir         }
2764cdf0e10cSrcweir     }
2765cdf0e10cSrcweir }
2766cdf0e10cSrcweir 
2767cdf0e10cSrcweir //------------------------------------------------------------------
2768cdf0e10cSrcweir void SfxMedium::CloseStreams_Impl()
2769cdf0e10cSrcweir {
2770cdf0e10cSrcweir 	CloseInStream_Impl();
2771cdf0e10cSrcweir 	CloseOutStream_Impl();
2772cdf0e10cSrcweir 
2773cdf0e10cSrcweir     if ( pSet )
2774cdf0e10cSrcweir         pSet->ClearItem( SID_CONTENT );
2775cdf0e10cSrcweir 
2776cdf0e10cSrcweir     pImp->aContent = ::ucbhelper::Content();
2777cdf0e10cSrcweir }
2778cdf0e10cSrcweir 
2779cdf0e10cSrcweir //------------------------------------------------------------------
2780cdf0e10cSrcweir 
2781cdf0e10cSrcweir void SfxMedium::RefreshName_Impl()
2782cdf0e10cSrcweir {
2783cdf0e10cSrcweir #if 0   //(dv)
2784cdf0e10cSrcweir     if ( pImp->aContent.get().is() )
2785cdf0e10cSrcweir     {
2786cdf0e10cSrcweir         String aNameP = pImp->xAnchor->GetViewURL();
2787cdf0e10cSrcweir         pImp->aOrigURL = aNameP;
2788cdf0e10cSrcweir         aLogicName = aNameP;
2789cdf0e10cSrcweir         DELETEZ( pURLObj );
2790cdf0e10cSrcweir         if (aLogicName.Len())
2791cdf0e10cSrcweir             aLogicName = GetURLObject().GetMainURL( INetURLObject::NO_DECODE );
2792cdf0e10cSrcweir         SetIsRemote_Impl();
2793cdf0e10cSrcweir     }
2794cdf0e10cSrcweir #endif  //(dv)
2795cdf0e10cSrcweir }
2796cdf0e10cSrcweir 
2797cdf0e10cSrcweir void SfxMedium::SetIsRemote_Impl()
2798cdf0e10cSrcweir {
2799cdf0e10cSrcweir     INetURLObject aObj( GetName() );
2800cdf0e10cSrcweir     switch( aObj.GetProtocol() )
2801cdf0e10cSrcweir     {
2802cdf0e10cSrcweir         case INET_PROT_FTP:
2803cdf0e10cSrcweir         case INET_PROT_HTTP:
2804cdf0e10cSrcweir         case INET_PROT_HTTPS:
2805cdf0e10cSrcweir         case INET_PROT_POP3:
2806cdf0e10cSrcweir         case INET_PROT_NEWS:
2807cdf0e10cSrcweir         case INET_PROT_IMAP:
2808cdf0e10cSrcweir //        case INET_PROT_OUT:
2809cdf0e10cSrcweir         case INET_PROT_VIM:
2810cdf0e10cSrcweir             bRemote = sal_True; break;
2811cdf0e10cSrcweir         default:
2812cdf0e10cSrcweir             bRemote = ( GetName().CompareToAscii( "private:msgid", 13 ) == COMPARE_EQUAL );
2813cdf0e10cSrcweir             break;
2814cdf0e10cSrcweir     }
2815cdf0e10cSrcweir 
2816cdf0e10cSrcweir     // Da Dateien, die Remote geschrieben werden zur Uebertragung auch
2817cdf0e10cSrcweir     // gelesen werden koennen muessen
2818cdf0e10cSrcweir     if( bRemote )
2819cdf0e10cSrcweir         nStorOpenMode |= STREAM_READ;
2820cdf0e10cSrcweir }
2821cdf0e10cSrcweir 
2822cdf0e10cSrcweir 
2823cdf0e10cSrcweir 
2824cdf0e10cSrcweir void SfxMedium::SetName( const String& aNameP, sal_Bool bSetOrigURL )
2825cdf0e10cSrcweir {
2826cdf0e10cSrcweir     if( !pImp->aOrigURL.Len() )
2827cdf0e10cSrcweir         pImp->aOrigURL = aLogicName;
2828cdf0e10cSrcweir     if( bSetOrigURL )
2829cdf0e10cSrcweir         pImp->aOrigURL = aNameP;
2830cdf0e10cSrcweir     aLogicName = aNameP;
2831cdf0e10cSrcweir     DELETEZ( pURLObj );
2832cdf0e10cSrcweir     pImp->aContent = ::ucbhelper::Content();
2833cdf0e10cSrcweir     Init_Impl();
2834cdf0e10cSrcweir }
2835cdf0e10cSrcweir 
2836cdf0e10cSrcweir //----------------------------------------------------------------
2837cdf0e10cSrcweir const String& SfxMedium::GetOrigURL() const
2838cdf0e10cSrcweir {
2839cdf0e10cSrcweir     return !pImp->aOrigURL.Len() ? (String &)aLogicName : pImp->aOrigURL;
2840cdf0e10cSrcweir }
2841cdf0e10cSrcweir 
2842cdf0e10cSrcweir //----------------------------------------------------------------
2843cdf0e10cSrcweir 
2844cdf0e10cSrcweir void SfxMedium::SetPhysicalName_Impl( const String& rNameP )
2845cdf0e10cSrcweir {
2846cdf0e10cSrcweir     if ( rNameP != aName )
2847cdf0e10cSrcweir     {
2848cdf0e10cSrcweir         if( pImp->pTempFile )
2849cdf0e10cSrcweir         {
2850cdf0e10cSrcweir             delete pImp->pTempFile;
2851cdf0e10cSrcweir             pImp->pTempFile = NULL;
2852cdf0e10cSrcweir         }
2853cdf0e10cSrcweir 
2854cdf0e10cSrcweir         if ( aName.Len() || rNameP.Len() )
2855cdf0e10cSrcweir             pImp->aContent = ::ucbhelper::Content();
2856cdf0e10cSrcweir 
2857cdf0e10cSrcweir         aName = rNameP;
2858cdf0e10cSrcweir         bTriedStorage = sal_False;
2859cdf0e10cSrcweir         pImp->bIsStorage = sal_False;
2860cdf0e10cSrcweir     }
2861cdf0e10cSrcweir }
2862cdf0e10cSrcweir 
2863cdf0e10cSrcweir //------------------------------------------------------------------
2864cdf0e10cSrcweir void SfxMedium::SetTemporary( sal_Bool bTemp )
2865cdf0e10cSrcweir {
2866cdf0e10cSrcweir     pImp->bIsTemp = bTemp;
2867cdf0e10cSrcweir }
2868cdf0e10cSrcweir 
2869cdf0e10cSrcweir //------------------------------------------------------------------
2870cdf0e10cSrcweir sal_Bool SfxMedium::IsTemporary() const
2871cdf0e10cSrcweir {
2872cdf0e10cSrcweir     return pImp->bIsTemp;
2873cdf0e10cSrcweir }
2874cdf0e10cSrcweir 
2875cdf0e10cSrcweir //------------------------------------------------------------------
2876cdf0e10cSrcweir 
2877cdf0e10cSrcweir sal_Bool SfxMedium::Exists( sal_Bool /*bForceSession*/ )
2878cdf0e10cSrcweir {
2879cdf0e10cSrcweir     DBG_ERROR( "Not implemented!" );
2880cdf0e10cSrcweir     return sal_True;
2881cdf0e10cSrcweir }
2882cdf0e10cSrcweir 
2883cdf0e10cSrcweir //------------------------------------------------------------------
2884cdf0e10cSrcweir 
2885cdf0e10cSrcweir void SfxMedium::ReOpen()
2886cdf0e10cSrcweir {
2887cdf0e10cSrcweir     sal_Bool bUseInteractionHandler = pImp->bUseInteractionHandler;
2888cdf0e10cSrcweir     pImp->bUseInteractionHandler = sal_False;
2889cdf0e10cSrcweir     GetMedium_Impl();
2890cdf0e10cSrcweir     pImp->bUseInteractionHandler = bUseInteractionHandler;
2891cdf0e10cSrcweir }
2892cdf0e10cSrcweir 
2893cdf0e10cSrcweir //------------------------------------------------------------------
2894cdf0e10cSrcweir 
2895cdf0e10cSrcweir void SfxMedium::CompleteReOpen()
2896cdf0e10cSrcweir {
2897cdf0e10cSrcweir 	// do not use temporary file for reopen and in case of success throw the temporary file away
2898cdf0e10cSrcweir     sal_Bool bUseInteractionHandler = pImp->bUseInteractionHandler;
2899cdf0e10cSrcweir     pImp->bUseInteractionHandler = sal_False;
2900cdf0e10cSrcweir 
2901cdf0e10cSrcweir     ::utl::TempFile* pTmpFile = NULL;
2902cdf0e10cSrcweir 	if ( pImp->pTempFile )
2903cdf0e10cSrcweir 	{
2904cdf0e10cSrcweir 		pTmpFile = pImp->pTempFile;
2905cdf0e10cSrcweir 		pImp->pTempFile = NULL;
2906cdf0e10cSrcweir 		aName = String();
2907cdf0e10cSrcweir 	}
2908cdf0e10cSrcweir 
2909cdf0e10cSrcweir     GetMedium_Impl();
2910cdf0e10cSrcweir 
2911cdf0e10cSrcweir 	if ( GetError() )
2912cdf0e10cSrcweir 	{
2913cdf0e10cSrcweir 		if ( pImp->pTempFile )
2914cdf0e10cSrcweir 		{
2915cdf0e10cSrcweir     		pImp->pTempFile->EnableKillingFile( sal_True );
2916cdf0e10cSrcweir 			delete pImp->pTempFile;
2917cdf0e10cSrcweir 		}
2918cdf0e10cSrcweir 		pImp->pTempFile = pTmpFile;
2919cdf0e10cSrcweir         if ( pImp->pTempFile )
2920cdf0e10cSrcweir             aName = pImp->pTempFile->GetFileName();
2921cdf0e10cSrcweir 	}
2922cdf0e10cSrcweir 	else
2923cdf0e10cSrcweir 	{
2924cdf0e10cSrcweir 		pTmpFile->EnableKillingFile( sal_True );
2925cdf0e10cSrcweir 		delete pTmpFile;
2926cdf0e10cSrcweir 
2927cdf0e10cSrcweir 	}
2928cdf0e10cSrcweir 
2929cdf0e10cSrcweir     pImp->bUseInteractionHandler = bUseInteractionHandler;
2930cdf0e10cSrcweir }
2931cdf0e10cSrcweir 
2932cdf0e10cSrcweir //------------------------------------------------------------------
2933cdf0e10cSrcweir SfxMedium::SfxMedium
2934cdf0e10cSrcweir (
2935cdf0e10cSrcweir     const String &rName, StreamMode nOpenMode,  sal_Bool bDirectP,
2936cdf0e10cSrcweir     const SfxFilter *pFlt, SfxItemSet *pInSet
2937cdf0e10cSrcweir )
2938cdf0e10cSrcweir :   IMPL_CTOR( sal_False, 0 ),	// bRoot, pURLObj
2939cdf0e10cSrcweir     pFilter(pFlt),
2940cdf0e10cSrcweir     pSet( pInSet ),
2941cdf0e10cSrcweir     pImp(new SfxMedium_Impl( this ))
2942cdf0e10cSrcweir {
2943cdf0e10cSrcweir     aLogicName = rName;
2944cdf0e10cSrcweir     nStorOpenMode = nOpenMode;
2945cdf0e10cSrcweir     bDirect = bDirectP;
2946cdf0e10cSrcweir     Init_Impl();
2947cdf0e10cSrcweir }
2948cdf0e10cSrcweir 
2949cdf0e10cSrcweir 
2950cdf0e10cSrcweir SfxMedium::SfxMedium( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& aArgs )
2951cdf0e10cSrcweir     : IMPL_CTOR( sal_False, 0 ),  // bRoot, pURLObj
2952cdf0e10cSrcweir     pFilter(0),
2953cdf0e10cSrcweir     pSet(0),
2954cdf0e10cSrcweir     pImp(new SfxMedium_Impl( this ))
2955cdf0e10cSrcweir {
2956cdf0e10cSrcweir     SfxAllItemSet *pParams = new SfxAllItemSet( SFX_APP()->GetPool() );
2957cdf0e10cSrcweir     pSet = pParams;
2958cdf0e10cSrcweir     TransformParameters( SID_OPENDOC, aArgs, *pParams );
2959cdf0e10cSrcweir 
2960cdf0e10cSrcweir     String aFilterName;
2961cdf0e10cSrcweir     SFX_ITEMSET_ARG( pSet, pFilterNameItem, SfxStringItem, SID_FILTER_NAME, sal_False );
2962cdf0e10cSrcweir     if( pFilterNameItem )
2963cdf0e10cSrcweir         aFilterName = pFilterNameItem->GetValue();
2964cdf0e10cSrcweir     pFilter = SFX_APP()->GetFilterMatcher().GetFilter4FilterName( aFilterName );
2965cdf0e10cSrcweir 
2966cdf0e10cSrcweir     sal_Bool bSalvage = sal_False;
2967cdf0e10cSrcweir     SFX_ITEMSET_ARG( pSet, pSalvageItem, SfxStringItem, SID_DOC_SALVAGE, sal_False );
2968cdf0e10cSrcweir     if( pSalvageItem )
2969cdf0e10cSrcweir     {
2970cdf0e10cSrcweir         // QUESTION: there is some treatment of Salvage in Init_Impl; align!
2971cdf0e10cSrcweir         bSalvage = sal_True;
2972cdf0e10cSrcweir         if ( pSalvageItem->GetValue().Len() )
2973cdf0e10cSrcweir         {
2974cdf0e10cSrcweir             // if an URL is provided in SalvageItem that means that the FileName refers to a temporary file
2975cdf0e10cSrcweir             // that must be copied here
2976cdf0e10cSrcweir 
2977cdf0e10cSrcweir             SFX_ITEMSET_ARG( pSet, pFileNameItem, SfxStringItem, SID_FILE_NAME, sal_False );
2978cdf0e10cSrcweir             if (!pFileNameItem) throw uno::RuntimeException();
2979cdf0e10cSrcweir             ::rtl::OUString aNewTempFileURL = SfxMedium::CreateTempCopyWithExt( pFileNameItem->GetValue() );
2980cdf0e10cSrcweir             if ( aNewTempFileURL.getLength() )
2981cdf0e10cSrcweir             {
2982cdf0e10cSrcweir                 pSet->Put( SfxStringItem( SID_FILE_NAME, aNewTempFileURL ) );
2983cdf0e10cSrcweir                 pSet->ClearItem( SID_INPUTSTREAM );
2984cdf0e10cSrcweir                 pSet->ClearItem( SID_STREAM );
2985cdf0e10cSrcweir                 pSet->ClearItem( SID_CONTENT );
2986cdf0e10cSrcweir             }
2987cdf0e10cSrcweir             else
2988cdf0e10cSrcweir             {
2989cdf0e10cSrcweir                 OSL_ENSURE( sal_False, "Can not create a new temporary file for crash recovery!\n" );
2990cdf0e10cSrcweir             }
2991cdf0e10cSrcweir         }
2992cdf0e10cSrcweir     }
2993cdf0e10cSrcweir 
2994cdf0e10cSrcweir     sal_Bool bReadOnly = sal_False;
2995cdf0e10cSrcweir     SFX_ITEMSET_ARG( pSet, pReadOnlyItem, SfxBoolItem, SID_DOC_READONLY, sal_False );
2996cdf0e10cSrcweir     if ( pReadOnlyItem && pReadOnlyItem->GetValue() )
2997cdf0e10cSrcweir         bReadOnly = sal_True;
2998cdf0e10cSrcweir 
2999cdf0e10cSrcweir     SFX_ITEMSET_ARG( pSet, pFileNameItem, SfxStringItem, SID_FILE_NAME, sal_False );
3000cdf0e10cSrcweir     if (!pFileNameItem) throw uno::RuntimeException();
3001cdf0e10cSrcweir     aLogicName = pFileNameItem->GetValue();
3002cdf0e10cSrcweir     nStorOpenMode = bReadOnly ? SFX_STREAM_READONLY : SFX_STREAM_READWRITE;
3003cdf0e10cSrcweir     bDirect = sal_False;
3004cdf0e10cSrcweir     Init_Impl();
3005cdf0e10cSrcweir }
3006cdf0e10cSrcweir 
3007cdf0e10cSrcweir 
3008cdf0e10cSrcweir //------------------------------------------------------------------
3009cdf0e10cSrcweir 
3010cdf0e10cSrcweir SfxMedium::SfxMedium( const uno::Reference < embed::XStorage >& rStor, const String& rBaseURL, const SfxItemSet* p, sal_Bool bRootP )
3011cdf0e10cSrcweir :   IMPL_CTOR( bRootP, 0 ),	// bRoot, pURLObj
3012cdf0e10cSrcweir     pSet(0),
3013cdf0e10cSrcweir     pImp( new SfxMedium_Impl( this ))
3014cdf0e10cSrcweir {
3015cdf0e10cSrcweir     String aType = SfxFilter::GetTypeFromStorage( rStor );
3016cdf0e10cSrcweir 	pFilter = SFX_APP()->GetFilterMatcher().GetFilter4EA( aType );
3017cdf0e10cSrcweir     DBG_ASSERT( pFilter, "No Filter for storage found!" );
3018cdf0e10cSrcweir 
3019cdf0e10cSrcweir     Init_Impl();
3020cdf0e10cSrcweir     pImp->xStorage = rStor;
3021cdf0e10cSrcweir     pImp->bDisposeStorage = sal_False;
3022cdf0e10cSrcweir 
3023cdf0e10cSrcweir     // always take BaseURL first, could be overwritten by ItemSet
3024cdf0e10cSrcweir     GetItemSet()->Put( SfxStringItem( SID_DOC_BASEURL, rBaseURL ) );
3025cdf0e10cSrcweir     if ( p )
3026cdf0e10cSrcweir         GetItemSet()->Put( *p );
3027cdf0e10cSrcweir }
3028cdf0e10cSrcweir 
3029b45043c5SMathias Bauer SfxMedium::SfxMedium( const uno::Reference < embed::XStorage >& rStor, const String& rBaseURL, const String& rTypeName, const SfxItemSet* p, sal_Bool bRootP )
3030b45043c5SMathias Bauer :   IMPL_CTOR( bRootP, 0 ),	// bRoot, pURLObj
3031b45043c5SMathias Bauer     pSet(0),
3032b45043c5SMathias Bauer     pImp( new SfxMedium_Impl( this ))
3033b45043c5SMathias Bauer {
3034b45043c5SMathias Bauer 	pFilter = SFX_APP()->GetFilterMatcher().GetFilter4EA( rTypeName );
3035b45043c5SMathias Bauer     DBG_ASSERT( pFilter, "No Filter for storage found!" );
3036b45043c5SMathias Bauer 
3037b45043c5SMathias Bauer     Init_Impl();
3038b45043c5SMathias Bauer     pImp->xStorage = rStor;
3039b45043c5SMathias Bauer     pImp->bDisposeStorage = sal_False;
3040b45043c5SMathias Bauer 
3041b45043c5SMathias Bauer     // always take BaseURL first, could be overwritten by ItemSet
3042b45043c5SMathias Bauer     GetItemSet()->Put( SfxStringItem( SID_DOC_BASEURL, rBaseURL ) );
3043b45043c5SMathias Bauer     if ( p )
3044b45043c5SMathias Bauer         GetItemSet()->Put( *p );
3045b45043c5SMathias Bauer }
3046b45043c5SMathias Bauer 
3047cdf0e10cSrcweir //------------------------------------------------------------------
3048cdf0e10cSrcweir 
3049cdf0e10cSrcweir SfxMedium::~SfxMedium()
3050cdf0e10cSrcweir {
3051cdf0e10cSrcweir     /* Attention
3052cdf0e10cSrcweir         Don't enable CancelTransfers() till you know that the writer/web has changed his asynchronous load
3053cdf0e10cSrcweir         behaviour. Otherwhise may StyleSheets inside a html file will be loaded at the right time.
3054cdf0e10cSrcweir         => further the help will be empty then ... #100490#
3055cdf0e10cSrcweir      */
3056cdf0e10cSrcweir     //CancelTransfers();
3057cdf0e10cSrcweir 
3058cdf0e10cSrcweir 	// if there is a requirement to clean the backup this is the last possibility to do it
3059cdf0e10cSrcweir 	ClearBackup_Impl();
3060cdf0e10cSrcweir 
3061cdf0e10cSrcweir     Close();
3062cdf0e10cSrcweir 
3063cdf0e10cSrcweir     delete pSet;
3064cdf0e10cSrcweir 
3065cdf0e10cSrcweir     if( pImp->bIsTemp && aName.Len() )
3066cdf0e10cSrcweir     {
3067cdf0e10cSrcweir         String aTemp;
3068cdf0e10cSrcweir         if ( !::utl::LocalFileHelper::ConvertPhysicalNameToURL( aName, aTemp ))
3069cdf0e10cSrcweir         {
3070cdf0e10cSrcweir 			DBG_ERROR("Physical name not convertable!");
3071cdf0e10cSrcweir         }
3072cdf0e10cSrcweir 
3073cdf0e10cSrcweir         if ( !::utl::UCBContentHelper::Kill( aTemp ) )
3074cdf0e10cSrcweir         {
3075cdf0e10cSrcweir 			DBG_ERROR("Couldn't remove temporary file!");
3076cdf0e10cSrcweir         }
3077cdf0e10cSrcweir     }
3078cdf0e10cSrcweir 
3079cdf0e10cSrcweir     pFilter = 0;
3080cdf0e10cSrcweir 
3081cdf0e10cSrcweir     delete pURLObj;
3082cdf0e10cSrcweir     delete pImp;
3083cdf0e10cSrcweir }
3084cdf0e10cSrcweir 
3085cdf0e10cSrcweir //------------------------------------------------------------------
3086cdf0e10cSrcweir void SfxMedium::SetItemSet(SfxItemSet *pNewSet)
3087cdf0e10cSrcweir {
3088cdf0e10cSrcweir     delete pSet;
3089cdf0e10cSrcweir     pSet = pNewSet;
3090cdf0e10cSrcweir }
3091cdf0e10cSrcweir 
3092cdf0e10cSrcweir //----------------------------------------------------------------
3093cdf0e10cSrcweir const INetURLObject& SfxMedium::GetURLObject() const
3094cdf0e10cSrcweir {
3095cdf0e10cSrcweir     if( !pURLObj )
3096cdf0e10cSrcweir     {
3097cdf0e10cSrcweir         SfxMedium* pThis = const_cast < SfxMedium* > (this);
3098cdf0e10cSrcweir         pThis->pURLObj = new INetURLObject( aLogicName );
3099cdf0e10cSrcweir 		if ( pThis->pURLObj->HasMark() )
3100cdf0e10cSrcweir 			(*pThis->pURLObj) = INetURLObject( aLogicName ).GetURLNoMark();
3101cdf0e10cSrcweir     }
3102cdf0e10cSrcweir 
3103cdf0e10cSrcweir     return *pURLObj;
3104cdf0e10cSrcweir }
3105cdf0e10cSrcweir 
3106cdf0e10cSrcweir //----------------------------------------------------------------
3107cdf0e10cSrcweir 
3108cdf0e10cSrcweir const String& SfxMedium::GetPreRedirectedURL() const
3109cdf0e10cSrcweir {
3110cdf0e10cSrcweir     return pImp->aPreRedirectionURL;
3111cdf0e10cSrcweir }
3112cdf0e10cSrcweir //----------------------------------------------------------------
3113cdf0e10cSrcweir 
3114cdf0e10cSrcweir sal_uInt32 SfxMedium::GetMIMEAndRedirect( String& /*rName*/ )
3115cdf0e10cSrcweir {
3116cdf0e10cSrcweir /* dv !!!! not needed any longer ?
3117cdf0e10cSrcweir     INetProtocol eProt = GetURLObject().GetProtocol();
3118cdf0e10cSrcweir     if( eProt == INET_PROT_FTP && SvBinding::ShouldUseFtpProxy( GetURLObject().GetMainURL( INetURLObject::NO_DECODE ) ) )
3119cdf0e10cSrcweir     {
3120cdf0e10cSrcweir         Any aAny( UCB_Helper::GetProperty( GetContent(), WID_FLAG_IS_FOLDER ) );
3121cdf0e10cSrcweir         sal_Bool bIsFolder = sal_False;
3122cdf0e10cSrcweir         if ( ( aAny >>= bIsFolder ) && bIsFolder )
3123cdf0e10cSrcweir             return ERRCODE_NONE;
3124cdf0e10cSrcweir     }
3125cdf0e10cSrcweir 
3126cdf0e10cSrcweir     GetMedium_Impl();
3127cdf0e10cSrcweir     if( !eError && pImp->xBinding.Is() )
3128cdf0e10cSrcweir     {
3129cdf0e10cSrcweir         eError = pImp->xBinding->GetMimeType( rName );
3130cdf0e10cSrcweir 
3131cdf0e10cSrcweir         // Wir koennen keine Parameter wie CharSets usw.
3132cdf0e10cSrcweir         rName = rName.GetToken( 0, ';' );
3133cdf0e10cSrcweir         if( !eError )
3134cdf0e10cSrcweir         {
3135cdf0e10cSrcweir             if( !pImp->aPreRedirectionURL.Len() )
3136cdf0e10cSrcweir                 pImp->aPreRedirectionURL = aLogicName;
3137cdf0e10cSrcweir             SetName( pImp->xBinding->GetRedirectedURL() );
3138cdf0e10cSrcweir         }
3139cdf0e10cSrcweir         pImp->aExpireTime = pImp->xBinding->GetExpireDateTime();
3140cdf0e10cSrcweir     }
3141cdf0e10cSrcweir     return eError;
3142cdf0e10cSrcweir */
3143cdf0e10cSrcweir     return 0;
3144cdf0e10cSrcweir }
3145cdf0e10cSrcweir 
3146cdf0e10cSrcweir //----------------------------------------------------------------
3147cdf0e10cSrcweir 
3148cdf0e10cSrcweir void SfxMedium::SetReferer( const String& rRefer )
3149cdf0e10cSrcweir {
3150cdf0e10cSrcweir     pImp->aReferer = rRefer;
3151cdf0e10cSrcweir }
3152cdf0e10cSrcweir //----------------------------------------------------------------
3153cdf0e10cSrcweir 
3154cdf0e10cSrcweir const String& SfxMedium::GetReferer( ) const
3155cdf0e10cSrcweir {
3156cdf0e10cSrcweir     return pImp->aReferer;
3157cdf0e10cSrcweir }
3158cdf0e10cSrcweir 
3159cdf0e10cSrcweir //----------------------------------------------------------------
3160cdf0e10cSrcweir 
3161cdf0e10cSrcweir void SfxMedium::SetExpired_Impl( const DateTime& rDateTime )
3162cdf0e10cSrcweir {
3163cdf0e10cSrcweir     pImp->aExpireTime = rDateTime;
3164cdf0e10cSrcweir }
3165cdf0e10cSrcweir //----------------------------------------------------------------
3166cdf0e10cSrcweir 
3167cdf0e10cSrcweir sal_Bool SfxMedium::IsExpired() const
3168cdf0e10cSrcweir {
3169cdf0e10cSrcweir     return pImp->aExpireTime.IsValid() && pImp->aExpireTime < DateTime();
3170cdf0e10cSrcweir }
3171cdf0e10cSrcweir //----------------------------------------------------------------
3172cdf0e10cSrcweir 
3173cdf0e10cSrcweir void SfxMedium::ForceSynchronStream_Impl( sal_Bool bForce )
3174cdf0e10cSrcweir {
3175cdf0e10cSrcweir     if( pInStream )
3176cdf0e10cSrcweir     {
3177cdf0e10cSrcweir         SvLockBytes* pBytes = pInStream->GetLockBytes();
3178cdf0e10cSrcweir         if( pBytes )
3179cdf0e10cSrcweir             pBytes->SetSynchronMode( bForce );
3180cdf0e10cSrcweir     }
3181cdf0e10cSrcweir     pImp->bForceSynchron = bForce;
3182cdf0e10cSrcweir }
3183cdf0e10cSrcweir 
3184cdf0e10cSrcweir //----------------------------------------------------------------
3185cdf0e10cSrcweir SfxFrame* SfxMedium::GetLoadTargetFrame() const
3186cdf0e10cSrcweir {
3187cdf0e10cSrcweir     return pImp->wLoadTargetFrame;
3188cdf0e10cSrcweir }
3189cdf0e10cSrcweir //----------------------------------------------------------------
3190cdf0e10cSrcweir 
3191cdf0e10cSrcweir void SfxMedium::SetLoadTargetFrame(SfxFrame* pFrame )
3192cdf0e10cSrcweir {
3193cdf0e10cSrcweir     pImp->wLoadTargetFrame = pFrame;
3194cdf0e10cSrcweir }
3195cdf0e10cSrcweir //----------------------------------------------------------------
3196cdf0e10cSrcweir 
3197cdf0e10cSrcweir void SfxMedium::SetStorage_Impl( const uno::Reference < embed::XStorage >& rStor )
3198cdf0e10cSrcweir {
3199cdf0e10cSrcweir     pImp->xStorage = rStor;
3200cdf0e10cSrcweir }
3201cdf0e10cSrcweir //----------------------------------------------------------------
3202cdf0e10cSrcweir 
3203cdf0e10cSrcweir SfxItemSet* SfxMedium::GetItemSet() const
3204cdf0e10cSrcweir {
3205cdf0e10cSrcweir 	// this method *must* return an ItemSet, returning NULL can cause crashes
3206cdf0e10cSrcweir     if( !pSet )
3207cdf0e10cSrcweir 		((SfxMedium*)this)->pSet = new SfxAllItemSet( SFX_APP()->GetPool() );
3208cdf0e10cSrcweir     return pSet;
3209cdf0e10cSrcweir }
3210cdf0e10cSrcweir //----------------------------------------------------------------
3211cdf0e10cSrcweir 
3212cdf0e10cSrcweir SvKeyValueIterator* SfxMedium::GetHeaderAttributes_Impl()
3213cdf0e10cSrcweir {
3214cdf0e10cSrcweir     if( !pImp->xAttributes.Is() )
3215cdf0e10cSrcweir 	{
3216cdf0e10cSrcweir 		pImp->xAttributes = SvKeyValueIteratorRef( new SvKeyValueIterator );
3217cdf0e10cSrcweir 
3218cdf0e10cSrcweir 		if ( GetContent().is() )
3219cdf0e10cSrcweir 		{
3220cdf0e10cSrcweir 			pImp->bIsCharsetInitialized = sal_True;
3221cdf0e10cSrcweir 
3222cdf0e10cSrcweir 			try
3223cdf0e10cSrcweir 			{
3224cdf0e10cSrcweir 				Any aAny = pImp->aContent.getPropertyValue( ::rtl::OUString::createFromAscii( "MediaType" ) );
3225cdf0e10cSrcweir 				::rtl::OUString aContentType;
3226cdf0e10cSrcweir 				aAny >>= aContentType;
3227cdf0e10cSrcweir 
3228cdf0e10cSrcweir 				pImp->xAttributes->Append( SvKeyValue( ::rtl::OUString::createFromAscii( "content-type" ), aContentType ) );
3229cdf0e10cSrcweir 			}
3230cdf0e10cSrcweir 			catch ( ::com::sun::star::uno::Exception& )
3231cdf0e10cSrcweir 			{
3232cdf0e10cSrcweir 			}
3233cdf0e10cSrcweir 		}
3234cdf0e10cSrcweir 	}
3235cdf0e10cSrcweir 
3236cdf0e10cSrcweir     return pImp->xAttributes;
3237cdf0e10cSrcweir }
3238cdf0e10cSrcweir //----------------------------------------------------------------
3239cdf0e10cSrcweir 
3240cdf0e10cSrcweir SvCompatWeakHdl* SfxMedium::GetHdl()
3241cdf0e10cSrcweir {
3242cdf0e10cSrcweir     return pImp->GetHdl();
3243cdf0e10cSrcweir }
3244cdf0e10cSrcweir 
3245cdf0e10cSrcweir sal_Bool SfxMedium::IsDownloadDone_Impl()
3246cdf0e10cSrcweir {
3247cdf0e10cSrcweir     return pImp->bDownloadDone;
3248cdf0e10cSrcweir }
3249cdf0e10cSrcweir 
3250cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >  SfxMedium::GetInputStream()
3251cdf0e10cSrcweir {
3252cdf0e10cSrcweir 	if ( !pImp->xInputStream.is() )
3253cdf0e10cSrcweir 		GetMedium_Impl();
3254cdf0e10cSrcweir     return pImp->xInputStream;
3255cdf0e10cSrcweir }
3256cdf0e10cSrcweir 
3257cdf0e10cSrcweir const uno::Sequence < util::RevisionTag >& SfxMedium::GetVersionList( bool _bNoReload )
3258cdf0e10cSrcweir {
3259cdf0e10cSrcweir 	// if the medium has no name, then this medium should represent a new document and can have no version info
3260cdf0e10cSrcweir     if ( ( !_bNoReload || !pImp->m_bVersionsAlreadyLoaded ) && !pImp->aVersions.getLength() &&
3261cdf0e10cSrcweir          ( aName.Len() || aLogicName.Len() ) && GetStorage().is() )
3262cdf0e10cSrcweir     {
3263cdf0e10cSrcweir         uno::Reference < document::XDocumentRevisionListPersistence > xReader( comphelper::getProcessServiceFactory()->createInstance(
3264cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii("com.sun.star.document.DocumentRevisionListPersistence") ), uno::UNO_QUERY );
3265cdf0e10cSrcweir         if ( xReader.is() )
3266cdf0e10cSrcweir         {
3267cdf0e10cSrcweir             try
3268cdf0e10cSrcweir             {
3269cdf0e10cSrcweir                 pImp->aVersions = xReader->load( GetStorage() );
3270cdf0e10cSrcweir             }
3271cdf0e10cSrcweir             catch ( uno::Exception& )
3272cdf0e10cSrcweir             {
3273cdf0e10cSrcweir             }
3274cdf0e10cSrcweir         }
3275cdf0e10cSrcweir     }
3276cdf0e10cSrcweir 
3277cdf0e10cSrcweir     if ( !pImp->m_bVersionsAlreadyLoaded )
3278cdf0e10cSrcweir         pImp->m_bVersionsAlreadyLoaded = sal_True;
3279cdf0e10cSrcweir 
3280cdf0e10cSrcweir     return pImp->aVersions;
3281cdf0e10cSrcweir }
3282cdf0e10cSrcweir 
3283cdf0e10cSrcweir uno::Sequence < util::RevisionTag > SfxMedium::GetVersionList( const uno::Reference < embed::XStorage >& xStorage )
3284cdf0e10cSrcweir {
3285cdf0e10cSrcweir     uno::Reference < document::XDocumentRevisionListPersistence > xReader( comphelper::getProcessServiceFactory()->createInstance(
3286cdf0e10cSrcweir             ::rtl::OUString::createFromAscii("com.sun.star.document.DocumentRevisionListPersistence") ), uno::UNO_QUERY );
3287cdf0e10cSrcweir     if ( xReader.is() )
3288cdf0e10cSrcweir     {
3289cdf0e10cSrcweir         try
3290cdf0e10cSrcweir         {
3291cdf0e10cSrcweir             return xReader->load( xStorage );
3292cdf0e10cSrcweir         }
3293cdf0e10cSrcweir         catch ( uno::Exception& )
3294cdf0e10cSrcweir         {
3295cdf0e10cSrcweir         }
3296cdf0e10cSrcweir     }
3297cdf0e10cSrcweir 
3298cdf0e10cSrcweir     return uno::Sequence < util::RevisionTag >();
3299cdf0e10cSrcweir }
3300cdf0e10cSrcweir 
3301cdf0e10cSrcweir sal_uInt16 SfxMedium::AddVersion_Impl( util::RevisionTag& rRevision )
3302cdf0e10cSrcweir {
3303cdf0e10cSrcweir     if ( GetStorage().is() )
3304cdf0e10cSrcweir     {
3305cdf0e10cSrcweir         // Einen eindeutigen Namen f"ur den Stream ermitteln
3306cdf0e10cSrcweir         SvULongs aLongs;
3307cdf0e10cSrcweir         sal_Int32 nLength = pImp->aVersions.getLength();
3308cdf0e10cSrcweir         for ( sal_Int32 m=0; m<nLength; m++ )
3309cdf0e10cSrcweir         {
3310cdf0e10cSrcweir             sal_uInt32 nVer = (sal_uInt32) String( pImp->aVersions[m].Identifier ).Copy(7).ToInt32();
3311cdf0e10cSrcweir             sal_uInt16 n;
3312cdf0e10cSrcweir             for ( n=0; n<aLongs.Count(); n++ )
3313cdf0e10cSrcweir                 if ( nVer<aLongs[n] )
3314cdf0e10cSrcweir                     break;
3315cdf0e10cSrcweir 
3316cdf0e10cSrcweir             aLongs.Insert( nVer, n );
3317cdf0e10cSrcweir         }
3318cdf0e10cSrcweir 
3319cdf0e10cSrcweir         sal_uInt16 nKey;
3320cdf0e10cSrcweir         for ( nKey=0; nKey<aLongs.Count(); nKey++ )
3321cdf0e10cSrcweir             if ( aLongs[nKey] > ( sal_uIntPtr ) nKey+1 )
3322cdf0e10cSrcweir                 break;
3323cdf0e10cSrcweir 
3324cdf0e10cSrcweir         String aRevName = DEFINE_CONST_UNICODE( "Version" );
3325cdf0e10cSrcweir         aRevName += String::CreateFromInt32( nKey + 1 );
3326cdf0e10cSrcweir         pImp->aVersions.realloc( nLength+1 );
3327cdf0e10cSrcweir         rRevision.Identifier = aRevName;
3328cdf0e10cSrcweir         pImp->aVersions[nLength] = rRevision;
3329cdf0e10cSrcweir         return nKey;
3330cdf0e10cSrcweir     }
3331cdf0e10cSrcweir 
3332cdf0e10cSrcweir     return 0;
3333cdf0e10cSrcweir }
3334cdf0e10cSrcweir 
3335cdf0e10cSrcweir sal_Bool SfxMedium::RemoveVersion_Impl( const ::rtl::OUString& rName )
3336cdf0e10cSrcweir {
3337cdf0e10cSrcweir     if ( !pImp->aVersions.getLength() )
3338cdf0e10cSrcweir         return sal_False;
3339cdf0e10cSrcweir 
3340cdf0e10cSrcweir     sal_Int32 nLength = pImp->aVersions.getLength();
3341cdf0e10cSrcweir     for ( sal_Int32 n=0; n<nLength; n++ )
3342cdf0e10cSrcweir     {
3343cdf0e10cSrcweir         if ( pImp->aVersions[n].Identifier == rName )
3344cdf0e10cSrcweir         {
3345cdf0e10cSrcweir             for ( sal_Int32 m=n; m<nLength-1; m++ )
3346cdf0e10cSrcweir                 pImp->aVersions[m] = pImp->aVersions[m+1];
3347cdf0e10cSrcweir             pImp->aVersions.realloc(nLength-1);
3348cdf0e10cSrcweir             return sal_True;
3349cdf0e10cSrcweir         }
3350cdf0e10cSrcweir     }
3351cdf0e10cSrcweir 
3352cdf0e10cSrcweir     return sal_False;
3353cdf0e10cSrcweir }
3354cdf0e10cSrcweir 
3355cdf0e10cSrcweir sal_Bool SfxMedium::TransferVersionList_Impl( SfxMedium& rMedium )
3356cdf0e10cSrcweir {
3357cdf0e10cSrcweir     if ( rMedium.pImp->aVersions.getLength() )
3358cdf0e10cSrcweir     {
3359cdf0e10cSrcweir         pImp->aVersions = rMedium.pImp->aVersions;
3360cdf0e10cSrcweir         return sal_True;
3361cdf0e10cSrcweir     }
3362cdf0e10cSrcweir 
3363cdf0e10cSrcweir     return sal_False;
3364cdf0e10cSrcweir }
3365cdf0e10cSrcweir 
3366cdf0e10cSrcweir sal_Bool SfxMedium::SaveVersionList_Impl( sal_Bool /*bUseXML*/ )
3367cdf0e10cSrcweir {
3368cdf0e10cSrcweir     if ( GetStorage().is() )
3369cdf0e10cSrcweir     {
3370cdf0e10cSrcweir         if ( !pImp->aVersions.getLength() )
3371cdf0e10cSrcweir             return sal_True;
3372cdf0e10cSrcweir 
3373cdf0e10cSrcweir         uno::Reference < document::XDocumentRevisionListPersistence > xWriter( comphelper::getProcessServiceFactory()->createInstance(
3374cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii("com.sun.star.document.DocumentRevisionListPersistence") ), uno::UNO_QUERY );
3375cdf0e10cSrcweir         if ( xWriter.is() )
3376cdf0e10cSrcweir         {
3377cdf0e10cSrcweir             try
3378cdf0e10cSrcweir             {
3379cdf0e10cSrcweir                 xWriter->store( GetStorage(), pImp->aVersions );
3380cdf0e10cSrcweir                 return sal_True;
3381cdf0e10cSrcweir             }
3382cdf0e10cSrcweir             catch ( uno::Exception& )
3383cdf0e10cSrcweir             {
3384cdf0e10cSrcweir             }
3385cdf0e10cSrcweir         }
3386cdf0e10cSrcweir     }
3387cdf0e10cSrcweir 
3388cdf0e10cSrcweir     return sal_False;
3389cdf0e10cSrcweir }
3390cdf0e10cSrcweir 
3391cdf0e10cSrcweir //----------------------------------------------------------------
3392cdf0e10cSrcweir sal_Bool SfxMedium::IsReadOnly()
3393cdf0e10cSrcweir {
3394cdf0e10cSrcweir     sal_Bool bReadOnly = sal_False;
3395cdf0e10cSrcweir 
3396cdf0e10cSrcweir     // a) ReadOnly filter cant produce read/write contents!
3397cdf0e10cSrcweir     bReadOnly = (
3398cdf0e10cSrcweir                     (pFilter                                                                         ) &&
3399cdf0e10cSrcweir                     ((pFilter->GetFilterFlags() & SFX_FILTER_OPENREADONLY) == SFX_FILTER_OPENREADONLY)
3400cdf0e10cSrcweir                 );
3401cdf0e10cSrcweir 
3402cdf0e10cSrcweir     // b) if filter allow read/write contents .. check open mode of the storage
3403cdf0e10cSrcweir     if (!bReadOnly)
3404cdf0e10cSrcweir         bReadOnly = !( GetOpenMode() & STREAM_WRITE );
3405cdf0e10cSrcweir 
3406cdf0e10cSrcweir     // c) the API can force the readonly state!
3407cdf0e10cSrcweir     if (!bReadOnly)
3408cdf0e10cSrcweir     {
3409cdf0e10cSrcweir         SFX_ITEMSET_ARG( GetItemSet(), pItem, SfxBoolItem, SID_DOC_READONLY, sal_False);
3410cdf0e10cSrcweir         if (pItem)
3411cdf0e10cSrcweir             bReadOnly = pItem->GetValue();
3412cdf0e10cSrcweir     }
3413cdf0e10cSrcweir 
3414cdf0e10cSrcweir     return bReadOnly;
3415cdf0e10cSrcweir }
3416cdf0e10cSrcweir 
3417cdf0e10cSrcweir //----------------------------------------------------------------
3418cdf0e10cSrcweir sal_Bool SfxMedium::SetWritableForUserOnly( const ::rtl::OUString& aURL )
3419cdf0e10cSrcweir {
3420cdf0e10cSrcweir     // UCB does not allow to allow write access only for the user,
3421cdf0e10cSrcweir     // use osl API
3422cdf0e10cSrcweir     sal_Bool bResult = sal_False;
3423cdf0e10cSrcweir 
3424cdf0e10cSrcweir     ::osl::DirectoryItem aDirItem;
3425cdf0e10cSrcweir     if ( ::osl::DirectoryItem::get( aURL, aDirItem ) == ::osl::FileBase::E_None )
3426cdf0e10cSrcweir     {
3427cdf0e10cSrcweir         ::osl::FileStatus aFileStatus( FileStatusMask_Attributes );
3428cdf0e10cSrcweir         if ( aDirItem.getFileStatus( aFileStatus ) == osl::FileBase::E_None
3429cdf0e10cSrcweir           && aFileStatus.isValid( FileStatusMask_Attributes ) )
3430cdf0e10cSrcweir         {
3431cdf0e10cSrcweir             sal_uInt64 nAttributes = aFileStatus.getAttributes();
3432cdf0e10cSrcweir 
3433cdf0e10cSrcweir             nAttributes &= ~(Attribute_OwnWrite |
3434cdf0e10cSrcweir                              Attribute_GrpWrite |
3435cdf0e10cSrcweir                              Attribute_OthWrite |
3436cdf0e10cSrcweir                              Attribute_ReadOnly);
3437cdf0e10cSrcweir             nAttributes |= Attribute_OwnWrite;
3438cdf0e10cSrcweir 
3439cdf0e10cSrcweir             bResult = ( osl::File::setAttributes( aURL, nAttributes ) == ::osl::FileBase::E_None );
3440cdf0e10cSrcweir         }
3441cdf0e10cSrcweir     }
3442cdf0e10cSrcweir 
3443cdf0e10cSrcweir     return bResult;
3444cdf0e10cSrcweir }
3445cdf0e10cSrcweir 
3446cdf0e10cSrcweir //----------------------------------------------------------------
3447cdf0e10cSrcweir void SfxMedium::CreateTempFile( sal_Bool bReplace )
3448cdf0e10cSrcweir {
3449cdf0e10cSrcweir     if ( pImp->pTempFile )
3450cdf0e10cSrcweir 	{
3451cdf0e10cSrcweir         if ( !bReplace )
3452cdf0e10cSrcweir             return;
3453cdf0e10cSrcweir 
3454cdf0e10cSrcweir         DELETEZ( pImp->pTempFile );
3455cdf0e10cSrcweir 		aName = String();
3456cdf0e10cSrcweir 	}
3457cdf0e10cSrcweir 
3458cdf0e10cSrcweir     pImp->pTempFile = new ::utl::TempFile();
3459cdf0e10cSrcweir     pImp->pTempFile->EnableKillingFile( sal_True );
3460cdf0e10cSrcweir     aName = pImp->pTempFile->GetFileName();
3461cdf0e10cSrcweir     ::rtl::OUString aTmpURL = pImp->pTempFile->GetURL();
3462cdf0e10cSrcweir     if ( !aName.Len() || !aTmpURL.getLength() )
3463cdf0e10cSrcweir     {
3464cdf0e10cSrcweir         SetError( ERRCODE_IO_CANTWRITE, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
3465cdf0e10cSrcweir         return;
3466cdf0e10cSrcweir     }
3467cdf0e10cSrcweir 
3468cdf0e10cSrcweir     if ( !( nStorOpenMode & STREAM_TRUNC ) )
3469cdf0e10cSrcweir     {
3470cdf0e10cSrcweir         sal_Bool bTransferSuccess = sal_False;
3471cdf0e10cSrcweir 
3472cdf0e10cSrcweir         if ( GetContent().is()
3473cdf0e10cSrcweir           && ::utl::LocalFileHelper::IsLocalFile( GetURLObject().GetMainURL( INetURLObject::NO_DECODE ) )
3474cdf0e10cSrcweir           && ::utl::UCBContentHelper::IsDocument( GetURLObject().GetMainURL( INetURLObject::NO_DECODE ) ) )
3475cdf0e10cSrcweir         {
3476cdf0e10cSrcweir             // if there is already such a document, we should copy it
3477cdf0e10cSrcweir             // if it is a file system use OS copy process
3478cdf0e10cSrcweir             try
3479cdf0e10cSrcweir             {
3480cdf0e10cSrcweir                 uno::Reference< ::com::sun::star::ucb::XCommandEnvironment > xComEnv;
3481cdf0e10cSrcweir                 INetURLObject aTmpURLObj( aTmpURL );
3482cdf0e10cSrcweir                 ::rtl::OUString aFileName = aTmpURLObj.getName( INetURLObject::LAST_SEGMENT,
3483cdf0e10cSrcweir                                                                 true,
3484cdf0e10cSrcweir                                                                 INetURLObject::DECODE_WITH_CHARSET );
3485cdf0e10cSrcweir                 if ( aFileName.getLength() && aTmpURLObj.removeSegment() )
3486cdf0e10cSrcweir                 {
3487cdf0e10cSrcweir                     ::ucbhelper::Content aTargetContent( aTmpURLObj.GetMainURL( INetURLObject::NO_DECODE ), xComEnv );
3488cdf0e10cSrcweir                     if ( aTargetContent.transferContent( pImp->aContent, ::ucbhelper::InsertOperation_COPY, aFileName, NameClash::OVERWRITE ) )
3489cdf0e10cSrcweir                     {
3490cdf0e10cSrcweir                         SetWritableForUserOnly( aTmpURL );
3491cdf0e10cSrcweir                         bTransferSuccess = sal_True;
3492cdf0e10cSrcweir                     }
3493cdf0e10cSrcweir                 }
3494cdf0e10cSrcweir             }
3495cdf0e10cSrcweir             catch( uno::Exception& )
3496cdf0e10cSrcweir             {}
3497cdf0e10cSrcweir 
3498cdf0e10cSrcweir             if ( bTransferSuccess )
3499cdf0e10cSrcweir             {
3500cdf0e10cSrcweir                 CloseOutStream();
3501cdf0e10cSrcweir                 CloseInStream();
3502cdf0e10cSrcweir             }
3503cdf0e10cSrcweir         }
3504cdf0e10cSrcweir 
3505cdf0e10cSrcweir         if ( !bTransferSuccess && pInStream )
3506cdf0e10cSrcweir         {
3507cdf0e10cSrcweir             // the case when there is no URL-access available or this is a remote protocoll
3508cdf0e10cSrcweir             // but there is an input stream
3509cdf0e10cSrcweir             GetOutStream();
3510cdf0e10cSrcweir             if ( pOutStream )
3511cdf0e10cSrcweir             {
3512cdf0e10cSrcweir                 char        *pBuf = new char [8192];
3513cdf0e10cSrcweir                 sal_uInt32   nErr = ERRCODE_NONE;
3514cdf0e10cSrcweir 
3515cdf0e10cSrcweir                 pInStream->Seek(0);
3516cdf0e10cSrcweir                 pOutStream->Seek(0);
3517cdf0e10cSrcweir 
3518cdf0e10cSrcweir                 while( !pInStream->IsEof() && nErr == ERRCODE_NONE )
3519cdf0e10cSrcweir                 {
3520cdf0e10cSrcweir                     sal_uInt32 nRead = pInStream->Read( pBuf, 8192 );
3521cdf0e10cSrcweir                     nErr = pInStream->GetError();
3522cdf0e10cSrcweir                     pOutStream->Write( pBuf, nRead );
3523cdf0e10cSrcweir                 }
3524cdf0e10cSrcweir 
3525cdf0e10cSrcweir                 bTransferSuccess = sal_True;
3526cdf0e10cSrcweir                 delete[] pBuf;
3527cdf0e10cSrcweir                 CloseInStream();
3528cdf0e10cSrcweir             }
3529cdf0e10cSrcweir             CloseOutStream_Impl();
3530cdf0e10cSrcweir         }
3531cdf0e10cSrcweir         else
3532cdf0e10cSrcweir         {
3533cdf0e10cSrcweir             // Quite strange design, but currently it is expected that in this case no transfer happens
3534cdf0e10cSrcweir             // TODO/LATER: get rid of this inconsistent part of the call design
3535cdf0e10cSrcweir             bTransferSuccess = sal_True;
3536cdf0e10cSrcweir             CloseInStream();
3537cdf0e10cSrcweir         }
3538cdf0e10cSrcweir 
3539cdf0e10cSrcweir         if ( !bTransferSuccess )
3540cdf0e10cSrcweir         {
3541cdf0e10cSrcweir             SetError( ERRCODE_IO_CANTWRITE, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
3542cdf0e10cSrcweir             return;
3543cdf0e10cSrcweir         }
3544cdf0e10cSrcweir     }
3545cdf0e10cSrcweir 
3546cdf0e10cSrcweir     CloseStorage();
3547cdf0e10cSrcweir }
3548cdf0e10cSrcweir 
3549cdf0e10cSrcweir //----------------------------------------------------------------
3550cdf0e10cSrcweir void SfxMedium::CreateTempFileNoCopy()
3551cdf0e10cSrcweir {
3552cdf0e10cSrcweir     // this call always replaces the existing temporary file
3553cdf0e10cSrcweir     if ( pImp->pTempFile )
3554cdf0e10cSrcweir         delete pImp->pTempFile;
3555cdf0e10cSrcweir 
3556cdf0e10cSrcweir     pImp->pTempFile = new ::utl::TempFile();
3557cdf0e10cSrcweir     pImp->pTempFile->EnableKillingFile( sal_True );
3558cdf0e10cSrcweir     aName = pImp->pTempFile->GetFileName();
3559cdf0e10cSrcweir     if ( !aName.Len() )
3560cdf0e10cSrcweir     {
3561cdf0e10cSrcweir         SetError( ERRCODE_IO_CANTWRITE, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
3562cdf0e10cSrcweir         return;
3563cdf0e10cSrcweir     }
3564cdf0e10cSrcweir 
3565cdf0e10cSrcweir     CloseOutStream_Impl();
3566cdf0e10cSrcweir     CloseStorage();
3567cdf0e10cSrcweir }
3568cdf0e10cSrcweir 
3569cdf0e10cSrcweir ::rtl::OUString SfxMedium::GetCharset()
3570cdf0e10cSrcweir {
3571cdf0e10cSrcweir     if( !pImp->bIsCharsetInitialized )
3572cdf0e10cSrcweir 	{
3573cdf0e10cSrcweir 		// Set an error in case there is no content?
3574cdf0e10cSrcweir 		if ( GetContent().is() )
3575cdf0e10cSrcweir 		{
3576cdf0e10cSrcweir 			pImp->bIsCharsetInitialized = sal_True;
3577cdf0e10cSrcweir 
3578cdf0e10cSrcweir 			try
3579cdf0e10cSrcweir 			{
3580cdf0e10cSrcweir 				Any aAny = pImp->aContent.getPropertyValue( ::rtl::OUString::createFromAscii( "MediaType" ) );
3581cdf0e10cSrcweir 				::rtl::OUString aField;
3582cdf0e10cSrcweir 				aAny >>= aField;
3583cdf0e10cSrcweir 
3584cdf0e10cSrcweir 				::rtl::OString sContent = ::rtl::OUStringToOString( aField, RTL_TEXTENCODING_ASCII_US );
3585cdf0e10cSrcweir 				ByteString sType, sSubType;
3586cdf0e10cSrcweir 				INetContentTypeParameterList aParameters;
3587cdf0e10cSrcweir 
3588cdf0e10cSrcweir 				if( INetContentTypes::parse( sContent, sType, sSubType, &aParameters ) )
3589cdf0e10cSrcweir 				{
3590cdf0e10cSrcweir 					const INetContentTypeParameter * pCharset = aParameters.find("charset");
3591cdf0e10cSrcweir 					if (pCharset != 0)
3592cdf0e10cSrcweir 						pImp->aCharset = pCharset->m_sValue;
3593cdf0e10cSrcweir 				}
3594cdf0e10cSrcweir 			}
3595cdf0e10cSrcweir 			catch ( ::com::sun::star::uno::Exception& )
3596cdf0e10cSrcweir 			{
3597cdf0e10cSrcweir 			}
3598cdf0e10cSrcweir 		}
3599cdf0e10cSrcweir 	}
3600cdf0e10cSrcweir 
3601cdf0e10cSrcweir 	return pImp->aCharset;
3602cdf0e10cSrcweir }
3603cdf0e10cSrcweir 
3604cdf0e10cSrcweir void SfxMedium::SetCharset( ::rtl::OUString aChs )
3605cdf0e10cSrcweir {
3606cdf0e10cSrcweir 	pImp->bIsCharsetInitialized = sal_True;
3607cdf0e10cSrcweir 	pImp->aCharset = aChs;
3608cdf0e10cSrcweir }
3609cdf0e10cSrcweir 
3610cdf0e10cSrcweir sal_Bool SfxMedium::SignContents_Impl( sal_Bool bScriptingContent, const ::rtl::OUString& aODFVersion, sal_Bool bHasValidDocumentSignature )
3611cdf0e10cSrcweir {
3612cdf0e10cSrcweir     sal_Bool bChanges = sal_False;
3613cdf0e10cSrcweir 
3614cdf0e10cSrcweir     // the medium should be closed to be able to sign, the caller is responsible to close it
3615cdf0e10cSrcweir     if ( !IsOpen() && !GetError() )
3616cdf0e10cSrcweir     {
3617cdf0e10cSrcweir         // The component should know if there was a valid document signature, since
3618cdf0e10cSrcweir         // it should show a warning in this case
3619cdf0e10cSrcweir         uno::Sequence< uno::Any > aArgs( 2 );
3620cdf0e10cSrcweir         aArgs[0] <<= aODFVersion;
3621cdf0e10cSrcweir         aArgs[1] <<= bHasValidDocumentSignature;
3622cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::security::XDocumentDigitalSignatures > xSigner(
3623cdf0e10cSrcweir             comphelper::getProcessServiceFactory()->createInstanceWithArguments(
3624cdf0e10cSrcweir                 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.security.DocumentDigitalSignatures" ) ),
3625cdf0e10cSrcweir                 aArgs ),
3626cdf0e10cSrcweir             ::com::sun::star::uno::UNO_QUERY );
3627cdf0e10cSrcweir 
3628cdf0e10cSrcweir         if ( xSigner.is() )
3629cdf0e10cSrcweir         {
3630cdf0e10cSrcweir             uno::Reference< embed::XStorage > xWriteableZipStor;
3631cdf0e10cSrcweir             if ( !IsReadOnly() )
3632cdf0e10cSrcweir             {
3633cdf0e10cSrcweir                 // we can reuse the temporary file if there is one already
3634cdf0e10cSrcweir                 CreateTempFile( sal_False );
3635cdf0e10cSrcweir                 GetMedium_Impl();
3636cdf0e10cSrcweir 
3637cdf0e10cSrcweir                 try
3638cdf0e10cSrcweir                 {
3639cdf0e10cSrcweir                     if ( !pImp->xStream.is() )
3640cdf0e10cSrcweir                         throw uno::RuntimeException();
3641cdf0e10cSrcweir 
3642cdf0e10cSrcweir                     xWriteableZipStor = ::comphelper::OStorageHelper::GetStorageOfFormatFromStream( ZIP_STORAGE_FORMAT_STRING, pImp->xStream );
3643cdf0e10cSrcweir                     if ( !xWriteableZipStor.is() )
3644cdf0e10cSrcweir                         throw uno::RuntimeException();
3645cdf0e10cSrcweir 
3646cdf0e10cSrcweir                     uno::Reference< embed::XStorage > xMetaInf = xWriteableZipStor->openStorageElement(
3647cdf0e10cSrcweir                                                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "META-INF" ) ),
3648cdf0e10cSrcweir                                                     embed::ElementModes::READWRITE );
3649cdf0e10cSrcweir                     if ( !xMetaInf.is() )
3650cdf0e10cSrcweir                         throw uno::RuntimeException();
3651cdf0e10cSrcweir 
3652cdf0e10cSrcweir                     if ( bScriptingContent )
3653cdf0e10cSrcweir                     {
3654cdf0e10cSrcweir                         // If the signature has already the document signature it will be removed
3655cdf0e10cSrcweir                         // after the scripting signature is inserted.
3656cdf0e10cSrcweir                         uno::Reference< io::XStream > xStream(
3657cdf0e10cSrcweir                             xMetaInf->openStreamElement( xSigner->getScriptingContentSignatureDefaultStreamName(),
3658cdf0e10cSrcweir                                                          embed::ElementModes::READWRITE ),
3659cdf0e10cSrcweir                             uno::UNO_SET_THROW );
3660cdf0e10cSrcweir 
3661cdf0e10cSrcweir                         if ( xSigner->signScriptingContent( GetZipStorageToSign_Impl(), xStream ) )
3662cdf0e10cSrcweir                         {
3663cdf0e10cSrcweir                             // remove the document signature if any
3664cdf0e10cSrcweir                             ::rtl::OUString aDocSigName = xSigner->getDocumentContentSignatureDefaultStreamName();
3665cdf0e10cSrcweir                             if ( aDocSigName.getLength() && xMetaInf->hasByName( aDocSigName ) )
3666cdf0e10cSrcweir                                 xMetaInf->removeElement( aDocSigName );
3667cdf0e10cSrcweir 
3668cdf0e10cSrcweir                             uno::Reference< embed::XTransactedObject > xTransact( xMetaInf, uno::UNO_QUERY_THROW );
3669cdf0e10cSrcweir                             xTransact->commit();
3670cdf0e10cSrcweir                             xTransact.set( xWriteableZipStor, uno::UNO_QUERY_THROW );
3671cdf0e10cSrcweir                             xTransact->commit();
3672cdf0e10cSrcweir 
3673cdf0e10cSrcweir                             // the temporary file has been written, commit it to the original file
3674cdf0e10cSrcweir                             Commit();
3675cdf0e10cSrcweir                             bChanges = sal_True;
3676cdf0e10cSrcweir                         }
3677cdf0e10cSrcweir                     }
3678cdf0e10cSrcweir                     else
3679cdf0e10cSrcweir                     {
3680cdf0e10cSrcweir                          uno::Reference< io::XStream > xStream(
3681cdf0e10cSrcweir                             xMetaInf->openStreamElement( xSigner->getDocumentContentSignatureDefaultStreamName(),
3682cdf0e10cSrcweir                                                          embed::ElementModes::READWRITE ),
3683cdf0e10cSrcweir                             uno::UNO_SET_THROW );
3684cdf0e10cSrcweir 
3685cdf0e10cSrcweir                         if ( xSigner->signDocumentContent( GetZipStorageToSign_Impl(), xStream ) )
3686cdf0e10cSrcweir                         {
3687cdf0e10cSrcweir                             uno::Reference< embed::XTransactedObject > xTransact( xMetaInf, uno::UNO_QUERY_THROW );
3688cdf0e10cSrcweir                             xTransact->commit();
3689cdf0e10cSrcweir                             xTransact.set( xWriteableZipStor, uno::UNO_QUERY_THROW );
3690cdf0e10cSrcweir                             xTransact->commit();
3691cdf0e10cSrcweir 
3692cdf0e10cSrcweir                             // the temporary file has been written, commit it to the original file
3693cdf0e10cSrcweir                             Commit();
3694cdf0e10cSrcweir                             bChanges = sal_True;
3695cdf0e10cSrcweir                         }
3696cdf0e10cSrcweir                     }
3697cdf0e10cSrcweir                 }
3698cdf0e10cSrcweir                 catch ( uno::Exception& )
3699cdf0e10cSrcweir                 {
3700cdf0e10cSrcweir                     OSL_ENSURE( sal_False, "Couldn't use signing functionality!\n" );
3701cdf0e10cSrcweir                 }
3702cdf0e10cSrcweir 
3703cdf0e10cSrcweir                 CloseAndRelease();
3704cdf0e10cSrcweir             }
3705cdf0e10cSrcweir             else
3706cdf0e10cSrcweir             {
3707cdf0e10cSrcweir                 try
3708cdf0e10cSrcweir                 {
3709cdf0e10cSrcweir                     if ( bScriptingContent )
3710cdf0e10cSrcweir                         xSigner->showScriptingContentSignatures( GetZipStorageToSign_Impl(), uno::Reference< io::XInputStream >() );
3711cdf0e10cSrcweir                     else
3712cdf0e10cSrcweir                         xSigner->showDocumentContentSignatures( GetZipStorageToSign_Impl(), uno::Reference< io::XInputStream >() );
3713cdf0e10cSrcweir                 }
3714cdf0e10cSrcweir                 catch( uno::Exception& )
3715cdf0e10cSrcweir                 {
3716cdf0e10cSrcweir                     OSL_ENSURE( sal_False, "Couldn't use signing functionality!\n" );
3717cdf0e10cSrcweir                 }
3718cdf0e10cSrcweir             }
3719cdf0e10cSrcweir         }
3720cdf0e10cSrcweir 
3721cdf0e10cSrcweir         ResetError();
3722cdf0e10cSrcweir     }
3723cdf0e10cSrcweir 
3724cdf0e10cSrcweir 	return bChanges;
3725cdf0e10cSrcweir }
3726cdf0e10cSrcweir 
3727cdf0e10cSrcweir //----------------------------------------------------------------
3728cdf0e10cSrcweir sal_uInt16 SfxMedium::GetCachedSignatureState_Impl()
3729cdf0e10cSrcweir {
3730cdf0e10cSrcweir 	return pImp->m_nSignatureState;
3731cdf0e10cSrcweir }
3732cdf0e10cSrcweir 
3733cdf0e10cSrcweir //----------------------------------------------------------------
3734cdf0e10cSrcweir void SfxMedium::SetCachedSignatureState_Impl( sal_uInt16 nState )
3735cdf0e10cSrcweir {
3736cdf0e10cSrcweir 	pImp->m_nSignatureState = nState;
3737cdf0e10cSrcweir }
3738cdf0e10cSrcweir 
3739cdf0e10cSrcweir sal_Bool SfxMedium::HasStorage_Impl() const
3740cdf0e10cSrcweir {
3741cdf0e10cSrcweir     return pImp->xStorage.is();
3742cdf0e10cSrcweir }
3743cdf0e10cSrcweir 
3744cdf0e10cSrcweir sal_Bool SfxMedium::IsOpen() const
3745cdf0e10cSrcweir {
3746cdf0e10cSrcweir     return pInStream || pOutStream || pImp->xStorage.is();
3747cdf0e10cSrcweir }
3748cdf0e10cSrcweir 
3749cdf0e10cSrcweir ::rtl::OUString SfxMedium::CreateTempCopyWithExt( const ::rtl::OUString& aURL )
3750cdf0e10cSrcweir {
3751cdf0e10cSrcweir 	::rtl::OUString aResult;
3752cdf0e10cSrcweir 
3753cdf0e10cSrcweir 	if ( aURL.getLength() )
3754cdf0e10cSrcweir 	{
3755cdf0e10cSrcweir 		sal_Int32 nPrefixLen = aURL.lastIndexOf( '.' );
3756cdf0e10cSrcweir 		String aExt = ( nPrefixLen == -1 ) ? String() : String( aURL.copy( nPrefixLen ) );
3757cdf0e10cSrcweir 
3758cdf0e10cSrcweir 		::rtl::OUString aNewTempFileURL = ::utl::TempFile( String(), &aExt ).GetURL();
3759cdf0e10cSrcweir 		if ( aNewTempFileURL.getLength() )
3760cdf0e10cSrcweir 		{
3761cdf0e10cSrcweir 			INetURLObject aSource( aURL );
3762cdf0e10cSrcweir 			INetURLObject aDest( aNewTempFileURL );
3763cdf0e10cSrcweir 			::rtl::OUString aFileName = aDest.getName( INetURLObject::LAST_SEGMENT,
3764cdf0e10cSrcweir 														true,
3765cdf0e10cSrcweir 														INetURLObject::DECODE_WITH_CHARSET );
3766cdf0e10cSrcweir 			if ( aFileName.getLength() && aDest.removeSegment() )
3767cdf0e10cSrcweir 			{
3768cdf0e10cSrcweir 				try
3769cdf0e10cSrcweir 				{
3770cdf0e10cSrcweir 					uno::Reference< ::com::sun::star::ucb::XCommandEnvironment > xComEnv;
3771cdf0e10cSrcweir 					::ucbhelper::Content aTargetContent( aDest.GetMainURL( INetURLObject::NO_DECODE ), xComEnv );
3772cdf0e10cSrcweir 					::ucbhelper::Content aSourceContent( aSource.GetMainURL( INetURLObject::NO_DECODE ), xComEnv );
3773cdf0e10cSrcweir 					if ( aTargetContent.transferContent( aSourceContent,
3774cdf0e10cSrcweir 														::ucbhelper::InsertOperation_COPY,
3775cdf0e10cSrcweir 														aFileName,
3776cdf0e10cSrcweir 														NameClash::OVERWRITE ) )
3777cdf0e10cSrcweir 					{
3778cdf0e10cSrcweir 						// Success
3779cdf0e10cSrcweir 						aResult = aNewTempFileURL;
3780cdf0e10cSrcweir 					}
3781cdf0e10cSrcweir 				}
3782cdf0e10cSrcweir 				catch( uno::Exception& )
3783cdf0e10cSrcweir 				{}
3784cdf0e10cSrcweir 			}
3785cdf0e10cSrcweir 		}
3786cdf0e10cSrcweir 	}
3787cdf0e10cSrcweir 
3788cdf0e10cSrcweir 	return aResult;
3789cdf0e10cSrcweir }
3790cdf0e10cSrcweir 
3791cdf0e10cSrcweir sal_Bool SfxMedium::CallApproveHandler( const uno::Reference< task::XInteractionHandler >& xHandler, uno::Any aRequest, sal_Bool bAllowAbort )
3792cdf0e10cSrcweir {
3793cdf0e10cSrcweir     sal_Bool bResult = sal_False;
3794cdf0e10cSrcweir 
3795cdf0e10cSrcweir     if ( xHandler.is() )
3796cdf0e10cSrcweir     {
3797cdf0e10cSrcweir         try
3798cdf0e10cSrcweir         {
3799cdf0e10cSrcweir             uno::Sequence< uno::Reference< task::XInteractionContinuation > > aContinuations( bAllowAbort ? 2 : 1 );
3800cdf0e10cSrcweir 
3801cdf0e10cSrcweir             ::rtl::Reference< ::comphelper::OInteractionApprove > pApprove( new ::comphelper::OInteractionApprove );
3802cdf0e10cSrcweir             aContinuations[ 0 ] = pApprove.get();
3803cdf0e10cSrcweir 
3804cdf0e10cSrcweir             if ( bAllowAbort )
3805cdf0e10cSrcweir             {
3806cdf0e10cSrcweir                 ::rtl::Reference< ::comphelper::OInteractionAbort > pAbort( new ::comphelper::OInteractionAbort );
3807cdf0e10cSrcweir                 aContinuations[ 1 ] = pAbort.get();
3808cdf0e10cSrcweir             }
3809cdf0e10cSrcweir 
3810cdf0e10cSrcweir             xHandler->handle(::framework::InteractionRequest::CreateRequest (aRequest,aContinuations));
3811cdf0e10cSrcweir             bResult = pApprove->wasSelected();
3812cdf0e10cSrcweir         }
3813cdf0e10cSrcweir         catch( const Exception& )
3814cdf0e10cSrcweir         {
3815cdf0e10cSrcweir         }
3816cdf0e10cSrcweir     }
3817cdf0e10cSrcweir 
3818cdf0e10cSrcweir     return bResult;
3819cdf0e10cSrcweir }
3820cdf0e10cSrcweir 
3821cdf0e10cSrcweir ::rtl::OUString SfxMedium::SwitchDocumentToTempFile()
3822cdf0e10cSrcweir {
3823cdf0e10cSrcweir     // the method returns empty string in case of failure
3824cdf0e10cSrcweir     ::rtl::OUString aResult;
3825cdf0e10cSrcweir     ::rtl::OUString aOrigURL = aLogicName;
3826cdf0e10cSrcweir 
3827cdf0e10cSrcweir     if ( aOrigURL.getLength() )
3828cdf0e10cSrcweir     {
3829cdf0e10cSrcweir 		sal_Int32 nPrefixLen = aOrigURL.lastIndexOf( '.' );
3830cdf0e10cSrcweir 		String aExt = ( nPrefixLen == -1 ) ? String() : String( aOrigURL.copy( nPrefixLen ) );
3831cdf0e10cSrcweir 		::rtl::OUString aNewURL = ::utl::TempFile( String(), &aExt ).GetURL();
3832cdf0e10cSrcweir 
3833cdf0e10cSrcweir         // TODO/LATER: In future the aLogicName should be set to shared folder URL
3834cdf0e10cSrcweir         //             and a temporary file should be created. Transport_Impl should be impossible then.
3835cdf0e10cSrcweir         if ( aNewURL.getLength() )
3836cdf0e10cSrcweir         {
3837cdf0e10cSrcweir             uno::Reference< embed::XStorage > xStorage = GetStorage();
3838cdf0e10cSrcweir             uno::Reference< embed::XOptimizedStorage > xOptStorage( xStorage, uno::UNO_QUERY );
3839cdf0e10cSrcweir 
3840cdf0e10cSrcweir             if ( xOptStorage.is() )
3841cdf0e10cSrcweir             {
3842cdf0e10cSrcweir                 // TODO/LATER: reuse the pImp->pTempFile if it already exists
3843cdf0e10cSrcweir 				CanDisposeStorage_Impl( sal_False );
3844cdf0e10cSrcweir 				Close();
3845cdf0e10cSrcweir                 SetPhysicalName_Impl( String() );
3846cdf0e10cSrcweir                 SetName( aNewURL );
3847cdf0e10cSrcweir 
3848cdf0e10cSrcweir                 // remove the readonly state
3849cdf0e10cSrcweir                 sal_Bool bWasReadonly = sal_False;
3850cdf0e10cSrcweir                 nStorOpenMode = SFX_STREAM_READWRITE;
3851cdf0e10cSrcweir                 SFX_ITEMSET_ARG( pSet, pReadOnlyItem, SfxBoolItem, SID_DOC_READONLY, sal_False );
3852cdf0e10cSrcweir                 if ( pReadOnlyItem && pReadOnlyItem->GetValue() )
3853cdf0e10cSrcweir                     bWasReadonly = sal_True;
3854cdf0e10cSrcweir 				GetItemSet()->ClearItem( SID_DOC_READONLY );
3855cdf0e10cSrcweir 
3856cdf0e10cSrcweir                 GetMedium_Impl();
3857cdf0e10cSrcweir 				LockOrigFileOnDemand( sal_False, sal_False );
3858cdf0e10cSrcweir 				CreateTempFile( sal_True );
3859cdf0e10cSrcweir 				GetMedium_Impl();
3860cdf0e10cSrcweir 
3861cdf0e10cSrcweir                 if ( pImp->xStream.is() )
3862cdf0e10cSrcweir                 {
3863cdf0e10cSrcweir                     try
3864cdf0e10cSrcweir                     {
3865cdf0e10cSrcweir                         xOptStorage->writeAndAttachToStream( pImp->xStream );
3866cdf0e10cSrcweir                         pImp->xStorage = xStorage;
3867cdf0e10cSrcweir                         aResult = aNewURL;
3868cdf0e10cSrcweir                     }
3869cdf0e10cSrcweir                     catch( uno::Exception& )
3870cdf0e10cSrcweir                     {}
3871cdf0e10cSrcweir                 }
3872cdf0e10cSrcweir 
3873cdf0e10cSrcweir                 if ( !aResult.getLength() )
3874cdf0e10cSrcweir                 {
3875cdf0e10cSrcweir                     Close();
3876cdf0e10cSrcweir                     SetPhysicalName_Impl( String() );
3877cdf0e10cSrcweir                     SetName( aOrigURL );
3878cdf0e10cSrcweir                     if ( bWasReadonly )
3879cdf0e10cSrcweir                     {
3880cdf0e10cSrcweir                         // set the readonly state back
3881cdf0e10cSrcweir                         nStorOpenMode = SFX_STREAM_READONLY;
3882cdf0e10cSrcweir 						GetItemSet()->Put( SfxBoolItem(SID_DOC_READONLY, sal_True));
3883cdf0e10cSrcweir                     }
3884cdf0e10cSrcweir                     GetMedium_Impl();
3885cdf0e10cSrcweir                     pImp->xStorage = xStorage;
3886cdf0e10cSrcweir                 }
3887cdf0e10cSrcweir             }
3888cdf0e10cSrcweir         }
3889cdf0e10cSrcweir     }
3890cdf0e10cSrcweir 
3891cdf0e10cSrcweir     return aResult;
3892cdf0e10cSrcweir }
3893cdf0e10cSrcweir 
3894cdf0e10cSrcweir sal_Bool SfxMedium::SwitchDocumentToFile( ::rtl::OUString aURL )
3895cdf0e10cSrcweir {
3896cdf0e10cSrcweir     // the method is only for storage based documents
3897cdf0e10cSrcweir     sal_Bool bResult = sal_False;
3898cdf0e10cSrcweir     ::rtl::OUString aOrigURL = aLogicName;
3899cdf0e10cSrcweir 
3900cdf0e10cSrcweir     if ( aURL.getLength() && aOrigURL.getLength() )
3901cdf0e10cSrcweir     {
3902cdf0e10cSrcweir         uno::Reference< embed::XStorage > xStorage = GetStorage();
3903cdf0e10cSrcweir         uno::Reference< embed::XOptimizedStorage > xOptStorage( xStorage, uno::UNO_QUERY );
3904cdf0e10cSrcweir 
3905cdf0e10cSrcweir         if ( xOptStorage.is() )
3906cdf0e10cSrcweir         {
3907cdf0e10cSrcweir             // TODO/LATER: reuse the pImp->pTempFile if it already exists
3908cdf0e10cSrcweir             CanDisposeStorage_Impl( sal_False );
3909cdf0e10cSrcweir             Close();
3910cdf0e10cSrcweir             SetPhysicalName_Impl( String() );
3911cdf0e10cSrcweir             SetName( aURL );
3912cdf0e10cSrcweir 
3913cdf0e10cSrcweir             // open the temporary file based document
3914cdf0e10cSrcweir             GetMedium_Impl();
3915cdf0e10cSrcweir             LockOrigFileOnDemand( sal_False, sal_False );
3916cdf0e10cSrcweir             CreateTempFile( sal_True );
3917cdf0e10cSrcweir             GetMedium_Impl();
3918cdf0e10cSrcweir 
3919cdf0e10cSrcweir             if ( pImp->xStream.is() )
3920cdf0e10cSrcweir             {
3921cdf0e10cSrcweir                 try
3922cdf0e10cSrcweir                 {
3923cdf0e10cSrcweir                     uno::Reference< io::XTruncate > xTruncate( pImp->xStream, uno::UNO_QUERY_THROW );
3924cdf0e10cSrcweir                     if ( xTruncate.is() )
3925cdf0e10cSrcweir                         xTruncate->truncate();
3926cdf0e10cSrcweir 
3927cdf0e10cSrcweir                     xOptStorage->writeAndAttachToStream( pImp->xStream );
3928cdf0e10cSrcweir                     pImp->xStorage = xStorage;
3929cdf0e10cSrcweir                     bResult = sal_True;
3930cdf0e10cSrcweir                 }
3931cdf0e10cSrcweir                 catch( uno::Exception& )
3932cdf0e10cSrcweir                 {}
3933cdf0e10cSrcweir             }
3934cdf0e10cSrcweir 
3935cdf0e10cSrcweir             if ( !bResult )
3936cdf0e10cSrcweir             {
3937cdf0e10cSrcweir                 Close();
3938cdf0e10cSrcweir                 SetPhysicalName_Impl( String() );
3939cdf0e10cSrcweir                 SetName( aOrigURL );
3940cdf0e10cSrcweir                 GetMedium_Impl();
3941cdf0e10cSrcweir                 pImp->xStorage = xStorage;
3942cdf0e10cSrcweir             }
3943cdf0e10cSrcweir         }
3944cdf0e10cSrcweir     }
3945cdf0e10cSrcweir 
3946cdf0e10cSrcweir     return bResult;
3947cdf0e10cSrcweir }
3948cdf0e10cSrcweir 
3949