Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / cui / source / options / connpoolconfig.cxx
bloba20f4f38af092163caee5ba0418d71bb222f77a0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "connpoolconfig.hxx"
21 #include "connpoolsettings.hxx"
23 #include "connpooloptions.hxx"
24 #include <svl/itemset.hxx>
25 #include <unotools/confignode.hxx>
26 #include <comphelper/extract.hxx>
27 #include <svl/eitem.hxx>
28 #include <comphelper/processfactory.hxx>
29 #include "sdbcdriverenum.hxx"
31 //........................................................................
32 namespace offapp
34 //........................................................................
36 using namespace ::utl;
37 using namespace ::com::sun::star::uno;
39 //--------------------------------------------------------------------
40 static const ::rtl::OUString& getConnectionPoolNodeName()
42 static ::rtl::OUString s_sNodeName(RTL_CONSTASCII_USTRINGPARAM("org.openoffice.Office.DataAccess/ConnectionPool") );
43 return s_sNodeName;
46 //--------------------------------------------------------------------
47 static const ::rtl::OUString& getEnablePoolingNodeName()
49 static ::rtl::OUString s_sNodeName(RTL_CONSTASCII_USTRINGPARAM("EnablePooling"));
50 return s_sNodeName;
53 //--------------------------------------------------------------------
54 static const ::rtl::OUString& getDriverSettingsNodeName()
56 static ::rtl::OUString s_sNodeName(RTL_CONSTASCII_USTRINGPARAM("DriverSettings"));
57 return s_sNodeName;
60 //--------------------------------------------------------------------
61 static const ::rtl::OUString& getDriverNameNodeName()
63 static ::rtl::OUString s_sNodeName(RTL_CONSTASCII_USTRINGPARAM("DriverName"));
64 return s_sNodeName;
67 //--------------------------------------------------------------------
68 static const ::rtl::OUString& getEnableNodeName()
70 static ::rtl::OUString s_sNodeName(RTL_CONSTASCII_USTRINGPARAM("Enable"));
71 return s_sNodeName;
74 //--------------------------------------------------------------------
75 static const ::rtl::OUString& getTimeoutNodeName()
77 static ::rtl::OUString s_sNodeName(RTL_CONSTASCII_USTRINGPARAM("Timeout"));
78 return s_sNodeName;
81 //====================================================================
82 //= ConnectionPoolConfig
83 //====================================================================
84 //--------------------------------------------------------------------
85 void ConnectionPoolConfig::GetOptions(SfxItemSet& _rFillItems)
87 // the config node where all pooling relevant info are stored under
88 OConfigurationTreeRoot aConnectionPoolRoot = OConfigurationTreeRoot::createWithServiceFactory(
89 ::comphelper::getProcessServiceFactory(), getConnectionPoolNodeName(), -1, OConfigurationTreeRoot::CM_READONLY);
91 // the global "enabled" flag
92 Any aEnabled = aConnectionPoolRoot.getNodeValue(getEnablePoolingNodeName());
93 sal_Bool bEnabled = sal_True;
94 aEnabled >>= bEnabled;
95 _rFillItems.Put(SfxBoolItem(SID_SB_POOLING_ENABLED, bEnabled));
97 // the settings for the single drivers
98 DriverPoolingSettings aSettings;
99 // first get all the drivers register at the driver manager
100 ODriverEnumeration aEnumDrivers;
101 for ( ODriverEnumeration::const_iterator aLoopDrivers = aEnumDrivers.begin();
102 aLoopDrivers != aEnumDrivers.end();
103 ++aLoopDrivers
106 aSettings.push_back(DriverPooling(*aLoopDrivers, sal_False, 120));
109 // then look for which of them settings are stored in the configuration
110 OConfigurationNode aDriverSettings = aConnectionPoolRoot.openNode(getDriverSettingsNodeName());
112 Sequence< ::rtl::OUString > aDriverKeys = aDriverSettings.getNodeNames();
113 const ::rtl::OUString* pDriverKeys = aDriverKeys.getConstArray();
114 const ::rtl::OUString* pDriverKeysEnd = pDriverKeys + aDriverKeys.getLength();
115 for (;pDriverKeys != pDriverKeysEnd; ++pDriverKeys)
117 // the name of the driver in this round
118 OConfigurationNode aThisDriverSettings = aDriverSettings.openNode(*pDriverKeys);
119 ::rtl::OUString sThisDriverName;
120 aThisDriverSettings.getNodeValue(getDriverNameNodeName()) >>= sThisDriverName;
122 // look if we (resp. the driver manager) know this driver
123 // doing O(n) search here, which is expensive, but this doesn't matter in this small case ...
124 DriverPoolingSettings::iterator aLookup;
125 for ( aLookup = aSettings.begin();
126 aLookup != aSettings.end();
127 ++aLookup
129 if (sThisDriverName.equals(aLookup->sName))
130 break;
132 if (aLookup == aSettings.end())
133 { // do not know the driver - add it
134 aSettings.push_back(DriverPooling(sThisDriverName, sal_False, 120));
136 // and the position of the new entry
137 aLookup = aSettings.end();
138 --aLookup;
141 // now fill this entry with the settings from the configuration
142 aThisDriverSettings.getNodeValue(getEnableNodeName()) >>= aLookup->bEnabled;
143 aThisDriverSettings.getNodeValue(getTimeoutNodeName()) >>= aLookup->nTimeoutSeconds;
146 _rFillItems.Put(DriverPoolingSettingsItem(SID_SB_DRIVER_TIMEOUTS, aSettings));
149 //--------------------------------------------------------------------
150 void ConnectionPoolConfig::SetOptions(const SfxItemSet& _rSourceItems)
152 // the config node where all pooling relevant info are stored under
153 OConfigurationTreeRoot aConnectionPoolRoot = OConfigurationTreeRoot::createWithServiceFactory(
154 ::comphelper::getProcessServiceFactory(), getConnectionPoolNodeName(), -1, OConfigurationTreeRoot::CM_UPDATABLE);
156 if (!aConnectionPoolRoot.isValid())
157 // already asserted by the OConfigurationTreeRoot
158 return;
160 sal_Bool bNeedCommit = sal_False;
162 // the global "enabled" flag
163 SFX_ITEMSET_GET( _rSourceItems, pEnabled, SfxBoolItem, SID_SB_POOLING_ENABLED, sal_True );
164 if (pEnabled)
166 sal_Bool bEnabled = pEnabled->GetValue();
167 aConnectionPoolRoot.setNodeValue(getEnablePoolingNodeName(), Any(&bEnabled, ::getBooleanCppuType()));
168 bNeedCommit = sal_True;
171 // the settings for the single drivers
172 SFX_ITEMSET_GET( _rSourceItems, pDriverSettings, DriverPoolingSettingsItem, SID_SB_DRIVER_TIMEOUTS, sal_True );
173 if (pDriverSettings)
175 OConfigurationNode aDriverSettings = aConnectionPoolRoot.openNode(getDriverSettingsNodeName());
176 if (!aDriverSettings.isValid())
177 return;
179 ::rtl::OUString sThisDriverName;
180 OConfigurationNode aThisDriverSettings;
182 const DriverPoolingSettings& rNewSettings = pDriverSettings->getSettings();
183 for ( DriverPoolingSettings::const_iterator aLoop = rNewSettings.begin();
184 aLoop != rNewSettings.end();
185 ++aLoop
188 // need the name as ::rtl::OUString
189 sThisDriverName = aLoop->sName;
191 // the sub-node for this driver
192 if (aDriverSettings.hasByName(aLoop->sName))
193 aThisDriverSettings = aDriverSettings.openNode(aLoop->sName);
194 else
195 aThisDriverSettings = aDriverSettings.createNode(aLoop->sName);
197 // set the values
198 aThisDriverSettings.setNodeValue(getDriverNameNodeName(), makeAny(sThisDriverName));
199 aThisDriverSettings.setNodeValue(getEnableNodeName(), Any(&aLoop->bEnabled, ::getBooleanCppuType()));
200 aThisDriverSettings.setNodeValue(getTimeoutNodeName(), makeAny(aLoop->nTimeoutSeconds));
202 bNeedCommit = sal_True;
204 if (bNeedCommit)
205 aConnectionPoolRoot.commit();
208 //........................................................................
209 } // namespace offapp
210 //........................................................................
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */