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