1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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>
63 #include <arrdecl.hxx>
69 unsigned SfxStack::nLevel
= 0;
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
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
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
)
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
);
167 !pFilter
->GetServiceName().equalsIgnoreAsciiCase(sServiceName
)
173 // If at least no default filter could be located - use any filter of this
180 for (const std::shared_ptr
<const SfxFilter
>& pCheckFilter
: *pFilterArr
)
182 if ( pCheckFilter
->GetServiceName().equalsIgnoreAsciiCase(sServiceName
) )
184 pFilter
= pCheckFilter
;
194 // Impl-Data is shared between all FilterMatchers of the same factory
195 class SfxFilterMatcher_Impl
199 mutable SfxFilterList_Impl
* pList
; // is created on demand
201 void InitForIterating() const;
203 explicit SfxFilterMatcher_Impl(OUString _aName
)
204 : aName(std::move(_aName
))
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
)
220 std::vector
<std::unique_ptr
<SfxFilterMatcher_Impl
> > aImplArr
;
221 int nSfxFilterMatcherCount
;
223 SfxFilterMatcher_Impl
& getSfxFilterMatcher_Impl(const OUString
&rName
)
227 if (!rName
.isEmpty())
228 aName
= SfxObjectShell::GetServiceNameFromFactory(rName
);
230 // find the impl-Data of any comparable FilterMatcher that was created
232 for (std::unique_ptr
<SfxFilterMatcher_Impl
>& aImpl
: aImplArr
)
233 if (aImpl
->aName
== aName
)
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
253 ++nSfxFilterMatcherCount
;
256 SfxFilterMatcher::~SfxFilterMatcher()
258 --nSfxFilterMatcherCount
;
259 if (nSfxFilterMatcherCount
== 0)
263 void SfxFilterMatcher_Impl::Update() const
267 // this List was already used
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
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
;
294 // global matcher: use global filter array
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
) )
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
);
323 sTypeName
= xDetection
->queryTypeByURL( rMedium
.GetURLObject().GetMainURL( INetURLObject::DecodeMechanism::NONE
) );
325 catch (uno::Exception
&)
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
;
368 // stream exists => deep detection (with preselection ... if possible)
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
);
378 aDescriptor
[utl::MediaDescriptor::PROP_REFERRER
]
382 if ( !m_rImpl
.aName
.isEmpty() )
383 aDescriptor
[utl::MediaDescriptor::PROP_DOCUMENTSERVICE
] <<= m_rImpl
.aName
;
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
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
);
428 rpFilter
= std::move(xNewFilter
);
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
,
450 xQueryBox
->set_default_response(RET_YES
);
452 short nRet
= xQueryBox
->run();
453 if ( nRet
== RET_YES
)
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
));
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
,
483 ErrCode
SfxFilterMatcher::DetectFilter( SfxMedium
& rMedium
, std::shared_ptr
<const SfxFilter
>& rpFilter
) const
486 Here the Filter selection box is pulled up. Otherwise GuessFilter
490 std::shared_ptr
<const SfxFilter
> pFilter
= rMedium
.GetFilter();
493 if( !IsFilterInstalled_Impl( pFilter
) )
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
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
)
513 if ( nErr
== ERRCODE_IO_PENDING
)
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
);
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' ) )
555 nErr
= pFilter
? ERRCODE_NONE
: ERRCODE_ABORT
;
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
)
565 static constexpr OUStringLiteral sTypeDetection
= u
"com.sun.star.document.TypeDetection";
566 uno::Reference
< container::XContainerQuery
> xTypeCFG( xServiceManager
->createInstance( sTypeDetection
), uno::UNO_QUERY
);
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
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
);
619 std::shared_ptr
<const SfxFilter
> SfxFilterMatcher::GetFilter4Mime( const OUString
& rMediaType
, SfxFilterFlags nMust
, SfxFilterFlags nDont
) const
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
)
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
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
)
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
667 if (OUString sExt
= ToUpper_Impl(rExt
); !sExt
.isEmpty())
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
))
689 // Use extension without dot!
690 OUString
sExt( rExt
);
691 if ( sExt
.startsWith(".") )
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
)
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
)
721 else if ( !pFirstFilter
)
722 pFirstFilter
= pFilter
;
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(": ");
734 SAL_WARN( "sfx.bastyp", "Old filter name used!");
735 aName
= rName
.subView( nIndex
+ 2 );
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() )
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
))
765 SfxFilterContainer::ReadSingleFilter_Impl( rName
, xTypeCFG
, xFilterCFG
, false );
769 SfxFilterList_Impl
* pList
= m_rImpl
.pList
;
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
))
783 IMPL_LINK( SfxFilterMatcher
, MaybeFileHdl_Impl
, OUString
*, pString
, bool )
785 std::shared_ptr
<const SfxFilter
> pFilter
= GetFilter4Extension( *pString
);
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
) )
820 std::shared_ptr
<const SfxFilter
> SfxFilterMatcherIter::First()
827 std::shared_ptr
<const SfxFilter
> SfxFilterMatcherIter::Next()
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
] );
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
,
866 OUString
sFilterName( rName
);
867 SfxFilterList_Impl
& rList
= *pFilterArr
;
868 uno::Sequence
< beans::PropertyValue
> lFilterProperties
;
872 aResult
= xFilterCFG
->getByName( sFilterName
);
874 catch( container::NoSuchElementException
& )
876 aResult
= uno::Any();
879 if( !(aResult
>>= lFilterProperties
) )
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 ;
890 OUString sHumanName
;
891 OUString sDefaultTemplate
;
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" )
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() )
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;
1010 pFilter
= std::make_shared
<SfxFilter
>( sFilterName
,
1019 rList
.push_back( pFilter
);
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
)
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() )
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." );
1107 // global filter array was modified, factory specific ones might need an
1109 for (const auto& aImpl
: aImplArr
)
1114 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */