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