tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / toolkit / source / awt / vclxprinter.cxx
blob187218cd252cd0e01b310cab37d2381c0aa546d0
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 <com/sun/star/uno/XComponentContext.hpp>
21 #include <awt/vclxprinter.hxx>
22 #include <cppuhelper/supportsservice.hxx>
23 #include <comphelper/string.hxx>
25 #include <vcl/print.hxx>
26 #include <vcl/jobset.hxx>
27 #include <vcl/oldprintadaptor.hxx>
28 #include <vcl/svapp.hxx>
30 #include <tools/debug.hxx>
31 #include <tools/stream.hxx>
32 #include <o3tl/string_view.hxx>
34 #include <toolkit/awt/vclxdevice.hxx>
37 #define BINARYSETUPMARKER 0x23864691
39 #define PROPERTY_Orientation 0
40 #define PROPERTY_Horizontal 1
42 // ----------------------------------------------------
43 // class VCLXPrinterPropertySet
44 // ----------------------------------------------------
46 IMPLEMENT_FORWARD_XINTERFACE2( VCLXPrinterPropertySet, VCLXPrinterPropertySet_Base, OPropertySetHelper )
47 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXPrinterPropertySet, VCLXPrinterPropertySet_Base, ::cppu::OPropertySetHelper )
49 VCLXPrinterPropertySet::VCLXPrinterPropertySet( const OUString& rPrinterName )
50 : OPropertySetHelper( m_aBHelper )
51 , mxPrinter(VclPtrInstance< Printer >(rPrinterName))
53 SolarMutexGuard aSolarGuard;
55 mnOrientation = 0;
56 mbHorizontal = false;
59 VCLXPrinterPropertySet::~VCLXPrinterPropertySet()
61 SolarMutexGuard aSolarGuard;
62 mxPrinter.reset();
65 rtl::Reference< VCLXDevice > const & VCLXPrinterPropertySet::GetDevice()
67 if ( !mxPrnDevice.is() )
69 mxPrnDevice = new VCLXDevice;
70 mxPrnDevice->SetOutputDevice(GetPrinter());
72 return mxPrnDevice;
75 css::uno::Reference< css::beans::XPropertySetInfo > VCLXPrinterPropertySet::getPropertySetInfo( )
77 static css::uno::Reference< css::beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
78 return xInfo;
81 ::cppu::IPropertyArrayHelper& VCLXPrinterPropertySet::getInfoHelper()
83 static ::cppu::OPropertyArrayHelper s_PropertyArrayHelper(
84 css::uno::Sequence<css::beans::Property>{
85 css::beans::Property( u"Orientation"_ustr, PROPERTY_Orientation, cppu::UnoType<sal_Int16>::get(), 0 ),
86 css::beans::Property( u"Horizontal"_ustr, PROPERTY_Horizontal, cppu::UnoType<bool>::get(), 0 )},
87 false);
89 return s_PropertyArrayHelper;
92 sal_Bool VCLXPrinterPropertySet::convertFastPropertyValue( css::uno::Any & rConvertedValue, css::uno::Any & rOldValue, sal_Int32 nHandle, const css::uno::Any& rValue )
94 ::osl::MutexGuard aGuard( m_aMutex );
96 bool bDifferent = false;
97 switch ( nHandle )
99 case PROPERTY_Orientation:
101 sal_Int16 n;
102 if( ( rValue >>= n ) && ( n != mnOrientation ) )
104 rConvertedValue <<= n;
105 rOldValue <<= mnOrientation;
106 bDifferent = true;
109 break;
110 case PROPERTY_Horizontal:
112 bool b;
113 if( ( rValue >>= b ) && ( b != mbHorizontal ) )
115 rConvertedValue <<= b;
116 rOldValue <<= mbHorizontal;
117 bDifferent = true;
120 break;
121 default:
123 OSL_FAIL( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
126 return bDifferent;
129 void VCLXPrinterPropertySet::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const css::uno::Any& rValue )
131 ::osl::MutexGuard aGuard( m_aMutex );
133 switch( nHandle )
135 case PROPERTY_Orientation:
137 rValue >>= mnOrientation;
139 break;
140 case PROPERTY_Horizontal:
142 rValue >>= mbHorizontal;
144 break;
145 default:
147 OSL_FAIL( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
152 void VCLXPrinterPropertySet::getFastPropertyValue( css::uno::Any& rValue, sal_Int32 nHandle ) const
154 ::osl::MutexGuard aGuard( const_cast<VCLXPrinterPropertySet*>(this)->m_aMutex );
156 switch( nHandle )
158 case PROPERTY_Orientation:
159 rValue <<= mnOrientation;
160 break;
161 case PROPERTY_Horizontal:
162 rValue <<= mbHorizontal;
163 break;
164 default:
166 OSL_FAIL( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
171 // css::awt::XPrinterPropertySet
172 void VCLXPrinterPropertySet::setHorizontal( sal_Bool bHorizontal )
174 ::osl::MutexGuard aGuard( m_aMutex );
176 css::uno::Any aValue;
177 aValue <<= bHorizontal;
178 setFastPropertyValue( PROPERTY_Horizontal, aValue );
181 css::uno::Sequence< OUString > VCLXPrinterPropertySet::getFormDescriptions( )
183 ::osl::MutexGuard aGuard( m_aMutex );
185 const sal_uInt16 nPaperBinCount = GetPrinter()->GetPaperBinCount();
186 css::uno::Sequence< OUString > aDescriptions( nPaperBinCount );
187 for ( sal_uInt16 n = 0; n < nPaperBinCount; n++ )
189 // Format: <DisplayFormName;FormNameId;DisplayPaperBinName;PaperBinNameId;DisplayPaperName;PaperNameId>
190 aDescriptions.getArray()[n] = "*;*;" + GetPrinter()->GetPaperBinName( n ) + ";" +
191 OUString::number(n) + ";*;*";
193 return aDescriptions;
196 void VCLXPrinterPropertySet::selectForm( const OUString& rFormDescription )
198 ::osl::MutexGuard aGuard( m_aMutex );
200 sal_uInt16 nPaperBin = sal::static_int_cast< sal_uInt16 >(
201 o3tl::toInt32(o3tl::getToken(rFormDescription, 3, ';' )));
202 GetPrinter()->SetPaperBin( nPaperBin );
205 css::uno::Sequence< sal_Int8 > VCLXPrinterPropertySet::getBinarySetup( )
207 ::osl::MutexGuard aGuard( m_aMutex );
209 SvMemoryStream aMem;
210 aMem.WriteUInt32( BINARYSETUPMARKER );
211 WriteJobSetup( aMem, GetPrinter()->GetJobSetup() );
212 return css::uno::Sequence<sal_Int8>( static_cast<sal_Int8 const *>(aMem.GetData()), aMem.Tell() );
215 void VCLXPrinterPropertySet::setBinarySetup( const css::uno::Sequence< sal_Int8 >& data )
217 ::osl::MutexGuard aGuard( m_aMutex );
219 SvMemoryStream aMem( const_cast<signed char*>(data.getConstArray()), data.getLength(), StreamMode::READ );
220 sal_uInt32 nMarker;
221 aMem.ReadUInt32( nMarker );
222 DBG_ASSERT( nMarker == BINARYSETUPMARKER, "setBinarySetup - invalid!" );
223 if ( nMarker == BINARYSETUPMARKER )
225 JobSetup aSetup;
226 ReadJobSetup( aMem, aSetup );
227 GetPrinter()->SetJobSetup( aSetup );
232 // ----------------------------------------------------
233 // class VCLXPrinter
234 // ----------------------------------------------------
235 VCLXPrinter::VCLXPrinter( const OUString& rPrinterName )
236 : VCLXPrinter_Base( rPrinterName )
240 VCLXPrinter::~VCLXPrinter()
244 sal_Bool VCLXPrinter::start( const OUString& /*rJobName*/, sal_Int16 /*nCopies*/, sal_Bool /*bCollate*/ )
246 ::osl::MutexGuard aGuard( m_aMutex );
248 if (mxPrinter)
250 maInitJobSetup = mxPrinter->GetJobSetup();
251 mxListener = std::make_shared<vcl::OldStylePrintAdaptor>(mxPrinter, nullptr);
254 return true;
257 void VCLXPrinter::end( )
259 ::osl::MutexGuard aGuard( m_aMutex );
261 if (mxListener)
263 Printer::PrintJob(mxListener, maInitJobSetup);
264 mxListener.reset();
268 void VCLXPrinter::terminate( )
270 ::osl::MutexGuard aGuard( m_aMutex );
272 mxListener.reset();
275 css::uno::Reference< css::awt::XDevice > VCLXPrinter::startPage( )
277 ::osl::MutexGuard aGuard( m_aMutex );
279 if (mxListener)
281 mxListener->StartPage();
283 return GetDevice();
286 void VCLXPrinter::endPage( )
288 ::osl::MutexGuard aGuard( m_aMutex );
290 if (mxListener)
292 mxListener->EndPage();
297 // ----------------------------------------------------
298 // class VCLXInfoPrinter
299 // ----------------------------------------------------
301 VCLXInfoPrinter::VCLXInfoPrinter( const OUString& rPrinterName )
302 : VCLXInfoPrinter_Base( rPrinterName )
306 VCLXInfoPrinter::~VCLXInfoPrinter()
310 // css::awt::XInfoPrinter
311 css::uno::Reference< css::awt::XDevice > VCLXInfoPrinter::createDevice( )
313 ::osl::MutexGuard aGuard( m_aMutex );
315 return GetDevice();
318 // ----------------------------------------------------
319 // class VCLXPrinterServer
320 // ----------------------------------------------------
322 // css::awt::XPrinterServer2
323 css::uno::Sequence< OUString > VCLXPrinterServer::getPrinterNames( )
325 const std::vector<OUString>& rQueues = Printer::GetPrinterQueues();
326 sal_uInt32 nPrinters = rQueues.size();
328 css::uno::Sequence< OUString > aNames( nPrinters );
329 for ( sal_uInt32 n = 0; n < nPrinters; n++ )
330 aNames.getArray()[n] = rQueues[n];
332 return aNames;
335 OUString VCLXPrinterServer::getDefaultPrinterName()
337 return Printer::GetDefaultPrinterName();
340 css::uno::Reference< css::awt::XPrinter > VCLXPrinterServer::createPrinter( const OUString& rPrinterName )
342 css::uno::Reference< css::awt::XPrinter > xP = new VCLXPrinter( rPrinterName );
343 return xP;
346 css::uno::Reference< css::awt::XInfoPrinter > VCLXPrinterServer::createInfoPrinter( const OUString& rPrinterName )
348 css::uno::Reference< css::awt::XInfoPrinter > xP = new VCLXInfoPrinter( rPrinterName );
349 return xP;
352 OUString VCLXPrinterServer::getImplementationName()
354 return u"stardiv.Toolkit.VCLXPrinterServer"_ustr;
357 sal_Bool VCLXPrinterServer::supportsService(OUString const & ServiceName)
359 return cppu::supportsService(this, ServiceName);
362 css::uno::Sequence<OUString> VCLXPrinterServer::getSupportedServiceNames()
364 return css::uno::Sequence<OUString>{
365 u"com.sun.star.awt.PrinterServer"_ustr, u"stardiv.vcl.PrinterServer"_ustr};
368 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
369 stardiv_Toolkit_VCLXPrinterServer_get_implementation(
370 css::uno::XComponentContext *,
371 css::uno::Sequence<css::uno::Any> const &)
373 return cppu::acquire(new VCLXPrinterServer);
376 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */