Line data Source code
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 :
24 : #include "documentenumeration.hxx"
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 : #include <com/sun/star/lang/XServiceInfo.hpp>
30 : #include <com/sun/star/frame/XFramesSupplier.hpp>
31 : #include <com/sun/star/uno/XComponentContext.hpp>
32 :
33 : #include <tools/diagnose_ex.h>
34 :
35 : #include <comphelper/stl_types.hxx>
36 :
37 : namespace basctl { namespace docs {
38 :
39 : using ::com::sun::star::uno::Exception;
40 : using ::com::sun::star::uno::Reference;
41 : using ::com::sun::star::uno::UNO_QUERY_THROW;
42 : using ::com::sun::star::uno::UNO_SET_THROW;
43 : using ::com::sun::star::frame::Desktop;
44 : using ::com::sun::star::frame::XDesktop2;
45 : using ::com::sun::star::container::XEnumerationAccess;
46 : using ::com::sun::star::container::XEnumeration;
47 : using ::com::sun::star::uno::Any;
48 : using ::com::sun::star::frame::XModel;
49 : using ::com::sun::star::frame::XFramesSupplier;
50 : using ::com::sun::star::frame::XFrames;
51 : using ::com::sun::star::frame::XController;
52 : using ::com::sun::star::frame::XModel2;
53 : using ::com::sun::star::uno::UNO_QUERY;
54 : using ::com::sun::star::lang::XServiceInfo;
55 : using ::com::sun::star::uno::Sequence;
56 : using ::com::sun::star::frame::XFrame;
57 :
58 : namespace FrameSearchFlag = ::com::sun::star::frame::FrameSearchFlag;
59 :
60 : // DocumentEnumeration_Data
61 0 : struct DocumentEnumeration_Data
62 : {
63 : Reference< com::sun::star::uno::XComponentContext > aContext;
64 : const IDocumentDescriptorFilter* pFilter;
65 :
66 0 : DocumentEnumeration_Data( Reference< com::sun::star::uno::XComponentContext > const & _rContext, const IDocumentDescriptorFilter* _pFilter )
67 : :aContext( _rContext )
68 0 : ,pFilter( _pFilter )
69 : {
70 0 : }
71 : };
72 :
73 : // DocumentEnumeration
74 0 : DocumentEnumeration::DocumentEnumeration( Reference< com::sun::star::uno::XComponentContext > const & _rContext, const IDocumentDescriptorFilter* _pFilter )
75 0 : :m_pData( new DocumentEnumeration_Data( _rContext, _pFilter ) )
76 : {
77 0 : }
78 :
79 0 : DocumentEnumeration::~DocumentEnumeration()
80 : {
81 0 : }
82 :
83 : namespace
84 : {
85 0 : void lcl_getDocumentControllers_nothrow( DocumentDescriptor& _io_rDocDesc )
86 : {
87 : OSL_PRECOND( _io_rDocDesc.xModel.is(), "lcl_getDocumentControllers_nothrow: illegal model!" );
88 :
89 0 : _io_rDocDesc.aControllers.clear();
90 : try
91 : {
92 0 : Reference< XModel2 > xModel2( _io_rDocDesc.xModel, UNO_QUERY );
93 0 : if ( xModel2.is() )
94 : {
95 0 : Reference< XEnumeration > xEnum( xModel2->getControllers(), UNO_SET_THROW );
96 0 : while ( xEnum->hasMoreElements() )
97 : {
98 0 : Reference< XController > xController( xEnum->nextElement(), UNO_QUERY_THROW );
99 0 : _io_rDocDesc.aControllers.push_back( xController );
100 0 : }
101 : }
102 0 : else if ( _io_rDocDesc.xModel.is() )
103 0 : _io_rDocDesc.aControllers.push_back( _io_rDocDesc.xModel->getCurrentController() );
104 : }
105 0 : catch( const Exception& )
106 : {
107 : DBG_UNHANDLED_EXCEPTION();
108 : }
109 0 : }
110 :
111 0 : void lcl_getDocuments_nothrow( const Sequence< Reference< XFrame > >& _rFrames, Documents& _out_rDocuments,
112 : const IDocumentDescriptorFilter* _pFilter )
113 : {
114 : // ensure we don't encounter some models multiple times
115 0 : ::std::set< Reference< XModel >, ::comphelper::OInterfaceCompare< XModel > > aEncounteredModels;
116 :
117 0 : for ( const Reference< XFrame >* pFrame = _rFrames.getConstArray();
118 0 : pFrame != _rFrames.getConstArray() + _rFrames.getLength();
119 : ++pFrame
120 : )
121 : {
122 : try
123 : {
124 : OSL_ENSURE( pFrame->is(), "lcl_getDocuments_nothrow: illegal frame!" );
125 0 : if ( !pFrame->is() )
126 0 : continue;
127 0 : Reference< XController > xController( (*pFrame)->getController() );
128 0 : if ( !xController.is() )
129 0 : continue;
130 :
131 0 : Reference< XModel > xModel( xController->getModel() );
132 0 : if ( !xModel.is() )
133 : // though it's legal for a controller to not have a model, we're not interested in
134 : // those
135 0 : continue;
136 :
137 0 : if ( aEncounteredModels.find( xModel ) != aEncounteredModels.end() )
138 : // there might be multiple frames for the same model
139 : // handle it only once
140 0 : continue;
141 0 : aEncounteredModels.insert( xModel );
142 :
143 : // create a DocumentDescriptor
144 0 : DocumentDescriptor aDescriptor;
145 0 : aDescriptor.xModel = xModel;
146 0 : lcl_getDocumentControllers_nothrow( aDescriptor );
147 :
148 : // consult filter, if there is one
149 0 : if ( _pFilter && !_pFilter->includeDocument( aDescriptor ) )
150 0 : continue;
151 :
152 0 : _out_rDocuments.push_back( aDescriptor );
153 : }
154 0 : catch( const Exception& )
155 : {
156 : DBG_UNHANDLED_EXCEPTION();
157 : }
158 0 : }
159 0 : }
160 : }
161 :
162 0 : void DocumentEnumeration::getDocuments( Documents& _out_rDocuments ) const
163 : {
164 0 : _out_rDocuments.clear();
165 :
166 : try
167 : {
168 0 : const Reference< XDesktop2 > xDesktop = Desktop::create( m_pData->aContext );
169 0 : const Reference< XFrames > xFrames( xDesktop->getFrames(), UNO_SET_THROW );
170 0 : const Sequence< Reference< XFrame > > aFrames( xFrames->queryFrames( FrameSearchFlag::ALL ) );
171 :
172 0 : lcl_getDocuments_nothrow( aFrames, _out_rDocuments, m_pData->pFilter );
173 : }
174 0 : catch( const Exception& )
175 : {
176 : DBG_UNHANDLED_EXCEPTION();
177 : }
178 0 : }
179 :
180 : } } // namespace basctl::docs
181 :
182 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|