Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / cui / source / options / connpoolconfig.cxx
blob8bf95ee0db5dfedbd8c98378de36f349e514a080
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 <svl/itemset.hxx>
24 #include <svx/databaseregistrationui.hxx>
25 #include <unotools/confignode.hxx>
26 #include <svl/eitem.hxx>
27 #include <comphelper/processfactory.hxx>
28 #include "sdbcdriverenum.hxx"
30 #include <com/sun/star/uno/Sequence.hxx>
32 namespace offapp
36 using namespace ::utl;
37 using namespace ::com::sun::star::uno;
40 static OUString getConnectionPoolNodeName()
42 return "org.openoffice.Office.DataAccess/ConnectionPool";
46 static OUString getEnablePoolingNodeName()
48 return "EnablePooling";
52 static OUString getDriverSettingsNodeName()
54 return "DriverSettings";
58 static OUString getDriverNameNodeName()
60 return "DriverName";
64 static OUString getEnableNodeName()
66 return "Enable";
70 static OUString getTimeoutNodeName()
72 return "Timeout";
75 void ConnectionPoolConfig::GetOptions(SfxItemSet& _rFillItems)
77 // the config node where all pooling relevant info are stored under
78 OConfigurationTreeRoot aConnectionPoolRoot = OConfigurationTreeRoot::createWithComponentContext(
79 ::comphelper::getProcessComponentContext(), getConnectionPoolNodeName(), -1, OConfigurationTreeRoot::CM_READONLY);
81 // the global "enabled" flag
82 Any aEnabled = aConnectionPoolRoot.getNodeValue(getEnablePoolingNodeName());
83 bool bEnabled = true;
84 aEnabled >>= bEnabled;
85 _rFillItems.Put(SfxBoolItem(SID_SB_POOLING_ENABLED, bEnabled));
87 // the settings for the single drivers
88 DriverPoolingSettings aSettings;
89 // first get all the drivers register at the driver manager
90 ODriverEnumeration aEnumDrivers;
91 for (auto const& elem : aEnumDrivers)
93 aSettings.push_back(DriverPooling(elem));
96 // then look for which of them settings are stored in the configuration
97 OConfigurationNode aDriverSettings = aConnectionPoolRoot.openNode(getDriverSettingsNodeName());
99 Sequence< OUString > aDriverKeys = aDriverSettings.getNodeNames();
100 const OUString* pDriverKeys = aDriverKeys.getConstArray();
101 const OUString* pDriverKeysEnd = pDriverKeys + aDriverKeys.getLength();
102 for (;pDriverKeys != pDriverKeysEnd; ++pDriverKeys)
104 // the name of the driver in this round
105 OConfigurationNode aThisDriverSettings = aDriverSettings.openNode(*pDriverKeys);
106 OUString sThisDriverName;
107 aThisDriverSettings.getNodeValue(getDriverNameNodeName()) >>= sThisDriverName;
109 // look if we (resp. the driver manager) know this driver
110 // doing O(n) search here, which is expensive, but this doesn't matter in this small case ...
111 DriverPoolingSettings::iterator aLookup;
112 for ( aLookup = aSettings.begin();
113 aLookup != aSettings.end();
114 ++aLookup
116 if (sThisDriverName == aLookup->sName)
117 break;
119 if (aLookup == aSettings.end())
120 { // do not know the driver - add it
121 aSettings.push_back(DriverPooling(sThisDriverName));
123 // and the position of the new entry
124 aLookup = aSettings.end();
125 --aLookup;
128 // now fill this entry with the settings from the configuration
129 aThisDriverSettings.getNodeValue(getEnableNodeName()) >>= aLookup->bEnabled;
130 aThisDriverSettings.getNodeValue(getTimeoutNodeName()) >>= aLookup->nTimeoutSeconds;
133 _rFillItems.Put(DriverPoolingSettingsItem(SID_SB_DRIVER_TIMEOUTS, aSettings));
137 void ConnectionPoolConfig::SetOptions(const SfxItemSet& _rSourceItems)
139 // the config node where all pooling relevant info are stored under
140 OConfigurationTreeRoot aConnectionPoolRoot = OConfigurationTreeRoot::createWithComponentContext(
141 ::comphelper::getProcessComponentContext(), getConnectionPoolNodeName());
143 if (!aConnectionPoolRoot.isValid())
144 // already asserted by the OConfigurationTreeRoot
145 return;
147 bool bNeedCommit = false;
149 // the global "enabled" flag
150 const SfxBoolItem* pEnabled = _rSourceItems.GetItem<SfxBoolItem>(SID_SB_POOLING_ENABLED);
151 if (pEnabled)
153 bool bEnabled = pEnabled->GetValue();
154 aConnectionPoolRoot.setNodeValue(getEnablePoolingNodeName(), Any(bEnabled));
155 bNeedCommit = true;
158 // the settings for the single drivers
159 const DriverPoolingSettingsItem* pDriverSettings = _rSourceItems.GetItem<DriverPoolingSettingsItem>(SID_SB_DRIVER_TIMEOUTS);
160 if (pDriverSettings)
162 OConfigurationNode aDriverSettings = aConnectionPoolRoot.openNode(getDriverSettingsNodeName());
163 if (!aDriverSettings.isValid())
164 return;
166 OUString sThisDriverName;
167 OConfigurationNode aThisDriverSettings;
169 const DriverPoolingSettings& rNewSettings = pDriverSettings->getSettings();
170 for (auto const& newSetting : rNewSettings)
172 // need the name as OUString
173 sThisDriverName = newSetting.sName;
175 // the sub-node for this driver
176 if (aDriverSettings.hasByName(newSetting.sName))
177 aThisDriverSettings = aDriverSettings.openNode(newSetting.sName);
178 else
179 aThisDriverSettings = aDriverSettings.createNode(newSetting.sName);
181 // set the values
182 aThisDriverSettings.setNodeValue(getDriverNameNodeName(), Any(sThisDriverName));
183 aThisDriverSettings.setNodeValue(getEnableNodeName(), Any(newSetting.bEnabled));
184 aThisDriverSettings.setNodeValue(getTimeoutNodeName(), Any(newSetting.nTimeoutSeconds));
186 bNeedCommit = true;
188 if (bNeedCommit)
189 aConnectionPoolRoot.commit();
193 } // namespace offapp
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */