Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / unotools / source / config / misccfg.cxx
blob720cfcff740362f9ddb3f606952f15a32e2044b1
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 .
21 #include <unotools/misccfg.hxx>
22 #include "rtl/instance.hxx"
23 #include <unotools/configmgr.hxx>
24 #include <unotools/configitem.hxx>
25 #include <tools/debug.hxx>
26 #include <com/sun/star/uno/Any.hxx>
27 #include <com/sun/star/uno/Sequence.hxx>
28 #include <osl/mutex.hxx>
29 #include <osl/mutex.hxx>
30 #include <rtl/logfile.hxx>
31 #include "itemholder1.hxx"
33 using namespace com::sun::star::uno;
35 using ::rtl::OUString;
37 namespace utl
40 static SfxMiscCfg* pOptions = NULL;
41 static sal_Int32 nRefCount = 0;
43 class SfxMiscCfg : public utl::ConfigItem
45 sal_Bool bPaperSize; // printer warnings
46 sal_Bool bPaperOrientation;
47 sal_Bool bNotFound;
48 sal_Int32 nYear2000; // two digit year representation
50 const com::sun::star::uno::Sequence<rtl::OUString> GetPropertyNames();
51 void Load();
53 public:
54 SfxMiscCfg( );
55 ~SfxMiscCfg( );
57 virtual void Notify( const com::sun::star::uno::Sequence<rtl::OUString>& aPropertyNames);
58 virtual void Commit();
60 sal_Bool IsNotFoundWarning() const {return bNotFound;}
61 void SetNotFoundWarning( sal_Bool bSet);
63 sal_Bool IsPaperSizeWarning() const {return bPaperSize;}
64 void SetPaperSizeWarning(sal_Bool bSet);
66 sal_Bool IsPaperOrientationWarning() const {return bPaperOrientation;}
67 void SetPaperOrientationWarning( sal_Bool bSet);
69 // 0 ... 99
70 sal_Int32 GetYear2000() const { return nYear2000; }
71 void SetYear2000( sal_Int32 nSet );
75 SfxMiscCfg::SfxMiscCfg() :
76 ConfigItem(OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Common")) ),
77 bPaperSize(sal_False),
78 bPaperOrientation (sal_False),
79 bNotFound (sal_False),
80 nYear2000( 1930 )
82 RTL_LOGFILE_CONTEXT(aLog, "svl SfxMiscCfg::SfxMiscCfg()");
84 Load();
87 SfxMiscCfg::~SfxMiscCfg()
91 void SfxMiscCfg::SetNotFoundWarning( sal_Bool bSet)
93 if(bNotFound != bSet)
94 SetModified();
95 bNotFound = bSet;
98 void SfxMiscCfg::SetPaperSizeWarning( sal_Bool bSet)
100 if(bPaperSize != bSet)
101 SetModified();
102 bPaperSize = bSet;
105 void SfxMiscCfg::SetPaperOrientationWarning( sal_Bool bSet)
107 if(bPaperOrientation != bSet)
108 SetModified();
109 bPaperOrientation = bSet;
112 void SfxMiscCfg::SetYear2000( sal_Int32 nSet )
114 if(nYear2000 != nSet)
115 SetModified();
116 nYear2000 = nSet;
119 const Sequence<OUString> SfxMiscCfg::GetPropertyNames()
121 const OUString pProperties[] =
123 OUString(RTL_CONSTASCII_USTRINGPARAM("Print/Warning/PaperSize")),
124 OUString(RTL_CONSTASCII_USTRINGPARAM("Print/Warning/PaperOrientation")),
125 OUString(RTL_CONSTASCII_USTRINGPARAM("Print/Warning/NotFound")),
126 OUString(RTL_CONSTASCII_USTRINGPARAM("DateFormat/TwoDigitYear"))
128 const Sequence< OUString > seqPropertyNames( pProperties, 4 );
129 return seqPropertyNames;
132 void SfxMiscCfg::Load()
134 const Sequence<OUString>& rNames = GetPropertyNames();
135 Sequence<Any> aValues = GetProperties(rNames);
136 EnableNotification(rNames);
137 const Any* pValues = aValues.getConstArray();
138 DBG_ASSERT(aValues.getLength() == rNames.getLength(), "GetProperties failed");
139 if(aValues.getLength() == rNames.getLength())
141 for(int nProp = 0; nProp < rNames.getLength(); nProp++)
143 if(pValues[nProp].hasValue())
145 switch(nProp)
147 case 0: bPaperSize = *(sal_Bool*)pValues[nProp].getValue(); break; //"Print/Warning/PaperSize",
148 case 1: bPaperOrientation = *(sal_Bool*)pValues[nProp].getValue(); break; //"Print/Warning/PaperOrientation",
149 case 2: bNotFound = *(sal_Bool*)pValues[nProp].getValue() ; break; //"Print/Warning/NotFound",
150 case 3: pValues[nProp] >>= nYear2000;break; //"DateFormat/TwoDigitYear",
157 void SfxMiscCfg::Notify( const com::sun::star::uno::Sequence<rtl::OUString>& )
159 Load();
162 void SfxMiscCfg::Commit()
164 const Sequence<OUString>& rNames = GetPropertyNames();
165 Sequence<Any> aValues(rNames.getLength());
166 Any* pValues = aValues.getArray();
168 const Type& rType = ::getBooleanCppuType();
169 for(int nProp = 0; nProp < rNames.getLength(); nProp++)
171 switch(nProp)
173 case 0: pValues[nProp].setValue(&bPaperSize, rType);break; //"Print/Warning/PaperSize",
174 case 1: pValues[nProp].setValue(&bPaperOrientation, rType);break; //"Print/Warning/PaperOrientation",
175 case 2: pValues[nProp].setValue(&bNotFound, rType);break; //"Print/Warning/NotFound",
176 case 3: pValues[nProp] <<= nYear2000;break; //"DateFormat/TwoDigitYear",
179 PutProperties(rNames, aValues);
181 // -----------------------------------------------------------------------
182 namespace
184 class LocalSingleton : public rtl::Static< osl::Mutex, LocalSingleton >
189 MiscCfg::MiscCfg( )
191 // Global access, must be guarded (multithreading)
192 ::osl::MutexGuard aGuard( LocalSingleton::get() );
193 if ( !pOptions )
195 RTL_LOGFILE_CONTEXT(aLog, "unotools ( ??? ) SfxMiscCfg::ctor()");
196 pOptions = new SfxMiscCfg;
198 ItemHolder1::holdConfigItem(E_MISCCFG);
201 ++nRefCount;
202 pImpl = pOptions;
203 pImpl->AddListener(this);
206 MiscCfg::~MiscCfg( )
208 // Global access, must be guarded (multithreading)
209 ::osl::MutexGuard aGuard( LocalSingleton::get() );
210 pImpl->RemoveListener(this);
211 if ( !--nRefCount )
213 if ( pOptions->IsModified() )
214 pOptions->Commit();
215 DELETEZ( pOptions );
219 sal_Bool MiscCfg::IsNotFoundWarning() const
221 return pImpl->IsNotFoundWarning();
224 void MiscCfg::SetNotFoundWarning( sal_Bool bSet)
226 pImpl->SetNotFoundWarning( bSet );
229 sal_Bool MiscCfg::IsPaperSizeWarning() const
231 return pImpl->IsPaperSizeWarning();
234 void MiscCfg::SetPaperSizeWarning(sal_Bool bSet)
236 pImpl->SetPaperSizeWarning( bSet );
239 sal_Bool MiscCfg::IsPaperOrientationWarning() const
241 return pImpl->IsPaperOrientationWarning();
244 void MiscCfg::SetPaperOrientationWarning( sal_Bool bSet)
246 pImpl->SetPaperOrientationWarning( bSet );
249 sal_Int32 MiscCfg::GetYear2000() const
251 return pImpl->GetYear2000();
254 void MiscCfg::SetYear2000( sal_Int32 nSet )
256 pImpl->SetYear2000( nSet );
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */