update dev300-m58
[ooovba.git] / framework / source / dispatch / windowcommanddispatch.cxx
blob531d6807ee9018b460103ee2a933756cdc7fd12e
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: windowcommanddispatch.cxx,v $
10 * $Revision: 1.3 $
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_framework.hxx"
34 //_______________________________________________
35 // my own includes
37 #include <dispatch/windowcommanddispatch.hxx>
38 #include <threadhelp/readguard.hxx>
39 #include <threadhelp/writeguard.hxx>
40 #include <targets.h>
41 #include <services.h>
43 //_______________________________________________
44 // interface includes
46 #include <com/sun/star/frame/XDispatchProvider.hpp>
47 #include <com/sun/star/frame/XDispatch.hpp>
48 #include <com/sun/star/util/XURLTransformer.hpp>
50 //_______________________________________________
51 // includes of other projects
53 #include <vcl/window.hxx>
54 #include <vcl/svapp.hxx>
55 #include <vcl/cmdevt.hxx>
56 #include <vos/mutex.hxx>
57 #include <toolkit/helper/vclunohelper.hxx>
58 #include <rtl/logfile.hxx>
60 //_______________________________________________
61 // namespace
63 namespace framework{
65 namespace css = ::com::sun::star;
67 //_______________________________________________
68 // declarations
70 const ::rtl::OUString WindowCommandDispatch::COMMAND_PREFERENCES = ::rtl::OUString::createFromAscii(".uno:OptionsTreeDialog");
71 const ::rtl::OUString WindowCommandDispatch::COMMAND_ABOUTBOX = ::rtl::OUString::createFromAscii(".uno:About");
73 //-----------------------------------------------
74 WindowCommandDispatch::WindowCommandDispatch(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR ,
75 const css::uno::Reference< css::frame::XFrame >& xFrame)
76 : ThreadHelpBase( )
77 , m_xSMGR (xSMGR )
78 , m_xFrame (xFrame )
79 , m_xWindow (xFrame->getContainerWindow())
81 impl_startListening();
84 //-----------------------------------------------
85 WindowCommandDispatch::~WindowCommandDispatch()
87 m_xSMGR.clear();
90 //-----------------------------------------------
91 void SAL_CALL WindowCommandDispatch::disposing(const css::lang::EventObject& /*aSource*/)
92 throw (css::uno::RuntimeException)
94 // We hold our window weak ... so there is no need to clear it's reference here.
95 // The window and we will die by ref count automatically.
98 //-----------------------------------------------
99 void WindowCommandDispatch::impl_startListening()
101 // SYNCHRONIZED ->
102 ReadGuard aReadLock(m_aLock);
103 css::uno::Reference< css::awt::XWindow > xWindow( m_xWindow.get(), css::uno::UNO_QUERY );
104 aReadLock.unlock();
105 // <- SYNCHRONIZED
107 if ( ! xWindow.is())
108 return;
110 // SYNCHRONIZED ->
111 ::vos::OClearableGuard aSolarLock(Application::GetSolarMutex());
113 Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
114 if ( ! pWindow)
115 return;
117 pWindow->AddEventListener( LINK(this, WindowCommandDispatch, impl_notifyCommand) );
119 aSolarLock.clear();
120 // <- SYNCHRONIZED
123 //-----------------------------------------------
124 IMPL_LINK(WindowCommandDispatch, impl_notifyCommand, void*, pParam)
126 if ( ! pParam)
127 return 0L;
129 const VclWindowEvent* pEvent = (VclWindowEvent*)pParam;
130 if (pEvent->GetId() != VCLEVENT_WINDOW_COMMAND)
131 return 0L;
133 const CommandEvent* pCommand = (CommandEvent*)pEvent->GetData();
134 if (pCommand->GetCommand() != COMMAND_SHOWDIALOG)
135 return 0L;
137 const CommandDialogData* pData = pCommand->GetDialogData();
138 if ( ! pData)
139 return 0L;
141 const int nCommand = pData->GetDialogId();
142 ::rtl::OUString sCommand;
144 switch (nCommand)
146 case SHOWDIALOG_ID_PREFERENCES :
147 sCommand = WindowCommandDispatch::COMMAND_PREFERENCES;
148 break;
150 case SHOWDIALOG_ID_ABOUT :
151 sCommand = WindowCommandDispatch::COMMAND_ABOUTBOX;
152 break;
154 default :
155 return 0L;
158 impl_dispatchCommand(sCommand);
160 return 0L;
163 //-----------------------------------------------
164 void WindowCommandDispatch::impl_dispatchCommand(const ::rtl::OUString& sCommand)
166 // ignore all errors here. It's clicking a menu entry only ...
167 // The user will try it again, in case nothing happens .-)
170 // SYNCHRONIZED ->
171 ReadGuard aReadLock(m_aLock);
172 css::uno::Reference< css::frame::XDispatchProvider > xProvider(m_xFrame.get(), css::uno::UNO_QUERY_THROW);
173 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
174 aReadLock.unlock();
175 // <- SYNCHRONIZED
177 // check provider ... we know it's weak reference only
178 if ( ! xProvider.is())
179 return;
181 css::uno::Reference< css::util::XURLTransformer > xParser(xSMGR->createInstance(SERVICENAME_URLTRANSFORMER), css::uno::UNO_QUERY_THROW);
182 css::util::URL aCommand;
183 aCommand.Complete = sCommand;
184 xParser->parseStrict(aCommand);
186 css::uno::Reference< css::frame::XDispatch > xDispatch = xProvider->queryDispatch(aCommand, SPECIALTARGET_SELF, 0);
187 if (xDispatch.is())
188 xDispatch->dispatch(aCommand, css::uno::Sequence< css::beans::PropertyValue >());
190 catch(const css::uno::Exception&)
194 } // namespace framework