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 864 : ImplJobSetup::ImplJobSetup()
53 : {
54 864 : mnRefCount = 1;
55 864 : mnSystem = 0;
56 864 : meOrientation = ORIENTATION_PORTRAIT;
57 864 : meDuplexMode = DUPLEX_UNKNOWN;
58 864 : mnPaperBin = 0;
59 864 : mePaperFormat = PAPER_USER;
60 864 : mnPaperWidth = 0;
61 864 : mnPaperHeight = 0;
62 864 : mnDriverDataLen = 0;
63 864 : mpDriverData = NULL;
64 864 : }
65 :
66 100 : ImplJobSetup::ImplJobSetup( const ImplJobSetup& rJobSetup ) :
67 : maPrinterName( rJobSetup.maPrinterName ),
68 100 : maDriver( rJobSetup.maDriver )
69 : {
70 100 : mnRefCount = 1;
71 100 : mnSystem = rJobSetup.mnSystem;
72 100 : meOrientation = rJobSetup.meOrientation;
73 100 : meDuplexMode = rJobSetup.meDuplexMode;
74 100 : mnPaperBin = rJobSetup.mnPaperBin;
75 100 : mePaperFormat = rJobSetup.mePaperFormat;
76 100 : mnPaperWidth = rJobSetup.mnPaperWidth;
77 100 : mnPaperHeight = rJobSetup.mnPaperHeight;
78 100 : mnDriverDataLen = rJobSetup.mnDriverDataLen;
79 100 : if ( rJobSetup.mpDriverData )
80 : {
81 100 : mpDriverData = (sal_uInt8*)rtl_allocateMemory( mnDriverDataLen );
82 100 : memcpy( mpDriverData, rJobSetup.mpDriverData, mnDriverDataLen );
83 : }
84 : else
85 0 : mpDriverData = NULL;
86 100 : maValueMap = rJobSetup.maValueMap;
87 100 : }
88 :
89 1922 : ImplJobSetup::~ImplJobSetup()
90 : {
91 961 : rtl_freeMemory( mpDriverData );
92 961 : }
93 :
94 1560 : ImplJobSetup* JobSetup::ImplGetData()
95 : {
96 1560 : if ( !mpData )
97 680 : mpData = new ImplJobSetup;
98 880 : else if ( mpData->mnRefCount != 1 )
99 : {
100 100 : mpData->mnRefCount--;
101 100 : mpData = new ImplJobSetup( *mpData );
102 : }
103 :
104 1560 : return mpData;
105 : }
106 :
107 1560 : ImplJobSetup* JobSetup::ImplGetConstData()
108 : {
109 1560 : if ( !mpData )
110 0 : mpData = new ImplJobSetup;
111 1560 : return mpData;
112 : }
113 :
114 556 : const ImplJobSetup* JobSetup::ImplGetConstData() const
115 : {
116 556 : if ( !mpData )
117 0 : ((JobSetup*)this)->mpData = new ImplJobSetup;
118 556 : return mpData;
119 : }
120 :
121 11296 : JobSetup::JobSetup()
122 : {
123 :
124 11296 : mpData = NULL;
125 11296 : }
126 :
127 100 : JobSetup::JobSetup( const JobSetup& rJobSetup )
128 : {
129 : DBG_ASSERT( !rJobSetup.mpData || (rJobSetup.mpData->mnRefCount < 0xFFFE), "JobSetup: RefCount overflow" );
130 :
131 100 : mpData = rJobSetup.mpData;
132 100 : if ( mpData )
133 100 : mpData->mnRefCount++;
134 100 : }
135 :
136 11390 : JobSetup::~JobSetup()
137 : {
138 :
139 11390 : if ( mpData )
140 : {
141 961 : if ( mpData->mnRefCount == 1 )
142 861 : delete mpData;
143 : else
144 100 : mpData->mnRefCount--;
145 : }
146 11390 : }
147 :
148 1018 : OUString JobSetup::GetPrinterName() const
149 : {
150 1018 : if ( mpData )
151 408 : return mpData->maPrinterName;
152 : else
153 610 : return OUString();
154 : }
155 :
156 40 : OUString JobSetup::GetDriverName() const
157 : {
158 40 : if ( mpData )
159 40 : return mpData->maDriver;
160 : else
161 0 : return OUString();
162 : }
163 :
164 100 : 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 100 : if ( rJobSetup.mpData )
170 100 : rJobSetup.mpData->mnRefCount++;
171 :
172 : // If it's not static ImpData and the last reference, delete it, else
173 : // decrement refcount
174 100 : if ( mpData )
175 : {
176 100 : if ( mpData->mnRefCount == 1 )
177 100 : delete mpData;
178 : else
179 0 : mpData->mnRefCount--;
180 : }
181 :
182 100 : mpData = rJobSetup.mpData;
183 :
184 100 : 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 184 : SvStream& ReadJobSetup( SvStream& rIStream, JobSetup& rJobSetup )
217 : {
218 : DBG_ASSERTWARNING( rIStream.GetVersion(), "JobSetup::>> - Solar-Version not set on rOStream" );
219 :
220 : {
221 184 : sal_Size nFirstPos = rIStream.Tell();
222 :
223 184 : sal_uInt16 nLen = 0;
224 184 : rIStream.ReadUInt16( nLen );
225 184 : if ( !nLen )
226 0 : return rIStream;
227 :
228 184 : sal_uInt16 nSystem = 0;
229 184 : rIStream.ReadUInt16( nSystem );
230 :
231 184 : boost::scoped_array<char> pTempBuf(new char[nLen]);
232 184 : rIStream.Read( pTempBuf.get(), nLen - sizeof( nLen ) - sizeof( nSystem ) );
233 184 : if ( nLen >= sizeof(ImplOldJobSetupData)+4 )
234 : {
235 184 : ImplOldJobSetupData* pData = (ImplOldJobSetupData*)pTempBuf.get();
236 184 : 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 184 : rtl_TextEncoding aStreamEncoding = RTL_TEXTENCODING_UTF8;
245 184 : if( nSystem == JOBSET_FILE364_SYSTEM )
246 0 : aStreamEncoding = rIStream.GetStreamCharSet();
247 :
248 184 : rJobSetup.mpData = new ImplJobSetup;
249 184 : ImplJobSetup* pJobData = rJobSetup.mpData;
250 184 : pJobData->maPrinterName = OStringToOUString(pData->cPrinterName, aStreamEncoding);
251 184 : pJobData->maDriver = OStringToOUString(pData->cDriverName, aStreamEncoding);
252 :
253 : // Are these our new JobSetup files?
254 368 : if ( nSystem == JOBSET_FILE364_SYSTEM ||
255 184 : nSystem == JOBSET_FILE605_SYSTEM )
256 : {
257 184 : Impl364JobSetupData* pOldJobData = (Impl364JobSetupData*)(pTempBuf.get() + sizeof( ImplOldJobSetupData ));
258 184 : sal_uInt16 nOldJobDataSize = SVBT16ToShort( pOldJobData->nSize );
259 184 : pJobData->mnSystem = SVBT16ToShort( pOldJobData->nSystem );
260 184 : pJobData->mnDriverDataLen = SVBT32ToUInt32( pOldJobData->nDriverDataLen );
261 184 : pJobData->meOrientation = (Orientation)SVBT16ToShort( pOldJobData->nOrientation );
262 184 : pJobData->meDuplexMode = DUPLEX_UNKNOWN;
263 184 : pJobData->mnPaperBin = SVBT16ToShort( pOldJobData->nPaperBin );
264 184 : pJobData->mePaperFormat = (Paper)SVBT16ToShort( pOldJobData->nPaperFormat );
265 184 : pJobData->mnPaperWidth = (long)SVBT32ToUInt32( pOldJobData->nPaperWidth );
266 184 : pJobData->mnPaperHeight = (long)SVBT32ToUInt32( pOldJobData->nPaperHeight );
267 184 : if ( pJobData->mnDriverDataLen )
268 : {
269 180 : sal_uInt8* pDriverData = ((sal_uInt8*)pOldJobData) + nOldJobDataSize;
270 180 : pJobData->mpDriverData = (sal_uInt8*)rtl_allocateMemory( pJobData->mnDriverDataLen );
271 180 : memcpy( pJobData->mpDriverData, pDriverData, pJobData->mnDriverDataLen );
272 : }
273 184 : if( nSystem == JOBSET_FILE605_SYSTEM )
274 : {
275 184 : rIStream.Seek( nFirstPos + sizeof( ImplOldJobSetupData ) + 4 + sizeof( Impl364JobSetupData ) + pJobData->mnDriverDataLen );
276 546 : while( rIStream.Tell() < nFirstPos + nLen )
277 : {
278 178 : OUString aKey = read_uInt16_lenPrefixed_uInt8s_ToOUString(rIStream, RTL_TEXTENCODING_UTF8);
279 356 : OUString aValue = read_uInt16_lenPrefixed_uInt8s_ToOUString(rIStream, RTL_TEXTENCODING_UTF8);
280 178 : if( aKey.equalsAscii( "COMPAT_DUPLEX_MODE" ) )
281 : {
282 172 : if( aValue.equalsAscii( "DUPLEX_UNKNOWN" ) )
283 36 : pJobData->meDuplexMode = DUPLEX_UNKNOWN;
284 136 : else if( aValue.equalsAscii( "DUPLEX_OFF" ) )
285 132 : pJobData->meDuplexMode = DUPLEX_OFF;
286 4 : else if( aValue.equalsAscii( "DUPLEX_SHORTEDGE" ) )
287 0 : pJobData->meDuplexMode = DUPLEX_SHORTEDGE;
288 4 : else if( aValue.equalsAscii( "DUPLEX_LONGEDGE" ) )
289 4 : pJobData->meDuplexMode = DUPLEX_LONGEDGE;
290 : }
291 : else
292 6 : pJobData->maValueMap[ aKey ] = aValue;
293 178 : }
294 : DBG_ASSERT( rIStream.Tell() == nFirstPos+nLen, "corrupted job setup" );
295 : // ensure correct stream position
296 184 : rIStream.Seek( nFirstPos + nLen );
297 : }
298 : }
299 184 : }
300 : }
301 :
302 184 : return rIStream;
303 : }
304 :
305 40 : 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 40 : sal_uInt16 nLen = 0;
314 40 : if ( !rJobSetup.mpData )
315 0 : rOStream.WriteUInt16( nLen );
316 : else
317 : {
318 40 : sal_uInt16 nSystem = JOBSET_FILE605_SYSTEM;
319 :
320 40 : const ImplJobSetup* pJobData = rJobSetup.ImplGetConstData();
321 : Impl364JobSetupData aOldJobData;
322 40 : sal_uInt16 nOldJobDataSize = sizeof( aOldJobData );
323 40 : ShortToSVBT16( nOldJobDataSize, aOldJobData.nSize );
324 40 : ShortToSVBT16( pJobData->mnSystem, aOldJobData.nSystem );
325 40 : UInt32ToSVBT32( pJobData->mnDriverDataLen, aOldJobData.nDriverDataLen );
326 40 : ShortToSVBT16( (sal_uInt16)(pJobData->meOrientation), aOldJobData.nOrientation );
327 40 : ShortToSVBT16( pJobData->mnPaperBin, aOldJobData.nPaperBin );
328 40 : ShortToSVBT16( (sal_uInt16)(pJobData->mePaperFormat), aOldJobData.nPaperFormat );
329 40 : UInt32ToSVBT32( (sal_uLong)(pJobData->mnPaperWidth), aOldJobData.nPaperWidth );
330 40 : UInt32ToSVBT32( (sal_uLong)(pJobData->mnPaperHeight), aOldJobData.nPaperHeight );
331 :
332 : ImplOldJobSetupData aOldData;
333 40 : memset( &aOldData, 0, sizeof( aOldData ) );
334 40 : OString aPrnByteName(OUStringToOString(rJobSetup.GetPrinterName(), RTL_TEXTENCODING_UTF8));
335 40 : strncpy( aOldData.cPrinterName, aPrnByteName.getStr(), 63 );
336 80 : OString aDriverByteName(OUStringToOString(rJobSetup.GetDriverName(), RTL_TEXTENCODING_UTF8));
337 40 : strncpy( aOldData.cDriverName, aDriverByteName.getStr(), 31 );
338 : // nLen = sizeof( aOldData ) + 4 + nOldJobDataSize + pJobData->mnDriverDataLen;
339 40 : int nPos = rOStream.Tell();
340 40 : rOStream.WriteUInt16( nLen );
341 40 : rOStream.WriteUInt16( nSystem );
342 40 : rOStream.Write( (char*)&aOldData, sizeof( aOldData ) );
343 40 : rOStream.Write( (char*)&aOldJobData, nOldJobDataSize );
344 40 : rOStream.Write( (char*)pJobData->mpDriverData, pJobData->mnDriverDataLen );
345 40 : ::boost::unordered_map< OUString, OUString, OUStringHash >::const_iterator it;
346 40 : 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 40 : write_uInt16_lenPrefixed_uInt8s_FromOString(rOStream, "COMPAT_DUPLEX_MODE");
352 40 : 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 40 : write_uInt16_lenPrefixed_uInt8s_FromOString(rOStream, "DUPLEX_OFF");
359 40 : 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 40 : nLen = sal::static_int_cast<sal_uInt16>(rOStream.Tell() - nPos);
368 40 : rOStream.Seek( nPos );
369 40 : rOStream.WriteUInt16( nLen );
370 80 : rOStream.Seek( nPos + nLen );
371 : }
372 : }
373 :
374 40 : return rOStream;
375 : }
376 :
377 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|