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