Coverage Report

Created: 2026-10-14 09:33

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