xref: /trunk/main/unotools/source/config/misccfg.cxx (revision b5088357)
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 
27 #include <unotools/misccfg.hxx>
28 #include "rtl/instance.hxx"
29 #include <unotools/configmgr.hxx>
30 #include <unotools/configitem.hxx>
31 #include <tools/debug.hxx>
32 #include <com/sun/star/uno/Any.hxx>
33 #include <com/sun/star/uno/Sequence.hxx>
34 #include <vos/mutex.hxx>
35 #include <osl/mutex.hxx>
36 #include <rtl/logfile.hxx>
37 #include "itemholder1.hxx"
38 
39 #define DEFAULT_TAB 2000
40 
41 #define DEF_INCH	2540L
42 #define DEF_RELTWIP	1440L
43 
44 using namespace rtl;
45 using namespace com::sun::star::uno;
46 
47 #define C2U(cChar) OUString::createFromAscii(cChar)
48 
49 namespace utl
50 {
51 
52 static SfxMiscCfg* pOptions = NULL;
53 static sal_Int32 nRefCount = 0;
54 
55 class SfxMiscCfg : public utl::ConfigItem
56 {
57 	sal_Bool			bPaperSize;     // printer warnings
58 	sal_Bool			bPaperOrientation;
59 	sal_Bool			bNotFound;
60 	sal_Int32		nYear2000;		// two digit year representation
61 
62 	const com::sun::star::uno::Sequence<rtl::OUString>& GetPropertyNames();
63 	void					Load();
64 
65 public:
66 	SfxMiscCfg( );
67 	~SfxMiscCfg( );
68 
69 	virtual void 			Notify( const com::sun::star::uno::Sequence<rtl::OUString>& aPropertyNames);
70 	virtual void			Commit();
71 
IsNotFoundWarning() const72 	sal_Bool		IsNotFoundWarning() 	const {return bNotFound;}
73 	void		SetNotFoundWarning(	sal_Bool bSet);
74 
IsPaperSizeWarning() const75 	sal_Bool		IsPaperSizeWarning() 	const {return bPaperSize;}
76 	void		SetPaperSizeWarning(sal_Bool bSet);
77 
IsPaperOrientationWarning() const78 	sal_Bool		IsPaperOrientationWarning() 	const {return bPaperOrientation;}
79 	void		SetPaperOrientationWarning(	sal_Bool bSet);
80 
81 				// 0 ... 99
GetYear2000() const82 	sal_Int32	GetYear2000()			const { return nYear2000; }
83     void        SetYear2000( sal_Int32 nSet );
84 
85 };
86 
87 /*--------------------------------------------------------------------
88 	 Beschreibung:
89  --------------------------------------------------------------------*/
SfxMiscCfg()90 SfxMiscCfg::SfxMiscCfg() :
91 	ConfigItem(C2U("Office.Common") ),
92 	bPaperSize(sal_False),
93 	bPaperOrientation (sal_False),
94 	bNotFound (sal_False),
95 	nYear2000( 1930 )
96 {
97 	RTL_LOGFILE_CONTEXT(aLog, "svl SfxMiscCfg::SfxMiscCfg()");
98 
99 	Load();
100 }
101 /* -----------------------------02.03.01 15:31--------------------------------
102 
103  ---------------------------------------------------------------------------*/
~SfxMiscCfg()104 SfxMiscCfg::~SfxMiscCfg()
105 {
106 }
107 /*--------------------------------------------------------------------
108 	 Beschreibung:
109  --------------------------------------------------------------------*/
110 
SetNotFoundWarning(sal_Bool bSet)111 void SfxMiscCfg::SetNotFoundWarning( sal_Bool bSet)
112 {
113 	if(bNotFound != bSet)
114 		SetModified();
115 	bNotFound = bSet;
116 }
117 
118 /*--------------------------------------------------------------------
119 	 Beschreibung:
120  --------------------------------------------------------------------*/
121 
SetPaperSizeWarning(sal_Bool bSet)122 void SfxMiscCfg::SetPaperSizeWarning( sal_Bool bSet)
123 {
124 	if(bPaperSize != bSet)
125 		SetModified();
126 	bPaperSize = bSet;
127 }
128 
129 /*--------------------------------------------------------------------
130 	 Beschreibung:
131  --------------------------------------------------------------------*/
SetPaperOrientationWarning(sal_Bool bSet)132 void SfxMiscCfg::SetPaperOrientationWarning( sal_Bool bSet)
133 {
134 	if(bPaperOrientation != bSet)
135 		SetModified();
136 	bPaperOrientation = bSet;
137 }
138 /*--------------------------------------------------------------------
139 	 Beschreibung:
140  --------------------------------------------------------------------*/
141 
SetYear2000(sal_Int32 nSet)142 void SfxMiscCfg::SetYear2000( sal_Int32 nSet )
143 {
144 	if(nYear2000 != nSet)
145 		SetModified();
146 	nYear2000 = nSet;
147 }
148 /* -----------------------------02.03.01 15:31--------------------------------
149 
150  ---------------------------------------------------------------------------*/
GetPropertyNames()151 const Sequence<OUString>& SfxMiscCfg::GetPropertyNames()
152 {
153 	static Sequence<OUString> aNames;
154 	if(!aNames.getLength())
155 	{
156 		static const char* aPropNames[] =
157 		{
158    			"Print/Warning/PaperSize",				//  0
159    			"Print/Warning/PaperOrientation",		//  1
160    			"Print/Warning/NotFound",				//  2
161 			"DateFormat/TwoDigitYear",            	//  3
162 		};
163         const int nCount = 4;
164 		aNames.realloc(nCount);
165 		OUString* pNames = aNames.getArray();
166 		for(int i = 0; i < nCount; i++)
167 			pNames[i] = OUString::createFromAscii(aPropNames[i]);
168 	}
169 	return aNames;
170 }
171 /* -----------------------------02.03.01 15:31--------------------------------
172 
173  ---------------------------------------------------------------------------*/
Load()174 void SfxMiscCfg::Load()
175 {
176 	const Sequence<OUString>& aNames = GetPropertyNames();
177 	Sequence<Any> aValues = GetProperties(aNames);
178 	EnableNotification(aNames);
179 	const Any* pValues = aValues.getConstArray();
180 	DBG_ASSERT(aValues.getLength() == aNames.getLength(), "GetProperties failed");
181 	if(aValues.getLength() == aNames.getLength())
182 	{
183 		for(int nProp = 0; nProp < aNames.getLength(); nProp++)
184 		{
185 			if(pValues[nProp].hasValue())
186 			{
187 				switch(nProp)
188 				{
189 					case  0: bPaperSize 	   = *(sal_Bool*)pValues[nProp].getValue(); break;		//"Print/Warning/PaperSize",
190                     case  1: bPaperOrientation = *(sal_Bool*)pValues[nProp].getValue();  break;     //"Print/Warning/PaperOrientation",
191                     case  2: bNotFound         = *(sal_Bool*)pValues[nProp].getValue()  ;  break;   //"Print/Warning/NotFound",
192 					case  3: pValues[nProp] >>= nYear2000;break;                                    //"DateFormat/TwoDigitYear",
193 				}
194 			}
195 		}
196 	}
197 }
198 /* -----------------------------02.03.01 15:31--------------------------------
199 
200  ---------------------------------------------------------------------------*/
Notify(const com::sun::star::uno::Sequence<rtl::OUString> &)201 void SfxMiscCfg::Notify( const com::sun::star::uno::Sequence<rtl::OUString>& )
202 {
203 	Load();
204 }
205 /* -----------------------------02.03.01 15:31--------------------------------
206 
207  ---------------------------------------------------------------------------*/
Commit()208 void SfxMiscCfg::Commit()
209 {
210 	const Sequence<OUString>& aNames = GetPropertyNames();
211 	Sequence<Any> aValues(aNames.getLength());
212 	Any* pValues = aValues.getArray();
213 
214 	const Type& rType = ::getBooleanCppuType();
215 	for(int nProp = 0; nProp < aNames.getLength(); nProp++)
216 	{
217 		switch(nProp)
218 		{
219 			case  0: pValues[nProp].setValue(&bPaperSize, rType);break;	 //"Print/Warning/PaperSize",
220 			case  1: pValues[nProp].setValue(&bPaperOrientation, rType);break;     //"Print/Warning/PaperOrientation",
221 			case  2: pValues[nProp].setValue(&bNotFound, rType);break;   //"Print/Warning/NotFound",
222 			case  3: pValues[nProp] <<= nYear2000;break;                 //"DateFormat/TwoDigitYear",
223 		}
224 	}
225 	PutProperties(aNames, aValues);
226 }
227 // -----------------------------------------------------------------------
228 namespace
229 {
230     class LocalSingleton : public rtl::Static< osl::Mutex, LocalSingleton >
231     {
232     };
233 }
234 
MiscCfg()235 MiscCfg::MiscCfg( )
236 {
237     // Global access, must be guarded (multithreading)
238     ::osl::MutexGuard aGuard( LocalSingleton::get() );
239     if ( !pOptions )
240     {
241         RTL_LOGFILE_CONTEXT(aLog, "unotools ( ??? ) SfxMiscCfg::ctor()");
242         pOptions = new SfxMiscCfg;
243 
244         ItemHolder1::holdConfigItem(E_MISCCFG);
245     }
246 
247     ++nRefCount;
248     pImpl = pOptions;
249     pImpl->AddListener(this);
250 }
251 
~MiscCfg()252 MiscCfg::~MiscCfg( )
253 {
254     // Global access, must be guarded (multithreading)
255     ::osl::MutexGuard aGuard( LocalSingleton::get() );
256     pImpl->RemoveListener(this);
257     if ( !--nRefCount )
258 	{
259 		if ( pOptions->IsModified() )
260 			pOptions->Commit();
261         DELETEZ( pOptions );
262 	}
263 }
264 
IsNotFoundWarning() const265 sal_Bool MiscCfg::IsNotFoundWarning() 	const
266 {
267 	return pImpl->IsNotFoundWarning();
268 }
269 
SetNotFoundWarning(sal_Bool bSet)270 void MiscCfg::SetNotFoundWarning(	sal_Bool bSet)
271 {
272 	pImpl->SetNotFoundWarning( bSet );
273 }
274 
IsPaperSizeWarning() const275 sal_Bool MiscCfg::IsPaperSizeWarning() 	const
276 {
277 	return pImpl->IsPaperSizeWarning();
278 }
279 
SetPaperSizeWarning(sal_Bool bSet)280 void MiscCfg::SetPaperSizeWarning(sal_Bool bSet)
281 {
282 	pImpl->SetPaperSizeWarning( bSet );
283 }
284 
IsPaperOrientationWarning() const285 sal_Bool MiscCfg::IsPaperOrientationWarning() 	const
286 {
287 	return pImpl->IsPaperOrientationWarning();
288 }
289 
SetPaperOrientationWarning(sal_Bool bSet)290 void MiscCfg::SetPaperOrientationWarning(	sal_Bool bSet)
291 {
292 	pImpl->SetPaperOrientationWarning( bSet );
293 }
294 
GetYear2000() const295 sal_Int32 MiscCfg::GetYear2000() const
296 {
297 	return pImpl->GetYear2000();
298 }
299 
SetYear2000(sal_Int32 nSet)300 void MiscCfg::SetYear2000( sal_Int32 nSet )
301 {
302 	pImpl->SetYear2000( nSet );
303 }
304 
305 }
306 
307