Avoid potential negative array index access to cached text.
[LibreOffice.git] / toolkit / source / controls / stdtabcontrollermodel.cxx
blob43fd80f1702c56b933c67c0fd19484c612d782ba
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 .
20 #include <com/sun/star/io/XMarkableStream.hpp>
21 #include <com/sun/star/uno/XComponentContext.hpp>
23 #include <controls/stdtabcontrollermodel.hxx>
24 #include <toolkit/helper/macros.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <cppuhelper/queryinterface.hxx>
28 #include <tools/debug.hxx>
30 #define UNOCONTROL_STREAMVERSION short(2)
34 UnoControlModelEntryList::UnoControlModelEntryList()
38 UnoControlModelEntryList::~UnoControlModelEntryList()
40 Reset();
43 void UnoControlModelEntryList::Reset()
45 for ( size_t n = maList.size(); n; )
46 DestroyEntry( --n );
49 void UnoControlModelEntryList::DestroyEntry( size_t nEntry )
51 UnoControlModelEntryListBase::iterator it = maList.begin();
52 ::std::advance( it, nEntry );
54 if ( (*it)->bGroup )
55 delete (*it)->pGroup;
56 else
57 delete (*it)->pxControl;
59 delete *it;
60 maList.erase( it );
63 size_t UnoControlModelEntryList::size() const {
64 return maList.size();
67 UnoControlModelEntry* UnoControlModelEntryList::operator[]( size_t i ) const {
68 return ( i < maList.size() ) ? maList[ i ] : nullptr;
71 void UnoControlModelEntryList::push_back( UnoControlModelEntry* item ) {
72 maList.push_back( item );
75 void UnoControlModelEntryList::insert( size_t i, UnoControlModelEntry* item ) {
76 if ( i < maList.size() ) {
77 UnoControlModelEntryListBase::iterator it = maList.begin();
78 ::std::advance( it, i );
79 maList.insert( it, item );
80 } else {
81 maList.push_back( item );
87 StdTabControllerModel::StdTabControllerModel()
89 mbGroupControl = true;
92 StdTabControllerModel::~StdTabControllerModel()
96 sal_uInt32 StdTabControllerModel::ImplGetControlCount( const UnoControlModelEntryList& rList ) const
98 sal_uInt32 nCount = 0;
99 size_t nEntries = rList.size();
100 for ( size_t n = 0; n < nEntries; n++ )
102 UnoControlModelEntry* pEntry = rList[ n ];
103 if ( pEntry->bGroup )
104 nCount += ImplGetControlCount( *pEntry->pGroup );
105 else
106 nCount++;
108 return nCount;
111 void StdTabControllerModel::ImplGetControlModels( css::uno::Reference< css::awt::XControlModel > ** ppRefs, const UnoControlModelEntryList& rList ) const
113 size_t nEntries = rList.size();
114 for ( size_t n = 0; n < nEntries; n++ )
116 UnoControlModelEntry* pEntry = rList[ n ];
117 if ( pEntry->bGroup )
118 ImplGetControlModels( ppRefs, *pEntry->pGroup );
119 else
121 **ppRefs = *pEntry->pxControl;
122 (*ppRefs)++;
127 void StdTabControllerModel::ImplSetControlModels( UnoControlModelEntryList& rList, const css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > >& Controls )
129 for ( const css::uno::Reference< css::awt::XControlModel >& rRef : Controls )
131 UnoControlModelEntry* pNewEntry = new UnoControlModelEntry;
132 pNewEntry->bGroup = false;
133 pNewEntry->pxControl = new css::uno::Reference< css::awt::XControlModel > ;
134 *pNewEntry->pxControl = rRef;
135 rList.push_back( pNewEntry );
139 sal_uInt32 StdTabControllerModel::ImplGetControlPos( const css::uno::Reference< css::awt::XControlModel >& rCtrl, const UnoControlModelEntryList& rList )
141 for ( size_t n = rList.size(); n; )
143 UnoControlModelEntry* pEntry = rList[ --n ];
144 if ( !pEntry->bGroup && ( *pEntry->pxControl == rCtrl ) )
145 return n;
147 return CONTROLPOS_NOTFOUND;
150 static void ImplWriteControls( const css::uno::Reference< css::io::XObjectOutputStream > & OutStream, const css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > >& rCtrls )
152 css::uno::Reference< css::io::XMarkableStream > xMark( OutStream, css::uno::UNO_QUERY );
153 DBG_ASSERT( xMark.is(), "write: no XMarkableStream!" );
155 sal_uInt32 nStoredControls = 0;
156 sal_Int32 nDataBeginMark = xMark->createMark();
158 OutStream->writeLong( 0 ); // DataLen
159 OutStream->writeLong( 0 ); // nStoredControls
161 for ( const css::uno::Reference< css::awt::XControlModel >& xI : rCtrls )
163 css::uno::Reference< css::io::XPersistObject > xPO( xI, css::uno::UNO_QUERY );
164 DBG_ASSERT( xPO.is(), "write: Control doesn't support XPersistObject" );
165 if ( xPO.is() )
167 OutStream->writeObject( xPO );
168 nStoredControls++;
171 sal_Int32 nDataLen = xMark->offsetToMark( nDataBeginMark );
172 xMark->jumpToMark( nDataBeginMark );
173 OutStream->writeLong( nDataLen );
174 OutStream->writeLong( nStoredControls );
175 xMark->jumpToFurthest();
176 xMark->deleteMark(nDataBeginMark);
179 static css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > > ImplReadControls( const css::uno::Reference< css::io::XObjectInputStream > & InStream )
181 css::uno::Reference< css::io::XMarkableStream > xMark( InStream, css::uno::UNO_QUERY );
182 DBG_ASSERT( xMark.is(), "write: no XMarkableStream!" );
184 sal_Int32 nDataBeginMark = xMark->createMark();
186 sal_Int32 nDataLen = InStream->readLong();
187 sal_uInt32 nCtrls = InStream->readLong();
189 css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > > aSeq( nCtrls );
190 for ( sal_uInt32 n = 0; n < nCtrls; n++ )
192 css::uno::Reference< css::io::XPersistObject > xObj = InStream->readObject();
193 css::uno::Reference< css::awt::XControlModel > xI( xObj, css::uno::UNO_QUERY );
194 aSeq.getArray()[n] = xI;
197 // Skip remainder if more data exists than this version recognizes
198 xMark->jumpToMark( nDataBeginMark );
199 InStream->skipBytes( nDataLen );
200 xMark->deleteMark(nDataBeginMark);
201 return aSeq;
205 // css::uno::XInterface
206 css::uno::Any StdTabControllerModel::queryAggregation( const css::uno::Type & rType )
208 css::uno::Any aRet = ::cppu::queryInterface( rType,
209 static_cast< css::awt::XTabControllerModel* >(this),
210 static_cast< css::lang::XServiceInfo* >(this),
211 static_cast< css::io::XPersistObject* >(this),
212 static_cast< css::lang::XTypeProvider* >(this) );
213 return (aRet.hasValue() ? aRet : OWeakAggObject::queryAggregation( rType ));
216 IMPL_IMPLEMENTATION_ID( StdTabControllerModel )
218 // css::lang::XTypeProvider
219 css::uno::Sequence< css::uno::Type > StdTabControllerModel::getTypes()
221 static const css::uno::Sequence< css::uno::Type > aTypeList {
222 cppu::UnoType<css::lang::XTypeProvider>::get(),
223 cppu::UnoType<css::awt::XTabControllerModel>::get(),
224 cppu::UnoType<css::lang::XServiceInfo>::get(),
225 cppu::UnoType<css::io::XPersistObject>::get()
227 return aTypeList;
230 sal_Bool StdTabControllerModel::getGroupControl( )
232 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
234 return mbGroupControl;
237 void StdTabControllerModel::setGroupControl( sal_Bool GroupControl )
239 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
241 mbGroupControl = GroupControl;
244 void StdTabControllerModel::setControlModels( const css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > >& Controls )
246 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
248 maControls.Reset();
249 ImplSetControlModels( maControls, Controls );
252 css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > > StdTabControllerModel::getControlModels( )
254 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
256 css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > > aSeq( ImplGetControlCount( maControls ) );
257 css::uno::Reference< css::awt::XControlModel > * pRefs = aSeq.getArray();
258 ImplGetControlModels( &pRefs, maControls );
259 return aSeq;
262 void StdTabControllerModel::setGroup( const css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > >& Group, const OUString& GroupName )
264 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
266 // The controls might occur as a flat list and will be grouped.
267 // Nested groups are not possible.
268 // The first element of a group determines its position.
269 UnoControlModelEntry* pNewEntry = new UnoControlModelEntry;
270 pNewEntry->bGroup = true;
271 pNewEntry->pGroup = new UnoControlModelEntryList;
272 pNewEntry->pGroup->SetName( GroupName );
273 ImplSetControlModels( *pNewEntry->pGroup, Group );
275 bool bInserted = false;
276 size_t nElements = pNewEntry->pGroup->size();
277 for ( size_t n = 0; n < nElements; n++ )
279 UnoControlModelEntry* pEntry = (*pNewEntry->pGroup)[ n ];
280 if ( !pEntry->bGroup )
282 sal_uInt32 nPos = ImplGetControlPos( *pEntry->pxControl, maControls );
283 // At the beginning, all Controls should be in a flattened list
284 DBG_ASSERT( nPos != CONTROLPOS_NOTFOUND, "setGroup - Element not found" );
285 if ( nPos != CONTROLPOS_NOTFOUND )
287 maControls.DestroyEntry( nPos );
288 if ( !bInserted )
290 maControls.insert( nPos, pNewEntry );
291 bInserted = true;
296 if ( !bInserted )
297 maControls.push_back( pNewEntry );
300 sal_Int32 StdTabControllerModel::getGroupCount( )
302 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
304 // Start with only one group layer, even though Model and Impl-methods
305 // work recursively, this is not presented to the outside.
307 sal_Int32 nGroups = 0;
308 size_t nEntries = maControls.size();
309 for ( size_t n = 0; n < nEntries; n++ )
311 UnoControlModelEntry* pEntry = maControls[ n ];
312 if ( pEntry->bGroup )
313 nGroups++;
315 return nGroups;
318 void StdTabControllerModel::getGroup( sal_Int32 nGroup, css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > >& rGroup, OUString& rName )
320 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
322 css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > > aSeq;
323 sal_uInt32 nG = 0;
324 size_t nEntries = maControls.size();
325 for ( size_t n = 0; n < nEntries; n++ )
327 UnoControlModelEntry* pEntry = maControls[ n ];
328 if ( pEntry->bGroup )
330 if ( nG == static_cast<sal_uInt32>(nGroup) )
332 sal_uInt32 nCount = ImplGetControlCount( *pEntry->pGroup );
333 aSeq = css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > >( nCount );
334 css::uno::Reference< css::awt::XControlModel > * pRefs = aSeq.getArray();
335 ImplGetControlModels( &pRefs, *pEntry->pGroup );
336 rName = pEntry->pGroup->GetName();
337 break;
339 nG++;
342 rGroup = aSeq;
345 void StdTabControllerModel::getGroupByName( const OUString& rName, css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > >& rGroup )
347 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
349 sal_uInt32 nGroup = 0;
350 size_t nEntries = maControls.size();
351 for ( size_t n = 0; n < nEntries; n++ )
353 UnoControlModelEntry* pEntry = maControls[ n ];
354 if ( pEntry->bGroup )
356 if ( pEntry->pGroup->GetName() == rName )
358 OUString Dummy;
359 getGroup( nGroup, rGroup, Dummy );
360 break;
362 nGroup++;
368 // css::io::XPersistObject
369 OUString StdTabControllerModel::getServiceName( )
371 return "stardiv.vcl.controlmodel.TabController";
374 void StdTabControllerModel::write( const css::uno::Reference< css::io::XObjectOutputStream >& OutStream )
376 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
378 css::uno::Reference< css::io::XMarkableStream > xMark( OutStream, css::uno::UNO_QUERY );
379 DBG_ASSERT( xMark.is(), "write: no XMarkableStream!" );
381 OutStream->writeShort( UNOCONTROL_STREAMVERSION );
383 css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > > aCtrls = getControlModels();
384 ImplWriteControls( OutStream, aCtrls );
386 sal_uInt32 nGroups = getGroupCount();
387 OutStream->writeLong( nGroups );
388 for ( sal_uInt32 n = 0; n < nGroups; n++ )
390 css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > > aGroupCtrls;
391 OUString aGroupName;
392 getGroup( n, aGroupCtrls, aGroupName );
393 OutStream->writeUTF( aGroupName );
394 ImplWriteControls( OutStream, aGroupCtrls );
398 void StdTabControllerModel::read( const css::uno::Reference< css::io::XObjectInputStream >& InStream )
400 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
402 css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > > aSeq = ImplReadControls( InStream );
403 setControlModels( aSeq );
405 sal_uInt32 nGroups = InStream->readLong();
406 for ( sal_uInt32 n = 0; n < nGroups; n++ )
408 OUString aGroupName = InStream->readUTF();
409 css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > > aCtrlSeq = ImplReadControls( InStream );
410 setGroup( aCtrlSeq, aGroupName );
414 OUString StdTabControllerModel::getImplementationName()
416 return "stardiv.Toolkit.StdTabControllerModel";
419 sal_Bool StdTabControllerModel::supportsService(OUString const & ServiceName)
421 return cppu::supportsService(this, ServiceName);
424 css::uno::Sequence<OUString> StdTabControllerModel::getSupportedServiceNames()
426 return css::uno::Sequence<OUString>{
427 "com.sun.star.awt.TabControllerModel",
428 "stardiv.vcl.controlmodel.TabController"};
431 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
432 stardiv_Toolkit_StdTabControllerModel_get_implementation(
433 css::uno::XComponentContext *,
434 css::uno::Sequence<css::uno::Any> const &)
436 return cppu::acquire(new StdTabControllerModel());
439 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */