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/oframes.hxx>
21 :
22 : #include <threadhelp/resetableguard.hxx>
23 :
24 : #include <com/sun/star/frame/XDesktop.hpp>
25 : #include <com/sun/star/frame/FrameSearchFlag.hpp>
26 :
27 : #include <vcl/svapp.hxx>
28 :
29 : namespace framework{
30 :
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 ;
38 :
39 :
40 : //*****************************************************************************************************************
41 : // constructor
42 : //*****************************************************************************************************************
43 1241 : OFrames::OFrames( const css::uno::Reference< XFrame >& xOwner ,
44 : FrameContainer* pFrameContainer )
45 : // Init baseclasses first
46 1241 : : ThreadHelpBase ( &Application::GetSolarMutex() )
47 : // Init member
48 : , m_xOwner ( xOwner )
49 : , m_pFrameContainer ( pFrameContainer )
50 1241 : , m_bRecursiveSearchProtection( sal_False )
51 : {
52 : // Safe impossible cases
53 : // Method is not defined for ALL incoming parameters!
54 : LOG_ASSERT( impldbg_checkParameter_OFramesCtor( xOwner, pFrameContainer ), "OFrames::OFrames()\nInvalid parameter detected!\n" )
55 1241 : }
56 :
57 : //*****************************************************************************************************************
58 : // (proteced!) destructor
59 : //*****************************************************************************************************************
60 3699 : OFrames::~OFrames()
61 : {
62 : // Reset instance, free memory ....
63 1233 : impl_resetObject();
64 2466 : }
65 :
66 : //*****************************************************************************************************************
67 : // XFrames
68 : //*****************************************************************************************************************
69 1106 : void SAL_CALL OFrames::append( const css::uno::Reference< XFrame >& xFrame ) throw( RuntimeException )
70 : {
71 : // Ready for multithreading
72 1106 : ResetableGuard aGuard( m_aLock );
73 :
74 : // Safe impossible cases
75 : // Method is not defined for ALL incoming parameters!
76 : LOG_ASSERT( impldbg_checkParameter_append( xFrame ), "OFrames::append()\nInvalid parameter detected!\n" )
77 :
78 : // Do the follow only, if owner instance valid!
79 : // Lock owner for follow operations - make a "hard reference"!
80 2212 : css::uno::Reference< XFramesSupplier > xOwner( m_xOwner.get(), UNO_QUERY );
81 1106 : if ( xOwner.is() == sal_True )
82 : {
83 : // Append frame to the end of the container ...
84 1106 : m_pFrameContainer->append( xFrame );
85 : // Set owner of this instance as parent of the new frame in container!
86 1106 : xFrame->setCreator( xOwner );
87 1106 : }
88 : // Else; Do nothing! Ouer owner is dead.
89 : LOG_ASSERT( !(xOwner.is()==sal_False), "OFrames::append()\nOuer owner is dead - you can't append any frames ...!\n" )
90 1106 : }
91 :
92 : //*****************************************************************************************************************
93 : // XFrames
94 : //*****************************************************************************************************************
95 1099 : void SAL_CALL OFrames::remove( const css::uno::Reference< XFrame >& xFrame ) throw( RuntimeException )
96 : {
97 : // Ready for multithreading
98 1099 : ResetableGuard aGuard( m_aLock );
99 :
100 : // Safe impossible cases
101 : // Method is not defined for ALL incoming parameters!
102 : LOG_ASSERT( impldbg_checkParameter_remove( xFrame ), "OFrames::remove()\nInvalid parameter detected!\n" )
103 :
104 : // Do the follow only, if owner instance valid!
105 : // Lock owner for follow operations - make a "hard reference"!
106 2198 : css::uno::Reference< XFramesSupplier > xOwner( m_xOwner.get(), UNO_QUERY );
107 1099 : if ( xOwner.is() == sal_True )
108 : {
109 : // Search frame and remove it from container ...
110 1099 : m_pFrameContainer->remove( xFrame );
111 : // Don't reset owner-property of removed frame!
112 : // This must do the caller of this method himself.
113 : // See documentation of interface XFrames for further information.
114 1099 : }
115 : // Else; Do nothing! Ouer owner is dead.
116 : LOG_ASSERT( !(xOwner.is()==sal_False), "OFrames::remove()\nOuer owner is dead - you can't remove any frames ...!\n" )
117 1099 : }
118 :
119 : //*****************************************************************************************************************
120 : // XFrames
121 : //*****************************************************************************************************************
122 174 : Sequence< css::uno::Reference< XFrame > > SAL_CALL OFrames::queryFrames( sal_Int32 nSearchFlags ) throw( RuntimeException )
123 : {
124 : // Ready for multithreading
125 174 : ResetableGuard aGuard( m_aLock );
126 :
127 : // Safe impossible cases
128 : // Method is not defined for ALL incoming parameters!
129 : LOG_ASSERT( impldbg_checkParameter_queryFrames( nSearchFlags ), "OFrames::queryFrames()\nInvalid parameter detected!\n" )
130 :
131 : // Set default return value. (empty sequence)
132 174 : Sequence< css::uno::Reference< XFrame > > seqFrames;
133 :
134 : // Do the follow only, if owner instance valid.
135 : // Lock owner for follow operations - make a "hard reference"!
136 348 : css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
137 174 : if ( xOwner.is() == sal_True )
138 : {
139 : // Work only, if search was not started here ...!
140 174 : if( m_bRecursiveSearchProtection == sal_False )
141 : {
142 : // This class is a helper for services, which must implement XFrames.
143 : // His parent and children are MY parent and children to.
144 : // All searchflags are supported by this implementation!
145 : // If some flags should not be supported - don't call me with this flags!!!
146 :
147 : //_____________________________________________________________________________________________________________
148 : // Search with AUTO-flag is not supported yet!
149 : // We think about right implementation.
150 : LOG_ASSERT( !(nSearchFlags & FrameSearchFlag::AUTO), "OFrames::queryFrames()\nSearch with AUTO-flag is not supported yet!\nWe think about right implementation.\n" )
151 : // If searched for tasks ...
152 : // Its not supported yet.
153 : LOG_ASSERT( !(nSearchFlags & FrameSearchFlag::AUTO), "OFrames::queryFrames()\nSearch for tasks not supported yet!\n" )
154 :
155 : //_____________________________________________________________________________________________________________
156 : // Search for ALL and GLOBAL is superflous!
157 : // We support all necessary flags, from which these two flags are derived.
158 : // ALL = PARENT + SELF + CHILDREN + SIBLINGS
159 : // GLOBAL = ALL + TASKS
160 :
161 : //_____________________________________________________________________________________________________________
162 : // Add parent to list ... if any exist!
163 174 : if( nSearchFlags & FrameSearchFlag::PARENT )
164 : {
165 0 : css::uno::Reference< XFrame > xParent( xOwner->getCreator(), UNO_QUERY );
166 0 : if( xParent.is() == sal_True )
167 : {
168 0 : Sequence< css::uno::Reference< XFrame > > seqParent( 1 );
169 0 : seqParent[0] = xParent;
170 0 : impl_appendSequence( seqFrames, seqParent );
171 0 : }
172 : }
173 :
174 : //_____________________________________________________________________________________________________________
175 : // Add owner to list if SELF is searched.
176 174 : if( nSearchFlags & FrameSearchFlag::SELF )
177 : {
178 89 : Sequence< css::uno::Reference< XFrame > > seqSelf( 1 );
179 89 : seqSelf[0] = xOwner;
180 89 : impl_appendSequence( seqFrames, seqSelf );
181 : }
182 :
183 : //_____________________________________________________________________________________________________________
184 : // Add SIBLINGS to list.
185 174 : if( nSearchFlags & FrameSearchFlag::SIBLINGS )
186 : {
187 : // Else; start a new search.
188 : // Protect this instance against recursive calls from parents.
189 0 : m_bRecursiveSearchProtection = sal_True;
190 : // Ask parent of my owner for frames and append results to return list.
191 0 : css::uno::Reference< XFramesSupplier > xParent( xOwner->getCreator(), UNO_QUERY );
192 : // If a parent exist ...
193 0 : if ( xParent.is() == sal_True )
194 : {
195 : // ... ask him for right frames.
196 0 : impl_appendSequence( seqFrames, xParent->getFrames()->queryFrames( nSearchFlags ) );
197 : }
198 : // We have all searched information.
199 : // Reset protection-mode.
200 0 : m_bRecursiveSearchProtection = sal_False;
201 : }
202 :
203 : //_____________________________________________________________________________________________________________
204 : // If searched for children, step over all elements in container and collect the information.
205 174 : if ( nSearchFlags & FrameSearchFlag::CHILDREN )
206 : {
207 : // Don't search for parents, siblings and self at children!
208 : // These things are supported by this instance himself.
209 174 : sal_Int32 nChildSearchFlags = FrameSearchFlag::SELF | FrameSearchFlag::CHILDREN;
210 : // Step over all items of container and ask children for frames.
211 174 : sal_uInt32 nCount = m_pFrameContainer->getCount();
212 263 : for ( sal_uInt32 nIndex=0; nIndex<nCount; ++nIndex )
213 : {
214 : // We don't must control this conversion.
215 : // We have done this at append()!
216 89 : css::uno::Reference< XFramesSupplier > xItem( (*m_pFrameContainer)[nIndex], UNO_QUERY );
217 89 : impl_appendSequence( seqFrames, xItem->getFrames()->queryFrames( nChildSearchFlags ) );
218 89 : }
219 : }
220 : }
221 : }
222 : // Else; Do nothing! Ouer owner is dead.
223 : LOG_ASSERT( !(xOwner.is()==sal_False), "OFrames::queryFrames()\nOuer owner is dead - you can't query for frames ...!\n" )
224 :
225 : // Resturn result of this operation.
226 348 : return seqFrames;
227 : }
228 :
229 : //*****************************************************************************************************************
230 : // XIndexAccess
231 : //*****************************************************************************************************************
232 7456 : sal_Int32 SAL_CALL OFrames::getCount() throw( RuntimeException )
233 : {
234 : // Ready for multithreading
235 7456 : ResetableGuard aGuard( m_aLock );
236 :
237 : // Set default return value.
238 7456 : sal_Int32 nCount = 0;
239 :
240 : // Do the follow only, if owner instance valid.
241 : // Lock owner for follow operations - make a "hard reference"!
242 14912 : css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
243 7456 : if ( xOwner.is() == sal_True )
244 : {
245 : // Set CURRENT size of container for return.
246 7456 : nCount = m_pFrameContainer->getCount();
247 : }
248 :
249 : // Return result.
250 14912 : return nCount;
251 : }
252 :
253 : //*****************************************************************************************************************
254 : // XIndexAccess
255 : //*****************************************************************************************************************
256 5978 : Any SAL_CALL OFrames::getByIndex( sal_Int32 nIndex ) throw( IndexOutOfBoundsException ,
257 : WrappedTargetException ,
258 : RuntimeException )
259 : {
260 : // Ready for multithreading
261 5978 : ResetableGuard aGuard( m_aLock );
262 :
263 5978 : sal_uInt32 nCount = m_pFrameContainer->getCount();
264 5978 : if ( nIndex < 0 || ( sal::static_int_cast< sal_uInt32 >( nIndex ) >= nCount ))
265 : throw IndexOutOfBoundsException("OFrames::getByIndex - Index out of bounds",
266 0 : (OWeakObject *)this );
267 :
268 : // Set default return value.
269 5978 : Any aReturnValue;
270 :
271 : // Do the follow only, if owner instance valid.
272 : // Lock owner for follow operations - make a "hard reference"!
273 11956 : css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
274 5978 : if ( xOwner.is() == sal_True )
275 : {
276 : // Get element form container.
277 : // (If index not valid, FrameContainer return NULL!)
278 5978 : aReturnValue <<= (*m_pFrameContainer)[nIndex];
279 : }
280 :
281 : // Return result of this operation.
282 11956 : return aReturnValue;
283 : }
284 :
285 : //*****************************************************************************************************************
286 : // XElementAccess
287 : //*****************************************************************************************************************
288 0 : Type SAL_CALL OFrames::getElementType() throw( RuntimeException )
289 : {
290 : // This "container" support XFrame-interfaces only!
291 0 : return ::getCppuType( (const css::uno::Reference< XFrame >*)NULL );
292 : }
293 :
294 : //*****************************************************************************************************************
295 : // XElementAccess
296 : //*****************************************************************************************************************
297 51 : sal_Bool SAL_CALL OFrames::hasElements() throw( RuntimeException )
298 : {
299 : // Ready for multithreading
300 51 : ResetableGuard aGuard( m_aLock );
301 :
302 : // Set default return value.
303 51 : sal_Bool bHasElements = sal_False;
304 : // Do the follow only, if owner instance valid.
305 : // Lock owner for follow operations - make a "hard reference"!
306 102 : css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
307 51 : if ( xOwner.is() == sal_True )
308 : {
309 : // If some elements exist ...
310 51 : if ( m_pFrameContainer->getCount() > 0 )
311 : {
312 : // ... change this state value!
313 0 : bHasElements = sal_True;
314 : }
315 : }
316 : // Return result of this operation.
317 102 : return bHasElements;
318 : }
319 :
320 : //*****************************************************************************************************************
321 : // proteced method
322 : //*****************************************************************************************************************
323 1233 : void OFrames::impl_resetObject()
324 : {
325 : // Attention:
326 : // Write this for multiple calls - NOT AT THE SAME TIME - but for more then one call again)!
327 : // It exist two ways to call this method. From destructor and from disposing().
328 : // I can't say, which one is the first. Normaly the disposing-call - but other way ....
329 :
330 : // This instance can't work if the weakreference to owner is invalid!
331 : // Destroy this to reset this object.
332 1233 : m_xOwner = WeakReference< XFrame >();
333 : // Reset pointer to shared container to!
334 1233 : m_pFrameContainer = NULL;
335 1233 : }
336 :
337 : //*****************************************************************************************************************
338 : // private method
339 : //*****************************************************************************************************************
340 178 : void OFrames::impl_appendSequence( Sequence< css::uno::Reference< XFrame > >& seqDestination ,
341 : const Sequence< css::uno::Reference< XFrame > >& seqSource )
342 : {
343 : // Get some information about the sequences.
344 178 : sal_Int32 nSourceCount = seqSource.getLength();
345 178 : sal_Int32 nDestinationCount = seqDestination.getLength();
346 178 : const css::uno::Reference< XFrame >* pSourceAccess = seqSource.getConstArray();
347 178 : css::uno::Reference< XFrame >* pDestinationAccess = seqDestination.getArray();
348 :
349 : // Get memory for result list.
350 178 : Sequence< css::uno::Reference< XFrame > > seqResult ( nSourceCount + nDestinationCount );
351 178 : css::uno::Reference< XFrame >* pResultAccess = seqResult.getArray();
352 178 : sal_Int32 nResultPosition = 0;
353 :
354 : // Copy all items from first sequence.
355 356 : for ( sal_Int32 nSourcePosition=0; nSourcePosition<nSourceCount; ++nSourcePosition )
356 : {
357 178 : pResultAccess[nResultPosition] = pSourceAccess[nSourcePosition];
358 178 : ++nResultPosition;
359 : }
360 :
361 : // Don't manipulate nResultPosition between these two loops!
362 : // Its the current position in the result list.
363 :
364 : // Copy all items from second sequence.
365 182 : for ( sal_Int32 nDestinationPosition=0; nDestinationPosition<nDestinationCount; ++nDestinationPosition )
366 : {
367 4 : pResultAccess[nResultPosition] = pDestinationAccess[nDestinationPosition];
368 4 : ++nResultPosition;
369 : }
370 :
371 : // Return result of this operation.
372 178 : seqDestination.realloc( 0 );
373 178 : seqDestination = seqResult;
374 178 : }
375 :
376 : //_________________________________________________________________________________________________________________
377 : // debug methods
378 : //_________________________________________________________________________________________________________________
379 :
380 : /*-----------------------------------------------------------------------------------------------------------------
381 : The follow methods checks the parameter for other functions. If a parameter or his value is non valid,
382 : we return "sal_False". (else sal_True) This mechanism is used to throw an ASSERT!
383 :
384 : ATTENTION
385 :
386 : If you miss a test for one of this parameters, contact the autor or add it himself !(?)
387 : But ... look for right testing! See using of this methods!
388 : -----------------------------------------------------------------------------------------------------------------*/
389 :
390 : #ifdef ENABLE_ASSERTIONS
391 :
392 : //*****************************************************************************************************************
393 : // An instance of this class can only work with valid initialization.
394 : // We share the mutex with ouer owner class, need a valid factory to instanciate new services and
395 : // use the access to ouer owner for some operations.
396 : sal_Bool OFrames::impldbg_checkParameter_OFramesCtor( const css::uno::Reference< XFrame >& xOwner ,
397 : FrameContainer* pFrameContainer )
398 : {
399 : // Set default return value.
400 : sal_Bool bOK = sal_True;
401 : // Check parameter.
402 : if (
403 : ( &xOwner == NULL ) ||
404 : ( xOwner.is() == sal_False ) ||
405 : ( pFrameContainer == NULL )
406 : )
407 : {
408 : bOK = sal_False ;
409 : }
410 : // Return result of check.
411 : return bOK ;
412 : }
413 :
414 : //*****************************************************************************************************************
415 : // Its only allowed to add valid references to container.
416 : // AND - alle frames must support XFrames-interface!
417 : sal_Bool OFrames::impldbg_checkParameter_append( const css::uno::Reference< XFrame >& xFrame )
418 : {
419 : // Set default return value.
420 : sal_Bool bOK = sal_True;
421 : // Check parameter.
422 : if (
423 : ( &xFrame == NULL ) ||
424 : ( xFrame.is() == sal_False )
425 : )
426 : {
427 : bOK = sal_False ;
428 : }
429 : // Return result of check.
430 : return bOK ;
431 : }
432 :
433 : //*****************************************************************************************************************
434 : // Its only allowed to add valid references to container...
435 : // ... => You can only delete valid references!
436 : sal_Bool OFrames::impldbg_checkParameter_remove( const css::uno::Reference< XFrame >& xFrame )
437 : {
438 : // Set default return value.
439 : sal_Bool bOK = sal_True;
440 : // Check parameter.
441 : if (
442 : ( &xFrame == NULL ) ||
443 : ( xFrame.is() == sal_False )
444 : )
445 : {
446 : bOK = sal_False ;
447 : }
448 : // Return result of check.
449 : return bOK ;
450 : }
451 :
452 : //*****************************************************************************************************************
453 : // A search for frames must initiate with right flags.
454 : // Some one are superflous and not supported yet. But here we control only the range of incoming parameter!
455 : sal_Bool OFrames::impldbg_checkParameter_queryFrames( sal_Int32 nSearchFlags )
456 : {
457 : // Set default return value.
458 : sal_Bool bOK = sal_True;
459 : // Check parameter.
460 : if (
461 : ( nSearchFlags != FrameSearchFlag::AUTO ) &&
462 : ( !( nSearchFlags & FrameSearchFlag::PARENT ) ) &&
463 : ( !( nSearchFlags & FrameSearchFlag::SELF ) ) &&
464 : ( !( nSearchFlags & FrameSearchFlag::CHILDREN ) ) &&
465 : ( !( nSearchFlags & FrameSearchFlag::CREATE ) ) &&
466 : ( !( nSearchFlags & FrameSearchFlag::SIBLINGS ) ) &&
467 : ( !( nSearchFlags & FrameSearchFlag::TASKS ) ) &&
468 : ( !( nSearchFlags & FrameSearchFlag::ALL ) ) &&
469 : ( !( nSearchFlags & FrameSearchFlag::GLOBAL ) )
470 : )
471 : {
472 : bOK = sal_False ;
473 : }
474 : // Return result of check.
475 : return bOK ;
476 : }
477 :
478 : #endif // #ifdef ENABLE_ASSERTIONS
479 :
480 402 : } // namespace framework
481 :
482 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|