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_dbaccess.hxx"
26 
27 #include "adminpages.hxx"
28 #include "DbAdminImpl.hxx"
29 #include "dbu_dlg.hrc"
30 #include "DriverSettings.hxx"
31 #include "dsitems.hxx"
32 #include "propertysetitem.hxx"
33 #include "UITools.hxx"
34 #include "UserAdmin.hxx"
35 #include "UserAdminDlg.hrc"
36 #include "UserAdminDlg.hxx"
37 
38 #include <comphelper/componentcontext.hxx>
39 #include <connectivity/dbmetadata.hxx>
40 #include <cppuhelper/exc_hlp.hxx>
41 #include <svl/eitem.hxx>
42 #include <svl/intitem.hxx>
43 #include <svl/stritem.hxx>
44 #include <tools/diagnose_ex.h>
45 #include <vcl/msgbox.hxx>
46 #include <vcl/stdtext.hxx>
47 
48 //.........................................................................
49 namespace dbaui
50 {
51 //.........................................................................
52 	using namespace ::com::sun::star::uno;
53 	using namespace ::com::sun::star::beans;
54 	using namespace ::com::sun::star::lang;
55 	using namespace ::com::sun::star::sdbc;
56 	using namespace ::com::sun::star::sdbcx;
57 
58 	//========================================================================
59 	//= OUserAdminDlg
DBG_NAME(OUserAdminDlg)60 DBG_NAME(OUserAdminDlg)
61 //========================================================================
62 	OUserAdminDlg::OUserAdminDlg(Window* _pParent
63 											, SfxItemSet* _pItems
64 											,const Reference< XMultiServiceFactory >& _rxORB
65 											,const ::com::sun::star::uno::Any& _aDataSourceName
66 											,const Reference< XConnection >& _xConnection)
67 		:SfxTabDialog(_pParent, ModuleRes(DLG_DATABASE_USERADMIN), _pItems)
68 		,m_pItemSet(_pItems)
69 		,m_xConnection(_xConnection)
70 		,m_bOwnConnection(!_xConnection.is())
71 	{
72         DBG_CTOR(OUserAdminDlg,NULL);
73 
74 		m_pImpl = ::std::auto_ptr<ODbDataSourceAdministrationHelper>(new ODbDataSourceAdministrationHelper(_rxORB,_pParent,this));
75 		m_pImpl->setDataSourceOrName(_aDataSourceName);
76 		Reference< XPropertySet > xDatasource = m_pImpl->getCurrentDataSource();
77 		m_pImpl->translateProperties(xDatasource, *_pItems);
78 		SetInputSet(_pItems);
79 		// propagate this set as our new input set and reset the example set
80 		delete pExampleSet;
81 		pExampleSet = new SfxItemSet(*GetInputSetImpl());
82 
83 		AddTabPage(TAB_PAGE_USERADMIN, String(ModuleRes(STR_PAGETITLE_USERADMIN)), OUserAdmin::Create,0, sal_False, 1);
84 
85 		// remove the reset button - it's meaning is much too ambiguous in this dialog
86 		RemoveResetButton();
87 		FreeResource();
88 	}
89 
90 	// -----------------------------------------------------------------------
~OUserAdminDlg()91 	OUserAdminDlg::~OUserAdminDlg()
92 	{
93 		if ( m_bOwnConnection )
94 			try
95 			{
96 				::comphelper::disposeComponent(m_xConnection);
97 			}
98 			catch(Exception){}
99 
100 		SetInputSet(NULL);
101 		DELETEZ(pExampleSet);
102 
103         DBG_DTOR(OUserAdminDlg,NULL);
104     }
105 	// -----------------------------------------------------------------------
Execute()106 	short OUserAdminDlg::Execute()
107 	{
108 		try
109 		{
110             ::dbtools::DatabaseMetaData aMetaData( createConnection().first );
111             if ( !aMetaData.supportsUserAdministration( ::comphelper::ComponentContext( getORB() ) ) )
112 			{
113 				String sError(ModuleRes(STR_USERADMIN_NOT_AVAILABLE));
114 				throw SQLException(sError,NULL,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("S1000")) ,0,Any());
115 			}
116 		}
117 		catch(const SQLException& e)
118 		{
119             ::dbaui::showError( ::dbtools::SQLExceptionInfo( ::cppu::getCaughtException() ), GetParent(), getORB() );
120 			return RET_CANCEL;
121 		}
122         catch( const Exception& )
123         {
124             DBG_UNHANDLED_EXCEPTION();
125         }
126 		short nRet = SfxTabDialog::Execute();
127 		if ( nRet == RET_OK )
128 			m_pImpl->saveChanges(*GetOutputItemSet());
129 		return nRet;
130 	}
131 	//-------------------------------------------------------------------------
PageCreated(sal_uInt16 _nId,SfxTabPage & _rPage)132 	void OUserAdminDlg::PageCreated(sal_uInt16 _nId, SfxTabPage& _rPage)
133 	{
134 		// register ourself as modified listener
135 		static_cast<OGenericAdministrationPage&>(_rPage).SetServiceFactory(m_pImpl->getORB());
136 		static_cast<OGenericAdministrationPage&>(_rPage).SetAdminDialog(this,this);
137 
138 		AdjustLayout();
139 		Window *pWin = GetViewWindow();
140 		if(pWin)
141 			pWin->Invalidate();
142 
143 		SfxTabDialog::PageCreated(_nId, _rPage);
144 	}
145 	// -----------------------------------------------------------------------------
getOutputSet() const146 	const SfxItemSet* OUserAdminDlg::getOutputSet() const
147 	{
148 		return m_pItemSet;
149 	}
150 	// -----------------------------------------------------------------------------
getWriteOutputSet()151 	SfxItemSet* OUserAdminDlg::getWriteOutputSet()
152 	{
153 		return m_pItemSet;
154 	}
155 	// -----------------------------------------------------------------------------
createConnection()156 	::std::pair< Reference<XConnection>,sal_Bool> OUserAdminDlg::createConnection()
157 	{
158 		if ( !m_xConnection.is() )
159 		{
160 			m_xConnection = m_pImpl->createConnection().first;
161 			m_bOwnConnection = m_xConnection.is();
162 		}
163 		return ::std::pair< Reference<XConnection>,sal_Bool> (m_xConnection,sal_False);
164 	}
165 	// -----------------------------------------------------------------------------
getORB() const166 	Reference< XMultiServiceFactory > OUserAdminDlg::getORB() const
167 	{
168 		return m_pImpl->getORB();
169 	}
170 	// -----------------------------------------------------------------------------
getDriver()171 	Reference< XDriver > OUserAdminDlg::getDriver()
172 	{
173 		return m_pImpl->getDriver();
174 	}
175 	// -----------------------------------------------------------------------------
getDatasourceType(const SfxItemSet & _rSet) const176 	::rtl::OUString	OUserAdminDlg::getDatasourceType(const SfxItemSet& _rSet) const
177 	{
178 		return m_pImpl->getDatasourceType(_rSet);
179 	}
180 	// -----------------------------------------------------------------------------
clearPassword()181 	void OUserAdminDlg::clearPassword()
182 	{
183 		m_pImpl->clearPassword();
184 	}
185 	// -----------------------------------------------------------------------------
setTitle(const::rtl::OUString & _sTitle)186 	void OUserAdminDlg::setTitle(const ::rtl::OUString& _sTitle)
187 	{
188 		SetText(_sTitle);
189 	}
190     //-------------------------------------------------------------------------
enableConfirmSettings(bool _bEnable)191     void OUserAdminDlg::enableConfirmSettings( bool _bEnable )
192     {
193         (void)_bEnable;
194     }
195 	//-------------------------------------------------------------------------
saveDatasource()196 	sal_Bool OUserAdminDlg::saveDatasource()
197 	{
198 		return PrepareLeaveCurrentPage();
199 	}
200 //.........................................................................
201 }	// namespace dbaui
202 //.........................................................................
203