tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / scripting / source / provider / ProviderCache.cxx
blobb32ab099002e2c465fd6a46f197d59193155fa22
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 <comphelper/sequence.hxx>
21 #include <comphelper/diagnose_ex.hxx>
22 #include <sal/log.hxx>
24 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
25 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
26 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include "ProviderCache.hxx"
29 using namespace com::sun::star;
30 using namespace com::sun::star::uno;
31 using namespace com::sun::star::script;
33 namespace func_provider
36 ProviderCache::ProviderCache( const Reference< XComponentContext >& xContext, const Sequence< Any >& scriptContext ) : m_Sctx( scriptContext ), m_xContext( xContext )
38 // initialise m_hProviderDetailsCache with details of ScriptProviders
39 // will use createContentEnumeration
41 m_xMgr = m_xContext->getServiceManager();
42 ENSURE_OR_THROW( m_xMgr.is(), "ProviderCache::ProviderCache() failed to obtain ServiceManager" );
43 populateCache();
47 ProviderCache::ProviderCache( const Reference< XComponentContext >& xContext, const Sequence< Any >& scriptContext, const Sequence< OUString >& denyList ) : m_sDenyList( denyList ), m_Sctx( scriptContext ), m_xContext( xContext )
50 // initialise m_hProviderDetailsCache with details of ScriptProviders
51 // will use createContentEnumeration
53 m_xMgr = m_xContext->getServiceManager();
54 ENSURE_OR_THROW( m_xMgr.is(), "ProviderCache::ProviderCache() failed to obtain ServiceManager" );
55 populateCache();
58 ProviderCache::~ProviderCache()
62 Reference< provider::XScriptProvider >
63 ProviderCache::getProvider( const OUString& providerName )
65 std::scoped_lock aGuard( m_mutex );
66 Reference< provider::XScriptProvider > provider;
67 ProviderDetails_hash::iterator h_it = m_hProviderDetailsCache.find( providerName );
68 if ( h_it != m_hProviderDetailsCache.end() )
70 if ( h_it->second.provider.is() )
72 provider = h_it->second.provider;
74 else
76 // need to create provider and insert into hash
77 provider = createProvider( h_it->second );
80 return provider;
83 Sequence < Reference< provider::XScriptProvider > >
84 ProviderCache::getAllProviders()
86 // need to create providers that haven't been created already
87 // so check what providers exist and what ones don't
89 std::scoped_lock aGuard( m_mutex );
90 Sequence < Reference< provider::XScriptProvider > > providers ( m_hProviderDetailsCache.size() );
91 // should assert if size !> 0
92 if ( !m_hProviderDetailsCache.empty() )
94 auto pproviders = providers.getArray();
95 sal_Int32 providerIndex = 0;
96 for (auto& rDetail : m_hProviderDetailsCache)
98 Reference<provider::XScriptProvider> xScriptProvider = rDetail.second.provider;
99 if ( xScriptProvider.is() )
101 pproviders[ providerIndex++ ] = std::move(xScriptProvider);
103 else
105 // create provider
108 xScriptProvider = createProvider(rDetail.second);
109 pproviders[ providerIndex++ ] = std::move(xScriptProvider);
111 catch ( const Exception& )
113 DBG_UNHANDLED_EXCEPTION("scripting");
118 if (providerIndex < providers.getLength())
120 providers.realloc( providerIndex );
124 else
126 SAL_WARN("scripting", "no available providers, something very wrong!!!");
128 return providers;
131 void
132 ProviderCache::populateCache()
134 // wrong name in services.rdb
135 OUString serviceName;
136 std::scoped_lock aGuard( m_mutex );
139 Reference< container::XContentEnumerationAccess > xEnumAccess( m_xMgr, UNO_QUERY_THROW );
140 Reference< container::XEnumeration > xEnum = xEnumAccess->createContentEnumeration ( u"com.sun.star.script.provider.LanguageScriptProvider"_ustr );
142 while ( xEnum->hasMoreElements() )
145 Reference< lang::XSingleComponentFactory > factory( xEnum->nextElement(), UNO_QUERY_THROW );
146 Reference< lang::XServiceInfo > xServiceInfo( factory, UNO_QUERY_THROW );
148 const Sequence< OUString > serviceNames = xServiceInfo->getSupportedServiceNames();
150 if ( serviceNames.hasElements() )
152 auto pName = std::find_if(serviceNames.begin(), serviceNames.end(),
153 [this](const OUString& rName) {
154 return rName.startsWith("com.sun.star.script.provider.ScriptProviderFor")
155 && !isInDenyList(rName);
157 if (pName != serviceNames.end())
159 serviceName = *pName;
160 ProviderDetails details;
161 details.factory = std::move(factory);
162 m_hProviderDetailsCache[ serviceName ] = std::move(details);
167 catch ( const Exception &e )
169 css::uno::Any anyEx = cppu::getCaughtException();
170 throw css::lang::WrappedTargetRuntimeException(
171 "ProviderCache::populateCache: couldn't obtain XSingleComponentFactory for " + serviceName
172 + " " + e.Message,
173 nullptr, anyEx );
177 Reference< provider::XScriptProvider >
178 ProviderCache::createProvider( ProviderDetails& details )
182 details.provider.set(
183 details.factory->createInstanceWithArgumentsAndContext( m_Sctx, m_xContext ), UNO_QUERY_THROW );
185 catch ( const Exception& e )
187 css::uno::Any anyEx = cppu::getCaughtException();
188 throw css::lang::WrappedTargetRuntimeException(
189 "ProviderCache::createProvider() Error creating provider from factory. " + e.Message,
190 nullptr, anyEx );
193 return details.provider;
196 bool
197 ProviderCache::isInDenyList( const OUString& serviceName ) const
199 return comphelper::findValue(m_sDenyList, serviceName) != -1;
201 } //end namespace
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */