Coverage Report

Created: 2026-04-09 11:41

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