Coverage Report

Created: 2025-07-07 10:01

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