xref: /aoo41x/main/unotools/source/config/undoopt.cxx (revision b5088357)
1*b5088357SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b5088357SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b5088357SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b5088357SAndrew Rist  * distributed with this work for additional information
6*b5088357SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b5088357SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b5088357SAndrew Rist  * "License"); you may not use this file except in compliance
9*b5088357SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b5088357SAndrew Rist  *
11*b5088357SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b5088357SAndrew Rist  *
13*b5088357SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b5088357SAndrew Rist  * software distributed under the License is distributed on an
15*b5088357SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b5088357SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b5088357SAndrew Rist  * specific language governing permissions and limitations
18*b5088357SAndrew Rist  * under the License.
19*b5088357SAndrew Rist  *
20*b5088357SAndrew Rist  *************************************************************/
21*b5088357SAndrew Rist 
22*b5088357SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_unotools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <unotools/undoopt.hxx>
28cdf0e10cSrcweir #include "rtl/instance.hxx"
29cdf0e10cSrcweir #include <unotools/configmgr.hxx>
30cdf0e10cSrcweir #include <unotools/configitem.hxx>
31cdf0e10cSrcweir #include <tools/debug.hxx>
32cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
33cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
34cdf0e10cSrcweir #include <vos/mutex.hxx>
35cdf0e10cSrcweir #include <osl/mutex.hxx>
36cdf0e10cSrcweir #include <rtl/logfile.hxx>
37cdf0e10cSrcweir #include "itemholder1.hxx"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir using namespace utl;
40cdf0e10cSrcweir using namespace rtl;
41cdf0e10cSrcweir using namespace com::sun::star::uno;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir static SvtUndoOptions_Impl* pOptions = NULL;
44cdf0e10cSrcweir static sal_Int32           nRefCount = 0;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #define STEPS 0
47cdf0e10cSrcweir 
48cdf0e10cSrcweir class SvtUndoOptions_Impl : public utl::ConfigItem
49cdf0e10cSrcweir {
50cdf0e10cSrcweir     sal_Int32               nUndoCount;
51cdf0e10cSrcweir     Sequence< rtl::OUString > m_aPropertyNames;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir public:
54cdf0e10cSrcweir                             SvtUndoOptions_Impl();
55cdf0e10cSrcweir 
56cdf0e10cSrcweir     virtual void            Notify( const com::sun::star::uno::Sequence< rtl::OUString >& aPropertyNames );
57cdf0e10cSrcweir     virtual void            Commit();
58cdf0e10cSrcweir     void                    Load();
59cdf0e10cSrcweir 
SetUndoCount(sal_Int32 n)60cdf0e10cSrcweir     void                    SetUndoCount( sal_Int32 n ) { nUndoCount = n; SetModified();  }
GetUndoCount() const61cdf0e10cSrcweir     sal_Int32               GetUndoCount() const        { return nUndoCount; }
62cdf0e10cSrcweir };
63cdf0e10cSrcweir 
64cdf0e10cSrcweir // -----------------------------------------------------------------------
65cdf0e10cSrcweir 
SvtUndoOptions_Impl()66cdf0e10cSrcweir SvtUndoOptions_Impl::SvtUndoOptions_Impl()
67cdf0e10cSrcweir     : ConfigItem( OUString::createFromAscii("Office.Common/Undo") )
68cdf0e10cSrcweir     , nUndoCount( 20 )
69cdf0e10cSrcweir {
70cdf0e10cSrcweir     Load();
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
Commit()73cdf0e10cSrcweir void SvtUndoOptions_Impl::Commit()
74cdf0e10cSrcweir {
75cdf0e10cSrcweir     Sequence< Any > aValues( m_aPropertyNames.getLength() );
76cdf0e10cSrcweir 	Any* pValues = aValues.getArray();
77cdf0e10cSrcweir     for ( int nProp = 0; nProp < m_aPropertyNames.getLength(); nProp++ )
78cdf0e10cSrcweir 	{
79cdf0e10cSrcweir         switch ( nProp )
80cdf0e10cSrcweir         {
81cdf0e10cSrcweir             case STEPS :
82cdf0e10cSrcweir                 pValues[nProp] <<= nUndoCount;
83cdf0e10cSrcweir                 break;
84cdf0e10cSrcweir             default:
85cdf0e10cSrcweir                 DBG_ERRORFILE( "invalid index to save a path" );
86cdf0e10cSrcweir         }
87cdf0e10cSrcweir 	}
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     PutProperties( m_aPropertyNames, aValues );
90cdf0e10cSrcweir     NotifyListeners(0);
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir // -----------------------------------------------------------------------
Load()94cdf0e10cSrcweir void SvtUndoOptions_Impl::Load()
95cdf0e10cSrcweir {
96cdf0e10cSrcweir     if(!m_aPropertyNames.getLength())
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir         static const char* aPropNames[] =
99cdf0e10cSrcweir         {
100cdf0e10cSrcweir             "Steps",
101cdf0e10cSrcweir         };
102cdf0e10cSrcweir 
103cdf0e10cSrcweir         const int nCount = sizeof( aPropNames ) / sizeof( const char* );
104cdf0e10cSrcweir         m_aPropertyNames.realloc(nCount);
105cdf0e10cSrcweir         OUString* pNames = m_aPropertyNames.getArray();
106cdf0e10cSrcweir         for ( int i = 0; i < nCount; i++ )
107cdf0e10cSrcweir             pNames[i] = OUString::createFromAscii( aPropNames[i] );
108cdf0e10cSrcweir         EnableNotification( m_aPropertyNames );
109cdf0e10cSrcweir     }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     Sequence< Any > aValues = GetProperties( m_aPropertyNames );
112cdf0e10cSrcweir     const Any* pValues = aValues.getConstArray();
113cdf0e10cSrcweir     DBG_ASSERT( aValues.getLength() == m_aPropertyNames.getLength(), "GetProperties failed" );
114cdf0e10cSrcweir     if ( aValues.getLength() == m_aPropertyNames.getLength() )
115cdf0e10cSrcweir     {
116cdf0e10cSrcweir         for ( int nProp = 0; nProp < m_aPropertyNames.getLength(); nProp++ )
117cdf0e10cSrcweir         {
118cdf0e10cSrcweir             DBG_ASSERT( pValues[nProp].hasValue(), "property value missing" );
119cdf0e10cSrcweir             if ( pValues[nProp].hasValue() )
120cdf0e10cSrcweir             {
121cdf0e10cSrcweir                 switch ( nProp )
122cdf0e10cSrcweir                 {
123cdf0e10cSrcweir                     case STEPS :
124cdf0e10cSrcweir                     {
125cdf0e10cSrcweir                         sal_Int32 nTemp = 0;
126cdf0e10cSrcweir                         if ( pValues[nProp] >>= nTemp )
127cdf0e10cSrcweir                             nUndoCount = nTemp;
128cdf0e10cSrcweir                         else
129cdf0e10cSrcweir                         {
130cdf0e10cSrcweir                             DBG_ERROR( "Wrong Type!" );
131cdf0e10cSrcweir                         }
132cdf0e10cSrcweir                         break;
133cdf0e10cSrcweir                     }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir                     default:
136cdf0e10cSrcweir                         DBG_ERROR( "Wrong Type!" );
137cdf0e10cSrcweir                         break;
138cdf0e10cSrcweir                 }
139cdf0e10cSrcweir             }
140cdf0e10cSrcweir         }
141cdf0e10cSrcweir     }
142cdf0e10cSrcweir }
143cdf0e10cSrcweir // -----------------------------------------------------------------------
Notify(const Sequence<rtl::OUString> &)144cdf0e10cSrcweir void SvtUndoOptions_Impl::Notify( const Sequence<rtl::OUString>& )
145cdf0e10cSrcweir {
146cdf0e10cSrcweir     Load();
147cdf0e10cSrcweir }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir // -----------------------------------------------------------------------
150cdf0e10cSrcweir namespace
151cdf0e10cSrcweir {
152cdf0e10cSrcweir     class LocalSingleton : public rtl::Static< osl::Mutex, LocalSingleton >
153cdf0e10cSrcweir     {
154cdf0e10cSrcweir     };
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir // -----------------------------------------------------------------------
SvtUndoOptions()158cdf0e10cSrcweir SvtUndoOptions::SvtUndoOptions()
159cdf0e10cSrcweir {
160cdf0e10cSrcweir     // Global access, must be guarded (multithreading)
161cdf0e10cSrcweir     ::osl::MutexGuard aGuard( LocalSingleton::get() );
162cdf0e10cSrcweir     if ( !pOptions )
163cdf0e10cSrcweir     {
164cdf0e10cSrcweir         RTL_LOGFILE_CONTEXT(aLog, "unotools ( ??? ) ::SvtUndoOptions_Impl::ctor()");
165cdf0e10cSrcweir         pOptions = new SvtUndoOptions_Impl;
166cdf0e10cSrcweir 
167cdf0e10cSrcweir         ItemHolder1::holdConfigItem(E_UNDOOPTIONS);
168cdf0e10cSrcweir     }
169cdf0e10cSrcweir     ++nRefCount;
170cdf0e10cSrcweir     pImp = pOptions;
171cdf0e10cSrcweir     pImp->AddListener(this);
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir // -----------------------------------------------------------------------
175cdf0e10cSrcweir 
~SvtUndoOptions()176cdf0e10cSrcweir SvtUndoOptions::~SvtUndoOptions()
177cdf0e10cSrcweir {
178cdf0e10cSrcweir     // Global access, must be guarded (multithreading)
179cdf0e10cSrcweir     ::osl::MutexGuard aGuard( LocalSingleton::get() );
180cdf0e10cSrcweir     pImp->RemoveListener(this);
181cdf0e10cSrcweir     if ( !--nRefCount )
182cdf0e10cSrcweir 	{
183cdf0e10cSrcweir 		if ( pOptions->IsModified() )
184cdf0e10cSrcweir 			pOptions->Commit();
185cdf0e10cSrcweir         DELETEZ( pOptions );
186cdf0e10cSrcweir 	}
187cdf0e10cSrcweir }
188cdf0e10cSrcweir 
SetUndoCount(sal_Int32 n)189cdf0e10cSrcweir void SvtUndoOptions::SetUndoCount( sal_Int32 n )
190cdf0e10cSrcweir {
191cdf0e10cSrcweir     pImp->SetUndoCount( n );
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
GetUndoCount() const194cdf0e10cSrcweir sal_Int32 SvtUndoOptions::GetUndoCount() const
195cdf0e10cSrcweir {
196cdf0e10cSrcweir     return pImp->GetUndoCount();
197cdf0e10cSrcweir }
198