1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <svl/slstitm.hxx>
22 #include <svl/poolitem.hxx>
23 #include <com/sun/star/uno/Any.hxx>
24 #include <com/sun/star/uno/Sequence.hxx>
25 #include <tools/stream.hxx>
27 // STATIC DATA -----------------------------------------------------------
29 DBG_NAME(SfxStringListItem
)
31 // -----------------------------------------------------------------------
33 TYPEINIT1_AUTOFACTORY(SfxStringListItem
, SfxPoolItem
);
35 class SfxImpStringList
39 std::vector
<OUString
> aList
;
41 SfxImpStringList() { nRefCount
= 1; }
46 SfxImpStringList::~SfxImpStringList()
48 DBG_ASSERT(nRefCount
!=0xffff,"ImpList already deleted");
52 // class SfxStringListItem -----------------------------------------------
54 SfxStringListItem::SfxStringListItem() :
60 SfxStringListItem::SfxStringListItem( sal_uInt16 which
, const std::vector
<OUString
>* pList
) :
64 // PB: das Putten einer leeren Liste funktionierte nicht,
65 // deshalb habe ich hier die Abfrage nach dem Count auskommentiert
66 if( pList
/*!!! && pList->Count() */ )
68 pImp
= new SfxImpStringList
;
76 SfxStringListItem::SfxStringListItem( sal_uInt16 which
, SvStream
& rStream
) :
80 //fdo#39428 SvStream no longer supports operator>>(long&)
81 sal_Int32 nEntryCount
;
82 rStream
>> nEntryCount
;
85 pImp
= new SfxImpStringList
;
89 for( sal_Int32 i
=0; i
< nEntryCount
; i
++ )
91 pImp
->aList
.push_back( readByteString(rStream
) );
97 SfxStringListItem::SfxStringListItem( const SfxStringListItem
& rItem
) :
103 DBG_ASSERT(pImp
->nRefCount
!=0xffff,"ImpList not valid");
109 SfxStringListItem::~SfxStringListItem()
113 DBG_ASSERT(pImp
->nRefCount
!=0xffff,"ImpList not valid");
114 if( pImp
->nRefCount
> 1 )
122 std::vector
<OUString
>& SfxStringListItem::GetList()
125 pImp
= new SfxImpStringList
;
126 DBG_ASSERT(pImp
->nRefCount
!=0xffff,"ImpList not valid");
130 const std::vector
<OUString
>& SfxStringListItem::GetList () const
132 return (const_cast< SfxStringListItem
* >(this))->GetList();
136 int SfxStringListItem::operator==( const SfxPoolItem
& rItem
) const
138 DBG_ASSERT( SfxPoolItem::operator==( rItem
), "unequal type" );
140 SfxStringListItem
* pItem
= (SfxStringListItem
*)&rItem
;
142 return pImp
== pItem
->pImp
;
146 SfxItemPresentation
SfxStringListItem::GetPresentation
148 SfxItemPresentation
/*ePresentation*/,
149 SfxMapUnit
/*eCoreMetric*/,
150 SfxMapUnit
/*ePresentationMetric*/,
156 return SFX_ITEM_PRESENTATION_NONE
;
160 SfxPoolItem
* SfxStringListItem::Clone( SfxItemPool
*) const
162 return new SfxStringListItem( *this );
165 return new SfxStringListItem( Which(), &(pImp->aList) );
167 return new SfxStringListItem( Which(), NULL );
173 SfxPoolItem
* SfxStringListItem::Create( SvStream
& rStream
, sal_uInt16
) const
175 return new SfxStringListItem( Which(), rStream
);
179 SvStream
& SfxStringListItem::Store( SvStream
& rStream
, sal_uInt16
) const
183 //fdo#39428 SvStream no longer supports operator<<(long)
184 rStream
<< (sal_Int32
) 0;
188 DBG_ASSERT(pImp
->nRefCount
!=0xffff,"ImpList not valid");
190 sal_uInt32 nCount
= pImp
->aList
.size();
193 for( sal_uInt32 i
=0; i
< nCount
; i
++ )
194 writeByteString(rStream
, pImp
->aList
[i
]);
200 void SfxStringListItem::SetString( const OUString
& rStr
)
202 DBG_ASSERT(GetRefCount()==0,"SetString:RefCount!=0");
204 if ( pImp
&& (pImp
->nRefCount
== 1) )
208 pImp
= new SfxImpStringList
;
210 sal_Int32 nStart
= 0;
211 OUString
aStr(convertLineEnd(rStr
, LINEEND_CR
));
214 const sal_Int32 nDelimPos
= aStr
.indexOf( '\r', nStart
);
217 if (nStart
<aStr
.getLength())
219 // put last string only if not empty
220 pImp
->aList
.push_back(aStr
.copy(nStart
));
225 pImp
->aList
.push_back(aStr
.copy(nStart
, nDelimPos
-nStart
));
227 // skip both inserted string and delimiter
228 nStart
= nDelimPos
+ 1 ;
233 OUString
SfxStringListItem::GetString()
238 DBG_ASSERT(pImp
->nRefCount
!=0xffff,"ImpList not valid");
240 std::vector
<OUString
>::iterator iter
= pImp
->aList
.begin();
246 if (iter
== pImp
->aList
.end())
252 return convertLineEnd(aStr
, GetSystemLineEnd());
256 void SfxStringListItem::SetStringList( const com::sun::star::uno::Sequence
< OUString
>& rList
)
258 DBG_ASSERT(GetRefCount()==0,"SetString:RefCount!=0");
260 if ( pImp
&& (pImp
->nRefCount
== 1) )
264 pImp
= new SfxImpStringList
;
268 // String gehoert der Liste
269 for ( sal_Int32 n
= 0; n
< rList
.getLength(); n
++ )
270 pImp
->aList
.push_back(rList
[n
]);
274 void SfxStringListItem::GetStringList( com::sun::star::uno::Sequence
< OUString
>& rList
) const
276 long nCount
= pImp
->aList
.size();
278 rList
.realloc( nCount
);
279 for( long i
=0; i
< nCount
; i
++ )
280 rList
[i
] = pImp
->aList
[i
];
284 bool SfxStringListItem::PutValue( const com::sun::star::uno::Any
& rVal
, sal_uInt8
)
286 com::sun::star::uno::Sequence
< OUString
> aValue
;
287 if ( rVal
>>= aValue
)
289 SetStringList( aValue
);
293 OSL_FAIL( "SfxStringListItem::PutValue - Wrong type!" );
298 bool SfxStringListItem::QueryValue( com::sun::star::uno::Any
& rVal
, sal_uInt8
) const
300 // GetString() is not const!!!
301 SfxStringListItem
* pThis
= const_cast< SfxStringListItem
* >( this );
303 com::sun::star::uno::Sequence
< OUString
> aStringList
;
304 pThis
->GetStringList( aStringList
);
305 rVal
= ::com::sun::star::uno::makeAny( aStringList
);
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */