1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_unotools.hxx"
30 #ifndef GCC
31 #endif
32 
33 //_________________________________________________________________________________________________________________
34 //	includes
35 //_________________________________________________________________________________________________________________
36 
37 #include <unotools/printwarningoptions.hxx>
38 #include <unotools/configmgr.hxx>
39 #include <unotools/configitem.hxx>
40 #include <tools/debug.hxx>
41 #include <com/sun/star/uno/Any.hxx>
42 #include <com/sun/star/uno/Sequence.hxx>
43 
44 #include <itemholder1.hxx>
45 
46 //_________________________________________________________________________________________________________________
47 //	namespaces
48 //_________________________________________________________________________________________________________________
49 
50 using namespace ::utl					;
51 using namespace ::rtl					;
52 using namespace ::osl					;
53 using namespace ::com::sun::star::uno	;
54 
55 //_________________________________________________________________________________________________________________
56 //	const
57 //_________________________________________________________________________________________________________________
58 
59 #define ROOTNODE_START                  OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Common/Print"))
60 
61 #define PROPERTYNAME_PAPERSIZE          OUString(RTL_CONSTASCII_USTRINGPARAM("Warning/PaperSize"))
62 #define PROPERTYNAME_PAPERORIENTATION   OUString(RTL_CONSTASCII_USTRINGPARAM("Warning/PaperOrientation"))
63 #define PROPERTYNAME_NOTFOUND           OUString(RTL_CONSTASCII_USTRINGPARAM("Warning/NotFound"))
64 #define PROPERTYNAME_TRANSPARENCY       OUString(RTL_CONSTASCII_USTRINGPARAM("Warning/Transparency"))
65 #define PROPERTYNAME_PRINTINGMODIFIESDOCUMENT  OUString(RTL_CONSTASCII_USTRINGPARAM("PrintingModifiesDocument"))
66 
67 #define	PROPERTYHANDLE_PAPERSIZE		0
68 #define	PROPERTYHANDLE_PAPERORIENTATION	1
69 #define	PROPERTYHANDLE_NOTFOUND	        2
70 #define	PROPERTYHANDLE_TRANSPARENCY 	3
71 #define PROPERTYHDL_PRINTINGMODIFIESDOCUMENT            4
72 
73 #define PROPERTYCOUNT                   5
74 
75 class SvtPrintWarningOptions_Impl : public ConfigItem
76 {
77 public:
78 
79 //---------------------------------------------------------------------------------------------------------
80 //	constructor / destructor
81 //---------------------------------------------------------------------------------------------------------
82 
83 	 SvtPrintWarningOptions_Impl();
84 	~SvtPrintWarningOptions_Impl();
85 
86 //---------------------------------------------------------------------------------------------------------
87 //	overloaded methods of baseclass
88 //---------------------------------------------------------------------------------------------------------
89 
90 	virtual void Commit();
91     virtual void    Notify( const com::sun::star::uno::Sequence< rtl::OUString >& aPropertyNames );
92 
93 //---------------------------------------------------------------------------------------------------------
94 //	public interface
95 //---------------------------------------------------------------------------------------------------------
96 
97 	sal_Bool	IsPaperSize() const { return m_bPaperSize; }
98 	sal_Bool	IsPaperOrientation() const { return m_bPaperOrientation; }
99 	sal_Bool	IsNotFound() const { return m_bNotFound; }
100 	sal_Bool	IsTransparency() const { return m_bTransparency; }
101     sal_Bool    IsModifyDocumentOnPrintingAllowed() const { return m_bModifyDocumentOnPrintingAllowed; }
102 
103 	void		SetPaperSize( sal_Bool bState ) { m_bPaperSize = bState; SetModified(); }
104 	void		SetPaperOrientation( sal_Bool bState ) { m_bPaperOrientation = bState; SetModified(); }
105 	void		SetNotFound( sal_Bool bState ) { m_bNotFound = bState; SetModified(); }
106 	void		SetTransparency( sal_Bool bState ) { m_bTransparency = bState; SetModified(); }
107     void        SetModifyDocumentOnPrintingAllowed( sal_Bool bState ) { m_bModifyDocumentOnPrintingAllowed = bState; SetModified(); }
108 
109 //-------------------------------------------------------------------------------------------------------------
110 //	private methods
111 //-------------------------------------------------------------------------------------------------------------
112 
113 private:
114 
115 	static Sequence< OUString > impl_GetPropertyNames();
116 
117 //-------------------------------------------------------------------------------------------------------------
118 //	private member
119 //-------------------------------------------------------------------------------------------------------------
120 
121 private:
122 
123 	sal_Bool	m_bPaperSize;
124 	sal_Bool	m_bPaperOrientation;
125 	sal_Bool	m_bNotFound;
126 	sal_Bool	m_bTransparency;
127     sal_Bool    m_bModifyDocumentOnPrintingAllowed;
128 };
129 
130 //_________________________________________________________________________________________________________________
131 //	definitions
132 //_________________________________________________________________________________________________________________
133 
134 //*****************************************************************************************************************
135 //	constructor
136 //*****************************************************************************************************************
137 SvtPrintWarningOptions_Impl::SvtPrintWarningOptions_Impl() :
138 	ConfigItem( ROOTNODE_START	),
139 	m_bPaperSize( sal_False ),
140 	m_bPaperOrientation( sal_False ),
141 	m_bNotFound( sal_False ),
142     m_bTransparency( sal_True ),
143     m_bModifyDocumentOnPrintingAllowed( sal_True )
144 {
145 	Sequence< OUString >	seqNames( impl_GetPropertyNames() );
146 	Sequence< Any >			seqValues( GetProperties( seqNames ) );
147 
148 	DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtPrintWarningOptions_Impl::SvtPrintWarningOptions_Impl()\nI miss some values of configuration keys!\n" );
149 
150 	// Copy values from list in right order to our internal member.
151 	sal_Int32 nPropertyCount = seqValues.getLength();
152 	sal_Int32 nProperty	= 0;
153 
154 	for( nProperty=0; nProperty<nPropertyCount; ++nProperty )
155 	{
156 		DBG_ASSERT( !(seqValues[nProperty].hasValue()==sal_False), "SvtPrintWarningOptions_Impl::SvtPrintWarningOptions_Impl()\nInvalid property value for property detected!\n" );
157 
158         switch( nProperty )
159         {
160             case PROPERTYHANDLE_PAPERSIZE:
161 			{
162 				DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "Invalid type" );
163 				seqValues[nProperty] >>= m_bPaperSize;
164 			}
165 			break;
166 
167             case PROPERTYHANDLE_PAPERORIENTATION:
168 			{
169 				DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "Invalid type" );
170 				seqValues[nProperty] >>= m_bPaperOrientation;
171 			}
172 			break;
173 
174             case PROPERTYHANDLE_NOTFOUND:
175 			{
176 				DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "Invalid type" );
177 				seqValues[nProperty] >>= m_bNotFound;
178 			}
179 			break;
180 
181             case PROPERTYHANDLE_TRANSPARENCY:
182 			{
183 				DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "Invalid type" );
184 				seqValues[nProperty] >>= m_bTransparency;
185 			}
186 			break;
187             case PROPERTYHDL_PRINTINGMODIFIESDOCUMENT:
188             {
189 				DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "Invalid type" );
190                 seqValues[nProperty] >>= m_bModifyDocumentOnPrintingAllowed;
191 			}
192 			break;
193 
194         }
195 	}
196 }
197 
198 //*****************************************************************************************************************
199 //	destructor
200 //*****************************************************************************************************************
201 SvtPrintWarningOptions_Impl::~SvtPrintWarningOptions_Impl()
202 {
203 	if( IsModified() )
204 		Commit();
205 }
206 
207 //*****************************************************************************************************************
208 //	Commit
209 //*****************************************************************************************************************
210 void SvtPrintWarningOptions_Impl::Commit()
211 {
212 	Sequence< OUString >	aSeqNames( impl_GetPropertyNames() );
213 	Sequence< Any >			aSeqValues( aSeqNames.getLength() );
214 
215 	for( sal_Int32 nProperty = 0, nCount = aSeqNames.getLength(); nProperty < nCount; ++nProperty )
216 	{
217         switch( nProperty )
218         {
219             case PROPERTYHANDLE_PAPERSIZE:
220 				aSeqValues[nProperty] <<= m_bPaperSize;
221 			break;
222 
223             case PROPERTYHANDLE_PAPERORIENTATION:
224 				aSeqValues[nProperty] <<= m_bPaperOrientation;
225 			break;
226 
227             case PROPERTYHANDLE_NOTFOUND:
228 				aSeqValues[nProperty] <<= m_bNotFound;
229 			break;
230 
231             case PROPERTYHANDLE_TRANSPARENCY:
232 				aSeqValues[nProperty] <<= m_bTransparency;
233 			break;
234             case PROPERTYHDL_PRINTINGMODIFIESDOCUMENT:
235                 aSeqValues[nProperty] <<= m_bModifyDocumentOnPrintingAllowed;
236 			break;
237         }
238 	}
239 
240 	PutProperties( aSeqNames, aSeqValues );
241 }
242 
243 void SvtPrintWarningOptions_Impl::Notify( const Sequence< rtl::OUString >&  )
244 {
245 }
246 
247 //*****************************************************************************************************************
248 //	private method
249 //*****************************************************************************************************************
250 Sequence< OUString > SvtPrintWarningOptions_Impl::impl_GetPropertyNames()
251 {
252 	// Build static list of configuration key names.
253 	static const OUString pProperties[] =
254 	{
255 		PROPERTYNAME_PAPERSIZE,
256 		PROPERTYNAME_PAPERORIENTATION,
257 		PROPERTYNAME_NOTFOUND,
258         PROPERTYNAME_TRANSPARENCY,
259         PROPERTYNAME_PRINTINGMODIFIESDOCUMENT
260 	};
261 
262     // Initialize return sequence with these list ...
263 	static const Sequence< OUString > seqPropertyNames( pProperties, PROPERTYCOUNT );
264 
265 	return seqPropertyNames;
266 }
267 
268 //*****************************************************************************************************************
269 //	initialize static member
270 //	DON'T DO IT IN YOUR HEADER!
271 //	see definition for further informations
272 //*****************************************************************************************************************
273 SvtPrintWarningOptions_Impl*    SvtPrintWarningOptions::m_pDataContainer = NULL;
274 sal_Int32				        SvtPrintWarningOptions::m_nRefCount = 0;
275 
276 //*****************************************************************************************************************
277 //	constructor
278 //*****************************************************************************************************************
279 SvtPrintWarningOptions::SvtPrintWarningOptions()
280 {
281     // Global access, must be guarded (multithreading!).
282     MutexGuard aGuard( GetOwnStaticMutex() );
283 	// Increase ouer refcount ...
284 	++m_nRefCount;
285 	// ... and initialize ouer data container only if it not already!
286     if( m_pDataContainer == NULL )
287 	{
288         m_pDataContainer = new SvtPrintWarningOptions_Impl();
289 		ItemHolder1::holdConfigItem(E_PRINTWARNINGOPTIONS);
290 	}
291 }
292 
293 //*****************************************************************************************************************
294 //	destructor
295 //*****************************************************************************************************************
296 SvtPrintWarningOptions::~SvtPrintWarningOptions()
297 {
298     // Global access, must be guarded (multithreading!)
299     MutexGuard aGuard( GetOwnStaticMutex() );
300 	// Decrease ouer refcount.
301 	--m_nRefCount;
302 	// If last instance was deleted ...
303 	// we must destroy ouer static data container!
304     if( m_nRefCount <= 0 )
305 	{
306 		delete m_pDataContainer;
307 		m_pDataContainer = NULL;
308 	}
309 }
310 
311 //*****************************************************************************************************************
312 //	public method
313 //*****************************************************************************************************************
314 sal_Bool SvtPrintWarningOptions::IsPaperSize() const
315 {
316     MutexGuard aGuard( GetOwnStaticMutex() );
317 	return m_pDataContainer->IsPaperSize();
318 }
319 
320 //*****************************************************************************************************************
321 //	public method
322 //*****************************************************************************************************************
323 sal_Bool SvtPrintWarningOptions::IsPaperOrientation() const
324 {
325     MutexGuard aGuard( GetOwnStaticMutex() );
326 	return m_pDataContainer->IsPaperOrientation();
327 }
328 
329 //*****************************************************************************************************************
330 //	public method
331 //*****************************************************************************************************************
332 sal_Bool SvtPrintWarningOptions::IsNotFound() const
333 {
334     MutexGuard aGuard( GetOwnStaticMutex() );
335 	return m_pDataContainer->IsNotFound();
336 }
337 
338 //*****************************************************************************************************************
339 //	public method
340 //*****************************************************************************************************************
341 sal_Bool SvtPrintWarningOptions::IsTransparency() const
342 {
343     MutexGuard aGuard( GetOwnStaticMutex() );
344 	return m_pDataContainer->IsTransparency();
345 }
346 
347 //*****************************************************************************************************************
348 //	public method
349 //*****************************************************************************************************************
350 void SvtPrintWarningOptions::SetPaperSize( sal_Bool bState )
351 {
352     MutexGuard aGuard( GetOwnStaticMutex() );
353 	m_pDataContainer->SetPaperSize( bState );
354 }
355 
356 //*****************************************************************************************************************
357 //	public method
358 //*****************************************************************************************************************
359 void SvtPrintWarningOptions::SetPaperOrientation( sal_Bool bState )
360 {
361     MutexGuard aGuard( GetOwnStaticMutex() );
362 	m_pDataContainer->SetPaperOrientation( bState );
363 }
364 
365 //*****************************************************************************************************************
366 //	public method
367 //*****************************************************************************************************************
368 void SvtPrintWarningOptions::SetNotFound( sal_Bool bState )
369 {
370     MutexGuard aGuard( GetOwnStaticMutex() );
371 	m_pDataContainer->SetNotFound( bState );
372 }
373 
374 //*****************************************************************************************************************
375 //	public method
376 //*****************************************************************************************************************
377 void SvtPrintWarningOptions::SetTransparency( sal_Bool bState )
378 {
379     MutexGuard aGuard( GetOwnStaticMutex() );
380 	m_pDataContainer->SetTransparency( bState );
381 }
382 // -----------------------------------------------------------------------------
383 
384 sal_Bool SvtPrintWarningOptions::IsModifyDocumentOnPrintingAllowed() const
385 {
386     MutexGuard aGuard( GetOwnStaticMutex() );
387     return m_pDataContainer->IsModifyDocumentOnPrintingAllowed();
388 }
389 
390 // -----------------------------------------------------------------------------
391 
392 void SvtPrintWarningOptions::SetModifyDocumentOnPrintingAllowed( sal_Bool bState )
393 {
394     MutexGuard aGuard( GetOwnStaticMutex() );
395     m_pDataContainer->SetModifyDocumentOnPrintingAllowed( bState ) ;
396 }
397 
398 //*****************************************************************************************************************
399 //	private method
400 //*****************************************************************************************************************
401 Mutex& SvtPrintWarningOptions::GetOwnStaticMutex()
402 {
403 	// Initialize static mutex only for one time!
404     static Mutex* pMutex = NULL;
405 	// If these method first called (Mutex not already exist!) ...
406     if( pMutex == NULL )
407     {
408 		// ... we must create a new one. Protect follow code with the global mutex -
409 		// It must be - we create a static variable!
410         MutexGuard aGuard( Mutex::getGlobalMutex() );
411 		// We must check our pointer again - because it can be that another instance of ouer class will be fastr then these!
412         if( pMutex == NULL )
413         {
414 			// Create the new mutex and set it for return on static variable.
415             static Mutex aMutex;
416             pMutex = &aMutex;
417         }
418     }
419 	// Return new created or already existing mutex object.
420     return *pMutex;
421 }
422