LibreOffice Module vcl (master)  1
jobset.cxx
Go to the documentation of this file.
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/ustring.hxx>
21 #include <sal/log.hxx>
22 #include <tools/solar.h>
23 #include <tools/stream.hxx>
24 #include <vcl/jobset.hxx>
25 #include <jobset.h>
26 #include <memory>
27 #include <rtl/instance.hxx>
28 
29 #define JOBSET_FILE364_SYSTEM (sal_uInt16(0xFFFF))
30 #define JOBSET_FILE605_SYSTEM (sal_uInt16(0xFFFE))
31 
32 namespace {
33 
34 struct ImplOldJobSetupData
35 {
36  char cPrinterName[64];
37  char cDeviceName[32];
38  char cPortName[32];
39  char cDriverName[32];
40 };
41 
42 struct Impl364JobSetupData
43 {
44  SVBT16 nSize;
45  SVBT16 nSystem;
46  SVBT32 nDriverDataLen;
47  SVBT16 nOrientation;
48  SVBT16 nPaperBin;
49  SVBT16 nPaperFormat;
50  SVBT32 nPaperWidth;
51  SVBT32 nPaperHeight;
52 };
53 
54 }
55 
57 {
58  mnSystem = 0;
61  mnPaperBin = 0;
63  mnPaperWidth = 0;
64  mnPaperHeight = 0;
65  mnDriverDataLen = 0;
66  mpDriverData = nullptr;
67  mbPapersizeFromSetup = false;
69 }
70 
72  mnSystem( rJobSetup.GetSystem() ),
73  maPrinterName( rJobSetup.GetPrinterName() ),
74  maDriver( rJobSetup.GetDriver() ),
75  meOrientation( rJobSetup.GetOrientation() ),
76  meDuplexMode( rJobSetup.GetDuplexMode() ),
77  mnPaperBin( rJobSetup.GetPaperBin() ),
78  mePaperFormat( rJobSetup.GetPaperFormat() ),
79  mnPaperWidth( rJobSetup.GetPaperWidth() ),
80  mnPaperHeight( rJobSetup.GetPaperHeight() ),
81  mnDriverDataLen( rJobSetup.GetDriverDataLen() ),
82  mbPapersizeFromSetup( rJobSetup.GetPapersizeFromSetup() ),
83  meSetupMode( rJobSetup.GetPrinterSetupMode() ),
84  maValueMap( rJobSetup.GetValueMap() )
85  {
86  if ( rJobSetup.GetDriverData() )
87  {
88  mpDriverData = static_cast<sal_uInt8*>(std::malloc( mnDriverDataLen ));
89  memcpy( mpDriverData, rJobSetup.GetDriverData(), mnDriverDataLen );
90  }
91  else
92  mpDriverData = nullptr;
93 }
94 
96 {
97  std::free( mpDriverData );
98 }
99 
100 void ImplJobSetup::SetSystem(sal_uInt16 nSystem)
101 {
102  mnSystem = nSystem;
103 }
104 
105 void ImplJobSetup::SetPrinterName(const OUString& rPrinterName)
106 {
107  maPrinterName = rPrinterName;
108 }
109 
110 void ImplJobSetup::SetDriver(const OUString& rDriver)
111 {
112  maDriver = rDriver;
113 }
114 
116 {
117  meOrientation = eOrientation;
118 }
119 
121 {
122  meDuplexMode = eDuplexMode;
123 }
124 
125 void ImplJobSetup::SetPaperBin(sal_uInt16 nPaperBin)
126 {
127  mnPaperBin = nPaperBin;
128 }
129 
131 {
132  mePaperFormat = ePaperFormat;
133 }
134 
135 void ImplJobSetup::SetPaperWidth(long nPaperWidth)
136 {
137  mnPaperWidth = nPaperWidth;
138 }
139 
140 void ImplJobSetup::SetPaperHeight(long nPaperHeight)
141 {
142  mnPaperHeight = nPaperHeight;
143 }
144 
145 void ImplJobSetup::SetDriverDataLen(sal_uInt32 nDriverDataLen)
146 {
147  mnDriverDataLen = nDriverDataLen;
148 }
149 
151 {
152  mpDriverData = pDriverData;
153 }
154 
155 void ImplJobSetup::SetPapersizeFromSetup(bool bPapersizeFromSetup)
156 {
157  mbPapersizeFromSetup = bPapersizeFromSetup;
158 }
159 
161 {
162  meSetupMode = eMode;
163 }
164 
165 void ImplJobSetup::SetValueMap( const OUString& rKey, const OUString& rValue )
166 {
167  maValueMap [ rKey ] = rValue;
168 }
169 
170 JobSetup& JobSetup::operator=( const JobSetup& ) = default;
171 
172 JobSetup& JobSetup::operator=( JobSetup&& ) = default;
173 
174 bool ImplJobSetup::operator==( const ImplJobSetup& rImplJobSetup ) const
175 {
176  return mnSystem == rImplJobSetup.mnSystem &&
177  maPrinterName == rImplJobSetup.maPrinterName &&
178  maDriver == rImplJobSetup.maDriver &&
179  meOrientation == rImplJobSetup.meOrientation &&
180  meDuplexMode == rImplJobSetup.meDuplexMode &&
181  mnPaperBin == rImplJobSetup.mnPaperBin &&
182  mePaperFormat == rImplJobSetup.mePaperFormat &&
183  mnPaperWidth == rImplJobSetup.mnPaperWidth &&
184  mnPaperHeight == rImplJobSetup.mnPaperHeight &&
185  mbPapersizeFromSetup == rImplJobSetup.mbPapersizeFromSetup &&
186  mnDriverDataLen == rImplJobSetup.mnDriverDataLen &&
187  maValueMap == rImplJobSetup.maValueMap &&
188  memcmp( mpDriverData, rImplJobSetup.mpDriverData, mnDriverDataLen ) == 0;
189 }
190 
191 namespace
192 {
193  struct theGlobalDefault :
194  public rtl::Static< JobSetup::ImplType, theGlobalDefault > {};
195 }
196 
197 JobSetup::JobSetup() : mpData(theGlobalDefault::get())
198 {
199 }
200 
201 JobSetup::JobSetup( const JobSetup& ) = default;
202 
203 JobSetup::~JobSetup() = default;
204 
205 bool JobSetup::operator==( const JobSetup& rJobSetup ) const
206 {
207  return mpData == rJobSetup.mpData;
208 }
209 
211 {
212  return *mpData;
213 }
214 
216 {
217  return *mpData;
218 }
219 
220 OUString const & JobSetup::GetPrinterName() const
221 {
222  return mpData->GetPrinterName();
223 }
224 
226 {
227  return mpData.same_object(theGlobalDefault::get());
228 }
229 
230 SvStream& ReadJobSetup( SvStream& rIStream, JobSetup& rJobSetup )
231 {
232  {
233  sal_uInt16 nLen = 0;
234  rIStream.ReadUInt16( nLen );
235  if (nLen <= 4)
236  return rIStream;
237 
238  sal_uInt16 nSystem = 0;
239  rIStream.ReadUInt16( nSystem );
240  size_t nRead = nLen - sizeof(nLen) - sizeof(nSystem);
241  if (nRead > rIStream.remainingSize())
242  {
243  SAL_WARN("vcl", "Parsing error: " << rIStream.remainingSize() <<
244  " max possible entries, but " << nRead << " claimed, truncating");
245  return rIStream;
246  }
247  sal_uInt64 const nFirstPos = rIStream.Tell();
248  std::unique_ptr<char[]> pTempBuf(new char[nRead]);
249  nRead = rIStream.ReadBytes(pTempBuf.get(), nRead);
250  if (nRead >= sizeof(ImplOldJobSetupData))
251  {
252  ImplOldJobSetupData* pData = reinterpret_cast<ImplOldJobSetupData*>(pTempBuf.get());
253 
254  rtl_TextEncoding aStreamEncoding = RTL_TEXTENCODING_UTF8;
255  if( nSystem == JOBSET_FILE364_SYSTEM )
256  aStreamEncoding = rIStream.GetStreamCharSet();
257 
258  ImplJobSetup& rJobData = rJobSetup.ImplGetData();
259 
260  pData->cPrinterName[SAL_N_ELEMENTS(pData->cPrinterName) - 1] = 0;
261  rJobData.SetPrinterName( OStringToOUString(pData->cPrinterName, aStreamEncoding) );
262  pData->cDriverName[SAL_N_ELEMENTS(pData->cDriverName) - 1] = 0;
263  rJobData.SetDriver( OStringToOUString(pData->cDriverName, aStreamEncoding) );
264 
265  // Are these our new JobSetup files?
266  if ( nSystem == JOBSET_FILE364_SYSTEM ||
267  nSystem == JOBSET_FILE605_SYSTEM )
268  {
269  Impl364JobSetupData* pOldJobData = reinterpret_cast<Impl364JobSetupData*>(pTempBuf.get() + sizeof( ImplOldJobSetupData ));
270  sal_uInt16 nOldJobDataSize = SVBT16ToUInt16( pOldJobData->nSize );
271  rJobData.SetSystem( SVBT16ToUInt16( pOldJobData->nSystem ) );
272  rJobData.SetDriverDataLen( SVBT32ToUInt32( pOldJobData->nDriverDataLen ) );
273  rJobData.SetOrientation( static_cast<Orientation>(SVBT16ToUInt16( pOldJobData->nOrientation )) );
275  rJobData.SetPaperBin( SVBT16ToUInt16( pOldJobData->nPaperBin ) );
276  rJobData.SetPaperFormat( static_cast<Paper>(SVBT16ToUInt16( pOldJobData->nPaperFormat )) );
277  rJobData.SetPaperWidth( static_cast<long>(SVBT32ToUInt32( pOldJobData->nPaperWidth )) );
278  rJobData.SetPaperHeight( static_cast<long>(SVBT32ToUInt32( pOldJobData->nPaperHeight )) );
279  if ( rJobData.GetDriverDataLen() )
280  {
281  const char* pDriverData = reinterpret_cast<const char*>(pOldJobData) + nOldJobDataSize;
282  const char* pDriverDataEnd = pDriverData + rJobData.GetDriverDataLen();
283  if (pDriverDataEnd > pTempBuf.get() + nRead)
284  {
285  SAL_WARN("vcl", "corrupted job setup");
286  }
287  else
288  {
289  sal_uInt8* pNewDriverData = static_cast<sal_uInt8*>(
290  std::malloc( rJobData.GetDriverDataLen() ));
291  memcpy( pNewDriverData, pDriverData, rJobData.GetDriverDataLen() );
292  rJobData.SetDriverData( pNewDriverData );
293  }
294  }
295  if( nSystem == JOBSET_FILE605_SYSTEM )
296  {
297  rIStream.Seek( nFirstPos + sizeof( ImplOldJobSetupData ) +
298  sizeof( Impl364JobSetupData ) + rJobData.GetDriverDataLen() );
299  while( rIStream.Tell() < nFirstPos + nRead )
300  {
301  OUString aKey = read_uInt16_lenPrefixed_uInt8s_ToOUString(rIStream, RTL_TEXTENCODING_UTF8);
302  OUString aValue = read_uInt16_lenPrefixed_uInt8s_ToOUString(rIStream, RTL_TEXTENCODING_UTF8);
303  if( aKey == "COMPAT_DUPLEX_MODE" )
304  {
305  if( aValue == "DuplexMode::Unknown" )
307  else if( aValue == "DuplexMode::Off" )
308  rJobData.SetDuplexMode( DuplexMode::Off );
309  else if( aValue == "DuplexMode::ShortEdge" )
311  else if( aValue == "DuplexMode::LongEdge" )
313  }
314  else
315  rJobData.SetValueMap(aKey, aValue);
316  }
317  SAL_WARN_IF( rIStream.Tell() != nFirstPos+nRead, "vcl", "corrupted job setup" );
318  // ensure correct stream position
319  rIStream.Seek(nFirstPos + nRead);
320  }
321  }
322  }
323  }
324 
325  return rIStream;
326 }
327 
328 SvStream& WriteJobSetup( SvStream& rOStream, const JobSetup& rJobSetup )
329 {
330  {
331  sal_uInt16 nLen = 0;
332  if ( rJobSetup.IsDefault() )
333  rOStream.WriteUInt16( nLen );
334  else
335  {
336  const ImplJobSetup& rJobData = rJobSetup.ImplGetConstData();
337  Impl364JobSetupData aOldJobData;
338  sal_uInt16 nOldJobDataSize = sizeof( aOldJobData );
339  ShortToSVBT16( nOldJobDataSize, aOldJobData.nSize );
340  ShortToSVBT16( rJobData.GetSystem(), aOldJobData.nSystem );
341  UInt32ToSVBT32( rJobData.GetDriverDataLen(), aOldJobData.nDriverDataLen );
342  ShortToSVBT16( static_cast<sal_uInt16>(rJobData.GetOrientation()), aOldJobData.nOrientation );
343  ShortToSVBT16( rJobData.GetPaperBin(), aOldJobData.nPaperBin );
344  ShortToSVBT16( static_cast<sal_uInt16>(rJobData.GetPaperFormat()), aOldJobData.nPaperFormat );
345  UInt32ToSVBT32( static_cast<sal_uLong>(rJobData.GetPaperWidth()), aOldJobData.nPaperWidth );
346  UInt32ToSVBT32( static_cast<sal_uLong>(rJobData.GetPaperHeight()), aOldJobData.nPaperHeight );
347 
348  ImplOldJobSetupData aOldData = {};
349  OString aPrnByteName(OUStringToOString(rJobData.GetPrinterName(), RTL_TEXTENCODING_UTF8));
350  strncpy(aOldData.cPrinterName, aPrnByteName.getStr(), SAL_N_ELEMENTS(aOldData.cPrinterName) - 1);
351  OString aDriverByteName(OUStringToOString(rJobData.GetDriver(), RTL_TEXTENCODING_UTF8));
352  strncpy(aOldData.cDriverName, aDriverByteName.getStr(), SAL_N_ELEMENTS(aOldData.cDriverName) - 1);
353  int nPos = rOStream.Tell();
354  rOStream.WriteUInt16( 0 );
356  rOStream.WriteBytes( &aOldData, sizeof( aOldData ) );
357  rOStream.WriteBytes( &aOldJobData, nOldJobDataSize );
358  rOStream.WriteBytes( rJobData.GetDriverData(), rJobData.GetDriverDataLen() );
359 
360  const std::unordered_map< OUString, OUString >& rValueMap(
361  rJobData.GetValueMap());
362 
363  for (auto const& value : rValueMap)
364  {
365  write_uInt16_lenPrefixed_uInt8s_FromOUString(rOStream, value.first, RTL_TEXTENCODING_UTF8);
366  write_uInt16_lenPrefixed_uInt8s_FromOUString(rOStream, value.second, RTL_TEXTENCODING_UTF8);
367  }
368  write_uInt16_lenPrefixed_uInt8s_FromOString(rOStream, "COMPAT_DUPLEX_MODE");
369  switch( rJobData.GetDuplexMode() )
370  {
371  case DuplexMode::Unknown:
372  write_uInt16_lenPrefixed_uInt8s_FromOString(rOStream, "DuplexMode::Unknown");
373  break;
374  case DuplexMode::Off:
375  write_uInt16_lenPrefixed_uInt8s_FromOString(rOStream, "DuplexMode::Off");
376  break;
378  write_uInt16_lenPrefixed_uInt8s_FromOString(rOStream, "DuplexMode::ShortEdge");
379  break;
381  write_uInt16_lenPrefixed_uInt8s_FromOString(rOStream, "DuplexMode::LongEdge");
382  break;
383  }
384  nLen = sal::static_int_cast<sal_uInt16>(rOStream.Tell() - nPos);
385  rOStream.Seek( nPos );
386  rOStream.WriteUInt16( nLen );
387  rOStream.Seek( nPos + nLen );
388  }
389  }
390 
391  return rOStream;
392 }
393 
394 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
sal_uInt32 mnDriverDataLen
Definition: jobset.h:46
void SetPrinterSetupMode(PrinterSetupMode eMode)
Definition: jobset.cxx:160
long mnPaperHeight
Definition: jobset.h:45
OUString maDriver
Definition: jobset.h:39
long GetPaperWidth() const
Definition: jobset.h:82
long GetPaperHeight() const
Definition: jobset.h:85
sal_uInt8 SVBT16[2]
SvStream & WriteUInt16(sal_uInt16 nUInt16)
sal_uInt16 mnSystem
Definition: jobset.h:37
SvStream & WriteJobSetup(SvStream &rOStream, const JobSetup &rJobSetup)
Definition: jobset.cxx:328
SvStream & ReadUInt16(sal_uInt16 &rUInt16)
bool operator==(const ImplJobSetup &rImplJobSetup) const
Definition: jobset.cxx:174
std::unique_ptr< ContentProperties > pData
void SetValueMap(const OUString &rKey, const OUString &rValue)
Definition: jobset.cxx:165
sal_uInt64 Seek(sal_uInt64 nPos)
~ImplJobSetup()
Definition: jobset.cxx:95
PrinterSetupMode
Definition: prntypes.hxx:85
const OUString & GetDriver() const
Definition: jobset.h:67
JobSetup()
Definition: jobset.cxx:197
sal_uInt8 SVBT32[4]
SAL_DLLPRIVATE const ImplJobSetup & ImplGetConstData() const
Definition: jobset.cxx:210
void SetDriverDataLen(sal_uInt32 nDriverDataLen)
Definition: jobset.cxx:145
DuplexMode GetDuplexMode() const
Definition: jobset.h:73
Paper
DuplexMode
Definition: prntypes.hxx:28
void SetSystem(sal_uInt16 nSystem)
Definition: jobset.cxx:100
void SetDuplexMode(DuplexMode eDuplexMode)
Definition: jobset.cxx:120
Orientation meOrientation
Definition: jobset.h:40
void SetDriverData(sal_uInt8 *pDriverData)
Definition: jobset.cxx:150
SAL_DLLPRIVATE ImplJobSetup & ImplGetData()
Definition: jobset.cxx:215
sal_uInt64 remainingSize()
ImplJobSetup()
Definition: jobset.cxx:56
#define SAL_N_ELEMENTS(arr)
OUString read_uInt16_lenPrefixed_uInt8s_ToOUString(SvStream &rStrm, rtl_TextEncoding eEnc)
void SetOrientation(Orientation eOrientation)
Definition: jobset.cxx:115
const OUString & GetPrinterName() const
Definition: jobset.h:64
PrinterSetupMode meSetupMode
Definition: jobset.h:50
bool same_object(const cow_wrapper &rOther) const
const sal_uInt8 * GetDriverData() const
Definition: jobset.h:91
std::size_t WriteBytes(const void *pData, std::size_t nSize)
sal_uInt16 GetSystem() const
Definition: jobset.h:61
long mnPaperWidth
Definition: jobset.h:44
void SetPapersizeFromSetup(bool bPapersizeFromSetup)
Definition: jobset.cxx:155
sal_uInt8 * mpDriverData
Definition: jobset.h:47
Orientation GetOrientation() const
Definition: jobset.h:70
std::unordered_map< OUString, OUString > maValueMap
Definition: jobset.h:52
std::size_t write_uInt16_lenPrefixed_uInt8s_FromOString(SvStream &rStrm, const OString &rStr)
Paper mePaperFormat
Definition: jobset.h:43
void SetPaperWidth(long nWidth)
Definition: jobset.cxx:135
std::size_t ReadBytes(void *pData, std::size_t nSize)
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
sal_uInt16 GetPaperBin() const
Definition: jobset.h:76
OUString const & GetPrinterName() const
Definition: jobset.cxx:220
void SetPaperBin(sal_uInt16 nPaperBin)
Definition: jobset.cxx:125
#define SAL_WARN_IF(condition, area, stream)
unsigned char sal_uInt8
sal_uInt32 GetDriverDataLen() const
Definition: jobset.h:88
rtl_TextEncoding GetStreamCharSet() const
Orientation
Definition: prntypes.hxx:31
void SetPrinterName(const OUString &rPrinterName)
Definition: jobset.cxx:105
sal_uInt64 Tell() const
Any value
void SetPaperHeight(long nHeight)
Definition: jobset.cxx:140
ImplType mpData
Definition: jobset.hxx:58
Paper GetPaperFormat() const
Definition: jobset.h:79
bool operator==(const JobSetup &rJobSetup) const
Definition: jobset.cxx:205
void SetDriver(const OUString &rDriver)
Definition: jobset.cxx:110
sal_uInt16 mnPaperBin
Definition: jobset.h:42
SvStream & ReadJobSetup(SvStream &rIStream, JobSetup &rJobSetup)
Definition: jobset.cxx:230
#define SAL_WARN(area, stream)
void SetPaperFormat(Paper ePaperFormat)
Definition: jobset.cxx:130
JobSetup & operator=(const JobSetup &rJob)
DuplexMode meDuplexMode
Definition: jobset.h:41
std::size_t write_uInt16_lenPrefixed_uInt8s_FromOUString(SvStream &rStrm, const OUString &rStr, rtl_TextEncoding eEnc)
PAPER_USER
#define JOBSET_FILE364_SYSTEM
Definition: jobset.cxx:29
bool IsDefault() const
Definition: jobset.cxx:225
sal_uInt16 nPos
#define JOBSET_FILE605_SYSTEM
Definition: jobset.cxx:30
const std::unordered_map< OUString, OUString > & GetValueMap() const
Definition: jobset.h:100
bool mbPapersizeFromSetup
Definition: jobset.h:48
OUString maPrinterName
Definition: jobset.h:38