1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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 <comphelper/diagnose_ex.hxx>
32 namespace basctl::docs
{
34 using ::com::sun::star::uno::Exception
;
35 using ::com::sun::star::uno::Reference
;
36 using ::com::sun::star::uno::UNO_QUERY_THROW
;
37 using ::com::sun::star::uno::UNO_SET_THROW
;
38 using ::com::sun::star::frame::Desktop
;
39 using ::com::sun::star::frame::XDesktop2
;
40 using ::com::sun::star::container::XEnumeration
;
41 using ::com::sun::star::frame::XModel
;
42 using ::com::sun::star::frame::XFrames
;
43 using ::com::sun::star::frame::XController
;
44 using ::com::sun::star::frame::XModel2
;
45 using ::com::sun::star::uno::UNO_QUERY
;
46 using ::com::sun::star::uno::Sequence
;
47 using ::com::sun::star::frame::XFrame
;
49 namespace FrameSearchFlag
= ::com::sun::star::frame::FrameSearchFlag
;
51 // DocumentEnumeration
52 DocumentEnumeration::DocumentEnumeration( Reference
< css::uno::XComponentContext
> const & _rContext
, const IDocumentDescriptorFilter
* _pFilter
)
53 : m_xContext( _rContext
)
54 , m_pFilter( _pFilter
)
58 DocumentEnumeration::~DocumentEnumeration()
64 void lcl_getDocumentControllers_nothrow( DocumentDescriptor
& _io_rDocDesc
)
66 OSL_PRECOND( _io_rDocDesc
.xModel
.is(), "lcl_getDocumentControllers_nothrow: illegal model!" );
68 _io_rDocDesc
.aControllers
.clear();
71 Reference
< XModel2
> xModel2( _io_rDocDesc
.xModel
, UNO_QUERY
);
74 Reference
< XEnumeration
> xEnum( xModel2
->getControllers(), UNO_SET_THROW
);
75 while ( xEnum
->hasMoreElements() )
77 Reference
< XController
> xController( xEnum
->nextElement(), UNO_QUERY_THROW
);
78 _io_rDocDesc
.aControllers
.push_back( xController
);
81 else if ( _io_rDocDesc
.xModel
.is() )
82 _io_rDocDesc
.aControllers
.push_back( _io_rDocDesc
.xModel
->getCurrentController() );
84 catch( const Exception
& )
86 DBG_UNHANDLED_EXCEPTION("basctl.basicide");
90 void lcl_getDocuments_nothrow( const Sequence
< Reference
< XFrame
> >& _rFrames
, Documents
& _out_rDocuments
,
91 const IDocumentDescriptorFilter
* _pFilter
)
93 // ensure we don't encounter some models multiple times
94 std::set
< Reference
< XModel
> > aEncounteredModels
;
96 for ( auto const & rFrame
: _rFrames
)
100 OSL_ENSURE( rFrame
.is(), "lcl_getDocuments_nothrow: illegal frame!" );
103 Reference
< XController
> xController( rFrame
->getController() );
104 if ( !xController
.is() )
107 Reference
< XModel
> xModel( xController
->getModel() );
109 // though it's legal for a controller to not have a model, we're not interested in
113 if ( !aEncounteredModels
.insert( xModel
).second
)
114 // there might be multiple frames for the same model
115 // handle it only once
118 // create a DocumentDescriptor
119 DocumentDescriptor aDescriptor
;
120 aDescriptor
.xModel
= xModel
;
121 lcl_getDocumentControllers_nothrow( aDescriptor
);
123 // consult filter, if there is one
124 if ( _pFilter
&& !_pFilter
->includeDocument( aDescriptor
) )
127 _out_rDocuments
.push_back( aDescriptor
);
129 catch( const Exception
& )
131 DBG_UNHANDLED_EXCEPTION("basctl.basicide");
137 void DocumentEnumeration::getDocuments( Documents
& _out_rDocuments
) const
139 _out_rDocuments
.clear();
143 const Reference
< XDesktop2
> xDesktop
= Desktop::create( m_xContext
);
144 const Reference
< XFrames
> xFrames( xDesktop
->getFrames(), UNO_SET_THROW
);
145 const Sequence
< Reference
< XFrame
> > aFrames( xFrames
->queryFrames( FrameSearchFlag::ALL
) );
147 lcl_getDocuments_nothrow( aFrames
, _out_rDocuments
, m_pFilter
);
149 catch( const Exception
& )
151 DBG_UNHANDLED_EXCEPTION("basctl.basicide");
155 } // namespace basctl::docs
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */