1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <helper/oframes.hxx>
22 #include <threadhelp/resetableguard.hxx>
24 #include <com/sun/star/frame/XDesktop.hpp>
25 #include <com/sun/star/frame/FrameSearchFlag.hpp>
27 #include <vcl/svapp.hxx>
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 ::std
;
40 //*****************************************************************************************************************
42 //*****************************************************************************************************************
43 OFrames::OFrames( const css::uno::Reference
< XMultiServiceFactory
>& xFactory
,
44 const css::uno::Reference
< XFrame
>& xOwner
,
45 FrameContainer
* pFrameContainer
)
46 // Init baseclasses first
47 : ThreadHelpBase ( &Application::GetSolarMutex() )
49 , m_xFactory ( xFactory
)
51 , m_pFrameContainer ( pFrameContainer
)
52 , m_bRecursiveSearchProtection( sal_False
)
54 // Safe impossible cases
55 // Method is not defined for ALL incoming parameters!
56 LOG_ASSERT( impldbg_checkParameter_OFramesCtor( xFactory
, xOwner
, pFrameContainer
), "OFrames::OFrames()\nInvalid parameter detected!\n" )
59 //*****************************************************************************************************************
60 // (proteced!) destructor
61 //*****************************************************************************************************************
64 // Reset instance, free memory ....
68 //*****************************************************************************************************************
70 //*****************************************************************************************************************
71 void SAL_CALL
OFrames::append( const css::uno::Reference
< XFrame
>& xFrame
) throw( RuntimeException
)
73 // Ready for multithreading
74 ResetableGuard
aGuard( m_aLock
);
76 // Safe impossible cases
77 // Method is not defined for ALL incoming parameters!
78 LOG_ASSERT( impldbg_checkParameter_append( xFrame
), "OFrames::append()\nInvalid parameter detected!\n" )
80 // Do the follow only, if owner instance valid!
81 // Lock owner for follow operations - make a "hard reference"!
82 css::uno::Reference
< XFramesSupplier
> xOwner( m_xOwner
.get(), UNO_QUERY
);
83 if ( xOwner
.is() == sal_True
)
85 // Append frame to the end of the container ...
86 m_pFrameContainer
->append( xFrame
);
87 // Set owner of this instance as parent of the new frame in container!
88 xFrame
->setCreator( xOwner
);
90 // Else; Do nothing! Ouer owner is dead.
91 LOG_ASSERT( !(xOwner
.is()==sal_False
), "OFrames::append()\nOuer owner is dead - you can't append any frames ...!\n" )
94 //*****************************************************************************************************************
96 //*****************************************************************************************************************
97 void SAL_CALL
OFrames::remove( const css::uno::Reference
< XFrame
>& xFrame
) throw( RuntimeException
)
99 // Ready for multithreading
100 ResetableGuard
aGuard( m_aLock
);
102 // Safe impossible cases
103 // Method is not defined for ALL incoming parameters!
104 LOG_ASSERT( impldbg_checkParameter_remove( xFrame
), "OFrames::remove()\nInvalid parameter detected!\n" )
106 // Do the follow only, if owner instance valid!
107 // Lock owner for follow operations - make a "hard reference"!
108 css::uno::Reference
< XFramesSupplier
> xOwner( m_xOwner
.get(), UNO_QUERY
);
109 if ( xOwner
.is() == sal_True
)
111 // Search frame and remove it from container ...
112 m_pFrameContainer
->remove( xFrame
);
113 // Don't reset owner-property of removed frame!
114 // This must do the caller of this method himself.
115 // See documentation of interface XFrames for further information.
117 // Else; Do nothing! Ouer owner is dead.
118 LOG_ASSERT( !(xOwner
.is()==sal_False
), "OFrames::remove()\nOuer owner is dead - you can't remove any frames ...!\n" )
121 //*****************************************************************************************************************
123 //*****************************************************************************************************************
124 Sequence
< css::uno::Reference
< XFrame
> > SAL_CALL
OFrames::queryFrames( sal_Int32 nSearchFlags
) throw( RuntimeException
)
126 // Ready for multithreading
127 ResetableGuard
aGuard( m_aLock
);
129 // Safe impossible cases
130 // Method is not defined for ALL incoming parameters!
131 LOG_ASSERT( impldbg_checkParameter_queryFrames( nSearchFlags
), "OFrames::queryFrames()\nInvalid parameter detected!\n" )
133 // Set default return value. (empty sequence)
134 Sequence
< css::uno::Reference
< XFrame
> > seqFrames
;
136 // Do the follow only, if owner instance valid.
137 // Lock owner for follow operations - make a "hard reference"!
138 css::uno::Reference
< XFrame
> xOwner( m_xOwner
.get(), UNO_QUERY
);
139 if ( xOwner
.is() == sal_True
)
141 // Work only, if search was not started here ...!
142 if( m_bRecursiveSearchProtection
== sal_False
)
144 // This class is a helper for services, which must implement XFrames.
145 // His parent and children are MY parent and children to.
146 // All searchflags are supported by this implementation!
147 // If some flags should not be supported - don't call me with this flags!!!
149 //_____________________________________________________________________________________________________________
150 // Search with AUTO-flag is not supported yet!
151 // We think about right implementation.
152 LOG_ASSERT( !(nSearchFlags
& FrameSearchFlag::AUTO
), "OFrames::queryFrames()\nSearch with AUTO-flag is not supported yet!\nWe think about right implementation.\n" )
153 // If searched for tasks ...
154 // Its not supported yet.
155 LOG_ASSERT( !(nSearchFlags
& FrameSearchFlag::AUTO
), "OFrames::queryFrames()\nSearch for tasks not supported yet!\n" )
157 //_____________________________________________________________________________________________________________
158 // Search for ALL and GLOBAL is superflous!
159 // We support all necessary flags, from which these two flags are derived.
160 // ALL = PARENT + SELF + CHILDREN + SIBLINGS
161 // GLOBAL = ALL + TASKS
163 //_____________________________________________________________________________________________________________
164 // Add parent to list ... if any exist!
165 if( nSearchFlags
& FrameSearchFlag::PARENT
)
167 css::uno::Reference
< XFrame
> xParent( xOwner
->getCreator(), UNO_QUERY
);
168 if( xParent
.is() == sal_True
)
170 Sequence
< css::uno::Reference
< XFrame
> > seqParent( 1 );
171 seqParent
[0] = xParent
;
172 impl_appendSequence( seqFrames
, seqParent
);
176 //_____________________________________________________________________________________________________________
177 // Add owner to list if SELF is searched.
178 if( nSearchFlags
& FrameSearchFlag::SELF
)
180 Sequence
< css::uno::Reference
< XFrame
> > seqSelf( 1 );
182 impl_appendSequence( seqFrames
, seqSelf
);
185 //_____________________________________________________________________________________________________________
186 // Add SIBLINGS to list.
187 if( nSearchFlags
& FrameSearchFlag::SIBLINGS
)
189 // Else; start a new search.
190 // Protect this instance against recursive calls from parents.
191 m_bRecursiveSearchProtection
= sal_True
;
192 // Ask parent of my owner for frames and append results to return list.
193 css::uno::Reference
< XFramesSupplier
> xParent( xOwner
->getCreator(), UNO_QUERY
);
194 // If a parent exist ...
195 if ( xParent
.is() == sal_True
)
197 // ... ask him for right frames.
198 impl_appendSequence( seqFrames
, xParent
->getFrames()->queryFrames( nSearchFlags
) );
200 // We have all searched information.
201 // Reset protection-mode.
202 m_bRecursiveSearchProtection
= sal_False
;
205 //_____________________________________________________________________________________________________________
206 // If searched for children, step over all elements in container and collect the information.
207 if ( nSearchFlags
& FrameSearchFlag::CHILDREN
)
209 // Don't search for parents, siblings and self at children!
210 // These things are supported by this instance himself.
211 sal_Int32 nChildSearchFlags
= FrameSearchFlag::SELF
| FrameSearchFlag::CHILDREN
;
212 // Step over all items of container and ask children for frames.
213 sal_uInt32 nCount
= m_pFrameContainer
->getCount();
214 for ( sal_uInt32 nIndex
=0; nIndex
<nCount
; ++nIndex
)
216 // We don't must control this conversion.
217 // We have done this at append()!
218 css::uno::Reference
< XFramesSupplier
> xItem( (*m_pFrameContainer
)[nIndex
], UNO_QUERY
);
219 impl_appendSequence( seqFrames
, xItem
->getFrames()->queryFrames( nChildSearchFlags
) );
224 // Else; Do nothing! Ouer owner is dead.
225 LOG_ASSERT( !(xOwner
.is()==sal_False
), "OFrames::queryFrames()\nOuer owner is dead - you can't query for frames ...!\n" )
227 // Resturn result of this operation.
231 //*****************************************************************************************************************
233 //*****************************************************************************************************************
234 sal_Int32 SAL_CALL
OFrames::getCount() throw( RuntimeException
)
236 // Ready for multithreading
237 ResetableGuard
aGuard( m_aLock
);
239 // Set default return value.
240 sal_Int32 nCount
= 0;
242 // Do the follow only, if owner instance valid.
243 // Lock owner for follow operations - make a "hard reference"!
244 css::uno::Reference
< XFrame
> xOwner( m_xOwner
.get(), UNO_QUERY
);
245 if ( xOwner
.is() == sal_True
)
247 // Set CURRENT size of container for return.
248 nCount
= m_pFrameContainer
->getCount();
255 //*****************************************************************************************************************
257 //*****************************************************************************************************************
258 Any SAL_CALL
OFrames::getByIndex( sal_Int32 nIndex
) throw( IndexOutOfBoundsException
,
259 WrappedTargetException
,
262 // Ready for multithreading
263 ResetableGuard
aGuard( m_aLock
);
265 sal_uInt32 nCount
= m_pFrameContainer
->getCount();
266 if ( nIndex
< 0 || ( sal::static_int_cast
< sal_uInt32
>( nIndex
) >= nCount
))
267 throw IndexOutOfBoundsException( OUString("OFrames::getByIndex - Index out of bounds"),
268 (OWeakObject
*)this );
270 // Set default return value.
273 // Do the follow only, if owner instance valid.
274 // Lock owner for follow operations - make a "hard reference"!
275 css::uno::Reference
< XFrame
> xOwner( m_xOwner
.get(), UNO_QUERY
);
276 if ( xOwner
.is() == sal_True
)
278 // Get element form container.
279 // (If index not valid, FrameContainer return NULL!)
280 aReturnValue
<<= (*m_pFrameContainer
)[nIndex
];
283 // Return result of this operation.
287 //*****************************************************************************************************************
289 //*****************************************************************************************************************
290 Type SAL_CALL
OFrames::getElementType() throw( RuntimeException
)
292 // This "container" support XFrame-interfaces only!
293 return ::getCppuType( (const css::uno::Reference
< XFrame
>*)NULL
);
296 //*****************************************************************************************************************
298 //*****************************************************************************************************************
299 sal_Bool SAL_CALL
OFrames::hasElements() throw( RuntimeException
)
301 // Ready for multithreading
302 ResetableGuard
aGuard( m_aLock
);
304 // Set default return value.
305 sal_Bool bHasElements
= sal_False
;
306 // Do the follow only, if owner instance valid.
307 // Lock owner for follow operations - make a "hard reference"!
308 css::uno::Reference
< XFrame
> xOwner( m_xOwner
.get(), UNO_QUERY
);
309 if ( xOwner
.is() == sal_True
)
311 // If some elements exist ...
312 if ( m_pFrameContainer
->getCount() > 0 )
314 // ... change this state value!
315 bHasElements
= sal_True
;
318 // Return result of this operation.
322 //*****************************************************************************************************************
324 //*****************************************************************************************************************
325 void OFrames::impl_resetObject()
328 // Write this for multiple calls - NOT AT THE SAME TIME - but for more then one call again)!
329 // It exist two ways to call this method. From destructor and from disposing().
330 // I can't say, which one is the first. Normaly the disposing-call - but other way ....
332 // This instance can't work if the weakreference to owner is invalid!
333 // Destroy this to reset this object.
334 m_xOwner
= WeakReference
< XFrame
>();
335 // Reset pointer to shared container to!
336 m_pFrameContainer
= NULL
;
339 //*****************************************************************************************************************
341 //*****************************************************************************************************************
342 void OFrames::impl_appendSequence( Sequence
< css::uno::Reference
< XFrame
> >& seqDestination
,
343 const Sequence
< css::uno::Reference
< XFrame
> >& seqSource
)
345 // Get some information about the sequences.
346 sal_Int32 nSourceCount
= seqSource
.getLength();
347 sal_Int32 nDestinationCount
= seqDestination
.getLength();
348 const css::uno::Reference
< XFrame
>* pSourceAccess
= seqSource
.getConstArray();
349 css::uno::Reference
< XFrame
>* pDestinationAccess
= seqDestination
.getArray();
351 // Get memory for result list.
352 Sequence
< css::uno::Reference
< XFrame
> > seqResult ( nSourceCount
+ nDestinationCount
);
353 css::uno::Reference
< XFrame
>* pResultAccess
= seqResult
.getArray();
354 sal_Int32 nResultPosition
= 0;
356 // Copy all items from first sequence.
357 for ( sal_Int32 nSourcePosition
=0; nSourcePosition
<nSourceCount
; ++nSourcePosition
)
359 pResultAccess
[nResultPosition
] = pSourceAccess
[nSourcePosition
];
363 // Don't manipulate nResultPosition between these two loops!
364 // Its the current position in the result list.
366 // Copy all items from second sequence.
367 for ( sal_Int32 nDestinationPosition
=0; nDestinationPosition
<nDestinationCount
; ++nDestinationPosition
)
369 pResultAccess
[nResultPosition
] = pDestinationAccess
[nDestinationPosition
];
373 // Return result of this operation.
374 seqDestination
.realloc( 0 );
375 seqDestination
= seqResult
;
378 //_________________________________________________________________________________________________________________
380 //_________________________________________________________________________________________________________________
382 /*-----------------------------------------------------------------------------------------------------------------
383 The follow methods checks the parameter for other functions. If a parameter or his value is non valid,
384 we return "sal_False". (else sal_True) This mechanism is used to throw an ASSERT!
388 If you miss a test for one of this parameters, contact the autor or add it himself !(?)
389 But ... look for right testing! See using of this methods!
390 -----------------------------------------------------------------------------------------------------------------*/
392 #ifdef ENABLE_ASSERTIONS
394 //*****************************************************************************************************************
395 // An instance of this class can only work with valid initialization.
396 // We share the mutex with ouer owner class, need a valid factory to instanciate new services and
397 // use the access to ouer owner for some operations.
398 sal_Bool
OFrames::impldbg_checkParameter_OFramesCtor( const css::uno::Reference
< XMultiServiceFactory
>& xFactory
,
399 const css::uno::Reference
< XFrame
>& xOwner
,
400 FrameContainer
* pFrameContainer
)
402 // Set default return value.
403 sal_Bool bOK
= sal_True
;
406 ( &xFactory
== NULL
) ||
407 ( &xOwner
== NULL
) ||
408 ( xFactory
.is() == sal_False
) ||
409 ( xOwner
.is() == sal_False
) ||
410 ( pFrameContainer
== NULL
)
415 // Return result of check.
419 //*****************************************************************************************************************
420 // Its only allowed to add valid references to container.
421 // AND - alle frames must support XFrames-interface!
422 sal_Bool
OFrames::impldbg_checkParameter_append( const css::uno::Reference
< XFrame
>& xFrame
)
424 // Set default return value.
425 sal_Bool bOK
= sal_True
;
428 ( &xFrame
== NULL
) ||
429 ( xFrame
.is() == sal_False
)
434 // Return result of check.
438 //*****************************************************************************************************************
439 // Its only allowed to add valid references to container...
440 // ... => You can only delete valid references!
441 sal_Bool
OFrames::impldbg_checkParameter_remove( const css::uno::Reference
< XFrame
>& xFrame
)
443 // Set default return value.
444 sal_Bool bOK
= sal_True
;
447 ( &xFrame
== NULL
) ||
448 ( xFrame
.is() == sal_False
)
453 // Return result of check.
457 //*****************************************************************************************************************
458 // A search for frames must initiate with right flags.
459 // Some one are superflous and not supported yet. But here we control only the range of incoming parameter!
460 sal_Bool
OFrames::impldbg_checkParameter_queryFrames( sal_Int32 nSearchFlags
)
462 // Set default return value.
463 sal_Bool bOK
= sal_True
;
466 ( nSearchFlags
!= FrameSearchFlag::AUTO
) &&
467 ( !( nSearchFlags
& FrameSearchFlag::PARENT
) ) &&
468 ( !( nSearchFlags
& FrameSearchFlag::SELF
) ) &&
469 ( !( nSearchFlags
& FrameSearchFlag::CHILDREN
) ) &&
470 ( !( nSearchFlags
& FrameSearchFlag::CREATE
) ) &&
471 ( !( nSearchFlags
& FrameSearchFlag::SIBLINGS
) ) &&
472 ( !( nSearchFlags
& FrameSearchFlag::TASKS
) ) &&
473 ( !( nSearchFlags
& FrameSearchFlag::ALL
) ) &&
474 ( !( nSearchFlags
& FrameSearchFlag::GLOBAL
) )
479 // Return result of check.
483 #endif // #ifdef ENABLE_ASSERTIONS
485 } // namespace framework
487 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */