bump product version to 6.4.0.3
[LibreOffice.git] / vcl / headless / svpprn.cxx
blobaa8cd59de2d241b36428c3663a78c658365328a0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <vcl/svapp.hxx>
21 #include <vcl/timer.hxx>
22 #include <printerinfomanager.hxx>
24 #include <jobset.h>
25 #include <print.h>
26 #include <salptype.hxx>
27 #include <saldatabasic.hxx>
29 #include <unx/genpspgraphics.h>
31 #include <headless/svpprn.hxx>
32 #include <headless/svpinst.hxx>
34 using namespace psp;
37 * static helpers
40 static OUString getPdfDir( const PrinterInfo& rInfo )
42 OUString aDir;
43 sal_Int32 nIndex = 0;
44 while( nIndex != -1 )
46 OUString aToken( rInfo.m_aFeatures.getToken( 0, ',', nIndex ) );
47 if( aToken.startsWith( "pdf=" ) )
49 sal_Int32 nPos = 0;
50 aDir = aToken.getToken( 1, '=', nPos );
51 if( aDir.isEmpty() )
52 aDir = OStringToOUString( OString( getenv( "HOME" ) ), osl_getThreadTextEncoding() );
53 break;
56 return aDir;
59 static int PtTo10Mu( int nPoints ) { return static_cast<int>((static_cast<double>(nPoints)*35.27777778)+0.5); }
61 static void copyJobDataToJobSetup( ImplJobSetup* pJobSetup, JobData& rData )
63 pJobSetup->SetOrientation( rData.m_eOrientation == orientation::Landscape ? Orientation::Landscape : Orientation::Portrait );
65 // copy page size
66 OUString aPaper;
67 int width, height;
69 rData.m_aContext.getPageSize( aPaper, width, height );
70 pJobSetup->SetPaperFormat( PaperInfo::fromPSName(OUStringToOString( aPaper, RTL_TEXTENCODING_ISO_8859_1 )) );
71 pJobSetup->SetPaperWidth( 0 );
72 pJobSetup->SetPaperHeight( 0 );
73 if( pJobSetup->GetPaperFormat() == PAPER_USER )
75 // transform to 100dth mm
76 width = PtTo10Mu( width );
77 height = PtTo10Mu( height );
79 if( rData.m_eOrientation == psp::orientation::Portrait )
81 pJobSetup->SetPaperWidth( width );
82 pJobSetup->SetPaperHeight( height );
84 else
86 pJobSetup->SetPaperWidth( height );
87 pJobSetup->SetPaperHeight( width );
91 // copy input slot
92 const PPDKey* pKey = nullptr;
93 const PPDValue* pValue = nullptr;
95 pJobSetup->SetPaperBin( 0xffff );
96 if( rData.m_pParser )
97 pKey = rData.m_pParser->getKey( "InputSlot" );
98 if( pKey )
99 pValue = rData.m_aContext.getValue( pKey );
100 if( pKey && pValue )
102 int nPaperBin;
103 for( nPaperBin = 0;
104 pValue != pKey->getValue( nPaperBin ) &&
105 nPaperBin < pKey->countValues();
106 nPaperBin++ );
107 pJobSetup->SetPaperBin(
108 (nPaperBin == pKey->countValues()
109 || pValue == pKey->getDefaultValue())
110 ? 0xffff : nPaperBin);
113 // copy duplex
114 pKey = nullptr;
115 pValue = nullptr;
117 pJobSetup->SetDuplexMode( DuplexMode::Unknown );
118 if( rData.m_pParser )
119 pKey = rData.m_pParser->getKey( "Duplex" );
120 if( pKey )
121 pValue = rData.m_aContext.getValue( pKey );
122 if( pKey && pValue )
124 if( pValue->m_aOption.equalsIgnoreAsciiCase( "None" ) ||
125 pValue->m_aOption.startsWithIgnoreAsciiCase( "Simplex" )
128 pJobSetup->SetDuplexMode( DuplexMode::Off );
130 else if( pValue->m_aOption.equalsIgnoreAsciiCase( "DuplexNoTumble" ) )
132 pJobSetup->SetDuplexMode( DuplexMode::LongEdge );
134 else if( pValue->m_aOption.equalsIgnoreAsciiCase( "DuplexTumble" ) )
136 pJobSetup->SetDuplexMode( DuplexMode::ShortEdge );
140 // copy the whole context
141 if( pJobSetup->GetDriverData() )
142 std::free( const_cast<sal_uInt8*>(pJobSetup->GetDriverData()) );
144 sal_uInt32 nBytes;
145 void* pBuffer = nullptr;
146 if( rData.getStreamBuffer( pBuffer, nBytes ) )
148 pJobSetup->SetDriverDataLen( nBytes );
149 pJobSetup->SetDriverData( static_cast<sal_uInt8*>(pBuffer) );
151 else
153 pJobSetup->SetDriverDataLen( 0 );
154 pJobSetup->SetDriverData( nullptr );
158 // SalInstance
160 SalInfoPrinter* SvpSalInstance::CreateInfoPrinter( SalPrinterQueueInfo* pQueueInfo,
161 ImplJobSetup* pJobSetup )
163 // create and initialize SalInfoPrinter
164 SvpSalInfoPrinter* pPrinter = new SvpSalInfoPrinter;
166 if( pJobSetup )
168 PrinterInfoManager& rManager( PrinterInfoManager::get() );
169 PrinterInfo aInfo( rManager.getPrinterInfo( pQueueInfo->maPrinterName ) );
170 pPrinter->m_aJobData = aInfo;
171 pPrinter->m_aPrinterGfx.Init( pPrinter->m_aJobData );
173 if( pJobSetup->GetDriverData() )
174 JobData::constructFromStreamBuffer( pJobSetup->GetDriverData(),
175 pJobSetup->GetDriverDataLen(), aInfo );
177 pJobSetup->SetSystem( JOBSETUP_SYSTEM_UNIX );
178 pJobSetup->SetPrinterName( pQueueInfo->maPrinterName );
179 pJobSetup->SetDriver( aInfo.m_aDriverName );
180 copyJobDataToJobSetup( pJobSetup, aInfo );
183 return pPrinter;
186 void SvpSalInstance::DestroyInfoPrinter( SalInfoPrinter* pPrinter )
188 delete pPrinter;
191 std::unique_ptr<SalPrinter> SvpSalInstance::CreatePrinter( SalInfoPrinter* pInfoPrinter )
193 // create and initialize SalPrinter
194 SvpSalPrinter* pPrinter = new SvpSalPrinter( pInfoPrinter );
195 pPrinter->m_aJobData = static_cast<SvpSalInfoPrinter*>(pInfoPrinter)->m_aJobData;
197 return std::unique_ptr<SalPrinter>(pPrinter);
200 void SvpSalInstance::GetPrinterQueueInfo( ImplPrnQueueList* pList )
202 PrinterInfoManager& rManager( PrinterInfoManager::get() );
203 static const char* pNoSyncDetection = getenv( "SAL_DISABLE_SYNCHRONOUS_PRINTER_DETECTION" );
204 if( ! pNoSyncDetection || ! *pNoSyncDetection )
206 // #i62663# synchronize possible asynchronouse printer detection now
207 rManager.checkPrintersChanged( true );
209 ::std::vector< OUString > aPrinters;
210 rManager.listPrinters( aPrinters );
212 for (auto const& printer : aPrinters)
214 const PrinterInfo& rInfo( rManager.getPrinterInfo(printer) );
215 // create new entry
216 std::unique_ptr<SalPrinterQueueInfo> pInfo(new SalPrinterQueueInfo);
217 pInfo->maPrinterName = printer;
218 pInfo->maDriver = rInfo.m_aDriverName;
219 pInfo->maLocation = rInfo.m_aLocation;
220 pInfo->maComment = rInfo.m_aComment;
222 sal_Int32 nIndex = 0;
223 while( nIndex != -1 )
225 OUString aToken( rInfo.m_aFeatures.getToken( 0, ',', nIndex ) );
226 if( aToken.startsWith( "pdf=" ) )
228 pInfo->maLocation = getPdfDir( rInfo );
229 break;
233 pList->Add( std::move(pInfo) );
237 void SvpSalInstance::GetPrinterQueueState( SalPrinterQueueInfo* )
241 OUString SvpSalInstance::GetDefaultPrinter()
243 PrinterInfoManager& rManager( PrinterInfoManager::get() );
244 return rManager.getDefaultPrinter();
247 void SvpSalInstance::PostPrintersChanged()
249 SvpSalInstance *pInst = SvpSalInstance::s_pDefaultInstance;
250 for (auto pSalFrame : pInst->getFrames() )
251 pInst->PostEvent( pSalFrame, nullptr, SalEvent::PrinterChanged );
254 std::unique_ptr<GenPspGraphics> SvpSalInstance::CreatePrintGraphics()
256 return std::make_unique<GenPspGraphics>();
259 bool SvpSalInfoPrinter::Setup( weld::Window*, ImplJobSetup* )
261 return false;
264 SvpSalPrinter::SvpSalPrinter( SalInfoPrinter* pInfoPrinter )
265 : PspSalPrinter( pInfoPrinter )
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */