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