Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <com/sun/star/uno/XComponentContext.hpp>
21 : #include <toolkit/awt/vclxprinter.hxx>
22 : #include <toolkit/helper/macros.hxx>
23 : #include <cppuhelper/supportsservice.hxx>
24 : #include <cppuhelper/typeprovider.hxx>
25 : #include <rtl/uuid.h>
26 :
27 :
28 : #include <vcl/print.hxx>
29 : #include <vcl/jobset.hxx>
30 : #include <vcl/svapp.hxx>
31 :
32 : #include <tools/debug.hxx>
33 : #include <tools/stream.hxx>
34 :
35 : #include <toolkit/awt/vclxdevice.hxx>
36 :
37 :
38 : #define BINARYSETUPMARKER 0x23864691
39 :
40 : #define PROPERTY_Orientation 0
41 : #define PROPERTY_Horizontal 1
42 :
43 0 : ::com::sun::star::beans::Property* ImplGetProperties( sal_uInt16& rElementCount )
44 : {
45 : static ::com::sun::star::beans::Property* pProperties = NULL;
46 : static sal_uInt16 nElements = 0;
47 0 : if( !pProperties )
48 : {
49 0 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
50 0 : if( !pProperties )
51 : {
52 : static ::com::sun::star::beans::Property aPropTable[] =
53 : {
54 0 : ::com::sun::star::beans::Property( OUString("Orientation"), PROPERTY_Orientation, cppu::UnoType<sal_Int16>::get(), 0 ),
55 0 : ::com::sun::star::beans::Property( OUString("Horizontal"), PROPERTY_Horizontal, cppu::UnoType<bool>::get(), 0 )
56 0 : };
57 0 : pProperties = aPropTable;
58 0 : nElements = sizeof( aPropTable ) / sizeof( ::com::sun::star::beans::Property );
59 0 : }
60 : }
61 0 : rElementCount = nElements;
62 0 : return pProperties;
63 : }
64 :
65 : // ----------------------------------------------------
66 : // class VCLXPrinterPropertySet
67 : // ----------------------------------------------------
68 :
69 0 : IMPLEMENT_FORWARD_XINTERFACE2( VCLXPrinterPropertySet, VCLXPrinterPropertySet_Base, OPropertySetHelper )
70 0 : IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXPrinterPropertySet, VCLXPrinterPropertySet_Base, ::cppu::OPropertySetHelper )
71 :
72 0 : VCLXPrinterPropertySet::VCLXPrinterPropertySet( const OUString& rPrinterName )
73 : : OPropertySetHelper( BrdcstHelper )
74 0 : , mxPrinter(VclPtrInstance< Printer >(rPrinterName))
75 : {
76 0 : SolarMutexGuard aSolarGuard;
77 :
78 0 : mnOrientation = 0;
79 0 : mbHorizontal = false;
80 0 : }
81 :
82 0 : VCLXPrinterPropertySet::~VCLXPrinterPropertySet()
83 : {
84 0 : SolarMutexGuard aSolarGuard;
85 0 : mxPrinter.reset();
86 0 : }
87 :
88 0 : ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > VCLXPrinterPropertySet::GetDevice()
89 : {
90 0 : if ( !mxPrnDevice.is() )
91 : {
92 0 : VCLXDevice* pDev = new VCLXDevice;
93 0 : pDev->SetOutputDevice( GetPrinter() );
94 0 : mxPrnDevice = pDev;
95 : }
96 0 : return mxPrnDevice;
97 : }
98 :
99 0 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > VCLXPrinterPropertySet::getPropertySetInfo( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
100 : {
101 0 : static ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
102 0 : return xInfo;
103 : }
104 :
105 0 : ::cppu::IPropertyArrayHelper& VCLXPrinterPropertySet::getInfoHelper()
106 : {
107 : static ::cppu::OPropertyArrayHelper* pPropertyArrayHelper = NULL;
108 0 : if ( !pPropertyArrayHelper )
109 : {
110 0 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
111 0 : if( !pPropertyArrayHelper )
112 : {
113 : sal_uInt16 nElements;
114 0 : ::com::sun::star::beans::Property* pProps = ImplGetProperties( nElements );
115 0 : pPropertyArrayHelper = new ::cppu::OPropertyArrayHelper( pProps, nElements, sal_False );
116 0 : }
117 : }
118 0 : return *pPropertyArrayHelper ;
119 : }
120 :
121 0 : sal_Bool VCLXPrinterPropertySet::convertFastPropertyValue( ::com::sun::star::uno::Any & rConvertedValue, ::com::sun::star::uno::Any & rOldValue, sal_Int32 nHandle, const ::com::sun::star::uno::Any& rValue ) throw (::com::sun::star::lang::IllegalArgumentException)
122 : {
123 0 : ::osl::MutexGuard aGuard( Mutex );
124 :
125 0 : bool bDifferent = false;
126 0 : switch ( nHandle )
127 : {
128 : case PROPERTY_Orientation:
129 : {
130 : sal_Int16 n;
131 0 : if( ( rValue >>= n ) && ( n != mnOrientation ) )
132 : {
133 0 : rConvertedValue <<= n;
134 0 : rOldValue <<= mnOrientation;
135 0 : bDifferent = true;
136 : }
137 : }
138 0 : break;
139 : case PROPERTY_Horizontal:
140 : {
141 : bool b;
142 0 : if( ( rValue >>= b ) && ( b != mbHorizontal ) )
143 : {
144 0 : rConvertedValue <<= b;
145 0 : rOldValue <<= mbHorizontal;
146 0 : bDifferent = true;
147 : }
148 : }
149 0 : break;
150 : default:
151 : {
152 : OSL_FAIL( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
153 : }
154 : }
155 0 : return bDifferent;
156 : }
157 :
158 0 : void VCLXPrinterPropertySet::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const ::com::sun::star::uno::Any& rValue ) throw (::com::sun::star::uno::Exception, std::exception)
159 : {
160 0 : ::osl::MutexGuard aGuard( Mutex );
161 :
162 0 : switch( nHandle )
163 : {
164 : case PROPERTY_Orientation:
165 : {
166 0 : rValue >>= mnOrientation;
167 : }
168 0 : break;
169 : case PROPERTY_Horizontal:
170 : {
171 0 : rValue >>= mbHorizontal;
172 : }
173 0 : break;
174 : default:
175 : {
176 : OSL_FAIL( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
177 : }
178 0 : }
179 0 : }
180 :
181 0 : void VCLXPrinterPropertySet::getFastPropertyValue( ::com::sun::star::uno::Any& rValue, sal_Int32 nHandle ) const
182 : {
183 0 : ::osl::MutexGuard aGuard( const_cast<VCLXPrinterPropertySet*>(this)->Mutex );
184 :
185 0 : switch( nHandle )
186 : {
187 : case PROPERTY_Orientation:
188 0 : rValue <<= mnOrientation;
189 0 : break;
190 : case PROPERTY_Horizontal:
191 0 : rValue <<= mbHorizontal;
192 0 : break;
193 : default:
194 : {
195 : OSL_FAIL( "VCLXPrinterPropertySet_Impl::convertFastPropertyValue - invalid Handle" );
196 : }
197 0 : }
198 0 : }
199 :
200 : // ::com::sun::star::awt::XPrinterPropertySet
201 0 : void VCLXPrinterPropertySet::setHorizontal( sal_Bool bHorizontal ) throw(::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception)
202 : {
203 0 : ::osl::MutexGuard aGuard( Mutex );
204 :
205 0 : ::com::sun::star::uno::Any aValue;
206 0 : aValue <<= bHorizontal;
207 0 : setFastPropertyValue( PROPERTY_Horizontal, aValue );
208 0 : }
209 :
210 0 : ::com::sun::star::uno::Sequence< OUString > VCLXPrinterPropertySet::getFormDescriptions( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
211 : {
212 0 : ::osl::MutexGuard aGuard( Mutex );
213 :
214 0 : sal_uInt16 nPaperBinCount = GetPrinter()->GetPaperBinCount();
215 0 : ::com::sun::star::uno::Sequence< OUString > aDescriptions( nPaperBinCount );
216 0 : for ( sal_uInt16 n = 0; n < nPaperBinCount; n++ )
217 : {
218 : // Format: <DisplayFormName;FormNameId;DisplayPaperBinName;PaperBinNameId;DisplayPaperName;PaperNameId>
219 0 : OUStringBuffer aDescr( "*;*;" );
220 0 : aDescr.append(GetPrinter()->GetPaperBinName( n ));
221 0 : aDescr.append(';');
222 0 : aDescr.append(OUString::number(n));
223 0 : aDescr.append(";*;*");
224 :
225 0 : aDescriptions.getArray()[n] = aDescr.makeStringAndClear();
226 0 : }
227 0 : return aDescriptions;
228 : }
229 :
230 0 : void VCLXPrinterPropertySet::selectForm( const OUString& rFormDescription ) throw(::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception)
231 : {
232 0 : ::osl::MutexGuard aGuard( Mutex );
233 :
234 0 : sal_Int32 nIndex = 0;
235 : sal_uInt16 nPaperBin = sal::static_int_cast< sal_uInt16 >(
236 0 : rFormDescription.getToken( 3, ';', nIndex ).toInt32());
237 0 : GetPrinter()->SetPaperBin( nPaperBin );
238 0 : }
239 :
240 0 : ::com::sun::star::uno::Sequence< sal_Int8 > VCLXPrinterPropertySet::getBinarySetup( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
241 : {
242 0 : ::osl::MutexGuard aGuard( Mutex );
243 :
244 0 : SvMemoryStream aMem;
245 0 : aMem.WriteUInt32( BINARYSETUPMARKER );
246 0 : WriteJobSetup( aMem, GetPrinter()->GetJobSetup() );
247 0 : return ::com::sun::star::uno::Sequence<sal_Int8>( static_cast<sal_Int8 const *>(aMem.GetData()), aMem.Tell() );
248 : }
249 :
250 0 : void VCLXPrinterPropertySet::setBinarySetup( const ::com::sun::star::uno::Sequence< sal_Int8 >& data ) throw(::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception)
251 : {
252 0 : ::osl::MutexGuard aGuard( Mutex );
253 :
254 0 : SvMemoryStream aMem( const_cast<signed char*>(data.getConstArray()), data.getLength(), StreamMode::READ );
255 : sal_uInt32 nMarker;
256 0 : aMem.ReadUInt32( nMarker );
257 : DBG_ASSERT( nMarker == BINARYSETUPMARKER, "setBinarySetup - invalid!" );
258 0 : if ( nMarker == BINARYSETUPMARKER )
259 : {
260 0 : JobSetup aSetup;
261 0 : ReadJobSetup( aMem, aSetup );
262 0 : GetPrinter()->SetJobSetup( aSetup );
263 0 : }
264 0 : }
265 :
266 :
267 : // ----------------------------------------------------
268 : // class VCLXPrinter
269 : // ----------------------------------------------------
270 0 : VCLXPrinter::VCLXPrinter( const OUString& rPrinterName )
271 0 : : VCLXPrinter_Base( rPrinterName )
272 : {
273 0 : }
274 :
275 0 : VCLXPrinter::~VCLXPrinter()
276 : {
277 0 : }
278 :
279 0 : sal_Bool VCLXPrinter::start( const OUString& /*rJobName*/, sal_Int16 /*nCopies*/, sal_Bool /*bCollate*/ ) throw(::com::sun::star::awt::PrinterException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception)
280 : {
281 0 : ::osl::MutexGuard aGuard( Mutex );
282 :
283 0 : bool bDone = true;
284 0 : if (mxPrinter.get())
285 : {
286 0 : maInitJobSetup = mxPrinter->GetJobSetup();
287 0 : mxListener.reset(new vcl::OldStylePrintAdaptor(mxPrinter));
288 : }
289 :
290 0 : return bDone;
291 : }
292 :
293 0 : void VCLXPrinter::end( ) throw(::com::sun::star::awt::PrinterException, ::com::sun::star::uno::RuntimeException, std::exception)
294 : {
295 0 : ::osl::MutexGuard aGuard( Mutex );
296 :
297 0 : if (mxListener.get())
298 : {
299 0 : Printer::PrintJob(mxListener, maInitJobSetup);
300 0 : mxListener.reset();
301 0 : }
302 0 : }
303 :
304 0 : void VCLXPrinter::terminate( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
305 : {
306 0 : ::osl::MutexGuard aGuard( Mutex );
307 :
308 0 : mxListener.reset();
309 0 : }
310 :
311 0 : ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > VCLXPrinter::startPage( ) throw(::com::sun::star::awt::PrinterException, ::com::sun::star::uno::RuntimeException, std::exception)
312 : {
313 0 : ::osl::MutexGuard aGuard( Mutex );
314 :
315 0 : if (mxListener.get())
316 : {
317 0 : mxListener->StartPage();
318 : }
319 0 : return GetDevice();
320 : }
321 :
322 0 : void VCLXPrinter::endPage( ) throw(::com::sun::star::awt::PrinterException, ::com::sun::star::uno::RuntimeException, std::exception)
323 : {
324 0 : ::osl::MutexGuard aGuard( Mutex );
325 :
326 0 : if (mxListener.get())
327 : {
328 0 : mxListener->EndPage();
329 0 : }
330 0 : }
331 :
332 :
333 : // ----------------------------------------------------
334 : // class VCLXInfoPrinter
335 : // ----------------------------------------------------
336 :
337 0 : VCLXInfoPrinter::VCLXInfoPrinter( const OUString& rPrinterName )
338 0 : : VCLXInfoPrinter_Base( rPrinterName )
339 : {
340 0 : }
341 :
342 0 : VCLXInfoPrinter::~VCLXInfoPrinter()
343 : {
344 0 : }
345 :
346 : // ::com::sun::star::awt::XInfoPrinter
347 0 : ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice > VCLXInfoPrinter::createDevice( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
348 : {
349 0 : ::osl::MutexGuard aGuard( Mutex );
350 :
351 0 : return GetDevice();
352 : }
353 :
354 : // ----------------------------------------------------
355 : // class VCLXPrinterServer
356 : // ----------------------------------------------------
357 :
358 : // ::com::sun::star::awt::XPrinterServer
359 0 : ::com::sun::star::uno::Sequence< OUString > VCLXPrinterServer::getPrinterNames( ) throw(::com::sun::star::uno::RuntimeException, std::exception)
360 : {
361 0 : const std::vector<OUString>& rQueues = Printer::GetPrinterQueues();
362 0 : sal_uInt32 nPrinters = rQueues.size();
363 :
364 0 : ::com::sun::star::uno::Sequence< OUString > aNames( nPrinters );
365 0 : for ( sal_uInt32 n = 0; n < nPrinters; n++ )
366 0 : aNames.getArray()[n] = rQueues[n];
367 :
368 0 : return aNames;
369 : }
370 :
371 0 : ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPrinter > VCLXPrinterServer::createPrinter( const OUString& rPrinterName ) throw(::com::sun::star::uno::RuntimeException, std::exception)
372 : {
373 0 : ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPrinter > xP;
374 0 : xP = new VCLXPrinter( rPrinterName );
375 0 : return xP;
376 : }
377 :
378 0 : ::com::sun::star::uno::Reference< ::com::sun::star::awt::XInfoPrinter > VCLXPrinterServer::createInfoPrinter( const OUString& rPrinterName ) throw(::com::sun::star::uno::RuntimeException, std::exception)
379 : {
380 0 : ::com::sun::star::uno::Reference< ::com::sun::star::awt::XInfoPrinter > xP;
381 0 : xP = new VCLXInfoPrinter( rPrinterName );
382 0 : return xP;
383 : }
384 :
385 1 : OUString VCLXPrinterServer::getImplementationName()
386 : throw (css::uno::RuntimeException, std::exception)
387 : {
388 1 : return OUString("stardiv.Toolkit.VCLXPrinterServer");
389 : }
390 :
391 0 : sal_Bool VCLXPrinterServer::supportsService(OUString const & ServiceName)
392 : throw (css::uno::RuntimeException, std::exception)
393 : {
394 0 : return cppu::supportsService(this, ServiceName);
395 : }
396 :
397 1 : css::uno::Sequence<OUString> VCLXPrinterServer::getSupportedServiceNames()
398 : throw (css::uno::RuntimeException, std::exception)
399 : {
400 : return css::uno::Sequence<OUString>{
401 1 : "com.sun.star.awt.PrinterServer", "stardiv.vcl.PrinterServer"};
402 : }
403 :
404 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
405 1 : stardiv_Toolkit_VCLXPrinterServer_get_implementation(
406 : css::uno::XComponentContext *,
407 : css::uno::Sequence<css::uno::Any> const &)
408 : {
409 1 : return cppu::acquire(new VCLXPrinterServer());
410 : }
411 :
412 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|