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 <tools/stream.hxx>
21 
22 #include <basic/sbx.hxx>
23 #include <basic/sberrors.hxx>
24 #include "sbxres.hxx"
25 
26 
27 static OUString pCount;
28 static OUString pAdd;
29 static OUString pItem;
30 static OUString pRemove;
31 static sal_uInt16 nCountHash = 0, nAddHash, nItemHash, nRemoveHash;
32 
33 
SbxCollection()34 SbxCollection::SbxCollection()
35              : SbxObject( "" )
36 {
37     if( !nCountHash )
38     {
39         pCount  = GetSbxRes( StringId::CountProp );
40         pAdd    = GetSbxRes( StringId::AddMeth );
41         pItem   = GetSbxRes( StringId::ItemMeth );
42         pRemove = GetSbxRes( StringId::RemoveMeth );
43         nCountHash  = MakeHashCode( pCount );
44         nAddHash    = MakeHashCode( pAdd );
45         nItemHash   = MakeHashCode( pItem );
46         nRemoveHash = MakeHashCode( pRemove );
47     }
48     Initialize();
49     // For Access on itself
50     StartListening(GetBroadcaster(), DuplicateHandling::Prevent);
51 }
52 
SbxCollection(const SbxCollection & rColl)53 SbxCollection::SbxCollection( const SbxCollection& rColl )
54     : SvRefBase( rColl ), SbxObject( rColl )
55 {}
56 
operator =(const SbxCollection & r)57 SbxCollection& SbxCollection::operator=( const SbxCollection& r )
58 {
59     if( &r != this )
60         SbxObject::operator=( r );
61     return *this;
62 }
63 
~SbxCollection()64 SbxCollection::~SbxCollection()
65 {}
66 
Clear()67 void SbxCollection::Clear()
68 {
69     SbxObject::Clear();
70     Initialize();
71 }
72 
Initialize()73 void SbxCollection::Initialize()
74 {
75     SetType( SbxOBJECT );
76     SetFlag( SbxFlagBits::Fixed );
77     ResetFlag( SbxFlagBits::Write );
78     SbxVariable* p;
79     p = Make( pCount , SbxClassType::Property, SbxINTEGER );
80     p->ResetFlag( SbxFlagBits::Write );
81     p->SetFlag( SbxFlagBits::DontStore );
82     p = Make( pAdd, SbxClassType::Method, SbxEMPTY );
83     p->SetFlag( SbxFlagBits::DontStore );
84     p = Make( pItem , SbxClassType::Method, SbxOBJECT );
85     p->SetFlag( SbxFlagBits::DontStore );
86     p = Make( pRemove, SbxClassType::Method, SbxEMPTY );
87     p->SetFlag( SbxFlagBits::DontStore );
88 }
89 
Find(const OUString & rName,SbxClassType t)90 SbxVariable* SbxCollection::Find( const OUString& rName, SbxClassType t )
91 {
92     if( GetParameters() )
93     {
94         SbxObject* pObj = static_cast<SbxObject*>(GetObject());
95         return pObj ? pObj->Find( rName, t ) : nullptr;
96     }
97     else
98     {
99         return SbxObject::Find( rName, t );
100     }
101 }
102 
Notify(SfxBroadcaster & rCst,const SfxHint & rHint)103 void SbxCollection::Notify( SfxBroadcaster& rCst, const SfxHint& rHint )
104 {
105     const SbxHint* p = dynamic_cast<const SbxHint*>(&rHint);
106     if( p )
107     {
108         const SfxHintId nId = p->GetId();
109         bool bRead  = ( nId == SfxHintId::BasicDataWanted );
110         bool bWrite = ( nId == SfxHintId::BasicDataChanged );
111         SbxVariable* pVar = p->GetVar();
112         SbxArray* pArg = pVar->GetParameters();
113         if( bRead || bWrite )
114         {
115             OUString aVarName( pVar->GetName() );
116             if( pVar == this )
117             {
118                 CollItem( pArg );
119             }
120             else if( pVar->GetHashCode() == nCountHash
121                   && aVarName.equalsIgnoreAsciiCase( pCount ) )
122             {
123                 pVar->PutLong( pObjs->Count() );
124             }
125             else if( pVar->GetHashCode() == nAddHash
126                   && aVarName.equalsIgnoreAsciiCase( pAdd ) )
127             {
128                 CollAdd( pArg );
129             }
130             else if( pVar->GetHashCode() == nItemHash
131                   && aVarName.equalsIgnoreAsciiCase( pItem ) )
132             {
133                 CollItem( pArg );
134             }
135             else if( pVar->GetHashCode() == nRemoveHash
136                   && aVarName.equalsIgnoreAsciiCase( pRemove ) )
137             {
138                 CollRemove( pArg );
139             }
140             else
141             {
142                 SbxObject::Notify( rCst, rHint );
143             }
144             return;
145         }
146     }
147     SbxObject::Notify( rCst, rHint );
148 }
149 
150 // Default: argument is object
151 
CollAdd(SbxArray * pPar_)152 void SbxCollection::CollAdd( SbxArray* pPar_ )
153 {
154     if( pPar_->Count() != 2 )
155     {
156         SetError( ERRCODE_BASIC_WRONG_ARGS );
157     }
158     else
159     {
160         SbxBase* pObj = pPar_->Get( 1 )->GetObject();
161         if( !pObj || dynamic_cast<const SbxObject*>(pObj) == nullptr )
162         {
163             SetError( ERRCODE_BASIC_BAD_ARGUMENT );
164         }
165         else
166         {
167             Insert( static_cast<SbxObject*>(pObj) );
168         }
169     }
170 }
171 
172 // Default: index from 1 or object name
173 
CollItem(SbxArray * pPar_)174 void SbxCollection::CollItem( SbxArray* pPar_ )
175 {
176     if( pPar_->Count() != 2 )
177     {
178         SetError( ERRCODE_BASIC_WRONG_ARGS );
179     }
180     else
181     {
182         SbxVariable* pRes = nullptr;
183         SbxVariable* p = pPar_->Get( 1 );
184         if( p->GetType() == SbxSTRING )
185         {
186             pRes = Find( p->GetOUString(), SbxClassType::Object );
187         }
188         else
189         {
190             short n = p->GetInteger();
191             if( n >= 1 && n <= static_cast<short>(pObjs->Count()) )
192             {
193                 pRes = pObjs->Get( static_cast<sal_uInt16>(n) - 1 );
194             }
195         }
196         if( !pRes )
197         {
198             SetError( ERRCODE_BASIC_BAD_INDEX );
199         }
200         pPar_->Get( 0 )->PutObject( pRes );
201     }
202 }
203 
204 // Default: index from 1
205 
CollRemove(SbxArray * pPar_)206 void SbxCollection::CollRemove( SbxArray* pPar_ )
207 {
208     if( pPar_->Count() != 2 )
209         SetError( ERRCODE_BASIC_WRONG_ARGS );
210     else
211     {
212         short n = pPar_->Get( 1 )->GetInteger();
213         if( n < 1 || n > static_cast<short>(pObjs->Count()) )
214             SetError( ERRCODE_BASIC_BAD_INDEX );
215         else
216             Remove( pObjs->Get( static_cast<sal_uInt16>(n) - 1 ) );
217     }
218 }
219 
LoadData(SvStream & rStrm,sal_uInt16 nVer)220 bool SbxCollection::LoadData( SvStream& rStrm, sal_uInt16 nVer )
221 {
222     bool bRes = SbxObject::LoadData( rStrm, nVer );
223     Initialize();
224     return bRes;
225 }
226 
227 
SbxStdCollection()228 SbxStdCollection::SbxStdCollection()
229                   : bAddRemoveOk( true )
230 {}
231 
SbxStdCollection(const SbxStdCollection & r)232 SbxStdCollection::SbxStdCollection( const SbxStdCollection& r )
233                   : SvRefBase( r ), SbxCollection( r ),
234                     aElemClass( r.aElemClass ), bAddRemoveOk( r.bAddRemoveOk )
235 {}
236 
operator =(const SbxStdCollection & r)237 SbxStdCollection& SbxStdCollection::operator=( const SbxStdCollection& r )
238 {
239     if( &r != this )
240     {
241         if( !r.aElemClass.equalsIgnoreAsciiCase( aElemClass ) )
242         {
243             SetError( ERRCODE_BASIC_CONVERSION );
244         }
245         else
246         {
247             SbxCollection::operator=( r );
248         }
249     }
250     return *this;
251 }
252 
~SbxStdCollection()253 SbxStdCollection::~SbxStdCollection()
254 {}
255 
256 // Default: Error, if wrong object
257 
Insert(SbxVariable * p)258 void SbxStdCollection::Insert( SbxVariable* p )
259 {
260     SbxObject* pObj = dynamic_cast<SbxObject*>( p );
261     if( pObj && !pObj->IsClass( aElemClass ) )
262         SetError( ERRCODE_BASIC_BAD_ACTION );
263     else
264         SbxCollection::Insert( p );
265 }
266 
CollAdd(SbxArray * pPar_)267 void SbxStdCollection::CollAdd( SbxArray* pPar_ )
268 {
269     if( !bAddRemoveOk )
270         SetError( ERRCODE_BASIC_BAD_ACTION );
271     else
272         SbxCollection::CollAdd( pPar_ );
273 }
274 
CollRemove(SbxArray * pPar_)275 void SbxStdCollection::CollRemove( SbxArray* pPar_ )
276 {
277     if( !bAddRemoveOk )
278         SetError( ERRCODE_BASIC_BAD_ACTION );
279     else
280         SbxCollection::CollRemove( pPar_ );
281 }
282 
LoadData(SvStream & rStrm,sal_uInt16 nVer)283 bool SbxStdCollection::LoadData( SvStream& rStrm, sal_uInt16 nVer )
284 {
285     bool bRes = SbxCollection::LoadData( rStrm, nVer );
286     if( bRes )
287     {
288         aElemClass = read_uInt16_lenPrefixed_uInt8s_ToOUString(rStrm,
289             RTL_TEXTENCODING_ASCII_US);
290         rStrm.ReadCharAsBool( bAddRemoveOk );
291     }
292     return bRes;
293 }
294 
StoreData(SvStream & rStrm) const295 bool SbxStdCollection::StoreData( SvStream& rStrm ) const
296 {
297     bool bRes = SbxCollection::StoreData( rStrm );
298     if( bRes )
299     {
300         write_uInt16_lenPrefixed_uInt8s_FromOUString(rStrm, aElemClass,
301             RTL_TEXTENCODING_ASCII_US);
302         rStrm.WriteBool( bAddRemoveOk );
303     }
304     return bRes;
305 }
306 
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
308