merged tag ooo/DEV300_m102
[LibreOffice.git] / uui / source / iahndl-filter.cxx
blobc05762a82332ac164878954e941d95124a0ef153
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include "com/sun/star/beans/XPropertyAccess.hpp"
29 #include "com/sun/star/container/XContainerQuery.hpp"
30 #include "com/sun/star/container/XNameContainer.hpp"
31 #include "com/sun/star/document/AmbigousFilterRequest.hpp"
32 #include "com/sun/star/document/FilterOptionsRequest.hpp"
33 #include "com/sun/star/document/NoSuchFilterRequest.hpp"
34 #include "com/sun/star/document/XImporter.hpp"
35 #include "com/sun/star/document/XInteractionFilterOptions.hpp"
36 #include "com/sun/star/document/XInteractionFilterSelect.hpp"
37 #include "com/sun/star/lang/XMultiServiceFactory.hpp"
38 #include "com/sun/star/task/XInteractionAbort.hpp"
39 #include "com/sun/star/task/XInteractionRequest.hpp"
40 #include "com/sun/star/ui/dialogs/XExecutableDialog.hpp"
42 #include "vos/mutex.hxx"
43 #include "comphelper/sequenceashashmap.hxx"
44 #include "vcl/svapp.hxx"
46 #include "getcontinuations.hxx"
47 #include "fltdlg.hxx"
49 #include "iahndl.hxx"
51 using namespace com::sun::star;
53 namespace {
55 void
56 executeFilterDialog(
57 Window * pParent ,
58 rtl::OUString const & rURL ,
59 uui::FilterNameList const & rFilters,
60 rtl::OUString & rFilter )
61 SAL_THROW((uno::RuntimeException))
63 try
65 vos::OGuard aGuard(Application::GetSolarMutex());
67 std::auto_ptr< ResMgr > xManager(
68 ResMgr::CreateResMgr(CREATEVERSIONRESMGR_NAME(uui)));
70 std::auto_ptr< uui::FilterDialog > xDialog(
71 new uui::FilterDialog(pParent, xManager.get()));
73 xDialog->SetURL(rURL);
74 xDialog->ChangeFilters(&rFilters);
76 uui::FilterNameListPtr pSelected = rFilters.end();
77 if( xDialog->AskForFilter( pSelected ) )
79 rFilter = pSelected->sInternal;
82 catch (std::bad_alloc const &)
84 throw uno::RuntimeException(
85 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("out of memory")),
86 uno::Reference< uno::XInterface >());
90 void
91 handleNoSuchFilterRequest_(
92 Window * pParent,
93 uno::Reference< lang::XMultiServiceFactory > const & xServiceFactory,
94 document::NoSuchFilterRequest const & rRequest,
95 uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
96 rContinuations )
97 SAL_THROW((uno::RuntimeException))
99 uno::Reference< task::XInteractionAbort > xAbort;
100 uno::Reference< document::XInteractionFilterSelect > xFilterTransport;
101 getContinuations(rContinuations, &xAbort, &xFilterTransport);
103 // check neccessary ressources - if they don't exist - abort or
104 // break this operation
105 if (!xAbort.is())
106 return;
108 if (!xFilterTransport.is())
110 xAbort->select();
111 return;
114 uno::Reference< container::XContainerQuery > xFilterContainer;
117 xFilterContainer.set( xServiceFactory->createInstance(
118 ::rtl::OUString::createFromAscii(
119 "com.sun.star.document.FilterFactory") ),
120 uno::UNO_QUERY );
122 catch ( uno::Exception const & )
126 if (!xFilterContainer.is())
128 xAbort->select();
129 return;
132 uui::FilterNameList lNames;
134 // Note: We look for all filters here which match the following criteria:
135 // - they are import filters as minimum (of course they can
136 // support export too)
137 // - we don't show any filter which are flaged as "don't show it
138 // at the UI" or "they are not installed"
139 // - we ignore filters, which have not set any valid
140 // DocumentService (e.g. our pure graphic filters)
141 // - we show it sorted by her UIName's
142 // - We don't use the order flag or prefer default filters.
143 // (Because this list shows all filters and the user should
144 // find his filter vry easy by his UIName ...)
145 // - We use "_query_all" here ... but we filter graphic filters
146 // out by using DocumentService property later!
147 uno::Reference< container::XEnumeration > xFilters
148 = xFilterContainer->createSubSetEnumerationByQuery(
149 ::rtl::OUString::createFromAscii(
150 "_query_all:sort_prop=uiname:iflags=1:eflags=143360"));
151 while (xFilters->hasMoreElements())
155 ::comphelper::SequenceAsHashMap lProps(xFilters->nextElement());
156 uui::FilterNamePair aPair;
158 aPair.sInternal = lProps.getUnpackedValueOrDefault(
159 rtl::OUString::createFromAscii("Name"), ::rtl::OUString());
160 aPair.sUI = lProps.getUnpackedValueOrDefault(
161 rtl::OUString::createFromAscii("UIName"), ::rtl::OUString());
162 if ( (!aPair.sInternal.Len()) || (!aPair.sUI.Len() ) )
164 continue;
166 lNames.push_back( aPair );
168 catch(const uno::RuntimeException&)
170 throw;
172 catch(const uno::Exception&)
174 continue;
178 // no list available for showing
179 // -> abort operation
180 if (lNames.size()<1)
182 xAbort->select();
183 return;
186 // let the user select the right filter
187 rtl::OUString sSelectedFilter;
188 executeFilterDialog( pParent,
189 rRequest.URL,
190 lNames,
191 sSelectedFilter );
193 // If he doesn't select anyone
194 // -> abort operation
195 if (sSelectedFilter.getLength()<1)
197 xAbort->select();
198 return;
201 // otherwhise set it for return
202 xFilterTransport->setFilter( sSelectedFilter );
203 xFilterTransport->select();
206 void
207 handleAmbigousFilterRequest_(
208 Window * pParent,
209 uno::Reference< lang::XMultiServiceFactory > const & xServiceFactory,
210 document::AmbigousFilterRequest const & rRequest,
211 uno::Sequence<
212 uno::Reference<
213 task::XInteractionContinuation > > const & rContinuations)
214 SAL_THROW((uno::RuntimeException))
216 uno::Reference< task::XInteractionAbort > xAbort;
217 uno::Reference< document::XInteractionFilterSelect > xFilterTransport;
218 getContinuations(rContinuations, &xAbort, &xFilterTransport);
220 uui::FilterNameList lNames;
222 uno::Reference< container::XNameContainer > xFilterContainer;
225 xFilterContainer.set( xServiceFactory->createInstance(
226 ::rtl::OUString::createFromAscii(
227 "com.sun.star.document.FilterFactory") ),
228 uno::UNO_QUERY );
230 catch ( uno::Exception & )
234 if( xFilterContainer.is() )
236 uno::Any aPackedSet ;
237 uno::Sequence< beans::PropertyValue > lProps ;
238 sal_Int32 nStep ;
239 uui::FilterNamePair aPair ;
243 aPackedSet = xFilterContainer->getByName( rRequest.SelectedFilter );
245 catch(const container::NoSuchElementException&)
247 aPackedSet.clear();
249 aPackedSet >>= lProps;
250 for( nStep=0; nStep<lProps.getLength(); ++nStep )
252 if( lProps[nStep].Name.compareToAscii("UIName") == 0 )
254 ::rtl::OUString sTemp;
255 lProps[nStep].Value >>= sTemp;
256 aPair.sUI = sTemp;
257 aPair.sInternal = rRequest.SelectedFilter;
258 lNames.push_back( aPair );
259 break;
265 aPackedSet = xFilterContainer->getByName( rRequest.DetectedFilter );
267 catch(const container::NoSuchElementException&)
269 aPackedSet.clear();
271 aPackedSet >>= lProps;
272 for( nStep=0; nStep<lProps.getLength(); ++nStep )
274 if( lProps[nStep].Name.compareToAscii("UIName") == 0 )
276 ::rtl::OUString sTemp;
277 lProps[nStep].Value >>= sTemp;
278 aPair.sUI = sTemp;
279 aPair.sInternal = rRequest.DetectedFilter;
280 lNames.push_back( aPair );
281 break;
286 if( xAbort.is() && xFilterTransport.is() )
288 if( lNames.size() < 1 )
290 xAbort->select();
292 else
294 rtl::OUString sFilter;
295 executeFilterDialog( pParent,
296 rRequest.URL,
297 lNames,
298 sFilter );
300 if( sFilter.getLength() > 0 )
302 xFilterTransport->setFilter( sFilter );
303 xFilterTransport->select();
305 else
306 xAbort->select();
311 void
312 handleFilterOptionsRequest_(
313 uno::Reference< lang::XMultiServiceFactory > const & xServiceFactory,
314 document::FilterOptionsRequest const & rRequest,
315 uno::Sequence< uno::Reference< task::XInteractionContinuation > > const &
316 rContinuations)
317 SAL_THROW((uno::RuntimeException))
319 uno::Reference< task::XInteractionAbort > xAbort;
320 uno::Reference< document::XInteractionFilterOptions > xFilterOptions;
321 getContinuations(rContinuations, &xAbort, &xFilterOptions);
323 uno::Reference< container::XNameAccess > xFilterCFG;
326 xFilterCFG.set( xServiceFactory->createInstance(
327 ::rtl::OUString::createFromAscii(
328 "com.sun.star.document.FilterFactory" ) ),
329 uno::UNO_QUERY );
331 catch ( uno::Exception const & )
335 if( xFilterCFG.is() && rRequest.rProperties.getLength() )
339 ::rtl::OUString aFilterName;
340 sal_Int32 nPropCount = rRequest.rProperties.getLength();
341 for( sal_Int32 ind = 0; ind < nPropCount; ++ind )
343 rtl::OUString tmp = rRequest.rProperties[ind].Name;
344 if( rRequest.rProperties[ind].Name.equals(
345 ::rtl::OUString::createFromAscii("FilterName")) )
347 rRequest.rProperties[ind].Value >>= aFilterName;
348 break;
352 uno::Sequence < beans::PropertyValue > aProps;
353 if ( xFilterCFG->getByName( aFilterName ) >>= aProps )
355 sal_Int32 nPropertyCount = aProps.getLength();
356 for( sal_Int32 nProperty=0;
357 nProperty < nPropertyCount;
358 ++nProperty )
359 if( aProps[nProperty].Name.equals(
360 ::rtl::OUString::createFromAscii("UIComponent")) )
362 ::rtl::OUString aServiceName;
363 aProps[nProperty].Value >>= aServiceName;
364 if( aServiceName.getLength() )
366 uno::Reference<
367 ui::dialogs::XExecutableDialog > xFilterDialog(
368 xServiceFactory->createInstance(
369 aServiceName ),
370 uno::UNO_QUERY );
371 uno::Reference< beans::XPropertyAccess >
372 xFilterProperties( xFilterDialog,
373 uno::UNO_QUERY );
375 if( xFilterDialog.is() && xFilterProperties.is() )
377 uno::Reference<
378 document::XImporter > xImporter(
379 xFilterDialog, uno::UNO_QUERY );
380 if( xImporter.is() )
381 xImporter->setTargetDocument(
382 uno::Reference< lang::XComponent >(
383 rRequest.rModel, uno::UNO_QUERY ) );
385 xFilterProperties->setPropertyValues(
386 rRequest.rProperties );
388 if( xFilterDialog->execute() )
390 xFilterOptions->setFilterOptions(
391 xFilterProperties->getPropertyValues() );
392 xFilterOptions->select();
393 return;
397 break;
401 catch( container::NoSuchElementException& )
403 // the filter name is unknown
405 catch( uno::Exception& )
410 xAbort->select();
413 } // namespace
415 bool
416 UUIInteractionHelper::handleNoSuchFilterRequest(
417 uno::Reference< task::XInteractionRequest > const & rRequest)
418 SAL_THROW((uno::RuntimeException))
420 uno::Any aAnyRequest(rRequest->getRequest());
422 document::NoSuchFilterRequest aNoSuchFilterRequest;
423 if (aAnyRequest >>= aNoSuchFilterRequest)
425 handleNoSuchFilterRequest_(getParentProperty(),
426 m_xServiceFactory,
427 aNoSuchFilterRequest,
428 rRequest->getContinuations());
429 return true;
431 return false;
434 bool
435 UUIInteractionHelper::handleAmbigousFilterRequest(
436 uno::Reference< task::XInteractionRequest > const & rRequest)
437 SAL_THROW((uno::RuntimeException))
439 uno::Any aAnyRequest(rRequest->getRequest());
441 document::AmbigousFilterRequest aAmbigousFilterRequest;
442 if (aAnyRequest >>= aAmbigousFilterRequest)
444 handleAmbigousFilterRequest_(getParentProperty(),
445 m_xServiceFactory,
446 aAmbigousFilterRequest,
447 rRequest->getContinuations());
448 return true;
450 return false;
453 bool
454 UUIInteractionHelper::handleFilterOptionsRequest(
455 uno::Reference< task::XInteractionRequest > const & rRequest)
456 SAL_THROW((uno::RuntimeException))
458 uno::Any aAnyRequest(rRequest->getRequest());
460 document::FilterOptionsRequest aFilterOptionsRequest;
461 if (aAnyRequest >>= aFilterOptionsRequest)
463 handleFilterOptionsRequest_(m_xServiceFactory,
464 aFilterOptionsRequest,
465 rRequest->getContinuations());
466 return true;
468 return false;