1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: jobset.cxx,v $
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>
36 #include <rtl/alloc.h>
38 #include <vcl/jobset.hxx>
39 #include <vcl/jobset.h>
41 // =======================================================================
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];
57 struct Impl364JobSetupData
61 SVBT32 nDriverDataLen
;
69 // =======================================================================
71 ImplJobSetup::ImplJobSetup()
75 meOrientation
= ORIENTATION_PORTRAIT
;
77 mePaperFormat
= PAPER_USER
;
84 // -----------------------------------------------------------------------
86 ImplJobSetup::ImplJobSetup( const ImplJobSetup
& rJobSetup
) :
87 maPrinterName( rJobSetup
.maPrinterName
),
88 maDriver( rJobSetup
.maDriver
)
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
);
105 maValueMap
= rJobSetup
.maValueMap
;
108 // -----------------------------------------------------------------------
110 ImplJobSetup::~ImplJobSetup()
112 rtl_freeMemory( mpDriverData
);
115 // =======================================================================
117 ImplJobSetup
* JobSetup::ImplGetData()
120 mpData
= new ImplJobSetup
;
121 else if ( mpData
->mnRefCount
!= 1 )
123 mpData
->mnRefCount
--;
124 mpData
= new ImplJobSetup( *mpData
);
130 // -----------------------------------------------------------------------
132 ImplJobSetup
* JobSetup::ImplGetConstData()
135 mpData
= new ImplJobSetup
;
139 // -----------------------------------------------------------------------
141 const ImplJobSetup
* JobSetup::ImplGetConstData() const
144 ((JobSetup
*)this)->mpData
= new ImplJobSetup
;
148 // =======================================================================
152 DBG_CTOR( JobSetup
, 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
;
167 mpData
->mnRefCount
++;
170 // -----------------------------------------------------------------------
172 JobSetup::~JobSetup()
174 DBG_DTOR( JobSetup
, NULL
);
178 if ( mpData
->mnRefCount
== 1 )
181 mpData
->mnRefCount
--;
185 // -----------------------------------------------------------------------
187 XubString
JobSetup::GetPrinterName() const
190 return mpData
->maPrinterName
;
198 // -----------------------------------------------------------------------
200 XubString
JobSetup::GetDriverName() const
203 return mpData
->maDriver
;
211 // -----------------------------------------------------------------------
213 String
JobSetup::GetValue( const String
& rKey
) const
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();
224 // -----------------------------------------------------------------------
226 void JobSetup::SetValue( const String
& rKey
, const String
& rValue
)
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
250 if ( mpData
->mnRefCount
== 1 )
253 mpData
->mnRefCount
--;
256 mpData
= rJobSetup
.mpData
;
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
)
271 if ( !mpData
|| !rJobSetup
.mpData
)
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
)
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 )
304 sal_Size nFirstPos
= rIStream
.Tell();
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
;
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
)
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
);
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 )
387 if ( !rJobSetup
.mpData
)
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();
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
);
427 rOStream
.Seek( nPos
+ nLen
);