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