diff options
author | Noel Power <npower@openoffice.org> | 2003-09-10 07:08:14 +0000 |
---|---|---|
committer | Noel Power <npower@openoffice.org> | 2003-09-10 07:08:14 +0000 |
commit | 4fe2c8f905fc115b1a0a057ac0f227d6a4cb68d2 (patch) | |
tree | 221aa7e1867ec1766b65e5f9194458f363e67664 /scripting/source/provider/ActiveMSPList.cxx | |
parent | de11ba9ee4366ed81c54e62382c020c62ad21532 (diff) |
#i12906#
Updates from code inspection
Diffstat (limited to 'scripting/source/provider/ActiveMSPList.cxx')
-rw-r--r-- | scripting/source/provider/ActiveMSPList.cxx | 269 |
1 files changed, 152 insertions, 117 deletions
diff --git a/scripting/source/provider/ActiveMSPList.cxx b/scripting/source/provider/ActiveMSPList.cxx index 467f39756a65..b7e4ae536b7a 100644 --- a/scripting/source/provider/ActiveMSPList.cxx +++ b/scripting/source/provider/ActiveMSPList.cxx @@ -2,9 +2,9 @@ * * $RCSfile: ActiveMSPList.cxx,v $ * - * $Revision: 1.1 $ + * $Revision: 1.2 $ * - * last change: $Author: npower $ $Date: 2003-09-04 07:23:28 $ + * last change: $Author: npower $ $Date: 2003-09-10 08:08:13 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -68,9 +68,9 @@ #include <com/sun/star/beans/XPropertySet.hpp> #include <com/sun/star/util/XMacroExpander.hpp> -#include <drafts/com/sun/star/script/framework/provider/XScriptProviderAccess.hpp> #include <drafts/com/sun/star/script/framework/browse/BrowseNodeTypes.hpp> +#include "MasterScriptProvider.hxx" #include "ActiveMSPList.hxx" using namespace com::sun::star; @@ -83,35 +83,23 @@ namespace func_provider class BrowseNodeImpl : public ::cppu::WeakImplHelper1< browse::XBrowseNode > { public: -BrowseNodeImpl( const Reference< provider::XScriptProvider >& msp, const ::rtl::OUString& location ): m_sNodeName( location ), m_xSP( msp ) +BrowseNodeImpl( const Reference< provider::XScriptProvider >& msp, const ::rtl::OUString& location ): m_xSP( msp ) { - // strip out the last leaf of location name - // e.g. file://dir1/dir2/Blah.sxw - > Blah.sxw - ::rtl::OUString temp = location; - sal_Int32 lastSlashIndex = temp.lastIndexOf( ::rtl::OUString::createFromAscii( "/" ) ); - - if ( ( lastSlashIndex + 1 ) < temp.getLength() ) - { - temp = temp.copy( lastSlashIndex + 1 ); - } - // maybe we should throw here!!! - else - { - OSL_TRACE("Something wrong with name, perhaps we should throw an exception"); - } - m_sNodeName = temp ; + m_sNodeName = parseLocationName( location ); } - virtual ::rtl::OUString + +virtual ::rtl::OUString SAL_CALL getName() throw ( RuntimeException ) { return m_sNodeName; } + virtual Sequence< Reference< browse::XBrowseNode > > SAL_CALL getChildNodes() throw ( RuntimeException ) { - Reference < provider::XScriptProviderAccess > providerGetter( m_xSP, UNO_QUERY ); + MasterScriptProvider* providerGetter = static_cast< MasterScriptProvider* >( m_xSP.get() ); Sequence< Reference< provider::XScriptProvider > > providers = providerGetter->getAllProviders(); Sequence< Reference< browse::XBrowseNode > > children( providers.getLength() ); for ( sal_Int32 index = 0; index < providers.getLength(); index++ ) @@ -133,10 +121,116 @@ virtual sal_Int16 SAL_CALL getType() { return browse::BrowseNodeTypes::CONTAINER; } -private: +protected: +::rtl::OUString parseLocationName( const ::rtl::OUString& location ) +{ + // strip out the last leaf of location name + // e.g. file://dir1/dir2/Blah.sxw - > Blah.sxw + ::rtl::OUString temp = location; + sal_Int32 lastSlashIndex = temp.lastIndexOf( ::rtl::OUString::createFromAscii( "/" ) ); + + if ( ( lastSlashIndex + 1 ) < temp.getLength() ) + { + temp = temp.copy( lastSlashIndex + 1 ); + } + // maybe we should throw here!!! + else + { + OSL_TRACE("Something wrong with name, perhaps we should throw an exception"); + } + return temp; +} + BrowseNodeImpl(){} ::rtl::OUString m_sNodeName; Reference < provider::XScriptProvider > m_xSP; }; + + +class DocBrowseNodeImpl : public BrowseNodeImpl +{ +public: +DocBrowseNodeImpl( const Reference< provider::XScriptProvider >& msp, + const Reference< frame::XModel >& xModel ) : m_xModel( xModel ) + +{ + OSL_TRACE("DocBrowseNodeImpl() ctor"); + m_sNodeName = parseLocationName( getDocNameOrURLFromModel( m_xModel ) ); + m_xSP = msp; +} + +virtual ::rtl::OUString SAL_CALL +getName() throw ( RuntimeException ) +{ + OSL_TRACE("DocBrowseNodeImpl::getName() have to change name"); + if ( m_xModel->getURL().getLength() > 0 ) + { + ::rtl::OUString docName = parseLocationName( m_xModel->getURL() ); + if ( !m_sNodeName.equals( docName ) ) + { + m_sNodeName = docName; + } + } + return m_sNodeName; +} + +private: + Reference< frame::XModel > m_xModel; +::rtl::OUString +getDocNameOrURLFromModel( const Reference< frame::XModel >& xModel ) +{ + // Set a default name, this should never be seen. + ::rtl::OUString docNameOrURL; + + docNameOrURL = ::rtl::OUString::createFromAscii("Unknown"); + if ( xModel.is() ) + { + if ( xModel->getURL().getLength() != 0) + { + docNameOrURL = xModel->getURL(); + OSL_TRACE("DocBrowseNodeImpl::getDocNameOrURLFromModel() url for document %s.", + ::rtl::OUStringToOString( docNameOrURL, + RTL_TEXTENCODING_ASCII_US ).pData->buffer ); + } + else + // Untitled document + { + ::rtl::OUString tempName; + try + { + Reference< beans::XPropertySet > propSet( xModel->getCurrentController()->getFrame(), UNO_QUERY ); + if ( propSet.is() ) + { + if ( sal_True == ( propSet->getPropertyValue(::rtl::OUString::createFromAscii( "Title" ) ) >>= tempName ) ) + { + // process "UntitledX - YYYYYYYY" + // to get UntitledX + sal_Int32 pos = 0; + docNameOrURL = tempName.getToken(0,' ',pos); + OSL_TRACE("DocBrowseNodeImpl::getDocNameOrURLFromModel() Title for document is %s.", + ::rtl::OUStringToOString( docNameOrURL, + RTL_TEXTENCODING_ASCII_US ).pData->buffer ); + } + } + else + { + OSL_TRACE("DocBrowseNodeImpl::getDocNameOrURLFromModel() doc model invalid" ); + } + } + catch ( Exception& e ) + { + OSL_TRACE("DocBrowseNodeImpl::getDocNameOrURLFromModel() exception thrown: ", + ::rtl::OUStringToOString( e.Message, + RTL_TEXTENCODING_ASCII_US ).pData->buffer ); + } + } + } + else + { + OSL_TRACE("DocBrowseNodeImpl::getDocNameOrURLFromModel() doc model is null" ); + } + return docNameOrURL; +} +}; ActiveMSPList::ActiveMSPList( const Reference< XComponentContext > & xContext ) : m_xContext( xContext ) { OSL_TRACE("ActiveMSPList::ActiveMSPList) - ctor"); @@ -168,45 +262,37 @@ void ActiveMSPList::addActiveMSP( const Reference< frame::XModel >& xModel, const Reference< dcsssf::provider::XScriptProvider >& msp ) { - // add self as listener for document dispose - // should probably throw from this method!!, reexamine - try - { - Reference< lang::XComponent > xComponent = - Reference< lang::XComponent >( xModel, UNO_QUERY_THROW ); - validateXRef( xComponent, "ActiveMSPList::addActiveMSP: model not XComponent\n" ); - xComponent->addEventListener( this ); - } - catch ( RuntimeException& e ) - { - OSL_TRACE("ActiveMSPList::addActiveMSP() failed to add self as listener: %s", - ::rtl::OUStringToOString( e.Message, - RTL_TEXTENCODING_ASCII_US ).pData->buffer ); - } - - ::rtl::OUString doc = getDocNameOrURLFromModel( xModel ); - - if ( doc.getLength() != 0 ) + ::osl::MutexGuard guard( m_mutex ); + Model_map::const_iterator itr = m_mModels.find( xModel ); + if ( itr == m_mModels.end() ) { - ::osl::MutexGuard guard( m_mutex ); - Model_map::const_iterator itr = m_mModels.find( xModel ); - if ( itr != m_mModels.end() ) + MspInst theMsp; + theMsp.provider = msp; + theMsp.node = new DocBrowseNodeImpl( msp, xModel ); + m_mModels[ xModel ] = theMsp; + + // add self as listener for document dispose + // should probably throw from this method!!, reexamine + try { - OSL_TRACE("ActiveMSPList::addActiveMSP() have MSP for model already" ); + Reference< lang::XComponent > xComponent = + Reference< lang::XComponent >( xModel, UNO_QUERY_THROW ); + validateXRef( xComponent, "ActiveMSPList::addActiveMSP: model not XComponent\n" ); + xComponent->addEventListener( this ); + } - else + catch ( RuntimeException& e ) { - MspInst theMsp; - theMsp.docName = doc; - theMsp.provider = msp; - m_mModels[ xModel ] = theMsp; + OSL_TRACE("ActiveMSPList::addActiveMSP() failed to add self as listener: %s", + ::rtl::OUStringToOString( e.Message, + RTL_TEXTENCODING_ASCII_US ).pData->buffer ); } - } + else { - OSL_TRACE("ActiveMSPList::addActiveMSP() couldn't get document name from model" ); + OSL_TRACE("ActiveMSPList::addActiveMSP() model for document exists already in map" ); } } @@ -254,63 +340,13 @@ throw ( ::com::sun::star::uno::RuntimeException ) } } -::rtl::OUString -ActiveMSPList::getDocNameOrURLFromModel( const Reference< frame::XModel >& xModel ) -{ - ::rtl::OUString docNameOrURL; - if ( xModel.is() ) - { - if ( xModel->getURL().getLength() != 0) - { - docNameOrURL = xModel->getURL(); - OSL_TRACE("ActiveMSPList::getDocNameOrURLFromModel() url for document %s.", - ::rtl::OUStringToOString( docNameOrURL, - RTL_TEXTENCODING_ASCII_US ).pData->buffer ); - } - else - // Untitled document - { - ::rtl::OUString tempName; - try - { - Reference< beans::XPropertySet > propSet( xModel->getCurrentController()->getFrame(), UNO_QUERY ); - if ( propSet.is() ) - { - if ( sal_True == ( propSet->getPropertyValue(::rtl::OUString::createFromAscii( "Title" ) ) >>= tempName ) ) - { - // process "UntitledX - YYYYYYYY" - // to get UntitledX - sal_Int32 pos = 0; - docNameOrURL = tempName.getToken(0,' ',pos); - OSL_TRACE("ActiveMSPList::getDocNameOrURLFromModel() Title for document is %s.", - ::rtl::OUStringToOString( docNameOrURL, - RTL_TEXTENCODING_ASCII_US ).pData->buffer ); - } - } - else - { - OSL_TRACE("ActiveMSPList::getDocNameOrURLFromModel() doc model invalid" ); - } - } - catch ( Exception& e ) - { - OSL_TRACE("ActiveMSPList::getDocNameOrURLFromModel() exception thrown: ", - ::rtl::OUStringToOString( e.Message, - RTL_TEXTENCODING_ASCII_US ).pData->buffer ); - } - } - } - else - { - OSL_TRACE("ActiveMSPList::getDocNameOrURLFromModel() doc model is null" ); - } - return docNameOrURL; -} ActiveMSPList& ActiveMSPList::instance( const Reference< XComponentContext > & xContext ) { static ActiveMSPList* inst = 0; + // need to not only hold a static pointer to this object but also + // keep it aqcuired static Reference< lang::XEventListener > holder; if ( !inst ) { @@ -346,12 +382,18 @@ ActiveMSPList::createNonDocMSPs() args[ 0 ] <<= userDirString; Reference< provider::XScriptProvider > userMsp( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( serviceName, args, m_xContext ), UNO_QUERY ); // should check if provider reference is valid - m_hMsps[ userDirString ] = userMsp; + MspInst userInstance; + userInstance.node = new BrowseNodeImpl( userMsp, userDirString ); + userInstance.provider = userMsp; + m_hMsps[ userDirString ] = userInstance; args[ 0 ] <<= shareDirString; Reference< provider::XScriptProvider > shareMsp( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( serviceName, args, m_xContext ), UNO_QUERY ); + MspInst shareInstance; + shareInstance.node = new BrowseNodeImpl( shareMsp, shareDirString ); + shareInstance.provider = shareMsp; // should check if provider reference is valid - m_hMsps[ shareDirString ] = shareMsp; + m_hMsps[ shareDirString ] = shareInstance; created = true; } @@ -382,10 +424,12 @@ ActiveMSPList::getChildNodes() Msp_hash::iterator h_itEnd = m_hMsps.end(); Sequence< Reference< browse::XBrowseNode > > children( numChildNodes ); sal_Int32 count = 0; + + for ( Msp_hash::iterator h_it = m_hMsps.begin(); h_it != h_itEnd; ++h_it ) { OSL_TRACE("Adding application browsenode index [ %d ]", count ); - children[ count++ ] = new BrowseNodeImpl( h_it->second, h_it->first ); + children[ count++ ] = h_it->second.node; } // get providers for active documents @@ -394,16 +438,7 @@ ActiveMSPList::getChildNodes() for ( Model_map::iterator m_it = m_mModels.begin(); m_it != m_itEnd; ++m_it ) { OSL_TRACE("Adding document browsenode index [ %d ]", count ); - ::rtl::OUString docName = m_it->second.docName; - if ( m_it->first->getURL().getLength() > 0 ) - { - if ( ! m_it->second.docName.equals( m_it->first->getURL() ) ) - { - OSL_TRACE("Need to change doc name" ); - m_it->second.docName = m_it->first->getURL(); - } - } - children[ count++ ] = new BrowseNodeImpl( m_it->second.provider, m_it->second.docName ); + children[ count++ ] = m_it->second.node; } return children; |