Line data Source code
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 "documentevents.hxx"
21 :
22 : #include <com/sun/star/beans/PropertyValue.hpp>
23 :
24 : #include <comphelper/namedvaluecollection.hxx>
25 :
26 : #include <algorithm>
27 : #include <functional>
28 : #include <o3tl/compat_functional.hxx>
29 :
30 : namespace dbaccess
31 : {
32 :
33 : using ::com::sun::star::uno::Reference;
34 : using ::com::sun::star::uno::XInterface;
35 : using ::com::sun::star::uno::UNO_QUERY;
36 : using ::com::sun::star::uno::UNO_QUERY_THROW;
37 : using ::com::sun::star::uno::UNO_SET_THROW;
38 : using ::com::sun::star::uno::Exception;
39 : using ::com::sun::star::uno::RuntimeException;
40 : using ::com::sun::star::uno::Any;
41 : using ::com::sun::star::uno::makeAny;
42 : using ::com::sun::star::beans::PropertyValue;
43 : using ::com::sun::star::container::NoSuchElementException;
44 : using ::com::sun::star::lang::WrappedTargetException;
45 : using ::com::sun::star::lang::IllegalArgumentException;
46 : using ::com::sun::star::uno::Sequence;
47 : using ::com::sun::star::uno::Type;
48 :
49 : // DocumentEvents_Data
50 : struct DocumentEvents_Data : public ::boost::noncopyable
51 : {
52 : ::cppu::OWeakObject& rParent;
53 : ::osl::Mutex& rMutex;
54 : DocumentEventsData& rEventsData;
55 :
56 0 : DocumentEvents_Data( ::cppu::OWeakObject& _rParent, ::osl::Mutex& _rMutex, DocumentEventsData& _rEventsData )
57 : :rParent( _rParent )
58 : ,rMutex( _rMutex )
59 0 : ,rEventsData( _rEventsData )
60 : {
61 0 : }
62 : };
63 :
64 : // helper
65 : struct DocumentEventData
66 : {
67 : const sal_Char* pAsciiEventName;
68 : bool bNeedsSyncNotify;
69 : };
70 :
71 : namespace
72 : {
73 0 : static const DocumentEventData* lcl_getDocumentEventData()
74 : {
75 : static const DocumentEventData s_aData[] = {
76 : { "OnCreate", true },
77 : { "OnLoadFinished", true },
78 : { "OnNew", false }, // compatibility, see http://www.openoffice.org/issues/show_bug.cgi?id=46484
79 : { "OnLoad", false }, // compatibility, see http://www.openoffice.org/issues/show_bug.cgi?id=46484
80 : { "OnSaveAs", true },
81 : { "OnSaveAsDone", false },
82 : { "OnSaveAsFailed", false },
83 : { "OnSave", true },
84 : { "OnSaveDone", false },
85 : { "OnSaveFailed", false },
86 : { "OnSaveTo", true },
87 : { "OnSaveToDone", false },
88 : { "OnSaveToFailed", false },
89 : { "OnPrepareUnload", true },
90 : { "OnUnload", true },
91 : { "OnFocus", false },
92 : { "OnUnfocus", false },
93 : { "OnModifyChanged", false },
94 : { "OnViewCreated", false },
95 : { "OnPrepareViewClosing", true },
96 : { "OnViewClosed", false },
97 : { "OnTitleChanged", false },
98 : { "OnSubComponentOpened", false },
99 : { "OnSubComponentClosed", false },
100 : { NULL, false }
101 : };
102 0 : return s_aData;
103 : }
104 : }
105 :
106 : // DocumentEvents
107 0 : DocumentEvents::DocumentEvents( ::cppu::OWeakObject& _rParent, ::osl::Mutex& _rMutex, DocumentEventsData& _rEventsData )
108 0 : :m_pData( new DocumentEvents_Data( _rParent, _rMutex, _rEventsData ) )
109 : {
110 0 : const DocumentEventData* pEventData = lcl_getDocumentEventData();
111 0 : while ( pEventData->pAsciiEventName )
112 : {
113 0 : OUString sEventName = OUString::createFromAscii( pEventData->pAsciiEventName );
114 0 : DocumentEventsData::iterator existingPos = m_pData->rEventsData.find( sEventName );
115 0 : if ( existingPos == m_pData->rEventsData.end() )
116 0 : m_pData->rEventsData[ sEventName ] = Sequence< PropertyValue >();
117 0 : ++pEventData;
118 0 : }
119 0 : }
120 :
121 0 : DocumentEvents::~DocumentEvents()
122 : {
123 0 : }
124 :
125 0 : void SAL_CALL DocumentEvents::acquire() throw()
126 : {
127 0 : m_pData->rParent.acquire();
128 0 : }
129 :
130 0 : void SAL_CALL DocumentEvents::release() throw()
131 : {
132 0 : m_pData->rParent.release();
133 0 : }
134 :
135 0 : bool DocumentEvents::needsSynchronousNotification( const OUString& _rEventName )
136 : {
137 0 : const DocumentEventData* pEventData = lcl_getDocumentEventData();
138 0 : while ( pEventData->pAsciiEventName )
139 : {
140 0 : if ( _rEventName.equalsAscii( pEventData->pAsciiEventName ) )
141 0 : return pEventData->bNeedsSyncNotify;
142 0 : ++pEventData;
143 : }
144 :
145 : // this is an unknown event ... assume async notification
146 0 : return false;
147 : }
148 :
149 0 : void SAL_CALL DocumentEvents::replaceByName( const OUString& _Name, const Any& _Element ) throw (IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException, std::exception)
150 : {
151 0 : ::osl::MutexGuard aGuard( m_pData->rMutex );
152 :
153 0 : DocumentEventsData::iterator elementPos = m_pData->rEventsData.find( _Name );
154 0 : if ( elementPos == m_pData->rEventsData.end() )
155 0 : throw NoSuchElementException( _Name, *this );
156 :
157 0 : Sequence< PropertyValue > aEventDescriptor;
158 0 : if ( _Element.hasValue() && !( _Element >>= aEventDescriptor ) )
159 0 : throw IllegalArgumentException( _Element.getValueTypeName(), *this, 2 );
160 :
161 : // Weird enough, the event assignment UI has (well: had) the idea of using an empty "EventType"/"Script"
162 : // to indicate the event descriptor should be reset, instead of just passing an empty event descriptor.
163 0 : ::comphelper::NamedValueCollection aCheck( aEventDescriptor );
164 0 : if ( aCheck.has( "EventType" ) )
165 : {
166 0 : OUString sEventType = aCheck.getOrDefault( "EventType", OUString() );
167 : OSL_ENSURE( !sEventType.isEmpty(), "DocumentEvents::replaceByName: doing a reset via an empty EventType is weird!" );
168 0 : if ( sEventType.isEmpty() )
169 0 : aEventDescriptor.realloc( 0 );
170 : }
171 0 : if ( aCheck.has( "Script" ) )
172 : {
173 0 : OUString sScript = aCheck.getOrDefault( "Script", OUString() );
174 : OSL_ENSURE( !sScript.isEmpty(), "DocumentEvents::replaceByName: doing a reset via an empty Script is weird!" );
175 0 : if ( sScript.isEmpty() )
176 0 : aEventDescriptor.realloc( 0 );
177 : }
178 :
179 0 : elementPos->second = aEventDescriptor;
180 0 : }
181 :
182 0 : Any SAL_CALL DocumentEvents::getByName( const OUString& _Name ) throw (NoSuchElementException, WrappedTargetException, RuntimeException, std::exception)
183 : {
184 0 : ::osl::MutexGuard aGuard( m_pData->rMutex );
185 :
186 0 : DocumentEventsData::const_iterator elementPos = m_pData->rEventsData.find( _Name );
187 0 : if ( elementPos == m_pData->rEventsData.end() )
188 0 : throw NoSuchElementException( _Name, *this );
189 :
190 0 : Any aReturn;
191 0 : const Sequence< PropertyValue >& rEventDesc( elementPos->second );
192 0 : if ( rEventDesc.getLength() > 0 )
193 0 : aReturn <<= rEventDesc;
194 0 : return aReturn;
195 : }
196 :
197 0 : Sequence< OUString > SAL_CALL DocumentEvents::getElementNames( ) throw (RuntimeException, std::exception)
198 : {
199 0 : ::osl::MutexGuard aGuard( m_pData->rMutex );
200 :
201 0 : Sequence< OUString > aNames( m_pData->rEventsData.size() );
202 : ::std::transform(
203 0 : m_pData->rEventsData.begin(),
204 0 : m_pData->rEventsData.end(),
205 : aNames.getArray(),
206 : ::o3tl::select1st< DocumentEventsData::value_type >()
207 0 : );
208 0 : return aNames;
209 : }
210 :
211 0 : sal_Bool SAL_CALL DocumentEvents::hasByName( const OUString& _Name ) throw (RuntimeException, std::exception)
212 : {
213 0 : ::osl::MutexGuard aGuard( m_pData->rMutex );
214 :
215 0 : return m_pData->rEventsData.find( _Name ) != m_pData->rEventsData.end();
216 : }
217 :
218 0 : Type SAL_CALL DocumentEvents::getElementType( ) throw (RuntimeException, std::exception)
219 : {
220 0 : return ::cppu::UnoType< Sequence< PropertyValue > >::get();
221 : }
222 :
223 0 : sal_Bool SAL_CALL DocumentEvents::hasElements( ) throw (RuntimeException, std::exception)
224 : {
225 0 : ::osl::MutexGuard aGuard( m_pData->rMutex );
226 0 : return !m_pData->rEventsData.empty();
227 : }
228 :
229 : } // namespace dbaccess
230 :
231 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|