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