1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <config_features.h>
21 
22 #include <sfx2/app.hxx>
23 #include <com/sun/star/frame/XTerminateListener.hpp>
24 #include <com/sun/star/uno/Reference.hxx>
25 #include <com/sun/star/frame/theGlobalEventBroadcaster.hpp>
26 #include <com/sun/star/frame/Desktop.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 
29 #include <basic/sbdef.hxx>
30 #include <tools/svlibrary.h>
31 #include <svtools/soerr.hxx>
32 #include <unotools/configmgr.hxx>
33 #include <svtools/ehdl.hxx>
34 #include <comphelper/processfactory.hxx>
35 #include <osl/module.hxx>
36 #include <cppuhelper/implbase.hxx>
37 #include <cppuhelper/supportsservice.hxx>
38 
39 #include <vcl/specialchars.hxx>
40 #include <vcl/help.hxx>
41 #include <vcl/svapp.hxx>
42 
43 #include <unoctitm.hxx>
44 #include <appdata.hxx>
45 #include <sfx2/dispatch.hxx>
46 #include <sfx2/msgpool.hxx>
47 #include <nochaos.hxx>
48 #include <sfxpicklist.hxx>
49 #include <ctrlfactoryimpl.hxx>
50 #include <shellimpl.hxx>
51 
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::frame;
54 using namespace ::com::sun::star::lang;
55 using namespace ::com::sun::star;
56 
57 namespace {
58 
59 class SfxTerminateListener_Impl : public ::cppu::WeakImplHelper< XTerminateListener, XServiceInfo >
60 {
61 public:
62 
63     // XTerminateListener
64     virtual void SAL_CALL queryTermination( const EventObject& aEvent ) override;
65     virtual void SAL_CALL notifyTermination( const EventObject& aEvent ) override;
66     virtual void SAL_CALL disposing( const EventObject& Source ) override;
67 
68     // XServiceInfo
69     virtual OUString SAL_CALL getImplementationName() override;
70     virtual sal_Bool SAL_CALL supportsService( const OUString& sServiceName ) override;
71     virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
72 };
73 
74 }
75 
disposing(const EventObject &)76 void SAL_CALL SfxTerminateListener_Impl::disposing( const EventObject& )
77 {
78 }
79 
queryTermination(const EventObject &)80 void SAL_CALL SfxTerminateListener_Impl::queryTermination( const EventObject& )
81 {
82 }
83 
notifyTermination(const EventObject & aEvent)84 void SAL_CALL SfxTerminateListener_Impl::notifyTermination( const EventObject& aEvent )
85 {
86     Reference< XDesktop > xDesktop( aEvent.Source, UNO_QUERY );
87     if( xDesktop.is() )
88         xDesktop->removeTerminateListener( this );
89 
90     SolarMutexGuard aGuard;
91     utl::ConfigManager::storeConfigItems();
92 
93     SfxApplication* pApp = SfxGetpApp();
94     pApp->Broadcast( SfxHint( SfxHintId::Deinitializing ) );
95     pApp->Get_Impl()->mxAppDispatch->ReleaseAll();
96     pApp->Get_Impl()->mxAppDispatch.clear();
97 
98     css::uno::Reference< css::uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
99     css::uno::Reference< css::document::XDocumentEventListener > xGlobalBroadcaster(css::frame::theGlobalEventBroadcaster::get(xContext), css::uno::UNO_QUERY_THROW);
100 
101     css::document::DocumentEvent aEvent2;
102     aEvent2.EventName = "OnCloseApp";
103     xGlobalBroadcaster->documentEventOccured(aEvent2);
104 
105     delete pApp;
106     Application::Quit();
107 }
108 
getImplementationName()109 OUString SAL_CALL SfxTerminateListener_Impl::getImplementationName()
110 {
111     return "com.sun.star.comp.sfx2.SfxTerminateListener";
112 }
113 
supportsService(const OUString & sServiceName)114 sal_Bool SAL_CALL SfxTerminateListener_Impl::supportsService( const OUString& sServiceName )
115 {
116     return cppu::supportsService(this, sServiceName);
117 }
118 
getSupportedServiceNames()119 Sequence< OUString > SAL_CALL SfxTerminateListener_Impl::getSupportedServiceNames()
120 {
121     // Note: That service  does not really exists .-)
122     // But this implementation is not thought to be registered really within our service.rdb.
123     // At least we need the implementation name only to identify these service at the global desktop instance.
124     // The desktop must know, which listener will terminate the SfxApplication in real !
125     // It must call this special listener as last one ... otherwise we shutdown the SfxApplication BEFORE other listener
126     // can react ...
127     return { "com.sun.star.frame.TerminateListener" };
128 }
129 
130 
131 typedef bool (*PFunc_getSpecialCharsForEdit)(weld::Widget* i_pParent, const vcl::Font& i_rFont, OUString& o_rOutString);
132 
133 
134 // Lazy binding of the GetSpecialCharsForEdit function as it resides in
135 // a library above us.
136 
137 
138 #ifndef DISABLE_DYNLOADING
139 
thisModule()140 extern "C" { static void thisModule() {} }
141 
142 #else
143 
144 extern "C" bool GetSpecialCharsForEdit(weld::Widget* i_pParent, const vcl::Font& i_rFont, OUString& o_rOutString);
145 
146 #endif
147 
SfxGetSpecialCharsForEdit(weld::Widget * pParent,const vcl::Font & rFont)148 static OUString SfxGetSpecialCharsForEdit(weld::Widget* pParent, const vcl::Font& rFont)
149 {
150     static const PFunc_getSpecialCharsForEdit pfunc_getSpecialCharsForEdit = [] {
151         PFunc_getSpecialCharsForEdit pfunc = nullptr;
152 #ifndef DISABLE_DYNLOADING
153         osl::Module aMod;
154         aMod.loadRelative(&thisModule, SVLIBRARY("cui"));
155 
156         // get symbol
157         pfunc = reinterpret_cast<PFunc_getSpecialCharsForEdit>(aMod.getFunctionSymbol("GetSpecialCharsForEdit"));
158         DBG_ASSERT( pfunc, "GetSpecialCharsForEdit() not found!" );
159         aMod.release();
160 #else
161         pfunc = GetSpecialCharsForEdit;
162 #endif
163         return pfunc;
164     }();
165 
166     OUString aRet;
167     if ( pfunc_getSpecialCharsForEdit )
168     {
169         SolarMutexGuard aGuard;
170         (*pfunc_getSpecialCharsForEdit)( pParent, rFont, aRet );
171     }
172     return aRet;
173 }
174 
175 
Initialize_Impl()176 void SfxApplication::Initialize_Impl()
177 {
178 #ifdef TLX_VALIDATE
179     StgIo::SetErrorLink( LINK( this, SfxStorageErrHdl, Error ) );
180 #endif
181 
182     Reference < XDesktop2 > xDesktop = Desktop::create ( ::comphelper::getProcessComponentContext() );
183     xDesktop->addTerminateListener( new SfxTerminateListener_Impl );
184 
185     pImpl->mxAppDispatch = new SfxStatusDispatcher;
186 
187     // SV-Look
188     Help::EnableContextHelp();
189     Help::EnableExtHelp();
190 
191     pImpl->m_pToolsErrorHdl.reset(new SfxErrorHandler(
192         RID_ERRHDL, ErrCodeArea::Io, ErrCodeArea::Vcl));
193 
194     pImpl->m_pSoErrorHdl.reset(new SfxErrorHandler(
195         RID_SO_ERROR_HANDLER, ErrCodeArea::So, ErrCodeArea::So, SvtResLocale()));
196 #if HAVE_FEATURE_SCRIPTING
197     pImpl->m_pSbxErrorHdl.reset(new SfxErrorHandler(
198         RID_BASIC_START, ErrCodeArea::Sbx, ErrCodeArea::Sbx, BasResLocale()));
199 #endif
200 
201     if (!utl::ConfigManager::IsFuzzing())
202     {
203         SolarMutexGuard aGuard;
204         //ensure instantiation of listener that manages the internal recently-used
205         //list
206         pImpl->mxAppPickList.reset(new SfxPickList(*this));
207     }
208 
209     DBG_ASSERT( !pImpl->pAppDispat, "AppDispatcher already exists" );
210     pImpl->pAppDispat.reset(new SfxDispatcher);
211     pImpl->pSlotPool.reset(new SfxSlotPool);
212     pImpl->pTbxCtrlFac.reset(new SfxTbxCtrlFactArr_Impl);
213     pImpl->pStbCtrlFac.reset(new SfxStbCtrlFactArr_Impl);
214     pImpl->pViewFrames.reset(new SfxViewFrameArr_Impl);
215     pImpl->pViewShells.reset(new SfxViewShellArr_Impl);
216     pImpl->pObjShells.reset(new SfxObjectShellArr_Impl);
217 
218     Registrations_Impl();
219 
220     // initialize the subclass
221     pImpl->bDowning = false;
222 
223     // get CHAOS item pool...
224     pImpl->pPool = NoChaos::GetItemPool();
225     SetPool( pImpl->pPool );
226 
227     if ( pImpl->bDowning )
228         return;
229 
230     // build the app dispatcher
231     pImpl->pAppDispat->Push(*this);
232     pImpl->pAppDispat->Flush();
233     pImpl->pAppDispat->DoActivate_Impl( true );
234 
235     {
236         SolarMutexGuard aGuard;
237         // Set special characters callback on vcl edit control
238         vcl::SetGetSpecialCharsFunction(&SfxGetSpecialCharsForEdit);
239     }
240 }
241 
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
243