OInterfaceContainerHelper3 needs to be thread-safe
[LibreOffice.git] / svl / source / items / macitem.cxx
blobb0750212e3dc60750682f791288e570bc322046f
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 <sal/config.h>
22 #include <o3tl/safeint.hxx>
23 #include <sal/log.hxx>
24 #include <comphelper/fileformat.h>
25 #include <tools/stream.hxx>
27 #include <svl/macitem.hxx>
28 #include <stringio.hxx>
29 #include <algorithm>
31 SvxMacro::SvxMacro( const OUString &rMacName, const OUString &rLanguage)
32 : aMacName( rMacName ), aLibName( rLanguage),
33 eType( EXTENDED_STYPE)
35 if ( rLanguage == SVX_MACRO_LANGUAGE_STARBASIC )
36 eType=STARBASIC;
37 else if ( rLanguage == SVX_MACRO_LANGUAGE_JAVASCRIPT )
38 eType=JAVASCRIPT;
41 OUString SvxMacro::GetLanguage()const
43 if(eType==STARBASIC)
45 return SVX_MACRO_LANGUAGE_STARBASIC;
47 else if(eType==JAVASCRIPT)
49 return SVX_MACRO_LANGUAGE_JAVASCRIPT;
51 else if(eType==EXTENDED_STYPE)
53 return SVX_MACRO_LANGUAGE_SF;
56 return aLibName;
59 SvxMacroTableDtor& SvxMacroTableDtor::operator=( const SvxMacroTableDtor& rTbl )
61 if (this != &rTbl)
63 aSvxMacroTable.clear();
64 aSvxMacroTable.insert(rTbl.aSvxMacroTable.begin(), rTbl.aSvxMacroTable.end());
66 return *this;
69 bool SvxMacroTableDtor::operator==( const SvxMacroTableDtor& rOther ) const
71 // Count different => odd in any case
72 // Compare single ones; the sequence matters due to performance reasons
73 return std::equal(aSvxMacroTable.begin(), aSvxMacroTable.end(),
74 rOther.aSvxMacroTable.begin(), rOther.aSvxMacroTable.end(),
75 [](const SvxMacroTable::value_type& rOwnEntry, const SvxMacroTable::value_type& rOtherEntry) {
76 const SvxMacro& rOwnMac = rOwnEntry.second;
77 const SvxMacro& rOtherMac = rOtherEntry.second;
78 return rOwnEntry.first == rOtherEntry.first
79 && rOwnMac.GetLibName() == rOtherMac.GetLibName()
80 && rOwnMac.GetMacName() == rOtherMac.GetMacName();
81 });
84 void SvxMacroTableDtor::Read( SvStream& rStrm )
86 sal_uInt16 nVersion;
87 rStrm.ReadUInt16( nVersion );
89 short nReadMacro(0);
90 rStrm.ReadInt16(nReadMacro);
91 if (nReadMacro < 0)
93 SAL_WARN("editeng", "Parsing error: negative value " << nReadMacro);
94 return;
97 auto nMacro = o3tl::make_unsigned(nReadMacro);
99 const size_t nMinStringSize = rStrm.GetStreamCharSet() == RTL_TEXTENCODING_UNICODE ? 4 : 2;
100 size_t nMinRecordSize = 2 + 2*nMinStringSize;
101 if( SVX_MACROTBL_VERSION40 <= nVersion )
102 nMinRecordSize+=2;
104 const size_t nMaxRecords = rStrm.remainingSize() / nMinRecordSize;
105 if (nMacro > nMaxRecords)
107 SAL_WARN("editeng", "Parsing error: " << nMaxRecords <<
108 " max possible entries, but " << nMacro<< " claimed, truncating");
109 nMacro = nMaxRecords;
112 for (decltype(nMacro) i = 0; i < nMacro; ++i)
114 sal_uInt16 nCurKey, eType = STARBASIC;
115 OUString aLibName, aMacName;
116 rStrm.ReadUInt16( nCurKey );
117 aLibName = readByteString(rStrm);
118 aMacName = readByteString(rStrm);
120 if( SVX_MACROTBL_VERSION40 <= nVersion )
121 rStrm.ReadUInt16( eType );
123 aSvxMacroTable.emplace( SvMacroItemId(nCurKey), SvxMacro( aMacName, aLibName, static_cast<ScriptType>(eType) ) );
127 SvStream& SvxMacroTableDtor::Write( SvStream& rStream ) const
129 sal_uInt16 nVersion = SOFFICE_FILEFORMAT_31 == rStream.GetVersion()
130 ? SVX_MACROTBL_VERSION31
131 : SVX_MACROTBL_VERSION40;
133 if( SVX_MACROTBL_VERSION40 <= nVersion )
134 rStream.WriteUInt16( nVersion );
136 rStream.WriteUInt16( aSvxMacroTable.size() );
138 for( const auto& rEntry : aSvxMacroTable )
140 if (rStream.GetError() != ERRCODE_NONE)
141 break;
142 const SvxMacro& rMac = rEntry.second;
143 rStream.WriteUInt16( static_cast<sal_uInt16>(rEntry.first) );
144 writeByteString(rStream, rMac.GetLibName());
145 writeByteString(rStream, rMac.GetMacName());
147 if( SVX_MACROTBL_VERSION40 <= nVersion )
148 rStream.WriteUInt16( rMac.GetScriptType() );
150 return rStream;
153 // returns NULL if no entry exists, or a pointer to the internal value
154 const SvxMacro* SvxMacroTableDtor::Get(SvMacroItemId nEvent) const
156 SvxMacroTable::const_iterator it = aSvxMacroTable.find(nEvent);
157 return it == aSvxMacroTable.end() ? nullptr : &(it->second);
160 // returns NULL if no entry exists, or a pointer to the internal value
161 SvxMacro* SvxMacroTableDtor::Get(SvMacroItemId nEvent)
163 SvxMacroTable::iterator it = aSvxMacroTable.find(nEvent);
164 return it == aSvxMacroTable.end() ? nullptr : &(it->second);
167 // return true if the key exists
168 bool SvxMacroTableDtor::IsKeyValid(SvMacroItemId nEvent) const
170 SvxMacroTable::const_iterator it = aSvxMacroTable.find(nEvent);
171 return it != aSvxMacroTable.end();
174 // This stores a copy of the rMacro parameter
175 SvxMacro& SvxMacroTableDtor::Insert(SvMacroItemId nEvent, const SvxMacro& rMacro)
177 return aSvxMacroTable.emplace( nEvent, rMacro ).first->second;
180 // If the entry exists, remove it from the map and release it's storage
181 void SvxMacroTableDtor::Erase(SvMacroItemId nEvent)
183 SvxMacroTable::iterator it = aSvxMacroTable.find(nEvent);
184 if ( it != aSvxMacroTable.end())
186 aSvxMacroTable.erase(it);
190 bool SvxMacroItem::operator==( const SfxPoolItem& rAttr ) const
192 assert(SfxPoolItem::operator==(rAttr));
194 const SvxMacroTableDtor& rOwn = aMacroTable;
195 const SvxMacroTableDtor& rOther = static_cast<const SvxMacroItem&>(rAttr).aMacroTable;
197 return rOwn == rOther;
200 SvxMacroItem* SvxMacroItem::Clone( SfxItemPool* ) const
202 return new SvxMacroItem( *this );
205 bool SvxMacroItem::GetPresentation
207 SfxItemPresentation /*ePres*/,
208 MapUnit /*eCoreUnit*/,
209 MapUnit /*ePresUnit*/,
210 OUString& rText,
211 const IntlWrapper&
212 ) const
214 /*!!!
215 SvxMacroTableDtor& rTbl = (SvxMacroTableDtor&)GetMacroTable();
216 SvxMacro* pMac = rTbl.First();
218 while ( pMac )
220 rText += pMac->GetLibName();
221 rText += cpDelim;
222 rText += pMac->GetMacName();
223 pMac = rTbl.Next();
224 if ( pMac )
225 rText += cpDelim;
228 rText.clear();
229 return false;
233 void SvxMacroItem::SetMacro( SvMacroItemId nEvent, const SvxMacro& rMacro )
235 aMacroTable.Insert( nEvent, rMacro);
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */