merge the formfield patch from ooo-build
[ooovba.git] / vcl / source / gdi / jobset.cxx
blob03a1d702f97bdb8c93dbec1c1550a1a821604dcd
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: jobset.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
33 #include <tools/debug.hxx>
34 #include <tools/stream.hxx>
35 #ifndef _RTL_ALLOC_H
36 #include <rtl/alloc.h>
37 #endif
38 #include <vcl/jobset.hxx>
39 #include <vcl/jobset.h>
41 // =======================================================================
43 DBG_NAME( JobSetup )
45 #define JOBSET_FILEFORMAT2 3780
46 #define JOBSET_FILE364_SYSTEM ((USHORT)0xFFFF)
47 #define JOBSET_FILE605_SYSTEM ((USHORT)0xFFFE)
49 struct ImplOldJobSetupData
51 char cPrinterName[64];
52 char cDeviceName[32];
53 char cPortName[32];
54 char cDriverName[32];
57 struct Impl364JobSetupData
59 SVBT16 nSize;
60 SVBT16 nSystem;
61 SVBT32 nDriverDataLen;
62 SVBT16 nOrientation;
63 SVBT16 nPaperBin;
64 SVBT16 nPaperFormat;
65 SVBT32 nPaperWidth;
66 SVBT32 nPaperHeight;
69 // =======================================================================
71 ImplJobSetup::ImplJobSetup()
73 mnRefCount = 1;
74 mnSystem = 0;
75 meOrientation = ORIENTATION_PORTRAIT;
76 mnPaperBin = 0;
77 mePaperFormat = PAPER_USER;
78 mnPaperWidth = 0;
79 mnPaperHeight = 0;
80 mnDriverDataLen = 0;
81 mpDriverData = NULL;
84 // -----------------------------------------------------------------------
86 ImplJobSetup::ImplJobSetup( const ImplJobSetup& rJobSetup ) :
87 maPrinterName( rJobSetup.maPrinterName ),
88 maDriver( rJobSetup.maDriver )
90 mnRefCount = 1;
91 mnSystem = rJobSetup.mnSystem;
92 meOrientation = rJobSetup.meOrientation;
93 mnPaperBin = rJobSetup.mnPaperBin;
94 mePaperFormat = rJobSetup.mePaperFormat;
95 mnPaperWidth = rJobSetup.mnPaperWidth;
96 mnPaperHeight = rJobSetup.mnPaperHeight;
97 mnDriverDataLen = rJobSetup.mnDriverDataLen;
98 if ( rJobSetup.mpDriverData )
100 mpDriverData = (BYTE*)rtl_allocateMemory( mnDriverDataLen );
101 memcpy( mpDriverData, rJobSetup.mpDriverData, mnDriverDataLen );
103 else
104 mpDriverData = NULL;
105 maValueMap = rJobSetup.maValueMap;
108 // -----------------------------------------------------------------------
110 ImplJobSetup::~ImplJobSetup()
112 rtl_freeMemory( mpDriverData );
115 // =======================================================================
117 ImplJobSetup* JobSetup::ImplGetData()
119 if ( !mpData )
120 mpData = new ImplJobSetup;
121 else if ( mpData->mnRefCount != 1 )
123 mpData->mnRefCount--;
124 mpData = new ImplJobSetup( *mpData );
127 return mpData;
130 // -----------------------------------------------------------------------
132 ImplJobSetup* JobSetup::ImplGetConstData()
134 if ( !mpData )
135 mpData = new ImplJobSetup;
136 return mpData;
139 // -----------------------------------------------------------------------
141 const ImplJobSetup* JobSetup::ImplGetConstData() const
143 if ( !mpData )
144 ((JobSetup*)this)->mpData = new ImplJobSetup;
145 return mpData;
148 // =======================================================================
150 JobSetup::JobSetup()
152 DBG_CTOR( JobSetup, NULL );
154 mpData = NULL;
157 // -----------------------------------------------------------------------
159 JobSetup::JobSetup( const JobSetup& rJobSetup )
161 DBG_CTOR( JobSetup, NULL );
162 DBG_CHKOBJ( &rJobSetup, JobSetup, NULL );
163 DBG_ASSERT( !rJobSetup.mpData || (rJobSetup.mpData->mnRefCount < 0xFFFE), "JobSetup: RefCount overflow" );
165 mpData = rJobSetup.mpData;
166 if ( mpData )
167 mpData->mnRefCount++;
170 // -----------------------------------------------------------------------
172 JobSetup::~JobSetup()
174 DBG_DTOR( JobSetup, NULL );
176 if ( mpData )
178 if ( mpData->mnRefCount == 1 )
179 delete mpData;
180 else
181 mpData->mnRefCount--;
185 // -----------------------------------------------------------------------
187 XubString JobSetup::GetPrinterName() const
189 if ( mpData )
190 return mpData->maPrinterName;
191 else
193 XubString aName;
194 return aName;
198 // -----------------------------------------------------------------------
200 XubString JobSetup::GetDriverName() const
202 if ( mpData )
203 return mpData->maDriver;
204 else
206 XubString aDriver;
207 return aDriver;
211 // -----------------------------------------------------------------------
213 String JobSetup::GetValue( const String& rKey ) const
215 if( mpData )
217 ::std::hash_map< ::rtl::OUString, ::rtl::OUString, ::rtl::OUStringHash >::const_iterator it;
218 it = mpData->maValueMap.find( rKey );
219 return it != mpData->maValueMap.end() ? String( it->second ) : String();
221 return String();
224 // -----------------------------------------------------------------------
226 void JobSetup::SetValue( const String& rKey, const String& rValue )
228 if( ! mpData )
229 mpData = new ImplJobSetup();
231 mpData->maValueMap[ rKey ] = rValue;
234 // -----------------------------------------------------------------------
236 JobSetup& JobSetup::operator=( const JobSetup& rJobSetup )
238 DBG_CHKTHIS( JobSetup, NULL );
239 DBG_CHKOBJ( &rJobSetup, JobSetup, NULL );
240 DBG_ASSERT( !rJobSetup.mpData || (rJobSetup.mpData->mnRefCount) < 0xFFFE, "JobSetup: RefCount overflow" );
242 // Zuerst Referenzcounter erhoehen, damit man sich selbst zuweisen kann
243 if ( rJobSetup.mpData )
244 rJobSetup.mpData->mnRefCount++;
246 // Wenn es keine statischen ImpDaten sind, dann loeschen, wenn es
247 // die letzte Referenz ist, sonst Referenzcounter decrementieren
248 if ( mpData )
250 if ( mpData->mnRefCount == 1 )
251 delete mpData;
252 else
253 mpData->mnRefCount--;
256 mpData = rJobSetup.mpData;
258 return *this;
261 // -----------------------------------------------------------------------
263 BOOL JobSetup::operator==( const JobSetup& rJobSetup ) const
265 DBG_CHKTHIS( JobSetup, NULL );
266 DBG_CHKOBJ( &rJobSetup, JobSetup, NULL );
268 if ( mpData == rJobSetup.mpData )
269 return TRUE;
271 if ( !mpData || !rJobSetup.mpData )
272 return FALSE;
274 ImplJobSetup* pData1 = mpData;
275 ImplJobSetup* pData2 = rJobSetup.mpData;
276 if ( (pData1->mnSystem == pData2->mnSystem) &&
277 (pData1->maPrinterName == pData2->maPrinterName) &&
278 (pData1->maDriver == pData2->maDriver) &&
279 (pData1->meOrientation == pData2->meOrientation) &&
280 (pData1->mnPaperBin == pData2->mnPaperBin) &&
281 (pData1->mePaperFormat == pData2->mePaperFormat) &&
282 (pData1->mnPaperWidth == pData2->mnPaperWidth) &&
283 (pData1->mnPaperHeight == pData2->mnPaperHeight) &&
284 (pData1->mnDriverDataLen == pData2->mnDriverDataLen) &&
285 (memcmp( pData1->mpDriverData, pData2->mpDriverData, pData1->mnDriverDataLen ) == 0) &&
286 (pData1->maValueMap == pData2->maValueMap)
288 return TRUE;
290 return FALSE;
293 // -----------------------------------------------------------------------
295 SvStream& operator>>( SvStream& rIStream, JobSetup& rJobSetup )
297 DBG_ASSERTWARNING( rIStream.GetVersion(), "JobSetup::>> - Solar-Version not set on rOStream" );
299 // Zur Zeit haben wir noch kein neues FileFormat
300 // if ( rIStream.GetVersion() < JOBSET_FILEFORMAT2 )
302 USHORT nLen;
303 USHORT nSystem;
304 sal_Size nFirstPos = rIStream.Tell();
305 rIStream >> nLen;
306 if ( !nLen )
307 return rIStream;
308 rIStream >> nSystem;
309 char* pTempBuf = new char[nLen];
310 rIStream.Read( pTempBuf, nLen - sizeof( nLen ) - sizeof( nSystem ) );
311 if ( nLen >= sizeof(ImplOldJobSetupData)+4 )
313 ImplOldJobSetupData* pData = (ImplOldJobSetupData*)pTempBuf;
314 if ( rJobSetup.mpData )
316 if ( rJobSetup.mpData->mnRefCount == 1 )
317 delete rJobSetup.mpData;
318 else
319 rJobSetup.mpData->mnRefCount--;
322 rtl_TextEncoding aStreamEncoding = RTL_TEXTENCODING_UTF8;
323 if( nSystem == JOBSET_FILE364_SYSTEM )
324 aStreamEncoding = rIStream.GetStreamCharSet();
326 rJobSetup.mpData = new ImplJobSetup;
327 ImplJobSetup* pJobData = rJobSetup.mpData;
328 pJobData->maPrinterName = UniString( pData->cPrinterName, aStreamEncoding );
329 pJobData->maDriver = UniString( pData->cDriverName, aStreamEncoding );
331 // Sind es unsere neuen JobSetup-Daten?
332 if ( nSystem == JOBSET_FILE364_SYSTEM ||
333 nSystem == JOBSET_FILE605_SYSTEM )
335 Impl364JobSetupData* pOldJobData = (Impl364JobSetupData*)(pTempBuf + sizeof( ImplOldJobSetupData ));
336 USHORT nOldJobDataSize = SVBT16ToShort( pOldJobData->nSize );
337 pJobData->mnSystem = SVBT16ToShort( pOldJobData->nSystem );
338 pJobData->mnDriverDataLen = SVBT32ToUInt32( pOldJobData->nDriverDataLen );
339 pJobData->meOrientation = (Orientation)SVBT16ToShort( pOldJobData->nOrientation );
340 pJobData->mnPaperBin = SVBT16ToShort( pOldJobData->nPaperBin );
341 pJobData->mePaperFormat = (Paper)SVBT16ToShort( pOldJobData->nPaperFormat );
342 pJobData->mnPaperWidth = (long)SVBT32ToUInt32( pOldJobData->nPaperWidth );
343 pJobData->mnPaperHeight = (long)SVBT32ToUInt32( pOldJobData->nPaperHeight );
344 if ( pJobData->mnDriverDataLen )
346 BYTE* pDriverData = ((BYTE*)pOldJobData) + nOldJobDataSize;
347 pJobData->mpDriverData = (BYTE*)rtl_allocateMemory( pJobData->mnDriverDataLen );
348 memcpy( pJobData->mpDriverData, pDriverData, pJobData->mnDriverDataLen );
350 if( nSystem == JOBSET_FILE605_SYSTEM )
352 rIStream.Seek( nFirstPos + sizeof( ImplOldJobSetupData ) + 4 + sizeof( Impl364JobSetupData ) + pJobData->mnDriverDataLen );
353 while( rIStream.Tell() < nFirstPos + nLen )
355 String aKey, aValue;
356 rIStream.ReadByteString( aKey, RTL_TEXTENCODING_UTF8 );
357 rIStream.ReadByteString( aValue, RTL_TEXTENCODING_UTF8 );
358 pJobData->maValueMap[ aKey ] = aValue;
360 DBG_ASSERT( rIStream.Tell() == nFirstPos+nLen, "corrupted job setup" );
361 // ensure correct stream position
362 rIStream.Seek( nFirstPos + nLen );
366 delete[] pTempBuf;
369 else
374 return rIStream;
377 // -----------------------------------------------------------------------
379 SvStream& operator<<( SvStream& rOStream, const JobSetup& rJobSetup )
381 DBG_ASSERTWARNING( rOStream.GetVersion(), "JobSetup::<< - Solar-Version not set on rOStream" );
383 // Zur Zeit haben wir noch kein neues FileFormat
384 // if ( rOStream.GetVersion() < JOBSET_FILEFORMAT2 )
386 USHORT nLen = 0;
387 if ( !rJobSetup.mpData )
388 rOStream << nLen;
389 else
391 USHORT nSystem = JOBSET_FILE605_SYSTEM;
393 const ImplJobSetup* pJobData = rJobSetup.ImplGetConstData();
394 Impl364JobSetupData aOldJobData;
395 USHORT nOldJobDataSize = sizeof( aOldJobData );
396 ShortToSVBT16( nOldJobDataSize, aOldJobData.nSize );
397 ShortToSVBT16( pJobData->mnSystem, aOldJobData.nSystem );
398 UInt32ToSVBT32( pJobData->mnDriverDataLen, aOldJobData.nDriverDataLen );
399 ShortToSVBT16( (USHORT)(pJobData->meOrientation), aOldJobData.nOrientation );
400 ShortToSVBT16( pJobData->mnPaperBin, aOldJobData.nPaperBin );
401 ShortToSVBT16( (USHORT)(pJobData->mePaperFormat), aOldJobData.nPaperFormat );
402 UInt32ToSVBT32( (ULONG)(pJobData->mnPaperWidth), aOldJobData.nPaperWidth );
403 UInt32ToSVBT32( (ULONG)(pJobData->mnPaperHeight), aOldJobData.nPaperHeight );
405 ImplOldJobSetupData aOldData;
406 memset( &aOldData, 0, sizeof( aOldData ) );
407 ByteString aPrnByteName( rJobSetup.GetPrinterName(), RTL_TEXTENCODING_UTF8 );
408 strncpy( aOldData.cPrinterName, aPrnByteName.GetBuffer(), 63 );
409 ByteString aDriverByteName( rJobSetup.GetDriverName(), RTL_TEXTENCODING_UTF8 );
410 strncpy( aOldData.cDriverName, aDriverByteName.GetBuffer(), 31 );
411 // nLen = sizeof( aOldData ) + 4 + nOldJobDataSize + pJobData->mnDriverDataLen;
412 int nPos = rOStream.Tell();
413 rOStream << nLen;
414 rOStream << nSystem;
415 rOStream.Write( (char*)&aOldData, sizeof( aOldData ) );
416 rOStream.Write( (char*)&aOldJobData, nOldJobDataSize );
417 rOStream.Write( (char*)pJobData->mpDriverData, pJobData->mnDriverDataLen );
418 ::std::hash_map< ::rtl::OUString, ::rtl::OUString, ::rtl::OUStringHash >::const_iterator it;
419 for( it = pJobData->maValueMap.begin(); it != pJobData->maValueMap.end(); ++it )
421 rOStream.WriteByteString( it->first, RTL_TEXTENCODING_UTF8 );
422 rOStream.WriteByteString( it->second, RTL_TEXTENCODING_UTF8 );
424 nLen = sal::static_int_cast<USHORT>(rOStream.Tell() - nPos);
425 rOStream.Seek( nPos );
426 rOStream << nLen;
427 rOStream.Seek( nPos + nLen );
431 else
436 return rOStream;