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