1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sal/log.hxx>
23 #include <comphelper/fileformat.h>
24 #include <tools/stream.hxx>
26 #include <svl/macitem.hxx>
27 #include <stringio.hxx>
29 SvxMacro::SvxMacro( const OUString
&rMacName
, const OUString
&rLanguage
)
30 : aMacName( rMacName
), aLibName( rLanguage
),
31 eType( EXTENDED_STYPE
)
33 if ( rLanguage
== SVX_MACRO_LANGUAGE_STARBASIC
)
35 else if ( rLanguage
== SVX_MACRO_LANGUAGE_JAVASCRIPT
)
39 OUString
SvxMacro::GetLanguage()const
43 return OUString(SVX_MACRO_LANGUAGE_STARBASIC
);
45 else if(eType
==JAVASCRIPT
)
47 return OUString(SVX_MACRO_LANGUAGE_JAVASCRIPT
);
49 else if(eType
==EXTENDED_STYPE
)
51 return OUString(SVX_MACRO_LANGUAGE_SF
);
57 SvxMacro
& SvxMacro::operator=( const SvxMacro
& rBase
)
61 aMacName
= rBase
.aMacName
;
62 aLibName
= rBase
.aLibName
;
69 SvxMacroTableDtor
& SvxMacroTableDtor::operator=( const SvxMacroTableDtor
& rTbl
)
73 aSvxMacroTable
.clear();
74 aSvxMacroTable
.insert(rTbl
.aSvxMacroTable
.begin(), rTbl
.aSvxMacroTable
.end());
79 bool SvxMacroTableDtor::operator==( const SvxMacroTableDtor
& rOther
) const
81 // Count different => odd in any case
82 if ( aSvxMacroTable
.size() != rOther
.aSvxMacroTable
.size() )
85 // Compare single ones; the sequence matters due to performance reasons
86 SvxMacroTable::const_iterator it1
= aSvxMacroTable
.begin();
87 SvxMacroTable::const_iterator it2
= rOther
.aSvxMacroTable
.begin();
88 for ( ; it1
!= aSvxMacroTable
.end(); ++it1
, ++it2
)
90 const SvxMacro
& rOwnMac
= it1
->second
;
91 const SvxMacro
& rOtherMac
= it2
->second
;
92 if ( it1
->first
!= it2
->first
||
93 rOwnMac
.GetLibName() != rOtherMac
.GetLibName() ||
94 rOwnMac
.GetMacName() != rOtherMac
.GetMacName() )
101 void SvxMacroTableDtor::Read( SvStream
& rStrm
)
104 rStrm
.ReadUInt16( nVersion
);
107 rStrm
.ReadInt16(nMacro
);
110 SAL_WARN("editeng", "Parsing error: negative value " << nMacro
);
114 const size_t nMinStringSize
= rStrm
.GetStreamCharSet() == RTL_TEXTENCODING_UNICODE
? 4 : 2;
115 size_t nMinRecordSize
= 2 + 2*nMinStringSize
;
116 if( SVX_MACROTBL_VERSION40
<= nVersion
)
119 const size_t nMaxRecords
= rStrm
.remainingSize() / nMinRecordSize
;
120 if (static_cast<size_t>(nMacro
) > nMaxRecords
)
122 SAL_WARN("editeng", "Parsing error: " << nMaxRecords
<<
123 " max possible entries, but " << nMacro
<< " claimed, truncating");
124 nMacro
= nMaxRecords
;
127 for (short i
= 0; i
< nMacro
; ++i
)
129 sal_uInt16 nCurKey
, eType
= STARBASIC
;
130 OUString aLibName
, aMacName
;
131 rStrm
.ReadUInt16( nCurKey
);
132 aLibName
= readByteString(rStrm
);
133 aMacName
= readByteString(rStrm
);
135 if( SVX_MACROTBL_VERSION40
<= nVersion
)
136 rStrm
.ReadUInt16( eType
);
138 aSvxMacroTable
.emplace( SvMacroItemId(nCurKey
), SvxMacro( aMacName
, aLibName
, static_cast<ScriptType
>(eType
) ) );
143 SvStream
& SvxMacroTableDtor::Write( SvStream
& rStream
) const
145 sal_uInt16 nVersion
= SOFFICE_FILEFORMAT_31
== rStream
.GetVersion()
146 ? SVX_MACROTBL_VERSION31
147 : SVX_MACROTBL_AKTVERSION
;
149 if( SVX_MACROTBL_VERSION40
<= nVersion
)
150 rStream
.WriteUInt16( nVersion
);
152 rStream
.WriteUInt16( aSvxMacroTable
.size() );
154 SvxMacroTable::const_iterator it
= aSvxMacroTable
.begin();
155 while( it
!= aSvxMacroTable
.end() && rStream
.GetError() == ERRCODE_NONE
)
157 const SvxMacro
& rMac
= it
->second
;
158 rStream
.WriteUInt16( static_cast<sal_uInt16
>(it
->first
) );
159 writeByteString(rStream
, rMac
.GetLibName());
160 writeByteString(rStream
, rMac
.GetMacName());
162 if( SVX_MACROTBL_VERSION40
<= nVersion
)
163 rStream
.WriteUInt16( rMac
.GetScriptType() );
169 // returns NULL if no entry exists, or a pointer to the internal value
170 const SvxMacro
* SvxMacroTableDtor::Get(SvMacroItemId nEvent
) const
172 SvxMacroTable::const_iterator it
= aSvxMacroTable
.find(nEvent
);
173 return it
== aSvxMacroTable
.end() ? nullptr : &(it
->second
);
176 // returns NULL if no entry exists, or a pointer to the internal value
177 SvxMacro
* SvxMacroTableDtor::Get(SvMacroItemId nEvent
)
179 SvxMacroTable::iterator it
= aSvxMacroTable
.find(nEvent
);
180 return it
== aSvxMacroTable
.end() ? nullptr : &(it
->second
);
183 // return true if the key exists
184 bool SvxMacroTableDtor::IsKeyValid(SvMacroItemId nEvent
) const
186 SvxMacroTable::const_iterator it
= aSvxMacroTable
.find(nEvent
);
187 return it
!= aSvxMacroTable
.end();
190 // This stores a copy of the rMacro parameter
191 SvxMacro
& SvxMacroTableDtor::Insert(SvMacroItemId nEvent
, const SvxMacro
& rMacro
)
193 return aSvxMacroTable
.emplace( nEvent
, rMacro
).first
->second
;
196 // If the entry exists, remove it from the map and release it's storage
197 void SvxMacroTableDtor::Erase(SvMacroItemId nEvent
)
199 SvxMacroTable::iterator it
= aSvxMacroTable
.find(nEvent
);
200 if ( it
!= aSvxMacroTable
.end())
202 aSvxMacroTable
.erase(it
);
207 bool SvxMacroItem::operator==( const SfxPoolItem
& rAttr
) const
209 assert(SfxPoolItem::operator==(rAttr
));
211 const SvxMacroTableDtor
& rOwn
= aMacroTable
;
212 const SvxMacroTableDtor
& rOther
= static_cast<const SvxMacroItem
&>(rAttr
).aMacroTable
;
214 return rOwn
== rOther
;
218 SfxPoolItem
* SvxMacroItem::Clone( SfxItemPool
* ) const
220 return new SvxMacroItem( *this );
224 bool SvxMacroItem::GetPresentation
226 SfxItemPresentation
/*ePres*/,
227 MapUnit
/*eCoreUnit*/,
228 MapUnit
/*ePresUnit*/,
234 SvxMacroTableDtor& rTbl = (SvxMacroTableDtor&)GetMacroTable();
235 SvxMacro* pMac = rTbl.First();
239 rText += pMac->GetLibName();
241 rText += pMac->GetMacName();
252 void SvxMacroItem::SetMacro( SvMacroItemId nEvent
, const SvxMacro
& rMacro
)
254 aMacroTable
.Insert( nEvent
, rMacro
);
258 sal_uInt16
SvxMacroItem::GetVersion( sal_uInt16 nFileFormatVersion
) const
260 return SOFFICE_FILEFORMAT_31
== nFileFormatVersion
261 ? 0 : SvxMacroTableDtor::GetVersion();
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */