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