tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / sfx2 / source / bastyp / fltfnc.cxx
blobf32a7ef368470b3b7da80387f30a69928c14305c
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 .
21 #include <com/sun/star/uno/Exception.hpp>
22 #include <com/sun/star/beans/PropertyValue.hpp>
23 #include <com/sun/star/beans/NamedValue.hpp>
24 #include <com/sun/star/container/XNameAccess.hpp>
25 #include <com/sun/star/container/XEnumeration.hpp>
26 #include <com/sun/star/document/XTypeDetection.hpp>
27 #include <com/sun/star/container/XContainerQuery.hpp>
28 #include <com/sun/star/io/XInputStream.hpp>
29 #include <com/sun/star/task/XInteractionHandler.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <comphelper/sequenceashashmap.hxx>
34 #include <sot/exchange.hxx>
35 #include <utility>
36 #include <vcl/svapp.hxx>
37 #include <vcl/weld.hxx>
38 #include <rtl/ustring.hxx>
39 #include <rtl/ustrbuf.hxx>
40 #include <sal/log.hxx>
41 #include <svl/stritem.hxx>
43 #include <comphelper/processfactory.hxx>
45 #include <sal/types.h>
46 #include <com/sun/star/uno/Reference.hxx>
47 #include <unotools/moduleoptions.hxx>
48 #include <unotools/mediadescriptor.hxx>
49 #include <tools/urlobj.hxx>
51 #include <unotools/syslocale.hxx>
52 #include <unotools/charclass.hxx>
54 #include <sfx2/docfilt.hxx>
55 #include <sfx2/fcontnr.hxx>
56 #include <sfxtypes.hxx>
57 #include <sfx2/docfile.hxx>
58 #include <sfx2/strings.hrc>
59 #include <sfx2/sfxresid.hxx>
60 #include <sfx2/objsh.hxx>
61 #include <sfx2/sfxsids.hrc>
62 #include "fltlst.hxx"
63 #include <arrdecl.hxx>
65 #include <vector>
66 #include <memory>
68 #if defined(DBG_UTIL)
69 unsigned SfxStack::nLevel = 0;
70 #endif
72 using namespace com::sun::star;
74 static SfxFilterList_Impl* pFilterArr = nullptr;
75 static bool bFirstRead = true;
77 static void CreateFilterArr()
79 static SfxFilterList_Impl theSfxFilterArray;
80 pFilterArr = &theSfxFilterArray;
81 static SfxFilterListener theSfxFilterListener;
84 static OUString ToUpper_Impl( const OUString &rStr )
86 return SvtSysLocale().GetCharClass().uppercase( rStr );
89 class SfxFilterContainer_Impl
91 public:
92 OUString aName;
94 explicit SfxFilterContainer_Impl( OUString _aName )
95 : aName(std::move( _aName ))
100 std::shared_ptr<const SfxFilter> SfxFilterContainer::GetFilter4EA(const OUString& rEA, SfxFilterFlags nMust, SfxFilterFlags nDont) const
102 SfxFilterMatcher aMatch(pImpl->aName);
103 return aMatch.GetFilter4EA(rEA, nMust, nDont);
106 std::shared_ptr<const SfxFilter> SfxFilterContainer::GetFilter4Extension(const OUString& rExt, SfxFilterFlags nMust, SfxFilterFlags nDont) const
108 SfxFilterMatcher aMatch(pImpl->aName);
109 return aMatch.GetFilter4Extension(rExt, nMust, nDont);
112 std::shared_ptr<const SfxFilter> SfxFilterContainer::GetFilter4FilterName(const OUString& rName, SfxFilterFlags nMust, SfxFilterFlags nDont) const
114 SfxFilterMatcher aMatch(pImpl->aName);
115 return aMatch.GetFilter4FilterName(rName, nMust, nDont);
118 std::shared_ptr<const SfxFilter> SfxFilterContainer::GetAnyFilter( SfxFilterFlags nMust, SfxFilterFlags nDont ) const
120 SfxFilterMatcher aMatch( pImpl->aName );
121 return aMatch.GetAnyFilter( nMust, nDont );
125 SfxFilterContainer::SfxFilterContainer( const OUString& rName )
126 : pImpl( new SfxFilterContainer_Impl( rName ) )
131 SfxFilterContainer::~SfxFilterContainer()
136 OUString const & SfxFilterContainer::GetName() const
138 return pImpl->aName;
141 std::shared_ptr<const SfxFilter> SfxFilterContainer::GetDefaultFilter_Impl( std::u16string_view rName )
143 // Try to find out the type of factory.
144 // Interpret given name as Service- and ShortName!
145 SvtModuleOptions aOpt;
146 SvtModuleOptions::EFactory eFactory = SvtModuleOptions::ClassifyFactoryByServiceName(rName);
147 if (eFactory == SvtModuleOptions::EFactory::UNKNOWN_FACTORY)
148 eFactory = SvtModuleOptions::ClassifyFactoryByShortName(rName);
150 // could not classify factory by its service nor by its short name.
151 // Must be an unknown factory! => return NULL
152 if (eFactory == SvtModuleOptions::EFactory::UNKNOWN_FACTORY)
153 return nullptr;
155 // For the following code we need some additional information.
156 const OUString& sServiceName = aOpt.GetFactoryName(eFactory);
157 OUString sDefaultFilter = aOpt.GetFactoryDefaultFilter(eFactory);
159 // Try to get the default filter. Don't forget to verify it.
160 // May the set default filter does not exists any longer or
161 // does not fit the given factory.
162 const SfxFilterMatcher aMatcher;
163 std::shared_ptr<const SfxFilter> pFilter = aMatcher.GetFilter4FilterName(sDefaultFilter);
165 if (
166 pFilter &&
167 !pFilter->GetServiceName().equalsIgnoreAsciiCase(sServiceName)
170 pFilter = nullptr;
173 // If at least no default filter could be located - use any filter of this
174 // factory.
175 if (!pFilter)
177 if ( bFirstRead )
178 ReadFilters_Impl();
180 for (const std::shared_ptr<const SfxFilter>& pCheckFilter : *pFilterArr)
182 if ( pCheckFilter->GetServiceName().equalsIgnoreAsciiCase(sServiceName) )
184 pFilter = pCheckFilter;
185 break;
190 return pFilter;
194 // Impl-Data is shared between all FilterMatchers of the same factory
195 class SfxFilterMatcher_Impl
197 public:
198 OUString aName;
199 mutable SfxFilterList_Impl* pList; // is created on demand
201 void InitForIterating() const;
202 void Update() const;
203 explicit SfxFilterMatcher_Impl(OUString _aName)
204 : aName(std::move(_aName))
205 , pList(nullptr)
208 ~SfxFilterMatcher_Impl()
210 // SfxFilterMatcher_Impl::InitForIterating() will set pList to
211 // either the global filter array matcher pFilterArr, or to
212 // a new SfxFilterList_Impl.
213 if (pList != pFilterArr)
214 delete pList;
218 namespace
220 std::vector<std::unique_ptr<SfxFilterMatcher_Impl> > aImplArr;
221 int nSfxFilterMatcherCount;
223 SfxFilterMatcher_Impl & getSfxFilterMatcher_Impl(const OUString &rName)
225 OUString aName;
227 if (!rName.isEmpty())
228 aName = SfxObjectShell::GetServiceNameFromFactory(rName);
230 // find the impl-Data of any comparable FilterMatcher that was created
231 // previously
232 for (std::unique_ptr<SfxFilterMatcher_Impl>& aImpl : aImplArr)
233 if (aImpl->aName == aName)
234 return *aImpl;
236 // first Matcher created for this factory
237 aImplArr.push_back(std::make_unique<SfxFilterMatcher_Impl>(aName));
238 return *aImplArr.back();
242 SfxFilterMatcher::SfxFilterMatcher( const OUString& rName )
243 : m_rImpl( getSfxFilterMatcher_Impl(rName) )
245 ++nSfxFilterMatcherCount;
248 SfxFilterMatcher::SfxFilterMatcher()
249 : m_rImpl( getSfxFilterMatcher_Impl(OUString()) )
251 // global FilterMatcher always uses global filter array (also created on
252 // demand)
253 ++nSfxFilterMatcherCount;
256 SfxFilterMatcher::~SfxFilterMatcher()
258 --nSfxFilterMatcherCount;
259 if (nSfxFilterMatcherCount == 0)
260 aImplArr.clear();
263 void SfxFilterMatcher_Impl::Update() const
265 if ( pList )
267 // this List was already used
268 pList->clear();
269 for (const std::shared_ptr<const SfxFilter>& pFilter : *pFilterArr)
271 if ( pFilter->GetServiceName() == aName )
272 pList->push_back( pFilter );
277 void SfxFilterMatcher_Impl::InitForIterating() const
279 if ( pList )
280 return;
282 if ( bFirstRead )
283 // global filter array has not been created yet
284 SfxFilterContainer::ReadFilters_Impl();
286 if ( !aName.isEmpty() )
288 // matcher of factory: use only filters of that document type
289 pList = new SfxFilterList_Impl;
290 Update();
292 else
294 // global matcher: use global filter array
295 pList = pFilterArr;
299 std::shared_ptr<const SfxFilter> SfxFilterMatcher::GetAnyFilter( SfxFilterFlags nMust, SfxFilterFlags nDont ) const
301 m_rImpl.InitForIterating();
302 for (const std::shared_ptr<const SfxFilter>& pFilter : *m_rImpl.pList)
304 SfxFilterFlags nFlags = pFilter->GetFilterFlags();
305 if ( (nFlags & nMust) == nMust && !(nFlags & nDont ) )
306 return pFilter;
309 return nullptr;
313 ErrCode SfxFilterMatcher::GuessFilterIgnoringContent(
314 SfxMedium const & rMedium,
315 std::shared_ptr<const SfxFilter>& rpFilter ) const
317 uno::Reference<document::XTypeDetection> xDetection(
318 comphelper::getProcessServiceFactory()->createInstance(u"com.sun.star.document.TypeDetection"_ustr), uno::UNO_QUERY);
320 OUString sTypeName;
323 sTypeName = xDetection->queryTypeByURL( rMedium.GetURLObject().GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
325 catch (uno::Exception&)
329 rpFilter = nullptr;
330 if ( !sTypeName.isEmpty() )
332 // make sure filter list is initialized
333 m_rImpl.InitForIterating();
334 rpFilter = GetFilter4EA( sTypeName );
337 return rpFilter ? ERRCODE_NONE : ERRCODE_ABORT;
341 ErrCode SfxFilterMatcher::GuessFilter( SfxMedium& rMedium, std::shared_ptr<const SfxFilter>& rpFilter, SfxFilterFlags nMust, SfxFilterFlags nDont ) const
343 return GuessFilterControlDefaultUI( rMedium, rpFilter, nMust, nDont );
347 ErrCode SfxFilterMatcher::GuessFilterControlDefaultUI( SfxMedium& rMedium, std::shared_ptr<const SfxFilter>& rpFilter, SfxFilterFlags nMust, SfxFilterFlags nDont ) const
349 std::shared_ptr<const SfxFilter> pOldFilter = rpFilter;
351 // no detection service -> nothing to do !
352 uno::Reference<document::XTypeDetection> xDetection(
353 comphelper::getProcessServiceFactory()->createInstance(u"com.sun.star.document.TypeDetection"_ustr), uno::UNO_QUERY);
355 if (!xDetection.is())
356 return ERRCODE_ABORT;
360 // open the stream one times only ...
361 // Otherwise it will be tried more than once and show the same interaction more than once ...
363 OUString sURL( rMedium.GetURLObject().GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
364 uno::Reference< io::XInputStream > xInStream = rMedium.GetInputStream();
365 OUString aFilterName;
366 OUString sTypeName;
368 // stream exists => deep detection (with preselection ... if possible)
369 if (xInStream.is())
371 utl::MediaDescriptor aDescriptor;
373 aDescriptor[utl::MediaDescriptor::PROP_URL ] <<= sURL;
374 aDescriptor[utl::MediaDescriptor::PROP_INPUTSTREAM ] <<= xInStream;
375 aDescriptor[utl::MediaDescriptor::PROP_INTERACTIONHANDLER] <<= rMedium.GetInteractionHandler();
376 SfxStringItem const * it = rMedium.GetItemSet().GetItem(SID_REFERER);
377 if (it != nullptr) {
378 aDescriptor[utl::MediaDescriptor::PROP_REFERRER]
379 <<= it->GetValue();
382 if ( !m_rImpl.aName.isEmpty() )
383 aDescriptor[utl::MediaDescriptor::PROP_DOCUMENTSERVICE] <<= m_rImpl.aName;
385 if ( pOldFilter )
387 aDescriptor[utl::MediaDescriptor::PROP_TYPENAME ] <<= pOldFilter->GetTypeName();
388 aDescriptor[utl::MediaDescriptor::PROP_FILTERNAME] <<= pOldFilter->GetFilterName();
391 uno::Sequence< beans::PropertyValue > lDescriptor = aDescriptor.getAsConstPropertyValueList();
392 sTypeName = xDetection->queryTypeByDescriptor(lDescriptor, true); // lDescriptor is used as In/Out param ... don't use aDescriptor.getAsConstPropertyValueList() directly!
394 for (const auto& rProp : lDescriptor)
396 if (rProp.Name == "FilterName")
397 // Type detection picked a preferred filter for this format.
398 aFilterName = rProp.Value.get<OUString>();
401 // no stream exists => try flat detection without preselection as fallback
402 else
403 sTypeName = xDetection->queryTypeByURL(sURL);
405 if (!sTypeName.isEmpty())
407 std::shared_ptr<const SfxFilter> xNewFilter;
408 if (!aFilterName.isEmpty())
409 // Type detection returned a suitable filter for this. Use it.
410 xNewFilter = SfxFilter::GetFilterByName(aFilterName);
412 // fdo#78742 respect requested document service if set
413 if (!xNewFilter || (!m_rImpl.aName.isEmpty()
414 && m_rImpl.aName != xNewFilter->GetServiceName()))
416 // detect filter by given type
417 // In case of this matcher is bound to a particular document type:
418 // If there is no acceptable type for this document at all, the type detection has possibly returned something else.
419 // The DocumentService property is only a preselection, and all preselections are considered as optional!
420 // This "wrong" type will be sorted out now because we match only allowed filters to the detected type
421 uno::Sequence< beans::NamedValue > lQuery { { u"Name"_ustr, css::uno::Any(sTypeName) } };
423 xNewFilter = GetFilterForProps(lQuery, nMust, nDont);
426 if (xNewFilter)
428 rpFilter = std::move(xNewFilter);
429 return ERRCODE_NONE;
433 catch (const uno::Exception&)
436 return ERRCODE_ABORT;
440 bool SfxFilterMatcher::IsFilterInstalled_Impl( const std::shared_ptr<const SfxFilter>& pFilter )
442 if ( pFilter->GetFilterFlags() & SfxFilterFlags::MUSTINSTALL )
444 // Here could a re-installation be offered
445 OUString aText( SfxResId(STR_FILTER_NOT_INSTALLED) );
446 aText = aText.replaceFirst( "$(FILTER)", pFilter->GetUIName() );
447 std::unique_ptr<weld::MessageDialog> xQueryBox(Application::CreateMessageDialog(nullptr,
448 VclMessageType::Question, VclButtonsType::YesNo,
449 aText));
450 xQueryBox->set_default_response(RET_YES);
452 short nRet = xQueryBox->run();
453 if ( nRet == RET_YES )
455 #ifdef DBG_UTIL
456 // Start Setup
457 std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(nullptr,
458 VclMessageType::Info, VclButtonsType::Ok,
459 u"Here should the Setup now be starting!"_ustr));
460 xInfoBox->run();
461 #endif
462 // Installation must still give feedback if it worked or not,
463 // then the Filterflag be deleted
466 return ( !(pFilter->GetFilterFlags() & SfxFilterFlags::MUSTINSTALL) );
468 else if ( pFilter->GetFilterFlags() & SfxFilterFlags::CONSULTSERVICE )
470 OUString aText( SfxResId(STR_FILTER_CONSULT_SERVICE) );
471 aText = aText.replaceFirst( "$(FILTER)", pFilter->GetUIName() );
472 std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(nullptr,
473 VclMessageType::Info, VclButtonsType::Ok,
474 aText));
475 xInfoBox->run();
476 return false;
478 else
479 return true;
483 ErrCode SfxFilterMatcher::DetectFilter( SfxMedium& rMedium, std::shared_ptr<const SfxFilter>& rpFilter ) const
484 /* [Description]
486 Here the Filter selection box is pulled up. Otherwise GuessFilter
490 std::shared_ptr<const SfxFilter> pFilter = rMedium.GetFilter();
491 if ( pFilter )
493 if( !IsFilterInstalled_Impl( pFilter ) )
494 pFilter = nullptr;
495 else
497 const SfxStringItem* pSalvageItem = rMedium.GetItemSet().GetItem(SID_DOC_SALVAGE, false);
498 if ( ( pFilter->GetFilterFlags() & SfxFilterFlags::PACKED ) && pSalvageItem )
499 // Salvage is always done without packing
500 pFilter = nullptr;
504 bool bPreview = rMedium.IsPreview_Impl();
505 const SfxStringItem* pReferer = rMedium.GetItemSet().GetItem(SID_REFERER, false);
506 if ( bPreview && rMedium.IsRemote() && ( !pReferer || !pReferer->GetValue().match("private:searchfolder:") ) )
507 return ERRCODE_ABORT;
509 ErrCode nErr = GuessFilter( rMedium, pFilter );
510 if ( nErr == ERRCODE_ABORT )
511 return nErr;
513 if ( nErr == ERRCODE_IO_PENDING )
515 rpFilter = pFilter;
516 return nErr;
519 if ( !pFilter )
521 std::shared_ptr<const SfxFilter> pInstallFilter;
523 // Now test the filter which are not installed (ErrCode is irrelevant)
524 GuessFilter( rMedium, pInstallFilter, SfxFilterFlags::IMPORT, SfxFilterFlags::CONSULTSERVICE );
525 if ( pInstallFilter )
527 if ( IsFilterInstalled_Impl( pInstallFilter ) )
529 // Maybe the filter was installed afterwards.
530 pFilter = std::move(pInstallFilter);
533 else
535 // Now test the filter, which first must be obtained by Star
536 // (ErrCode is irrelevant)
537 GuessFilter( rMedium, pInstallFilter, SfxFilterFlags::IMPORT, SfxFilterFlags::NONE );
538 if ( pInstallFilter )
539 IsFilterInstalled_Impl( pInstallFilter );
543 bool bHidden = bPreview;
544 const SfxStringItem* pFlags = rMedium.GetItemSet().GetItem(SID_OPTIONS, false);
545 if ( !bHidden && pFlags )
547 OUString aFlags( pFlags->GetValue() );
548 aFlags = aFlags.toAsciiUpperCase();
549 if( -1 != aFlags.indexOf( 'H' ) )
550 bHidden = true;
552 rpFilter = pFilter;
554 if ( bHidden )
555 nErr = pFilter ? ERRCODE_NONE : ERRCODE_ABORT;
556 return nErr;
559 std::shared_ptr<const SfxFilter> SfxFilterMatcher::GetFilterForProps( const css::uno::Sequence < beans::NamedValue >& aSeq, SfxFilterFlags nMust, SfxFilterFlags nDont ) const
561 uno::Reference< lang::XMultiServiceFactory > xServiceManager = ::comphelper::getProcessServiceFactory();
562 if( !xServiceManager )
563 return nullptr;
565 static constexpr OUStringLiteral sTypeDetection = u"com.sun.star.document.TypeDetection";
566 uno::Reference< container::XContainerQuery > xTypeCFG( xServiceManager->createInstance( sTypeDetection ), uno::UNO_QUERY );
567 if ( !xTypeCFG )
568 return nullptr;
570 // make query for all types matching the properties
571 uno::Reference < css::container::XEnumeration > xEnum = xTypeCFG->createSubSetEnumerationByProperties( aSeq );
572 uno::Sequence<beans::PropertyValue> aProps;
573 while ( xEnum->hasMoreElements() )
575 static constexpr OUStringLiteral sPreferredFilter = u"PreferredFilter";
576 static constexpr OUStringLiteral sName = u"Name";
578 xEnum->nextElement() >>= aProps;
579 OUString aValue, aName;
580 for( const auto & rPropVal : aProps)
582 if (rPropVal.Name == sPreferredFilter)
583 rPropVal.Value >>= aValue;
584 else if (rPropVal.Name == sName)
585 rPropVal.Value >>= aName;
588 // try to get the preferred filter (works without loading all filters!)
589 if ( !aValue.isEmpty() )
591 std::shared_ptr<const SfxFilter> pFilter = SfxFilter::GetFilterByName( aValue );
592 if ( !pFilter || (pFilter->GetFilterFlags() & nMust) != nMust || (pFilter->GetFilterFlags() & nDont ) )
593 // check for filter flags
594 // pFilter == 0: if preferred filter is a Writer filter, but Writer module is not installed
595 continue;
597 if ( !m_rImpl.aName.isEmpty() )
599 // if this is not the global FilterMatcher: check if filter matches the document type
600 if ( pFilter->GetServiceName() != m_rImpl.aName )
602 // preferred filter belongs to another document type; now we must search the filter
603 m_rImpl.InitForIterating();
604 pFilter = GetFilter4EA( aName, nMust, nDont );
605 if ( pFilter )
606 return pFilter;
608 else
609 return pFilter;
611 else
612 return pFilter;
616 return nullptr;
619 std::shared_ptr<const SfxFilter> SfxFilterMatcher::GetFilter4Mime( const OUString& rMediaType, SfxFilterFlags nMust, SfxFilterFlags nDont ) const
621 if ( m_rImpl.pList )
623 for (const std::shared_ptr<const SfxFilter>& pFilter : *m_rImpl.pList)
625 SfxFilterFlags nFlags = pFilter->GetFilterFlags();
626 if ( (nFlags & nMust) == nMust && !(nFlags & nDont ) && pFilter->GetMimeType() == rMediaType )
627 return pFilter;
630 return nullptr;
633 css::uno::Sequence < css::beans::NamedValue > aSeq { { u"MediaType"_ustr, css::uno::Any(rMediaType) } };
634 return GetFilterForProps( aSeq, nMust, nDont );
637 std::shared_ptr<const SfxFilter> SfxFilterMatcher::GetFilter4EA( const OUString& rType, SfxFilterFlags nMust, SfxFilterFlags nDont ) const
639 if ( m_rImpl.pList )
641 std::shared_ptr<const SfxFilter> pFirst;
642 for (const std::shared_ptr<const SfxFilter>& pFilter : *m_rImpl.pList)
644 SfxFilterFlags nFlags = pFilter->GetFilterFlags();
645 if ( (nFlags & nMust) == nMust && !(nFlags & nDont ) && pFilter->GetTypeName() == rType )
647 if (nFlags & SfxFilterFlags::PREFERED)
648 return pFilter;
649 if (!pFirst)
650 pFirst = pFilter;
653 if (pFirst)
654 return pFirst;
656 return nullptr;
659 css::uno::Sequence < css::beans::NamedValue > aSeq { { u"Name"_ustr, css::uno::Any(rType) } };
660 return GetFilterForProps( aSeq, nMust, nDont );
663 std::shared_ptr<const SfxFilter> SfxFilterMatcher::GetFilter4Extension( const OUString& rExt, SfxFilterFlags nMust, SfxFilterFlags nDont ) const
665 if ( m_rImpl.pList )
667 if (OUString sExt = ToUpper_Impl(rExt); !sExt.isEmpty())
669 if (sExt[0] != '.')
670 sExt = "." + sExt;
672 for (const std::shared_ptr<const SfxFilter>& pFilter : *m_rImpl.pList)
674 SfxFilterFlags nFlags = pFilter->GetFilterFlags();
675 if ((nFlags & nMust) == nMust && !(nFlags & nDont))
677 OUString sWildCard = ToUpper_Impl(pFilter->GetWildcard().getGlob());
679 WildCard aCheck(sWildCard, ';');
680 if (aCheck.Matches(sExt))
681 return pFilter;
686 return nullptr;
689 // Use extension without dot!
690 OUString sExt( rExt );
691 if ( sExt.startsWith(".") )
692 sExt = sExt.copy(1);
694 css::uno::Sequence < css::beans::NamedValue > aSeq
695 { { u"Extensions"_ustr, css::uno::Any(uno::Sequence < OUString > { sExt } ) } };
696 return GetFilterForProps( aSeq, nMust, nDont );
699 std::shared_ptr<const SfxFilter> SfxFilterMatcher::GetFilter4ClipBoardId( SotClipboardFormatId nId, SfxFilterFlags nMust, SfxFilterFlags nDont ) const
701 if (nId == SotClipboardFormatId::NONE)
702 return nullptr;
704 css::uno::Sequence < css::beans::NamedValue > aSeq
705 { { u"ClipboardFormat"_ustr, css::uno::Any(SotExchange::GetFormatName( nId )) } };
706 return GetFilterForProps( aSeq, nMust, nDont );
709 std::shared_ptr<const SfxFilter> SfxFilterMatcher::GetFilter4UIName( std::u16string_view rName, SfxFilterFlags nMust, SfxFilterFlags nDont ) const
711 m_rImpl.InitForIterating();
712 std::shared_ptr<const SfxFilter> pFirstFilter;
713 for (const std::shared_ptr<const SfxFilter>& pFilter : *m_rImpl.pList)
715 SfxFilterFlags nFlags = pFilter->GetFilterFlags();
716 if ( (nFlags & nMust) == nMust &&
717 !(nFlags & nDont ) && pFilter->GetUIName() == rName )
719 if ( pFilter->GetFilterFlags() & SfxFilterFlags::PREFERED )
720 return pFilter;
721 else if ( !pFirstFilter )
722 pFirstFilter = pFilter;
725 return pFirstFilter;
728 std::shared_ptr<const SfxFilter> SfxFilterMatcher::GetFilter4FilterName( const OUString& rName, SfxFilterFlags nMust, SfxFilterFlags nDont ) const
730 std::u16string_view aName( rName );
731 sal_Int32 nIndex = rName.indexOf(": ");
732 if ( nIndex != -1 )
734 SAL_WARN( "sfx.bastyp", "Old filter name used!");
735 aName = rName.subView( nIndex + 2 );
738 if ( bFirstRead )
740 uno::Reference< lang::XMultiServiceFactory > xServiceManager = ::comphelper::getProcessServiceFactory();
741 uno::Reference< container::XNameAccess > xFilterCFG ;
742 uno::Reference< container::XNameAccess > xTypeCFG ;
743 if( xServiceManager.is() )
745 static constexpr OUStringLiteral sFilterFactory = u"com.sun.star.document.FilterFactory";
746 static constexpr OUStringLiteral sTypeDetection = u"com.sun.star.document.TypeDetection";
747 xFilterCFG.set( xServiceManager->createInstance( sFilterFactory ), uno::UNO_QUERY );
748 xTypeCFG.set( xServiceManager->createInstance( sTypeDetection ), uno::UNO_QUERY );
751 if( xFilterCFG.is() && xTypeCFG.is() )
753 if ( !pFilterArr )
754 CreateFilterArr();
755 else
757 for (const std::shared_ptr<const SfxFilter>& pFilter : *pFilterArr)
759 SfxFilterFlags nFlags = pFilter->GetFilterFlags();
760 if ((nFlags & nMust) == nMust && !(nFlags & nDont) && pFilter->GetFilterName().equalsIgnoreAsciiCase(aName))
761 return pFilter;
765 SfxFilterContainer::ReadSingleFilter_Impl( rName, xTypeCFG, xFilterCFG, false );
769 SfxFilterList_Impl* pList = m_rImpl.pList;
770 if ( !pList )
771 pList = pFilterArr;
773 for (const std::shared_ptr<const SfxFilter>& pFilter : *pList)
775 SfxFilterFlags nFlags = pFilter->GetFilterFlags();
776 if ( (nFlags & nMust) == nMust && !(nFlags & nDont ) && pFilter->GetFilterName().equalsIgnoreAsciiCase(aName))
777 return pFilter;
780 return nullptr;
783 IMPL_LINK( SfxFilterMatcher, MaybeFileHdl_Impl, OUString*, pString, bool )
785 std::shared_ptr<const SfxFilter> pFilter = GetFilter4Extension( *pString );
786 return pFilter &&
787 !pFilter->GetWildcard().Matches(u"") &&
788 !pFilter->GetWildcard().Matches(u"*.*") &&
789 !pFilter->GetWildcard().Matches(u"*");
793 SfxFilterMatcherIter::SfxFilterMatcherIter(
794 const SfxFilterMatcher& rMatcher,
795 SfxFilterFlags nOrMaskP, SfxFilterFlags nAndMaskP )
796 : nOrMask( nOrMaskP ), nAndMask( nAndMaskP ),
797 nCurrent(0), m_rMatch(rMatcher.m_rImpl)
799 if( nOrMask == static_cast<SfxFilterFlags>(0xffff) ) //Due to faulty build on s
800 nOrMask = SfxFilterFlags::NONE;
801 m_rMatch.InitForIterating();
805 std::shared_ptr<const SfxFilter> SfxFilterMatcherIter::Find_Impl()
807 std::shared_ptr<const SfxFilter> pFilter;
808 while( nCurrent < m_rMatch.pList->size() )
810 pFilter = (*m_rMatch.pList)[nCurrent++];
811 SfxFilterFlags nFlags = pFilter->GetFilterFlags();
812 if( ((nFlags & nOrMask) == nOrMask ) && !(nFlags & nAndMask ) )
813 break;
814 pFilter = nullptr;
817 return pFilter;
820 std::shared_ptr<const SfxFilter> SfxFilterMatcherIter::First()
822 nCurrent = 0;
823 return Find_Impl();
827 std::shared_ptr<const SfxFilter> SfxFilterMatcherIter::Next()
829 return Find_Impl();
832 /*---------------------------------------------------------------
833 helper to build own formatted string from given stringlist by
834 using given separator
835 ---------------------------------------------------------------*/
836 static OUString implc_convertStringlistToString( const uno::Sequence< OUString >& lList ,
837 sal_Unicode cSeparator,
838 std::u16string_view sPrefix )
840 OUStringBuffer sString ( 1000 ) ;
841 sal_Int32 nCount = lList.getLength();
842 sal_Int32 nItem = 0 ;
843 for( nItem=0; nItem<nCount; ++nItem )
845 if( !sPrefix.empty() )
847 sString.append( sPrefix );
849 sString.append( lList[nItem] );
850 if( nItem+1<nCount )
852 sString.append( cSeparator );
855 return sString.makeStringAndClear();
859 void SfxFilterContainer::ReadSingleFilter_Impl(
860 const OUString& rName,
861 const uno::Reference< container::XNameAccess >& xTypeCFG,
862 const uno::Reference< container::XNameAccess >& xFilterCFG,
863 bool bUpdate
866 OUString sFilterName( rName );
867 SfxFilterList_Impl& rList = *pFilterArr;
868 uno::Sequence< beans::PropertyValue > lFilterProperties;
869 uno::Any aResult;
872 aResult = xFilterCFG->getByName( sFilterName );
874 catch( container::NoSuchElementException& )
876 aResult = uno::Any();
879 if( !(aResult >>= lFilterProperties) )
880 return;
882 // collect information to add filter to container
883 // (attention: some information aren't available on filter directly ... you must search for corresponding type too!)
884 SfxFilterFlags nFlags = SfxFilterFlags::NONE;
885 SotClipboardFormatId nClipboardId = SotClipboardFormatId::NONE;
886 sal_Int32 nFormatVersion = 0 ;
887 OUString sMimeType ;
888 OUString sType ;
889 OUString sUIName ;
890 OUString sHumanName ;
891 OUString sDefaultTemplate ;
892 OUString sUserData ;
893 OUString sExtension ;
894 OUString sServiceName ;
895 bool bEnabled = true ;
897 // first get directly available properties
898 for (const auto& rFilterProperty : lFilterProperties)
900 if ( rFilterProperty.Name == "FileFormatVersion" )
902 rFilterProperty.Value >>= nFormatVersion;
904 else if ( rFilterProperty.Name == "TemplateName" )
906 rFilterProperty.Value >>= sDefaultTemplate;
908 else if ( rFilterProperty.Name == "Flags" )
910 sal_Int32 nTmp(0);
911 rFilterProperty.Value >>= nTmp;
912 assert((nTmp & ~o3tl::typed_flags<SfxFilterFlags>::mask) == 0);
913 nFlags = static_cast<SfxFilterFlags>(nTmp);
915 else if ( rFilterProperty.Name == "UIName" )
917 rFilterProperty.Value >>= sUIName;
919 else if ( rFilterProperty.Name == "UserData" )
921 uno::Sequence< OUString > lUserData;
922 rFilterProperty.Value >>= lUserData;
923 sUserData = implc_convertStringlistToString( lUserData, ',', u"" );
925 else if ( rFilterProperty.Name == "DocumentService" )
927 rFilterProperty.Value >>= sServiceName;
929 else if (rFilterProperty.Name == "ExportExtension")
931 // Extension preferred by the filter. This takes precedence
932 // over those that are given in the file format type.
933 rFilterProperty.Value >>= sExtension;
934 sExtension = "*." + sExtension;
936 else if ( rFilterProperty.Name == "Type" )
938 rFilterProperty.Value >>= sType;
939 // Try to get filter .. but look for any exceptions!
940 // May be filter was deleted by another thread ...
943 aResult = xTypeCFG->getByName( sType );
945 catch (const container::NoSuchElementException&)
947 aResult = uno::Any();
950 uno::Sequence< beans::PropertyValue > lTypeProperties;
951 if( aResult >>= lTypeProperties )
953 // get indirect available properties then (types)
954 for (const auto& rTypeProperty : lTypeProperties)
956 if ( rTypeProperty.Name == "ClipboardFormat" )
958 rTypeProperty.Value >>= sHumanName;
960 else if ( rTypeProperty.Name == "MediaType" )
962 rTypeProperty.Value >>= sMimeType;
964 else if ( rTypeProperty.Name == "Extensions" )
966 if (sExtension.isEmpty())
968 uno::Sequence< OUString > lExtensions;
969 rTypeProperty.Value >>= lExtensions;
970 sExtension = implc_convertStringlistToString( lExtensions, ';', u"*." );
976 else if ( rFilterProperty.Name == "Enabled" )
978 rFilterProperty.Value >>= bEnabled;
983 if ( sServiceName.isEmpty() )
984 return;
986 // old formats are found ... using HumanPresentableName!
987 if( !sHumanName.isEmpty() )
989 nClipboardId = SotExchange::RegisterFormatName( sHumanName );
991 // For external filters ignore clipboard IDs
992 if(nFlags & SfxFilterFlags::STARONEFILTER)
994 nClipboardId = SotClipboardFormatId::NONE;
997 // register SfxFilter
998 // first erase module name from old filter names!
999 // e.g: "scalc: DIF" => "DIF"
1000 sal_Int32 nStartRealName = sFilterName.indexOf( ": " );
1001 if( nStartRealName != -1 )
1003 SAL_WARN( "sfx.bastyp", "Old format, not supported!");
1004 sFilterName = sFilterName.copy( nStartRealName+2 );
1007 std::shared_ptr<const SfxFilter> pFilter = bUpdate ? SfxFilter::GetFilterByName( sFilterName ) : nullptr;
1008 if (!pFilter)
1010 pFilter = std::make_shared<SfxFilter>( sFilterName ,
1011 sExtension ,
1012 nFlags ,
1013 nClipboardId ,
1014 sType ,
1015 sMimeType ,
1016 sUserData ,
1017 sServiceName ,
1018 bEnabled );
1019 rList.push_back( pFilter );
1021 else
1023 SfxFilter* pFilt = const_cast<SfxFilter*>(pFilter.get());
1024 pFilt->maFilterName = sFilterName;
1025 pFilt->aWildCard = WildCard(sExtension, ';');
1026 pFilt->nFormatType = nFlags;
1027 pFilt->lFormat = nClipboardId;
1028 pFilt->aTypeName = sType;
1029 pFilt->aMimeType = sMimeType;
1030 pFilt->aUserData = sUserData;
1031 pFilt->aServiceName = sServiceName;
1032 pFilt->mbEnabled = bEnabled;
1035 SfxFilter* pFilt = const_cast<SfxFilter*>(pFilter.get());
1037 // Don't forget to set right UIName!
1038 // Otherwise internal name is used as fallback ...
1039 pFilt->SetUIName( sUIName );
1040 pFilt->SetDefaultTemplate( sDefaultTemplate );
1041 if( nFormatVersion )
1043 pFilt->SetVersion( nFormatVersion );
1047 void SfxFilterContainer::ReadFilters_Impl( bool bUpdate )
1049 if ( !pFilterArr )
1051 CreateFilterArr();
1052 assert(pFilterArr);
1055 bFirstRead = false;
1056 SfxFilterList_Impl& rList = *pFilterArr;
1060 // get the FilterFactory service to access the registered filters ... and types!
1061 uno::Reference< lang::XMultiServiceFactory > xServiceManager = ::comphelper::getProcessServiceFactory();
1062 uno::Reference< container::XNameAccess > xFilterCFG ;
1063 uno::Reference< container::XNameAccess > xTypeCFG ;
1064 if( xServiceManager.is() )
1066 xFilterCFG.set( xServiceManager->createInstance( u"com.sun.star.document.FilterFactory"_ustr ), uno::UNO_QUERY );
1067 xTypeCFG.set( xServiceManager->createInstance( u"com.sun.star.document.TypeDetection"_ustr ), uno::UNO_QUERY );
1070 if( xFilterCFG.is() && xTypeCFG.is() )
1072 // select right query to get right set of filters for search module
1073 const uno::Sequence< OUString > lFilterNames = xFilterCFG->getElementNames();
1074 if ( lFilterNames.hasElements() )
1076 // If list of filters already exist ...
1077 // ReadExternalFilters must work in update mode.
1078 // Best way seems to mark all filters NOT_INSTALLED
1079 // and change it back for all valid filters afterwards.
1080 if( !rList.empty() )
1082 bUpdate = true;
1083 for (const std::shared_ptr<const SfxFilter>& pFilter : rList)
1085 SfxFilter* pNonConstFilter = const_cast<SfxFilter*>(pFilter.get());
1086 pNonConstFilter->nFormatType |= SFX_FILTER_NOTINSTALLED;
1090 // get all properties of filters ... put it into the filter container
1091 for( const OUString& sFilterName : lFilterNames )
1093 // Try to get filter .. but look for any exceptions!
1094 // May be filter was deleted by another thread ...
1095 ReadSingleFilter_Impl( sFilterName, xTypeCFG, xFilterCFG, bUpdate );
1100 catch(const uno::Exception&)
1102 SAL_WARN( "sfx.bastyp", "SfxFilterContainer::ReadFilter()\nException detected. Possible not all filters could be cached." );
1105 if ( bUpdate )
1107 // global filter array was modified, factory specific ones might need an
1108 // update too
1109 for (const auto& aImpl : aImplArr)
1110 aImpl->Update();
1114 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */