xref: /aoo41x/main/unotools/source/config/undoopt.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/undoopt.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 using namespace utl;
40 using namespace rtl;
41 using namespace com::sun::star::uno;
42 
43 static SvtUndoOptions_Impl* pOptions = NULL;
44 static sal_Int32           nRefCount = 0;
45 
46 #define STEPS 0
47 
48 class SvtUndoOptions_Impl : public utl::ConfigItem
49 {
50     sal_Int32               nUndoCount;
51     Sequence< rtl::OUString > m_aPropertyNames;
52 
53 public:
54                             SvtUndoOptions_Impl();
55 
56     virtual void            Notify( const com::sun::star::uno::Sequence< rtl::OUString >& aPropertyNames );
57     virtual void            Commit();
58     void                    Load();
59 
SetUndoCount(sal_Int32 n)60     void                    SetUndoCount( sal_Int32 n ) { nUndoCount = n; SetModified();  }
GetUndoCount() const61     sal_Int32               GetUndoCount() const        { return nUndoCount; }
62 };
63 
64 // -----------------------------------------------------------------------
65 
SvtUndoOptions_Impl()66 SvtUndoOptions_Impl::SvtUndoOptions_Impl()
67     : ConfigItem( OUString::createFromAscii("Office.Common/Undo") )
68     , nUndoCount( 20 )
69 {
70     Load();
71 }
72 
Commit()73 void SvtUndoOptions_Impl::Commit()
74 {
75     Sequence< Any > aValues( m_aPropertyNames.getLength() );
76 	Any* pValues = aValues.getArray();
77     for ( int nProp = 0; nProp < m_aPropertyNames.getLength(); nProp++ )
78 	{
79         switch ( nProp )
80         {
81             case STEPS :
82                 pValues[nProp] <<= nUndoCount;
83                 break;
84             default:
85                 DBG_ERRORFILE( "invalid index to save a path" );
86         }
87 	}
88 
89     PutProperties( m_aPropertyNames, aValues );
90     NotifyListeners(0);
91 }
92 
93 // -----------------------------------------------------------------------
Load()94 void SvtUndoOptions_Impl::Load()
95 {
96     if(!m_aPropertyNames.getLength())
97     {
98         static const char* aPropNames[] =
99         {
100             "Steps",
101         };
102 
103         const int nCount = sizeof( aPropNames ) / sizeof( const char* );
104         m_aPropertyNames.realloc(nCount);
105         OUString* pNames = m_aPropertyNames.getArray();
106         for ( int i = 0; i < nCount; i++ )
107             pNames[i] = OUString::createFromAscii( aPropNames[i] );
108         EnableNotification( m_aPropertyNames );
109     }
110 
111     Sequence< Any > aValues = GetProperties( m_aPropertyNames );
112     const Any* pValues = aValues.getConstArray();
113     DBG_ASSERT( aValues.getLength() == m_aPropertyNames.getLength(), "GetProperties failed" );
114     if ( aValues.getLength() == m_aPropertyNames.getLength() )
115     {
116         for ( int nProp = 0; nProp < m_aPropertyNames.getLength(); nProp++ )
117         {
118             DBG_ASSERT( pValues[nProp].hasValue(), "property value missing" );
119             if ( pValues[nProp].hasValue() )
120             {
121                 switch ( nProp )
122                 {
123                     case STEPS :
124                     {
125                         sal_Int32 nTemp = 0;
126                         if ( pValues[nProp] >>= nTemp )
127                             nUndoCount = nTemp;
128                         else
129                         {
130                             DBG_ERROR( "Wrong Type!" );
131                         }
132                         break;
133                     }
134 
135                     default:
136                         DBG_ERROR( "Wrong Type!" );
137                         break;
138                 }
139             }
140         }
141     }
142 }
143 // -----------------------------------------------------------------------
Notify(const Sequence<rtl::OUString> &)144 void SvtUndoOptions_Impl::Notify( const Sequence<rtl::OUString>& )
145 {
146     Load();
147 }
148 
149 // -----------------------------------------------------------------------
150 namespace
151 {
152     class LocalSingleton : public rtl::Static< osl::Mutex, LocalSingleton >
153     {
154     };
155 }
156 
157 // -----------------------------------------------------------------------
SvtUndoOptions()158 SvtUndoOptions::SvtUndoOptions()
159 {
160     // Global access, must be guarded (multithreading)
161     ::osl::MutexGuard aGuard( LocalSingleton::get() );
162     if ( !pOptions )
163     {
164         RTL_LOGFILE_CONTEXT(aLog, "unotools ( ??? ) ::SvtUndoOptions_Impl::ctor()");
165         pOptions = new SvtUndoOptions_Impl;
166 
167         ItemHolder1::holdConfigItem(E_UNDOOPTIONS);
168     }
169     ++nRefCount;
170     pImp = pOptions;
171     pImp->AddListener(this);
172 }
173 
174 // -----------------------------------------------------------------------
175 
~SvtUndoOptions()176 SvtUndoOptions::~SvtUndoOptions()
177 {
178     // Global access, must be guarded (multithreading)
179     ::osl::MutexGuard aGuard( LocalSingleton::get() );
180     pImp->RemoveListener(this);
181     if ( !--nRefCount )
182 	{
183 		if ( pOptions->IsModified() )
184 			pOptions->Commit();
185         DELETEZ( pOptions );
186 	}
187 }
188 
SetUndoCount(sal_Int32 n)189 void SvtUndoOptions::SetUndoCount( sal_Int32 n )
190 {
191     pImp->SetUndoCount( n );
192 }
193 
GetUndoCount() const194 sal_Int32 SvtUndoOptions::GetUndoCount() const
195 {
196     return pImp->GetUndoCount();
197 }
198