xref: /aoo41x/main/vcl/unx/generic/printer/jobdata.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
30 
31 #include "vcl/jobdata.hxx"
32 #include "vcl/printerinfomanager.hxx"
33 
34 #include "tools/stream.hxx"
35 
36 #include "sal/alloca.h"
37 
38 using namespace psp;
39 using namespace rtl;
40 
41 JobData& JobData::operator=(const JobData& rRight)
42 {
43     m_nCopies				= rRight.m_nCopies;
44     m_nLeftMarginAdjust		= rRight.m_nLeftMarginAdjust;
45     m_nRightMarginAdjust	= rRight.m_nRightMarginAdjust;
46     m_nTopMarginAdjust		= rRight.m_nTopMarginAdjust;
47     m_nBottomMarginAdjust	= rRight.m_nBottomMarginAdjust;
48     m_nColorDepth			= rRight.m_nColorDepth;
49     m_eOrientation			= rRight.m_eOrientation;
50     m_aPrinterName			= rRight.m_aPrinterName;
51     m_pParser				= rRight.m_pParser;
52     m_aContext				= rRight.m_aContext;
53     m_nPSLevel				= rRight.m_nPSLevel;
54     m_nPDFDevice            = rRight.m_nPDFDevice;
55     m_nColorDevice			= rRight.m_nColorDevice;
56 
57     if( ! m_pParser && m_aPrinterName.getLength() )
58     {
59         PrinterInfoManager& rMgr = PrinterInfoManager::get();
60         rMgr.setupJobContextData( *this );
61     }
62     return *this;
63 }
64 
65 void JobData::setCollate( bool bCollate )
66 {
67     const PPDParser* pParser = m_aContext.getParser();
68     if( pParser )
69     {
70         const PPDKey* pKey = pParser->getKey( String( RTL_CONSTASCII_USTRINGPARAM( "Collate" ) ) );
71         if( pKey )
72         {
73             const PPDValue* pVal = NULL;
74             if( bCollate )
75                 pVal = pKey->getValue( String( RTL_CONSTASCII_USTRINGPARAM( "True" ) ) );
76             else
77             {
78                 pVal = pKey->getValue( String( RTL_CONSTASCII_USTRINGPARAM( "False" ) ) );
79                 if( ! pVal )
80                     pVal = pKey->getValue( String( RTL_CONSTASCII_USTRINGPARAM( "None" ) ) );
81             }
82             m_aContext.setValue( pKey, pVal );
83         }
84     }
85 }
86 
87 bool JobData::setPaper( int i_nWidth, int i_nHeight )
88 {
89     bool bSuccess = false;
90     if( m_pParser )
91     {
92         rtl::OUString aPaper( m_pParser->matchPaper( i_nWidth, i_nHeight ) );
93 
94         const PPDKey*   pKey = m_pParser->getKey( String( RTL_CONSTASCII_USTRINGPARAM( "PageSize" ) ) );
95         const PPDValue* pValue = pKey ? pKey->getValueCaseInsensitive( aPaper ) : NULL;
96 
97         bSuccess = pKey && pValue && m_aContext.setValue( pKey, pValue, false );
98     }
99     return bSuccess;
100 }
101 
102 bool JobData::setPaperBin( int i_nPaperBin )
103 {
104     bool bSuccess = false;
105     if( m_pParser )
106     {
107         const PPDKey*   pKey = m_pParser->getKey( String( RTL_CONSTASCII_USTRINGPARAM( "InputSlot" ) ) );
108         const PPDValue* pValue = pKey ? pKey->getValue( i_nPaperBin ) : NULL;
109 
110         bSuccess = pKey && pValue && m_aContext.setValue( pKey, pValue, false );
111     }
112     return bSuccess;
113 }
114 
115 bool JobData::getStreamBuffer( void*& pData, int& bytes )
116 {
117     // consistency checks
118     if( ! m_pParser )
119         m_pParser = m_aContext.getParser();
120     if( m_pParser != m_aContext.getParser() ||
121         ! m_pParser )
122         return false;
123 
124     SvMemoryStream aStream;
125     ByteString aLine;
126 
127     // write header job data
128     aStream.WriteLine( "JobData 1" );
129 
130     aLine = "printer=";
131     aLine += ByteString( String( m_aPrinterName ), RTL_TEXTENCODING_UTF8 );
132     aStream.WriteLine( aLine );
133 
134     aLine = "orientation=";
135     aLine += m_eOrientation == orientation::Landscape ? "Landscape" : "Portrait";
136     aStream.WriteLine( aLine );
137 
138     aLine = "copies=";
139     aLine += ByteString::CreateFromInt32( m_nCopies );
140     aStream.WriteLine( aLine );
141 
142     aLine = "margindajustment=";
143     aLine += ByteString::CreateFromInt32( m_nLeftMarginAdjust );
144     aLine += ',';
145     aLine += ByteString::CreateFromInt32( m_nRightMarginAdjust );
146     aLine += ',';
147     aLine += ByteString::CreateFromInt32( m_nTopMarginAdjust );
148     aLine += ',';
149     aLine += ByteString::CreateFromInt32( m_nBottomMarginAdjust );
150     aStream.WriteLine( aLine );
151 
152     aLine = "colordepth=";
153     aLine += ByteString::CreateFromInt32( m_nColorDepth );
154     aStream.WriteLine( aLine );
155 
156     aLine = "pslevel=";
157     aLine += ByteString::CreateFromInt32( m_nPSLevel );
158     aStream.WriteLine( aLine );
159 
160     aLine = "pdfdevice=";
161     aLine += ByteString::CreateFromInt32( m_nPDFDevice );
162     aStream.WriteLine( aLine );
163 
164     aLine = "colordevice=";
165     aLine += ByteString::CreateFromInt32( m_nColorDevice );
166     aStream.WriteLine( aLine );
167 
168     // now append the PPDContext stream buffer
169     aStream.WriteLine( "PPDContexData" );
170     sal_uLong nBytes;
171     void* pContextBuffer = m_aContext.getStreamableBuffer( nBytes );
172     if( nBytes )
173         aStream.Write( pContextBuffer, nBytes );
174 
175     // success
176     pData = rtl_allocateMemory( bytes = aStream.Tell() );
177     memcpy( pData, aStream.GetData(), bytes );
178     return true;
179 }
180 
181 bool JobData::constructFromStreamBuffer( void* pData, int bytes, JobData& rJobData )
182 {
183     SvMemoryStream aStream( pData, bytes, STREAM_READ );
184     ByteString aLine;
185     bool bVersion       = false;
186     bool bPrinter       = false;
187     bool bOrientation   = false;
188     bool bCopies        = false;
189     bool bContext       = false;
190     bool bMargin        = false;
191     bool bColorDepth    = false;
192     bool bColorDevice   = false;
193     bool bPSLevel       = false;
194     bool bPDFDevice     = false;
195     while( ! aStream.IsEof() )
196     {
197         aStream.ReadLine( aLine );
198         if( aLine.CompareTo( "JobData", 7 ) == COMPARE_EQUAL )
199             bVersion = true;
200         else if( aLine.CompareTo( "printer=", 8 ) == COMPARE_EQUAL )
201         {
202             bPrinter = true;
203             rJobData.m_aPrinterName = String( aLine.Copy( 8 ), RTL_TEXTENCODING_UTF8 );
204         }
205         else if( aLine.CompareTo( "orientation=", 12 ) == COMPARE_EQUAL )
206         {
207             bOrientation = true;
208             rJobData.m_eOrientation = aLine.Copy( 12 ).EqualsIgnoreCaseAscii( "landscape" ) ? orientation::Landscape : orientation::Portrait;
209         }
210         else if( aLine.CompareTo( "copies=", 7 ) == COMPARE_EQUAL )
211         {
212             bCopies = true;
213             rJobData.m_nCopies = aLine.Copy( 7 ).ToInt32();
214         }
215         else if( aLine.CompareTo( "margindajustment=",17 ) == COMPARE_EQUAL )
216         {
217             bMargin = true;
218             ByteString aValues( aLine.Copy( 17 ) );
219             rJobData.m_nLeftMarginAdjust = aValues.GetToken( 0, ',' ).ToInt32();
220             rJobData.m_nRightMarginAdjust = aValues.GetToken( 1, ',' ).ToInt32();
221             rJobData.m_nTopMarginAdjust = aValues.GetToken( 2, ',' ).ToInt32();
222             rJobData.m_nBottomMarginAdjust = aValues.GetToken( 3, ',' ).ToInt32();
223         }
224         else if( aLine.CompareTo( "colordepth=", 11 ) == COMPARE_EQUAL )
225         {
226             bColorDepth = true;
227             rJobData.m_nColorDepth = aLine.Copy( 11 ).ToInt32();
228         }
229         else if( aLine.CompareTo( "colordevice=", 12 ) == COMPARE_EQUAL )
230         {
231             bColorDevice = true;
232             rJobData.m_nColorDevice = aLine.Copy( 12 ).ToInt32();
233         }
234         else if( aLine.CompareTo( "pslevel=", 8 ) == COMPARE_EQUAL )
235         {
236             bPSLevel = true;
237             rJobData.m_nPSLevel = aLine.Copy( 8 ).ToInt32();
238         }
239         else if( aLine.CompareTo( "pdfdevice=", 10 ) == COMPARE_EQUAL )
240         {
241             bPDFDevice = true;
242             rJobData.m_nPDFDevice = aLine.Copy( 10 ).ToInt32();
243         }
244         else if( aLine.Equals( "PPDContexData" ) )
245         {
246             if( bPrinter )
247             {
248                 PrinterInfoManager& rManager = PrinterInfoManager::get();
249                 const PrinterInfo& rInfo = rManager.getPrinterInfo( rJobData.m_aPrinterName );
250                 rJobData.m_pParser = PPDParser::getParser( rInfo.m_aDriverName );
251                 if( rJobData.m_pParser )
252                 {
253                     rJobData.m_aContext.setParser( rJobData.m_pParser );
254                     int nBytes = bytes - aStream.Tell();
255                     void* pRemain = alloca( bytes - aStream.Tell() );
256                     aStream.Read( pRemain, nBytes );
257                     rJobData.m_aContext.rebuildFromStreamBuffer( pRemain, nBytes );
258                     bContext = true;
259                 }
260             }
261         }
262     }
263 
264     return bVersion && bPrinter && bOrientation && bCopies && bContext && bMargin && bPSLevel && bPDFDevice && bColorDevice && bColorDepth;
265 }
266