xref: /aoo41x/main/sw/inc/fmtmeta.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef SW_FMTMETA_HXX
29 #define SW_FMTMETA_HXX
30 
31 #include <cppuhelper/weakref.hxx>
32 
33 #include <svl/poolitem.hxx>
34 #include <sfx2/Metadatable.hxx>
35 
36 #include <boost/shared_ptr.hpp>
37 #include <boost/weak_ptr.hpp>
38 
39 #include <vector>
40 
41 
42 namespace com { namespace sun { namespace star {
43     namespace text {
44         class XTextField;
45     }
46 }}}
47 
48 
49 /**
50  * The classes that make up a meta entity are:
51  * <dl>
52  *   <dt>SwTxtMeta</dt><dd>the text hint</dd>
53  *   <dt>SwFmtMeta</dt><dd>the pool item</dd>
54  *   <dt>sw::Meta</dt><dd>the metadatable entity itself</dd>
55  *   <dt>SwXMeta</dt><dd>the UNO wrapper object</dd>
56  * </dl>
57  *
58  * The text hint contains the pool item (as usual) and has a pointer to the
59  * text node at which it is attached.
60  * The pool item has a shared pointer to the metadatable entity, and a reverse
61  * pointer to the text attribute at which it is attached.
62  * The pool item is non-poolable; it may only be attached to one text
63  * attribute.
64  * Of all the pool items that refer to a metadatable entity, only one may be
65  * in the document content at any time. Others may be in the undo array, or in
66  * undo objects.
67  * The metadatable entity has a reverse pointer to the pool item that is
68  * currently in the document. It also registers as a client at the text node
69  * at which it is attached via this pool item and its text attribute.
70  * The UNO wrapper object registers as a client at the metadatable entity.
71  *
72  * Copying the metadatable entity proceeds in the following way:
73  * <ol>
74  *   <li>The pool item is cloned (because it is non-poolable); the clone
75  *       points to the same metadatable entity, but the metadatable entity's
76  *       reverse pointer is unchanged.</li>
77  *   <li>The DoCopy() method is called at the new pool item:
78  *       it will clone the metadatable entity (using RegisterAsCopyOf).
79  *       This is necessary, because first, a metadatable entity may
80  *       only be inserted once into a document, and second, the copy may be
81  *       inserted into a different document than the source document!</li>
82  *   <li>A new text hint is created, taking over the new pool item.</li>
83  *   <li>The text hint is inserted into the hints array of some text node.</li>
84  * </ol>
85  */
86 
87 class SwTxtMeta;
88 class SwXMeta;
89 class SwXMetaField;
90 namespace sw {
91     class Meta;
92 }
93 
94 class SwFmtMeta
95     : public SfxPoolItem
96 {
97 private:
98     friend class SwTxtMeta; // needs SetTxtAttr, DoCopy
99     friend class ::sw::Meta; // needs m_pTxtAttr
100 
101     ::boost::shared_ptr< ::sw::Meta > m_pMeta;
102     SwTxtMeta * m_pTxtAttr;
103 
104     SwTxtMeta * GetTxtAttr() { return m_pTxtAttr; }
105     void SetTxtAttr(SwTxtMeta * const i_pTxtAttr);
106 
107     /// this method <em>must</em> be called when the hint is actually copied
108     void DoCopy(::sw::MetaFieldManager & i_rTargetDocManager,
109         SwTxtNode & i_rTargetTxtNode);
110 
111     explicit SwFmtMeta( const sal_uInt16 i_nWhich );
112 
113 public:
114     // takes ownership
115     explicit SwFmtMeta( ::boost::shared_ptr< ::sw::Meta > const & i_pMeta,
116                         const sal_uInt16 i_nWhich );
117     virtual ~SwFmtMeta();
118 
119     // SfxPoolItem
120     virtual int              operator==( const SfxPoolItem & ) const;
121     virtual SfxPoolItem *    Clone( SfxItemPool *pPool = 0 ) const;
122 
123     /// notify clients registered at m_pMeta that this meta is being (re-)moved
124     void NotifyChangeTxtNode(SwTxtNode *const pTxtNode);
125     static SwFmtMeta * CreatePoolDefault( const sal_uInt16 i_nWhich );
126     ::sw::Meta * GetMeta() { return m_pMeta.get(); }
127 };
128 
129 
130 namespace sw {
131 
132 class MetaFieldManager;
133 
134 class Meta
135     : public ::sfx2::Metadatable
136     , public SwModify
137 {
138 protected:
139     friend class ::SwFmtMeta; // SetFmtMeta, NotifyChangeTxtNode
140     friend class ::SwXMeta; // GetTxtNode, GetTxtAttr, Get/SetXMeta
141 
142     ::com::sun::star::uno::WeakReference<
143         ::com::sun::star::rdf::XMetadatable> m_wXMeta;
144 
145     SwFmtMeta * m_pFmt;
146     SwTxtNode * m_pTxtNode;
147 
148     SwTxtMeta * GetTxtAttr() const;
149     SwTxtNode * GetTxtNode() const; // returns 0 if not in document (undo)
150 
151     SwFmtMeta * GetFmtMeta() const { return m_pFmt; }
152     void SetFmtMeta( SwFmtMeta * const i_pFmt ) { m_pFmt = i_pFmt; };
153 
154     void NotifyChangeTxtNodeImpl();
155     void NotifyChangeTxtNode(SwTxtNode *const pTxtNode);
156 
157     ::com::sun::star::uno::WeakReference<
158         ::com::sun::star::rdf::XMetadatable> const& GetXMeta() const
159             { return m_wXMeta; }
160     void SetXMeta(::com::sun::star::uno::Reference<
161                     ::com::sun::star::rdf::XMetadatable> const& xMeta)
162             { m_wXMeta = xMeta; }
163 
164     // SwClient
165     virtual void Modify( const SfxPoolItem* pOld, const SfxPoolItem *pNew );
166 
167 public:
168     explicit Meta(SwFmtMeta * const i_pFmt = 0);
169     virtual ~Meta();
170 
171     // sfx2::Metadatable
172     virtual ::sfx2::IXmlIdRegistry& GetRegistry();
173     virtual bool IsInClipboard() const;
174     virtual bool IsInUndo() const;
175     virtual bool IsInContent() const;
176     virtual ::com::sun::star::uno::Reference<
177         ::com::sun::star::rdf::XMetadatable > MakeUnoObject();
178 };
179 
180 class MetaField
181     : public Meta
182 {
183 private:
184     friend class ::SwFmtMeta;
185     friend class ::SwXMetaField;
186     friend class ::sw::MetaFieldManager;
187 
188     sal_uInt32 m_nNumberFormat;
189     bool       m_bIsFixedLanguage;
190 
191     sal_uInt32 GetNumberFormat(::rtl::OUString const & rContent) const;
192     void SetNumberFormat(sal_uInt32 nNumberFormat);
193     bool IsFixedLanguage() const    { return m_bIsFixedLanguage; }
194     void SetIsFixedLanguage(bool b) { m_bIsFixedLanguage = b; }
195 
196     explicit MetaField(SwFmtMeta * const i_pFmt = 0,
197             const sal_uInt32 nNumberFormat = SAL_MAX_UINT32,
198             const bool bIsFixedLanguage = false );
199 
200 public:
201     /// get prefix/suffix from the RDF repository. @throws RuntimeException
202     void GetPrefixAndSuffix(
203         ::rtl::OUString *const o_pPrefix, ::rtl::OUString *const o_pSuffix);
204 };
205 
206 /** knows all meta-fields in the document. */
207 class MetaFieldManager
208     : private ::boost::noncopyable
209 {
210 private:
211     typedef ::std::vector< ::boost::weak_ptr<MetaField> > MetaFieldList_t;
212     MetaFieldList_t m_MetaFields;
213 
214 public:
215     MetaFieldManager();
216     ::boost::shared_ptr<MetaField> makeMetaField(
217                 SwFmtMeta * const i_pFmt = 0,
218                 const sal_uInt32 nNumberFormat = SAL_MAX_UINT32,
219                 const bool bIsFixedLanguage = false );
220     /// get all meta fields
221     ::std::vector< ::com::sun::star::uno::Reference<
222         ::com::sun::star::text::XTextField> > getMetaFields();
223 };
224 
225 } // namespace sw
226 
227 #endif // SW_FMTMETA_HXX
228 
229