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