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