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