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 <rtl/alloc.h>
21 : #include <rtl/ustring.hxx>
22 : #include <tools/debug.hxx>
23 : #include <tools/stream.hxx>
24 : #include <vcl/jobset.hxx>
25 :
26 : #include <jobset.h>
27 : #include <boost/scoped_array.hpp>
28 :
29 : #define JOBSET_FILE364_SYSTEM ((sal_uInt16)0xFFFF)
30 : #define JOBSET_FILE605_SYSTEM ((sal_uInt16)0xFFFE)
31 :
32 : struct ImplOldJobSetupData
33 : {
34 : char cPrinterName[64];
35 : char cDeviceName[32];
36 : char cPortName[32];
37 : char cDriverName[32];
38 : };
39 :
40 : struct Impl364JobSetupData
41 : {
42 : SVBT16 nSize;
43 : SVBT16 nSystem;
44 : SVBT32 nDriverDataLen;
45 : SVBT16 nOrientation;
46 : SVBT16 nPaperBin;
47 : SVBT16 nPaperFormat;
48 : SVBT32 nPaperWidth;
49 : SVBT32 nPaperHeight;
50 : };
51 :
52 0 : ImplJobSetup::ImplJobSetup()
53 : {
54 0 : mnRefCount = 1;
55 0 : mnSystem = 0;
56 0 : meOrientation = ORIENTATION_PORTRAIT;
57 0 : meDuplexMode = DUPLEX_UNKNOWN;
58 0 : mnPaperBin = 0;
59 0 : mePaperFormat = PAPER_USER;
60 0 : mnPaperWidth = 0;
61 0 : mnPaperHeight = 0;
62 0 : mnDriverDataLen = 0;
63 0 : mpDriverData = NULL;
64 0 : }
65 :
66 0 : ImplJobSetup::ImplJobSetup( const ImplJobSetup& rJobSetup ) :
67 : maPrinterName( rJobSetup.maPrinterName ),
68 0 : maDriver( rJobSetup.maDriver )
69 : {
70 0 : mnRefCount = 1;
71 0 : mnSystem = rJobSetup.mnSystem;
72 0 : meOrientation = rJobSetup.meOrientation;
73 0 : meDuplexMode = rJobSetup.meDuplexMode;
74 0 : mnPaperBin = rJobSetup.mnPaperBin;
75 0 : mePaperFormat = rJobSetup.mePaperFormat;
76 0 : mnPaperWidth = rJobSetup.mnPaperWidth;
77 0 : mnPaperHeight = rJobSetup.mnPaperHeight;
78 0 : mnDriverDataLen = rJobSetup.mnDriverDataLen;
79 0 : if ( rJobSetup.mpDriverData )
80 : {
81 0 : mpDriverData = (sal_uInt8*)rtl_allocateMemory( mnDriverDataLen );
82 0 : memcpy( mpDriverData, rJobSetup.mpDriverData, mnDriverDataLen );
83 : }
84 : else
85 0 : mpDriverData = NULL;
86 0 : maValueMap = rJobSetup.maValueMap;
87 0 : }
88 :
89 0 : ImplJobSetup::~ImplJobSetup()
90 : {
91 0 : rtl_freeMemory( mpDriverData );
92 0 : }
93 :
94 0 : ImplJobSetup* JobSetup::ImplGetData()
95 : {
96 0 : if ( !mpData )
97 0 : mpData = new ImplJobSetup;
98 0 : else if ( mpData->mnRefCount != 1 )
99 : {
100 0 : mpData->mnRefCount--;
101 0 : mpData = new ImplJobSetup( *mpData );
102 : }
103 :
104 0 : return mpData;
105 : }
106 :
107 0 : ImplJobSetup* JobSetup::ImplGetConstData()
108 : {
109 0 : if ( !mpData )
110 0 : mpData = new ImplJobSetup;
111 0 : return mpData;
112 : }
113 :
114 0 : const ImplJobSetup* JobSetup::ImplGetConstData() const
115 : {
116 0 : if ( !mpData )
117 0 : ((JobSetup*)this)->mpData = new ImplJobSetup;
118 0 : return mpData;
119 : }
120 :
121 0 : JobSetup::JobSetup()
122 : {
123 :
124 0 : mpData = NULL;
125 0 : }
126 :
127 0 : JobSetup::JobSetup( const JobSetup& rJobSetup )
128 : {
129 : DBG_ASSERT( !rJobSetup.mpData || (rJobSetup.mpData->mnRefCount < 0xFFFE), "JobSetup: RefCount overflow" );
130 :
131 0 : mpData = rJobSetup.mpData;
132 0 : if ( mpData )
133 0 : mpData->mnRefCount++;
134 0 : }
135 :
136 0 : JobSetup::~JobSetup()
137 : {
138 :
139 0 : if ( mpData )
140 : {
141 0 : if ( mpData->mnRefCount == 1 )
142 0 : delete mpData;
143 : else
144 0 : mpData->mnRefCount--;
145 : }
146 0 : }
147 :
148 0 : OUString JobSetup::GetPrinterName() const
149 : {
150 0 : if ( mpData )
151 0 : return mpData->maPrinterName;
152 : else
153 0 : return OUString();
154 : }
155 :
156 0 : OUString JobSetup::GetDriverName() const
157 : {
158 0 : if ( mpData )
159 0 : return mpData->maDriver;
160 : else
161 0 : return OUString();
162 : }
163 :
164 0 : JobSetup& JobSetup::operator=( const JobSetup& rJobSetup )
165 : {
166 : DBG_ASSERT( !rJobSetup.mpData || (rJobSetup.mpData->mnRefCount) < 0xFFFE, "JobSetup: RefCount overflow" );
167 :
168 : // Increment refcount first, so that we can assign to ourselves
169 0 : if ( rJobSetup.mpData )
170 0 : rJobSetup.mpData->mnRefCount++;
171 :
172 : // If it's not static ImpData and the last reference, delete it, else
173 : // decrement refcount
174 0 : if ( mpData )
175 : {
176 0 : if ( mpData->mnRefCount == 1 )
177 0 : delete mpData;
178 : else
179 0 : mpData->mnRefCount--;
180 : }
181 :
182 0 : mpData = rJobSetup.mpData;
183 :
184 0 : return *this;
185 : }
186 :
187 0 : bool JobSetup::operator==( const JobSetup& rJobSetup ) const
188 : {
189 :
190 0 : if ( mpData == rJobSetup.mpData )
191 0 : return true;
192 :
193 0 : if ( !mpData || !rJobSetup.mpData )
194 0 : return false;
195 :
196 0 : ImplJobSetup* pData1 = mpData;
197 0 : ImplJobSetup* pData2 = rJobSetup.mpData;
198 0 : if ( (pData1->mnSystem == pData2->mnSystem) &&
199 0 : (pData1->maPrinterName == pData2->maPrinterName) &&
200 0 : (pData1->maDriver == pData2->maDriver) &&
201 0 : (pData1->meOrientation == pData2->meOrientation) &&
202 0 : (pData1->meDuplexMode == pData2->meDuplexMode) &&
203 0 : (pData1->mnPaperBin == pData2->mnPaperBin) &&
204 0 : (pData1->mePaperFormat == pData2->mePaperFormat) &&
205 0 : (pData1->mnPaperWidth == pData2->mnPaperWidth) &&
206 0 : (pData1->mnPaperHeight == pData2->mnPaperHeight) &&
207 0 : (pData1->mnDriverDataLen == pData2->mnDriverDataLen) &&
208 0 : (memcmp( pData1->mpDriverData, pData2->mpDriverData, pData1->mnDriverDataLen ) == 0) &&
209 0 : (pData1->maValueMap == pData2->maValueMap)
210 : )
211 0 : return true;
212 :
213 0 : return false;
214 : }
215 :
216 0 : SvStream& ReadJobSetup( SvStream& rIStream, JobSetup& rJobSetup )
217 : {
218 : DBG_ASSERTWARNING( rIStream.GetVersion(), "JobSetup::>> - Solar-Version not set on rOStream" );
219 :
220 : {
221 0 : sal_Size nFirstPos = rIStream.Tell();
222 :
223 0 : sal_uInt16 nLen = 0;
224 0 : rIStream.ReadUInt16( nLen );
225 0 : if ( !nLen )
226 0 : return rIStream;
227 :
228 0 : sal_uInt16 nSystem = 0;
229 0 : rIStream.ReadUInt16( nSystem );
230 :
231 0 : boost::scoped_array<char> pTempBuf(new char[nLen]);
232 0 : rIStream.Read( pTempBuf.get(), nLen - sizeof( nLen ) - sizeof( nSystem ) );
233 0 : if ( nLen >= sizeof(ImplOldJobSetupData)+4 )
234 : {
235 0 : ImplOldJobSetupData* pData = (ImplOldJobSetupData*)pTempBuf.get();
236 0 : if ( rJobSetup.mpData )
237 : {
238 0 : if ( rJobSetup.mpData->mnRefCount == 1 )
239 0 : delete rJobSetup.mpData;
240 : else
241 0 : rJobSetup.mpData->mnRefCount--;
242 : }
243 :
244 0 : rtl_TextEncoding aStreamEncoding = RTL_TEXTENCODING_UTF8;
245 0 : if( nSystem == JOBSET_FILE364_SYSTEM )
246 0 : aStreamEncoding = rIStream.GetStreamCharSet();
247 :
248 0 : rJobSetup.mpData = new ImplJobSetup;
249 0 : ImplJobSetup* pJobData = rJobSetup.mpData;
250 0 : pJobData->maPrinterName = OStringToOUString(pData->cPrinterName, aStreamEncoding);
251 0 : pJobData->maDriver = OStringToOUString(pData->cDriverName, aStreamEncoding);
252 :
253 : // Are these our new JobSetup files?
254 0 : if ( nSystem == JOBSET_FILE364_SYSTEM ||
255 0 : nSystem == JOBSET_FILE605_SYSTEM )
256 : {
257 0 : Impl364JobSetupData* pOldJobData = (Impl364JobSetupData*)(pTempBuf.get() + sizeof( ImplOldJobSetupData ));
258 0 : sal_uInt16 nOldJobDataSize = SVBT16ToShort( pOldJobData->nSize );
259 0 : pJobData->mnSystem = SVBT16ToShort( pOldJobData->nSystem );
260 0 : pJobData->mnDriverDataLen = SVBT32ToUInt32( pOldJobData->nDriverDataLen );
261 0 : pJobData->meOrientation = (Orientation)SVBT16ToShort( pOldJobData->nOrientation );
262 0 : pJobData->meDuplexMode = DUPLEX_UNKNOWN;
263 0 : pJobData->mnPaperBin = SVBT16ToShort( pOldJobData->nPaperBin );
264 0 : pJobData->mePaperFormat = (Paper)SVBT16ToShort( pOldJobData->nPaperFormat );
265 0 : pJobData->mnPaperWidth = (long)SVBT32ToUInt32( pOldJobData->nPaperWidth );
266 0 : pJobData->mnPaperHeight = (long)SVBT32ToUInt32( pOldJobData->nPaperHeight );
267 0 : if ( pJobData->mnDriverDataLen )
268 : {
269 0 : sal_uInt8* pDriverData = ((sal_uInt8*)pOldJobData) + nOldJobDataSize;
270 0 : pJobData->mpDriverData = (sal_uInt8*)rtl_allocateMemory( pJobData->mnDriverDataLen );
271 0 : memcpy( pJobData->mpDriverData, pDriverData, pJobData->mnDriverDataLen );
272 : }
273 0 : if( nSystem == JOBSET_FILE605_SYSTEM )
274 : {
275 0 : rIStream.Seek( nFirstPos + sizeof( ImplOldJobSetupData ) + 4 + sizeof( Impl364JobSetupData ) + pJobData->mnDriverDataLen );
276 0 : while( rIStream.Tell() < nFirstPos + nLen )
277 : {
278 0 : OUString aKey = read_uInt16_lenPrefixed_uInt8s_ToOUString(rIStream, RTL_TEXTENCODING_UTF8);
279 0 : OUString aValue = read_uInt16_lenPrefixed_uInt8s_ToOUString(rIStream, RTL_TEXTENCODING_UTF8);
280 0 : if( aKey.equalsAscii( "COMPAT_DUPLEX_MODE" ) )
281 : {
282 0 : if( aValue.equalsAscii( "DUPLEX_UNKNOWN" ) )
283 0 : pJobData->meDuplexMode = DUPLEX_UNKNOWN;
284 0 : else if( aValue.equalsAscii( "DUPLEX_OFF" ) )
285 0 : pJobData->meDuplexMode = DUPLEX_OFF;
286 0 : else if( aValue.equalsAscii( "DUPLEX_SHORTEDGE" ) )
287 0 : pJobData->meDuplexMode = DUPLEX_SHORTEDGE;
288 0 : else if( aValue.equalsAscii( "DUPLEX_LONGEDGE" ) )
289 0 : pJobData->meDuplexMode = DUPLEX_LONGEDGE;
290 : }
291 : else
292 0 : pJobData->maValueMap[ aKey ] = aValue;
293 0 : }
294 : DBG_ASSERT( rIStream.Tell() == nFirstPos+nLen, "corrupted job setup" );
295 : // ensure correct stream position
296 0 : rIStream.Seek( nFirstPos + nLen );
297 : }
298 : }
299 0 : }
300 : }
301 :
302 0 : return rIStream;
303 : }
304 :
305 0 : SvStream& WriteJobSetup( SvStream& rOStream, const JobSetup& rJobSetup )
306 : {
307 : DBG_ASSERTWARNING( rOStream.GetVersion(), "JobSetup::<< - Solar-Version not set on rOStream" );
308 :
309 : // We do not have a new FileFormat at this point in time
310 : // #define JOBSET_FILEFORMAT2 3780
311 : // if ( rOStream.GetVersion() < JOBSET_FILEFORMAT2 )
312 : {
313 0 : sal_uInt16 nLen = 0;
314 0 : if ( !rJobSetup.mpData )
315 0 : rOStream.WriteUInt16( nLen );
316 : else
317 : {
318 0 : sal_uInt16 nSystem = JOBSET_FILE605_SYSTEM;
319 :
320 0 : const ImplJobSetup* pJobData = rJobSetup.ImplGetConstData();
321 : Impl364JobSetupData aOldJobData;
322 0 : sal_uInt16 nOldJobDataSize = sizeof( aOldJobData );
323 0 : ShortToSVBT16( nOldJobDataSize, aOldJobData.nSize );
324 0 : ShortToSVBT16( pJobData->mnSystem, aOldJobData.nSystem );
325 0 : UInt32ToSVBT32( pJobData->mnDriverDataLen, aOldJobData.nDriverDataLen );
326 0 : ShortToSVBT16( (sal_uInt16)(pJobData->meOrientation), aOldJobData.nOrientation );
327 0 : ShortToSVBT16( pJobData->mnPaperBin, aOldJobData.nPaperBin );
328 0 : ShortToSVBT16( (sal_uInt16)(pJobData->mePaperFormat), aOldJobData.nPaperFormat );
329 0 : UInt32ToSVBT32( (sal_uLong)(pJobData->mnPaperWidth), aOldJobData.nPaperWidth );
330 0 : UInt32ToSVBT32( (sal_uLong)(pJobData->mnPaperHeight), aOldJobData.nPaperHeight );
331 :
332 : ImplOldJobSetupData aOldData;
333 0 : memset( &aOldData, 0, sizeof( aOldData ) );
334 0 : OString aPrnByteName(OUStringToOString(rJobSetup.GetPrinterName(), RTL_TEXTENCODING_UTF8));
335 0 : strncpy( aOldData.cPrinterName, aPrnByteName.getStr(), 63 );
336 0 : OString aDriverByteName(OUStringToOString(rJobSetup.GetDriverName(), RTL_TEXTENCODING_UTF8));
337 0 : strncpy( aOldData.cDriverName, aDriverByteName.getStr(), 31 );
338 : // nLen = sizeof( aOldData ) + 4 + nOldJobDataSize + pJobData->mnDriverDataLen;
339 0 : int nPos = rOStream.Tell();
340 0 : rOStream.WriteUInt16( nLen );
341 0 : rOStream.WriteUInt16( nSystem );
342 0 : rOStream.Write( (char*)&aOldData, sizeof( aOldData ) );
343 0 : rOStream.Write( (char*)&aOldJobData, nOldJobDataSize );
344 0 : rOStream.Write( (char*)pJobData->mpDriverData, pJobData->mnDriverDataLen );
345 0 : ::boost::unordered_map< OUString, OUString, OUStringHash >::const_iterator it;
346 0 : for( it = pJobData->maValueMap.begin(); it != pJobData->maValueMap.end(); ++it )
347 : {
348 0 : write_uInt16_lenPrefixed_uInt8s_FromOUString(rOStream, it->first, RTL_TEXTENCODING_UTF8);
349 0 : write_uInt16_lenPrefixed_uInt8s_FromOUString(rOStream, it->second, RTL_TEXTENCODING_UTF8);
350 : }
351 0 : write_uInt16_lenPrefixed_uInt8s_FromOString(rOStream, "COMPAT_DUPLEX_MODE");
352 0 : switch( pJobData->meDuplexMode )
353 : {
354 : case DUPLEX_UNKNOWN:
355 0 : write_uInt16_lenPrefixed_uInt8s_FromOString(rOStream, "DUPLEX_UNKNOWN");
356 0 : break;
357 : case DUPLEX_OFF:
358 0 : write_uInt16_lenPrefixed_uInt8s_FromOString(rOStream, "DUPLEX_OFF");
359 0 : break;
360 : case DUPLEX_SHORTEDGE:
361 0 : write_uInt16_lenPrefixed_uInt8s_FromOString(rOStream, "DUPLEX_SHORTEDGE");
362 0 : break;
363 : case DUPLEX_LONGEDGE:
364 0 : write_uInt16_lenPrefixed_uInt8s_FromOString(rOStream, "DUPLEX_LONGEDGE");
365 0 : break;
366 : }
367 0 : nLen = sal::static_int_cast<sal_uInt16>(rOStream.Tell() - nPos);
368 0 : rOStream.Seek( nPos );
369 0 : rOStream.WriteUInt16( nLen );
370 0 : rOStream.Seek( nPos + nLen );
371 : }
372 : }
373 :
374 0 : return rOStream;
375 : }
376 :
377 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|