xref: /aoo42x/main/sw/source/ui/uno/unoatxt.cxx (revision efeef26f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 
27 
28 #define _SVSTDARR_STRINGS
29 
30 #include <com/sun/star/beans/PropertyAttribute.hpp>
31 #include <vos/mutex.hxx>
32 #include <tools/debug.hxx>
33 #include <vcl/svapp.hxx>
34 #include <svl/svstdarr.hxx>
35 #include <svtools/unoevent.hxx>
36 #include <svl/urihelper.hxx>
37 #include <sfx2/event.hxx>
38 #include <swtypes.hxx>
39 #include <glosdoc.hxx>
40 #include <shellio.hxx>
41 #include <initui.hxx>
42 #include <gloslst.hxx>
43 #include <unoatxt.hxx>
44 #include <unomap.hxx>
45 #include <unomid.h>
46 #include <unotextbodyhf.hxx>
47 #include <unotextrange.hxx>
48 #include <TextCursorHelper.hxx>
49 #include <swevent.hxx>
50 #include <doc.hxx>
51 #include <unocrsr.hxx>
52 #include <IMark.hxx>
53 #include <unoprnms.hxx>
54 #include <docsh.hxx>
55 #include <swunodef.hxx>
56 #include <swmodule.hxx>
57 #include <svl/smplhint.hxx>
58 #include <svl/macitem.hxx>
59 
60 #include <editeng/acorrcfg.hxx>
61 
62 #include <memory>
63 
64 
65 SV_IMPL_REF ( SwDocShell )
66 using namespace ::com::sun::star;
67 using ::rtl::OUString;
68 
69 /******************************************************************
70  *
71  ******************************************************************/
72 /* -----------------30.03.99 14:31-------------------
73  *
74  * --------------------------------------------------*/
SwXAutoTextContainer_createInstance(const uno::Reference<lang::XMultiServiceFactory> &)75 uno::Reference< uno::XInterface > SAL_CALL SwXAutoTextContainer_createInstance(
76     const uno::Reference< lang::XMultiServiceFactory > & ) throw( uno::Exception )
77 {
78 	//the module may not be loaded
79 	::vos::OGuard aGuard(Application::GetSolarMutex());
80 	SwDLL::Init();
81 	static uno::Reference< uno::XInterface > xAText = (cppu::OWeakObject*)new SwXAutoTextContainer();;
82 	return xAText;
83 }
84 /* -----------------------------17.04.01 13:17--------------------------------
85 
86  ---------------------------------------------------------------------------*/
SwXAutoTextContainer_getSupportedServiceNames()87 uno::Sequence< OUString > SAL_CALL SwXAutoTextContainer_getSupportedServiceNames() throw()
88 {
89     OUString sService( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.AutoTextContainer"));
90     const uno::Sequence< OUString > aSeq( &sService, 1 );
91 	return aSeq;
92 }
93 /* -----------------------------17.04.01 13:18--------------------------------
94 
95  ---------------------------------------------------------------------------*/
SwXAutoTextContainer_getImplementationName()96 OUString SAL_CALL SwXAutoTextContainer_getImplementationName() throw()
97 {
98     return OUString( RTL_CONSTASCII_USTRINGPARAM("SwXAutoTextContainer" ) );
99 }
100 
101 /*-- 21.12.98 12:42:16---------------------------------------------------
102 
103   -----------------------------------------------------------------------*/
SwXAutoTextContainer()104 SwXAutoTextContainer::SwXAutoTextContainer()
105 {
106 	pGlossaries = ::GetGlossaries();
107 
108 }
109 /*-- 21.12.98 12:42:17---------------------------------------------------
110 
111   -----------------------------------------------------------------------*/
~SwXAutoTextContainer()112 SwXAutoTextContainer::~SwXAutoTextContainer()
113 {
114 
115 }
116 /*-- 21.12.98 12:42:17---------------------------------------------------
117 
118   -----------------------------------------------------------------------*/
getCount(void)119 sal_Int32 SwXAutoTextContainer::getCount(void) throw( uno::RuntimeException )
120 {
121 	return pGlossaries->GetGroupCnt();
122 }
123 /*-- 21.12.98 12:42:18---------------------------------------------------
124 
125   -----------------------------------------------------------------------*/
getByIndex(sal_Int32 nIndex)126 uno::Any SwXAutoTextContainer::getByIndex(sal_Int32 nIndex)
127 	throw( lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException )
128 {
129 	::vos::OGuard aGuard(Application::GetSolarMutex());
130 	uno::Any aRet;
131 	sal_uInt16 nCount = pGlossaries->GetGroupCnt();
132 	if ( 0 <= nIndex && nIndex < nCount )
133         aRet = getByName(pGlossaries->GetGroupName( static_cast< sal_uInt16 >(nIndex) ));
134 	else
135 		throw lang::IndexOutOfBoundsException();
136 	return aRet;
137 }
138 /*-- 21.12.98 12:42:18---------------------------------------------------
139 
140   -----------------------------------------------------------------------*/
getElementType(void)141 uno::Type SwXAutoTextContainer::getElementType(void) throw( uno::RuntimeException )
142 {
143 	return ::getCppuType((const uno::Reference<text::XAutoTextGroup>*)0);
144 
145 }
146 /*-- 21.12.98 12:42:18---------------------------------------------------
147 
148   -----------------------------------------------------------------------*/
hasElements(void)149 sal_Bool SwXAutoTextContainer::hasElements(void) throw( uno::RuntimeException )
150 {
151 	//zumindest Standard sollte es immer geben!
152 	return sal_True;
153 }
154 /*-- 21.12.98 12:42:18---------------------------------------------------
155 
156   -----------------------------------------------------------------------*/
getByName(const OUString & GroupName)157 uno::Any SwXAutoTextContainer::getByName(const OUString& GroupName)
158 	throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
159 {
160 	::vos::OGuard aGuard(Application::GetSolarMutex());
161 
162 	uno::Reference< text::XAutoTextGroup > xGroup;
163 	if ( pGlossaries && hasByName( GroupName ) )	// group name already known?
164 		// sal_True = create group if not already available
165         xGroup = pGlossaries->GetAutoTextGroup( GroupName, sal_True );
166 
167 	if ( !xGroup.is() )
168 		throw container::NoSuchElementException();
169 
170 	return makeAny( xGroup );
171 }
172 /*-- 21.12.98 12:42:19---------------------------------------------------
173 
174   -----------------------------------------------------------------------*/
getElementNames(void)175 uno::Sequence< OUString > SwXAutoTextContainer::getElementNames(void) throw( uno::RuntimeException )
176 {
177 	::vos::OGuard aGuard(Application::GetSolarMutex());
178 	sal_uInt16 nCount = pGlossaries->GetGroupCnt();
179 
180 	uno::Sequence< OUString > aGroupNames(nCount);
181 	OUString *pArr = aGroupNames.getArray();
182 
183 	for ( sal_uInt16 i = 0; i < nCount; i++ )
184 	{
185 		// Die Namen werden ohne Pfad-Extension weitergegeben
186 		String sGroupName(pGlossaries->GetGroupName(i));
187 		pArr[i] = sGroupName.GetToken(0, GLOS_DELIM);
188 	}
189 	return aGroupNames;
190 }
191 /*-- 21.12.98 12:42:19---------------------------------------------------
192 	findet Gruppennamen mit und ohne Pfadindex
193   -----------------------------------------------------------------------*/
hasByName(const OUString & Name)194 sal_Bool SwXAutoTextContainer::hasByName(const OUString& Name)
195 	throw( uno::RuntimeException )
196 {
197 	::vos::OGuard aGuard(Application::GetSolarMutex());
198 	String sGroupName( pGlossaries->GetCompleteGroupName( Name ) );
199 	if(sGroupName.Len())
200 		return sal_True;
201 	return sal_False;
202 }
203 /*-- 21.12.98 12:42:19---------------------------------------------------
204 
205   -----------------------------------------------------------------------*/
insertNewByName(const OUString & aGroupName)206 uno::Reference< text::XAutoTextGroup >  SwXAutoTextContainer::insertNewByName(
207 	const OUString& aGroupName)
208 	throw( lang::IllegalArgumentException, container::ElementExistException, uno::RuntimeException )
209 {
210 	::vos::OGuard aGuard(Application::GetSolarMutex());
211 	if(hasByName(aGroupName))
212 		throw container::ElementExistException();
213 	//check for non-ASCII characters
214 	if(!aGroupName.getLength())
215 	{
216 		lang::IllegalArgumentException aIllegal;
217 		aIllegal.Message = C2U("group name must not be empty");
218 		throw aIllegal;
219 	}
220 	for(sal_Int32 nPos = 0; nPos < aGroupName.getLength(); nPos++)
221 	{
222 		sal_Unicode cChar = aGroupName[nPos];
223 		if(	((cChar >= 'A') && (cChar <= 'Z')) ||
224             ((cChar >= 'a') && (cChar <= 'z')) ||
225             ((cChar >= '0') && (cChar <= '9')) ||
226             (cChar == '_') ||
227             (cChar == 0x20) ||
228             (cChar == GLOS_DELIM) )
229         {
230 			continue;
231 		}
232 		lang::IllegalArgumentException aIllegal;
233         aIllegal.Message = C2U("group name must contain a-z, A-z, '_', ' ' only");
234 		throw aIllegal;
235 	}
236 	String sGroup(aGroupName);
237 	if(STRING_NOTFOUND == sGroup.Search(GLOS_DELIM))
238 	{
239 		sGroup += GLOS_DELIM;
240 		sGroup += UniString::CreateFromInt32(0);
241 	}
242 	pGlossaries->NewGroupDoc(sGroup, sGroup.GetToken(0, GLOS_DELIM));
243 
244 	uno::Reference< text::XAutoTextGroup > xGroup = pGlossaries->GetAutoTextGroup( sGroup, true );
245 	DBG_ASSERT( xGroup.is(), "SwXAutoTextContainer::insertNewByName: no UNO object created? How this?" );
246 		// we just inserted the group into the glossaries, so why doesn't it exist?
247 
248 	return xGroup;
249 }
250 /*-- 21.12.98 12:42:19---------------------------------------------------
251 
252   -----------------------------------------------------------------------*/
removeByName(const OUString & aGroupName)253 void SwXAutoTextContainer::removeByName(const OUString& aGroupName)
254 	throw( container::NoSuchElementException, uno::RuntimeException )
255 {
256 	::vos::OGuard aGuard(Application::GetSolarMutex());
257 	//zunaechst den Namen mit Pfad-Extension finden
258 	String sGroupName = pGlossaries->GetCompleteGroupName( aGroupName );
259 	if(!sGroupName.Len())
260 		throw container::NoSuchElementException();
261 	pGlossaries->DelGroupDoc(sGroupName);
262 }
263 /* -----------------------------06.04.00 11:11--------------------------------
264 
265  ---------------------------------------------------------------------------*/
getImplementationName(void)266 OUString SwXAutoTextContainer::getImplementationName(void) throw( uno::RuntimeException )
267 {
268     return SwXAutoTextContainer_getImplementationName();
269 }
270 /* -----------------------------06.04.00 11:11--------------------------------
271 
272  ---------------------------------------------------------------------------*/
supportsService(const OUString & rServiceName)273 sal_Bool SwXAutoTextContainer::supportsService(const OUString& rServiceName) throw( uno::RuntimeException )
274 {
275     const uno::Sequence< OUString > aNames = SwXAutoTextContainer_getSupportedServiceNames();
276     for(sal_Int32 nService = 0; nService < aNames.getLength(); nService++)
277     {
278         if(aNames.getConstArray()[nService] == rServiceName)
279             return sal_True;
280     }
281     return sal_False;
282 }
283 /* -----------------------------06.04.00 11:11--------------------------------
284 
285  ---------------------------------------------------------------------------*/
getSupportedServiceNames(void)286 uno::Sequence< OUString > SwXAutoTextContainer::getSupportedServiceNames(void) throw( uno::RuntimeException )
287 {
288     return SwXAutoTextContainer_getSupportedServiceNames();
289 }
290 /******************************************************************
291  *
292  ******************************************************************/
293 /* -----------------------------10.03.00 18:02--------------------------------
294 
295  ---------------------------------------------------------------------------*/
getUnoTunnelId()296 const uno::Sequence< sal_Int8 > & SwXAutoTextGroup::getUnoTunnelId()
297 {
298     static uno::Sequence< sal_Int8 > aSeq = ::CreateUnoTunnelId();
299 	return aSeq;
300 }
301 /* -----------------------------10.03.00 18:04--------------------------------
302 
303  ---------------------------------------------------------------------------*/
getSomething(const uno::Sequence<sal_Int8> & rId)304 sal_Int64 SAL_CALL SwXAutoTextGroup::getSomething( const uno::Sequence< sal_Int8 >& rId )
305 	throw(uno::RuntimeException)
306 {
307     if( rId.getLength() == 16
308         && 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(),
309 										rId.getConstArray(), 16 ) )
310     {
311             return sal::static_int_cast< sal_Int64 >( reinterpret_cast< sal_IntPtr >( this ));
312     }
313 	return 0;
314 }
315 
316 /*-- 21.12.98 12:42:24---------------------------------------------------
317 
318   -----------------------------------------------------------------------*/
SwXAutoTextGroup(const OUString & rName,SwGlossaries * pGlos)319 SwXAutoTextGroup::SwXAutoTextGroup(const OUString& rName,
320 			SwGlossaries*	pGlos) :
321     pPropSet(aSwMapProvider.GetPropertySet(PROPERTY_MAP_AUTO_TEXT_GROUP)),
322     pGlossaries(pGlos),
323 	sName(rName),
324     m_sGroupName(rName)
325 {
326 	DBG_ASSERT( -1 != rName.indexOf( GLOS_DELIM ),
327 		"SwXAutoTextGroup::SwXAutoTextGroup: to be constructed with a complete name only!" );
328 }
329 
330 /*-- 21.12.98 12:42:24---------------------------------------------------
331 
332   -----------------------------------------------------------------------*/
~SwXAutoTextGroup()333 SwXAutoTextGroup::~SwXAutoTextGroup()
334 {
335 }
336 /*-- 21.12.98 12:42:24---------------------------------------------------
337 
338   -----------------------------------------------------------------------*/
339 /*-- 21.12.98 12:42:25---------------------------------------------------
340 
341   -----------------------------------------------------------------------*/
getTitles(void)342 uno::Sequence< OUString > SwXAutoTextGroup::getTitles(void) throw( uno::RuntimeException )
343 {
344 	::vos::OGuard aGuard(Application::GetSolarMutex());
345 	sal_uInt16 nCount = 0;
346 	SwTextBlocks* pGlosGroup = pGlossaries ? pGlossaries->GetGroupDoc(m_sGroupName, sal_False) : 0;
347     if(pGlosGroup && !pGlosGroup->GetError())
348 		nCount = pGlosGroup->GetCount();
349 	else
350 		throw uno::RuntimeException();
351 
352 	uno::Sequence< OUString > aEntryTitles(nCount);
353 	OUString *pArr = aEntryTitles.getArray();
354 
355 	for ( sal_uInt16 i = 0; i < nCount; i++ )
356 		pArr[i] = pGlosGroup->GetLongName(i);
357 	delete pGlosGroup;
358 	return aEntryTitles;
359 }
360 /*-- 21.12.98 12:42:25---------------------------------------------------
361 
362   -----------------------------------------------------------------------*/
renameByName(const OUString & aElementName,const OUString & aNewElementName,const OUString & aNewElementTitle)363 void SwXAutoTextGroup::renameByName(const OUString& aElementName,
364 	const OUString& aNewElementName, const OUString& aNewElementTitle)
365 	throw( lang::IllegalArgumentException, container::ElementExistException, io::IOException,
366 													 uno::RuntimeException)
367 {
368 	::vos::OGuard aGuard(Application::GetSolarMutex());
369 	// throw exception only if the programmatic name is to be changed into an existing name
370 	if(aNewElementName != aElementName && hasByName(aNewElementName))
371 		throw container::ElementExistException();
372 	SwTextBlocks* pGlosGroup = pGlossaries ? pGlossaries->GetGroupDoc(m_sGroupName, sal_False) : 0;
373     if(pGlosGroup && !pGlosGroup->GetError())
374 	{
375 		sal_uInt16 nIdx = pGlosGroup->GetIndex( aElementName);
376 		if(USHRT_MAX == nIdx)
377 			throw lang::IllegalArgumentException();
378 		String aNewShort( aNewElementName);
379 		String aNewName( aNewElementTitle);
380 		sal_uInt16 nOldLongIdx = pGlosGroup->GetLongIndex( aNewShort );
381 		sal_uInt16 nOldIdx = pGlosGroup->GetIndex( aNewName );
382 
383 		if( nIdx != USHRT_MAX &&
384 				(nOldLongIdx == USHRT_MAX || nOldLongIdx == nIdx )&&
385 					(nOldIdx == USHRT_MAX || nOldIdx == nIdx ))
386 		{
387 			pGlosGroup->Rename( nIdx, &aNewShort, &aNewName );
388 			if(pGlosGroup->GetError() != 0)
389 				throw io::IOException();
390 		}
391 		delete pGlosGroup;
392 	}
393 	else
394 		throw uno::RuntimeException();
395 }
396 
lcl_CopySelToDoc(SwDoc * pInsDoc,OTextCursorHelper * pxCursor,SwXTextRange * pxRange)397 sal_Bool lcl_CopySelToDoc( SwDoc* pInsDoc, OTextCursorHelper* pxCursor, SwXTextRange* pxRange)
398 {
399 	ASSERT( pInsDoc, "kein Ins.Dokument"  );
400 
401 	SwNodes& rNds = pInsDoc->GetNodes();
402 
403 	SwNodeIndex aIdx( rNds.GetEndOfContent(), -1 );
404 	SwCntntNode * pNd = aIdx.GetNode().GetCntntNode();
405 	SwPosition aPos( aIdx, SwIndex( pNd, pNd->Len() ));
406 
407     bool bRet = false;
408 	pInsDoc->LockExpFlds();
409 	{
410         SwDoc *const pDoc((pxCursor) ? pxCursor->GetDoc() : pxRange->GetDoc());
411         SwPaM aPam(pDoc->GetNodes());
412         SwPaM * pPam(0);
413         if(pxCursor)
414         {
415             pPam = pxCursor->GetPaM();
416         }
417         else
418         {
419             if (pxRange->GetPositions(aPam))
420             {
421                 pPam = & aPam;
422             }
423         }
424         if (!pPam) { return false; }
425         bRet = pDoc->CopyRange( *pPam, aPos, false ) || bRet;
426 	}
427 
428 	pInsDoc->UnlockExpFlds();
429 	if( !pInsDoc->IsExpFldsLocked() )
430 		pInsDoc->UpdateExpFlds(NULL, true);
431 
432 	return bRet;
433 }
434 /*-- 21.12.98 12:42:25---------------------------------------------------
435 
436   -----------------------------------------------------------------------*/
insertNewByName(const OUString & aName,const OUString & aTitle,const uno::Reference<text::XTextRange> & xTextRange)437 uno::Reference< text::XAutoTextEntry >  SwXAutoTextGroup::insertNewByName(const OUString& aName,
438 		const OUString& aTitle, const uno::Reference< text::XTextRange > & xTextRange)
439 		throw( container::ElementExistException, uno::RuntimeException )
440 {
441 	::vos::OGuard aGuard(Application::GetSolarMutex());
442 	if(hasByName(aName))
443 		throw container::ElementExistException();
444 	if(!xTextRange.is())
445 		throw uno::RuntimeException();
446 
447 	SwTextBlocks* pGlosGroup = pGlossaries ? pGlossaries->GetGroupDoc(m_sGroupName, sal_False) : 0;
448 	String sShortName(aName);
449 	String sLongName(aTitle);
450     if(pGlosGroup && !pGlosGroup->GetError())
451 	{
452         /*if( pGlosGroup->IsOld() && pGlosGroup->ConvertToNew())
453 		{
454             throw uno::RuntimeException();
455         } */
456 		uno::Reference<lang::XUnoTunnel> xRangeTunnel( xTextRange, uno::UNO_QUERY);
457 		SwXTextRange* pxRange = 0;
458 		OTextCursorHelper* pxCursor = 0;
459 		if(xRangeTunnel.is())
460 		{
461             pxRange = reinterpret_cast<SwXTextRange*>(xRangeTunnel->getSomething(
462                                     SwXTextRange::getUnoTunnelId()));
463             pxCursor = reinterpret_cast<OTextCursorHelper*>(xRangeTunnel->getSomething(
464                                     OTextCursorHelper::getUnoTunnelId()));
465 		}
466 
467 		String sOnlyTxt;
468 		String* pOnlyTxt = 0;
469 		sal_Bool bNoAttr = !pxCursor && !pxRange;
470 		if(bNoAttr)
471 		{
472 			sOnlyTxt = UniString(xTextRange->getString());
473 			pOnlyTxt = &sOnlyTxt;
474 		}
475 
476 		const SvxAutoCorrCfg* pCfg = SvxAutoCorrCfg::Get();
477 
478 		SwDoc* pGDoc = pGlosGroup->GetDoc();
479 
480 		// Bis es eine Option dafuer gibt, base util::URL loeschen
481 		if(pCfg->IsSaveRelFile())
482 		{
483             INetURLObject aTemp(pGlosGroup->GetFileName());
484             pGlosGroup->SetBaseURL( aTemp.GetMainURL(INetURLObject::NO_DECODE));
485 		}
486 		else
487             pGlosGroup->SetBaseURL( aEmptyStr );
488 
489 		sal_uInt16 nRet;
490 		if( pOnlyTxt )
491 			nRet = pGlosGroup->PutText( sShortName, sLongName, *pOnlyTxt );
492 		else
493 		{
494 			pGlosGroup->ClearDoc();
495 			if( pGlosGroup->BeginPutDoc( sShortName, sLongName ) )
496 			{
497 				pGDoc->SetRedlineMode_intern( nsRedlineMode_t::REDLINE_DELETE_REDLINES );
498 				lcl_CopySelToDoc( pGDoc, pxCursor, pxRange );
499 				pGDoc->SetRedlineMode_intern((RedlineMode_t)( 0 ));
500 				nRet = pGlosGroup->PutDoc();
501 			}
502 			else
503 				nRet = (sal_uInt16) -1;
504 		}
505 
506 		if(nRet == (sal_uInt16) -1 )
507 		{
508 			throw uno::RuntimeException();
509 		}
510 		pGlossaries->PutGroupDoc( pGlosGroup );
511 	}
512 
513 	uno::Reference< text::XAutoTextEntry > xEntry = pGlossaries->GetAutoTextEntry( m_sGroupName, sName, sShortName, true );
514 	DBG_ASSERT( xEntry.is(), "SwXAutoTextGroup::insertNewByName: no UNO object created? How this?" );
515 		// we just inserted the entry into the group, so why doesn't it exist?
516 
517 	return xEntry;
518 }
519 /*-- 21.12.98 12:42:25---------------------------------------------------
520 
521   -----------------------------------------------------------------------*/
removeByName(const OUString & aEntryName)522 void SwXAutoTextGroup::removeByName(const OUString& aEntryName) throw( container::NoSuchElementException, uno::RuntimeException )
523 {
524 	::vos::OGuard aGuard(Application::GetSolarMutex());
525 	SwTextBlocks* pGlosGroup = pGlossaries ? pGlossaries->GetGroupDoc(m_sGroupName, sal_False) : 0;
526     if(pGlosGroup && !pGlosGroup->GetError())
527 	{
528 		sal_uInt16 nIdx = pGlosGroup->GetIndex(aEntryName);
529 		if ( nIdx != USHRT_MAX )
530 			pGlosGroup->Delete(nIdx);
531 		delete pGlosGroup;
532 	}
533 	else
534 		throw container::NoSuchElementException();
535 }
536 /*-- 21.12.98 12:42:25---------------------------------------------------
537 
538   -----------------------------------------------------------------------*/
getName(void)539 OUString SwXAutoTextGroup::getName(void) throw( uno::RuntimeException )
540 {
541 	::vos::OGuard aGuard(Application::GetSolarMutex());
542 	return sName;
543 }
544 /*-- 21.12.98 12:42:25---------------------------------------------------
545 
546   -----------------------------------------------------------------------*/
setName(const OUString & rName)547 void SwXAutoTextGroup::setName(const OUString& rName) throw( uno::RuntimeException )
548 {
549 	::vos::OGuard aGuard(Application::GetSolarMutex());
550 	if( !pGlossaries )
551 		throw uno::RuntimeException();
552 
553     sal_Int32 nNewDelimPos = rName.lastIndexOf( GLOS_DELIM );
554     sal_Int32 nOldDelimPos = sName.lastIndexOf( GLOS_DELIM );
555 
556     OUString aNewSuffix;
557     if (nNewDelimPos > -1)
558         aNewSuffix = rName.copy( nNewDelimPos + 1 );
559     OUString aOldSuffix;
560     if (nOldDelimPos > -1)
561         aOldSuffix = sName.copy( nOldDelimPos + 1 );
562 
563 	sal_Int32 nNewNumeric = aNewSuffix.toInt32();
564 	sal_Int32 nOldNumeric = aOldSuffix.toInt32();
565 
566     OUString aNewPrefix( (nNewDelimPos > 1) ? rName.copy( 0, nNewDelimPos ) : rName );
567     OUString aOldPrefix( (nOldDelimPos > 1) ? sName.copy( 0, nOldDelimPos ) : sName );
568 
569 	if ( sName == rName ||
570 	   ( nNewNumeric == nOldNumeric && aNewPrefix == aOldPrefix ) )
571 		return;
572 	String sNewGroup(rName);
573 	if(STRING_NOTFOUND == sNewGroup.Search(GLOS_DELIM))
574 	{
575 		sNewGroup += GLOS_DELIM;
576 		sNewGroup += UniString::CreateFromInt32(0);
577 	}
578 
579 	//the name must be saved, the group may be invalidated while in RenameGroupDoc()
580 	SwGlossaries* pTempGlossaries = pGlossaries;
581 
582 	String sPreserveTitle( pGlossaries->GetGroupTitle( sName ) );
583 	if ( !pGlossaries->RenameGroupDoc( sName, sNewGroup, sPreserveTitle ) )
584 		throw uno::RuntimeException();
585 	else
586 	{
587 		sName = rName;
588 		m_sGroupName = sNewGroup;
589 		pGlossaries = pTempGlossaries;
590 	}
591 }
592 /*-- 21.12.98 12:42:26---------------------------------------------------
593 
594   -----------------------------------------------------------------------*/
getCount(void)595 sal_Int32 SwXAutoTextGroup::getCount(void) throw( uno::RuntimeException )
596 {
597 	::vos::OGuard aGuard(Application::GetSolarMutex());
598 	int nCount = 0;
599 	SwTextBlocks* pGlosGroup = pGlossaries ? pGlossaries->GetGroupDoc(m_sGroupName, sal_False) : 0;
600     if(pGlosGroup && !pGlosGroup->GetError())
601 		nCount = pGlosGroup->GetCount();
602 	else
603 		throw uno::RuntimeException();
604 	delete pGlosGroup;
605 	return nCount;
606 }
607 /*-- 21.12.98 12:42:26---------------------------------------------------
608 
609   -----------------------------------------------------------------------*/
getByIndex(sal_Int32 nIndex)610 uno::Any SwXAutoTextGroup::getByIndex(sal_Int32 nIndex)
611 	throw( lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException )
612 {
613 	::vos::OGuard aGuard(Application::GetSolarMutex());
614 	uno::Any aRet;
615 	sal_uInt16 nCount = 0;
616 	SwTextBlocks* pGlosGroup = pGlossaries ? pGlossaries->GetGroupDoc(m_sGroupName, sal_False) : 0;
617     if(pGlosGroup && !pGlosGroup->GetError())
618 		nCount = pGlosGroup->GetCount();
619 	else
620 		throw uno::RuntimeException();
621 	if(0 <= nIndex && nIndex < nCount)
622 		aRet = getByName(pGlosGroup->GetShortName((sal_uInt16) nIndex));
623 	else
624 		throw lang::IndexOutOfBoundsException();
625 	delete pGlosGroup;
626 	return aRet;
627 }
628 /*-- 21.12.98 12:42:26---------------------------------------------------
629 
630   -----------------------------------------------------------------------*/
getElementType(void)631 uno::Type SwXAutoTextGroup::getElementType(void) throw( uno::RuntimeException )
632 {
633 	return ::getCppuType((uno::Reference<text::XAutoTextEntry>*)0);
634 
635 }
636 /*-- 21.12.98 12:42:26---------------------------------------------------
637 
638   -----------------------------------------------------------------------*/
hasElements(void)639 sal_Bool SwXAutoTextGroup::hasElements(void) throw( uno::RuntimeException )
640 {
641 	::vos::OGuard aGuard(Application::GetSolarMutex());
642 	SwTextBlocks* pGlosGroup = pGlossaries ? pGlossaries->GetGroupDoc(m_sGroupName, sal_False) : 0;
643 	sal_uInt16 nCount = 0;
644     if(pGlosGroup && !pGlosGroup->GetError())
645 		nCount = pGlosGroup->GetCount();
646 	else
647 		throw uno::RuntimeException();
648 	delete pGlosGroup;
649 	return nCount > 0;
650 
651 }
652 /*-- 21.12.98 12:42:27---------------------------------------------------
653 
654   -----------------------------------------------------------------------*/
getByName(const OUString & _rName)655 uno::Any SwXAutoTextGroup::getByName(const OUString& _rName)
656 	throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
657 {
658 	::vos::OGuard aGuard(Application::GetSolarMutex());
659 	uno::Reference< text::XAutoTextEntry > xEntry = pGlossaries->GetAutoTextEntry( m_sGroupName, sName, _rName, true );
660 	DBG_ASSERT( xEntry.is(), "SwXAutoTextGroup::getByName: GetAutoTextEntry is fractious!" );
661 		// we told it to create the object, so why didn't it?
662 	return makeAny( xEntry );
663 }
664 /*-- 21.12.98 12:42:27---------------------------------------------------
665 
666   -----------------------------------------------------------------------*/
getElementNames(void)667 uno::Sequence< OUString > SwXAutoTextGroup::getElementNames(void)
668 	throw( uno::RuntimeException )
669 {
670 	::vos::OGuard aGuard(Application::GetSolarMutex());
671 	sal_uInt16 nCount = 0;
672 	SwTextBlocks* pGlosGroup = pGlossaries ? pGlossaries->GetGroupDoc(m_sGroupName, sal_False) : 0;
673     if(pGlosGroup && !pGlosGroup->GetError())
674 		nCount = pGlosGroup->GetCount();
675 	else
676 		throw uno::RuntimeException();
677 
678 	uno::Sequence< OUString > aEntryNames(nCount);
679 	OUString *pArr = aEntryNames.getArray();
680 
681 	for ( sal_uInt16 i = 0; i < nCount; i++ )
682 		pArr[i] = pGlosGroup->GetShortName(i);
683 	delete pGlosGroup;
684 	return aEntryNames;
685 }
686 /*-- 21.12.98 12:42:27---------------------------------------------------
687 
688   -----------------------------------------------------------------------*/
hasByName(const OUString & rName)689 sal_Bool SwXAutoTextGroup::hasByName(const OUString& rName)
690 	throw( uno::RuntimeException )
691 {
692 	::vos::OGuard aGuard(Application::GetSolarMutex());
693 	sal_Bool bRet = sal_False;
694 	sal_uInt16 nCount = 0;
695 	SwTextBlocks* pGlosGroup = pGlossaries ? pGlossaries->GetGroupDoc(m_sGroupName, sal_False) : 0;
696     if(pGlosGroup && !pGlosGroup->GetError())
697 		nCount = pGlosGroup->GetCount();
698 	else
699 		throw uno::RuntimeException();
700 
701 	for( sal_uInt16 i = 0; i < nCount; i++ )
702 	{
703 		String sCompare(pGlosGroup->GetShortName(i));
704 		if(COMPARE_EQUAL == sCompare.CompareIgnoreCaseToAscii(String(rName)))
705 		{
706 			bRet = sal_True;
707 			break;
708 		}
709 	}
710 	delete pGlosGroup;
711 	return bRet;
712 }
713 
714 /*-- 09.02.00 15:33:30---------------------------------------------------
715 
716   -----------------------------------------------------------------------*/
getPropertySetInfo(void)717 uno::Reference< beans::XPropertySetInfo >  SwXAutoTextGroup::getPropertySetInfo(void)
718 	throw( uno::RuntimeException )
719 {
720     static uno::Reference< beans::XPropertySetInfo >  xRet = pPropSet->getPropertySetInfo();
721 	return xRet;
722 }
723 /*-- 09.02.00 15:33:31---------------------------------------------------
724 
725   -----------------------------------------------------------------------*/
setPropertyValue(const OUString & rPropertyName,const uno::Any & aValue)726 void SwXAutoTextGroup::setPropertyValue(
727 	const OUString& rPropertyName, const uno::Any& aValue)
728 	throw( beans::UnknownPropertyException, beans::PropertyVetoException,
729  		lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
730 {
731 	::vos::OGuard aGuard(Application::GetSolarMutex());
732     const SfxItemPropertySimpleEntry*   pEntry = pPropSet->getPropertyMap()->getByName( rPropertyName );
733 
734     if(!pEntry)
735 		throw beans::UnknownPropertyException();
736 
737 	SwTextBlocks* pGlosGroup = pGlossaries ? pGlossaries->GetGroupDoc(m_sGroupName, sal_False) : 0;
738     if(!pGlosGroup || pGlosGroup->GetError())
739 		throw uno::RuntimeException();
740     switch(pEntry->nWID)
741 	{
742 		case  WID_GROUP_TITLE:
743 		{
744             OUString sNewTitle;
745             aValue >>= sNewTitle;
746             if(!sNewTitle.getLength())
747 				throw lang::IllegalArgumentException();
748             sal_Bool bChanged = !sNewTitle.equals(pGlosGroup->GetName());
749 			pGlosGroup->SetName(sNewTitle);
750 			if(bChanged && HasGlossaryList())
751 				GetGlossaryList()->ClearGroups();
752 		}
753 		break;
754 	}
755 	delete pGlosGroup;
756 }
757 /*-- 09.02.00 15:33:31---------------------------------------------------
758 
759   -----------------------------------------------------------------------*/
getPropertyValue(const OUString & rPropertyName)760 uno::Any SwXAutoTextGroup::getPropertyValue(const OUString& rPropertyName)
761 	throw( beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException )
762 {
763 	::vos::OGuard aGuard(Application::GetSolarMutex());
764     const SfxItemPropertySimpleEntry*   pEntry = pPropSet->getPropertyMap()->getByName( rPropertyName);
765 
766     if(!pEntry)
767 		throw beans::UnknownPropertyException();
768 	SwTextBlocks* pGlosGroup = pGlossaries ? pGlossaries->GetGroupDoc(m_sGroupName, sal_False) : 0;
769     if(!pGlosGroup  || pGlosGroup->GetError())
770 		throw uno::RuntimeException();
771 
772 	uno::Any aAny;
773     switch(pEntry->nWID)
774 	{
775 		case  WID_GROUP_PATH:
776 			aAny <<= OUString(pGlosGroup->GetFileName());
777 		break;
778 		case  WID_GROUP_TITLE:
779 			aAny <<= OUString(pGlosGroup->GetName());
780 		break;
781 	}
782 	delete pGlosGroup;
783 	return aAny;
784 }
785 /*-- 09.02.00 15:33:31---------------------------------------------------
786 
787   -----------------------------------------------------------------------*/
addPropertyChangeListener(const OUString &,const uno::Reference<beans::XPropertyChangeListener> &)788 void SwXAutoTextGroup::addPropertyChangeListener(
789     const OUString& /*PropertyName*/, const uno::Reference< beans::XPropertyChangeListener > & /*aListener*/)
790 	throw( beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException )
791 {
792 }
793 /*-- 09.02.00 15:33:31---------------------------------------------------
794 
795   -----------------------------------------------------------------------*/
removePropertyChangeListener(const OUString &,const uno::Reference<beans::XPropertyChangeListener> &)796 void SwXAutoTextGroup::removePropertyChangeListener(
797     const OUString& /*PropertyName*/, const uno::Reference< beans::XPropertyChangeListener > & /*aListener*/)
798 	throw( beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException )
799 {
800 }
801 /*-- 09.02.00 15:33:32---------------------------------------------------
802 
803   -----------------------------------------------------------------------*/
addVetoableChangeListener(const OUString &,const uno::Reference<beans::XVetoableChangeListener> &)804 void SwXAutoTextGroup::addVetoableChangeListener(
805     const OUString& /*PropertyName*/, const uno::Reference< beans::XVetoableChangeListener > & /*aListener*/)
806 	throw( beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException )
807 {
808 }
809 /*-- 09.02.00 15:33:32---------------------------------------------------
810 
811   -----------------------------------------------------------------------*/
removeVetoableChangeListener(const OUString &,const uno::Reference<beans::XVetoableChangeListener> &)812 void SwXAutoTextGroup::removeVetoableChangeListener(
813     const OUString& /*PropertyName*/, const uno::Reference< beans::XVetoableChangeListener > & /*aListener*/)
814 	throw( beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException )
815 {
816 }
817 /*-- 21.12.98 12:42:27---------------------------------------------------
818 
819   -----------------------------------------------------------------------*/
Invalidate()820 void SwXAutoTextGroup::Invalidate()
821 {
822 	pGlossaries = 0;
823 	sName = aEmptyStr;
824 	m_sGroupName = aEmptyStr;
825 }
826 /* -----------------------------06.04.00 11:11--------------------------------
827 
828  ---------------------------------------------------------------------------*/
getImplementationName(void)829 OUString SwXAutoTextGroup::getImplementationName(void) throw( uno::RuntimeException )
830 {
831 	return C2U("SwXAutoTextGroup");
832 }
833 /* -----------------------------06.04.00 11:11--------------------------------
834 
835  ---------------------------------------------------------------------------*/
supportsService(const OUString & rServiceName)836 sal_Bool SwXAutoTextGroup::supportsService(const OUString& rServiceName) throw( uno::RuntimeException )
837 {
838 	return C2U("com.sun.star.text.AutoTextGroup") == rServiceName;
839 }
840 /* -----------------------------06.04.00 11:11--------------------------------
841 
842  ---------------------------------------------------------------------------*/
getSupportedServiceNames(void)843 uno::Sequence< OUString > SwXAutoTextGroup::getSupportedServiceNames(void) throw( uno::RuntimeException )
844 {
845 	uno::Sequence< OUString > aRet(1);
846 	OUString* pArray = aRet.getArray();
847 	pArray[0] = C2U("com.sun.star.text.AutoTextGroup");
848 	return aRet;
849 }
850 /******************************************************************
851  *
852  ******************************************************************/
853 /* -----------------------------10.03.00 18:02--------------------------------
854 
855  ---------------------------------------------------------------------------*/
getUnoTunnelId()856 const uno::Sequence< sal_Int8 > & SwXAutoTextEntry::getUnoTunnelId()
857 {
858     static uno::Sequence< sal_Int8 > aSeq = ::CreateUnoTunnelId();
859 	return aSeq;
860 }
861 /* -----------------------------10.03.00 18:04--------------------------------
862 
863  ---------------------------------------------------------------------------*/
getSomething(const uno::Sequence<sal_Int8> & rId)864 sal_Int64 SAL_CALL SwXAutoTextEntry::getSomething( const uno::Sequence< sal_Int8 >& rId )
865 	throw(uno::RuntimeException)
866 {
867     if( rId.getLength() == 16
868         && 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(),
869 										rId.getConstArray(), 16 ) )
870     {
871             return sal::static_int_cast< sal_Int64 >( reinterpret_cast< sal_IntPtr >( this ));
872     }
873 	return 0;
874 }
875 /*-- 21.12.98 12:42:33---------------------------------------------------
876 
877   -----------------------------------------------------------------------*/
SwXAutoTextEntry(SwGlossaries * pGlss,const String & rGroupName,const String & rEntryName)878 SwXAutoTextEntry::SwXAutoTextEntry(SwGlossaries* pGlss, const String& rGroupName,
879 											const String& rEntryName) :
880 	pGlossaries(pGlss),
881 	sGroupName(rGroupName),
882 	sEntryName(rEntryName),
883 	pBodyText ( NULL )
884 {
885 }
886 /*-- 21.12.98 12:42:33---------------------------------------------------
887 
888   -----------------------------------------------------------------------*/
~SwXAutoTextEntry()889 SwXAutoTextEntry::~SwXAutoTextEntry()
890 {
891 	{
892 	    ::vos::OGuard aGuard(Application::GetSolarMutex());
893 
894 		// ensure that any pending modifications are written
895 		implFlushDocument( true );
896 
897 		//! Bug #96559
898 		// DocShell must be cleared before mutex is lost.
899 		// Needs to be done explicitly since xDocSh is a class member.
900 		// Thus, an own block here, guarded by the SolarMutex
901 	}
902 }
903 
904 //---------------------------------------------------------------------
905 //--- 03.03.2003 13:24:58 -----------------------------------------------
906 
implFlushDocument(bool _bCloseDoc)907 void SwXAutoTextEntry::implFlushDocument( bool _bCloseDoc )
908 {
909 	if ( xDocSh.Is() )
910 	{
911 		if ( xDocSh->GetDoc()->IsModified () )
912 			xDocSh->Save();
913 
914         if ( _bCloseDoc )
915 		{
916 			// stop listening at the document
917 			EndListening( *&xDocSh );
918 
919 			xDocSh->DoClose();
920 			xDocSh.Clear();
921 		}
922 	}
923 }
924 
925 //-----------------------------------------------------------------------
926 //--- 03.03.2003 15:51:52 -----------------------------------------------
927 
Notify(SfxBroadcaster & _rBC,const SfxHint & _rHint)928 void SwXAutoTextEntry::Notify( SfxBroadcaster& _rBC, const SfxHint& _rHint )
929 {
930 	if ( &_rBC == &xDocSh )
931 	{	// it's our document
932 		if ( _rHint.ISA( SfxSimpleHint ) )
933 		{
934             if ( SFX_HINT_DEINITIALIZING == static_cast< const SfxSimpleHint& >( _rHint ).GetId() )
935 			{
936 				// our document is dying (possibly because we're shuting down, and the document was notified
937 				// earlier than we are?)
938 				// stop listening at the docu
939 				EndListening( *&xDocSh );
940 				// and release our reference
941 				xDocSh.Clear();
942 			}
943 		}
944         else if(_rHint.ISA(SfxEventHint))
945         {
946             if(SFX_EVENT_PREPARECLOSEDOC == static_cast< const SfxEventHint& >( _rHint ).GetEventId())
947             {
948                 implFlushDocument( sal_False );
949                 xBodyText = 0;
950                 xDocSh.Clear();
951             }
952         }
953 	}
954 }
955 
GetBodyText()956 void SwXAutoTextEntry::GetBodyText ()
957 {
958 	::vos::OGuard aGuard(Application::GetSolarMutex());
959 
960 	xDocSh = pGlossaries->EditGroupDoc ( sGroupName, sEntryName, sal_False );
961 	DBG_ASSERT( xDocSh.Is(), "SwXAutoTextEntry::GetBodyText: unexpected: no doc returned by EditGroupDoc!" );
962 
963 	// start listening at the document
964 	StartListening( *&xDocSh );
965 
966 	pBodyText = new SwXBodyText ( xDocSh->GetDoc() );
967 	xBodyText = uno::Reference < lang::XServiceInfo > ( *pBodyText, uno::UNO_QUERY);
968 }
969 
createTextCursor(void)970 uno::Reference< text::XTextCursor >  SwXAutoTextEntry::createTextCursor(void) throw( uno::RuntimeException )
971 {
972 	::vos::OGuard aGuard(Application::GetSolarMutex());
973 	EnsureBodyText();
974 	return pBodyText->createTextCursor();
975 }
976 /*-- 21.12.98 12:42:34---------------------------------------------------
977 
978   -----------------------------------------------------------------------*/
createTextCursorByRange(const uno::Reference<text::XTextRange> & aTextPosition)979 uno::Reference< text::XTextCursor >  SwXAutoTextEntry::createTextCursorByRange(
980 	const uno::Reference< text::XTextRange > & aTextPosition) throw( uno::RuntimeException )
981 {
982 	::vos::OGuard aGuard(Application::GetSolarMutex());
983 	EnsureBodyText();
984 	return pBodyText->createTextCursorByRange ( aTextPosition );
985 }
986 /*-- 21.12.98 12:42:34---------------------------------------------------
987 
988   -----------------------------------------------------------------------*/
insertString(const uno::Reference<text::XTextRange> & xRange,const OUString & aString,sal_Bool bAbsorb)989 void SwXAutoTextEntry::insertString(const uno::Reference< text::XTextRange > & xRange, const OUString& aString, sal_Bool bAbsorb) throw( uno::RuntimeException )
990 {
991 	::vos::OGuard aGuard(Application::GetSolarMutex());
992 	EnsureBodyText();
993 	pBodyText->insertString ( xRange, aString, bAbsorb );
994 }
995 /*-- 21.12.98 12:42:34---------------------------------------------------
996 
997   -----------------------------------------------------------------------*/
insertControlCharacter(const uno::Reference<text::XTextRange> & xRange,sal_Int16 nControlCharacter,sal_Bool bAbsorb)998 void SwXAutoTextEntry::insertControlCharacter(const uno::Reference< text::XTextRange > & xRange,
999 	sal_Int16 nControlCharacter, sal_Bool bAbsorb)
1000 		throw( lang::IllegalArgumentException, uno::RuntimeException )
1001 {
1002 	::vos::OGuard aGuard(Application::GetSolarMutex());
1003 	EnsureBodyText();
1004 	pBodyText->insertControlCharacter ( xRange, nControlCharacter, bAbsorb );
1005 }
1006 /*-- 21.12.98 12:42:34---------------------------------------------------
1007 
1008   -----------------------------------------------------------------------*/
insertTextContent(const uno::Reference<text::XTextRange> & xRange,const uno::Reference<text::XTextContent> & xContent,sal_Bool bAbsorb)1009 void SwXAutoTextEntry::insertTextContent(
1010 	const uno::Reference< text::XTextRange > & xRange,
1011 	const uno::Reference< text::XTextContent > & xContent, sal_Bool bAbsorb)
1012 		throw( lang::IllegalArgumentException, uno::RuntimeException )
1013 {
1014 	::vos::OGuard aGuard(Application::GetSolarMutex());
1015 	EnsureBodyText();
1016 	pBodyText->insertTextContent ( xRange, xContent, bAbsorb );
1017 }
1018 /*-- 21.12.98 12:42:34---------------------------------------------------
1019 
1020   -----------------------------------------------------------------------*/
removeTextContent(const uno::Reference<text::XTextContent> & xContent)1021 void SwXAutoTextEntry::removeTextContent(
1022 	const uno::Reference< text::XTextContent > & xContent)
1023 		throw( container::NoSuchElementException, uno::RuntimeException )
1024 {
1025 	::vos::OGuard aGuard(Application::GetSolarMutex());
1026 	EnsureBodyText();
1027 	pBodyText->removeTextContent ( xContent );
1028 }
1029 /*-- 21.12.98 12:42:35---------------------------------------------------
1030 
1031   -----------------------------------------------------------------------*/
getText(void)1032 uno::Reference< text::XText >  SwXAutoTextEntry::getText(void) throw( uno::RuntimeException )
1033 {
1034 	::vos::OGuard aGuard(Application::GetSolarMutex());
1035 	uno::Reference< text::XText >  xRet =  (text::XText*)this;
1036 	return xRet;
1037 }
1038 /*-- 21.12.98 12:42:35---------------------------------------------------
1039 
1040   -----------------------------------------------------------------------*/
getStart(void)1041 uno::Reference< text::XTextRange >  SwXAutoTextEntry::getStart(void) throw( uno::RuntimeException )
1042 {
1043 	::vos::OGuard aGuard(Application::GetSolarMutex());
1044 	EnsureBodyText();
1045 	return pBodyText->getStart();
1046 }
1047 /*-- 21.12.98 12:42:36---------------------------------------------------
1048 
1049   -----------------------------------------------------------------------*/
getEnd(void)1050 uno::Reference< text::XTextRange >  SwXAutoTextEntry::getEnd(void) throw( uno::RuntimeException )
1051 {
1052 	::vos::OGuard aGuard(Application::GetSolarMutex());
1053 	EnsureBodyText();
1054 	return pBodyText->getEnd();
1055 }
1056 /*-- 21.12.98 12:42:36---------------------------------------------------
1057 
1058   -----------------------------------------------------------------------*/
getString(void)1059 OUString SwXAutoTextEntry::getString(void) throw( uno::RuntimeException )
1060 {
1061 	::vos::OGuard aGuard(Application::GetSolarMutex());
1062 	EnsureBodyText();
1063 	return pBodyText->getString();
1064 }
1065 /*-- 21.12.98 12:42:36---------------------------------------------------
1066 
1067   -----------------------------------------------------------------------*/
setString(const OUString & aString)1068 void SwXAutoTextEntry::setString(const OUString& aString) throw( uno::RuntimeException )
1069 {
1070 	::vos::OGuard aGuard(Application::GetSolarMutex());
1071 	EnsureBodyText();
1072 	pBodyText->setString( aString );
1073 }
1074 /* -----------------15.07.99 10:11-------------------
1075 
1076  --------------------------------------------------*/
applyTo(const uno::Reference<text::XTextRange> & xTextRange)1077 void SwXAutoTextEntry::applyTo(const uno::Reference< text::XTextRange > & xTextRange)throw( uno::RuntimeException )
1078 {
1079 	::vos::OGuard aGuard(Application::GetSolarMutex());
1080 
1081 	// ensure that any pending modifications are written
1082 	// reason is that we're holding the _copy_ of the auto text, while the real auto text
1083 	// is stored somewhere. And below, we're not working with our copy, but only tell the target
1084 	// TextRange to work with the stored version.
1085 	// #96380# - 2003-03-03 - fs@openoffice.org
1086 	implFlushDocument( false );
1087 		// TODO: think about if we should pass "true" here
1088 		// The difference would be that when the next modification is made to this instance here, then
1089 		// we would be forced to open the document again, instead of working on our current copy.
1090 		// This means that we would reflect any changes which were done to the AutoText by foreign instances
1091 		// in the meantime
1092 
1093 	uno::Reference<lang::XUnoTunnel> xTunnel( xTextRange, uno::UNO_QUERY);
1094 	SwXTextRange* pRange = 0;
1095 	OTextCursorHelper* pCursor = 0;
1096 	SwXText *pText = 0;
1097 
1098 	if(xTunnel.is())
1099 	{
1100 		pRange = reinterpret_cast < SwXTextRange* >
1101 				( xTunnel->getSomething( SwXTextRange::getUnoTunnelId() ) );
1102 		pCursor = reinterpret_cast < OTextCursorHelper*>
1103 				( xTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() ) );
1104 		pText = reinterpret_cast < SwXText* >
1105 				( xTunnel->getSomething( SwXText::getUnoTunnelId() ) );
1106 	}
1107 
1108 	SwDoc* pDoc = 0;
1109     if (pRange)
1110 		pDoc = pRange->GetDoc();
1111 	else if ( pCursor )
1112 		pDoc = pCursor->GetDoc();
1113 	else if ( pText && pText->GetDoc() )
1114 	{
1115 		xTunnel = uno::Reference < lang::XUnoTunnel > (pText->getStart(), uno::UNO_QUERY);
1116 		if (xTunnel.is())
1117 		{
1118 			pCursor = reinterpret_cast < OTextCursorHelper* >
1119 				( xTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() ) );
1120 			if (pCursor)
1121 				pDoc = pText->GetDoc();
1122 		}
1123 	}
1124 
1125 	if(!pDoc)
1126 		throw uno::RuntimeException();
1127 
1128     SwPaM InsertPaM(pDoc->GetNodes());
1129     if (pRange)
1130     {
1131         if (!pRange->GetPositions(InsertPaM))
1132         {
1133             throw uno::RuntimeException();
1134         }
1135     }
1136     else
1137     {
1138         InsertPaM = *pCursor->GetPaM();
1139     }
1140 
1141     ::std::auto_ptr<SwTextBlocks> pBlock(pGlossaries->GetGroupDoc(sGroupName));
1142     const bool bResult = pBlock.get() && !pBlock->GetError()
1143                     && pDoc->InsertGlossary( *pBlock, sEntryName, InsertPaM);
1144 
1145 	if(!bResult)
1146 		throw uno::RuntimeException();
1147 }
1148 /* -----------------------------06.04.00 11:11--------------------------------
1149 
1150  ---------------------------------------------------------------------------*/
getImplementationName(void)1151 OUString SwXAutoTextEntry::getImplementationName(void) throw( uno::RuntimeException )
1152 {
1153 	return C2U("SwXAutoTextEntry");
1154 }
1155 /* -----------------------------06.04.00 11:11--------------------------------
1156 
1157  ---------------------------------------------------------------------------*/
supportsService(const OUString & rServiceName)1158 sal_Bool SwXAutoTextEntry::supportsService(const OUString& rServiceName) throw( uno::RuntimeException )
1159 {
1160 	return C2U("com.sun.star.text.AutoTextEntry") == rServiceName;
1161 }
1162 /* -----------------------------06.04.00 11:11--------------------------------
1163 
1164  ---------------------------------------------------------------------------*/
getSupportedServiceNames(void)1165 uno::Sequence< OUString > SwXAutoTextEntry::getSupportedServiceNames(void) throw( uno::RuntimeException )
1166 {
1167 	uno::Sequence< OUString > aRet(1);
1168 	OUString* pArray = aRet.getArray();
1169 	pArray[0] = C2U("com.sun.star.text.AutoTextEntry");
1170 	return aRet;
1171 }
1172 /* -----------------------------06.04.00 11:11--------------------------------
1173 
1174  ---------------------------------------------------------------------------*/
getEvents()1175 uno::Reference< container::XNameReplace > SwXAutoTextEntry::getEvents()
1176 	throw( uno::RuntimeException )
1177 {
1178 	return new SwAutoTextEventDescriptor( *this );
1179 }
1180 /* -----------------------------30.01.01 18:40--------------------------------
1181 
1182  ---------------------------------------------------------------------------*/
1183 const struct SvEventDescription aAutotextEvents[] =
1184 {
1185 	{ SW_EVENT_START_INS_GLOSSARY,	"OnInsertStart" },
1186 	{ SW_EVENT_END_INS_GLOSSARY,	"OnInsertDone" },
1187 	{ 0, NULL }
1188 };
1189 
1190 /* -----------------------------30.01.01 18:40--------------------------------
1191 
1192  ---------------------------------------------------------------------------*/
SwAutoTextEventDescriptor(SwXAutoTextEntry & rAutoText)1193 SwAutoTextEventDescriptor::SwAutoTextEventDescriptor(
1194 	SwXAutoTextEntry& rAutoText ) :
1195 		SvBaseEventDescriptor(aAutotextEvents),
1196 		sSwAutoTextEventDescriptor(RTL_CONSTASCII_USTRINGPARAM(
1197 			"SwAutoTextEventDescriptor")),
1198 		rAutoTextEntry(rAutoText)
1199 {
1200 }
1201 /* -----------------------------30.01.01 18:40--------------------------------
1202 
1203  ---------------------------------------------------------------------------*/
~SwAutoTextEventDescriptor()1204 SwAutoTextEventDescriptor::~SwAutoTextEventDescriptor()
1205 {
1206 }
1207 /* -----------------------------30.01.01 18:40--------------------------------
1208 
1209  ---------------------------------------------------------------------------*/
getImplementationName()1210 OUString SwAutoTextEventDescriptor::getImplementationName()
1211 	throw( uno::RuntimeException )
1212 {
1213 	return sSwAutoTextEventDescriptor;
1214 }
1215 /* -----------------------------30.01.01 18:40--------------------------------
1216 
1217  ---------------------------------------------------------------------------*/
replaceByName(const sal_uInt16 nEvent,const SvxMacro & rMacro)1218 void SwAutoTextEventDescriptor::replaceByName(
1219 	const sal_uInt16 nEvent,
1220 	const SvxMacro& rMacro)
1221 			throw(
1222 				lang::IllegalArgumentException,
1223 				container::NoSuchElementException,
1224 				lang::WrappedTargetException,
1225 				uno::RuntimeException)
1226 {
1227 	DBG_ASSERT( NULL != rAutoTextEntry.GetGlossaries(),
1228 				"Strangely enough, the AutoText vanished!" );
1229 	DBG_ASSERT( (nEvent == SW_EVENT_END_INS_GLOSSARY) ||
1230 				(nEvent == SW_EVENT_START_INS_GLOSSARY) ,
1231 				"Unknown event ID" );
1232 
1233 	const SwGlossaries* pGlossaries = rAutoTextEntry.GetGlossaries();
1234 	SwTextBlocks* pBlocks =
1235 		pGlossaries->GetGroupDoc( rAutoTextEntry.GetGroupName() );
1236 	DBG_ASSERT( NULL != pBlocks,
1237 			    "can't get autotext group; SwAutoTextEntry has illegal name?");
1238 
1239     if( pBlocks && !pBlocks->GetError())
1240 	{
1241 		sal_uInt16 nIndex = pBlocks->GetIndex( rAutoTextEntry.GetEntryName() );
1242 		if( nIndex != USHRT_MAX )
1243 		{
1244 			SvxMacroTableDtor aMacroTable;
1245 			if( pBlocks->GetMacroTable( nIndex, aMacroTable ) )
1246 			{
1247 				SvxMacro* pNewMacro = new SvxMacro(rMacro);
1248 				aMacroTable.Replace( nEvent, pNewMacro );
1249 				pBlocks->SetMacroTable( nIndex, aMacroTable );
1250 			}
1251 		}
1252 
1253 		delete pBlocks;
1254 	}
1255 	// else: ignore
1256 }
1257 /* -----------------------------30.01.01 18:40--------------------------------
1258 
1259  ---------------------------------------------------------------------------*/
getByName(SvxMacro & rMacro,const sal_uInt16 nEvent)1260 void SwAutoTextEventDescriptor::getByName(
1261 	SvxMacro& rMacro,
1262 	const sal_uInt16 nEvent )
1263 			throw(
1264 				container::NoSuchElementException,
1265 				lang::WrappedTargetException,
1266 				uno::RuntimeException)
1267 {
1268 	DBG_ASSERT( NULL != rAutoTextEntry.GetGlossaries(), "no AutoText" );
1269 	DBG_ASSERT( (nEvent == SW_EVENT_END_INS_GLOSSARY) ||
1270 				(nEvent == SW_EVENT_START_INS_GLOSSARY) ,
1271 				"Unknown event ID" );
1272 
1273 	const SwGlossaries* pGlossaries = rAutoTextEntry.GetGlossaries();
1274 	SwTextBlocks* pBlocks =
1275 		pGlossaries->GetGroupDoc( rAutoTextEntry.GetGroupName() );
1276 	DBG_ASSERT( NULL != pBlocks,
1277 			    "can't get autotext group; SwAutoTextEntry has illegal name?");
1278 
1279 	// return empty macro, unless macro is found
1280     OUString sEmptyStr;
1281     SvxMacro aEmptyMacro(sEmptyStr, sEmptyStr);
1282 	rMacro = aEmptyMacro;
1283 
1284     if ( pBlocks &&  !pBlocks->GetError())
1285 	{
1286 		sal_uInt16 nIndex = pBlocks->GetIndex( rAutoTextEntry.GetEntryName() );
1287 		if( nIndex != USHRT_MAX )
1288 		{
1289 			SvxMacroTableDtor aMacroTable;
1290 			if( pBlocks->GetMacroTable( nIndex, aMacroTable ) )
1291 			{
1292 				SvxMacro *pMacro = aMacroTable.Get( nEvent );
1293 				if( pMacro )
1294 					rMacro = *pMacro;
1295 			}
1296 		}
1297 
1298 		delete pBlocks;
1299 	}
1300 }
1301 
1302 
1303 
1304 
1305