bump product version to 5.0.4.1
[LibreOffice.git] / svl / source / items / slstitm.cxx
blob5221e46a94a3d6dde545a13fbf4c644c0ac5d12a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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
33 public:
34 sal_uInt16 nRefCount;
35 std::vector<OUString> aList;
37 SfxImpStringList() { nRefCount = 1; }
38 ~SfxImpStringList();
42 SfxImpStringList::~SfxImpStringList()
44 DBG_ASSERT(nRefCount!=0xffff,"ImpList already deleted");
45 nRefCount = 0xffff;
48 SfxStringListItem::SfxStringListItem() :
49 pImp(NULL)
54 SfxStringListItem::SfxStringListItem( sal_uInt16 which, const std::vector<OUString>* pList ) :
55 SfxPoolItem( which ),
56 pImp(NULL)
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;
63 pImp->aList = *pList;
68 SfxStringListItem::SfxStringListItem( sal_uInt16 which, SvStream& rStream ) :
69 SfxPoolItem( which ),
70 pImp(NULL)
72 sal_Int32 nEntryCount;
73 rStream.ReadInt32( nEntryCount );
75 if( nEntryCount )
76 pImp = new SfxImpStringList;
78 if (pImp)
80 for( sal_Int32 i=0; i < nEntryCount; i++ )
82 pImp->aList.push_back( readByteString(rStream) );
88 SfxStringListItem::SfxStringListItem( const SfxStringListItem& rItem ) :
89 SfxPoolItem( rItem ),
90 pImp(rItem.pImp)
92 if( pImp )
94 DBG_ASSERT(pImp->nRefCount!=0xffff,"ImpList not valid");
95 pImp->nRefCount++;
100 SfxStringListItem::~SfxStringListItem()
102 if( pImp )
104 DBG_ASSERT(pImp->nRefCount!=0xffff,"ImpList not valid");
105 if( pImp->nRefCount > 1 )
106 pImp->nRefCount--;
107 else
108 delete pImp;
113 std::vector<OUString>& SfxStringListItem::GetList()
115 if( !pImp )
116 pImp = new SfxImpStringList;
117 DBG_ASSERT(pImp->nRefCount!=0xffff,"ImpList not valid");
118 return pImp->aList;
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*/,
142 OUString& rText,
143 const IntlWrapper *
144 ) const
146 rText = "(List)";
147 return false;
151 SfxPoolItem* SfxStringListItem::Clone( SfxItemPool *) const
153 return new SfxStringListItem( *this );
155 if( pImp )
156 return new SfxStringListItem( Which(), &(pImp->aList) );
157 else
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
172 if( !pImp )
174 rStream.WriteInt32( 0 );
175 return rStream;
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]);
186 return rStream;
190 void SfxStringListItem::SetString( const OUString& rStr )
192 DBG_ASSERT(GetRefCount()==0,"SetString:RefCount!=0");
194 if ( pImp && (pImp->nRefCount == 1) )
195 delete pImp;
196 else if( pImp )
197 pImp->nRefCount--;
198 pImp = new SfxImpStringList;
200 sal_Int32 nStart = 0;
201 OUString aStr(convertLineEnd(rStr, LINEEND_CR));
202 for (;;)
204 const sal_Int32 nDelimPos = aStr.indexOf( '\r', nStart );
205 if ( nDelimPos < 0 )
207 if (nStart<aStr.getLength())
209 // put last string only if not empty
210 pImp->aList.push_back(aStr.copy(nStart));
212 break;
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()
225 OUString aStr;
226 if ( pImp )
228 DBG_ASSERT(pImp->nRefCount!=0xffff,"ImpList not valid");
230 std::vector<OUString>::iterator iter = pImp->aList.begin();
231 for (;;)
233 aStr += *iter;
234 ++iter;
236 if (iter == pImp->aList.end())
237 break;
239 aStr += "\r";
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) )
251 delete pImp;
252 else if( pImp )
253 pImp->nRefCount--;
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];
270 // virtual
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 );
277 return true;
280 OSL_FAIL( "SfxStringListItem::PutValue - Wrong type!" );
281 return false;
284 // virtual
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 );
293 return true;
297 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */