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