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