23#include <com/sun/star/view/XPrintJob.hpp>
24#include <com/sun/star/awt/Size.hpp>
25#include <com/sun/star/lang/IllegalArgumentException.hpp>
26#include <com/sun/star/view/PaperFormat.hpp>
27#include <com/sun/star/view/PaperOrientation.hpp>
28#include <com/sun/star/ucb/NameClash.hpp>
29#include <com/sun/star/frame/XModel.hpp>
30#include <com/sun/star/view/DuplexMode.hpp>
36#include <osl/file.hxx>
37#include <osl/thread.hxx>
53#define SFX_PRINTABLESTATE_CANCELJOB css::view::PrintableState(-2)
72 const SfxHint& aHint )
override ;
77 awt::Size aReturnValue;
78 aReturnValue.Width = aSize.
Width() ;
79 aReturnValue.Height = aSize.
Height() ;
86 aReturnValue.
setWidth( aSize.Width ) ;
93class SfxPrintJob_Impl :
public cppu::WeakImplHelper
102 virtual Sequence< css::beans::PropertyValue > SAL_CALL getPrintOptions( )
override;
103 virtual Sequence< css::beans::PropertyValue > SAL_CALL getPrinter( )
override;
104 virtual Reference< css::view::XPrintable > SAL_CALL getPrintable( )
override;
105 virtual void SAL_CALL cancelJob()
override;
115Sequence< css::beans::PropertyValue > SAL_CALL SfxPrintJob_Impl::getPrintOptions()
117 return m_pData->m_aPrintOptions;
120Sequence< css::beans::PropertyValue > SAL_CALL SfxPrintJob_Impl::getPrinter()
122 if(
m_pData->m_pObjectShell.is() )
124 Reference < view::XPrintable > xPrintable(
m_pData->m_pObjectShell->GetModel(), UNO_QUERY );
125 if ( xPrintable.is() )
126 return xPrintable->getPrinter();
128 return Sequence< css::beans::PropertyValue >();
131Reference< css::view::XPrintable > SAL_CALL SfxPrintJob_Impl::getPrintable()
133 Reference < view::XPrintable > xPrintable(
m_pData->m_pObjectShell.is() ?
m_pData->m_pObjectShell->GetModel() :
nullptr, UNO_QUERY );
137void SAL_CALL SfxPrintJob_Impl::cancelJob()
140 if(
m_pData->m_pObjectShell.is() )
154 css::uno::Reference < css::frame::XModel >
xModel;
167 view::PaperFormat convertToPaperFormat(
Paper eFormat)
169 view::PaperFormat eRet;
173 eRet = view::PaperFormat_A3;
176 eRet = view::PaperFormat_A4;
179 eRet = view::PaperFormat_A5;
182 eRet = view::PaperFormat_B4;
185 eRet = view::PaperFormat_B5;
188 eRet = view::PaperFormat_LETTER;
191 eRet = view::PaperFormat_LEGAL;
194 eRet = view::PaperFormat_TABLOID;
198 eRet = view::PaperFormat_USER;
204 Paper convertToPaper(view::PaperFormat eFormat)
206 Paper eRet(PAPER_USER);
209 case view::PaperFormat_A3:
212 case view::PaperFormat_A4:
215 case view::PaperFormat_A5:
218 case view::PaperFormat_B4:
221 case view::PaperFormat_B5:
224 case view::PaperFormat_LETTER:
227 case view::PaperFormat_LEGAL:
230 case view::PaperFormat_TABLOID:
233 case view::PaperFormat_USER:
236 case view::PaperFormat::PaperFormat_MAKE_FIXED_SIZE:
254 const Printer *pPrinter =
nullptr;
257 while ( pViewFrm && !pPrinter )
264 if ( !pPrinter && pFirst )
268 return uno::Sequence< beans::PropertyValue >();
306 sal_Int32 lDummy = 0;
307 auto pProp = std::find_if(rPrinter.begin(), rPrinter.end(),
308 [](
const beans::PropertyValue &rProp) { return rProp.Name ==
"Name"; });
309 if (pProp != rPrinter.end())
311 OUString aPrinterName;
312 if ( ! ( pProp->Value >>= aPrinterName ) )
313 throw css::lang::IllegalArgumentException();
315 if ( aPrinterName != pPrinter->GetName() )
322 Size aSetPaperSize( 0, 0);
323 view::PaperFormat nPaperFormat = view::PaperFormat_USER;
326 for (
const beans::PropertyValue &rProp : rPrinter )
330 if ( rProp.Name ==
"PaperOrientation" )
332 view::PaperOrientation eOrient;
333 if ( !( rProp.Value >>= eOrient ) )
335 if ( !( rProp.Value >>= lDummy ) )
336 throw css::lang::IllegalArgumentException();
337 eOrient =
static_cast<view::PaperOrientation
>(lDummy);
340 if (
static_cast<Orientation>(eOrient) != pPrinter->GetOrientation() )
342 pPrinter->SetOrientation(
static_cast<Orientation>(eOrient) );
348 else if ( rProp.Name ==
"PaperFormat" )
350 if ( !( rProp.Value >>= nPaperFormat ) )
352 if ( !( rProp.Value >>= lDummy ) )
353 throw css::lang::IllegalArgumentException();
354 nPaperFormat =
static_cast<view::PaperFormat
>(lDummy);
357 if ( convertToPaper(nPaperFormat) != pPrinter->GetPaper() )
359 pPrinter->SetPaper( convertToPaper(nPaperFormat) );
365 else if ( rProp.Name ==
"PaperSize" )
367 awt::Size aTempSize ;
368 if ( !( rProp.Value >>= aTempSize ) )
370 throw css::lang::IllegalArgumentException();
376 else if ( rProp.Name ==
"PrinterPaperTray" )
379 if ( !( rProp.Value >>= aTmp ) )
380 throw css::lang::IllegalArgumentException();
381 const sal_uInt16
nCount = pPrinter->GetPaperBinCount();
382 for (sal_uInt16 nBin=0; nBin<
nCount; nBin++)
384 OUString
aName( pPrinter->GetPaperBinName(nBin) );
387 pPrinter->SetPaperBin(nBin);
396 if(nPaperFormat == view::PaperFormat_USER && aSetPaperSize.
Width())
400 aSetPaperSize = pPrinter->LogicToPixel(aSetPaperSize,
MapMode(MapUnit::Map100thMM));
401 if( aSetPaperSize != pPrinter->GetPaperSizePixel() )
403 pPrinter->SetPaperSizeUser( pPrinter->PixelToLogic( aSetPaperSize ) );
424 if ( pViewSh && pPrinter )
425 pViewSh->
SetPrinter( pPrinter, nChangeFlags );
437class ImplUCBPrintWatcher :
public ::osl::Thread
443 OUString m_sTargetURL;
450 : m_pPrinter ( pPrinter )
451 , m_sTargetURL(
std::move( sTargetURL ))
452 , m_pTempFile ( pTempFile )
459 void SAL_CALL
run()
override
461 osl_setThreadName(
"ImplUCBPrintWatcher");
474 moveAndDeleteTemp(&m_pTempFile,m_sTargetURL);
482 void SAL_CALL onTerminated()
override
492 static void moveAndDeleteTemp(
::utl::TempFileNamed** ppTempFile, std::u16string_view sTargetURL )
498 OUString sFileName = aSplitter.getName(
502 if (aSplitter.removeSegment() && !sFileName.isEmpty())
505 (*ppTempFile)->GetURL(),
506 css::uno::Reference< css::ucb::XCommandEnvironment >(),
511 css::uno::Reference< css::ucb::XCommandEnvironment >(),
514 aTarget.transferContent(
518 css::ucb::NameClash::OVERWRITE);
521 catch (
const css::uno::Exception&)
528 *ppTempFile =
nullptr;
553 bool bMonitor =
false;
563 uno::Sequence < beans::PropertyValue > aCheckedArgs( rOptions.getLength() );
564 auto pCheckedArgs = aCheckedArgs.getArray();
565 sal_Int32 nProps = 0;
566 bool bWaitUntilEnd =
false;
567 sal_Int16 nDuplexMode = css::view::DuplexMode::UNKNOWN;
568 for (
const beans::PropertyValue &rProp : rOptions )
572 if ( rProp.Name ==
"FileName" )
578 (!(rProp.Value>>=sTemp))
581 throw css::lang::IllegalArgumentException();
585 OUString sURL (sTemp);
597 const OUString& sSystemPath(sTemp);
599 if (::osl::FileBase::getFileURLFromSystemPath(sSystemPath,sFileURL)!=::osl::FileBase::E_None)
600 throw css::lang::IllegalArgumentException();
601 pCheckedArgs[nProps].Name = rProp.Name;
602 pCheckedArgs[nProps++].Value <<= sFileURL;
604 aCheckedArgs.realloc( aCheckedArgs.getLength()+1 );
605 pCheckedArgs = aCheckedArgs.getArray();
606 pCheckedArgs[nProps].Name =
"LocalFileName";
607 pCheckedArgs[nProps++].Value <<= sTemp;
612 if (osl::FileBase::getSystemPathFromFileURL(sURL, sPath) == osl::FileBase::E_None)
618 pCheckedArgs[nProps].Name = rProp.Name;
619 pCheckedArgs[nProps++].Value <<= sTemp;
621 aCheckedArgs.realloc( aCheckedArgs.getLength()+1 );
622 pCheckedArgs = aCheckedArgs.getArray();
623 pCheckedArgs[nProps].Name =
"LocalFileName";
624 pCheckedArgs[nProps++].Value <<= sPath;
637 if(!pUCBPrintTempFile)
638 pUCBPrintTempFile = new ::utl::TempFileNamed();
642 pCheckedArgs[nProps].Name =
"LocalFileName";
643 pCheckedArgs[nProps++].Value <<= pUCBPrintTempFile->
GetFileName();
649 else if ( rProp.Name ==
"CopyCount" )
651 sal_Int32 nCopies = 0;
652 if ( !( rProp.Value >>= nCopies ) )
653 throw css::lang::IllegalArgumentException();
654 pCheckedArgs[nProps].Name = rProp.Name;
655 pCheckedArgs[nProps++].Value <<= nCopies;
660 else if ( rProp.Name ==
"Collate" || rProp.Name ==
"Sort" )
663 if ( !(rProp.Value >>= bTemp) )
664 throw css::lang::IllegalArgumentException();
665 pCheckedArgs[nProps].Name =
"Collate";
666 pCheckedArgs[nProps++].Value <<= bTemp;
669 else if ( rProp.Name ==
"SinglePrintJobs" )
672 if ( !(rProp.Value >>= bTemp) )
673 throw css::lang::IllegalArgumentException();
674 pCheckedArgs[nProps].Name =
"SinglePrintJobs";
675 pCheckedArgs[nProps++].Value <<= bTemp;
678 else if ( rProp.Name ==
"JobName" )
681 if( !(rProp.Value >>= sTemp) )
682 throw css::lang::IllegalArgumentException();
683 pCheckedArgs[nProps].Name = rProp.Name;
684 pCheckedArgs[nProps++].Value <<= sTemp;
688 else if ( rProp.Name ==
"Pages" )
691 if( !(rProp.Value >>= sTemp) )
692 throw css::lang::IllegalArgumentException();
693 pCheckedArgs[nProps].Name = rProp.Name;
694 pCheckedArgs[nProps++].Value <<= sTemp;
698 else if ( rProp.Name ==
"MonitorVisible" )
700 if( !(rProp.Value >>= bMonitor) )
701 throw css::lang::IllegalArgumentException();
702 pCheckedArgs[nProps].Name = rProp.Name;
703 pCheckedArgs[nProps++].Value <<= bMonitor;
707 else if ( rProp.Name ==
"Wait" )
709 if ( !(rProp.Value >>= bWaitUntilEnd) )
710 throw css::lang::IllegalArgumentException();
711 pCheckedArgs[nProps].Name = rProp.Name;
712 pCheckedArgs[nProps++].Value <<= bWaitUntilEnd;
715 else if ( rProp.Name ==
"DuplexMode" )
717 if ( !(rProp.Value >>= nDuplexMode ) )
718 throw css::lang::IllegalArgumentException();
719 pCheckedArgs[nProps].Name = rProp.Name;
720 pCheckedArgs[nProps++].Value <<= nDuplexMode;
724 if ( nProps != aCheckedArgs.getLength() )
725 aCheckedArgs.realloc(nProps);
731 pView->
ExecPrint( aCheckedArgs,
true,
false );
740 if (!pUCBPrintTempFile)
746 ImplUCBPrintWatcher::moveAndDeleteTemp(&pUCBPrintTempFile,sUcbUrl);
752 ImplUCBPrintWatcher* pWatcher =
new ImplUCBPrintWatcher( pPrinter, pUCBPrintTempFile, sUcbUrl );
765 if ( pPrintHint->
GetWhich() == css::view::PrintableState_JOB_STARTED )
775 view::PrintJobEvent
aEvent;
787 std::unique_lock aGuard(
m_pData->m_aMutex);
788 m_pData->m_aJobListeners.addInterface( aGuard, xListener );
793 std::unique_lock aGuard(
m_pData->m_aMutex);
794 m_pData->m_aJobListeners.removeInterface( aGuard, xListener );
static const AllSettings & GetSettings()
INetProtocol GetProtocol() const
bool HasSupport(PrinterSupport eFeature) const
Size GetPaperSize() const
const OUString & GetName() const
Orientation GetOrientation() const
static SfxObjectShell * GetShellFromComponent(const css::uno::Reference< css::uno::XInterface > &xComp)
std::unique_ptr< IMPL_PrintListener_DataContainer > m_pData
void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
virtual void SAL_CALL print(const css::uno::Sequence< css::beans::PropertyValue > &seqOptions) override
void impl_setPrinter(const css::uno::Sequence< css::beans::PropertyValue > &rPrinter, VclPtr< SfxPrinter > &pPrinter, SfxPrinterChangeFlags &nChangeFlags, SfxViewShell *&pViewSh)
virtual css::uno::Sequence< css::beans::PropertyValue > SAL_CALL getPrinter() override
virtual void SAL_CALL addPrintJobListener(const css::uno::Reference< css::view::XPrintJobListener > &xListener) override
virtual void SAL_CALL removePrintJobListener(const css::uno::Reference< css::view::XPrintJobListener > &xListener) override
virtual ~SfxPrintHelper() override
virtual void SAL_CALL setPrinter(const css::uno::Sequence< css::beans::PropertyValue > &seqPrinter) override
css::view::PrintableState GetWhich() const
const css::uno::Sequence< css::beans::PropertyValue > & GetOptions() const
SfxViewShell * GetViewShell() const
Returns the SfxViewShell in which they are located in the subshells.
static SAL_WARN_UNUSED_RESULT SfxViewFrame * GetNext(const SfxViewFrame &rPrev, const SfxObjectShell *pDoc=nullptr, bool bOnlyVisible=true)
static SAL_WARN_UNUSED_RESULT SfxViewFrame * GetFirst(const SfxObjectShell *pDoc=nullptr, bool bOnlyVisible=true)
One SfxViewShell more or less represents one edit window for a document, there can be multiple ones f...
virtual SfxPrinter * GetPrinter(bool bCreate=false)
virtual sal_uInt16 SetPrinter(SfxPrinter *pNewPrinter, SfxPrinterChangeFlags nDiffFlags=SFX_PRINTER_ALL)
void ExecPrint(const css::uno::Sequence< css::beans::PropertyValue > &, bool, bool)
Printer * GetActivePrinter() const
constexpr tools::Long Height() const
void setWidth(tools::Long nWidth)
void setHeight(tools::Long nHeight)
constexpr tools::Long Width() const
static VclPtr< reference_type > Create(Arg &&... arg)
sal_Int32 getLength(std::unique_lock< std::mutex > &rGuard) const
bool hasMoreElements() const
css::uno::Reference< ListenerT > const & next()
void EnableKillingFile(bool bEnable=true)
OUString GetFileName() const
#define TOOLS_WARN_EXCEPTION(area, stream)
Sequence< PropertyValue > aArguments
std::unique_ptr< sal_Int32[]> pData
def run(arg=None, arg2=-1)
Reference< XComponentContext > getProcessComponentContext()
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
static Size impl_Size_Struct2Object(const awt::Size &aSize)
#define SFX_PRINTABLESTATE_CANCELJOB
static awt::Size impl_Size_Object2Struct(const Size &aSize)
uno::Reference< css::view::XPrintJob > m_xPrintJob
comphelper::OInterfaceContainerHelper4< view::XPrintJobListener > m_aJobListeners
SfxObjectShellRef m_pObjectShell
IMPL_PrintListener_DataContainer()
css::uno::Sequence< css::beans::PropertyValue > m_aPrintOptions
void Notify(SfxBroadcaster &aBC, const SfxHint &aHint) override
Reference< XModel > xModel