xref: /trunk/main/dbaccess/source/ui/app/AppControllerDnD.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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_dbui.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 #include <iterator>
188 #ifndef _SVTREEBOX_HXX
189 #include <svtools/svtreebx.hxx>
190 #endif
191 #ifndef _COM_SUN_STAR_SDB_XREPORTDOCUMENTSSUPPLIER_HPP_
192 #include <com/sun/star/sdb/XReportDocumentsSupplier.hpp>
193 #endif
194 #ifndef _COM_SUN_STAR_SDB_XFORMDOCUMENTSSUPPLIER_HPP_
195 #include <com/sun/star/sdb/XFormDocumentsSupplier.hpp>
196 #endif
197 #ifndef _FILEDLGHELPER_HXX
198 #include <sfx2/filedlghelper.hxx>
199 #endif
200 #ifndef INCLUDED_SVTOOLS_PATHOPTIONS_HXX
201 #include <unotools/pathoptions.hxx>
202 #endif
203 #ifndef _SFX_DOCFILT_HACK_HXX
204 #include <sfx2/docfilt.hxx>
205 #endif
206 #ifndef _SVT_FILEVIEW_HXX
207 #include <svtools/fileview.hxx>
208 #endif
209 #ifndef TOOLS_DIAGNOSE_EX_H
210 #include <tools/diagnose_ex.h>
211 #endif
212 #ifndef DBACCESS_SOURCE_UI_MISC_DEFAULTOBJECTNAMECHECK_HXX
213 #include "defaultobjectnamecheck.hxx"
214 #endif
215 #ifndef _VOS_MUTEX_HXX_
216 #include <vos/mutex.hxx>
217 #endif
218 #include "subcomponentmanager.hxx"
219 
220 //........................................................................
221 namespace dbaui
222 {
223 //........................................................................
224 using namespace ::dbtools;
225 using namespace ::svx;
226 using namespace ::svtools;
227 using namespace ::com::sun::star::uno;
228 using namespace ::com::sun::star::task;
229 using namespace ::com::sun::star::beans;
230 using namespace ::com::sun::star::lang;
231 using namespace ::com::sun::star::container;
232 using namespace ::com::sun::star::sdb;
233 using namespace ::com::sun::star::sdbc;
234 using namespace ::com::sun::star::sdbcx;
235 using namespace ::com::sun::star::frame;
236 using namespace ::com::sun::star::ucb;
237 using namespace ::com::sun::star::util;
238 
239 // -----------------------------------------------------------------------------
deleteTables(const::std::vector<::rtl::OUString> & _rList)240 void OApplicationController::deleteTables(const ::std::vector< ::rtl::OUString>& _rList)
241 {
242     SharedConnection xConnection( ensureConnection() );
243 
244     Reference<XTablesSupplier> xSup(xConnection,UNO_QUERY);
245     OSL_ENSURE(xSup.is(),"OApplicationController::deleteTable: no XTablesSuppier!");
246     if ( xSup.is() )
247     {
248         Reference<XNameAccess> xTables = xSup->getTables();
249         Reference<XDrop> xDrop(xTables,UNO_QUERY);
250         if ( xDrop.is() )
251         {
252             bool bConfirm = true;
253             ::std::vector< ::rtl::OUString>::const_iterator aEnd = _rList.end();
254             for (::std::vector< ::rtl::OUString>::const_iterator aIter = _rList.begin(); aIter != aEnd; ++aIter)
255             {
256                 ::rtl::OUString sTableName = *aIter;
257 
258                 sal_Int32 nResult = RET_YES;
259                 if ( bConfirm )
260                     nResult = ::dbaui::askForUserAction(getView(),STR_TITLE_CONFIRM_DELETION ,STR_QUERY_DELETE_TABLE,_rList.size() > 1 && (aIter+1) != _rList.end(),sTableName);
261 
262                 bool bUserConfirmedDelete =
263                             ( RET_YES == nResult )
264                         ||  ( RET_ALL == nResult );
265                 if ( bUserConfirmedDelete && m_pSubComponentManager->closeSubFrames( sTableName, E_TABLE ) )
266                 {
267                     SQLExceptionInfo aErrorInfo;
268                     try
269                     {
270                         if ( xTables->hasByName(sTableName) )
271                             xDrop->dropByName(sTableName);
272                         else
273                         {// could be a view
274                             Reference<XViewsSupplier> xViewsSup(xConnection,UNO_QUERY);
275 
276                             Reference<XNameAccess> xViews;
277                             if ( xViewsSup.is() )
278                             {
279                                 xViews = xViewsSup->getViews();
280                                 if ( xViews.is() && xViews->hasByName(sTableName) )
281                                 {
282                                     xDrop.set(xViews,UNO_QUERY);
283                                     if ( xDrop.is() )
284                                         xDrop->dropByName(sTableName);
285                                 }
286                             }
287                         }
288                     }
289                     catch(SQLContext& e) { aErrorInfo = e; }
290                     catch(SQLWarning& e) { aErrorInfo = e; }
291                     catch(SQLException& e) { aErrorInfo = e; }
292                     catch(WrappedTargetException& e)
293                     {
294                         SQLException aSql;
295                         if(e.TargetException >>= aSql)
296                             aErrorInfo = aSql;
297                         else
298                             OSL_ENSURE(sal_False, "OApplicationController::implDropTable: something strange happened!");
299                     }
300                     catch( const Exception& )
301                     {
302                         DBG_UNHANDLED_EXCEPTION();
303                     }
304 
305                     if ( aErrorInfo.isValid() )
306                         showError(aErrorInfo);
307 
308                     if ( RET_ALL == nResult )
309                         bConfirm = false;
310                 }
311                 else
312                     break;
313             }
314         }
315         else
316         {
317             String sMessage(ModuleRes(STR_MISSING_TABLES_XDROP));
318             ErrorBox aError(getView(), WB_OK, sMessage);
319             aError.Execute();
320         }
321     }
322 }
323 // -----------------------------------------------------------------------------
deleteObjects(ElementType _eType,const::std::vector<::rtl::OUString> & _rList,bool _bConfirm)324 void OApplicationController::deleteObjects( ElementType _eType, const ::std::vector< ::rtl::OUString>& _rList, bool _bConfirm )
325 {
326     Reference< XNameContainer > xNames( getElements( _eType ), UNO_QUERY );
327     Reference< XHierarchicalNameContainer > xHierarchyName( xNames, UNO_QUERY );
328     if ( xNames.is() )
329     {
330         ByteString sDialogPosition;
331         svtools::QueryDeleteResult_Impl eResult = _bConfirm ? svtools::QUERYDELETE_YES : svtools::QUERYDELETE_ALL;
332 
333         // The list of elements to delete is allowed to contain related elements: A given element may
334         // be the ancestor or child of another element from the list.
335         // We want to ensure that ancestors get deleted first, so we normalize the list in this respect.
336         // #i33353# - 2004-09-27 - fs@openoffice.org
337         ::std::set< ::rtl::OUString > aDeleteNames;
338             // Note that this implicitly uses ::std::less< ::rtl::OUString > a comparison operation, which
339             // results in lexicographical order, which is exactly what we need, because "foo" is *before*
340             // any "foo/bar" in this order.
341         ::std::copy(
342             _rList.begin(), _rList.end(),
343             ::std::insert_iterator< ::std::set< ::rtl::OUString > >( aDeleteNames, aDeleteNames.begin() )
344         );
345 
346         ::std::set< ::rtl::OUString >::size_type nCount = aDeleteNames.size();
347         for ( ::std::set< ::rtl::OUString >::size_type nObjectsLeft = nCount; !aDeleteNames.empty(); )
348         {
349             ::std::set< ::rtl::OUString >::iterator  aThisRound = aDeleteNames.begin();
350 
351             if ( eResult != svtools::QUERYDELETE_ALL )
352             {
353                 svtools::QueryDeleteDlg_Impl aDlg( getView(), *aThisRound );
354 
355                 if ( sDialogPosition.Len() )
356                     aDlg.SetWindowState( sDialogPosition );
357 
358                 if ( nObjectsLeft > 1 )
359                     aDlg.EnableAllButton();
360 
361                 if ( aDlg.Execute() == RET_OK )
362                     eResult = aDlg.GetResult();
363                 else
364                     return;
365 
366                 sDialogPosition = aDlg.GetWindowState( );
367             }
368 
369             bool bSuccess = false;
370 
371             bool bUserConfirmedDelete =
372                         ( eResult == svtools::QUERYDELETE_ALL )
373                     ||  ( eResult == svtools::QUERYDELETE_YES );
374 
375             if  (   bUserConfirmedDelete
376                 &&  (   ( _eType == E_QUERY ) ? m_pSubComponentManager->closeSubFrames( *aThisRound, _eType ) : true )
377                 )
378             {
379                 try
380                 {
381                     if ( xHierarchyName.is() )
382                         xHierarchyName->removeByHierarchicalName( *aThisRound );
383                     else
384                         xNames->removeByName( *aThisRound );
385 
386                     bSuccess = true;
387 
388                     // now that we removed the element, care for all it's child elements
389                     // which may also be a part of the list
390                     // #i33353# - 2004-09-27 - fs@openoffice.org
391                     OSL_ENSURE( aThisRound->getLength() - 1 >= 0, "OApplicationController::deleteObjects: empty name?" );
392                     ::rtl::OUStringBuffer sSmallestSiblingName( *aThisRound );
393                     sSmallestSiblingName.append( (sal_Unicode)( '/' + 1) );
394 
395                     ::std::set< ::rtl::OUString >::iterator aUpperChildrenBound = aDeleteNames.lower_bound( sSmallestSiblingName.makeStringAndClear() );
396                     for ( ::std::set< ::rtl::OUString >::iterator aObsolete = aThisRound;
397                           aObsolete != aUpperChildrenBound;
398                         )
399                     {
400 #if OSL_DEBUG_LEVEL > 0
401                         ::rtl::OUString sObsoleteName = *aObsolete;
402 #endif
403                         ::std::set< ::rtl::OUString >::iterator aNextObsolete = aObsolete; ++aNextObsolete;
404                         aDeleteNames.erase( aObsolete );
405                         --nObjectsLeft;
406                         aObsolete = aNextObsolete;
407                     }
408                 }
409                 catch(const SQLException&)
410                 {
411                     showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
412                 }
413                 catch(WrappedTargetException& e)
414                 {
415                     SQLException aSql;
416                     if ( e.TargetException >>= aSql )
417                         showError( SQLExceptionInfo( e.TargetException ) );
418                     else
419                         OSL_ENSURE( sal_False, "OApplicationController::deleteObjects: something strange happened!" );
420                 }
421                 catch( const Exception& )
422                 {
423                     DBG_UNHANDLED_EXCEPTION();
424                 }
425             }
426 
427             if ( !bSuccess )
428             {
429                 // okay, this object could not be deleted (or the user did not want to delete it),
430                 // but continue with the rest
431                 aDeleteNames.erase( aThisRound );
432                 --nObjectsLeft;
433             }
434         }
435     }
436 }
437 // -----------------------------------------------------------------------------
deleteEntries()438 void OApplicationController::deleteEntries()
439 {
440     ::vos::OGuard aSolarGuard(Application::GetSolarMutex());
441     ::osl::MutexGuard aGuard( getMutex() );
442 
443     if ( getContainer() )
444     {
445         ::std::vector< ::rtl::OUString> aList;
446         getSelectionElementNames(aList);
447         ElementType eType = getContainer()->getElementType();
448         switch(eType)
449         {
450         case E_TABLE:
451             deleteTables(aList);
452             break;
453         case E_QUERY:
454             deleteObjects( E_QUERY, aList, true );
455             break;
456         case E_FORM:
457             deleteObjects( E_FORM, aList, true );
458             break;
459         case E_REPORT:
460             deleteObjects( E_REPORT, aList, true );
461             break;
462         case E_NONE:
463             break;
464         }
465     }
466 }
467 // -----------------------------------------------------------------------------
ensureConnection(::dbtools::SQLExceptionInfo * _pErrorInfo)468 const SharedConnection& OApplicationController::ensureConnection( ::dbtools::SQLExceptionInfo* _pErrorInfo )
469 {
470     ::vos::OGuard aSolarGuard(Application::GetSolarMutex());
471     ::osl::MutexGuard aGuard( getMutex() );
472 
473     if ( !m_xDataSourceConnection.is() )
474     {
475         WaitObject aWO(getView());
476         String sConnectingContext( ModuleRes( STR_COULDNOTCONNECT_DATASOURCE ) );
477         sConnectingContext.SearchAndReplaceAscii("$name$", getStrippedDatabaseName());
478 
479         m_xDataSourceConnection.reset( connect( getDatabaseName(), sConnectingContext, _pErrorInfo ) );
480         if ( m_xDataSourceConnection.is() )
481         {
482             SQLExceptionInfo aError;
483             try
484             {
485                 m_xMetaData = m_xDataSourceConnection->getMetaData();
486             }
487             catch( const SQLException& )
488             {
489                 aError = ::cppu::getCaughtException();
490             }
491             catch( const Exception& )
492             {
493                 DBG_UNHANDLED_EXCEPTION();
494             }
495             if ( aError.isValid() )
496             {
497                 if ( _pErrorInfo )
498                 {
499                     *_pErrorInfo = aError;
500                 }
501                 else
502                 {
503                     showError( aError );
504                 }
505             }
506         }
507     }
508     return m_xDataSourceConnection;
509 }
510 // -----------------------------------------------------------------------------
isDataSourceReadOnly() const511 sal_Bool OApplicationController::isDataSourceReadOnly() const
512 {
513     Reference<XStorable> xStore(m_xModel,UNO_QUERY);
514     return !xStore.is() || xStore->isReadonly();
515 }
516 // -----------------------------------------------------------------------------
isConnectionReadOnly() const517 sal_Bool OApplicationController::isConnectionReadOnly() const
518 {
519     sal_Bool bIsConnectionReadOnly = sal_True;
520     if ( m_xMetaData.is() )
521     {
522         try
523         {
524             bIsConnectionReadOnly = m_xMetaData->isReadOnly();
525         }
526         catch(const SQLException&)
527         {
528             DBG_UNHANDLED_EXCEPTION();
529         }
530     }
531     // TODO check configuration
532     return bIsConnectionReadOnly;
533 }
534 // -----------------------------------------------------------------------------
getElements(ElementType _eType)535 Reference< XNameAccess > OApplicationController::getElements( ElementType _eType )
536 {
537     Reference< XNameAccess > xElements;
538     try
539     {
540         switch ( _eType )
541         {
542         case E_REPORT:
543         {
544             Reference< XReportDocumentsSupplier > xSupp( m_xModel, UNO_QUERY_THROW );
545             xElements.set( xSupp->getReportDocuments(), UNO_SET_THROW );
546         }
547         break;
548 
549         case E_FORM:
550         {
551             Reference< XFormDocumentsSupplier > xSupp( m_xModel, UNO_QUERY_THROW );
552             xElements.set( xSupp->getFormDocuments(), UNO_SET_THROW );
553         }
554         break;
555 
556         case E_QUERY:
557         {
558             xElements.set( getQueryDefintions(), UNO_QUERY_THROW );
559         }
560         break;
561 
562         case E_TABLE:
563         {
564             if ( m_xDataSourceConnection.is() )
565             {
566                 Reference< XTablesSupplier > xSup( getConnection(), UNO_QUERY_THROW );
567                 xElements.set( xSup->getTables(), UNO_SET_THROW );
568             }
569         }
570         break;
571 
572         default:
573             break;
574         }
575     }
576     catch(const Exception&)
577     {
578         DBG_UNHANDLED_EXCEPTION();
579     }
580 
581     return xElements;
582 }
583 // -----------------------------------------------------------------------------
getSelectionElementNames(::std::vector<::rtl::OUString> & _rNames) const584 void OApplicationController::getSelectionElementNames(::std::vector< ::rtl::OUString>& _rNames) const
585 {
586     ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
587     ::osl::MutexGuard aGuard( getMutex() );
588 
589     OSL_ENSURE(getContainer(),"View isn't valid! -> GPF");
590 
591     getContainer()->getSelectionElementNames( _rNames );
592 }
593 
594 // -----------------------------------------------------------------------------
getDocumentsAccess(ElementType _eType)595 ::std::auto_ptr< OLinkedDocumentsAccess > OApplicationController::getDocumentsAccess( ElementType _eType )
596 {
597     OSL_ENSURE( ( _eType == E_TABLE ) || ( _eType == E_QUERY ) || ( _eType == E_FORM ) || ( _eType == E_REPORT ),
598         "OApplicationController::getDocumentsAccess: only forms and reports are supported here!" );
599 
600     SharedConnection xConnection( ensureConnection() );
601     Reference< XNameAccess > xDocContainer;
602 
603     if ( ( _eType == E_FORM ) | ( _eType == E_REPORT ) )
604     {
605         xDocContainer.set( getElements( _eType ) );
606         OSL_ENSURE( xDocContainer.is(), "OApplicationController::getDocumentsAccess: invalid container!" );
607     }
608 
609     ::std::auto_ptr< OLinkedDocumentsAccess > pDocuments( new OLinkedDocumentsAccess(
610         getView(), this, getORB(), xDocContainer, xConnection, getDatabaseName()
611     ) );
612     return pDocuments;
613 }
614 // -----------------------------------------------------------------------------
copyObject()615 TransferableHelper* OApplicationController::copyObject()
616 {
617     try
618     {
619         ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
620         ::osl::MutexGuard aGuard( getMutex() );
621 
622         ElementType eType = getContainer()->getElementType();
623         TransferableHelper* pData = NULL;
624         switch( eType )
625         {
626             case E_TABLE:
627             case E_QUERY:
628             {
629                 SharedConnection xConnection( ensureConnection() );
630                 Reference< XDatabaseMetaData> xMetaData;
631                 if ( xConnection.is() )
632                     xMetaData = xConnection->getMetaData();
633 
634                 ::rtl::OUString sName = getContainer()->getQualifiedName( NULL );
635                 if ( sName.getLength() )
636                 {
637                     ::rtl::OUString sDataSource = getDatabaseName();
638 
639                     if ( eType == E_TABLE )
640                     {
641                         pData = new ODataClipboard(sDataSource, CommandType::TABLE, sName, xConnection, getNumberFormatter(xConnection,getORB()), getORB());
642                     }
643                     else
644                     {
645                         pData = new ODataClipboard(sDataSource, CommandType::QUERY, sName, getNumberFormatter(xConnection,getORB()), getORB());
646                     }
647                 }
648             }
649                 break;
650             case E_FORM:
651             case E_REPORT:
652             {
653                 ::std::vector< ::rtl::OUString> aList;
654                 getSelectionElementNames(aList);
655                 Reference< XHierarchicalNameAccess > xElements(getElements(eType),UNO_QUERY);
656                 if ( xElements.is() && !aList.empty() )
657                 {
658                     Reference< XContent> xContent(xElements->getByHierarchicalName(*aList.begin()),UNO_QUERY);
659                     pData = new OComponentTransferable( getDatabaseName(), xContent );
660                 }
661             }
662             break;
663             default:
664                 break;
665         }
666 
667         // the owner ship goes to ODataClipboards
668         return pData;
669     }
670     catch(const SQLException&)
671     {
672         showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
673     }
674     catch( const Exception& )
675     {
676         DBG_UNHANDLED_EXCEPTION();
677     }
678     return NULL;
679 }
680 // -----------------------------------------------------------------------------
paste(ElementType _eType,const::svx::ODataAccessDescriptor & _rPasteData,const String & _sParentFolder,sal_Bool _bMove)681 sal_Bool OApplicationController::paste( ElementType _eType,const ::svx::ODataAccessDescriptor& _rPasteData,const String& _sParentFolder ,sal_Bool _bMove)
682 {
683     try
684     {
685         if ( _eType == E_QUERY )
686         {
687             sal_Int32 nCommandType = CommandType::TABLE;
688             if ( _rPasteData.has(daCommandType) )
689                 _rPasteData[daCommandType]      >>= nCommandType;
690 
691             if ( CommandType::QUERY == nCommandType || CommandType::COMMAND == nCommandType )
692             {
693                 // read all necessary data
694 
695                 ::rtl::OUString sCommand;
696                 sal_Bool bEscapeProcessing = sal_True;
697 
698                 _rPasteData[daCommand] >>= sCommand;
699                 if ( _rPasteData.has(daEscapeProcessing) )
700                     _rPasteData[daEscapeProcessing] >>= bEscapeProcessing;
701 
702                 // plausibility check
703                 sal_Bool bValidDescriptor = sal_False;
704                 ::rtl::OUString sDataSourceName = _rPasteData.getDataSource();
705                 if (CommandType::QUERY == nCommandType)
706                     bValidDescriptor = sDataSourceName.getLength() && sCommand.getLength();
707                 else if (CommandType::COMMAND == nCommandType)
708                     bValidDescriptor = (0 != sCommand.getLength());
709                 if (!bValidDescriptor)
710                 {
711                     DBG_ERROR("OApplicationController::paste: invalid descriptor!");
712                     return sal_False;
713                 }
714 
715                 // the target object name (as we'll suggest it to the user)
716                 ::rtl::OUString sTargetName;
717                 try
718                 {
719                     if ( CommandType::QUERY == nCommandType )
720                         sTargetName = sCommand;
721 
722                     if ( !sTargetName.getLength() )
723                     {
724                         String sDefaultName = String( ModuleRes( STR_QRY_TITLE ) );
725                         sDefaultName = sDefaultName.GetToken( 0, ' ' );
726 
727                         Reference< XNameAccess > xQueries( getQueryDefintions(), UNO_QUERY_THROW );
728                         sTargetName = ::dbtools::createUniqueName( xQueries, sDefaultName, sal_False );
729                     }
730                 }
731                 catch(const Exception&)
732                 {
733                     DBG_UNHANDLED_EXCEPTION();
734                 }
735 
736                 Reference< XPropertySet > xQuery;
737                 if (CommandType::QUERY == nCommandType)
738                 {
739                     // need to extract the statement and the escape processing flag from the query object
740                     sal_Bool bSuccess = sal_False;
741                     try
742                     {
743                         // the concrete query
744                         Reference< XQueryDefinitionsSupplier > xSourceQuerySup(
745                             getDataSourceByName( sDataSourceName, getView(), getORB(), NULL ),
746                             UNO_QUERY_THROW );
747                         Reference< XNameAccess > xQueries( xSourceQuerySup->getQueryDefinitions(), UNO_SET_THROW );
748                         if ( xQueries->hasByName( sCommand ) )
749                         {
750                             xQuery.set( xQueries->getByName(sCommand), UNO_QUERY_THROW );
751                             bSuccess = true;
752                         }
753                     }
754                     catch(SQLException&) { throw; } // caught and handled by the outer catch
755                     catch( const Exception& )
756                     {
757                         DBG_UNHANDLED_EXCEPTION();
758                     }
759 
760                     if (!bSuccess)
761                     {
762                         DBG_ERROR("OApplicationController::paste: could not extract the source query object!");
763                         // TODO: maybe this is worth an error message to be displayed to the user ....
764                         return sal_False;
765                     }
766                 }
767 
768 
769                 Reference< XNameContainer > xDestQueries(getQueryDefintions(), UNO_QUERY);
770                 Reference< XSingleServiceFactory > xQueryFactory(xDestQueries, UNO_QUERY);
771                 if (!xQueryFactory.is())
772                 {
773                     DBG_ERROR("OApplicationController::paste: invalid destination query container!");
774                     return sal_False;
775                 }
776 
777                 // here we have everything needed to create a new query object ...
778                 // ... ehm, except a new name
779                 ensureConnection();
780 
781                 DynamicTableOrQueryNameCheck aNameChecker( getConnection(), CommandType::QUERY );
782                 ::dbtools::SQLExceptionInfo aDummy;
783                 bool bNeedAskForName =  ( sCommand.getLength() == 0 )
784                                             /* we did not have a source name, so the target name was auto-generated */
785                                     ||  ( !aNameChecker.isNameValid( sTargetName, aDummy ) );
786                                             /*  name is invalid in the target DB (e.g. because it already
787                                                 has a /table/ with that name) */
788                 if ( bNeedAskForName )
789                 {
790                     OSaveAsDlg aAskForName( getView(),
791                                             CommandType::QUERY,
792                                             getORB(),
793                                             getConnection(),
794                                             sTargetName,
795                                             aNameChecker,
796                                             SAD_ADDITIONAL_DESCRIPTION | SAD_TITLE_PASTE_AS);
797                     if ( RET_OK != aAskForName.Execute() )
798                         // cancelled by the user
799                         return sal_False;
800                     sTargetName = aAskForName.getName();
801                 }
802 
803                 // create a new object
804                 Reference< XPropertySet > xNewQuery(xQueryFactory->createInstance(), UNO_QUERY);
805                 DBG_ASSERT(xNewQuery.is(), "OApplicationController::paste: invalid object created by factory!");
806                 if (xNewQuery.is())
807                 {
808                     // initialize
809                     if ( xQuery.is() )
810                         ::comphelper::copyProperties(xQuery,xNewQuery);
811                     else
812                     {
813                         xNewQuery->setPropertyValue(PROPERTY_COMMAND,makeAny(sCommand));
814                         xNewQuery->setPropertyValue(PROPERTY_ESCAPE_PROCESSING,makeAny(bEscapeProcessing));
815                     }
816                     // insert
817                     xDestQueries->insertByName( sTargetName, makeAny(xNewQuery) );
818                     xNewQuery.set(xDestQueries->getByName( sTargetName),UNO_QUERY);
819                     if ( xQuery.is() && xNewQuery.is() )
820                     {
821                         Reference<XColumnsSupplier> xSrcColSup(xQuery,UNO_QUERY);
822                         Reference<XColumnsSupplier> xDstColSup(xNewQuery,UNO_QUERY);
823                         if ( xSrcColSup.is() && xDstColSup.is() )
824                         {
825                             Reference<XNameAccess> xSrcNameAccess = xSrcColSup->getColumns();
826                             Reference<XNameAccess> xDstNameAccess = xDstColSup->getColumns();
827                             Reference<XDataDescriptorFactory> xFac(xDstNameAccess,UNO_QUERY);
828                             Reference<XAppend> xAppend(xFac,UNO_QUERY);
829                             if ( xSrcNameAccess.is() && xDstNameAccess.is() && xSrcNameAccess->hasElements() && xAppend.is() )
830                             {
831                                 Reference<XPropertySet> xDstProp(xFac->createDataDescriptor());
832 
833                                 Sequence< ::rtl::OUString> aSeq = xSrcNameAccess->getElementNames();
834                                 const ::rtl::OUString* pIter = aSeq.getConstArray();
835                                 const ::rtl::OUString* pEnd   = pIter + aSeq.getLength();
836                                 for( ; pIter != pEnd ; ++pIter)
837                                 {
838                                     Reference<XPropertySet> xSrcProp(xSrcNameAccess->getByName(*pIter),UNO_QUERY);
839                                     ::comphelper::copyProperties(xSrcProp,xDstProp);
840                                     xAppend->appendByDescriptor(xDstProp);
841                                 }
842                             }
843                         }
844                     }
845                 }
846             }
847             else
848                 OSL_TRACE("There should be a sequence in it!");
849             return sal_True;
850         }
851         else if ( _rPasteData.has(daComponent) ) // forms or reports
852         {
853             Reference<XContent> xContent;
854             _rPasteData[daComponent] >>= xContent;
855             return insertHierachyElement(_eType,_sParentFolder,Reference<XNameAccess>(xContent,UNO_QUERY).is(),xContent,_bMove);
856         }
857     }
858     catch(const SQLException&) { showError( SQLExceptionInfo( ::cppu::getCaughtException() ) ); }
859     catch(const Exception& )
860     {
861         DBG_UNHANDLED_EXCEPTION();
862     }
863     return sal_False;
864 }
865 // -----------------------------------------------------------------------------
getQueryDefintions() const866 Reference<XNameContainer> OApplicationController::getQueryDefintions() const
867 {
868     Reference<XQueryDefinitionsSupplier> xSet(m_xDataSource,UNO_QUERY);
869     Reference<XNameContainer> xNames;
870     if ( xSet.is() )
871     {
872         xNames.set(xSet->getQueryDefinitions(),UNO_QUERY);
873     }
874     return xNames;
875 }
876 // -----------------------------------------------------------------------------
getSupportedFormats(ElementType _eType,::std::vector<SotFormatStringId> & _rFormatIds) const877 void OApplicationController::getSupportedFormats(ElementType _eType,::std::vector<SotFormatStringId>& _rFormatIds) const
878 {
879     switch( _eType )
880     {
881         case E_TABLE:
882             _rFormatIds.push_back(SOT_FORMATSTR_ID_DBACCESS_TABLE);
883             _rFormatIds.push_back(SOT_FORMAT_RTF);
884             _rFormatIds.push_back(SOT_FORMATSTR_ID_HTML);
885             // run through
886         case E_QUERY:
887             _rFormatIds.push_back(SOT_FORMATSTR_ID_DBACCESS_QUERY);
888             break;
889         default:
890             break;
891     }
892 }
893 // -----------------------------------------------------------------------------
isTableFormat() const894 sal_Bool OApplicationController::isTableFormat()  const
895 {
896     return m_aTableCopyHelper.isTableFormat(getViewClipboard());
897 }
898 // -----------------------------------------------------------------------------
899 IMPL_LINK( OApplicationController, OnAsyncDrop, void*, /*NOTINTERESTEDIN*/ )
900 {
901     m_nAsyncDrop = 0;
902     ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
903     ::osl::MutexGuard aGuard( getMutex() );
904 
905 
906     if ( m_aAsyncDrop.nType == E_TABLE )
907     {
908         SharedConnection xConnection( ensureConnection() );
909         if ( xConnection.is() )
910             m_aTableCopyHelper.asyncCopyTagTable( m_aAsyncDrop, getDatabaseName(), xConnection );
911     }
912     else
913     {
914         if ( paste(m_aAsyncDrop.nType,m_aAsyncDrop.aDroppedData,m_aAsyncDrop.aUrl,m_aAsyncDrop.nAction == DND_ACTION_MOVE)
915             && m_aAsyncDrop.nAction == DND_ACTION_MOVE )
916         {
917             Reference<XContent> xContent;
918             m_aAsyncDrop.aDroppedData[daComponent] >>= xContent;
919             ::std::vector< ::rtl::OUString> aList;
920             sal_Int32 nIndex = 0;
921             ::rtl::OUString sName = xContent->getIdentifier()->getContentIdentifier();
922             ::rtl::OUString sErase = sName.getToken(0,'/',nIndex); // we don't want to have the "private:forms" part
923             if ( nIndex != -1 )
924             {
925                 aList.push_back(sName.copy(sErase.getLength() + 1));
926                 deleteObjects( m_aAsyncDrop.nType, aList, false );
927             }
928         }
929     }
930 
931     m_aAsyncDrop.aDroppedData.clear();
932 
933     return 0L;
934 }
935 //........................................................................
936 }   // namespace dbaui
937 //........................................................................
938