1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sal/config.h>
22 #include <string_view>
24 #include <vcl/svapp.hxx>
25 #include <vcl/timer.hxx>
26 #include <vcl/QueueInfo.hxx>
27 #include <printerinfomanager.hxx>
31 #include <salptype.hxx>
34 #include <unx/genpspgraphics.h>
36 #include <headless/svpprn.hxx>
37 #include <headless/svpinst.hxx>
45 static OUString
getPdfDir( const PrinterInfo
& rInfo
)
51 OUString
aToken( rInfo
.m_aFeatures
.getToken( 0, ',', nIndex
) );
52 if( aToken
.startsWith( "pdf=" ) )
55 aDir
= aToken
.getToken( 1, '=', nPos
);
57 if (auto const env
= getenv( "HOME" )) {
58 aDir
= OStringToOUString(
59 std::string_view( env
), osl_getThreadTextEncoding() );
67 static int PtTo10Mu( int nPoints
) { return static_cast<int>((static_cast<double>(nPoints
)*35.27777778)+0.5); }
69 static void copyJobDataToJobSetup( ImplJobSetup
* pJobSetup
, JobData
& rData
)
71 pJobSetup
->SetOrientation( rData
.m_eOrientation
== orientation::Landscape
? Orientation::Landscape
: Orientation::Portrait
);
77 rData
.m_aContext
.getPageSize( aPaper
, width
, height
);
78 pJobSetup
->SetPaperFormat( PaperInfo::fromPSName(OUStringToOString( aPaper
, RTL_TEXTENCODING_ISO_8859_1
)) );
79 pJobSetup
->SetPaperWidth( 0 );
80 pJobSetup
->SetPaperHeight( 0 );
81 if( pJobSetup
->GetPaperFormat() == PAPER_USER
)
83 // transform to 100dth mm
84 width
= PtTo10Mu( width
);
85 height
= PtTo10Mu( height
);
87 if( rData
.m_eOrientation
== psp::orientation::Portrait
)
89 pJobSetup
->SetPaperWidth( width
);
90 pJobSetup
->SetPaperHeight( height
);
94 pJobSetup
->SetPaperWidth( height
);
95 pJobSetup
->SetPaperHeight( width
);
100 const PPDKey
* pKey
= nullptr;
101 const PPDValue
* pValue
= nullptr;
103 pJobSetup
->SetPaperBin( 0xffff );
104 if( rData
.m_pParser
)
105 pKey
= rData
.m_pParser
->getKey( "InputSlot" );
107 pValue
= rData
.m_aContext
.getValue( pKey
);
112 pValue
!= pKey
->getValue( nPaperBin
) &&
113 nPaperBin
< pKey
->countValues();
115 pJobSetup
->SetPaperBin(
116 (nPaperBin
== pKey
->countValues()
117 || pValue
== pKey
->getDefaultValue())
118 ? 0xffff : nPaperBin
);
125 pJobSetup
->SetDuplexMode( DuplexMode::Unknown
);
126 if( rData
.m_pParser
)
127 pKey
= rData
.m_pParser
->getKey( "Duplex" );
129 pValue
= rData
.m_aContext
.getValue( pKey
);
132 if( pValue
->m_aOption
.equalsIgnoreAsciiCase( "None" ) ||
133 pValue
->m_aOption
.startsWithIgnoreAsciiCase( "Simplex" )
136 pJobSetup
->SetDuplexMode( DuplexMode::Off
);
138 else if( pValue
->m_aOption
.equalsIgnoreAsciiCase( "DuplexNoTumble" ) )
140 pJobSetup
->SetDuplexMode( DuplexMode::LongEdge
);
142 else if( pValue
->m_aOption
.equalsIgnoreAsciiCase( "DuplexTumble" ) )
144 pJobSetup
->SetDuplexMode( DuplexMode::ShortEdge
);
148 // copy the whole context
149 if( pJobSetup
->GetDriverData() )
150 std::free( const_cast<sal_uInt8
*>(pJobSetup
->GetDriverData()) );
153 void* pBuffer
= nullptr;
154 if( rData
.getStreamBuffer( pBuffer
, nBytes
) )
156 pJobSetup
->SetDriverDataLen( nBytes
);
157 pJobSetup
->SetDriverData( static_cast<sal_uInt8
*>(pBuffer
) );
161 pJobSetup
->SetDriverDataLen( 0 );
162 pJobSetup
->SetDriverData( nullptr );
168 SalInfoPrinter
* SvpSalInstance::CreateInfoPrinter( SalPrinterQueueInfo
* pQueueInfo
,
169 ImplJobSetup
* pJobSetup
)
171 // create and initialize SalInfoPrinter
172 SvpSalInfoPrinter
* pPrinter
= new SvpSalInfoPrinter
;
176 PrinterInfoManager
& rManager( PrinterInfoManager::get() );
177 PrinterInfo
aInfo( rManager
.getPrinterInfo( pQueueInfo
->maPrinterName
) );
178 pPrinter
->m_aJobData
= aInfo
;
179 pPrinter
->m_aPrinterGfx
.Init( pPrinter
->m_aJobData
);
181 if( pJobSetup
->GetDriverData() )
182 JobData::constructFromStreamBuffer( pJobSetup
->GetDriverData(),
183 pJobSetup
->GetDriverDataLen(), aInfo
);
185 pJobSetup
->SetSystem( JOBSETUP_SYSTEM_UNIX
);
186 pJobSetup
->SetPrinterName( pQueueInfo
->maPrinterName
);
187 pJobSetup
->SetDriver( aInfo
.m_aDriverName
);
188 copyJobDataToJobSetup( pJobSetup
, aInfo
);
194 void SvpSalInstance::DestroyInfoPrinter( SalInfoPrinter
* pPrinter
)
199 std::unique_ptr
<SalPrinter
> SvpSalInstance::CreatePrinter( SalInfoPrinter
* pInfoPrinter
)
201 // create and initialize SalPrinter
202 SvpSalPrinter
* pPrinter
= new SvpSalPrinter( pInfoPrinter
);
203 pPrinter
->m_aJobData
= static_cast<SvpSalInfoPrinter
*>(pInfoPrinter
)->m_aJobData
;
205 return std::unique_ptr
<SalPrinter
>(pPrinter
);
208 void SvpSalInstance::GetPrinterQueueInfo( ImplPrnQueueList
* pList
)
210 PrinterInfoManager
& rManager( PrinterInfoManager::get() );
211 static const char* pNoSyncDetection
= getenv( "SAL_DISABLE_SYNCHRONOUS_PRINTER_DETECTION" );
212 if( ! pNoSyncDetection
|| ! *pNoSyncDetection
)
214 // #i62663# synchronize possible asynchronouse printer detection now
215 rManager
.checkPrintersChanged( true );
217 ::std::vector
< OUString
> aPrinters
;
218 rManager
.listPrinters( aPrinters
);
220 for (auto const& printer
: aPrinters
)
222 const PrinterInfo
& rInfo( rManager
.getPrinterInfo(printer
) );
224 std::unique_ptr
<SalPrinterQueueInfo
> pInfo(new SalPrinterQueueInfo
);
225 pInfo
->maPrinterName
= printer
;
226 pInfo
->maDriver
= rInfo
.m_aDriverName
;
227 pInfo
->maLocation
= rInfo
.m_aLocation
;
228 pInfo
->maComment
= rInfo
.m_aComment
;
230 sal_Int32 nIndex
= 0;
231 while( nIndex
!= -1 )
233 OUString
aToken( rInfo
.m_aFeatures
.getToken( 0, ',', nIndex
) );
234 if( aToken
.startsWith( "pdf=" ) )
236 pInfo
->maLocation
= getPdfDir( rInfo
);
241 pList
->Add( std::move(pInfo
) );
245 void SvpSalInstance::GetPrinterQueueState( SalPrinterQueueInfo
* )
249 OUString
SvpSalInstance::GetDefaultPrinter()
251 PrinterInfoManager
& rManager( PrinterInfoManager::get() );
252 return rManager
.getDefaultPrinter();
255 void SvpSalInstance::PostPrintersChanged()
257 SvpSalInstance
*pInst
= SvpSalInstance::s_pDefaultInstance
;
258 for (auto pSalFrame
: pInst
->getFrames() )
259 pInst
->PostEvent( pSalFrame
, nullptr, SalEvent::PrinterChanged
);
262 std::unique_ptr
<GenPspGraphics
> SvpSalInstance::CreatePrintGraphics()
264 return std::make_unique
<GenPspGraphics
>();
267 bool SvpSalInfoPrinter::Setup( weld::Window
*, ImplJobSetup
* )
272 SvpSalPrinter::SvpSalPrinter( SalInfoPrinter
* pInfoPrinter
)
273 : PspSalPrinter( pInfoPrinter
)
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */