Branch data 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 : : #include <helper/ocomponentaccess.hxx>
21 : : #include <helper/ocomponentenumeration.hxx>
22 : :
23 : : #include <threadhelp/resetableguard.hxx>
24 : :
25 : : #include <com/sun/star/frame/FrameSearchFlag.hpp>
26 : :
27 : : #include <vcl/svapp.hxx>
28 : :
29 : : namespace framework{
30 : :
31 : : using namespace ::com::sun::star::container ;
32 : : using namespace ::com::sun::star::frame ;
33 : : using namespace ::com::sun::star::lang ;
34 : : using namespace ::com::sun::star::uno ;
35 : : using namespace ::cppu ;
36 : : using namespace ::osl ;
37 : : using namespace ::rtl ;
38 : :
39 : : //*****************************************************************************************************************
40 : : // constructor
41 : : //*****************************************************************************************************************
42 : 8 : OComponentAccess::OComponentAccess( const css::uno::Reference< XDesktop >& xOwner )
43 : : // Init baseclasses first
44 : 8 : : ThreadHelpBase ( &Application::GetSolarMutex() )
45 : : // Init member
46 [ + - ][ + - ]: 8 : , m_xOwner ( xOwner )
47 : : {
48 : : // Safe impossible cases
49 : : LOG_ASSERT( impldbg_checkParameter_OComponentAccessCtor( xOwner ), "OComponentAccess::OComponentAccess()\nInvalid parameter detected!\n" )
50 : 8 : }
51 : :
52 : : //*****************************************************************************************************************
53 : : // destructor
54 : : //*****************************************************************************************************************
55 [ + - ][ + - ]: 8 : OComponentAccess::~OComponentAccess()
56 : : {
57 [ - + ]: 16 : }
58 : :
59 : : //*****************************************************************************************************************
60 : : // XEnumerationAccess
61 : : //*****************************************************************************************************************
62 : 8 : css::uno::Reference< XEnumeration > SAL_CALL OComponentAccess::createEnumeration() throw( RuntimeException )
63 : : {
64 : : // Ready for multithreading
65 [ + - ]: 8 : ResetableGuard aGuard( m_aLock );
66 : :
67 : : // Set default return value, if method failed.
68 : : // If no desktop exist and there is no task container - return an empty enumeration!
69 : 8 : css::uno::Reference< XEnumeration > xReturn = css::uno::Reference< XEnumeration >();
70 : :
71 : : // Try to "lock" the desktop for access to task container.
72 [ + - ]: 8 : css::uno::Reference< XInterface > xLock = m_xOwner.get();
73 [ + - ]: 8 : if ( xLock.is() == sal_True )
74 : : {
75 : : // Desktop exist => pointer to task container must be valid.
76 : : // Initialize a new enumeration ... if some tasks and his components exist!
77 : : // (OTasksEnumeration will make an assert, if we initialize the new instance without valid values!)
78 : :
79 [ + - ]: 8 : Sequence< css::uno::Reference< XComponent > > seqComponents;
80 [ + - ][ + - ]: 8 : impl_collectAllChildComponents( css::uno::Reference< XFramesSupplier >( xLock, UNO_QUERY ), seqComponents );
81 [ + - ]: 8 : OComponentEnumeration* pEnumeration = new OComponentEnumeration( seqComponents );
82 [ + - ][ + - ]: 8 : xReturn = css::uno::Reference< XEnumeration >( (OWeakObject*)pEnumeration, UNO_QUERY );
[ + - ]
83 : : }
84 : :
85 : : // Return result of this operation.
86 [ + - ]: 8 : return xReturn;
87 : : }
88 : :
89 : : //*****************************************************************************************************************
90 : : // XElementAccess
91 : : //*****************************************************************************************************************
92 : 0 : Type SAL_CALL OComponentAccess::getElementType() throw( RuntimeException )
93 : : {
94 : : // Elements in list an enumeration are components!
95 : : // Return the uno-type of XComponent.
96 : 0 : return ::getCppuType((const css::uno::Reference< XComponent >*)NULL);
97 : : }
98 : :
99 : : //*****************************************************************************************************************
100 : : // XElementAccess
101 : : //*****************************************************************************************************************
102 : 0 : sal_Bool SAL_CALL OComponentAccess::hasElements() throw( RuntimeException )
103 : : {
104 : : // Ready for multithreading
105 [ # # ]: 0 : ResetableGuard aGuard( m_aLock );
106 : :
107 : : // Set default return value, if method failed.
108 : 0 : sal_Bool bReturn = sal_False;
109 : :
110 : : // Try to "lock" the desktop for access to task container.
111 [ # # ][ # # ]: 0 : css::uno::Reference< XFramesSupplier > xLock( m_xOwner.get(), UNO_QUERY );
112 [ # # ]: 0 : if ( xLock.is() == sal_True )
113 : : {
114 : : // Ask container of owner for existing elements.
115 [ # # ][ # # ]: 0 : bReturn = xLock->getFrames()->hasElements();
[ # # ][ # # ]
116 : : }
117 : :
118 : : // Return result of this operation.
119 [ # # ]: 0 : return bReturn;
120 : : }
121 : :
122 : : //*****************************************************************************************************************
123 : : // private method
124 : : //*****************************************************************************************************************
125 : 8 : void OComponentAccess::impl_collectAllChildComponents( const css::uno::Reference< XFramesSupplier >& xNode ,
126 : : Sequence< css::uno::Reference< XComponent > >& seqComponents )
127 : : {
128 : : // If valid node was given ...
129 [ + - ]: 8 : if( xNode.is() == sal_True )
130 : : {
131 : : // ... continue collection at these.
132 : :
133 : : // Get the container of current node, collect the components of existing child frames
134 : : // and go down to next level in tree (recursive!).
135 : :
136 : 8 : sal_Int32 nComponentCount = seqComponents.getLength();
137 : :
138 [ + - ][ + - ]: 8 : const css::uno::Reference< XFrames > xContainer = xNode->getFrames();
139 [ + - ][ + - ]: 8 : const Sequence< css::uno::Reference< XFrame > > seqFrames = xContainer->queryFrames( FrameSearchFlag::CHILDREN );
140 : :
141 : 8 : const sal_Int32 nFrameCount = seqFrames.getLength();
142 [ + + ]: 16 : for( sal_Int32 nFrame=0; nFrame<nFrameCount; ++nFrame )
143 : : {
144 [ + - ]: 8 : css::uno::Reference< XComponent > xComponent = impl_getFrameComponent( seqFrames[nFrame] );
145 [ + - ]: 8 : if( xComponent.is() == sal_True )
146 : : {
147 : 8 : nComponentCount++;
148 [ + - ]: 8 : seqComponents.realloc( nComponentCount );
149 [ + - ][ + - ]: 8 : seqComponents[nComponentCount-1] = xComponent;
150 : : }
151 [ + - ]: 16 : }
152 : : }
153 : : // ... otherwise break a recursive path and go back at current stack!
154 : 8 : }
155 : :
156 : : //*****************************************************************************************************************
157 : : // private method
158 : : //*****************************************************************************************************************
159 : 8 : css::uno::Reference< XComponent > OComponentAccess::impl_getFrameComponent( const css::uno::Reference< XFrame >& xFrame ) const
160 : : {
161 : : // Set default return value, if method failed.
162 : 8 : css::uno::Reference< XComponent > xComponent = css::uno::Reference< XComponent >();
163 : : // Does no controller exists?
164 [ + - ][ + - ]: 8 : css::uno::Reference< XController > xController = xFrame->getController();
165 [ - + ]: 8 : if ( xController.is() == sal_False )
166 : : {
167 : : // Controller not exist - use the VCL-component.
168 [ # # ][ # # ]: 0 : xComponent = css::uno::Reference< XComponent >( xFrame->getComponentWindow(), UNO_QUERY );
[ # # ][ # # ]
169 : : }
170 : : else
171 : : {
172 : : // Does no model exists?
173 [ + - ][ + - ]: 8 : css::uno::Reference< XModel > xModel( xController->getModel(), UNO_QUERY );
[ + - ]
174 [ + - ]: 8 : if ( xModel.is() == sal_True )
175 : : {
176 : : // Model exist - use the model as component.
177 [ + - ][ + - ]: 8 : xComponent = css::uno::Reference< XComponent >( xModel, UNO_QUERY );
178 : : }
179 : : else
180 : : {
181 : : // Model not exist - use the controller as component.
182 [ # # ][ # # ]: 0 : xComponent = css::uno::Reference< XComponent >( xController, UNO_QUERY );
183 : 8 : }
184 : : }
185 : :
186 : 8 : return xComponent;
187 : : }
188 : :
189 : : //_________________________________________________________________________________________________________________
190 : : // debug methods
191 : : //_________________________________________________________________________________________________________________
192 : :
193 : : /*-----------------------------------------------------------------------------------------------------------------
194 : : The follow methods checks the parameter for other functions. If a parameter or his value is non valid,
195 : : we return "sal_False". (else sal_True) This mechanism is used to throw an ASSERT!
196 : :
197 : : ATTENTION
198 : :
199 : : If you miss a test for one of this parameters, contact the autor or add it himself !(?)
200 : : But ... look for right testing! See using of this methods!
201 : : -----------------------------------------------------------------------------------------------------------------*/
202 : :
203 : : #ifdef ENABLE_ASSERTIONS
204 : :
205 : : //*****************************************************************************************************************
206 : : sal_Bool OComponentAccess::impldbg_checkParameter_OComponentAccessCtor( const css::uno::Reference< XDesktop >& xOwner )
207 : : {
208 : : // Set default return value.
209 : : sal_Bool bOK = sal_True;
210 : : // Check parameter.
211 : : if (
212 : : ( &xOwner == NULL ) ||
213 : : ( xOwner.is() == sal_False )
214 : : )
215 : : {
216 : : bOK = sal_False ;
217 : : }
218 : : // Return result of check.
219 : : return bOK ;
220 : : }
221 : :
222 : : #endif // #ifdef ENABLE_ASSERTIONS
223 : :
224 : : } // namespace framework
225 : :
226 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|