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