xref: /trunk/main/sfx2/source/appl/appinit.cxx (revision 86e1cf34)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sfx2.hxx"
26 
27 #include <sfx2/app.hxx>
28 #include <com/sun/star/frame/XTerminateListener.hpp>
29 #include <com/sun/star/uno/Reference.hxx>
30 #include <com/sun/star/frame/XDesktop.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 
33 #include <svtools/soerr.hxx>
34 #include <svtools/svtools.hrc>
35 #include <unotools/saveopt.hxx>
36 #include <unotools/localisationoptions.hxx>
37 #include <tools/config.hxx>
38 #ifndef _SV_RESARY_HXX
39 #include <tools/resary.hxx>
40 #endif
41 #include <tools/urlobj.hxx>
42 #include <svl/intitem.hxx>
43 #include <svl/eitem.hxx>
44 #include <svl/stritem.hxx>
45 #ifndef _MSGBOX_HXX //autogen
46 #include <vcl/msgbox.hxx>
47 #endif
48 #include <svtools/ehdl.hxx>
49 #ifndef _UNOTOOLS_PROCESSFACTORY_HXX
50 #include <comphelper/processfactory.hxx>
51 #endif
52 #include <unotools/configmgr.hxx>
53 #include <rtl/ustrbuf.hxx>
54 #include <vos/security.hxx>
55 #include <ucbhelper/configurationkeys.hxx>
56 #include <unotools/pathoptions.hxx>
57 #include <unotools/historyoptions.hxx>
58 #include <unotools/moduleoptions.hxx>
59 #include <cppuhelper/implbase2.hxx>
60 
61 #include <rtl/logfile.hxx>
62 #include <vcl/edit.hxx>
63 
64 #include <sfx2/unoctitm.hxx>
65 #include "app.hrc"
66 #include "sfxlocal.hrc"
67 #include "appdata.hxx"
68 #include "arrdecl.hxx"
69 #include <sfx2/dispatch.hxx>
70 #include <sfx2/docfac.hxx>
71 #include <sfx2/evntconf.hxx>
72 #include "intro.hxx"
73 #include <sfx2/mnumgr.hxx>
74 #include <sfx2/msgpool.hxx>
75 #include <sfx2/progress.hxx>
76 #include "sfx2/sfxhelp.hxx"
77 #include "sfx2/sfxresid.hxx"
78 #include "sfxtypes.hxx"
79 #include <sfx2/viewsh.hxx>
80 #include "nochaos.hxx"
81 #include <sfx2/fcontnr.hxx>
82 #include "helper.hxx"	// SfxContentHelper::Kill()
83 #include "sfxpicklist.hxx"
84 #include <tools/svlibrary.hxx>
85 
86 #ifdef UNX
87 #define stricmp(a,b) strcmp(a,b)
88 #endif
89 
90 using namespace ::com::sun::star::uno;
91 using namespace ::com::sun::star::frame;
92 using namespace ::com::sun::star::lang;
93 using namespace ::com::sun::star;
94 namespace css = ::com::sun::star;
95 
96 class SfxTerminateListener_Impl : public ::cppu::WeakImplHelper2< XTerminateListener, XServiceInfo >
97 {
98 public:
99 
100     // XTerminateListener
101     virtual void SAL_CALL queryTermination( const EventObject& aEvent ) throw( TerminationVetoException, RuntimeException );
102     virtual void SAL_CALL notifyTermination( const EventObject& aEvent ) throw( RuntimeException );
103     virtual void SAL_CALL disposing( const EventObject& Source ) throw( RuntimeException );
104 
105     // XServiceInfo
106     virtual ::rtl::OUString SAL_CALL getImplementationName() throw (RuntimeException);
107     virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& sServiceName ) throw (RuntimeException);
108     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw (RuntimeException);
109 };
110 
disposing(const EventObject &)111 void SAL_CALL SfxTerminateListener_Impl::disposing( const EventObject& ) throw( RuntimeException )
112 {
113 }
114 
queryTermination(const EventObject &)115 void SAL_CALL SfxTerminateListener_Impl::queryTermination( const EventObject& ) throw(TerminationVetoException, RuntimeException )
116 {
117     ::vos::OGuard aGuard( Application::GetSolarMutex() );
118     if ( !SFX_APP()->QueryExit_Impl() )
119         throw TerminationVetoException();
120 }
121 
notifyTermination(const EventObject & aEvent)122 void SAL_CALL SfxTerminateListener_Impl::notifyTermination( const EventObject& aEvent ) throw(RuntimeException )
123 {
124     static ::rtl::OUString SERVICE_GLOBALEVENTBROADCASTER = ::rtl::OUString::createFromAscii("com.sun.star.frame.GlobalEventBroadcaster");
125     static ::rtl::OUString EVENT_QUIT_APP                 = ::rtl::OUString::createFromAscii("OnCloseApp");
126 
127     Reference< XDesktop > xDesktop( aEvent.Source, UNO_QUERY );
128     if( xDesktop.is() == sal_True )
129         xDesktop->removeTerminateListener( this );
130 
131     ::vos::OGuard aGuard( Application::GetSolarMutex() );
132     utl::ConfigManager::GetConfigManager()->StoreConfigItems();
133     SfxApplication* pApp = SFX_APP();
134     pApp->Broadcast( SfxSimpleHint( SFX_HINT_DEINITIALIZING ) );
135     pApp->Get_Impl()->pAppDispatch->ReleaseAll();
136     pApp->Get_Impl()->pAppDispatch->release();
137 
138     css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = ::comphelper::getProcessServiceFactory();
139     css::uno::Reference< css::document::XEventListener > xGlobalBroadcaster(xSMGR->createInstance(SERVICE_GLOBALEVENTBROADCASTER), css::uno::UNO_QUERY);
140     if (xGlobalBroadcaster.is())
141     {
142         css::document::EventObject aEvent2;
143         aEvent2.EventName = EVENT_QUIT_APP;
144         xGlobalBroadcaster->notifyEvent(aEvent2);
145     }
146 
147     //pApp->Deinitialize();
148 	delete pApp;
149 	Application::Quit();
150 }
151 
getImplementationName()152 ::rtl::OUString SAL_CALL SfxTerminateListener_Impl::getImplementationName() throw (RuntimeException)
153 {
154     static const ::rtl::OUString IMPLNAME = ::rtl::OUString::createFromAscii("com.sun.star.comp.sfx2.SfxTerminateListener");
155     return IMPLNAME;
156 }
157 
supportsService(const::rtl::OUString & sServiceName)158 ::sal_Bool SAL_CALL SfxTerminateListener_Impl::supportsService( const ::rtl::OUString& sServiceName ) throw (RuntimeException)
159 {
160     Sequence< ::rtl::OUString > lNames  = getSupportedServiceNames();
161     ::sal_Int32                 c       = lNames.getLength();
162     ::sal_Int32                 i       = 0;
163 
164     for (i=0; i<c; ++i)
165     {
166         if (lNames[i].equals(sServiceName))
167             return sal_True;
168     }
169 
170     return sal_False;
171 }
172 
getSupportedServiceNames()173 Sequence< ::rtl::OUString > SAL_CALL SfxTerminateListener_Impl::getSupportedServiceNames() throw (RuntimeException)
174 {
175     // Note: That service  does not really exists .-)
176     // But this implementation is not thought to be registered really within our service.rdb.
177     // At least we need the implementation name only to identify these service at the global desktop instance.
178     // The desktop must know, which listener will terminate the SfxApplication in real !
179     // It must call this special listener as last one ... otherwise we shutdown the SfxApplication BEFORE other listener
180     // can react ...
181     static const ::rtl::OUString SERVICENAME = ::rtl::OUString::createFromAscii("com.sun.star.frame.TerminateListener");
182     Sequence< ::rtl::OUString > lNames(1);
183     lNames[0] = SERVICENAME;
184     return lNames;
185 }
186 
187 //====================================================================
188 
189 #define DOSTRING( x )			   			#x
190 #define STRING( x )				   			DOSTRING( x )
191 
192 typedef bool ( *PFunc_getSpecialCharsForEdit)( Window* i_pParent, const Font& i_rFont, String& o_rOutString );
193 
194 //====================================================================
195 // Lazy binding of the GetSpecialCharsForEdit function as it resides in
196 // a library above us.
197 //====================================================================
198 
thisModule()199 extern "C" { static void SAL_CALL thisModule() {} }
200 
GetSpecialCharsForEdit(Window * pParent,const Font & rFont)201 String GetSpecialCharsForEdit(Window* pParent, const Font& rFont)
202 {
203     static bool bDetermineFunction = false;
204     static PFunc_getSpecialCharsForEdit pfunc_getSpecialCharsForEdit = 0;
205 
206     ::vos::OGuard aGuard( Application::GetSolarMutex() );
207     if ( !bDetermineFunction )
208     {
209         bDetermineFunction = true;
210 
211         static ::rtl::OUString aLibName( RTL_CONSTASCII_USTRINGPARAM( SVLIBRARY( "cui" ) ) );
212         oslModule handleMod = osl_loadModuleRelative(
213             &thisModule, aLibName.pData, 0 );
214 
215         // get symbol
216         ::rtl::OUString aSymbol( RTL_CONSTASCII_USTRINGPARAM( "GetSpecialCharsForEdit" ) );
217         pfunc_getSpecialCharsForEdit = (PFunc_getSpecialCharsForEdit)osl_getFunctionSymbol( handleMod, aSymbol.pData );
218 	    DBG_ASSERT( pfunc_getSpecialCharsForEdit, "GetSpecialCharsForEdit() not found!" );
219     }
220 
221     String aRet;
222     if ( pfunc_getSpecialCharsForEdit )
223         (*pfunc_getSpecialCharsForEdit)( pParent, rFont, aRet );
224     return aRet;
225 }
226 
227 //====================================================================
228 
Initialize_Impl()229 bool SfxApplication::Initialize_Impl()
230 {
231 	RTL_LOGFILE_CONTEXT( aLog, "sfx2 (mb93783) ::SfxApplication::Initialize_Impl" );
232 
233 #ifdef TLX_VALIDATE
234 	StgIo::SetErrorLink( LINK( this, SfxStorageErrHdl, Error ) );
235 #endif
236 
237     Reference < XDesktop > xDesktop ( ::comphelper::getProcessServiceFactory()->createInstance( DEFINE_CONST_UNICODE("com.sun.star.frame.Desktop") ), UNO_QUERY );
238     if (!xDesktop.is())
239          throw RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Couldn't create mandatory desktop service!" )), xDesktop );
240     xDesktop->addTerminateListener( new SfxTerminateListener_Impl() );
241 
242 	Application::EnableAutoHelpId();
243 
244 	pAppData_Impl->pAppDispatch = new SfxStatusDispatcher;
245 	pAppData_Impl->pAppDispatch->acquire();
246 
247 	// SV-Look
248 	Help::EnableContextHelp();
249 	Help::EnableExtHelp();
250 
251 	SvtLocalisationOptions aLocalisation;
252 	Application::EnableAutoMnemonic	( aLocalisation.IsAutoMnemonic() );
253 	Application::SetDialogScaleX	( (short)(aLocalisation.GetDialogScale()) );
254 
255 
256 #ifdef DBG_UTIL
257 	// Der SimplerErrorHandler dient Debugzwecken. In der Product werden
258 	// nichtgehandelte Fehler durch Errorcode 1 an SFX gegeben.
259 	new SimpleErrorHandler;
260 #endif
261 	pAppData_Impl->pBasicResMgr = CreateResManager("sb");
262 	pAppData_Impl->pSvtResMgr = CreateResManager("svt");
263 	new SfxErrorHandler( RID_ERRHDL, ERRCODE_AREA_TOOLS, ERRCODE_AREA_LIB1 );
264     new SfxErrorHandler( RID_SO_ERROR_HANDLER, ERRCODE_AREA_SO, ERRCODE_AREA_SO_END, pAppData_Impl->pSvtResMgr );
265 	new SfxErrorHandler( RID_BASIC_START, ERRCODE_AREA_SBX, ERRCODE_AREA_SBX_END, pAppData_Impl->pBasicResMgr );
266 
267 	// diverse Pointer
268 	SfxPickList::GetOrCreate( SvtHistoryOptions().GetSize( ePICKLIST ) );
269 
270 	/////////////////////////////////////////////////////////////////
271 
272     DBG_ASSERT( !pAppData_Impl->pAppDispat, "AppDispatcher already exists" );
273     pAppData_Impl->pAppDispat = new SfxDispatcher((SfxDispatcher*)0);
274     pAppData_Impl->pSlotPool = new SfxSlotPool;
275     pAppData_Impl->pTbxCtrlFac = new SfxTbxCtrlFactArr_Impl;
276     pAppData_Impl->pStbCtrlFac = new SfxStbCtrlFactArr_Impl;
277     pAppData_Impl->pMenuCtrlFac = new SfxMenuCtrlFactArr_Impl;
278     pAppData_Impl->pViewFrames = new SfxViewFrameArr_Impl;
279     pAppData_Impl->pViewShells = new SfxViewShellArr_Impl;
280     pAppData_Impl->pObjShells = new SfxObjectShellArr_Impl;
281     pAppData_Impl->nInterfaces = SFX_INTERFACE_APP+8;
282     pAppData_Impl->pInterfaces = new SfxInterface*[pAppData_Impl->nInterfaces];
283     memset( pAppData_Impl->pInterfaces, 0, sizeof(SfxInterface*) * pAppData_Impl->nInterfaces );
284 
285 	Registrations_Impl();
286 
287 	// Subklasse initialisieren
288     pAppData_Impl->bDowning = sal_False;
289 	Init();
290 
291 	// get CHAOS item pool...
292 	pAppData_Impl->pPool = NoChaos::GetItemPool();
293 	SetPool( pAppData_Impl->pPool );
294 
295     if ( pAppData_Impl->bDowning )
296 		return sal_False;
297 
298 	// App-Dispatcher aufbauen
299     pAppData_Impl->pAppDispat->Push(*this);
300     pAppData_Impl->pAppDispat->Flush();
301     pAppData_Impl->pAppDispat->DoActivate_Impl( sal_True, NULL );
302 
303     {
304         ::vos::OGuard aGuard( Application::GetSolarMutex() );
305         // Set special characters callback on vcl edit control
306         Edit::SetGetSpecialCharsFunction(&GetSpecialCharsForEdit);
307     }
308 
309     return sal_True;
310 }
311