Files
Neil Roberts be27055b34 tdf#172532 basprov: Use new concrete interfaces for optional methods
Instead of using XInvocation and XPropertySet to expose the optional
edit method, the BASIC script provider now uses the new concrete
interface XEditableBrowseNode.

Change-Id: I7a99a1c335f408da9b0ac26c741577eb449e63c4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/207348
Reviewed-by: Neil Roberts <nroberts@libreoffice.org>
Tested-by: Jenkins
2026-07-15 09:08:17 +02:00

213 lines
6.6 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "basmethnode.hxx"
#include <com/sun/star/beans/PropertyAttribute.hpp>
#include <com/sun/star/frame/DispatchHelper.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/frame/XDispatchProvider.hpp>
#include <com/sun/star/script/browse/BrowseNodeTypes.hpp>
#include <comphelper/propertyvalue.hxx>
#include <utility>
#include <vcl/svapp.hxx>
#include <basic/sbstar.hxx>
#include <basic/sbmeth.hxx>
#include <basic/sbmod.hxx>
#include <util/MiscUtils.hxx>
using namespace ::com::sun::star;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::beans;
using namespace ::comphelper;
using namespace ::com::sun::star::script;
using namespace ::sf_misc;
#define BASPROV_PROPERTY_ID_URI 1
constexpr OUStringLiteral BASPROV_PROPERTY_URI = u"URI";
#define BASPROV_DEFAULT_ATTRIBS() PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT | PropertyAttribute::READONLY
namespace basprov
{
// BasicMethodNodeImpl
BasicMethodNodeImpl::BasicMethodNodeImpl( const Reference< XComponentContext >& rxContext,
OUString sScriptingContext, SbMethod* pMethod, bool isAppScript )
:
m_xContext( rxContext )
,m_sScriptingContext(std::move( sScriptingContext ))
,m_pMethod( pMethod )
,m_bIsAppScript( isAppScript )
{
if ( m_pMethod )
{
SbModule* pModule = m_pMethod->GetModule();
if ( pModule )
{
StarBASIC* pBasic = static_cast< StarBASIC* >( pModule->GetParent() );
if ( pBasic )
{
m_sURI = "vnd.sun.star.script:";
m_sURI += pBasic->GetName();
m_sURI += ".";
m_sURI += pModule->GetName();
m_sURI += ".";
m_sURI += m_pMethod->GetName();
m_sURI += "?language=Basic&location=";
if ( m_bIsAppScript )
m_sURI += "application";
else
m_sURI += "document";
}
}
}
registerProperty( BASPROV_PROPERTY_URI, BASPROV_PROPERTY_ID_URI, BASPROV_DEFAULT_ATTRIBS(), &m_sURI, cppu::UnoType<decltype(m_sURI)>::get() );
}
BasicMethodNodeImpl::~BasicMethodNodeImpl()
{
}
// XBrowseNode
OUString BasicMethodNodeImpl::getName( )
{
SolarMutexGuard aGuard;
OUString sMethodName;
if ( m_pMethod )
sMethodName = m_pMethod->GetName();
return sMethodName;
}
Sequence< Reference< browse::XBrowseNode > > BasicMethodNodeImpl::getChildNodes( )
{
return Sequence< Reference< browse::XBrowseNode > >();
}
sal_Bool BasicMethodNodeImpl::hasChildNodes( )
{
return false;
}
sal_Int16 BasicMethodNodeImpl::getType( )
{
return browse::BrowseNodeTypes::SCRIPT;
}
// OPropertyArrayUsageHelper
::cppu::IPropertyArrayHelper* BasicMethodNodeImpl::createArrayHelper( ) const
{
Sequence< Property > aProps;
describeProperties( aProps );
return new ::cppu::OPropertyArrayHelper( aProps );
}
sal_Bool BasicMethodNodeImpl::isEditableNode( ) { return true; }
sal_Bool BasicMethodNodeImpl::editNode( )
{
OUString sDocURL, sLibName, sModName;
sal_uInt16 nLine1 = 0;
if ( !m_bIsAppScript )
{
Reference< frame::XModel > xModel = MiscUtils::tDocUrlToModel( m_sScriptingContext );
if ( xModel.is() )
{
sDocURL = xModel->getURL();
if ( sDocURL.isEmpty() )
{
const Sequence < PropertyValue > aProps = xModel->getArgs();
// TODO: according to MBA the property 'Title' may change in future
const PropertyValue* pProp = std::find_if(aProps.begin(), aProps.end(),
[](const PropertyValue& rProp) { return rProp.Name == "Title"; });
if (pProp != aProps.end())
pProp->Value >>= sDocURL;
}
}
}
if ( m_pMethod )
{
sal_uInt16 nLine2;
m_pMethod->GetLineRange( nLine1, nLine2 );
SbModule* pModule = m_pMethod->GetModule();
if ( pModule )
{
sModName = pModule->GetName();
StarBASIC* pBasic = static_cast< StarBASIC* >( pModule->GetParent() );
if ( pBasic )
sLibName = pBasic->GetName();
}
}
if ( m_xContext.is() )
{
Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create( m_xContext );
Reference < frame::XDispatchProvider > xProv( xDesktop->getCurrentFrame(), UNO_QUERY );
if ( xProv.is() )
{
Reference< frame::XDispatchHelper > xHelper( frame::DispatchHelper::create( m_xContext ) );
Sequence < PropertyValue > aArgs{
comphelper::makePropertyValue(u"Document"_ustr, sDocURL),
comphelper::makePropertyValue(u"LibName"_ustr, sLibName),
comphelper::makePropertyValue(u"Name"_ustr, sModName),
comphelper::makePropertyValue(u"Type"_ustr, u"Module"_ustr),
comphelper::makePropertyValue(u"Line"_ustr, static_cast< sal_uInt32 >( nLine1 ))
};
xHelper->executeDispatch( xProv, u".uno:BasicIDEAppear"_ustr, OUString(), 0, aArgs );
}
}
return true;
}
} // namespace basprov
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */