tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / dbaccess / source / ui / control / TableGrantCtrl.cxx
blob607c0199729a4dfd7331510efc8e4382ec139cb1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <TableGrantCtrl.hxx>
21 #include <core_resource.hxx>
22 #include <com/sun/star/sdbc/SQLException.hpp>
23 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
24 #include <com/sun/star/sdbcx/Privilege.hpp>
25 #include <com/sun/star/sdbcx/PrivilegeObject.hpp>
26 #include <com/sun/star/sdbcx/XUsersSupplier.hpp>
27 #include <com/sun/star/sdbcx/XAuthorizable.hpp>
28 #include <connectivity/dbtools.hxx>
29 #include <toolkit/helper/vclunohelper.hxx>
30 #include <vcl/svapp.hxx>
31 #include <osl/diagnose.h>
32 #include <strings.hrc>
34 using namespace ::com::sun::star::accessibility;
35 using namespace ::com::sun::star::container;
36 using namespace ::com::sun::star::sdbcx;
37 using namespace ::com::sun::star::sdbc;
38 using namespace ::com::sun::star::uno;
39 using namespace ::dbaui;
40 using namespace ::svt;
42 const sal_uInt16 COL_TABLE_NAME = 1;
43 const sal_uInt16 COL_SELECT = 2;
44 const sal_uInt16 COL_INSERT = 3;
45 const sal_uInt16 COL_DELETE = 4;
46 const sal_uInt16 COL_UPDATE = 5;
47 const sal_uInt16 COL_ALTER = 6;
48 const sal_uInt16 COL_REF = 7;
49 const sal_uInt16 COL_DROP = 8;
52 // OTableGrantControl
53 OTableGrantControl::OTableGrantControl(const css::uno::Reference<css::awt::XWindow> &rParent)
54 :EditBrowseBox(VCLUnoHelper::GetWindow(rParent), EditBrowseBoxFlags::SMART_TAB_TRAVEL | EditBrowseBoxFlags::NO_HANDLE_COLUMN_CONTENT, WB_TABSTOP)
55 ,m_pCheckCell( nullptr )
56 ,m_pEdit( nullptr )
57 ,m_nDataPos( 0 )
58 ,m_nDeactivateEvent(nullptr)
60 // insert columns
61 sal_uInt16 i=1;
62 InsertDataColumn( i, DBA_RES(STR_TABLE_PRIV_NAME), 75);
63 FreezeColumn(i++);
64 InsertDataColumn( i++, DBA_RES(STR_TABLE_PRIV_SELECT), 75);
65 InsertDataColumn( i++, DBA_RES(STR_TABLE_PRIV_INSERT), 75);
66 InsertDataColumn( i++, DBA_RES(STR_TABLE_PRIV_DELETE), 75);
67 InsertDataColumn( i++, DBA_RES(STR_TABLE_PRIV_UPDATE), 75);
68 InsertDataColumn( i++, DBA_RES(STR_TABLE_PRIV_ALTER), 75);
69 InsertDataColumn( i++, DBA_RES(STR_TABLE_PRIV_REFERENCE), 75);
70 InsertDataColumn( i++, DBA_RES(STR_TABLE_PRIV_DROP), 75);
72 while(--i)
73 SetColumnWidth(i,GetAutoColumnWidth(i));
76 OTableGrantControl::~OTableGrantControl()
78 disposeOnce();
81 void OTableGrantControl::dispose()
83 if (m_nDeactivateEvent)
85 Application::RemoveUserEvent(m_nDeactivateEvent);
86 m_nDeactivateEvent = nullptr;
89 m_pCheckCell.disposeAndClear();
90 m_pEdit.disposeAndClear();
92 m_xTables = nullptr;
93 ::svt::EditBrowseBox::dispose();
96 void OTableGrantControl::setTablesSupplier(const Reference< XTablesSupplier >& _xTablesSup)
98 // first we need the users
99 Reference< XUsersSupplier> xUserSup(_xTablesSup,UNO_QUERY);
100 if(xUserSup.is())
101 m_xUsers = xUserSup->getUsers();
103 // second we need the tables to determine which privileges the user has
104 if(_xTablesSup.is())
105 m_xTables = _xTablesSup->getTables();
107 if(m_xTables.is())
108 m_aTableNames = m_xTables->getElementNames();
110 OSL_ENSURE(m_xUsers.is(),"No user access supported!");
111 OSL_ENSURE(m_xTables.is(),"No tables supported!");
114 void OTableGrantControl::setComponentContext(const Reference< css::uno::XComponentContext>& _rxContext)
116 m_xContext = _rxContext;
119 void OTableGrantControl::UpdateTables()
121 RemoveRows();
123 if(m_xTables.is())
124 RowInserted(0, m_aTableNames.getLength());
125 // m_bEnable = m_xDb->GetUser() != ((OUserAdmin*)GetParent())->GetUser();
128 void OTableGrantControl::Init()
130 EditBrowseBox::Init();
132 // instantiate ComboBox
133 if(!m_pCheckCell)
135 m_pCheckCell = VclPtr<CheckBoxControl>::Create( &GetDataWindow() );
136 m_pCheckCell->EnableTriState(false);
138 m_pEdit = VclPtr<EditControl>::Create(&GetDataWindow());
139 weld::Entry& rEntry = m_pEdit->get_widget();
140 rEntry.set_editable(false);
141 rEntry.set_sensitive(false);
144 UpdateTables();
145 // set browser mode
146 BrowserMode const nMode = BrowserMode::COLUMNSELECTION | BrowserMode::HLINES | BrowserMode::VLINES |
147 BrowserMode::HIDECURSOR | BrowserMode::HIDESELECT;
149 SetMode(nMode);
152 bool OTableGrantControl::PreNotify(NotifyEvent& rNEvt)
154 if (rNEvt.GetType() == NotifyEventType::LOSEFOCUS)
155 if (!HasChildPathFocus())
157 if (m_nDeactivateEvent)
158 Application::RemoveUserEvent(m_nDeactivateEvent);
159 m_nDeactivateEvent = Application::PostUserEvent(LINK(this, OTableGrantControl, AsynchDeactivate), nullptr, true);
161 if (rNEvt.GetType() == NotifyEventType::GETFOCUS)
163 if (m_nDeactivateEvent)
164 Application::RemoveUserEvent(m_nDeactivateEvent);
165 m_nDeactivateEvent = Application::PostUserEvent(LINK(this, OTableGrantControl, AsynchActivate), nullptr, true);
167 return EditBrowseBox::PreNotify(rNEvt);
170 IMPL_LINK_NOARG(OTableGrantControl, AsynchActivate, void*, void)
172 m_nDeactivateEvent = nullptr;
173 ActivateCell();
176 IMPL_LINK_NOARG(OTableGrantControl, AsynchDeactivate, void*, void)
178 m_nDeactivateEvent = nullptr;
179 DeactivateCell();
182 bool OTableGrantControl::IsTabAllowed(bool bForward) const
184 sal_Int32 nRow = GetCurRow();
185 sal_uInt16 nCol = GetCurColumnId();
187 if (bForward && (nCol == 2) && (nRow == GetRowCount() - 1))
188 return false;
190 if (!bForward && (nCol == 1) && (nRow == 0))
191 return false;
193 return EditBrowseBox::IsTabAllowed(bForward);
196 bool OTableGrantControl::SaveModified()
199 sal_Int32 nRow = GetCurRow();
200 if(nRow == -1 || nRow >= m_aTableNames.getLength())
201 return false;
203 OUString sTableName = m_aTableNames[nRow];
204 bool bErg = true;
208 if ( m_xUsers->hasByName(m_sUserName) )
210 Reference<XAuthorizable> xAuth(m_xUsers->getByName(m_sUserName),UNO_QUERY);
211 if ( xAuth.is() )
213 switch( GetCurColumnId() )
215 case COL_INSERT:
216 if (m_pCheckCell->GetBox().get_active())
217 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,Privilege::INSERT);
218 else
219 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,Privilege::INSERT);
220 break;
221 case COL_DELETE:
222 if (m_pCheckCell->GetBox().get_active())
223 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,Privilege::DELETE);
224 else
225 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,Privilege::DELETE);
226 break;
227 case COL_UPDATE:
228 if (m_pCheckCell->GetBox().get_active())
229 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,Privilege::UPDATE);
230 else
231 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,Privilege::UPDATE);
232 break;
233 case COL_ALTER:
234 if (m_pCheckCell->GetBox().get_active())
235 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,Privilege::ALTER);
236 else
237 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,Privilege::ALTER);
238 break;
239 case COL_SELECT:
240 if (m_pCheckCell->GetBox().get_active())
241 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,Privilege::SELECT);
242 else
243 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,Privilege::SELECT);
244 break;
245 case COL_REF:
246 if (m_pCheckCell->GetBox().get_active())
247 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,Privilege::REFERENCE);
248 else
249 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,Privilege::REFERENCE);
250 break;
251 case COL_DROP:
252 if (m_pCheckCell->GetBox().get_active())
253 xAuth->grantPrivileges(sTableName,PrivilegeObject::TABLE,Privilege::DROP);
254 else
255 xAuth->revokePrivileges(sTableName,PrivilegeObject::TABLE,Privilege::DROP);
256 break;
258 fillPrivilege(nRow);
262 catch(SQLException& e)
264 bErg = false;
265 ::dbtools::showError(::dbtools::SQLExceptionInfo(e),VCLUnoHelper::GetInterface(GetParent()),m_xContext);
267 if(bErg && Controller().is())
268 Controller()->SaveValue();
269 if(!bErg)
270 UpdateTables();
272 return bErg;
275 OUString OTableGrantControl::GetCellText( sal_Int32 nRow, sal_uInt16 nColId ) const
277 if(COL_TABLE_NAME == nColId)
278 return m_aTableNames[nRow];
280 sal_Int32 nPriv = 0;
281 TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
282 if(aFind != m_aPrivMap.end())
283 nPriv = aFind->second.nRights;
285 return OUString::number(isAllowed(nColId,nPriv) ? 1 :0);
288 void OTableGrantControl::InitController( CellControllerRef& /*rController*/, sal_Int32 nRow, sal_uInt16 nColumnId )
290 OUString sTablename = m_aTableNames[nRow];
291 // special case for tablename
292 if (nColumnId == COL_TABLE_NAME)
293 m_pEdit->get_widget().set_text(sTablename);
294 else
296 // get the privileges from the user
297 TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
298 m_pCheckCell->GetBox().set_active(aFind != m_aPrivMap.end() && isAllowed(nColumnId,aFind->second.nRights));
302 void OTableGrantControl::fillPrivilege(sal_Int32 _nRow) const
305 if ( !m_xUsers->hasByName(m_sUserName) )
306 return;
310 Reference<XAuthorizable> xAuth(m_xUsers->getByName(m_sUserName),UNO_QUERY);
311 if ( xAuth.is() )
313 // get the privileges
314 TPrivileges nRights;
315 nRights.nRights = xAuth->getPrivileges(m_aTableNames[_nRow],PrivilegeObject::TABLE);
316 if(m_xGrantUser.is())
317 nRights.nWithGrant = m_xGrantUser->getGrantablePrivileges(m_aTableNames[_nRow],PrivilegeObject::TABLE);
318 else
319 nRights.nWithGrant = 0;
321 m_aPrivMap[m_aTableNames[_nRow]] = nRights;
324 catch(SQLException& e)
326 ::dbtools::showError(::dbtools::SQLExceptionInfo(e),VCLUnoHelper::GetInterface(GetParent()),m_xContext);
328 catch(Exception& )
333 bool OTableGrantControl::isAllowed(sal_uInt16 _nColumnId,sal_Int32 _nPrivilege)
335 bool bAllowed = false;
336 switch (_nColumnId)
338 case COL_INSERT:
339 bAllowed = (Privilege::INSERT & _nPrivilege) == Privilege::INSERT;
340 break;
341 case COL_DELETE:
342 bAllowed = (Privilege::DELETE & _nPrivilege) == Privilege::DELETE;
343 break;
344 case COL_UPDATE:
345 bAllowed = (Privilege::UPDATE & _nPrivilege) == Privilege::UPDATE;
346 break;
347 case COL_ALTER:
348 bAllowed = (Privilege::ALTER & _nPrivilege) == Privilege::ALTER;
349 break;
350 case COL_SELECT:
351 bAllowed = (Privilege::SELECT & _nPrivilege) == Privilege::SELECT;
352 break;
353 case COL_REF:
354 bAllowed = (Privilege::REFERENCE & _nPrivilege) == Privilege::REFERENCE;
355 break;
356 case COL_DROP:
357 bAllowed = (Privilege::DROP & _nPrivilege) == Privilege::DROP;
358 break;
360 return bAllowed;
363 void OTableGrantControl::setUserName(const OUString& _sUserName)
365 m_sUserName = _sUserName;
366 m_aPrivMap = TTablePrivilegeMap();
369 void OTableGrantControl::setGrantUser(const Reference< XAuthorizable>& _xGrantUser)
371 OSL_ENSURE(_xGrantUser.is(),"OTableGrantControl::setGrantUser: GrantUser is null!");
372 m_xGrantUser = _xGrantUser;
375 CellController* OTableGrantControl::GetController( sal_Int32 nRow, sal_uInt16 nColumnId )
378 CellController* pController = nullptr;
379 switch( nColumnId )
381 case COL_TABLE_NAME:
382 break;
383 case COL_INSERT:
384 case COL_DELETE:
385 case COL_UPDATE:
386 case COL_ALTER:
387 case COL_SELECT:
388 case COL_REF:
389 case COL_DROP:
391 TTablePrivilegeMap::const_iterator aFind = findPrivilege(nRow);
392 if(aFind != m_aPrivMap.end() && isAllowed(nColumnId,aFind->second.nWithGrant))
393 pController = new CheckBoxCellController( m_pCheckCell );
395 break;
396 default:
399 return pController;
402 bool OTableGrantControl::SeekRow( sal_Int32 nRow )
404 m_nDataPos = nRow;
406 return (nRow <= m_aTableNames.getLength());
409 void OTableGrantControl::PaintCell( OutputDevice& rDev, const tools::Rectangle& rRect, sal_uInt16 nColumnId ) const
412 if(nColumnId != COL_TABLE_NAME)
414 TTablePrivilegeMap::const_iterator aFind = findPrivilege(m_nDataPos);
415 if(aFind != m_aPrivMap.end())
416 PaintTristate(rRect, isAllowed(nColumnId,aFind->second.nRights) ? TRISTATE_TRUE : TRISTATE_FALSE,isAllowed(nColumnId,aFind->second.nWithGrant));
417 else
418 PaintTristate(rRect, TRISTATE_FALSE, false);
420 else
422 OUString aText(GetCellText( m_nDataPos, nColumnId ));
423 Point aPos( rRect.TopLeft() );
424 sal_Int32 nWidth = GetDataWindow().GetTextWidth( aText );
425 sal_Int32 nHeight = GetDataWindow().GetTextHeight();
427 if( aPos.X() < rRect.Left() || aPos.X() + nWidth > rRect.Right() ||
428 aPos.Y() < rRect.Top() || aPos.Y() + nHeight > rRect.Bottom() )
430 rDev.SetClipRegion(vcl::Region(rRect));
433 rDev.DrawText( aPos, aText );
436 if( rDev.IsClipRegion() )
437 rDev.SetClipRegion();
440 void OTableGrantControl::CellModified()
442 EditBrowseBox::CellModified();
443 SaveModified();
446 OTableGrantControl::TTablePrivilegeMap::const_iterator OTableGrantControl::findPrivilege(sal_Int32 _nRow) const
448 TTablePrivilegeMap::const_iterator aFind = m_aPrivMap.find(m_aTableNames[_nRow]);
449 if(aFind == m_aPrivMap.end())
451 fillPrivilege(_nRow);
452 aFind = m_aPrivMap.find(m_aTableNames[_nRow]);
454 return aFind;
457 Reference< XAccessible > OTableGrantControl::CreateAccessibleCell( sal_Int32 _nRow, sal_uInt16 _nColumnPos )
459 sal_uInt16 nColumnId = GetColumnId( _nColumnPos );
460 if(nColumnId != COL_TABLE_NAME)
462 TriState eState = TRISTATE_FALSE;
463 TTablePrivilegeMap::const_iterator aFind = findPrivilege(_nRow);
464 if(aFind != m_aPrivMap.end())
466 eState = isAllowed(nColumnId,aFind->second.nRights) ? TRISTATE_TRUE : TRISTATE_FALSE;
468 else
469 eState = TRISTATE_FALSE;
471 return EditBrowseBox::CreateAccessibleCheckBoxCell( _nRow, _nColumnPos,eState );
473 return EditBrowseBox::CreateAccessibleCell( _nRow, _nColumnPos );
476 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */