Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / sfx2 / source / appl / app.cxx
blob6d5f30bca0ad0f3d1d94d28d70e9e25d89e448b5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #if defined UNX
30 #include <limits.h>
31 #else // UNX
32 #include <stdlib.h>
33 #ifndef PATH_MAX
34 #define PATH_MAX _MAX_PATH
35 #endif
36 #endif // UNX
38 #include <sfx2/app.hxx>
39 #include <sfx2/frame.hxx>
40 #include <basic/basrdll.hxx>
41 #include <basic/sbmeth.hxx>
42 #include <basic/sbmod.hxx>
43 #include <svtools/asynclink.hxx>
44 #include <svl/stritem.hxx>
45 #include <vcl/sound.hxx>
46 #include <svl/eitem.hxx>
47 #include <svl/urlbmk.hxx>
48 #include <vcl/msgbox.hxx>
49 #include <svtools/sfxecode.hxx>
50 #include <svtools/ehdl.hxx>
52 #include <svl/svdde.hxx>
53 #include <unotools/tempfile.hxx>
54 #include <osl/file.hxx>
55 #include <svl/svstdarr.hxx>
56 #include <com/sun/star/uno/Sequence.hxx>
57 #include <com/sun/star/uno/Any.hxx>
58 #include <com/sun/star/lang/XInitialization.hpp>
59 #include <com/sun/star/frame/XFrameActionListener.hpp>
60 #include <com/sun/star/frame/XComponentLoader.hpp>
61 #include <com/sun/star/frame/XFrame.hpp>
62 #include <com/sun/star/frame/FrameActionEvent.hpp>
63 #include <com/sun/star/frame/FrameAction.hpp>
64 #include <com/sun/star/loader/XImplementationLoader.hpp>
65 #include <com/sun/star/loader/CannotActivateFactoryException.hpp>
66 #include <com/sun/star/mozilla/XPluginInstance.hpp>
67 #include <com/sun/star/frame/XFramesSupplier.hpp>
68 #include <com/sun/star/container/XIndexAccess.hpp>
69 #include <com/sun/star/beans/XPropertySet.hpp>
70 #include <comphelper/processfactory.hxx>
71 #include <com/sun/star/uri/XUriReferenceFactory.hpp>
72 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
73 #include <basic/basmgr.hxx>
74 #include <toolkit/helper/vclunohelper.hxx>
75 #include <vcl/svapp.hxx>
76 #include <rtl/logfile.hxx>
77 #include <sfx2/appuno.hxx>
78 #include "sfx2/sfxhelp.hxx"
79 #include <sfx2/request.hxx>
80 #include "sfxtypes.hxx"
81 #include "sfx2/sfxresid.hxx"
82 #include "arrdecl.hxx"
83 #include <sfx2/progress.hxx>
84 #include <sfx2/objsh.hxx>
85 #include <sfx2/docfac.hxx>
86 #include <sfx2/docfile.hxx>
87 #include <sfx2/docfilt.hxx>
88 #include "fltfnc.hxx"
89 #include "nfltdlg.hxx"
90 #include <sfx2/new.hxx>
91 #include <sfx2/bindings.hxx>
92 #include <sfx2/dispatch.hxx>
93 #include <sfx2/viewsh.hxx>
94 #include <sfx2/genlink.hxx>
95 #include <sfx2/viewfrm.hxx>
96 #include "appdata.hxx"
97 #include "openflag.hxx"
98 #include "app.hrc"
99 #include "virtmenu.hxx"
100 #include <sfx2/module.hxx>
101 #include <sfx2/event.hxx>
102 #include "imestatuswindow.hxx"
103 #include "workwin.hxx"
104 #include <sfx2/tbxctrl.hxx>
105 #include <sfx2/sfxdlg.hxx>
106 #include "sfx2/stbitem.hxx"
107 #include "eventsupplier.hxx"
108 #include <sfx2/dockwin.hxx>
110 #ifdef DBG_UTIL
111 #include <sfx2/mnuitem.hxx>
112 #endif
114 #include <unotools/saveopt.hxx>
115 #include <svtools/helpopt.hxx>
116 #include <unotools/pathoptions.hxx>
117 #include <unotools/viewoptions.hxx>
118 #include <unotools/moduleoptions.hxx>
119 #include <unotools/historyoptions.hxx>
120 #include <svtools/menuoptions.hxx>
121 #include <svtools/miscopt.hxx>
122 #include <unotools/useroptions.hxx>
123 #include <unotools/startoptions.hxx>
124 #include <unotools/securityoptions.hxx>
125 #include <unotools/localisationoptions.hxx>
126 #include <unotools/fontoptions.hxx>
127 #include <unotools/internaloptions.hxx>
128 #include <unotools/workingsetoptions.hxx>
129 #include <unotools/syslocaleoptions.hxx>
130 #include <unotools/syslocale.hxx>
131 #include <framework/addonsoptions.hxx>
132 #include <unotools/extendedsecurityoptions.hxx>
133 #include <rtl/instance.hxx>
134 #include <rtl/strbuf.hxx>
136 using namespace ::com::sun::star;
138 // Static member
139 SfxApplication* SfxApplication::pApp = NULL;
140 #ifndef DISABLE_SCRIPTING
141 static BasicDLL* pBasic = NULL;
142 #endif
143 static SfxHelp* pSfxHelp = NULL;
145 namespace
147 class theApplicationMutex
148 : public rtl::Static<osl::Mutex, theApplicationMutex> {};
151 #include <framework/imageproducer.hxx>
152 #include <framework/sfxhelperfunctions.hxx>
153 #include "sfx2/imagemgr.hxx"
154 #include "fwkhelper.hxx"
156 SfxApplication* SfxApplication::GetOrCreate()
158 // SFX on demand
159 ::osl::MutexGuard aGuard(theApplicationMutex::get());
160 if (!pApp)
162 RTL_LOGFILE_CONTEXT( aLog, "sfx2 (mb93783) ::SfxApplication::SetApp" );
164 pApp = new SfxApplication;
166 // at the moment a bug may occur when Initialize_Impl returns FALSE,
167 // but this is only temporary because all code that may cause such
168 // a fault will be moved outside the SFX
169 pApp->Initialize_Impl();
171 ::framework::SetImageProducer( GetImage );
172 ::framework::SetRefreshToolbars( RefreshToolbars );
173 ::framework::SetToolBoxControllerCreator( SfxToolBoxControllerFactory );
174 ::framework::SetStatusBarControllerCreator( SfxStatusBarControllerFactory );
175 ::framework::SetDockingWindowCreator( SfxDockingWindowFactory );
176 ::framework::SetIsDockingWindowVisible( IsDockingWindowVisible );
177 ::framework::SetActivateToolPanel( &SfxViewFrame::ActivateToolPanel );
179 Application::SetHelp( pSfxHelp );
180 if ( SvtHelpOptions().IsHelpTips() )
181 Help::EnableQuickHelp();
182 else
183 Help::DisableQuickHelp();
184 if ( SvtHelpOptions().IsHelpTips() && SvtHelpOptions().IsExtendedHelp() )
185 Help::EnableBalloonHelp();
186 else
187 Help::DisableBalloonHelp();
189 return pApp;
192 SfxApplication::SfxApplication()
193 : pAppData_Impl( 0 )
195 RTL_LOGFILE_CONTEXT( aLog, "sfx2 (mb93783) ::SfxApplication::SfxApplication" );
197 SetName( DEFINE_CONST_UNICODE("StarOffice") );
198 SvtViewOptions::AcquireOptions();
200 pAppData_Impl = new SfxAppData_Impl( this );
201 pAppData_Impl->m_xImeStatusWindow->init();
202 pApp->PreInit();
204 RTL_LOGFILE_CONTEXT_TRACE( aLog, "{ initialize DDE" );
206 sal_Bool bOk = InitializeDde();
208 #ifdef DBG_UTIL
209 if( !bOk )
211 rtl::OStringBuffer aStr(
212 RTL_CONSTASCII_STRINGPARAM("No DDE-Service possible. Error: "));
213 if( GetDdeService() )
214 aStr.append(static_cast<sal_Int32>(GetDdeService()->GetError()));
215 else
216 aStr.append('?');
217 DBG_ASSERT( sal_False, aStr.getStr() );
219 #else
220 (void)bOk;
221 #endif
223 pSfxHelp = new SfxHelp;
225 #ifndef DISABLE_SCRIPTING
226 pBasic = new BasicDLL;
227 StarBASIC::SetGlobalErrorHdl( LINK( this, SfxApplication, GlobalBasicErrorHdl_Impl ) );
228 #endif
229 RTL_LOGFILE_CONTEXT_TRACE( aLog, "} initialize DDE" );
232 SfxApplication::~SfxApplication()
234 OSL_ENSURE( GetObjectShells_Impl().Count() == 0, "Memory leak: some object shells were not removed!" );
236 Broadcast( SfxSimpleHint(SFX_HINT_DYING) );
238 SfxModule::DestroyModules_Impl();
240 delete pSfxHelp;
241 Application::SetHelp( NULL );
243 // delete global options
244 SvtViewOptions::ReleaseOptions();
245 #ifndef DISABLE_SCRIPTING
246 delete pBasic;
247 #endif
248 if ( !pAppData_Impl->bDowning )
249 Deinitialize();
251 delete pAppData_Impl;
252 pApp = 0;
255 //====================================================================
257 const String& SfxApplication::GetLastDir_Impl() const
259 /* [Description]
261 Internal method by which the last set directory with the method
262 <SfxApplication::SetLastDir_Impl()> in SFX is returned.
264 This is usually the most recently addressed by the
265 SfxFileDialog directory.
267 [Cross-reference]
268 <SfxApplication::SetLastDir_Impl()>
272 return pAppData_Impl->aLastDir;
275 const String& SfxApplication::GetLastSaveDirectory() const
277 /* [Description]
279 As <SfxApplication::GetLastDir_Impl()>, only external
281 [Cross-reference]
282 <SfxApplication::GetLastDir_Impl()>
286 return GetLastDir_Impl();
289 //--------------------------------------------------------------------
291 void SfxApplication::SetLastDir_Impl
293 const String& rNewDir /* Complete directory path as a string */
296 /* [Description]
298 Internal Method, by which a directory path is set that was last addressed
299 (eg by the SfxFileDialog).
301 [Cross-reference]
302 <SfxApplication::GetLastDir_Impl()>
306 pAppData_Impl->aLastDir = rNewDir;
309 //--------------------------------------------------------------------
311 void SfxApplication::ResetLastDir()
313 String aEmpty;
314 pAppData_Impl->aLastDir = aEmpty;
317 //--------------------------------------------------------------------
319 SfxDispatcher* SfxApplication::GetDispatcher_Impl()
321 return pAppData_Impl->pViewFrame? pAppData_Impl->pViewFrame->GetDispatcher(): pAppData_Impl->pAppDispat;
324 //--------------------------------------------------------------------
325 void SfxApplication::SetViewFrame_Impl( SfxViewFrame *pFrame )
327 if ( pFrame != pAppData_Impl->pViewFrame )
329 // get the containerframes ( if one of the frames is an InPlaceFrame )
330 SfxViewFrame *pOldContainerFrame = pAppData_Impl->pViewFrame;
331 while ( pOldContainerFrame && pOldContainerFrame->GetParentViewFrame_Impl() )
332 pOldContainerFrame = pOldContainerFrame->GetParentViewFrame_Impl();
333 SfxViewFrame *pNewContainerFrame = pFrame;
334 while ( pNewContainerFrame && pNewContainerFrame->GetParentViewFrame_Impl() )
335 pNewContainerFrame = pNewContainerFrame->GetParentViewFrame_Impl();
337 // DocWinActivate : both frames belong to the same TopWindow
338 // TopWinActivate : both frames belong to different TopWindows
340 sal_Bool bTaskActivate = pOldContainerFrame != pNewContainerFrame;
342 if ( pOldContainerFrame )
344 if ( bTaskActivate )
345 NotifyEvent( SfxViewEventHint( SFX_EVENT_DEACTIVATEDOC, GlobalEventConfig::GetEventName(STR_EVENT_DEACTIVATEDOC), pOldContainerFrame->GetObjectShell(), pOldContainerFrame->GetFrame().GetController() ) );
346 pOldContainerFrame->DoDeactivate( bTaskActivate, pFrame );
348 if( pOldContainerFrame->GetProgress() )
349 pOldContainerFrame->GetProgress()->Suspend();
352 pAppData_Impl->pViewFrame = pFrame;
354 if( pNewContainerFrame )
356 pNewContainerFrame->DoActivate( bTaskActivate );
357 if ( bTaskActivate && pNewContainerFrame->GetObjectShell() )
359 pNewContainerFrame->GetObjectShell()->PostActivateEvent_Impl( pNewContainerFrame );
360 NotifyEvent(SfxViewEventHint(SFX_EVENT_ACTIVATEDOC, GlobalEventConfig::GetEventName(STR_EVENT_ACTIVATEDOC), pNewContainerFrame->GetObjectShell(), pNewContainerFrame->GetFrame().GetController() ) );
363 SfxProgress *pProgress = pNewContainerFrame->GetProgress();
364 if ( pProgress )
366 if( pProgress->IsSuspended() )
367 pProgress->Resume();
368 else
369 pProgress->SetState( pProgress->GetState() );
372 if ( pAppData_Impl->pViewFrame->GetViewShell() )
374 SfxDispatcher* pDisp = pAppData_Impl->pViewFrame->GetDispatcher();
375 pDisp->Flush();
376 pDisp->Update_Impl(sal_True);
381 // even if the frame actually didn't change, ensure its document is forwarded
382 // to SfxObjectShell::SetCurrentComponent.
383 // Otherwise, the CurrentComponent might not be correct, in case it has meanwhile
384 // been reset to some other document, by some non-SFX component.
385 // #i49133# / 2007-12-19 / frank.schoenheit@sun.com
386 if ( pFrame && pFrame->GetViewShell() )
387 pFrame->GetViewShell()->SetCurrentDocument();
390 //---------------------------------------------------------------------
392 ResMgr* SfxApplication::CreateResManager( const char *pPrefix )
394 return ResMgr::CreateResMgr(pPrefix);
397 //--------------------------------------------------------------------
399 ResMgr* SfxApplication::GetSfxResManager()
401 return SfxResId::GetResMgr();
404 //------------------------------------------------------------------------
406 void SfxApplication::SetProgress_Impl
408 SfxProgress *pProgress
412 DBG_ASSERT( ( !pAppData_Impl->pProgress && pProgress ) ||
413 ( pAppData_Impl->pProgress && !pProgress ),
414 "Progress acitivation/deacitivation mismatch" );
416 if ( pAppData_Impl->pProgress && pProgress )
418 pAppData_Impl->pProgress->Suspend();
419 pAppData_Impl->pProgress->UnLock();
420 delete pAppData_Impl->pProgress;
423 pAppData_Impl->pProgress = pProgress;
426 //------------------------------------------------------------------------
428 sal_uInt16 SfxApplication::GetFreeIndex()
430 return pAppData_Impl->aIndexBitSet.GetFreeIndex()+1;
433 //------------------------------------------------------------------------
435 void SfxApplication::ReleaseIndex(sal_uInt16 i)
437 pAppData_Impl->aIndexBitSet.ReleaseIndex(i-1);
440 //--------------------------------------------------------------------
442 Window* SfxApplication::GetTopWindow() const
444 SfxWorkWindow* pWork = GetWorkWindow_Impl( SfxViewFrame::Current() );
445 return pWork ? pWork->GetWindow() : NULL;
448 SfxTbxCtrlFactArr_Impl& SfxApplication::GetTbxCtrlFactories_Impl() const
450 return *pAppData_Impl->pTbxCtrlFac;
453 SfxStbCtrlFactArr_Impl& SfxApplication::GetStbCtrlFactories_Impl() const
455 return *pAppData_Impl->pStbCtrlFac;
458 SfxMenuCtrlFactArr_Impl& SfxApplication::GetMenuCtrlFactories_Impl() const
460 return *pAppData_Impl->pMenuCtrlFac;
463 SfxViewFrameArr_Impl& SfxApplication::GetViewFrames_Impl() const
465 return *pAppData_Impl->pViewFrames;
468 SfxViewShellArr_Impl& SfxApplication::GetViewShells_Impl() const
470 return *pAppData_Impl->pViewShells;
473 SfxObjectShellArr_Impl& SfxApplication::GetObjectShells_Impl() const
475 return *pAppData_Impl->pObjShells;
478 void SfxApplication::Invalidate( sal_uInt16 nId )
480 for( SfxViewFrame* pFrame = SfxViewFrame::GetFirst(); pFrame; pFrame = SfxViewFrame::GetNext( *pFrame ) )
481 Invalidate_Impl( pFrame->GetBindings(), nId );
484 #define DOSTRING( x ) #x
485 #define STRING( x ) DOSTRING( x )
487 #ifndef DISABLE_SCRIPTING
489 typedef long (SAL_CALL *basicide_handle_basic_error)(void*);
490 typedef rtl_uString* (SAL_CALL *basicide_choose_macro)(void*, sal_Bool, rtl_uString*);
491 typedef void* (SAL_CALL *basicide_macro_organizer)(sal_Int16);
493 extern "C" { static void SAL_CALL thisModule() {} }
495 #endif
497 IMPL_LINK( SfxApplication, GlobalBasicErrorHdl_Impl, StarBASIC*, pStarBasic )
499 #ifdef DISABLE_SCRIPTING
500 (void) pStarBasic;
501 return 0;
502 #else
503 // get basctl dllname
504 static ::rtl::OUString aLibName( SVLIBRARY( "basctl" ) );
506 // load module
507 oslModule handleMod = osl_loadModuleRelative(
508 &thisModule, aLibName.pData, 0 );
510 // get symbol
511 ::rtl::OUString aSymbol( "basicide_handle_basic_error" );
512 basicide_handle_basic_error pSymbol = (basicide_handle_basic_error) osl_getFunctionSymbol( handleMod, aSymbol.pData );
514 // call basicide_handle_basic_error in basctl
515 long nRet = pSymbol ? pSymbol( pStarBasic ) : 0;
517 return nRet;
518 #endif
521 sal_Bool SfxApplication::IsXScriptURL( const String& rScriptURL )
523 sal_Bool result = sal_False;
525 #ifdef DISABLE_SCRIPTING
526 (void) rScriptURL;
527 #else
528 ::com::sun::star::uno::Reference
529 < ::com::sun::star::lang::XMultiServiceFactory > xSMgr =
530 ::comphelper::getProcessServiceFactory();
532 ::com::sun::star::uno::Reference
533 < ::com::sun::star::uri::XUriReferenceFactory >
534 xFactory( xSMgr->createInstance(
535 ::rtl::OUString(
536 "com.sun.star.uri.UriReferenceFactory" ) ),
537 ::com::sun::star::uno::UNO_QUERY );
539 if ( xFactory.is() )
543 ::com::sun::star::uno::Reference
544 < ::com::sun::star::uri::XVndSunStarScriptUrl >
545 xUrl( xFactory->parse( rScriptURL ),
546 ::com::sun::star::uno::UNO_QUERY );
548 if ( xUrl.is() )
550 result = sal_True;
553 catch (const ::com::sun::star::uno::RuntimeException&)
555 // ignore, will just return FALSE
558 #endif
559 return result;
562 ::rtl::OUString
563 SfxApplication::ChooseScript()
565 ::rtl::OUString aScriptURL;
567 #ifndef DISABLE_SCRIPTING
568 SfxAbstractDialogFactory* pFact = SfxAbstractDialogFactory::Create();
569 if ( pFact )
571 OSL_TRACE("create selector dialog");
573 const SfxViewFrame* pViewFrame = SfxViewFrame::Current();
574 const SfxFrame* pFrame = pViewFrame ? &pViewFrame->GetFrame() : NULL;
575 uno::Reference< frame::XFrame > xFrame( pFrame ? pFrame->GetFrameInterface() : uno::Reference< frame::XFrame >() );
577 AbstractScriptSelectorDialog* pDlg =
578 pFact->CreateScriptSelectorDialog( NULL, sal_False, xFrame );
580 OSL_TRACE("done, now exec it");
582 sal_uInt16 nRet = pDlg->Execute();
584 OSL_TRACE("has returned");
586 if ( nRet == RET_OK )
588 aScriptURL = pDlg->GetScriptURL();
591 delete pDlg;
593 #endif
594 return aScriptURL;
597 void SfxApplication::MacroOrganizer( sal_Int16 nTabId )
599 #ifdef DISABLE_SCRIPTING
600 (void) nTabId;
601 #else
602 // get basctl dllname
603 static ::rtl::OUString aLibName( SVLIBRARY( "basctl" ) );
605 // load module
606 oslModule handleMod = osl_loadModuleRelative(
607 &thisModule, aLibName.pData, 0 );
609 // get symbol
610 ::rtl::OUString aSymbol( "basicide_macro_organizer" );
611 basicide_macro_organizer pSymbol = (basicide_macro_organizer) osl_getFunctionSymbol( handleMod, aSymbol.pData );
613 // call basicide_choose_macro in basctl
614 pSymbol( nTabId );
615 #endif
618 ErrCode SfxApplication::CallBasic( const String& rCode, BasicManager* pMgr, SbxArray* pArgs, SbxValue* pRet )
620 #ifdef DISABLE_SCRIPTING
621 (void) rCode;
622 (void) pMgr;
623 (void) pArgs;
624 (void) pRet;
625 return ERRCODE_BASIC_CANNOT_LOAD;
626 #else
627 return pMgr->ExecuteMacro( rCode, pArgs, pRet);
628 #endif
631 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */