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