bump product version to 5.0.4.1
[LibreOffice.git] / basctl / source / basicide / documentenumeration.cxx
blobc20ee862b88248bac042b3ea28d93b27ce029b31
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <set>
23 #include "documentenumeration.hxx"
25 #include <com/sun/star/frame/Desktop.hpp>
26 #include <com/sun/star/frame/XModel2.hpp>
27 #include <com/sun/star/frame/FrameSearchFlag.hpp>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <tools/diagnose_ex.h>
32 #include <comphelper/stl_types.hxx>
34 namespace basctl { namespace docs {
36 using ::com::sun::star::uno::Exception;
37 using ::com::sun::star::uno::Reference;
38 using ::com::sun::star::uno::UNO_QUERY_THROW;
39 using ::com::sun::star::uno::UNO_SET_THROW;
40 using ::com::sun::star::frame::Desktop;
41 using ::com::sun::star::frame::XDesktop2;
42 using ::com::sun::star::container::XEnumerationAccess;
43 using ::com::sun::star::container::XEnumeration;
44 using ::com::sun::star::uno::Any;
45 using ::com::sun::star::frame::XModel;
46 using ::com::sun::star::frame::XFramesSupplier;
47 using ::com::sun::star::frame::XFrames;
48 using ::com::sun::star::frame::XController;
49 using ::com::sun::star::frame::XModel2;
50 using ::com::sun::star::uno::UNO_QUERY;
51 using ::com::sun::star::lang::XServiceInfo;
52 using ::com::sun::star::uno::Sequence;
53 using ::com::sun::star::frame::XFrame;
55 namespace FrameSearchFlag = ::com::sun::star::frame::FrameSearchFlag;
57 // DocumentEnumeration_Data
58 struct DocumentEnumeration_Data
60 Reference< com::sun::star::uno::XComponentContext > aContext;
61 const IDocumentDescriptorFilter* pFilter;
63 DocumentEnumeration_Data( Reference< com::sun::star::uno::XComponentContext > const & _rContext, const IDocumentDescriptorFilter* _pFilter )
64 :aContext( _rContext )
65 ,pFilter( _pFilter )
70 // DocumentEnumeration
71 DocumentEnumeration::DocumentEnumeration( Reference< com::sun::star::uno::XComponentContext > const & _rContext, const IDocumentDescriptorFilter* _pFilter )
72 :m_pData( new DocumentEnumeration_Data( _rContext, _pFilter ) )
76 DocumentEnumeration::~DocumentEnumeration()
80 namespace
82 void lcl_getDocumentControllers_nothrow( DocumentDescriptor& _io_rDocDesc )
84 OSL_PRECOND( _io_rDocDesc.xModel.is(), "lcl_getDocumentControllers_nothrow: illegal model!" );
86 _io_rDocDesc.aControllers.clear();
87 try
89 Reference< XModel2 > xModel2( _io_rDocDesc.xModel, UNO_QUERY );
90 if ( xModel2.is() )
92 Reference< XEnumeration > xEnum( xModel2->getControllers(), UNO_SET_THROW );
93 while ( xEnum->hasMoreElements() )
95 Reference< XController > xController( xEnum->nextElement(), UNO_QUERY_THROW );
96 _io_rDocDesc.aControllers.push_back( xController );
99 else if ( _io_rDocDesc.xModel.is() )
100 _io_rDocDesc.aControllers.push_back( _io_rDocDesc.xModel->getCurrentController() );
102 catch( const Exception& )
104 DBG_UNHANDLED_EXCEPTION();
108 void lcl_getDocuments_nothrow( const Sequence< Reference< XFrame > >& _rFrames, Documents& _out_rDocuments,
109 const IDocumentDescriptorFilter* _pFilter )
111 // ensure we don't encounter some models multiple times
112 ::std::set< Reference< XModel >, ::comphelper::OInterfaceCompare< XModel > > aEncounteredModels;
114 for ( const Reference< XFrame >* pFrame = _rFrames.getConstArray();
115 pFrame != _rFrames.getConstArray() + _rFrames.getLength();
116 ++pFrame
121 OSL_ENSURE( pFrame->is(), "lcl_getDocuments_nothrow: illegal frame!" );
122 if ( !pFrame->is() )
123 continue;
124 Reference< XController > xController( (*pFrame)->getController() );
125 if ( !xController.is() )
126 continue;
128 Reference< XModel > xModel( xController->getModel() );
129 if ( !xModel.is() )
130 // though it's legal for a controller to not have a model, we're not interested in
131 // those
132 continue;
134 if ( aEncounteredModels.find( xModel ) != aEncounteredModels.end() )
135 // there might be multiple frames for the same model
136 // handle it only once
137 continue;
138 aEncounteredModels.insert( xModel );
140 // create a DocumentDescriptor
141 DocumentDescriptor aDescriptor;
142 aDescriptor.xModel = xModel;
143 lcl_getDocumentControllers_nothrow( aDescriptor );
145 // consult filter, if there is one
146 if ( _pFilter && !_pFilter->includeDocument( aDescriptor ) )
147 continue;
149 _out_rDocuments.push_back( aDescriptor );
151 catch( const Exception& )
153 DBG_UNHANDLED_EXCEPTION();
159 void DocumentEnumeration::getDocuments( Documents& _out_rDocuments ) const
161 _out_rDocuments.clear();
165 const Reference< XDesktop2 > xDesktop = Desktop::create( m_pData->aContext );
166 const Reference< XFrames > xFrames( xDesktop->getFrames(), UNO_SET_THROW );
167 const Sequence< Reference< XFrame > > aFrames( xFrames->queryFrames( FrameSearchFlag::ALL ) );
169 lcl_getDocuments_nothrow( aFrames, _out_rDocuments, m_pData->pFilter );
171 catch( const Exception& )
173 DBG_UNHANDLED_EXCEPTION();
177 } } // namespace basctl::docs
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */