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 <tools/stream.hxx>
25 #include <svl/macitem.hxx>
26 #include <stringio.hxx>
28 TYPEINIT1_FACTORY(SvxMacroItem
, SfxPoolItem
, new SvxMacroItem(0));
32 SvxMacro::SvxMacro( const OUString
&rMacName
, const OUString
&rLanguage
)
33 : aMacName( rMacName
), aLibName( rLanguage
),
34 eType( EXTENDED_STYPE
)
36 if ( rLanguage
== SVX_MACRO_LANGUAGE_STARBASIC
)
38 else if ( rLanguage
== SVX_MACRO_LANGUAGE_JAVASCRIPT
)
42 OUString
SvxMacro::GetLanguage()const
46 return OUString(SVX_MACRO_LANGUAGE_STARBASIC
);
48 else if(eType
==JAVASCRIPT
)
50 return OUString(SVX_MACRO_LANGUAGE_JAVASCRIPT
);
52 else if(eType
==EXTENDED_STYPE
)
54 return OUString(SVX_MACRO_LANGUAGE_SF
);
60 SvxMacro
& SvxMacro::operator=( const SvxMacro
& rBase
)
64 aMacName
= rBase
.aMacName
;
65 aLibName
= rBase
.aLibName
;
73 SvxMacroTableDtor
& SvxMacroTableDtor::operator=( const SvxMacroTableDtor
& rTbl
)
75 aSvxMacroTable
.clear();
76 aSvxMacroTable
.insert(rTbl
.aSvxMacroTable
.begin(), rTbl
.aSvxMacroTable
.end());
80 bool SvxMacroTableDtor::operator==( const SvxMacroTableDtor
& rOther
) const
82 // Count different => odd in any case
83 if ( aSvxMacroTable
.size() != rOther
.aSvxMacroTable
.size() )
86 // Compare single ones; the sequence matters due to performance reasons
87 SvxMacroTable::const_iterator it1
= aSvxMacroTable
.begin();
88 SvxMacroTable::const_iterator it2
= rOther
.aSvxMacroTable
.begin();
89 for ( ; it1
!= aSvxMacroTable
.end(); ++it1
, ++it2
)
91 const SvxMacro
& rOwnMac
= it1
->second
;
92 const SvxMacro
& rOtherMac
= it2
->second
;
93 if ( it1
->first
!= it2
->first
||
94 rOwnMac
.GetLibName() != rOtherMac
.GetLibName() ||
95 rOwnMac
.GetMacName() != rOtherMac
.GetMacName() )
102 SvStream
& SvxMacroTableDtor::Read( SvStream
& rStrm
, sal_uInt16 nVersion
)
104 if( SVX_MACROTBL_VERSION40
<= nVersion
)
105 rStrm
.ReadUInt16( nVersion
);
108 rStrm
.ReadInt16(nMacro
);
111 SAL_WARN("editeng", "Parsing error: negative value " << nMacro
);
115 const size_t nMinStringSize
= rStrm
.GetStreamCharSet() == RTL_TEXTENCODING_UNICODE
? 4 : 2;
116 size_t nMinRecordSize
= 2 + 2*nMinStringSize
;
117 if( SVX_MACROTBL_VERSION40
<= nVersion
)
120 const size_t nMaxRecords
= rStrm
.remainingSize() / nMinRecordSize
;
121 if (static_cast<size_t>(nMacro
) > nMaxRecords
)
123 SAL_WARN("editeng", "Parsing error: " << nMaxRecords
<<
124 " max possible entries, but " << nMacro
<< " claimed, truncating");
125 nMacro
= nMaxRecords
;
128 for (short i
= 0; i
< nMacro
; ++i
)
130 sal_uInt16 nCurKey
, eType
= STARBASIC
;
131 OUString aLibName
, aMacName
;
132 rStrm
.ReadUInt16( nCurKey
);
133 aLibName
= readByteString(rStrm
);
134 aMacName
= readByteString(rStrm
);
136 if( SVX_MACROTBL_VERSION40
<= nVersion
)
137 rStrm
.ReadUInt16( eType
);
139 aSvxMacroTable
.insert( SvxMacroTable::value_type(nCurKey
, SvxMacro( aMacName
, aLibName
, (ScriptType
)eType
) ));
145 SvStream
& SvxMacroTableDtor::Write( SvStream
& rStream
) const
147 sal_uInt16 nVersion
= SOFFICE_FILEFORMAT_31
== rStream
.GetVersion()
148 ? SVX_MACROTBL_VERSION31
149 : SVX_MACROTBL_AKTVERSION
;
151 if( SVX_MACROTBL_VERSION40
<= nVersion
)
152 rStream
.WriteUInt16( nVersion
);
154 rStream
.WriteUInt16( aSvxMacroTable
.size() );
156 SvxMacroTable::const_iterator it
= aSvxMacroTable
.begin();
157 while( it
!= aSvxMacroTable
.end() && rStream
.GetError() == SVSTREAM_OK
)
159 const SvxMacro
& rMac
= it
->second
;
160 rStream
.WriteUInt16( it
->first
);
161 writeByteString(rStream
, rMac
.GetLibName());
162 writeByteString(rStream
, rMac
.GetMacName());
164 if( SVX_MACROTBL_VERSION40
<= nVersion
)
165 rStream
.WriteUInt16( rMac
.GetScriptType() );
171 // returns NULL if no entry exists, or a pointer to the internal value
172 const SvxMacro
* SvxMacroTableDtor::Get(sal_uInt16 nEvent
) const
174 SvxMacroTable::const_iterator it
= aSvxMacroTable
.find(nEvent
);
175 return it
== aSvxMacroTable
.end() ? NULL
: &(it
->second
);
178 // returns NULL if no entry exists, or a pointer to the internal value
179 SvxMacro
* SvxMacroTableDtor::Get(sal_uInt16 nEvent
)
181 SvxMacroTable::iterator it
= aSvxMacroTable
.find(nEvent
);
182 return it
== aSvxMacroTable
.end() ? NULL
: &(it
->second
);
185 // return true if the key exists
186 bool SvxMacroTableDtor::IsKeyValid(sal_uInt16 nEvent
) const
188 SvxMacroTable::const_iterator it
= aSvxMacroTable
.find(nEvent
);
189 return it
!= aSvxMacroTable
.end();
192 // This stores a copy of the rMacro parameter
193 SvxMacro
& SvxMacroTableDtor::Insert(sal_uInt16 nEvent
, const SvxMacro
& rMacro
)
195 return aSvxMacroTable
.insert( SvxMacroTable::value_type( nEvent
, rMacro
) ).first
->second
;
198 // If the entry exists, remove it from the map and release it's storage
199 bool SvxMacroTableDtor::Erase(sal_uInt16 nEvent
)
201 SvxMacroTable::iterator it
= aSvxMacroTable
.find(nEvent
);
202 if ( it
!= aSvxMacroTable
.end())
204 aSvxMacroTable
.erase(it
);
212 bool SvxMacroItem::operator==( const SfxPoolItem
& rAttr
) const
214 DBG_ASSERT( SfxPoolItem::operator==(rAttr
), "unequal types" );
216 const SvxMacroTableDtor
& rOwn
= aMacroTable
;
217 const SvxMacroTableDtor
& rOther
= static_cast<const SvxMacroItem
&>(rAttr
).aMacroTable
;
219 return rOwn
== rOther
;
224 SfxPoolItem
* SvxMacroItem::Clone( SfxItemPool
* ) const
226 return new SvxMacroItem( *this );
230 bool SvxMacroItem::GetPresentation
232 SfxItemPresentation
/*ePres*/,
233 SfxMapUnit
/*eCoreUnit*/,
234 SfxMapUnit
/*ePresUnit*/,
240 SvxMacroTableDtor& rTbl = (SvxMacroTableDtor&)GetMacroTable();
241 SvxMacro* pMac = rTbl.First();
245 rText += pMac->GetLibName();
247 rText += pMac->GetMacName();
259 SvStream
& SvxMacroItem::Store( SvStream
& rStrm
, sal_uInt16
) const
261 return aMacroTable
.Write( rStrm
);
266 SfxPoolItem
* SvxMacroItem::Create( SvStream
& rStrm
, sal_uInt16 nVersion
) const
268 SvxMacroItem
* pAttr
= new SvxMacroItem( Which() );
269 pAttr
->aMacroTable
.Read( rStrm
, nVersion
);
275 void SvxMacroItem::SetMacro( sal_uInt16 nEvent
, const SvxMacro
& rMacro
)
277 aMacroTable
.Insert( nEvent
, rMacro
);
282 sal_uInt16
SvxMacroItem::GetVersion( sal_uInt16 nFileFormatVersion
) const
284 return SOFFICE_FILEFORMAT_31
== nFileFormatVersion
285 ? 0 : SvxMacroTableDtor::GetVersion();
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */