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 #include <mutex>
34 
35 namespace com::sun::star::beans { struct PropertyValue; }
36 namespace com::sun::star::embed { class XStorage; }
37 namespace com::sun::star::graphic { class XGraphic; }
38 namespace com::sun::star::io { class XInputStream; }
39 namespace com::sun::star::security { class XCertificate; }
40 namespace com::sun::star::task { class XInteractionHandler; }
41 namespace com::sun::star::ucb { class XCommandEnvironment; }
42 namespace com::sun::star::ucb { class XContent; }
43 namespace com::sun::star::util { struct DateTime; }
44 namespace com::sun::star::util { struct RevisionTag; }
45 namespace com::sun::star::frame
46 {
47 class XModel;
48 }
49 namespace ucbhelper { class Content; }
50 
51 class SvKeyValueIterator;
52 class SfxFilter;
53 class SfxMedium_Impl;
54 class INetURLObject;
55 class SfxFrame;
56 class DateTime;
57 struct ImplSVEvent;
58 
59 namespace weld
60 {
61     class Window;
62 }
63 
64 class SFX2_DLLPUBLIC SfxMedium final : public SvRefBase
65 {
66     std::unique_ptr< SfxMedium_Impl > pImpl;
67 
68     SAL_DLLPRIVATE void SetIsRemote_Impl();
69     SAL_DLLPRIVATE void CloseInStream_Impl(bool bInDestruction = false);
70     SAL_DLLPRIVATE void CloseOutStream_Impl();
71     SAL_DLLPRIVATE void CloseStreams_Impl(bool bInDestruction = false);
72 
73     SAL_DLLPRIVATE void SetEncryptionDataToStorage_Impl();
74 
75 public:
76 
77                         SfxMedium();
78                         SfxMedium( const OUString &rName,
79                                    StreamMode nOpenMode,
80                                    std::shared_ptr<const SfxFilter> pFilter = nullptr,
81                                    const std::shared_ptr<SfxItemSet>& pSet = nullptr );
82                         SfxMedium( const OUString &rName,
83                                    const OUString &rReferer,
84                                    StreamMode nOpenMode,
85                                    std::shared_ptr<const SfxFilter> pFilter = nullptr,
86                                    const std::shared_ptr<SfxItemSet>& pSet = nullptr );
87                         SfxMedium( const css::uno::Reference< css::embed::XStorage >& xStorage,
88                                    const OUString& rBaseURL,
89                                    const std::shared_ptr<SfxItemSet>& pSet = nullptr  );
90                         SfxMedium( const css::uno::Reference< css::embed::XStorage >& xStorage,
91                                    const OUString& rBaseURL,
92                                    const OUString& rTypeName,
93                                    const std::shared_ptr<SfxItemSet>& pSet = nullptr );
94                         SfxMedium( const css::uno::Sequence< css::beans::PropertyValue >& aArgs );
95 
96                         virtual ~SfxMedium() override;
97 
98     DECL_STATIC_LINK(SfxMedium, ShowReloadEditableDialog, void*, void);
99     bool CheckCanGetLockfile() const;
100     void SetOriginallyReadOnly(bool val);
101     void AddToCheckEditableWorkerList();
102     void SetWorkerReloadEvent(ImplSVEvent* pEvent);
103     ImplSVEvent* GetWorkerReloadEvent() const;
104     std::shared_ptr<std::recursive_mutex> GetCheckEditableMutex() const;
105     void CancelCheckEditableEntry(bool bRemoveEvent = true);
106 
107     void                UseInteractionHandler( bool );
108     css::uno::Reference< css::task::XInteractionHandler >
109                         GetInteractionHandler( bool bGetAlways = false );
110 
111     void setStreamToLoadFrom(
112         const css::uno::Reference<css::io::XInputStream>& xInputStream,
113         bool bIsReadOnly);
114 
115     void                SetLoadTargetFrame(SfxFrame* pFrame );
116     SfxFrame*           GetLoadTargetFrame() const;
117 
118     /**
119      * Does not take ownership of pFlt but pFlt needs to be around as long as the SfxMedium instance.
120      */
121     void                SetFilter(const std::shared_ptr<const SfxFilter>& pFilter);
122     const std::shared_ptr<const SfxFilter>& GetFilter() const;
123     const OUString&     GetOrigURL() const;
124 
125     SfxItemSet  *       GetItemSet() const;
126     void SetArgs(const css::uno::Sequence<css::beans::PropertyValue>& rArgs);
127     const css::uno::Sequence<css::beans::PropertyValue> & GetArgs() const;
128     void                Close(bool bInDestruction = false);
129     void                CloseAndRelease();
130     void                ReOpen();
131     void                CompleteReOpen();
132     const OUString& GetName() const;
133     const INetURLObject& GetURLObject() const;
134 
135     void                CheckFileDate( const css::util::DateTime& aInitDate );
136     [[nodiscard]] bool  DocNeedsFileDateCheck() const;
137     css::util::DateTime const & GetInitFileDate( bool bIgnoreOldValue );
138 
139     css::uno::Reference< css::ucb::XContent > GetContent() const;
140     const OUString& GetPhysicalName() const;
141     [[nodiscard]] bool IsRemote() const;
142     [[nodiscard]] bool IsOpen() const; // { return aStorage.Is() || pInStream; }
143     void                Download( const Link<void*,void>& aLink = Link<void*,void>());
144     void                SetDoneLink( const Link<void*,void>& rLink );
145 
146     ErrCode             GetErrorCode() const;
GetError() const147     ErrCode             GetError() const
148                         { return GetErrorCode().IgnoreWarning(); }
149     ErrCode const &     GetLastStorageCreationState() const;
150 
151     void                SetError(ErrCode nError);
152 
153     void                CloseInStream();
154     void                CloseOutStream();
155 
156     void                CloseStorage();
157 
158     StreamMode          GetOpenMode() const;
159     void                SetOpenMode( StreamMode nStorOpen, bool bDontClose = false );
160 
161     SvStream*           GetInStream();
162     SvStream*           GetOutStream();
163 
164     bool                Commit();
165     bool                IsStorage();
166 
167     enum class          LockFileResult
168     {
169         Failed,
170         FailedLockFile, // there was only lock file that prevented success - no syslock or IO error
171         Succeeded,
172     };
173     LockFileResult LockOrigFileOnDemand(bool bLoading, bool bNoUI, bool bTryIgnoreLockFile = false,
174                                         LockFileEntry* pLockData = nullptr);
175     void                DisableUnlockWebDAV( bool bDisableUnlockWebDAV = true );
176     void                UnlockFile( bool bReleaseLockStream );
177     /// Lets Transfer_Impl() not fsync the output file.
178     void DisableFileSync(bool bDisableFileSync);
179 
180     css::uno::Reference< css::embed::XStorage > GetStorage( bool bCreateTempFile = true );
181     css::uno::Reference< css::embed::XStorage > GetOutputStorage();
182     void                ResetError();
183     [[nodiscard]] bool  IsExpired() const;
184     void                SetName( const OUString& rName, bool bSetOrigURL = false );
185 
186     const css::uno::Sequence < css::util::RevisionTag >&
187                         GetVersionList( bool _bNoReload = false );
188     [[nodiscard]] bool  IsReadOnly() const;
189 
190     // Whether the medium had originally been opened r/o (either because it is
191     // "physically" r/o, or because it was requested to be opened r/o,
192     // independent of later changes via SetOpenMode; used to keep track of the
193     // "true" state of the medium across toggles via SID_EDITDOC (which do
194     // change SetOpenMode):
195     [[nodiscard]] bool  IsOriginallyReadOnly() const;
196 
197     // Whether the medium had originally been requested to be opened r/o,
198     // independent of later changes via SetOpenMode; used for SID_RELOAD:
199     [[nodiscard]] bool IsOriginallyLoadedReadOnly() const;
200 
201     css::uno::Reference< css::io::XInputStream > const &  GetInputStream();
202 
203     void                CreateTempFile( bool bReplace = true );
204     void                CreateTempFileNoCopy();
205     OUString            SwitchDocumentToTempFile();
206     bool                SwitchDocumentToFile( const OUString& aURL );
207 
208     OUString            GetBaseURL( bool bForSaving=false );
209     void                SetInCheckIn( bool bInCheckIn );
210     bool                IsInCheckIn( ) const;
211     bool                IsSkipImages( ) const;
212 
213     SAL_DLLPRIVATE bool HasStorage_Impl() const;
214 
215     SAL_DLLPRIVATE void StorageBackup_Impl();
216     SAL_DLLPRIVATE OUString const & GetBackup_Impl();
217 
218     SAL_DLLPRIVATE css::uno::Reference< css::embed::XStorage > const & GetZipStorageToSign_Impl( bool bReadOnly = true );
219     SAL_DLLPRIVATE void CloseZipStorage_Impl();
220 
221     // the storage that will be returned by the medium on GetStorage request
222     SAL_DLLPRIVATE void SetStorage_Impl( const css::uno::Reference< css::embed::XStorage >& xNewStorage );
223 
224     SAL_DLLPRIVATE void CloseAndReleaseStreams_Impl();
225     SAL_DLLPRIVATE void AddVersion_Impl( css::util::RevisionTag& rVersion );
226     SAL_DLLPRIVATE bool TransferVersionList_Impl( SfxMedium const & rMedium );
227     SAL_DLLPRIVATE void SaveVersionList_Impl();
228     SAL_DLLPRIVATE void RemoveVersion_Impl( const OUString& rVersion );
229 
230     SAL_DLLPRIVATE void SetExpired_Impl( const DateTime& rDateTime );
231     SAL_DLLPRIVATE SvKeyValueIterator* GetHeaderAttributes_Impl();
232 
233     SAL_DLLPRIVATE void Init_Impl();
234 
235     SAL_DLLPRIVATE void GetLockingStream_Impl();
236     SAL_DLLPRIVATE void GetMedium_Impl();
237     SAL_DLLPRIVATE bool TryDirectTransfer( const OUString& aURL, SfxItemSet const & aTargetSet );
238     SAL_DLLPRIVATE void Transfer_Impl();
239     SAL_DLLPRIVATE void CreateFileStream();
240     SAL_DLLPRIVATE void SetUpdatePickList(bool);
241     SAL_DLLPRIVATE bool IsUpdatePickList() const;
242 
243     SAL_DLLPRIVATE void SetLongName(const OUString &rName);
244     SAL_DLLPRIVATE const OUString & GetLongName() const;
245     SAL_DLLPRIVATE bool IsPreview_Impl() const;
246     SAL_DLLPRIVATE void ClearBackup_Impl();
247     SAL_DLLPRIVATE void SetPhysicalName_Impl(const OUString& rName);
248     SAL_DLLPRIVATE void CanDisposeStorage_Impl( bool bDisposeStorage );
249     SAL_DLLPRIVATE bool WillDisposeStorageOnClose_Impl();
250 
251     SAL_DLLPRIVATE void DoBackup_Impl();
252     SAL_DLLPRIVATE void DoInternalBackup_Impl( const ::ucbhelper::Content& aOriginalContent );
253     SAL_DLLPRIVATE void DoInternalBackup_Impl( const ::ucbhelper::Content& aOriginalContent,
254                                                 const OUString& aPrefix,
255                                                 const OUString& aExtension,
256                                                 const OUString& aDestDir );
257 
258     SAL_DLLPRIVATE bool UseBackupToRestore_Impl( ::ucbhelper::Content& aOriginalContent,
259                              const css::uno::Reference< css::ucb::XCommandEnvironment >& xComEnv );
260 
261     SAL_DLLPRIVATE bool StorageCommit_Impl();
262 
263     SAL_DLLPRIVATE void TransactedTransferForFS_Impl( const INetURLObject& aSource,
264                              const INetURLObject& aDest,
265                              const css::uno::Reference< css::ucb::XCommandEnvironment >& xComEnv );
266 
267     SAL_DLLPRIVATE bool
268     SignContents_Impl(weld::Window* pDialogParent,
269                       bool bSignScriptingContent, bool bHasValidDocumentSignature,
270                       const OUString& aSignatureLineId = OUString(),
271                       const css::uno::Reference<css::security::XCertificate>& xCert
272                       = css::uno::Reference<css::security::XCertificate>(),
273                       const css::uno::Reference<css::graphic::XGraphic>& xValidGraphic
274                       = css::uno::Reference<css::graphic::XGraphic>(),
275                       const css::uno::Reference<css::graphic::XGraphic>& xInvalidGraphic
276                       = css::uno::Reference<css::graphic::XGraphic>(),
277                       const OUString& aComment = OUString());
278 
279     SAL_DLLPRIVATE bool SignDocumentContentUsingCertificate(
280         const css::uno::Reference<css::frame::XModel>& xModel, bool bHasValidDocumentSignature,
281         const css::uno::Reference<css::security::XCertificate>& xCertificate);
282 
283     // the following two methods must be used and make sense only during saving currently
284     // TODO/LATER: in future the signature state should be controlled by the medium not by the document
285     //             in this case the methods will be used generally, and might need to be renamed
286     SAL_DLLPRIVATE SignatureState GetCachedSignatureState_Impl() const;
287     SAL_DLLPRIVATE void       SetCachedSignatureState_Impl( SignatureState nState );
288 
289     void SetHasEmbeddedObjects(bool bHasEmbeddedObjects);
290 
291     static css::uno::Sequence < css::util::RevisionTag > GetVersionList(
292                     const css::uno::Reference< css::embed::XStorage >& xStorage );
293     static OUString CreateTempCopyWithExt( const OUString& aURL );
294     static bool CallApproveHandler(const css::uno::Reference< css::task::XInteractionHandler >& xHandler, const css::uno::Any& rRequest, bool bAllowAbort);
295 
296     static bool         SetWritableForUserOnly( const OUString& aURL );
297     static sal_uInt32   CreatePasswordToModifyHash( const OUString& aPasswd, bool bWriter );
298 
299 private:
300     enum class ShowLockResult { NoLock, Succeeded, Try };
301     ShowLockResult ShowLockedDocumentDialog(const LockFileEntry& aData,
302                                             bool bIsLoading, bool bOwnLock, bool bHandleSysLocked);
303     enum class MessageDlg { LockFileIgnore, LockFileCorrupt };
304     bool                ShowLockFileProblemDialog(MessageDlg nWhichDlg);
305     bool ShowReadOnlyOpenDialog();
306 
307 };
308 
309 #endif
310 
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
312