merge the formfield patch from ooo-build
[ooovba.git] / dbaccess / source / core / dataaccess / documentevents.cxx
blobbbba99bb79b5dc46e405107b11ba3ed3a88a940f
1 /*************************************************************************
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * Copyright 2008 by Sun Microsystems, Inc.
6 * OpenOffice.org - a multi-platform office productivity suite
8 * $RCSfile: documentevents.cxx,v $
10 * $Revision: 1.1.2.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
28 ************************************************************************/
30 // MARKER(update_precomp.py): autogen include statement, do not remove
31 #include "precompiled_dbaccess.hxx"
33 #include "documentevents.hxx"
35 /** === begin UNO includes === **/
36 #include <com/sun/star/beans/PropertyValue.hpp>
37 /** === end UNO includes === **/
39 #include <comphelper/namedvaluecollection.hxx>
41 #include <algorithm>
42 #include <functional>
44 //........................................................................
45 namespace dbaccess
47 //........................................................................
49 /** === begin UNO using === **/
50 using ::com::sun::star::uno::Reference;
51 using ::com::sun::star::uno::XInterface;
52 using ::com::sun::star::uno::UNO_QUERY;
53 using ::com::sun::star::uno::UNO_QUERY_THROW;
54 using ::com::sun::star::uno::UNO_SET_THROW;
55 using ::com::sun::star::uno::Exception;
56 using ::com::sun::star::uno::RuntimeException;
57 using ::com::sun::star::uno::Any;
58 using ::com::sun::star::uno::makeAny;
59 using ::com::sun::star::beans::PropertyValue;
60 using ::com::sun::star::container::NoSuchElementException;
61 using ::com::sun::star::lang::WrappedTargetException;
62 using ::com::sun::star::lang::IllegalArgumentException;
63 using ::com::sun::star::uno::Sequence;
64 using ::com::sun::star::uno::Type;
65 /** === end UNO using === **/
67 //====================================================================
68 //= DocumentEvents_Data
69 //====================================================================
70 struct DocumentEvents_Data : public ::boost::noncopyable
72 ::cppu::OWeakObject& rParent;
73 ::osl::Mutex& rMutex;
74 DocumentEventsData& rEventsData;
76 DocumentEvents_Data( ::cppu::OWeakObject& _rParent, ::osl::Mutex& _rMutex, DocumentEventsData& _rEventsData )
77 :rParent( _rParent )
78 ,rMutex( _rMutex )
79 ,rEventsData( _rEventsData )
84 //====================================================================
85 //= helper
86 //====================================================================
87 struct DocumentEventData
89 const sal_Char* pAsciiEventName;
90 bool bNeedsSyncNotify;
93 //--------------------------------------------------------------------
94 namespace
96 static const DocumentEventData* lcl_getDocumentEventData()
98 static const DocumentEventData s_aData[] = {
99 { "OnCreate", true },
100 { "OnLoadFinished", true },
101 { "OnNew", false }, // compatibility, see http://www.openoffice.org/issues/show_bug.cgi?id=46484
102 { "OnLoad", false }, // compatibility, see http://www.openoffice.org/issues/show_bug.cgi?id=46484
103 { "OnSaveAs", true },
104 { "OnSaveAsDone", false },
105 { "OnSaveAsFailed", false },
106 { "OnSave", true },
107 { "OnSaveDone", false },
108 { "OnSaveFailed", false },
109 { "OnSaveTo", true },
110 { "OnSaveToDone", false },
111 { "OnSaveToFailed", false },
112 { "OnPrepareUnload", true },
113 { "OnUnload", true },
114 { "OnFocus", false },
115 { "OnUnfocus", false },
116 { "OnModifyChanged", false },
117 { "OnViewCreated", false },
118 { "OnPrepareViewClosing", true },
119 { "OnViewClosed", false },
120 { "OnTitleChanged", false },
121 { "OnSubComponentOpened", false },
122 { "OnSubComponentClosed", false },
123 { NULL, false }
125 return s_aData;
129 //====================================================================
130 //= DocumentEvents
131 //====================================================================
132 //--------------------------------------------------------------------
133 DocumentEvents::DocumentEvents( ::cppu::OWeakObject& _rParent, ::osl::Mutex& _rMutex, DocumentEventsData& _rEventsData )
134 :m_pData( new DocumentEvents_Data( _rParent, _rMutex, _rEventsData ) )
136 const DocumentEventData* pEventData = lcl_getDocumentEventData();
137 while ( pEventData->pAsciiEventName )
139 ::rtl::OUString sEventName = ::rtl::OUString::createFromAscii( pEventData->pAsciiEventName );
140 DocumentEventsData::iterator existingPos = m_pData->rEventsData.find( sEventName );
141 if ( existingPos == m_pData->rEventsData.end() )
142 m_pData->rEventsData[ sEventName ] = Sequence< PropertyValue >();
143 ++pEventData;
147 //--------------------------------------------------------------------
148 DocumentEvents::~DocumentEvents()
152 //--------------------------------------------------------------------
153 void SAL_CALL DocumentEvents::acquire() throw()
155 m_pData->rParent.acquire();
158 //--------------------------------------------------------------------
159 void SAL_CALL DocumentEvents::release() throw()
161 m_pData->rParent.release();
164 //--------------------------------------------------------------------
165 bool DocumentEvents::needsSynchronousNotification( const ::rtl::OUString& _rEventName )
167 const DocumentEventData* pEventData = lcl_getDocumentEventData();
168 while ( pEventData->pAsciiEventName )
170 if ( _rEventName.compareToAscii( pEventData->pAsciiEventName ) == 0 )
171 return pEventData->bNeedsSyncNotify;
172 ++pEventData;
175 // this is an unknown event ... assume async notification
176 return false;
179 //--------------------------------------------------------------------
180 void SAL_CALL DocumentEvents::replaceByName( const ::rtl::OUString& _Name, const Any& _Element ) throw (IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
182 ::osl::MutexGuard aGuard( m_pData->rMutex );
184 DocumentEventsData::iterator elementPos = m_pData->rEventsData.find( _Name );
185 if ( elementPos == m_pData->rEventsData.end() )
186 throw NoSuchElementException( _Name, *this );
188 Sequence< PropertyValue > aEventDescriptor;
189 if ( _Element.hasValue() && !( _Element >>= aEventDescriptor ) )
190 throw IllegalArgumentException( _Element.getValueTypeName(), *this, 2 );
192 // Weird enough, the event assignment UI has (well: had) the idea of using an empty "EventType"/"Script"
193 // to indicate the event descriptor should be reset, instead of just passing an empty event descriptor.
194 ::comphelper::NamedValueCollection aCheck( aEventDescriptor );
195 if ( aCheck.has( "EventType" ) )
197 ::rtl::OUString sEventType = aCheck.getOrDefault( "EventType", ::rtl::OUString() );
198 OSL_ENSURE( sEventType.getLength(), "DocumentEvents::replaceByName: doing a reset via an empty EventType is weird!" );
199 if ( !sEventType.getLength() )
200 aEventDescriptor.realloc( 0 );
202 if ( aCheck.has( "Script" ) )
204 ::rtl::OUString sScript = aCheck.getOrDefault( "Script", ::rtl::OUString() );
205 OSL_ENSURE( sScript.getLength(), "DocumentEvents::replaceByName: doing a reset via an empty Script is weird!" );
206 if ( !sScript.getLength() )
207 aEventDescriptor.realloc( 0 );
210 elementPos->second = aEventDescriptor;
213 //--------------------------------------------------------------------
214 Any SAL_CALL DocumentEvents::getByName( const ::rtl::OUString& _Name ) throw (NoSuchElementException, WrappedTargetException, RuntimeException)
216 ::osl::MutexGuard aGuard( m_pData->rMutex );
218 DocumentEventsData::const_iterator elementPos = m_pData->rEventsData.find( _Name );
219 if ( elementPos == m_pData->rEventsData.end() )
220 throw NoSuchElementException( _Name, *this );
222 Any aReturn;
223 const Sequence< PropertyValue >& rEventDesc( elementPos->second );
224 if ( rEventDesc.getLength() > 0 )
225 aReturn <<= rEventDesc;
226 return aReturn;
229 //--------------------------------------------------------------------
230 Sequence< ::rtl::OUString > SAL_CALL DocumentEvents::getElementNames( ) throw (RuntimeException)
232 ::osl::MutexGuard aGuard( m_pData->rMutex );
234 Sequence< ::rtl::OUString > aNames( m_pData->rEventsData.size() );
235 ::std::transform(
236 m_pData->rEventsData.begin(),
237 m_pData->rEventsData.end(),
238 aNames.getArray(),
239 ::std::select1st< DocumentEventsData::value_type >()
241 return aNames;
244 //--------------------------------------------------------------------
245 ::sal_Bool SAL_CALL DocumentEvents::hasByName( const ::rtl::OUString& _Name ) throw (RuntimeException)
247 ::osl::MutexGuard aGuard( m_pData->rMutex );
249 return m_pData->rEventsData.find( _Name ) != m_pData->rEventsData.end();
252 //--------------------------------------------------------------------
253 Type SAL_CALL DocumentEvents::getElementType( ) throw (RuntimeException)
255 return ::cppu::UnoType< Sequence< PropertyValue > >::get();
258 //--------------------------------------------------------------------
259 ::sal_Bool SAL_CALL DocumentEvents::hasElements( ) throw (RuntimeException)
261 ::osl::MutexGuard aGuard( m_pData->rMutex );
262 return !m_pData->rEventsData.empty();
266 //........................................................................
267 } // namespace dbaccess
268 //........................................................................