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