/src/libreoffice/sfx2/source/config/evntconf.cxx
Line | Count | Source (jump to first uncovered line) |
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 | | |
20 | | |
21 | | #include <memory> |
22 | | |
23 | | #include <sal/log.hxx> |
24 | | #include <com/sun/star/uno/Any.hxx> |
25 | | #include <comphelper/processfactory.hxx> |
26 | | #include <sfx2/evntconf.hxx> |
27 | | #include <svl/macitem.hxx> |
28 | | #include <comphelper/diagnose_ex.hxx> |
29 | | |
30 | | #include <sfx2/objsh.hxx> |
31 | | #include <eventsupplier.hxx> |
32 | | |
33 | | #include <com/sun/star/beans/PropertyValue.hpp> |
34 | | #include <com/sun/star/container/XNameReplace.hpp> |
35 | | #include <com/sun/star/document/XEventsSupplier.hpp> |
36 | | #include <com/sun/star/frame/theGlobalEventBroadcaster.hpp> |
37 | | #include <com/sun/star/uno/Sequence.hxx> |
38 | | #include <com/sun/star/uno/Reference.hxx> |
39 | | |
40 | | |
41 | | using namespace com::sun::star; |
42 | | |
43 | 0 | SfxEventNamesList& SfxEventNamesList::operator=( const SfxEventNamesList& ) = default; |
44 | | |
45 | | SfxEventNamesList::~SfxEventNamesList() |
46 | 0 | { |
47 | 0 | } |
48 | | |
49 | | bool SfxEventNamesItem::operator==( const SfxPoolItem& rAttr ) const |
50 | 0 | { |
51 | 0 | assert(SfxPoolItem::operator==(rAttr)); |
52 | |
|
53 | 0 | const SfxEventNamesList& rOwn = aEventsList; |
54 | 0 | const SfxEventNamesList& rOther = static_cast<const SfxEventNamesItem&>( rAttr ).aEventsList; |
55 | |
|
56 | 0 | if ( rOwn.size() != rOther.size() ) |
57 | 0 | return false; |
58 | | |
59 | 0 | for ( size_t nNo = 0, nCnt = rOwn.size(); nNo < nCnt; ++nNo ) |
60 | 0 | { |
61 | 0 | const SfxEventName &rOwnEvent = rOwn.at( nNo ); |
62 | 0 | const SfxEventName &rOtherEvent = rOther.at( nNo ); |
63 | 0 | if ( rOwnEvent.mnId != rOtherEvent.mnId || |
64 | 0 | rOwnEvent.maEventName != rOtherEvent.maEventName || |
65 | 0 | rOwnEvent.maUIName != rOtherEvent.maUIName ) |
66 | 0 | return false; |
67 | 0 | } |
68 | | |
69 | 0 | return true; |
70 | |
|
71 | 0 | } |
72 | | |
73 | | bool SfxEventNamesItem::GetPresentation( SfxItemPresentation, |
74 | | MapUnit, |
75 | | MapUnit, |
76 | | OUString &rText, |
77 | | const IntlWrapper& ) const |
78 | 0 | { |
79 | 0 | rText.clear(); |
80 | 0 | return false; |
81 | 0 | } |
82 | | |
83 | | SfxEventNamesItem* SfxEventNamesItem::Clone( SfxItemPool *) const |
84 | 0 | { |
85 | 0 | return new SfxEventNamesItem(*this); |
86 | 0 | } |
87 | | |
88 | | void SfxEventNamesItem::AddEvent( const OUString& rName, const OUString& rUIName, SvMacroItemId nID ) |
89 | 0 | { |
90 | 0 | aEventsList.push_back( SfxEventName( nID, rName, !rUIName.isEmpty() ? rUIName : rName ) ); |
91 | 0 | } |
92 | | |
93 | | |
94 | | static uno::Any CreateEventData_Impl( const SvxMacro *pMacro ) |
95 | 0 | { |
96 | | /* |
97 | | This function converts a SvxMacro into an Any containing three |
98 | | properties. These properties are EventType and Script. Possible |
99 | | values for EventType are StarBasic, JavaScript, ... |
100 | | The Script property should contain the URL to the macro and looks |
101 | | like "macro://./standard.module1.main()" |
102 | | |
103 | | If pMacro is NULL, we return an empty property sequence, so PropagateEvent_Impl |
104 | | can delete an event binding. |
105 | | */ |
106 | 0 | uno::Any aEventData; |
107 | |
|
108 | 0 | if ( pMacro ) |
109 | 0 | { |
110 | 0 | if ( pMacro->GetScriptType() == STARBASIC ) |
111 | 0 | { |
112 | 0 | uno::Sequence < beans::PropertyValue > aProperties(3); |
113 | 0 | beans::PropertyValue *pValues = aProperties.getArray(); |
114 | |
|
115 | 0 | pValues[ 0 ].Name = PROP_EVENT_TYPE; |
116 | 0 | pValues[ 0 ].Value <<= u"STAR_BASIC"_ustr; |
117 | |
|
118 | 0 | pValues[ 1 ].Name = PROP_LIBRARY; |
119 | 0 | pValues[ 1 ].Value <<= pMacro->GetLibName(); |
120 | |
|
121 | 0 | pValues[ 2 ].Name = PROP_MACRO_NAME; |
122 | 0 | pValues[ 2 ].Value <<= pMacro->GetMacName(); |
123 | |
|
124 | 0 | aEventData <<= aProperties; |
125 | 0 | } |
126 | 0 | else if ( pMacro->GetScriptType() == EXTENDED_STYPE ) |
127 | 0 | { |
128 | 0 | uno::Sequence < beans::PropertyValue > aProperties(2); |
129 | 0 | beans::PropertyValue *pValues = aProperties.getArray(); |
130 | |
|
131 | 0 | pValues[ 0 ].Name = PROP_EVENT_TYPE; |
132 | 0 | pValues[ 0 ].Value <<= pMacro->GetLibName(); |
133 | |
|
134 | 0 | pValues[ 1 ].Name = PROP_SCRIPT; |
135 | 0 | pValues[ 1 ].Value <<= pMacro->GetMacName(); |
136 | |
|
137 | 0 | aEventData <<= aProperties; |
138 | 0 | } |
139 | 0 | else if ( pMacro->GetScriptType() == JAVASCRIPT ) |
140 | 0 | { |
141 | 0 | uno::Sequence < beans::PropertyValue > aProperties(2); |
142 | 0 | beans::PropertyValue *pValues = aProperties.getArray(); |
143 | |
|
144 | 0 | pValues[ 0 ].Name = PROP_EVENT_TYPE; |
145 | 0 | pValues[ 0 ].Value <<= SVX_MACRO_LANGUAGE_JAVASCRIPT; |
146 | |
|
147 | 0 | pValues[ 1 ].Name = PROP_MACRO_NAME; |
148 | 0 | pValues[ 1 ].Value <<= pMacro->GetMacName(); |
149 | |
|
150 | 0 | aEventData <<= aProperties; |
151 | 0 | } |
152 | 0 | else |
153 | 0 | { |
154 | 0 | SAL_WARN( "sfx.config", "CreateEventData_Impl(): ScriptType not supported!"); |
155 | 0 | } |
156 | 0 | } |
157 | 0 | else |
158 | 0 | { |
159 | 0 | uno::Sequence < beans::PropertyValue > aProperties; |
160 | 0 | aEventData <<= aProperties; |
161 | 0 | } |
162 | | |
163 | 0 | return aEventData; |
164 | 0 | } |
165 | | |
166 | | |
167 | | static void PropagateEvent_Impl( SfxObjectShell const *pDoc, const OUString& aEventName, const SvxMacro* pMacro ) |
168 | 158 | { |
169 | 158 | uno::Reference < document::XEventsSupplier > xSupplier; |
170 | 158 | if ( pDoc ) |
171 | 158 | { |
172 | 158 | xSupplier.set( pDoc->GetModel(), uno::UNO_QUERY ); |
173 | 158 | } |
174 | 0 | else |
175 | 0 | { |
176 | 0 | xSupplier = frame::theGlobalEventBroadcaster::get(::comphelper::getProcessComponentContext()); |
177 | 0 | } |
178 | | |
179 | 158 | if ( !xSupplier.is() ) |
180 | 0 | return; |
181 | | |
182 | 158 | uno::Reference < container::XNameReplace > xEvents = xSupplier->getEvents(); |
183 | 158 | if ( !aEventName.isEmpty() ) |
184 | 0 | { |
185 | 0 | uno::Any aEventData = CreateEventData_Impl( pMacro ); |
186 | |
|
187 | 0 | try |
188 | 0 | { |
189 | 0 | xEvents->replaceByName( aEventName, aEventData ); |
190 | 0 | } |
191 | 0 | catch( const css::lang::IllegalArgumentException& ) |
192 | 0 | { |
193 | 0 | TOOLS_WARN_EXCEPTION( "sfx.config", "PropagateEvents_Impl: caught IllegalArgumentException" ); |
194 | 0 | } |
195 | 0 | catch( const css::container::NoSuchElementException& ) |
196 | 0 | { |
197 | 0 | TOOLS_WARN_EXCEPTION( "sfx.config", "PropagateEvents_Impl: caught NoSuchElementException" ); |
198 | 0 | } |
199 | 0 | } |
200 | 158 | else { |
201 | 158 | SAL_INFO( "sfx.config", "PropagateEvents_Impl: Got unknown event" ); |
202 | 158 | } |
203 | 158 | } |
204 | | |
205 | | |
206 | | void SfxEventConfiguration::ConfigureEvent( const OUString& aName, const SvxMacro& rMacro, SfxObjectShell const *pDoc ) |
207 | 158 | { |
208 | 158 | std::optional<SvxMacro> pMacro; |
209 | 158 | if ( rMacro.HasMacro() ) |
210 | 158 | pMacro.emplace( rMacro.GetMacName(), rMacro.GetLibName(), rMacro.GetScriptType() ); |
211 | 158 | PropagateEvent_Impl( pDoc ? pDoc : nullptr, aName, pMacro ? &*pMacro : nullptr ); |
212 | 158 | } |
213 | | |
214 | | |
215 | | std::unique_ptr<SvxMacro> SfxEventConfiguration::ConvertToMacro( const css::uno::Any& rElement, SfxObjectShell* pDoc ) |
216 | 0 | { |
217 | 0 | return SfxEvents_Impl::ConvertToMacro( rElement, pDoc ); |
218 | 0 | } |
219 | | |
220 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |