update dev300-m58
[ooovba.git] / dbaccess / source / ui / control / TableGrantCtrl.cxx
blob6a424a2badaae8b0cb70668b000be4d247821556
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: TableGrantCtrl.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
33 #ifndef DBAUI_TABLEGRANTCONTROL_HXX
34 #include "TableGrantCtrl.hxx"
35 #endif
36 #ifndef _TOOLS_DEBUG_HXX
37 #include <tools/debug.hxx>
38 #endif
39 #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
40 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
41 #endif
42 #ifndef _COM_SUN_STAR_SDBCX_PRIVILEGE_HPP_
43 #include <com/sun/star/sdbcx/Privilege.hpp>
44 #endif
45 #ifndef _COM_SUN_STAR_SDBCX_PRIVILEGEOBJECT_HPP_
46 #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
47 #endif
48 #ifndef _COM_SUN_STAR_SDBCX_XUSERSSUPPLIER_HPP_
49 #include <com/sun/star/sdbcx/XUsersSupplier.hpp>
50 #endif
51 #ifndef _COM_SUN_STAR_SDBCX_XAUTHORIZABLE_HPP_
52 #include <com/sun/star/sdbcx/XAuthorizable.hpp>
53 #endif
54 #ifndef _DBU_CONTROL_HRC_
55 #include "dbu_control.hrc"
56 #endif
57 #ifndef DBAUI_TOOLS_HXX
58 #include "UITools.hxx"
59 #endif
61 using namespace ::com::sun::star::accessibility;
62 using namespace ::com::sun::star::container;
63 using namespace ::com::sun::star::sdbcx;
64 using namespace ::com::sun::star::sdbc;
65 using namespace ::com::sun::star::uno;
66 using namespace ::dbaui;
67 using namespace ::svt;
69 const USHORT COL_TABLE_NAME = 1;
70 const USHORT COL_SELECT = 2;
71 const USHORT COL_INSERT = 3;
72 const USHORT COL_DELETE = 4;
73 const USHORT COL_UPDATE = 5;
74 const USHORT COL_ALTER = 6;
75 const USHORT COL_REF = 7;
76 const USHORT COL_DROP = 8;
78 DBG_NAME(OTableGrantControl)
80 //================================================================================
81 // OTableGrantControl
82 //================================================================================
83 OTableGrantControl::OTableGrantControl( Window* pParent,const ResId& _RsId)
84 :EditBrowseBox( pParent,_RsId, EBBF_SMART_TAB_TRAVEL | EBBF_NOROWPICTURE )
85 ,m_pCheckCell( NULL )
86 ,m_pEdit( NULL )
87 ,m_nDataPos( 0 )
88 ,m_bEnable(TRUE)
89 ,m_nDeactivateEvent(0)
91 DBG_CTOR(OTableGrantControl,NULL);
92 //////////////////////////////////////////////////////////////////////
93 // Spalten einfuegen
94 USHORT i=1;
95 InsertDataColumn( i, String(ModuleRes(STR_TABLE_PRIV_NAME) ), 75);
96 FreezeColumn(i++);
97 InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_SELECT)), 75);
98 InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_INSERT)), 75);
99 InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_DELETE)), 75);
100 InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_UPDATE)), 75);
101 InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_ALTER)), 75);
102 InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_REFERENCE)), 75);
103 InsertDataColumn( i++, String(ModuleRes(STR_TABLE_PRIV_DROP)), 75);
105 while(--i)
106 SetColumnWidth(i,GetAutoColumnWidth(i));
109 //------------------------------------------------------------------------
110 OTableGrantControl::~OTableGrantControl()
112 DBG_DTOR(OTableGrantControl,NULL);
113 if (m_nDeactivateEvent)
115 Application::RemoveUserEvent(m_nDeactivateEvent);
116 m_nDeactivateEvent = 0;
119 delete m_pCheckCell;
120 delete m_pEdit;
122 m_xTables = NULL;
124 // -----------------------------------------------------------------------------
125 void OTableGrantControl::setTablesSupplier(const Reference< XTablesSupplier >& _xTablesSup)
127 // first we need the users
128 Reference< XUsersSupplier> xUserSup(_xTablesSup,UNO_QUERY);
129 if(xUserSup.is())
130 m_xUsers = xUserSup->getUsers();
132 // second we need the tables to determine which privileges the user has
133 if(_xTablesSup.is())
134 m_xTables = _xTablesSup->getTables();
136 if(m_xTables.is())
137 m_aTableNames = m_xTables->getElementNames();
139 OSL_ENSURE(m_xUsers.is(),"No user access supported!");
140 OSL_ENSURE(m_xTables.is(),"No tables supported!");
142 // -----------------------------------------------------------------------------
143 void OTableGrantControl::setORB(const Reference< ::com::sun::star::lang::XMultiServiceFactory>& _xORB)
145 m_xORB = _xORB;
147 //------------------------------------------------------------------------
148 void OTableGrantControl::UpdateTables()
150 RemoveRows();
152 if(m_xTables.is())
153 RowInserted(0, m_aTableNames.getLength());
154 // m_bEnable = m_xDb->GetUser() != ((OUserAdmin*)GetParent())->GetUser();
156 //------------------------------------------------------------------------
157 void OTableGrantControl::Init()
159 DBG_CHKTHIS(OTableGrantControl,NULL);
160 EditBrowseBox::Init();
162 //////////////////////////////////////////////////////////////////////
163 // ComboBox instanzieren
164 if(!m_pCheckCell)
166 m_pCheckCell = new CheckBoxControl( &GetDataWindow() );
167 m_pCheckCell->GetBox().EnableTriState(FALSE);
169 m_pEdit = new Edit( &GetDataWindow() );
170 m_pEdit->SetReadOnly();
171 m_pEdit->Enable(FALSE);
174 UpdateTables();
175 //////////////////////////////////////////////////////////////////////
176 // Browser Mode setzen
177 BrowserMode nMode = BROWSER_COLUMNSELECTION | BROWSER_HLINESFULL | BROWSER_VLINESFULL |
178 BROWSER_HIDECURSOR | BROWSER_HIDESELECT;
180 SetMode(nMode);
183 //------------------------------------------------------------------------------
184 void OTableGrantControl::Resize()
186 DBG_CHKTHIS(OTableGrantControl,NULL);
187 EditBrowseBox::Resize();
190 //------------------------------------------------------------------------------
191 long OTableGrantControl::PreNotify(NotifyEvent& rNEvt)
193 DBG_CHKTHIS(OTableGrantControl,NULL);
194 if (rNEvt.GetType() == EVENT_LOSEFOCUS)
195 if (!HasChildPathFocus())
197 if (m_nDeactivateEvent)
198 Application::RemoveUserEvent(m_nDeactivateEvent);
199 m_nDeactivateEvent = Application::PostUserEvent(LINK(this, OTableGrantControl, AsynchDeactivate));
201 if (rNEvt.GetType() == EVENT_GETFOCUS)
203 if (m_nDeactivateEvent)
204 Application::RemoveUserEvent(m_nDeactivateEvent);
205 m_nDeactivateEvent = Application::PostUserEvent(LINK(this, OTableGrantControl, AsynchActivate));
207 return EditBrowseBox::PreNotify(rNEvt);
210 //------------------------------------------------------------------------------
211 IMPL_LINK(OTableGrantControl, AsynchActivate, void*, EMPTYARG)
213 m_nDeactivateEvent = 0;
214 ActivateCell();
215 return 0L;
218 //------------------------------------------------------------------------------
219 IMPL_LINK(OTableGrantControl, AsynchDeactivate, void*, EMPTYARG)
221 m_nDeactivateEvent = 0;
222 DeactivateCell();
223 return 0L;
226 //------------------------------------------------------------------------------
227 BOOL OTableGrantControl::IsTabAllowed(BOOL bForward) const
229 DBG_CHKTHIS(OTableGrantControl,NULL);
230 long nRow = GetCurRow();
231 USHORT nCol = GetCurColumnId();
233 if (bForward && (nCol == 2) && (nRow == GetRowCount() - 1))
234 return FALSE;
236 if (!bForward && (nCol == 1) && (nRow == 0))
237 return FALSE;
239 return EditBrowseBox::IsTabAllowed(bForward);
241 //------------------------------------------------------------------------------
242 #define GRANT_REVOKE_RIGHT(what) \
243 if(m_pCheckCell->GetBox().IsChecked()) \
244 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,what);\
245 else \
246 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,what)
248 //------------------------------------------------------------------------------
249 BOOL OTableGrantControl::SaveModified()
251 DBG_CHKTHIS(OTableGrantControl,NULL);
253 sal_Int32 nRow = GetCurRow();
254 if(nRow == -1 || nRow >= m_aTableNames.getLength())
255 return FALSE;
257 ::rtl::OUString sTableName = m_aTableNames[nRow];
258 BOOL bErg = TRUE;
262 if ( m_xUsers->hasByName(m_sUserName) )
264 Reference<XAuthorizable> xAuth(m_xUsers->getByName(m_sUserName),UNO_QUERY);
265 if ( xAuth.is() )
267 switch( GetCurColumnId() )
269 case COL_INSERT:
270 GRANT_REVOKE_RIGHT(Privilege::INSERT);
271 break;
272 case COL_DELETE:
273 GRANT_REVOKE_RIGHT(Privilege::DELETE);
274 break;
275 case COL_UPDATE:
276 GRANT_REVOKE_RIGHT(Privilege::UPDATE);
277 break;
278 case COL_ALTER:
279 GRANT_REVOKE_RIGHT(Privilege::ALTER);
280 break;
281 case COL_SELECT:
282 GRANT_REVOKE_RIGHT(Privilege::SELECT);
283 break;
284 case COL_REF:
285 GRANT_REVOKE_RIGHT(Privilege::REFERENCE);
286 break;
287 case COL_DROP:
288 GRANT_REVOKE_RIGHT(Privilege::DROP);
289 break;
291 fillPrivilege(nRow);
295 catch(SQLException& e)
297 bErg = FALSE;
298 ::dbaui::showError(::dbtools::SQLExceptionInfo(e),GetParent(),m_xORB);
300 if(bErg && Controller().Is())
301 Controller()->ClearModified();
302 if(!bErg)
303 UpdateTables();
305 return bErg;
308 //------------------------------------------------------------------------------
309 String OTableGrantControl::GetCellText( long nRow, USHORT nColId ) const
311 DBG_CHKTHIS(OTableGrantControl,NULL);
312 if(COL_TABLE_NAME == nColId)
313 return m_aTableNames[nRow];
315 sal_Int32 nPriv = 0;
316 TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
317 if(aFind != m_aPrivMap.end())
318 nPriv = aFind->second.nRights;
320 return String::CreateFromInt32(isAllowed(nColId,nPriv) ? 1 :0);
323 //------------------------------------------------------------------------------
324 void OTableGrantControl::InitController( CellControllerRef& /*rController*/, long nRow, USHORT nColumnId )
326 DBG_CHKTHIS(OTableGrantControl,NULL);
327 String sTablename = m_aTableNames[nRow];
328 // special case for tablename
329 if(nColumnId == COL_TABLE_NAME)
330 m_pEdit->SetText(sTablename);
331 else
333 // get the privileges from the user
334 TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
335 m_pCheckCell->GetBox().Check(aFind != m_aPrivMap.end() ? isAllowed(nColumnId,aFind->second.nRights) : FALSE);
338 // -----------------------------------------------------------------------------
339 void OTableGrantControl::fillPrivilege(sal_Int32 _nRow) const
342 if ( m_xUsers->hasByName(m_sUserName) )
346 Reference<XAuthorizable> xAuth(m_xUsers->getByName(m_sUserName),UNO_QUERY);
347 if ( xAuth.is() )
349 // get the privileges
350 TPrivileges nRights;
351 nRights.nRights = xAuth->getPrivileges(m_aTableNames[_nRow],PrivilegeObject::TABLE);
352 if(m_xGrantUser.is())
353 nRights.nWithGrant = m_xGrantUser->getGrantablePrivileges(m_aTableNames[_nRow],PrivilegeObject::TABLE);
354 else
355 nRights.nWithGrant = 0;
357 m_aPrivMap[m_aTableNames[_nRow]] = nRights;
360 catch(SQLException& e)
362 ::dbaui::showError(::dbtools::SQLExceptionInfo(e),GetParent(),m_xORB);
364 catch(Exception& )
369 // -----------------------------------------------------------------------------
370 sal_Bool OTableGrantControl::isAllowed(USHORT _nColumnId,sal_Int32 _nPrivilege) const
372 sal_Bool bAllowed = sal_False;
373 switch (_nColumnId)
375 case COL_INSERT:
376 bAllowed = (Privilege::INSERT & _nPrivilege) == Privilege::INSERT;
377 break;
378 case COL_DELETE:
379 bAllowed = (Privilege::DELETE & _nPrivilege) == Privilege::DELETE;
380 break;
381 case COL_UPDATE:
382 bAllowed = (Privilege::UPDATE & _nPrivilege) == Privilege::UPDATE;
383 break;
384 case COL_ALTER:
385 bAllowed = (Privilege::ALTER & _nPrivilege) == Privilege::ALTER;
386 break;
387 case COL_SELECT:
388 bAllowed = (Privilege::SELECT & _nPrivilege) == Privilege::SELECT;
389 break;
390 case COL_REF:
391 bAllowed = (Privilege::REFERENCE & _nPrivilege) == Privilege::REFERENCE;
392 break;
393 case COL_DROP:
394 bAllowed = (Privilege::DROP & _nPrivilege) == Privilege::DROP;
395 break;
397 return bAllowed;
399 // -----------------------------------------------------------------------------
400 void OTableGrantControl::setUserName(const ::rtl::OUString _sUserName)
402 m_sUserName = _sUserName;
403 m_aPrivMap = TTablePrivilegeMap();
405 // -----------------------------------------------------------------------------
406 void OTableGrantControl::setGrantUser(const Reference< XAuthorizable>& _xGrantUser)
408 OSL_ENSURE(_xGrantUser.is(),"OTableGrantControl::setGrantUser: GrantUser is null!");
409 m_xGrantUser = _xGrantUser;
411 //------------------------------------------------------------------------------
412 CellController* OTableGrantControl::GetController( long nRow, USHORT nColumnId )
414 DBG_CHKTHIS(OTableGrantControl,NULL);
416 CellController* pController = NULL;
417 switch( nColumnId )
419 case COL_TABLE_NAME:
420 break;
421 case COL_INSERT:
422 case COL_DELETE:
423 case COL_UPDATE:
424 case COL_ALTER:
425 case COL_SELECT:
426 case COL_REF:
427 case COL_DROP:
429 TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
430 if(aFind != m_aPrivMap.end() && isAllowed(nColumnId,aFind->second.nWithGrant))
431 pController = new CheckBoxCellController( m_pCheckCell );
433 break;
434 default:
437 return pController;
439 //------------------------------------------------------------------------------
440 BOOL OTableGrantControl::SeekRow( long nRow )
442 DBG_CHKTHIS(OTableGrantControl,NULL);
443 m_nDataPos = nRow;
445 return (nRow <= m_aTableNames.getLength());
448 //------------------------------------------------------------------------------
449 void OTableGrantControl::PaintCell( OutputDevice& rDev, const Rectangle& rRect, USHORT nColumnId ) const
451 DBG_CHKTHIS(OTableGrantControl,NULL);
453 if(nColumnId != COL_TABLE_NAME)
455 TTablePrivilegeMap::const_iterator aFind = findPrivilege(m_nDataPos);
456 if(aFind != m_aPrivMap.end())
457 PaintTristate(rDev, rRect, isAllowed(nColumnId,aFind->second.nRights) ? STATE_CHECK : STATE_NOCHECK,isAllowed(nColumnId,aFind->second.nWithGrant));
458 else
459 PaintTristate(rDev, rRect, STATE_NOCHECK,FALSE);
461 else
463 String aText(((OTableGrantControl*)this)->GetCellText( m_nDataPos, nColumnId ));
464 Point aPos( rRect.TopLeft() );
465 sal_Int32 nWidth = GetDataWindow().GetTextWidth( aText );
466 sal_Int32 nHeight = GetDataWindow().GetTextHeight();
468 if( aPos.X() < rRect.Right() || aPos.X() + nWidth > rRect.Right() ||
469 aPos.Y() < rRect.Top() || aPos.Y() + nHeight > rRect.Bottom() )
470 rDev.SetClipRegion( rRect );
472 rDev.DrawText( aPos, aText );
475 if( rDev.IsClipRegion() )
476 rDev.SetClipRegion();
479 //------------------------------------------------------------------------
480 void OTableGrantControl::CellModified()
482 DBG_CHKTHIS(OTableGrantControl,NULL);
483 EditBrowseBox::CellModified();
484 SaveModified();
486 // -----------------------------------------------------------------------------
487 OTableGrantControl::TTablePrivilegeMap::const_iterator OTableGrantControl::findPrivilege(sal_Int32 _nRow) const
489 TTablePrivilegeMap::const_iterator aFind = m_aPrivMap.find(m_aTableNames[_nRow]);
490 if(aFind == m_aPrivMap.end())
492 fillPrivilege(_nRow);
493 aFind = m_aPrivMap.find(m_aTableNames[_nRow]);
495 return aFind;
497 // -----------------------------------------------------------------------------
498 Reference< XAccessible > OTableGrantControl::CreateAccessibleCell( sal_Int32 _nRow, sal_uInt16 _nColumnPos )
500 USHORT nColumnId = GetColumnId( _nColumnPos );
501 if(nColumnId != COL_TABLE_NAME)
503 TriState eState = STATE_NOCHECK;
504 BOOL bEnable = FALSE;
505 TTablePrivilegeMap::const_iterator aFind = findPrivilege(_nRow);
506 if(aFind != m_aPrivMap.end())
508 eState = isAllowed(nColumnId,aFind->second.nRights) ? STATE_CHECK : STATE_NOCHECK;
509 bEnable = isAllowed(nColumnId,aFind->second.nWithGrant);
511 else
512 eState = STATE_NOCHECK;
514 return EditBrowseBox::CreateAccessibleCheckBoxCell( _nRow, _nColumnPos,eState,bEnable );
516 return EditBrowseBox::CreateAccessibleCell( _nRow, _nColumnPos );
518 // -----------------------------------------------------------------------------