tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / dbaccess / source / ui / relationdesign / RelationTableView.cxx
blobcaa4e65346184a3995acc5ca5c80df4abf7fdb71
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 <RelationTableView.hxx>
21 #include <core_resource.hxx>
22 #include <browserids.hxx>
23 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
24 #include <com/sun/star/sdbc/XConnection.hpp>
25 #include <com/sun/star/sdbc/SQLException.hpp>
26 #include <com/sun/star/container/XContainer.hpp>
27 #include <com/sun/star/container/XIndexAccess.hpp>
28 #include <com/sun/star/container/XNameAccess.hpp>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <connectivity/dbtools.hxx>
31 #include <comphelper/diagnose_ex.hxx>
32 #include <helpids.h>
33 #include <RelationDesignView.hxx>
34 #include <JoinController.hxx>
35 #include <TableWindow.hxx>
36 #include <TableWindowData.hxx>
37 #include "RTableConnection.hxx"
38 #include <RTableConnectionData.hxx>
39 #include <RelationDlg.hxx>
40 #include <sqlmessage.hxx>
41 #include <strings.hrc>
42 #include <connectivity/dbexception.hxx>
43 #include "RTableWindow.hxx"
44 #include <JAccess.hxx>
45 #include <vcl/stdtext.hxx>
46 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
48 using namespace dbaui;
49 using namespace ::dbtools;
50 using namespace ::com::sun::star;
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::sdbc;
53 using namespace ::com::sun::star::sdbcx;
54 using namespace ::com::sun::star::beans;
55 using namespace ::com::sun::star::container;
56 using namespace ::com::sun::star::accessibility;
58 ORelationTableView::ORelationTableView( vcl::Window* pParent, ORelationDesignView* pView )
59 :OJoinTableView( pParent, pView )
60 , ::comphelper::OContainerListener(m_aMutex)
61 ,m_pExistingConnection(nullptr)
62 ,m_bInRemove(false)
65 SetHelpId(HID_CTL_RELATIONTAB);
68 ORelationTableView::~ORelationTableView()
70 disposeOnce();
73 void ORelationTableView::dispose()
75 if ( m_pContainerListener.is() )
76 m_pContainerListener->dispose();
77 m_pExistingConnection.clear();
78 OJoinTableView::dispose();
81 void ORelationTableView::ReSync()
83 if ( !m_pContainerListener.is() )
85 Reference< XConnection> xConnection = m_pView->getController().getConnection();
86 Reference< XTablesSupplier > xTableSupp( xConnection, UNO_QUERY_THROW );
87 Reference< XNameAccess > xTables = xTableSupp->getTables();
88 Reference< XContainer> xContainer(xTables,uno::UNO_QUERY);
89 if ( xContainer.is() )
90 m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
92 // Tables could have been hidden in the database, which are part of a relation. Or a table was in layout
93 // (quite often without a relation) and does not exist anymore. In both cases creation of TabWins will fail
94 // and all TabWinDatas and related ConnDates should be deleted.
95 std::vector< OUString> arrInvalidTables;
97 // create and insert windows
98 TTableWindowData& rTabWinDataList = m_pView->getController().getTableWindowData();
99 TTableWindowData::const_reverse_iterator aIter = rTabWinDataList.rbegin();
100 for(;aIter != rTabWinDataList.rend();++aIter)
102 const TTableWindowData::value_type& pData = *aIter;
103 VclPtr<OTableWindow> pTabWin = createWindow(pData);
105 if (!pTabWin->Init())
107 // initialisation failed, which means this TabWin is not available, therefore,
108 // it should be cleaned up, including its data in the document
109 pTabWin->clearListBox();
110 pTabWin.disposeAndClear();
111 // get the full name of the tables to ensure uniqueness across catalogs and schema
112 arrInvalidTables.push_back(pData->GetComposedName());
114 std::erase(rTabWinDataList, *aIter);
115 continue;
118 GetTabWinMap()[pData->GetComposedName()] = pTabWin; // insert at the beginning, as the Datalist is walked through backward
119 // if there's no position or size contained in the data -> Default
120 if (!pData->HasPosition() && !pData->HasSize())
121 SetDefaultTabWinPosSize(pTabWin);
123 pTabWin->Show();
126 // insert connection
127 TTableConnectionData& rTabConnDataList = m_pView->getController().getTableConnectionData();
128 TTableConnectionData::const_reverse_iterator aConIter = rTabConnDataList.rbegin();
130 for(;aConIter != rTabConnDataList.rend();++aConIter)
132 ORelationTableConnectionData* pTabConnData = static_cast<ORelationTableConnectionData*>(aConIter->get());
133 if ( !arrInvalidTables.empty() )
135 // do the tables to the connection exist?
136 OUString strTabExistenceTest = pTabConnData->getReferencingTable()->GetComposedName();
137 bool bInvalid = std::find(arrInvalidTables.begin(),arrInvalidTables.end(),strTabExistenceTest) != arrInvalidTables.end();
138 strTabExistenceTest = pTabConnData->getReferencedTable()->GetComposedName();
139 bInvalid = bInvalid || std::find(arrInvalidTables.begin(),arrInvalidTables.end(),strTabExistenceTest) != arrInvalidTables.end();
141 if (bInvalid)
143 // no -> bad luck, the connection is gone
144 std::erase(rTabConnDataList, *aConIter);
145 continue;
149 addConnection( VclPtr<ORelationTableConnection>::Create(this, *aConIter), false );
152 if ( !GetTabWinMap().empty() )
153 GetTabWinMap().begin()->second->GrabFocus();
156 bool ORelationTableView::IsAddAllowed()
159 return !m_pView->getController().isReadOnly();
162 void ORelationTableView::AddConnection(const OJoinExchangeData& jxdSource, const OJoinExchangeData& jxdDest)
164 // Set LineDataObject based on selected fieldname
165 // check if relation already exists
166 OTableWindow* pSourceWin = jxdSource.pListBox->GetTabWin();
167 OTableWindow* pDestWin = jxdDest.pListBox->GetTabWin();
169 for(VclPtr<OTableConnection> const & pFirst : getTableConnections())
171 if((pFirst->GetSourceWin() == pSourceWin && pFirst->GetDestWin() == pDestWin) ||
172 (pFirst->GetSourceWin() == pDestWin && pFirst->GetDestWin() == pSourceWin))
174 m_pExistingConnection = pFirst;
175 break;
178 // insert table connection into view
179 TTableConnectionData::value_type pTabConnData = std::make_shared<ORelationTableConnectionData>(pSourceWin->GetData(),
180 pDestWin->GetData());
182 // the names of the affected fields
183 weld::TreeView& rSourceTreeView = jxdSource.pListBox->get_widget();
184 OUString sSourceFieldName = rSourceTreeView.get_text(jxdSource.nEntry);
185 weld::TreeView& rDestTreeView = jxdDest.pListBox->get_widget();
186 OUString sDestFieldName = rDestTreeView.get_text(jxdDest.nEntry);
188 // the number of PKey-Fields in the source
189 const Reference< XNameAccess> xPrimaryKeyColumns = getPrimaryKeyColumns_throw(pSourceWin->GetData()->getTable());
190 bool bAskUser = xPrimaryKeyColumns.is() && Reference< XIndexAccess>(xPrimaryKeyColumns,UNO_QUERY_THROW)->getCount() > 1;
192 pTabConnData->SetConnLine( 0, sSourceFieldName, sDestFieldName );
194 if ( bAskUser || m_pExistingConnection )
195 m_pCurrentlyTabConnData = std::move(pTabConnData); // this implies that we ask the user what to do
196 else
200 // hand over data to the database
201 if( pTabConnData->Update() )
203 // enter UI-object into ConnList
204 addConnection( VclPtr<ORelationTableConnection>::Create( this, pTabConnData ) );
207 catch(const SQLException&)
209 throw;
211 catch(const Exception&)
213 TOOLS_WARN_EXCEPTION( "dbaccess", "ORelationTableView::AddConnection");
218 void ORelationTableView::ConnDoubleClicked(VclPtr<OTableConnection>& rConnection)
220 ORelationDialog aRelDlg(this, rConnection->GetData());
221 switch (aRelDlg.run())
223 case RET_OK:
224 // successfully updated
225 rConnection->UpdateLineList();
226 // The connection references 1 ConnData and n ConnLines, each ConnData references n LineDatas, each Line exactly 1 LineData
227 // As the Dialog and the ConnData->Update may have changed the LineDatas we have to restore the consistent state
228 break;
230 case RET_NO:
231 // tried at least one update, but did not succeed -> the original connection is lost
232 RemoveConnection(rConnection ,true);
233 break;
235 case RET_CANCEL:
236 // no break, as nothing happened and we don't need the code below
237 return;
241 Invalidate(InvalidateFlags::NoChildren);
244 void ORelationTableView::AddNewRelation()
247 TTableConnectionData::value_type pNewConnData = std::make_shared<ORelationTableConnectionData>();
248 ORelationDialog aRelDlg(this, pNewConnData, true);
250 bool bSuccess = (aRelDlg.run() == RET_OK);
251 if (bSuccess)
253 // already updated by the dialog
254 // announce it to the document
255 addConnection( VclPtr<ORelationTableConnection>::Create(this, pNewConnData) );
259 bool ORelationTableView::RemoveConnection(VclPtr<OTableConnection>& rConn, bool /*_bDelete*/)
261 ORelationTableConnectionData* pTabConnData = static_cast<ORelationTableConnectionData*>(rConn->GetData().get());
264 if (!m_bInRemove)
265 pTabConnData->DropRelation();
266 return OJoinTableView::RemoveConnection(rConn, true);
268 catch(SQLException& e)
270 getDesignView()->getController().showError(SQLExceptionInfo(e));
272 catch(Exception&)
274 TOOLS_WARN_EXCEPTION( "dbaccess", "ORelationTableView::RemoveConnection: Something other than SQLException occurred!");
276 return false;
279 void ORelationTableView::AddTabWin(const OUString& _rComposedName, const OUString& rWinName, bool /*bNewTable*/)
281 OSL_ENSURE(!_rComposedName.isEmpty(),"There must be a table name supplied!");
282 OJoinTableView::OTableWindowMap::const_iterator aIter = GetTabWinMap().find(_rComposedName);
284 if(aIter != GetTabWinMap().end())
286 aIter->second->SetZOrder(nullptr, ZOrderFlags::First);
287 aIter->second->GrabFocus();
288 EnsureVisible(aIter->second);
289 // no new one
290 return;
293 // enter the new data structure into DocShell
294 // show the table's full name as window name to ensure uniqueness across catalogs and schema
295 TTableWindowData::value_type pNewTabWinData(createTableWindowData( _rComposedName, rWinName, _rComposedName ));
296 pNewTabWinData->ShowAll(false);
298 // link new window into the window list
299 VclPtr<OTableWindow> pNewTabWin = createWindow( pNewTabWinData );
300 if(pNewTabWin->Init())
302 m_pView->getController().getTableWindowData().push_back( pNewTabWinData);
303 // when we already have a table with this name insert the full qualified one instead
304 GetTabWinMap()[_rComposedName] = pNewTabWin;
306 SetDefaultTabWinPosSize( pNewTabWin );
307 pNewTabWin->Show();
309 modified();
311 if ( m_pAccessible )
312 m_pAccessible->notifyAccessibleEvent( AccessibleEventId::CHILD,
313 Any(),
314 Any(pNewTabWin->GetAccessible()));
316 else
318 pNewTabWin->clearListBox();
319 pNewTabWin.disposeAndClear();
323 void ORelationTableView::RemoveTabWin( OTableWindow* pTabWin )
325 OSQLWarningBox aDlg(GetFrameWeld(), DBA_RES(STR_QUERY_REL_DELETE_WINDOW), MessBoxStyle::YesNo | MessBoxStyle::DefaultYes);
326 if (m_bInRemove || aDlg.run() == RET_YES)
328 m_pView->getController().ClearUndoManager();
329 OJoinTableView::RemoveTabWin( pTabWin );
331 m_pView->getController().InvalidateFeature(SID_RELATION_ADD_RELATION);
332 m_pView->getController().InvalidateFeature(ID_BROWSER_UNDO);
333 m_pView->getController().InvalidateFeature(ID_BROWSER_REDO);
337 void ORelationTableView::lookForUiActivities()
339 if(m_pExistingConnection)
341 OUString sTitle(DBA_RES(STR_RELATIONDESIGN));
342 sTitle = sTitle.copy(3);
343 OSQLMessageBox aDlg(GetFrameWeld(), DBA_RES(STR_QUERY_REL_EDIT_RELATION), OUString(), MessBoxStyle::NONE);
344 aDlg.set_title(sTitle);
345 aDlg.add_button(DBA_RES(STR_QUERY_REL_EDIT), RET_OK);
346 aDlg.set_default_response(RET_OK);
347 aDlg.add_button(DBA_RES(STR_QUERY_REL_CREATE), RET_YES);
348 aDlg.add_button(GetStandardText(StandardButtonType::Cancel), RET_CANCEL);
349 sal_uInt16 nRet = aDlg.run();
350 if (nRet == RET_CANCEL)
352 m_pCurrentlyTabConnData.reset();
354 else if ( nRet == RET_OK ) // EDIT
356 ConnDoubleClicked(m_pExistingConnection);
357 m_pCurrentlyTabConnData.reset();
359 m_pExistingConnection = nullptr;
361 if(m_pCurrentlyTabConnData)
363 ORelationDialog aRelDlg(this, m_pCurrentlyTabConnData);
364 if (aRelDlg.run() == RET_OK)
366 // already updated by the dialog
367 addConnection( VclPtr<ORelationTableConnection>::Create( this, m_pCurrentlyTabConnData ) );
369 m_pCurrentlyTabConnData.reset();
373 VclPtr<OTableWindow> ORelationTableView::createWindow(const TTableWindowData::value_type& _pData)
375 return VclPtr<ORelationTableWindow>::Create(this,_pData);
378 bool ORelationTableView::allowQueries() const
380 return false;
383 void ORelationTableView::_elementInserted( const container::ContainerEvent& /*_rEvent*/ )
388 void ORelationTableView::_elementRemoved( const container::ContainerEvent& _rEvent )
390 m_bInRemove = true;
391 OUString sName;
392 if ( _rEvent.Accessor >>= sName )
394 OTableWindow* pTableWindow = GetTabWindow(sName);
395 if ( pTableWindow )
397 m_pView->getController().ClearUndoManager();
398 OJoinTableView::RemoveTabWin( pTableWindow );
400 m_pView->getController().InvalidateFeature(SID_RELATION_ADD_RELATION);
401 m_pView->getController().InvalidateFeature(ID_BROWSER_UNDO);
402 m_pView->getController().InvalidateFeature(ID_BROWSER_REDO);
405 m_bInRemove = false;
408 void ORelationTableView::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ )
412 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */