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 <com/sun/star/frame/XDesktop.hpp>
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 ::std;
36 :
37 : // constructor
38 :
39 3494 : OFrames::OFrames( const css::uno::Reference< XFrame >& xOwner ,
40 : FrameContainer* pFrameContainer )
41 : : m_xOwner ( xOwner )
42 : , m_pFrameContainer ( pFrameContainer )
43 3494 : , m_bRecursiveSearchProtection( false )
44 : {
45 : // Safe impossible cases
46 : // Method is not defined for ALL incoming parameters!
47 : SAL_WARN_IF( !impldbg_checkParameter_OFramesCtor( xOwner, pFrameContainer ), "fwk", "OFrames::OFrames(): Invalid parameter detected!" );
48 3494 : }
49 :
50 : // (proteced!) destructor
51 :
52 10449 : OFrames::~OFrames()
53 : {
54 : // Reset instance, free memory ....
55 3483 : impl_resetObject();
56 6966 : }
57 :
58 : // XFrames
59 3282 : void SAL_CALL OFrames::append( const css::uno::Reference< XFrame >& xFrame ) throw( RuntimeException, std::exception )
60 : {
61 3282 : SolarMutexGuard g;
62 :
63 : // Safe impossible cases
64 : // Method is not defined for ALL incoming parameters!
65 : SAL_WARN_IF( !impldbg_checkParameter_append( xFrame ), "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 6564 : css::uno::Reference< XFramesSupplier > xOwner( m_xOwner.get(), UNO_QUERY );
70 3282 : if ( xOwner.is() )
71 : {
72 : // Append frame to the end of the container ...
73 3282 : m_pFrameContainer->append( xFrame );
74 : // Set owner of this instance as parent of the new frame in container!
75 3282 : xFrame->setCreator( xOwner );
76 : }
77 : // Else; Do nothing! Our owner is dead.
78 3282 : SAL_WARN_IF( !xOwner.is(), "fwk", "OFrames::append():Our owner is dead - you can't append any frames ...!" );
79 3282 : }
80 :
81 : // XFrames
82 3273 : void SAL_CALL OFrames::remove( const css::uno::Reference< XFrame >& xFrame ) throw( RuntimeException, std::exception )
83 : {
84 3273 : SolarMutexGuard g;
85 :
86 : // Safe impossible cases
87 : // Method is not defined for ALL incoming parameters!
88 : SAL_WARN_IF( !impldbg_checkParameter_remove( xFrame ), "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 6546 : css::uno::Reference< XFramesSupplier > xOwner( m_xOwner.get(), UNO_QUERY );
93 3273 : if ( xOwner.is() )
94 : {
95 : // Search frame and remove it from container ...
96 3273 : m_pFrameContainer->remove( xFrame );
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 3273 : SAL_WARN_IF( !xOwner.is(), "fwk", "OFrames::remove(): Our owner is dead - you can't remove any frames ...!" );
103 3273 : }
104 :
105 : // XFrames
106 182 : Sequence< css::uno::Reference< XFrame > > SAL_CALL OFrames::queryFrames( sal_Int32 nSearchFlags ) throw( RuntimeException, std::exception )
107 : {
108 182 : SolarMutexGuard g;
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 182 : 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 364 : css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
120 182 : if ( xOwner.is() )
121 : {
122 : // Work only, if search was not started here ...!
123 182 : if( !m_bRecursiveSearchProtection )
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 superflous!
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 182 : if( nSearchFlags & FrameSearchFlag::PARENT )
141 : {
142 0 : css::uno::Reference< XFrame > xParent( xOwner->getCreator(), UNO_QUERY );
143 0 : if( xParent.is() )
144 : {
145 0 : Sequence< css::uno::Reference< XFrame > > seqParent( 1 );
146 0 : seqParent[0] = xParent;
147 0 : impl_appendSequence( seqFrames, seqParent );
148 0 : }
149 : }
150 :
151 : // Add owner to list if SELF is searched.
152 182 : if( nSearchFlags & FrameSearchFlag::SELF )
153 : {
154 93 : Sequence< css::uno::Reference< XFrame > > seqSelf( 1 );
155 93 : seqSelf[0] = xOwner;
156 93 : impl_appendSequence( seqFrames, seqSelf );
157 : }
158 :
159 : // Add SIBLINGS to list.
160 182 : if( nSearchFlags & FrameSearchFlag::SIBLINGS )
161 : {
162 : // Else; start a new search.
163 : // Protect this instance against recursive calls from parents.
164 0 : m_bRecursiveSearchProtection = true;
165 : // Ask parent of my owner for frames and append results to return list.
166 0 : css::uno::Reference< XFramesSupplier > xParent( xOwner->getCreator(), UNO_QUERY );
167 : // If a parent exist ...
168 0 : if ( xParent.is() )
169 : {
170 : // ... ask him for right frames.
171 0 : impl_appendSequence( seqFrames, xParent->getFrames()->queryFrames( nSearchFlags ) );
172 : }
173 : // We have all searched information.
174 : // Reset protection-mode.
175 0 : m_bRecursiveSearchProtection = false;
176 : }
177 :
178 : // If searched for children, step over all elements in container and collect the information.
179 182 : if ( nSearchFlags & FrameSearchFlag::CHILDREN )
180 : {
181 : // Don't search for parents, siblings and self at children!
182 : // These things are supported by this instance himself.
183 182 : sal_Int32 nChildSearchFlags = FrameSearchFlag::SELF | FrameSearchFlag::CHILDREN;
184 : // Step over all items of container and ask children for frames.
185 182 : sal_uInt32 nCount = m_pFrameContainer->getCount();
186 275 : for ( sal_uInt32 nIndex=0; nIndex<nCount; ++nIndex )
187 : {
188 : // We don't must control this conversion.
189 : // We have done this at append()!
190 93 : css::uno::Reference< XFramesSupplier > xItem( (*m_pFrameContainer)[nIndex], UNO_QUERY );
191 93 : impl_appendSequence( seqFrames, xItem->getFrames()->queryFrames( nChildSearchFlags ) );
192 93 : }
193 : }
194 : }
195 : }
196 : // Else; Do nothing! Our owner is dead.
197 : SAL_WARN_IF( !xOwner.is(), "fwk", "OFrames::queryFrames(): Our owner is dead - you can't query for frames ...!" );
198 :
199 : // Resturn result of this operation.
200 364 : return seqFrames;
201 : }
202 :
203 : // XIndexAccess
204 24340 : sal_Int32 SAL_CALL OFrames::getCount() throw( RuntimeException, std::exception )
205 : {
206 24340 : SolarMutexGuard g;
207 :
208 : // Set default return value.
209 24340 : sal_Int32 nCount = 0;
210 :
211 : // Do the follow only, if owner instance valid.
212 : // Lock owner for follow operations - make a "hard reference"!
213 48680 : css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
214 24340 : if ( xOwner.is() )
215 : {
216 : // Set CURRENT size of container for return.
217 24340 : nCount = m_pFrameContainer->getCount();
218 : }
219 :
220 : // Return result.
221 48680 : return nCount;
222 : }
223 :
224 : // XIndexAccess
225 :
226 19336 : Any SAL_CALL OFrames::getByIndex( sal_Int32 nIndex ) throw( IndexOutOfBoundsException ,
227 : WrappedTargetException ,
228 : RuntimeException, std::exception )
229 : {
230 19336 : SolarMutexGuard g;
231 :
232 19336 : sal_uInt32 nCount = m_pFrameContainer->getCount();
233 19336 : if ( nIndex < 0 || ( sal::static_int_cast< sal_uInt32 >( nIndex ) >= nCount ))
234 : throw IndexOutOfBoundsException("OFrames::getByIndex - Index out of bounds",
235 0 : static_cast<OWeakObject *>(this) );
236 :
237 : // Set default return value.
238 19336 : Any aReturnValue;
239 :
240 : // Do the follow only, if owner instance valid.
241 : // Lock owner for follow operations - make a "hard reference"!
242 38672 : css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
243 19336 : if ( xOwner.is() )
244 : {
245 : // Get element form container.
246 : // (If index not valid, FrameContainer return NULL!)
247 19336 : aReturnValue <<= (*m_pFrameContainer)[nIndex];
248 : }
249 :
250 : // Return result of this operation.
251 38672 : return aReturnValue;
252 : }
253 :
254 : // XElementAccess
255 0 : Type SAL_CALL OFrames::getElementType() throw( RuntimeException, std::exception )
256 : {
257 : // This "container" support XFrame-interfaces only!
258 0 : return cppu::UnoType<XFrame>::get();
259 : }
260 :
261 : // XElementAccess
262 65 : sal_Bool SAL_CALL OFrames::hasElements() throw( RuntimeException, std::exception )
263 : {
264 65 : SolarMutexGuard g;
265 :
266 : // Set default return value.
267 65 : bool bHasElements = false;
268 : // Do the follow only, if owner instance valid.
269 : // Lock owner for follow operations - make a "hard reference"!
270 130 : css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
271 65 : if ( xOwner.is() )
272 : {
273 : // If some elements exist ...
274 65 : if ( m_pFrameContainer->getCount() > 0 )
275 : {
276 : // ... change this state value!
277 2 : bHasElements = true;
278 : }
279 : }
280 : // Return result of this operation.
281 130 : return bHasElements;
282 : }
283 :
284 : // proteced method
285 :
286 3483 : void OFrames::impl_resetObject()
287 : {
288 : // Attention:
289 : // Write this for multiple calls - NOT AT THE SAME TIME - but for more than one call again)!
290 : // It exist two ways to call this method. From destructor and from disposing().
291 : // I can't say, which one is the first. Normally the disposing-call - but other way ....
292 :
293 : // This instance can't work if the weakreference to owner is invalid!
294 : // Destroy this to reset this object.
295 3483 : m_xOwner = WeakReference< XFrame >();
296 : // Reset pointer to shared container to!
297 3483 : m_pFrameContainer = NULL;
298 3483 : }
299 :
300 186 : void OFrames::impl_appendSequence( Sequence< css::uno::Reference< XFrame > >& seqDestination ,
301 : const Sequence< css::uno::Reference< XFrame > >& seqSource )
302 : {
303 : // Get some information about the sequences.
304 186 : sal_Int32 nSourceCount = seqSource.getLength();
305 186 : sal_Int32 nDestinationCount = seqDestination.getLength();
306 186 : const css::uno::Reference< XFrame >* pSourceAccess = seqSource.getConstArray();
307 186 : css::uno::Reference< XFrame >* pDestinationAccess = seqDestination.getArray();
308 :
309 : // Get memory for result list.
310 186 : Sequence< css::uno::Reference< XFrame > > seqResult ( nSourceCount + nDestinationCount );
311 186 : css::uno::Reference< XFrame >* pResultAccess = seqResult.getArray();
312 186 : sal_Int32 nResultPosition = 0;
313 :
314 : // Copy all items from first sequence.
315 372 : for ( sal_Int32 nSourcePosition=0; nSourcePosition<nSourceCount; ++nSourcePosition )
316 : {
317 186 : pResultAccess[nResultPosition] = pSourceAccess[nSourcePosition];
318 186 : ++nResultPosition;
319 : }
320 :
321 : // Don't manipulate nResultPosition between these two loops!
322 : // Its the current position in the result list.
323 :
324 : // Copy all items from second sequence.
325 190 : for ( sal_Int32 nDestinationPosition=0; nDestinationPosition<nDestinationCount; ++nDestinationPosition )
326 : {
327 4 : pResultAccess[nResultPosition] = pDestinationAccess[nDestinationPosition];
328 4 : ++nResultPosition;
329 : }
330 :
331 : // Return result of this operation.
332 186 : seqDestination.realloc( 0 );
333 186 : seqDestination = seqResult;
334 186 : }
335 :
336 : // debug methods
337 :
338 : /*-----------------------------------------------------------------------------------------------------------------
339 : The follow methods checks the parameter for other functions. If a parameter or his value is non valid,
340 : we return "sal_False". (else sal_True) This mechanism is used to throw an ASSERT!
341 :
342 : ATTENTION
343 :
344 : If you miss a test for one of this parameters, contact the author or add it himself !(?)
345 : But ... look for right testing! See using of this methods!
346 : -----------------------------------------------------------------------------------------------------------------*/
347 :
348 : // An instance of this class can only work with valid initialization.
349 : // We share the mutex with our owner class, need a valid factory to instanciate new services and
350 : // use the access to our owner for some operations.
351 0 : bool OFrames::impldbg_checkParameter_OFramesCtor( const css::uno::Reference< XFrame >& xOwner ,
352 : FrameContainer* pFrameContainer )
353 : {
354 0 : return xOwner.is() && pFrameContainer != 0;
355 : }
356 :
357 : // Its only allowed to add valid references to container.
358 : // AND - alle frames must support XFrames-interface!
359 0 : bool OFrames::impldbg_checkParameter_append( const css::uno::Reference< XFrame >& xFrame )
360 : {
361 0 : return xFrame.is();
362 : }
363 :
364 : // Its only allowed to add valid references to container...
365 : // ... => You can only delete valid references!
366 0 : bool OFrames::impldbg_checkParameter_remove( const css::uno::Reference< XFrame >& xFrame )
367 : {
368 0 : return xFrame.is();
369 : }
370 :
371 : // A search for frames must initiate with right flags.
372 : // Some one are superflous and not supported yet. But here we control only the range of incoming parameter!
373 0 : bool OFrames::impldbg_checkParameter_queryFrames( sal_Int32 nSearchFlags )
374 : {
375 : // Set default return value.
376 0 : bool bOK = true;
377 : // Check parameter.
378 0 : if (
379 0 : ( nSearchFlags != FrameSearchFlag::AUTO ) &&
380 0 : ( !( nSearchFlags & FrameSearchFlag::PARENT ) ) &&
381 0 : ( !( nSearchFlags & FrameSearchFlag::SELF ) ) &&
382 0 : ( !( nSearchFlags & FrameSearchFlag::CHILDREN ) ) &&
383 0 : ( !( nSearchFlags & FrameSearchFlag::CREATE ) ) &&
384 0 : ( !( nSearchFlags & FrameSearchFlag::SIBLINGS ) ) &&
385 0 : ( !( nSearchFlags & FrameSearchFlag::TASKS ) ) &&
386 0 : ( !( nSearchFlags & FrameSearchFlag::ALL ) ) &&
387 0 : ( !( nSearchFlags & FrameSearchFlag::GLOBAL ) )
388 : )
389 : {
390 0 : bOK = false;
391 : }
392 : // Return result of check.
393 0 : return bOK;
394 : }
395 :
396 : } // namespace framework
397 :
398 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|