Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / helper / ocomponentaccess.cxx
blobad7409990d25db39cf0d18caa6723afa691caebe
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 <helper/ocomponentaccess.hxx>
21 #include <helper/ocomponentenumeration.hxx>
23 #include <com/sun/star/frame/FrameSearchFlag.hpp>
25 #include <vcl/svapp.hxx>
26 #include <sal/log.hxx>
28 namespace framework{
30 using namespace ::com::sun::star::container;
31 using namespace ::com::sun::star::frame;
32 using namespace ::com::sun::star::lang;
33 using namespace ::com::sun::star::uno;
34 using namespace ::cppu;
35 using namespace ::osl;
37 // constructor
39 OComponentAccess::OComponentAccess( const css::uno::Reference< XDesktop >& xOwner )
40 : m_xOwner ( xOwner )
42 // Safe impossible cases
43 SAL_WARN_IF( !xOwner.is(), "fwk", "OComponentAccess::OComponentAccess(): Invalid parameter detected!" );
46 // destructor
48 OComponentAccess::~OComponentAccess()
52 // XEnumerationAccess
53 css::uno::Reference< XEnumeration > SAL_CALL OComponentAccess::createEnumeration()
55 SolarMutexGuard g;
57 // Set default return value, if method failed.
58 // If no desktop exist and there is no task container - return an empty enumeration!
59 css::uno::Reference< XEnumeration > xReturn;
61 // Try to "lock" the desktop for access to task container.
62 css::uno::Reference< XInterface > xLock = m_xOwner.get();
63 if ( xLock.is() )
65 // Desktop exist => pointer to task container must be valid.
66 // Initialize a new enumeration ... if some tasks and his components exist!
67 // (OTasksEnumeration will make an assert, if we initialize the new instance without valid values!)
69 std::vector< css::uno::Reference< XComponent > > seqComponents;
70 impl_collectAllChildComponents( css::uno::Reference< XFramesSupplier >( xLock, UNO_QUERY ), seqComponents );
71 xReturn = new OComponentEnumeration( std::move(seqComponents) );
74 // Return result of this operation.
75 return xReturn;
78 // XElementAccess
79 Type SAL_CALL OComponentAccess::getElementType()
81 // Elements in list an enumeration are components!
82 // Return the uno-type of XComponent.
83 return cppu::UnoType<XComponent>::get();
86 // XElementAccess
87 sal_Bool SAL_CALL OComponentAccess::hasElements()
89 SolarMutexGuard g;
91 // Set default return value, if method failed.
92 bool bReturn = false;
94 // Try to "lock" the desktop for access to task container.
95 css::uno::Reference< XFramesSupplier > xLock( m_xOwner.get(), UNO_QUERY );
96 if ( xLock.is() )
98 // Ask container of owner for existing elements.
99 bReturn = xLock->getFrames()->hasElements();
102 // Return result of this operation.
103 return bReturn;
107 void OComponentAccess::impl_collectAllChildComponents( const css::uno::Reference< XFramesSupplier >& xNode ,
108 std::vector< css::uno::Reference< XComponent > >& seqComponents )
110 // If valid node was given ...
111 if( !xNode.is() )
112 return;
114 // ... continue collection at these.
116 // Get the container of current node, collect the components of existing child frames
117 // and go down to next level in tree (recursive!).
119 const css::uno::Reference< XFrames > xContainer = xNode->getFrames();
120 const Sequence< css::uno::Reference< XFrame > > seqFrames = xContainer->queryFrames( FrameSearchFlag::CHILDREN );
122 const sal_Int32 nFrameCount = seqFrames.getLength();
123 for( sal_Int32 nFrame=0; nFrame<nFrameCount; ++nFrame )
125 css::uno::Reference< XComponent > xComponent = impl_getFrameComponent( seqFrames[nFrame] );
126 if( xComponent.is() )
128 seqComponents.push_back( xComponent );
131 // ... otherwise break a recursive path and go back at current stack!
134 css::uno::Reference< XComponent > OComponentAccess::impl_getFrameComponent( const css::uno::Reference< XFrame >& xFrame ) const
136 // Set default return value, if method failed.
137 css::uno::Reference< XComponent > xComponent;
138 // Does no controller exists?
139 css::uno::Reference< XController > xController = xFrame->getController();
140 if ( !xController.is() )
142 // Controller not exist - use the VCL-component.
143 xComponent = xFrame->getComponentWindow();
145 else
147 // Does no model exists?
148 css::uno::Reference< XModel > xModel = xController->getModel();
149 if ( xModel.is() )
151 // Model exist - use the model as component.
152 xComponent = xModel;
154 else
156 // Model not exist - use the controller as component.
157 xComponent = xController;
161 return xComponent;
165 } // namespace framework
167 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */