merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / config / undoopt.cxx
blobd301d9a5c3a9fb5ecc411ba40687a6cc779fa1c6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: undoopt.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
33 #ifdef SVL_DLLIMPLEMENTATION
34 #undef SVL_DLLIMPLEMENTATION
35 #endif
36 #define SVT_DLLIMPLEMENTATION
38 #include <svtools/undoopt.hxx>
39 #include "rtl/instance.hxx"
40 #include <unotools/configmgr.hxx>
41 #include <unotools/configitem.hxx>
42 #include <tools/debug.hxx>
43 #include <com/sun/star/uno/Any.hxx>
44 #include <com/sun/star/uno/Sequence.hxx>
45 #include <vos/mutex.hxx>
46 #include <svtools/smplhint.hxx>
47 #include <vcl/svapp.hxx>
48 #include <osl/mutex.hxx>
49 #include <rtl/logfile.hxx>
50 #include "itemholder2.hxx"
52 using namespace utl;
53 using namespace rtl;
54 using namespace com::sun::star::uno;
56 static SvtUndoOptions_Impl* pOptions = NULL;
57 static sal_Int32 nRefCount = 0;
59 #define STEPS 0
61 class SvtUndoOptions_Impl : public utl::ConfigItem, public SfxBroadcaster
63 sal_Int32 nUndoCount;
64 Sequence< rtl::OUString > m_aPropertyNames;
66 public:
67 SvtUndoOptions_Impl();
69 virtual void Notify( const com::sun::star::uno::Sequence< rtl::OUString >& aPropertyNames );
70 virtual void Commit();
71 void Load();
73 void SetUndoCount( sal_Int32 n ) { nUndoCount = n; SetModified(); }
74 sal_Int32 GetUndoCount() const { return nUndoCount; }
77 // -----------------------------------------------------------------------
79 SvtUndoOptions_Impl::SvtUndoOptions_Impl()
80 : ConfigItem( OUString::createFromAscii("Office.Common/Undo") )
81 , nUndoCount( 20 )
83 Load();
86 void SvtUndoOptions_Impl::Commit()
88 Sequence< Any > aValues( m_aPropertyNames.getLength() );
89 Any* pValues = aValues.getArray();
90 for ( int nProp = 0; nProp < m_aPropertyNames.getLength(); nProp++ )
92 switch ( nProp )
94 case STEPS :
95 pValues[nProp] <<= nUndoCount;
96 break;
97 default:
98 DBG_ERRORFILE( "invalid index to save a path" );
102 PutProperties( m_aPropertyNames, aValues );
103 //broadcast changes
104 Broadcast(SfxSimpleHint(SFX_HINT_UNDO_OPTIONS_CHANGED));
107 // -----------------------------------------------------------------------
108 void SvtUndoOptions_Impl::Load()
110 if(!m_aPropertyNames.getLength())
112 static const char* aPropNames[] =
114 "Steps",
117 const int nCount = sizeof( aPropNames ) / sizeof( const char* );
118 m_aPropertyNames.realloc(nCount);
119 OUString* pNames = m_aPropertyNames.getArray();
120 for ( int i = 0; i < nCount; i++ )
121 pNames[i] = OUString::createFromAscii( aPropNames[i] );
122 EnableNotification( m_aPropertyNames );
125 Sequence< Any > aValues = GetProperties( m_aPropertyNames );
126 const Any* pValues = aValues.getConstArray();
127 DBG_ASSERT( aValues.getLength() == m_aPropertyNames.getLength(), "GetProperties failed" );
128 if ( aValues.getLength() == m_aPropertyNames.getLength() )
130 for ( int nProp = 0; nProp < m_aPropertyNames.getLength(); nProp++ )
132 DBG_ASSERT( pValues[nProp].hasValue(), "property value missing" );
133 if ( pValues[nProp].hasValue() )
135 switch ( nProp )
137 case STEPS :
139 sal_Int32 nTemp = 0;
140 if ( pValues[nProp] >>= nTemp )
141 nUndoCount = nTemp;
142 else
144 DBG_ERROR( "Wrong Type!" );
146 break;
149 default:
150 DBG_ERROR( "Wrong Type!" );
151 break;
157 // -----------------------------------------------------------------------
158 void SvtUndoOptions_Impl::Notify( const Sequence<rtl::OUString>& )
160 Load();
161 //broadcast changes
162 Broadcast(SfxSimpleHint(SFX_HINT_UNDO_OPTIONS_CHANGED));
165 // -----------------------------------------------------------------------
166 namespace
168 class LocalSingleton : public rtl::Static< osl::Mutex, LocalSingleton >
173 // -----------------------------------------------------------------------
174 SvtUndoOptions::SvtUndoOptions()
176 // Global access, must be guarded (multithreading)
177 ::osl::MutexGuard aGuard( LocalSingleton::get() );
178 if ( !pOptions )
180 RTL_LOGFILE_CONTEXT(aLog, "svtools ( ??? ) ::SvtUndoOptions_Impl::ctor()");
181 pOptions = new SvtUndoOptions_Impl;
183 ItemHolder2::holdConfigItem(E_UNDOOPTIONS);
185 ++nRefCount;
186 pImp = pOptions;
187 StartListening(*pImp);
190 // -----------------------------------------------------------------------
192 SvtUndoOptions::~SvtUndoOptions()
194 // Global access, must be guarded (multithreading)
195 ::osl::MutexGuard aGuard( LocalSingleton::get() );
196 EndListening(*pImp);
197 if ( !--nRefCount )
199 if ( pOptions->IsModified() )
200 pOptions->Commit();
201 DELETEZ( pOptions );
205 void SvtUndoOptions::SetUndoCount( sal_Int32 n )
207 pImp->SetUndoCount( n );
210 sal_Int32 SvtUndoOptions::GetUndoCount() const
212 return pImp->GetUndoCount();
215 void SvtUndoOptions::Notify( SfxBroadcaster&, const SfxHint& rHint )
217 vos::OGuard aVclGuard( Application::GetSolarMutex() );
218 Broadcast( rHint );