merge the formfield patch from ooo-build
[ooovba.git] / dbaccess / source / ui / app / AppControllerDnD.cxx
blob74c628be37ca5c55ee6c68ba41ff391249d863ca
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: AppControllerDnD.cxx,v $
10 * $Revision: 1.26.6.4 $
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"
34 #ifndef DBAUI_APPCONTROLLER_HXX
35 #include "AppController.hxx"
36 #endif
37 #ifndef _COMPHELPER_SEQUENCE_HXX_
38 #include <comphelper/sequence.hxx>
39 #endif
40 #ifndef _COMPHELPER_PROPERTY_HXX_
41 #include <comphelper/property.hxx>
42 #endif
43 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
44 #include "dbustrings.hrc"
45 #endif
46 #ifndef _COM_SUN_STAR_SDBCX_XDATADESCRIPTORFACTORY_HPP_
47 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
48 #endif
49 #ifndef _COM_SUN_STAR_SDBCX_XAPPEND_HPP_
50 #include <com/sun/star/sdbcx/XAppend.hpp>
51 #endif
52 #ifndef _COM_SUN_STAR_SDBCX_XCOLUMNSSUPPLIER_HPP_
53 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
54 #endif
55 #ifndef _COM_SUN_STAR_SDB_XSINGLESELECTQUERYCOMPOSER_HPP_
56 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
57 #endif
58 #ifndef _COM_SUN_STAR_CONTAINER_XNAMECONTAINER_HPP_
59 #include <com/sun/star/container/XNameContainer.hpp>
60 #endif
61 #ifndef _COM_SUN_STAR_UNO_XNAMINGSERVICE_HPP_
62 #include <com/sun/star/uno/XNamingService.hpp>
63 #endif
64 #ifndef _COM_SUN_STAR_SDBC_XDATASOURCE_HPP_
65 #include <com/sun/star/sdbc/XDataSource.hpp>
66 #endif
67 #ifndef _COM_SUN_STAR_FRAME_XSTORABLE_HPP_
68 #include <com/sun/star/frame/XStorable.hpp>
69 #endif
70 #ifndef _COM_SUN_STAR_CONTAINER_XCHILD_HPP_
71 #include <com/sun/star/container/XChild.hpp>
72 #endif
73 #ifndef _COM_SUN_STAR_CONTAINER_XHIERARCHICALNAMECONTAINER_HPP_
74 #include <com/sun/star/container/XHierarchicalNameContainer.hpp>
75 #endif
76 #ifndef _COM_SUN_STAR_SDBC_DATATYPE_HPP_
77 #include <com/sun/star/sdbc/DataType.hpp>
78 #endif
79 #ifndef _COM_SUN_STAR_SDB_COMMANDTYPE_HPP_
80 #include <com/sun/star/sdb/CommandType.hpp>
81 #endif
82 #ifndef _COM_SUN_STAR_SDB_XBOOKMARKSSUPPLIER_HPP_
83 #include <com/sun/star/sdb/XBookmarksSupplier.hpp>
84 #endif
85 #ifndef _COM_SUN_STAR_SDB_SQLCONTEXT_HPP_
86 #include <com/sun/star/sdb/SQLContext.hpp>
87 #endif
88 #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
89 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
90 #endif
91 #ifndef _COM_SUN_STAR_SDBCX_XVIEWSSUPPLIER_HPP_
92 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
93 #endif
94 #ifndef _COM_SUN_STAR_SDB_XQUERYDEFINITIONSSUPPLIER_HPP_
95 #include <com/sun/star/sdb/XQueryDefinitionsSupplier.hpp>
96 #endif
97 #ifndef _COM_SUN_STAR_SDBCX_XDROP_HPP_
98 #include <com/sun/star/sdbcx/XDrop.hpp>
99 #endif
100 #ifndef _TOOLS_DEBUG_HXX
101 #include <tools/debug.hxx>
102 #endif
103 #ifndef _URLOBJ_HXX
104 #include <tools/urlobj.hxx>
105 #endif
106 #ifndef _UNOTOOLS_UCBHELPER_HXX
107 #include <unotools/ucbhelper.hxx>
108 #endif
109 #ifndef DBAUI_DLGSAVE_HXX
110 #include "dlgsave.hxx"
111 #endif
112 #ifndef _COMPHELPER_TYPES_HXX_
113 #include <comphelper/types.hxx>
114 #endif
115 #ifndef _SV_MSGBOX_HXX
116 #include <vcl/msgbox.hxx>
117 #endif
118 #ifndef _CPPUHELPER_TYPEPROVIDER_HXX_
119 #include <cppuhelper/typeprovider.hxx>
120 #endif
121 #ifndef _CPPUHELPER_EXC_HLP_HXX_
122 #include <cppuhelper/exc_hlp.hxx>
123 #endif
124 #ifndef _DBHELPER_DBEXCEPTION_HXX_
125 #include <connectivity/dbexception.hxx>
126 #endif
127 #ifndef _SV_WAITOBJ_HXX
128 #include <vcl/waitobj.hxx>
129 #endif
130 #ifndef _RTL_USTRBUF_HXX_
131 #include <rtl/ustrbuf.hxx>
132 #endif
133 #ifndef DBAUI_APPVIEW_HXX
134 #include "AppView.hxx"
135 #endif
136 #ifndef _SVX_DATACCESSDESCRIPTOR_HXX_
137 #include <svx/dataaccessdescriptor.hxx>
138 #endif
139 #ifndef SVX_DBAOBJECTEX_HXX
140 #include <svx/dbaobjectex.hxx>
141 #endif
142 #ifndef DBACCESS_UI_BROWSER_ID_HXX
143 #include "browserids.hxx"
144 #endif
145 #ifndef _DBAU_REGHELPER_HXX_
146 #include "dbu_reghelper.hxx"
147 #endif
148 #ifndef _DBU_APP_HRC_
149 #include "dbu_app.hrc"
150 #endif
151 #ifndef _SV_MENU_HXX
152 #include <vcl/menu.hxx>
153 #endif
154 #ifndef _COMPHELPER_UNO3_HXX_
155 #include <comphelper/uno3.hxx>
156 #endif
157 #ifndef _SV_SVAPP_HXX //autogen
158 #include <vcl/svapp.hxx>
159 #endif
160 #ifndef _SVLBOXITM_HXX
161 #include <svtools/svlbitm.hxx>
162 #endif
163 #ifndef _DBAUI_LISTVIEWITEMS_HXX_
164 #include "listviewitems.hxx"
165 #endif
166 #ifndef DBAUI_APPDETAILVIEW_HXX
167 #include "AppDetailView.hxx"
168 #endif
169 #ifndef _DBAUI_LINKEDDOCUMENTS_HXX_
170 #include "linkeddocuments.hxx"
171 #endif
172 #ifndef _SV_LSTBOX_HXX
173 #include <vcl/lstbox.hxx>
174 #endif
175 #ifndef _DBHELPER_DBEXCEPTION_HXX_
176 #include <connectivity/dbexception.hxx>
177 #endif
178 #ifndef _CONNECTIVITY_DBTOOLS_HXX_
179 #include <connectivity/dbtools.hxx>
180 #endif
181 #ifndef _DBAUI_SQLMESSAGE_HXX_
182 #include "sqlmessage.hxx"
183 #endif
184 #ifndef _STRING_HXX
185 #include <tools/string.hxx>
186 #endif
187 #ifndef DBAUI_DBEXCHANGE_HXX
188 #include "dbexchange.hxx"
189 #endif
190 #ifndef DBAUI_TOOLS_HXX
191 #include "UITools.hxx"
192 #endif
193 #include <algorithm>
194 #ifndef _SVTREEBOX_HXX
195 #include <svtools/svtreebx.hxx>
196 #endif
197 #ifndef _COM_SUN_STAR_SDB_XREPORTDOCUMENTSSUPPLIER_HPP_
198 #include <com/sun/star/sdb/XReportDocumentsSupplier.hpp>
199 #endif
200 #ifndef _COM_SUN_STAR_SDB_XFORMDOCUMENTSSUPPLIER_HPP_
201 #include <com/sun/star/sdb/XFormDocumentsSupplier.hpp>
202 #endif
203 #ifndef _FILEDLGHELPER_HXX
204 #include <sfx2/filedlghelper.hxx>
205 #endif
206 #ifndef INCLUDED_SVTOOLS_PATHOPTIONS_HXX
207 #include <svtools/pathoptions.hxx>
208 #endif
209 #ifndef _SFX_DOCFILT_HACK_HXX
210 #include <sfx2/docfilt.hxx>
211 #endif
212 #ifndef _SVT_FILEVIEW_HXX
213 #include <svtools/fileview.hxx>
214 #endif
215 #ifndef TOOLS_DIAGNOSE_EX_H
216 #include <tools/diagnose_ex.h>
217 #endif
218 #ifndef DBACCESS_SOURCE_UI_MISC_DEFAULTOBJECTNAMECHECK_HXX
219 #include "defaultobjectnamecheck.hxx"
220 #endif
221 #ifndef _VOS_MUTEX_HXX_
222 #include <vos/mutex.hxx>
223 #endif
224 #include "subcomponentmanager.hxx"
226 //........................................................................
227 namespace dbaui
229 //........................................................................
230 using namespace ::dbtools;
231 using namespace ::svx;
232 using namespace ::svtools;
233 using namespace ::com::sun::star::uno;
234 using namespace ::com::sun::star::task;
235 using namespace ::com::sun::star::beans;
236 using namespace ::com::sun::star::lang;
237 using namespace ::com::sun::star::container;
238 using namespace ::com::sun::star::sdb;
239 using namespace ::com::sun::star::sdbc;
240 using namespace ::com::sun::star::sdbcx;
241 using namespace ::com::sun::star::frame;
242 using namespace ::com::sun::star::ucb;
243 using namespace ::com::sun::star::util;
245 // -----------------------------------------------------------------------------
246 void OApplicationController::deleteTables(const ::std::vector< ::rtl::OUString>& _rList)
248 SharedConnection xConnection( ensureConnection() );
250 Reference<XTablesSupplier> xSup(xConnection,UNO_QUERY);
251 OSL_ENSURE(xSup.is(),"OApplicationController::deleteTable: no XTablesSuppier!");
252 if ( xSup.is() )
254 Reference<XNameAccess> xTables = xSup->getTables();
255 Reference<XDrop> xDrop(xTables,UNO_QUERY);
256 if ( xDrop.is() )
258 bool bConfirm = true;
259 ::std::vector< ::rtl::OUString>::const_iterator aEnd = _rList.end();
260 for (::std::vector< ::rtl::OUString>::const_iterator aIter = _rList.begin(); aIter != aEnd; ++aIter)
262 ::rtl::OUString sTableName = *aIter;
264 sal_Int32 nResult = RET_YES;
265 if ( bConfirm )
266 nResult = ::dbaui::askForUserAction(getView(),STR_TITLE_CONFIRM_DELETION ,STR_QUERY_DELETE_TABLE,_rList.size() > 1 && (aIter+1) != _rList.end(),sTableName);
268 bool bUserConfirmedDelete =
269 ( RET_YES == nResult )
270 || ( RET_ALL == nResult );
271 if ( bUserConfirmedDelete && m_pSubComponentManager->closeSubFrames( sTableName, E_TABLE ) )
273 SQLExceptionInfo aErrorInfo;
276 if ( xTables->hasByName(sTableName) )
277 xDrop->dropByName(sTableName);
278 else
279 {// could be a view
280 Reference<XViewsSupplier> xViewsSup(xConnection,UNO_QUERY);
282 Reference<XNameAccess> xViews;
283 if ( xViewsSup.is() )
285 xViews = xViewsSup->getViews();
286 if ( xViews.is() && xViews->hasByName(sTableName) )
288 xDrop.set(xViews,UNO_QUERY);
289 if ( xDrop.is() )
290 xDrop->dropByName(sTableName);
295 catch(SQLContext& e) { aErrorInfo = e; }
296 catch(SQLWarning& e) { aErrorInfo = e; }
297 catch(SQLException& e) { aErrorInfo = e; }
298 catch(WrappedTargetException& e)
300 SQLException aSql;
301 if(e.TargetException >>= aSql)
302 aErrorInfo = aSql;
303 else
304 OSL_ENSURE(sal_False, "OApplicationController::implDropTable: something strange happended!");
306 catch( const Exception& )
308 DBG_UNHANDLED_EXCEPTION();
311 if ( aErrorInfo.isValid() )
312 showError(aErrorInfo);
314 if ( RET_ALL == nResult )
315 bConfirm = false;
317 else
318 break;
321 else
323 String sMessage(ModuleRes(STR_MISSING_TABLES_XDROP));
324 ErrorBox aError(getView(), WB_OK, sMessage);
325 aError.Execute();
329 // -----------------------------------------------------------------------------
330 void OApplicationController::deleteObjects( ElementType _eType, const ::std::vector< ::rtl::OUString>& _rList, bool _bConfirm )
332 Reference< XNameContainer > xNames( getElements( _eType ), UNO_QUERY );
333 Reference< XHierarchicalNameContainer > xHierarchyName( xNames, UNO_QUERY );
334 if ( xNames.is() )
336 ByteString sDialogPosition;
337 svtools::QueryDeleteResult_Impl eResult = _bConfirm ? svtools::QUERYDELETE_YES : svtools::QUERYDELETE_ALL;
339 // The list of elements to delete is allowed to contain related elements: A given element may
340 // be the ancestor or child of another element from the list.
341 // We want to ensure that ancestors get deleted first, so we normalize the list in this respect.
342 // #i33353# - 2004-09-27 - fs@openoffice.org
343 ::std::set< ::rtl::OUString > aDeleteNames;
344 // Note that this implicitly uses ::std::less< ::rtl::OUString > a comparison operation, which
345 // results in lexicographical order, which is exactly what we need, because "foo" is *before*
346 // any "foo/bar" in this order.
347 ::std::copy(
348 _rList.begin(), _rList.end(),
349 ::std::insert_iterator< ::std::set< ::rtl::OUString > >( aDeleteNames, aDeleteNames.begin() )
352 ::std::set< ::rtl::OUString >::size_type nCount = aDeleteNames.size();
353 for ( ::std::set< ::rtl::OUString >::size_type nObjectsLeft = nCount; !aDeleteNames.empty(); )
355 ::std::set< ::rtl::OUString >::iterator aThisRound = aDeleteNames.begin();
357 if ( eResult != svtools::QUERYDELETE_ALL )
359 svtools::QueryDeleteDlg_Impl aDlg( getView(), *aThisRound );
361 if ( sDialogPosition.Len() )
362 aDlg.SetWindowState( sDialogPosition );
364 if ( nObjectsLeft > 1 )
365 aDlg.EnableAllButton();
367 if ( aDlg.Execute() == RET_OK )
368 eResult = aDlg.GetResult();
369 else
370 return;
372 sDialogPosition = aDlg.GetWindowState( );
375 bool bSuccess = false;
377 bool bUserConfirmedDelete =
378 ( eResult == svtools::QUERYDELETE_ALL )
379 || ( eResult == svtools::QUERYDELETE_YES );
381 if ( bUserConfirmedDelete
382 && ( ( _eType == E_QUERY ) ? m_pSubComponentManager->closeSubFrames( *aThisRound, _eType ) : true )
387 if ( xHierarchyName.is() )
388 xHierarchyName->removeByHierarchicalName( *aThisRound );
389 else
390 xNames->removeByName( *aThisRound );
392 bSuccess = true;
394 // now that we removed the element, care for all it's child elements
395 // which may also be a part of the list
396 // #i33353# - 2004-09-27 - fs@openoffice.org
397 OSL_ENSURE( aThisRound->getLength() - 1 >= 0, "OApplicationController::deleteObjects: empty name?" );
398 ::rtl::OUStringBuffer sSmallestSiblingName( *aThisRound );
399 sSmallestSiblingName.append( (sal_Unicode)( '/' + 1) );
401 ::std::set< ::rtl::OUString >::iterator aUpperChildrenBound = aDeleteNames.lower_bound( sSmallestSiblingName.makeStringAndClear() );
402 for ( ::std::set< ::rtl::OUString >::iterator aObsolete = aThisRound;
403 aObsolete != aUpperChildrenBound;
406 #if OSL_DEBUG_LEVEL > 0
407 ::rtl::OUString sObsoleteName = *aObsolete;
408 #endif
409 ::std::set< ::rtl::OUString >::iterator aNextObsolete = aObsolete; ++aNextObsolete;
410 aDeleteNames.erase( aObsolete );
411 --nObjectsLeft;
412 aObsolete = aNextObsolete;
415 catch(const SQLException&)
417 showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
419 catch(WrappedTargetException& e)
421 SQLException aSql;
422 if ( e.TargetException >>= aSql )
423 showError( SQLExceptionInfo( e.TargetException ) );
424 else
425 OSL_ENSURE( sal_False, "OApplicationController::deleteObjects: something strange happended!" );
427 catch( const Exception& )
429 DBG_UNHANDLED_EXCEPTION();
433 if ( !bSuccess )
435 // okay, this object could not be deleted (or the user did not want to delete it),
436 // but continue with the rest
437 aDeleteNames.erase( aThisRound );
438 --nObjectsLeft;
443 // -----------------------------------------------------------------------------
444 void OApplicationController::deleteEntries()
446 ::vos::OGuard aSolarGuard(Application::GetSolarMutex());
447 ::osl::MutexGuard aGuard( getMutex() );
449 if ( getContainer() )
451 ::std::vector< ::rtl::OUString> aList;
452 getSelectionElementNames(aList);
453 ElementType eType = getContainer()->getElementType();
454 switch(eType)
456 case E_TABLE:
457 deleteTables(aList);
458 break;
459 case E_QUERY:
460 deleteObjects( E_QUERY, aList, true );
461 break;
462 case E_FORM:
463 deleteObjects( E_FORM, aList, true );
464 break;
465 case E_REPORT:
466 deleteObjects( E_REPORT, aList, true );
467 break;
468 case E_NONE:
469 break;
473 // -----------------------------------------------------------------------------
474 const SharedConnection& OApplicationController::ensureConnection( ::dbtools::SQLExceptionInfo* _pErrorInfo )
476 ::vos::OGuard aSolarGuard(Application::GetSolarMutex());
477 ::osl::MutexGuard aGuard( getMutex() );
479 if ( !m_xDataSourceConnection.is() )
481 WaitObject aWO(getView());
482 String sConnectingContext( ModuleRes( STR_COULDNOTCONNECT_DATASOURCE ) );
483 sConnectingContext.SearchAndReplaceAscii("$name$", getStrippedDatabaseName());
485 m_xDataSourceConnection.reset( connect( getDatabaseName(), sConnectingContext, _pErrorInfo ) );
486 if ( m_xDataSourceConnection.is() )
488 SQLExceptionInfo aError;
491 m_xMetaData = m_xDataSourceConnection->getMetaData();
493 catch( const SQLException& )
495 aError = ::cppu::getCaughtException();
497 catch( const Exception& )
499 DBG_UNHANDLED_EXCEPTION();
501 if ( aError.isValid() )
503 if ( _pErrorInfo )
505 *_pErrorInfo = aError;
507 else
509 showError( aError );
514 return m_xDataSourceConnection;
516 // -----------------------------------------------------------------------------
517 sal_Bool OApplicationController::isDataSourceReadOnly() const
519 Reference<XStorable> xStore(m_xModel,UNO_QUERY);
520 return !xStore.is() || xStore->isReadonly();
522 // -----------------------------------------------------------------------------
523 sal_Bool OApplicationController::isConnectionReadOnly() const
525 sal_Bool bIsConnectionReadOnly = sal_True;
526 if ( m_xMetaData.is() )
530 bIsConnectionReadOnly = m_xMetaData->isReadOnly();
532 catch(const SQLException&)
534 DBG_UNHANDLED_EXCEPTION();
537 // TODO check configuration
538 return bIsConnectionReadOnly;
540 // -----------------------------------------------------------------------------
541 Reference< XNameAccess > OApplicationController::getElements( ElementType _eType )
543 Reference< XNameAccess > xElements;
546 switch ( _eType )
548 case E_REPORT:
550 Reference< XReportDocumentsSupplier > xSupp( m_xModel, UNO_QUERY_THROW );
551 xElements.set( xSupp->getReportDocuments(), UNO_SET_THROW );
553 break;
555 case E_FORM:
557 Reference< XFormDocumentsSupplier > xSupp( m_xModel, UNO_QUERY_THROW );
558 xElements.set( xSupp->getFormDocuments(), UNO_SET_THROW );
560 break;
562 case E_QUERY:
564 xElements.set( getQueryDefintions(), UNO_QUERY_THROW );
566 break;
568 case E_TABLE:
570 if ( m_xDataSourceConnection.is() )
572 Reference< XTablesSupplier > xSup( getConnection(), UNO_QUERY_THROW );
573 xElements.set( xSup->getTables(), UNO_SET_THROW );
576 break;
578 default:
579 break;
582 catch(const Exception&)
584 DBG_UNHANDLED_EXCEPTION();
587 return xElements;
589 // -----------------------------------------------------------------------------
590 void OApplicationController::getSelectionElementNames(::std::vector< ::rtl::OUString>& _rNames) const
592 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
593 ::osl::MutexGuard aGuard( getMutex() );
595 OSL_ENSURE(getContainer(),"View isn't valid! -> GPF");
597 getContainer()->getSelectionElementNames( _rNames );
600 // -----------------------------------------------------------------------------
601 ::std::auto_ptr< OLinkedDocumentsAccess > OApplicationController::getDocumentsAccess( ElementType _eType )
603 OSL_ENSURE( ( _eType == E_TABLE ) || ( _eType == E_QUERY ) || ( _eType == E_FORM ) || ( _eType == E_REPORT ),
604 "OApplicationController::getDocumentsAccess: only forms and reports are supported here!" );
606 SharedConnection xConnection( ensureConnection() );
607 Reference< XNameAccess > xDocContainer;
609 if ( ( _eType == E_FORM ) | ( _eType == E_REPORT ) )
611 xDocContainer.set( getElements( _eType ) );
612 OSL_ENSURE( xDocContainer.is(), "OApplicationController::getDocumentsAccess: invalid container!" );
615 ::std::auto_ptr< OLinkedDocumentsAccess > pDocuments( new OLinkedDocumentsAccess(
616 getView(), m_aCurrentFrame.getFrame(), getORB(), xDocContainer, xConnection, getDatabaseName()
617 ) );
618 return pDocuments;
620 // -----------------------------------------------------------------------------
621 TransferableHelper* OApplicationController::copyObject()
625 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
626 ::osl::MutexGuard aGuard( getMutex() );
628 ElementType eType = getContainer()->getElementType();
629 TransferableHelper* pData = NULL;
630 switch( eType )
632 case E_TABLE:
633 case E_QUERY:
635 SharedConnection xConnection( ensureConnection() );
636 Reference< XDatabaseMetaData> xMetaData;
637 if ( xConnection.is() )
638 xMetaData = xConnection->getMetaData();
640 ::rtl::OUString sName = getContainer()->getQualifiedName( NULL );
641 if ( sName.getLength() )
643 ::rtl::OUString sDataSource = getDatabaseName();
645 if ( eType == E_TABLE )
647 pData = new ODataClipboard(sDataSource, CommandType::TABLE, sName, xConnection, getNumberFormatter(xConnection,getORB()), getORB());
649 else
651 pData = new ODataClipboard(sDataSource, CommandType::QUERY, sName, getNumberFormatter(xConnection,getORB()), getORB());
655 break;
656 case E_FORM:
657 case E_REPORT:
659 ::std::vector< ::rtl::OUString> aList;
660 getSelectionElementNames(aList);
661 Reference< XHierarchicalNameAccess > xElements(getElements(eType),UNO_QUERY);
662 if ( xElements.is() && !aList.empty() )
664 Reference< XContent> xContent(xElements->getByHierarchicalName(*aList.begin()),UNO_QUERY);
665 pData = new OComponentTransferable( getDatabaseName(), xContent );
668 break;
669 default:
670 break;
673 // the owner ship goes to ODataClipboards
674 return pData;
676 catch(const SQLException&)
678 showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
680 catch( const Exception& )
682 DBG_UNHANDLED_EXCEPTION();
684 return NULL;
686 // -----------------------------------------------------------------------------
687 sal_Bool OApplicationController::paste( ElementType _eType,const ::svx::ODataAccessDescriptor& _rPasteData,const String& _sParentFolder ,sal_Bool _bMove)
691 if ( _eType == E_QUERY )
693 sal_Int32 nCommandType = CommandType::TABLE;
694 if ( _rPasteData.has(daCommandType) )
695 _rPasteData[daCommandType] >>= nCommandType;
697 if ( CommandType::QUERY == nCommandType || CommandType::COMMAND == nCommandType )
699 // read all nescessary data
701 ::rtl::OUString sCommand;
702 sal_Bool bEscapeProcessing = sal_True;
704 _rPasteData[daCommand] >>= sCommand;
705 if ( _rPasteData.has(daEscapeProcessing) )
706 _rPasteData[daEscapeProcessing] >>= bEscapeProcessing;
708 // plausibility check
709 sal_Bool bValidDescriptor = sal_False;
710 ::rtl::OUString sDataSourceName = _rPasteData.getDataSource();
711 if (CommandType::QUERY == nCommandType)
712 bValidDescriptor = sDataSourceName.getLength() && sCommand.getLength();
713 else if (CommandType::COMMAND == nCommandType)
714 bValidDescriptor = (0 != sCommand.getLength());
715 if (!bValidDescriptor)
717 DBG_ERROR("OApplicationController::paste: invalid descriptor!");
718 return sal_False;
721 // the target object name (as we'll suggest it to the user)
722 ::rtl::OUString sTargetName;
725 if ( CommandType::QUERY == nCommandType )
726 sTargetName = sCommand;
728 if ( !sTargetName.getLength() )
730 String sDefaultName = String( ModuleRes( STR_QRY_TITLE ) );
731 sDefaultName = sDefaultName.GetToken( 0, ' ' );
733 Reference< XNameAccess > xQueries( getQueryDefintions(), UNO_QUERY_THROW );
734 sTargetName = ::dbtools::createUniqueName( xQueries, sDefaultName, sal_False );
737 catch(const Exception&)
739 DBG_UNHANDLED_EXCEPTION();
742 Reference< XPropertySet > xQuery;
743 if (CommandType::QUERY == nCommandType)
745 // need to extract the statement and the escape processing flag from the query object
746 sal_Bool bSuccess = sal_False;
749 // the concrete query
750 Reference< XQueryDefinitionsSupplier > xSourceQuerySup(
751 getDataSourceByName( sDataSourceName, getView(), getORB(), NULL ),
752 UNO_QUERY_THROW );
753 Reference< XNameAccess > xQueries( xSourceQuerySup->getQueryDefinitions(), UNO_SET_THROW );
754 if ( xQueries->hasByName( sCommand ) )
756 xQuery.set( xQueries->getByName(sCommand), UNO_QUERY_THROW );
757 bSuccess = true;
760 catch(SQLException&) { throw; } // caught and handled by the outer catch
761 catch( const Exception& )
763 DBG_UNHANDLED_EXCEPTION();
766 if (!bSuccess)
768 DBG_ERROR("OApplicationController::paste: could not extract the source query object!");
769 // TODO: maybe this is worth an error message to be displayed to the user ....
770 return sal_False;
775 Reference< XNameContainer > xDestQueries(getQueryDefintions(), UNO_QUERY);
776 Reference< XSingleServiceFactory > xQueryFactory(xDestQueries, UNO_QUERY);
777 if (!xQueryFactory.is())
779 DBG_ERROR("OApplicationController::paste: invalid destination query container!");
780 return sal_False;
783 // here we have everything needed to create a new query object ...
784 // ... ehm, except a new name
785 ensureConnection();
787 DynamicTableOrQueryNameCheck aNameChecker( getConnection(), CommandType::QUERY );
788 ::dbtools::SQLExceptionInfo aDummy;
789 bool bNeedAskForName = ( sCommand.getLength() == 0 )
790 /* we did not have a source name, so the target name was auto-generated */
791 || ( !aNameChecker.isNameValid( sTargetName, aDummy ) );
792 /* name is invalid in the target DB (e.g. because it already
793 has a /table/ with that name) */
794 if ( bNeedAskForName )
796 OSaveAsDlg aAskForName( getView(),
797 CommandType::QUERY,
798 getORB(),
799 getConnection(),
800 sTargetName,
801 aNameChecker,
802 SAD_ADDITIONAL_DESCRIPTION | SAD_TITLE_PASTE_AS);
803 if ( RET_OK != aAskForName.Execute() )
804 // cancelled by the user
805 return sal_False;
806 sTargetName = aAskForName.getName();
809 // create a new object
810 Reference< XPropertySet > xNewQuery(xQueryFactory->createInstance(), UNO_QUERY);
811 DBG_ASSERT(xNewQuery.is(), "OApplicationController::paste: invalid object created by factory!");
812 if (xNewQuery.is())
814 // initialize
815 if ( xQuery.is() )
816 ::comphelper::copyProperties(xQuery,xNewQuery);
817 else
819 xNewQuery->setPropertyValue(PROPERTY_COMMAND,makeAny(sCommand));
820 xNewQuery->setPropertyValue(PROPERTY_ESCAPE_PROCESSING,makeAny(bEscapeProcessing));
822 // insert
823 xDestQueries->insertByName( sTargetName, makeAny(xNewQuery) );
824 xNewQuery.set(xDestQueries->getByName( sTargetName),UNO_QUERY);
825 if ( xQuery.is() && xNewQuery.is() )
827 Reference<XColumnsSupplier> xSrcColSup(xQuery,UNO_QUERY);
828 Reference<XColumnsSupplier> xDstColSup(xNewQuery,UNO_QUERY);
829 if ( xSrcColSup.is() && xDstColSup.is() )
831 Reference<XNameAccess> xSrcNameAccess = xSrcColSup->getColumns();
832 Reference<XNameAccess> xDstNameAccess = xDstColSup->getColumns();
833 Reference<XDataDescriptorFactory> xFac(xDstNameAccess,UNO_QUERY);
834 Reference<XAppend> xAppend(xFac,UNO_QUERY);
835 if ( xSrcNameAccess.is() && xDstNameAccess.is() && xSrcNameAccess->hasElements() && xAppend.is() )
837 Reference<XPropertySet> xDstProp(xFac->createDataDescriptor());
839 Sequence< ::rtl::OUString> aSeq = xSrcNameAccess->getElementNames();
840 const ::rtl::OUString* pIter = aSeq.getConstArray();
841 const ::rtl::OUString* pEnd = pIter + aSeq.getLength();
842 for( ; pIter != pEnd ; ++pIter)
844 Reference<XPropertySet> xSrcProp(xSrcNameAccess->getByName(*pIter),UNO_QUERY);
845 ::comphelper::copyProperties(xSrcProp,xDstProp);
846 xAppend->appendByDescriptor(xDstProp);
853 else
854 OSL_TRACE("There should be a sequence in it!");
855 return sal_True;
857 else if ( _rPasteData.has(daComponent) ) // forms or reports
859 Reference<XContent> xContent;
860 _rPasteData[daComponent] >>= xContent;
861 return insertHierachyElement(_eType,_sParentFolder,Reference<XNameAccess>(xContent,UNO_QUERY).is(),xContent,_bMove);
864 catch(const SQLException&) { showError( SQLExceptionInfo( ::cppu::getCaughtException() ) ); }
865 catch(const Exception& )
867 DBG_UNHANDLED_EXCEPTION();
869 return sal_False;
871 // -----------------------------------------------------------------------------
872 Reference<XNameContainer> OApplicationController::getQueryDefintions() const
874 Reference<XQueryDefinitionsSupplier> xSet(m_xDataSource,UNO_QUERY);
875 Reference<XNameContainer> xNames;
876 if ( xSet.is() )
878 xNames.set(xSet->getQueryDefinitions(),UNO_QUERY);
880 return xNames;
882 // -----------------------------------------------------------------------------
883 void OApplicationController::getSupportedFormats(ElementType _eType,::std::vector<SotFormatStringId>& _rFormatIds) const
885 switch( _eType )
887 case E_TABLE:
888 _rFormatIds.push_back(SOT_FORMATSTR_ID_DBACCESS_TABLE);
889 _rFormatIds.push_back(SOT_FORMAT_RTF);
890 _rFormatIds.push_back(SOT_FORMATSTR_ID_HTML);
891 // run through
892 case E_QUERY:
893 _rFormatIds.push_back(SOT_FORMATSTR_ID_DBACCESS_QUERY);
894 break;
895 default:
896 break;
899 // -----------------------------------------------------------------------------
900 sal_Bool OApplicationController::isTableFormat() const
902 return m_aTableCopyHelper.isTableFormat(getViewClipboard());
904 // -----------------------------------------------------------------------------
905 sal_Bool OApplicationController::copyTagTable(OTableCopyHelper::DropDescriptor& _rDesc, sal_Bool _bCheck)
907 // first get the dest connection
908 ::osl::MutexGuard aGuard( getMutex() );
910 SharedConnection xConnection( ensureConnection() );
911 if ( !xConnection.is() )
912 return sal_False;
914 return m_aTableCopyHelper.copyTagTable( _rDesc, _bCheck, xConnection );
916 // -----------------------------------------------------------------------------
917 IMPL_LINK( OApplicationController, OnAsyncDrop, void*, /*NOTINTERESTEDIN*/ )
919 m_nAsyncDrop = 0;
920 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
921 ::osl::MutexGuard aGuard( getMutex() );
924 if ( m_aAsyncDrop.nType == E_TABLE )
926 SharedConnection xConnection( ensureConnection() );
927 if ( xConnection.is() )
928 m_aTableCopyHelper.asyncCopyTagTable( m_aAsyncDrop, getDatabaseName(), xConnection );
930 else
932 if ( paste(m_aAsyncDrop.nType,m_aAsyncDrop.aDroppedData,m_aAsyncDrop.aUrl,m_aAsyncDrop.nAction == DND_ACTION_MOVE)
933 && m_aAsyncDrop.nAction == DND_ACTION_MOVE )
935 Reference<XContent> xContent;
936 m_aAsyncDrop.aDroppedData[daComponent] >>= xContent;
937 ::std::vector< ::rtl::OUString> aList;
938 sal_Int32 nIndex = 0;
939 ::rtl::OUString sName = xContent->getIdentifier()->getContentIdentifier();
940 ::rtl::OUString sErase = sName.getToken(0,'/',nIndex); // we don't want to have the "private:forms" part
941 if ( nIndex != -1 )
943 aList.push_back(sName.copy(sErase.getLength() + 1));
944 deleteObjects( m_aAsyncDrop.nType, aList, false );
949 m_aAsyncDrop.aDroppedData.clear();
951 return 0L;
953 //........................................................................
954 } // namespace dbaui
955 //........................................................................