| 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 |
|
| 21 |
#include <propacc.hxx> |
| 22 |
|
| 23 |
#include <basic/sberrors.hxx> |
| 24 |
#include <basic/sbstar.hxx> |
| 25 |
#include <basic/sbuno.hxx> |
| 26 |
#include <sbunoobj.hxx> |
| 27 |
|
| 28 |
#include <comphelper/propertysetinfo.hxx> |
| 29 |
#include <comphelper/sequence.hxx> |
| 30 |
#include <o3tl/any.hxx> |
| 31 |
|
| 32 |
#include <algorithm> |
| 33 |
|
| 34 |
using com::sun::star::uno::Reference; |
| 35 |
using namespace com::sun::star; |
| 36 |
using namespace com::sun::star::uno; |
| 37 |
using namespace com::sun::star::lang; |
| 38 |
using namespace com::sun::star::beans; |
| 39 |
using namespace cppu; |
| 40 |
|
| 41 |
static bool SbCompare_UString_PropertyValue_Impl(PropertyValue const & lhs, const OUString& rhs) |
| 42 |
{ |
| 43 |
return lhs.Name.compareTo(rhs) < 0; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
SbPropertyValues::SbPropertyValues() = default; |
| 48 |
|
| 49 |
|
| 50 |
SbPropertyValues::~SbPropertyValues() = default; |
| 51 |
|
| 52 |
Reference< XPropertySetInfo > SbPropertyValues::getPropertySetInfo() |
| 53 |
{ |
| 54 |
// create on demand? |
| 55 |
if (!m_xInfo.is()) |
| 56 |
{ |
| 57 |
assert(m_aPropInfos.empty()); |
| 58 |
for (auto const& it : m_aPropVals) |
| 59 |
m_aPropInfos.emplace_back(it.Name, it.Handle, cppu::UnoType<void>::get(), 0, 0); |
| 60 |
m_xInfo.set(new ::comphelper::PropertySetInfo(m_aPropInfos)); |
| 61 |
} |
| 62 |
return m_xInfo; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
size_t SbPropertyValues::GetIndex_Impl( const OUString &rPropName ) const |
| 67 |
{ |
| 68 |
SbPropertyValueArr_Impl::const_iterator it = std::lower_bound( |
| 69 |
m_aPropVals.begin(), m_aPropVals.end(), rPropName, |
| 70 |
SbCompare_UString_PropertyValue_Impl ); |
| 71 |
if (it == m_aPropVals.end() || it->Name != rPropName) |
| 72 |
{ |
| 73 |
throw beans::UnknownPropertyException( |
| 74 |
"Property not found: " + rPropName, |
| 75 |
const_cast<SbPropertyValues&>(*this)); |
| 76 |
} |
| 77 |
return it - m_aPropVals.begin(); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
void SbPropertyValues::setPropertyValue( |
| 82 |
const OUString& aPropertyName, |
| 83 |
const Any& aValue) |
| 84 |
{ |
| 85 |
size_t const nIndex = GetIndex_Impl( aPropertyName ); |
| 86 |
PropertyValue & rPropVal = m_aPropVals[nIndex]; |
| 87 |
rPropVal.Value = aValue; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
Any SbPropertyValues::getPropertyValue( |
| 92 |
const OUString& aPropertyName) |
| 93 |
{ |
| 94 |
size_t const nIndex = GetIndex_Impl( aPropertyName ); |
| 95 |
return m_aPropVals[nIndex].Value; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
void SbPropertyValues::addPropertyChangeListener( |
| 100 |
const OUString&, |
| 101 |
const Reference< XPropertyChangeListener >& ) |
| 102 |
{} |
| 103 |
|
| 104 |
|
| 105 |
void SbPropertyValues::removePropertyChangeListener( |
| 106 |
const OUString&, |
| 107 |
const Reference< XPropertyChangeListener >& ) |
| 108 |
{} |
| 109 |
|
| 110 |
|
| 111 |
void SbPropertyValues::addVetoableChangeListener( |
| 112 |
const OUString&, |
| 113 |
const Reference< XVetoableChangeListener >& ) |
| 114 |
{} |
| 115 |
|
| 116 |
|
| 117 |
void SbPropertyValues::removeVetoableChangeListener( |
| 118 |
const OUString&, |
| 119 |
const Reference< XVetoableChangeListener >& ) |
| 120 |
{} |
| 121 |
|
| 122 |
|
| 123 |
Sequence< PropertyValue > SbPropertyValues::getPropertyValues() |
| 124 |
{ |
| 125 |
return comphelper::containerToSequence(m_aPropVals); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
void SbPropertyValues::setPropertyValues(const Sequence< PropertyValue >& rPropertyValues ) |
| 130 |
{ |
| 131 |
if (!m_aPropVals.empty()) |
| 132 |
throw IllegalArgumentException(u"m_aPropVals not empty"_ustr, getXWeak(), -1); |
| 133 |
|
| 134 |
m_aPropVals.insert(m_aPropVals.end(), rPropertyValues.begin(), rPropertyValues.end()); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
void RTL_Impl_CreatePropertySet( SbxArray& rPar ) |
| 139 |
{ |
| 140 |
// We need at least one parameter |
| 141 |
// TODO: In this case < 2 is not correct ;-) |
| 142 |
if (rPar.Count() < 2) |
| 143 |
{ |
| 144 |
StarBASIC::Error( ERRCODE_BASIC_BAD_ARGUMENT ); |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
// Get class names of struct |
| 149 |
|
| 150 |
Reference xInterface(getXWeak(new SbPropertyValues())); |
| 151 |
|
| 152 |
SbxVariableRef refVar = rPar.Get(0); |
| 153 |
// Set PropertyValues |
| 154 |
Any aArgAsAny = sbxToUnoValue(rPar.Get(1), |
| 155 |
cppu::UnoType<Sequence<PropertyValue>>::get() ); |
| 156 |
auto pArg = o3tl::doAccess<Sequence<PropertyValue>>(aArgAsAny); |
| 157 |
Reference< XPropertyAccess > xPropAcc( xInterface, UNO_QUERY ); |
| 158 |
xPropAcc->setPropertyValues( *pArg ); |
| 159 |
|
| 160 |
// Build a SbUnoObject and return it |
| 161 |
auto xUnoObj = tools::make_ref<SbUnoObject>( "stardiv.uno.beans.PropertySet", Any(xInterface) ); |
| 162 |
if( xUnoObj->getUnoAny().hasValue() ) |
| 163 |
{ |
| 164 |
// Return object |
| 165 |
refVar->PutObject( xUnoObj.get() ); |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
// Object could not be created |
| 170 |
refVar->PutObject( nullptr ); |
| 171 |
} |
| 172 |
|
| 173 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |