1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: documentenumeration.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basctl.hxx"
34 #include "documentenumeration.hxx"
36 /** === begin UNO includes === **/
37 #include <com/sun/star/frame/XDesktop.hpp>
38 #include <com/sun/star/frame/XModel2.hpp>
39 #include <com/sun/star/frame/FrameSearchFlag.hpp>
40 #include <com/sun/star/lang/XServiceInfo.hpp>
41 #include <com/sun/star/frame/XFramesSupplier.hpp>
42 /** === end UNO includes === **/
44 #include <tools/diagnose_ex.h>
46 #include <comphelper/stl_types.hxx>
48 //........................................................................
49 namespace basctl
{ namespace docs
{
50 //........................................................................
52 /** === begin UNO using === **/
53 using ::com::sun::star::uno::Exception
;
54 using ::com::sun::star::uno::Reference
;
55 using ::com::sun::star::uno::UNO_QUERY_THROW
;
56 using ::com::sun::star::uno::UNO_SET_THROW
;
57 using ::com::sun::star::frame::XDesktop
;
58 using ::com::sun::star::container::XEnumerationAccess
;
59 using ::com::sun::star::container::XEnumeration
;
60 using ::com::sun::star::uno::Any
;
61 using ::com::sun::star::frame::XModel
;
62 using ::com::sun::star::frame::XFramesSupplier
;
63 using ::com::sun::star::frame::XFrames
;
64 using ::com::sun::star::frame::XController
;
65 using ::com::sun::star::frame::XModel2
;
66 using ::com::sun::star::uno::UNO_QUERY
;
67 using ::com::sun::star::lang::XServiceInfo
;
68 using ::com::sun::star::uno::Sequence
;
69 using ::com::sun::star::frame::XFrame
;
70 /** === end UNO using === **/
71 namespace FrameSearchFlag
= ::com::sun::star::frame::FrameSearchFlag
;
73 //====================================================================
74 //= DocumentEnumeration_Data
75 //====================================================================
76 struct DocumentEnumeration_Data
78 ::comphelper::ComponentContext aContext
;
79 const IDocumentDescriptorFilter
* pFilter
;
81 DocumentEnumeration_Data( const ::comphelper::ComponentContext
& _rContext
, const IDocumentDescriptorFilter
* _pFilter
)
82 :aContext( _rContext
)
88 //====================================================================
89 //= DocumentEnumeration
90 //====================================================================
91 //--------------------------------------------------------------------
92 DocumentEnumeration::DocumentEnumeration( const ::comphelper::ComponentContext
& _rContext
, const IDocumentDescriptorFilter
* _pFilter
)
93 :m_pData( new DocumentEnumeration_Data( _rContext
, _pFilter
) )
97 //--------------------------------------------------------------------
98 DocumentEnumeration::~DocumentEnumeration()
102 //--------------------------------------------------------------------
105 //................................................................
106 void lcl_getDocumentControllers_nothrow( DocumentDescriptor
& _io_rDocDesc
)
108 OSL_PRECOND( _io_rDocDesc
.xModel
.is(), "lcl_getDocumentControllers_nothrow: illegal model!" );
110 _io_rDocDesc
.aControllers
.clear();
113 Reference
< XModel2
> xModel2( _io_rDocDesc
.xModel
, UNO_QUERY
);
116 Reference
< XEnumeration
> xEnum( xModel2
->getControllers(), UNO_SET_THROW
);
117 while ( xEnum
->hasMoreElements() )
119 Reference
< XController
> xController( xEnum
->nextElement(), UNO_QUERY_THROW
);
120 _io_rDocDesc
.aControllers
.push_back( xController
);
123 else if ( _io_rDocDesc
.xModel
.is() )
124 _io_rDocDesc
.aControllers
.push_back( _io_rDocDesc
.xModel
->getCurrentController() );
126 catch( const Exception
& )
128 DBG_UNHANDLED_EXCEPTION();
132 //................................................................
133 void lcl_getDocuments_nothrow( const Sequence
< Reference
< XFrame
> >& _rFrames
, Documents
& _out_rDocuments
,
134 const IDocumentDescriptorFilter
* _pFilter
)
136 // ensure we don't encounter some models multiple times
137 ::std::set
< Reference
< XModel
>, ::comphelper::OInterfaceCompare
< XModel
> > aEncounteredModels
;
139 for ( const Reference
< XFrame
>* pFrame
= _rFrames
.getConstArray();
140 pFrame
!= _rFrames
.getConstArray() + _rFrames
.getLength();
146 OSL_ENSURE( pFrame
->is(), "lcl_getDocuments_nothrow: illegal frame!" );
149 Reference
< XController
> xController( (*pFrame
)->getController() );
150 if ( !xController
.is() )
153 Reference
< XModel
> xModel( xController
->getModel() );
155 // though it's legal for a controller to not have a model, we're not interested in
159 if ( aEncounteredModels
.find( xModel
) != aEncounteredModels
.end() )
160 // there might be multiple frames for the same model
161 // handle it only once
163 aEncounteredModels
.insert( xModel
);
165 // create a DocumentDescriptor
166 DocumentDescriptor aDescriptor
;
167 aDescriptor
.xModel
= xModel
;
168 lcl_getDocumentControllers_nothrow( aDescriptor
);
170 // consult filter, if there is one
171 if ( _pFilter
&& !_pFilter
->includeDocument( aDescriptor
) )
174 _out_rDocuments
.push_back( aDescriptor
);
176 catch( const Exception
& )
178 DBG_UNHANDLED_EXCEPTION();
184 //--------------------------------------------------------------------
185 void DocumentEnumeration::getDocuments( Documents
& _out_rDocuments
) const
187 _out_rDocuments
.clear();
191 const Reference
< XDesktop
> xDesktop( m_pData
->aContext
.createComponent( "com.sun.star.frame.Desktop" ), UNO_QUERY_THROW
);
192 const Reference
< XFramesSupplier
> xSuppFrames( xDesktop
, UNO_QUERY_THROW
);
193 const Reference
< XFrames
> xFrames( xSuppFrames
->getFrames(), UNO_SET_THROW
);
194 const Sequence
< Reference
< XFrame
> > aFrames( xFrames
->queryFrames( FrameSearchFlag::ALL
) );
196 lcl_getDocuments_nothrow( aFrames
, _out_rDocuments
, m_pData
->pFilter
);
198 catch( const Exception
& )
200 DBG_UNHANDLED_EXCEPTION();
204 //........................................................................
205 } } // namespace basctl::docs
206 //........................................................................