1*96de5490SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*96de5490SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*96de5490SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*96de5490SAndrew Rist  * distributed with this work for additional information
6*96de5490SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*96de5490SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*96de5490SAndrew Rist  * "License"); you may not use this file except in compliance
9*96de5490SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*96de5490SAndrew Rist  *
11*96de5490SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*96de5490SAndrew Rist  *
13*96de5490SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*96de5490SAndrew Rist  * software distributed under the License is distributed on an
15*96de5490SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*96de5490SAndrew Rist  * KIND, either express or implied.  See the License for the
17*96de5490SAndrew Rist  * specific language governing permissions and limitations
18*96de5490SAndrew Rist  * under the License.
19*96de5490SAndrew Rist  *
20*96de5490SAndrew Rist  *************************************************************/
21*96de5490SAndrew Rist 
22*96de5490SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "documentevents.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir /** === begin UNO includes === **/
30cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
31cdf0e10cSrcweir /** === end UNO includes === **/
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <comphelper/namedvaluecollection.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <algorithm>
36cdf0e10cSrcweir #include <functional>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir //........................................................................
39cdf0e10cSrcweir namespace dbaccess
40cdf0e10cSrcweir {
41cdf0e10cSrcweir //........................................................................
42cdf0e10cSrcweir 
43cdf0e10cSrcweir     /** === begin UNO using === **/
44cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
45cdf0e10cSrcweir     using ::com::sun::star::uno::XInterface;
46cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY;
47cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
48cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_SET_THROW;
49cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
50cdf0e10cSrcweir     using ::com::sun::star::uno::RuntimeException;
51cdf0e10cSrcweir     using ::com::sun::star::uno::Any;
52cdf0e10cSrcweir     using ::com::sun::star::uno::makeAny;
53cdf0e10cSrcweir     using ::com::sun::star::beans::PropertyValue;
54cdf0e10cSrcweir     using ::com::sun::star::container::NoSuchElementException;
55cdf0e10cSrcweir     using ::com::sun::star::lang::WrappedTargetException;
56cdf0e10cSrcweir     using ::com::sun::star::lang::IllegalArgumentException;
57cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
58cdf0e10cSrcweir     using ::com::sun::star::uno::Type;
59cdf0e10cSrcweir     /** === end UNO using === **/
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     //====================================================================
62cdf0e10cSrcweir     //= DocumentEvents_Data
63cdf0e10cSrcweir     //====================================================================
64cdf0e10cSrcweir     struct DocumentEvents_Data : public ::boost::noncopyable
65cdf0e10cSrcweir     {
66cdf0e10cSrcweir         ::cppu::OWeakObject&    rParent;
67cdf0e10cSrcweir         ::osl::Mutex&           rMutex;
68cdf0e10cSrcweir         DocumentEventsData&     rEventsData;
69cdf0e10cSrcweir 
DocumentEvents_Datadbaccess::DocumentEvents_Data70cdf0e10cSrcweir         DocumentEvents_Data( ::cppu::OWeakObject& _rParent, ::osl::Mutex& _rMutex, DocumentEventsData& _rEventsData )
71cdf0e10cSrcweir             :rParent( _rParent )
72cdf0e10cSrcweir             ,rMutex( _rMutex )
73cdf0e10cSrcweir             ,rEventsData( _rEventsData )
74cdf0e10cSrcweir         {
75cdf0e10cSrcweir         }
76cdf0e10cSrcweir     };
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     //====================================================================
79cdf0e10cSrcweir     //= helper
80cdf0e10cSrcweir     //====================================================================
81cdf0e10cSrcweir     struct DocumentEventData
82cdf0e10cSrcweir     {
83cdf0e10cSrcweir         const sal_Char* pAsciiEventName;
84cdf0e10cSrcweir         bool            bNeedsSyncNotify;
85cdf0e10cSrcweir     };
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     //--------------------------------------------------------------------
88cdf0e10cSrcweir     namespace
89cdf0e10cSrcweir     {
lcl_getDocumentEventData()90cdf0e10cSrcweir         static const DocumentEventData* lcl_getDocumentEventData()
91cdf0e10cSrcweir         {
92cdf0e10cSrcweir             static const DocumentEventData s_aData[] = {
93cdf0e10cSrcweir                 { "OnCreate",               true  },
94cdf0e10cSrcweir                 { "OnLoadFinished",         true  },
95cdf0e10cSrcweir                 { "OnNew",                  false },    // compatibility, see http://www.openoffice.org/issues/show_bug.cgi?id=46484
96cdf0e10cSrcweir                 { "OnLoad",                 false },    // compatibility, see http://www.openoffice.org/issues/show_bug.cgi?id=46484
97cdf0e10cSrcweir                 { "OnSaveAs",               true  },
98cdf0e10cSrcweir                 { "OnSaveAsDone",           false },
99cdf0e10cSrcweir                 { "OnSaveAsFailed",         false },
100cdf0e10cSrcweir                 { "OnSave",                 true  },
101cdf0e10cSrcweir                 { "OnSaveDone",             false },
102cdf0e10cSrcweir                 { "OnSaveFailed",           false },
103cdf0e10cSrcweir                 { "OnSaveTo",               true  },
104cdf0e10cSrcweir                 { "OnSaveToDone",           false },
105cdf0e10cSrcweir                 { "OnSaveToFailed",         false },
106cdf0e10cSrcweir                 { "OnPrepareUnload",        true  },
107cdf0e10cSrcweir                 { "OnUnload",               true  },
108cdf0e10cSrcweir                 { "OnFocus",                false },
109cdf0e10cSrcweir                 { "OnUnfocus",              false },
110cdf0e10cSrcweir                 { "OnModifyChanged",        false },
111cdf0e10cSrcweir                 { "OnViewCreated",          false },
112cdf0e10cSrcweir                 { "OnPrepareViewClosing",   true  },
113cdf0e10cSrcweir                 { "OnViewClosed",           false },
114cdf0e10cSrcweir                 { "OnTitleChanged",         false },
115cdf0e10cSrcweir                 { "OnSubComponentOpened",   false },
116cdf0e10cSrcweir                 { "OnSubComponentClosed",   false },
117cdf0e10cSrcweir                 { NULL, false }
118cdf0e10cSrcweir             };
119cdf0e10cSrcweir             return s_aData;
120cdf0e10cSrcweir         }
121cdf0e10cSrcweir     }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     //====================================================================
124cdf0e10cSrcweir     //= DocumentEvents
125cdf0e10cSrcweir     //====================================================================
126cdf0e10cSrcweir     //--------------------------------------------------------------------
DocumentEvents(::cppu::OWeakObject & _rParent,::osl::Mutex & _rMutex,DocumentEventsData & _rEventsData)127cdf0e10cSrcweir     DocumentEvents::DocumentEvents( ::cppu::OWeakObject& _rParent, ::osl::Mutex& _rMutex, DocumentEventsData& _rEventsData )
128cdf0e10cSrcweir         :m_pData( new DocumentEvents_Data( _rParent, _rMutex, _rEventsData ) )
129cdf0e10cSrcweir     {
130cdf0e10cSrcweir         const DocumentEventData* pEventData = lcl_getDocumentEventData();
131cdf0e10cSrcweir         while ( pEventData->pAsciiEventName )
132cdf0e10cSrcweir         {
133cdf0e10cSrcweir             ::rtl::OUString sEventName = ::rtl::OUString::createFromAscii( pEventData->pAsciiEventName );
134cdf0e10cSrcweir             DocumentEventsData::iterator existingPos = m_pData->rEventsData.find( sEventName );
135cdf0e10cSrcweir             if ( existingPos == m_pData->rEventsData.end() )
136cdf0e10cSrcweir                 m_pData->rEventsData[ sEventName ] = Sequence< PropertyValue >();
137cdf0e10cSrcweir             ++pEventData;
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir     }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     //--------------------------------------------------------------------
~DocumentEvents()142cdf0e10cSrcweir     DocumentEvents::~DocumentEvents()
143cdf0e10cSrcweir     {
144cdf0e10cSrcweir     }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     //--------------------------------------------------------------------
acquire()147cdf0e10cSrcweir     void SAL_CALL DocumentEvents::acquire() throw()
148cdf0e10cSrcweir     {
149cdf0e10cSrcweir         m_pData->rParent.acquire();
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     //--------------------------------------------------------------------
release()153cdf0e10cSrcweir     void SAL_CALL DocumentEvents::release() throw()
154cdf0e10cSrcweir     {
155cdf0e10cSrcweir         m_pData->rParent.release();
156cdf0e10cSrcweir     }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     //--------------------------------------------------------------------
needsSynchronousNotification(const::rtl::OUString & _rEventName)159cdf0e10cSrcweir     bool DocumentEvents::needsSynchronousNotification( const ::rtl::OUString& _rEventName )
160cdf0e10cSrcweir     {
161cdf0e10cSrcweir         const DocumentEventData* pEventData = lcl_getDocumentEventData();
162cdf0e10cSrcweir         while ( pEventData->pAsciiEventName )
163cdf0e10cSrcweir         {
164cdf0e10cSrcweir             if ( _rEventName.compareToAscii( pEventData->pAsciiEventName ) == 0 )
165cdf0e10cSrcweir                 return pEventData->bNeedsSyncNotify;
166cdf0e10cSrcweir             ++pEventData;
167cdf0e10cSrcweir         }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir         // this is an unknown event ... assume async notification
170cdf0e10cSrcweir         return false;
171cdf0e10cSrcweir     }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir     //--------------------------------------------------------------------
replaceByName(const::rtl::OUString & _Name,const Any & _Element)174cdf0e10cSrcweir     void SAL_CALL DocumentEvents::replaceByName( const ::rtl::OUString& _Name, const Any& _Element ) throw (IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
175cdf0e10cSrcweir     {
176cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_pData->rMutex );
177cdf0e10cSrcweir 
178cdf0e10cSrcweir         DocumentEventsData::iterator elementPos = m_pData->rEventsData.find( _Name );
179cdf0e10cSrcweir         if ( elementPos == m_pData->rEventsData.end() )
180cdf0e10cSrcweir             throw NoSuchElementException( _Name, *this );
181cdf0e10cSrcweir 
182cdf0e10cSrcweir         Sequence< PropertyValue > aEventDescriptor;
183cdf0e10cSrcweir         if ( _Element.hasValue() && !( _Element >>= aEventDescriptor ) )
184cdf0e10cSrcweir             throw IllegalArgumentException( _Element.getValueTypeName(), *this, 2 );
185cdf0e10cSrcweir 
186cdf0e10cSrcweir         // Weird enough, the event assignment UI has (well: had) the idea of using an empty "EventType"/"Script"
187cdf0e10cSrcweir         // to indicate the event descriptor should be reset, instead of just passing an empty event descriptor.
188cdf0e10cSrcweir         ::comphelper::NamedValueCollection aCheck( aEventDescriptor );
189cdf0e10cSrcweir         if ( aCheck.has( "EventType" ) )
190cdf0e10cSrcweir         {
191cdf0e10cSrcweir             ::rtl::OUString sEventType = aCheck.getOrDefault( "EventType", ::rtl::OUString() );
192cdf0e10cSrcweir             OSL_ENSURE( sEventType.getLength(), "DocumentEvents::replaceByName: doing a reset via an empty EventType is weird!" );
193cdf0e10cSrcweir             if ( !sEventType.getLength() )
194cdf0e10cSrcweir                 aEventDescriptor.realloc( 0 );
195cdf0e10cSrcweir         }
196cdf0e10cSrcweir         if ( aCheck.has( "Script" ) )
197cdf0e10cSrcweir         {
198cdf0e10cSrcweir             ::rtl::OUString sScript = aCheck.getOrDefault( "Script", ::rtl::OUString() );
199cdf0e10cSrcweir             OSL_ENSURE( sScript.getLength(), "DocumentEvents::replaceByName: doing a reset via an empty Script is weird!" );
200cdf0e10cSrcweir             if ( !sScript.getLength() )
201cdf0e10cSrcweir                 aEventDescriptor.realloc( 0 );
202cdf0e10cSrcweir         }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir         elementPos->second = aEventDescriptor;
205cdf0e10cSrcweir     }
206cdf0e10cSrcweir 
207cdf0e10cSrcweir     //--------------------------------------------------------------------
getByName(const::rtl::OUString & _Name)208cdf0e10cSrcweir     Any SAL_CALL DocumentEvents::getByName( const ::rtl::OUString& _Name ) throw (NoSuchElementException, WrappedTargetException, RuntimeException)
209cdf0e10cSrcweir     {
210cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_pData->rMutex );
211cdf0e10cSrcweir 
212cdf0e10cSrcweir         DocumentEventsData::const_iterator elementPos = m_pData->rEventsData.find( _Name );
213cdf0e10cSrcweir         if ( elementPos == m_pData->rEventsData.end() )
214cdf0e10cSrcweir             throw NoSuchElementException( _Name, *this );
215cdf0e10cSrcweir 
216cdf0e10cSrcweir         Any aReturn;
217cdf0e10cSrcweir         const Sequence< PropertyValue >& rEventDesc( elementPos->second );
218cdf0e10cSrcweir         if ( rEventDesc.getLength() > 0 )
219cdf0e10cSrcweir             aReturn <<= rEventDesc;
220cdf0e10cSrcweir         return aReturn;
221cdf0e10cSrcweir     }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir     //--------------------------------------------------------------------
getElementNames()224cdf0e10cSrcweir     Sequence< ::rtl::OUString > SAL_CALL DocumentEvents::getElementNames(  ) throw (RuntimeException)
225cdf0e10cSrcweir     {
226cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_pData->rMutex );
227cdf0e10cSrcweir 
228cdf0e10cSrcweir         Sequence< ::rtl::OUString > aNames( m_pData->rEventsData.size() );
229cdf0e10cSrcweir         ::std::transform(
230cdf0e10cSrcweir             m_pData->rEventsData.begin(),
231cdf0e10cSrcweir             m_pData->rEventsData.end(),
232cdf0e10cSrcweir             aNames.getArray(),
233cdf0e10cSrcweir             ::std::select1st< DocumentEventsData::value_type >()
234cdf0e10cSrcweir         );
235cdf0e10cSrcweir         return aNames;
236cdf0e10cSrcweir     }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir     //--------------------------------------------------------------------
hasByName(const::rtl::OUString & _Name)239cdf0e10cSrcweir     ::sal_Bool SAL_CALL DocumentEvents::hasByName( const ::rtl::OUString& _Name ) throw (RuntimeException)
240cdf0e10cSrcweir     {
241cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_pData->rMutex );
242cdf0e10cSrcweir 
243cdf0e10cSrcweir         return m_pData->rEventsData.find( _Name ) != m_pData->rEventsData.end();
244cdf0e10cSrcweir     }
245cdf0e10cSrcweir 
246cdf0e10cSrcweir     //--------------------------------------------------------------------
getElementType()247cdf0e10cSrcweir     Type SAL_CALL DocumentEvents::getElementType(  ) throw (RuntimeException)
248cdf0e10cSrcweir     {
249cdf0e10cSrcweir         return ::cppu::UnoType< Sequence< PropertyValue > >::get();
250cdf0e10cSrcweir     }
251cdf0e10cSrcweir 
252cdf0e10cSrcweir     //--------------------------------------------------------------------
hasElements()253cdf0e10cSrcweir     ::sal_Bool SAL_CALL DocumentEvents::hasElements(  ) throw (RuntimeException)
254cdf0e10cSrcweir     {
255cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_pData->rMutex );
256cdf0e10cSrcweir         return !m_pData->rEventsData.empty();
257cdf0e10cSrcweir     }
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 
260cdf0e10cSrcweir //........................................................................
261cdf0e10cSrcweir } // namespace dbaccess
262cdf0e10cSrcweir //........................................................................
263