1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_framework.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir //_______________________________________________
32*cdf0e10cSrcweir // my own includes
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <dispatch/windowcommanddispatch.hxx>
35*cdf0e10cSrcweir #include <threadhelp/readguard.hxx>
36*cdf0e10cSrcweir #include <threadhelp/writeguard.hxx>
37*cdf0e10cSrcweir #include <targets.h>
38*cdf0e10cSrcweir #include <services.h>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir //_______________________________________________
41*cdf0e10cSrcweir // interface includes
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include <com/sun/star/frame/XDispatchProvider.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/frame/XDispatch.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/util/XURLTransformer.hpp>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir //_______________________________________________
48*cdf0e10cSrcweir // includes of other projects
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include <vcl/window.hxx>
51*cdf0e10cSrcweir #include <vcl/svapp.hxx>
52*cdf0e10cSrcweir #include <vcl/cmdevt.hxx>
53*cdf0e10cSrcweir #include <vos/mutex.hxx>
54*cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx>
55*cdf0e10cSrcweir #include <rtl/logfile.hxx>
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir //_______________________________________________
58*cdf0e10cSrcweir // namespace
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir namespace framework{
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir namespace css = ::com::sun::star;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir //_______________________________________________
65*cdf0e10cSrcweir // declarations
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir const ::rtl::OUString WindowCommandDispatch::COMMAND_PREFERENCES = ::rtl::OUString::createFromAscii(".uno:OptionsTreeDialog");
68*cdf0e10cSrcweir const ::rtl::OUString WindowCommandDispatch::COMMAND_ABOUTBOX    = ::rtl::OUString::createFromAscii(".uno:About");
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir //-----------------------------------------------
71*cdf0e10cSrcweir WindowCommandDispatch::WindowCommandDispatch(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR ,
72*cdf0e10cSrcweir                          const css::uno::Reference< css::frame::XFrame >&              xFrame)
73*cdf0e10cSrcweir     : ThreadHelpBase(                            )
74*cdf0e10cSrcweir     , m_xSMGR       (xSMGR                       )
75*cdf0e10cSrcweir     , m_xFrame      (xFrame                      )
76*cdf0e10cSrcweir     , m_xWindow     (xFrame->getContainerWindow())
77*cdf0e10cSrcweir {
78*cdf0e10cSrcweir     impl_startListening();
79*cdf0e10cSrcweir }
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir //-----------------------------------------------
82*cdf0e10cSrcweir WindowCommandDispatch::~WindowCommandDispatch()
83*cdf0e10cSrcweir {
84*cdf0e10cSrcweir     m_xSMGR.clear();
85*cdf0e10cSrcweir }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir //-----------------------------------------------
88*cdf0e10cSrcweir void SAL_CALL WindowCommandDispatch::disposing(const css::lang::EventObject& /*aSource*/)
89*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
90*cdf0e10cSrcweir {
91*cdf0e10cSrcweir     // We hold our window weak ... so there is no need to clear it's reference here.
92*cdf0e10cSrcweir     // The window and we will die by ref count automatically.
93*cdf0e10cSrcweir }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir //-----------------------------------------------
96*cdf0e10cSrcweir void WindowCommandDispatch::impl_startListening()
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir     // SYNCHRONIZED ->
99*cdf0e10cSrcweir     ReadGuard aReadLock(m_aLock);
100*cdf0e10cSrcweir     css::uno::Reference< css::awt::XWindow > xWindow( m_xWindow.get(), css::uno::UNO_QUERY );
101*cdf0e10cSrcweir     aReadLock.unlock();
102*cdf0e10cSrcweir     // <- SYNCHRONIZED
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     if ( ! xWindow.is())
105*cdf0e10cSrcweir         return;
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir     // SYNCHRONIZED ->
108*cdf0e10cSrcweir     ::vos::OClearableGuard aSolarLock(Application::GetSolarMutex());
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir     Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
111*cdf0e10cSrcweir     if ( ! pWindow)
112*cdf0e10cSrcweir         return;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     pWindow->AddEventListener( LINK(this, WindowCommandDispatch, impl_notifyCommand) );
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     aSolarLock.clear();
117*cdf0e10cSrcweir     // <- SYNCHRONIZED
118*cdf0e10cSrcweir }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir //-----------------------------------------------
121*cdf0e10cSrcweir IMPL_LINK(WindowCommandDispatch, impl_notifyCommand, void*, pParam)
122*cdf0e10cSrcweir {
123*cdf0e10cSrcweir     if ( ! pParam)
124*cdf0e10cSrcweir         return 0L;
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     const VclWindowEvent* pEvent = (VclWindowEvent*)pParam;
127*cdf0e10cSrcweir     if (pEvent->GetId() != VCLEVENT_WINDOW_COMMAND)
128*cdf0e10cSrcweir         return 0L;
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir     const CommandEvent* pCommand = (CommandEvent*)pEvent->GetData();
131*cdf0e10cSrcweir     if (pCommand->GetCommand() != COMMAND_SHOWDIALOG)
132*cdf0e10cSrcweir         return 0L;
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir     const CommandDialogData* pData = pCommand->GetDialogData();
135*cdf0e10cSrcweir     if ( ! pData)
136*cdf0e10cSrcweir         return 0L;
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir     const int nCommand = pData->GetDialogId();
139*cdf0e10cSrcweir           ::rtl::OUString sCommand;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir     switch (nCommand)
142*cdf0e10cSrcweir     {
143*cdf0e10cSrcweir         case SHOWDIALOG_ID_PREFERENCES :
144*cdf0e10cSrcweir                 sCommand = WindowCommandDispatch::COMMAND_PREFERENCES;
145*cdf0e10cSrcweir                 break;
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir         case SHOWDIALOG_ID_ABOUT :
148*cdf0e10cSrcweir                 sCommand = WindowCommandDispatch::COMMAND_ABOUTBOX;
149*cdf0e10cSrcweir                 break;
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir         default :
152*cdf0e10cSrcweir                 return 0L;
153*cdf0e10cSrcweir     }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir     impl_dispatchCommand(sCommand);
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     return 0L;
158*cdf0e10cSrcweir }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir //-----------------------------------------------
161*cdf0e10cSrcweir void WindowCommandDispatch::impl_dispatchCommand(const ::rtl::OUString& sCommand)
162*cdf0e10cSrcweir {
163*cdf0e10cSrcweir     // ignore all errors here. It's clicking a menu entry only ...
164*cdf0e10cSrcweir     // The user will try it again, in case nothing happens .-)
165*cdf0e10cSrcweir     try
166*cdf0e10cSrcweir     {
167*cdf0e10cSrcweir         // SYNCHRONIZED ->
168*cdf0e10cSrcweir         ReadGuard aReadLock(m_aLock);
169*cdf0e10cSrcweir         css::uno::Reference< css::frame::XDispatchProvider >   xProvider(m_xFrame.get(), css::uno::UNO_QUERY_THROW);
170*cdf0e10cSrcweir         css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR    = m_xSMGR;
171*cdf0e10cSrcweir         aReadLock.unlock();
172*cdf0e10cSrcweir         // <- SYNCHRONIZED
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir         // check provider ... we know it's weak reference only
175*cdf0e10cSrcweir         if ( ! xProvider.is())
176*cdf0e10cSrcweir             return;
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir         css::uno::Reference< css::util::XURLTransformer > xParser(xSMGR->createInstance(SERVICENAME_URLTRANSFORMER), css::uno::UNO_QUERY_THROW);
179*cdf0e10cSrcweir         css::util::URL aCommand;
180*cdf0e10cSrcweir         aCommand.Complete = sCommand;
181*cdf0e10cSrcweir         xParser->parseStrict(aCommand);
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir         css::uno::Reference< css::frame::XDispatch > xDispatch = xProvider->queryDispatch(aCommand, SPECIALTARGET_SELF, 0);
184*cdf0e10cSrcweir         if (xDispatch.is())
185*cdf0e10cSrcweir             xDispatch->dispatch(aCommand, css::uno::Sequence< css::beans::PropertyValue >());
186*cdf0e10cSrcweir     }
187*cdf0e10cSrcweir     catch(const css::uno::Exception&)
188*cdf0e10cSrcweir     {}
189*cdf0e10cSrcweir }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir } // namespace framework
192