merge the formfield patch from ooo-build
[ooovba.git] / framework / source / jobs / configaccess.cxx
blob848ec301423829f193494ec46a98732be1629ffd
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: configaccess.cxx,v $
10 * $Revision: 1.6 $
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_framework.hxx"
34 //________________________________
35 // my own includes
36 #include <jobs/configaccess.hxx>
37 #include <threadhelp/readguard.hxx>
38 #include <threadhelp/writeguard.hxx>
39 #include <threadhelp/resetableguard.hxx>
40 #include <general.h>
41 #include <services.h>
43 //________________________________
44 // interface includes
45 #include <com/sun/star/beans/XPropertySet.hpp>
46 #include <com/sun/star/beans/XMultiHierarchicalPropertySet.hpp>
47 #include <com/sun/star/container/XNameAccess.hpp>
48 #include <com/sun/star/beans/PropertyValue.hpp>
49 #include <com/sun/star/util/XChangesBatch.hpp>
51 //________________________________
52 // includes of other projects
53 #include <unotools/configpathes.hxx>
54 #include <rtl/ustrbuf.hxx>
56 //________________________________
57 // namespace
59 namespace framework{
61 //________________________________
62 // non exported const
64 //________________________________
65 // non exported definitions
67 //________________________________
68 // declarations
70 //________________________________
71 /**
72 @short open the configuration of this job
73 @descr We open the configuration of this job only. Not the whole package or the whole
74 job set. We are interested on our own properties only.
75 We set the opened configuration access as our member. So any following method,
76 which needs cfg access, can use it. That prevent us against multiple open/close requests.
77 But you can use this method to upgrade an already opened configuration too.
79 @param eMode
80 force opening of the configuration access in readonly or in read/write mode
82 ConfigAccess::ConfigAccess( /*IN*/ const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR ,
83 /*IN*/ const ::rtl::OUString& sRoot )
84 : ThreadHelpBase( )
85 , m_xSMGR ( xSMGR )
86 , m_sRoot ( sRoot )
87 , m_eMode ( E_CLOSED )
91 //________________________________
92 /**
93 @short last chance to close an open configuration access point
94 @descr In case our user forgot to close this configuration point
95 in the right way, normaly he will run into some trouble -
96 e.g. losing data.
98 ConfigAccess::~ConfigAccess()
100 close();
103 //________________________________
105 @short return the internal mode of this instance
106 @descr May be the outside user need any information about successfully opened
107 or closed config access point objects. He can control the internal mode to do so.
109 @return The internal open state of this object.
111 ConfigAccess::EOpenMode ConfigAccess::getMode() const
113 /* SAFE { */
114 ReadGuard aReadLock(m_aLock);
115 return m_eMode;
116 /* } SAFE */
119 //________________________________
121 @short open the configuration access in the specified mode
122 @descr We set the opened configuration access as our member. So any following method,
123 which needs cfg access, can use it. That prevent us against multiple open/close requests.
124 But you can use this method to upgrade an already opened configuration too.
125 It's possible to open a config access in READONLY mode first and "open" it at a second
126 time within the mode READWRITE. Then we will upgrade it. Dowgrade will be possible too.
128 But note: closing will be done explicitly by calling method close() ... not by
129 downgrading with mode CLOSED!
131 @param eMode
132 force (re)opening of the configuration access in readonly or in read/write mode
134 void ConfigAccess::open( /*IN*/ EOpenMode eMode )
136 /* SAFE { */
137 // We must lock the whole method to be shure, that nobody
138 // outside uses our internal member m_xAccess!
139 WriteGuard aWriteLock(m_aLock);
141 // check if configuration is already open in the right mode.
142 // By the way: Don't allow closing by using this method!
143 if (
144 (eMode !=E_CLOSED) &&
145 (m_eMode!=eMode )
148 // We have to close the old access point without any question here.
149 // It will be open again using the new mode.
150 // can be called without checks! It does the checks by itself ...
151 // e.g. for already closed or not opened configuration.
152 // Flushing of all made changes will be done here too.
153 close();
155 // create the configuration provider, which provides sub access points
156 css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider(m_xSMGR->createInstance(SERVICENAME_CFGPROVIDER), css::uno::UNO_QUERY);
157 if (xConfigProvider.is())
159 css::beans::PropertyValue aParam;
160 aParam.Name = DECLARE_ASCII("nodepath");
161 aParam.Value <<= m_sRoot;
163 css::uno::Sequence< css::uno::Any > lParams(1);
164 lParams[0] <<= aParam;
166 // open it
169 if (eMode==E_READONLY)
170 m_xConfig = xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGREADACCESS , lParams);
171 else
172 if (eMode==E_READWRITE)
173 m_xConfig = xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGUPDATEACCESS, lParams);
175 catch(css::uno::Exception& ex)
177 (void) ex; // avoid warning
178 LOG_WARNING("open config ...", U2B(ex.Message))
181 m_eMode = E_CLOSED;
182 if (m_xConfig.is())
183 m_eMode = eMode;
187 aWriteLock.unlock();
188 /* } SAFE */
191 //________________________________
193 @short close the internal opened configuration access and flush all changes
194 @descr It checks, if the given access is valid and react in the right way.
195 It flushes all changes ... so nobody else must know this state.
197 void ConfigAccess::close()
199 /* SAFE { */
200 // Lock the whole method, to be shure that nobody else uses our internal members
201 // during this time.
202 WriteGuard aWriteLock(m_aLock);
204 // check already closed configuration
205 if (m_xConfig.is())
207 css::uno::Reference< css::util::XChangesBatch > xFlush(m_xConfig, css::uno::UNO_QUERY);
208 if (xFlush.is())
209 xFlush->commitChanges();
210 m_xConfig = css::uno::Reference< css::uno::XInterface >();
211 m_eMode = E_CLOSED;
214 aWriteLock.unlock();
215 /* } SAFE */
218 //________________________________
220 @short provides an access to the internal wrapped configuration access
221 @descr It's not allowed to safe this c++ (!) reference outside. You have
222 to use it directly. Further you must use our public lock member m_aLock
223 to synchronize your code with our internal structures and our interface
224 methods. Acquire it before you call cfg() and release it afterwards immediatly.
226 E.g.: ConfigAccess aAccess(...);
227 ReadGuard aReadLock(aAccess.m_aLock);
228 Reference< XPropertySet > xSet(aAccess.cfg(), UNO_QUERY);
229 Any aProp = xSet->getPropertyValue("...");
230 aReadLock.unlock();
232 @attention During this time it's not allowed to call the methods open() or close()!
233 Otherwhise you will change your own referenced config access. Anything will
234 be possible then.
236 @return A c++(!) reference to the uno instance of the configuration access point.
238 const css::uno::Reference< css::uno::XInterface >& ConfigAccess::cfg()
240 // must be synchronized from outside!
241 // => no lock here ...
242 return m_xConfig;
245 } // namespace framework