LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / appl / module.cxx
blobfe68c5e57edacd62c952639812365550759240da
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 <sfx2/module.hxx>
21 #include <sfx2/app.hxx>
22 #include <sfx2/msgpool.hxx>
23 #include <sfx2/tbxctrl.hxx>
24 #include <sfx2/stbitem.hxx>
25 #include <sfx2/childwin.hxx>
26 #include <sfx2/docfac.hxx>
27 #include <sfx2/objface.hxx>
28 #include <sfx2/viewfrm.hxx>
29 #include <sfx2/tabdlg.hxx>
30 #include <sfx2/sfxsids.hrc>
31 #include <svl/intitem.hxx>
32 #include <tools/diagnose_ex.h>
33 #include <unotools/resmgr.hxx>
34 #include <sal/log.hxx>
36 #define ShellClass_SfxModule
37 #include <sfxslots.hxx>
38 #include <optional>
40 class SfxModule_Impl
42 public:
44 std::optional<SfxSlotPool> pSlotPool;
45 std::vector<SfxTbxCtrlFactory> maTbxCtrlFactories;
46 std::vector<SfxStbCtrlFactory> maStbCtrlFactories;
47 std::vector<SfxChildWinFactory> maFactories;
48 OString maResName;
50 SfxModule_Impl();
51 ~SfxModule_Impl();
54 SfxModule_Impl::SfxModule_Impl()
58 SfxModule_Impl::~SfxModule_Impl()
60 pSlotPool.reset();
61 maTbxCtrlFactories.clear();
62 maStbCtrlFactories.clear();
65 SFX_IMPL_SUPERCLASS_INTERFACE(SfxModule, SfxShell)
67 SfxModule::SfxModule(const OString& rResName, std::initializer_list<SfxObjectFactory*> pFactoryList)
68 : pImpl(nullptr)
70 Construct_Impl(rResName);
71 for (auto pFactory : pFactoryList)
73 if (pFactory)
74 pFactory->SetModule_Impl( this );
78 void SfxModule::Construct_Impl(const OString& rResName)
80 SfxApplication *pApp = SfxApplication::GetOrCreate();
81 pImpl = new SfxModule_Impl;
82 pImpl->pSlotPool.emplace(&pApp->GetAppSlotPool_Impl());
83 pImpl->maResName = rResName;
85 SetPool( &pApp->GetPool() );
88 SfxModule::~SfxModule()
90 //TODO how to silence useuniqueptr
91 if (true)
93 delete pImpl;
97 std::locale SfxModule::GetResLocale() const
99 return Translate::Create(pImpl->maResName);
102 SfxSlotPool* SfxModule::GetSlotPool() const
104 return &*pImpl->pSlotPool;
108 void SfxModule::RegisterChildWindow(const SfxChildWinFactory& rFact)
110 DBG_ASSERT( pImpl, "No real Module!" );
112 for (size_t nFactory=0; nFactory<pImpl->maFactories.size(); ++nFactory)
114 if (rFact.nId == pImpl->maFactories[nFactory].nId)
116 pImpl->maFactories.erase( pImpl->maFactories.begin() + nFactory );
117 SAL_WARN("sfx.appl", "ChildWindow registered multiple times!");
118 return;
122 pImpl->maFactories.push_back( rFact );
126 void SfxModule::RegisterToolBoxControl( const SfxTbxCtrlFactory& rFact )
128 #ifdef DBG_UTIL
129 for ( size_t n=0; n<pImpl->maTbxCtrlFactories.size(); n++ )
131 SfxTbxCtrlFactory *pF = &pImpl->maTbxCtrlFactories[n];
132 if ( pF->nTypeId == rFact.nTypeId &&
133 (pF->nSlotId == rFact.nSlotId || pF->nSlotId == 0) )
135 SAL_INFO("sfx.appl", "TbxController-Registering is not clearly defined!");
138 #endif
140 pImpl->maTbxCtrlFactories.push_back( rFact );
144 void SfxModule::RegisterStatusBarControl( const SfxStbCtrlFactory& rFact )
146 #ifdef DBG_UTIL
147 for ( size_t n=0; n<pImpl->maStbCtrlFactories.size(); n++ )
149 SfxStbCtrlFactory *pF = &pImpl->maStbCtrlFactories[n];
150 if ( pF->nTypeId == rFact.nTypeId &&
151 (pF->nSlotId == rFact.nSlotId || pF->nSlotId == 0) )
153 SAL_INFO("sfx.appl", "TbxController-Registering is not clearly defined!");
156 #endif
158 pImpl->maStbCtrlFactories.push_back( rFact );
162 SfxTbxCtrlFactory* SfxModule::GetTbxCtrlFactory(const std::type_info& rSlotType, sal_uInt16 nSlotID) const
164 // search for a factory with the given slot id
165 for (auto& rFactory : pImpl->maTbxCtrlFactories)
166 if( rFactory.nTypeId == rSlotType && rFactory.nSlotId == nSlotID )
167 return &rFactory;
169 // if no factory exists for the given slot id, see if we
170 // have a generic factory with the correct slot type and slot id == 0
171 for (auto& rFactory : pImpl->maTbxCtrlFactories)
172 if( rFactory.nTypeId == rSlotType && rFactory.nSlotId == 0 )
173 return &rFactory;
175 return nullptr;
179 SfxStbCtrlFactory* SfxModule::GetStbCtrlFactory(const std::type_info& rSlotType, sal_uInt16 nSlotID) const
181 for (auto& rFactory : pImpl->maStbCtrlFactories)
182 if ( rFactory.nTypeId == rSlotType &&
183 ( rFactory.nSlotId == 0 || rFactory.nSlotId == nSlotID ) )
184 return &rFactory;
185 return nullptr;
188 SfxChildWinFactory* SfxModule::GetChildWinFactoryById(sal_uInt16 nId) const
190 for (auto& rFactory : pImpl->maFactories)
191 if (rFactory.nId == nId)
192 return &rFactory;
193 return nullptr;
196 std::unique_ptr<SfxTabPage> SfxModule::CreateTabPage(sal_uInt16, weld::Container*, weld::DialogController*, const SfxItemSet&)
198 return nullptr;
201 void SfxModule::Invalidate( sal_uInt16 nId )
203 for( SfxViewFrame* pFrame = SfxViewFrame::GetFirst(); pFrame; pFrame = SfxViewFrame::GetNext( *pFrame ) )
204 if ( pFrame->GetObjectShell()->GetModule() == this )
205 Invalidate_Impl( pFrame->GetBindings(), nId );
208 SfxModule* SfxModule::GetActiveModule( SfxViewFrame* pFrame )
210 if ( !pFrame )
211 pFrame = SfxViewFrame::Current();
212 SfxObjectShell* pSh = nullptr;
213 if( pFrame )
214 pSh = pFrame->GetObjectShell();
215 return pSh ? pSh->GetModule() : nullptr;
218 FieldUnit SfxModule::GetModuleFieldUnit( css::uno::Reference< css::frame::XFrame > const & i_frame )
220 ENSURE_OR_RETURN( i_frame.is(), "SfxModule::GetModuleFieldUnit: invalid frame!", FieldUnit::MM_100TH );
222 // find SfxViewFrame for the given XFrame
223 SfxViewFrame* pViewFrame = SfxViewFrame::GetFirst();
224 while ( pViewFrame != nullptr )
226 if ( pViewFrame->GetFrame().GetFrameInterface() == i_frame )
227 break;
228 pViewFrame = SfxViewFrame::GetNext( *pViewFrame );
230 ENSURE_OR_RETURN(
231 pViewFrame != nullptr,
232 "SfxModule::GetModuleFieldUnit: unable to find an SfxViewFrame for the given XFrame",
233 FieldUnit::MM_100TH);
235 // find the module
236 SfxModule const * pModule = GetActiveModule( pViewFrame );
237 ENSURE_OR_RETURN(pModule != nullptr,
238 "SfxModule::GetModuleFieldUnit: no SfxModule for the given frame!",
239 FieldUnit::MM_100TH);
240 return pModule->GetFieldUnit();
243 FieldUnit SfxModule::GetCurrentFieldUnit()
245 FieldUnit eUnit = FieldUnit::INCH;
246 SfxModule* pModule = GetActiveModule();
247 if ( pModule )
249 const SfxPoolItem* pItem = pModule->GetItem( SID_ATTR_METRIC );
250 if ( pItem )
251 eUnit = static_cast<FieldUnit>(static_cast<const SfxUInt16Item*>(pItem)->GetValue());
253 else
254 SAL_WARN( "sfx.appl", "GetModuleFieldUnit(): no module found" );
255 return eUnit;
258 FieldUnit SfxModule::GetFieldUnit() const
260 FieldUnit eUnit = FieldUnit::INCH;
261 const SfxPoolItem* pItem = GetItem( SID_ATTR_METRIC );
262 if ( pItem )
263 eUnit = static_cast<FieldUnit>(static_cast<const SfxUInt16Item*>(pItem)->GetValue());
264 return eUnit;
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */