1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_SFX2_DOCFILE_HXX
21 #define INCLUDED_SFX2_DOCFILE_HXX
22 
23 #include <memory>
24 #include <sal/config.h>
25 #include <sfx2/dllapi.h>
26 #include <sfx2/signaturestate.hxx>
27 #include <svl/lockfilecommon.hxx>
28 #include <sal/types.h>
29 #include <rtl/ustring.hxx>
30 #include <svl/itemset.hxx>
31 #include <tools/link.hxx>
32 #include <tools/stream.hxx>
33 
34 namespace com::sun::star::beans { struct PropertyValue; }
35 namespace com::sun::star::embed { class XStorage; }
36 namespace com::sun::star::graphic { class XGraphic; }
37 namespace com::sun::star::io { class XInputStream; }
38 namespace com::sun::star::security { class XCertificate; }
39 namespace com::sun::star::task { class XInteractionHandler; }
40 namespace com::sun::star::ucb { class XCommandEnvironment; }
41 namespace com::sun::star::ucb { class XContent; }
42 namespace com::sun::star::util { struct DateTime; }
43 namespace com::sun::star::util { struct RevisionTag; }
44 namespace ucbhelper { class Content; }
45 
46 class SvKeyValueIterator;
47 class SfxFilter;
48 class SfxMedium_Impl;
49 class INetURLObject;
50 class SfxFrame;
51 class DateTime;
52 
53 namespace weld
54 {
55     class Window;
56 }
57 
58 class SFX2_DLLPUBLIC SfxMedium final : public SvRefBase
59 {
60     std::unique_ptr< SfxMedium_Impl > pImpl;
61 
62     SAL_DLLPRIVATE void SetIsRemote_Impl();
63     SAL_DLLPRIVATE void CloseInStream_Impl(bool bInDestruction = false);
64     SAL_DLLPRIVATE void CloseOutStream_Impl();
65     SAL_DLLPRIVATE void CloseStreams_Impl(bool bInDestruction = false);
66 
67     SAL_DLLPRIVATE void SetEncryptionDataToStorage_Impl();
68 
69 public:
70 
71                         SfxMedium();
72                         SfxMedium( const OUString &rName,
73                                    StreamMode nOpenMode,
74                                    std::shared_ptr<const SfxFilter> pFilter = nullptr,
75                                    const std::shared_ptr<SfxItemSet>& pSet = nullptr );
76                         SfxMedium( const OUString &rName,
77                                    const OUString &rReferer,
78                                    StreamMode nOpenMode,
79                                    std::shared_ptr<const SfxFilter> pFilter = nullptr,
80                                    const std::shared_ptr<SfxItemSet>& pSet = nullptr );
81                         SfxMedium( const css::uno::Reference< css::embed::XStorage >& xStorage,
82                                    const OUString& rBaseURL,
83                                    const std::shared_ptr<SfxItemSet>& pSet = nullptr  );
84                         SfxMedium( const css::uno::Reference< css::embed::XStorage >& xStorage,
85                                    const OUString& rBaseURL,
86                                    const OUString& rTypeName,
87                                    const std::shared_ptr<SfxItemSet>& pSet = nullptr );
88                         SfxMedium( const css::uno::Sequence< css::beans::PropertyValue >& aArgs );
89 
90                         virtual ~SfxMedium() override;
91 
92     void                UseInteractionHandler( bool );
93     css::uno::Reference< css::task::XInteractionHandler >
94                         GetInteractionHandler( bool bGetAlways = false );
95 
96     void setStreamToLoadFrom(
97         const css::uno::Reference<css::io::XInputStream>& xInputStream,
98         bool bIsReadOnly);
99 
100     void                SetLoadTargetFrame(SfxFrame* pFrame );
101     SfxFrame*           GetLoadTargetFrame() const;
102 
103     /**
104      * Does not take ownership of pFlt but pFlt needs to be around as long as the SfxMedium instance.
105      */
106     void                SetFilter(const std::shared_ptr<const SfxFilter>& pFilter);
107     const std::shared_ptr<const SfxFilter>& GetFilter() const;
108     const OUString&     GetOrigURL() const;
109 
110     SfxItemSet  *       GetItemSet() const;
111     void                Close(bool bInDestruction = false);
112     void                CloseAndRelease();
113     void                ReOpen();
114     void                CompleteReOpen();
115     const OUString& GetName() const;
116     const INetURLObject& GetURLObject() const;
117 
118     void                CheckFileDate( const css::util::DateTime& aInitDate );
119     [[nodiscard]] bool  DocNeedsFileDateCheck() const;
120     css::util::DateTime const & GetInitFileDate( bool bIgnoreOldValue );
121 
122     css::uno::Reference< css::ucb::XContent > GetContent() const;
123     const OUString& GetPhysicalName() const;
124     [[nodiscard]] bool IsRemote() const;
125     [[nodiscard]] bool IsOpen() const; // { return aStorage.Is() || pInStream; }
126     void                Download( const Link<void*,void>& aLink = Link<void*,void>());
127     void                SetDoneLink( const Link<void*,void>& rLink );
128 
129     ErrCode             GetErrorCode() const;
GetError() const130     ErrCode             GetError() const
131                         { return GetErrorCode().IgnoreWarning(); }
132     ErrCode const &     GetLastStorageCreationState() const;
133 
134     void                SetError(ErrCode nError);
135 
136     void                CloseInStream();
137     void                CloseOutStream();
138 
139     void                CloseStorage();
140 
141     StreamMode          GetOpenMode() const;
142     void                SetOpenMode( StreamMode nStorOpen, bool bDontClose = false );
143 
144     SvStream*           GetInStream();
145     SvStream*           GetOutStream();
146 
147     bool                Commit();
148     bool                IsStorage();
149 
150     enum class          LockFileResult
151     {
152         Failed,
153         FailedLockFile, // there was only lock file that prevented success - no syslock or IO error
154         Succeeded,
155     };
156     LockFileResult LockOrigFileOnDemand(bool bLoading, bool bNoUI, bool bTryIgnoreLockFile = false,
157                                         LockFileEntry* pLockData = nullptr);
158     void                DisableUnlockWebDAV( bool bDisableUnlockWebDAV = true );
159     void                UnlockFile( bool bReleaseLockStream );
160     /// Lets Transfer_Impl() not fsync the output file.
161     void DisableFileSync(bool bDisableFileSync);
162 
163     css::uno::Reference< css::embed::XStorage > GetStorage( bool bCreateTempFile = true );
164     css::uno::Reference< css::embed::XStorage > GetOutputStorage();
165     void                ResetError();
166     [[nodiscard]] bool  IsExpired() const;
167     void                SetName( const OUString& rName, bool bSetOrigURL = false );
168 
169     const css::uno::Sequence < css::util::RevisionTag >&
170                         GetVersionList( bool _bNoReload = false );
171     [[nodiscard]] bool  IsReadOnly() const;
172 
173     // Whether the medium had originally been opened r/o (either because it is
174     // "physically" r/o, or because it was requested to be opened r/o,
175     // independent of later changes via SetOpenMode; used to keep track of the
176     // "true" state of the medium across toggles via SID_EDITDOC (which do
177     // change SetOpenMode):
178     [[nodiscard]] bool  IsOriginallyReadOnly() const;
179 
180     // Whether the medium had originally been requested to be opened r/o,
181     // independent of later changes via SetOpenMode; used for SID_RELOAD:
182     [[nodiscard]] bool IsOriginallyLoadedReadOnly() const;
183 
184     css::uno::Reference< css::io::XInputStream > const &  GetInputStream();
185 
186     void                CreateTempFile( bool bReplace = true );
187     void                CreateTempFileNoCopy();
188     OUString            SwitchDocumentToTempFile();
189     bool                SwitchDocumentToFile( const OUString& aURL );
190 
191     OUString            GetBaseURL( bool bForSaving=false );
192     void                SetInCheckIn( bool bInCheckIn );
193     bool                IsInCheckIn( ) const;
194     bool                IsSkipImages( ) const;
195 
196     SAL_DLLPRIVATE bool HasStorage_Impl() const;
197 
198     SAL_DLLPRIVATE void StorageBackup_Impl();
199     SAL_DLLPRIVATE OUString const & GetBackup_Impl();
200 
201     SAL_DLLPRIVATE css::uno::Reference< css::embed::XStorage > const & GetZipStorageToSign_Impl( bool bReadOnly = true );
202     SAL_DLLPRIVATE void CloseZipStorage_Impl();
203 
204     // the storage that will be returned by the medium on GetStorage request
205     SAL_DLLPRIVATE void SetStorage_Impl( const css::uno::Reference< css::embed::XStorage >& xNewStorage );
206 
207     SAL_DLLPRIVATE void CloseAndReleaseStreams_Impl();
208     SAL_DLLPRIVATE void AddVersion_Impl( css::util::RevisionTag& rVersion );
209     SAL_DLLPRIVATE bool TransferVersionList_Impl( SfxMedium const & rMedium );
210     SAL_DLLPRIVATE void SaveVersionList_Impl();
211     SAL_DLLPRIVATE void RemoveVersion_Impl( const OUString& rVersion );
212 
213     SAL_DLLPRIVATE void SetExpired_Impl( const DateTime& rDateTime );
214     SAL_DLLPRIVATE SvKeyValueIterator* GetHeaderAttributes_Impl();
215 
216     SAL_DLLPRIVATE void Init_Impl();
217 
218     SAL_DLLPRIVATE void GetLockingStream_Impl();
219     SAL_DLLPRIVATE void GetMedium_Impl();
220     SAL_DLLPRIVATE bool TryDirectTransfer( const OUString& aURL, SfxItemSet const & aTargetSet );
221     SAL_DLLPRIVATE void Transfer_Impl();
222     SAL_DLLPRIVATE void CreateFileStream();
223     SAL_DLLPRIVATE void SetUpdatePickList(bool);
224     SAL_DLLPRIVATE bool IsUpdatePickList() const;
225 
226     SAL_DLLPRIVATE void SetLongName(const OUString &rName);
227     SAL_DLLPRIVATE const OUString & GetLongName() const;
228     SAL_DLLPRIVATE bool IsPreview_Impl() const;
229     SAL_DLLPRIVATE void ClearBackup_Impl();
230     SAL_DLLPRIVATE void SetPhysicalName_Impl(const OUString& rName);
231     SAL_DLLPRIVATE void CanDisposeStorage_Impl( bool bDisposeStorage );
232     SAL_DLLPRIVATE bool WillDisposeStorageOnClose_Impl();
233 
234     SAL_DLLPRIVATE void DoBackup_Impl();
235     SAL_DLLPRIVATE void DoInternalBackup_Impl( const ::ucbhelper::Content& aOriginalContent );
236     SAL_DLLPRIVATE void DoInternalBackup_Impl( const ::ucbhelper::Content& aOriginalContent,
237                                                 const OUString& aPrefix,
238                                                 const OUString& aExtension,
239                                                 const OUString& aDestDir );
240 
241     SAL_DLLPRIVATE bool UseBackupToRestore_Impl( ::ucbhelper::Content& aOriginalContent,
242                              const css::uno::Reference< css::ucb::XCommandEnvironment >& xComEnv );
243 
244     SAL_DLLPRIVATE bool StorageCommit_Impl();
245 
246     SAL_DLLPRIVATE void TransactedTransferForFS_Impl( const INetURLObject& aSource,
247                              const INetURLObject& aDest,
248                              const css::uno::Reference< css::ucb::XCommandEnvironment >& xComEnv );
249 
250     SAL_DLLPRIVATE bool
251     SignContents_Impl(weld::Window* pDialogParent,
252                       bool bSignScriptingContent, bool bHasValidDocumentSignature,
253                       const OUString& aSignatureLineId = OUString(),
254                       const css::uno::Reference<css::security::XCertificate>& xCert
255                       = css::uno::Reference<css::security::XCertificate>(),
256                       const css::uno::Reference<css::graphic::XGraphic>& xValidGraphic
257                       = css::uno::Reference<css::graphic::XGraphic>(),
258                       const css::uno::Reference<css::graphic::XGraphic>& xInvalidGraphic
259                       = css::uno::Reference<css::graphic::XGraphic>(),
260                       const OUString& aComment = OUString());
261 
262     SAL_DLLPRIVATE bool
263     SignDocumentContentUsingCertificate(bool bHasValidDocumentSignature,
264                  const css::uno::Reference<css::security::XCertificate>& xCertificate);
265 
266     // the following two methods must be used and make sense only during saving currently
267     // TODO/LATER: in future the signature state should be controlled by the medium not by the document
268     //             in this case the methods will be used generally, and might need to be renamed
269     SAL_DLLPRIVATE SignatureState GetCachedSignatureState_Impl() const;
270     SAL_DLLPRIVATE void       SetCachedSignatureState_Impl( SignatureState nState );
271 
272     void SetHasEmbeddedObjects(bool bHasEmbeddedObjects);
273 
274     static css::uno::Sequence < css::util::RevisionTag > GetVersionList(
275                     const css::uno::Reference< css::embed::XStorage >& xStorage );
276     static OUString CreateTempCopyWithExt( const OUString& aURL );
277     static bool CallApproveHandler(const css::uno::Reference< css::task::XInteractionHandler >& xHandler, const css::uno::Any& rRequest, bool bAllowAbort);
278 
279     static bool         SetWritableForUserOnly( const OUString& aURL );
280     static sal_uInt32   CreatePasswordToModifyHash( const OUString& aPasswd, bool bWriter );
281 
282 private:
283     enum class ShowLockResult { NoLock, Succeeded, Try };
284     ShowLockResult ShowLockedDocumentDialog(const LockFileEntry& aData,
285                                             bool bIsLoading, bool bOwnLock, bool bHandleSysLocked);
286     enum class MessageDlg { LockFileIgnore, LockFileCorrupt };
287     bool                ShowLockFileProblemDialog(MessageDlg nWhichDlg);
288 
289 };
290 
291 #endif
292 
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
294