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 <osl/diagnose.h>
26 #include <tools/stream.hxx>
27 #include <stringio.hxx>
29 TYPEINIT1_AUTOFACTORY(SfxStringListItem
, SfxPoolItem
);
31 class SfxImpStringList
35 std::vector
<OUString
> aList
;
37 SfxImpStringList() { nRefCount
= 1; }
42 SfxImpStringList::~SfxImpStringList()
44 DBG_ASSERT(nRefCount
!=0xffff,"ImpList already deleted");
48 SfxStringListItem::SfxStringListItem() :
54 SfxStringListItem::SfxStringListItem( sal_uInt16 which
, const std::vector
<OUString
>* pList
) :
58 // FIXME: Putting an empty list does not work
59 // Therefore the query after the count is commented out
60 if( pList
/*!!! && pList->Count() */ )
62 pImp
= new SfxImpStringList
;
68 SfxStringListItem::SfxStringListItem( sal_uInt16 which
, SvStream
& rStream
) :
72 sal_Int32 nEntryCount
;
73 rStream
.ReadInt32( nEntryCount
);
76 pImp
= new SfxImpStringList
;
80 for( sal_Int32 i
=0; i
< nEntryCount
; i
++ )
82 pImp
->aList
.push_back( readByteString(rStream
) );
88 SfxStringListItem::SfxStringListItem( const SfxStringListItem
& rItem
) :
94 DBG_ASSERT(pImp
->nRefCount
!=0xffff,"ImpList not valid");
100 SfxStringListItem::~SfxStringListItem()
104 DBG_ASSERT(pImp
->nRefCount
!=0xffff,"ImpList not valid");
105 if( pImp
->nRefCount
> 1 )
113 std::vector
<OUString
>& SfxStringListItem::GetList()
116 pImp
= new SfxImpStringList
;
117 DBG_ASSERT(pImp
->nRefCount
!=0xffff,"ImpList not valid");
121 const std::vector
<OUString
>& SfxStringListItem::GetList () const
123 return (const_cast< SfxStringListItem
* >(this))->GetList();
127 bool SfxStringListItem::operator==( const SfxPoolItem
& rItem
) const
129 DBG_ASSERT( SfxPoolItem::operator==( rItem
), "unequal type" );
131 const SfxStringListItem
& rSSLItem
= static_cast<const SfxStringListItem
&>(rItem
);
133 return pImp
== rSSLItem
.pImp
;
137 bool SfxStringListItem::GetPresentation
139 SfxItemPresentation
/*ePresentation*/,
140 SfxMapUnit
/*eCoreMetric*/,
141 SfxMapUnit
/*ePresentationMetric*/,
151 SfxPoolItem
* SfxStringListItem::Clone( SfxItemPool
*) const
153 return new SfxStringListItem( *this );
156 return new SfxStringListItem( Which(), &(pImp->aList) );
158 return new SfxStringListItem( Which(), NULL );
164 SfxPoolItem
* SfxStringListItem::Create( SvStream
& rStream
, sal_uInt16
) const
166 return new SfxStringListItem( Which(), rStream
);
170 SvStream
& SfxStringListItem::Store( SvStream
& rStream
, sal_uInt16
) const
174 rStream
.WriteInt32( 0 );
178 DBG_ASSERT(pImp
->nRefCount
!=0xffff,"ImpList not valid");
180 sal_uInt32 nCount
= pImp
->aList
.size();
181 rStream
.WriteUInt32( nCount
);
183 for( sal_uInt32 i
=0; i
< nCount
; i
++ )
184 writeByteString(rStream
, pImp
->aList
[i
]);
190 void SfxStringListItem::SetString( const OUString
& rStr
)
192 DBG_ASSERT(GetRefCount()==0,"SetString:RefCount!=0");
194 if ( pImp
&& (pImp
->nRefCount
== 1) )
198 pImp
= new SfxImpStringList
;
200 sal_Int32 nStart
= 0;
201 OUString
aStr(convertLineEnd(rStr
, LINEEND_CR
));
204 const sal_Int32 nDelimPos
= aStr
.indexOf( '\r', nStart
);
207 if (nStart
<aStr
.getLength())
209 // put last string only if not empty
210 pImp
->aList
.push_back(aStr
.copy(nStart
));
215 pImp
->aList
.push_back(aStr
.copy(nStart
, nDelimPos
-nStart
));
217 // skip both inserted string and delimiter
218 nStart
= nDelimPos
+ 1 ;
223 OUString
SfxStringListItem::GetString()
228 DBG_ASSERT(pImp
->nRefCount
!=0xffff,"ImpList not valid");
230 std::vector
<OUString
>::iterator iter
= pImp
->aList
.begin();
236 if (iter
== pImp
->aList
.end())
242 return convertLineEnd(aStr
, GetSystemLineEnd());
246 void SfxStringListItem::SetStringList( const com::sun::star::uno::Sequence
< OUString
>& rList
)
248 DBG_ASSERT(GetRefCount()==0,"SetString:RefCount!=0");
250 if ( pImp
&& (pImp
->nRefCount
== 1) )
254 pImp
= new SfxImpStringList
;
256 // String belongs to the list
257 for ( sal_Int32 n
= 0; n
< rList
.getLength(); n
++ )
258 pImp
->aList
.push_back(rList
[n
]);
261 void SfxStringListItem::GetStringList( com::sun::star::uno::Sequence
< OUString
>& rList
) const
263 long nCount
= pImp
->aList
.size();
265 rList
.realloc( nCount
);
266 for( long i
=0; i
< nCount
; i
++ )
267 rList
[i
] = pImp
->aList
[i
];
271 bool SfxStringListItem::PutValue( const com::sun::star::uno::Any
& rVal
, sal_uInt8
)
273 com::sun::star::uno::Sequence
< OUString
> aValue
;
274 if ( rVal
>>= aValue
)
276 SetStringList( aValue
);
280 OSL_FAIL( "SfxStringListItem::PutValue - Wrong type!" );
285 bool SfxStringListItem::QueryValue( com::sun::star::uno::Any
& rVal
, sal_uInt8
) const
287 // GetString() is not const!!!
288 SfxStringListItem
* pThis
= const_cast< SfxStringListItem
* >( this );
290 com::sun::star::uno::Sequence
< OUString
> aStringList
;
291 pThis
->GetStringList( aStringList
);
292 rVal
= ::com::sun::star::uno::makeAny( aStringList
);
297 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */