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 "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/AmbigousFilterRequest.hpp"
24 : #include "com/sun/star/document/FilterOptionsRequest.hpp"
25 : #include "com/sun/star/document/NoSuchFilterRequest.hpp"
26 : #include "com/sun/star/document/XImporter.hpp"
27 : #include "com/sun/star/document/XInteractionFilterOptions.hpp"
28 : #include "com/sun/star/document/XInteractionFilterSelect.hpp"
29 : #include "com/sun/star/lang/XMultiServiceFactory.hpp"
30 : #include "com/sun/star/task/XInteractionAbort.hpp"
31 : #include "com/sun/star/task/XInteractionRequest.hpp"
32 : #include "com/sun/star/ui/dialogs/XExecutableDialog.hpp"
33 :
34 : #include "osl/mutex.hxx"
35 : #include "comphelper/sequenceashashmap.hxx"
36 : #include "vcl/svapp.hxx"
37 :
38 : #include "getcontinuations.hxx"
39 : #include "fltdlg.hxx"
40 :
41 : #include "iahndl.hxx"
42 :
43 : using namespace com::sun::star;
44 :
45 : namespace {
46 :
47 : void
48 0 : executeFilterDialog(
49 : Window * pParent ,
50 : OUString const & rURL ,
51 : uui::FilterNameList const & rFilters,
52 : OUString & rFilter )
53 : SAL_THROW((uno::RuntimeException))
54 : {
55 : try
56 : {
57 0 : SolarMutexGuard aGuard;
58 :
59 0 : std::auto_ptr< ResMgr > xManager(ResMgr::CreateResMgr("uui"));
60 :
61 : std::auto_ptr< uui::FilterDialog > xDialog(
62 0 : new uui::FilterDialog(pParent, xManager.get()));
63 :
64 0 : xDialog->SetURL(rURL);
65 0 : xDialog->ChangeFilters(&rFilters);
66 :
67 0 : uui::FilterNameListPtr pSelected = rFilters.end();
68 0 : if( xDialog->AskForFilter( pSelected ) )
69 : {
70 0 : rFilter = pSelected->sInternal;
71 0 : }
72 : }
73 0 : catch (std::bad_alloc const &)
74 : {
75 : throw uno::RuntimeException(
76 : OUString("out of memory"),
77 0 : uno::Reference< uno::XInterface >());
78 : }
79 0 : }
80 :
81 : void
82 0 : handleNoSuchFilterRequest_(
83 : Window * pParent,
84 : uno::Reference< uno::XComponentContext > const & xContext,
85 : document::NoSuchFilterRequest const & rRequest,
86 : uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
87 : rContinuations )
88 : SAL_THROW((uno::RuntimeException))
89 : {
90 0 : uno::Reference< task::XInteractionAbort > xAbort;
91 0 : uno::Reference< document::XInteractionFilterSelect > xFilterTransport;
92 0 : getContinuations(rContinuations, &xAbort, &xFilterTransport);
93 :
94 : // check necessary resources - if they don't exist - abort or
95 : // break this operation
96 0 : if (!xAbort.is())
97 0 : return;
98 :
99 0 : if (!xFilterTransport.is())
100 : {
101 0 : xAbort->select();
102 0 : return;
103 : }
104 :
105 0 : uno::Reference< container::XContainerQuery > xFilterContainer;
106 : try
107 : {
108 0 : xFilterContainer.set( xContext->getServiceManager()->createInstanceWithContext(
109 0 : OUString( "com.sun.star.document.FilterFactory"), xContext ),
110 0 : uno::UNO_QUERY );
111 : }
112 0 : catch ( uno::Exception const & )
113 : {
114 : }
115 :
116 0 : if (!xFilterContainer.is())
117 : {
118 0 : xAbort->select();
119 0 : return;
120 : }
121 :
122 0 : uui::FilterNameList lNames;
123 :
124 : // Note: We look for all filters here which match the following criteria:
125 : // - they are import filters as minimum (of course they can
126 : // support export too)
127 : // - we don't show any filter which are flagged as "don't show it
128 : // at the UI" or "they are not installed"
129 : // - we ignore filters, which have not set any valid
130 : // DocumentService (e.g. our pure graphic filters)
131 : // - we show it sorted by her UIName's
132 : // - We don't use the order flag or prefer default filters.
133 : // (Because this list shows all filters and the user should
134 : // find his filter very easy by his UIName ...)
135 : // - We use "_query_all" here ... but we filter graphic filters
136 : // out by using DocumentService property later!
137 : uno::Reference< container::XEnumeration > xFilters
138 0 : = xFilterContainer->createSubSetEnumerationByQuery(
139 0 : OUString( "_query_all:sort_prop=uiname:iflags=1:eflags=143360"));
140 0 : while (xFilters->hasMoreElements())
141 : {
142 : try
143 : {
144 0 : ::comphelper::SequenceAsHashMap lProps(xFilters->nextElement());
145 0 : uui::FilterNamePair aPair;
146 :
147 0 : aPair.sInternal = lProps.getUnpackedValueOrDefault(
148 0 : OUString("Name"), OUString());
149 0 : aPair.sUI = lProps.getUnpackedValueOrDefault(
150 0 : OUString("UIName"), OUString());
151 0 : if ( (!aPair.sInternal.Len()) || (!aPair.sUI.Len() ) )
152 : {
153 0 : continue;
154 : }
155 0 : lNames.push_back( aPair );
156 : }
157 0 : catch(const uno::RuntimeException&)
158 : {
159 0 : throw;
160 : }
161 0 : catch(const uno::Exception&)
162 : {
163 0 : continue;
164 : }
165 : }
166 :
167 : // no list available for showing
168 : // -> abort operation
169 0 : if (lNames.size()<1)
170 : {
171 0 : xAbort->select();
172 0 : return;
173 : }
174 :
175 : // let the user select the right filter
176 0 : OUString sSelectedFilter;
177 : executeFilterDialog( pParent,
178 : rRequest.URL,
179 : lNames,
180 0 : sSelectedFilter );
181 :
182 : // If he doesn't select anyone
183 : // -> abort operation
184 0 : if (sSelectedFilter.isEmpty())
185 : {
186 0 : xAbort->select();
187 0 : return;
188 : }
189 :
190 : // otherwise set it for return
191 0 : xFilterTransport->setFilter( sSelectedFilter );
192 0 : xFilterTransport->select();
193 : }
194 :
195 : void
196 0 : handleAmbigousFilterRequest_(
197 : Window * pParent,
198 : uno::Reference< uno::XComponentContext > const & xContext,
199 : document::AmbigousFilterRequest const & rRequest,
200 : uno::Sequence<
201 : uno::Reference<
202 : task::XInteractionContinuation > > const & rContinuations)
203 : SAL_THROW((uno::RuntimeException))
204 : {
205 0 : uno::Reference< task::XInteractionAbort > xAbort;
206 0 : uno::Reference< document::XInteractionFilterSelect > xFilterTransport;
207 0 : getContinuations(rContinuations, &xAbort, &xFilterTransport);
208 :
209 0 : uui::FilterNameList lNames;
210 :
211 0 : uno::Reference< container::XNameContainer > xFilterContainer;
212 : try
213 : {
214 0 : xFilterContainer.set( xContext->getServiceManager()->createInstanceWithContext(
215 0 : OUString( "com.sun.star.document.FilterFactory"), xContext ),
216 0 : uno::UNO_QUERY );
217 : }
218 0 : catch ( uno::Exception & )
219 : {
220 : }
221 :
222 0 : if( xFilterContainer.is() )
223 : {
224 0 : uno::Any aPackedSet ;
225 0 : uno::Sequence< beans::PropertyValue > lProps ;
226 : sal_Int32 nStep ;
227 0 : uui::FilterNamePair aPair ;
228 :
229 : try
230 : {
231 0 : aPackedSet = xFilterContainer->getByName( rRequest.SelectedFilter );
232 : }
233 0 : catch(const container::NoSuchElementException&)
234 : {
235 0 : aPackedSet.clear();
236 : }
237 0 : aPackedSet >>= lProps;
238 0 : for( nStep=0; nStep<lProps.getLength(); ++nStep )
239 : {
240 0 : if( lProps[nStep].Name.compareToAscii("UIName") == 0 )
241 : {
242 0 : OUString sTemp;
243 0 : lProps[nStep].Value >>= sTemp;
244 0 : aPair.sUI = sTemp;
245 0 : aPair.sInternal = rRequest.SelectedFilter;
246 0 : lNames.push_back( aPair );
247 0 : break;
248 : }
249 : }
250 :
251 : try
252 : {
253 0 : aPackedSet = xFilterContainer->getByName( rRequest.DetectedFilter );
254 : }
255 0 : catch(const container::NoSuchElementException&)
256 : {
257 0 : aPackedSet.clear();
258 : }
259 0 : aPackedSet >>= lProps;
260 0 : for( nStep=0; nStep<lProps.getLength(); ++nStep )
261 : {
262 0 : if( lProps[nStep].Name.compareToAscii("UIName") == 0 )
263 : {
264 0 : OUString sTemp;
265 0 : lProps[nStep].Value >>= sTemp;
266 0 : aPair.sUI = sTemp;
267 0 : aPair.sInternal = rRequest.DetectedFilter;
268 0 : lNames.push_back( aPair );
269 0 : break;
270 : }
271 0 : }
272 : }
273 :
274 0 : if( xAbort.is() && xFilterTransport.is() )
275 : {
276 0 : if( lNames.size() < 1 )
277 : {
278 0 : xAbort->select();
279 : }
280 : else
281 : {
282 0 : OUString sFilter;
283 : executeFilterDialog( pParent,
284 : rRequest.URL,
285 : lNames,
286 0 : sFilter );
287 :
288 0 : if( !sFilter.isEmpty() )
289 : {
290 0 : xFilterTransport->setFilter( sFilter );
291 0 : xFilterTransport->select();
292 : }
293 : else
294 0 : xAbort->select();
295 : }
296 0 : }
297 0 : }
298 :
299 : void
300 0 : handleFilterOptionsRequest_(
301 : uno::Reference< uno::XComponentContext > const & xContext,
302 : document::FilterOptionsRequest const & rRequest,
303 : uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
304 : rContinuations)
305 : SAL_THROW((uno::RuntimeException))
306 : {
307 0 : uno::Reference< task::XInteractionAbort > xAbort;
308 0 : uno::Reference< document::XInteractionFilterOptions > xFilterOptions;
309 0 : getContinuations(rContinuations, &xAbort, &xFilterOptions);
310 :
311 0 : uno::Reference< container::XNameAccess > xFilterCFG;
312 : try
313 : {
314 0 : xFilterCFG.set( xContext->getServiceManager()->createInstanceWithContext(
315 0 : OUString( "com.sun.star.document.FilterFactory" ), xContext ),
316 0 : uno::UNO_QUERY );
317 : }
318 0 : catch ( uno::Exception const & )
319 : {
320 : }
321 :
322 0 : if( xFilterCFG.is() && rRequest.rProperties.getLength() )
323 : {
324 : try
325 : {
326 0 : OUString aFilterName;
327 0 : sal_Int32 nPropCount = rRequest.rProperties.getLength();
328 0 : for( sal_Int32 ind = 0; ind < nPropCount; ++ind )
329 : {
330 0 : if( rRequest.rProperties[ind].Name.equals(
331 0 : OUString("FilterName")) )
332 : {
333 0 : rRequest.rProperties[ind].Value >>= aFilterName;
334 0 : break;
335 : }
336 : }
337 :
338 0 : uno::Sequence < beans::PropertyValue > aProps;
339 0 : if ( xFilterCFG->getByName( aFilterName ) >>= aProps )
340 : {
341 0 : sal_Int32 nPropertyCount = aProps.getLength();
342 0 : for( sal_Int32 nProperty=0;
343 : nProperty < nPropertyCount;
344 : ++nProperty )
345 0 : if( aProps[nProperty].Name.equals(
346 0 : OUString("UIComponent")) )
347 : {
348 0 : OUString aServiceName;
349 0 : aProps[nProperty].Value >>= aServiceName;
350 0 : if( !aServiceName.isEmpty() )
351 : {
352 : uno::Reference<
353 : ui::dialogs::XExecutableDialog > xFilterDialog(
354 0 : xContext->getServiceManager()->createInstanceWithContext(
355 0 : aServiceName, xContext ),
356 0 : uno::UNO_QUERY );
357 : uno::Reference< beans::XPropertyAccess >
358 : xFilterProperties( xFilterDialog,
359 0 : uno::UNO_QUERY );
360 :
361 0 : if( xFilterDialog.is() && xFilterProperties.is() )
362 : {
363 : uno::Reference<
364 : document::XImporter > xImporter(
365 0 : xFilterDialog, uno::UNO_QUERY );
366 0 : if( xImporter.is() )
367 0 : xImporter->setTargetDocument(
368 : uno::Reference< lang::XComponent >(
369 0 : rRequest.rModel, uno::UNO_QUERY ) );
370 :
371 0 : xFilterProperties->setPropertyValues(
372 0 : rRequest.rProperties );
373 :
374 0 : if( xFilterDialog->execute() )
375 : {
376 0 : xFilterOptions->setFilterOptions(
377 0 : xFilterProperties->getPropertyValues() );
378 0 : xFilterOptions->select();
379 0 : return;
380 0 : }
381 0 : }
382 : }
383 0 : break;
384 : }
385 0 : }
386 : }
387 0 : catch( container::NoSuchElementException& )
388 : {
389 : // the filter name is unknown
390 : }
391 0 : catch( uno::Exception& )
392 : {
393 : }
394 : }
395 :
396 0 : xAbort->select();
397 : }
398 :
399 : } // namespace
400 :
401 : bool
402 0 : UUIInteractionHelper::handleNoSuchFilterRequest(
403 : uno::Reference< task::XInteractionRequest > const & rRequest)
404 : SAL_THROW((uno::RuntimeException))
405 : {
406 0 : uno::Any aAnyRequest(rRequest->getRequest());
407 :
408 0 : document::NoSuchFilterRequest aNoSuchFilterRequest;
409 0 : if (aAnyRequest >>= aNoSuchFilterRequest)
410 : {
411 : handleNoSuchFilterRequest_(getParentProperty(),
412 : m_xContext,
413 : aNoSuchFilterRequest,
414 0 : rRequest->getContinuations());
415 0 : return true;
416 : }
417 0 : return false;
418 : }
419 :
420 : bool
421 0 : UUIInteractionHelper::handleAmbigousFilterRequest(
422 : uno::Reference< task::XInteractionRequest > const & rRequest)
423 : SAL_THROW((uno::RuntimeException))
424 : {
425 0 : uno::Any aAnyRequest(rRequest->getRequest());
426 :
427 0 : document::AmbigousFilterRequest aAmbigousFilterRequest;
428 0 : if (aAnyRequest >>= aAmbigousFilterRequest)
429 : {
430 : handleAmbigousFilterRequest_(getParentProperty(),
431 : m_xContext,
432 : aAmbigousFilterRequest,
433 0 : rRequest->getContinuations());
434 0 : return true;
435 : }
436 0 : return false;
437 : }
438 :
439 : bool
440 0 : UUIInteractionHelper::handleFilterOptionsRequest(
441 : uno::Reference< task::XInteractionRequest > const & rRequest)
442 : SAL_THROW((uno::RuntimeException))
443 : {
444 0 : uno::Any aAnyRequest(rRequest->getRequest());
445 :
446 0 : document::FilterOptionsRequest aFilterOptionsRequest;
447 0 : if (aAnyRequest >>= aFilterOptionsRequest)
448 : {
449 : handleFilterOptionsRequest_(m_xContext,
450 : aFilterOptionsRequest,
451 0 : rRequest->getContinuations());
452 0 : return true;
453 : }
454 0 : return false;
455 36 : }
456 :
457 :
458 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|