sync master with lastest vba changes
[ooovba.git] / psprint / source / printer / jobdata.cxx
blobdfe980bed7359ba3a1d7568d2aae546ea5fa8266
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: jobdata.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_psprint.hxx"
34 #include <psprint/jobdata.hxx>
35 #include <psprint/printerinfomanager.hxx>
36 #include <tools/stream.hxx>
37 #include <sal/alloca.h>
39 using namespace psp;
40 using namespace rtl;
42 JobData& JobData::operator=(const JobData& rRight)
44 m_nCopies = rRight.m_nCopies;
45 m_nLeftMarginAdjust = rRight.m_nLeftMarginAdjust;
46 m_nRightMarginAdjust = rRight.m_nRightMarginAdjust;
47 m_nTopMarginAdjust = rRight.m_nTopMarginAdjust;
48 m_nBottomMarginAdjust = rRight.m_nBottomMarginAdjust;
49 m_nColorDepth = rRight.m_nColorDepth;
50 m_eOrientation = rRight.m_eOrientation;
51 m_aPrinterName = rRight.m_aPrinterName;
52 m_pParser = rRight.m_pParser;
53 m_aContext = rRight.m_aContext;
54 m_nPSLevel = rRight.m_nPSLevel;
55 m_nColorDevice = rRight.m_nColorDevice;
57 if( ! m_pParser && m_aPrinterName.getLength() )
59 PrinterInfoManager& rMgr = PrinterInfoManager::get();
60 rMgr.setupJobContextData( *this );
62 return *this;
65 bool JobData::getStreamBuffer( void*& pData, int& bytes )
67 // consistency checks
68 if( ! m_pParser )
69 m_pParser = m_aContext.getParser();
70 if( m_pParser != m_aContext.getParser() ||
71 ! m_pParser )
72 return false;
74 SvMemoryStream aStream;
75 ByteString aLine;
77 // write header job data
78 aStream.WriteLine( "JobData 1" );
80 aLine = "printer=";
81 aLine += ByteString( String( m_aPrinterName ), RTL_TEXTENCODING_UTF8 );
82 aStream.WriteLine( aLine );
84 aLine = "orientation=";
85 aLine += m_eOrientation == orientation::Landscape ? "Landscape" : "Portrait";
86 aStream.WriteLine( aLine );
88 aLine = "copies=";
89 aLine += ByteString::CreateFromInt32( m_nCopies );
90 aStream.WriteLine( aLine );
92 aLine = "margindajustment=";
93 aLine += ByteString::CreateFromInt32( m_nLeftMarginAdjust );
94 aLine += ',';
95 aLine += ByteString::CreateFromInt32( m_nRightMarginAdjust );
96 aLine += ',';
97 aLine += ByteString::CreateFromInt32( m_nTopMarginAdjust );
98 aLine += ',';
99 aLine += ByteString::CreateFromInt32( m_nBottomMarginAdjust );
100 aStream.WriteLine( aLine );
102 aLine = "colordepth=";
103 aLine += ByteString::CreateFromInt32( m_nColorDepth );
104 aStream.WriteLine( aLine );
106 aLine = "pslevel=";
107 aLine += ByteString::CreateFromInt32( m_nPSLevel );
108 aStream.WriteLine( aLine );
110 aLine = "colordevice=";
111 aLine += ByteString::CreateFromInt32( m_nColorDevice );
112 aStream.WriteLine( aLine );
114 // now append the PPDContext stream buffer
115 aStream.WriteLine( "PPDContexData" );
116 ULONG nBytes;
117 void* pContextBuffer = m_aContext.getStreamableBuffer( nBytes );
118 if( nBytes )
119 aStream.Write( pContextBuffer, nBytes );
121 // success
122 pData = rtl_allocateMemory( bytes = aStream.Tell() );
123 memcpy( pData, aStream.GetData(), bytes );
124 return true;
127 bool JobData::constructFromStreamBuffer( void* pData, int bytes, JobData& rJobData )
129 SvMemoryStream aStream( pData, bytes, STREAM_READ );
130 ByteString aLine;
131 bool bVersion = false;
132 bool bPrinter = false;
133 bool bOrientation = false;
134 bool bCopies = false;
135 bool bContext = false;
136 bool bMargin = false;
137 bool bColorDepth = false;
138 bool bColorDevice = false;
139 bool bPSLevel = false;
140 while( ! aStream.IsEof() )
142 aStream.ReadLine( aLine );
143 if( aLine.CompareTo( "JobData", 7 ) == COMPARE_EQUAL )
144 bVersion = true;
145 else if( aLine.CompareTo( "printer=", 8 ) == COMPARE_EQUAL )
147 bPrinter = true;
148 rJobData.m_aPrinterName = String( aLine.Copy( 8 ), RTL_TEXTENCODING_UTF8 );
150 else if( aLine.CompareTo( "orientation=", 12 ) == COMPARE_EQUAL )
152 bOrientation = true;
153 rJobData.m_eOrientation = aLine.Copy( 12 ).EqualsIgnoreCaseAscii( "landscape" ) ? orientation::Landscape : orientation::Portrait;
155 else if( aLine.CompareTo( "copies=", 7 ) == COMPARE_EQUAL )
157 bCopies = true;
158 rJobData.m_nCopies = aLine.Copy( 7 ).ToInt32();
160 else if( aLine.CompareTo( "margindajustment=",17 ) == COMPARE_EQUAL )
162 bMargin = true;
163 ByteString aValues( aLine.Copy( 17 ) );
164 rJobData.m_nLeftMarginAdjust = aValues.GetToken( 0, ',' ).ToInt32();
165 rJobData.m_nRightMarginAdjust = aValues.GetToken( 1, ',' ).ToInt32();
166 rJobData.m_nTopMarginAdjust = aValues.GetToken( 2, ',' ).ToInt32();
167 rJobData.m_nBottomMarginAdjust = aValues.GetToken( 3, ',' ).ToInt32();
169 else if( aLine.CompareTo( "colordepth=", 11 ) == COMPARE_EQUAL )
171 bColorDepth = true;
172 rJobData.m_nColorDepth = aLine.Copy( 11 ).ToInt32();
174 else if( aLine.CompareTo( "colordevice=", 12 ) == COMPARE_EQUAL )
176 bColorDevice = true;
177 rJobData.m_nColorDevice = aLine.Copy( 12 ).ToInt32();
179 else if( aLine.CompareTo( "pslevel=", 8 ) == COMPARE_EQUAL )
181 bPSLevel = true;
182 rJobData.m_nPSLevel = aLine.Copy( 8 ).ToInt32();
184 else if( aLine.Equals( "PPDContexData" ) )
186 if( bPrinter )
188 PrinterInfoManager& rManager = PrinterInfoManager::get();
189 const PrinterInfo& rInfo = rManager.getPrinterInfo( rJobData.m_aPrinterName );
190 rJobData.m_pParser = PPDParser::getParser( rInfo.m_aDriverName );
191 if( rJobData.m_pParser )
193 rJobData.m_aContext.setParser( rJobData.m_pParser );
194 int nBytes = bytes - aStream.Tell();
195 void* pRemain = alloca( bytes - aStream.Tell() );
196 aStream.Read( pRemain, nBytes );
197 rJobData.m_aContext.rebuildFromStreamBuffer( pRemain, nBytes );
198 bContext = true;
204 return bVersion && bPrinter && bOrientation && bCopies && bContext && bMargin && bPSLevel && bColorDevice && bColorDepth;