Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / basctl / source / basicide / documentenumeration.cxx
blob4140bca4c90665f263c956a51aee72df141b0cc1
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 .
20 #include <sal/config.h>
22 #include <set>
24 #include "documentenumeration.hxx"
26 #include <com/sun/star/frame/Desktop.hpp>
27 #include <com/sun/star/frame/XModel2.hpp>
28 #include <com/sun/star/frame/FrameSearchFlag.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::XEnumeration;
43 using ::com::sun::star::frame::XModel;
44 using ::com::sun::star::frame::XFrames;
45 using ::com::sun::star::frame::XController;
46 using ::com::sun::star::frame::XModel2;
47 using ::com::sun::star::uno::UNO_QUERY;
48 using ::com::sun::star::uno::Sequence;
49 using ::com::sun::star::frame::XFrame;
51 namespace FrameSearchFlag = ::com::sun::star::frame::FrameSearchFlag;
53 // DocumentEnumeration_Data
54 struct DocumentEnumeration_Data
56 Reference< css::uno::XComponentContext > aContext;
57 const IDocumentDescriptorFilter* pFilter;
59 DocumentEnumeration_Data( Reference< css::uno::XComponentContext > const & _rContext, const IDocumentDescriptorFilter* _pFilter )
60 :aContext( _rContext )
61 ,pFilter( _pFilter )
66 // DocumentEnumeration
67 DocumentEnumeration::DocumentEnumeration( Reference< css::uno::XComponentContext > const & _rContext, const IDocumentDescriptorFilter* _pFilter )
68 :m_pData( new DocumentEnumeration_Data( _rContext, _pFilter ) )
72 DocumentEnumeration::~DocumentEnumeration()
76 namespace
78 void lcl_getDocumentControllers_nothrow( DocumentDescriptor& _io_rDocDesc )
80 OSL_PRECOND( _io_rDocDesc.xModel.is(), "lcl_getDocumentControllers_nothrow: illegal model!" );
82 _io_rDocDesc.aControllers.clear();
83 try
85 Reference< XModel2 > xModel2( _io_rDocDesc.xModel, UNO_QUERY );
86 if ( xModel2.is() )
88 Reference< XEnumeration > xEnum( xModel2->getControllers(), UNO_SET_THROW );
89 while ( xEnum->hasMoreElements() )
91 Reference< XController > xController( xEnum->nextElement(), UNO_QUERY_THROW );
92 _io_rDocDesc.aControllers.push_back( xController );
95 else if ( _io_rDocDesc.xModel.is() )
96 _io_rDocDesc.aControllers.push_back( _io_rDocDesc.xModel->getCurrentController() );
98 catch( const Exception& )
100 DBG_UNHANDLED_EXCEPTION("basctl.basicide");
104 void lcl_getDocuments_nothrow( const Sequence< Reference< XFrame > >& _rFrames, Documents& _out_rDocuments,
105 const IDocumentDescriptorFilter* _pFilter )
107 // ensure we don't encounter some models multiple times
108 std::set< Reference< XModel >, ::comphelper::OInterfaceCompare< XModel > > aEncounteredModels;
110 for ( auto const & rFrame : _rFrames )
114 OSL_ENSURE( rFrame.is(), "lcl_getDocuments_nothrow: illegal frame!" );
115 if ( !rFrame.is() )
116 continue;
117 Reference< XController > xController( rFrame->getController() );
118 if ( !xController.is() )
119 continue;
121 Reference< XModel > xModel( xController->getModel() );
122 if ( !xModel.is() )
123 // though it's legal for a controller to not have a model, we're not interested in
124 // those
125 continue;
127 if ( aEncounteredModels.find( xModel ) != aEncounteredModels.end() )
128 // there might be multiple frames for the same model
129 // handle it only once
130 continue;
131 aEncounteredModels.insert( xModel );
133 // create a DocumentDescriptor
134 DocumentDescriptor aDescriptor;
135 aDescriptor.xModel = xModel;
136 lcl_getDocumentControllers_nothrow( aDescriptor );
138 // consult filter, if there is one
139 if ( _pFilter && !_pFilter->includeDocument( aDescriptor ) )
140 continue;
142 _out_rDocuments.push_back( aDescriptor );
144 catch( const Exception& )
146 DBG_UNHANDLED_EXCEPTION("basctl.basicide");
152 void DocumentEnumeration::getDocuments( Documents& _out_rDocuments ) const
154 _out_rDocuments.clear();
158 const Reference< XDesktop2 > xDesktop = Desktop::create( m_pData->aContext );
159 const Reference< XFrames > xFrames( xDesktop->getFrames(), UNO_SET_THROW );
160 const Sequence< Reference< XFrame > > aFrames( xFrames->queryFrames( FrameSearchFlag::ALL ) );
162 lcl_getDocuments_nothrow( aFrames, _out_rDocuments, m_pData->pFilter );
164 catch( const Exception& )
166 DBG_UNHANDLED_EXCEPTION("basctl.basicide");
170 } } // namespace basctl::docs
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */