1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
36 using namespace ::utl
;
37 using namespace ::com::sun::star::uno
;
39 constexpr OUString CONNECTIONPOOL_NODENAME
= u
"org.openoffice.Office.DataAccess/ConnectionPool"_ustr
;
40 constexpr OUString ENABLE_POOLING
= u
"EnablePooling"_ustr
;
41 constexpr OUString DRIVER_SETTINGS
= u
"DriverSettings"_ustr
;
42 constexpr OUString DRIVER_NAME
= u
"DriverName"_ustr
;
43 constexpr OUString ENABLE
= u
"Enable"_ustr
;
44 constexpr OUString TIMEOUT
= u
"Timeout"_ustr
;
46 void ConnectionPoolConfig::GetOptions(SfxItemSet
& _rFillItems
)
48 // the config node where all pooling relevant info are stored under
49 OConfigurationTreeRoot aConnectionPoolRoot
= OConfigurationTreeRoot::createWithComponentContext(
50 ::comphelper::getProcessComponentContext(), CONNECTIONPOOL_NODENAME
, -1, OConfigurationTreeRoot::CM_READONLY
);
52 // the global "enabled" flag
53 Any aEnabled
= aConnectionPoolRoot
.getNodeValue(ENABLE_POOLING
);
55 aEnabled
>>= bEnabled
;
56 _rFillItems
.Put(SfxBoolItem(SID_SB_POOLING_ENABLED
, bEnabled
));
58 // the settings for the single drivers
59 DriverPoolingSettings aSettings
;
60 // first get all the drivers register at the driver manager
61 ODriverEnumeration aEnumDrivers
;
62 for (auto const& elem
: aEnumDrivers
)
64 aSettings
.push_back(DriverPooling(elem
));
67 // then look for which of them settings are stored in the configuration
68 OConfigurationNode aDriverSettings
= aConnectionPoolRoot
.openNode(DRIVER_SETTINGS
);
70 for (auto& driverKey
: aDriverSettings
.getNodeNames())
72 // the name of the driver in this round
73 OConfigurationNode aThisDriverSettings
= aDriverSettings
.openNode(driverKey
);
74 OUString sThisDriverName
;
75 aThisDriverSettings
.getNodeValue(DRIVER_NAME
) >>= sThisDriverName
;
77 // look if we (resp. the driver manager) know this driver
78 // doing O(n) search here, which is expensive, but this doesn't matter in this small case ...
79 DriverPoolingSettings::iterator aLookup
;
80 for ( aLookup
= aSettings
.begin();
81 aLookup
!= aSettings
.end();
84 if (sThisDriverName
== aLookup
->sName
)
87 if (aLookup
== aSettings
.end())
88 { // do not know the driver - add it
89 aSettings
.push_back(DriverPooling(sThisDriverName
));
91 // and the position of the new entry
92 aLookup
= aSettings
.end();
96 // now fill this entry with the settings from the configuration
97 aThisDriverSettings
.getNodeValue(ENABLE
) >>= aLookup
->bEnabled
;
98 aThisDriverSettings
.getNodeValue(TIMEOUT
) >>= aLookup
->nTimeoutSeconds
;
101 _rFillItems
.Put(DriverPoolingSettingsItem(SID_SB_DRIVER_TIMEOUTS
, std::move(aSettings
)));
105 void ConnectionPoolConfig::SetOptions(const SfxItemSet
& _rSourceItems
)
107 // the config node where all pooling relevant info are stored under
108 OConfigurationTreeRoot aConnectionPoolRoot
= OConfigurationTreeRoot::createWithComponentContext(
109 ::comphelper::getProcessComponentContext(), CONNECTIONPOOL_NODENAME
);
111 if (!aConnectionPoolRoot
.isValid())
112 // already asserted by the OConfigurationTreeRoot
115 bool bNeedCommit
= false;
117 // the global "enabled" flag
118 const SfxBoolItem
* pEnabled
= _rSourceItems
.GetItem
<SfxBoolItem
>(SID_SB_POOLING_ENABLED
);
121 bool bEnabled
= pEnabled
->GetValue();
122 aConnectionPoolRoot
.setNodeValue(ENABLE_POOLING
, Any(bEnabled
));
126 // the settings for the single drivers
127 const DriverPoolingSettingsItem
* pDriverSettings
= _rSourceItems
.GetItem
<DriverPoolingSettingsItem
>(SID_SB_DRIVER_TIMEOUTS
);
130 OConfigurationNode aDriverSettings
= aConnectionPoolRoot
.openNode(DRIVER_SETTINGS
);
131 if (!aDriverSettings
.isValid())
134 OUString sThisDriverName
;
135 OConfigurationNode aThisDriverSettings
;
137 const DriverPoolingSettings
& rNewSettings
= pDriverSettings
->getSettings();
138 for (auto const& newSetting
: rNewSettings
)
140 // need the name as OUString
141 sThisDriverName
= newSetting
.sName
;
143 // the sub-node for this driver
144 if (aDriverSettings
.hasByName(newSetting
.sName
))
145 aThisDriverSettings
= aDriverSettings
.openNode(newSetting
.sName
);
147 aThisDriverSettings
= aDriverSettings
.createNode(newSetting
.sName
);
150 aThisDriverSettings
.setNodeValue(DRIVER_NAME
, Any(sThisDriverName
));
151 aThisDriverSettings
.setNodeValue(ENABLE
, Any(newSetting
.bEnabled
));
152 aThisDriverSettings
.setNodeValue(TIMEOUT
, Any(newSetting
.nTimeoutSeconds
));
157 aConnectionPoolRoot
.commit();
161 } // namespace offapp
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */