1*6d739b60SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*6d739b60SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*6d739b60SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*6d739b60SAndrew Rist  * distributed with this work for additional information
6*6d739b60SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*6d739b60SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*6d739b60SAndrew Rist  * "License"); you may not use this file except in compliance
9*6d739b60SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*6d739b60SAndrew Rist  *
11*6d739b60SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*6d739b60SAndrew Rist  *
13*6d739b60SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*6d739b60SAndrew Rist  * software distributed under the License is distributed on an
15*6d739b60SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*6d739b60SAndrew Rist  * KIND, either express or implied.  See the License for the
17*6d739b60SAndrew Rist  * specific language governing permissions and limitations
18*6d739b60SAndrew Rist  * under the License.
19*6d739b60SAndrew Rist  *
20*6d739b60SAndrew Rist  *************************************************************/
21*6d739b60SAndrew Rist 
22*6d739b60SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_framework.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir //_______________________________________________
28cdf0e10cSrcweir // my own includes
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <dispatch/windowcommanddispatch.hxx>
31cdf0e10cSrcweir #include <threadhelp/readguard.hxx>
32cdf0e10cSrcweir #include <threadhelp/writeguard.hxx>
33cdf0e10cSrcweir #include <targets.h>
34cdf0e10cSrcweir #include <services.h>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir //_______________________________________________
37cdf0e10cSrcweir // interface includes
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <com/sun/star/frame/XDispatchProvider.hpp>
40cdf0e10cSrcweir #include <com/sun/star/frame/XDispatch.hpp>
41cdf0e10cSrcweir #include <com/sun/star/util/XURLTransformer.hpp>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir //_______________________________________________
44cdf0e10cSrcweir // includes of other projects
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include <vcl/window.hxx>
47cdf0e10cSrcweir #include <vcl/svapp.hxx>
48cdf0e10cSrcweir #include <vcl/cmdevt.hxx>
49cdf0e10cSrcweir #include <vos/mutex.hxx>
50cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx>
51cdf0e10cSrcweir #include <rtl/logfile.hxx>
52cdf0e10cSrcweir 
53cdf0e10cSrcweir //_______________________________________________
54cdf0e10cSrcweir // namespace
55cdf0e10cSrcweir 
56cdf0e10cSrcweir namespace framework{
57cdf0e10cSrcweir 
58cdf0e10cSrcweir namespace css = ::com::sun::star;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir //_______________________________________________
61cdf0e10cSrcweir // declarations
62cdf0e10cSrcweir 
63cdf0e10cSrcweir const ::rtl::OUString WindowCommandDispatch::COMMAND_PREFERENCES = ::rtl::OUString::createFromAscii(".uno:OptionsTreeDialog");
64cdf0e10cSrcweir const ::rtl::OUString WindowCommandDispatch::COMMAND_ABOUTBOX    = ::rtl::OUString::createFromAscii(".uno:About");
65cdf0e10cSrcweir 
66cdf0e10cSrcweir //-----------------------------------------------
67cdf0e10cSrcweir WindowCommandDispatch::WindowCommandDispatch(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR ,
68cdf0e10cSrcweir                          const css::uno::Reference< css::frame::XFrame >&              xFrame)
69cdf0e10cSrcweir     : ThreadHelpBase(                            )
70cdf0e10cSrcweir     , m_xSMGR       (xSMGR                       )
71cdf0e10cSrcweir     , m_xFrame      (xFrame                      )
72cdf0e10cSrcweir     , m_xWindow     (xFrame->getContainerWindow())
73cdf0e10cSrcweir {
74cdf0e10cSrcweir     impl_startListening();
75cdf0e10cSrcweir }
76cdf0e10cSrcweir 
77cdf0e10cSrcweir //-----------------------------------------------
78cdf0e10cSrcweir WindowCommandDispatch::~WindowCommandDispatch()
79cdf0e10cSrcweir {
80cdf0e10cSrcweir     m_xSMGR.clear();
81cdf0e10cSrcweir }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir //-----------------------------------------------
84cdf0e10cSrcweir void SAL_CALL WindowCommandDispatch::disposing(const css::lang::EventObject& /*aSource*/)
85cdf0e10cSrcweir     throw (css::uno::RuntimeException)
86cdf0e10cSrcweir {
87cdf0e10cSrcweir     // We hold our window weak ... so there is no need to clear it's reference here.
88cdf0e10cSrcweir     // The window and we will die by ref count automatically.
89cdf0e10cSrcweir }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir //-----------------------------------------------
92cdf0e10cSrcweir void WindowCommandDispatch::impl_startListening()
93cdf0e10cSrcweir {
94cdf0e10cSrcweir     // SYNCHRONIZED ->
95cdf0e10cSrcweir     ReadGuard aReadLock(m_aLock);
96cdf0e10cSrcweir     css::uno::Reference< css::awt::XWindow > xWindow( m_xWindow.get(), css::uno::UNO_QUERY );
97cdf0e10cSrcweir     aReadLock.unlock();
98cdf0e10cSrcweir     // <- SYNCHRONIZED
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     if ( ! xWindow.is())
101cdf0e10cSrcweir         return;
102cdf0e10cSrcweir 
103cdf0e10cSrcweir     // SYNCHRONIZED ->
104cdf0e10cSrcweir     ::vos::OClearableGuard aSolarLock(Application::GetSolarMutex());
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     Window* pWindow = VCLUnoHelper::GetWindow(xWindow);
107cdf0e10cSrcweir     if ( ! pWindow)
108cdf0e10cSrcweir         return;
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     pWindow->AddEventListener( LINK(this, WindowCommandDispatch, impl_notifyCommand) );
111cdf0e10cSrcweir 
112cdf0e10cSrcweir     aSolarLock.clear();
113cdf0e10cSrcweir     // <- SYNCHRONIZED
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir //-----------------------------------------------
117cdf0e10cSrcweir IMPL_LINK(WindowCommandDispatch, impl_notifyCommand, void*, pParam)
118cdf0e10cSrcweir {
119cdf0e10cSrcweir     if ( ! pParam)
120cdf0e10cSrcweir         return 0L;
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     const VclWindowEvent* pEvent = (VclWindowEvent*)pParam;
123cdf0e10cSrcweir     if (pEvent->GetId() != VCLEVENT_WINDOW_COMMAND)
124cdf0e10cSrcweir         return 0L;
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     const CommandEvent* pCommand = (CommandEvent*)pEvent->GetData();
127cdf0e10cSrcweir     if (pCommand->GetCommand() != COMMAND_SHOWDIALOG)
128cdf0e10cSrcweir         return 0L;
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     const CommandDialogData* pData = pCommand->GetDialogData();
131cdf0e10cSrcweir     if ( ! pData)
132cdf0e10cSrcweir         return 0L;
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     const int nCommand = pData->GetDialogId();
135cdf0e10cSrcweir           ::rtl::OUString sCommand;
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     switch (nCommand)
138cdf0e10cSrcweir     {
139cdf0e10cSrcweir         case SHOWDIALOG_ID_PREFERENCES :
140cdf0e10cSrcweir                 sCommand = WindowCommandDispatch::COMMAND_PREFERENCES;
141cdf0e10cSrcweir                 break;
142cdf0e10cSrcweir 
143cdf0e10cSrcweir         case SHOWDIALOG_ID_ABOUT :
144cdf0e10cSrcweir                 sCommand = WindowCommandDispatch::COMMAND_ABOUTBOX;
145cdf0e10cSrcweir                 break;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir         default :
148cdf0e10cSrcweir                 return 0L;
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     impl_dispatchCommand(sCommand);
152cdf0e10cSrcweir 
153cdf0e10cSrcweir     return 0L;
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir //-----------------------------------------------
157cdf0e10cSrcweir void WindowCommandDispatch::impl_dispatchCommand(const ::rtl::OUString& sCommand)
158cdf0e10cSrcweir {
159cdf0e10cSrcweir     // ignore all errors here. It's clicking a menu entry only ...
160cdf0e10cSrcweir     // The user will try it again, in case nothing happens .-)
161cdf0e10cSrcweir     try
162cdf0e10cSrcweir     {
163cdf0e10cSrcweir         // SYNCHRONIZED ->
164cdf0e10cSrcweir         ReadGuard aReadLock(m_aLock);
165cdf0e10cSrcweir         css::uno::Reference< css::frame::XDispatchProvider >   xProvider(m_xFrame.get(), css::uno::UNO_QUERY_THROW);
166cdf0e10cSrcweir         css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR    = m_xSMGR;
167cdf0e10cSrcweir         aReadLock.unlock();
168cdf0e10cSrcweir         // <- SYNCHRONIZED
169cdf0e10cSrcweir 
170cdf0e10cSrcweir         // check provider ... we know it's weak reference only
171cdf0e10cSrcweir         if ( ! xProvider.is())
172cdf0e10cSrcweir             return;
173cdf0e10cSrcweir 
174cdf0e10cSrcweir         css::uno::Reference< css::util::XURLTransformer > xParser(xSMGR->createInstance(SERVICENAME_URLTRANSFORMER), css::uno::UNO_QUERY_THROW);
175cdf0e10cSrcweir         css::util::URL aCommand;
176cdf0e10cSrcweir         aCommand.Complete = sCommand;
177cdf0e10cSrcweir         xParser->parseStrict(aCommand);
178cdf0e10cSrcweir 
179cdf0e10cSrcweir         css::uno::Reference< css::frame::XDispatch > xDispatch = xProvider->queryDispatch(aCommand, SPECIALTARGET_SELF, 0);
180cdf0e10cSrcweir         if (xDispatch.is())
181cdf0e10cSrcweir             xDispatch->dispatch(aCommand, css::uno::Sequence< css::beans::PropertyValue >());
182cdf0e10cSrcweir     }
183cdf0e10cSrcweir     catch(const css::uno::Exception&)
184cdf0e10cSrcweir     {}
185cdf0e10cSrcweir }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir } // namespace framework
188