Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / framework / source / helper / ocomponentaccess.cxx
blobb170ae5258a03cc9aac82784abef02da88c2be49
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 OComponentEnumeration* pEnumeration = new OComponentEnumeration( seqComponents );
72 xReturn.set( static_cast<OWeakObject*>(pEnumeration), UNO_QUERY );
75 // Return result of this operation.
76 return xReturn;
79 // XElementAccess
80 Type SAL_CALL OComponentAccess::getElementType()
82 // Elements in list an enumeration are components!
83 // Return the uno-type of XComponent.
84 return cppu::UnoType<XComponent>::get();
87 // XElementAccess
88 sal_Bool SAL_CALL OComponentAccess::hasElements()
90 SolarMutexGuard g;
92 // Set default return value, if method failed.
93 bool bReturn = false;
95 // Try to "lock" the desktop for access to task container.
96 css::uno::Reference< XFramesSupplier > xLock( m_xOwner.get(), UNO_QUERY );
97 if ( xLock.is() )
99 // Ask container of owner for existing elements.
100 bReturn = xLock->getFrames()->hasElements();
103 // Return result of this operation.
104 return bReturn;
108 void OComponentAccess::impl_collectAllChildComponents( const css::uno::Reference< XFramesSupplier >& xNode ,
109 std::vector< css::uno::Reference< XComponent > >& seqComponents )
111 // If valid node was given ...
112 if( xNode.is() )
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 );
132 // ... otherwise break a recursive path and go back at current stack!
135 css::uno::Reference< XComponent > OComponentAccess::impl_getFrameComponent( const css::uno::Reference< XFrame >& xFrame ) const
137 // Set default return value, if method failed.
138 css::uno::Reference< XComponent > xComponent;
139 // Does no controller exists?
140 css::uno::Reference< XController > xController = xFrame->getController();
141 if ( !xController.is() )
143 // Controller not exist - use the VCL-component.
144 xComponent = xFrame->getComponentWindow();
146 else
148 // Does no model exists?
149 css::uno::Reference< XModel > xModel = xController->getModel();
150 if ( xModel.is() )
152 // Model exist - use the model as component.
153 xComponent = xModel;
155 else
157 // Model not exist - use the controller as component.
158 xComponent = xController;
162 return xComponent;
166 } // namespace framework
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */