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