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 <sal/config.h>
21 
22 #include <o3tl/any.hxx>
23 #include <unotools/misccfg.hxx>
24 #include <rtl/instance.hxx>
25 #include <unotools/configitem.hxx>
26 #include <tools/debug.hxx>
27 #include <com/sun/star/uno/Sequence.hxx>
28 #include <osl/mutex.hxx>
29 #include "itemholder1.hxx"
30 
31 using namespace com::sun::star::uno;
32 
33 namespace utl
34 {
35 class SfxMiscCfg;
36 
37 static std::weak_ptr<SfxMiscCfg> g_pOptions;
38 
39 class SfxMiscCfg : public utl::ConfigItem
40 {
41 private:
42     bool            bPaperSize;     // printer warnings
43     bool            bPaperOrientation;
44     bool            bNotFound;
45     sal_Int32       nYear2000;      // two digit year representation
46 
47     static css::uno::Sequence<OUString> GetPropertyNames();
48     void                    Load();
49 
50     virtual void            ImplCommit() final override;
51 
52 public:
53     SfxMiscCfg( );
54     virtual ~SfxMiscCfg( ) override;
55 
56     virtual void            Notify( const css::uno::Sequence<OUString>& aPropertyNames) override;
57 
IsNotFoundWarning() const58     bool        IsNotFoundWarning()     const {return bNotFound;}
59     void        SetNotFoundWarning( bool bSet);
60 
IsPaperSizeWarning() const61     bool        IsPaperSizeWarning()    const {return bPaperSize;}
62     void        SetPaperSizeWarning(bool bSet);
63 
IsPaperOrientationWarning() const64     bool        IsPaperOrientationWarning()     const {return bPaperOrientation;}
65     void        SetPaperOrientationWarning( bool bSet);
66 
67                 // 0 ... 99
GetYear2000() const68     sal_Int32   GetYear2000()           const { return nYear2000; }
69     void        SetYear2000( sal_Int32 nSet );
70 
71 };
72 
SfxMiscCfg()73 SfxMiscCfg::SfxMiscCfg() :
74     ConfigItem( "Office.Common" ),
75     bPaperSize(false),
76     bPaperOrientation (false),
77     bNotFound (false),
78     nYear2000( 1930 )
79 {
80     Load();
81 }
82 
~SfxMiscCfg()83 SfxMiscCfg::~SfxMiscCfg()
84 {
85     if ( IsModified() )
86         Commit();
87 }
88 
SetNotFoundWarning(bool bSet)89 void SfxMiscCfg::SetNotFoundWarning( bool bSet)
90 {
91     if(bNotFound != bSet)
92         SetModified();
93     bNotFound = bSet;
94 }
95 
SetPaperSizeWarning(bool bSet)96 void SfxMiscCfg::SetPaperSizeWarning( bool bSet)
97 {
98     if(bPaperSize != bSet)
99         SetModified();
100     bPaperSize = bSet;
101 }
102 
SetPaperOrientationWarning(bool bSet)103 void SfxMiscCfg::SetPaperOrientationWarning( bool bSet)
104 {
105     if(bPaperOrientation != bSet)
106         SetModified();
107     bPaperOrientation = bSet;
108 }
109 
SetYear2000(sal_Int32 nSet)110 void SfxMiscCfg::SetYear2000( sal_Int32 nSet )
111 {
112     if(nYear2000 != nSet)
113         SetModified();
114     nYear2000 = nSet;
115 }
116 
GetPropertyNames()117 Sequence<OUString> SfxMiscCfg::GetPropertyNames()
118 {
119     return
120     {
121         OUString("Print/Warning/PaperSize"),
122         OUString("Print/Warning/PaperOrientation"),
123         OUString("Print/Warning/NotFound"),
124         OUString("DateFormat/TwoDigitYear")
125     };
126 }
127 
Load()128 void SfxMiscCfg::Load()
129 {
130     const Sequence<OUString>& rNames = GetPropertyNames();
131     Sequence<Any> aValues = GetProperties(rNames);
132     EnableNotification(rNames);
133     const Any* pValues = aValues.getConstArray();
134     DBG_ASSERT(aValues.getLength() == rNames.getLength(), "GetProperties failed");
135     if(aValues.getLength() == rNames.getLength())
136     {
137         for(int nProp = 0; nProp < rNames.getLength(); nProp++)
138         {
139             if(pValues[nProp].hasValue())
140             {
141                 switch(nProp)
142                 {
143                     case  0: bPaperSize        = *o3tl::doAccess<bool>(pValues[nProp]); break;      //"Print/Warning/PaperSize",
144                     case  1: bPaperOrientation = *o3tl::doAccess<bool>(pValues[nProp]);  break;     //"Print/Warning/PaperOrientation",
145                     case  2: bNotFound         = *o3tl::doAccess<bool>(pValues[nProp]);  break;   //"Print/Warning/NotFound",
146                     case  3: pValues[nProp] >>= nYear2000;break;                                    //"DateFormat/TwoDigitYear",
147                 }
148             }
149         }
150     }
151 }
152 
Notify(const css::uno::Sequence<OUString> &)153 void SfxMiscCfg::Notify( const css::uno::Sequence<OUString>& )
154 {
155     Load();
156 }
157 
ImplCommit()158 void SfxMiscCfg::ImplCommit()
159 {
160     const Sequence<OUString>& rNames = GetPropertyNames();
161     Sequence<Any> aValues(rNames.getLength());
162     Any* pValues = aValues.getArray();
163 
164     for(int nProp = 0; nProp < rNames.getLength(); nProp++)
165     {
166         switch(nProp)
167         {
168             case  0: pValues[nProp] <<= bPaperSize;break;  //"Print/Warning/PaperSize",
169             case  1: pValues[nProp] <<= bPaperOrientation;break;     //"Print/Warning/PaperOrientation",
170             case  2: pValues[nProp] <<= bNotFound;break;   //"Print/Warning/NotFound",
171             case  3: pValues[nProp] <<= nYear2000;break;                 //"DateFormat/TwoDigitYear",
172         }
173     }
174     PutProperties(rNames, aValues);
175 }
176 
177 namespace
178 {
179     class LocalSingleton : public rtl::Static< osl::Mutex, LocalSingleton >
180     {
181     };
182 }
183 
MiscCfg()184 MiscCfg::MiscCfg( )
185 {
186     // Global access, must be guarded (multithreading)
187     ::osl::MutexGuard aGuard( LocalSingleton::get() );
188     m_pImpl = g_pOptions.lock();
189     if ( !m_pImpl )
190     {
191         m_pImpl = std::make_shared<SfxMiscCfg>();
192         g_pOptions = m_pImpl;
193         ItemHolder1::holdConfigItem(EItem::MiscConfig);
194     }
195 
196     m_pImpl->AddListener(this);
197 }
198 
~MiscCfg()199 MiscCfg::~MiscCfg( )
200 {
201     // Global access, must be guarded (multithreading)
202     ::osl::MutexGuard aGuard( LocalSingleton::get() );
203     m_pImpl->RemoveListener(this);
204     m_pImpl.reset();
205 }
206 
IsNotFoundWarning() const207 bool MiscCfg::IsNotFoundWarning()   const
208 {
209     return m_pImpl->IsNotFoundWarning();
210 }
211 
SetNotFoundWarning(bool bSet)212 void MiscCfg::SetNotFoundWarning(   bool bSet)
213 {
214     m_pImpl->SetNotFoundWarning( bSet );
215 }
216 
IsPaperSizeWarning() const217 bool MiscCfg::IsPaperSizeWarning()  const
218 {
219     return m_pImpl->IsPaperSizeWarning();
220 }
221 
SetPaperSizeWarning(bool bSet)222 void MiscCfg::SetPaperSizeWarning(bool bSet)
223 {
224     m_pImpl->SetPaperSizeWarning( bSet );
225 }
226 
IsPaperOrientationWarning() const227 bool MiscCfg::IsPaperOrientationWarning()   const
228 {
229     return m_pImpl->IsPaperOrientationWarning();
230 }
231 
SetPaperOrientationWarning(bool bSet)232 void MiscCfg::SetPaperOrientationWarning(   bool bSet)
233 {
234     m_pImpl->SetPaperOrientationWarning( bSet );
235 }
236 
GetYear2000() const237 sal_Int32 MiscCfg::GetYear2000() const
238 {
239     return m_pImpl->GetYear2000();
240 }
241 
SetYear2000(sal_Int32 nSet)242 void MiscCfg::SetYear2000( sal_Int32 nSet )
243 {
244     m_pImpl->SetYear2000( nSet );
245 }
246 
247 }
248 
249 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
250