bump product version to 4.1.6.2
[LibreOffice.git] / dbaccess / source / ui / querydesign / JoinController.cxx
blob89b192ac158bbd3411479d15da86a904cbf88097
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/basicio.hxx>
44 #include <comphelper/seqstream.hxx>
45 #include <com/sun/star/io/XActiveDataSource.hpp>
46 #include <com/sun/star/io/XActiveDataSink.hpp>
47 #include "sqlmessage.hxx"
48 #include "JoinController.hxx"
49 #include <vcl/msgbox.hxx>
50 #include "TableWindowData.hxx"
51 #include "TableWindow.hxx"
52 #include "TableConnectionData.hxx"
53 #include "adtabdlg.hxx"
54 #include <vcl/waitobj.hxx>
55 #include <vcl/svapp.hxx>
56 #include <osl/mutex.hxx>
57 #include "UITools.hxx"
58 #include <osl/diagnose.h>
60 #include <boost/optional.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 // .............................................................................
77 namespace dbaui
79 // .............................................................................
81 // =============================================================================
82 // = AddTableDialogContext
83 // =============================================================================
84 class AddTableDialogContext : public IAddTableDialogContext
86 OJoinController& m_rController;
88 public:
89 AddTableDialogContext( OJoinController& _rController )
90 :m_rController( _rController )
94 virtual ~AddTableDialogContext() {}
96 // IAddTableDialogContext
97 virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >
98 getConnection() const;
99 virtual bool allowViews() const;
100 virtual bool allowQueries() const;
101 virtual bool allowAddition() const;
102 virtual void addTableWindow( const String& _rQualifiedTableName, const String& _rAliasName );
103 virtual void onWindowClosing( const Window* _pWindow );
105 private:
106 OJoinTableView* getTableView() const;
109 // -----------------------------------------------------------------------------
110 Reference< XConnection > AddTableDialogContext::getConnection() const
112 return m_rController.getConnection();
115 // -----------------------------------------------------------------------------
116 bool AddTableDialogContext::allowViews() const
118 return m_rController.allowViews();
121 // -----------------------------------------------------------------------------
122 bool AddTableDialogContext::allowQueries() const
124 return m_rController.allowQueries();
127 // -----------------------------------------------------------------------------
128 bool AddTableDialogContext::allowAddition() const
130 return const_cast< OJoinController& >( m_rController ).getJoinView()->getTableView()->IsAddAllowed();
133 // -----------------------------------------------------------------------------
134 void AddTableDialogContext::addTableWindow( const String& _rQualifiedTableName, const String& _rAliasName )
136 getTableView()->AddTabWin( _rQualifiedTableName, _rAliasName, sal_True );
139 // -----------------------------------------------------------------------------
140 void AddTableDialogContext::onWindowClosing( const Window* _pWindow )
142 if ( !m_rController.getView() )
143 return;
145 ::dbaui::notifySystemWindow(
146 m_rController.getView(), const_cast< Window* >( _pWindow ), ::comphelper::mem_fun( &TaskPaneList::RemoveWindow ) );
148 m_rController.InvalidateFeature( ID_BROWSER_ADDTABLE );
149 m_rController.getView()->GrabFocus();
152 // -----------------------------------------------------------------------------
153 OJoinTableView* AddTableDialogContext::getTableView() const
155 if ( m_rController.getJoinView() )
156 return m_rController.getJoinView()->getTableView();
157 return NULL;
160 // =============================================================================
161 // = OJoinController
162 // =============================================================================
164 DBG_NAME(OJoinController)
165 // -----------------------------------------------------------------------------
166 OJoinController::OJoinController(const Reference< XComponentContext >& _rM)
167 :OJoinController_BASE(_rM)
168 ,m_pAddTableDialog(NULL)
170 DBG_CTOR(OJoinController,NULL);
172 // -----------------------------------------------------------------------------
173 OJoinController::~OJoinController()
175 DBG_DTOR(OJoinController,NULL);
178 // -----------------------------------------------------------------------------
179 void SAL_CALL OJoinController::disposing( const EventObject& _rSource ) throw(RuntimeException)
181 OJoinController_BASE::disposing( _rSource );
184 // -----------------------------------------------------------------------------
185 OJoinDesignView* OJoinController::getJoinView()
187 return static_cast< OJoinDesignView* >( getView() );
190 // -----------------------------------------------------------------------------
191 void OJoinController::disposing()
194 ::std::auto_ptr< Window > pEnsureDelete( m_pAddTableDialog );
195 m_pAddTableDialog = NULL;
198 OJoinController_BASE::disposing();
200 clearView();
202 m_vTableConnectionData.clear();
203 m_vTableData.clear();
205 // -----------------------------------------------------------------------------
206 void OJoinController::reconnect( sal_Bool _bUI )
208 OJoinController_BASE::reconnect( _bUI );
209 if ( isConnected() && m_pAddTableDialog )
210 m_pAddTableDialog->Update();
213 // -----------------------------------------------------------------------------
214 void OJoinController::impl_onModifyChanged()
216 OJoinController_BASE::impl_onModifyChanged();
217 InvalidateFeature( SID_RELATION_ADD_RELATION );
219 // -----------------------------------------------------------------------------
220 void OJoinController::SaveTabWinPosSize(OTableWindow* pTabWin, long nOffsetX, long nOffsetY)
222 // the data for the window
223 TTableWindowData::value_type pData = pTabWin->GetData();
224 OSL_ENSURE(pData != NULL, "SaveTabWinPosSize : TabWin hat keine Daten !");
226 // set Position & Size of data anew (with current window parameters)
227 Point aPos = pTabWin->GetPosPixel();
228 aPos.X() += nOffsetX;
229 aPos.Y() += nOffsetY;
230 pData->SetPosition(aPos);
231 pData->SetSize(pTabWin->GetSizePixel());
234 // -----------------------------------------------------------------------------
235 FeatureState OJoinController::GetState(sal_uInt16 _nId) const
237 FeatureState aReturn;
238 // (disabled automatically)
239 aReturn.bEnabled = sal_True;
241 switch (_nId)
243 case ID_BROWSER_EDITDOC:
244 aReturn.bChecked = isEditable();
245 break;
246 case ID_BROWSER_ADDTABLE:
247 aReturn.bEnabled = ( getView() != NULL )
248 && const_cast< OJoinController* >( this )->getJoinView()->getTableView()->IsAddAllowed();
249 aReturn.bChecked = aReturn.bEnabled && m_pAddTableDialog != NULL && m_pAddTableDialog->IsVisible() ;
250 if ( aReturn.bEnabled )
251 aReturn.sTitle = OAddTableDlg::getDialogTitleForContext( impl_getDialogContext() );
252 break;
254 default:
255 aReturn = OJoinController_BASE::GetState(_nId);
257 return aReturn;
260 // -----------------------------------------------------------------------------
261 AddTableDialogContext& OJoinController::impl_getDialogContext() const
263 if ( !m_pDialogContext.get() )
265 OJoinController* pNonConstThis = const_cast< OJoinController* >( this );
266 pNonConstThis->m_pDialogContext.reset( new AddTableDialogContext( *pNonConstThis ) );
268 return *m_pDialogContext;
271 // -----------------------------------------------------------------------------
272 void OJoinController::Execute(sal_uInt16 _nId, const Sequence< PropertyValue >& aArgs)
274 switch(_nId)
276 case ID_BROWSER_EDITDOC:
277 if(isEditable())
278 { // the state should be changed to not editable
279 switch (saveModified())
281 case RET_CANCEL:
282 // don't change anything here so return
283 return;
284 case RET_NO:
285 reset();
286 setModified(sal_False); // and we are not modified yet
287 break;
288 default:
289 break;
292 setEditable(!isEditable());
293 getJoinView()->setReadOnly(!isEditable());
294 InvalidateAll();
295 return;
296 case ID_BROWSER_ADDTABLE:
297 if ( !m_pAddTableDialog )
298 m_pAddTableDialog = new OAddTableDlg( getView(), impl_getDialogContext() );
300 if ( m_pAddTableDialog->IsVisible() )
302 m_pAddTableDialog->Show( sal_False );
303 getView()->GrabFocus();
305 else
308 WaitObject aWaitCursor( getView() );
309 m_pAddTableDialog->Update();
311 m_pAddTableDialog->Show( sal_True );
312 ::dbaui::notifySystemWindow(getView(),m_pAddTableDialog,::comphelper::mem_fun(&TaskPaneList::AddWindow));
314 break;
315 default:
316 OJoinController_BASE::Execute(_nId,aArgs);
318 InvalidateFeature(_nId);
320 // -----------------------------------------------------------------------------
321 void OJoinController::SaveTabWinsPosSize( OJoinTableView::OTableWindowMap* pTabWinList, long nOffsetX, long nOffsetY )
323 // Deletion and recreation of the old implementation with the current model is not correct anymore:
324 // 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.
325 // 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.
326 // So I don't delete the TabWinDatas, but only update them.
327 OSL_ENSURE(m_vTableData.size() == pTabWinList->size(),
328 "OJoinController::SaveTabWinsPosSize : inkonsistenter Zustand : sollte genausviel TabWinDatas haben wie TabWins !");
330 OJoinTableView::OTableWindowMap::iterator aIter = pTabWinList->begin();
331 OJoinTableView::OTableWindowMap::iterator aEnd = pTabWinList->end();
332 for(;aIter != aEnd;++aIter)
333 SaveTabWinPosSize(aIter->second, nOffsetX, nOffsetY);
335 // -----------------------------------------------------------------------------
336 void OJoinController::removeConnectionData(const TTableConnectionData::value_type& _pData)
338 m_vTableConnectionData.erase( ::std::remove(m_vTableConnectionData.begin(),m_vTableConnectionData.end(),_pData),m_vTableConnectionData.end());
340 // -----------------------------------------------------------------------------
341 void OJoinController::describeSupportedFeatures()
343 OJoinController_BASE::describeSupportedFeatures();
344 implDescribeSupportedFeature( ".uno:Redo", ID_BROWSER_REDO, CommandGroup::EDIT );
345 implDescribeSupportedFeature( ".uno:Save", ID_BROWSER_SAVEDOC, CommandGroup::DOCUMENT );
346 implDescribeSupportedFeature( ".uno:Undo", ID_BROWSER_UNDO, CommandGroup::EDIT );
347 implDescribeSupportedFeature( ".uno:AddTable", ID_BROWSER_ADDTABLE,CommandGroup::EDIT );
348 implDescribeSupportedFeature( ".uno:EditDoc", ID_BROWSER_EDITDOC, CommandGroup::EDIT );
350 // -----------------------------------------------------------------------------
351 sal_Bool SAL_CALL OJoinController::suspend(sal_Bool _bSuspend) throw( RuntimeException )
353 if ( getBroadcastHelper().bInDispose || getBroadcastHelper().bDisposed )
354 return sal_True;
356 SolarMutexGuard aSolarGuard;
357 ::osl::MutexGuard aGuard( getMutex() );
358 if ( getView() && getView()->IsInModalMode() )
359 return sal_False;
360 sal_Bool bCheck = sal_True;
361 if ( _bSuspend )
363 bCheck = saveModified() != RET_CANCEL;
364 if ( bCheck )
365 OSingleDocumentController::suspend(_bSuspend);
367 return bCheck;
369 // -----------------------------------------------------------------------------
370 void OJoinController::loadTableWindows( const ::comphelper::NamedValueCollection& i_rViewSettings )
372 m_vTableData.clear();
374 m_aMinimumTableViewSize = Point();
376 Sequence< PropertyValue > aWindowData;
377 aWindowData = i_rViewSettings.getOrDefault( "Tables", aWindowData );
379 const PropertyValue* pTablesIter = aWindowData.getConstArray();
380 const PropertyValue* pTablesEnd = pTablesIter + aWindowData.getLength();
381 for ( ; pTablesIter != pTablesEnd; ++pTablesIter )
383 ::comphelper::NamedValueCollection aSingleTableData( pTablesIter->Value );
384 loadTableWindow( aSingleTableData );
386 if ( m_aMinimumTableViewSize != Point() )
388 getJoinView()->getScrollHelper()->resetRange( m_aMinimumTableViewSize );
392 // -----------------------------------------------------------------------------
393 void OJoinController::loadTableWindow( const ::comphelper::NamedValueCollection& i_rTableWindowSettings )
395 sal_Int32 nX = -1, nY = -1, nHeight = -1, nWidth = -1;
397 OUString sComposedName,sTableName,sWindowName;
398 sal_Bool bShowAll = false;
400 sComposedName = i_rTableWindowSettings.getOrDefault( "ComposedName", sComposedName );
401 sTableName = i_rTableWindowSettings.getOrDefault( "TableName", sTableName );
402 sWindowName = i_rTableWindowSettings.getOrDefault( "WindowName", sWindowName );
403 nY = i_rTableWindowSettings.getOrDefault( "WindowTop", nY );
404 nX = i_rTableWindowSettings.getOrDefault( "WindowLeft", nX );
405 nWidth = i_rTableWindowSettings.getOrDefault( "WindowWidth", nWidth );
406 nHeight = i_rTableWindowSettings.getOrDefault( "WindowHeight", nHeight );
407 bShowAll = i_rTableWindowSettings.getOrDefault( "ShowAll", bShowAll );
409 TTableWindowData::value_type pData = createTableWindowData(sComposedName,sTableName,sWindowName);
410 if ( pData )
412 pData->SetPosition(Point(nX,nY));
413 pData->SetSize( Size( nWidth, nHeight ) );
414 pData->ShowAll(bShowAll);
415 m_vTableData.push_back(pData);
416 if ( m_aMinimumTableViewSize.X() < (nX+nWidth) )
417 m_aMinimumTableViewSize.X() = (nX+nWidth);
418 if ( m_aMinimumTableViewSize.Y() < (nY+nHeight) )
419 m_aMinimumTableViewSize.Y() = (nY+nHeight);
422 // -----------------------------------------------------------------------------
423 void OJoinController::saveTableWindows( ::comphelper::NamedValueCollection& o_rViewSettings ) const
425 if ( !m_vTableData.empty() )
427 ::comphelper::NamedValueCollection aAllTablesData;
429 TTableWindowData::const_iterator aIter = m_vTableData.begin();
430 TTableWindowData::const_iterator aEnd = m_vTableData.end();
431 for ( sal_Int32 i = 1; aIter != aEnd; ++aIter, ++i )
433 ::comphelper::NamedValueCollection aWindowData;
434 aWindowData.put( "ComposedName", (*aIter)->GetComposedName() );
435 aWindowData.put( "TableName", (*aIter)->GetTableName() );
436 aWindowData.put( "WindowName", (*aIter)->GetWinName() );
437 aWindowData.put( "WindowTop", static_cast<sal_Int32>((*aIter)->GetPosition().Y()) );
438 aWindowData.put( "WindowLeft", static_cast<sal_Int32>((*aIter)->GetPosition().X()) );
439 aWindowData.put( "WindowWidth", static_cast<sal_Int32>((*aIter)->GetSize().Width()) );
440 aWindowData.put( "WindowHeight", static_cast<sal_Int32>((*aIter)->GetSize().Height()) );
441 aWindowData.put( "ShowAll", (*aIter)->IsShowAll() );
443 const OUString sTableName( OUString( "Table" ) + OUString::valueOf( i ) );
444 aAllTablesData.put( sTableName, aWindowData.getPropertyValues() );
447 o_rViewSettings.put( "Tables", aAllTablesData.getPropertyValues() );
450 // -----------------------------------------------------------------------------
451 TTableWindowData::value_type OJoinController::createTableWindowData(const OUString& _sComposedName,const OUString& _sTableName,const OUString& _sWindowName)
453 OJoinDesignView* pView = getJoinView();
454 if( pView )
455 return pView->getTableView()->createTableWindowData(_sComposedName,_sTableName,_sWindowName);
456 OSL_FAIL("We should never ever reach this point!");
458 return TTableWindowData::value_type();
460 // .............................................................................
461 } // namespace dbaui
462 // .............................................................................
464 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */