xref: /trunk/main/framework/source/helper/ocomponentaccess.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
16d739b60SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
36d739b60SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
46d739b60SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
56d739b60SAndrew Rist  * distributed with this work for additional information
66d739b60SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
76d739b60SAndrew Rist  * to you under the Apache License, Version 2.0 (the
86d739b60SAndrew Rist  * "License"); you may not use this file except in compliance
96d739b60SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
116d739b60SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
136d739b60SAndrew Rist  * Unless required by applicable law or agreed to in writing,
146d739b60SAndrew Rist  * software distributed under the License is distributed on an
156d739b60SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
166d739b60SAndrew Rist  * KIND, either express or implied.  See the License for the
176d739b60SAndrew Rist  * specific language governing permissions and limitations
186d739b60SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
206d739b60SAndrew Rist  *************************************************************/
216d739b60SAndrew Rist 
226d739b60SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_framework.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir //_________________________________________________________________________________________________________________
28cdf0e10cSrcweir //  my own includes
29cdf0e10cSrcweir //_________________________________________________________________________________________________________________
30cdf0e10cSrcweir #include <helper/ocomponentaccess.hxx>
31cdf0e10cSrcweir #include <helper/ocomponentenumeration.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #ifndef _FRAMEWORK_THREADHELP_RESETABLEGUARD_HXX_
34cdf0e10cSrcweir #include <threadhelp/resetableguard.hxx>
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir 
37cdf0e10cSrcweir //_________________________________________________________________________________________________________________
38cdf0e10cSrcweir //  interface includes
39cdf0e10cSrcweir //_________________________________________________________________________________________________________________
40cdf0e10cSrcweir #include <com/sun/star/frame/FrameSearchFlag.hpp>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir //_________________________________________________________________________________________________________________
43cdf0e10cSrcweir //  includes of other projects
44cdf0e10cSrcweir //_________________________________________________________________________________________________________________
45cdf0e10cSrcweir #include <vcl/svapp.hxx>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir //_________________________________________________________________________________________________________________
48cdf0e10cSrcweir //  namespace
49cdf0e10cSrcweir //_________________________________________________________________________________________________________________
50cdf0e10cSrcweir 
51cdf0e10cSrcweir namespace framework{
52cdf0e10cSrcweir 
53cdf0e10cSrcweir using namespace ::com::sun::star::container     ;
54cdf0e10cSrcweir using namespace ::com::sun::star::frame         ;
55cdf0e10cSrcweir using namespace ::com::sun::star::lang          ;
56cdf0e10cSrcweir using namespace ::com::sun::star::uno           ;
57cdf0e10cSrcweir using namespace ::cppu                          ;
58cdf0e10cSrcweir using namespace ::osl                           ;
59cdf0e10cSrcweir using namespace ::rtl                           ;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir //_________________________________________________________________________________________________________________
62cdf0e10cSrcweir //  non exported const
63cdf0e10cSrcweir //_________________________________________________________________________________________________________________
64cdf0e10cSrcweir 
65cdf0e10cSrcweir //_________________________________________________________________________________________________________________
66cdf0e10cSrcweir //  non exported definitions
67cdf0e10cSrcweir //_________________________________________________________________________________________________________________
68cdf0e10cSrcweir 
69cdf0e10cSrcweir //_________________________________________________________________________________________________________________
70cdf0e10cSrcweir //  declarations
71cdf0e10cSrcweir //_________________________________________________________________________________________________________________
72cdf0e10cSrcweir 
73cdf0e10cSrcweir //*****************************************************************************************************************
74cdf0e10cSrcweir //  constructor
75cdf0e10cSrcweir //*****************************************************************************************************************
OComponentAccess(const css::uno::Reference<XDesktop> & xOwner)76cdf0e10cSrcweir OComponentAccess::OComponentAccess( const css::uno::Reference< XDesktop >& xOwner )
77cdf0e10cSrcweir         //  Init baseclasses first
78cdf0e10cSrcweir         :   ThreadHelpBase  ( &Application::GetSolarMutex() )
79cdf0e10cSrcweir         // Init member
80cdf0e10cSrcweir         ,   m_xOwner        ( xOwner                        )
81cdf0e10cSrcweir {
82cdf0e10cSrcweir     // Safe impossible cases
83cdf0e10cSrcweir     LOG_ASSERT( impldbg_checkParameter_OComponentAccessCtor( xOwner ), "OComponentAccess::OComponentAccess()\nInvalid parameter detected!\n" )
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir //*****************************************************************************************************************
87cdf0e10cSrcweir //  destructor
88cdf0e10cSrcweir //*****************************************************************************************************************
~OComponentAccess()89cdf0e10cSrcweir OComponentAccess::~OComponentAccess()
90cdf0e10cSrcweir {
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir //*****************************************************************************************************************
94cdf0e10cSrcweir //  XEnumerationAccess
95cdf0e10cSrcweir //*****************************************************************************************************************
createEnumeration()96cdf0e10cSrcweir css::uno::Reference< XEnumeration > SAL_CALL OComponentAccess::createEnumeration() throw( RuntimeException )
97cdf0e10cSrcweir {
98cdf0e10cSrcweir     // Ready for multithreading
99cdf0e10cSrcweir     ResetableGuard aGuard( m_aLock );
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     // Set default return value, if method failed.
102cdf0e10cSrcweir     // If no desktop exist and there is no task container - return an empty enumeration!
103cdf0e10cSrcweir     css::uno::Reference< XEnumeration > xReturn = css::uno::Reference< XEnumeration >();
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     // Try to "lock" the desktop for access to task container.
106cdf0e10cSrcweir     css::uno::Reference< XInterface > xLock = m_xOwner.get();
107cdf0e10cSrcweir     if ( xLock.is() == sal_True )
108cdf0e10cSrcweir     {
109cdf0e10cSrcweir         // Desktop exist => pointer to task container must be valid.
110cdf0e10cSrcweir         // Initialize a new enumeration ... if some tasks and his components exist!
111cdf0e10cSrcweir         // (OTasksEnumeration will make an assert, if we initialize the new instance without valid values!)
112cdf0e10cSrcweir 
113cdf0e10cSrcweir         Sequence< css::uno::Reference< XComponent > > seqComponents;
114cdf0e10cSrcweir         impl_collectAllChildComponents( css::uno::Reference< XFramesSupplier >( xLock, UNO_QUERY ), seqComponents );
115cdf0e10cSrcweir         OComponentEnumeration* pEnumeration = new OComponentEnumeration( seqComponents );
116cdf0e10cSrcweir         xReturn = css::uno::Reference< XEnumeration >( (OWeakObject*)pEnumeration, UNO_QUERY );
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     // Return result of this operation.
120cdf0e10cSrcweir     return xReturn;
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir //*****************************************************************************************************************
124cdf0e10cSrcweir //  XElementAccess
125cdf0e10cSrcweir //*****************************************************************************************************************
getElementType()126cdf0e10cSrcweir Type SAL_CALL OComponentAccess::getElementType() throw( RuntimeException )
127cdf0e10cSrcweir {
128cdf0e10cSrcweir     // Elements in list an enumeration are components!
129cdf0e10cSrcweir     // Return the uno-type of XComponent.
130cdf0e10cSrcweir     return ::getCppuType((const css::uno::Reference< XComponent >*)NULL);
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir //*****************************************************************************************************************
134cdf0e10cSrcweir //  XElementAccess
135cdf0e10cSrcweir //*****************************************************************************************************************
hasElements()136cdf0e10cSrcweir sal_Bool SAL_CALL OComponentAccess::hasElements() throw( RuntimeException )
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     // Ready for multithreading
139cdf0e10cSrcweir     ResetableGuard aGuard( m_aLock );
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     // Set default return value, if method failed.
142cdf0e10cSrcweir     sal_Bool bReturn = sal_False;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir     // Try to "lock" the desktop for access to task container.
145cdf0e10cSrcweir     css::uno::Reference< XFramesSupplier > xLock( m_xOwner.get(), UNO_QUERY );
146cdf0e10cSrcweir     if ( xLock.is() == sal_True )
147cdf0e10cSrcweir     {
148cdf0e10cSrcweir         // Ask container of owner for existing elements.
149cdf0e10cSrcweir         bReturn = xLock->getFrames()->hasElements();
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     // Return result of this operation.
153cdf0e10cSrcweir     return bReturn;
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir //*****************************************************************************************************************
157cdf0e10cSrcweir //  private method
158cdf0e10cSrcweir //*****************************************************************************************************************
impl_collectAllChildComponents(const css::uno::Reference<XFramesSupplier> & xNode,Sequence<css::uno::Reference<XComponent>> & seqComponents)159cdf0e10cSrcweir void OComponentAccess::impl_collectAllChildComponents(  const   css::uno::Reference< XFramesSupplier >&         xNode           ,
160cdf0e10cSrcweir                                                                 Sequence< css::uno::Reference< XComponent > >&  seqComponents   )
161cdf0e10cSrcweir {
162cdf0e10cSrcweir     // If valid node was given ...
163cdf0e10cSrcweir     if( xNode.is() == sal_True )
164cdf0e10cSrcweir     {
165cdf0e10cSrcweir         // ... continue collection at these.
166cdf0e10cSrcweir 
167cdf0e10cSrcweir         // Get the container of current node, collect the components of existing child frames
168cdf0e10cSrcweir         // and go down to next level in tree (recursive!).
169cdf0e10cSrcweir 
170cdf0e10cSrcweir         sal_Int32 nComponentCount = seqComponents.getLength();
171cdf0e10cSrcweir 
172cdf0e10cSrcweir         const css::uno::Reference< XFrames >                xContainer  = xNode->getFrames();
173cdf0e10cSrcweir         const Sequence< css::uno::Reference< XFrame > > seqFrames   = xContainer->queryFrames( FrameSearchFlag::CHILDREN );
174cdf0e10cSrcweir 
175cdf0e10cSrcweir         const sal_Int32 nFrameCount = seqFrames.getLength();
176cdf0e10cSrcweir         for( sal_Int32 nFrame=0; nFrame<nFrameCount; ++nFrame )
177cdf0e10cSrcweir         {
178cdf0e10cSrcweir             css::uno::Reference< XComponent > xComponent = impl_getFrameComponent( seqFrames[nFrame] );
179cdf0e10cSrcweir             if( xComponent.is() == sal_True )
180cdf0e10cSrcweir             {
181cdf0e10cSrcweir                 nComponentCount++;
182cdf0e10cSrcweir                 seqComponents.realloc( nComponentCount );
183cdf0e10cSrcweir                 seqComponents[nComponentCount-1] = xComponent;
184cdf0e10cSrcweir             }
185cdf0e10cSrcweir         }
186cdf0e10cSrcweir     }
187cdf0e10cSrcweir     // ... otherwise break a recursive path and go back at current stack!
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir //*****************************************************************************************************************
191cdf0e10cSrcweir //  private method
192cdf0e10cSrcweir //*****************************************************************************************************************
impl_getFrameComponent(const css::uno::Reference<XFrame> & xFrame) const193cdf0e10cSrcweir css::uno::Reference< XComponent > OComponentAccess::impl_getFrameComponent( const css::uno::Reference< XFrame >& xFrame ) const
194cdf0e10cSrcweir {
195cdf0e10cSrcweir     // Set default return value, if method failed.
196cdf0e10cSrcweir     css::uno::Reference< XComponent > xComponent = css::uno::Reference< XComponent >();
197cdf0e10cSrcweir     // Does no controller exists?
198cdf0e10cSrcweir     css::uno::Reference< XController > xController = xFrame->getController();
199cdf0e10cSrcweir     if ( xController.is() == sal_False )
200cdf0e10cSrcweir     {
201cdf0e10cSrcweir         // Controller not exist - use the VCL-component.
202cdf0e10cSrcweir         xComponent = css::uno::Reference< XComponent >( xFrame->getComponentWindow(), UNO_QUERY );
203cdf0e10cSrcweir     }
204cdf0e10cSrcweir     else
205cdf0e10cSrcweir     {
206cdf0e10cSrcweir         // Does no model exists?
207cdf0e10cSrcweir         css::uno::Reference< XModel > xModel( xController->getModel(), UNO_QUERY );
208cdf0e10cSrcweir         if ( xModel.is() == sal_True )
209cdf0e10cSrcweir         {
210cdf0e10cSrcweir             // Model exist - use the model as component.
211cdf0e10cSrcweir             xComponent = css::uno::Reference< XComponent >( xModel, UNO_QUERY );
212cdf0e10cSrcweir         }
213cdf0e10cSrcweir         else
214cdf0e10cSrcweir         {
215cdf0e10cSrcweir             // Model not exist - use the controller as component.
216cdf0e10cSrcweir             xComponent = css::uno::Reference< XComponent >( xController, UNO_QUERY );
217cdf0e10cSrcweir         }
218cdf0e10cSrcweir     }
219cdf0e10cSrcweir 
220cdf0e10cSrcweir     return xComponent;
221cdf0e10cSrcweir }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir //_________________________________________________________________________________________________________________
224cdf0e10cSrcweir //  debug methods
225cdf0e10cSrcweir //_________________________________________________________________________________________________________________
226cdf0e10cSrcweir 
227cdf0e10cSrcweir /*-----------------------------------------------------------------------------------------------------------------
228cdf0e10cSrcweir     The follow methods checks the parameter for other functions. If a parameter or his value is non valid,
229cdf0e10cSrcweir     we return "sal_False". (else sal_True) This mechanism is used to throw an ASSERT!
230cdf0e10cSrcweir 
231cdf0e10cSrcweir     ATTENTION
232cdf0e10cSrcweir 
233*07a3d7f1SPedro Giffuni         If you miss a test for one of this parameters, contact the author or add it himself !(?)
234cdf0e10cSrcweir         But ... look for right testing! See using of this methods!
235cdf0e10cSrcweir -----------------------------------------------------------------------------------------------------------------*/
236cdf0e10cSrcweir 
237cdf0e10cSrcweir #ifdef ENABLE_ASSERTIONS
238cdf0e10cSrcweir 
239cdf0e10cSrcweir //*****************************************************************************************************************
impldbg_checkParameter_OComponentAccessCtor(const css::uno::Reference<XDesktop> & xOwner)240cdf0e10cSrcweir sal_Bool OComponentAccess::impldbg_checkParameter_OComponentAccessCtor( const   css::uno::Reference< XDesktop >&      xOwner  )
241cdf0e10cSrcweir {
242cdf0e10cSrcweir     // Set default return value.
243cdf0e10cSrcweir     sal_Bool bOK = sal_True;
244cdf0e10cSrcweir     // Check parameter.
245cdf0e10cSrcweir     if  (
246cdf0e10cSrcweir             ( &xOwner       ==  NULL        )   ||
247cdf0e10cSrcweir             ( xOwner.is()   ==  sal_False   )
248cdf0e10cSrcweir         )
249cdf0e10cSrcweir     {
250cdf0e10cSrcweir         bOK = sal_False ;
251cdf0e10cSrcweir     }
252cdf0e10cSrcweir     // Return result of check.
253cdf0e10cSrcweir     return bOK ;
254cdf0e10cSrcweir }
255cdf0e10cSrcweir 
256cdf0e10cSrcweir #endif  //  #ifdef ENABLE_ASSERTIONS
257cdf0e10cSrcweir 
258cdf0e10cSrcweir }       //  namespace framework
259