tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sfx2 / source / doc / docfac.cxx
blob3887e153eb6c3610053cd2bb539451ef8b1915ee
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 <com/sun/star/container/XNameAccess.hpp>
21 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
22 #include <com/sun/star/document/XTypeDetection.hpp>
23 #include <com/sun/star/frame/ModuleManager.hpp>
24 #include <com/sun/star/frame/XLoadable.hpp>
25 #include <com/sun/star/frame/XStorable.hpp>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <comphelper/processfactory.hxx>
28 #include <comphelper/propertyvalue.hxx>
29 #include <unotools/moduleoptions.hxx>
30 #include <comphelper/sequenceashashmap.hxx>
31 #include <comphelper/configurationhelper.hxx>
33 #include <sfx2/docfilt.hxx>
34 #include <sfx2/docfac.hxx>
35 #include <sfx2/viewfac.hxx>
36 #include <sfx2/fcontnr.hxx>
37 #include <sfx2/module.hxx>
38 #include "syspath.hxx"
39 #include <osl/file.hxx>
40 #include <osl/security.hxx>
42 #include <sal/log.hxx>
43 #include <tools/debug.hxx>
44 #include <tools/globname.hxx>
46 #include <memory>
47 #include <utility>
49 using namespace ::com::sun::star;
52 struct SfxObjectFactory_Impl
54 std::vector<SfxViewFactory*> aViewFactoryArr;// List of <SfxViewFactory>s
55 OUString aServiceName;
56 SfxFilterContainer* pFilterContainer;
57 SfxModule* pModule;
58 SvGlobalName aClassName;
60 SfxObjectFactory_Impl() :
61 pFilterContainer ( nullptr ),
62 pModule ( nullptr )
66 SfxFilterContainer* SfxObjectFactory::GetFilterContainer() const
68 return pImpl->pFilterContainer;
71 SfxObjectFactory::SfxObjectFactory
73 const SvGlobalName& rName,
74 OUString sName
75 ) : m_sFactoryName(std::move( sName )),
76 pImpl( new SfxObjectFactory_Impl )
78 pImpl->pFilterContainer = new SfxFilterContainer( m_sFactoryName );
79 pImpl->aClassName = rName;
82 SfxObjectFactory::~SfxObjectFactory()
84 delete pImpl->pFilterContainer;
88 void SfxObjectFactory::RegisterViewFactory
90 SfxViewFactory &rFactory
93 #if OSL_DEBUG_LEVEL > 0
95 const OUString sViewName( rFactory.GetAPIViewName() );
96 for (auto const& viewFactory : pImpl->aViewFactoryArr)
98 if ( viewFactory->GetAPIViewName() != sViewName )
99 continue;
100 SAL_WARN( "sfx", "SfxObjectFactory::RegisterViewFactory: duplicate view name: " << sViewName );
101 break;
104 #endif
105 auto it = std::find_if(pImpl->aViewFactoryArr.begin(), pImpl->aViewFactoryArr.end(),
106 [&rFactory](SfxViewFactory* pFactory) { return pFactory->GetOrdinal() > rFactory.GetOrdinal(); });
107 pImpl->aViewFactoryArr.insert(it, &rFactory);
111 sal_uInt16 SfxObjectFactory::GetViewFactoryCount() const
113 return pImpl->aViewFactoryArr.size();
117 SfxViewFactory& SfxObjectFactory::GetViewFactory(sal_uInt16 i) const
119 return *pImpl->aViewFactoryArr[i];
123 SfxModule* SfxObjectFactory::GetModule() const
125 return pImpl->pModule;
128 void SfxObjectFactory::SetModule_Impl( SfxModule *pMod )
130 pImpl->pModule = pMod;
133 void SfxObjectFactory::SetSystemTemplate( const OUString& rServiceName, const OUString& rTemplateName )
135 static const int nMaxPathSize = 16000;
137 const OUString sConfPath = "Office/Factories/" + rServiceName;
138 static constexpr OUString PROP_DEF_TEMPL_CHANGED
139 = u"ooSetupFactorySystemDefaultTemplateChanged"_ustr;
141 static const char DEF_TPL_STR[] = "/soffice.";
143 OUString sUserTemplateURL;
144 OUString sPath;
145 sal_Unicode aPathBuffer[nMaxPathSize];
146 if ( SystemPath::GetUserTemplateLocation( aPathBuffer, nMaxPathSize ))
147 sPath = OUString( aPathBuffer );
148 osl::FileBase::getFileURLFromSystemPath( sPath, sUserTemplateURL );
150 if ( sUserTemplateURL.isEmpty())
151 return;
155 uno::Reference< lang::XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
156 uno::Reference< uno::XInterface > xConfig = ::comphelper::ConfigurationHelper::openConfig(
157 ::comphelper::getProcessComponentContext(), u"/org.openoffice.Setup"_ustr, ::comphelper::EConfigurationModes::Standard );
159 OUString aActualFilter;
160 ::comphelper::ConfigurationHelper::readRelativeKey( xConfig, sConfPath, u"ooSetupFactoryActualFilter"_ustr ) >>= aActualFilter;
161 bool bChanged(false);
162 ::comphelper::ConfigurationHelper::readRelativeKey( xConfig, sConfPath, PROP_DEF_TEMPL_CHANGED ) >>= bChanged;
164 uno::Reference< container::XNameAccess > xFilterFactory(
165 xFactory->createInstance( u"com.sun.star.document.FilterFactory"_ustr ), uno::UNO_QUERY_THROW );
166 uno::Reference< container::XNameAccess > xTypeDetection(
167 xFactory->createInstance( u"com.sun.star.document.TypeDetection"_ustr ), uno::UNO_QUERY_THROW );
169 OUString aActualFilterTypeName;
170 uno::Sequence< beans::PropertyValue > aActuralFilterData;
171 xFilterFactory->getByName( aActualFilter ) >>= aActuralFilterData;
172 for (const auto& rProp : aActuralFilterData)
173 if ( rProp.Name == "Type" )
174 rProp.Value >>= aActualFilterTypeName;
175 ::comphelper::SequenceAsHashMap aProps1( xTypeDetection->getByName( aActualFilterTypeName ) );
176 uno::Sequence< OUString > aAllExt =
177 aProps1.getUnpackedValueOrDefault(u"Extensions"_ustr, uno::Sequence< OUString >() );
178 //To-do: check if aAllExt is empty first
179 const OUString aExt = DEF_TPL_STR + aAllExt[0];
181 sUserTemplateURL += aExt;
183 uno::Reference<ucb::XSimpleFileAccess3> xSimpleFileAccess(
184 ucb::SimpleFileAccess::create( ::comphelper::getComponentContext(xFactory) ) );
186 OUString aBackupURL;
187 ::osl::Security().getConfigDir(aBackupURL);
188 aBackupURL += "/temp";
190 if ( !xSimpleFileAccess->exists( aBackupURL ) )
191 xSimpleFileAccess->createFolder( aBackupURL );
193 aBackupURL += aExt;
195 if ( !rTemplateName.isEmpty() )
197 if ( xSimpleFileAccess->exists( sUserTemplateURL ) && !bChanged )
198 xSimpleFileAccess->copy( sUserTemplateURL, aBackupURL );
200 uno::Reference< document::XTypeDetection > xTypeDetector( xTypeDetection, uno::UNO_QUERY );
201 ::comphelper::SequenceAsHashMap aProps2( xTypeDetection->getByName( xTypeDetector->queryTypeByURL( rTemplateName ) ) );
202 OUString aFilterName =
203 aProps2.getUnpackedValueOrDefault(u"PreferredFilter"_ustr, OUString() );
205 uno::Sequence< beans::PropertyValue > aArgs{
206 comphelper::makePropertyValue(u"FilterName"_ustr, aFilterName),
207 comphelper::makePropertyValue(u"AsTemplate"_ustr, true),
208 comphelper::makePropertyValue(u"URL"_ustr, rTemplateName)
211 uno::Reference< frame::XLoadable > xLoadable( xFactory->createInstance( rServiceName ), uno::UNO_QUERY );
212 xLoadable->load( aArgs );
214 aArgs.realloc( 2 );
215 auto pArgs = aArgs.getArray();
216 pArgs[1].Name = "Overwrite";
217 pArgs[1].Value <<= true;
219 uno::Reference< frame::XStorable > xStorable( xLoadable, uno::UNO_QUERY );
220 xStorable->storeToURL( sUserTemplateURL, aArgs );
221 ::comphelper::ConfigurationHelper::writeRelativeKey( xConfig, sConfPath, PROP_DEF_TEMPL_CHANGED, uno::Any( true ));
222 ::comphelper::ConfigurationHelper::flush( xConfig );
224 else
226 DBG_ASSERT( bChanged, "invalid ooSetupFactorySystemDefaultTemplateChanged value!" );
228 xSimpleFileAccess->copy( aBackupURL, sUserTemplateURL );
229 xSimpleFileAccess->kill( aBackupURL );
230 ::comphelper::ConfigurationHelper::writeRelativeKey( xConfig, sConfPath, PROP_DEF_TEMPL_CHANGED, uno::Any( false ));
231 ::comphelper::ConfigurationHelper::flush( xConfig );
234 catch(const uno::Exception&)
239 void SfxObjectFactory::SetStandardTemplate( const OUString& rServiceName, const OUString& rTemplate )
241 SvtModuleOptions::EFactory eFac = SvtModuleOptions::ClassifyFactoryByServiceName(rServiceName);
242 if (eFac == SvtModuleOptions::EFactory::UNKNOWN_FACTORY)
243 eFac = SvtModuleOptions::ClassifyFactoryByShortName(rServiceName);
244 if (eFac != SvtModuleOptions::EFactory::UNKNOWN_FACTORY)
246 SetSystemTemplate( rServiceName, rTemplate );
247 SvtModuleOptions().SetFactoryStandardTemplate(eFac, rTemplate);
251 OUString SfxObjectFactory::GetStandardTemplate( std::u16string_view rServiceName )
253 SvtModuleOptions::EFactory eFac = SvtModuleOptions::ClassifyFactoryByServiceName(rServiceName);
254 if (eFac == SvtModuleOptions::EFactory::UNKNOWN_FACTORY)
255 eFac = SvtModuleOptions::ClassifyFactoryByShortName(rServiceName);
257 if (eFac != SvtModuleOptions::EFactory::UNKNOWN_FACTORY)
258 return SvtModuleOptions().GetFactoryStandardTemplate(eFac);
260 return OUString();
263 std::shared_ptr<const SfxFilter> SfxObjectFactory::GetTemplateFilter() const
265 sal_uInt16 nVersion=0;
266 SfxFilterMatcher aMatcher ( m_sFactoryName );
267 SfxFilterMatcherIter aIter( aMatcher );
268 std::shared_ptr<const SfxFilter> pFilter;
269 std::shared_ptr<const SfxFilter> pTemp = aIter.First();
270 while ( pTemp )
272 if( pTemp->IsOwnFormat() && pTemp->IsOwnTemplateFormat() && ( pTemp->GetVersion() > nVersion ) )
274 pFilter = pTemp;
275 nVersion = static_cast<sal_uInt16>(pTemp->GetVersion());
278 pTemp = aIter.Next();
281 return pFilter;
284 void SfxObjectFactory::SetDocumentServiceName( const OUString& rServiceName )
286 pImpl->aServiceName = rServiceName;
289 const OUString& SfxObjectFactory::GetDocumentServiceName() const
291 return pImpl->aServiceName;
294 const SvGlobalName& SfxObjectFactory::GetClassId() const
296 return pImpl->aClassName;
299 OUString SfxObjectFactory::GetFactoryURL() const
301 return "private:factory/" + m_sFactoryName;
304 OUString SfxObjectFactory::GetModuleName() const
308 const css::uno::Reference< css::uno::XComponentContext >& xContext = ::comphelper::getProcessComponentContext();
310 css::uno::Reference< css::frame::XModuleManager2 > xModuleManager(
311 css::frame::ModuleManager::create(xContext));
313 ::comphelper::SequenceAsHashMap aPropSet( xModuleManager->getByName(GetDocumentServiceName()) );
314 return aPropSet.getUnpackedValueOrDefault(u"ooSetupFactoryUIName"_ustr, OUString());
316 catch(const css::uno::RuntimeException&)
318 throw;
320 catch(const css::uno::Exception&)
324 return OUString();
328 sal_uInt16 SfxObjectFactory::GetViewNo_Impl( const SfxInterfaceId i_nViewId, const sal_uInt16 i_nFallback ) const
330 for ( sal_uInt16 curViewNo = 0; curViewNo < GetViewFactoryCount(); ++curViewNo )
332 const SfxInterfaceId curViewId = GetViewFactory( curViewNo ).GetOrdinal();
333 if ( i_nViewId == curViewId )
334 return curViewNo;
336 return i_nFallback;
339 SfxViewFactory* SfxObjectFactory::GetViewFactoryByViewName( std::u16string_view i_rViewName ) const
341 for ( sal_uInt16 nViewNo = 0;
342 nViewNo < GetViewFactoryCount();
343 ++nViewNo
346 SfxViewFactory& rViewFac( GetViewFactory( nViewNo ) );
347 if ( ( rViewFac.GetAPIViewName() == i_rViewName )
348 || ( rViewFac.GetLegacyViewName() == i_rViewName )
350 return &rViewFac;
352 return nullptr;
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */