xref: /AOO41X/main/unotools/source/config/undoopt.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_unotools.hxx"
30 
31 #include <unotools/undoopt.hxx>
32 #include "rtl/instance.hxx"
33 #include <unotools/configmgr.hxx>
34 #include <unotools/configitem.hxx>
35 #include <tools/debug.hxx>
36 #include <com/sun/star/uno/Any.hxx>
37 #include <com/sun/star/uno/Sequence.hxx>
38 #include <vos/mutex.hxx>
39 #include <osl/mutex.hxx>
40 #include <rtl/logfile.hxx>
41 #include "itemholder1.hxx"
42 
43 using namespace utl;
44 using namespace rtl;
45 using namespace com::sun::star::uno;
46 
47 static SvtUndoOptions_Impl* pOptions = NULL;
48 static sal_Int32           nRefCount = 0;
49 
50 #define STEPS 0
51 
52 class SvtUndoOptions_Impl : public utl::ConfigItem
53 {
54     sal_Int32               nUndoCount;
55     Sequence< rtl::OUString > m_aPropertyNames;
56 
57 public:
58                             SvtUndoOptions_Impl();
59 
60     virtual void            Notify( const com::sun::star::uno::Sequence< rtl::OUString >& aPropertyNames );
61     virtual void            Commit();
62     void                    Load();
63 
64     void                    SetUndoCount( sal_Int32 n ) { nUndoCount = n; SetModified();  }
65     sal_Int32               GetUndoCount() const        { return nUndoCount; }
66 };
67 
68 // -----------------------------------------------------------------------
69 
70 SvtUndoOptions_Impl::SvtUndoOptions_Impl()
71     : ConfigItem( OUString::createFromAscii("Office.Common/Undo") )
72     , nUndoCount( 20 )
73 {
74     Load();
75 }
76 
77 void SvtUndoOptions_Impl::Commit()
78 {
79     Sequence< Any > aValues( m_aPropertyNames.getLength() );
80 	Any* pValues = aValues.getArray();
81     for ( int nProp = 0; nProp < m_aPropertyNames.getLength(); nProp++ )
82 	{
83         switch ( nProp )
84         {
85             case STEPS :
86                 pValues[nProp] <<= nUndoCount;
87                 break;
88             default:
89                 DBG_ERRORFILE( "invalid index to save a path" );
90         }
91 	}
92 
93     PutProperties( m_aPropertyNames, aValues );
94     NotifyListeners(0);
95 }
96 
97 // -----------------------------------------------------------------------
98 void SvtUndoOptions_Impl::Load()
99 {
100     if(!m_aPropertyNames.getLength())
101     {
102         static const char* aPropNames[] =
103         {
104             "Steps",
105         };
106 
107         const int nCount = sizeof( aPropNames ) / sizeof( const char* );
108         m_aPropertyNames.realloc(nCount);
109         OUString* pNames = m_aPropertyNames.getArray();
110         for ( int i = 0; i < nCount; i++ )
111             pNames[i] = OUString::createFromAscii( aPropNames[i] );
112         EnableNotification( m_aPropertyNames );
113     }
114 
115     Sequence< Any > aValues = GetProperties( m_aPropertyNames );
116     const Any* pValues = aValues.getConstArray();
117     DBG_ASSERT( aValues.getLength() == m_aPropertyNames.getLength(), "GetProperties failed" );
118     if ( aValues.getLength() == m_aPropertyNames.getLength() )
119     {
120         for ( int nProp = 0; nProp < m_aPropertyNames.getLength(); nProp++ )
121         {
122             DBG_ASSERT( pValues[nProp].hasValue(), "property value missing" );
123             if ( pValues[nProp].hasValue() )
124             {
125                 switch ( nProp )
126                 {
127                     case STEPS :
128                     {
129                         sal_Int32 nTemp = 0;
130                         if ( pValues[nProp] >>= nTemp )
131                             nUndoCount = nTemp;
132                         else
133                         {
134                             DBG_ERROR( "Wrong Type!" );
135                         }
136                         break;
137                     }
138 
139                     default:
140                         DBG_ERROR( "Wrong Type!" );
141                         break;
142                 }
143             }
144         }
145     }
146 }
147 // -----------------------------------------------------------------------
148 void SvtUndoOptions_Impl::Notify( const Sequence<rtl::OUString>& )
149 {
150     Load();
151 }
152 
153 // -----------------------------------------------------------------------
154 namespace
155 {
156     class LocalSingleton : public rtl::Static< osl::Mutex, LocalSingleton >
157     {
158     };
159 }
160 
161 // -----------------------------------------------------------------------
162 SvtUndoOptions::SvtUndoOptions()
163 {
164     // Global access, must be guarded (multithreading)
165     ::osl::MutexGuard aGuard( LocalSingleton::get() );
166     if ( !pOptions )
167     {
168         RTL_LOGFILE_CONTEXT(aLog, "unotools ( ??? ) ::SvtUndoOptions_Impl::ctor()");
169         pOptions = new SvtUndoOptions_Impl;
170 
171         ItemHolder1::holdConfigItem(E_UNDOOPTIONS);
172     }
173     ++nRefCount;
174     pImp = pOptions;
175     pImp->AddListener(this);
176 }
177 
178 // -----------------------------------------------------------------------
179 
180 SvtUndoOptions::~SvtUndoOptions()
181 {
182     // Global access, must be guarded (multithreading)
183     ::osl::MutexGuard aGuard( LocalSingleton::get() );
184     pImp->RemoveListener(this);
185     if ( !--nRefCount )
186 	{
187 		if ( pOptions->IsModified() )
188 			pOptions->Commit();
189         DELETEZ( pOptions );
190 	}
191 }
192 
193 void SvtUndoOptions::SetUndoCount( sal_Int32 n )
194 {
195     pImp->SetUndoCount( n );
196 }
197 
198 sal_Int32 SvtUndoOptions::GetUndoCount() const
199 {
200     return pImp->GetUndoCount();
201 }
202