1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <dispatch/windowcommanddispatch.hxx>
24 #include <com/sun/star/frame/XDispatchProvider.hpp>
25 #include <com/sun/star/frame/XDispatch.hpp>
26 #include <com/sun/star/util/URLTransformer.hpp>
27 #include <com/sun/star/util/XURLTransformer.hpp>
29 #include <comphelper/processfactory.hxx>
30 #include <vcl/window.hxx>
31 #include <vcl/svapp.hxx>
32 #include <vcl/cmdevt.hxx>
33 #include <osl/mutex.hxx>
34 #include <toolkit/helper/vclunohelper.hxx>
38 WindowCommandDispatch::WindowCommandDispatch(const css::uno::Reference
< css::uno::XComponentContext
>& xContext
,
39 const css::uno::Reference
< css::frame::XFrame
>& xFrame
)
40 : m_xContext (xContext
)
42 , m_xWindow (xFrame
->getContainerWindow())
44 impl_startListening();
47 WindowCommandDispatch::~WindowCommandDispatch()
53 void WindowCommandDispatch::impl_startListening()
55 osl::ClearableMutexGuard
aReadLock(m_mutex
);
56 css::uno::Reference
< css::awt::XWindow
> xWindow( m_xWindow
.get(), css::uno::UNO_QUERY
);
63 SolarMutexGuard aSolarLock
;
65 vcl::Window
* pWindow
= VCLUnoHelper::GetWindow(xWindow
);
69 pWindow
->AddEventListener( LINK(this, WindowCommandDispatch
, impl_notifyCommand
) );
73 void WindowCommandDispatch::impl_stopListening()
75 osl::ClearableMutexGuard
aReadLock(m_mutex
);
76 css::uno::Reference
< css::awt::XWindow
> xWindow( m_xWindow
.get(), css::uno::UNO_QUERY
);
83 SolarMutexGuard aSolarLock
;
85 vcl::Window
* pWindow
= VCLUnoHelper::GetWindow(xWindow
);
89 pWindow
->RemoveEventListener( LINK(this, WindowCommandDispatch
, impl_notifyCommand
) );
95 IMPL_LINK(WindowCommandDispatch
, impl_notifyCommand
, void*, pParam
)
100 const VclWindowEvent
* pEvent
= static_cast<VclWindowEvent
*>(pParam
);
101 if (pEvent
->GetId() == VCLEVENT_OBJECT_DYING
)
103 impl_stopListening();
106 if (pEvent
->GetId() != VCLEVENT_WINDOW_COMMAND
)
109 const CommandEvent
* pCommand
= static_cast<CommandEvent
*>(pEvent
->GetData());
110 if (pCommand
->GetCommand() != CommandEventId::ShowDialog
)
113 const CommandDialogData
* pData
= pCommand
->GetDialogData();
117 const ShowDialogId nCommand
= pData
->GetDialogId();
122 case ShowDialogId::Preferences
:
123 sCommand
= ".uno:OptionsTreeDialog";
126 case ShowDialogId::About
:
127 sCommand
= ".uno:About";
134 impl_dispatchCommand(sCommand
);
139 void WindowCommandDispatch::impl_dispatchCommand(const OUString
& sCommand
)
141 // ignore all errors here. It's clicking a menu entry only ...
142 // The user will try it again, in case nothing happens .-)
146 osl::ClearableMutexGuard
aReadLock(m_mutex
);
147 css::uno::Reference
< css::frame::XDispatchProvider
> xProvider(m_xFrame
.get(), css::uno::UNO_QUERY_THROW
);
148 css::uno::Reference
< css::uno::XComponentContext
> xContext
= m_xContext
;
152 // check provider ... we know it's weak reference only
153 if ( ! xProvider
.is())
156 css::uno::Reference
< css::util::XURLTransformer
> xParser(css::util::URLTransformer::create(xContext
));
157 css::util::URL aCommand
;
158 aCommand
.Complete
= sCommand
;
159 xParser
->parseStrict(aCommand
);
161 css::uno::Reference
< css::frame::XDispatch
> xDispatch
= xProvider
->queryDispatch(aCommand
, SPECIALTARGET_SELF
, 0);
163 xDispatch
->dispatch(aCommand
, css::uno::Sequence
< css::beans::PropertyValue
>());
165 catch(const css::uno::Exception
&)
169 } // namespace framework
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */