Avoid potential negative array index access to cached text.
[LibreOffice.git] / toolkit / source / awt / vclxprinter.cxx
blob6ace061795074231ae5844829c5464cfb5b7a357
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 css::uno::Reference< css::awt::XDevice > const & VCLXPrinterPropertySet::GetDevice()
67 if ( !mxPrnDevice.is() )
69 rtl::Reference<VCLXDevice> pDev = new VCLXDevice;
70 pDev->SetOutputDevice( GetPrinter() );
71 mxPrnDevice = pDev;
73 return mxPrnDevice;
76 css::uno::Reference< css::beans::XPropertySetInfo > VCLXPrinterPropertySet::getPropertySetInfo( )
78 static css::uno::Reference< css::beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
79 return xInfo;
82 ::cppu::IPropertyArrayHelper& VCLXPrinterPropertySet::getInfoHelper()
84 static ::cppu::OPropertyArrayHelper s_PropertyArrayHelper(
85 css::uno::Sequence<css::beans::Property>{
86 css::beans::Property( "Orientation", PROPERTY_Orientation, cppu::UnoType<sal_Int16>::get(), 0 ),
87 css::beans::Property( "Horizontal", PROPERTY_Horizontal, cppu::UnoType<bool>::get(), 0 )},
88 false);
90 return s_PropertyArrayHelper;
93 sal_Bool VCLXPrinterPropertySet::convertFastPropertyValue( css::uno::Any & rConvertedValue, css::uno::Any & rOldValue, sal_Int32 nHandle, const css::uno::Any& rValue )
95 ::osl::MutexGuard aGuard( m_aMutex );
97 bool bDifferent = false;
98 switch ( nHandle )
100 case PROPERTY_Orientation:
102 sal_Int16 n;
103 if( ( rValue >>= n ) && ( n != mnOrientation ) )
105 rConvertedValue <<= n;
106 rOldValue <<= mnOrientation;
107 bDifferent = true;
110 break;
111 case PROPERTY_Horizontal:
113 bool b;
114 if( ( rValue >>= b ) && ( b != mbHorizontal ) )
116 rConvertedValue <<= b;
117 rOldValue <<= mbHorizontal;
118 bDifferent = true;
121 break;
122 default:
124 OSL_FAIL( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
127 return bDifferent;
130 void VCLXPrinterPropertySet::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const css::uno::Any& rValue )
132 ::osl::MutexGuard aGuard( m_aMutex );
134 switch( nHandle )
136 case PROPERTY_Orientation:
138 rValue >>= mnOrientation;
140 break;
141 case PROPERTY_Horizontal:
143 rValue >>= mbHorizontal;
145 break;
146 default:
148 OSL_FAIL( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
153 void VCLXPrinterPropertySet::getFastPropertyValue( css::uno::Any& rValue, sal_Int32 nHandle ) const
155 ::osl::MutexGuard aGuard( const_cast<VCLXPrinterPropertySet*>(this)->m_aMutex );
157 switch( nHandle )
159 case PROPERTY_Orientation:
160 rValue <<= mnOrientation;
161 break;
162 case PROPERTY_Horizontal:
163 rValue <<= mbHorizontal;
164 break;
165 default:
167 OSL_FAIL( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
172 // css::awt::XPrinterPropertySet
173 void VCLXPrinterPropertySet::setHorizontal( sal_Bool bHorizontal )
175 ::osl::MutexGuard aGuard( m_aMutex );
177 css::uno::Any aValue;
178 aValue <<= bHorizontal;
179 setFastPropertyValue( PROPERTY_Horizontal, aValue );
182 css::uno::Sequence< OUString > VCLXPrinterPropertySet::getFormDescriptions( )
184 ::osl::MutexGuard aGuard( m_aMutex );
186 const sal_uInt16 nPaperBinCount = GetPrinter()->GetPaperBinCount();
187 css::uno::Sequence< OUString > aDescriptions( nPaperBinCount );
188 for ( sal_uInt16 n = 0; n < nPaperBinCount; n++ )
190 // Format: <DisplayFormName;FormNameId;DisplayPaperBinName;PaperBinNameId;DisplayPaperName;PaperNameId>
191 OUString aDescr = "*;*;" + GetPrinter()->GetPaperBinName( n ) + ";" +
192 OUString::number(n) + ";*;*";
194 aDescriptions.getArray()[n] = aDescr;
196 return aDescriptions;
199 void VCLXPrinterPropertySet::selectForm( const OUString& rFormDescription )
201 ::osl::MutexGuard aGuard( m_aMutex );
203 sal_uInt16 nPaperBin = sal::static_int_cast< sal_uInt16 >(
204 o3tl::toInt32(o3tl::getToken(rFormDescription, 3, ';' )));
205 GetPrinter()->SetPaperBin( nPaperBin );
208 css::uno::Sequence< sal_Int8 > VCLXPrinterPropertySet::getBinarySetup( )
210 ::osl::MutexGuard aGuard( m_aMutex );
212 SvMemoryStream aMem;
213 aMem.WriteUInt32( BINARYSETUPMARKER );
214 WriteJobSetup( aMem, GetPrinter()->GetJobSetup() );
215 return css::uno::Sequence<sal_Int8>( static_cast<sal_Int8 const *>(aMem.GetData()), aMem.Tell() );
218 void VCLXPrinterPropertySet::setBinarySetup( const css::uno::Sequence< sal_Int8 >& data )
220 ::osl::MutexGuard aGuard( m_aMutex );
222 SvMemoryStream aMem( const_cast<signed char*>(data.getConstArray()), data.getLength(), StreamMode::READ );
223 sal_uInt32 nMarker;
224 aMem.ReadUInt32( nMarker );
225 DBG_ASSERT( nMarker == BINARYSETUPMARKER, "setBinarySetup - invalid!" );
226 if ( nMarker == BINARYSETUPMARKER )
228 JobSetup aSetup;
229 ReadJobSetup( aMem, aSetup );
230 GetPrinter()->SetJobSetup( aSetup );
235 // ----------------------------------------------------
236 // class VCLXPrinter
237 // ----------------------------------------------------
238 VCLXPrinter::VCLXPrinter( const OUString& rPrinterName )
239 : VCLXPrinter_Base( rPrinterName )
243 VCLXPrinter::~VCLXPrinter()
247 sal_Bool VCLXPrinter::start( const OUString& /*rJobName*/, sal_Int16 /*nCopies*/, sal_Bool /*bCollate*/ )
249 ::osl::MutexGuard aGuard( m_aMutex );
251 if (mxPrinter)
253 maInitJobSetup = mxPrinter->GetJobSetup();
254 mxListener = std::make_shared<vcl::OldStylePrintAdaptor>(mxPrinter, nullptr);
257 return true;
260 void VCLXPrinter::end( )
262 ::osl::MutexGuard aGuard( m_aMutex );
264 if (mxListener)
266 Printer::PrintJob(mxListener, maInitJobSetup);
267 mxListener.reset();
271 void VCLXPrinter::terminate( )
273 ::osl::MutexGuard aGuard( m_aMutex );
275 mxListener.reset();
278 css::uno::Reference< css::awt::XDevice > VCLXPrinter::startPage( )
280 ::osl::MutexGuard aGuard( m_aMutex );
282 if (mxListener)
284 mxListener->StartPage();
286 return GetDevice();
289 void VCLXPrinter::endPage( )
291 ::osl::MutexGuard aGuard( m_aMutex );
293 if (mxListener)
295 mxListener->EndPage();
300 // ----------------------------------------------------
301 // class VCLXInfoPrinter
302 // ----------------------------------------------------
304 VCLXInfoPrinter::VCLXInfoPrinter( const OUString& rPrinterName )
305 : VCLXInfoPrinter_Base( rPrinterName )
309 VCLXInfoPrinter::~VCLXInfoPrinter()
313 // css::awt::XInfoPrinter
314 css::uno::Reference< css::awt::XDevice > VCLXInfoPrinter::createDevice( )
316 ::osl::MutexGuard aGuard( m_aMutex );
318 return GetDevice();
321 // ----------------------------------------------------
322 // class VCLXPrinterServer
323 // ----------------------------------------------------
325 // css::awt::XPrinterServer2
326 css::uno::Sequence< OUString > VCLXPrinterServer::getPrinterNames( )
328 const std::vector<OUString>& rQueues = Printer::GetPrinterQueues();
329 sal_uInt32 nPrinters = rQueues.size();
331 css::uno::Sequence< OUString > aNames( nPrinters );
332 for ( sal_uInt32 n = 0; n < nPrinters; n++ )
333 aNames.getArray()[n] = rQueues[n];
335 return aNames;
338 OUString VCLXPrinterServer::getDefaultPrinterName()
340 return Printer::GetDefaultPrinterName();
343 css::uno::Reference< css::awt::XPrinter > VCLXPrinterServer::createPrinter( const OUString& rPrinterName )
345 css::uno::Reference< css::awt::XPrinter > xP = new VCLXPrinter( rPrinterName );
346 return xP;
349 css::uno::Reference< css::awt::XInfoPrinter > VCLXPrinterServer::createInfoPrinter( const OUString& rPrinterName )
351 css::uno::Reference< css::awt::XInfoPrinter > xP = new VCLXInfoPrinter( rPrinterName );
352 return xP;
355 OUString VCLXPrinterServer::getImplementationName()
357 return "stardiv.Toolkit.VCLXPrinterServer";
360 sal_Bool VCLXPrinterServer::supportsService(OUString const & ServiceName)
362 return cppu::supportsService(this, ServiceName);
365 css::uno::Sequence<OUString> VCLXPrinterServer::getSupportedServiceNames()
367 return css::uno::Sequence<OUString>{
368 "com.sun.star.awt.PrinterServer", "stardiv.vcl.PrinterServer"};
371 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
372 stardiv_Toolkit_VCLXPrinterServer_get_implementation(
373 css::uno::XComponentContext *,
374 css::uno::Sequence<css::uno::Any> const &)
376 return cppu::acquire(new VCLXPrinterServer);
379 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */