LibreOffice Module framework (master) 1
ocomponentaccess.cxx
Go to the documentation of this file.
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
22
23#include <com/sun/star/frame/FrameSearchFlag.hpp>
24
25#include <vcl/svapp.hxx>
26#include <sal/log.hxx>
27
28namespace framework{
29
30using namespace ::com::sun::star::container;
31using namespace ::com::sun::star::frame;
32using namespace ::com::sun::star::lang;
33using namespace ::com::sun::star::uno;
34using namespace ::cppu;
35using namespace ::osl;
36
37// constructor
38
39OComponentAccess::OComponentAccess( const css::uno::Reference< XDesktop >& xOwner )
40 : m_xOwner ( xOwner )
41{
42 // Safe impossible cases
43 SAL_WARN_IF( !xOwner.is(), "fwk", "OComponentAccess::OComponentAccess(): Invalid parameter detected!" );
44}
45
46// destructor
47
49{
50}
51
52// XEnumerationAccess
53css::uno::Reference< XEnumeration > SAL_CALL OComponentAccess::createEnumeration()
54{
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 css::uno::Reference< XEnumeration > xReturn;
60
61 // Try to "lock" the desktop for access to task container.
62 css::uno::Reference< XInterface > xLock = m_xOwner.get();
63 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 std::vector< css::uno::Reference< XComponent > > seqComponents;
70 impl_collectAllChildComponents( css::uno::Reference< XFramesSupplier >( xLock, UNO_QUERY ), seqComponents );
71 xReturn = new OComponentEnumeration( std::move(seqComponents) );
72 }
73
74 // Return result of this operation.
75 return xReturn;
76}
77
78// XElementAccess
80{
81 // Elements in list an enumeration are components!
82 // Return the uno-type of XComponent.
84}
85
86// XElementAccess
88{
90
91 // Set default return value, if method failed.
92 bool bReturn = false;
93
94 // Try to "lock" the desktop for access to task container.
95 css::uno::Reference< XFramesSupplier > xLock( m_xOwner.get(), UNO_QUERY );
96 if ( xLock.is() )
97 {
98 // Ask container of owner for existing elements.
99 bReturn = xLock->getFrames()->hasElements();
100 }
101
102 // Return result of this operation.
103 return bReturn;
104}
105
106
107void OComponentAccess::impl_collectAllChildComponents( const css::uno::Reference< XFramesSupplier >& xNode ,
108 std::vector< css::uno::Reference< XComponent > >& seqComponents )
109{
110 // If valid node was given ...
111 if( !xNode.is() )
112 return;
113
114 // ... continue collection at these.
115
116 // Get the container of current node, collect the components of existing child frames
117 // and go down to next level in tree (recursive!).
118
119 const css::uno::Reference< XFrames > xContainer = xNode->getFrames();
120 const Sequence< css::uno::Reference< XFrame > > seqFrames = xContainer->queryFrames( FrameSearchFlag::CHILDREN );
121
122 const sal_Int32 nFrameCount = seqFrames.getLength();
123 for( sal_Int32 nFrame=0; nFrame<nFrameCount; ++nFrame )
124 {
125 css::uno::Reference< XComponent > xComponent = impl_getFrameComponent( seqFrames[nFrame] );
126 if( xComponent.is() )
127 {
128 seqComponents.push_back( xComponent );
129 }
130 }
131 // ... otherwise break a recursive path and go back at current stack!
132}
133
134css::uno::Reference< XComponent > OComponentAccess::impl_getFrameComponent( const css::uno::Reference< XFrame >& xFrame ) const
135{
136 // Set default return value, if method failed.
137 css::uno::Reference< XComponent > xComponent;
138 // Does no controller exists?
139 css::uno::Reference< XController > xController = xFrame->getController();
140 if ( !xController.is() )
141 {
142 // Controller not exist - use the VCL-component.
143 xComponent = xFrame->getComponentWindow();
144 }
145 else
146 {
147 // Does no model exists?
148 css::uno::Reference< XModel > xModel = xController->getModel();
149 if ( xModel.is() )
150 {
151 // Model exist - use the model as component.
152 xComponent = xModel;
153 }
154 else
155 {
156 // Model not exist - use the controller as component.
157 xComponent = xController;
158 }
159 }
160
161 return xComponent;
162}
163
164
165} // namespace framework
166
167/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Type const & get()
css::uno::WeakReference< css::frame::XDesktop > m_xOwner
OComponentAccess(const css::uno::Reference< css::frame::XDesktop > &xOwner)
virtual sal_Bool SAL_CALL hasElements() override
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
css::uno::Reference< css::lang::XComponent > impl_getFrameComponent(const css::uno::Reference< css::frame::XFrame > &xFrame) const
virtual css::uno::Type SAL_CALL getElementType() override
void impl_collectAllChildComponents(const css::uno::Reference< css::frame::XFramesSupplier > &xNode, std::vector< css::uno::Reference< css::lang::XComponent > > &seqComponents)
virtual ~OComponentAccess() override
#define SAL_WARN_IF(condition, area, stream)
Type
Reference< XController > xController
Reference< XFrame > xFrame
Reference< XModel > xModel
unsigned char sal_Bool