Line data Source code
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 <sal/config.h>
21 :
22 : #include <sal/log.hxx>
23 : #include <tools/stream.hxx>
24 :
25 : #include <svl/macitem.hxx>
26 : #include <stringio.hxx>
27 :
28 9024 : TYPEINIT1_FACTORY(SvxMacroItem, SfxPoolItem, new SvxMacroItem(0));
29 :
30 :
31 :
32 1843 : SvxMacro::SvxMacro( const OUString &rMacName, const OUString &rLanguage)
33 : : aMacName( rMacName ), aLibName( rLanguage),
34 1843 : eType( EXTENDED_STYPE)
35 : {
36 1843 : if ( rLanguage == SVX_MACRO_LANGUAGE_STARBASIC )
37 0 : eType=STARBASIC;
38 1843 : else if ( rLanguage == SVX_MACRO_LANGUAGE_JAVASCRIPT )
39 0 : eType=JAVASCRIPT;
40 1843 : }
41 :
42 0 : OUString SvxMacro::GetLanguage()const
43 : {
44 0 : if(eType==STARBASIC)
45 : {
46 0 : return OUString(SVX_MACRO_LANGUAGE_STARBASIC);
47 : }
48 0 : else if(eType==JAVASCRIPT)
49 : {
50 0 : return OUString(SVX_MACRO_LANGUAGE_JAVASCRIPT);
51 : }
52 0 : else if(eType==EXTENDED_STYPE)
53 : {
54 0 : return OUString(SVX_MACRO_LANGUAGE_SF);
55 :
56 : }
57 0 : return aLibName;
58 : }
59 :
60 891 : SvxMacro& SvxMacro::operator=( const SvxMacro& rBase )
61 : {
62 891 : if( this != &rBase )
63 : {
64 891 : aMacName = rBase.aMacName;
65 891 : aLibName = rBase.aLibName;
66 891 : eType = rBase.eType;
67 : }
68 891 : return *this;
69 : }
70 :
71 :
72 :
73 1 : SvxMacroTableDtor& SvxMacroTableDtor::operator=( const SvxMacroTableDtor& rTbl )
74 : {
75 1 : aSvxMacroTable.clear();
76 1 : aSvxMacroTable.insert(rTbl.aSvxMacroTable.begin(), rTbl.aSvxMacroTable.end());
77 1 : return *this;
78 : }
79 :
80 53 : bool SvxMacroTableDtor::operator==( const SvxMacroTableDtor& rOther ) const
81 : {
82 : // Count different => odd in any case
83 53 : if ( aSvxMacroTable.size() != rOther.aSvxMacroTable.size() )
84 0 : return false;
85 :
86 : // Compare single ones; the sequence matters due to performance reasons
87 53 : SvxMacroTable::const_iterator it1 = aSvxMacroTable.begin();
88 53 : SvxMacroTable::const_iterator it2 = rOther.aSvxMacroTable.begin();
89 53 : for ( ; it1 != aSvxMacroTable.end(); ++it1, ++it2 )
90 : {
91 0 : const SvxMacro& rOwnMac = it1->second;
92 0 : const SvxMacro& rOtherMac = it2->second;
93 0 : if ( it1->first != it2->first ||
94 0 : rOwnMac.GetLibName() != rOtherMac.GetLibName() ||
95 0 : rOwnMac.GetMacName() != rOtherMac.GetMacName() )
96 0 : return false;
97 : }
98 :
99 53 : return true;
100 : }
101 :
102 0 : SvStream& SvxMacroTableDtor::Read( SvStream& rStrm, sal_uInt16 nVersion )
103 : {
104 0 : if( SVX_MACROTBL_VERSION40 <= nVersion )
105 0 : rStrm.ReadUInt16( nVersion );
106 :
107 0 : short nMacro(0);
108 0 : rStrm.ReadInt16(nMacro);
109 0 : if (nMacro < 0)
110 : {
111 : SAL_WARN("editeng", "Parsing error: negative value " << nMacro);
112 0 : return rStrm;
113 : }
114 :
115 0 : const size_t nMinStringSize = rStrm.GetStreamCharSet() == RTL_TEXTENCODING_UNICODE ? 4 : 2;
116 0 : size_t nMinRecordSize = 2 + 2*nMinStringSize;
117 0 : if( SVX_MACROTBL_VERSION40 <= nVersion )
118 0 : nMinRecordSize+=2;
119 :
120 0 : const size_t nMaxRecords = rStrm.remainingSize() / nMinRecordSize;
121 0 : if (static_cast<size_t>(nMacro) > nMaxRecords)
122 : {
123 : SAL_WARN("editeng", "Parsing error: " << nMaxRecords <<
124 : " max possible entries, but " << nMacro<< " claimed, truncating");
125 0 : nMacro = nMaxRecords;
126 : }
127 :
128 0 : for (short i = 0; i < nMacro; ++i)
129 : {
130 0 : sal_uInt16 nCurKey, eType = STARBASIC;
131 0 : OUString aLibName, aMacName;
132 0 : rStrm.ReadUInt16( nCurKey );
133 0 : aLibName = readByteString(rStrm);
134 0 : aMacName = readByteString(rStrm);
135 :
136 0 : if( SVX_MACROTBL_VERSION40 <= nVersion )
137 0 : rStrm.ReadUInt16( eType );
138 :
139 0 : aSvxMacroTable.insert( SvxMacroTable::value_type(nCurKey, SvxMacro( aMacName, aLibName, (ScriptType)eType ) ));
140 0 : }
141 0 : return rStrm;
142 : }
143 :
144 :
145 0 : SvStream& SvxMacroTableDtor::Write( SvStream& rStream ) const
146 : {
147 0 : sal_uInt16 nVersion = SOFFICE_FILEFORMAT_31 == rStream.GetVersion()
148 : ? SVX_MACROTBL_VERSION31
149 0 : : SVX_MACROTBL_AKTVERSION;
150 :
151 0 : if( SVX_MACROTBL_VERSION40 <= nVersion )
152 0 : rStream.WriteUInt16( nVersion );
153 :
154 0 : rStream.WriteUInt16( aSvxMacroTable.size() );
155 :
156 0 : SvxMacroTable::const_iterator it = aSvxMacroTable.begin();
157 0 : while( it != aSvxMacroTable.end() && rStream.GetError() == SVSTREAM_OK )
158 : {
159 0 : const SvxMacro& rMac = it->second;
160 0 : rStream.WriteUInt16( it->first );
161 0 : writeByteString(rStream, rMac.GetLibName());
162 0 : writeByteString(rStream, rMac.GetMacName());
163 :
164 0 : if( SVX_MACROTBL_VERSION40 <= nVersion )
165 0 : rStream.WriteUInt16( rMac.GetScriptType() );
166 0 : ++it;
167 : }
168 0 : return rStream;
169 : }
170 :
171 : // returns NULL if no entry exists, or a pointer to the internal value
172 0 : const SvxMacro* SvxMacroTableDtor::Get(sal_uInt16 nEvent) const
173 : {
174 0 : SvxMacroTable::const_iterator it = aSvxMacroTable.find(nEvent);
175 0 : return it == aSvxMacroTable.end() ? NULL : &(it->second);
176 : }
177 :
178 : // returns NULL if no entry exists, or a pointer to the internal value
179 0 : SvxMacro* SvxMacroTableDtor::Get(sal_uInt16 nEvent)
180 : {
181 0 : SvxMacroTable::iterator it = aSvxMacroTable.find(nEvent);
182 0 : return it == aSvxMacroTable.end() ? NULL : &(it->second);
183 : }
184 :
185 : // return true if the key exists
186 893 : bool SvxMacroTableDtor::IsKeyValid(sal_uInt16 nEvent) const
187 : {
188 893 : SvxMacroTable::const_iterator it = aSvxMacroTable.find(nEvent);
189 893 : return it != aSvxMacroTable.end();
190 : }
191 :
192 : // This stores a copy of the rMacro parameter
193 0 : SvxMacro& SvxMacroTableDtor::Insert(sal_uInt16 nEvent, const SvxMacro& rMacro)
194 : {
195 0 : return aSvxMacroTable.insert( SvxMacroTable::value_type( nEvent, rMacro ) ).first->second;
196 : }
197 :
198 : // If the entry exists, remove it from the map and release it's storage
199 0 : bool SvxMacroTableDtor::Erase(sal_uInt16 nEvent)
200 : {
201 0 : SvxMacroTable::iterator it = aSvxMacroTable.find(nEvent);
202 0 : if ( it != aSvxMacroTable.end())
203 : {
204 0 : aSvxMacroTable.erase(it);
205 0 : return true;
206 : }
207 0 : return false;
208 : }
209 :
210 :
211 :
212 53 : bool SvxMacroItem::operator==( const SfxPoolItem& rAttr ) const
213 : {
214 : DBG_ASSERT( SfxPoolItem::operator==(rAttr), "unequal types" );
215 :
216 53 : const SvxMacroTableDtor& rOwn = aMacroTable;
217 53 : const SvxMacroTableDtor& rOther = static_cast<const SvxMacroItem&>(rAttr).aMacroTable;
218 :
219 53 : return rOwn == rOther;
220 : }
221 :
222 :
223 :
224 0 : SfxPoolItem* SvxMacroItem::Clone( SfxItemPool* ) const
225 : {
226 0 : return new SvxMacroItem( *this );
227 : }
228 :
229 :
230 0 : bool SvxMacroItem::GetPresentation
231 : (
232 : SfxItemPresentation /*ePres*/,
233 : SfxMapUnit /*eCoreUnit*/,
234 : SfxMapUnit /*ePresUnit*/,
235 : OUString& rText,
236 : const IntlWrapper *
237 : ) const
238 : {
239 : /*!!!
240 : SvxMacroTableDtor& rTbl = (SvxMacroTableDtor&)GetMacroTable();
241 : SvxMacro* pMac = rTbl.First();
242 :
243 : while ( pMac )
244 : {
245 : rText += pMac->GetLibName();
246 : rText += cpDelim;
247 : rText += pMac->GetMacName();
248 : pMac = rTbl.Next();
249 : if ( pMac )
250 : rText += cpDelim;
251 : }
252 : */
253 0 : rText.clear();
254 0 : return false;
255 : }
256 :
257 :
258 :
259 0 : SvStream& SvxMacroItem::Store( SvStream& rStrm , sal_uInt16 ) const
260 : {
261 0 : return aMacroTable.Write( rStrm );
262 : }
263 :
264 :
265 :
266 0 : SfxPoolItem* SvxMacroItem::Create( SvStream& rStrm, sal_uInt16 nVersion ) const
267 : {
268 0 : SvxMacroItem* pAttr = new SvxMacroItem( Which() );
269 0 : pAttr->aMacroTable.Read( rStrm, nVersion );
270 0 : return pAttr;
271 : }
272 :
273 :
274 :
275 0 : void SvxMacroItem::SetMacro( sal_uInt16 nEvent, const SvxMacro& rMacro )
276 : {
277 0 : aMacroTable.Insert( nEvent, rMacro);
278 0 : }
279 :
280 :
281 :
282 0 : sal_uInt16 SvxMacroItem::GetVersion( sal_uInt16 nFileFormatVersion ) const
283 : {
284 : return SOFFICE_FILEFORMAT_31 == nFileFormatVersion
285 0 : ? 0 : SvxMacroTableDtor::GetVersion();
286 : }
287 :
288 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|