Line data Source code
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 :
21 : #include <osl/mutex.hxx>
22 : #include <vcl/svapp.hxx>
23 :
24 : #include <unomid.h>
25 : #include <unotextrange.hxx>
26 : #include <unorefmark.hxx>
27 : #include <unotextcursor.hxx>
28 : #include <unomap.hxx>
29 : #include <unocrsr.hxx>
30 : #include <unoevtlstnr.hxx>
31 : #include <unocrsrhelper.hxx>
32 : #include <doc.hxx>
33 : #include <ndtxt.hxx>
34 : #include <fmtrfmrk.hxx>
35 : #include <txtrfmrk.hxx>
36 : #include <hints.hxx>
37 : #include <comphelper/servicehelper.hxx>
38 :
39 :
40 : using namespace ::com::sun::star;
41 : using ::rtl::OUString;
42 :
43 : /******************************************************************
44 : * SwXReferenceMark
45 : ******************************************************************/
46 0 : class SwXReferenceMark::Impl
47 : : public SwClient
48 : {
49 :
50 : public:
51 : SwEventListenerContainer m_ListenerContainer;
52 : bool m_bIsDescriptor;
53 : SwDoc * m_pDoc;
54 : const SwFmtRefMark * m_pMarkFmt;
55 : ::rtl::OUString m_sMarkName;
56 :
57 0 : Impl( SwXReferenceMark & rThis,
58 : SwDoc *const pDoc, SwFmtRefMark const*const pRefMark)
59 : : SwClient((pDoc) ? pDoc->GetUnoCallBack() : 0)
60 : , m_ListenerContainer(static_cast< ::cppu::OWeakObject* >(&rThis))
61 : // #i111177# unxsols4 (Sun C++ 5.9 SunOS_sparc) may generate wrong code
62 : , m_bIsDescriptor((0 == pRefMark) ? true : false)
63 : , m_pDoc(pDoc)
64 0 : , m_pMarkFmt(pRefMark)
65 : {
66 0 : if (pRefMark)
67 : {
68 0 : m_sMarkName = pRefMark->GetRefName();
69 : }
70 0 : }
71 :
72 0 : bool IsValid() const { return 0 != GetRegisteredIn(); }
73 : void InsertRefMark( SwPaM & rPam, SwXTextCursor const*const pCursor );
74 : void Invalidate();
75 : protected:
76 : // SwClient
77 : virtual void Modify( const SfxPoolItem* pOld, const SfxPoolItem *pNew);
78 :
79 : };
80 :
81 0 : void SwXReferenceMark::Impl::Invalidate()
82 : {
83 0 : if (IsValid())
84 : {
85 0 : const_cast<SwModify*>(GetRegisteredIn())->Remove(this);
86 : }
87 0 : m_ListenerContainer.Disposing();
88 0 : m_pDoc = 0;
89 0 : m_pMarkFmt = 0;
90 0 : }
91 :
92 0 : void SwXReferenceMark::Impl::Modify( const SfxPoolItem* pOld, const SfxPoolItem *pNew)
93 : {
94 0 : ClientModify(this, pOld, pNew);
95 :
96 0 : if (!GetRegisteredIn()) // removed => dispose
97 : {
98 0 : Invalidate();
99 : }
100 0 : else if (pOld)
101 : {
102 0 : switch (pOld->Which())
103 : {
104 : case RES_REFMARK_DELETED:
105 0 : if (static_cast<const void*>(m_pMarkFmt) ==
106 : static_cast<const SwPtrMsgPoolItem *>(pOld)->pObject)
107 : {
108 0 : Invalidate();
109 : }
110 0 : break;
111 : }
112 : }
113 0 : }
114 :
115 0 : SwXReferenceMark::SwXReferenceMark(
116 : SwDoc *const pDoc, SwFmtRefMark const*const pRefMark)
117 0 : : m_pImpl( new SwXReferenceMark::Impl(*this, pDoc, pRefMark) )
118 : {
119 0 : }
120 :
121 0 : SwXReferenceMark::~SwXReferenceMark()
122 : {
123 0 : }
124 :
125 : SwXReferenceMark *
126 0 : SwXReferenceMark::GetReferenceMark(
127 : SwModify const& /*rUnoCB*/, SwFmtRefMark const& /*rMarkFmt*/)
128 : {
129 : // #i105557#: do not iterate over the registered clients: race condition
130 : // to do this properly requires the SwXReferenceMark to register at the
131 : // SwFmtRefMark directly, not at the unocallback
132 0 : return 0;
133 : }
134 :
135 : SwXReferenceMark *
136 0 : SwXReferenceMark::CreateXReferenceMark(
137 : SwDoc & rDoc, SwFmtRefMark const& rMarkFmt)
138 : {
139 : SwXReferenceMark *const pXMark(
140 0 : GetReferenceMark(*rDoc.GetUnoCallBack(), rMarkFmt) );
141 : return (pXMark)
142 : ? pXMark
143 0 : : new SwXReferenceMark(&rDoc, &rMarkFmt);
144 : }
145 :
146 : namespace
147 : {
148 : class theSwXReferenceMarkUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theSwXReferenceMarkUnoTunnelId > {};
149 : }
150 :
151 258 : const uno::Sequence< sal_Int8 > & SwXReferenceMark::getUnoTunnelId()
152 : {
153 258 : return theSwXReferenceMarkUnoTunnelId::get().getSeq();
154 : }
155 :
156 : sal_Int64 SAL_CALL
157 0 : SwXReferenceMark::getSomething(const uno::Sequence< sal_Int8 >& rId)
158 : throw (uno::RuntimeException)
159 : {
160 0 : return ::sw::UnoTunnelImpl<SwXReferenceMark>(rId, this);
161 : }
162 :
163 0 : OUString SAL_CALL SwXReferenceMark::getImplementationName()
164 : throw (uno::RuntimeException)
165 : {
166 0 : return C2U("SwXReferenceMark");
167 : }
168 :
169 : static char const*const g_ServicesReferenceMark[] =
170 : {
171 : "com.sun.star.text.TextContent",
172 : "com.sun.star.text.ReferenceMark",
173 : };
174 :
175 : static const size_t g_nServicesReferenceMark(
176 : sizeof(g_ServicesReferenceMark)/sizeof(g_ServicesReferenceMark[0]));
177 :
178 : sal_Bool SAL_CALL
179 0 : SwXReferenceMark::supportsService(const OUString& rServiceName)
180 : throw (uno::RuntimeException)
181 : {
182 : return ::sw::SupportsServiceImpl(
183 0 : g_nServicesReferenceMark, g_ServicesReferenceMark, rServiceName);
184 : }
185 :
186 : uno::Sequence< OUString > SAL_CALL
187 0 : SwXReferenceMark::getSupportedServiceNames()
188 : throw (uno::RuntimeException)
189 : {
190 : return ::sw::GetSupportedServiceNamesImpl(
191 0 : g_nServicesReferenceMark, g_ServicesReferenceMark);
192 : }
193 :
194 : template<typename T> struct NotContainedIn
195 : {
196 : ::std::vector<T> const& m_rVector;
197 0 : explicit NotContainedIn(::std::vector<T> const& rVector)
198 0 : : m_rVector(rVector) { }
199 0 : bool operator() (T const& rT) {
200 : return ::std::find(m_rVector.begin(), m_rVector.end(), rT)
201 0 : == m_rVector.end();
202 : }
203 : };
204 :
205 0 : void SwXReferenceMark::Impl::InsertRefMark(SwPaM& rPam,
206 : SwXTextCursor const*const pCursor)
207 : {
208 : //! in some cases when this function is called the pDoc pointer member may have become
209 : //! invalid/deleted thus we obtain the document pointer from rPaM where it should always
210 : //! be valid.
211 0 : SwDoc *pDoc2 = rPam.GetDoc();
212 :
213 0 : UnoActionContext aCont(pDoc2);
214 0 : SwFmtRefMark aRefMark(m_sMarkName);
215 0 : bool bMark = *rPam.GetPoint() != *rPam.GetMark();
216 :
217 0 : const bool bForceExpandHints( (!bMark && pCursor)
218 0 : ? pCursor->IsAtEndOfMeta() : false );
219 : const SetAttrMode nInsertFlags = (bForceExpandHints)
220 : ? ( nsSetAttrMode::SETATTR_FORCEHINTEXPAND
221 : | nsSetAttrMode::SETATTR_DONTEXPAND)
222 0 : : nsSetAttrMode::SETATTR_DONTEXPAND;
223 :
224 0 : ::std::vector<SwTxtAttr *> oldMarks;
225 0 : if (bMark)
226 : {
227 : oldMarks = rPam.GetNode()->GetTxtNode()->GetTxtAttrsAt(
228 0 : rPam.GetPoint()->nContent.GetIndex(), RES_TXTATR_REFMARK);
229 : }
230 :
231 0 : pDoc2->InsertPoolItem( rPam, aRefMark, nInsertFlags );
232 :
233 0 : if( bMark && *rPam.GetPoint() > *rPam.GetMark())
234 : {
235 0 : rPam.Exchange();
236 : }
237 :
238 : // aRefMark was copied into the document pool; now retrieve real format...
239 0 : SwTxtAttr * pTxtAttr(0);
240 0 : if (bMark)
241 : {
242 : // #i107672#
243 : // ensure that we do not retrieve a different mark at the same position
244 : ::std::vector<SwTxtAttr *> const newMarks(
245 : rPam.GetNode()->GetTxtNode()->GetTxtAttrsAt(
246 0 : rPam.GetPoint()->nContent.GetIndex(), RES_TXTATR_REFMARK));
247 : ::std::vector<SwTxtAttr *>::const_iterator const iter(
248 : ::std::find_if(newMarks.begin(), newMarks.end(),
249 0 : NotContainedIn<SwTxtAttr *>(oldMarks)));
250 : OSL_ASSERT(newMarks.end() != iter);
251 0 : if (newMarks.end() != iter)
252 : {
253 0 : pTxtAttr = *iter;
254 0 : }
255 : }
256 : else
257 : {
258 0 : SwTxtNode *pTxtNd = rPam.GetNode()->GetTxtNode();
259 : OSL_ASSERT(pTxtNd);
260 : pTxtAttr = pTxtNd ? rPam.GetNode()->GetTxtNode()->GetTxtAttrForCharAt(
261 0 : rPam.GetPoint()->nContent.GetIndex() - 1, RES_TXTATR_REFMARK) : NULL;
262 : }
263 :
264 0 : if (!pTxtAttr)
265 : {
266 : throw uno::RuntimeException(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
267 0 : "SwXReferenceMark::InsertRefMark(): cannot insert attribute")), 0);
268 : }
269 :
270 0 : m_pMarkFmt = &pTxtAttr->GetRefMark();
271 :
272 0 : pDoc2->GetUnoCallBack()->Add(this);
273 0 : }
274 :
275 : void SAL_CALL
276 0 : SwXReferenceMark::attach(const uno::Reference< text::XTextRange > & xTextRange)
277 : throw (lang::IllegalArgumentException, uno::RuntimeException)
278 : {
279 0 : SolarMutexGuard aGuard;
280 :
281 0 : if (!m_pImpl->m_bIsDescriptor)
282 : {
283 0 : throw uno::RuntimeException();
284 : }
285 0 : uno::Reference<lang::XUnoTunnel> xRangeTunnel( xTextRange, uno::UNO_QUERY);
286 0 : SwXTextRange* pRange = 0;
287 0 : OTextCursorHelper* pCursor = 0;
288 0 : if(xRangeTunnel.is())
289 : {
290 0 : pRange = ::sw::UnoTunnelGetImplementation<SwXTextRange>(xRangeTunnel);
291 : pCursor =
292 0 : ::sw::UnoTunnelGetImplementation<OTextCursorHelper>(xRangeTunnel);
293 : }
294 : SwDoc *const pDocument =
295 0 : (pRange) ? pRange->GetDoc() : ((pCursor) ? pCursor->GetDoc() : 0);
296 0 : if (!pDocument)
297 : {
298 0 : throw lang::IllegalArgumentException();
299 : }
300 :
301 0 : SwUnoInternalPaM aPam(*pDocument);
302 : //das muss jetzt sal_True liefern
303 0 : ::sw::XTextRangeToSwPaM(aPam, xTextRange);
304 0 : m_pImpl->InsertRefMark(aPam, dynamic_cast<SwXTextCursor*>(pCursor));
305 0 : m_pImpl->m_bIsDescriptor = sal_False;
306 0 : m_pImpl->m_pDoc = pDocument;
307 0 : }
308 :
309 : uno::Reference< text::XTextRange > SAL_CALL
310 0 : SwXReferenceMark::getAnchor() throw (uno::RuntimeException)
311 : {
312 0 : SolarMutexGuard aGuard;
313 :
314 0 : if (m_pImpl->IsValid())
315 : {
316 : SwFmtRefMark const*const pNewMark =
317 0 : m_pImpl->m_pDoc->GetRefMark(m_pImpl->m_sMarkName);
318 0 : if (pNewMark && (pNewMark == m_pImpl->m_pMarkFmt))
319 : {
320 : SwTxtRefMark const*const pTxtMark =
321 0 : m_pImpl->m_pMarkFmt->GetTxtRefMark();
322 0 : if (pTxtMark &&
323 0 : (&pTxtMark->GetTxtNode().GetNodes() ==
324 0 : &m_pImpl->m_pDoc->GetNodes()))
325 : {
326 0 : SwTxtNode const& rTxtNode = pTxtMark->GetTxtNode();
327 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
328 0 : const ::std::auto_ptr<SwPaM> pPam( (pTxtMark->GetEnd())
329 0 : ? new SwPaM( rTxtNode, *pTxtMark->GetEnd(),
330 0 : rTxtNode, *pTxtMark->GetStart())
331 0 : : new SwPaM( rTxtNode, *pTxtMark->GetStart()) );
332 : SAL_WNODEPRECATED_DECLARATIONS_POP
333 :
334 : return SwXTextRange::CreateXTextRange(
335 0 : *m_pImpl->m_pDoc, *pPam->Start(), pPam->End());
336 : }
337 : }
338 : }
339 0 : return 0;
340 : }
341 :
342 0 : void SAL_CALL SwXReferenceMark::dispose() throw (uno::RuntimeException)
343 : {
344 0 : SolarMutexGuard aGuard;
345 0 : if (m_pImpl->IsValid())
346 : {
347 : SwFmtRefMark const*const pNewMark =
348 0 : m_pImpl->m_pDoc->GetRefMark(m_pImpl->m_sMarkName);
349 0 : if (pNewMark && (pNewMark == m_pImpl->m_pMarkFmt))
350 : {
351 : SwTxtRefMark const*const pTxtMark =
352 0 : m_pImpl->m_pMarkFmt->GetTxtRefMark();
353 0 : if (pTxtMark &&
354 0 : (&pTxtMark->GetTxtNode().GetNodes() ==
355 0 : &m_pImpl->m_pDoc->GetNodes()))
356 : {
357 0 : SwTxtNode const& rTxtNode = pTxtMark->GetTxtNode();
358 0 : xub_StrLen nStt = *pTxtMark->GetStart(),
359 0 : nEnd = pTxtMark->GetEnd() ? *pTxtMark->GetEnd()
360 0 : : nStt + 1;
361 :
362 0 : SwPaM aPam( rTxtNode, nStt, rTxtNode, nEnd );
363 0 : m_pImpl->m_pDoc->DeleteAndJoin( aPam );
364 : }
365 : }
366 : }
367 0 : else if (m_pImpl->m_bIsDescriptor)
368 : {
369 0 : m_pImpl->Invalidate();
370 0 : }
371 0 : }
372 :
373 0 : void SAL_CALL SwXReferenceMark::addEventListener(
374 : const uno::Reference< lang::XEventListener > & xListener)
375 : throw (uno::RuntimeException)
376 : {
377 0 : SolarMutexGuard g;
378 :
379 0 : if (!m_pImpl->IsValid())
380 : {
381 0 : throw uno::RuntimeException();
382 : }
383 0 : m_pImpl->m_ListenerContainer.AddListener(xListener);
384 0 : }
385 :
386 0 : void SAL_CALL SwXReferenceMark::removeEventListener(
387 : const uno::Reference< lang::XEventListener > & xListener)
388 : throw (uno::RuntimeException)
389 : {
390 0 : SolarMutexGuard g;
391 :
392 0 : if (!m_pImpl->IsValid() ||
393 0 : !m_pImpl->m_ListenerContainer.RemoveListener(xListener))
394 : {
395 0 : throw uno::RuntimeException();
396 0 : }
397 0 : }
398 :
399 0 : OUString SAL_CALL SwXReferenceMark::getName()
400 : throw (uno::RuntimeException)
401 : {
402 0 : SolarMutexGuard aGuard;
403 0 : if (!m_pImpl->IsValid() ||
404 0 : !m_pImpl->m_pDoc->GetRefMark(m_pImpl->m_sMarkName))
405 : {
406 0 : throw uno::RuntimeException();
407 : }
408 0 : return m_pImpl->m_sMarkName;
409 : }
410 :
411 0 : void SAL_CALL SwXReferenceMark::setName(const OUString& rName)
412 : throw (uno::RuntimeException)
413 : {
414 0 : SolarMutexGuard aGuard;
415 0 : if (m_pImpl->m_bIsDescriptor)
416 : {
417 0 : m_pImpl->m_sMarkName = rName;
418 : }
419 : else
420 : {
421 0 : if (!m_pImpl->IsValid()
422 0 : || !m_pImpl->m_pDoc->GetRefMark(m_pImpl->m_sMarkName)
423 0 : || m_pImpl->m_pDoc->GetRefMark(rName))
424 : {
425 0 : throw uno::RuntimeException();
426 : }
427 : SwFmtRefMark const*const pCurMark =
428 0 : m_pImpl->m_pDoc->GetRefMark(m_pImpl->m_sMarkName);
429 0 : if ((rName != m_pImpl->m_sMarkName)
430 0 : && pCurMark && (pCurMark == m_pImpl->m_pMarkFmt))
431 : {
432 0 : const UnoActionContext aCont(m_pImpl->m_pDoc);
433 : SwTxtRefMark const*const pTxtMark =
434 0 : m_pImpl->m_pMarkFmt->GetTxtRefMark();
435 0 : if (pTxtMark &&
436 0 : (&pTxtMark->GetTxtNode().GetNodes() ==
437 0 : &m_pImpl->m_pDoc->GetNodes()))
438 : {
439 0 : SwTxtNode const& rTxtNode = pTxtMark->GetTxtNode();
440 0 : xub_StrLen nStt = *pTxtMark->GetStart(),
441 0 : nEnd = pTxtMark->GetEnd() ? *pTxtMark->GetEnd()
442 0 : : nStt + 1;
443 :
444 0 : SwPaM aPam( rTxtNode, nStt, rTxtNode, nEnd );
445 : // deletes the m_pImpl->m_pDoc member in the SwXReferenceMark!
446 0 : m_pImpl->m_pDoc->DeleteAndJoin( aPam );
447 : // The aPam will keep the correct and functional doc though
448 :
449 0 : m_pImpl->m_sMarkName = rName;
450 : //create a new one
451 0 : m_pImpl->InsertRefMark( aPam, 0 );
452 0 : m_pImpl->m_pDoc = aPam.GetDoc();
453 0 : }
454 : }
455 0 : }
456 0 : }
457 :
458 : uno::Reference< beans::XPropertySetInfo > SAL_CALL
459 0 : SwXReferenceMark::getPropertySetInfo() throw (uno::RuntimeException)
460 : {
461 0 : SolarMutexGuard g;
462 :
463 : static uno::Reference< beans::XPropertySetInfo > xRef =
464 : aSwMapProvider.GetPropertySet(PROPERTY_MAP_PARAGRAPH_EXTENSIONS)
465 0 : ->getPropertySetInfo();
466 0 : return xRef;
467 : }
468 :
469 0 : void SAL_CALL SwXReferenceMark::setPropertyValue(
470 : const OUString& /*rPropertyName*/, const uno::Any& /*rValue*/ )
471 : throw (beans::UnknownPropertyException, beans::PropertyVetoException,
472 : lang::IllegalArgumentException, lang::WrappedTargetException,
473 : uno::RuntimeException)
474 : {
475 0 : throw lang::IllegalArgumentException();
476 : }
477 :
478 : uno::Any SAL_CALL
479 0 : SwXReferenceMark::getPropertyValue(const OUString& rPropertyName)
480 : throw (beans::UnknownPropertyException, lang::WrappedTargetException,
481 : uno::RuntimeException)
482 : {
483 : // does not seem to need SolarMutex
484 0 : uno::Any aRet;
485 0 : if (! ::sw::GetDefaultTextContentValue(aRet, rPropertyName))
486 : {
487 0 : throw beans::UnknownPropertyException();
488 : }
489 0 : return aRet;
490 : }
491 :
492 0 : void SAL_CALL SwXReferenceMark::addPropertyChangeListener(
493 : const OUString& /*rPropertyName*/,
494 : const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/)
495 : throw (beans::UnknownPropertyException, lang::WrappedTargetException,
496 : uno::RuntimeException)
497 : {
498 : OSL_FAIL("SwXReferenceMark::addPropertyChangeListener(): not implemented");
499 0 : }
500 :
501 0 : void SAL_CALL SwXReferenceMark::removePropertyChangeListener(
502 : const OUString& /*rPropertyName*/,
503 : const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/)
504 : throw (beans::UnknownPropertyException, lang::WrappedTargetException,
505 : uno::RuntimeException)
506 : {
507 : OSL_FAIL("SwXReferenceMark::removePropertyChangeListener(): not implemented");
508 0 : }
509 :
510 0 : void SAL_CALL SwXReferenceMark::addVetoableChangeListener(
511 : const OUString& /*rPropertyName*/,
512 : const uno::Reference< beans::XVetoableChangeListener >& /*xListener*/)
513 : throw (beans::UnknownPropertyException, lang::WrappedTargetException,
514 : uno::RuntimeException)
515 : {
516 : OSL_FAIL("SwXReferenceMark::addVetoableChangeListener(): not implemented");
517 0 : }
518 :
519 0 : void SAL_CALL SwXReferenceMark::removeVetoableChangeListener(
520 : const OUString& /*rPropertyName*/,
521 : const uno::Reference< beans::XVetoableChangeListener >& /*xListener*/)
522 : throw (beans::UnknownPropertyException, lang::WrappedTargetException,
523 : uno::RuntimeException)
524 : {
525 : OSL_FAIL("SwXReferenceMark::removeVetoableChangeListener(): not implemented");
526 0 : }
527 :
528 : #include <com/sun/star/lang/DisposedException.hpp>
529 : #include <unometa.hxx>
530 : #include <unotext.hxx>
531 : #include <unoport.hxx>
532 : #include <txtatr.hxx>
533 : #include <fmtmeta.hxx>
534 : #include <docsh.hxx>
535 :
536 : /******************************************************************
537 : * SwXMetaText
538 : ******************************************************************/
539 0 : class SwXMetaText
540 : : public SwXText
541 : {
542 : private:
543 : SwXMeta & m_rMeta;
544 :
545 : virtual void PrepareForAttach(uno::Reference< text::XTextRange > & xRange,
546 : const SwPaM & rPam);
547 :
548 : virtual bool CheckForOwnMemberMeta(const SwPaM & rPam, const bool bAbsorb)
549 : throw (lang::IllegalArgumentException, uno::RuntimeException);
550 :
551 : protected:
552 : virtual const SwStartNode *GetStartNode() const;
553 : virtual uno::Reference< text::XTextCursor >
554 : CreateCursor() throw (uno::RuntimeException);
555 :
556 : public:
557 : SwXMetaText(SwDoc & rDoc, SwXMeta & rMeta);
558 :
559 : /// make available for SwXMeta
560 0 : void Invalidate() { SwXText::Invalidate(); };
561 :
562 : // XInterface
563 0 : virtual void SAL_CALL acquire() throw()
564 0 : { OSL_FAIL("ERROR: SwXMetaText::acquire"); }
565 0 : virtual void SAL_CALL release() throw()
566 0 : { OSL_FAIL("ERROR: SwXMetaText::release"); }
567 :
568 : // XTypeProvider
569 : virtual uno::Sequence< sal_Int8 > SAL_CALL
570 : getImplementationId() throw (uno::RuntimeException);
571 :
572 : // XText
573 : virtual uno::Reference< text::XTextCursor > SAL_CALL
574 : createTextCursor() throw (uno::RuntimeException);
575 : virtual uno::Reference< text::XTextCursor > SAL_CALL
576 : createTextCursorByRange(
577 : const uno::Reference< text::XTextRange > & xTextPosition)
578 : throw (uno::RuntimeException);
579 :
580 : SwXMeta & GetXMeta() { return m_rMeta; }
581 :
582 : };
583 :
584 0 : SwXMetaText::SwXMetaText(SwDoc & rDoc, SwXMeta & rMeta)
585 : : SwXText(&rDoc, CURSOR_META)
586 0 : , m_rMeta(rMeta)
587 : {
588 0 : }
589 :
590 0 : const SwStartNode *SwXMetaText::GetStartNode() const
591 : {
592 : SwXText const * const pParent(
593 0 : dynamic_cast<SwXText*>(m_rMeta.GetParentText().get()));
594 0 : return (pParent) ? pParent->GetStartNode() : 0;
595 : }
596 :
597 0 : void SwXMetaText::PrepareForAttach( uno::Reference<text::XTextRange> & xRange,
598 : const SwPaM & rPam)
599 : {
600 : // create a new cursor to prevent modifying SwXTextRange
601 : xRange = static_cast<text::XWordCursor*>(
602 0 : new SwXTextCursor(*GetDoc(), &m_rMeta, CURSOR_META, *rPam.GetPoint(),
603 0 : (rPam.HasMark()) ? rPam.GetMark() : 0));
604 0 : }
605 :
606 0 : bool SwXMetaText::CheckForOwnMemberMeta(const SwPaM & rPam, const bool bAbsorb)
607 : throw (lang::IllegalArgumentException, uno::RuntimeException)
608 : {
609 0 : return m_rMeta.CheckForOwnMemberMeta(rPam, bAbsorb);
610 : }
611 :
612 0 : uno::Reference< text::XTextCursor > SwXMetaText::CreateCursor()
613 : throw (uno::RuntimeException)
614 : {
615 0 : uno::Reference< text::XTextCursor > xRet;
616 0 : if (IsValid())
617 : {
618 : SwTxtNode * pTxtNode;
619 : xub_StrLen nMetaStart;
620 : xub_StrLen nMetaEnd;
621 : const bool bSuccess(
622 0 : m_rMeta.SetContentRange(pTxtNode, nMetaStart, nMetaEnd) );
623 0 : if (bSuccess)
624 : {
625 0 : SwPosition aPos(*pTxtNode, nMetaStart);
626 : xRet = static_cast<text::XWordCursor*>(
627 0 : new SwXTextCursor(*GetDoc(), &m_rMeta, CURSOR_META, aPos));
628 : }
629 : }
630 0 : return xRet;
631 : }
632 :
633 : uno::Sequence<sal_Int8> SAL_CALL
634 0 : SwXMetaText::getImplementationId() throw (uno::RuntimeException)
635 : {
636 0 : return m_rMeta.getImplementationId();
637 : }
638 :
639 : // XText
640 : uno::Reference< text::XTextCursor > SAL_CALL
641 0 : SwXMetaText::createTextCursor() throw (uno::RuntimeException)
642 : {
643 0 : return CreateCursor();
644 : }
645 :
646 : uno::Reference< text::XTextCursor > SAL_CALL
647 0 : SwXMetaText::createTextCursorByRange(
648 : const uno::Reference<text::XTextRange> & xTextPosition)
649 : throw (uno::RuntimeException)
650 : {
651 0 : const uno::Reference<text::XTextCursor> xCursor( CreateCursor() );
652 0 : xCursor->gotoRange(xTextPosition, sal_False);
653 0 : return xCursor;
654 : }
655 :
656 : /******************************************************************
657 : * SwXMeta
658 : ******************************************************************/
659 : // the Meta has a cached list of text portions for its contents
660 : // this list is created by SwXTextPortionEnumeration
661 : // the Meta listens at the SwTxtNode and throws away the cache when it changes
662 0 : class SwXMeta::Impl
663 : : public SwClient
664 : {
665 :
666 : public:
667 :
668 : SwEventListenerContainer m_ListenerContainer;
669 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
670 : ::std::auto_ptr<const TextRangeList_t> m_pTextPortions;
671 : SAL_WNODEPRECATED_DECLARATIONS_POP
672 : // 3 possible states: not attached, attached, disposed
673 : bool m_bIsDisposed;
674 : bool m_bIsDescriptor;
675 : uno::Reference<text::XText> m_xParentText;
676 : SwXMetaText m_Text;
677 :
678 0 : Impl( SwXMeta & rThis, SwDoc & rDoc,
679 : ::sw::Meta * const pMeta,
680 : uno::Reference<text::XText> const& xParentText,
681 : TextRangeList_t const * const pPortions)
682 : : SwClient(pMeta)
683 : , m_ListenerContainer(static_cast< ::cppu::OWeakObject* >(&rThis))
684 : , m_pTextPortions( pPortions )
685 : , m_bIsDisposed( false )
686 : // #i111177# unxsols4 (Sun C++ 5.9 SunOS_sparc) may generate wrong code
687 : , m_bIsDescriptor((0 == pMeta) ? true : false)
688 : , m_xParentText(xParentText)
689 0 : , m_Text(rDoc, rThis)
690 : {
691 0 : }
692 :
693 : inline const ::sw::Meta * GetMeta() const;
694 : // only for SwXMetaField!
695 : inline const ::sw::MetaField * GetMetaField() const;
696 : protected:
697 : // SwClient
698 : virtual void Modify( const SfxPoolItem* pOld, const SfxPoolItem *pNew);
699 :
700 : };
701 :
702 0 : inline const ::sw::Meta * SwXMeta::Impl::GetMeta() const
703 : {
704 0 : return static_cast< const ::sw::Meta * >(GetRegisteredIn());
705 : }
706 :
707 : // SwModify
708 0 : void SwXMeta::Impl::Modify( const SfxPoolItem* pOld, const SfxPoolItem *pNew )
709 : {
710 0 : m_pTextPortions.reset(); // throw away cache (SwTxtNode changed)
711 :
712 0 : ClientModify(this, pOld, pNew);
713 :
714 0 : if (!GetRegisteredIn()) // removed => dispose
715 : {
716 0 : m_ListenerContainer.Disposing();
717 0 : m_bIsDisposed = true;
718 0 : m_Text.Invalidate();
719 : }
720 0 : }
721 :
722 0 : uno::Reference<text::XText> SwXMeta::GetParentText() const
723 : {
724 0 : return m_pImpl->m_xParentText;
725 : }
726 :
727 0 : SwXMeta::SwXMeta(SwDoc *const pDoc, ::sw::Meta *const pMeta,
728 : uno::Reference<text::XText> const& xParentText,
729 : TextRangeList_t const*const pPortions)
730 0 : : m_pImpl( new SwXMeta::Impl(*this, *pDoc, pMeta, xParentText, pPortions) )
731 : {
732 0 : }
733 :
734 0 : SwXMeta::SwXMeta(SwDoc *const pDoc)
735 0 : : m_pImpl( new SwXMeta::Impl(*this, *pDoc, 0, 0, 0) )
736 : {
737 0 : }
738 :
739 0 : SwXMeta::~SwXMeta()
740 : {
741 0 : }
742 :
743 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
744 : uno::Reference<rdf::XMetadatable>
745 0 : SwXMeta::CreateXMeta(::sw::Meta & rMeta,
746 : uno::Reference<text::XText> const& i_xParent,
747 : ::std::auto_ptr<TextRangeList_t const> pPortions)
748 : {
749 : // re-use existing SwXMeta
750 : // #i105557#: do not iterate over the registered clients: race condition
751 0 : uno::Reference<rdf::XMetadatable> xMeta(rMeta.GetXMeta());
752 0 : if (xMeta.is())
753 : {
754 0 : if (pPortions.get()) // set cache in the XMeta to the given portions
755 : {
756 0 : const uno::Reference<lang::XUnoTunnel> xUT(xMeta, uno::UNO_QUERY);
757 : SwXMeta *const pXMeta(
758 0 : ::sw::UnoTunnelGetImplementation<SwXMeta>(xUT));
759 : OSL_ENSURE(pXMeta, "no pXMeta?");
760 : // NB: the meta must always be created with the complete content
761 : // if SwXTextPortionEnumeration is created for a selection,
762 : // it must be checked that the Meta is contained in the selection!
763 0 : pXMeta->m_pImpl->m_pTextPortions = pPortions;
764 : // ??? is this necessary?
765 0 : if (pXMeta->m_pImpl->m_xParentText.get() != i_xParent.get())
766 : {
767 : OSL_FAIL("SwXMeta with different parent?");
768 0 : pXMeta->m_pImpl->m_xParentText.set(i_xParent);
769 0 : }
770 : }
771 0 : return xMeta;
772 : }
773 :
774 : // create new SwXMeta
775 0 : SwTxtNode * const pTxtNode( rMeta.GetTxtNode() );
776 : OSL_ENSURE(pTxtNode, "CreateXMeta: no text node?");
777 0 : if (!pTxtNode) { return 0; }
778 0 : uno::Reference<text::XText> xParentText(i_xParent);
779 0 : if (!xParentText.is())
780 : {
781 0 : SwTxtMeta * const pTxtAttr( rMeta.GetTxtAttr() );
782 : OSL_ENSURE(pTxtAttr, "CreateXMeta: no text attr?");
783 0 : if (!pTxtAttr) { return 0; }
784 0 : const SwPosition aPos(*pTxtNode, *pTxtAttr->GetStart());
785 0 : xParentText.set( ::sw::CreateParentXText(*pTxtNode->GetDoc(), aPos) );
786 : }
787 0 : if (!xParentText.is()) { return 0; }
788 0 : SwXMeta *const pXMeta( (RES_TXTATR_META == rMeta.GetFmtMeta()->Which())
789 0 : ? new SwXMeta (pTxtNode->GetDoc(), &rMeta, xParentText,
790 0 : pPortions.release()) // temporarily un-auto_ptr :-(
791 0 : : new SwXMetaField(pTxtNode->GetDoc(), &rMeta, xParentText,
792 0 : pPortions.release()));
793 : // this is why the constructor is private: need to acquire pXMeta here
794 0 : xMeta.set(pXMeta);
795 : // in order to initialize the weak pointer cache in the core object
796 0 : rMeta.SetXMeta(xMeta);
797 0 : return xMeta;
798 : }
799 : SAL_WNODEPRECATED_DECLARATIONS_POP
800 :
801 0 : bool SwXMeta::SetContentRange(
802 : SwTxtNode *& rpNode, xub_StrLen & rStart, xub_StrLen & rEnd ) const
803 : {
804 0 : ::sw::Meta const * const pMeta( m_pImpl->GetMeta() );
805 0 : if (pMeta)
806 : {
807 0 : SwTxtMeta const * const pTxtAttr( pMeta->GetTxtAttr() );
808 0 : if (pTxtAttr)
809 : {
810 0 : rpNode = pMeta->GetTxtNode();
811 0 : if (rpNode)
812 : {
813 : // rStart points at the first position _within_ the meta!
814 0 : rStart = *pTxtAttr->GetStart() + 1;
815 0 : rEnd = *pTxtAttr->GetEnd();
816 0 : return true;
817 : }
818 : }
819 : }
820 0 : return false;
821 : }
822 :
823 0 : bool SwXMeta::CheckForOwnMemberMeta(const SwPaM & rPam, const bool bAbsorb)
824 : throw (lang::IllegalArgumentException, uno::RuntimeException)
825 : {
826 : SwTxtNode * pTxtNode;
827 : xub_StrLen nMetaStart;
828 : xub_StrLen nMetaEnd;
829 0 : const bool bSuccess( SetContentRange(pTxtNode, nMetaStart, nMetaEnd) );
830 : OSL_ENSURE(bSuccess, "no pam?");
831 0 : if (!bSuccess)
832 0 : throw lang::DisposedException();
833 :
834 0 : SwPosition const * const pStartPos( rPam.Start() );
835 0 : if (&pStartPos->nNode.GetNode() != pTxtNode)
836 : {
837 : throw lang::IllegalArgumentException(
838 : C2U("trying to insert into a nesting text content, but start "
839 : "of text range not in same paragraph as text content"),
840 0 : 0, 0);
841 : }
842 0 : bool bForceExpandHints(false);
843 0 : const xub_StrLen nStartPos(pStartPos->nContent.GetIndex());
844 : // not <= but < because nMetaStart is behind dummy char!
845 : // not >= but > because == means insert at end!
846 0 : if ((nStartPos < nMetaStart) || (nStartPos > nMetaEnd))
847 : {
848 : throw lang::IllegalArgumentException(
849 : C2U("trying to insert into a nesting text content, but start "
850 : "of text range not inside text content"),
851 0 : 0, 0);
852 : }
853 0 : else if (nStartPos == nMetaEnd)
854 : {
855 0 : bForceExpandHints = true;
856 : }
857 0 : if (rPam.HasMark() && bAbsorb)
858 : {
859 0 : SwPosition const * const pEndPos( rPam.End() );
860 0 : if (&pEndPos->nNode.GetNode() != pTxtNode)
861 : {
862 : throw lang::IllegalArgumentException(
863 : C2U("trying to insert into a nesting text content, but end "
864 : "of text range not in same paragraph as text content"),
865 0 : 0, 0);
866 : }
867 0 : const xub_StrLen nEndPos(pEndPos->nContent.GetIndex());
868 : // not <= but < because nMetaStart is behind dummy char!
869 : // not >= but > because == means insert at end!
870 0 : if ((nEndPos < nMetaStart) || (nEndPos > nMetaEnd))
871 : {
872 : throw lang::IllegalArgumentException(
873 : C2U("trying to insert into a nesting text content, but end "
874 : "of text range not inside text content"),
875 0 : 0, 0);
876 : }
877 0 : else if (nEndPos == nMetaEnd)
878 : {
879 0 : bForceExpandHints = true;
880 : }
881 : }
882 0 : return bForceExpandHints;
883 : }
884 :
885 : namespace
886 : {
887 : class theSwXMetaUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theSwXMetaUnoTunnelId > {};
888 : }
889 :
890 258 : const uno::Sequence< sal_Int8 > & SwXMeta::getUnoTunnelId()
891 : {
892 258 : return theSwXMetaUnoTunnelId::get().getSeq();
893 : }
894 :
895 : // XUnoTunnel
896 : sal_Int64 SAL_CALL
897 0 : SwXMeta::getSomething( const uno::Sequence< sal_Int8 > & i_rId )
898 : throw (uno::RuntimeException)
899 : {
900 0 : return ::sw::UnoTunnelImpl<SwXMeta>(i_rId, this);
901 : }
902 :
903 : // XServiceInfo
904 : ::rtl::OUString SAL_CALL
905 0 : SwXMeta::getImplementationName() throw (uno::RuntimeException)
906 : {
907 0 : return C2U("SwXMeta");
908 : }
909 :
910 : static char const*const g_ServicesMeta[] =
911 : {
912 : "com.sun.star.text.TextContent",
913 : "com.sun.star.text.InContentMetadata",
914 : };
915 :
916 : static const size_t g_nServicesMeta(
917 : sizeof(g_ServicesMeta)/sizeof(g_ServicesMeta[0]));
918 :
919 : sal_Bool SAL_CALL
920 0 : SwXMeta::supportsService(const ::rtl::OUString& rServiceName)
921 : throw (uno::RuntimeException)
922 : {
923 : return ::sw::SupportsServiceImpl(
924 0 : g_nServicesMeta, g_ServicesMeta, rServiceName);
925 : }
926 :
927 : uno::Sequence< ::rtl::OUString > SAL_CALL
928 0 : SwXMeta::getSupportedServiceNames() throw (uno::RuntimeException)
929 : {
930 0 : return ::sw::GetSupportedServiceNamesImpl(g_nServicesMeta, g_ServicesMeta);
931 : }
932 :
933 : // XComponent
934 : void SAL_CALL
935 0 : SwXMeta::addEventListener(
936 : uno::Reference< lang::XEventListener> const & xListener )
937 : throw (uno::RuntimeException)
938 : {
939 0 : SolarMutexGuard g;
940 :
941 0 : m_pImpl->m_ListenerContainer.AddListener(xListener);
942 0 : if (m_pImpl->m_bIsDisposed)
943 : {
944 0 : m_pImpl->m_ListenerContainer.Disposing();
945 0 : }
946 0 : }
947 :
948 : void SAL_CALL
949 0 : SwXMeta::removeEventListener(
950 : uno::Reference< lang::XEventListener> const & xListener )
951 : throw (uno::RuntimeException)
952 : {
953 0 : SolarMutexGuard g;
954 :
955 0 : if (!m_pImpl->m_bIsDisposed)
956 : {
957 0 : m_pImpl->m_ListenerContainer.RemoveListener(xListener);
958 0 : }
959 0 : }
960 :
961 : void SAL_CALL
962 0 : SwXMeta::dispose() throw (uno::RuntimeException)
963 : {
964 0 : SolarMutexGuard g;
965 :
966 0 : if (m_pImpl->m_bIsDescriptor)
967 : {
968 0 : m_pImpl->m_pTextPortions.reset();
969 0 : m_pImpl->m_ListenerContainer.Disposing();
970 0 : m_pImpl->m_bIsDisposed = true;
971 0 : m_pImpl->m_Text.Invalidate();
972 : }
973 0 : else if (!m_pImpl->m_bIsDisposed)
974 : {
975 : SwTxtNode * pTxtNode;
976 : xub_StrLen nMetaStart;
977 : xub_StrLen nMetaEnd;
978 0 : const bool bSuccess(SetContentRange(pTxtNode, nMetaStart, nMetaEnd));
979 : OSL_ENSURE(bSuccess, "no pam?");
980 0 : if (bSuccess)
981 : {
982 : // -1 because of CH_TXTATR
983 0 : SwPaM aPam( *pTxtNode, nMetaStart - 1, *pTxtNode, nMetaEnd );
984 0 : SwDoc * const pDoc( pTxtNode->GetDoc() );
985 0 : pDoc->DeleteAndJoin( aPam );
986 :
987 : // removal should call Modify and do the dispose
988 0 : OSL_ENSURE(m_pImpl->m_bIsDisposed, "zombie meta");
989 : }
990 0 : }
991 0 : }
992 :
993 : void SAL_CALL
994 0 : SwXMeta::AttachImpl(const uno::Reference< text::XTextRange > & i_xTextRange,
995 : const sal_uInt16 i_nWhich)
996 : throw (lang::IllegalArgumentException, uno::RuntimeException)
997 : {
998 0 : SolarMutexGuard g;
999 :
1000 0 : if (m_pImpl->m_bIsDisposed)
1001 : {
1002 0 : throw lang::DisposedException();
1003 : }
1004 0 : if (!m_pImpl->m_bIsDescriptor)
1005 : {
1006 : throw uno::RuntimeException(
1007 : rtl::OUString("SwXMeta::attach(): already attached"),
1008 0 : static_cast< ::cppu::OWeakObject* >(this));
1009 : }
1010 :
1011 0 : uno::Reference<lang::XUnoTunnel> xRangeTunnel(i_xTextRange, uno::UNO_QUERY);
1012 0 : if (!xRangeTunnel.is())
1013 : {
1014 : throw lang::IllegalArgumentException(
1015 : rtl::OUString("SwXMeta::attach(): argument is no XUnoTunnel"),
1016 0 : static_cast< ::cppu::OWeakObject* >(this), 0);
1017 : }
1018 : SwXTextRange *const pRange(
1019 0 : ::sw::UnoTunnelGetImplementation<SwXTextRange>(xRangeTunnel));
1020 : OTextCursorHelper *const pCursor( (pRange) ? 0 :
1021 0 : ::sw::UnoTunnelGetImplementation<OTextCursorHelper>(xRangeTunnel));
1022 0 : if (!pRange && !pCursor)
1023 : {
1024 : throw lang::IllegalArgumentException(
1025 : rtl::OUString("SwXMeta::attach(): argument not supported type"),
1026 0 : static_cast< ::cppu::OWeakObject* >(this), 0);
1027 : }
1028 :
1029 : SwDoc * const pDoc(
1030 0 : pRange ? pRange->GetDoc() : pCursor ? pCursor->GetDoc() : 0 );
1031 0 : if (!pDoc)
1032 : {
1033 : throw lang::IllegalArgumentException(
1034 : rtl::OUString("SwXMeta::attach(): argument has no SwDoc"),
1035 0 : static_cast< ::cppu::OWeakObject* >(this), 0);
1036 : }
1037 :
1038 0 : SwUnoInternalPaM aPam(*pDoc);
1039 0 : ::sw::XTextRangeToSwPaM(aPam, i_xTextRange);
1040 :
1041 0 : UnoActionContext aContext(pDoc);
1042 :
1043 : SwXTextCursor const*const pTextCursor(
1044 0 : dynamic_cast<SwXTextCursor*>(pCursor));
1045 : const bool bForceExpandHints((pTextCursor)
1046 0 : ? pTextCursor->IsAtEndOfMeta() : false);
1047 : const SetAttrMode nInsertFlags( (bForceExpandHints)
1048 : ? ( nsSetAttrMode::SETATTR_FORCEHINTEXPAND
1049 : | nsSetAttrMode::SETATTR_DONTEXPAND)
1050 0 : : nsSetAttrMode::SETATTR_DONTEXPAND );
1051 :
1052 : const ::boost::shared_ptr< ::sw::Meta> pMeta( (RES_TXTATR_META == i_nWhich)
1053 0 : ? ::boost::shared_ptr< ::sw::Meta>( new ::sw::Meta() )
1054 : : ::boost::shared_ptr< ::sw::Meta>(
1055 0 : pDoc->GetMetaFieldManager().makeMetaField()) );
1056 0 : SwFmtMeta meta(pMeta, i_nWhich); // this is cloned by Insert!
1057 0 : const bool bSuccess( pDoc->InsertPoolItem( aPam, meta, nInsertFlags ) );
1058 0 : SwTxtAttr * const pTxtAttr( pMeta->GetTxtAttr() );
1059 0 : if (!bSuccess)
1060 : {
1061 : throw lang::IllegalArgumentException(
1062 : rtl::OUString("SwXMeta::attach(): cannot create meta: range invalid?"),
1063 0 : static_cast< ::cppu::OWeakObject* >(this), 1);
1064 : }
1065 0 : if (!pTxtAttr)
1066 : {
1067 : OSL_FAIL("meta inserted, but has no text attribute?");
1068 : throw uno::RuntimeException(
1069 : rtl::OUString("SwXMeta::attach(): cannot create meta"),
1070 0 : static_cast< ::cppu::OWeakObject* >(this));
1071 : }
1072 :
1073 0 : pMeta->Add(m_pImpl.get());
1074 0 : pMeta->SetXMeta(uno::Reference<rdf::XMetadatable>(this));
1075 :
1076 0 : m_pImpl->m_xParentText = ::sw::CreateParentXText(*pDoc, *aPam.GetPoint());
1077 :
1078 0 : m_pImpl->m_bIsDescriptor = false;
1079 0 : }
1080 :
1081 : // XTextContent
1082 : void SAL_CALL
1083 0 : SwXMeta::attach(const uno::Reference< text::XTextRange > & i_xTextRange)
1084 : throw (lang::IllegalArgumentException, uno::RuntimeException)
1085 : {
1086 0 : return SwXMeta::AttachImpl(i_xTextRange, RES_TXTATR_META);
1087 : }
1088 :
1089 : uno::Reference< text::XTextRange > SAL_CALL
1090 0 : SwXMeta::getAnchor() throw (uno::RuntimeException)
1091 : {
1092 0 : SolarMutexGuard g;
1093 :
1094 0 : if (m_pImpl->m_bIsDisposed)
1095 : {
1096 0 : throw lang::DisposedException();
1097 : }
1098 0 : if (m_pImpl->m_bIsDescriptor)
1099 : {
1100 : throw uno::RuntimeException(
1101 : rtl::OUString("SwXMeta::getAnchor(): not inserted"),
1102 0 : static_cast< ::cppu::OWeakObject* >(this));
1103 : }
1104 :
1105 : SwTxtNode * pTxtNode;
1106 : xub_StrLen nMetaStart;
1107 : xub_StrLen nMetaEnd;
1108 0 : const bool bSuccess(SetContentRange(pTxtNode, nMetaStart, nMetaEnd));
1109 : OSL_ENSURE(bSuccess, "no pam?");
1110 0 : if (!bSuccess)
1111 : {
1112 : throw lang::DisposedException(
1113 : rtl::OUString("SwXMeta::getAnchor(): not attached"),
1114 0 : static_cast< ::cppu::OWeakObject* >(this));
1115 : }
1116 :
1117 0 : const SwPosition start(*pTxtNode, nMetaStart - 1); // -1 due to CH_TXTATR
1118 0 : const SwPosition end(*pTxtNode, nMetaEnd);
1119 0 : return SwXTextRange::CreateXTextRange(*pTxtNode->GetDoc(), start, &end);
1120 : }
1121 :
1122 : // XTextRange
1123 : uno::Reference< text::XText > SAL_CALL
1124 0 : SwXMeta::getText() throw (uno::RuntimeException)
1125 : {
1126 0 : SolarMutexGuard g;
1127 0 : return this;
1128 : }
1129 :
1130 : uno::Reference< text::XTextRange > SAL_CALL
1131 0 : SwXMeta::getStart() throw (uno::RuntimeException)
1132 : {
1133 0 : SolarMutexGuard g;
1134 0 : return m_pImpl->m_Text.getStart();
1135 : }
1136 :
1137 : uno::Reference< text::XTextRange > SAL_CALL
1138 0 : SwXMeta::getEnd() throw (uno::RuntimeException)
1139 : {
1140 0 : SolarMutexGuard g;
1141 0 : return m_pImpl->m_Text.getEnd();
1142 : }
1143 :
1144 : rtl::OUString SAL_CALL
1145 0 : SwXMeta::getString() throw (uno::RuntimeException)
1146 : {
1147 0 : SolarMutexGuard g;
1148 0 : return m_pImpl->m_Text.getString();
1149 : }
1150 :
1151 : void SAL_CALL
1152 0 : SwXMeta::setString(const rtl::OUString& rString) throw (uno::RuntimeException)
1153 : {
1154 0 : SolarMutexGuard g;
1155 0 : return m_pImpl->m_Text.setString(rString);
1156 : }
1157 :
1158 : // XSimpleText
1159 : uno::Reference< text::XTextCursor > SAL_CALL
1160 0 : SwXMeta::createTextCursor() throw (uno::RuntimeException)
1161 : {
1162 0 : SolarMutexGuard g;
1163 0 : return m_pImpl->m_Text.createTextCursor();
1164 : }
1165 :
1166 : uno::Reference< text::XTextCursor > SAL_CALL
1167 0 : SwXMeta::createTextCursorByRange(
1168 : const uno::Reference<text::XTextRange> & xTextPosition)
1169 : throw (uno::RuntimeException)
1170 : {
1171 0 : SolarMutexGuard g;
1172 0 : return m_pImpl->m_Text.createTextCursorByRange(xTextPosition);
1173 : }
1174 :
1175 : void SAL_CALL
1176 0 : SwXMeta::insertString(const uno::Reference<text::XTextRange> & xRange,
1177 : const rtl::OUString& rString, sal_Bool bAbsorb)
1178 : throw (uno::RuntimeException)
1179 : {
1180 0 : SolarMutexGuard g;
1181 0 : return m_pImpl->m_Text.insertString(xRange, rString, bAbsorb);
1182 : }
1183 :
1184 : void SAL_CALL
1185 0 : SwXMeta::insertControlCharacter(const uno::Reference<text::XTextRange> & xRange,
1186 : sal_Int16 nControlCharacter, sal_Bool bAbsorb)
1187 : throw (lang::IllegalArgumentException, uno::RuntimeException)
1188 : {
1189 0 : SolarMutexGuard g;
1190 0 : return m_pImpl->m_Text.insertControlCharacter(xRange, nControlCharacter,
1191 0 : bAbsorb);
1192 : }
1193 :
1194 : // XText
1195 : void SAL_CALL
1196 0 : SwXMeta::insertTextContent( const uno::Reference<text::XTextRange> & xRange,
1197 : const uno::Reference<text::XTextContent> & xContent, sal_Bool bAbsorb)
1198 : throw (lang::IllegalArgumentException, uno::RuntimeException)
1199 : {
1200 0 : SolarMutexGuard g;
1201 0 : return m_pImpl->m_Text.insertTextContent(xRange, xContent, bAbsorb);
1202 : }
1203 :
1204 : void SAL_CALL
1205 0 : SwXMeta::removeTextContent(
1206 : const uno::Reference< text::XTextContent > & xContent)
1207 : throw (container::NoSuchElementException, uno::RuntimeException)
1208 : {
1209 0 : SolarMutexGuard g;
1210 0 : return m_pImpl->m_Text.removeTextContent(xContent);
1211 : }
1212 :
1213 : // XChild
1214 : uno::Reference< uno::XInterface > SAL_CALL
1215 0 : SwXMeta::getParent() throw (uno::RuntimeException)
1216 : {
1217 0 : SolarMutexGuard g;
1218 : SwTxtNode * pTxtNode;
1219 : xub_StrLen nMetaStart;
1220 : xub_StrLen nMetaEnd;
1221 0 : bool const bSuccess( SetContentRange(pTxtNode, nMetaStart, nMetaEnd) );
1222 : OSL_ENSURE(bSuccess, "no pam?");
1223 0 : if (!bSuccess) { throw lang::DisposedException(); }
1224 : // in order to prevent getting this meta, subtract 1 from nMetaStart;
1225 : // so we get the index of the dummy character, and we exclude it
1226 : // by calling GetTxtAttrAt(_, _, PARENT) in GetNestedTextContent
1227 : uno::Reference<text::XTextContent> const xRet(
1228 : SwUnoCursorHelper::GetNestedTextContent(*pTxtNode, nMetaStart - 1,
1229 0 : true) );
1230 0 : return xRet;
1231 : }
1232 :
1233 : void SAL_CALL
1234 0 : SwXMeta::setParent(uno::Reference< uno::XInterface > const& /*xParent*/)
1235 : throw (uno::RuntimeException, lang::NoSupportException)
1236 : {
1237 0 : throw lang::NoSupportException(rtl::OUString("setting parent not supported"), *this);
1238 : }
1239 :
1240 : // XElementAccess
1241 : uno::Type SAL_CALL
1242 0 : SwXMeta::getElementType() throw (uno::RuntimeException)
1243 : {
1244 0 : return text::XTextRange::static_type();
1245 : }
1246 :
1247 : sal_Bool SAL_CALL
1248 0 : SwXMeta::hasElements() throw (uno::RuntimeException)
1249 : {
1250 0 : SolarMutexGuard g;
1251 :
1252 0 : return m_pImpl->GetRegisteredIn() ? sal_True : sal_False;
1253 : }
1254 :
1255 : // XEnumerationAccess
1256 : uno::Reference< container::XEnumeration > SAL_CALL
1257 0 : SwXMeta::createEnumeration() throw (uno::RuntimeException)
1258 : {
1259 0 : SolarMutexGuard g;
1260 :
1261 0 : if (m_pImpl->m_bIsDisposed)
1262 : {
1263 0 : throw lang::DisposedException();
1264 : }
1265 0 : if (m_pImpl->m_bIsDescriptor)
1266 : {
1267 : throw uno::RuntimeException(
1268 : rtl::OUString("createEnumeration(): not inserted"),
1269 0 : static_cast< ::cppu::OWeakObject* >(this));
1270 : }
1271 :
1272 : SwTxtNode * pTxtNode;
1273 : xub_StrLen nMetaStart;
1274 : xub_StrLen nMetaEnd;
1275 0 : const bool bSuccess(SetContentRange(pTxtNode, nMetaStart, nMetaEnd));
1276 : OSL_ENSURE(bSuccess, "no pam?");
1277 0 : if (!bSuccess)
1278 0 : throw lang::DisposedException();
1279 :
1280 0 : SwPaM aPam(*pTxtNode, nMetaStart);
1281 :
1282 0 : if (!m_pImpl->m_pTextPortions.get())
1283 : {
1284 : return new SwXTextPortionEnumeration(
1285 0 : aPam, GetParentText(), nMetaStart, nMetaEnd);
1286 : }
1287 : else // cached!
1288 : {
1289 0 : return new SwXTextPortionEnumeration(aPam, *m_pImpl->m_pTextPortions);
1290 0 : }
1291 : }
1292 :
1293 : // MetadatableMixin
1294 0 : ::sfx2::Metadatable* SwXMeta::GetCoreObject()
1295 : {
1296 0 : return const_cast< ::sw::Meta * >(m_pImpl->GetMeta());
1297 : }
1298 :
1299 0 : uno::Reference<frame::XModel> SwXMeta::GetModel()
1300 : {
1301 0 : ::sw::Meta const * const pMeta( m_pImpl->GetMeta() );
1302 0 : if (pMeta)
1303 : {
1304 0 : SwTxtNode const * const pTxtNode( pMeta->GetTxtNode() );
1305 0 : if (pTxtNode)
1306 : {
1307 0 : SwDocShell const * const pShell(pTxtNode->GetDoc()->GetDocShell());
1308 0 : return (pShell) ? pShell->GetModel() : 0;
1309 : }
1310 : }
1311 0 : return 0;
1312 : }
1313 :
1314 : /******************************************************************
1315 : * SwXMetaField
1316 : ******************************************************************/
1317 0 : inline const ::sw::MetaField * SwXMeta::Impl::GetMetaField() const
1318 : {
1319 0 : return static_cast< const ::sw::MetaField * >(GetRegisteredIn());
1320 : }
1321 :
1322 0 : SwXMetaField::SwXMetaField(SwDoc *const pDoc, ::sw::Meta *const pMeta,
1323 : uno::Reference<text::XText> const& xParentText,
1324 : TextRangeList_t const*const pPortions)
1325 0 : : SwXMetaField_Base(pDoc, pMeta, xParentText, pPortions)
1326 : {
1327 : OSL_ENSURE(pMeta && dynamic_cast< ::sw::MetaField* >(pMeta),
1328 : "SwXMetaField created for wrong hint!");
1329 0 : }
1330 :
1331 0 : SwXMetaField::SwXMetaField(SwDoc *const pDoc)
1332 0 : : SwXMetaField_Base(pDoc)
1333 : {
1334 0 : }
1335 :
1336 0 : SwXMetaField::~SwXMetaField()
1337 : {
1338 0 : }
1339 :
1340 : // XServiceInfo
1341 : ::rtl::OUString SAL_CALL
1342 0 : SwXMetaField::getImplementationName() throw (uno::RuntimeException)
1343 : {
1344 0 : return C2U("SwXMetaField");
1345 : }
1346 :
1347 : static char const*const g_ServicesMetaField[] =
1348 : {
1349 : "com.sun.star.text.TextContent",
1350 : "com.sun.star.text.TextField",
1351 : "com.sun.star.text.textfield.MetadataField",
1352 : };
1353 :
1354 : static const size_t g_nServicesMetaField(
1355 : sizeof(g_ServicesMetaField)/sizeof(g_ServicesMetaField[0]));
1356 :
1357 : sal_Bool SAL_CALL
1358 0 : SwXMetaField::supportsService(const ::rtl::OUString& rServiceName)
1359 : throw (uno::RuntimeException)
1360 : {
1361 : return ::sw::SupportsServiceImpl(
1362 0 : g_nServicesMetaField, g_ServicesMetaField, rServiceName);
1363 : }
1364 :
1365 : uno::Sequence< ::rtl::OUString > SAL_CALL
1366 0 : SwXMetaField::getSupportedServiceNames() throw (uno::RuntimeException)
1367 : {
1368 : return ::sw::GetSupportedServiceNamesImpl(
1369 0 : g_nServicesMetaField, g_ServicesMetaField);
1370 : }
1371 :
1372 : // XComponent
1373 : void SAL_CALL
1374 0 : SwXMetaField::addEventListener(
1375 : uno::Reference< lang::XEventListener> const & xListener )
1376 : throw (uno::RuntimeException)
1377 : {
1378 0 : return SwXMeta::addEventListener(xListener);
1379 : }
1380 :
1381 : void SAL_CALL
1382 0 : SwXMetaField::removeEventListener(
1383 : uno::Reference< lang::XEventListener> const & xListener )
1384 : throw (uno::RuntimeException)
1385 : {
1386 0 : return SwXMeta::removeEventListener(xListener);
1387 : }
1388 :
1389 : void SAL_CALL
1390 0 : SwXMetaField::dispose() throw (uno::RuntimeException)
1391 : {
1392 0 : return SwXMeta::dispose();
1393 : }
1394 :
1395 : // XTextContent
1396 : void SAL_CALL
1397 0 : SwXMetaField::attach(const uno::Reference< text::XTextRange > & i_xTextRange)
1398 : throw (lang::IllegalArgumentException, uno::RuntimeException)
1399 : {
1400 0 : return SwXMeta::AttachImpl(i_xTextRange, RES_TXTATR_METAFIELD);
1401 : }
1402 :
1403 : uno::Reference< text::XTextRange > SAL_CALL
1404 0 : SwXMetaField::getAnchor() throw (uno::RuntimeException)
1405 : {
1406 0 : return SwXMeta::getAnchor();
1407 : }
1408 :
1409 : // XPropertySet
1410 : uno::Reference< beans::XPropertySetInfo > SAL_CALL
1411 0 : SwXMetaField::getPropertySetInfo() throw (uno::RuntimeException)
1412 : {
1413 0 : SolarMutexGuard g;
1414 :
1415 : static uno::Reference< beans::XPropertySetInfo > xRef(
1416 : aSwMapProvider.GetPropertySet(PROPERTY_MAP_METAFIELD)
1417 0 : ->getPropertySetInfo() );
1418 0 : return xRef;
1419 : }
1420 :
1421 : void SAL_CALL
1422 0 : SwXMetaField::setPropertyValue(
1423 : const ::rtl::OUString& rPropertyName, const uno::Any& rValue)
1424 : throw (beans::UnknownPropertyException, beans::PropertyVetoException,
1425 : lang::IllegalArgumentException, lang::WrappedTargetException,
1426 : uno::RuntimeException)
1427 : {
1428 0 : SolarMutexGuard g;
1429 :
1430 : ::sw::MetaField * const pMeta(
1431 0 : const_cast< ::sw::MetaField * >(m_pImpl->GetMetaField()) );
1432 0 : if (!pMeta)
1433 0 : throw lang::DisposedException();
1434 :
1435 0 : if ( rPropertyName == "NumberFormat" )
1436 : {
1437 0 : sal_Int32 nNumberFormat(0);
1438 0 : if (rValue >>= nNumberFormat)
1439 : {
1440 0 : pMeta->SetNumberFormat(static_cast<sal_uInt32>(nNumberFormat));
1441 : }
1442 : }
1443 0 : else if ( rPropertyName == "IsFixedLanguage" )
1444 : {
1445 0 : bool b(false);
1446 0 : if (rValue >>= b)
1447 : {
1448 0 : pMeta->SetIsFixedLanguage(b);
1449 : }
1450 : }
1451 : else
1452 : {
1453 0 : throw beans::UnknownPropertyException();
1454 0 : }
1455 0 : }
1456 :
1457 : uno::Any SAL_CALL
1458 0 : SwXMetaField::getPropertyValue(const ::rtl::OUString& rPropertyName)
1459 : throw (beans::UnknownPropertyException, lang::WrappedTargetException,
1460 : uno::RuntimeException)
1461 : {
1462 0 : SolarMutexGuard g;
1463 :
1464 0 : ::sw::MetaField const * const pMeta( m_pImpl->GetMetaField() );
1465 0 : if (!pMeta)
1466 0 : throw lang::DisposedException();
1467 :
1468 0 : uno::Any any;
1469 :
1470 0 : if ( rPropertyName == "NumberFormat" )
1471 : {
1472 0 : const ::rtl::OUString text( getPresentation(sal_False) );
1473 0 : any <<= static_cast<sal_Int32>(pMeta->GetNumberFormat(text));
1474 : }
1475 0 : else if ( rPropertyName == "IsFixedLanguage" )
1476 : {
1477 0 : any <<= pMeta->IsFixedLanguage();
1478 : }
1479 : else
1480 : {
1481 0 : throw beans::UnknownPropertyException();
1482 : }
1483 :
1484 0 : return any;
1485 : }
1486 :
1487 : void SAL_CALL
1488 0 : SwXMetaField::addPropertyChangeListener(
1489 : const ::rtl::OUString& /*rPropertyName*/,
1490 : const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/)
1491 : throw (beans::UnknownPropertyException, lang::WrappedTargetException,
1492 : uno::RuntimeException)
1493 : {
1494 : OSL_FAIL("SwXMetaField::addPropertyChangeListener(): not implemented");
1495 0 : }
1496 :
1497 : void SAL_CALL
1498 0 : SwXMetaField::removePropertyChangeListener(
1499 : const ::rtl::OUString& /*rPropertyName*/,
1500 : const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/)
1501 : throw (beans::UnknownPropertyException, lang::WrappedTargetException,
1502 : uno::RuntimeException)
1503 : {
1504 : OSL_FAIL("SwXMetaField::removePropertyChangeListener(): not implemented");
1505 0 : }
1506 :
1507 : void SAL_CALL
1508 0 : SwXMetaField::addVetoableChangeListener(
1509 : const ::rtl::OUString& /*rPropertyName*/,
1510 : const uno::Reference< beans::XVetoableChangeListener >& /*xListener*/)
1511 : throw (beans::UnknownPropertyException, lang::WrappedTargetException,
1512 : uno::RuntimeException)
1513 : {
1514 : OSL_FAIL("SwXMetaField::addVetoableChangeListener(): not implemented");
1515 0 : }
1516 :
1517 : void SAL_CALL
1518 0 : SwXMetaField::removeVetoableChangeListener(
1519 : const ::rtl::OUString& /*rPropertyName*/,
1520 : const uno::Reference< beans::XVetoableChangeListener >& /*xListener*/)
1521 : throw (beans::UnknownPropertyException, lang::WrappedTargetException,
1522 : uno::RuntimeException)
1523 : {
1524 : OSL_FAIL("SwXMetaField::removeVetoableChangeListener(): not implemented");
1525 0 : }
1526 :
1527 : #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
1528 : #include <com/sun/star/rdf/Statement.hpp>
1529 : #include <com/sun/star/rdf/URI.hpp>
1530 : #include <com/sun/star/rdf/URIs.hpp>
1531 : #include <com/sun/star/rdf/XLiteral.hpp>
1532 : #include <com/sun/star/rdf/XRepositorySupplier.hpp>
1533 : #include <comphelper/processfactory.hxx>
1534 :
1535 : static uno::Reference<rdf::XURI> const&
1536 0 : lcl_getURI(const bool bPrefix)
1537 : {
1538 : static uno::Reference< uno::XComponentContext > xContext(
1539 0 : ::comphelper::getProcessComponentContext());
1540 : static uno::Reference< rdf::XURI > xOdfPrefix(
1541 : rdf::URI::createKnown(xContext, rdf::URIs::ODF_PREFIX),
1542 0 : uno::UNO_SET_THROW);
1543 : static uno::Reference< rdf::XURI > xOdfSuffix(
1544 : rdf::URI::createKnown(xContext, rdf::URIs::ODF_SUFFIX),
1545 0 : uno::UNO_SET_THROW);
1546 0 : return (bPrefix) ? xOdfPrefix : xOdfSuffix;
1547 : }
1548 :
1549 : static ::rtl::OUString
1550 0 : lcl_getPrefixOrSuffix(
1551 : uno::Reference<rdf::XRepository> const & xRepository,
1552 : uno::Reference<rdf::XResource> const & xMetaField,
1553 : uno::Reference<rdf::XURI> const & xPredicate)
1554 : {
1555 : const uno::Reference<container::XEnumeration> xEnum(
1556 0 : xRepository->getStatements(xMetaField, xPredicate, 0),
1557 0 : uno::UNO_SET_THROW);
1558 0 : while (xEnum->hasMoreElements()) {
1559 0 : rdf::Statement stmt;
1560 0 : if (!(xEnum->nextElement() >>= stmt)) {
1561 0 : throw uno::RuntimeException();
1562 : }
1563 : const uno::Reference<rdf::XLiteral> xObject(stmt.Object,
1564 0 : uno::UNO_QUERY);
1565 0 : if (!xObject.is()) continue;
1566 0 : if (xEnum->hasMoreElements()) {
1567 : OSL_TRACE("ignoring other odf:Prefix/odf:Suffix statements");
1568 : }
1569 0 : return xObject->getValue();
1570 0 : }
1571 0 : return ::rtl::OUString();
1572 : }
1573 :
1574 : void
1575 0 : getPrefixAndSuffix(
1576 : const uno::Reference<frame::XModel>& xModel,
1577 : const uno::Reference<rdf::XMetadatable>& xMetaField,
1578 : ::rtl::OUString *const o_pPrefix, ::rtl::OUString *const o_pSuffix)
1579 : {
1580 : try {
1581 : const uno::Reference<rdf::XRepositorySupplier> xRS(
1582 0 : xModel, uno::UNO_QUERY_THROW);
1583 : const uno::Reference<rdf::XRepository> xRepo(
1584 0 : xRS->getRDFRepository(), uno::UNO_SET_THROW);
1585 : const uno::Reference<rdf::XResource> xMeta(
1586 0 : xMetaField, uno::UNO_QUERY_THROW);
1587 0 : if (o_pPrefix)
1588 : {
1589 0 : *o_pPrefix = lcl_getPrefixOrSuffix(xRepo, xMeta, lcl_getURI(true));
1590 : }
1591 0 : if (o_pSuffix)
1592 : {
1593 0 : *o_pSuffix = lcl_getPrefixOrSuffix(xRepo, xMeta, lcl_getURI(false));
1594 0 : }
1595 0 : } catch (uno::RuntimeException &) {
1596 0 : throw;
1597 0 : } catch (const uno::Exception & e) {
1598 : throw lang::WrappedTargetRuntimeException(
1599 : ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("getPrefixAndSuffix: exception")),
1600 0 : 0, uno::makeAny(e));
1601 : }
1602 0 : }
1603 :
1604 : // XTextField
1605 : ::rtl::OUString SAL_CALL
1606 0 : SwXMetaField::getPresentation(sal_Bool bShowCommand)
1607 : throw (uno::RuntimeException)
1608 : {
1609 0 : SolarMutexGuard g;
1610 :
1611 0 : if (bShowCommand)
1612 : {
1613 : //FIXME ?
1614 0 : return ::rtl::OUString();
1615 : }
1616 : else
1617 : {
1618 : // getString should check if this is invalid
1619 0 : const ::rtl::OUString content( this->getString() );
1620 0 : ::rtl::OUString prefix;
1621 0 : ::rtl::OUString suffix;
1622 0 : getPrefixAndSuffix(GetModel(), this, &prefix, &suffix);
1623 0 : return prefix + content + suffix;
1624 0 : }
1625 : }
1626 :
1627 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|