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