1*31598a22SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*31598a22SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*31598a22SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*31598a22SAndrew Rist  * distributed with this work for additional information
6*31598a22SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*31598a22SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*31598a22SAndrew Rist  * "License"); you may not use this file except in compliance
9*31598a22SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*31598a22SAndrew Rist  *
11*31598a22SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*31598a22SAndrew Rist  *
13*31598a22SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*31598a22SAndrew Rist  * software distributed under the License is distributed on an
15*31598a22SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*31598a22SAndrew Rist  * KIND, either express or implied.  See the License for the
17*31598a22SAndrew Rist  * specific language governing permissions and limitations
18*31598a22SAndrew Rist  * under the License.
19*31598a22SAndrew Rist  *
20*31598a22SAndrew Rist  *************************************************************/
21*31598a22SAndrew Rist 
22*31598a22SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_basctl.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "documentenumeration.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir /** === begin UNO includes === **/
30cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp>
31cdf0e10cSrcweir #include <com/sun/star/frame/XModel2.hpp>
32cdf0e10cSrcweir #include <com/sun/star/frame/FrameSearchFlag.hpp>
33cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
34cdf0e10cSrcweir #include <com/sun/star/frame/XFramesSupplier.hpp>
35cdf0e10cSrcweir /** === end UNO includes === **/
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <tools/diagnose_ex.h>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir //........................................................................
42cdf0e10cSrcweir namespace basctl { namespace docs {
43cdf0e10cSrcweir //........................................................................
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 	/** === begin UNO using === **/
46cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
47cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
48cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
49cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_SET_THROW;
50cdf0e10cSrcweir     using ::com::sun::star::frame::XDesktop;
51cdf0e10cSrcweir     using ::com::sun::star::container::XEnumerationAccess;
52cdf0e10cSrcweir     using ::com::sun::star::container::XEnumeration;
53cdf0e10cSrcweir     using ::com::sun::star::uno::Any;
54cdf0e10cSrcweir     using ::com::sun::star::frame::XModel;
55cdf0e10cSrcweir     using ::com::sun::star::frame::XFramesSupplier;
56cdf0e10cSrcweir     using ::com::sun::star::frame::XFrames;
57cdf0e10cSrcweir     using ::com::sun::star::frame::XController;
58cdf0e10cSrcweir     using ::com::sun::star::frame::XModel2;
59cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY;
60cdf0e10cSrcweir     using ::com::sun::star::lang::XServiceInfo;
61cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
62cdf0e10cSrcweir     using ::com::sun::star::frame::XFrame;
63cdf0e10cSrcweir 	/** === end UNO using === **/
64cdf0e10cSrcweir     namespace FrameSearchFlag = ::com::sun::star::frame::FrameSearchFlag;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 	//====================================================================
67cdf0e10cSrcweir 	//= DocumentEnumeration_Data
68cdf0e10cSrcweir 	//====================================================================
69cdf0e10cSrcweir     struct DocumentEnumeration_Data
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir         ::comphelper::ComponentContext      aContext;
72cdf0e10cSrcweir         const IDocumentDescriptorFilter*    pFilter;
73cdf0e10cSrcweir 
DocumentEnumeration_Databasctl::docs::DocumentEnumeration_Data74cdf0e10cSrcweir         DocumentEnumeration_Data( const ::comphelper::ComponentContext& _rContext, const IDocumentDescriptorFilter* _pFilter )
75cdf0e10cSrcweir             :aContext( _rContext )
76cdf0e10cSrcweir             ,pFilter( _pFilter )
77cdf0e10cSrcweir         {
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir     };
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 	//====================================================================
82cdf0e10cSrcweir 	//= DocumentEnumeration
83cdf0e10cSrcweir 	//====================================================================
84cdf0e10cSrcweir 	//--------------------------------------------------------------------
DocumentEnumeration(const::comphelper::ComponentContext & _rContext,const IDocumentDescriptorFilter * _pFilter)85cdf0e10cSrcweir     DocumentEnumeration::DocumentEnumeration( const ::comphelper::ComponentContext& _rContext, const IDocumentDescriptorFilter* _pFilter )
86cdf0e10cSrcweir         :m_pData( new DocumentEnumeration_Data( _rContext, _pFilter ) )
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir     }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     //--------------------------------------------------------------------
~DocumentEnumeration()91cdf0e10cSrcweir     DocumentEnumeration::~DocumentEnumeration()
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     //--------------------------------------------------------------------
96cdf0e10cSrcweir     namespace
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir         //................................................................
lcl_getDocumentControllers_nothrow(DocumentDescriptor & _io_rDocDesc)99cdf0e10cSrcweir         void lcl_getDocumentControllers_nothrow( DocumentDescriptor& _io_rDocDesc )
100cdf0e10cSrcweir         {
101cdf0e10cSrcweir             OSL_PRECOND( _io_rDocDesc.xModel.is(), "lcl_getDocumentControllers_nothrow: illegal model!" );
102cdf0e10cSrcweir 
103cdf0e10cSrcweir             _io_rDocDesc.aControllers.clear();
104cdf0e10cSrcweir             try
105cdf0e10cSrcweir             {
106cdf0e10cSrcweir                 Reference< XModel2 > xModel2( _io_rDocDesc.xModel, UNO_QUERY );
107cdf0e10cSrcweir                 if ( xModel2.is() )
108cdf0e10cSrcweir                 {
109cdf0e10cSrcweir                     Reference< XEnumeration > xEnum( xModel2->getControllers(), UNO_SET_THROW );
110cdf0e10cSrcweir                     while ( xEnum->hasMoreElements() )
111cdf0e10cSrcweir                     {
112cdf0e10cSrcweir                         Reference< XController > xController( xEnum->nextElement(), UNO_QUERY_THROW );
113cdf0e10cSrcweir                         _io_rDocDesc.aControllers.push_back( xController );
114cdf0e10cSrcweir                     }
115cdf0e10cSrcweir                 }
116cdf0e10cSrcweir                 else if ( _io_rDocDesc.xModel.is() )
117cdf0e10cSrcweir                     _io_rDocDesc.aControllers.push_back( _io_rDocDesc.xModel->getCurrentController() );
118cdf0e10cSrcweir             }
119cdf0e10cSrcweir             catch( const Exception& )
120cdf0e10cSrcweir             {
121cdf0e10cSrcweir             	DBG_UNHANDLED_EXCEPTION();
122cdf0e10cSrcweir             }
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir         //................................................................
lcl_getDocuments_nothrow(const Sequence<Reference<XFrame>> & _rFrames,Documents & _out_rDocuments,const IDocumentDescriptorFilter * _pFilter)126cdf0e10cSrcweir         void lcl_getDocuments_nothrow( const Sequence< Reference< XFrame > >& _rFrames, Documents& _out_rDocuments,
127cdf0e10cSrcweir             const IDocumentDescriptorFilter* _pFilter )
128cdf0e10cSrcweir         {
129cdf0e10cSrcweir             // ensure we don't encounter some models multiple times
130cdf0e10cSrcweir             ::std::set< Reference< XModel >, ::comphelper::OInterfaceCompare< XModel > > aEncounteredModels;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir             for (   const Reference< XFrame >* pFrame = _rFrames.getConstArray();
133cdf0e10cSrcweir                     pFrame != _rFrames.getConstArray() + _rFrames.getLength();
134cdf0e10cSrcweir                     ++pFrame
135cdf0e10cSrcweir                 )
136cdf0e10cSrcweir             {
137cdf0e10cSrcweir                 try
138cdf0e10cSrcweir                 {
139cdf0e10cSrcweir                     OSL_ENSURE( pFrame->is(), "lcl_getDocuments_nothrow: illegal frame!" );
140cdf0e10cSrcweir                     if ( !pFrame->is() )
141cdf0e10cSrcweir                         continue;
142cdf0e10cSrcweir                     Reference< XController > xController( (*pFrame)->getController() );
143cdf0e10cSrcweir                     if ( !xController.is() )
144cdf0e10cSrcweir                         continue;
145cdf0e10cSrcweir 
146cdf0e10cSrcweir                     Reference< XModel > xModel( xController->getModel() );
147cdf0e10cSrcweir                     if ( !xModel.is() )
148cdf0e10cSrcweir                         // though it's legal for a controller to not have a model, we're not interested in
149cdf0e10cSrcweir                         // those
150cdf0e10cSrcweir                         continue;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir                     if ( aEncounteredModels.find( xModel ) != aEncounteredModels.end() )
153cdf0e10cSrcweir                         // there might be multiple frames for the same model
154cdf0e10cSrcweir                         // handle it only once
155cdf0e10cSrcweir                         continue;
156cdf0e10cSrcweir                     aEncounteredModels.insert( xModel );
157cdf0e10cSrcweir 
158cdf0e10cSrcweir                     // create a DocumentDescriptor
159cdf0e10cSrcweir                     DocumentDescriptor aDescriptor;
160cdf0e10cSrcweir                     aDescriptor.xModel = xModel;
161cdf0e10cSrcweir                     lcl_getDocumentControllers_nothrow( aDescriptor );
162cdf0e10cSrcweir 
163cdf0e10cSrcweir                     // consult filter, if there is one
164cdf0e10cSrcweir                     if ( _pFilter && !_pFilter->includeDocument( aDescriptor ) )
165cdf0e10cSrcweir                         continue;
166cdf0e10cSrcweir 
167cdf0e10cSrcweir                     _out_rDocuments.push_back( aDescriptor );
168cdf0e10cSrcweir                 }
169cdf0e10cSrcweir                 catch( const Exception& )
170cdf0e10cSrcweir                 {
171cdf0e10cSrcweir             	    DBG_UNHANDLED_EXCEPTION();
172cdf0e10cSrcweir                 }
173cdf0e10cSrcweir             }
174cdf0e10cSrcweir         }
175cdf0e10cSrcweir     }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     //--------------------------------------------------------------------
getDocuments(Documents & _out_rDocuments) const178cdf0e10cSrcweir     void DocumentEnumeration::getDocuments( Documents& _out_rDocuments ) const
179cdf0e10cSrcweir     {
180cdf0e10cSrcweir         _out_rDocuments.clear();
181cdf0e10cSrcweir 
182cdf0e10cSrcweir         try
183cdf0e10cSrcweir         {
184cdf0e10cSrcweir             const Reference< XDesktop > xDesktop( m_pData->aContext.createComponent( "com.sun.star.frame.Desktop" ), UNO_QUERY_THROW );
185cdf0e10cSrcweir             const Reference< XFramesSupplier > xSuppFrames( xDesktop, UNO_QUERY_THROW );
186cdf0e10cSrcweir             const Reference< XFrames > xFrames( xSuppFrames->getFrames(), UNO_SET_THROW );
187cdf0e10cSrcweir             const Sequence< Reference< XFrame > > aFrames( xFrames->queryFrames( FrameSearchFlag::ALL ) );
188cdf0e10cSrcweir 
189cdf0e10cSrcweir             lcl_getDocuments_nothrow( aFrames, _out_rDocuments, m_pData->pFilter );
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir         catch( const Exception& )
192cdf0e10cSrcweir         {
193cdf0e10cSrcweir         	DBG_UNHANDLED_EXCEPTION();
194cdf0e10cSrcweir         }
195cdf0e10cSrcweir     }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir //........................................................................
198cdf0e10cSrcweir } } // namespace basctl::docs
199cdf0e10cSrcweir //........................................................................
200