LCOV - code coverage report
Current view: top level - dbaccess/source/core/dataaccess - documentevents.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 64 75 85.3 %
Date: 2014-11-03 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 <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         210 :         DocumentEvents_Data( ::cppu::OWeakObject& _rParent, ::osl::Mutex& _rMutex, DocumentEventsData& _rEventsData )
      57             :             :rParent( _rParent )
      58             :             ,rMutex( _rMutex )
      59         210 :             ,rEventsData( _rEventsData )
      60             :         {
      61         210 :         }
      62             :     };
      63             : 
      64             :     // helper
      65             :     struct DocumentEventData
      66             :     {
      67             :         const sal_Char* pAsciiEventName;
      68             :         bool            bNeedsSyncNotify;
      69             :     };
      70             : 
      71             :     namespace
      72             :     {
      73         224 :         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         224 :             return s_aData;
     103             :         }
     104             :     }
     105             : 
     106             :     // DocumentEvents
     107         210 :     DocumentEvents::DocumentEvents( ::cppu::OWeakObject& _rParent, ::osl::Mutex& _rMutex, DocumentEventsData& _rEventsData )
     108         210 :         :m_pData( new DocumentEvents_Data( _rParent, _rMutex, _rEventsData ) )
     109             :     {
     110         210 :         const DocumentEventData* pEventData = lcl_getDocumentEventData();
     111        5460 :         while ( pEventData->pAsciiEventName )
     112             :         {
     113        5040 :             OUString sEventName = OUString::createFromAscii( pEventData->pAsciiEventName );
     114        5040 :             DocumentEventsData::iterator existingPos = m_pData->rEventsData.find( sEventName );
     115        5040 :             if ( existingPos == m_pData->rEventsData.end() )
     116        5040 :                 m_pData->rEventsData[ sEventName ] = Sequence< PropertyValue >();
     117        5040 :             ++pEventData;
     118        5040 :         }
     119         210 :     }
     120             : 
     121         392 :     DocumentEvents::~DocumentEvents()
     122             :     {
     123         392 :     }
     124             : 
     125        2502 :     void SAL_CALL DocumentEvents::acquire() throw()
     126             :     {
     127        2502 :         m_pData->rParent.acquire();
     128        2502 :     }
     129             : 
     130        2502 :     void SAL_CALL DocumentEvents::release() throw()
     131             :     {
     132        2502 :         m_pData->rParent.release();
     133        2502 :     }
     134             : 
     135          14 :     bool DocumentEvents::needsSynchronousNotification( const OUString& _rEventName )
     136             :     {
     137          14 :         const DocumentEventData* pEventData = lcl_getDocumentEventData();
     138         238 :         while ( pEventData->pAsciiEventName )
     139             :         {
     140         224 :             if ( _rEventName.equalsAscii( pEventData->pAsciiEventName ) )
     141          14 :                 return pEventData->bNeedsSyncNotify;
     142         210 :             ++pEventData;
     143             :         }
     144             : 
     145             :         // this is an unknown event ... assume async notification
     146           0 :         return false;
     147             :     }
     148             : 
     149           6 :     void SAL_CALL DocumentEvents::replaceByName( const OUString& _Name, const Any& _Element ) throw (IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException, std::exception)
     150             :     {
     151           6 :         ::osl::MutexGuard aGuard( m_pData->rMutex );
     152             : 
     153           6 :         DocumentEventsData::iterator elementPos = m_pData->rEventsData.find( _Name );
     154           6 :         if ( elementPos == m_pData->rEventsData.end() )
     155           0 :             throw NoSuchElementException( _Name, *this );
     156             : 
     157          12 :         Sequence< PropertyValue > aEventDescriptor;
     158           6 :         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          12 :         ::comphelper::NamedValueCollection aCheck( aEventDescriptor );
     164           6 :         if ( aCheck.has( "EventType" ) )
     165             :         {
     166           6 :             OUString sEventType = aCheck.getOrDefault( "EventType", OUString() );
     167             :             OSL_ENSURE( !sEventType.isEmpty(), "DocumentEvents::replaceByName: doing a reset via an empty EventType is weird!" );
     168           6 :             if ( sEventType.isEmpty() )
     169           0 :                 aEventDescriptor.realloc( 0 );
     170             :         }
     171           6 :         if ( aCheck.has( "Script" ) )
     172             :         {
     173           6 :             OUString sScript = aCheck.getOrDefault( "Script", OUString() );
     174             :             OSL_ENSURE( !sScript.isEmpty(), "DocumentEvents::replaceByName: doing a reset via an empty Script is weird!" );
     175           6 :             if ( sScript.isEmpty() )
     176           0 :                 aEventDescriptor.realloc( 0 );
     177             :         }
     178             : 
     179          12 :         elementPos->second = aEventDescriptor;
     180           6 :     }
     181             : 
     182        5480 :     Any SAL_CALL DocumentEvents::getByName( const OUString& _Name ) throw (NoSuchElementException, WrappedTargetException, RuntimeException, std::exception)
     183             :     {
     184        5480 :         ::osl::MutexGuard aGuard( m_pData->rMutex );
     185             : 
     186        5480 :         DocumentEventsData::const_iterator elementPos = m_pData->rEventsData.find( _Name );
     187        5480 :         if ( elementPos == m_pData->rEventsData.end() )
     188           0 :             throw NoSuchElementException( _Name, *this );
     189             : 
     190        5480 :         Any aReturn;
     191        5480 :         const Sequence< PropertyValue >& rEventDesc( elementPos->second );
     192        5480 :         if ( rEventDesc.getLength() > 0 )
     193          12 :             aReturn <<= rEventDesc;
     194        5480 :         return aReturn;
     195             :     }
     196             : 
     197         184 :     Sequence< OUString > SAL_CALL DocumentEvents::getElementNames(  ) throw (RuntimeException, std::exception)
     198             :     {
     199         184 :         ::osl::MutexGuard aGuard( m_pData->rMutex );
     200             : 
     201         184 :         Sequence< OUString > aNames( m_pData->rEventsData.size() );
     202             :         ::std::transform(
     203         184 :             m_pData->rEventsData.begin(),
     204         184 :             m_pData->rEventsData.end(),
     205             :             aNames.getArray(),
     206             :             ::o3tl::select1st< DocumentEventsData::value_type >()
     207         552 :         );
     208         184 :         return aNames;
     209             :     }
     210             : 
     211        1070 :     sal_Bool SAL_CALL DocumentEvents::hasByName( const OUString& _Name ) throw (RuntimeException, std::exception)
     212             :     {
     213        1070 :         ::osl::MutexGuard aGuard( m_pData->rMutex );
     214             : 
     215        1070 :         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: */

Generated by: LCOV version 1.10