Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / ui / querydesign / JoinController.cxx
blob3852ef9d33229a83b4f9d1e7b744a14cd8e7da77
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 <sfx2/sfxsids.hrc>
21 #include "dbu_qry.hrc"
22 #include "browserids.hxx"
23 #include <comphelper/types.hxx>
24 #include "dbustrings.hrc"
25 #include <connectivity/dbtools.hxx>
26 #include <com/sun/star/frame/FrameSearchFlag.hpp>
27 #include <comphelper/extract.hxx>
28 #include <com/sun/star/container/XChild.hpp>
29 #include <com/sun/star/container/XNameContainer.hpp>
30 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
31 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
32 #include <com/sun/star/sdbcx/KeyType.hpp>
33 #include <com/sun/star/sdbcx/XDrop.hpp>
34 #include <com/sun/star/sdbcx/XAlterTable.hpp>
35 #include <com/sun/star/sdbcx/XAppend.hpp>
36 #include <com/sun/star/sdb/SQLContext.hpp>
37 #include <com/sun/star/sdbc/SQLWarning.hpp>
38 #include <com/sun/star/sdbc/ColumnValue.hpp>
39 #include <com/sun/star/sdbc/XRow.hpp>
40 #include <connectivity/dbexception.hxx>
41 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
42 #include <comphelper/streamsection.hxx>
43 #include <comphelper/seqstream.hxx>
44 #include <com/sun/star/io/XActiveDataSource.hpp>
45 #include <com/sun/star/io/XActiveDataSink.hpp>
46 #include "sqlmessage.hxx"
47 #include "JoinController.hxx"
48 #include <vcl/msgbox.hxx>
49 #include "TableWindowData.hxx"
50 #include "TableWindow.hxx"
51 #include "TableConnectionData.hxx"
52 #include "adtabdlg.hxx"
53 #include <vcl/waitobj.hxx>
54 #include <vcl/svapp.hxx>
55 #include <osl/mutex.hxx>
56 #include "UITools.hxx"
57 #include <osl/diagnose.h>
59 #include <boost/optional.hpp>
60 #include <boost/scoped_ptr.hpp>
62 using namespace ::com::sun::star::uno;
63 using namespace ::com::sun::star::io;
64 using namespace ::com::sun::star::beans;
65 using namespace ::com::sun::star::frame;
66 using namespace ::com::sun::star::util;
67 using namespace ::com::sun::star::lang;
68 using namespace ::com::sun::star::container;
69 using namespace ::com::sun::star::sdbcx;
70 using namespace ::com::sun::star::sdbc;
71 using namespace ::com::sun::star::sdb;
72 using namespace ::com::sun::star::ui::dialogs;
73 using namespace ::dbtools;
74 using namespace ::comphelper;
76 namespace dbaui
79 // AddTableDialogContext
80 class AddTableDialogContext : public IAddTableDialogContext
82 OJoinController& m_rController;
84 public:
85 AddTableDialogContext( OJoinController& _rController )
86 :m_rController( _rController )
90 virtual ~AddTableDialogContext() {}
92 // IAddTableDialogContext
93 virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >
94 getConnection() const SAL_OVERRIDE;
95 virtual bool allowViews() const SAL_OVERRIDE;
96 virtual bool allowQueries() const SAL_OVERRIDE;
97 virtual bool allowAddition() const SAL_OVERRIDE;
98 virtual void addTableWindow( const OUString& _rQualifiedTableName, const OUString& _rAliasName ) SAL_OVERRIDE;
99 virtual void onWindowClosing( const vcl::Window* _pWindow ) SAL_OVERRIDE;
101 private:
102 OJoinTableView* getTableView() const;
105 Reference< XConnection > AddTableDialogContext::getConnection() const
107 return m_rController.getConnection();
110 bool AddTableDialogContext::allowViews() const
112 return m_rController.allowViews();
115 bool AddTableDialogContext::allowQueries() const
117 return m_rController.allowQueries();
120 bool AddTableDialogContext::allowAddition() const
122 return const_cast< OJoinController& >( m_rController ).getJoinView()->getTableView()->IsAddAllowed();
125 void AddTableDialogContext::addTableWindow( const OUString& _rQualifiedTableName, const OUString& _rAliasName )
127 getTableView()->AddTabWin( _rQualifiedTableName, _rAliasName, true );
130 void AddTableDialogContext::onWindowClosing( const vcl::Window* _pWindow )
132 if ( !m_rController.getView() )
133 return;
135 ::dbaui::notifySystemWindow(
136 m_rController.getView(), const_cast< vcl::Window* >( _pWindow ), ::comphelper::mem_fun( &TaskPaneList::RemoveWindow ) );
138 m_rController.InvalidateFeature( ID_BROWSER_ADDTABLE );
139 m_rController.getView()->GrabFocus();
142 OJoinTableView* AddTableDialogContext::getTableView() const
144 if ( m_rController.getJoinView() )
145 return m_rController.getJoinView()->getTableView();
146 return NULL;
149 // OJoinController
151 OJoinController::OJoinController(const Reference< XComponentContext >& _rM)
152 :OJoinController_BASE(_rM)
153 ,m_pAddTableDialog(NULL)
157 OJoinController::~OJoinController()
161 void SAL_CALL OJoinController::disposing( const EventObject& _rSource ) throw(RuntimeException, std::exception)
163 OJoinController_BASE::disposing( _rSource );
166 OJoinDesignView* OJoinController::getJoinView()
168 return static_cast< OJoinDesignView* >( getView() );
171 void OJoinController::disposing()
173 m_pAddTableDialog.disposeAndClear();
175 OJoinController_BASE::disposing();
177 clearView();
179 m_vTableConnectionData.clear();
180 m_vTableData.clear();
183 void OJoinController::reconnect( bool _bUI )
185 OJoinController_BASE::reconnect( _bUI );
186 if ( isConnected() && m_pAddTableDialog )
187 m_pAddTableDialog->Update();
190 void OJoinController::impl_onModifyChanged()
192 OJoinController_BASE::impl_onModifyChanged();
193 InvalidateFeature( SID_RELATION_ADD_RELATION );
196 void OJoinController::SaveTabWinPosSize(OTableWindow* pTabWin, long nOffsetX, long nOffsetY)
198 // the data for the window
199 TTableWindowData::value_type pData = pTabWin->GetData();
200 OSL_ENSURE(pData != 0, "SaveTabWinPosSize : TabWin hat keine Daten !");
202 // set Position & Size of data anew (with current window parameters)
203 Point aPos = pTabWin->GetPosPixel();
204 aPos.X() += nOffsetX;
205 aPos.Y() += nOffsetY;
206 pData->SetPosition(aPos);
207 pData->SetSize(pTabWin->GetSizePixel());
211 FeatureState OJoinController::GetState(sal_uInt16 _nId) const
213 FeatureState aReturn;
214 // (disabled automatically)
215 aReturn.bEnabled = true;
217 switch (_nId)
219 case ID_BROWSER_EDITDOC:
220 aReturn.bChecked = isEditable();
221 break;
222 case ID_BROWSER_ADDTABLE:
223 aReturn.bEnabled = ( getView() != NULL )
224 && const_cast< OJoinController* >( this )->getJoinView()->getTableView()->IsAddAllowed();
225 aReturn.bChecked = aReturn.bEnabled && m_pAddTableDialog != nullptr && m_pAddTableDialog->IsVisible() ;
226 if ( aReturn.bEnabled )
227 aReturn.sTitle = OAddTableDlg::getDialogTitleForContext( impl_getDialogContext() );
228 break;
230 default:
231 aReturn = OJoinController_BASE::GetState(_nId);
233 return aReturn;
236 AddTableDialogContext& OJoinController::impl_getDialogContext() const
238 if ( !m_pDialogContext.get() )
240 OJoinController* pNonConstThis = const_cast< OJoinController* >( this );
241 pNonConstThis->m_pDialogContext.reset( new AddTableDialogContext( *pNonConstThis ) );
243 return *m_pDialogContext;
246 void OJoinController::Execute(sal_uInt16 _nId, const Sequence< PropertyValue >& aArgs)
248 switch(_nId)
250 case ID_BROWSER_EDITDOC:
251 if(isEditable())
252 { // the state should be changed to not editable
253 switch (saveModified())
255 case RET_CANCEL:
256 // don't change anything here so return
257 return;
258 case RET_NO:
259 reset();
260 setModified(sal_False); // and we are not modified yet
261 break;
262 default:
263 break;
266 setEditable(!isEditable());
267 getJoinView()->setReadOnly(!isEditable());
268 InvalidateAll();
269 return;
270 case ID_BROWSER_ADDTABLE:
271 if ( !m_pAddTableDialog )
272 m_pAddTableDialog = VclPtr<OAddTableDlg>::Create( getView(), impl_getDialogContext() );
274 if ( m_pAddTableDialog->IsVisible() )
276 m_pAddTableDialog->Show( false );
277 getView()->GrabFocus();
279 else
282 WaitObject aWaitCursor( getView() );
283 m_pAddTableDialog->Update();
285 m_pAddTableDialog->Show( true );
286 ::dbaui::notifySystemWindow(getView(),m_pAddTableDialog,::comphelper::mem_fun(&TaskPaneList::AddWindow));
288 break;
289 default:
290 OJoinController_BASE::Execute(_nId,aArgs);
292 InvalidateFeature(_nId);
295 void OJoinController::SaveTabWinsPosSize( OJoinTableView::OTableWindowMap* pTabWinList, long nOffsetX, long nOffsetY )
297 // Deletion and recreation of the old implementation with the current model is not correct anymore:
298 // The TabWins have a pointer to their data, but they are managed by me. When I delete the old ones, the TabWins suddenly have a pointer to objects, which no longer exist.
299 // If the TabWins had a SetData, I could save that effort... but they don't, further I also would still have to set information anew, which actually didn't change.
300 // So I don't delete the TabWinDatas, but only update them.
301 OSL_ENSURE(m_vTableData.size() == pTabWinList->size(),
302 "OJoinController::SaveTabWinsPosSize : inkonsistenter Zustand : sollte genausviel TabWinDatas haben wie TabWins !");
304 OJoinTableView::OTableWindowMap::iterator aIter = pTabWinList->begin();
305 OJoinTableView::OTableWindowMap::iterator aEnd = pTabWinList->end();
306 for(;aIter != aEnd;++aIter)
307 SaveTabWinPosSize(aIter->second, nOffsetX, nOffsetY);
310 void OJoinController::removeConnectionData(const TTableConnectionData::value_type& _pData)
312 m_vTableConnectionData.erase( ::std::remove(m_vTableConnectionData.begin(),m_vTableConnectionData.end(),_pData),m_vTableConnectionData.end());
315 void OJoinController::describeSupportedFeatures()
317 OJoinController_BASE::describeSupportedFeatures();
318 implDescribeSupportedFeature( ".uno:Redo", ID_BROWSER_REDO, CommandGroup::EDIT );
319 implDescribeSupportedFeature( ".uno:Save", ID_BROWSER_SAVEDOC, CommandGroup::DOCUMENT );
320 implDescribeSupportedFeature( ".uno:Undo", ID_BROWSER_UNDO, CommandGroup::EDIT );
321 implDescribeSupportedFeature( ".uno:AddTable", ID_BROWSER_ADDTABLE,CommandGroup::EDIT );
322 implDescribeSupportedFeature( ".uno:EditDoc", ID_BROWSER_EDITDOC, CommandGroup::EDIT );
325 sal_Bool SAL_CALL OJoinController::suspend(sal_Bool _bSuspend) throw( RuntimeException, std::exception )
327 if ( getBroadcastHelper().bInDispose || getBroadcastHelper().bDisposed )
328 return sal_True;
330 SolarMutexGuard aSolarGuard;
331 ::osl::MutexGuard aGuard( getMutex() );
332 if ( getView() && getView()->IsInModalMode() )
333 return sal_False;
334 bool bCheck = true;
335 if ( _bSuspend )
337 bCheck = saveModified() != RET_CANCEL;
338 if ( bCheck )
339 OSingleDocumentController::suspend(_bSuspend);
341 return bCheck;
344 void OJoinController::loadTableWindows( const ::comphelper::NamedValueCollection& i_rViewSettings )
346 m_vTableData.clear();
348 m_aMinimumTableViewSize = Point();
350 Sequence< PropertyValue > aWindowData;
351 aWindowData = i_rViewSettings.getOrDefault( "Tables", aWindowData );
353 const PropertyValue* pTablesIter = aWindowData.getConstArray();
354 const PropertyValue* pTablesEnd = pTablesIter + aWindowData.getLength();
355 for ( ; pTablesIter != pTablesEnd; ++pTablesIter )
357 ::comphelper::NamedValueCollection aSingleTableData( pTablesIter->Value );
358 loadTableWindow( aSingleTableData );
360 if ( m_aMinimumTableViewSize != Point() )
362 getJoinView()->getScrollHelper()->resetRange( m_aMinimumTableViewSize );
366 void OJoinController::loadTableWindow( const ::comphelper::NamedValueCollection& i_rTableWindowSettings )
368 sal_Int32 nX = -1, nY = -1, nHeight = -1, nWidth = -1;
370 OUString sComposedName,sTableName,sWindowName;
371 bool bShowAll = false;
373 sComposedName = i_rTableWindowSettings.getOrDefault( "ComposedName", sComposedName );
374 sTableName = i_rTableWindowSettings.getOrDefault( "TableName", sTableName );
375 sWindowName = i_rTableWindowSettings.getOrDefault( "WindowName", sWindowName );
376 nY = i_rTableWindowSettings.getOrDefault( "WindowTop", nY );
377 nX = i_rTableWindowSettings.getOrDefault( "WindowLeft", nX );
378 nWidth = i_rTableWindowSettings.getOrDefault( "WindowWidth", nWidth );
379 nHeight = i_rTableWindowSettings.getOrDefault( "WindowHeight", nHeight );
380 bShowAll = i_rTableWindowSettings.getOrDefault( "ShowAll", bShowAll );
382 TTableWindowData::value_type pData = createTableWindowData(sComposedName,sTableName,sWindowName);
383 if ( pData )
385 pData->SetPosition(Point(nX,nY));
386 pData->SetSize( Size( nWidth, nHeight ) );
387 pData->ShowAll(bShowAll);
388 m_vTableData.push_back(pData);
389 if ( m_aMinimumTableViewSize.X() < (nX+nWidth) )
390 m_aMinimumTableViewSize.X() = (nX+nWidth);
391 if ( m_aMinimumTableViewSize.Y() < (nY+nHeight) )
392 m_aMinimumTableViewSize.Y() = (nY+nHeight);
396 void OJoinController::saveTableWindows( ::comphelper::NamedValueCollection& o_rViewSettings ) const
398 if ( !m_vTableData.empty() )
400 ::comphelper::NamedValueCollection aAllTablesData;
402 TTableWindowData::const_iterator aIter = m_vTableData.begin();
403 TTableWindowData::const_iterator aEnd = m_vTableData.end();
404 for ( sal_Int32 i = 1; aIter != aEnd; ++aIter, ++i )
406 ::comphelper::NamedValueCollection aWindowData;
407 aWindowData.put( "ComposedName", (*aIter)->GetComposedName() );
408 aWindowData.put( "TableName", (*aIter)->GetTableName() );
409 aWindowData.put( "WindowName", (*aIter)->GetWinName() );
410 aWindowData.put( "WindowTop", static_cast<sal_Int32>((*aIter)->GetPosition().Y()) );
411 aWindowData.put( "WindowLeft", static_cast<sal_Int32>((*aIter)->GetPosition().X()) );
412 aWindowData.put( "WindowWidth", static_cast<sal_Int32>((*aIter)->GetSize().Width()) );
413 aWindowData.put( "WindowHeight", static_cast<sal_Int32>((*aIter)->GetSize().Height()) );
414 aWindowData.put( "ShowAll", (*aIter)->IsShowAll() );
416 const OUString sTableName( "Table" + OUString::number( i ) );
417 aAllTablesData.put( sTableName, aWindowData.getPropertyValues() );
420 o_rViewSettings.put( "Tables", aAllTablesData.getPropertyValues() );
424 TTableWindowData::value_type OJoinController::createTableWindowData(const OUString& _sComposedName,const OUString& _sTableName,const OUString& _sWindowName)
426 OJoinDesignView* pView = getJoinView();
427 if( pView )
428 return pView->getTableView()->createTableWindowData(_sComposedName,_sTableName,_sWindowName);
429 OSL_FAIL("We should never ever reach this point!");
431 return TTableWindowData::value_type();
434 } // namespace dbaui
436 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */