nss: upgrade to release 3.73
[LibreOffice.git] / basic / source / sbx / sbxbase.cxx
blob0e057540c8bc4b0898f36c89ea9b2e8e0a3f46ff
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <memory>
21 #include <tools/debug.hxx>
22 #include <tools/stream.hxx>
23 #include <vcl/svapp.hxx>
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>
32 #include <rtl/ustring.hxx>
33 #include <sal/log.hxx>
35 // AppData-Structure for SBX:
38 SbxAppData::SbxAppData()
39 : eErrCode(ERRCODE_NONE)
40 , eBasicFormaterLangType(LANGUAGE_DONTKNOW)
44 SbxAppData::~SbxAppData()
46 SolarMutexGuard g;
48 pBasicFormater.reset();
49 // basic manager repository must be destroyed before factories
50 mrImplRepository.clear();
53 SbxBase::SbxBase()
55 nFlags = SbxFlagBits::ReadWrite;
58 SbxBase::SbxBase( const SbxBase& r )
59 : SvRefBase( r )
61 nFlags = r.nFlags;
64 SbxBase::~SbxBase()
68 SbxBase& SbxBase::operator=( const SbxBase& r )
70 nFlags = r.nFlags;
71 return *this;
74 SbxDataType SbxBase::GetType() const
76 return SbxEMPTY;
79 bool SbxBase::IsFixed() const
81 return IsSet( SbxFlagBits::Fixed );
84 void SbxBase::SetModified( bool b )
86 if( IsSet( SbxFlagBits::NoModify ) )
87 return;
88 if( b )
89 SetFlag( SbxFlagBits::Modified );
90 else
91 ResetFlag( SbxFlagBits::Modified );
94 ErrCode const & SbxBase::GetError()
96 return GetSbxData_Impl().eErrCode;
99 void SbxBase::SetError( ErrCode e )
101 SbxAppData& r = GetSbxData_Impl();
102 if( e && r.eErrCode == ERRCODE_NONE )
103 r.eErrCode = e;
106 bool SbxBase::IsError()
108 return GetSbxData_Impl().eErrCode != ERRCODE_NONE;
111 void SbxBase::ResetError()
113 GetSbxData_Impl().eErrCode = ERRCODE_NONE;
116 void SbxBase::AddFactory( SbxFactory* pFac )
118 GetSbxData_Impl().m_Factories.emplace_back(pFac);
121 void SbxBase::RemoveFactory( SbxFactory const * pFac )
123 if (!IsSbxData_Impl())
124 return;
125 SbxAppData& r = GetSbxData_Impl();
126 auto it = std::find(r.m_Factories.begin(), r.m_Factories.end(), pFac);
127 if (it != r.m_Factories.end())
128 r.m_Factories.erase( it );
132 SbxBase* SbxBase::Create( sal_uInt16 nSbxId, sal_uInt32 nCreator )
134 // #91626: Hack to skip old Basic dialogs
135 // Problem: There does not exist a factory any more,
136 // so we have to create a dummy SbxVariable instead
137 if( nSbxId == 0x65 ) // Dialog Id
138 return new SbxVariable;
140 if( nCreator == SBXCR_SBX )
141 switch( nSbxId )
143 case SBXID_VALUE: return new SbxValue;
144 case SBXID_VARIABLE: return new SbxVariable;
145 case SBXID_ARRAY: return new SbxArray;
146 case SBXID_DIMARRAY: return new SbxDimArray;
147 case SBXID_OBJECT: return new SbxObject( "" );
148 case SBXID_COLLECTION: return new SbxCollection;
149 case SBXID_FIXCOLLECTION:
150 return new SbxStdCollection;
151 case SBXID_METHOD: return new SbxMethod( "", SbxEMPTY );
152 case SBXID_PROPERTY: return new SbxProperty( "", SbxEMPTY );
154 // Unknown type: go over the factories!
155 SbxAppData& r = GetSbxData_Impl();
156 SbxBase* pNew = nullptr;
157 for (auto const& rpFac : r.m_Factories)
159 pNew = rpFac->Create( nSbxId, nCreator );
160 if( pNew )
161 break;
163 SAL_WARN_IF(!pNew, "basic", "No factory for SBX ID " << nSbxId);
164 return pNew;
167 SbxObject* SbxBase::CreateObject( const OUString& rClass )
169 SbxAppData& r = GetSbxData_Impl();
170 SbxObject* pNew = nullptr;
171 for (auto const& rpFac : r.m_Factories)
173 pNew = rpFac->CreateObject( rClass );
174 if( pNew )
175 break;
177 SAL_WARN_IF(!pNew, "basic", "No factory for object class " << rClass);
178 return pNew;
181 SbxBase* SbxBase::Load( SvStream& rStrm )
183 sal_uInt16 nSbxId, nFlagsTmp, nVer;
184 sal_uInt32 nCreator, nSize;
185 rStrm.ReadUInt32( nCreator ).ReadUInt16( nSbxId ).ReadUInt16( nFlagsTmp ).ReadUInt16( nVer );
186 SbxFlagBits nFlags = static_cast<SbxFlagBits>(nFlagsTmp);
188 // Correcting a foolishness of mine:
189 if( nFlags & SbxFlagBits::Reserved )
190 nFlags = ( nFlags & ~SbxFlagBits::Reserved ) | SbxFlagBits::GlobalSearch;
192 sal_uInt64 nOldPos = rStrm.Tell();
193 rStrm.ReadUInt32( nSize );
194 SbxBase* p = Create( nSbxId, nCreator );
195 if( p )
197 p->nFlags = nFlags;
198 if( p->LoadData( rStrm, nVer ) )
200 sal_uInt64 const nNewPos = rStrm.Tell();
201 nOldPos += nSize;
202 DBG_ASSERT( nOldPos >= nNewPos, "SBX: Too much data loaded" );
203 if( nOldPos != nNewPos )
204 rStrm.Seek( nOldPos );
205 if( !p->LoadCompleted() )
207 // Deleting of the object
208 SbxBaseRef xDeleteRef( p );
209 p = nullptr;
212 else
214 rStrm.SetError( SVSTREAM_FILEFORMAT_ERROR );
215 // Deleting of the object
216 SbxBaseRef xDeleteRef( p );
217 p = nullptr;
220 else
221 rStrm.SetError( SVSTREAM_FILEFORMAT_ERROR );
222 return p;
225 bool SbxBase::Store( SvStream& rStrm )
227 if( ( nFlags & SbxFlagBits::DontStore ) == SbxFlagBits::NONE )
229 rStrm.WriteUInt32( SBXCR_SBX )
230 .WriteUInt16( GetSbxId() )
231 .WriteUInt16( static_cast<sal_uInt16>(GetFlags()) )
232 .WriteUInt16( GetVersion() );
233 sal_uInt64 const nOldPos = rStrm.Tell();
234 rStrm.WriteUInt32( 0 );
235 bool bRes = StoreData( rStrm );
236 sal_uInt64 const nNewPos = rStrm.Tell();
237 rStrm.Seek( nOldPos );
238 rStrm.WriteUInt32( nNewPos - nOldPos );
239 rStrm.Seek( nNewPos );
240 if( rStrm.GetError() != ERRCODE_NONE )
241 bRes = false;
242 if( bRes )
243 bRes = true;
244 return bRes;
246 else
247 return true;
250 bool SbxBase::LoadCompleted()
252 return true;
255 //////////////////////////////// SbxFactory
257 SbxFactory::~SbxFactory()
261 SbxBase* SbxFactory::Create( sal_uInt16, sal_uInt32 )
263 return nullptr;
266 SbxObject* SbxFactory::CreateObject( const OUString& )
268 return nullptr;
271 ///////////////////////////////// SbxInfo
273 SbxInfo::~SbxInfo()
276 void SbxInfo::AddParam(const OUString& rName, SbxDataType eType, SbxFlagBits nFlags)
278 m_Params.push_back(std::make_unique<SbxParamInfo>(rName, eType, nFlags));
281 const SbxParamInfo* SbxInfo::GetParam( sal_uInt16 n ) const
283 if (n < 1 || n > m_Params.size())
284 return nullptr;
285 else
286 return m_Params[n - 1].get();
289 void SbxInfo::LoadData( SvStream& rStrm, sal_uInt16 nVer )
291 m_Params.clear();
292 sal_uInt16 nParam;
293 aComment = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm,
294 RTL_TEXTENCODING_ASCII_US);
295 aHelpFile = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm,
296 RTL_TEXTENCODING_ASCII_US);
297 rStrm.ReadUInt32( nHelpId ).ReadUInt16( nParam );
298 while( nParam-- )
300 sal_uInt16 nType, nFlagsTmp;
301 sal_uInt32 nUserData = 0;
302 OUString aName = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm,
303 RTL_TEXTENCODING_ASCII_US);
304 rStrm.ReadUInt16( nType ).ReadUInt16( nFlagsTmp );
305 SbxFlagBits nFlags = static_cast<SbxFlagBits>(nFlagsTmp);
306 if( nVer > 1 )
307 rStrm.ReadUInt32( nUserData );
308 AddParam( aName, static_cast<SbxDataType>(nType), nFlags );
309 SbxParamInfo& p(*m_Params.back());
310 p.nUserData = nUserData;
314 void SbxInfo::StoreData( SvStream& rStrm ) const
316 write_uInt16_lenPrefixed_uInt8s_FromOUString(rStrm, aComment,
317 RTL_TEXTENCODING_ASCII_US );
318 write_uInt16_lenPrefixed_uInt8s_FromOUString(rStrm, aHelpFile,
319 RTL_TEXTENCODING_ASCII_US);
320 rStrm.WriteUInt32( nHelpId ).WriteUInt16( m_Params.size() );
321 for (auto const& i : m_Params)
323 write_uInt16_lenPrefixed_uInt8s_FromOUString(rStrm, i->aName,
324 RTL_TEXTENCODING_ASCII_US);
325 rStrm.WriteUInt16( i->eType )
326 .WriteUInt16( static_cast<sal_uInt16>(i->nFlags) )
327 .WriteUInt32( i->nUserData );
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */