xref: /trunk/main/sw/source/core/fields/authfld.cxx (revision 86e1cf34)
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_STRINGSDTOR
29 #define _SVSTDARR_USHORTS
30 #define _SVSTDARR_LONGS
31 #define _SVSTDARR_ULONGS
32 #include <hintids.hxx>
33 
34 #include <svl/svstdarr.hxx>
35 #include <editeng/unolingu.hxx>
36 #include <editeng/langitem.hxx>
37 #include <swtypes.hxx>
38 #include <tools/resid.hxx>
39 #include <comcore.hrc>
40 #include <authfld.hxx>
41 #include <expfld.hxx>
42 #include <pam.hxx>
43 #include <cntfrm.hxx>
44 #include <tox.hxx>
45 #include <txmsrt.hxx>
46 #include <doctxm.hxx>
47 #include <fmtfld.hxx>
48 #include <txtfld.hxx>
49 #include <ndtxt.hxx>
50 #include <doc.hxx>
51 #include <unofldmid.h>
52 #include <unoprnms.hxx>
53 #include <switerator.hxx>
54 #include <unomid.h>
55 
56 using namespace ::com::sun::star::uno;
57 using namespace ::com::sun::star::beans;
58 using namespace ::com::sun::star::lang;
59 using rtl::OUString;
60 
61 
62 typedef SwAuthEntry* SwAuthEntryPtr;
63 SV_DECL_PTRARR_DEL( SwAuthDataArr, SwAuthEntryPtr, 5, 5 )
64 SV_IMPL_PTRARR( SwAuthDataArr, SwAuthEntryPtr )
65 
66 
67 typedef SwTOXSortKey* TOXSortKeyPtr;
68 SV_DECL_PTRARR_DEL( SortKeyArr, TOXSortKeyPtr, 5, 5 )
SV_IMPL_PTRARR(SortKeyArr,TOXSortKeyPtr)69 SV_IMPL_PTRARR( SortKeyArr, TOXSortKeyPtr )
70 
71 
72 SwAuthEntry::SwAuthEntry(const SwAuthEntry& rCopy)
73 	: nRefCount(0)
74 {
75 	for(sal_uInt16 i = 0; i < AUTH_FIELD_END; i++)
76 		aAuthFields[i] = rCopy.aAuthFields[i];
77 }
78 // --------------------------------------------------------
operator ==(const SwAuthEntry & rComp)79 sal_Bool 	SwAuthEntry::operator==(const SwAuthEntry& rComp)
80 {
81 	for(sal_uInt16 i = 0; i < AUTH_FIELD_END; i++)
82 		if(aAuthFields[i] != rComp.aAuthFields[i])
83 			return sal_False;
84 	return sal_True;
85 }
86 // --------------------------------------------------------
SwAuthorityFieldType(SwDoc * pDoc)87 SwAuthorityFieldType::SwAuthorityFieldType(SwDoc* pDoc)
88 	: SwFieldType( RES_AUTHORITY ),
89 	m_pDoc(pDoc),
90 	m_pDataArr(new SwAuthDataArr ),
91 	m_pSequArr(new SvLongs(5, 5)),
92 	m_pSortKeyArr(new SortKeyArr(3, 3)),
93 	m_cPrefix('['),
94     m_cSuffix(']'),
95 	m_bIsSequence(sal_False),
96 	m_bSortByDocument(sal_True),
97     m_eLanguage((LanguageType)::GetAppLanguage())
98 {
99 }
100 
SwAuthorityFieldType(const SwAuthorityFieldType & rFType)101 SwAuthorityFieldType::SwAuthorityFieldType( const SwAuthorityFieldType& rFType)
102 	: SwFieldType( RES_AUTHORITY ),
103 	m_pDataArr(new SwAuthDataArr ),
104 	m_pSequArr(new SvLongs(5, 5)),
105 	m_pSortKeyArr(new SortKeyArr(3, 3)),
106 	m_cPrefix(rFType.m_cPrefix),
107     m_cSuffix(rFType.m_cSuffix),
108 	m_bIsSequence(rFType.m_bIsSequence),
109 	m_bSortByDocument(rFType.m_bSortByDocument),
110     m_eLanguage(rFType.m_eLanguage),
111     m_sSortAlgorithm(rFType.m_sSortAlgorithm)
112 {
113 	for(sal_uInt16 i = 0; i < rFType.m_pSortKeyArr->Count(); i++)
114 		m_pSortKeyArr->Insert((*rFType.m_pSortKeyArr)[i], i);
115 }
116 
~SwAuthorityFieldType()117 SwAuthorityFieldType::~SwAuthorityFieldType()
118 {
119 //	DBG_ASSERT(!m_pDataArr->Count(), "Array is not empty");
120 	m_pSortKeyArr->DeleteAndDestroy(0, m_pSortKeyArr->Count());
121 	delete m_pSortKeyArr;
122 	delete m_pSequArr;
123 	delete m_pDataArr;
124 }
125 
Copy() const126 SwFieldType* 	SwAuthorityFieldType::Copy()  const
127 {
128 	return new SwAuthorityFieldType(m_pDoc);
129 }
130 
RemoveField(long nHandle)131 void	SwAuthorityFieldType::RemoveField(long nHandle)
132 {
133 #ifdef DBG_UTIL
134 	sal_Bool bRemoved = sal_False;
135 #endif
136 	for(sal_uInt16 j = 0; j < m_pDataArr->Count(); j++)
137 	{
138 		SwAuthEntry* pTemp = m_pDataArr->GetObject(j);
139 		long nRet = (long)(void*)pTemp;
140 		if(nRet == nHandle)
141 		{
142 #ifdef DBG_UTIL
143 			bRemoved = sal_True;
144 #endif
145 			pTemp->RemoveRef();
146 			if(!pTemp->GetRefCount())
147 			{
148 				m_pDataArr->DeleteAndDestroy(j, 1);
149 				//re-generate positions of the fields
150 				DelSequenceArray();
151 			}
152 			break;
153 		}
154 	}
155 #ifdef DBG_UTIL
156 	DBG_ASSERT(bRemoved, "Field unknown" );
157 #endif
158 }
159 
AddField(const String & rFieldContents)160 long	SwAuthorityFieldType::AddField(const String& rFieldContents)
161 {
162 	long nRet = 0;
163 	SwAuthEntry* pEntry = new SwAuthEntry;
164 	for( sal_uInt16 i = 0; i < AUTH_FIELD_END; ++i )
165 		pEntry->SetAuthorField( (ToxAuthorityField)i,
166 						rFieldContents.GetToken( i, TOX_STYLE_DELIMITER ));
167 
168 	for(sal_uInt16 j = 0; j < m_pDataArr->Count() && pEntry; j++)
169 	{
170 		SwAuthEntry* pTemp = m_pDataArr->GetObject(j);
171 		if(*pTemp == *pEntry)
172 		{
173 			DELETEZ(pEntry);
174 			nRet = (long)(void*)pTemp;
175 			pTemp->AddRef();
176 		}
177 	}
178 	//if it is a new Entry - insert
179 	if(pEntry)
180 	{
181 		nRet = (long)(void*)pEntry;
182 		pEntry->AddRef();
183 		m_pDataArr->Insert(pEntry, m_pDataArr->Count());
184 		//re-generate positions of the fields
185 		DelSequenceArray();
186 	}
187 	return nRet;
188 }
189 
AddField(long nHandle)190 sal_Bool SwAuthorityFieldType::AddField(long nHandle)
191 {
192 	sal_Bool bRet = sal_False;
193 	for( sal_uInt16 j = 0; j < m_pDataArr->Count(); j++ )
194 	{
195 		SwAuthEntry* pTemp = m_pDataArr->GetObject(j);
196 		long nTmp = (long)(void*)pTemp;
197 		if( nTmp == nHandle )
198 		{
199 			bRet = sal_True;
200 			pTemp->AddRef();
201 			//re-generate positions of the fields
202 			DelSequenceArray();
203 			break;
204 		}
205 	}
206 	DBG_ASSERT(bRet, "::AddField(long) failed");
207 	return bRet;
208 }
209 
GetEntryByHandle(long nHandle) const210 const SwAuthEntry*	SwAuthorityFieldType::GetEntryByHandle(long nHandle) const
211 {
212 	const SwAuthEntry* pRet = 0;
213 	for(sal_uInt16 j = 0; j < m_pDataArr->Count(); j++)
214 	{
215 		const SwAuthEntry* pTemp = m_pDataArr->GetObject(j);
216 		long nTmp = (long)(void*)pTemp;
217 		if( nTmp == nHandle )
218 		{
219 			pRet = pTemp;
220 			break;
221 		}
222 	}
223 	ASSERT( pRet, "invalid Handle" );
224 	return pRet;
225 }
226 
GetAllEntryIdentifiers(SvStringsDtor & rToFill) const227 void SwAuthorityFieldType::GetAllEntryIdentifiers(
228 				SvStringsDtor& rToFill )const
229 {
230 	for(sal_uInt16 j = 0; j < m_pDataArr->Count(); j++)
231 	{
232 		SwAuthEntry* pTemp = m_pDataArr->GetObject(j);
233 		rToFill.Insert( new String( pTemp->GetAuthorField(
234 					AUTH_FIELD_IDENTIFIER )), rToFill.Count() );
235 	}
236 }
237 
GetEntryByIdentifier(const String & rIdentifier) const238 const SwAuthEntry* 	SwAuthorityFieldType::GetEntryByIdentifier(
239 								const String& rIdentifier)const
240 {
241 	const SwAuthEntry* pRet = 0;
242 	for( sal_uInt16 j = 0; j < m_pDataArr->Count(); ++j )
243 	{
244 		const SwAuthEntry* pTemp = m_pDataArr->GetObject(j);
245 		if( rIdentifier == pTemp->GetAuthorField( AUTH_FIELD_IDENTIFIER ))
246 		{
247 			pRet = pTemp;
248 			break;
249 		}
250 	}
251 	return pRet;
252 }
253 
ChangeEntryContent(const SwAuthEntry * pNewEntry)254 bool SwAuthorityFieldType::ChangeEntryContent(const SwAuthEntry* pNewEntry)
255 {
256     bool bChanged = false;
257 	for( sal_uInt16 j = 0; j < m_pDataArr->Count(); ++j )
258 	{
259 		SwAuthEntry* pTemp = m_pDataArr->GetObject(j);
260 		if(pTemp->GetAuthorField(AUTH_FIELD_IDENTIFIER) ==
261 					pNewEntry->GetAuthorField(AUTH_FIELD_IDENTIFIER))
262 		{
263 			for(sal_uInt16 i = 0; i < AUTH_FIELD_END; i++)
264 				pTemp->SetAuthorField((ToxAuthorityField) i,
265 					pNewEntry->GetAuthorField((ToxAuthorityField)i));
266             bChanged = true;
267 			break;
268 		}
269 	}
270     return bChanged;
271 }
272 
273 /*-------------------------------------------------------------------------
274   appends a new entry (if new) and returns the array position
275   -----------------------------------------------------------------------*/
AppendField(const SwAuthEntry & rInsert)276 sal_uInt16	SwAuthorityFieldType::AppendField( const SwAuthEntry& rInsert )
277 {
278 	sal_uInt16 nRet = 0;
279 	for( nRet = 0; nRet < m_pDataArr->Count(); ++nRet )
280 	{
281 		SwAuthEntry* pTemp = m_pDataArr->GetObject( nRet );
282 		if( *pTemp == rInsert )
283 		{
284 			break;
285 			//ref count unchanged
286 		}
287 	}
288 
289 	//if it is a new Entry - insert
290 	if( nRet == m_pDataArr->Count() )
291 		m_pDataArr->Insert( new SwAuthEntry( rInsert ), nRet );
292 
293 	return nRet;
294 }
295 
GetHandle(sal_uInt16 nPos)296 long	SwAuthorityFieldType::GetHandle(sal_uInt16 nPos)
297 {
298 	long nRet = 0;
299 	if( nPos < m_pDataArr->Count() )
300 	{
301 		SwAuthEntry* pTemp = m_pDataArr->GetObject(nPos);
302 		nRet = (long)(void*)pTemp;
303 	}
304 	return nRet;
305 }
306 
GetSequencePos(long nHandle)307 sal_uInt16	SwAuthorityFieldType::GetSequencePos(long nHandle)
308 {
309 	//find the field in a sorted array of handles,
310 #ifdef DBG_UTIL
311 	sal_Bool bCurrentFieldWithoutTextNode = sal_False;
312 #endif
313 	if(m_pSequArr->Count() && m_pSequArr->Count() != m_pDataArr->Count())
314 		DelSequenceArray();
315 	if(!m_pSequArr->Count())
316 	{
317 		SwTOXSortTabBases aSortArr;
318 		SwIterator<SwFmtFld,SwFieldType> aIter( *this );
319 
320         SwTOXInternational aIntl(m_eLanguage, 0, m_sSortAlgorithm);
321 
322 		for( SwFmtFld* pFmtFld = aIter.First(); pFmtFld; pFmtFld = aIter.Next() )
323 		{
324             const SwTxtFld* pTxtFld = pFmtFld->GetTxtFld();
325             if(!pTxtFld || !pTxtFld->GetpTxtNode())
326 			{
327 #ifdef DBG_UTIL
328                 if(nHandle == ((SwAuthorityField*)pFmtFld->GetField())->GetHandle())
329 					bCurrentFieldWithoutTextNode = sal_True;
330 #endif
331 				continue;
332 			}
333             const SwTxtNode& rFldTxtNode = pTxtFld->GetTxtNode();
334             SwPosition aFldPos(rFldTxtNode);
335             SwDoc& rDoc = *(SwDoc*)rFldTxtNode.GetDoc();
336             SwCntntFrm *pFrm = rFldTxtNode.getLayoutFrm( rDoc.GetCurrentLayout() );
337             const SwTxtNode* pTxtNode = 0;
338             if(pFrm && !pFrm->IsInDocBody())
339                 pTxtNode = GetBodyTxtNode( rDoc, aFldPos, *pFrm );
340             //if no text node could be found or the field is in the document
341             //body the directly available text node will be used
342             if(!pTxtNode)
343                 pTxtNode = &rFldTxtNode;
344             if( pTxtNode->GetTxt().Len() && pTxtNode->getLayoutFrm( rDoc.GetCurrentLayout() ) &&
345                 pTxtNode->GetNodes().IsDocNodes() )
346 			{
347                 SwTOXAuthority* pNew = new SwTOXAuthority( *pTxtNode,
348 															*pFmtFld, aIntl );
349 
350 				for(short i = 0; i < aSortArr.Count(); ++i)
351 				{
352 					SwTOXSortTabBase* pOld = aSortArr[i];
353 					if(*pOld == *pNew)
354 					{
355 						//only the first occurrence in the document
356 						//has to be in the array
357 						if(*pOld < *pNew)
358 							DELETEZ(pNew);
359 						else // remove the old content
360 							aSortArr.DeleteAndDestroy( i, 1 );
361 						break;
362 					}
363 				}
364 				//if it still exists - insert at the correct position
365 				if(pNew)
366 				{
367 					short j;
368 
369 					for( j = 0; j < aSortArr.Count(); ++j)
370 					{
371 						SwTOXSortTabBase* pOld = aSortArr[j];
372 						if(*pNew < *pOld)
373 							break;
374 					}
375 					aSortArr.Insert(pNew, j );
376 				}
377 			}
378 		}
379 
380 		for(sal_uInt16 i = 0; i < aSortArr.Count(); i++)
381 		{
382 			const SwTOXSortTabBase& rBase = *aSortArr[i];
383 			SwFmtFld& rFmtFld = ((SwTOXAuthority&)rBase).GetFldFmt();
384 			SwAuthorityField* pAFld = (SwAuthorityField*)rFmtFld.GetField();
385 			m_pSequArr->Insert(pAFld->GetHandle(), i);
386 		}
387 		aSortArr.DeleteAndDestroy(0, aSortArr.Count());
388 	}
389 	//find nHandle
390 	sal_uInt16 nRet = 0;
391 	for(sal_uInt16 i = 0; i < m_pSequArr->Count(); i++)
392 	{
393 		if((*m_pSequArr)[i] == nHandle)
394 		{
395 			nRet = i + 1;
396 			break;
397 		}
398 	}
399 	ASSERT(bCurrentFieldWithoutTextNode || nRet, "Handle not found")
400 	return nRet;
401 }
402 
QueryValue(Any & rVal,sal_uInt16 nWhichId) const403 sal_Bool    SwAuthorityFieldType::QueryValue( Any& rVal, sal_uInt16 nWhichId ) const
404 {
405     switch( nWhichId )
406 	{
407 	case FIELD_PROP_PAR1:
408 	case FIELD_PROP_PAR2:
409 		{
410 			OUString sVal;
411             sal_Unicode uRet = FIELD_PROP_PAR1 == nWhichId ? m_cPrefix : m_cSuffix;
412 			if(uRet)
413 				sVal = OUString(uRet);
414 			rVal <<= sVal;
415 		}
416 		break;
417 	case FIELD_PROP_PAR3:
418         rVal <<= OUString(GetSortAlgorithm());
419 		break;
420 
421 	case FIELD_PROP_BOOL1:
422 	case FIELD_PROP_BOOL2:
423 		{
424             sal_Bool bVal = FIELD_PROP_BOOL1 == nWhichId ? m_bIsSequence: m_bSortByDocument;
425 			rVal.setValue(&bVal, ::getBooleanCppuType());
426 		}
427 		break;
428 
429 	case FIELD_PROP_LOCALE:
430         rVal <<= SvxCreateLocale(GetLanguage());
431 		break;
432 
433 	case FIELD_PROP_PROP_SEQ:
434 		{
435 			Sequence<PropertyValues> aRet(m_pSortKeyArr->Count());
436 			PropertyValues* pValues = aRet.getArray();
437 			OUString sProp1( C2U(SW_PROP_NAME_STR(UNO_NAME_SORT_KEY)) ),
438 					 sProp2( C2U(SW_PROP_NAME_STR(UNO_NAME_IS_SORT_ASCENDING)));
439 			for(sal_uInt16 i = 0; i < m_pSortKeyArr->Count(); i++)
440 			{
441 				const SwTOXSortKey*	pKey = (*m_pSortKeyArr)[i];
442 				pValues[i].realloc(2);
443 				PropertyValue* pValue = pValues[i].getArray();
444 				pValue[0].Name = sProp1;
445 				pValue[0].Value <<= sal_Int16(pKey->eField);
446 				pValue[1].Name = sProp2;
447 				pValue[1].Value.setValue(&pKey->bSortAscending, ::getBooleanCppuType());
448 			}
449 			rVal <<= aRet;
450 		}
451 		break;
452 	default:
453 		DBG_ERROR("illegal property");
454 	}
455 	return sal_True;
456 }
457 
PutValue(const Any & rAny,sal_uInt16 nWhichId)458 sal_Bool    SwAuthorityFieldType::PutValue( const Any& rAny, sal_uInt16 nWhichId )
459 {
460 	sal_Bool bRet = sal_True;
461 	String sTmp;
462     switch( nWhichId )
463 	{
464 	case FIELD_PROP_PAR1:
465 	case FIELD_PROP_PAR2:
466     {
467         ::GetString( rAny, sTmp );
468         sal_Unicode uSet = sTmp.GetChar(0);
469         if( FIELD_PROP_PAR1 == nWhichId )
470             m_cPrefix = uSet;
471         else
472             m_cSuffix = uSet;
473     }
474     break;
475 	case FIELD_PROP_PAR3:
476 		SetSortAlgorithm( ::GetString( rAny, sTmp ));
477 		break;
478 
479 	case FIELD_PROP_BOOL1:
480 		m_bIsSequence = *(sal_Bool*)rAny.getValue();
481 		break;
482 	case FIELD_PROP_BOOL2:
483 		m_bSortByDocument = *(sal_Bool*)rAny.getValue();
484 		break;
485 
486 	case FIELD_PROP_LOCALE:
487 		{
488 	        Locale aLocale;
489     	    if( 0 != (bRet = rAny >>= aLocale ))
490 	            SetLanguage( SvxLocaleToLanguage( aLocale ));
491 		}
492 		break;
493 
494 	case FIELD_PROP_PROP_SEQ:
495 		{
496 			Sequence<PropertyValues> aSeq;
497 			if( 0 != (bRet = rAny >>= aSeq) )
498 			{
499 				m_pSortKeyArr->DeleteAndDestroy(0, m_pSortKeyArr->Count());
500 				const PropertyValues* pValues = aSeq.getConstArray();
501 				for(sal_Int32 i = 0; i < aSeq.getLength() && i < USHRT_MAX / 4; i++)
502 				{
503 					const PropertyValue* pValue = pValues[i].getConstArray();
504 					SwTOXSortKey* pSortKey = new SwTOXSortKey;
505 					for(sal_Int32 j = 0; j < pValues[i].getLength(); j++)
506 					{
507 						if(pValue[j].Name.equalsAsciiL(SW_PROP_NAME(UNO_NAME_SORT_KEY)))
508 						{
509 							sal_Int16 nVal = -1; pValue[j].Value >>= nVal;
510 							if(nVal >= 0 && nVal < AUTH_FIELD_END)
511 								pSortKey->eField = (ToxAuthorityField) nVal;
512 							else
513 								bRet = sal_False;
514 						}
515 						else if(pValue[j].Name.equalsAsciiL(SW_PROP_NAME(UNO_NAME_IS_SORT_ASCENDING)))
516 						{
517 							pSortKey->bSortAscending = *(sal_Bool*)pValue[j].Value.getValue();
518 						}
519 					}
520 					m_pSortKeyArr->Insert(pSortKey, m_pSortKeyArr->Count());
521 				}
522 			}
523 		}
524 		break;
525 	default:
526 		DBG_ERROR("illegal property");
527 	}
528 	return bRet;
529 }
530 
Modify(const SfxPoolItem * pOld,const SfxPoolItem * pNew)531 void SwAuthorityFieldType::Modify( const SfxPoolItem* pOld, const SfxPoolItem *pNew )
532 {
533 	//re-generate positions of the fields
534 	DelSequenceArray();
535 	NotifyClients( pOld, pNew );
536 }
537 
GetSortKeyCount() const538 sal_uInt16 SwAuthorityFieldType::GetSortKeyCount() const
539 {
540 	return m_pSortKeyArr->Count();
541 }
542 
GetSortKey(sal_uInt16 nIdx) const543 const SwTOXSortKey*  SwAuthorityFieldType::GetSortKey(sal_uInt16 nIdx) const
544 {
545 	SwTOXSortKey* pRet = 0;
546 	if(m_pSortKeyArr->Count() > nIdx)
547 		pRet = (*m_pSortKeyArr)[nIdx];
548 	DBG_ASSERT(pRet, "Sort key not found");
549 	return pRet;
550 }
551 
SetSortKeys(sal_uInt16 nKeyCount,SwTOXSortKey aKeys[])552 void SwAuthorityFieldType::SetSortKeys(sal_uInt16 nKeyCount, SwTOXSortKey aKeys[])
553 {
554 	m_pSortKeyArr->DeleteAndDestroy(0, m_pSortKeyArr->Count());
555 	sal_uInt16 nArrIdx = 0;
556 	for(sal_uInt16 i = 0; i < nKeyCount; i++)
557 		if(aKeys[i].eField < AUTH_FIELD_END)
558 			m_pSortKeyArr->Insert(new SwTOXSortKey(aKeys[i]), nArrIdx++);
559 }
560 
SwAuthorityField(SwAuthorityFieldType * pInitType,const String & rFieldContents)561 SwAuthorityField::SwAuthorityField( SwAuthorityFieldType* pInitType,
562 									const String& rFieldContents )
563     : SwField(pInitType),
564     m_nTempSequencePos( -1 )
565 {
566     m_nHandle = pInitType->AddField( rFieldContents );
567 }
568 
SwAuthorityField(SwAuthorityFieldType * pInitType,long nSetHandle)569 SwAuthorityField::SwAuthorityField( SwAuthorityFieldType* pInitType,
570 												long nSetHandle )
571 	: SwField( pInitType ),
572     m_nHandle( nSetHandle ),
573     m_nTempSequencePos( -1 )
574 {
575     pInitType->AddField( m_nHandle );
576 }
577 
~SwAuthorityField()578 SwAuthorityField::~SwAuthorityField()
579 {
580     ((SwAuthorityFieldType* )GetTyp())->RemoveField(m_nHandle);
581 }
582 
Expand() const583 String	SwAuthorityField::Expand() const
584 {
585 	SwAuthorityFieldType* pAuthType = (SwAuthorityFieldType*)GetTyp();
586 	String sRet;
587 	if(pAuthType->GetPrefix())
588 		sRet.Assign(pAuthType->GetPrefix());
589 
590 	if( pAuthType->IsSequence() )
591 	{
592        if(!pAuthType->GetDoc()->IsExpFldsLocked())
593            m_nTempSequencePos = pAuthType->GetSequencePos( m_nHandle );
594        if( m_nTempSequencePos >= 0 )
595            sRet += String::CreateFromInt32( m_nTempSequencePos );
596 	}
597 	else
598 	{
599         const SwAuthEntry* pEntry = pAuthType->GetEntryByHandle(m_nHandle);
600 		//TODO: Expand to: identifier, number sequence, ...
601 		if(pEntry)
602 			sRet += pEntry->GetAuthorField(AUTH_FIELD_IDENTIFIER);
603 	}
604 	if(pAuthType->GetSuffix())
605 		sRet += pAuthType->GetSuffix();
606 	return sRet;
607 }
608 
Copy() const609 SwField* SwAuthorityField::Copy() const
610 {
611 	SwAuthorityFieldType* pAuthType = (SwAuthorityFieldType*)GetTyp();
612     return new SwAuthorityField(pAuthType, m_nHandle);
613 }
614 
GetFieldText(ToxAuthorityField eField) const615 const String&	SwAuthorityField::GetFieldText(ToxAuthorityField eField) const
616 {
617 	SwAuthorityFieldType* pAuthType = (SwAuthorityFieldType*)GetTyp();
618     const SwAuthEntry* pEntry = pAuthType->GetEntryByHandle( m_nHandle );
619 	return pEntry->GetAuthorField( eField );
620 }
621 
SetPar1(const String & rStr)622 void	SwAuthorityField::SetPar1(const String& rStr)
623 {
624 	SwAuthorityFieldType* pInitType = (SwAuthorityFieldType* )GetTyp();
625     pInitType->RemoveField(m_nHandle);
626     m_nHandle = pInitType->AddField(rStr);
627 }
628 
GetDescription() const629 String SwAuthorityField::GetDescription() const
630 {
631     return SW_RES(STR_AUTHORITY_ENTRY);
632 }
633 
634 
635 const char* aFieldNames[] =
636 {
637 	"Identifier",
638 	"BibiliographicType",
639 	"Address",
640 	"Annote",
641 	"Author",
642 	"Booktitle",
643 	"Chapter",
644 	"Edition",
645 	"Editor",
646 	"Howpublished",
647 	"Institution",
648 	"Journal",
649 	"Month",
650 	"Note",
651 	"Number",
652 	"Organizations",
653 	"Pages",
654 	"Publisher",
655 	"School",
656 	"Series",
657 	"Title",
658 	"Report_Type",
659 	"Volume",
660 	"Year",
661 	"URL",
662 	"Custom1",
663 	"Custom2",
664 	"Custom3",
665 	"Custom4",
666 	"Custom5",
667 	"ISBN"
668 };
669 
QueryValue(Any & rAny,sal_uInt16) const670 sal_Bool    SwAuthorityField::QueryValue( Any& rAny, sal_uInt16 /*nWhichId*/ ) const
671 {
672 	if(!GetTyp())
673 		return sal_False;
674     const SwAuthEntry* pAuthEntry = ((SwAuthorityFieldType*)GetTyp())->GetEntryByHandle(m_nHandle);
675 	if(!pAuthEntry)
676 		return sal_False;
677 	Sequence <PropertyValue> aRet(AUTH_FIELD_END);
678 	PropertyValue* pValues = aRet.getArray();
679 	for(sal_Int16 i = 0; i < AUTH_FIELD_END; i++)
680 	{
681 		pValues[i].Name = C2U(aFieldNames[i]);
682 		const String& rField = pAuthEntry->GetAuthorField((ToxAuthorityField) i);
683 		if(i == AUTH_FIELD_AUTHORITY_TYPE)
684 			pValues[i].Value <<= sal_Int16(rField.ToInt32());
685 		else
686 			pValues[i].Value <<= OUString(rField);
687 	}
688 	rAny <<= aRet;
689 	return sal_False;
690 }
691 
lcl_Find(const OUString & rFieldName)692 sal_Int16 lcl_Find(const OUString& rFieldName)
693 {
694 	for(sal_Int16 i = 0; i < AUTH_FIELD_END; i++)
695 		if(!rFieldName.compareToAscii(aFieldNames[i]))
696 			return i;
697 	return -1;
698 }
699 //----------------------------------------------------------------------------
PutValue(const Any & rAny,sal_uInt16)700 sal_Bool    SwAuthorityField::PutValue( const Any& rAny, sal_uInt16 /*nWhichId*/ )
701 {
702     if(!GetTyp() || !((SwAuthorityFieldType*)GetTyp())->GetEntryByHandle(m_nHandle))
703 		return sal_False;
704 
705 	Sequence <PropertyValue> aParam;
706 	if(!(rAny >>= aParam))
707 		return sal_False;
708 
709 	String sToSet;
710 	sToSet.Fill(AUTH_FIELD_ISBN, TOX_STYLE_DELIMITER);
711 	const PropertyValue* pParam = aParam.getConstArray();
712 	for(sal_Int32 i = 0; i < aParam.getLength(); i++)
713 	{
714 		sal_Int16 nFound = lcl_Find(pParam[i].Name);
715 		if(nFound >= 0)
716 		{
717 			OUString sContent;
718 			if(AUTH_FIELD_AUTHORITY_TYPE == nFound)
719 			{
720 				sal_Int16 nVal = 0;
721 				pParam[i].Value >>= nVal;
722 				sContent = OUString::valueOf((sal_Int32)nVal);
723 			}
724 			else
725 				pParam[i].Value >>= sContent;
726 			sToSet.SetToken(nFound, TOX_STYLE_DELIMITER, sContent);
727 		}
728 	}
729 
730     ((SwAuthorityFieldType*)GetTyp())->RemoveField(m_nHandle);
731     m_nHandle = ((SwAuthorityFieldType*)GetTyp())->AddField(sToSet);
732 
733 	return sal_False;
734 }
735 
ChgTyp(SwFieldType * pFldTyp)736 SwFieldType* SwAuthorityField::ChgTyp( SwFieldType* pFldTyp )
737 {
738 	SwAuthorityFieldType* pSrcTyp = (SwAuthorityFieldType*)GetTyp(),
739 						* pDstTyp = (SwAuthorityFieldType*)pFldTyp;
740 	if( pSrcTyp != pDstTyp )
741 	{
742 
743         const SwAuthEntry* pEntry = pSrcTyp->GetEntryByHandle( m_nHandle );
744 		sal_uInt16 nHdlPos = pDstTyp->AppendField( *pEntry );
745         pSrcTyp->RemoveField( m_nHandle );
746         m_nHandle = pDstTyp->GetHandle( nHdlPos );
747         pDstTyp->AddField( m_nHandle );
748 		SwField::ChgTyp( pFldTyp );
749 	}
750 	return pSrcTyp;
751 }
752 
753