Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
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>
26 :
27 : // STATIC DATA -----------------------------------------------------------
28 :
29 : DBG_NAME(SfxStringListItem)
30 :
31 : // -----------------------------------------------------------------------
32 :
33 22 : TYPEINIT1_AUTOFACTORY(SfxStringListItem, SfxPoolItem);
34 :
35 : class SfxImpStringList
36 : {
37 : public:
38 : sal_uInt16 nRefCount;
39 : std::vector<String> aList;
40 :
41 0 : SfxImpStringList() { nRefCount = 1; }
42 : ~SfxImpStringList();
43 : };
44 :
45 : //------------------------------------------------------------------------
46 :
47 0 : SfxImpStringList::~SfxImpStringList()
48 : {
49 : DBG_ASSERT(nRefCount!=0xffff,"ImpList already deleted");
50 0 : nRefCount = 0xffff;
51 0 : }
52 :
53 : // class SfxStringListItem -----------------------------------------------
54 :
55 0 : SfxStringListItem::SfxStringListItem() :
56 0 : pImp(NULL)
57 : {
58 0 : }
59 :
60 : //------------------------------------------------------------------------
61 :
62 0 : SfxStringListItem::SfxStringListItem( sal_uInt16 which, const std::vector<String>* pList ) :
63 : SfxPoolItem( which ),
64 0 : pImp(NULL)
65 : {
66 : // PB: das Putten einer leeren Liste funktionierte nicht,
67 : // deshalb habe ich hier die Abfrage nach dem Count auskommentiert
68 0 : if( pList /*!!! && pList->Count() */ )
69 : {
70 0 : pImp = new SfxImpStringList;
71 :
72 0 : if (pImp)
73 0 : pImp->aList = *pList;
74 : }
75 0 : }
76 :
77 : //------------------------------------------------------------------------
78 :
79 0 : SfxStringListItem::SfxStringListItem( sal_uInt16 which, SvStream& rStream ) :
80 : SfxPoolItem( which ),
81 0 : pImp(NULL)
82 : {
83 : //fdo#39428 SvStream no longer supports operator>>(long&)
84 : sal_Int32 nEntryCount;
85 0 : rStream >> nEntryCount;
86 :
87 0 : if( nEntryCount )
88 0 : pImp = new SfxImpStringList;
89 :
90 0 : if (pImp)
91 : {
92 : long i;
93 0 : String aStr;
94 0 : for( i=0; i < nEntryCount; i++ )
95 : {
96 0 : aStr = readByteString(rStream);
97 0 : pImp->aList.push_back(aStr);
98 0 : }
99 : }
100 0 : }
101 :
102 : //------------------------------------------------------------------------
103 :
104 0 : SfxStringListItem::SfxStringListItem( const SfxStringListItem& rItem ) :
105 : SfxPoolItem( rItem ),
106 0 : pImp(rItem.pImp)
107 : {
108 0 : if( pImp )
109 : {
110 : DBG_ASSERT(pImp->nRefCount!=0xffff,"ImpList not valid");
111 0 : pImp->nRefCount++;
112 : }
113 0 : }
114 :
115 : //------------------------------------------------------------------------
116 :
117 0 : SfxStringListItem::~SfxStringListItem()
118 : {
119 0 : if( pImp )
120 : {
121 : DBG_ASSERT(pImp->nRefCount!=0xffff,"ImpList not valid");
122 0 : if( pImp->nRefCount > 1 )
123 0 : pImp->nRefCount--;
124 : else
125 0 : delete pImp;
126 : }
127 0 : }
128 :
129 : //------------------------------------------------------------------------
130 :
131 0 : std::vector<String>& SfxStringListItem::GetList()
132 : {
133 0 : if( !pImp )
134 0 : pImp = new SfxImpStringList;
135 : DBG_ASSERT(pImp->nRefCount!=0xffff,"ImpList not valid");
136 0 : return pImp->aList;
137 : }
138 :
139 0 : const std::vector<String>& SfxStringListItem::GetList () const
140 : {
141 0 : return (const_cast< SfxStringListItem * >(this))->GetList();
142 : }
143 :
144 : //------------------------------------------------------------------------
145 :
146 0 : int SfxStringListItem::operator==( const SfxPoolItem& rItem ) const
147 : {
148 : DBG_ASSERT( SfxPoolItem::operator==( rItem ), "unequal type" );
149 :
150 0 : SfxStringListItem* pItem = (SfxStringListItem*)&rItem;
151 :
152 0 : return pImp == pItem->pImp;
153 : }
154 :
155 : //------------------------------------------------------------------------
156 :
157 0 : SfxItemPresentation SfxStringListItem::GetPresentation
158 : (
159 : SfxItemPresentation /*ePresentation*/,
160 : SfxMapUnit /*eCoreMetric*/,
161 : SfxMapUnit /*ePresentationMetric*/,
162 : XubString& rText,
163 : const IntlWrapper *
164 : ) const
165 : {
166 0 : rText.AssignAscii(RTL_CONSTASCII_STRINGPARAM("(List)"));
167 0 : return SFX_ITEM_PRESENTATION_NONE;
168 : }
169 :
170 : //------------------------------------------------------------------------
171 :
172 0 : SfxPoolItem* SfxStringListItem::Clone( SfxItemPool *) const
173 : {
174 0 : return new SfxStringListItem( *this );
175 : /*
176 : if( pImp )
177 : return new SfxStringListItem( Which(), &(pImp->aList) );
178 : else
179 : return new SfxStringListItem( Which(), NULL );
180 : */
181 :
182 : }
183 :
184 : //------------------------------------------------------------------------
185 :
186 0 : SfxPoolItem* SfxStringListItem::Create( SvStream & rStream, sal_uInt16 ) const
187 : {
188 0 : return new SfxStringListItem( Which(), rStream );
189 : }
190 :
191 : //------------------------------------------------------------------------
192 :
193 0 : SvStream& SfxStringListItem::Store( SvStream & rStream, sal_uInt16 ) const
194 : {
195 0 : if( !pImp )
196 : {
197 : //fdo#39428 SvStream no longer supports operator<<(long)
198 0 : rStream << (sal_Int32) 0;
199 0 : return rStream;
200 : }
201 :
202 : DBG_ASSERT(pImp->nRefCount!=0xffff,"ImpList not valid");
203 :
204 0 : sal_uInt32 nCount = pImp->aList.size();
205 0 : rStream << nCount;
206 :
207 0 : for( sal_uInt32 i=0; i < nCount; i++ )
208 0 : writeByteString(rStream, pImp->aList[i]);
209 :
210 0 : return rStream;
211 : }
212 :
213 : //------------------------------------------------------------------------
214 :
215 0 : void SfxStringListItem::SetString( const XubString& rStr )
216 : {
217 : DBG_ASSERT(GetRefCount()==0,"SetString:RefCount!=0");
218 :
219 0 : if ( pImp && (pImp->nRefCount == 1) )
220 0 : delete pImp;
221 : else
222 0 : if( pImp )
223 0 : pImp->nRefCount--;
224 0 : pImp = new SfxImpStringList;
225 :
226 0 : xub_StrLen nStart = 0;
227 : xub_StrLen nDelimPos;
228 0 : XubString aStr(convertLineEnd(rStr, LINEEND_CR));
229 0 : do
230 : {
231 0 : nDelimPos = aStr.Search( _CR, nStart );
232 : xub_StrLen nLen;
233 0 : if ( nDelimPos == STRING_NOTFOUND )
234 0 : nLen = 0xffff;
235 : else
236 0 : nLen = nDelimPos - nStart;
237 :
238 : // String gehoert der Liste
239 0 : pImp->aList.push_back(aStr.Copy(nStart, nLen));
240 :
241 0 : nStart += nLen + 1 ; // delimiter ueberspringen
242 : } while( nDelimPos != STRING_NOTFOUND );
243 :
244 : // Kein Leerstring am Ende
245 0 : if (!pImp->aList.empty() && !(pImp->aList.rbegin())->Len())
246 0 : pImp->aList.pop_back();
247 0 : }
248 :
249 : //------------------------------------------------------------------------
250 :
251 0 : XubString SfxStringListItem::GetString()
252 : {
253 0 : XubString aStr;
254 0 : if ( pImp )
255 : {
256 : DBG_ASSERT(pImp->nRefCount!=0xffff,"ImpList not valid");
257 :
258 0 : std::vector<String>::iterator iter;
259 0 : for (iter = pImp->aList.begin();;)
260 : {
261 0 : aStr += *iter;
262 0 : ++iter;
263 :
264 0 : if (iter != pImp->aList.end())
265 0 : aStr += '\r';
266 : else
267 0 : break;
268 : }
269 : }
270 0 : return convertLineEnd(aStr, GetSystemLineEnd());
271 : }
272 :
273 : //------------------------------------------------------------------------
274 :
275 0 : void SfxStringListItem::SetStringList( const com::sun::star::uno::Sequence< rtl::OUString >& rList )
276 : {
277 : DBG_ASSERT(GetRefCount()==0,"SetString:RefCount!=0");
278 :
279 0 : if ( pImp && (pImp->nRefCount == 1) )
280 0 : delete pImp;
281 : else
282 0 : if( pImp )
283 0 : pImp->nRefCount--;
284 0 : pImp = new SfxImpStringList;
285 :
286 0 : if (pImp)
287 : {
288 : // String gehoert der Liste
289 0 : for ( sal_Int32 n = 0; n < rList.getLength(); n++ )
290 0 : pImp->aList.push_back(XubString(rList[n]));
291 : }
292 0 : }
293 :
294 : //----------------------------------------------------------------------------
295 0 : void SfxStringListItem::GetStringList( com::sun::star::uno::Sequence< rtl::OUString >& rList ) const
296 : {
297 0 : long nCount = pImp->aList.size();
298 :
299 0 : rList.realloc( nCount );
300 0 : for( long i=0; i < nCount; i++ )
301 0 : rList[i] = pImp->aList[i];
302 0 : }
303 :
304 : //----------------------------------------------------------------------------
305 : // virtual
306 0 : bool SfxStringListItem::PutValue( const com::sun::star::uno::Any& rVal, sal_uInt8 )
307 : {
308 0 : com::sun::star::uno::Sequence< rtl::OUString > aValue;
309 0 : if ( rVal >>= aValue )
310 : {
311 0 : SetStringList( aValue );
312 0 : return true;
313 : }
314 :
315 : OSL_FAIL( "SfxStringListItem::PutValue - Wrong type!" );
316 0 : return false;
317 : }
318 :
319 : //----------------------------------------------------------------------------
320 : // virtual
321 0 : bool SfxStringListItem::QueryValue( com::sun::star::uno::Any& rVal, sal_uInt8 ) const
322 : {
323 : // GetString() is not const!!!
324 0 : SfxStringListItem* pThis = const_cast< SfxStringListItem * >( this );
325 :
326 0 : com::sun::star::uno::Sequence< rtl::OUString > aStringList;
327 0 : pThis->GetStringList( aStringList );
328 0 : rVal = ::com::sun::star::uno::makeAny( aStringList );
329 0 : return true;
330 : }
331 :
332 :
333 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|