LibreOffice Module framework (master) 1
windowcommanddispatch.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21#include <targets.h>
22
23#include <com/sun/star/frame/XDispatchProvider.hpp>
24#include <com/sun/star/frame/XDispatch.hpp>
25#include <com/sun/star/util/URLTransformer.hpp>
26#include <com/sun/star/util/XURLTransformer.hpp>
27
28#include <utility>
29#include <vcl/window.hxx>
30#include <vcl/svapp.hxx>
31#include <vcl/commandevent.hxx>
33
34namespace framework{
35
36WindowCommandDispatch::WindowCommandDispatch(css::uno::Reference< css::uno::XComponentContext > xContext ,
37 const css::uno::Reference< css::frame::XFrame >& xFrame)
38 : m_xContext (std::move(xContext ))
39 , m_xFrame (xFrame )
40 , m_xWindow (xFrame->getContainerWindow())
41{
43}
44
46{
48 m_xContext.clear();
49}
50
52{
53 std::unique_lock aReadLock(m_mutex);
54 css::uno::Reference< css::awt::XWindow > xWindow( m_xWindow.get(), css::uno::UNO_QUERY );
55 aReadLock.unlock();
56
57 if ( ! xWindow.is())
58 return;
59
60 {
61 SolarMutexGuard aSolarLock;
62
64 if ( ! pWindow)
65 return;
66
67 pWindow->AddEventListener( LINK(this, WindowCommandDispatch, impl_notifyCommand) );
68 }
69}
70
72{
73 std::unique_lock aReadLock(m_mutex);
74 css::uno::Reference< css::awt::XWindow > xWindow( m_xWindow.get(), css::uno::UNO_QUERY );
75 aReadLock.unlock();
76
77 if (!xWindow.is())
78 return;
79
80 {
81 SolarMutexGuard aSolarLock;
82
84 if (!pWindow)
85 return;
86
87 pWindow->RemoveEventListener( LINK(this, WindowCommandDispatch, impl_notifyCommand) );
88
89 m_xWindow.clear();
90 }
91}
92
93IMPL_LINK(WindowCommandDispatch, impl_notifyCommand, VclWindowEvent&, rEvent, void)
94{
95 if (rEvent.GetId() == VclEventId::ObjectDying)
96 {
97 impl_stopListening();
98 return;
99 }
100 if (rEvent.GetId() != VclEventId::WindowCommand)
101 return;
102
103 const CommandEvent* pCommand = static_cast<CommandEvent*>(rEvent.GetData());
104 if (pCommand->GetCommand() != CommandEventId::ShowDialog)
105 return;
106
107 const CommandDialogData* pData = pCommand->GetDialogData();
108 if ( ! pData)
109 return;
110
111 const ShowDialogId nCommand = pData->GetDialogId();
112 OUString sCommand;
113
114 switch (nCommand)
115 {
116 case ShowDialogId::Preferences :
117 sCommand = ".uno:OptionsTreeDialog";
118 break;
119
120 case ShowDialogId::About :
121 sCommand = ".uno:About";
122 break;
123
124 default :
125 return;
126 }
127
128 // ignore all errors here. It's clicking a menu entry only ...
129 // The user will try it again, in case nothing happens .-)
130 try
131 {
132 // SYNCHRONIZED ->
133 std::unique_lock aReadLock(m_mutex);
134 css::uno::Reference< css::frame::XDispatchProvider > xProvider(m_xFrame.get(), css::uno::UNO_QUERY_THROW);
135 css::uno::Reference< css::uno::XComponentContext > xContext = m_xContext;
136 aReadLock.unlock();
137 // <- SYNCHRONIZED
138
139 // check provider ... we know it's weak reference only
140 if ( ! xProvider.is())
141 return;
142
143 css::uno::Reference< css::util::XURLTransformer > xParser(css::util::URLTransformer::create(xContext));
144 css::util::URL aCommand;
145 aCommand.Complete = sCommand;
146 xParser->parseStrict(aCommand);
147
148 css::uno::Reference< css::frame::XDispatch > xDispatch = xProvider->queryDispatch(aCommand, SPECIALTARGET_SELF, 0);
149 if (xDispatch.is())
150 xDispatch->dispatch(aCommand, css::uno::Sequence< css::beans::PropertyValue >());
151 }
152 catch(const css::uno::Exception&)
153 {}
154}
155
156} // namespace framework
157
158/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::lang::XComponent > m_xFrame
CommandEventId GetCommand() const
const CommandDialogData * GetDialogData() const
static vcl::Window * GetWindow(const css::uno::Reference< css::awt::XWindow > &rxWindow)
internal helper to bind e.g.
~WindowCommandDispatch()
used to free internal resources.
css::uno::WeakReference< css::awt::XWindow > m_xWindow
knows the VCL window (where the hard coded commands occurred) as weak XWindow reference
void impl_startListening()
establish all listener connections we need.
css::uno::Reference< css::uno::XComponentContext > m_xContext
can be used to create own needed services on demand.
void impl_stopListening()
drop all listener connections we need.
WindowCommandDispatch(css::uno::Reference< css::uno::XComponentContext > xContext, const css::uno::Reference< css::frame::XFrame > &xFrame)
creates a new instance and initialize it with all necessary parameters.
ShowDialogId
Reference< XDispatch > xDispatch
css::uno::Reference< css::uno::XComponentContext > m_xContext
std::mutex m_mutex
Definition: loadenv.cxx:108
std::unique_ptr< sal_Int32[]> pData
IMPL_LINK(WindowCommandDispatch, impl_notifyCommand, VclWindowEvent &, rEvent, void)
constexpr OUStringLiteral SPECIALTARGET_SELF
Definition: targets.h:28
Reference< XFrame > xFrame
OUString aCommand