bump product version to 4.1.6.2
[LibreOffice.git] / sfx2 / source / appl / app.cxx
blobb973bc41ea97e9fa2429ee36130aae64024ffc66
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 <config_features.h>
22 #if defined UNX
23 #include <limits.h>
24 #else // UNX
25 #include <stdlib.h>
26 #ifndef PATH_MAX
27 #define PATH_MAX _MAX_PATH
28 #endif
29 #endif // UNX
31 #include <sfx2/app.hxx>
32 #include <sfx2/frame.hxx>
33 #include <basic/basrdll.hxx>
34 #include <basic/sbmeth.hxx>
35 #include <basic/sbmod.hxx>
36 #include <svtools/asynclink.hxx>
37 #include <svl/stritem.hxx>
38 #include <svl/eitem.hxx>
39 #include <svl/urlbmk.hxx>
40 #include <vcl/msgbox.hxx>
41 #include <svtools/sfxecode.hxx>
42 #include <svtools/ehdl.hxx>
44 #include <svl/svdde.hxx>
45 #include <unotools/tempfile.hxx>
46 #include <osl/file.hxx>
47 #include <com/sun/star/uno/Sequence.hxx>
48 #include <com/sun/star/uno/Any.hxx>
49 #include <com/sun/star/lang/XInitialization.hpp>
50 #include <com/sun/star/frame/XFrameActionListener.hpp>
51 #include <com/sun/star/frame/XComponentLoader.hpp>
52 #include <com/sun/star/frame/XFrame.hpp>
53 #include <com/sun/star/frame/FrameActionEvent.hpp>
54 #include <com/sun/star/frame/FrameAction.hpp>
55 #include <com/sun/star/loader/XImplementationLoader.hpp>
56 #include <com/sun/star/mozilla/XPluginInstance.hpp>
57 #include <com/sun/star/frame/XFramesSupplier.hpp>
58 #include <com/sun/star/container/XIndexAccess.hpp>
59 #include <com/sun/star/beans/XPropertySet.hpp>
60 #include <comphelper/processfactory.hxx>
61 #include <com/sun/star/uri/UriReferenceFactory.hpp>
62 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
63 #include <basic/basmgr.hxx>
64 #include <toolkit/helper/vclunohelper.hxx>
65 #include <vcl/svapp.hxx>
66 #include <rtl/logfile.hxx>
67 #include <sfx2/appuno.hxx>
68 #include "sfx2/sfxhelp.hxx"
69 #include <sfx2/request.hxx>
70 #include "sfxtypes.hxx"
71 #include "sfx2/sfxresid.hxx"
72 #include "arrdecl.hxx"
73 #include <sfx2/progress.hxx>
74 #include <sfx2/objsh.hxx>
75 #include <sfx2/docfac.hxx>
76 #include <sfx2/docfile.hxx>
77 #include <sfx2/docfilt.hxx>
78 #include "fltfnc.hxx"
79 #include "nfltdlg.hxx"
80 #include <sfx2/new.hxx>
81 #include <sfx2/bindings.hxx>
82 #include <sfx2/dispatch.hxx>
83 #include <sfx2/viewsh.hxx>
84 #include <sfx2/genlink.hxx>
85 #include <sfx2/viewfrm.hxx>
86 #include "appdata.hxx"
87 #include "openflag.hxx"
88 #include "app.hrc"
89 #include "virtmenu.hxx"
90 #include <sfx2/module.hxx>
91 #include <sfx2/event.hxx>
92 #include "imestatuswindow.hxx"
93 #include "workwin.hxx"
94 #include <sfx2/tbxctrl.hxx>
95 #include <sfx2/sfxdlg.hxx>
96 #include "sfx2/stbitem.hxx"
97 #include "eventsupplier.hxx"
98 #include <sfx2/dockwin.hxx>
100 #ifdef DBG_UTIL
101 #include <sfx2/mnuitem.hxx>
102 #endif
104 #include <unotools/saveopt.hxx>
105 #include <svtools/helpopt.hxx>
106 #include <unotools/pathoptions.hxx>
107 #include <unotools/viewoptions.hxx>
108 #include <unotools/moduleoptions.hxx>
109 #include <unotools/historyoptions.hxx>
110 #include <svtools/menuoptions.hxx>
111 #include <svtools/miscopt.hxx>
112 #include <unotools/useroptions.hxx>
113 #include <unotools/securityoptions.hxx>
114 #include <unotools/localisationoptions.hxx>
115 #include <unotools/fontoptions.hxx>
116 #include <unotools/syslocaleoptions.hxx>
117 #include <unotools/syslocale.hxx>
118 #include <framework/addonsoptions.hxx>
119 #include <unotools/extendedsecurityoptions.hxx>
120 #include <rtl/instance.hxx>
121 #include <rtl/strbuf.hxx>
123 using namespace ::com::sun::star;
125 // Static member
126 SfxApplication* SfxApplication::pApp = NULL;
127 #ifndef DISABLE_SCRIPTING
128 static BasicDLL* pBasic = NULL;
129 #endif
131 #if HAVE_FEATURE_DESKTOP
132 static SfxHelp* pSfxHelp = NULL;
133 #endif
135 namespace
137 class theApplicationMutex
138 : public rtl::Static<osl::Mutex, theApplicationMutex> {};
141 #include <framework/imageproducer.hxx>
142 #include <framework/sfxhelperfunctions.hxx>
143 #include "sfx2/imagemgr.hxx"
144 #include "fwkhelper.hxx"
146 SfxApplication* SfxApplication::GetOrCreate()
148 // SFX on demand
149 ::osl::MutexGuard aGuard(theApplicationMutex::get());
150 if (!pApp)
152 RTL_LOGFILE_CONTEXT( aLog, "sfx2 (mb93783) ::SfxApplication::SetApp" );
154 pApp = new SfxApplication;
156 // at the moment a bug may occur when Initialize_Impl returns FALSE,
157 // but this is only temporary because all code that may cause such
158 // a fault will be moved outside the SFX
159 pApp->Initialize_Impl();
161 ::framework::SetImageProducer( GetImage );
162 ::framework::SetRefreshToolbars( RefreshToolbars );
163 ::framework::SetToolBoxControllerCreator( SfxToolBoxControllerFactory );
164 ::framework::SetStatusBarControllerCreator( SfxStatusBarControllerFactory );
165 ::framework::SetDockingWindowCreator( SfxDockingWindowFactory );
166 ::framework::SetIsDockingWindowVisible( IsDockingWindowVisible );
167 ::framework::SetActivateToolPanel( &SfxViewFrame::ActivateToolPanel );
168 #if HAVE_FEATURE_DESKTOP
169 Application::SetHelp( pSfxHelp );
170 if ( SvtHelpOptions().IsHelpTips() )
171 Help::EnableQuickHelp();
172 else
173 Help::DisableQuickHelp();
174 if ( SvtHelpOptions().IsHelpTips() && SvtHelpOptions().IsExtendedHelp() )
175 Help::EnableBalloonHelp();
176 else
177 Help::DisableBalloonHelp();
178 #endif
180 return pApp;
183 SfxApplication::SfxApplication()
184 : pAppData_Impl( 0 )
186 RTL_LOGFILE_CONTEXT( aLog, "sfx2 (mb93783) ::SfxApplication::SfxApplication" );
188 SetName( OUString("StarOffice") );
189 SvtViewOptions::AcquireOptions();
191 pAppData_Impl = new SfxAppData_Impl( this );
192 pAppData_Impl->m_xImeStatusWindow->init();
194 RTL_LOGFILE_CONTEXT_TRACE( aLog, "{ initialize DDE" );
196 sal_Bool bOk = InitializeDde();
198 #ifdef DBG_UTIL
199 if( !bOk )
201 OStringBuffer aStr(
202 RTL_CONSTASCII_STRINGPARAM("No DDE-Service possible. Error: "));
203 if( GetDdeService() )
204 aStr.append(static_cast<sal_Int32>(GetDdeService()->GetError()));
205 else
206 aStr.append('?');
207 DBG_ASSERT( sal_False, aStr.getStr() );
209 #else
210 (void)bOk;
211 #endif
213 #if HAVE_FEATURE_DESKTOP
214 pSfxHelp = new SfxHelp;
215 #endif
217 #ifndef DISABLE_SCRIPTING
218 pBasic = new BasicDLL;
219 StarBASIC::SetGlobalErrorHdl( LINK( this, SfxApplication, GlobalBasicErrorHdl_Impl ) );
220 #endif
221 RTL_LOGFILE_CONTEXT_TRACE( aLog, "} initialize DDE" );
224 SfxApplication::~SfxApplication()
226 OSL_ENSURE( GetObjectShells_Impl().size() == 0, "Memory leak: some object shells were not removed!" );
228 Broadcast( SfxSimpleHint(SFX_HINT_DYING) );
230 SfxModule::DestroyModules_Impl();
232 #if HAVE_FEATURE_DESKTOP
233 delete pSfxHelp;
234 Application::SetHelp( NULL );
235 #endif
237 // delete global options
238 SvtViewOptions::ReleaseOptions();
240 #ifndef DISABLE_SCRIPTING
241 delete pBasic;
242 #endif
243 if ( !pAppData_Impl->bDowning )
244 Deinitialize();
246 delete pAppData_Impl;
247 pApp = 0;
250 //====================================================================
252 const String& SfxApplication::GetLastDir_Impl() const
254 /* [Description]
256 Internal method by which the last set directory with the method
257 <SfxApplication::SetLastDir_Impl()> in SFX is returned.
259 This is usually the most recently addressed by the
260 SfxFileDialog directory.
262 [Cross-reference]
263 <SfxApplication::SetLastDir_Impl()>
267 return pAppData_Impl->aLastDir;
270 const String& SfxApplication::GetLastSaveDirectory() const
272 /* [Description]
274 As <SfxApplication::GetLastDir_Impl()>, only external
276 [Cross-reference]
277 <SfxApplication::GetLastDir_Impl()>
281 return GetLastDir_Impl();
284 //--------------------------------------------------------------------
286 void SfxApplication::SetLastDir_Impl
288 const String& rNewDir /* Complete directory path as a string */
291 /* [Description]
293 Internal Method, by which a directory path is set that was last addressed
294 (eg by the SfxFileDialog).
296 [Cross-reference]
297 <SfxApplication::GetLastDir_Impl()>
301 pAppData_Impl->aLastDir = rNewDir;
304 //--------------------------------------------------------------------
306 void SfxApplication::ResetLastDir()
308 String aEmpty;
309 pAppData_Impl->aLastDir = aEmpty;
312 //--------------------------------------------------------------------
314 SfxDispatcher* SfxApplication::GetDispatcher_Impl()
316 return pAppData_Impl->pViewFrame? pAppData_Impl->pViewFrame->GetDispatcher(): pAppData_Impl->pAppDispat;
319 //--------------------------------------------------------------------
320 void SfxApplication::SetViewFrame_Impl( SfxViewFrame *pFrame )
322 if ( pFrame != pAppData_Impl->pViewFrame )
324 // get the containerframes ( if one of the frames is an InPlaceFrame )
325 SfxViewFrame *pOldContainerFrame = pAppData_Impl->pViewFrame;
326 while ( pOldContainerFrame && pOldContainerFrame->GetParentViewFrame_Impl() )
327 pOldContainerFrame = pOldContainerFrame->GetParentViewFrame_Impl();
328 SfxViewFrame *pNewContainerFrame = pFrame;
329 while ( pNewContainerFrame && pNewContainerFrame->GetParentViewFrame_Impl() )
330 pNewContainerFrame = pNewContainerFrame->GetParentViewFrame_Impl();
332 // DocWinActivate : both frames belong to the same TopWindow
333 // TopWinActivate : both frames belong to different TopWindows
335 sal_Bool bTaskActivate = pOldContainerFrame != pNewContainerFrame;
337 if ( pOldContainerFrame )
339 if ( bTaskActivate )
340 NotifyEvent( SfxViewEventHint( SFX_EVENT_DEACTIVATEDOC, GlobalEventConfig::GetEventName(STR_EVENT_DEACTIVATEDOC), pOldContainerFrame->GetObjectShell(), pOldContainerFrame->GetFrame().GetController() ) );
341 pOldContainerFrame->DoDeactivate( bTaskActivate, pFrame );
343 if( pOldContainerFrame->GetProgress() )
344 pOldContainerFrame->GetProgress()->Suspend();
347 pAppData_Impl->pViewFrame = pFrame;
349 if( pNewContainerFrame )
351 pNewContainerFrame->DoActivate( bTaskActivate );
352 if ( bTaskActivate && pNewContainerFrame->GetObjectShell() )
354 pNewContainerFrame->GetObjectShell()->PostActivateEvent_Impl( pNewContainerFrame );
355 NotifyEvent(SfxViewEventHint(SFX_EVENT_ACTIVATEDOC, GlobalEventConfig::GetEventName(STR_EVENT_ACTIVATEDOC), pNewContainerFrame->GetObjectShell(), pNewContainerFrame->GetFrame().GetController() ) );
358 SfxProgress *pProgress = pNewContainerFrame->GetProgress();
359 if ( pProgress )
361 if( pProgress->IsSuspended() )
362 pProgress->Resume();
363 else
364 pProgress->SetState( pProgress->GetState() );
367 if ( pAppData_Impl->pViewFrame->GetViewShell() )
369 SfxDispatcher* pDisp = pAppData_Impl->pViewFrame->GetDispatcher();
370 pDisp->Flush();
371 pDisp->Update_Impl(sal_True);
376 // even if the frame actually didn't change, ensure its document is forwarded
377 // to SfxObjectShell::SetCurrentComponent.
378 // Otherwise, the CurrentComponent might not be correct, in case it has meanwhile
379 // been reset to some other document, by some non-SFX component.
380 // #i49133# / 2007-12-19 / frank.schoenheit@sun.com
381 if ( pFrame && pFrame->GetViewShell() )
382 pFrame->GetViewShell()->SetCurrentDocument();
385 //---------------------------------------------------------------------
387 ResMgr* SfxApplication::CreateResManager( const char *pPrefix )
389 return ResMgr::CreateResMgr(pPrefix);
392 //--------------------------------------------------------------------
394 ResMgr* SfxApplication::GetSfxResManager()
396 return SfxResId::GetResMgr();
399 //------------------------------------------------------------------------
401 void SfxApplication::SetProgress_Impl
403 SfxProgress *pProgress
407 DBG_ASSERT( ( !pAppData_Impl->pProgress && pProgress ) ||
408 ( pAppData_Impl->pProgress && !pProgress ),
409 "Progress acitivation/deacitivation mismatch" );
411 if ( pAppData_Impl->pProgress && pProgress )
413 pAppData_Impl->pProgress->Suspend();
414 pAppData_Impl->pProgress->UnLock();
415 delete pAppData_Impl->pProgress;
418 pAppData_Impl->pProgress = pProgress;
421 //------------------------------------------------------------------------
423 sal_uInt16 SfxApplication::GetFreeIndex()
425 return pAppData_Impl->aIndexBitSet.GetFreeIndex()+1;
428 //------------------------------------------------------------------------
430 void SfxApplication::ReleaseIndex(sal_uInt16 i)
432 pAppData_Impl->aIndexBitSet.ReleaseIndex(i-1);
435 //--------------------------------------------------------------------
437 Window* SfxApplication::GetTopWindow() const
439 SfxWorkWindow* pWork = GetWorkWindow_Impl( SfxViewFrame::Current() );
440 return pWork ? pWork->GetWindow() : NULL;
443 SfxTbxCtrlFactArr_Impl& SfxApplication::GetTbxCtrlFactories_Impl() const
445 return *pAppData_Impl->pTbxCtrlFac;
448 SfxStbCtrlFactArr_Impl& SfxApplication::GetStbCtrlFactories_Impl() const
450 return *pAppData_Impl->pStbCtrlFac;
453 SfxMenuCtrlFactArr_Impl& SfxApplication::GetMenuCtrlFactories_Impl() const
455 return *pAppData_Impl->pMenuCtrlFac;
458 SfxViewFrameArr_Impl& SfxApplication::GetViewFrames_Impl() const
460 return *pAppData_Impl->pViewFrames;
463 SfxViewShellArr_Impl& SfxApplication::GetViewShells_Impl() const
465 return *pAppData_Impl->pViewShells;
468 SfxObjectShellArr_Impl& SfxApplication::GetObjectShells_Impl() const
470 return *pAppData_Impl->pObjShells;
473 void SfxApplication::Invalidate( sal_uInt16 nId )
475 for( SfxViewFrame* pFrame = SfxViewFrame::GetFirst(); pFrame; pFrame = SfxViewFrame::GetNext( *pFrame ) )
476 Invalidate_Impl( pFrame->GetBindings(), nId );
479 #ifndef DISABLE_SCRIPTING
481 #ifndef DISABLE_DYNLOADING
483 typedef long (SAL_CALL *basicide_handle_basic_error)(void*);
484 typedef void* (SAL_CALL *basicide_macro_organizer)(sal_Int16);
486 extern "C" { static void SAL_CALL thisModule() {} }
488 #else
490 extern "C" long basicide_handle_basic_error(void*);
491 extern "C" void *basicide_macro_organizer(sal_Int16);
493 #endif
495 #endif
497 IMPL_LINK( SfxApplication, GlobalBasicErrorHdl_Impl, StarBASIC*, pStarBasic )
499 #ifdef DISABLE_SCRIPTING
500 (void) pStarBasic;
501 return 0;
502 #else
504 #ifndef DISABLE_DYNLOADING
505 // get basctl dllname
506 static OUString aLibName( SVLIBRARY( "basctl" ) );
508 // load module
509 oslModule handleMod = osl_loadModuleRelative(
510 &thisModule, aLibName.pData, 0 );
512 // get symbol
513 OUString aSymbol( "basicide_handle_basic_error" );
514 basicide_handle_basic_error pSymbol = (basicide_handle_basic_error) osl_getFunctionSymbol( handleMod, aSymbol.pData );
516 // call basicide_handle_basic_error in basctl
517 long nRet = pSymbol ? pSymbol( pStarBasic ) : 0;
519 #else
521 long nRet = basicide_handle_basic_error( pStarBasic );
523 #endif
525 return nRet;
527 #endif
530 sal_Bool SfxApplication::IsXScriptURL( const String& rScriptURL )
532 sal_Bool result = sal_False;
534 #ifdef DISABLE_SCRIPTING
535 (void) rScriptURL;
536 #else
537 ::com::sun::star::uno::Reference
538 < ::com::sun::star::uno::XComponentContext > xContext =
539 ::comphelper::getProcessComponentContext();
541 ::com::sun::star::uno::Reference
542 < ::com::sun::star::uri::XUriReferenceFactory >
543 xFactory = ::com::sun::star::uri::UriReferenceFactory::create( xContext );
547 ::com::sun::star::uno::Reference
548 < ::com::sun::star::uri::XVndSunStarScriptUrl >
549 xUrl( xFactory->parse( rScriptURL ),
550 ::com::sun::star::uno::UNO_QUERY );
552 if ( xUrl.is() )
554 result = sal_True;
557 catch (const ::com::sun::star::uno::RuntimeException&)
559 // ignore, will just return FALSE
561 #endif
562 return result;
565 OUString
566 SfxApplication::ChooseScript()
568 OUString aScriptURL;
570 #ifndef DISABLE_SCRIPTING
571 SfxAbstractDialogFactory* pFact = SfxAbstractDialogFactory::Create();
572 if ( pFact )
574 OSL_TRACE("create selector dialog");
576 const SfxViewFrame* pViewFrame = SfxViewFrame::Current();
577 const SfxFrame* pFrame = pViewFrame ? &pViewFrame->GetFrame() : NULL;
578 uno::Reference< frame::XFrame > xFrame( pFrame ? pFrame->GetFrameInterface() : uno::Reference< frame::XFrame >() );
580 AbstractScriptSelectorDialog* pDlg =
581 pFact->CreateScriptSelectorDialog( NULL, sal_False, xFrame );
583 OSL_TRACE("done, now exec it");
585 sal_uInt16 nRet = pDlg->Execute();
587 OSL_TRACE("has returned");
589 if ( nRet == RET_OK )
591 aScriptURL = pDlg->GetScriptURL();
594 delete pDlg;
596 #endif
597 return aScriptURL;
600 void SfxApplication::MacroOrganizer( sal_Int16 nTabId )
602 #ifdef DISABLE_SCRIPTING
603 (void) nTabId;
604 #else
606 #ifndef DISABLE_DYNLOADING
607 // get basctl dllname
608 static OUString aLibName( SVLIBRARY( "basctl" ) );
610 // load module
611 oslModule handleMod = osl_loadModuleRelative(
612 &thisModule, aLibName.pData, 0 );
614 // get symbol
615 OUString aSymbol( "basicide_macro_organizer" );
616 basicide_macro_organizer pSymbol = (basicide_macro_organizer) osl_getFunctionSymbol( handleMod, aSymbol.pData );
618 // call basicide_macro_organizer in basctl
619 pSymbol( nTabId );
621 #else
623 basicide_macro_organizer( nTabId );
625 #endif
627 #endif
630 ErrCode SfxApplication::CallBasic( const String& rCode, BasicManager* pMgr, SbxArray* pArgs, SbxValue* pRet )
632 #ifdef DISABLE_SCRIPTING
633 (void) rCode;
634 (void) pMgr;
635 (void) pArgs;
636 (void) pRet;
637 return ERRCODE_BASIC_CANNOT_LOAD;
638 #else
639 return pMgr->ExecuteMacro( rCode, pArgs, pRet);
640 #endif
643 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */