merge the formfield patch from ooo-build
[ooovba.git] / svx / source / cui / dbregisterednamesconfig.cxx
blobc31448a36e249d9099bcfae310bd80d9961446d8
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: dbregisterednamesconfig.cxx,v $
10 * $Revision: 1.10 $
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_svx.hxx"
34 #ifdef SVX_DLLIMPLEMENTATION
35 #undef SVX_DLLIMPLEMENTATION
36 #endif
38 #include "dbregisterednamesconfig.hxx"
41 #include <svtools/itemset.hxx>
42 #include <tools/diagnose_ex.h>
44 #include <svx/svxids.hrc>
45 #include <unotools/confignode.hxx>
46 #include <comphelper/extract.hxx>
47 #include <com/sun/star/container/XNameAccess.hpp>
48 #include <com/sun/star/uno/XNamingService.hpp>
49 #include <svtools/eitem.hxx>
50 #include <comphelper/processfactory.hxx>
51 #include <svtools/pathoptions.hxx>
52 #include "dbregistersettings.hxx"
53 #include "connpooloptions.hxx"
55 //........................................................................
56 namespace svx
58 //........................................................................
60 using namespace ::utl;
61 using namespace ::com::sun::star::uno;
62 using namespace ::com::sun::star::container;
64 //--------------------------------------------------------------------
65 static const ::rtl::OUString& getDbRegisteredNamesNodeName()
67 static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("org.openoffice.Office.DataAccess/RegisteredNames");
68 return s_sNodeName;
71 //--------------------------------------------------------------------
72 static const ::rtl::OUString& getDbNameNodeName()
74 static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("Name");
75 return s_sNodeName;
78 //--------------------------------------------------------------------
79 static const ::rtl::OUString& getDbLocationNodeName()
81 static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("Location");
82 return s_sNodeName;
85 //====================================================================
86 //= DbRegisteredNamesConfig
87 //====================================================================
88 //--------------------------------------------------------------------
89 void DbRegisteredNamesConfig::GetOptions(SfxItemSet& _rFillItems)
91 // the config node where all pooling relevant info are stored under
92 OConfigurationTreeRoot aDbRegisteredNamesRoot = OConfigurationTreeRoot::createWithServiceFactory(
93 ::comphelper::getProcessServiceFactory(), getDbRegisteredNamesNodeName(), -1, OConfigurationTreeRoot::CM_READONLY);
95 TNameLocationMap aSettings;
97 // then look for which of them settings are stored in the configuration
98 Sequence< ::rtl::OUString > aDriverKeys = aDbRegisteredNamesRoot.getNodeNames();
99 const ::rtl::OUString* pDriverKeys = aDriverKeys.getConstArray();
100 const ::rtl::OUString* pDriverKeysEnd = pDriverKeys + aDriverKeys.getLength();
101 for (;pDriverKeys != pDriverKeysEnd; ++pDriverKeys)
103 // the name of the driver in this round
104 OConfigurationNode aThisDriverSettings = aDbRegisteredNamesRoot.openNode(*pDriverKeys);
105 ::rtl::OUString sName, sLocation;
106 aThisDriverSettings.getNodeValue(getDbNameNodeName()) >>= sName;
107 aThisDriverSettings.getNodeValue(getDbLocationNodeName()) >>= sLocation;
108 sLocation = SvtPathOptions().SubstituteVariable(sLocation);
110 aSettings.insert(TNameLocationMap::value_type(sName,sLocation));
113 _rFillItems.Put(DatabaseMapItem(SID_SB_DB_REGISTER, aSettings));
116 //--------------------------------------------------------------------
117 void DbRegisteredNamesConfig::SetOptions(const SfxItemSet& _rSourceItems)
119 // the config node where all pooling relevant info are stored under
120 OConfigurationTreeRoot aDbRegisteredNamesRoot = OConfigurationTreeRoot::createWithServiceFactory(
121 ::comphelper::getProcessServiceFactory(), getDbRegisteredNamesNodeName(), -1, OConfigurationTreeRoot::CM_UPDATABLE);
123 if (!aDbRegisteredNamesRoot.isValid())
124 // already asserted by the OConfigurationTreeRoot
125 return;
127 sal_Bool bNeedCommit = sal_False;
130 // the settings for the single drivers
131 SFX_ITEMSET_GET( _rSourceItems, pDriverSettings, DatabaseMapItem, SID_SB_DB_REGISTER, sal_True );
132 if (pDriverSettings)
134 Reference< XNameAccess > xDatabaseContext = Reference< XNameAccess >(::comphelper::getProcessServiceFactory()->createInstance(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdb.DatabaseContext"))), UNO_QUERY);
135 Reference< XNamingService> xNamingService(xDatabaseContext,UNO_QUERY);
136 ::rtl::OUString sName, sLocation;
137 OConfigurationNode aThisDriverSettings;
139 const TNameLocationMap& rNewSettings = pDriverSettings->getSettings();
140 TNameLocationMap::const_iterator aEnd = rNewSettings.end();
141 for ( TNameLocationMap::const_iterator aLoop = rNewSettings.begin();
142 aLoop != aEnd;
143 ++aLoop
146 // need the name as ::rtl::OUString
147 sName = aLoop->first;
149 // the sub-node for this driver
150 if (aDbRegisteredNamesRoot.hasByName(sName))
152 aThisDriverSettings = aDbRegisteredNamesRoot.openNode(sName);
153 // set the values
154 aThisDriverSettings.setNodeValue(getDbNameNodeName(), makeAny(sName));
155 aThisDriverSettings.setNodeValue(getDbLocationNodeName(), makeAny(aLoop->second));
156 bNeedCommit = sal_True;
158 else
162 xNamingService->registerObject(sName,Reference< ::com::sun::star::uno::XInterface >(xDatabaseContext->getByName(aLoop->second),UNO_QUERY));
164 catch( const Exception& )
166 //DBG_UNHANDLED_EXCEPTION();
170 if (bNeedCommit)
171 aDbRegisteredNamesRoot.commit();
173 // delete unused entry
174 Sequence< ::rtl::OUString > aDriverKeys = xDatabaseContext->getElementNames();
175 const ::rtl::OUString* pDriverKeys = aDriverKeys.getConstArray();
176 const ::rtl::OUString* pDriverKeysEnd = pDriverKeys + aDriverKeys.getLength();
177 for (;pDriverKeys != pDriverKeysEnd; ++pDriverKeys)
179 if ( rNewSettings.find(*pDriverKeys) == rNewSettings.end() )
183 xNamingService->revokeObject(*pDriverKeys);
185 catch( const Exception& )
187 //DBG_UNHANDLED_EXCEPTION();
194 //........................................................................
195 } // namespace svx
196 //........................................................................