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 0 : OFrames::OFrames( const css::uno::Reference< XFrame >& xOwner ,
40 : FrameContainer* pFrameContainer )
41 : : m_xOwner ( xOwner )
42 : , m_pFrameContainer ( pFrameContainer )
43 0 : , 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 0 : }
49 :
50 : // (proteced!) destructor
51 :
52 0 : OFrames::~OFrames()
53 : {
54 : // Reset instance, free memory ....
55 0 : impl_resetObject();
56 0 : }
57 :
58 : // XFrames
59 0 : void SAL_CALL OFrames::append( const css::uno::Reference< XFrame >& xFrame ) throw( RuntimeException, std::exception )
60 : {
61 0 : 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 0 : css::uno::Reference< XFramesSupplier > xOwner( m_xOwner.get(), UNO_QUERY );
70 0 : if ( xOwner.is() )
71 : {
72 : // Append frame to the end of the container ...
73 0 : m_pFrameContainer->append( xFrame );
74 : // Set owner of this instance as parent of the new frame in container!
75 0 : xFrame->setCreator( xOwner );
76 : }
77 : // Else; Do nothing! Our owner is dead.
78 0 : SAL_WARN_IF( !xOwner.is(), "fwk", "OFrames::append():Our owner is dead - you can't append any frames ...!" );
79 0 : }
80 :
81 : // XFrames
82 0 : void SAL_CALL OFrames::remove( const css::uno::Reference< XFrame >& xFrame ) throw( RuntimeException, std::exception )
83 : {
84 0 : 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 0 : css::uno::Reference< XFramesSupplier > xOwner( m_xOwner.get(), UNO_QUERY );
93 0 : if ( xOwner.is() )
94 : {
95 : // Search frame and remove it from container ...
96 0 : 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 0 : SAL_WARN_IF( !xOwner.is(), "fwk", "OFrames::remove(): Our owner is dead - you can't remove any frames ...!" );
103 0 : }
104 :
105 : // XFrames
106 0 : Sequence< css::uno::Reference< XFrame > > SAL_CALL OFrames::queryFrames( sal_Int32 nSearchFlags ) throw( RuntimeException, std::exception )
107 : {
108 0 : 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 0 : 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 0 : css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
120 0 : if ( xOwner.is() )
121 : {
122 : // Work only, if search was not started here ...!
123 0 : if( m_bRecursiveSearchProtection == false )
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 0 : 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 0 : if( nSearchFlags & FrameSearchFlag::SELF )
153 : {
154 0 : Sequence< css::uno::Reference< XFrame > > seqSelf( 1 );
155 0 : seqSelf[0] = xOwner;
156 0 : impl_appendSequence( seqFrames, seqSelf );
157 : }
158 :
159 : // Add SIBLINGS to list.
160 0 : 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 0 : 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 0 : sal_Int32 nChildSearchFlags = FrameSearchFlag::SELF | FrameSearchFlag::CHILDREN;
184 : // Step over all items of container and ask children for frames.
185 0 : sal_uInt32 nCount = m_pFrameContainer->getCount();
186 0 : 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 0 : css::uno::Reference< XFramesSupplier > xItem( (*m_pFrameContainer)[nIndex], UNO_QUERY );
191 0 : impl_appendSequence( seqFrames, xItem->getFrames()->queryFrames( nChildSearchFlags ) );
192 0 : }
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 0 : return seqFrames;
201 : }
202 :
203 : // XIndexAccess
204 0 : sal_Int32 SAL_CALL OFrames::getCount() throw( RuntimeException, std::exception )
205 : {
206 0 : SolarMutexGuard g;
207 :
208 : // Set default return value.
209 0 : 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 0 : css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
214 0 : if ( xOwner.is() )
215 : {
216 : // Set CURRENT size of container for return.
217 0 : nCount = m_pFrameContainer->getCount();
218 : }
219 :
220 : // Return result.
221 0 : return nCount;
222 : }
223 :
224 : // XIndexAccess
225 :
226 0 : Any SAL_CALL OFrames::getByIndex( sal_Int32 nIndex ) throw( IndexOutOfBoundsException ,
227 : WrappedTargetException ,
228 : RuntimeException, std::exception )
229 : {
230 0 : SolarMutexGuard g;
231 :
232 0 : sal_uInt32 nCount = m_pFrameContainer->getCount();
233 0 : if ( nIndex < 0 || ( sal::static_int_cast< sal_uInt32 >( nIndex ) >= nCount ))
234 : throw IndexOutOfBoundsException("OFrames::getByIndex - Index out of bounds",
235 0 : (OWeakObject *)this );
236 :
237 : // Set default return value.
238 0 : Any aReturnValue;
239 :
240 : // Do the follow only, if owner instance valid.
241 : // Lock owner for follow operations - make a "hard reference"!
242 0 : css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
243 0 : if ( xOwner.is() )
244 : {
245 : // Get element form container.
246 : // (If index not valid, FrameContainer return NULL!)
247 0 : aReturnValue <<= (*m_pFrameContainer)[nIndex];
248 : }
249 :
250 : // Return result of this operation.
251 0 : 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 ::getCppuType( (const css::uno::Reference< XFrame >*)NULL );
259 : }
260 :
261 : // XElementAccess
262 0 : sal_Bool SAL_CALL OFrames::hasElements() throw( RuntimeException, std::exception )
263 : {
264 0 : SolarMutexGuard g;
265 :
266 : // Set default return value.
267 0 : bool bHasElements = false;
268 : // Do the follow only, if owner instance valid.
269 : // Lock owner for follow operations - make a "hard reference"!
270 0 : css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
271 0 : if ( xOwner.is() )
272 : {
273 : // If some elements exist ...
274 0 : if ( m_pFrameContainer->getCount() > 0 )
275 : {
276 : // ... change this state value!
277 0 : bHasElements = true;
278 : }
279 : }
280 : // Return result of this operation.
281 0 : return bHasElements;
282 : }
283 :
284 : // proteced method
285 :
286 0 : void OFrames::impl_resetObject()
287 : {
288 : // Attention:
289 : // Write this for multiple calls - NOT AT THE SAME TIME - but for more then 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. Normaly 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 0 : m_xOwner = WeakReference< XFrame >();
296 : // Reset pointer to shared container to!
297 0 : m_pFrameContainer = NULL;
298 0 : }
299 :
300 : // private method
301 :
302 0 : void OFrames::impl_appendSequence( Sequence< css::uno::Reference< XFrame > >& seqDestination ,
303 : const Sequence< css::uno::Reference< XFrame > >& seqSource )
304 : {
305 : // Get some information about the sequences.
306 0 : sal_Int32 nSourceCount = seqSource.getLength();
307 0 : sal_Int32 nDestinationCount = seqDestination.getLength();
308 0 : const css::uno::Reference< XFrame >* pSourceAccess = seqSource.getConstArray();
309 0 : css::uno::Reference< XFrame >* pDestinationAccess = seqDestination.getArray();
310 :
311 : // Get memory for result list.
312 0 : Sequence< css::uno::Reference< XFrame > > seqResult ( nSourceCount + nDestinationCount );
313 0 : css::uno::Reference< XFrame >* pResultAccess = seqResult.getArray();
314 0 : sal_Int32 nResultPosition = 0;
315 :
316 : // Copy all items from first sequence.
317 0 : for ( sal_Int32 nSourcePosition=0; nSourcePosition<nSourceCount; ++nSourcePosition )
318 : {
319 0 : pResultAccess[nResultPosition] = pSourceAccess[nSourcePosition];
320 0 : ++nResultPosition;
321 : }
322 :
323 : // Don't manipulate nResultPosition between these two loops!
324 : // Its the current position in the result list.
325 :
326 : // Copy all items from second sequence.
327 0 : for ( sal_Int32 nDestinationPosition=0; nDestinationPosition<nDestinationCount; ++nDestinationPosition )
328 : {
329 0 : pResultAccess[nResultPosition] = pDestinationAccess[nDestinationPosition];
330 0 : ++nResultPosition;
331 : }
332 :
333 : // Return result of this operation.
334 0 : seqDestination.realloc( 0 );
335 0 : seqDestination = seqResult;
336 0 : }
337 :
338 : // debug methods
339 :
340 : /*-----------------------------------------------------------------------------------------------------------------
341 : The follow methods checks the parameter for other functions. If a parameter or his value is non valid,
342 : we return "sal_False". (else sal_True) This mechanism is used to throw an ASSERT!
343 :
344 : ATTENTION
345 :
346 : If you miss a test for one of this parameters, contact the autor or add it himself !(?)
347 : But ... look for right testing! See using of this methods!
348 : -----------------------------------------------------------------------------------------------------------------*/
349 :
350 : // An instance of this class can only work with valid initialization.
351 : // We share the mutex with our owner class, need a valid factory to instanciate new services and
352 : // use the access to our owner for some operations.
353 0 : bool OFrames::impldbg_checkParameter_OFramesCtor( const css::uno::Reference< XFrame >& xOwner ,
354 : FrameContainer* pFrameContainer )
355 : {
356 0 : return xOwner.is() && pFrameContainer != 0;
357 : }
358 :
359 : // Its only allowed to add valid references to container.
360 : // AND - alle frames must support XFrames-interface!
361 0 : bool OFrames::impldbg_checkParameter_append( const css::uno::Reference< XFrame >& xFrame )
362 : {
363 0 : return xFrame.is();
364 : }
365 :
366 : // Its only allowed to add valid references to container...
367 : // ... => You can only delete valid references!
368 0 : bool OFrames::impldbg_checkParameter_remove( const css::uno::Reference< XFrame >& xFrame )
369 : {
370 0 : return xFrame.is();
371 : }
372 :
373 : // A search for frames must initiate with right flags.
374 : // Some one are superflous and not supported yet. But here we control only the range of incoming parameter!
375 0 : bool OFrames::impldbg_checkParameter_queryFrames( sal_Int32 nSearchFlags )
376 : {
377 : // Set default return value.
378 0 : bool bOK = true;
379 : // Check parameter.
380 0 : if (
381 0 : ( nSearchFlags != FrameSearchFlag::AUTO ) &&
382 0 : ( !( nSearchFlags & FrameSearchFlag::PARENT ) ) &&
383 0 : ( !( nSearchFlags & FrameSearchFlag::SELF ) ) &&
384 0 : ( !( nSearchFlags & FrameSearchFlag::CHILDREN ) ) &&
385 0 : ( !( nSearchFlags & FrameSearchFlag::CREATE ) ) &&
386 0 : ( !( nSearchFlags & FrameSearchFlag::SIBLINGS ) ) &&
387 0 : ( !( nSearchFlags & FrameSearchFlag::TASKS ) ) &&
388 0 : ( !( nSearchFlags & FrameSearchFlag::ALL ) ) &&
389 0 : ( !( nSearchFlags & FrameSearchFlag::GLOBAL ) )
390 : )
391 : {
392 0 : bOK = false;
393 : }
394 : // Return result of check.
395 0 : return bOK;
396 : }
397 :
398 : } // namespace framework
399 :
400 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|