LibreOffice Module basctl (master) 1
documentenumeration.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <set>
23
25
26#include <com/sun/star/frame/Desktop.hpp>
27#include <com/sun/star/frame/XModel2.hpp>
28#include <com/sun/star/frame/FrameSearchFlag.hpp>
29
31
32namespace basctl::docs {
33
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;
48
49 namespace FrameSearchFlag = ::com::sun::star::frame::FrameSearchFlag;
50
51 // DocumentEnumeration
53 : m_xContext( _rContext )
54 , m_pFilter( _pFilter )
55 {
56 }
57
59 {
60 }
61
62 namespace
63 {
64 void lcl_getDocumentControllers_nothrow( DocumentDescriptor& _io_rDocDesc )
65 {
66 OSL_PRECOND( _io_rDocDesc.xModel.is(), "lcl_getDocumentControllers_nothrow: illegal model!" );
67
68 _io_rDocDesc.aControllers.clear();
69 try
70 {
71 Reference< XModel2 > xModel2( _io_rDocDesc.xModel, UNO_QUERY );
72 if ( xModel2.is() )
73 {
74 Reference< XEnumeration > xEnum( xModel2->getControllers(), UNO_SET_THROW );
75 while ( xEnum->hasMoreElements() )
76 {
77 Reference< XController > xController( xEnum->nextElement(), UNO_QUERY_THROW );
78 _io_rDocDesc.aControllers.push_back( xController );
79 }
80 }
81 else if ( _io_rDocDesc.xModel.is() )
82 _io_rDocDesc.aControllers.push_back( _io_rDocDesc.xModel->getCurrentController() );
83 }
84 catch( const Exception& )
85 {
86 DBG_UNHANDLED_EXCEPTION("basctl.basicide");
87 }
88 }
89
90 void lcl_getDocuments_nothrow( const Sequence< Reference< XFrame > >& _rFrames, Documents& _out_rDocuments,
91 const IDocumentDescriptorFilter* _pFilter )
92 {
93 // ensure we don't encounter some models multiple times
94 std::set< Reference< XModel > > aEncounteredModels;
95
96 for ( auto const & rFrame : _rFrames )
97 {
98 try
99 {
100 OSL_ENSURE( rFrame.is(), "lcl_getDocuments_nothrow: illegal frame!" );
101 if ( !rFrame.is() )
102 continue;
103 Reference< XController > xController( rFrame->getController() );
104 if ( !xController.is() )
105 continue;
106
108 if ( !xModel.is() )
109 // though it's legal for a controller to not have a model, we're not interested in
110 // those
111 continue;
112
113 if ( !aEncounteredModels.insert( xModel ).second )
114 // there might be multiple frames for the same model
115 // handle it only once
116 continue;
117
118 // create a DocumentDescriptor
119 DocumentDescriptor aDescriptor;
120 aDescriptor.xModel = xModel;
121 lcl_getDocumentControllers_nothrow( aDescriptor );
122
123 // consult filter, if there is one
124 if ( _pFilter && !_pFilter->includeDocument( aDescriptor ) )
125 continue;
126
127 _out_rDocuments.push_back( aDescriptor );
128 }
129 catch( const Exception& )
130 {
131 DBG_UNHANDLED_EXCEPTION("basctl.basicide");
132 }
133 }
134 }
135 }
136
137 void DocumentEnumeration::getDocuments( Documents& _out_rDocuments ) const
138 {
139 _out_rDocuments.clear();
140
141 try
142 {
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 ) );
146
147 lcl_getDocuments_nothrow( aFrames, _out_rDocuments, m_pFilter );
148 }
149 catch( const Exception& )
150 {
151 DBG_UNHANDLED_EXCEPTION("basctl.basicide");
152 }
153 }
154
155} // namespace basctl::docs
156
157/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
css::uno::Reference< css::uno::XComponentContext > m_xContext
void getDocuments(Documents &_out_rDocuments) const
retrieves a list of all currently known documents in the application
const IDocumentDescriptorFilter * m_pFilter
DocumentEnumeration(css::uno::Reference< css::uno::XComponentContext > const &_rContext, const IDocumentDescriptorFilter *_pFilter)
allows pre-filtering when enumerating document descriptors
#define DBG_UNHANDLED_EXCEPTION(...)
std::vector< DocumentDescriptor > Documents
@ Exception
css::uno::Reference< css::frame::XModel > xModel
Reference< XController > xController
Reference< XModel > xModel
std::vector< uno::Reference< frame::XModel > > Documents