Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / uui / source / iahndl-filter.cxx
blob25a13a84e275425de625aa6b4f8de5090d036e0b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/beans/XPropertyAccess.hpp>
21 #include <com/sun/star/container/XContainerQuery.hpp>
22 #include <com/sun/star/container/XNameContainer.hpp>
23 #include <com/sun/star/document/FilterOptionsRequest.hpp>
24 #include <com/sun/star/document/NoSuchFilterRequest.hpp>
25 #include <com/sun/star/document/XImporter.hpp>
26 #include <com/sun/star/document/XInteractionFilterOptions.hpp>
27 #include <com/sun/star/document/XInteractionFilterSelect.hpp>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/task/XInteractionAbort.hpp>
30 #include <com/sun/star/task/XInteractionRequest.hpp>
31 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
33 #include <comphelper/propertysequence.hxx>
34 #include <comphelper/sequenceashashmap.hxx>
35 #include <vcl/svapp.hxx>
37 #include "getcontinuations.hxx"
38 #include "fltdlg.hxx"
40 #include "iahndl.hxx"
42 using namespace com::sun::star;
44 namespace {
46 void
47 executeFilterDialog(
48 weld::Window* pParent ,
49 OUString const & rURL ,
50 uui::FilterNameList const & rFilters,
51 OUString & rFilter )
53 SolarMutexGuard aGuard;
55 uui::FilterDialog aDialog(pParent);
57 aDialog.SetURL(rURL);
58 aDialog.ChangeFilters(&rFilters);
60 uui::FilterNameListPtr pSelected = rFilters.end();
61 if (aDialog.AskForFilter(pSelected))
63 rFilter = pSelected->sInternal;
67 void
68 handleNoSuchFilterRequest_(
69 weld::Window* pParent,
70 uno::Reference< uno::XComponentContext > const & xContext,
71 document::NoSuchFilterRequest const & rRequest,
72 uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
73 rContinuations )
75 uno::Reference< task::XInteractionAbort > xAbort;
76 uno::Reference< document::XInteractionFilterSelect > xFilterTransport;
77 getContinuations(rContinuations, &xAbort, &xFilterTransport);
79 // check necessary resources - if they don't exist - abort or
80 // break this operation
81 if (!xAbort.is())
82 return;
84 if (!xFilterTransport.is())
86 xAbort->select();
87 return;
90 uno::Reference< container::XContainerQuery > xFilterContainer;
91 try
93 xFilterContainer.set( xContext->getServiceManager()->createInstanceWithContext(
94 "com.sun.star.document.FilterFactory", xContext ),
95 uno::UNO_QUERY );
97 catch ( uno::Exception const & )
101 if (!xFilterContainer.is())
103 xAbort->select();
104 return;
107 uui::FilterNameList lNames;
109 // Note: We look for all filters here which match the following criteria:
110 // - they are import filters as minimum (of course they can
111 // support export too)
112 // - we don't show any filter which are flagged as "don't show it
113 // at the UI" or "they are not installed"
114 // - we ignore filters, which have not set any valid
115 // DocumentService (e.g. our pure graphic filters)
116 // - we show it sorted by her UIName's
117 // - We don't use the order flag or prefer default filters.
118 // (Because this list shows all filters and the user should
119 // find his filter very easy by his UIName ...)
120 // - We use "_query_all" here ... but we filter graphic filters
121 // out by using DocumentService property later!
122 uno::Reference< container::XEnumeration > xFilters
123 = xFilterContainer->createSubSetEnumerationByQuery(
124 "_query_all:sort_prop=uiname:iflags=1:eflags=143360");
125 while (xFilters->hasMoreElements())
129 ::comphelper::SequenceAsHashMap lProps(xFilters->nextElement());
130 uui::FilterNamePair aPair;
132 aPair.sInternal = lProps.getUnpackedValueOrDefault(
133 "Name", OUString());
134 aPair.sUI = lProps.getUnpackedValueOrDefault(
135 "UIName", OUString());
136 if ( aPair.sInternal.isEmpty() || aPair.sUI.isEmpty() )
138 continue;
140 lNames.push_back( aPair );
142 catch(const uno::RuntimeException&)
144 throw;
146 catch(const uno::Exception&)
148 continue;
152 // no list available for showing
153 // -> abort operation
154 if (lNames.empty())
156 xAbort->select();
157 return;
160 // let the user select the right filter
161 OUString sSelectedFilter;
162 executeFilterDialog( pParent,
163 rRequest.URL,
164 lNames,
165 sSelectedFilter );
167 // If he doesn't select anyone
168 // -> abort operation
169 if (sSelectedFilter.isEmpty())
171 xAbort->select();
172 return;
175 // otherwise set it for return
176 xFilterTransport->setFilter( sSelectedFilter );
177 xFilterTransport->select();
180 void
181 handleFilterOptionsRequest_(
182 uno::Reference<awt::XWindow> const & rWindow,
183 uno::Reference< uno::XComponentContext > const & xContext,
184 document::FilterOptionsRequest const & rRequest,
185 uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
186 rContinuations)
188 uno::Reference< task::XInteractionAbort > xAbort;
189 uno::Reference< document::XInteractionFilterOptions > xFilterOptions;
190 getContinuations(rContinuations, &xAbort, &xFilterOptions);
192 uno::Reference< container::XNameAccess > xFilterCFG;
195 xFilterCFG.set( xContext->getServiceManager()->createInstanceWithContext(
196 "com.sun.star.document.FilterFactory", xContext ),
197 uno::UNO_QUERY );
199 catch ( uno::Exception const & )
203 if( xFilterCFG.is() && rRequest.rProperties.hasElements() )
207 OUString aFilterName;
208 auto pProperty = std::find_if(rRequest.rProperties.begin(), rRequest.rProperties.end(),
209 [](const beans::PropertyValue& rProp) { return rProp.Name == "FilterName"; });
210 if (pProperty != rRequest.rProperties.end())
212 pProperty->Value >>= aFilterName;
215 uno::Sequence < beans::PropertyValue > aProps;
216 if ( xFilterCFG->getByName( aFilterName ) >>= aProps )
218 auto pProp = std::find_if(aProps.begin(), aProps.end(),
219 [](const beans::PropertyValue& rProp) { return rProp.Name == "UIComponent"; });
220 if (pProp != aProps.end())
222 OUString aServiceName;
223 pProp->Value >>= aServiceName;
224 if( !aServiceName.isEmpty() )
226 uno::Sequence<uno::Any> aDialogArgs(comphelper::InitAnyPropertySequence(
228 {"ParentWindow", uno::Any(rWindow)},
229 }));
231 uno::Reference<
232 ui::dialogs::XExecutableDialog > xFilterDialog(
233 xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
234 aServiceName, aDialogArgs, xContext ),
235 uno::UNO_QUERY );
237 uno::Reference< beans::XPropertyAccess >
238 xFilterProperties( xFilterDialog,
239 uno::UNO_QUERY );
241 if( xFilterDialog.is() && xFilterProperties.is() )
243 uno::Reference<
244 document::XImporter > xImporter(
245 xFilterDialog, uno::UNO_QUERY );
246 if( xImporter.is() )
247 xImporter->setTargetDocument( rRequest.rModel );
249 xFilterProperties->setPropertyValues(
250 rRequest.rProperties );
252 if( xFilterDialog->execute() )
254 xFilterOptions->setFilterOptions(
255 xFilterProperties->getPropertyValues() );
256 xFilterOptions->select();
257 return;
264 catch( container::NoSuchElementException& )
266 // the filter name is unknown
268 catch( uno::Exception& )
273 xAbort->select();
276 } // namespace
278 bool
279 UUIInteractionHelper::handleNoSuchFilterRequest(
280 uno::Reference< task::XInteractionRequest > const & rRequest)
282 uno::Any aAnyRequest(rRequest->getRequest());
284 document::NoSuchFilterRequest aNoSuchFilterRequest;
285 if (aAnyRequest >>= aNoSuchFilterRequest)
287 uno::Reference<awt::XWindow> xParent = getParentXWindow();
288 handleNoSuchFilterRequest_(Application::GetFrameWeld(xParent),
289 m_xContext,
290 aNoSuchFilterRequest,
291 rRequest->getContinuations());
292 return true;
294 return false;
297 bool
298 UUIInteractionHelper::handleFilterOptionsRequest(
299 uno::Reference< task::XInteractionRequest > const & rRequest)
301 uno::Any aAnyRequest(rRequest->getRequest());
303 document::FilterOptionsRequest aFilterOptionsRequest;
304 if (aAnyRequest >>= aFilterOptionsRequest)
306 handleFilterOptionsRequest_(getParentXWindow(),
307 m_xContext,
308 aFilterOptionsRequest,
309 rRequest->getContinuations());
310 return true;
312 return false;
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */