Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sw/inc/fmtmeta.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_SW_INC_FMTMETA_HXX
21
#define INCLUDED_SW_INC_FMTMETA_HXX
22
23
#include "calbck.hxx"
24
25
#include <unotools/weakref.hxx>
26
27
#include <svl/poolitem.hxx>
28
#include <sfx2/Metadatable.hxx>
29
30
#include <memory>
31
#include <vector>
32
33
namespace com::sun::star {
34
    namespace document {
35
        class XDocumentProperties;
36
    }
37
    namespace text {
38
        class XTextField;
39
    }
40
}
41
42
/**
43
 * The classes that make up a meta entity are:
44
 * <dl>
45
 *   <dt>SwTextMeta</dt><dd>the text hint</dd>
46
 *   <dt>SwFormatMeta</dt><dd>the pool item</dd>
47
 *   <dt>sw::Meta</dt><dd>the metadatable entity itself</dd>
48
 *   <dt>SwXMeta</dt><dd>the UNO wrapper object</dd>
49
 * </dl>
50
 *
51
 * The text hint contains the pool item (as usual) and has a pointer to the
52
 * text node at which it is attached.
53
 * The pool item has a shared pointer to the metadatable entity, and a reverse
54
 * pointer to the text attribute at which it is attached.
55
 * The pool item is non-poolable; it may only be attached to one text
56
 * attribute.
57
 * Of all the pool items that refer to a metadatable entity, only one may be
58
 * in the document content at any time. Others may be in the undo array, or in
59
 * undo objects.
60
 * The metadatable entity has a reverse pointer to the pool item that is
61
 * currently in the document. It also registers as a client at the text node
62
 * at which it is attached via this pool item and its text attribute.
63
 * The UNO wrapper object registers as a client at the metadatable entity.
64
 *
65
 * Copying the metadatable entity proceeds in the following way:
66
 * <ol>
67
 *   <li>The pool item is cloned (because it is non-poolable); the clone
68
 *       points to the same metadatable entity, but the metadatable entity's
69
 *       reverse pointer is unchanged.</li>
70
 *   <li>The DoCopy() method is called at the new pool item:
71
 *       it will clone the metadatable entity (using RegisterAsCopyOf).
72
 *       This is necessary, because first, a metadatable entity may
73
 *       only be inserted once into a document, and second, the copy may be
74
 *       inserted into a different document than the source document!</li>
75
 *   <li>A new text hint is created, taking over the new pool item.</li>
76
 *   <li>The text hint is inserted into the hints array of some text node.</li>
77
 * </ol>
78
 */
79
80
class SwTextMeta;
81
class SwXMeta;
82
class SwXMetaField;
83
class SwTextNode;
84
class SwDoc;
85
namespace sw {
86
    class Meta;
87
    class MetaFieldManager;
88
}
89
90
/// SwFormatMeta is a pool item subclass that owns a Meta.
91
class SwFormatMeta final
92
    : public SfxPoolItem
