| 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 <memory> |
| 21 |
#include <tools/debug.hxx> |
| 22 |
#include <tools/stream.hxx> |
| 23 |
#include <vcl/svapp.hxx> |
| 24 |
|
| 25 |
#include <basic/sbx.hxx> |
| 26 |
#include <sbxfac.hxx> |
| 27 |
#include <sbxform.hxx> |
| 28 |
#include <basic/sbxmeth.hxx> |
| 29 |
#include <sbxprop.hxx> |
| 30 |
#include <sbxbase.hxx> |
| 31 |
|
| 32 |
#include <rtl/ustring.hxx> |
| 33 |
#include <sal/log.hxx> |
| 34 |
#include <filefmt.hxx> |
| 35 |
|
| 36 |
// AppData-Structure for SBX: |
| 37 |
|
| 38 |
|
| 39 |
SbxAppData::SbxAppData() |
| 40 |
: eErrCode(ERRCODE_NONE) |
| 41 |
, aErrorMsg(OUString()) |
| 42 |
, eBasicFormaterLangType(LANGUAGE_DONTKNOW) |
| 43 |
{ |
| 44 |
} |
| 45 |
|
| 46 |
SbxAppData::~SbxAppData() |
| 47 |
{ |
| 48 |
SolarMutexGuard g; |
| 49 |
|
| 50 |
pBasicFormater.reset(); |
| 51 |
// basic manager repository must be destroyed before factories |
| 52 |
mrImplRepository.clear(); |
| 53 |
} |
| 54 |
|
| 55 |
SbxBase::SbxBase() |
| 56 |
{ |
| 57 |
nFlags = SbxFlagBits::ReadWrite; |
| 58 |
} |
| 59 |
|
| 60 |
SbxBase::SbxBase(const SbxBase& r) = default; |
| 61 |
|
| 62 |
SbxBase::~SbxBase() |
| 63 |
{ |
| 64 |
} |
| 65 |
|
| 66 |
SbxBase& SbxBase::operator=( const SbxBase& r ) |
| 67 |
{ |
| 68 |
nFlags = r.nFlags; |
| 69 |
return *this; |
| 70 |
} |
| 71 |
|
| 72 |
SbxDataType SbxBase::GetType() const |
| 73 |
{ |
| 74 |
return SbxEMPTY; |
| 75 |
} |
| 76 |
|
| 77 |
bool SbxBase::IsFixed() const |
| 78 |
{ |
| 79 |
return IsSet( SbxFlagBits::Fixed ); |
| 80 |
} |
| 81 |
|
| 82 |
void SbxBase::SetModified( bool b ) |
| 83 |
{ |
| 84 |
if( IsSet( SbxFlagBits::NoModify ) ) |
| 85 |
return; |
| 86 |
if( b ) |
| 87 |
SetFlag( SbxFlagBits::Modified ); |
| 88 |
else |
| 89 |
ResetFlag( SbxFlagBits::Modified ); |
| 90 |
} |
| 91 |
|
| 92 |
ErrCode const & SbxBase::GetError() |
| 93 |
{ |
| 94 |
return GetSbxData_Impl().eErrCode; |
| 95 |
} |
| 96 |
|
| 97 |
OUString const & SbxBase::GetErrorMsg() |
| 98 |
{ |
| 99 |
return GetSbxData_Impl().aErrorMsg; |
| 100 |
} |
| 101 |
|
| 102 |
void SbxBase::SetError(ErrCode e, const OUString& rMsg) |
| 103 |
{ |
| 104 |
SbxAppData& r = GetSbxData_Impl(); |
| 105 |
if (e && r.eErrCode == ERRCODE_NONE) |
| 106 |
{ |
| 107 |
r.eErrCode = e; |
| 108 |
r.aErrorMsg = rMsg; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
void SbxBase::SetError( ErrCode e ) |
| 113 |
{ |
| 114 |
SbxAppData& r = GetSbxData_Impl(); |
| 115 |
if( e && r.eErrCode == ERRCODE_NONE ) |
| 116 |
r.eErrCode = e; |
| 117 |
} |
| 118 |
|
| 119 |
bool SbxBase::IsError() |
| 120 |
{ |
| 121 |
return GetSbxData_Impl().eErrCode != ERRCODE_NONE; |
| 122 |
} |
| 123 |
|
| 124 |
void SbxBase::ResetError() |
| 125 |
{ |
| 126 |
GetSbxData_Impl().eErrCode = ERRCODE_NONE; |
| 127 |
GetSbxData_Impl().aErrorMsg = OUString(); |
| 128 |
} |
| 129 |
|
| 130 |
void SbxBase::AddFactory( SbxFactory* pFac ) |
| 131 |
{ |
| 132 |
GetSbxData_Impl().m_Factories.emplace_back(pFac); |
| 133 |
} |
| 134 |
|
| 135 |
void SbxBase::RemoveFactory( SbxFactory const * pFac ) |
| 136 |
{ |
| 137 |
if (!IsSbxData_Impl()) |
| 138 |
return; |
| 139 |
SbxAppData& r = GetSbxData_Impl(); |
| 140 |
auto it = std::find(r.m_Factories.begin(), r.m_Factories.end(), pFac); |
| 141 |
if (it != r.m_Factories.end()) |
| 142 |
r.m_Factories.erase( it ); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
SbxBaseRef SbxBase::Create( sal_uInt16 nSbxId, sal_uInt32 nCreator ) |
| 147 |
{ |
| 148 |
// #91626: Hack to skip old Basic dialogs |
| 149 |
// Problem: There does not exist a factory any more, |
| 150 |
// so we have to create a dummy SbxVariable instead |
| 151 |
if( nSbxId == 0x65 ) // Dialog Id |
| 152 |
return new SbxVariable; |
| 153 |
|
| 154 |
if( nCreator == SBXCR_SBX ) |
| 155 |
switch( nSbxId ) |
| 156 |
{ |
| 157 |
case SBXID_VALUE: return new SbxValue; |
| 158 |
case SBXID_VARIABLE: return new SbxVariable; |
| 159 |
case SBXID_ARRAY: return new SbxArray; |
| 160 |
case SBXID_DIMARRAY: return new SbxDimArray; |
| 161 |
case SBXID_OBJECT: return new SbxObject( u""_ustr ); |
| 162 |
case SBXID_COLLECTION: return new SbxCollection; |
| 163 |
case SBXID_FIXCOLLECTION: |
| 164 |
return new SbxStdCollection; |
| 165 |
case SBXID_METHOD: return new SbxMethod( u""_ustr, SbxEMPTY ); |
| 166 |
case SBXID_PROPERTY: return new SbxProperty( u""_ustr, SbxEMPTY ); |
| 167 |
} |
| 168 |
// Unknown type: go over the factories! |
| 169 |
SbxAppData& r = GetSbxData_Impl(); |
| 170 |
SbxBaseRef pNew; |
| 171 |
for (auto const& rpFac : r.m_Factories) |
| 172 |
{ |
| 173 |
pNew = rpFac->Create( nSbxId, nCreator ); |
| 174 |
if( pNew ) |
| 175 |
break; |
| 176 |
} |
| 177 |
SAL_WARN_IF(!pNew, "basic", "No factory for SBX ID " << nSbxId); |
| 178 |
return pNew; |
| 179 |
} |
| 180 |
|
| 181 |
SbxObjectRef SbxBase::CreateObject( const OUString& rClass ) |
| 182 |
{ |
| 183 |
SbxAppData& r = GetSbxData_Impl(); |
| 184 |
SbxObjectRef pNew; |
| 185 |
for (auto const& rpFac : r.m_Factories) |
| 186 |
{ |
| 187 |
pNew = rpFac->CreateObject( rClass ); |
| 188 |
if( pNew ) |
| 189 |
break; |
| 190 |
} |
| 191 |
SAL_WARN_IF(!pNew, "basic", "No factory for object class " << rClass); |
| 192 |
return pNew; |
| 193 |
} |
| 194 |
|
| 195 |
namespace { |
| 196 |
|
| 197 |
[[nodiscard]] SbxFlagBits CorrectFlags(SbxFlagBits nFlags) |
| 198 |
{ |
| 199 |
// Correcting a foolishness of mine: |
| 200 |
if (nFlags & SbxFlagBits::Reserved) |
| 201 |
nFlags |= SbxFlagBits::GlobalSearch; |
| 202 |
return nFlags & ~SbxFlagBits::Reserved; |
| 203 |
} |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
SbxBaseRef SbxBase::Load( SvStream& rStrm ) |
| 208 |
{ |
| 209 |
sal_uInt16 nSbxId(0), nFlagsTmp(0), nVer(0); |
| 210 |
sal_uInt32 nCreator(0), nSize(0); |
| 211 |
rStrm.ReadUInt32( nCreator ).ReadUInt16( nSbxId ).ReadUInt16( nFlagsTmp ).ReadUInt16( nVer ); |
| 212 |
SbxFlagBits nFlags = CorrectFlags(static_cast<SbxFlagBits>(nFlagsTmp)); |
| 213 |
|
| 214 |
sal_uInt64 nOldPos = rStrm.Tell(); |
| 215 |
rStrm.ReadUInt32( nSize ); |
| 216 |
SbxBaseRef p = Create( nSbxId, nCreator ); |
| 217 |
if( p ) |
| 218 |
{ |
| 219 |
p->nFlags = nFlags; |
| 220 |
if( p->LoadData( rStrm, nVer ) ) |
| 221 |
{ |
| 222 |
sal_uInt64 const nNewPos = rStrm.Tell(); |
| 223 |
nOldPos += nSize; |
| 224 |
DBG_ASSERT( nOldPos >= nNewPos, "SBX: Too much data loaded" ); |
| 225 |
if( nOldPos != nNewPos ) |
| 226 |
rStrm.Seek( nOldPos ); |
| 227 |
if( !p->LoadCompleted() ) |
| 228 |
{ |
| 229 |
// Deleting of the object |
| 230 |
SbxBaseRef xDeleteRef( p ); |
| 231 |
p = nullptr; |
| 232 |
} |
| 233 |
} |
| 234 |
else |
| 235 |
{ |
| 236 |
rStrm.SetError( SVSTREAM_FILEFORMAT_ERROR ); |
| 237 |
// Deleting of the object |
| 238 |
SbxBaseRef xDeleteRef( p ); |
| 239 |
p = nullptr; |
| 240 |
} |
| 241 |
} |
| 242 |
else |
| 243 |
rStrm.SetError( SVSTREAM_FILEFORMAT_ERROR ); |
| 244 |
return p; |
| 245 |
} |
| 246 |
|
| 247 |
std::pair<bool, sal_uInt32> SbxBase::Store( SvStream& rStrm ) |
| 248 |
{ |
| 249 |
if( ( nFlags & SbxFlagBits::DontStore ) == SbxFlagBits::NONE ) |
| 250 |
{ |
| 251 |
rStrm.WriteUInt32( SBXCR_SBX ) |
| 252 |
.WriteUInt16( GetSbxId() ) |
| 253 |
.WriteUInt16( static_cast<sal_uInt16>(GetFlags()) ) |
| 254 |
.WriteUInt16( GetVersion() ); |
| 255 |
sal_uInt64 const nOldPos = rStrm.Tell(); |
| 256 |
rStrm.WriteUInt32( 0 ); |
| 257 |
auto [bRes, nVersion] = StoreData(rStrm); |
| 258 |
sal_uInt64 const nNewPos = rStrm.Tell(); |
| 259 |
rStrm.Seek( nOldPos ); |
| 260 |
rStrm.WriteUInt32( nNewPos - nOldPos ); |
| 261 |
rStrm.Seek( nNewPos ); |
| 262 |
if( rStrm.GetError() != ERRCODE_NONE ) |
| 263 |
bRes = false; |
| 264 |
if( bRes ) |
| 265 |
bRes = true; |
| 266 |
return { bRes, nVersion }; |
| 267 |
} |
| 268 |
else |
| 269 |
return { true, B_IMG_VERSION_12 }; |
| 270 |
} |
| 271 |
|
| 272 |
bool SbxBase::LoadCompleted() |
| 273 |
{ |
| 274 |
return true; |
| 275 |
} |
| 276 |
|
| 277 |
//////////////////////////////// SbxFactory |
| 278 |
|
| 279 |
SbxFactory::~SbxFactory() |
| 280 |
{ |
| 281 |
} |
| 282 |
|
| 283 |
SbxBaseRef SbxFactory::Create( sal_uInt16, sal_uInt32 ) |
| 284 |
{ |
| 285 |
return nullptr; |
| 286 |
} |
| 287 |
|
| 288 |
SbxObjectRef SbxFactory::CreateObject( const OUString& ) |
| 289 |
{ |
| 290 |
return nullptr; |
| 291 |
} |
| 292 |
|
| 293 |
///////////////////////////////// SbxInfo |
| 294 |
|
| 295 |
SbxInfo::~SbxInfo() |
| 296 |
{} |
| 297 |
|
| 298 |
void SbxInfo::AddParam(const OUString& rName, SbxDataType eType, SbxFlagBits nFlags) |
| 299 |
{ |
| 300 |
m_Params.push_back(std::make_unique<SbxParamInfo>(rName, eType, nFlags)); |
| 301 |
} |
| 302 |
|
| 303 |
const SbxParamInfo* SbxInfo::GetParam( sal_uInt16 n ) const |
| 304 |
{ |
| 305 |
if (n < 1 || n > m_Params.size()) |
| 306 |
return nullptr; |
| 307 |
else |
| 308 |
return m_Params[n - 1].get(); |
| 309 |
} |
| 310 |
|
| 311 |
void SbxInfo::LoadData( SvStream& rStrm, sal_uInt16 nVer ) |
| 312 |
{ |
| 313 |
m_Params.clear(); |
| 314 |
sal_uInt16 nParam; |
| 315 |
aComment = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm, |
| 316 |
RTL_TEXTENCODING_ASCII_US); |
| 317 |
aHelpFile = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm, |
| 318 |
RTL_TEXTENCODING_ASCII_US); |
| 319 |
rStrm.ReadUInt32( nHelpId ).ReadUInt16( nParam ); |
| 320 |
while( nParam-- ) |
| 321 |
{ |
| 322 |
sal_uInt16 nType(0), nFlagsTmp(0); |
| 323 |
sal_uInt32 nUserData = 0; |
| 324 |
OUString aName = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm, |
| 325 |
RTL_TEXTENCODING_ASCII_US); |
| 326 |
rStrm.ReadUInt16( nType ).ReadUInt16( nFlagsTmp ); |
| 327 |
SbxFlagBits nFlags = static_cast<SbxFlagBits>(nFlagsTmp); |
| 328 |
if( nVer > 1 ) |
| 329 |
rStrm.ReadUInt32( nUserData ); |
| 330 |
AddParam( aName, static_cast<SbxDataType>(nType), nFlags ); |
| 331 |
SbxParamInfo& p(*m_Params.back()); |
| 332 |
p.nUserData = nUserData; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
void SbxInfo::StoreData( SvStream& rStrm ) const |
| 337 |
{ |
| 338 |
write_uInt16_lenPrefixed_uInt8s_FromOUString(rStrm, aComment, |
| 339 |
RTL_TEXTENCODING_ASCII_US ); |
| 340 |
write_uInt16_lenPrefixed_uInt8s_FromOUString(rStrm, aHelpFile, |
| 341 |
RTL_TEXTENCODING_ASCII_US); |
| 342 |
rStrm.WriteUInt32( nHelpId ).WriteUInt16( m_Params.size() ); |
| 343 |
for (auto const& i : m_Params) |
| 344 |
{ |
| 345 |
write_uInt16_lenPrefixed_uInt8s_FromOUString(rStrm, i->aName, |
| 346 |
RTL_TEXTENCODING_ASCII_US); |
| 347 |
rStrm.WriteUInt16( i->eType ) |
| 348 |
.WriteUInt16( static_cast<sal_uInt16>(i->nFlags) ) |
| 349 |
.WriteUInt32( i->nUserData ); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |