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 <officecfg/Office/Common.hxx>
21 : #include "vcl/jobdata.hxx"
22 : #include "vcl/printerinfomanager.hxx"
23 : #include "tools/stream.hxx"
24 :
25 : #include <sal/alloca.h>
26 : #include <rtl/strbuf.hxx>
27 : #include <boost/scoped_array.hpp>
28 :
29 : using namespace psp;
30 :
31 1162 : JobData& JobData::operator=(const JobData& rRight)
32 : {
33 1162 : m_nCopies = rRight.m_nCopies;
34 1162 : m_bCollate = rRight.m_bCollate;
35 1162 : m_nLeftMarginAdjust = rRight.m_nLeftMarginAdjust;
36 1162 : m_nRightMarginAdjust = rRight.m_nRightMarginAdjust;
37 1162 : m_nTopMarginAdjust = rRight.m_nTopMarginAdjust;
38 1162 : m_nBottomMarginAdjust = rRight.m_nBottomMarginAdjust;
39 1162 : m_nColorDepth = rRight.m_nColorDepth;
40 1162 : m_eOrientation = rRight.m_eOrientation;
41 1162 : m_aPrinterName = rRight.m_aPrinterName;
42 1162 : m_pParser = rRight.m_pParser;
43 1162 : m_aContext = rRight.m_aContext;
44 1162 : m_nPSLevel = rRight.m_nPSLevel;
45 1162 : m_nPDFDevice = rRight.m_nPDFDevice;
46 1162 : m_nColorDevice = rRight.m_nColorDevice;
47 :
48 1162 : if( !m_pParser && !m_aPrinterName.isEmpty() )
49 : {
50 0 : PrinterInfoManager& rMgr = PrinterInfoManager::get();
51 0 : rMgr.setupJobContextData( *this );
52 : }
53 1162 : return *this;
54 : }
55 :
56 0 : bool psp::operator==(const psp::JobData& rLeft, const psp::JobData& rRight)
57 : {
58 0 : return rLeft.m_nCopies == rRight.m_nCopies
59 : // && rLeft.m_bCollate == rRight.m_bCollate
60 0 : && rLeft.m_nLeftMarginAdjust == rRight.m_nLeftMarginAdjust
61 0 : && rLeft.m_nRightMarginAdjust == rRight.m_nRightMarginAdjust
62 0 : && rLeft.m_nTopMarginAdjust == rRight.m_nTopMarginAdjust
63 0 : && rLeft.m_nBottomMarginAdjust == rRight.m_nBottomMarginAdjust
64 0 : && rLeft.m_nColorDepth == rRight.m_nColorDepth
65 0 : && rLeft.m_eOrientation == rRight.m_eOrientation
66 0 : && rLeft.m_aPrinterName == rRight.m_aPrinterName
67 0 : && rLeft.m_pParser == rRight.m_pParser
68 : // && rLeft.m_aContext == rRight.m_aContext
69 0 : && rLeft.m_nPSLevel == rRight.m_nPSLevel
70 0 : && rLeft.m_nPDFDevice == rRight.m_nPDFDevice
71 0 : && rLeft.m_nColorDevice == rRight.m_nColorDevice;
72 : }
73 :
74 0 : void JobData::setCollate( bool bCollate )
75 : {
76 0 : if (m_nPDFDevice > 0)
77 : {
78 0 : m_bCollate = bCollate;
79 0 : return;
80 : }
81 0 : const PPDParser* pParser = m_aContext.getParser();
82 0 : if( pParser )
83 : {
84 0 : const PPDKey* pKey = pParser->getKey( OUString( "Collate" ) );
85 0 : if( pKey )
86 : {
87 0 : const PPDValue* pVal = NULL;
88 0 : if( bCollate )
89 0 : pVal = pKey->getValue( OUString( "True" ) );
90 : else
91 : {
92 0 : pVal = pKey->getValue( OUString( "False" ) );
93 0 : if( ! pVal )
94 0 : pVal = pKey->getValue( OUString( "None" ) );
95 : }
96 0 : m_aContext.setValue( pKey, pVal );
97 : }
98 : }
99 : }
100 :
101 0 : bool JobData::setPaper( int i_nWidth, int i_nHeight )
102 : {
103 0 : bool bSuccess = false;
104 0 : if( m_pParser )
105 : {
106 0 : OUString aPaper( m_pParser->matchPaper( i_nWidth, i_nHeight ) );
107 :
108 0 : const PPDKey* pKey = m_pParser->getKey( OUString( "PageSize" ) );
109 0 : const PPDValue* pValue = pKey ? pKey->getValueCaseInsensitive( aPaper ) : NULL;
110 :
111 0 : bSuccess = pKey && pValue && m_aContext.setValue( pKey, pValue, false );
112 : }
113 0 : return bSuccess;
114 : }
115 :
116 0 : bool JobData::setPaperBin( int i_nPaperBin )
117 : {
118 0 : bool bSuccess = false;
119 0 : if( m_pParser )
120 : {
121 0 : const PPDKey* pKey = m_pParser->getKey( OUString( "InputSlot" ) );
122 0 : const PPDValue* pValue = pKey ? pKey->getValue( i_nPaperBin ) : NULL;
123 :
124 0 : bSuccess = pKey && pValue && m_aContext.setValue( pKey, pValue, false );
125 : }
126 0 : return bSuccess;
127 : }
128 :
129 495 : bool JobData::getStreamBuffer( void*& pData, int& bytes )
130 : {
131 : // consistency checks
132 495 : if( ! m_pParser )
133 0 : m_pParser = m_aContext.getParser();
134 990 : if( m_pParser != m_aContext.getParser() ||
135 495 : ! m_pParser )
136 0 : return false;
137 :
138 495 : SvMemoryStream aStream;
139 :
140 : // write header job data
141 495 : aStream.WriteLine(OString("JobData 1"));
142 :
143 990 : OStringBuffer aLine;
144 :
145 495 : aLine.append("printer=");
146 495 : aLine.append(OUStringToOString(m_aPrinterName, RTL_TEXTENCODING_UTF8));
147 495 : aStream.WriteLine(aLine.makeStringAndClear());
148 :
149 495 : aLine.append("orientation=");
150 495 : if (m_eOrientation == orientation::Landscape)
151 0 : aLine.append("Landscape");
152 : else
153 495 : aLine.append("Portrait");
154 495 : aStream.WriteLine(aLine.makeStringAndClear());
155 :
156 495 : aLine.append("copies=");
157 495 : aLine.append(static_cast<sal_Int32>(m_nCopies));
158 495 : aStream.WriteLine(aLine.makeStringAndClear());
159 :
160 495 : if (m_nPDFDevice > 0)
161 : {
162 0 : aLine.append("collate=");
163 0 : aLine.append(OString::boolean(m_bCollate));
164 0 : aStream.WriteLine(aLine.makeStringAndClear());
165 : }
166 :
167 495 : aLine.append("margindajustment=");
168 495 : aLine.append(static_cast<sal_Int32>(m_nLeftMarginAdjust));
169 495 : aLine.append(',');
170 495 : aLine.append(static_cast<sal_Int32>(m_nRightMarginAdjust));
171 495 : aLine.append(',');
172 495 : aLine.append(static_cast<sal_Int32>(m_nTopMarginAdjust));
173 495 : aLine.append(',');
174 495 : aLine.append(static_cast<sal_Int32>(m_nBottomMarginAdjust));
175 495 : aStream.WriteLine(aLine.makeStringAndClear());
176 :
177 495 : aLine.append("colordepth=");
178 495 : aLine.append(static_cast<sal_Int32>(m_nColorDepth));
179 495 : aStream.WriteLine(aLine.makeStringAndClear());
180 :
181 495 : aLine.append("pslevel=");
182 495 : aLine.append(static_cast<sal_Int32>(m_nPSLevel));
183 495 : aStream.WriteLine(aLine.makeStringAndClear());
184 :
185 495 : aLine.append("pdfdevice=");
186 495 : aLine.append(static_cast<sal_Int32>(m_nPDFDevice));
187 495 : aStream.WriteLine(aLine.makeStringAndClear());
188 :
189 495 : aLine.append("colordevice=");
190 495 : aLine.append(static_cast<sal_Int32>(m_nColorDevice));
191 495 : aStream.WriteLine(aLine.makeStringAndClear());
192 :
193 : // now append the PPDContext stream buffer
194 495 : aStream.WriteLine( "PPDContexData" );
195 : sal_uLong nBytes;
196 990 : boost::scoped_array<char> pContextBuffer(m_aContext.getStreamableBuffer( nBytes ));
197 495 : if( nBytes )
198 495 : aStream.Write( pContextBuffer.get(), nBytes );
199 495 : pContextBuffer.reset();
200 :
201 : // success
202 495 : pData = rtl_allocateMemory( bytes = aStream.Tell() );
203 495 : memcpy( pData, aStream.GetData(), bytes );
204 990 : return true;
205 : }
206 :
207 560 : bool JobData::constructFromStreamBuffer( void* pData, int bytes, JobData& rJobData )
208 : {
209 560 : SvMemoryStream aStream( pData, bytes, StreamMode::READ );
210 1120 : OString aLine;
211 560 : bool bVersion = false;
212 560 : bool bPrinter = false;
213 560 : bool bOrientation = false;
214 560 : bool bCopies = false;
215 560 : bool bContext = false;
216 560 : bool bMargin = false;
217 560 : bool bColorDepth = false;
218 560 : bool bColorDevice = false;
219 560 : bool bPSLevel = false;
220 560 : bool bPDFDevice = false;
221 :
222 560 : const char printerEquals[] = "printer=";
223 560 : const char orientatationEquals[] = "orientation=";
224 560 : const char copiesEquals[] = "copies=";
225 560 : const char collateEquals[] = "collate=";
226 560 : const char margindajustmentEquals[] = "margindajustment=";
227 560 : const char colordepthEquals[] = "colordepth=";
228 560 : const char colordeviceEquals[] = "colordevice=";
229 560 : const char pslevelEquals[] = "pslevel=";
230 560 : const char pdfdeviceEquals[] = "pdfdevice=";
231 :
232 7279 : while( ! aStream.IsEof() )
233 : {
234 6159 : aStream.ReadLine( aLine );
235 6159 : if (aLine.startsWith("JobData"))
236 560 : bVersion = true;
237 5599 : else if (aLine.startsWith(printerEquals))
238 : {
239 560 : bPrinter = true;
240 560 : rJobData.m_aPrinterName = OStringToOUString(aLine.copy(RTL_CONSTASCII_LENGTH(printerEquals)), RTL_TEXTENCODING_UTF8);
241 : }
242 5039 : else if (aLine.startsWith(orientatationEquals))
243 : {
244 560 : bOrientation = true;
245 560 : rJobData.m_eOrientation = aLine.copy(RTL_CONSTASCII_LENGTH(orientatationEquals)).equalsIgnoreAsciiCase("landscape") ? orientation::Landscape : orientation::Portrait;
246 : }
247 4479 : else if (aLine.startsWith(copiesEquals))
248 : {
249 560 : bCopies = true;
250 560 : rJobData.m_nCopies = aLine.copy(RTL_CONSTASCII_LENGTH(copiesEquals)).toInt32();
251 : }
252 3919 : else if (aLine.startsWith(collateEquals))
253 : {
254 0 : rJobData.m_bCollate = aLine.copy(RTL_CONSTASCII_LENGTH(collateEquals)).toInt32();
255 : }
256 3919 : else if (aLine.startsWith(margindajustmentEquals))
257 : {
258 560 : bMargin = true;
259 560 : OString aValues(aLine.copy(RTL_CONSTASCII_LENGTH(margindajustmentEquals)));
260 560 : rJobData.m_nLeftMarginAdjust = aValues.getToken(0, ',').toInt32();
261 560 : rJobData.m_nRightMarginAdjust = aValues.getToken(1, ',').toInt32();
262 560 : rJobData.m_nTopMarginAdjust = aValues.getToken(2, ',').toInt32();
263 560 : rJobData.m_nBottomMarginAdjust = aValues.getToken(3, ',').toInt32();
264 : }
265 3359 : else if (aLine.startsWith(colordepthEquals))
266 : {
267 560 : bColorDepth = true;
268 560 : rJobData.m_nColorDepth = aLine.copy(RTL_CONSTASCII_LENGTH(colordepthEquals)).toInt32();
269 : }
270 2799 : else if (aLine.startsWith(colordeviceEquals))
271 : {
272 560 : bColorDevice = true;
273 560 : rJobData.m_nColorDevice = aLine.copy(RTL_CONSTASCII_LENGTH(colordeviceEquals)).toInt32();
274 : }
275 2239 : else if (aLine.startsWith(pslevelEquals))
276 : {
277 560 : bPSLevel = true;
278 560 : rJobData.m_nPSLevel = aLine.copy(RTL_CONSTASCII_LENGTH(pslevelEquals)).toInt32();
279 : }
280 1679 : else if (aLine.startsWith(pdfdeviceEquals))
281 : {
282 559 : bPDFDevice = true;
283 559 : rJobData.m_nPDFDevice = aLine.copy(RTL_CONSTASCII_LENGTH(pdfdeviceEquals)).toInt32();
284 : }
285 1120 : else if (aLine == "PPDContexData")
286 : {
287 560 : if( bPrinter )
288 : {
289 560 : PrinterInfoManager& rManager = PrinterInfoManager::get();
290 560 : const PrinterInfo& rInfo = rManager.getPrinterInfo( rJobData.m_aPrinterName );
291 560 : rJobData.m_pParser = PPDParser::getParser( rInfo.m_aDriverName );
292 560 : if( rJobData.m_pParser )
293 : {
294 560 : rJobData.m_aContext.setParser( rJobData.m_pParser );
295 560 : int nBytes = bytes - aStream.Tell();
296 560 : char* pRemain = static_cast<char*>(alloca( bytes - aStream.Tell() ));
297 560 : aStream.Read( pRemain, nBytes );
298 560 : rJobData.m_aContext.rebuildFromStreamBuffer( pRemain, nBytes );
299 560 : bContext = true;
300 : }
301 : }
302 : }
303 : }
304 :
305 1120 : return bVersion && bPrinter && bOrientation && bCopies && bContext && bMargin && bPSLevel && bPDFDevice && bColorDevice && bColorDepth;
306 : }
307 :
308 0 : void JobData::resolveDefaultBackend()
309 : {
310 0 : if (m_nPSLevel == 0 && m_nPDFDevice == 0)
311 0 : setDefaultBackend(officecfg::Office::Common::Print::Option::Printer::PDFAsStandardPrintJobFormat::get());
312 0 : }
313 :
314 0 : void JobData::setDefaultBackend(bool bUsePDF)
315 : {
316 0 : if (bUsePDF && m_nPSLevel == 0 && m_nPDFDevice == 0)
317 0 : m_nPDFDevice = 1;
318 0 : }
319 :
320 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|