LCOV - code coverage report
Current view: top level - dbaccess/source/core/dataaccess - documentevents.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 64 75 85.3 %
Date: 2015-06-13 12:38:46 Functions: 12 14 85.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11