merge the formfield patch from ooo-build
[ooovba.git] / sfx2 / source / appl / shutdownicon.cxx
blobcf9bd9302acf0700b490e0f3dbd31eb57707c8d3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: shutdownicon.cxx,v $
10 * $Revision: 1.64 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sfx2.hxx"
34 #include <shutdownicon.hxx>
35 #include <app.hrc>
36 #include <sfx2/app.hxx>
37 #include <vos/mutex.hxx>
38 #include <svtools/imagemgr.hxx>
39 // #include <cmdlineargs.hxx>
40 #include <com/sun/star/task/XInteractionHandler.hpp>
41 #include <com/sun/star/frame/XDispatchResultListener.hpp>
42 #include <com/sun/star/frame/XNotifyingDispatch.hpp>
43 #include <com/sun/star/frame/XFramesSupplier.hpp>
44 #include <com/sun/star/frame/XComponentLoader.hpp>
45 #include <com/sun/star/frame/XFrame.hpp>
46 #include <com/sun/star/util/XURLTransformer.hpp>
47 #include <com/sun/star/frame/XFramesSupplier.hpp>
48 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
49 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
50 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
51 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
52 #include <com/sun/star/ui/dialogs/ControlActions.hpp>
53 #include <com/sun/star/document/MacroExecMode.hpp>
54 #include <com/sun/star/document/UpdateDocMode.hpp>
55 #include <sfx2/filedlghelper.hxx>
56 #include <sfx2/fcontnr.hxx>
57 #ifndef _UNOTOOLS_PROCESSFACTORY_HXX
58 #include <comphelper/processfactory.hxx>
59 #endif
60 #include <cppuhelper/compbase1.hxx>
61 #include <sfx2/dispatch.hxx>
62 #include <comphelper/extract.hxx>
63 #include <tools/urlobj.hxx>
64 #include <osl/security.hxx>
65 #include <osl/file.hxx>
66 #include <rtl/bootstrap.hxx>
67 #include <tools/link.hxx>
68 #ifdef UNX // need symlink
69 #include <unistd.h>
70 #include <errno.h>
71 #endif
73 #include "sfxresid.hxx"
75 using namespace ::com::sun::star::uno;
76 using namespace ::com::sun::star::frame;
77 using namespace ::com::sun::star::container;
78 using namespace ::com::sun::star::io;
79 using namespace ::com::sun::star::lang;
80 using namespace ::com::sun::star::beans;
81 using namespace ::com::sun::star::util;
82 using namespace ::com::sun::star::ui::dialogs;
83 using namespace ::vos;
84 using namespace ::rtl;
85 using namespace ::sfx2;
87 #ifdef ENABLE_QUICKSTART_APPLET
88 # if !defined(WIN32) && !defined(QUARTZ)
89 extern "C" { static void SAL_CALL thisModule() {} }
90 # endif
91 #endif
93 class SfxNotificationListener_Impl : public cppu::WeakImplHelper1< XDispatchResultListener >
95 public:
96 virtual void SAL_CALL dispatchFinished( const DispatchResultEvent& aEvent ) throw( RuntimeException );
97 virtual void SAL_CALL disposing( const EventObject& aEvent ) throw( RuntimeException );
100 void SAL_CALL SfxNotificationListener_Impl::dispatchFinished( const DispatchResultEvent& ) throw( RuntimeException )
102 ShutdownIcon::LeaveModalMode();
105 void SAL_CALL SfxNotificationListener_Impl::disposing( const EventObject& ) throw( RuntimeException )
109 SFX_IMPL_XSERVICEINFO( ShutdownIcon, "com.sun.star.office.Quickstart", "com.sun.star.comp.desktop.QuickstartWrapper" ) \
110 SFX_IMPL_ONEINSTANCEFACTORY( ShutdownIcon );
112 bool ShutdownIcon::bModalMode = false;
113 ShutdownIcon* ShutdownIcon::pShutdownIcon = NULL;
115 // To remove conditionals
116 extern "C" {
117 static void disabled_initSystray() { }
118 static void disabled_deInitSystray() { }
120 #define DOSTRING( x ) #x
121 #define STRING( x ) DOSTRING( x )
123 bool ShutdownIcon::LoadModule( osl::Module **pModule,
124 oslGenericFunction *pInit,
125 oslGenericFunction *pDeInit )
127 if ( pModule )
129 OSL_ASSERT ( pInit && pDeInit );
130 *pInit = *pDeInit = NULL;
131 *pModule = NULL;
134 #ifdef ENABLE_QUICKSTART_APPLET
135 # ifdef WIN32
136 if ( pModule )
138 *pInit = win32_init_sys_tray;
139 *pDeInit = win32_shutdown_sys_tray;
141 return true;
142 # elif defined QUARTZ
143 *pInit = aqua_init_systray;
144 *pDeInit = aqua_shutdown_systray;
145 return true;
146 # else // UNX
147 osl::Module *pPlugin;
148 pPlugin = new osl::Module();
150 oslGenericFunction pTmpInit = NULL;
151 oslGenericFunction pTmpDeInit = NULL;
152 if ( pPlugin->loadRelative( &thisModule, OUString (RTL_CONSTASCII_USTRINGPARAM( STRING( PLUGIN_NAME ) ) ) ) )
154 pTmpInit = pPlugin->getFunctionSymbol(
155 OUString( RTL_CONSTASCII_USTRINGPARAM( "plugin_init_sys_tray" ) ) );
156 pTmpDeInit = pPlugin->getFunctionSymbol(
157 OUString( RTL_CONSTASCII_USTRINGPARAM( "plugin_shutdown_sys_tray" ) ) );
159 if ( !pTmpInit || !pTmpDeInit )
161 delete pPlugin;
162 pPlugin = NULL;
164 if ( pModule )
166 *pModule = pPlugin;
167 *pInit = pTmpInit;
168 *pDeInit = pTmpDeInit;
170 else
172 bool bRet = pPlugin != NULL;
173 delete pPlugin;
174 return bRet;
176 # endif // UNX
177 #endif // ENABLE_QUICKSTART_APPLET
178 if ( pModule )
180 if ( !*pInit )
181 *pInit = disabled_initSystray;
182 if ( !*pDeInit )
183 *pDeInit = disabled_deInitSystray;
186 return true;
189 void ShutdownIcon::initSystray()
191 if (m_bInitialized)
192 return;
193 m_bInitialized = true;
195 if (!m_pPlugin)
196 (void) LoadModule( &m_pPlugin, &m_pInitSystray, &m_pDeInitSystray );
198 m_bVeto = true;
199 m_pInitSystray();
202 void ShutdownIcon::deInitSystray()
204 if (!m_bInitialized)
205 return;
207 if (m_pDeInitSystray)
208 m_pDeInitSystray();
210 m_bVeto = false;
211 delete m_pFileDlg;
212 m_pFileDlg = NULL;
213 m_bInitialized = false;
217 ShutdownIcon::ShutdownIcon( Reference< XMultiServiceFactory > aSMgr ) :
218 ShutdownIconServiceBase( m_aMutex ),
219 m_bVeto ( false ),
220 m_bListenForTermination ( false ),
221 m_pResMgr( NULL ),
222 m_pFileDlg( NULL ),
223 m_xServiceManager( aSMgr ),
224 m_pInitSystray( 0 ),
225 m_pDeInitSystray( 0 ),
226 m_pPlugin( 0 ),
227 m_bInitialized( false )
231 ShutdownIcon::~ShutdownIcon()
233 deInitSystray();
234 //Bustage on dlclosing when: no qstarter, start writer, enable, close writer, choose disable/quit
235 //dlclose gets called and pulls .so from under hack-tower
236 #ifndef UNX
237 delete m_pPlugin;
238 #endif
241 // ---------------------------------------------------------------------------
243 void ShutdownIcon::OpenURL( const ::rtl::OUString& aURL, const ::rtl::OUString& rTarget, const Sequence< PropertyValue >& aArgs )
245 if ( getInstance() && getInstance()->m_xDesktop.is() )
247 Reference < XDispatchProvider > xDispatchProvider( getInstance()->m_xDesktop, UNO_QUERY );
248 if ( xDispatchProvider.is() )
250 com::sun::star::util::URL aDispatchURL;
251 aDispatchURL.Complete = aURL;
253 Reference < com::sun::star::util::XURLTransformer > xURLTransformer(
254 ::comphelper::getProcessServiceFactory()->createInstance( OUString::createFromAscii("com.sun.star.util.URLTransformer") ),
255 com::sun::star::uno::UNO_QUERY );
256 if ( xURLTransformer.is() )
260 Reference< com::sun::star::frame::XDispatch > xDispatch;
262 xURLTransformer->parseStrict( aDispatchURL );
263 xDispatch = xDispatchProvider->queryDispatch( aDispatchURL, rTarget, 0 );
264 if ( xDispatch.is() )
265 xDispatch->dispatch( aDispatchURL, aArgs );
267 catch ( com::sun::star::uno::RuntimeException& )
269 throw;
271 catch ( com::sun::star::uno::Exception& )
279 // ---------------------------------------------------------------------------
281 void ShutdownIcon::FileOpen()
283 if ( getInstance() && getInstance()->m_xDesktop.is() )
285 ::vos::OGuard aGuard( Application::GetSolarMutex() );
286 EnterModalMode();
287 getInstance()->StartFileDialog();
291 // ---------------------------------------------------------------------------
293 void ShutdownIcon::FromTemplate()
295 if ( getInstance() && getInstance()->m_xDesktop.is() )
297 Reference < ::com::sun::star::frame::XFramesSupplier > xDesktop ( getInstance()->m_xDesktop, UNO_QUERY);
298 Reference < ::com::sun::star::frame::XFrame > xFrame( xDesktop->getActiveFrame() );
299 if ( !xFrame.is() )
300 xFrame = Reference < ::com::sun::star::frame::XFrame >( xDesktop, UNO_QUERY );
302 URL aTargetURL;
303 aTargetURL.Complete = OUString( RTL_CONSTASCII_USTRINGPARAM( "slot:5500" ) );
304 Reference < XURLTransformer > xTrans( ::comphelper::getProcessServiceFactory()->createInstance( rtl::OUString::createFromAscii("com.sun.star.util.URLTransformer" )), UNO_QUERY );
305 xTrans->parseStrict( aTargetURL );
307 Reference < ::com::sun::star::frame::XDispatchProvider > xProv( xFrame, UNO_QUERY );
308 Reference < ::com::sun::star::frame::XDispatch > xDisp;
309 if ( xProv.is() )
311 if ( aTargetURL.Protocol.compareToAscii("slot:") == COMPARE_EQUAL )
312 xDisp = xProv->queryDispatch( aTargetURL, ::rtl::OUString(), 0 );
313 else
314 xDisp = xProv->queryDispatch( aTargetURL, ::rtl::OUString::createFromAscii("_blank"), 0 );
316 if ( xDisp.is() )
318 Sequence<PropertyValue> aArgs(1);
319 PropertyValue* pArg = aArgs.getArray();
320 pArg[0].Name = rtl::OUString::createFromAscii("Referer");
321 pArg[0].Value <<= ::rtl::OUString::createFromAscii("private:user");
322 Reference< ::com::sun::star::frame::XNotifyingDispatch > xNotifyer( xDisp, UNO_QUERY );
323 if ( xNotifyer.is() )
325 EnterModalMode();
326 xNotifyer->dispatchWithNotification( aTargetURL, aArgs, new SfxNotificationListener_Impl() );
328 else
329 xDisp->dispatch( aTargetURL, aArgs );
334 // ---------------------------------------------------------------------------
335 #include <tools/rcid.h>
336 OUString ShutdownIcon::GetResString( int id )
338 ::vos::OGuard aGuard( Application::GetSolarMutex() );
340 if( ! m_pResMgr )
341 m_pResMgr = SfxResId::GetResMgr();
342 ResId aResId( id, *m_pResMgr );
343 aResId.SetRT( RSC_STRING );
344 if( !m_pResMgr || !m_pResMgr->IsAvailable( aResId ) )
345 return OUString();
347 UniString aRes( ResId(id, *m_pResMgr) );
348 return OUString( aRes );
351 // ---------------------------------------------------------------------------
353 OUString ShutdownIcon::GetUrlDescription( const OUString& aUrl )
355 ::vos::OGuard aGuard( Application::GetSolarMutex() );
357 return OUString( SvFileInformationManager::GetDescription( INetURLObject( aUrl ) ) );
360 // ---------------------------------------------------------------------------
362 void ShutdownIcon::StartFileDialog()
364 ::vos::OGuard aGuard( Application::GetSolarMutex() );
366 if ( !m_pFileDlg )
367 m_pFileDlg = new FileDialogHelper( WB_OPEN | SFXWB_MULTISELECTION, String() );
368 m_pFileDlg->StartExecuteModal( STATIC_LINK( this, ShutdownIcon, DialogClosedHdl_Impl ) );
371 // ---------------------------------------------------------------------------
373 IMPL_STATIC_LINK( ShutdownIcon, DialogClosedHdl_Impl, FileDialogHelper*, EMPTYARG )
375 DBG_ASSERT( pThis->m_pFileDlg, "ShutdownIcon, DialogClosedHdl_Impl(): no file dialog" );
377 // use ctor for filling up filters automatically! #89169#
378 if ( ERRCODE_NONE == pThis->m_pFileDlg->GetError() )
380 Reference< XFilePicker > xPicker = pThis->m_pFileDlg->GetFilePicker();
385 if ( xPicker.is() )
388 Reference < XFilePickerControlAccess > xPickerControls ( xPicker, UNO_QUERY );
389 Reference < XFilterManager > xFilterManager ( xPicker, UNO_QUERY );
391 Sequence< OUString > sFiles = xPicker->getFiles();
392 int nFiles = sFiles.getLength();
394 int nArgs=3;
395 Sequence< PropertyValue > aArgs(3);
397 Reference < com::sun::star::task::XInteractionHandler > xInteraction(
398 ::comphelper::getProcessServiceFactory()->createInstance( OUString::createFromAscii("com.sun.star.task.InteractionHandler") ),
399 com::sun::star::uno::UNO_QUERY );
401 aArgs[0].Name = OUString::createFromAscii( "InteractionHandler" );
402 aArgs[0].Value <<= xInteraction;
404 sal_Int16 nMacroExecMode = ::com::sun::star::document::MacroExecMode::USE_CONFIG;
405 aArgs[1].Name = OUString::createFromAscii( "MacroExecutionMode" );
406 aArgs[1].Value <<= nMacroExecMode;
408 sal_Int16 nUpdateDoc = ::com::sun::star::document::UpdateDocMode::ACCORDING_TO_CONFIG;
409 aArgs[2].Name = OUString::createFromAscii( "UpdateDocMode" );
410 aArgs[2].Value <<= nUpdateDoc;
412 // pb: #102643# use the filedlghelper to get the current filter name,
413 // because it removes the extensions before you get the filter name.
414 OUString aFilterName( pThis->m_pFileDlg->GetCurrentFilter() );
416 if ( xPickerControls.is() )
419 // Set readonly flag
421 sal_Bool bReadOnly = sal_False;
424 xPickerControls->getValue( ExtendedFilePickerElementIds::CHECKBOX_READONLY, 0 ) >>= bReadOnly;
426 // #95239#: Only set porperty if readonly is set to TRUE
428 if ( bReadOnly )
430 aArgs.realloc( ++nArgs );
431 aArgs[nArgs-1].Name = OUString::createFromAscii( "ReadOnly" );
432 aArgs[nArgs-1].Value <<= bReadOnly;
435 // Get version string
437 sal_Int32 iVersion = -1;
439 xPickerControls->getValue( ExtendedFilePickerElementIds::LISTBOX_VERSION, ControlActions::GET_SELECTED_ITEM_INDEX ) >>= iVersion;
441 if ( iVersion >= 0 )
443 sal_Int16 uVersion = (sal_Int16)iVersion;
445 aArgs.realloc( ++nArgs );
446 aArgs[nArgs-1].Name = OUString::createFromAscii( "Version" );
447 aArgs[nArgs-1].Value <<= uVersion;
450 // Retrieve the current filter
452 if ( !aFilterName.getLength() )
453 xPickerControls->getValue( CommonFilePickerElementIds::LISTBOX_FILTER, ControlActions::GET_SELECTED_ITEM ) >>= aFilterName;
458 // Convert UI filter name to internal filter name
460 if ( aFilterName.getLength() )
462 const SfxFilter* pFilter = SFX_APP()->GetFilterMatcher().GetFilter4UIName( aFilterName, 0, SFX_FILTER_NOTINFILEDLG );
464 if ( pFilter )
466 aFilterName = pFilter->GetFilterName();
468 if ( aFilterName.getLength() )
470 aArgs.realloc( ++nArgs );
471 aArgs[nArgs-1].Name = OUString::createFromAscii( "FilterName" );
472 aArgs[nArgs-1].Value <<= aFilterName;
477 if ( 1 == nFiles )
478 OpenURL( sFiles[0], OUString( RTL_CONSTASCII_USTRINGPARAM( "_default" ) ), aArgs );
479 else
481 OUString aBaseDirURL = sFiles[0];
482 if ( aBaseDirURL.getLength() > 0 && aBaseDirURL[aBaseDirURL.getLength()-1] != '/' )
483 aBaseDirURL += OUString::createFromAscii("/");
485 int iFiles;
486 for ( iFiles = 1; iFiles < nFiles; iFiles++ )
488 OUString aURL = aBaseDirURL;
489 aURL += sFiles[iFiles];
490 OpenURL( aURL, OUString( RTL_CONSTASCII_USTRINGPARAM( "_default" ) ), aArgs );
495 catch ( ... )
500 #ifdef WNT
501 // #103346 Destroy dialog to prevent problems with custom controls
502 delete pThis->m_pFileDlg;
503 pThis->m_pFileDlg = NULL;
504 #endif
506 LeaveModalMode();
507 return 0;
510 // ---------------------------------------------------------------------------
512 void ShutdownIcon::addTerminateListener()
514 ShutdownIcon* pInst = getInstance();
515 if ( ! pInst)
516 return;
518 if (pInst->m_bListenForTermination)
519 return;
521 Reference< XDesktop > xDesktop = pInst->m_xDesktop;
522 if ( ! xDesktop.is())
523 return;
525 xDesktop->addTerminateListener( pInst );
526 pInst->m_bListenForTermination = true;
529 // ---------------------------------------------------------------------------
531 void ShutdownIcon::terminateDesktop()
533 ShutdownIcon* pInst = getInstance();
534 if ( ! pInst)
535 return;
537 Reference< XDesktop > xDesktop = pInst->m_xDesktop;
538 if ( ! xDesktop.is())
539 return;
541 // always remove ourselves as listener
542 xDesktop->removeTerminateListener( pInst );
543 pInst->m_bListenForTermination = true;
545 // terminate desktop only if no tasks exist
546 Reference< XFramesSupplier > xSupplier( xDesktop, UNO_QUERY );
547 if ( xSupplier.is() )
549 Reference< XIndexAccess > xTasks ( xSupplier->getFrames(), UNO_QUERY );
550 if( xTasks.is() )
552 if( xTasks->getCount() < 1 )
553 xDesktop->terminate();
557 // remove the instance pointer
558 ShutdownIcon::pShutdownIcon = 0;
561 // ---------------------------------------------------------------------------
563 ShutdownIcon* ShutdownIcon::getInstance()
565 OSL_ASSERT( pShutdownIcon );
566 return pShutdownIcon;
569 // ---------------------------------------------------------------------------
571 ShutdownIcon* ShutdownIcon::createInstance()
573 if (pShutdownIcon)
574 return pShutdownIcon;
576 ShutdownIcon *pIcon = NULL;
577 try {
578 Reference< XMultiServiceFactory > xSMgr( comphelper::getProcessServiceFactory() );
579 pIcon = new ShutdownIcon( xSMgr );
580 pIcon->init ();
581 pShutdownIcon = pIcon;
582 } catch (...) {
583 delete pIcon;
586 return pShutdownIcon;
589 void ShutdownIcon::init() throw( ::com::sun::star::uno::Exception )
591 // access resource system and sfx only protected by solarmutex
592 vos::OGuard aSolarGuard( Application::GetSolarMutex() );
593 ResMgr *pResMgr = SfxResId::GetResMgr();
595 ::osl::ResettableMutexGuard aGuard( m_aMutex );
596 m_pResMgr = pResMgr;
597 aGuard.clear();
598 Reference < XDesktop > xDesktop( m_xServiceManager->createInstance(
599 DEFINE_CONST_UNICODE( "com.sun.star.frame.Desktop" )),
600 UNO_QUERY );
601 aGuard.reset();
602 m_xDesktop = xDesktop;
605 // ---------------------------------------------------------------------------
607 void SAL_CALL ShutdownIcon::disposing()
609 m_xServiceManager = Reference< XMultiServiceFactory >();
610 m_xDesktop = Reference< XDesktop >();
613 // ---------------------------------------------------------------------------
615 // XEventListener
616 void SAL_CALL ShutdownIcon::disposing( const ::com::sun::star::lang::EventObject& )
617 throw(::com::sun::star::uno::RuntimeException)
621 // ---------------------------------------------------------------------------
623 // XTerminateListener
624 void SAL_CALL ShutdownIcon::queryTermination( const ::com::sun::star::lang::EventObject& )
625 throw(::com::sun::star::frame::TerminationVetoException, ::com::sun::star::uno::RuntimeException)
627 ::osl::ClearableMutexGuard aGuard( m_aMutex );
629 if ( m_bVeto )
630 throw ::com::sun::star::frame::TerminationVetoException();
634 // ---------------------------------------------------------------------------
636 void SAL_CALL ShutdownIcon::notifyTermination( const ::com::sun::star::lang::EventObject& )
637 throw(::com::sun::star::uno::RuntimeException)
642 // ---------------------------------------------------------------------------
644 void SAL_CALL ShutdownIcon::initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any>& aArguments )
645 throw( ::com::sun::star::uno::Exception )
647 ::osl::ResettableMutexGuard aGuard( m_aMutex );
649 // third argument only sets veto, everything else will be ignored!
650 if (aArguments.getLength() > 2)
652 sal_Bool bVeto = sal_True;
653 bVeto = ::cppu::any2bool(aArguments[2]);
654 m_bVeto = bVeto;
655 return;
658 if ( aArguments.getLength() > 0 )
660 if ( !ShutdownIcon::pShutdownIcon )
664 sal_Bool bQuickstart = sal_False;
665 bQuickstart = ::cppu::any2bool( aArguments[0] );
666 if( !bQuickstart && !GetAutostart() )
667 return;
668 aGuard.clear();
669 init ();
670 aGuard.reset();
671 if ( !m_xDesktop.is() )
672 return;
674 /* Create a sub-classed instance - foo */
675 ShutdownIcon::pShutdownIcon = this;
676 initSystray();
677 #ifdef OS2
678 // above win32 starts the quickstart thread, but we have
679 // quickstart running only when -quickstart is specified
680 // on command line (next boot).
681 // so if -quickstart was not specified, we cannot issue
682 // quickstart veto on shutdown.
683 if (bQuickstart)
685 // disable shutdown
686 ShutdownIcon::getInstance()->SetVeto( true );
687 ShutdownIcon::getInstance()->addTerminateListener();
689 #endif
691 catch(const ::com::sun::star::lang::IllegalArgumentException&)
696 if ( aArguments.getLength() > 1 )
698 sal_Bool bAutostart = sal_False;
699 bAutostart = ::cppu::any2bool( aArguments[1] );
700 if (bAutostart && !GetAutostart())
701 SetAutostart( sal_True );
702 if (!bAutostart && GetAutostart())
703 SetAutostart( sal_False );
708 // -------------------------------
710 void ShutdownIcon::EnterModalMode()
712 bModalMode = TRUE;
715 // -------------------------------
717 void ShutdownIcon::LeaveModalMode()
719 bModalMode = FALSE;
722 #ifdef WNT
723 // defined in shutdowniconw32.cxx
724 #elif defined(OS2)
725 // defined in shutdowniconOs2.cxx
726 #elif defined QUARTZ
727 // defined in shutdowniconaqua.cxx
728 #else
729 bool ShutdownIcon::IsQuickstarterInstalled()
731 #ifndef ENABLE_QUICKSTART_APPLET
732 return false;
733 #else // !ENABLE_QUICKSTART_APPLET
734 #ifdef UNX
735 return LoadModule( NULL, NULL, NULL);
736 #endif // UNX
737 #endif // !ENABLE_QUICKSTART_APPLET
739 #endif // !WNT
741 // ---------------------------------------------------------------------------
743 #if defined (ENABLE_QUICKSTART_APPLET) && defined (UNX)
744 static OUString getDotAutostart( bool bCreate = false )
746 OUString aShortcut;
747 const char *pConfigHome;
748 if( (pConfigHome = getenv("XDG_CONFIG_HOME") ) )
749 aShortcut = OStringToOUString( OString( pConfigHome ), RTL_TEXTENCODING_UTF8 );
750 else
752 OUString aHomeURL;
753 osl::Security().getHomeDir( aHomeURL );
754 ::osl::File::getSystemPathFromFileURL( aHomeURL, aShortcut );
755 aShortcut += OUString( RTL_CONSTASCII_USTRINGPARAM( "/.config" ) );
757 aShortcut += OUString( RTL_CONSTASCII_USTRINGPARAM( "/autostart" ) );
758 if (bCreate)
760 OUString aShortcutUrl;
761 osl::File::getFileURLFromSystemPath( aShortcut, aShortcutUrl );
762 osl::Directory::createPath( aShortcutUrl );
764 return aShortcut;
766 #endif
768 rtl::OUString ShutdownIcon::getShortcutName()
770 #ifndef ENABLE_QUICKSTART_APPLET
771 return OUString();
772 #else
774 OUString aShortcutName( RTL_CONSTASCII_USTRINGPARAM( "StarOffice 6.0" ) );
775 ResMgr* pMgr = SfxResId::GetResMgr();
776 if( pMgr )
778 ::vos::OGuard aGuard( Application::GetSolarMutex() );
779 UniString aRes( SfxResId( STR_QUICKSTART_LNKNAME ) );
780 aShortcutName = OUString( aRes );
782 #ifdef WNT
783 aShortcutName += OUString( RTL_CONSTASCII_USTRINGPARAM( ".lnk" ) );
785 OUString aShortcut(GetAutostartFolderNameW32());
786 aShortcut += OUString( RTL_CONSTASCII_USTRINGPARAM( "\\" ) );
787 aShortcut += aShortcutName;
788 #else // UNX
789 OUString aShortcut = getDotAutostart();
790 aShortcut += OUString( RTL_CONSTASCII_USTRINGPARAM( "/qstart.desktop" ) );
791 #endif // UNX
792 return aShortcut;
793 #endif // ENABLE_QUICKSTART_APPLET
796 bool ShutdownIcon::GetAutostart( )
798 #if defined(OS2)
799 return GetAutostartOs2( );
800 #elif defined QUARTZ
801 return true;
802 #else
803 bool bRet = false;
804 #ifdef ENABLE_QUICKSTART_APPLET
805 OUString aShortcut( getShortcutName() );
806 OUString aShortcutUrl;
807 osl::File::getFileURLFromSystemPath( aShortcut, aShortcutUrl );
808 osl::File f( aShortcutUrl );
809 osl::File::RC error = f.open( OpenFlag_Read );
810 if( error == osl::File::E_None )
812 f.close();
813 bRet = true;
815 #endif // ENABLE_QUICKSTART_APPLET
816 return bRet;
817 #endif
820 void ShutdownIcon::SetAutostart( bool bActivate )
822 #ifdef ENABLE_QUICKSTART_APPLET
823 OUString aShortcut( getShortcutName() );
825 if( bActivate && IsQuickstarterInstalled() )
827 #ifdef WNT
828 EnableAutostartW32( aShortcut );
829 #else // UNX
830 getDotAutostart( true );
832 OUString aPath( RTL_CONSTASCII_USTRINGPARAM("${BRAND_BASE_DIR}/share/xdg/qstart.desktop" ) );
833 Bootstrap::expandMacros( aPath );
835 OUString aDesktopFile;
836 ::osl::File::getSystemPathFromFileURL( aPath, aDesktopFile );
838 OString aDesktopFileUnx = OUStringToOString( aDesktopFile,
839 osl_getThreadTextEncoding() );
840 OString aShortcutUnx = OUStringToOString( aShortcut,
841 osl_getThreadTextEncoding() );
842 if ((0 != symlink(aDesktopFileUnx, aShortcutUnx)) && (errno == EEXIST))
844 unlink(aShortcutUnx);
845 symlink(aDesktopFileUnx, aShortcutUnx);
848 ShutdownIcon *pIcon = ShutdownIcon::createInstance();
849 if( pIcon )
850 pIcon->initSystray();
851 #endif // UNX
853 else
855 OUString aShortcutUrl;
856 ::osl::File::getFileURLFromSystemPath( aShortcut, aShortcutUrl );
857 ::osl::File::remove( aShortcutUrl );
858 #ifdef UNX
859 ShutdownIcon *pIcon = getInstance();
860 if( pIcon )
861 pIcon->deInitSystray();
862 #endif
864 #elif defined OS2
865 SetAutostartOs2( bActivate );
866 #else
867 (void)bActivate; // unused variable
868 #endif // ENABLE_QUICKSTART_APPLET
871 static const ::sal_Int32 PROPHANDLE_TERMINATEVETOSTATE = 0;
873 // XFastPropertySet
874 void SAL_CALL ShutdownIcon::setFastPropertyValue( ::sal_Int32 nHandle,
875 const ::com::sun::star::uno::Any& aValue )
876 throw (::com::sun::star::beans::UnknownPropertyException,
877 ::com::sun::star::beans::PropertyVetoException,
878 ::com::sun::star::lang::IllegalArgumentException,
879 ::com::sun::star::lang::WrappedTargetException,
880 ::com::sun::star::uno::RuntimeException)
882 switch(nHandle)
884 case PROPHANDLE_TERMINATEVETOSTATE :
886 // use new value in case it's a valid information only
887 ::sal_Bool bState( sal_False );
888 if (! (aValue >>= bState))
889 return;
891 m_bVeto = bState;
892 if (m_bVeto && ! m_bListenForTermination)
893 addTerminateListener();
895 break;
897 default :
898 throw ::com::sun::star::beans::UnknownPropertyException();
902 // XFastPropertySet
903 ::com::sun::star::uno::Any SAL_CALL ShutdownIcon::getFastPropertyValue( ::sal_Int32 nHandle )
904 throw (::com::sun::star::beans::UnknownPropertyException,
905 ::com::sun::star::lang::WrappedTargetException,
906 ::com::sun::star::uno::RuntimeException)
908 ::com::sun::star::uno::Any aValue;
909 switch(nHandle)
911 case PROPHANDLE_TERMINATEVETOSTATE :
913 bool bState = (m_bListenForTermination && m_bVeto);
914 aValue <<= bState;
916 break;
918 default :
919 throw ::com::sun::star::beans::UnknownPropertyException();
922 return aValue;