1*2ee96f1cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2ee96f1cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2ee96f1cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2ee96f1cSAndrew Rist  * distributed with this work for additional information
6*2ee96f1cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2ee96f1cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2ee96f1cSAndrew Rist  * "License"); you may not use this file except in compliance
9*2ee96f1cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2ee96f1cSAndrew Rist  *
11*2ee96f1cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2ee96f1cSAndrew Rist  *
13*2ee96f1cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2ee96f1cSAndrew Rist  * software distributed under the License is distributed on an
15*2ee96f1cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2ee96f1cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2ee96f1cSAndrew Rist  * specific language governing permissions and limitations
18*2ee96f1cSAndrew Rist  * under the License.
19*2ee96f1cSAndrew Rist  *
20*2ee96f1cSAndrew Rist  *************************************************************/
21*2ee96f1cSAndrew Rist 
22*2ee96f1cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_cui.hxx"
26cdf0e10cSrcweir #include "connpooloptions.hxx"
27cdf0e10cSrcweir #include "dbregisterednamesconfig.hxx"
28cdf0e10cSrcweir #include "dbregistersettings.hxx"
29cdf0e10cSrcweir #include "svx/svxids.hrc"
30cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
31cdf0e10cSrcweir #include <com/sun/star/sdb/XDatabaseRegistrations.hpp>
32cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
33cdf0e10cSrcweir #include <comphelper/extract.hxx>
34cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
35cdf0e10cSrcweir #include <svl/eitem.hxx>
36cdf0e10cSrcweir #include <svl/itemset.hxx>
37cdf0e10cSrcweir #include <unotools/pathoptions.hxx>
38cdf0e10cSrcweir #include <unotools/confignode.hxx>
39cdf0e10cSrcweir #include <tools/diagnose_ex.h>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir //........................................................................
42cdf0e10cSrcweir namespace svx
43cdf0e10cSrcweir {
44cdf0e10cSrcweir //........................................................................
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 	using namespace ::utl;
47cdf0e10cSrcweir 	using namespace ::com::sun::star::uno;
48cdf0e10cSrcweir 	using namespace ::com::sun::star::sdb;
49cdf0e10cSrcweir 	using namespace ::com::sun::star::container;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 	//====================================================================
52cdf0e10cSrcweir 	//= DbRegisteredNamesConfig
53cdf0e10cSrcweir 	//====================================================================
54cdf0e10cSrcweir 	//--------------------------------------------------------------------
GetOptions(SfxItemSet & _rFillItems)55cdf0e10cSrcweir 	void DbRegisteredNamesConfig::GetOptions( SfxItemSet& _rFillItems )
56cdf0e10cSrcweir 	{
57cdf0e10cSrcweir 		DatabaseRegistrations aSettings;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir         try
60cdf0e10cSrcweir         {
61cdf0e10cSrcweir             ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
62cdf0e10cSrcweir             Reference< XDatabaseRegistrations > xRegistrations(
63cdf0e10cSrcweir                 aContext.createComponent( "com.sun.star.sdb.DatabaseContext" ), UNO_QUERY_THROW );
64cdf0e10cSrcweir 
65cdf0e10cSrcweir             Sequence< ::rtl::OUString > aRegistrationNames( xRegistrations->getRegistrationNames() );
66cdf0e10cSrcweir             const ::rtl::OUString* pRegistrationName = aRegistrationNames.getConstArray();
67cdf0e10cSrcweir             const ::rtl::OUString* pRegistrationNamesEnd = pRegistrationName + aRegistrationNames.getLength();
68cdf0e10cSrcweir             for ( ; pRegistrationName != pRegistrationNamesEnd; ++pRegistrationName )
69cdf0e10cSrcweir             {
70cdf0e10cSrcweir                 ::rtl::OUString sLocation( xRegistrations->getDatabaseLocation( *pRegistrationName ) );
71cdf0e10cSrcweir                 aSettings[ *pRegistrationName ] =
72cdf0e10cSrcweir                     DatabaseRegistration( sLocation, xRegistrations->isDatabaseRegistrationReadOnly( *pRegistrationName ) );
73cdf0e10cSrcweir             }
74cdf0e10cSrcweir         }
75cdf0e10cSrcweir         catch( const Exception& )
76cdf0e10cSrcweir         {
77cdf0e10cSrcweir         	DBG_UNHANDLED_EXCEPTION();
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 		_rFillItems.Put( DatabaseMapItem( SID_SB_DB_REGISTER, aSettings ) );
81cdf0e10cSrcweir 	}
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 	//--------------------------------------------------------------------
SetOptions(const SfxItemSet & _rSourceItems)84cdf0e10cSrcweir 	void DbRegisteredNamesConfig::SetOptions(const SfxItemSet& _rSourceItems)
85cdf0e10cSrcweir 	{
86cdf0e10cSrcweir 		// the settings for the single drivers
87cdf0e10cSrcweir 		SFX_ITEMSET_GET( _rSourceItems, pRegistrations, DatabaseMapItem, SID_SB_DB_REGISTER, sal_True );
88cdf0e10cSrcweir 		if ( !pRegistrations )
89cdf0e10cSrcweir             return;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir         try
92cdf0e10cSrcweir         {
93cdf0e10cSrcweir             ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
94cdf0e10cSrcweir             Reference< XDatabaseRegistrations > xRegistrations(
95cdf0e10cSrcweir                 aContext.createComponent( "com.sun.star.sdb.DatabaseContext" ), UNO_QUERY_THROW );
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 		    const DatabaseRegistrations& rNewRegistrations = pRegistrations->getRegistrations();
98cdf0e10cSrcweir 		    for (	DatabaseRegistrations::const_iterator reg = rNewRegistrations.begin();
99cdf0e10cSrcweir 				    reg != rNewRegistrations.end();
100cdf0e10cSrcweir 				    ++reg
101cdf0e10cSrcweir 			    )
102cdf0e10cSrcweir 		    {
103cdf0e10cSrcweir                 const ::rtl::OUString sName = reg->first;
104cdf0e10cSrcweir                 const ::rtl::OUString sLocation = reg->second.sLocation;
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 			    if ( xRegistrations->hasRegisteredDatabase( sName ) )
107cdf0e10cSrcweir                 {
108cdf0e10cSrcweir                     if ( !xRegistrations->isDatabaseRegistrationReadOnly( sName ) )
109cdf0e10cSrcweir                         xRegistrations->changeDatabaseLocation( sName, sLocation );
110cdf0e10cSrcweir                     else
111cdf0e10cSrcweir                     {
112cdf0e10cSrcweir                         OSL_ENSURE( xRegistrations->getDatabaseLocation( sName ) == sLocation,
113cdf0e10cSrcweir                             "DbRegisteredNamesConfig::SetOptions: somebody changed a read-only registration. How unrespectful." );
114cdf0e10cSrcweir                     }
115cdf0e10cSrcweir                 }
116cdf0e10cSrcweir                 else
117cdf0e10cSrcweir                     xRegistrations->registerDatabaseLocation( sName, sLocation );
118cdf0e10cSrcweir             }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir             // delete unused entries
121cdf0e10cSrcweir             Sequence< ::rtl::OUString > aRegistrationNames = xRegistrations->getRegistrationNames();
122cdf0e10cSrcweir             const ::rtl::OUString* pRegistrationName = aRegistrationNames.getConstArray();
123cdf0e10cSrcweir             const ::rtl::OUString* pRegistrationNamesEnd = pRegistrationName + aRegistrationNames.getLength();
124cdf0e10cSrcweir             for ( ; pRegistrationName != pRegistrationNamesEnd; ++pRegistrationName )
125cdf0e10cSrcweir             {
126cdf0e10cSrcweir                 if ( rNewRegistrations.find( *pRegistrationName ) == rNewRegistrations.end() )
127cdf0e10cSrcweir                     xRegistrations->revokeDatabaseLocation( *pRegistrationName );
128cdf0e10cSrcweir             }
129cdf0e10cSrcweir         }
130cdf0e10cSrcweir         catch( const Exception& )
131cdf0e10cSrcweir         {
132cdf0e10cSrcweir         	DBG_UNHANDLED_EXCEPTION();
133cdf0e10cSrcweir         }
134cdf0e10cSrcweir 	}
135cdf0e10cSrcweir 
136cdf0e10cSrcweir //........................................................................
137cdf0e10cSrcweir }	// namespace svx
138cdf0e10cSrcweir //........................................................................
139