xref: /aoo41x/main/vcl/unx/generic/printer/jobdata.cxx (revision c82f2877)
1*c82f2877SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*c82f2877SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*c82f2877SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*c82f2877SAndrew Rist  * distributed with this work for additional information
6*c82f2877SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*c82f2877SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*c82f2877SAndrew Rist  * "License"); you may not use this file except in compliance
9*c82f2877SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c82f2877SAndrew Rist  *
11*c82f2877SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c82f2877SAndrew Rist  *
13*c82f2877SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*c82f2877SAndrew Rist  * software distributed under the License is distributed on an
15*c82f2877SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c82f2877SAndrew Rist  * KIND, either express or implied.  See the License for the
17*c82f2877SAndrew Rist  * specific language governing permissions and limitations
18*c82f2877SAndrew Rist  * under the License.
19*c82f2877SAndrew Rist  *
20*c82f2877SAndrew Rist  *************************************************************/
21*c82f2877SAndrew Rist 
22*c82f2877SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_vcl.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "vcl/jobdata.hxx"
28cdf0e10cSrcweir #include "vcl/printerinfomanager.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include "tools/stream.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include "sal/alloca.h"
33cdf0e10cSrcweir 
34cdf0e10cSrcweir using namespace psp;
35cdf0e10cSrcweir using namespace rtl;
36cdf0e10cSrcweir 
operator =(const JobData & rRight)37cdf0e10cSrcweir JobData& JobData::operator=(const JobData& rRight)
38cdf0e10cSrcweir {
39cdf0e10cSrcweir     m_nCopies				= rRight.m_nCopies;
40cdf0e10cSrcweir     m_nLeftMarginAdjust		= rRight.m_nLeftMarginAdjust;
41cdf0e10cSrcweir     m_nRightMarginAdjust	= rRight.m_nRightMarginAdjust;
42cdf0e10cSrcweir     m_nTopMarginAdjust		= rRight.m_nTopMarginAdjust;
43cdf0e10cSrcweir     m_nBottomMarginAdjust	= rRight.m_nBottomMarginAdjust;
44cdf0e10cSrcweir     m_nColorDepth			= rRight.m_nColorDepth;
45cdf0e10cSrcweir     m_eOrientation			= rRight.m_eOrientation;
46cdf0e10cSrcweir     m_aPrinterName			= rRight.m_aPrinterName;
47cdf0e10cSrcweir     m_pParser				= rRight.m_pParser;
48cdf0e10cSrcweir     m_aContext				= rRight.m_aContext;
49cdf0e10cSrcweir     m_nPSLevel				= rRight.m_nPSLevel;
50cdf0e10cSrcweir     m_nPDFDevice            = rRight.m_nPDFDevice;
51cdf0e10cSrcweir     m_nColorDevice			= rRight.m_nColorDevice;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir     if( ! m_pParser && m_aPrinterName.getLength() )
54cdf0e10cSrcweir     {
55cdf0e10cSrcweir         PrinterInfoManager& rMgr = PrinterInfoManager::get();
56cdf0e10cSrcweir         rMgr.setupJobContextData( *this );
57cdf0e10cSrcweir     }
58cdf0e10cSrcweir     return *this;
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
setCollate(bool bCollate)61cdf0e10cSrcweir void JobData::setCollate( bool bCollate )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     const PPDParser* pParser = m_aContext.getParser();
64cdf0e10cSrcweir     if( pParser )
65cdf0e10cSrcweir     {
66cdf0e10cSrcweir         const PPDKey* pKey = pParser->getKey( String( RTL_CONSTASCII_USTRINGPARAM( "Collate" ) ) );
67cdf0e10cSrcweir         if( pKey )
68cdf0e10cSrcweir         {
69cdf0e10cSrcweir             const PPDValue* pVal = NULL;
70cdf0e10cSrcweir             if( bCollate )
71cdf0e10cSrcweir                 pVal = pKey->getValue( String( RTL_CONSTASCII_USTRINGPARAM( "True" ) ) );
72cdf0e10cSrcweir             else
73cdf0e10cSrcweir             {
74cdf0e10cSrcweir                 pVal = pKey->getValue( String( RTL_CONSTASCII_USTRINGPARAM( "False" ) ) );
75cdf0e10cSrcweir                 if( ! pVal )
76cdf0e10cSrcweir                     pVal = pKey->getValue( String( RTL_CONSTASCII_USTRINGPARAM( "None" ) ) );
77cdf0e10cSrcweir             }
78cdf0e10cSrcweir             m_aContext.setValue( pKey, pVal );
79cdf0e10cSrcweir         }
80cdf0e10cSrcweir     }
81cdf0e10cSrcweir }
82cdf0e10cSrcweir 
setPaper(int i_nWidth,int i_nHeight)83cdf0e10cSrcweir bool JobData::setPaper( int i_nWidth, int i_nHeight )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir     bool bSuccess = false;
86cdf0e10cSrcweir     if( m_pParser )
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir         rtl::OUString aPaper( m_pParser->matchPaper( i_nWidth, i_nHeight ) );
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         const PPDKey*   pKey = m_pParser->getKey( String( RTL_CONSTASCII_USTRINGPARAM( "PageSize" ) ) );
91cdf0e10cSrcweir         const PPDValue* pValue = pKey ? pKey->getValueCaseInsensitive( aPaper ) : NULL;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir         bSuccess = pKey && pValue && m_aContext.setValue( pKey, pValue, false );
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir     return bSuccess;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
setPaperBin(int i_nPaperBin)98cdf0e10cSrcweir bool JobData::setPaperBin( int i_nPaperBin )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir     bool bSuccess = false;
101cdf0e10cSrcweir     if( m_pParser )
102cdf0e10cSrcweir     {
103cdf0e10cSrcweir         const PPDKey*   pKey = m_pParser->getKey( String( RTL_CONSTASCII_USTRINGPARAM( "InputSlot" ) ) );
104cdf0e10cSrcweir         const PPDValue* pValue = pKey ? pKey->getValue( i_nPaperBin ) : NULL;
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         bSuccess = pKey && pValue && m_aContext.setValue( pKey, pValue, false );
107cdf0e10cSrcweir     }
108cdf0e10cSrcweir     return bSuccess;
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
getStreamBuffer(void * & pData,int & bytes)111cdf0e10cSrcweir bool JobData::getStreamBuffer( void*& pData, int& bytes )
112cdf0e10cSrcweir {
113cdf0e10cSrcweir     // consistency checks
114cdf0e10cSrcweir     if( ! m_pParser )
115cdf0e10cSrcweir         m_pParser = m_aContext.getParser();
116cdf0e10cSrcweir     if( m_pParser != m_aContext.getParser() ||
117cdf0e10cSrcweir         ! m_pParser )
118cdf0e10cSrcweir         return false;
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     SvMemoryStream aStream;
121cdf0e10cSrcweir     ByteString aLine;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     // write header job data
124cdf0e10cSrcweir     aStream.WriteLine( "JobData 1" );
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     aLine = "printer=";
127cdf0e10cSrcweir     aLine += ByteString( String( m_aPrinterName ), RTL_TEXTENCODING_UTF8 );
128cdf0e10cSrcweir     aStream.WriteLine( aLine );
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     aLine = "orientation=";
131cdf0e10cSrcweir     aLine += m_eOrientation == orientation::Landscape ? "Landscape" : "Portrait";
132cdf0e10cSrcweir     aStream.WriteLine( aLine );
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     aLine = "copies=";
135cdf0e10cSrcweir     aLine += ByteString::CreateFromInt32( m_nCopies );
136cdf0e10cSrcweir     aStream.WriteLine( aLine );
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     aLine = "margindajustment=";
139cdf0e10cSrcweir     aLine += ByteString::CreateFromInt32( m_nLeftMarginAdjust );
140cdf0e10cSrcweir     aLine += ',';
141cdf0e10cSrcweir     aLine += ByteString::CreateFromInt32( m_nRightMarginAdjust );
142cdf0e10cSrcweir     aLine += ',';
143cdf0e10cSrcweir     aLine += ByteString::CreateFromInt32( m_nTopMarginAdjust );
144cdf0e10cSrcweir     aLine += ',';
145cdf0e10cSrcweir     aLine += ByteString::CreateFromInt32( m_nBottomMarginAdjust );
146cdf0e10cSrcweir     aStream.WriteLine( aLine );
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     aLine = "colordepth=";
149cdf0e10cSrcweir     aLine += ByteString::CreateFromInt32( m_nColorDepth );
150cdf0e10cSrcweir     aStream.WriteLine( aLine );
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     aLine = "pslevel=";
153cdf0e10cSrcweir     aLine += ByteString::CreateFromInt32( m_nPSLevel );
154cdf0e10cSrcweir     aStream.WriteLine( aLine );
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     aLine = "pdfdevice=";
157cdf0e10cSrcweir     aLine += ByteString::CreateFromInt32( m_nPDFDevice );
158cdf0e10cSrcweir     aStream.WriteLine( aLine );
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     aLine = "colordevice=";
161cdf0e10cSrcweir     aLine += ByteString::CreateFromInt32( m_nColorDevice );
162cdf0e10cSrcweir     aStream.WriteLine( aLine );
163cdf0e10cSrcweir 
164cdf0e10cSrcweir     // now append the PPDContext stream buffer
165cdf0e10cSrcweir     aStream.WriteLine( "PPDContexData" );
166cdf0e10cSrcweir     sal_uLong nBytes;
167cdf0e10cSrcweir     void* pContextBuffer = m_aContext.getStreamableBuffer( nBytes );
168cdf0e10cSrcweir     if( nBytes )
169cdf0e10cSrcweir         aStream.Write( pContextBuffer, nBytes );
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     // success
172cdf0e10cSrcweir     pData = rtl_allocateMemory( bytes = aStream.Tell() );
173cdf0e10cSrcweir     memcpy( pData, aStream.GetData(), bytes );
174cdf0e10cSrcweir     return true;
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
constructFromStreamBuffer(void * pData,int bytes,JobData & rJobData)177cdf0e10cSrcweir bool JobData::constructFromStreamBuffer( void* pData, int bytes, JobData& rJobData )
178cdf0e10cSrcweir {
179cdf0e10cSrcweir     SvMemoryStream aStream( pData, bytes, STREAM_READ );
180cdf0e10cSrcweir     ByteString aLine;
181cdf0e10cSrcweir     bool bVersion       = false;
182cdf0e10cSrcweir     bool bPrinter       = false;
183cdf0e10cSrcweir     bool bOrientation   = false;
184cdf0e10cSrcweir     bool bCopies        = false;
185cdf0e10cSrcweir     bool bContext       = false;
186cdf0e10cSrcweir     bool bMargin        = false;
187cdf0e10cSrcweir     bool bColorDepth    = false;
188cdf0e10cSrcweir     bool bColorDevice   = false;
189cdf0e10cSrcweir     bool bPSLevel       = false;
190cdf0e10cSrcweir     bool bPDFDevice     = false;
191cdf0e10cSrcweir     while( ! aStream.IsEof() )
192cdf0e10cSrcweir     {
193cdf0e10cSrcweir         aStream.ReadLine( aLine );
194cdf0e10cSrcweir         if( aLine.CompareTo( "JobData", 7 ) == COMPARE_EQUAL )
195cdf0e10cSrcweir             bVersion = true;
196cdf0e10cSrcweir         else if( aLine.CompareTo( "printer=", 8 ) == COMPARE_EQUAL )
197cdf0e10cSrcweir         {
198cdf0e10cSrcweir             bPrinter = true;
199cdf0e10cSrcweir             rJobData.m_aPrinterName = String( aLine.Copy( 8 ), RTL_TEXTENCODING_UTF8 );
200cdf0e10cSrcweir         }
201cdf0e10cSrcweir         else if( aLine.CompareTo( "orientation=", 12 ) == COMPARE_EQUAL )
202cdf0e10cSrcweir         {
203cdf0e10cSrcweir             bOrientation = true;
204cdf0e10cSrcweir             rJobData.m_eOrientation = aLine.Copy( 12 ).EqualsIgnoreCaseAscii( "landscape" ) ? orientation::Landscape : orientation::Portrait;
205cdf0e10cSrcweir         }
206cdf0e10cSrcweir         else if( aLine.CompareTo( "copies=", 7 ) == COMPARE_EQUAL )
207cdf0e10cSrcweir         {
208cdf0e10cSrcweir             bCopies = true;
209cdf0e10cSrcweir             rJobData.m_nCopies = aLine.Copy( 7 ).ToInt32();
210cdf0e10cSrcweir         }
211cdf0e10cSrcweir         else if( aLine.CompareTo( "margindajustment=",17 ) == COMPARE_EQUAL )
212cdf0e10cSrcweir         {
213cdf0e10cSrcweir             bMargin = true;
214cdf0e10cSrcweir             ByteString aValues( aLine.Copy( 17 ) );
215cdf0e10cSrcweir             rJobData.m_nLeftMarginAdjust = aValues.GetToken( 0, ',' ).ToInt32();
216cdf0e10cSrcweir             rJobData.m_nRightMarginAdjust = aValues.GetToken( 1, ',' ).ToInt32();
217cdf0e10cSrcweir             rJobData.m_nTopMarginAdjust = aValues.GetToken( 2, ',' ).ToInt32();
218cdf0e10cSrcweir             rJobData.m_nBottomMarginAdjust = aValues.GetToken( 3, ',' ).ToInt32();
219cdf0e10cSrcweir         }
220cdf0e10cSrcweir         else if( aLine.CompareTo( "colordepth=", 11 ) == COMPARE_EQUAL )
221cdf0e10cSrcweir         {
222cdf0e10cSrcweir             bColorDepth = true;
223cdf0e10cSrcweir             rJobData.m_nColorDepth = aLine.Copy( 11 ).ToInt32();
224cdf0e10cSrcweir         }
225cdf0e10cSrcweir         else if( aLine.CompareTo( "colordevice=", 12 ) == COMPARE_EQUAL )
226cdf0e10cSrcweir         {
227cdf0e10cSrcweir             bColorDevice = true;
228cdf0e10cSrcweir             rJobData.m_nColorDevice = aLine.Copy( 12 ).ToInt32();
229cdf0e10cSrcweir         }
230cdf0e10cSrcweir         else if( aLine.CompareTo( "pslevel=", 8 ) == COMPARE_EQUAL )
231cdf0e10cSrcweir         {
232cdf0e10cSrcweir             bPSLevel = true;
233cdf0e10cSrcweir             rJobData.m_nPSLevel = aLine.Copy( 8 ).ToInt32();
234cdf0e10cSrcweir         }
235cdf0e10cSrcweir         else if( aLine.CompareTo( "pdfdevice=", 10 ) == COMPARE_EQUAL )
236cdf0e10cSrcweir         {
237cdf0e10cSrcweir             bPDFDevice = true;
238cdf0e10cSrcweir             rJobData.m_nPDFDevice = aLine.Copy( 10 ).ToInt32();
239cdf0e10cSrcweir         }
240cdf0e10cSrcweir         else if( aLine.Equals( "PPDContexData" ) )
241cdf0e10cSrcweir         {
242cdf0e10cSrcweir             if( bPrinter )
243cdf0e10cSrcweir             {
244cdf0e10cSrcweir                 PrinterInfoManager& rManager = PrinterInfoManager::get();
245cdf0e10cSrcweir                 const PrinterInfo& rInfo = rManager.getPrinterInfo( rJobData.m_aPrinterName );
246cdf0e10cSrcweir                 rJobData.m_pParser = PPDParser::getParser( rInfo.m_aDriverName );
247cdf0e10cSrcweir                 if( rJobData.m_pParser )
248cdf0e10cSrcweir                 {
249cdf0e10cSrcweir                     rJobData.m_aContext.setParser( rJobData.m_pParser );
250cdf0e10cSrcweir                     int nBytes = bytes - aStream.Tell();
251cdf0e10cSrcweir                     void* pRemain = alloca( bytes - aStream.Tell() );
252cdf0e10cSrcweir                     aStream.Read( pRemain, nBytes );
253cdf0e10cSrcweir                     rJobData.m_aContext.rebuildFromStreamBuffer( pRemain, nBytes );
254cdf0e10cSrcweir                     bContext = true;
255cdf0e10cSrcweir                 }
256cdf0e10cSrcweir             }
257cdf0e10cSrcweir         }
258cdf0e10cSrcweir     }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir     return bVersion && bPrinter && bOrientation && bCopies && bContext && bMargin && bPSLevel && bPDFDevice && bColorDevice && bColorDepth;
261cdf0e10cSrcweir }
262