LibreOffice Module framework (master) 1
oframes.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
20#include <helper/oframes.hxx>
21
22#include <com/sun/star/frame/FrameSearchFlag.hpp>
23#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
24#include <vcl/svapp.hxx>
25#include <sal/log.hxx>
26
27namespace framework{
28
29using namespace ::com::sun::star::container;
30using namespace ::com::sun::star::frame;
31using namespace ::com::sun::star::lang;
32using namespace ::com::sun::star::uno;
33using namespace ::cppu;
34using namespace ::osl;
35
36// constructor
37
38OFrames::OFrames( const css::uno::Reference< XFrame >& xOwner ,
39 FrameContainer* pFrameContainer )
40 : m_xOwner ( xOwner )
41 , m_pFrameContainer ( pFrameContainer )
42 , m_bRecursiveSearchProtection( false )
43{
44 // An instance of this class can only work with valid initialization.
45 // We share the mutex with our owner class, need a valid factory to instantiate new services and
46 // use the access to our owner for some operations.
47 SAL_WARN_IF( !xOwner.is() || !pFrameContainer, "fwk", "OFrames::OFrames(): Invalid parameter detected!" );
48}
49
50// (protected!) destructor
51
53{
54 // Reset instance, free memory...
56}
57
58// XFrames
59void SAL_CALL OFrames::append( const css::uno::Reference< XFrame >& xFrame )
60{
62
63 // Safe impossible cases
64 // Method is not defined for ALL incoming parameters!
65 SAL_WARN_IF( !xFrame.is(), "fwk", "OFrames::append(): Invalid parameter detected!" );
66
67 // Do the follow only, if owner instance valid!
68 // Lock owner for follow operations - make a "hard reference"!
69 css::uno::Reference< XFramesSupplier > xOwner( m_xOwner.get(), UNO_QUERY );
70 if ( xOwner.is() )
71 {
72 // Append frame to the end of the container ...
74 // Set owner of this instance as parent of the new frame in container!
75 xFrame->setCreator( xOwner );
76 }
77 // Else; Do nothing! Our owner is dead.
78 SAL_WARN_IF( !xOwner.is(), "fwk", "OFrames::append():Our owner is dead - you can't append any frames ...!" );
79}
80
81// XFrames
82void SAL_CALL OFrames::remove( const css::uno::Reference< XFrame >& xFrame )
83{
85
86 // Safe impossible cases
87 // Method is not defined for ALL incoming parameters!
88 SAL_WARN_IF( !xFrame.is(), "fwk", "OFrames::remove(): Invalid parameter detected!" );
89
90 // Do the follow only, if owner instance valid!
91 // Lock owner for follow operations - make a "hard reference"!
92 css::uno::Reference< XFramesSupplier > xOwner( m_xOwner.get(), UNO_QUERY );
93 if ( xOwner.is() )
94 {
95 // Search frame and remove it from container ...
97 // Don't reset owner-property of removed frame!
98 // This must do the caller of this method himself.
99 // See documentation of interface XFrames for further information.
100 }
101 // Else; Do nothing! Our owner is dead.
102 SAL_WARN_IF( !xOwner.is(), "fwk", "OFrames::remove(): Our owner is dead - you can't remove any frames ...!" );
103}
104
105// XFrames
106Sequence< css::uno::Reference< XFrame > > SAL_CALL OFrames::queryFrames( sal_Int32 nSearchFlags )
107{
109
110 // Safe impossible cases
111 // Method is not defined for ALL incoming parameters!
112 SAL_WARN_IF( !impldbg_checkParameter_queryFrames( nSearchFlags ), "fwk", "OFrames::queryFrames(): Invalid parameter detected!" );
113
114 // Set default return value. (empty sequence)
115 Sequence< css::uno::Reference< XFrame > > seqFrames;
116
117 // Do the follow only, if owner instance valid.
118 // Lock owner for follow operations - make a "hard reference"!
119 css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
120 if ( xOwner.is() )
121 {
122 // Work only, if search was not started here ...!
124 {
125 // This class is a helper for services, which must implement XFrames.
126 // His parent and children are MY parent and children to.
127 // All searchflags are supported by this implementation!
128 // If some flags should not be supported - don't call me with this flags!!!
129
130 // Search with AUTO-flag is not supported yet!
131 // We think about right implementation.
132 SAL_WARN_IF( (nSearchFlags & FrameSearchFlag::AUTO), "fwk", "OFrames::queryFrames(): Search with AUTO-flag is not supported yet!" );
133
134 // Search for ALL and GLOBAL is superfluous!
135 // We support all necessary flags, from which these two flags are derived.
136 // ALL = PARENT + SELF + CHILDREN + SIBLINGS
137 // GLOBAL = ALL + TASKS
138
139 // Add parent to list ... if any exist!
140 if( nSearchFlags & FrameSearchFlag::PARENT )
141 {
142 css::uno::Reference< XFrame > xParent = xOwner->getCreator();
143 if( xParent.is() )
144 {
145 impl_appendSequence( seqFrames, { xParent } );
146 }
147 }
148
149 // Add owner to list if SELF is searched.
150 if( nSearchFlags & FrameSearchFlag::SELF )
151 {
152 impl_appendSequence( seqFrames, { xOwner } );
153 }
154
155 // Add SIBLINGS to list.
156 if( nSearchFlags & FrameSearchFlag::SIBLINGS )
157 {
158 // Else; start a new search.
159 // Protect this instance against recursive calls from parents.
161 // Ask parent of my owner for frames and append results to return list.
162 css::uno::Reference< XFramesSupplier > xParent = xOwner->getCreator();
163 // If a parent exist ...
164 if ( xParent.is() )
165 {
166 // ... ask him for right frames.
167 impl_appendSequence( seqFrames, xParent->getFrames()->queryFrames( nSearchFlags ) );
168 }
169 // We have all searched information.
170 // Reset protection-mode.
172 }
173
174 // If searched for children, step over all elements in container and collect the information.
175 if ( nSearchFlags & FrameSearchFlag::CHILDREN )
176 {
177 // Don't search for parents, siblings and self at children!
178 // These things are supported by this instance himself.
179 sal_Int32 const nChildSearchFlags = FrameSearchFlag::SELF | FrameSearchFlag::CHILDREN;
180 // Step over all items of container and ask children for frames.
181 sal_uInt32 nCount = m_pFrameContainer->getCount();
182 for ( sal_uInt32 nIndex=0; nIndex<nCount; ++nIndex )
183 {
184 // We don't must control this conversion.
185 // We have done this at append()!
186 css::uno::Reference< XFramesSupplier > xItem( (*m_pFrameContainer)[nIndex], UNO_QUERY );
187 impl_appendSequence( seqFrames, xItem->getFrames()->queryFrames( nChildSearchFlags ) );
188 }
189 }
190 }
191 }
192 // Else; Do nothing! Our owner is dead.
193 SAL_WARN_IF( !xOwner.is(), "fwk", "OFrames::queryFrames(): Our owner is dead - you can't query for frames ...!" );
194
195 // Return result of this operation.
196 return seqFrames;
197}
198
199// XIndexAccess
200sal_Int32 SAL_CALL OFrames::getCount()
201{
203
204 // Set default return value.
205 sal_Int32 nCount = 0;
206
207 // Do the follow only, if owner instance valid.
208 // Lock owner for follow operations - make a "hard reference"!
209 css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
210 if ( xOwner.is() )
211 {
212 // Set CURRENT size of container for return.
214 }
215
216 // Return result.
217 return nCount;
218}
219
220// XIndexAccess
221
222Any SAL_CALL OFrames::getByIndex( sal_Int32 nIndex )
223{
225
226 sal_uInt32 nCount = m_pFrameContainer->getCount();
227 if ( nIndex < 0 || ( sal::static_int_cast< sal_uInt32 >( nIndex ) >= nCount ))
228 throw IndexOutOfBoundsException("OFrames::getByIndex - Index out of bounds",
229 static_cast<OWeakObject *>(this) );
230
231 // Set default return value.
232 Any aReturnValue;
233
234 // Do the follow only, if owner instance valid.
235 // Lock owner for follow operations - make a "hard reference"!
236 css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
237 if ( xOwner.is() )
238 {
239 // Get element form container.
240 // (If index not valid, FrameContainer return NULL!)
241 aReturnValue <<= (*m_pFrameContainer)[nIndex];
242 }
243
244 // Return result of this operation.
245 return aReturnValue;
246}
247
248// XElementAccess
250{
251 // This "container" support XFrame-interfaces only!
253}
254
255// XElementAccess
257{
259
260 // Set default return value.
261 bool bHasElements = false;
262 // Do the follow only, if owner instance valid.
263 // Lock owner for follow operations - make a "hard reference"!
264 css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
265 if ( xOwner.is() )
266 {
267 // If some elements exist ...
268 if ( m_pFrameContainer->getCount() > 0 )
269 {
270 // ... change this state value!
271 bHasElements = true;
272 }
273 }
274 // Return result of this operation.
275 return bHasElements;
276}
277
278// protected method
279
281{
282 // Attention:
283 // Write this for multiple calls - NOT AT THE SAME TIME - but for more than one call again)!
284 // It exist two ways to call this method. From destructor and from disposing().
285 // I can't say, which one is the first. Normally the disposing-call - but other way...
286
287 // This instance can't work if the weakreference to owner is invalid!
288 // Destroy this to reset this object.
289 m_xOwner.clear();
290 // Reset pointer to shared container to!
291 m_pFrameContainer = nullptr;
292}
293
294void OFrames::impl_appendSequence( Sequence< css::uno::Reference< XFrame > >& seqDestination ,
295 const Sequence< css::uno::Reference< XFrame > >& seqSource )
296{
297 // Get some information about the sequences.
298 sal_Int32 nSourceCount = seqSource.getLength();
299 sal_Int32 nDestinationCount = seqDestination.getLength();
300 const css::uno::Reference< XFrame >* pSourceAccess = seqSource.getConstArray();
301 css::uno::Reference< XFrame >* pDestinationAccess = seqDestination.getArray();
302
303 // Get memory for result list.
304 Sequence< css::uno::Reference< XFrame > > seqResult ( nSourceCount + nDestinationCount );
305 css::uno::Reference< XFrame >* pResultAccess = seqResult.getArray();
306 sal_Int32 nResultPosition = 0;
307
308 // Copy all items from first sequence.
309 for ( sal_Int32 nSourcePosition=0; nSourcePosition<nSourceCount; ++nSourcePosition )
310 {
311 pResultAccess[nResultPosition] = pSourceAccess[nSourcePosition];
312 ++nResultPosition;
313 }
314
315 // Don't manipulate nResultPosition between these two loops!
316 // It's the current position in the result list.
317
318 // Copy all items from second sequence.
319 for ( sal_Int32 nDestinationPosition=0; nDestinationPosition<nDestinationCount; ++nDestinationPosition )
320 {
321 pResultAccess[nResultPosition] = pDestinationAccess[nDestinationPosition];
322 ++nResultPosition;
323 }
324
325 // Return result of this operation.
326 seqDestination.realloc( 0 );
327 seqDestination = seqResult;
328}
329
330// debug methods
331
332/*-----------------------------------------------------------------------------------------------------------------
333 The follow methods checks the parameter for other functions. If a parameter or his value is non valid,
334 we return "sal_False". (else sal_True) This mechanism is used to throw an ASSERT!
335
336 ATTENTION
337
338 If you miss a test for one of this parameters, contact the author or add it himself !(?)
339 But ... look for right testing! See using of this methods!
340-----------------------------------------------------------------------------------------------------------------*/
341
342// A search for frames must initiate with right flags.
343// Some one are superfluous and not supported yet. But here we control only the range of incoming parameter!
345{
346 // Set default return value.
347 bool bOK = true;
348 // Check parameter.
349 if (
350 ( nSearchFlags != FrameSearchFlag::AUTO ) &&
351 ( !( nSearchFlags & FrameSearchFlag::PARENT ) ) &&
352 ( !( nSearchFlags & FrameSearchFlag::SELF ) ) &&
353 ( !( nSearchFlags & FrameSearchFlag::CHILDREN ) ) &&
354 ( !( nSearchFlags & FrameSearchFlag::CREATE ) ) &&
355 ( !( nSearchFlags & FrameSearchFlag::SIBLINGS ) ) &&
356 ( !( nSearchFlags & FrameSearchFlag::TASKS ) ) &&
357 ( !( nSearchFlags & FrameSearchFlag::ALL ) ) &&
358 ( !( nSearchFlags & FrameSearchFlag::GLOBAL ) )
359 )
360 {
361 bOK = false;
362 }
363 // Return result of check.
364 return bOK;
365}
366
367} // namespace framework
368
369/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Type const & get()
void append(const css::uno::Reference< css::frame::XFrame > &xFrame)
add/remove/mark container items
sal_uInt32 getCount() const
deprecated IndexAccess!
void remove(const css::uno::Reference< css::frame::XFrame > &xFrame)
-****************************************************************************************************...
virtual void SAL_CALL append(const css::uno::Reference< css::frame::XFrame > &xFrame) override
append frame to container @descr We share the container with our owner.
Definition: oframes.cxx:59
virtual sal_Bool SAL_CALL hasElements() override
get fill state of current container @descr Call these to get information about, if items exist in con...
Definition: oframes.cxx:256
virtual css::uno::Any SAL_CALL getByIndex(sal_Int32 nIndex) override
get specified container item by index @descr If you called getCount() successful - this method return...
Definition: oframes.cxx:222
void impl_resetObject()
reset instance to default values @descr There are two ways to delete an instance of this class.
Definition: oframes.cxx:280
virtual sal_Int32 SAL_CALL getCount() override
get count of all current frames in container @descr This is the beginning of full index-access.
Definition: oframes.cxx:200
void impl_appendSequence(css::uno::Sequence< css::uno::Reference< css::frame::XFrame > > &seqDestination, const css::uno::Sequence< css::uno::Reference< css::frame::XFrame > > &seqSource)
append one sequence to another @descr There is no operation to add to sequences! Use this helper-meth...
Definition: oframes.cxx:294
bool m_bRecursiveSearchProtection
with owner shared list to hold all direct children of an XFramesSupplier
Definition: oframes.hxx:185
virtual css::uno::Type SAL_CALL getElementType() override
get uno-type of all container items @descr In current implementation type is fixed to XFrame!...
Definition: oframes.cxx:249
css::uno::WeakReference< css::frame::XFrame > m_xOwner
Definition: oframes.hxx:183
virtual void SAL_CALL remove(const css::uno::Reference< css::frame::XFrame > &xFrame) override
remove frame from container @descr This is the companion to append().
Definition: oframes.cxx:82
static bool impldbg_checkParameter_queryFrames(sal_Int32 nSearchFlags)
Definition: oframes.cxx:344
OFrames(const css::uno::Reference< css::frame::XFrame > &xOwner, FrameContainer *pFrameContainer)
standard ctor @descr These initialize a new instance of this class with all needed information for wo...
Definition: oframes.cxx:38
FrameContainer * m_pFrameContainer
reference to owner of this instance (Hold no hard reference!)
Definition: oframes.hxx:184
virtual css::uno::Sequence< css::uno::Reference< css::frame::XFrame > > SAL_CALL queryFrames(sal_Int32 nSearchFlags) override
return list of all applicable frames for given flags @descr Call these to get a list of all frames,...
Definition: oframes.cxx:106
virtual ~OFrames() override
standard destructor @descr This method destruct an instance of this class and clear some member.
Definition: oframes.cxx:52
int nCount
sal_Int32 nIndex
#define SAL_WARN_IF(condition, area, stream)
Type
Reference< XFrame > xFrame
unsigned char sal_Bool