1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_scripting.hxx"
26 
27 #include <cppuhelper/weakref.hxx>
28 #include <cppuhelper/implementationentry.hxx>
29 #include <cppuhelper/factory.hxx>
30 #include <cppuhelper/exc_hlp.hxx>
31 #include <cppuhelper/implbase1.hxx>
32 #include <comphelper/mediadescriptor.hxx>
33 
34 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
35 #include <com/sun/star/frame/XModel.hpp>
36 #include <com/sun/star/reflection/XProxyFactory.hpp>
37 
38 #include <com/sun/star/script/provider/XScriptProviderFactory.hpp>
39 #include <com/sun/star/script/browse/BrowseNodeFactoryViewTypes.hpp>
40 #include <com/sun/star/document/XScriptInvocationContext.hpp>
41 
42 #include <tools/diagnose_ex.h>
43 
44 #include "BrowseNodeFactoryImpl.hxx"
45 #include "ActiveMSPList.hxx"
46 #include <util/MiscUtils.hxx>
47 #include <util/util.hxx>
48 
49 #include <vector>
50 #include <algorithm>
51 using namespace ::com::sun::star;
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::script;
54 using namespace ::sf_misc;
55 
56 namespace browsenodefactory
57 {
58 class BrowseNodeAggregator :
59     public ::cppu::WeakImplHelper1< browse::XBrowseNode >
60 {
61 private:
62     ::rtl::OUString m_Name;
63     Sequence< Reference< browse::XBrowseNode > > m_Nodes;
64 
65 public:
66 
BrowseNodeAggregator(const Reference<browse::XBrowseNode> & node)67     BrowseNodeAggregator( const Reference< browse::XBrowseNode >& node )
68     {
69         m_Name = node->getName();
70         m_Nodes.realloc( 1 );
71         m_Nodes[ 0 ] = node;
72     }
73 
~BrowseNodeAggregator()74     ~BrowseNodeAggregator()
75     {
76     }
77 
addBrowseNode(const Reference<browse::XBrowseNode> & node)78     void addBrowseNode( const Reference< browse::XBrowseNode>& node )
79     {
80         sal_Int32 index = m_Nodes.getLength();
81 
82         m_Nodes.realloc( index + 1 );
83         m_Nodes[ index ] = node;
84     }
85 
86     virtual ::rtl::OUString
getName()87     SAL_CALL getName()
88             throw ( RuntimeException )
89     {
90         return m_Name;
91     }
92 
93     virtual Sequence< Reference< browse::XBrowseNode > > SAL_CALL
getChildNodes()94     getChildNodes()
95         throw ( RuntimeException )
96     {
97         std::vector<  Sequence< Reference < browse::XBrowseNode > > > seqs;
98         seqs.reserve( m_Nodes.getLength() );
99 
100         sal_Int32 numChildren = 0;
101 
102         for ( sal_Int32 i = 0; i < m_Nodes.getLength(); i++ )
103         {
104             Sequence< Reference < browse::XBrowseNode > > childs;
105             try
106             {
107                 childs = m_Nodes[ i ]->getChildNodes();
108                 seqs.push_back( childs );
109                 numChildren += childs.getLength();
110             }
111             catch ( Exception& )
112             {
113                 // some form of exception getting child nodes so they
114                 // won't be displayed
115             }
116         }
117 
118         std::vector<  Sequence< Reference < browse::XBrowseNode > > >::const_iterator it = seqs.begin();
119         std::vector<  Sequence< Reference < browse::XBrowseNode > > >::const_iterator it_end = seqs.end();
120 
121         Sequence< Reference < browse::XBrowseNode > > result( numChildren );
122         for ( sal_Int32 index = 0; it != it_end && index < numChildren ; ++it )
123         {
124             Sequence< Reference < browse::XBrowseNode > > childs = *it;
125             for ( sal_Int32 j = 0; j < childs.getLength(); j++ )
126             {
127                 result[ index++ ] = childs[ j ];
128             }
129         }
130         return result;
131     }
132 
133     virtual sal_Bool SAL_CALL
hasChildNodes()134     hasChildNodes()
135         throw ( RuntimeException )
136     {
137         if ( m_Nodes.getLength() != 0 )
138         {
139             for ( sal_Int32 i = 0 ; i < m_Nodes.getLength(); i++ )
140             {
141                 try
142                 {
143                     if ( m_Nodes[ i ]->hasChildNodes() )
144                     {
145                         return sal_True;
146                     }
147                 }
148                 catch ( Exception& )
149                 {
150                     // some form of exception getting child nodes so move
151                     // on to the next one
152                 }
153             }
154         }
155 
156         return sal_False;
157     }
158 
getType()159     virtual sal_Int16 SAL_CALL getType()
160         throw ( RuntimeException )
161     {
162         return browse::BrowseNodeTypes::CONTAINER;
163     }
164 };
165 
166 
167 //typedef ::std::map< ::rtl::OUString, Reference< browse::XBrowseNode > >
168 typedef ::std::hash_map< ::rtl::OUString, Reference< browse::XBrowseNode >,
169     ::rtl::OUStringHash, ::std::equal_to< ::rtl::OUString > >
170         BrowseNodeAggregatorHash;
171 typedef ::std::vector< ::rtl::OUString > vString;
172 
173 
174 struct alphaSort
175 {
operator ()browsenodefactory::alphaSort176     bool operator()( const ::rtl::OUString& a, const ::rtl::OUString& b )
177     {
178         return a.compareTo( b ) < 0;
179     }
180 };
181 class LocationBrowseNode :
182     public ::cppu::WeakImplHelper1< browse::XBrowseNode >
183 {
184 private:
185     BrowseNodeAggregatorHash* m_hBNA;
186     vString m_vStr;
187     ::rtl::OUString m_sNodeName;
188     Reference< browse::XBrowseNode > m_origNode;
189 
190 public:
191 
LocationBrowseNode(const Reference<browse::XBrowseNode> & node)192     LocationBrowseNode( const Reference< browse::XBrowseNode >& node )
193     {
194         m_sNodeName = node->getName();
195         m_hBNA = NULL;
196         m_origNode.set( node );
197     }
198 
~LocationBrowseNode()199     ~LocationBrowseNode()
200     {
201         if (m_hBNA)
202         {
203             delete m_hBNA;
204         }
205     }
206 
207     // -------------------------------------------------------------------------
208     // XBrowseNode
209     // -------------------------------------------------------------------------
210 
getName()211     virtual ::rtl::OUString SAL_CALL getName()
212         throw ( RuntimeException )
213     {
214         return m_sNodeName;
215     }
216 
217     virtual Sequence< Reference< browse::XBrowseNode > > SAL_CALL
getChildNodes()218     getChildNodes()
219         throw ( RuntimeException )
220     {
221         if ( m_hBNA == NULL )
222         {
223             loadChildNodes();
224         }
225 
226         Sequence<  Reference< browse::XBrowseNode > > children( m_hBNA->size() );
227         sal_Int32 index = 0;
228 
229         vString::const_iterator it = m_vStr.begin();
230 
231         for ( ; it != m_vStr.end(); ++it, index++ )
232         {
233             children[ index ].set( m_hBNA->find( *it )->second );
234         }
235 
236         return children;
237     }
238 
hasChildNodes()239     virtual sal_Bool SAL_CALL hasChildNodes()
240         throw ( RuntimeException )
241     {
242         return sal_True;
243     }
244 
getType()245     virtual sal_Int16 SAL_CALL getType()
246         throw ( RuntimeException )
247     {
248         return browse::BrowseNodeTypes::CONTAINER;
249     }
250 
251 private:
252 
loadChildNodes()253     void loadChildNodes()
254     {
255         m_hBNA = new BrowseNodeAggregatorHash();
256 
257         Sequence< Reference< browse::XBrowseNode > > langNodes =
258             m_origNode->getChildNodes();
259 
260         for ( sal_Int32 i = 0; i < langNodes.getLength(); i++ )
261         {
262             Reference< browse::XBrowseNode > xbn;
263             if ( langNodes[ i ]->getName().equals(::rtl::OUString::createFromAscii("uno_packages")) )
264             {
265                 xbn.set( new LocationBrowseNode( langNodes[ i ] ) );
266             }
267             else
268             {
269                 xbn.set( langNodes[ i ] );
270             }
271 
272             Sequence< Reference< browse::XBrowseNode > > grandchildren =
273                 xbn->getChildNodes();
274 
275             for ( sal_Int32 j = 0; j < grandchildren.getLength(); j++ )
276             {
277                 Reference< browse::XBrowseNode > grandchild(grandchildren[j]);
278 
279                 BrowseNodeAggregatorHash::iterator h_it =
280                     m_hBNA->find( grandchild->getName() );
281 
282                 if ( h_it != m_hBNA->end() )
283                 {
284                     BrowseNodeAggregator* bna = static_cast< BrowseNodeAggregator* >( h_it->second.get() );
285                     bna->addBrowseNode( grandchild );
286                 }
287                 else
288                 {
289                     Reference< browse::XBrowseNode > bna(
290                         new BrowseNodeAggregator( grandchild ) );
291                     (*m_hBNA)[ grandchild->getName() ].set( bna );
292                     m_vStr.push_back( grandchild->getName() );
293                 }
294             }
295         }
296         // sort children alpahbetically
297         ::std::sort( m_vStr.begin(), m_vStr.end(), alphaSort() );
298     }
299 };
300 
301 namespace
302 {
303 
getAllBrowseNodes(const Reference<XComponentContext> & xCtx)304 Sequence< Reference< browse::XBrowseNode > > getAllBrowseNodes( const Reference< XComponentContext >& xCtx )
305 {
306 	Reference< lang::XMultiComponentFactory > mcf =
307 		xCtx->getServiceManager();
308 
309 	Sequence< ::rtl::OUString > openDocs =
310 		MiscUtils::allOpenTDocUrls( xCtx );
311 
312 	Reference< provider::XScriptProviderFactory > xFac;
313 	sal_Int32 initialSize = openDocs.getLength() + 2;
314 	sal_Int32 mspIndex = 0;
315 
316 	Sequence < Reference < browse::XBrowseNode > > locnBNs( initialSize );
317 	try
318 	{
319 		xFac.set(
320 			xCtx->getValueByName(
321 				OUSTR("/singletons/com.sun.star.script.provider.theMasterScriptProviderFactory") ), UNO_QUERY_THROW );
322 
323 		locnBNs[ mspIndex++ ] = Reference< browse::XBrowseNode >( xFac->createScriptProvider( makeAny( ::rtl::OUString::createFromAscii("user") ) ), UNO_QUERY_THROW );
324 		locnBNs[ mspIndex++ ] = Reference< browse::XBrowseNode >( xFac->createScriptProvider( makeAny( ::rtl::OUString::createFromAscii("share") ) ), UNO_QUERY_THROW );
325 	}
326 	// TODO proper exception handling, should throw
327 	catch( Exception& e )
328 	{
329 		(void)e;
330 		OSL_TRACE("Caught Exception %s",
331 			::rtl::OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).pData->buffer );
332 		locnBNs.realloc( mspIndex );
333 		return locnBNs;
334 	}
335 
336 	for ( sal_Int32 i = 0; i < openDocs.getLength(); i++ )
337 	{
338 		try
339 		{
340 			Reference< frame::XModel > model( MiscUtils::tDocUrlToModel( openDocs[ i ] ), UNO_QUERY_THROW );
341 
342 			// #i44599 Check if it's a real document or something special like Hidden/Preview
343 			css::uno::Reference< css::frame::XController > xCurrentController = model->getCurrentController();
344 			if( xCurrentController.is() )
345 			{
346 				comphelper::MediaDescriptor aMD( model->getArgs() );
347 				sal_Bool bDefault = false;
348 				sal_Bool bHidden  = aMD.getUnpackedValueOrDefault( comphelper::MediaDescriptor::PROP_HIDDEN(),  bDefault );
349 				sal_Bool bPreview = aMD.getUnpackedValueOrDefault( comphelper::MediaDescriptor::PROP_PREVIEW(), bDefault );
350 				if( !bHidden && !bPreview )
351                 {
352                     Reference< document::XEmbeddedScripts > xScripts( model, UNO_QUERY );
353                     if ( xScripts.is() )
354 					    locnBNs[ mspIndex++ ] = Reference< browse::XBrowseNode >(
355                             xFac->createScriptProvider( makeAny( model ) ), UNO_QUERY_THROW );
356                 }
357 			}
358 		}
359 		catch( const Exception& )
360 		{
361             DBG_UNHANDLED_EXCEPTION();
362 		}
363 
364 	}
365 
366 	Sequence < Reference < browse::XBrowseNode > > locnBNs_Return( mspIndex );
367 	for ( sal_Int32 j = 0; j < mspIndex; j++ )
368 		locnBNs_Return[j] = locnBNs[j];
369 
370     return locnBNs_Return;
371 }
372 
373 } // namespace
374 
375 typedef ::std::vector< Reference< browse::XBrowseNode > > vXBrowseNodes;
376 
377 struct alphaSortForBNodes
378 {
operator ()browsenodefactory::alphaSortForBNodes379     bool operator()( const Reference< browse::XBrowseNode >& a, const Reference< browse::XBrowseNode >& b )
380     {
381         return a->getName().compareTo( b->getName() ) < 0;
382     }
383 };
384 
385 typedef ::cppu::WeakImplHelper1< browse::XBrowseNode > t_BrowseNodeBase;
386 class DefaultBrowseNode :
387     public t_BrowseNodeBase
388 {
389 
390 private:
391     Reference< browse::XBrowseNode > m_xWrappedBrowseNode;
392     Reference< lang::XTypeProvider > m_xWrappedTypeProv;
393     Reference< XAggregation >        m_xAggProxy;
394     Reference< XComponentContext >   m_xCtx;
395 
396     DefaultBrowseNode();
397 public:
DefaultBrowseNode(const Reference<XComponentContext> & xCtx,const Reference<browse::XBrowseNode> & xNode)398     DefaultBrowseNode( const Reference< XComponentContext >& xCtx, const Reference< browse::XBrowseNode>& xNode ) : m_xWrappedBrowseNode( xNode ), m_xWrappedTypeProv( xNode, UNO_QUERY ), m_xCtx( xCtx, UNO_QUERY )
399     {
400         OSL_ENSURE( m_xWrappedBrowseNode.is(), "DefaultBrowseNode::DefaultBrowseNode(): No BrowseNode to wrap" );
401         OSL_ENSURE( m_xWrappedTypeProv.is(), "DefaultBrowseNode::DefaultBrowseNode(): No BrowseNode to wrap" );
402         OSL_ENSURE( m_xCtx.is(), "DefaultBrowseNode::DefaultBrowseNode(): No ComponentContext" );
403     // Use proxy factory service to create aggregatable proxy.
404         try
405         {
406             Reference< lang::XMultiComponentFactory > xMFac( m_xCtx->getServiceManager(), UNO_QUERY_THROW );
407             Reference< reflection::XProxyFactory > xProxyFac(
408                 xMFac->createInstanceWithContext(
409                         rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
410                             "com.sun.star.reflection.ProxyFactory" ) ),
411                         m_xCtx  ), UNO_QUERY_THROW );
412             m_xAggProxy = xProxyFac->createProxy( m_xWrappedBrowseNode );
413         }
414         catch(  uno::Exception& )
415         {
416             OSL_ENSURE( false, "DefaultBrowseNode::DefaultBrowseNode: Caught exception!" );
417         }
418         OSL_ENSURE( m_xAggProxy.is(),
419             "DefaultBrowseNode::DefaultBrowseNode: Wrapped BrowseNode cannot be aggregated!" );
420 
421         if ( m_xAggProxy.is() )
422         {
423             osl_incrementInterlockedCount( &m_refCount );
424 
425             /* i35609 - Fix crash on Solaris. The setDelegator call needs
426                to be in its own block to ensure that all temporary Reference
427                instances that are acquired during the call are released
428                before m_refCount is decremented again */
429             {
430                 m_xAggProxy->setDelegator(
431                     static_cast< cppu::OWeakObject * >( this ) );
432             }
433 
434             osl_decrementInterlockedCount( &m_refCount );
435         }
436     }
437 
~DefaultBrowseNode()438     ~DefaultBrowseNode()
439     {
440         if ( m_xAggProxy.is() )
441         {
442             m_xAggProxy->setDelegator( uno::Reference< uno::XInterface >() );
443         }
444     }
445 
446     virtual Sequence< Reference< browse::XBrowseNode > > SAL_CALL
getChildNodes()447                 getChildNodes()
448     throw ( RuntimeException )
449     {
450         if ( hasChildNodes() )
451         {
452             vXBrowseNodes m_vNodes;
453             Sequence < Reference< browse::XBrowseNode > > nodes =
454                 m_xWrappedBrowseNode->getChildNodes();
455             for ( sal_Int32 i=0; i<nodes.getLength(); i++ )
456             {
457 				Reference< browse::XBrowseNode > xBrowseNode = nodes[ i ];
458 		        OSL_ENSURE( xBrowseNode.is(), "DefaultBrowseNode::getChildNodes(): Invalid BrowseNode" );
459 				if( xBrowseNode.is() )
460 	                m_vNodes.push_back( new DefaultBrowseNode( m_xCtx, xBrowseNode ) );
461             }
462 
463             ::std::sort( m_vNodes.begin(), m_vNodes.end(), alphaSortForBNodes() );
464             Sequence < Reference< browse::XBrowseNode > > children( m_vNodes.size() );
465             vXBrowseNodes::const_iterator it = m_vNodes.begin();
466             for ( sal_Int32 i=0; it != m_vNodes.end() && i<children.getLength(); i++, ++it )
467             {
468                 children[ i ].set( *it );
469             }
470             return children;
471         }
472         else
473         {
474             // no nodes
475 
476             Sequence < Reference< browse::XBrowseNode > > none;
477             return none;
478         }
479     }
480 
getType()481     virtual sal_Int16 SAL_CALL getType()
482         throw ( RuntimeException )
483     {
484         return m_xWrappedBrowseNode->getType();
485     }
486 
487     virtual ::rtl::OUString
getName()488     SAL_CALL getName()
489     throw ( RuntimeException )
490     {
491         return m_xWrappedBrowseNode->getName();
492     }
493 
494     virtual sal_Bool SAL_CALL
hasChildNodes()495     hasChildNodes()
496         throw ( RuntimeException )
497     {
498         return m_xWrappedBrowseNode->hasChildNodes();
499     }
500 
501     // XInterface
queryInterface(const Type & aType)502     virtual Any SAL_CALL queryInterface( const Type& aType )
503         throw ( com::sun::star::uno::RuntimeException )
504     {
505         Any aRet = t_BrowseNodeBase::queryInterface( aType );
506         if ( aRet.hasValue() )
507         {
508             return aRet;
509         }
510         if ( m_xAggProxy.is() )
511         {
512             return m_xAggProxy->queryAggregation( aType );
513         }
514         else
515         {
516             return Any();
517         }
518     }
519 
acquire()520     virtual void SAL_CALL acquire()
521         throw ()
522 
523     {
524         osl_incrementInterlockedCount( &m_refCount );
525     }
release()526     virtual void SAL_CALL release()
527         throw ()
528     {
529         if ( osl_decrementInterlockedCount( &m_refCount ) == 0 )
530         {
531             delete this;
532         }
533     }
534     // XTypeProvider (implemnented by base, but needs to be overridden for
535     //                delegating to aggregate)
getTypes()536     virtual Sequence< Type > SAL_CALL getTypes()
537         throw ( com::sun::star::uno::RuntimeException )
538     {
539         return m_xWrappedTypeProv->getTypes();
540     }
getImplementationId()541     virtual Sequence< sal_Int8 > SAL_CALL getImplementationId()
542         throw ( com::sun::star::uno::RuntimeException )
543     {
544         return m_xWrappedTypeProv->getImplementationId();
545 
546     }
547 };
548 
549 class DefaultRootBrowseNode :
550     public ::cppu::WeakImplHelper1< browse::XBrowseNode >
551 {
552 
553 private:
554     vXBrowseNodes m_vNodes;
555     ::rtl::OUString m_Name;
556 
557     DefaultRootBrowseNode();
558 public:
DefaultRootBrowseNode(const Reference<XComponentContext> & xCtx)559     DefaultRootBrowseNode( const Reference< XComponentContext >& xCtx )
560     {
561         Sequence < Reference< browse::XBrowseNode > > nodes =
562             getAllBrowseNodes( xCtx );
563 
564         for ( sal_Int32 i=0; i<nodes.getLength(); i++ )
565         {
566             m_vNodes.push_back( new DefaultBrowseNode( xCtx, nodes[ i ] ) );
567         }
568         m_Name = ::rtl::OUString::createFromAscii( "Root" );
569     }
570 
~DefaultRootBrowseNode()571     ~DefaultRootBrowseNode()
572     {
573     }
574 
575     virtual Sequence< Reference< browse::XBrowseNode > > SAL_CALL
getChildNodes()576                 getChildNodes()
577     throw ( RuntimeException )
578     {
579         // no need to sort user, share, doc1...docN
580         //::std::sort( m_vNodes.begin(), m_vNodes.end(), alphaSortForBNodes() );
581         Sequence < Reference< browse::XBrowseNode > > children( m_vNodes.size() );
582         vXBrowseNodes::const_iterator it = m_vNodes.begin();
583         for ( sal_Int32 i=0; it != m_vNodes.end() && i<children.getLength(); i++, ++it )
584         {
585             children[ i ].set( *it );
586         }
587         return children;
588     }
589 
getType()590     virtual sal_Int16 SAL_CALL getType()
591         throw ( RuntimeException )
592     {
593         return browse::BrowseNodeTypes::ROOT;
594     }
595 
596     virtual ::rtl::OUString
getName()597     SAL_CALL getName()
598     throw ( RuntimeException )
599     {
600         return m_Name;
601     }
602 
603     virtual sal_Bool SAL_CALL
hasChildNodes()604     hasChildNodes()
605         throw ( RuntimeException )
606     {
607         sal_Bool result = sal_True;
608         if ( !m_vNodes.size() )
609         {
610             result = sal_False;
611         }
612         return result;
613     }
614 };
615 
616 
617 class SelectorBrowseNode :
618     public ::cppu::WeakImplHelper1< browse::XBrowseNode >
619 {
620 private:
621     Reference< XComponentContext > m_xComponentContext;
622 
623 public:
SelectorBrowseNode(const Reference<XComponentContext> & xContext)624     SelectorBrowseNode( const Reference< XComponentContext >& xContext )
625       : m_xComponentContext( xContext )
626     {
627     }
628 
~SelectorBrowseNode()629     ~SelectorBrowseNode()
630     {
631     }
632 
getName()633     virtual ::rtl::OUString SAL_CALL getName()
634         throw ( RuntimeException )
635     {
636 	    return ::rtl::OUString::createFromAscii( "Root" );
637     }
638 
639     virtual Sequence< Reference< browse::XBrowseNode > > SAL_CALL
getChildNodes()640     getChildNodes()
641         throw ( RuntimeException )
642     {
643 
644         Sequence < Reference < browse::XBrowseNode > > locnBNs = getAllBrowseNodes( m_xComponentContext );
645 
646         Sequence<  Reference< browse::XBrowseNode > > children(
647             locnBNs.getLength() );
648 
649         for ( sal_Int32 j = 0; j < locnBNs.getLength(); j++ )
650         {
651             children[j] = new LocationBrowseNode( locnBNs[j] );
652         }
653 
654         return children;
655     }
656 
hasChildNodes()657     virtual sal_Bool SAL_CALL hasChildNodes()
658         throw ( RuntimeException )
659     {
660         return sal_True; // will always be user and share
661     }
662 
getType()663     virtual sal_Int16 SAL_CALL getType()
664         throw ( RuntimeException )
665     {
666         return browse::BrowseNodeTypes::CONTAINER;
667     }
668 };
669 
BrowseNodeFactoryImpl(Reference<XComponentContext> const & xComponentContext)670 BrowseNodeFactoryImpl::BrowseNodeFactoryImpl(
671     Reference< XComponentContext > const & xComponentContext )
672     : m_xComponentContext( xComponentContext )
673 {
674 }
675 
~BrowseNodeFactoryImpl()676 BrowseNodeFactoryImpl::~BrowseNodeFactoryImpl()
677 {
678 }
679 
680 
681 //############################################################################
682 // Implementation of XBrowseNodeFactory
683 //############################################################################
684 
685 /*
686  * The selector hierarchy is the standard hierarchy for organizers with the
687  * language nodes removed.
688  */
689 Reference< browse::XBrowseNode > SAL_CALL
createView(sal_Int16 viewType)690 BrowseNodeFactoryImpl::createView( sal_Int16 viewType )
691     throw (RuntimeException)
692 {
693     switch( viewType )
694     {
695         case browse::BrowseNodeFactoryViewTypes::MACROSELECTOR:
696             return getSelectorHierarchy();
697         case browse::BrowseNodeFactoryViewTypes::MACROORGANIZER:
698             return getOrganizerHierarchy();
699         default:
700             throw RuntimeException( OUSTR("Unknown view type" ), Reference< XInterface >() );
701     }
702 }
703 
704 Reference< browse::XBrowseNode >
getSelectorHierarchy()705 BrowseNodeFactoryImpl::getSelectorHierarchy()
706     throw (RuntimeException)
707 {
708     /*if ( !m_xSelectorBrowseNode.is() )
709     {
710         m_xSelectorBrowseNode = new SelectorBrowseNode( m_xComponentContext );
711     }*/
712     return new SelectorBrowseNode( m_xComponentContext );
713 }
714 
715 Reference< browse::XBrowseNode >
getOrganizerHierarchy()716 BrowseNodeFactoryImpl::getOrganizerHierarchy()
717     throw (RuntimeException)
718 {
719     Reference< browse::XBrowseNode > xRet = new  DefaultRootBrowseNode( m_xComponentContext );
720     return xRet;
721 }
722 //############################################################################
723 // Helper methods
724 //############################################################################
725 
726 //############################################################################
727 // Namespace global methods for setting up BrowseNodeFactory service
728 //############################################################################
729 
730 Sequence< ::rtl::OUString > SAL_CALL
bnf_getSupportedServiceNames()731 bnf_getSupportedServiceNames( )
732     SAL_THROW( () )
733 {
734     ::rtl::OUString str_name = ::rtl::OUString::createFromAscii(
735         "com.sun.star.script.browse.BrowseNodeFactory");
736 
737     return Sequence< ::rtl::OUString >( &str_name, 1 );
738 }
739 
740 ::rtl::OUString SAL_CALL
bnf_getImplementationName()741 bnf_getImplementationName( )
742     SAL_THROW( () )
743 {
744     return ::rtl::OUString::createFromAscii(
745         "com.sun.star.script.browse.BrowseNodeFactory" );
746 }
747 
748 Reference< XInterface > SAL_CALL
bnf_create(Reference<XComponentContext> const & xComponentContext)749 bnf_create( Reference< XComponentContext > const & xComponentContext )
750     SAL_THROW( (Exception) )
751 {
752     return static_cast< ::cppu::OWeakObject * >(
753         new BrowseNodeFactoryImpl( xComponentContext ) );
754 }
755 
756 //############################################################################
757 // Implementation of XServiceInfo
758 //############################################################################
759 
760 ::rtl::OUString SAL_CALL
getImplementationName()761 BrowseNodeFactoryImpl::getImplementationName()
762     throw (RuntimeException)
763 {
764     return bnf_getImplementationName();
765 }
766 
767 Sequence< ::rtl::OUString > SAL_CALL
getSupportedServiceNames()768 BrowseNodeFactoryImpl::getSupportedServiceNames()
769     throw (RuntimeException)
770 {
771     return bnf_getSupportedServiceNames();
772 }
773 
supportsService(::rtl::OUString const & serviceName)774 sal_Bool BrowseNodeFactoryImpl::supportsService(
775     ::rtl::OUString const & serviceName )
776     throw (RuntimeException)
777 {
778 //     check();
779 
780     Sequence< ::rtl::OUString > supported_services(
781         getSupportedServiceNames() );
782 
783     ::rtl::OUString const * ar = supported_services.getConstArray();
784 
785     for ( sal_Int32 pos = supported_services.getLength(); pos--; )
786     {
787         if (ar[ pos ].equals( serviceName ))
788             return sal_True;
789     }
790     return sal_False;
791 }
792 
793 } // namespace browsenodefactory
794