93
{
94
private:
95
    friend class SwTextMeta; ///< needs SetTextAttr, DoCopy
96
    friend class ::sw::Meta; ///< needs m_pTextAttr
97
98
    std::shared_ptr< ::sw::Meta > m_pMeta;
99
    SwTextMeta * m_pTextAttr;
100
101
0
    SwTextMeta * GetTextAttr() { return m_pTextAttr; }
102
    void SetTextAttr(SwTextMeta * const i_pTextAttr);
103
104
    /// this method <em>must</em> be called when the hint is actually copied
105
    void DoCopy(::sw::MetaFieldManager & i_rTargetDocManager,
106
        SwTextNode & i_rTargetTextNode);
107
108
    explicit SwFormatMeta( const sal_uInt16 i_nWhich );
109
110
public:
111
    /// takes ownership
112
    DECLARE_ITEM_TYPE_FUNCTION(SwFormatMeta)
113
    explicit SwFormatMeta( std::shared_ptr< ::sw::Meta > i_pMeta,
114
                        const sal_uInt16 i_nWhich );
115
    virtual ~SwFormatMeta() override;
116
117
    /// SfxPoolItem
118
    virtual bool             operator==( const SfxPoolItem & ) const override;
119
    virtual SwFormatMeta*    Clone( SfxItemPool *pPool = nullptr ) const override;
120
121
    /// notify clients registered at m_pMeta that this meta is being (re-)moved
122
    void NotifyChangeTextNode(SwTextNode *const pTextNode);
123
    static SwFormatMeta * CreatePoolDefault( const sal_uInt16 i_nWhich );
124
0
    ::sw::Meta * GetMeta() { return m_pMeta.get(); }
125
};
126
127
namespace sw {
128
129
/// Meta is an annotation on a range of text. There is no UI to insert such annotations, but the UNO
130
/// API can do so.
131
///
132
/// See
133
/// <https://wiki.documentfoundation.org/Documentation/DevGuide/Office_Development#Annotated_text_range>
134
/// for more details.
135
class Meta
136
    : public ::sfx2::Metadatable
137
    , public sw::BroadcastingModify
138
{
139
    friend class ::SwFormatMeta; ///< SetFormatMeta, NotifyChangeTextNode
140
    friend class ::SwXMeta;   ///< GetTextNode, GetTextAttr, Get/SetXMeta
141
142
    unotools::WeakReference<SwXMeta> m_wXMeta;
143
144
    SwFormatMeta * m_pFormat;
145
    SwTextNode * m_pTextNode;
146
147
protected:
148
149
    SwTextMeta * GetTextAttr() const;
150
0
    SwTextNode * GetTextNode() const { return m_pTextNode;} ///< @return 0 if not in document (undo)
151
152
0
    SwFormatMeta * GetFormatMeta() const { return m_pFormat; }
153
0
    void SetFormatMeta( SwFormatMeta * const i_pFormat ) { m_pFormat = i_pFormat; };
154
155
    void NotifyChangeTextNode(SwTextNode *const pTextNode);
156
157
    unotools::WeakReference<SwXMeta> const& GetXMeta() const
158
0
            { return m_wXMeta; }
159
    void SetXMeta(rtl::Reference<SwXMeta> const& xMeta);
160
161
    virtual void SwClientNotify(const SwModify&, const SfxHint&) override;
162
163
public:
164
    explicit Meta(SwFormatMeta * const i_pFormat);
165
    virtual ~Meta() override;
166
167
    /// sfx2::Metadatable
168
    virtual ::sfx2::IXmlIdRegistry& GetRegistry() override;
169
    virtual bool IsInClipboard() const override;
170
    virtual bool IsInUndo() const override;
171
    virtual bool IsInContent() const override;
172
    virtual css::uno::Reference< css::rdf::XMetadatable > MakeUnoObject() override;
173
};
174
175
class MetaField final
176
    : public Meta
177
{
178
private:
179
    friend class ::SwFormatMeta;
180
    friend class ::SwXMetaField;
181
    friend class ::sw::MetaFieldManager;
182
183
    sal_uInt32 m_nNumberFormat;
184
    bool       m_bIsFixedLanguage;
185
186
    sal_uInt32 GetNumberFormat(const OUString& aContent) const;
187
    void SetNumberFormat(sal_uInt32 nNumberFormat);
188
0
    bool IsFixedLanguage() const    { return m_bIsFixedLanguage; }
189
0
    void SetIsFixedLanguage(bool b) { m_bIsFixedLanguage = b; }
190
191
    explicit MetaField(SwFormatMeta * const i_pFormat,
192
            const sal_uInt32 nNumberFormat,
193
            const bool bIsFixedLanguage );
194
195
public:
196
    /// get prefix/suffix from the RDF repository. @throws RuntimeException
197
    void GetPrefixAndSuffix(
198
        OUString *const o_pPrefix, OUString *const o_pSuffix, OUString *const o_pShadingColor);
199
};
200
201
    /// knows all meta-fields in the document.
202
class SW_DLLPUBLIC MetaFieldManager
203
{
204
private:
205
    typedef std::vector< std::weak_ptr<MetaField> > MetaFieldList_t;
206
    MetaFieldList_t m_MetaFields;
207
    /// Document properties of a clipboard document, empty for non-clipboard documents.
208
    css::uno::Reference<css::document::XDocumentProperties> m_xDocumentProperties;
209
210
    MetaFieldManager(MetaFieldManager const&) = delete;
211
    MetaFieldManager& operator=(MetaFieldManager const&) = delete;
212
213
public:
214
    MetaFieldManager();
215
    std::shared_ptr<MetaField> makeMetaField(
216
                SwFormatMeta * const i_pFormat = nullptr,
217
                const sal_uInt32 nNumberFormat = SAL_MAX_UINT32,
218
                const bool bIsFixedLanguage = false );
219
    /// get all meta fields
220
    std::vector< css::uno::Reference<css::text::XTextField> > getMetaFields();
221
    /// Copy document properties from rSource to m_xDocumentProperties.
222
    void copyDocumentProperties(const SwDoc& rSource);
223
    const css::uno::Reference<css::document::XDocumentProperties>& getDocumentProperties() const;
224
};
225
226
} // namespace sw
227
228
#endif // INCLUDED_SW_INC_FMTMETA_HXX
229
230
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */