xref: /trunk/main/cui/source/options/dbregisterednamesconfig.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cui.hxx"
30 #include "connpooloptions.hxx"
31 #include "dbregisterednamesconfig.hxx"
32 #include "dbregistersettings.hxx"
33 #include "svx/svxids.hrc"
34 #include <com/sun/star/container/XNameAccess.hpp>
35 #include <com/sun/star/sdb/XDatabaseRegistrations.hpp>
36 #include <comphelper/componentcontext.hxx>
37 #include <comphelper/extract.hxx>
38 #include <comphelper/processfactory.hxx>
39 #include <svl/eitem.hxx>
40 #include <svl/itemset.hxx>
41 #include <unotools/pathoptions.hxx>
42 #include <unotools/confignode.hxx>
43 #include <tools/diagnose_ex.h>
44 
45 //........................................................................
46 namespace svx
47 {
48 //........................................................................
49 
50     using namespace ::utl;
51     using namespace ::com::sun::star::uno;
52     using namespace ::com::sun::star::sdb;
53     using namespace ::com::sun::star::container;
54 
55     //====================================================================
56     //= DbRegisteredNamesConfig
57     //====================================================================
58     //--------------------------------------------------------------------
59     void DbRegisteredNamesConfig::GetOptions( SfxItemSet& _rFillItems )
60     {
61         DatabaseRegistrations aSettings;
62 
63         try
64         {
65             ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
66             Reference< XDatabaseRegistrations > xRegistrations(
67                 aContext.createComponent( "com.sun.star.sdb.DatabaseContext" ), UNO_QUERY_THROW );
68 
69             Sequence< ::rtl::OUString > aRegistrationNames( xRegistrations->getRegistrationNames() );
70             const ::rtl::OUString* pRegistrationName = aRegistrationNames.getConstArray();
71             const ::rtl::OUString* pRegistrationNamesEnd = pRegistrationName + aRegistrationNames.getLength();
72             for ( ; pRegistrationName != pRegistrationNamesEnd; ++pRegistrationName )
73             {
74                 ::rtl::OUString sLocation( xRegistrations->getDatabaseLocation( *pRegistrationName ) );
75                 aSettings[ *pRegistrationName ] =
76                     DatabaseRegistration( sLocation, xRegistrations->isDatabaseRegistrationReadOnly( *pRegistrationName ) );
77             }
78         }
79         catch( const Exception& )
80         {
81             DBG_UNHANDLED_EXCEPTION();
82         }
83 
84         _rFillItems.Put( DatabaseMapItem( SID_SB_DB_REGISTER, aSettings ) );
85     }
86 
87     //--------------------------------------------------------------------
88     void DbRegisteredNamesConfig::SetOptions(const SfxItemSet& _rSourceItems)
89     {
90         // the settings for the single drivers
91         SFX_ITEMSET_GET( _rSourceItems, pRegistrations, DatabaseMapItem, SID_SB_DB_REGISTER, sal_True );
92         if ( !pRegistrations )
93             return;
94 
95         try
96         {
97             ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
98             Reference< XDatabaseRegistrations > xRegistrations(
99                 aContext.createComponent( "com.sun.star.sdb.DatabaseContext" ), UNO_QUERY_THROW );
100 
101             const DatabaseRegistrations& rNewRegistrations = pRegistrations->getRegistrations();
102             for (   DatabaseRegistrations::const_iterator reg = rNewRegistrations.begin();
103                     reg != rNewRegistrations.end();
104                     ++reg
105                 )
106             {
107                 const ::rtl::OUString sName = reg->first;
108                 const ::rtl::OUString sLocation = reg->second.sLocation;
109 
110                 if ( xRegistrations->hasRegisteredDatabase( sName ) )
111                 {
112                     if ( !xRegistrations->isDatabaseRegistrationReadOnly( sName ) )
113                         xRegistrations->changeDatabaseLocation( sName, sLocation );
114                     else
115                     {
116                         OSL_ENSURE( xRegistrations->getDatabaseLocation( sName ) == sLocation,
117                             "DbRegisteredNamesConfig::SetOptions: somebody changed a read-only registration. How unrespectful." );
118                     }
119                 }
120                 else
121                     xRegistrations->registerDatabaseLocation( sName, sLocation );
122             }
123 
124             // delete unused entries
125             Sequence< ::rtl::OUString > aRegistrationNames = xRegistrations->getRegistrationNames();
126             const ::rtl::OUString* pRegistrationName = aRegistrationNames.getConstArray();
127             const ::rtl::OUString* pRegistrationNamesEnd = pRegistrationName + aRegistrationNames.getLength();
128             for ( ; pRegistrationName != pRegistrationNamesEnd; ++pRegistrationName )
129             {
130                 if ( rNewRegistrations.find( *pRegistrationName ) == rNewRegistrations.end() )
131                     xRegistrations->revokeDatabaseLocation( *pRegistrationName );
132             }
133         }
134         catch( const Exception& )
135         {
136             DBG_UNHANDLED_EXCEPTION();
137         }
138     }
139 
140 //........................................................................
141 }   // namespace svx
142 //........................................................................
143