LCOV - code coverage report
Current view: top level - forms/source/component - ImageControl.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 194 418 46.4 %
Date: 2014-04-11 Functions: 31 53 58.5 %
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 "ImageControl.hxx"
      21             : 
      22             : #include "property.hrc"
      23             : #include "frm_resource.hrc"
      24             : #include "frm_resource.hxx"
      25             : #include "services.hxx"
      26             : #include "componenttools.hxx"
      27             : 
      28             : #include <svtools/imageresourceaccess.hxx>
      29             : #include <unotools/ucblockbytes.hxx>
      30             : #include <sfx2/filedlghelper.hxx>
      31             : #include <com/sun/star/awt/PopupMenu.hpp>
      32             : #include <com/sun/star/awt/XPopupMenu.hpp>
      33             : #include <com/sun/star/awt/PopupMenuDirection.hpp>
      34             : #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
      35             : #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
      36             : #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
      37             : #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
      38             : #include <com/sun/star/sdbc/DataType.hpp>
      39             : #include <com/sun/star/awt/MouseButton.hpp>
      40             : #include <com/sun/star/awt/XWindow.hpp>
      41             : #include <com/sun/star/awt/XDialog.hpp>
      42             : #include <com/sun/star/io/XActiveDataSink.hpp>
      43             : #include <com/sun/star/io/NotConnectedException.hpp>
      44             : #include <com/sun/star/beans/PropertyValue.hpp>
      45             : #include <com/sun/star/graphic/XGraphic.hpp>
      46             : #include <com/sun/star/graphic/GraphicObject.hpp>
      47             : #include <tools/urlobj.hxx>
      48             : #include <tools/stream.hxx>
      49             : #include <tools/debug.hxx>
      50             : #include <tools/diagnose_ex.h>
      51             : #include <vcl/svapp.hxx>
      52             : #include <unotools/streamhelper.hxx>
      53             : #include <comphelper/guarding.hxx>
      54             : #include <comphelper/processfactory.hxx>
      55             : #include <unotools/ucbstreamhelper.hxx>
      56             : #include <svl/urihelper.hxx>
      57             : 
      58             : #include <boost/scoped_ptr.hpp>
      59             : 
      60             : #define ID_OPEN_GRAPHICS            1
      61             : #define ID_CLEAR_GRAPHICS           2
      62             : 
      63             : 
      64             : namespace frm
      65             : {
      66             : 
      67             : using namespace ::com::sun::star;
      68             : using namespace ::com::sun::star::uno;
      69             : using namespace ::com::sun::star::sdb;
      70             : using namespace ::com::sun::star::sdbc;
      71             : using namespace ::com::sun::star::sdbcx;
      72             : using namespace ::com::sun::star::beans;
      73             : using namespace ::com::sun::star::container;
      74             : using namespace ::com::sun::star::form;
      75             : using namespace ::com::sun::star::awt;
      76             : using namespace ::com::sun::star::io;
      77             : using namespace ::com::sun::star::ui::dialogs;
      78             : using namespace ::com::sun::star::lang;
      79             : using namespace ::com::sun::star::util;
      80             : using namespace ::com::sun::star::graphic;
      81             : using namespace ::com::sun::star::frame;
      82             : 
      83             : 
      84             : //= OImageControlModel
      85             : 
      86             : namespace
      87             : {
      88             :     enum ImageStoreType
      89             :     {
      90             :         ImageStoreBinary,
      91             :         ImageStoreLink,
      92             : 
      93             :         ImageStoreInvalid
      94             :     };
      95             : 
      96          12 :     ImageStoreType lcl_getImageStoreType( const sal_Int32 _nFieldType )
      97             :     {
      98             :         // binary/longvarchar types could be used to store images in binary representation
      99          12 :         if  (   ( _nFieldType == DataType::BINARY )
     100          12 :             ||  ( _nFieldType == DataType::VARBINARY )
     101          12 :             ||  ( _nFieldType == DataType::LONGVARBINARY )
     102          12 :             ||  ( _nFieldType == DataType::OTHER )
     103           0 :             ||  ( _nFieldType == DataType::OBJECT )
     104           0 :             ||  ( _nFieldType == DataType::BLOB )
     105           0 :             ||  ( _nFieldType == DataType::LONGVARCHAR )
     106           0 :             ||  ( _nFieldType == DataType::CLOB )
     107             :             )
     108          12 :             return ImageStoreBinary;
     109             : 
     110             :         // char types could be used to store links to images
     111           0 :         if  (   ( _nFieldType == DataType::CHAR )
     112           0 :             ||  ( _nFieldType == DataType::VARCHAR )
     113             :             )
     114           0 :             return ImageStoreLink;
     115             : 
     116           0 :         return ImageStoreInvalid;
     117             :     }
     118             : }
     119             : 
     120             : 
     121             : // OImageControlModel
     122             : 
     123             : 
     124             : 
     125           9 : InterfaceRef SAL_CALL OImageControlModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
     126             : {
     127           9 :     return *(new OImageControlModel( comphelper::getComponentContext(_rxFactory) ));
     128             : }
     129             : 
     130             : 
     131           0 : Sequence<Type> OImageControlModel::_getTypes()
     132             : {
     133             :     return concatSequences(
     134             :         OBoundControlModel::_getTypes(),
     135             :         OImageControlModel_Base::getTypes()
     136           0 :     );
     137             : }
     138             : 
     139             : 
     140           9 : OImageControlModel::OImageControlModel(const Reference<XComponentContext>& _rxFactory)
     141             :     :OBoundControlModel( _rxFactory, VCL_CONTROLMODEL_IMAGECONTROL, FRM_SUN_CONTROL_IMAGECONTROL, sal_False, sal_False, sal_False )
     142             :                     // use the old control name for compytibility reasons
     143             :     ,m_pImageProducer( NULL )
     144             :     ,m_bExternalGraphic( true )
     145             :     ,m_bReadOnly( sal_False )
     146             :     ,m_sImageURL()
     147           9 :     ,m_xGraphicObject()
     148             : {
     149           9 :     m_nClassId = FormComponentType::IMAGECONTROL;
     150           9 :     initOwnValueProperty( PROPERTY_IMAGE_URL );
     151             : 
     152           9 :     implConstruct();
     153           9 : }
     154             : 
     155             : 
     156           1 : OImageControlModel::OImageControlModel( const OImageControlModel* _pOriginal, const Reference< XComponentContext >& _rxFactory )
     157             :     :OBoundControlModel( _pOriginal, _rxFactory )
     158             :                 // use the old control name for compytibility reasons
     159             :     ,m_pImageProducer( NULL )
     160             :     ,m_bExternalGraphic( true )
     161             :     ,m_bReadOnly( _pOriginal->m_bReadOnly )
     162             :     ,m_sImageURL( _pOriginal->m_sImageURL )
     163           1 :     ,m_xGraphicObject( _pOriginal->m_xGraphicObject )
     164             : {
     165           1 :     implConstruct();
     166             : 
     167           1 :     osl_atomic_increment( &m_refCount );
     168             :     {
     169             :         // simulate a propertyChanged event for the ImageURL
     170           1 :         ::osl::MutexGuard aGuard( m_aMutex );
     171           1 :         impl_handleNewImageURL_lck( eOther );
     172             :     }
     173           1 :     osl_atomic_decrement( &m_refCount );
     174           1 : }
     175             : 
     176             : 
     177          10 : void OImageControlModel::implConstruct()
     178             : {
     179          10 :     m_pImageProducer = new ImageProducer;
     180          10 :     m_xImageProducer = m_pImageProducer;
     181          10 :     m_pImageProducer->SetDoneHdl( LINK( this, OImageControlModel, OnImageImportDone ) );
     182          10 : }
     183             : 
     184             : 
     185          30 : OImageControlModel::~OImageControlModel()
     186             : {
     187          10 :     if (!OComponentHelper::rBHelper.bDisposed)
     188             :     {
     189           0 :         acquire();
     190           0 :         dispose();
     191             :     }
     192             : 
     193          20 : }
     194             : 
     195             : // XCloneable
     196             : 
     197           1 : IMPLEMENT_DEFAULT_CLONING( OImageControlModel )
     198             : 
     199             : // XServiceInfo
     200             : 
     201           6 : StringSequence  OImageControlModel::getSupportedServiceNames() throw(std::exception)
     202             : {
     203           6 :     StringSequence aSupported = OBoundControlModel::getSupportedServiceNames();
     204           6 :     aSupported.realloc(aSupported.getLength() + 1);
     205             : 
     206           6 :     OUString*pArray = aSupported.getArray();
     207           6 :     pArray[aSupported.getLength()-1] = FRM_SUN_COMPONENT_IMAGECONTROL;
     208           6 :     return aSupported;
     209             : }
     210             : 
     211             : 
     212        1690 : Any SAL_CALL OImageControlModel::queryAggregation(const Type& _rType) throw (RuntimeException, std::exception)
     213             : {
     214             :     // Order matters: we want to "override" the XImageProducer interface of the aggregate without
     215             :     // own XImageProducer interface, thus we need to query OImageControlModel_Base first
     216        1690 :     Any aReturn = OImageControlModel_Base::queryInterface( _rType );
     217             : 
     218             :     // BUT: _don't_ let it feel responsible for the XTypeProvider interface
     219             :     // (as this is implemented by our base class in the proper way)
     220        3380 :     if  (   _rType.equals( ::getCppuType( static_cast< Reference< XTypeProvider >* >( NULL ) ) )
     221        1690 :         ||  !aReturn.hasValue()
     222             :         )
     223         928 :         aReturn = OBoundControlModel::queryAggregation( _rType );
     224             : 
     225        1690 :     return aReturn;
     226             : }
     227             : 
     228             : 
     229           0 : sal_Bool OImageControlModel::approveDbColumnType( sal_Int32 _nColumnType )
     230             : {
     231           0 :     return ImageStoreInvalid != lcl_getImageStoreType( _nColumnType );
     232             : }
     233             : 
     234             : 
     235        1153 : void OImageControlModel::getFastPropertyValue(Any& rValue, sal_Int32 nHandle) const
     236             : {
     237        1153 :     switch (nHandle)
     238             :     {
     239             :         case PROPERTY_ID_READONLY:
     240          44 :             rValue <<= (sal_Bool)m_bReadOnly;
     241          44 :             break;
     242             :         case PROPERTY_ID_IMAGE_URL:
     243          35 :             rValue <<= m_sImageURL;
     244          35 :             break;
     245             :         case PROPERTY_ID_GRAPHIC:
     246          62 :             rValue <<= m_xGraphicObject.is() ? m_xGraphicObject->getGraphic() : Reference< XGraphic >();
     247          62 :             break;
     248             :         default:
     249        1012 :             OBoundControlModel::getFastPropertyValue(rValue, nHandle);
     250             :     }
     251        1153 : }
     252             : 
     253             : 
     254         103 : void OImageControlModel::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const Any& rValue) throw ( ::com::sun::star::uno::Exception, std::exception)
     255             : {
     256         103 :     switch (nHandle)
     257             :     {
     258             :         case PROPERTY_ID_READONLY :
     259             :             DBG_ASSERT(rValue.getValueType().getTypeClass() == TypeClass_BOOLEAN, "OImageControlModel::setFastPropertyValue_NoBroadcast : invalid type !" );
     260           9 :             m_bReadOnly = getBOOL(rValue);
     261           9 :             break;
     262             : 
     263             :         case PROPERTY_ID_IMAGE_URL:
     264           5 :             OSL_VERIFY( rValue >>= m_sImageURL );
     265           5 :             impl_handleNewImageURL_lck( eOther );
     266             :             {
     267           5 :                 ControlModelLock aLock( *this );
     268             :                     // that's a fake ... onValuePropertyChange expects to receive the only lock to our instance,
     269             :                     // but we're already called with our mutex locked ...
     270           5 :                 onValuePropertyChange( aLock );
     271             :             }
     272           5 :             break;
     273             : 
     274             :         case PROPERTY_ID_GRAPHIC:
     275             :         {
     276           1 :             Reference< XGraphic > xGraphic;
     277           1 :             OSL_VERIFY( rValue >>= xGraphic );
     278           1 :             if ( !xGraphic.is() )
     279           0 :                 m_xGraphicObject.clear();
     280             :             else
     281             :             {
     282           1 :                 m_xGraphicObject = GraphicObject::create( m_xContext );
     283           1 :                 m_xGraphicObject->setGraphic( xGraphic );
     284             :             }
     285             : 
     286           1 :             if ( m_bExternalGraphic )
     287             :             {
     288             :                 // if that's an external graphic, i.e. one which has not been loaded by ourselves in response to a
     289             :                 // new image URL, then also adjust our ImageURL.
     290           0 :                 OUString sNewImageURL;
     291           0 :                 if ( m_xGraphicObject.is() )
     292             :                 {
     293           0 :                     sNewImageURL = "vnd.sun.star.GraphicObject:";
     294           0 :                     sNewImageURL = sNewImageURL + m_xGraphicObject->getUniqueID();
     295             :                 }
     296           0 :                 m_sImageURL = sNewImageURL;
     297             :                 // TODO: speaking strictly, this would need to be notified, since ImageURL is a bound property. However,
     298             :                 // this method here is called with a locked mutex, so we cannot simply call listeners ...
     299             :                 // I think the missing notification (and thus clients which potentially cannot observe the change)
     300             :                 // is less severe than the potential deadlock ...
     301           1 :             }
     302             :         }
     303           1 :         break;
     304             : 
     305             :         default:
     306          88 :             OBoundControlModel::setFastPropertyValue_NoBroadcast(nHandle, rValue);
     307          88 :             break;
     308             :     }
     309         103 : }
     310             : 
     311             : 
     312         331 : sal_Bool OImageControlModel::convertFastPropertyValue(Any& rConvertedValue, Any& rOldValue, sal_Int32 nHandle, const Any& rValue)
     313             :                                 throw( IllegalArgumentException )
     314             : {
     315         331 :     switch (nHandle)
     316             :     {
     317             :         case PROPERTY_ID_READONLY :
     318          29 :             return tryPropertyValue(rConvertedValue, rOldValue, rValue, m_bReadOnly);
     319             : 
     320             :         case PROPERTY_ID_IMAGE_URL:
     321          25 :             return tryPropertyValue( rConvertedValue, rOldValue, rValue, m_sImageURL );
     322             : 
     323             :         case PROPERTY_ID_GRAPHIC:
     324             :         {
     325          28 :             const Reference< XGraphic > xGraphic( getFastPropertyValue( PROPERTY_ID_GRAPHIC ), UNO_QUERY );
     326          28 :             return tryPropertyValue( rConvertedValue, rOldValue, rValue, xGraphic );
     327             :         }
     328             : 
     329             :         default:
     330         249 :             return OBoundControlModel::convertFastPropertyValue(rConvertedValue, rOldValue, nHandle, rValue);
     331             :     }
     332             : }
     333             : 
     334             : 
     335          11 : void OImageControlModel::describeFixedProperties( Sequence< Property >& _rProps ) const
     336             : {
     337          11 :     BEGIN_DESCRIBE_PROPERTIES( 4, OBoundControlModel )
     338          11 :         DECL_IFACE_PROP2( GRAPHIC,   XGraphic,        BOUND, TRANSIENT );
     339          11 :         DECL_PROP1      ( IMAGE_URL, OUString, BOUND );
     340          11 :         DECL_BOOL_PROP1 ( READONLY,                   BOUND );
     341          11 :         DECL_PROP1      ( TABINDEX,  sal_Int16,       BOUND );
     342             :     END_DESCRIBE_PROPERTIES();
     343          11 : }
     344             : 
     345             : 
     346          11 : void OImageControlModel::describeAggregateProperties( Sequence< Property >& /* [out] */ o_rAggregateProperties ) const
     347             : {
     348          11 :     OBoundControlModel::describeAggregateProperties( o_rAggregateProperties );
     349             :     // remove ImageULR and Graphic properties, we "overload" them. This is because our aggregate synchronizes those
     350             :     // two, but we have an own sychronization mechanism.
     351          11 :     RemoveProperty( o_rAggregateProperties, PROPERTY_IMAGE_URL );
     352          11 :     RemoveProperty( o_rAggregateProperties, PROPERTY_GRAPHIC );
     353          11 : }
     354             : 
     355             : 
     356           1 : OUString OImageControlModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException, std::exception)
     357             : {
     358           1 :     return OUString(FRM_COMPONENT_IMAGECONTROL);  // old (non-sun) name for compatibility !
     359             : }
     360             : 
     361             : 
     362           1 : void OImageControlModel::write(const Reference<XObjectOutputStream>& _rxOutStream) throw ( ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception)
     363             : {
     364             :     // Base class
     365           1 :     OBoundControlModel::write(_rxOutStream);
     366             :     // Version
     367           1 :     _rxOutStream->writeShort(0x0003);
     368             :     // Name
     369           1 :     _rxOutStream->writeBoolean(m_bReadOnly);
     370           1 :     writeHelpTextCompatibly(_rxOutStream);
     371             :     // from version 0x0003 : common properties
     372           1 :     writeCommonProperties(_rxOutStream);
     373           1 : }
     374             : 
     375             : 
     376           1 : void OImageControlModel::read(const Reference<XObjectInputStream>& _rxInStream) throw ( ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception)
     377             : {
     378           1 :     OBoundControlModel::read(_rxInStream);
     379             : 
     380             :     // Version
     381           1 :     sal_uInt16 nVersion = _rxInStream->readShort();
     382           1 :     switch (nVersion)
     383             :     {
     384             :         case 0x0001:
     385           0 :             m_bReadOnly = _rxInStream->readBoolean();
     386           0 :             break;
     387             :         case 0x0002:
     388           0 :             m_bReadOnly = _rxInStream->readBoolean();
     389           0 :             readHelpTextCompatibly(_rxInStream);
     390           0 :             break;
     391             :         case 0x0003:
     392           1 :             m_bReadOnly = _rxInStream->readBoolean();
     393           1 :             readHelpTextCompatibly(_rxInStream);
     394           1 :             readCommonProperties(_rxInStream);
     395           1 :             break;
     396             :         default :
     397             :             OSL_FAIL("OImageControlModel::read : unknown version !");
     398           0 :             m_bReadOnly = sal_False;
     399           0 :             defaultCommonProperties();
     400           0 :             break;
     401             :     }
     402             :     // Display default values after read
     403           1 :     if ( !getControlSource().isEmpty() )
     404             :     {   // (not if we don't have a control source - the "State" property acts like it is persistent, then
     405           1 :         ::osl::MutexGuard aGuard(m_aMutex); // resetNoBroadcast expects this mutex guarding
     406           1 :         resetNoBroadcast();
     407             :     }
     408           1 : }
     409             : 
     410             : 
     411           6 : sal_Bool OImageControlModel::impl_updateStreamForURL_lck( const OUString& _rURL, ValueChangeInstigator _eInstigator )
     412             : {
     413             :     // create a stream for the image specified by the URL
     414           6 :     boost::scoped_ptr< SvStream > pImageStream;
     415          12 :     Reference< XInputStream > xImageStream;
     416             : 
     417           6 :     if ( ::svt::GraphicAccess::isSupportedURL( _rURL ) )
     418             :     {
     419           0 :         xImageStream = ::svt::GraphicAccess::getImageXStream( getContext(), _rURL );
     420             :     }
     421             :     else
     422             :     {
     423           6 :         pImageStream.reset( ::utl::UcbStreamHelper::CreateStream( _rURL, STREAM_READ ) );
     424           6 :         sal_Bool bSetNull = ( pImageStream.get() == NULL ) || ( ERRCODE_NONE != pImageStream->GetErrorCode() );
     425             : 
     426           6 :         if ( !bSetNull )
     427             :         {
     428             :             // get the size of the stream
     429           1 :             pImageStream->Seek(STREAM_SEEK_TO_END);
     430           1 :             sal_Int32 nSize = (sal_Int32)pImageStream->Tell();
     431           1 :             if (pImageStream->GetBufferSize() < 8192)
     432           1 :                 pImageStream->SetBufferSize(8192);
     433           1 :             pImageStream->Seek(STREAM_SEEK_TO_BEGIN);
     434             : 
     435           1 :             xImageStream = new ::utl::OInputStreamHelper( new SvLockBytes( pImageStream.get(), false ), nSize );
     436             :         }
     437             :     }
     438             : 
     439           6 :     if ( xImageStream.is() )
     440             :     {
     441           1 :         if ( m_xColumnUpdate.is() )
     442           0 :             m_xColumnUpdate->updateBinaryStream( xImageStream, xImageStream->available() );
     443             :         else
     444           1 :             setControlValue( makeAny( xImageStream ), _eInstigator );
     445           1 :         xImageStream->closeInput();
     446           1 :         return sal_True;
     447             :     }
     448             : 
     449          11 :     return sal_False;
     450             : }
     451             : 
     452             : 
     453           6 : sal_Bool OImageControlModel::impl_handleNewImageURL_lck( ValueChangeInstigator _eInstigator )
     454             : {
     455           6 :     switch ( lcl_getImageStoreType( getFieldType() ) )
     456             :     {
     457             :     case ImageStoreBinary:
     458           6 :         if ( impl_updateStreamForURL_lck( m_sImageURL, _eInstigator ) )
     459           1 :             return sal_True;
     460           5 :         break;
     461             : 
     462             :     case ImageStoreLink:
     463             :     {
     464           0 :         OUString sCommitURL( m_sImageURL );
     465           0 :         if ( !m_sDocumentURL.isEmpty() )
     466           0 :             sCommitURL = URIHelper::simpleNormalizedMakeRelative( m_sDocumentURL, sCommitURL );
     467             :         OSL_ENSURE( m_xColumnUpdate.is(), "OImageControlModel::impl_handleNewImageURL_lck: no bound field, but ImageStoreLink?!" );
     468           0 :         if ( m_xColumnUpdate.is() )
     469             :         {
     470           0 :             m_xColumnUpdate->updateString( sCommitURL );
     471           0 :             return sal_True;
     472           0 :         }
     473             :     }
     474           0 :     break;
     475             : 
     476             :     case ImageStoreInvalid:
     477             :         OSL_FAIL( "OImageControlModel::impl_handleNewImageURL_lck: image storage type type!" );
     478           0 :         break;
     479             :     }
     480             : 
     481             :     // if we're here, then the above code was unable to update our field/control from the given URL
     482             :     // => fall back to NULL/VOID
     483           5 :     if ( m_xColumnUpdate.is() )
     484           0 :         m_xColumnUpdate->updateNull();
     485             :     else
     486           5 :         setControlValue( Any(), _eInstigator );
     487             : 
     488           5 :     return sal_True;
     489             : }
     490             : 
     491             : 
     492           0 : sal_Bool OImageControlModel::commitControlValueToDbColumn( bool _bPostReset )
     493             : {
     494           0 :     if ( _bPostReset )
     495             :     {
     496             :         // since this is a "commit after reset", we can simply update the column
     497             :         // with null - this is our "default" which we were just reset to
     498           0 :         if ( m_xColumnUpdate.is() )
     499           0 :             m_xColumnUpdate->updateNull();
     500             :     }
     501             :     else
     502             :     {
     503           0 :         ::osl::MutexGuard aGuard(m_aMutex);
     504           0 :         return impl_handleNewImageURL_lck( eDbColumnBinding );
     505             :     }
     506             : 
     507           0 :     return sal_True;
     508             : }
     509             : 
     510             : 
     511             : namespace
     512             : {
     513           0 :     bool lcl_isValidDocumentURL( const OUString& _rDocURL )
     514             :     {
     515           0 :         return ( !_rDocURL.isEmpty() && _rDocURL != "private:object" );
     516             :     }
     517             : }
     518             : 
     519             : 
     520           0 : void OImageControlModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
     521             : {
     522           0 :     OBoundControlModel::onConnectedDbColumn( _rxForm );
     523             : 
     524             :     try
     525             :     {
     526           0 :         Reference< XModel > xDocument( getXModel( *this ) );
     527           0 :         if ( xDocument.is() )
     528             :         {
     529           0 :             m_sDocumentURL = xDocument->getURL();
     530           0 :             if ( !lcl_isValidDocumentURL( m_sDocumentURL ) )
     531             :             {
     532           0 :                 Reference< XChild > xAsChild( xDocument, UNO_QUERY );
     533           0 :                 while ( xAsChild.is() && !lcl_isValidDocumentURL( m_sDocumentURL ) )
     534             :                 {
     535           0 :                     xDocument.set( xAsChild->getParent(), UNO_QUERY );
     536           0 :                     if ( xDocument.is() )
     537           0 :                         m_sDocumentURL = xDocument->getURL();
     538           0 :                     xAsChild.set( xDocument, UNO_QUERY );
     539           0 :                 }
     540             :             }
     541           0 :         }
     542             :     }
     543           0 :     catch( const Exception& )
     544             :     {
     545             :         DBG_UNHANDLED_EXCEPTION();
     546             :     }
     547           0 : }
     548             : 
     549             : 
     550           0 : void OImageControlModel::onDisconnectedDbColumn()
     551             : {
     552           0 :     OBoundControlModel::onDisconnectedDbColumn();
     553             : 
     554           0 :     m_sDocumentURL = OUString();
     555           0 : }
     556             : 
     557             : 
     558           0 : Any OImageControlModel::translateDbColumnToControlValue()
     559             : {
     560           0 :     switch ( lcl_getImageStoreType( getFieldType() ) )
     561             :     {
     562             :     case ImageStoreBinary:
     563             :     {
     564           0 :         Reference< XInputStream > xImageStream( m_xColumn->getBinaryStream() );
     565           0 :         if ( m_xColumn->wasNull() )
     566           0 :             xImageStream.clear();
     567           0 :         return makeAny( xImageStream );
     568             :     }
     569             :     case ImageStoreLink:
     570             :     {
     571           0 :         OUString sImageLink( m_xColumn->getString() );
     572           0 :         if ( !m_sDocumentURL.isEmpty() )
     573           0 :             sImageLink = INetURLObject::GetAbsURL( m_sDocumentURL, sImageLink );
     574           0 :         return makeAny( sImageLink );
     575             :     }
     576             :     case ImageStoreInvalid:
     577             :         OSL_FAIL( "OImageControlModel::translateDbColumnToControlValue: invalid field type!" );
     578           0 :         break;
     579             :     }
     580           0 :     return Any();
     581             : }
     582             : 
     583             : 
     584           0 : Any OImageControlModel::getControlValue( ) const
     585             : {
     586           0 :     return makeAny( m_sImageURL );
     587             : }
     588             : 
     589             : 
     590           6 : void OImageControlModel::doSetControlValue( const Any& _rValue )
     591             : {
     592             :     DBG_ASSERT( GetImageProducer() && m_xImageProducer.is(), "OImageControlModel::doSetControlValue: no image producer!" );
     593           6 :     if ( !GetImageProducer() || !m_xImageProducer.is() )
     594           6 :         return;
     595             : 
     596           6 :     bool bStartProduction = false;
     597           6 :     switch ( lcl_getImageStoreType( getFieldType() ) )
     598             :     {
     599             :     case ImageStoreBinary:
     600             :     {
     601             :         // give the image producer the stream
     602           6 :         Reference< XInputStream > xInStream;
     603           6 :         _rValue >>= xInStream;
     604           6 :         GetImageProducer()->setImage( xInStream );
     605           6 :         bStartProduction = true;
     606             :     }
     607           6 :     break;
     608             : 
     609             :     case ImageStoreLink:
     610             :     {
     611           0 :         OUString sImageURL;
     612           0 :         _rValue >>= sImageURL;
     613           0 :         GetImageProducer()->SetImage( sImageURL );
     614           0 :         bStartProduction = true;
     615             :     }
     616           0 :     break;
     617             : 
     618             :     case ImageStoreInvalid:
     619             :         OSL_FAIL( "OImageControlModel::doSetControlValue: invalid field type!" );
     620           0 :         break;
     621             : 
     622             :     }   // switch ( lcl_getImageStoreType( getFieldType() ) )
     623             : 
     624           6 :     if ( bStartProduction )
     625             :     {
     626             :         // start production
     627           6 :         Reference< XImageProducer > xProducer = m_xImageProducer;
     628             :         {
     629             :             // release our mutex once (it's acquired in the calling method!), as starting the image production may
     630             :             // result in the locking of the solar mutex (unfortunately the default implementation of our aggregate,
     631             :             // VCLXImageControl, does this locking)
     632           6 :             MutexRelease aRelease(m_aMutex);
     633           6 :             xProducer->startProduction();
     634           6 :         }
     635             :     }
     636             : }
     637             : 
     638             : // OComponentHelper
     639             : 
     640          10 : void SAL_CALL OImageControlModel::disposing()
     641             : {
     642          10 :     OBoundControlModel::disposing();
     643          10 : }
     644             : 
     645             : 
     646           3 : void OImageControlModel::resetNoBroadcast()
     647             : {
     648           3 :     if ( hasField() )          // only reset when we are connected to a column
     649           0 :         OBoundControlModel::resetNoBroadcast( );
     650           3 : }
     651             : 
     652             : 
     653           1 : Reference< XImageProducer > SAL_CALL OImageControlModel::getImageProducer() throw ( RuntimeException, std::exception)
     654             : {
     655           1 :     return this;
     656             : }
     657             : 
     658             : 
     659           0 : void SAL_CALL OImageControlModel::addConsumer( const Reference< XImageConsumer >& _rxConsumer ) throw (RuntimeException, std::exception)
     660             : {
     661           0 :     GetImageProducer()->addConsumer( _rxConsumer );
     662           0 : }
     663             : 
     664             : 
     665           0 : void SAL_CALL OImageControlModel::removeConsumer( const Reference< XImageConsumer >& _rxConsumer ) throw (RuntimeException, std::exception)
     666             : {
     667           0 :     GetImageProducer()->removeConsumer( _rxConsumer );
     668           0 : }
     669             : 
     670             : 
     671           0 : void SAL_CALL OImageControlModel::startProduction(  ) throw (RuntimeException, std::exception)
     672             : {
     673           0 :     GetImageProducer()->startProduction();
     674           0 : }
     675             : 
     676             : 
     677          12 : IMPL_LINK( OImageControlModel, OnImageImportDone, ::Graphic*, i_pGraphic )
     678             : {
     679           6 :     const Reference< XGraphic > xGraphic( i_pGraphic != NULL ? Image( i_pGraphic->GetBitmapEx() ).GetXGraphic() : NULL );
     680           6 :     m_bExternalGraphic = false;
     681             :     try
     682             :     {
     683           6 :         setPropertyValue( PROPERTY_GRAPHIC, makeAny( xGraphic ) );
     684             :     }
     685           0 :     catch ( const Exception& )
     686             :     {
     687             :         DBG_UNHANDLED_EXCEPTION();
     688             :     }
     689           6 :     m_bExternalGraphic = true;
     690           6 :     return 1L;
     691             : }
     692             : 
     693             : 
     694             : // OImageControlControl
     695             : 
     696             : 
     697             : 
     698           1 : InterfaceRef SAL_CALL OImageControlControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
     699             : {
     700           1 :     return *(new OImageControlControl( comphelper::getComponentContext(_rxFactory) ));
     701             : }
     702             : 
     703             : 
     704           0 : Sequence<Type> OImageControlControl::_getTypes()
     705             : {
     706             :     return concatSequences(
     707             :         OBoundControl::_getTypes(),
     708             :         OImageControlControl_Base::getTypes()
     709           0 :     );
     710             : }
     711             : 
     712             : 
     713           1 : OImageControlControl::OImageControlControl(const Reference<XComponentContext>& _rxFactory)
     714             :     :OBoundControl(_rxFactory, VCL_CONTROL_IMAGECONTROL)
     715           1 :     ,m_aModifyListeners( m_aMutex )
     716             : {
     717           1 :     increment(m_refCount);
     718             :     {
     719             :         // Add as Focus- and MouseListener
     720           1 :         Reference< XWindow > xComp;
     721           1 :         query_aggregation( m_xAggregate, xComp );
     722           1 :         if ( xComp.is() )
     723           1 :             xComp->addMouseListener( this );
     724             :     }
     725           1 :     decrement(m_refCount);
     726           1 : }
     727             : 
     728             : 
     729          35 : Any SAL_CALL OImageControlControl::queryAggregation(const Type& _rType) throw (RuntimeException, std::exception)
     730             : {
     731          35 :     Any aReturn = OBoundControl::queryAggregation( _rType );
     732          35 :     if ( !aReturn.hasValue() )
     733           4 :         aReturn = ::cppu::queryInterface(
     734             :             _rType,
     735             :             static_cast< XMouseListener* >( this ),
     736             :             static_cast< XModifyBroadcaster* >( this )
     737           2 :         );
     738             : 
     739          35 :     return aReturn;
     740             : }
     741             : 
     742             : 
     743           0 : StringSequence  OImageControlControl::getSupportedServiceNames() throw(std::exception)
     744             : {
     745           0 :     StringSequence aSupported = OBoundControl::getSupportedServiceNames();
     746           0 :     aSupported.realloc(aSupported.getLength() + 1);
     747             : 
     748           0 :     OUString*pArray = aSupported.getArray();
     749           0 :     pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_IMAGECONTROL;
     750           0 :     return aSupported;
     751             : }
     752             : 
     753             : 
     754           0 : void SAL_CALL OImageControlControl::addModifyListener( const Reference< XModifyListener >& _Listener ) throw (RuntimeException, std::exception)
     755             : {
     756           0 :     m_aModifyListeners.addInterface( _Listener );
     757           0 : }
     758             : 
     759             : 
     760           0 : void SAL_CALL OImageControlControl::removeModifyListener( const Reference< XModifyListener >& _Listener ) throw (RuntimeException, std::exception)
     761             : {
     762           0 :     m_aModifyListeners.removeInterface( _Listener );
     763           0 : }
     764             : 
     765             : 
     766           1 : void SAL_CALL OImageControlControl::disposing()
     767             : {
     768           1 :     EventObject aEvent( *this );
     769           1 :     m_aModifyListeners.disposeAndClear( aEvent );
     770             : 
     771           1 :     OBoundControl::disposing();
     772           1 : }
     773             : 
     774             : 
     775           1 : void SAL_CALL OImageControlControl::disposing( const EventObject& _Event ) throw(RuntimeException, std::exception)
     776             : {
     777           1 :     OBoundControl::disposing( _Event );
     778           1 : }
     779             : 
     780             : 
     781           0 : void OImageControlControl::implClearGraphics( sal_Bool _bForce )
     782             : {
     783           0 :     Reference< XPropertySet > xSet( getModel(), UNO_QUERY );
     784           0 :     if ( xSet.is() )
     785             :     {
     786           0 :         if ( _bForce )
     787             :         {
     788           0 :             OUString sOldImageURL;
     789           0 :             xSet->getPropertyValue( PROPERTY_IMAGE_URL ) >>= sOldImageURL;
     790             : 
     791           0 :             if ( sOldImageURL.isEmpty() )
     792             :                 // the ImageURL is already empty, so simply setting a new empty one would not suffice
     793             :                 // (since it would be ignored)
     794           0 :                 xSet->setPropertyValue( PROPERTY_IMAGE_URL, makeAny( OUString( "private:emptyImage" ) ) );
     795             :                     // (the concrete URL we're passing here doens't matter. It's important that
     796             :                     // the model cannot resolve it to a a valid resource describing an image stream
     797             :         }
     798             : 
     799           0 :         xSet->setPropertyValue( PROPERTY_IMAGE_URL, makeAny( OUString() ) );
     800           0 :     }
     801           0 : }
     802             : 
     803             : 
     804           0 : bool OImageControlControl::implInsertGraphics()
     805             : {
     806           0 :     Reference< XPropertySet > xSet( getModel(), UNO_QUERY );
     807           0 :     if ( !xSet.is() )
     808           0 :         return false;
     809             : 
     810           0 :     OUString sTitle = FRM_RES_STRING(RID_STR_IMPORT_GRAPHIC);
     811             :     // build some arguments for the upcoming dialog
     812             :     try
     813             :     {
     814           0 :         ::sfx2::FileDialogHelper aDialog( TemplateDescription::FILEOPEN_LINK_PREVIEW, SFXWB_GRAPHIC );
     815           0 :         aDialog.SetTitle( sTitle );
     816             : 
     817           0 :         Reference< XFilePickerControlAccess > xController( aDialog.GetFilePicker(), UNO_QUERY_THROW );
     818           0 :         xController->setValue(ExtendedFilePickerElementIds::CHECKBOX_PREVIEW, 0, css::uno::Any(true));
     819             : 
     820           0 :         Reference<XPropertySet> xBoundField;
     821           0 :         if ( hasProperty( PROPERTY_BOUNDFIELD, xSet ) )
     822           0 :             xSet->getPropertyValue( PROPERTY_BOUNDFIELD ) >>= xBoundField;
     823           0 :         sal_Bool bHasField = xBoundField.is();
     824             : 
     825             :         // if the control is bound to a DB field, then it's not possible to decide whether or not to link
     826           0 :         xController->enableControl(ExtendedFilePickerElementIds::CHECKBOX_LINK, !bHasField );
     827             : 
     828             :         // if the control is bound to a DB field, then linking of the image depends on the type of the field
     829           0 :         sal_Bool bImageIsLinked = sal_True;
     830           0 :         if ( bHasField )
     831             :         {
     832           0 :             sal_Int32 nFieldType = DataType::OTHER;
     833           0 :             OSL_VERIFY( xBoundField->getPropertyValue( PROPERTY_FIELDTYPE ) >>= nFieldType );
     834           0 :             bImageIsLinked = ( lcl_getImageStoreType( nFieldType ) == ImageStoreLink );
     835             :         }
     836           0 :         xController->setValue(ExtendedFilePickerElementIds::CHECKBOX_LINK, 0, makeAny( bImageIsLinked ) );
     837             : 
     838           0 :         if ( ERRCODE_NONE == aDialog.Execute() )
     839             :         {
     840             :             // reset the url property in case it already has the value we're about to set - in this case
     841             :             // our propertyChanged would not get called without this.
     842           0 :             implClearGraphics( sal_False );
     843           0 :             sal_Bool bIsLink = sal_False;
     844           0 :             xController->getValue(ExtendedFilePickerElementIds::CHECKBOX_LINK, 0) >>= bIsLink;
     845             :             // Force bIsLink to be sal_True if we're bound to a field. Though we initialized the file picker with IsLink=TRUE
     846             :             // in this case, and disabled the respective control, there might be picker implementations which do not
     847             :             // respect this, and return IsLink=FALSE here. In this case, "normalize" the flag.
     848             :             // #i112659#
     849           0 :             bIsLink |= bHasField;
     850           0 :             if ( !bIsLink )
     851             :             {
     852           0 :                 Graphic aGraphic;
     853           0 :                 aDialog.GetGraphic( aGraphic );
     854           0 :                  xSet->setPropertyValue( PROPERTY_GRAPHIC, makeAny( aGraphic.GetXGraphic() ) );
     855             :             }
     856             :             else
     857           0 :                 xSet->setPropertyValue( PROPERTY_IMAGE_URL, makeAny( OUString( aDialog.GetPath() ) ) );
     858             : 
     859           0 :             return true;
     860           0 :         }
     861             :     }
     862           0 :     catch(const Exception&)
     863             :     {
     864             :         OSL_FAIL("OImageControlControl::implInsertGraphics: caught an exception while attempting to execute the FilePicker!");
     865             :     }
     866           0 :     return false;
     867             : }
     868             : 
     869             : 
     870           0 : bool OImageControlControl::impl_isEmptyGraphics_nothrow() const
     871             : {
     872           0 :     bool bIsEmpty = true;
     873             : 
     874             :     try
     875             :     {
     876           0 :         Reference< XPropertySet > xModelProps( const_cast< OImageControlControl* >( this )->getModel(), UNO_QUERY_THROW );
     877           0 :         Reference< XGraphic > xGraphic;
     878           0 :         OSL_VERIFY( xModelProps->getPropertyValue("Graphic") >>= xGraphic );
     879           0 :         bIsEmpty = !xGraphic.is();
     880             :     }
     881           0 :     catch( const Exception& )
     882             :     {
     883             :         DBG_UNHANDLED_EXCEPTION();
     884             :     }
     885             : 
     886           0 :     return bIsEmpty;
     887             : }
     888             : 
     889             : // MouseListener
     890             : 
     891           0 : void OImageControlControl::mousePressed(const ::com::sun::star::awt::MouseEvent& e) throw ( ::com::sun::star::uno::RuntimeException, std::exception)
     892             : {
     893           0 :     SolarMutexGuard aGuard;
     894             : 
     895           0 :     if (e.Buttons != MouseButton::LEFT)
     896           0 :         return;
     897             : 
     898           0 :     bool bModified = false;
     899             :     // is this a request for a context menu?
     900           0 :     if ( e.PopupTrigger )
     901             :     {
     902           0 :         Reference< XPopupMenu > xMenu( awt::PopupMenu::create( m_xContext ) );
     903             :         DBG_ASSERT( xMenu.is(), "OImageControlControl::mousePressed: could not create a popup menu!" );
     904             : 
     905           0 :         Reference< XWindowPeer > xWindowPeer = getPeer();
     906             :         DBG_ASSERT( xWindowPeer.is(), "OImageControlControl::mousePressed: no window!" );
     907             : 
     908           0 :         if ( xMenu.is() && xWindowPeer.is() )
     909             :         {
     910           0 :             xMenu->insertItem( ID_OPEN_GRAPHICS, FRM_RES_STRING( RID_STR_OPEN_GRAPHICS ), 0, 0 );
     911           0 :             xMenu->insertItem( ID_CLEAR_GRAPHICS, FRM_RES_STRING( RID_STR_CLEAR_GRAPHICS ), 0, 1 );
     912             : 
     913             :             // check if the ImageURL is empty
     914           0 :             if ( impl_isEmptyGraphics_nothrow() )
     915           0 :                 xMenu->enableItem( ID_CLEAR_GRAPHICS, sal_False );
     916             : 
     917           0 :             awt::Rectangle aRect( e.X, e.Y, 0, 0 );
     918           0 :             if ( ( e.X < 0 ) || ( e.Y < 0 ) )
     919             :             {   // context menu triggered by keyboard
     920             :                 // position it in the center of the control
     921           0 :                 Reference< XWindow > xWindow( static_cast< ::cppu::OWeakObject* >( this ), UNO_QUERY );
     922             :                 OSL_ENSURE( xWindow.is(), "OImageControlControl::mousePressed: me not a window? How this?" );
     923           0 :                 if ( xWindow.is() )
     924             :                 {
     925           0 :                     awt::Rectangle aPosSize = xWindow->getPosSize();
     926           0 :                     aRect.X = aPosSize.Width / 2;
     927           0 :                     aRect.Y = aPosSize.Height / 2;
     928           0 :                 }
     929             :             }
     930             : 
     931           0 :             const sal_Int16 nResult = xMenu->execute( xWindowPeer, aRect, PopupMenuDirection::EXECUTE_DEFAULT );
     932             : 
     933           0 :             switch ( nResult )
     934             :             {
     935             :             case ID_OPEN_GRAPHICS:
     936           0 :                 implInsertGraphics();
     937           0 :                 bModified = true;
     938           0 :                 break;
     939             : 
     940             :             case ID_CLEAR_GRAPHICS:
     941           0 :                 implClearGraphics( sal_True );
     942           0 :                 bModified = true;
     943           0 :                 break;
     944             :             }
     945           0 :         }
     946             :     }
     947             :     else
     948             :     {
     949             : 
     950             :         // Double click
     951           0 :         if (e.ClickCount == 2)
     952             :         {
     953             : 
     954           0 :             Reference<XPropertySet>  xSet(getModel(), UNO_QUERY);
     955           0 :             if (!xSet.is())
     956           0 :                 return;
     957             : 
     958             :             // If the Control is not bound, do not display a dialog (because the to-be-sent URL would be invalid anyway)
     959           0 :             Reference<XPropertySet> xBoundField;
     960           0 :             if (hasProperty(PROPERTY_BOUNDFIELD, xSet))
     961             :                 xBoundField.set(
     962           0 :                     xSet->getPropertyValue(PROPERTY_BOUNDFIELD),
     963           0 :                     css::uno::UNO_QUERY);
     964           0 :             if (!xBoundField.is())
     965             :             {
     966             :                 // but only if our IMAGE_URL property is handled as if it is transient, which is equivalent to
     967             :                 // an empty control source
     968           0 :                 if ( !hasProperty(PROPERTY_CONTROLSOURCE, xSet) || !::comphelper::getString(xSet->getPropertyValue(PROPERTY_CONTROLSOURCE)).isEmpty() )
     969           0 :                     return;
     970             :             }
     971             : 
     972           0 :             sal_Bool bReadOnly = false;
     973           0 :             xSet->getPropertyValue(PROPERTY_READONLY) >>= bReadOnly;
     974           0 :             if (bReadOnly)
     975           0 :                 return;
     976             : 
     977           0 :             if ( implInsertGraphics() )
     978           0 :                 bModified = true;
     979             :         }
     980             :     }
     981             : 
     982           0 :     if ( bModified )
     983             :     {
     984           0 :         EventObject aEvent( *this );
     985           0 :         m_aModifyListeners.notifyEach( &XModifyListener::modified, aEvent );
     986           0 :     }
     987             : }
     988             : 
     989             : 
     990           0 : void SAL_CALL OImageControlControl::mouseReleased(const awt::MouseEvent& /*e*/) throw ( RuntimeException, std::exception )
     991             : {
     992           0 : }
     993             : 
     994             : 
     995           0 : void SAL_CALL OImageControlControl::mouseEntered(const awt::MouseEvent& /*e*/) throw ( RuntimeException, std::exception )
     996             : {
     997           0 : }
     998             : 
     999             : 
    1000           0 : void SAL_CALL OImageControlControl::mouseExited(const awt::MouseEvent& /*e*/) throw ( RuntimeException, std::exception )
    1001             : {
    1002           0 : }
    1003             : 
    1004             : 
    1005             : }   // namespace frm
    1006             : 
    1007             : 
    1008             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10