1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dbaccess.hxx"
30 
31 #ifndef DBAUI_APPCONTROLLER_HXX
32 #include "AppController.hxx"
33 #endif
34 #ifndef _COMPHELPER_SEQUENCE_HXX_
35 #include <comphelper/sequence.hxx>
36 #endif
37 #ifndef _COMPHELPER_PROPERTY_HXX_
38 #include <comphelper/property.hxx>
39 #endif
40 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
41 #include "dbustrings.hrc"
42 #endif
43 #ifndef _COM_SUN_STAR_SDBCX_XDATADESCRIPTORFACTORY_HPP_
44 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
45 #endif
46 #ifndef _COM_SUN_STAR_SDBCX_XAPPEND_HPP_
47 #include <com/sun/star/sdbcx/XAppend.hpp>
48 #endif
49 #ifndef _COM_SUN_STAR_SDBCX_XCOLUMNSSUPPLIER_HPP_
50 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
51 #endif
52 #ifndef _COM_SUN_STAR_SDB_XSINGLESELECTQUERYCOMPOSER_HPP_
53 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
54 #endif
55 #ifndef _COM_SUN_STAR_CONTAINER_XNAMECONTAINER_HPP_
56 #include <com/sun/star/container/XNameContainer.hpp>
57 #endif
58 #ifndef _COM_SUN_STAR_UNO_XNAMINGSERVICE_HPP_
59 #include <com/sun/star/uno/XNamingService.hpp>
60 #endif
61 #ifndef _COM_SUN_STAR_SDBC_XDATASOURCE_HPP_
62 #include <com/sun/star/sdbc/XDataSource.hpp>
63 #endif
64 #ifndef _COM_SUN_STAR_FRAME_XSTORABLE_HPP_
65 #include <com/sun/star/frame/XStorable.hpp>
66 #endif
67 #ifndef _COM_SUN_STAR_CONTAINER_XCHILD_HPP_
68 #include <com/sun/star/container/XChild.hpp>
69 #endif
70 #ifndef _COM_SUN_STAR_CONTAINER_XHIERARCHICALNAMECONTAINER_HPP_
71 #include <com/sun/star/container/XHierarchicalNameContainer.hpp>
72 #endif
73 #ifndef _COM_SUN_STAR_SDBC_DATATYPE_HPP_
74 #include <com/sun/star/sdbc/DataType.hpp>
75 #endif
76 #ifndef _COM_SUN_STAR_SDB_COMMANDTYPE_HPP_
77 #include <com/sun/star/sdb/CommandType.hpp>
78 #endif
79 #ifndef _COM_SUN_STAR_SDB_XBOOKMARKSSUPPLIER_HPP_
80 #include <com/sun/star/sdb/XBookmarksSupplier.hpp>
81 #endif
82 #ifndef _COM_SUN_STAR_SDB_SQLCONTEXT_HPP_
83 #include <com/sun/star/sdb/SQLContext.hpp>
84 #endif
85 #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
86 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
87 #endif
88 #ifndef _COM_SUN_STAR_SDBCX_XVIEWSSUPPLIER_HPP_
89 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
90 #endif
91 #ifndef _COM_SUN_STAR_SDB_XQUERYDEFINITIONSSUPPLIER_HPP_
92 #include <com/sun/star/sdb/XQueryDefinitionsSupplier.hpp>
93 #endif
94 #ifndef _COM_SUN_STAR_SDBCX_XDROP_HPP_
95 #include <com/sun/star/sdbcx/XDrop.hpp>
96 #endif
97 #ifndef _TOOLS_DEBUG_HXX
98 #include <tools/debug.hxx>
99 #endif
100 #ifndef _URLOBJ_HXX
101 #include <tools/urlobj.hxx>
102 #endif
103 #ifndef _UNOTOOLS_UCBHELPER_HXX
104 #include <unotools/ucbhelper.hxx>
105 #endif
106 #ifndef DBAUI_DLGSAVE_HXX
107 #include "dlgsave.hxx"
108 #endif
109 #ifndef _COMPHELPER_TYPES_HXX_
110 #include <comphelper/types.hxx>
111 #endif
112 #ifndef _SV_MSGBOX_HXX
113 #include <vcl/msgbox.hxx>
114 #endif
115 #ifndef _CPPUHELPER_TYPEPROVIDER_HXX_
116 #include <cppuhelper/typeprovider.hxx>
117 #endif
118 #ifndef _CPPUHELPER_EXC_HLP_HXX_
119 #include <cppuhelper/exc_hlp.hxx>
120 #endif
121 #ifndef _DBHELPER_DBEXCEPTION_HXX_
122 #include <connectivity/dbexception.hxx>
123 #endif
124 #ifndef _SV_WAITOBJ_HXX
125 #include <vcl/waitobj.hxx>
126 #endif
127 #ifndef _RTL_USTRBUF_HXX_
128 #include <rtl/ustrbuf.hxx>
129 #endif
130 #ifndef DBAUI_APPVIEW_HXX
131 #include "AppView.hxx"
132 #endif
133 #ifndef _SVX_DATACCESSDESCRIPTOR_HXX_
134 #include <svx/dataaccessdescriptor.hxx>
135 #endif
136 #ifndef SVX_DBAOBJECTEX_HXX
137 #include <svx/dbaobjectex.hxx>
138 #endif
139 #ifndef DBACCESS_UI_BROWSER_ID_HXX
140 #include "browserids.hxx"
141 #endif
142 #ifndef _DBAU_REGHELPER_HXX_
143 #include "dbu_reghelper.hxx"
144 #endif
145 #ifndef _DBU_APP_HRC_
146 #include "dbu_app.hrc"
147 #endif
148 #ifndef _SV_MENU_HXX
149 #include <vcl/menu.hxx>
150 #endif
151 #ifndef _COMPHELPER_UNO3_HXX_
152 #include <comphelper/uno3.hxx>
153 #endif
154 #ifndef _SV_SVAPP_HXX //autogen
155 #include <vcl/svapp.hxx>
156 #endif
157 #ifndef _SVLBOXITM_HXX
158 #include <svtools/svlbitm.hxx>
159 #endif
160 #ifndef _DBAUI_LISTVIEWITEMS_HXX_
161 #include "listviewitems.hxx"
162 #endif
163 #ifndef DBAUI_APPDETAILVIEW_HXX
164 #include "AppDetailView.hxx"
165 #endif
166 #ifndef _DBAUI_LINKEDDOCUMENTS_HXX_
167 #include "linkeddocuments.hxx"
168 #endif
169 #ifndef _SV_LSTBOX_HXX
170 #include <vcl/lstbox.hxx>
171 #endif
172 #ifndef _DBHELPER_DBEXCEPTION_HXX_
173 #include <connectivity/dbexception.hxx>
174 #endif
175 #ifndef _CONNECTIVITY_DBTOOLS_HXX_
176 #include <connectivity/dbtools.hxx>
177 #endif
178 #ifndef _DBAUI_SQLMESSAGE_HXX_
179 #include "sqlmessage.hxx"
180 #endif
181 #ifndef _STRING_HXX
182 #include <tools/string.hxx>
183 #endif
184 #ifndef DBAUI_DBEXCHANGE_HXX
185 #include "dbexchange.hxx"
186 #endif
187 #ifndef DBAUI_TOOLS_HXX
188 #include "UITools.hxx"
189 #endif
190 #include <algorithm>
191 #ifndef _SVTREEBOX_HXX
192 #include <svtools/svtreebx.hxx>
193 #endif
194 #ifndef _COM_SUN_STAR_SDB_XREPORTDOCUMENTSSUPPLIER_HPP_
195 #include <com/sun/star/sdb/XReportDocumentsSupplier.hpp>
196 #endif
197 #ifndef _COM_SUN_STAR_SDB_XFORMDOCUMENTSSUPPLIER_HPP_
198 #include <com/sun/star/sdb/XFormDocumentsSupplier.hpp>
199 #endif
200 #ifndef _FILEDLGHELPER_HXX
201 #include <sfx2/filedlghelper.hxx>
202 #endif
203 #ifndef INCLUDED_SVTOOLS_PATHOPTIONS_HXX
204 #include <unotools/pathoptions.hxx>
205 #endif
206 #ifndef _SFX_DOCFILT_HACK_HXX
207 #include <sfx2/docfilt.hxx>
208 #endif
209 #ifndef _SVT_FILEVIEW_HXX
210 #include <svtools/fileview.hxx>
211 #endif
212 #ifndef TOOLS_DIAGNOSE_EX_H
213 #include <tools/diagnose_ex.h>
214 #endif
215 #ifndef DBACCESS_SOURCE_UI_MISC_DEFAULTOBJECTNAMECHECK_HXX
216 #include "defaultobjectnamecheck.hxx"
217 #endif
218 #ifndef _VOS_MUTEX_HXX_
219 #include <vos/mutex.hxx>
220 #endif
221 #include "subcomponentmanager.hxx"
222 
223 //........................................................................
224 namespace dbaui
225 {
226 //........................................................................
227 using namespace ::dbtools;
228 using namespace ::svx;
229 using namespace ::svtools;
230 using namespace ::com::sun::star::uno;
231 using namespace ::com::sun::star::task;
232 using namespace ::com::sun::star::beans;
233 using namespace ::com::sun::star::lang;
234 using namespace ::com::sun::star::container;
235 using namespace ::com::sun::star::sdb;
236 using namespace ::com::sun::star::sdbc;
237 using namespace ::com::sun::star::sdbcx;
238 using namespace ::com::sun::star::frame;
239 using namespace ::com::sun::star::ucb;
240 using namespace ::com::sun::star::util;
241 
242 // -----------------------------------------------------------------------------
243 void OApplicationController::deleteTables(const ::std::vector< ::rtl::OUString>& _rList)
244 {
245     SharedConnection xConnection( ensureConnection() );
246 
247 	Reference<XTablesSupplier> xSup(xConnection,UNO_QUERY);
248 	OSL_ENSURE(xSup.is(),"OApplicationController::deleteTable: no XTablesSuppier!");
249 	if ( xSup.is() )
250 	{
251 		Reference<XNameAccess> xTables = xSup->getTables();
252 		Reference<XDrop> xDrop(xTables,UNO_QUERY);
253 		if ( xDrop.is() )
254 		{
255 			bool bConfirm = true;
256 			::std::vector< ::rtl::OUString>::const_iterator aEnd = _rList.end();
257 			for (::std::vector< ::rtl::OUString>::const_iterator aIter = _rList.begin(); aIter != aEnd; ++aIter)
258 			{
259 				::rtl::OUString sTableName = *aIter;
260 
261 				sal_Int32 nResult = RET_YES;
262 				if ( bConfirm )
263 					nResult = ::dbaui::askForUserAction(getView(),STR_TITLE_CONFIRM_DELETION ,STR_QUERY_DELETE_TABLE,_rList.size() > 1 && (aIter+1) != _rList.end(),sTableName);
264 
265                 bool bUserConfirmedDelete =
266                             ( RET_YES == nResult )
267                         ||  ( RET_ALL == nResult );
268 				if ( bUserConfirmedDelete && m_pSubComponentManager->closeSubFrames( sTableName, E_TABLE ) )
269 				{
270 					SQLExceptionInfo aErrorInfo;
271 					try
272 					{
273 						if ( xTables->hasByName(sTableName) )
274 							xDrop->dropByName(sTableName);
275 						else
276 						{// could be a view
277 							Reference<XViewsSupplier> xViewsSup(xConnection,UNO_QUERY);
278 
279 							Reference<XNameAccess> xViews;
280 							if ( xViewsSup.is() )
281 							{
282 								xViews = xViewsSup->getViews();
283 								if ( xViews.is() && xViews->hasByName(sTableName) )
284 								{
285 									xDrop.set(xViews,UNO_QUERY);
286 									if ( xDrop.is() )
287 										xDrop->dropByName(sTableName);
288 								}
289 							}
290 						}
291 					}
292 					catch(SQLContext& e) { aErrorInfo = e; }
293 					catch(SQLWarning& e) { aErrorInfo = e; }
294 					catch(SQLException& e) { aErrorInfo = e; }
295 					catch(WrappedTargetException& e)
296 					{
297 						SQLException aSql;
298 						if(e.TargetException >>= aSql)
299 							aErrorInfo = aSql;
300 						else
301 							OSL_ENSURE(sal_False, "OApplicationController::implDropTable: something strange happended!");
302 					}
303 		            catch( const Exception& )
304 		            {
305                         DBG_UNHANDLED_EXCEPTION();
306 		            }
307 
308 					if ( aErrorInfo.isValid() )
309 						showError(aErrorInfo);
310 
311 					if ( RET_ALL == nResult )
312 						bConfirm = false;
313 				}
314 				else
315 					break;
316 			}
317 		}
318 		else
319 		{
320 			String sMessage(ModuleRes(STR_MISSING_TABLES_XDROP));
321 			ErrorBox aError(getView(), WB_OK, sMessage);
322 			aError.Execute();
323 		}
324 	}
325 }
326 // -----------------------------------------------------------------------------
327 void OApplicationController::deleteObjects( ElementType _eType, const ::std::vector< ::rtl::OUString>& _rList, bool _bConfirm )
328 {
329     Reference< XNameContainer > xNames( getElements( _eType ), UNO_QUERY );
330 	Reference< XHierarchicalNameContainer > xHierarchyName( xNames, UNO_QUERY );
331 	if ( xNames.is() )
332 	{
333 		ByteString sDialogPosition;
334 		svtools::QueryDeleteResult_Impl eResult = _bConfirm ? svtools::QUERYDELETE_YES : svtools::QUERYDELETE_ALL;
335 
336         // The list of elements to delete is allowed to contain related elements: A given element may
337         // be the ancestor or child of another element from the list.
338         // We want to ensure that ancestors get deleted first, so we normalize the list in this respect.
339         // #i33353# - 2004-09-27 - fs@openoffice.org
340         ::std::set< ::rtl::OUString > aDeleteNames;
341             // Note that this implicitly uses ::std::less< ::rtl::OUString > a comparison operation, which
342             // results in lexicographical order, which is exactly what we need, because "foo" is *before*
343             // any "foo/bar" in this order.
344         ::std::copy(
345             _rList.begin(), _rList.end(),
346             ::std::insert_iterator< ::std::set< ::rtl::OUString > >( aDeleteNames, aDeleteNames.begin() )
347         );
348 
349         ::std::set< ::rtl::OUString >::size_type nCount = aDeleteNames.size();
350         for ( ::std::set< ::rtl::OUString >::size_type nObjectsLeft = nCount; !aDeleteNames.empty(); )
351 		{
352             ::std::set< ::rtl::OUString >::iterator  aThisRound = aDeleteNames.begin();
353 
354 			if ( eResult != svtools::QUERYDELETE_ALL )
355 			{
356 				svtools::QueryDeleteDlg_Impl aDlg( getView(), *aThisRound );
357 
358                 if ( sDialogPosition.Len() )
359 					aDlg.SetWindowState( sDialogPosition );
360 
361                 if ( nObjectsLeft > 1 )
362 					aDlg.EnableAllButton();
363 
364                 if ( aDlg.Execute() == RET_OK )
365 					eResult = aDlg.GetResult();
366 				else
367 					return;
368 
369 				sDialogPosition = aDlg.GetWindowState( );
370 			}
371 
372             bool bSuccess = false;
373 
374             bool bUserConfirmedDelete =
375                         ( eResult == svtools::QUERYDELETE_ALL )
376                     ||  ( eResult == svtools::QUERYDELETE_YES );
377 
378             if  (   bUserConfirmedDelete
379                 &&  (   ( _eType == E_QUERY ) ? m_pSubComponentManager->closeSubFrames( *aThisRound, _eType ) : true )
380                 )
381 			{
382 				try
383 				{
384 					if ( xHierarchyName.is() )
385 						xHierarchyName->removeByHierarchicalName( *aThisRound );
386 					else
387 						xNames->removeByName( *aThisRound );
388 
389                     bSuccess = true;
390 
391                     // now that we removed the element, care for all it's child elements
392                     // which may also be a part of the list
393                     // #i33353# - 2004-09-27 - fs@openoffice.org
394                     OSL_ENSURE( aThisRound->getLength() - 1 >= 0, "OApplicationController::deleteObjects: empty name?" );
395 					::rtl::OUStringBuffer sSmallestSiblingName( *aThisRound );
396                     sSmallestSiblingName.append( (sal_Unicode)( '/' + 1) );
397 
398                     ::std::set< ::rtl::OUString >::iterator aUpperChildrenBound = aDeleteNames.lower_bound( sSmallestSiblingName.makeStringAndClear() );
399                     for ( ::std::set< ::rtl::OUString >::iterator aObsolete = aThisRound;
400                           aObsolete != aUpperChildrenBound;
401                         )
402                     {
403 #if OSL_DEBUG_LEVEL > 0
404                         ::rtl::OUString sObsoleteName = *aObsolete;
405 #endif
406                         ::std::set< ::rtl::OUString >::iterator aNextObsolete = aObsolete; ++aNextObsolete;
407                         aDeleteNames.erase( aObsolete );
408                         --nObjectsLeft;
409                         aObsolete = aNextObsolete;
410                     }
411 				}
412 				catch(const SQLException&)
413 				{
414                     showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
415 				}
416 				catch(WrappedTargetException& e)
417 				{
418 					SQLException aSql;
419 					if ( e.TargetException >>= aSql )
420 						showError( SQLExceptionInfo( e.TargetException ) );
421 					else
422 						OSL_ENSURE( sal_False, "OApplicationController::deleteObjects: something strange happended!" );
423 				}
424                 catch( const Exception& )
425                 {
426                     DBG_UNHANDLED_EXCEPTION();
427                 }
428 			}
429 
430             if ( !bSuccess )
431             {
432                 // okay, this object could not be deleted (or the user did not want to delete it),
433                 // but continue with the rest
434                 aDeleteNames.erase( aThisRound );
435                 --nObjectsLeft;
436             }
437 		}
438 	}
439 }
440 // -----------------------------------------------------------------------------
441 void OApplicationController::deleteEntries()
442 {
443 	::vos::OGuard aSolarGuard(Application::GetSolarMutex());
444 	::osl::MutexGuard aGuard( getMutex() );
445 
446 	if ( getContainer() )
447 	{
448 		::std::vector< ::rtl::OUString> aList;
449 		getSelectionElementNames(aList);
450 		ElementType eType = getContainer()->getElementType();
451 		switch(eType)
452 		{
453 		case E_TABLE:
454 			deleteTables(aList);
455 			break;
456 		case E_QUERY:
457 			deleteObjects( E_QUERY, aList, true );
458 			break;
459 		case E_FORM:
460 			deleteObjects( E_FORM, aList, true );
461 			break;
462 		case E_REPORT:
463 			deleteObjects( E_REPORT, aList, true );
464 			break;
465         case E_NONE:
466             break;
467 		}
468 	}
469 }
470 // -----------------------------------------------------------------------------
471 const SharedConnection& OApplicationController::ensureConnection( ::dbtools::SQLExceptionInfo* _pErrorInfo )
472 {
473 	::vos::OGuard aSolarGuard(Application::GetSolarMutex());
474 	::osl::MutexGuard aGuard( getMutex() );
475 
476     if ( !m_xDataSourceConnection.is() )
477     {
478 		WaitObject aWO(getView());
479 		String sConnectingContext( ModuleRes( STR_COULDNOTCONNECT_DATASOURCE ) );
480 		sConnectingContext.SearchAndReplaceAscii("$name$", getStrippedDatabaseName());
481 
482 		m_xDataSourceConnection.reset( connect( getDatabaseName(), sConnectingContext, _pErrorInfo ) );
483         if ( m_xDataSourceConnection.is() )
484         {
485             SQLExceptionInfo aError;
486             try
487             {
488                 m_xMetaData = m_xDataSourceConnection->getMetaData();
489             }
490             catch( const SQLException& )
491             {
492                 aError = ::cppu::getCaughtException();
493             }
494             catch( const Exception& )
495             {
496             	DBG_UNHANDLED_EXCEPTION();
497             }
498             if ( aError.isValid() )
499             {
500                 if ( _pErrorInfo )
501                 {
502                     *_pErrorInfo = aError;
503                 }
504                 else
505                 {
506                     showError( aError );
507                 }
508             }
509         }
510 	}
511     return m_xDataSourceConnection;
512 }
513 // -----------------------------------------------------------------------------
514 sal_Bool OApplicationController::isDataSourceReadOnly() const
515 {
516 	Reference<XStorable> xStore(m_xModel,UNO_QUERY);
517 	return !xStore.is() || xStore->isReadonly();
518 }
519 // -----------------------------------------------------------------------------
520 sal_Bool OApplicationController::isConnectionReadOnly() const
521 {
522 	sal_Bool bIsConnectionReadOnly = sal_True;
523 	if ( m_xMetaData.is() )
524 	{
525 		try
526 		{
527 			bIsConnectionReadOnly = m_xMetaData->isReadOnly();
528 		}
529 		catch(const SQLException&)
530 		{
531             DBG_UNHANDLED_EXCEPTION();
532 		}
533 	}
534 	// TODO check configuration
535 	return bIsConnectionReadOnly;
536 }
537 // -----------------------------------------------------------------------------
538 Reference< XNameAccess > OApplicationController::getElements( ElementType _eType )
539 {
540 	Reference< XNameAccess > xElements;
541 	try
542 	{
543 		switch ( _eType )
544 		{
545 		case E_REPORT:
546 		{
547 			Reference< XReportDocumentsSupplier > xSupp( m_xModel, UNO_QUERY_THROW );
548             xElements.set( xSupp->getReportDocuments(), UNO_SET_THROW );
549 		}
550 		break;
551 
552         case E_FORM:
553 		{
554 			Reference< XFormDocumentsSupplier > xSupp( m_xModel, UNO_QUERY_THROW );
555             xElements.set( xSupp->getFormDocuments(), UNO_SET_THROW );
556 		}
557 		break;
558 
559         case E_QUERY:
560 		{
561 			xElements.set( getQueryDefintions(), UNO_QUERY_THROW );
562 		}
563 		break;
564 
565 		case E_TABLE:
566 		{
567             if ( m_xDataSourceConnection.is() )
568 			{
569 				Reference< XTablesSupplier > xSup( getConnection(), UNO_QUERY_THROW );
570 				xElements.set( xSup->getTables(), UNO_SET_THROW );
571 			}
572 		}
573 		break;
574 
575         default:
576             break;
577 		}
578 	}
579 	catch(const Exception&)
580 	{
581         DBG_UNHANDLED_EXCEPTION();
582 	}
583 
584 	return xElements;
585 }
586 // -----------------------------------------------------------------------------
587 void OApplicationController::getSelectionElementNames(::std::vector< ::rtl::OUString>& _rNames) const
588 {
589 	::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
590 	::osl::MutexGuard aGuard( getMutex() );
591 
592 	OSL_ENSURE(getContainer(),"View isn't valid! -> GPF");
593 
594 	getContainer()->getSelectionElementNames( _rNames );
595 }
596 
597 // -----------------------------------------------------------------------------
598 ::std::auto_ptr< OLinkedDocumentsAccess > OApplicationController::getDocumentsAccess( ElementType _eType )
599 {
600     OSL_ENSURE( ( _eType == E_TABLE ) || ( _eType == E_QUERY ) || ( _eType == E_FORM ) || ( _eType == E_REPORT ),
601         "OApplicationController::getDocumentsAccess: only forms and reports are supported here!" );
602 
603     SharedConnection xConnection( ensureConnection() );
604     Reference< XNameAccess > xDocContainer;
605 
606     if ( ( _eType == E_FORM ) | ( _eType == E_REPORT ) )
607     {
608         xDocContainer.set( getElements( _eType ) );
609         OSL_ENSURE( xDocContainer.is(), "OApplicationController::getDocumentsAccess: invalid container!" );
610     }
611 
612     ::std::auto_ptr< OLinkedDocumentsAccess > pDocuments( new OLinkedDocumentsAccess(
613         getView(), this, getORB(), xDocContainer, xConnection, getDatabaseName()
614     ) );
615     return pDocuments;
616 }
617 // -----------------------------------------------------------------------------
618 TransferableHelper* OApplicationController::copyObject()
619 {
620 	try
621 	{
622 		::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
623 		::osl::MutexGuard aGuard( getMutex() );
624 
625 		ElementType eType = getContainer()->getElementType();
626 		TransferableHelper* pData = NULL;
627 		switch( eType )
628 		{
629 			case E_TABLE:
630 			case E_QUERY:
631 			{
632                 SharedConnection xConnection( ensureConnection() );
633 				Reference< XDatabaseMetaData> xMetaData;
634 				if ( xConnection.is() )
635 					xMetaData = xConnection->getMetaData();
636 
637 				::rtl::OUString sName = getContainer()->getQualifiedName( NULL );
638                 if ( sName.getLength() )
639                 {
640 				    ::rtl::OUString sDataSource	= getDatabaseName();
641 
642 				    if ( eType == E_TABLE )
643 				    {
644 					    pData = new ODataClipboard(sDataSource, CommandType::TABLE, sName, xConnection, getNumberFormatter(xConnection,getORB()), getORB());
645 				    }
646 				    else
647 				    {
648 					    pData = new ODataClipboard(sDataSource, CommandType::QUERY, sName, getNumberFormatter(xConnection,getORB()), getORB());
649 				    }
650                 }
651 			}
652 				break;
653 			case E_FORM:
654 			case E_REPORT:
655 			{
656 				::std::vector< ::rtl::OUString> aList;
657 				getSelectionElementNames(aList);
658 				Reference< XHierarchicalNameAccess > xElements(getElements(eType),UNO_QUERY);
659 				if ( xElements.is() && !aList.empty() )
660 				{
661 					Reference< XContent> xContent(xElements->getByHierarchicalName(*aList.begin()),UNO_QUERY);
662 					pData = new OComponentTransferable( getDatabaseName(), xContent );
663 				}
664 			}
665 			break;
666             default:
667                 break;
668 		}
669 
670 		// the owner ship goes to ODataClipboards
671 		return pData;
672 	}
673     catch(const SQLException&)
674     {
675         showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
676     }
677 	catch( const Exception& )
678 	{
679         DBG_UNHANDLED_EXCEPTION();
680 	}
681 	return NULL;
682 }
683 // -----------------------------------------------------------------------------
684 sal_Bool OApplicationController::paste( ElementType _eType,const ::svx::ODataAccessDescriptor& _rPasteData,const String& _sParentFolder ,sal_Bool _bMove)
685 {
686 	try
687 	{
688 		if ( _eType == E_QUERY )
689 		{
690 			sal_Int32 nCommandType = CommandType::TABLE;
691 			if ( _rPasteData.has(daCommandType) )
692 				_rPasteData[daCommandType]		>>= nCommandType;
693 
694 			if ( CommandType::QUERY == nCommandType || CommandType::COMMAND == nCommandType )
695 			{
696 				// read all nescessary data
697 
698 				::rtl::OUString	sCommand;
699 				sal_Bool bEscapeProcessing = sal_True;
700 
701 				_rPasteData[daCommand] >>= sCommand;
702 				if ( _rPasteData.has(daEscapeProcessing) )
703 					_rPasteData[daEscapeProcessing]	>>= bEscapeProcessing;
704 
705 				// plausibility check
706 				sal_Bool bValidDescriptor = sal_False;
707                 ::rtl::OUString	sDataSourceName = _rPasteData.getDataSource();
708 				if (CommandType::QUERY == nCommandType)
709 					bValidDescriptor = sDataSourceName.getLength() && sCommand.getLength();
710 				else if (CommandType::COMMAND == nCommandType)
711 					bValidDescriptor = (0 != sCommand.getLength());
712 				if (!bValidDescriptor)
713 				{
714 					DBG_ERROR("OApplicationController::paste: invalid descriptor!");
715 					return sal_False;
716 				}
717 
718 				// the target object name (as we'll suggest it to the user)
719                 ::rtl::OUString sTargetName;
720 				try
721 				{
722 				    if ( CommandType::QUERY == nCommandType )
723                         sTargetName = sCommand;
724 
725                     if ( !sTargetName.getLength() )
726                     {
727                         String sDefaultName = String( ModuleRes( STR_QRY_TITLE ) );
728                         sDefaultName = sDefaultName.GetToken( 0, ' ' );
729 
730                         Reference< XNameAccess > xQueries( getQueryDefintions(), UNO_QUERY_THROW );
731 				        sTargetName = ::dbtools::createUniqueName( xQueries, sDefaultName, sal_False );
732                     }
733 				}
734 				catch(const Exception&)
735 				{
736 					DBG_UNHANDLED_EXCEPTION();
737 				}
738 
739                 Reference< XPropertySet > xQuery;
740 				if (CommandType::QUERY == nCommandType)
741 				{
742 					// need to extract the statement and the escape processing flag from the query object
743 					sal_Bool bSuccess = sal_False;
744 					try
745 					{
746 						// the concrete query
747 						Reference< XQueryDefinitionsSupplier > xSourceQuerySup(
748                             getDataSourceByName( sDataSourceName, getView(), getORB(), NULL ),
749                             UNO_QUERY_THROW );
750                         Reference< XNameAccess > xQueries( xSourceQuerySup->getQueryDefinitions(), UNO_SET_THROW );
751 						if ( xQueries->hasByName( sCommand ) )
752 						{
753 							xQuery.set( xQueries->getByName(sCommand), UNO_QUERY_THROW );
754 							bSuccess = true;
755 						}
756 					}
757 					catch(SQLException&) { throw; }	// caught and handled by the outer catch
758                     catch( const Exception& )
759                     {
760                         DBG_UNHANDLED_EXCEPTION();
761                     }
762 
763 					if (!bSuccess)
764 					{
765 						DBG_ERROR("OApplicationController::paste: could not extract the source query object!");
766 						// TODO: maybe this is worth an error message to be displayed to the user ....
767 						return sal_False;
768 					}
769 				}
770 
771 
772 				Reference< XNameContainer > xDestQueries(getQueryDefintions(), UNO_QUERY);
773 				Reference< XSingleServiceFactory > xQueryFactory(xDestQueries, UNO_QUERY);
774 				if (!xQueryFactory.is())
775 				{
776 					DBG_ERROR("OApplicationController::paste: invalid destination query container!");
777 					return sal_False;
778 				}
779 
780 				// here we have everything needed to create a new query object ...
781 				// ... ehm, except a new name
782                 ensureConnection();
783 
784                 DynamicTableOrQueryNameCheck aNameChecker( getConnection(), CommandType::QUERY );
785                 ::dbtools::SQLExceptionInfo aDummy;
786                 bool bNeedAskForName =  ( sCommand.getLength() == 0 )
787                                             /* we did not have a source name, so the target name was auto-generated */
788                                     ||  ( !aNameChecker.isNameValid( sTargetName, aDummy ) );
789                                             /*  name is invalid in the target DB (e.g. because it already
790                                                 has a /table/ with that name) */
791                 if ( bNeedAskForName )
792                 {
793 				    OSaveAsDlg aAskForName(	getView(),
794 										    CommandType::QUERY,
795                                             getORB(),
796 										    getConnection(),
797 										    sTargetName,
798                                             aNameChecker,
799 										    SAD_ADDITIONAL_DESCRIPTION | SAD_TITLE_PASTE_AS);
800 				    if ( RET_OK != aAskForName.Execute() )
801 					    // cancelled by the user
802 					    return sal_False;
803 				    sTargetName = aAskForName.getName();
804                 }
805 
806 				// create a new object
807 				Reference< XPropertySet > xNewQuery(xQueryFactory->createInstance(), UNO_QUERY);
808 				DBG_ASSERT(xNewQuery.is(), "OApplicationController::paste: invalid object created by factory!");
809 				if (xNewQuery.is())
810 				{
811 					// initialize
812 					if ( xQuery.is() )
813 	                    ::comphelper::copyProperties(xQuery,xNewQuery);
814 					else
815 					{
816                         xNewQuery->setPropertyValue(PROPERTY_COMMAND,makeAny(sCommand));
817                         xNewQuery->setPropertyValue(PROPERTY_ESCAPE_PROCESSING,makeAny(bEscapeProcessing));
818                     }
819 					// insert
820 					xDestQueries->insertByName( sTargetName, makeAny(xNewQuery) );
821                     xNewQuery.set(xDestQueries->getByName( sTargetName),UNO_QUERY);
822                     if ( xQuery.is() && xNewQuery.is() )
823                     {
824                         Reference<XColumnsSupplier> xSrcColSup(xQuery,UNO_QUERY);
825                         Reference<XColumnsSupplier> xDstColSup(xNewQuery,UNO_QUERY);
826                         if ( xSrcColSup.is() && xDstColSup.is() )
827 	                    {
828 		                    Reference<XNameAccess> xSrcNameAccess = xSrcColSup->getColumns();
829                             Reference<XNameAccess> xDstNameAccess = xDstColSup->getColumns();
830                             Reference<XDataDescriptorFactory> xFac(xDstNameAccess,UNO_QUERY);
831                             Reference<XAppend> xAppend(xFac,UNO_QUERY);
832 		                    if ( xSrcNameAccess.is() && xDstNameAccess.is() && xSrcNameAccess->hasElements() && xAppend.is() )
833                             {
834                                 Reference<XPropertySet> xDstProp(xFac->createDataDescriptor());
835 
836                                 Sequence< ::rtl::OUString> aSeq = xSrcNameAccess->getElementNames();
837 			                    const ::rtl::OUString* pIter = aSeq.getConstArray();
838 			                    const ::rtl::OUString* pEnd	  = pIter + aSeq.getLength();
839 			                    for( ; pIter != pEnd ; ++pIter)
840 			                    {
841 				                    Reference<XPropertySet> xSrcProp(xSrcNameAccess->getByName(*pIter),UNO_QUERY);
842                                     ::comphelper::copyProperties(xSrcProp,xDstProp);
843                                     xAppend->appendByDescriptor(xDstProp);
844                                 }
845                             }
846                         }
847 					}
848 				}
849 			}
850 			else
851 				OSL_TRACE("There should be a sequence in it!");
852 			return sal_True;
853 		}
854 		else if ( _rPasteData.has(daComponent) ) // forms or reports
855 		{
856 			Reference<XContent> xContent;
857 			_rPasteData[daComponent] >>= xContent;
858 			return insertHierachyElement(_eType,_sParentFolder,Reference<XNameAccess>(xContent,UNO_QUERY).is(),xContent,_bMove);
859 		}
860 	}
861     catch(const SQLException&) { showError( SQLExceptionInfo( ::cppu::getCaughtException() ) ); }
862 	catch(const Exception& )
863 	{
864         DBG_UNHANDLED_EXCEPTION();
865 	}
866 	return sal_False;
867 }
868 // -----------------------------------------------------------------------------
869 Reference<XNameContainer> OApplicationController::getQueryDefintions() const
870 {
871 	Reference<XQueryDefinitionsSupplier> xSet(m_xDataSource,UNO_QUERY);
872 	Reference<XNameContainer> xNames;
873 	if ( xSet.is() )
874 	{
875 		xNames.set(xSet->getQueryDefinitions(),UNO_QUERY);
876 	}
877 	return xNames;
878 }
879 // -----------------------------------------------------------------------------
880 void OApplicationController::getSupportedFormats(ElementType _eType,::std::vector<SotFormatStringId>& _rFormatIds) const
881 {
882 	switch( _eType )
883 	{
884 		case E_TABLE:
885 			_rFormatIds.push_back(SOT_FORMATSTR_ID_DBACCESS_TABLE);
886 			_rFormatIds.push_back(SOT_FORMAT_RTF);
887 			_rFormatIds.push_back(SOT_FORMATSTR_ID_HTML);
888 			// run through
889 		case E_QUERY:
890 			_rFormatIds.push_back(SOT_FORMATSTR_ID_DBACCESS_QUERY);
891 			break;
892         default:
893             break;
894 	}
895 }
896 // -----------------------------------------------------------------------------
897 sal_Bool OApplicationController::isTableFormat()  const
898 {
899 	return m_aTableCopyHelper.isTableFormat(getViewClipboard());
900 }
901 // -----------------------------------------------------------------------------
902 IMPL_LINK( OApplicationController, OnAsyncDrop, void*, /*NOTINTERESTEDIN*/ )
903 {
904 	m_nAsyncDrop = 0;
905 	::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
906 	::osl::MutexGuard aGuard( getMutex() );
907 
908 
909 	if ( m_aAsyncDrop.nType == E_TABLE )
910 	{
911         SharedConnection xConnection( ensureConnection() );
912         if ( xConnection.is() )
913 		    m_aTableCopyHelper.asyncCopyTagTable( m_aAsyncDrop, getDatabaseName(), xConnection );
914 	}
915 	else
916 	{
917 		if ( paste(m_aAsyncDrop.nType,m_aAsyncDrop.aDroppedData,m_aAsyncDrop.aUrl,m_aAsyncDrop.nAction == DND_ACTION_MOVE)
918 			&& m_aAsyncDrop.nAction == DND_ACTION_MOVE )
919 		{
920 			Reference<XContent> xContent;
921 			m_aAsyncDrop.aDroppedData[daComponent] >>= xContent;
922 			::std::vector< ::rtl::OUString> aList;
923 			sal_Int32 nIndex = 0;
924 			::rtl::OUString sName = xContent->getIdentifier()->getContentIdentifier();
925 			::rtl::OUString sErase = sName.getToken(0,'/',nIndex); // we don't want to have the "private:forms" part
926 			if ( nIndex != -1 )
927 			{
928 				aList.push_back(sName.copy(sErase.getLength() + 1));
929 				deleteObjects( m_aAsyncDrop.nType, aList, false );
930 			}
931 		}
932 	}
933 
934 	m_aAsyncDrop.aDroppedData.clear();
935 
936 	return 0L;
937 }
938 //........................................................................
939 }	// namespace dbaui
940 //........................................................................
941 
942 
943