1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_basctl.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "documentenumeration.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir /** === begin UNO includes === **/
34*cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/frame/XModel2.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/frame/FrameSearchFlag.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/frame/XFramesSupplier.hpp>
39*cdf0e10cSrcweir /** === end UNO includes === **/
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir //........................................................................
46*cdf0e10cSrcweir namespace basctl { namespace docs {
47*cdf0e10cSrcweir //........................................................................
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir 	/** === begin UNO using === **/
50*cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
51*cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
52*cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
53*cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_SET_THROW;
54*cdf0e10cSrcweir     using ::com::sun::star::frame::XDesktop;
55*cdf0e10cSrcweir     using ::com::sun::star::container::XEnumerationAccess;
56*cdf0e10cSrcweir     using ::com::sun::star::container::XEnumeration;
57*cdf0e10cSrcweir     using ::com::sun::star::uno::Any;
58*cdf0e10cSrcweir     using ::com::sun::star::frame::XModel;
59*cdf0e10cSrcweir     using ::com::sun::star::frame::XFramesSupplier;
60*cdf0e10cSrcweir     using ::com::sun::star::frame::XFrames;
61*cdf0e10cSrcweir     using ::com::sun::star::frame::XController;
62*cdf0e10cSrcweir     using ::com::sun::star::frame::XModel2;
63*cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY;
64*cdf0e10cSrcweir     using ::com::sun::star::lang::XServiceInfo;
65*cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
66*cdf0e10cSrcweir     using ::com::sun::star::frame::XFrame;
67*cdf0e10cSrcweir 	/** === end UNO using === **/
68*cdf0e10cSrcweir     namespace FrameSearchFlag = ::com::sun::star::frame::FrameSearchFlag;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 	//====================================================================
71*cdf0e10cSrcweir 	//= DocumentEnumeration_Data
72*cdf0e10cSrcweir 	//====================================================================
73*cdf0e10cSrcweir     struct DocumentEnumeration_Data
74*cdf0e10cSrcweir     {
75*cdf0e10cSrcweir         ::comphelper::ComponentContext      aContext;
76*cdf0e10cSrcweir         const IDocumentDescriptorFilter*    pFilter;
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir         DocumentEnumeration_Data( const ::comphelper::ComponentContext& _rContext, const IDocumentDescriptorFilter* _pFilter )
79*cdf0e10cSrcweir             :aContext( _rContext )
80*cdf0e10cSrcweir             ,pFilter( _pFilter )
81*cdf0e10cSrcweir         {
82*cdf0e10cSrcweir         }
83*cdf0e10cSrcweir     };
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 	//====================================================================
86*cdf0e10cSrcweir 	//= DocumentEnumeration
87*cdf0e10cSrcweir 	//====================================================================
88*cdf0e10cSrcweir 	//--------------------------------------------------------------------
89*cdf0e10cSrcweir     DocumentEnumeration::DocumentEnumeration( const ::comphelper::ComponentContext& _rContext, const IDocumentDescriptorFilter* _pFilter )
90*cdf0e10cSrcweir         :m_pData( new DocumentEnumeration_Data( _rContext, _pFilter ) )
91*cdf0e10cSrcweir     {
92*cdf0e10cSrcweir     }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir     //--------------------------------------------------------------------
95*cdf0e10cSrcweir     DocumentEnumeration::~DocumentEnumeration()
96*cdf0e10cSrcweir     {
97*cdf0e10cSrcweir     }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir     //--------------------------------------------------------------------
100*cdf0e10cSrcweir     namespace
101*cdf0e10cSrcweir     {
102*cdf0e10cSrcweir         //................................................................
103*cdf0e10cSrcweir         void lcl_getDocumentControllers_nothrow( DocumentDescriptor& _io_rDocDesc )
104*cdf0e10cSrcweir         {
105*cdf0e10cSrcweir             OSL_PRECOND( _io_rDocDesc.xModel.is(), "lcl_getDocumentControllers_nothrow: illegal model!" );
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir             _io_rDocDesc.aControllers.clear();
108*cdf0e10cSrcweir             try
109*cdf0e10cSrcweir             {
110*cdf0e10cSrcweir                 Reference< XModel2 > xModel2( _io_rDocDesc.xModel, UNO_QUERY );
111*cdf0e10cSrcweir                 if ( xModel2.is() )
112*cdf0e10cSrcweir                 {
113*cdf0e10cSrcweir                     Reference< XEnumeration > xEnum( xModel2->getControllers(), UNO_SET_THROW );
114*cdf0e10cSrcweir                     while ( xEnum->hasMoreElements() )
115*cdf0e10cSrcweir                     {
116*cdf0e10cSrcweir                         Reference< XController > xController( xEnum->nextElement(), UNO_QUERY_THROW );
117*cdf0e10cSrcweir                         _io_rDocDesc.aControllers.push_back( xController );
118*cdf0e10cSrcweir                     }
119*cdf0e10cSrcweir                 }
120*cdf0e10cSrcweir                 else if ( _io_rDocDesc.xModel.is() )
121*cdf0e10cSrcweir                     _io_rDocDesc.aControllers.push_back( _io_rDocDesc.xModel->getCurrentController() );
122*cdf0e10cSrcweir             }
123*cdf0e10cSrcweir             catch( const Exception& )
124*cdf0e10cSrcweir             {
125*cdf0e10cSrcweir             	DBG_UNHANDLED_EXCEPTION();
126*cdf0e10cSrcweir             }
127*cdf0e10cSrcweir         }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir         //................................................................
130*cdf0e10cSrcweir         void lcl_getDocuments_nothrow( const Sequence< Reference< XFrame > >& _rFrames, Documents& _out_rDocuments,
131*cdf0e10cSrcweir             const IDocumentDescriptorFilter* _pFilter )
132*cdf0e10cSrcweir         {
133*cdf0e10cSrcweir             // ensure we don't encounter some models multiple times
134*cdf0e10cSrcweir             ::std::set< Reference< XModel >, ::comphelper::OInterfaceCompare< XModel > > aEncounteredModels;
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir             for (   const Reference< XFrame >* pFrame = _rFrames.getConstArray();
137*cdf0e10cSrcweir                     pFrame != _rFrames.getConstArray() + _rFrames.getLength();
138*cdf0e10cSrcweir                     ++pFrame
139*cdf0e10cSrcweir                 )
140*cdf0e10cSrcweir             {
141*cdf0e10cSrcweir                 try
142*cdf0e10cSrcweir                 {
143*cdf0e10cSrcweir                     OSL_ENSURE( pFrame->is(), "lcl_getDocuments_nothrow: illegal frame!" );
144*cdf0e10cSrcweir                     if ( !pFrame->is() )
145*cdf0e10cSrcweir                         continue;
146*cdf0e10cSrcweir                     Reference< XController > xController( (*pFrame)->getController() );
147*cdf0e10cSrcweir                     if ( !xController.is() )
148*cdf0e10cSrcweir                         continue;
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir                     Reference< XModel > xModel( xController->getModel() );
151*cdf0e10cSrcweir                     if ( !xModel.is() )
152*cdf0e10cSrcweir                         // though it's legal for a controller to not have a model, we're not interested in
153*cdf0e10cSrcweir                         // those
154*cdf0e10cSrcweir                         continue;
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir                     if ( aEncounteredModels.find( xModel ) != aEncounteredModels.end() )
157*cdf0e10cSrcweir                         // there might be multiple frames for the same model
158*cdf0e10cSrcweir                         // handle it only once
159*cdf0e10cSrcweir                         continue;
160*cdf0e10cSrcweir                     aEncounteredModels.insert( xModel );
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir                     // create a DocumentDescriptor
163*cdf0e10cSrcweir                     DocumentDescriptor aDescriptor;
164*cdf0e10cSrcweir                     aDescriptor.xModel = xModel;
165*cdf0e10cSrcweir                     lcl_getDocumentControllers_nothrow( aDescriptor );
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir                     // consult filter, if there is one
168*cdf0e10cSrcweir                     if ( _pFilter && !_pFilter->includeDocument( aDescriptor ) )
169*cdf0e10cSrcweir                         continue;
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir                     _out_rDocuments.push_back( aDescriptor );
172*cdf0e10cSrcweir                 }
173*cdf0e10cSrcweir                 catch( const Exception& )
174*cdf0e10cSrcweir                 {
175*cdf0e10cSrcweir             	    DBG_UNHANDLED_EXCEPTION();
176*cdf0e10cSrcweir                 }
177*cdf0e10cSrcweir             }
178*cdf0e10cSrcweir         }
179*cdf0e10cSrcweir     }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir     //--------------------------------------------------------------------
182*cdf0e10cSrcweir     void DocumentEnumeration::getDocuments( Documents& _out_rDocuments ) const
183*cdf0e10cSrcweir     {
184*cdf0e10cSrcweir         _out_rDocuments.clear();
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir         try
187*cdf0e10cSrcweir         {
188*cdf0e10cSrcweir             const Reference< XDesktop > xDesktop( m_pData->aContext.createComponent( "com.sun.star.frame.Desktop" ), UNO_QUERY_THROW );
189*cdf0e10cSrcweir             const Reference< XFramesSupplier > xSuppFrames( xDesktop, UNO_QUERY_THROW );
190*cdf0e10cSrcweir             const Reference< XFrames > xFrames( xSuppFrames->getFrames(), UNO_SET_THROW );
191*cdf0e10cSrcweir             const Sequence< Reference< XFrame > > aFrames( xFrames->queryFrames( FrameSearchFlag::ALL ) );
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir             lcl_getDocuments_nothrow( aFrames, _out_rDocuments, m_pData->pFilter );
194*cdf0e10cSrcweir         }
195*cdf0e10cSrcweir         catch( const Exception& )
196*cdf0e10cSrcweir         {
197*cdf0e10cSrcweir         	DBG_UNHANDLED_EXCEPTION();
198*cdf0e10cSrcweir         }
199*cdf0e10cSrcweir     }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir //........................................................................
202*cdf0e10cSrcweir } } // namespace basctl::docs
203*cdf0e10cSrcweir //........................................................................
204