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