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