/src/libreoffice/sc/source/ui/unoobj/eventuno.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 | | #include <eventuno.hxx> |
21 | | #include <miscuno.hxx> |
22 | | #include <docsh.hxx> |
23 | | #include <sheetevents.hxx> |
24 | | #include <unonames.hxx> |
25 | | #include <comphelper/propertysequence.hxx> |
26 | | #include <vcl/svapp.hxx> |
27 | | |
28 | | using namespace ::com::sun::star; |
29 | | |
30 | | SC_SIMPLE_SERVICE_INFO( ScSheetEventsObj, u"ScSheetEventsObj"_ustr, u"com.sun.star.document.Events"_ustr ) |
31 | | |
32 | | ScSheetEventsObj::ScSheetEventsObj(ScDocShell* pDocSh, SCTAB nT) : |
33 | 0 | mpDocShell( pDocSh ), |
34 | 0 | mnTab( nT ) |
35 | 0 | { |
36 | 0 | mpDocShell->GetDocument().AddUnoObject(*this); |
37 | 0 | } |
38 | | |
39 | | ScSheetEventsObj::~ScSheetEventsObj() |
40 | 0 | { |
41 | 0 | SolarMutexGuard g; |
42 | |
|
43 | 0 | if (mpDocShell) |
44 | 0 | mpDocShell->GetDocument().RemoveUnoObject(*this); |
45 | 0 | } |
46 | | |
47 | | void ScSheetEventsObj::Notify( SfxBroadcaster&, const SfxHint& rHint ) |
48 | 0 | { |
49 | | //! reference update |
50 | 0 | if ( rHint.GetId() == SfxHintId::Dying ) |
51 | 0 | { |
52 | 0 | mpDocShell = nullptr; |
53 | 0 | } |
54 | 0 | } |
55 | | |
56 | | static ScSheetEventId lcl_GetEventFromName( std::u16string_view aName ) |
57 | 0 | { |
58 | 0 | for (sal_Int32 nEvent=0; nEvent<static_cast<sal_Int32>(ScSheetEventId::COUNT); ++nEvent) |
59 | 0 | if ( aName == ScSheetEvents::GetEventName(static_cast<ScSheetEventId>(nEvent)) ) |
60 | 0 | return static_cast<ScSheetEventId>(nEvent); |
61 | | |
62 | 0 | return ScSheetEventId::NOTFOUND; // not found |
63 | 0 | } |
64 | | |
65 | | // XNameReplace |
66 | | |
67 | | void SAL_CALL ScSheetEventsObj::replaceByName( const OUString& aName, const uno::Any& aElement ) |
68 | 0 | { |
69 | 0 | SolarMutexGuard aGuard; |
70 | 0 | if (!mpDocShell) |
71 | 0 | throw uno::RuntimeException(); |
72 | | |
73 | 0 | ScSheetEventId nEvent = lcl_GetEventFromName(aName); |
74 | 0 | if (nEvent == ScSheetEventId::NOTFOUND) |
75 | 0 | throw container::NoSuchElementException(); |
76 | | |
77 | 0 | std::unique_ptr<ScSheetEvents> pNewEvents(new ScSheetEvents); |
78 | 0 | const ScSheetEvents* pOldEvents = mpDocShell->GetDocument().GetSheetEvents(mnTab); |
79 | 0 | if (pOldEvents) |
80 | 0 | *pNewEvents = *pOldEvents; |
81 | |
|
82 | 0 | OUString aScript; |
83 | 0 | if ( aElement.hasValue() ) // empty Any -> reset event |
84 | 0 | { |
85 | 0 | uno::Sequence<beans::PropertyValue> aPropSeq; |
86 | 0 | if ( aElement >>= aPropSeq ) |
87 | 0 | { |
88 | 0 | for (const beans::PropertyValue& rProp : aPropSeq) |
89 | 0 | { |
90 | 0 | if ( rProp.Name == SC_UNO_EVENTTYPE ) |
91 | 0 | { |
92 | 0 | OUString aEventType; |
93 | 0 | if ( rProp.Value >>= aEventType ) |
94 | 0 | { |
95 | | // only "Script" is supported |
96 | 0 | if ( aEventType != SC_UNO_SCRIPT ) |
97 | 0 | throw lang::IllegalArgumentException(); |
98 | 0 | } |
99 | 0 | } |
100 | 0 | else if ( rProp.Name == SC_UNO_SCRIPT ) |
101 | 0 | rProp.Value >>= aScript; |
102 | 0 | } |
103 | 0 | } |
104 | 0 | } |
105 | 0 | if (!aScript.isEmpty()) |
106 | 0 | pNewEvents->SetScript( nEvent, &aScript ); |
107 | 0 | else |
108 | 0 | pNewEvents->SetScript( nEvent, nullptr ); // reset |
109 | |
|
110 | 0 | mpDocShell->GetDocument().SetSheetEvents( mnTab, std::move(pNewEvents) ); |
111 | 0 | mpDocShell->SetDocumentModified(); |
112 | 0 | } |
113 | | |
114 | | // XNameAccess |
115 | | |
116 | | uno::Any SAL_CALL ScSheetEventsObj::getByName( const OUString& aName ) |
117 | 0 | { |
118 | 0 | SolarMutexGuard aGuard; |
119 | 0 | ScSheetEventId nEvent = lcl_GetEventFromName(aName); |
120 | 0 | if (nEvent == ScSheetEventId::NOTFOUND) |
121 | 0 | throw container::NoSuchElementException(); |
122 | | |
123 | 0 | const OUString* pScript = nullptr; |
124 | 0 | if (mpDocShell) |
125 | 0 | { |
126 | 0 | const ScSheetEvents* pEvents = mpDocShell->GetDocument().GetSheetEvents(mnTab); |
127 | 0 | if (pEvents) |
128 | 0 | pScript = pEvents->GetScript(nEvent); |
129 | 0 | } |
130 | |
|
131 | 0 | uno::Any aRet; |
132 | 0 | if (pScript) |
133 | 0 | { |
134 | 0 | uno::Sequence<beans::PropertyValue> aPropSeq( comphelper::InitPropertySequence({ |
135 | 0 | { "EventType", uno::Any( u"Script"_ustr ) }, |
136 | 0 | { "Script", uno::Any( *pScript ) } |
137 | 0 | })); |
138 | 0 | aRet <<= aPropSeq; |
139 | 0 | } |
140 | | // empty Any if nothing was set |
141 | 0 | return aRet; |
142 | 0 | } |
143 | | |
144 | | uno::Sequence<OUString> SAL_CALL ScSheetEventsObj::getElementNames() |
145 | 0 | { |
146 | 0 | auto aNames = uno::Sequence<OUString>(int(ScSheetEventId::COUNT)); |
147 | 0 | auto pNames = aNames.getArray(); |
148 | 0 | for (sal_Int32 nEvent=0; nEvent<int(ScSheetEventId::COUNT); ++nEvent) |
149 | 0 | pNames[nEvent] = ScSheetEvents::GetEventName(static_cast<ScSheetEventId>(nEvent)); |
150 | 0 | return aNames; |
151 | 0 | } |
152 | | |
153 | | sal_Bool SAL_CALL ScSheetEventsObj::hasByName( const OUString& aName ) |
154 | 0 | { |
155 | 0 | ScSheetEventId nEvent = lcl_GetEventFromName(aName); |
156 | 0 | return (nEvent != ScSheetEventId::NOTFOUND); |
157 | 0 | } |
158 | | |
159 | | // XElementAccess |
160 | | |
161 | | uno::Type SAL_CALL ScSheetEventsObj::getElementType() |
162 | 0 | { |
163 | 0 | return cppu::UnoType<uno::Sequence<beans::PropertyValue>>::get(); |
164 | 0 | } |
165 | | |
166 | | sal_Bool SAL_CALL ScSheetEventsObj::hasElements() |
167 | 0 | { |
168 | 0 | SolarMutexGuard aGuard; |
169 | 0 | if (mpDocShell) |
170 | 0 | return true; |
171 | 0 | return false; |
172 | 0 | } |
173 | | |
174 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |