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 <svx/dbaexchange.hxx>
21 : #include <osl/diagnose.h>
22 : #include <com/sun/star/sdb/CommandType.hpp>
23 : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
24 : #include <com/sun/star/sdb/XSQLQueryComposerFactory.hpp>
25 : #include "fmprop.hrc"
26 : #include <comphelper/extract.hxx>
27 : #include <sot/formats.hxx>
28 : #include <sot/exchange.hxx>
29 : #include <comphelper/propertysetinfo.hxx>
30 :
31 :
32 : namespace svx
33 : {
34 :
35 :
36 : using namespace ::com::sun::star::uno;
37 : using namespace ::com::sun::star::beans;
38 : using namespace ::com::sun::star::sdb;
39 : using namespace ::com::sun::star::sdbc;
40 : using namespace ::com::sun::star::lang;
41 : using namespace ::com::sun::star::sdbcx;
42 : using namespace ::com::sun::star::container;
43 : using namespace ::com::sun::star::datatransfer;
44 : using namespace ::comphelper;
45 :
46 0 : OColumnTransferable::OColumnTransferable(const OUString& _rDatasource
47 : ,const OUString& _rConnectionResource
48 : ,const sal_Int32 _nCommandType
49 : ,const OUString& _rCommand
50 : ,const OUString& _rFieldName
51 : ,sal_Int32 _nFormats)
52 0 : :m_nFormatFlags(_nFormats)
53 : {
54 0 : implConstruct(_rDatasource,_rConnectionResource,_nCommandType, _rCommand, _rFieldName);
55 0 : }
56 :
57 :
58 0 : OColumnTransferable::OColumnTransferable(const ODataAccessDescriptor& _rDescriptor, sal_Int32 _nFormats )
59 0 : :m_nFormatFlags(_nFormats)
60 : {
61 0 : OUString sDataSource, sDatabaseLocation, sConnectionResource, sCommand, sFieldName;
62 0 : if ( _rDescriptor.has( daDataSource ) ) _rDescriptor[ daDataSource ] >>= sDataSource;
63 0 : if ( _rDescriptor.has( daDatabaseLocation ) ) _rDescriptor[ daDatabaseLocation ] >>= sDatabaseLocation;
64 0 : if ( _rDescriptor.has( daConnectionResource ) ) _rDescriptor[ daConnectionResource ] >>= sConnectionResource;
65 0 : if ( _rDescriptor.has( daCommand ) ) _rDescriptor[ daCommand ] >>= sCommand;
66 0 : if ( _rDescriptor.has( daColumnName ) ) _rDescriptor[ daColumnName ] >>= sFieldName;
67 :
68 0 : sal_Int32 nCommandType = CommandType::TABLE;
69 0 : OSL_VERIFY( _rDescriptor[ daCommandType ] >>= nCommandType );
70 :
71 :
72 : implConstruct(
73 0 : sDataSource.isEmpty() ? sDatabaseLocation : sDataSource,
74 0 : sConnectionResource, nCommandType, sCommand, sFieldName );
75 :
76 0 : if ( m_nFormatFlags & CTF_COLUMN_DESCRIPTOR )
77 : {
78 0 : if ( _rDescriptor.has( daConnection ) )
79 0 : m_aDescriptor[ daConnection ] = _rDescriptor[ daConnection ];
80 0 : if ( _rDescriptor.has( daColumnObject ) )
81 0 : m_aDescriptor[ daColumnObject ] = _rDescriptor[ daColumnObject ];
82 0 : }
83 0 : }
84 :
85 :
86 0 : OColumnTransferable::OColumnTransferable(const Reference< XPropertySet >& _rxForm,
87 : const OUString& _rFieldName, const Reference< XPropertySet >& _rxColumn,
88 : const Reference< XConnection >& _rxConnection, sal_Int32 _nFormats)
89 0 : :m_nFormatFlags(_nFormats)
90 : {
91 : OSL_ENSURE(_rxForm.is(), "OColumnTransferable::OColumnTransferable: invalid form!");
92 : // collect the necessary information from the form
93 0 : OUString sCommand;
94 0 : sal_Int32 nCommandType = CommandType::TABLE;
95 0 : OUString sDatasource,sURL;
96 :
97 0 : bool bTryToParse = true;
98 : try
99 : {
100 0 : _rxForm->getPropertyValue(FM_PROP_COMMANDTYPE) >>= nCommandType;
101 0 : _rxForm->getPropertyValue(FM_PROP_COMMAND) >>= sCommand;
102 0 : _rxForm->getPropertyValue(FM_PROP_DATASOURCE) >>= sDatasource;
103 0 : _rxForm->getPropertyValue(FM_PROP_URL) >>= sURL;
104 0 : bTryToParse = ::cppu::any2bool(_rxForm->getPropertyValue(FM_PROP_ESCAPE_PROCESSING));
105 : }
106 0 : catch(Exception&)
107 : {
108 : OSL_FAIL("OColumnTransferable::OColumnTransferable: could not collect essential data source attributes !");
109 : }
110 :
111 : // If the data source is an SQL-statement and simple enough (means "select <field list> from <table> where ....")
112 : // we are able to fake the drag information we are about to create.
113 0 : if (bTryToParse && (CommandType::COMMAND == nCommandType))
114 : {
115 : try
116 : {
117 0 : Reference< XTablesSupplier > xSupTab;
118 0 : _rxForm->getPropertyValue("SingleSelectQueryComposer") >>= xSupTab;
119 :
120 0 : if(xSupTab.is())
121 : {
122 0 : Reference< XNameAccess > xNames = xSupTab->getTables();
123 0 : if (xNames.is())
124 : {
125 0 : Sequence< OUString > aTables = xNames->getElementNames();
126 0 : if (1 == aTables.getLength())
127 : {
128 0 : sCommand = aTables[0];
129 0 : nCommandType = CommandType::TABLE;
130 0 : }
131 0 : }
132 0 : }
133 : }
134 0 : catch(Exception&)
135 : {
136 : OSL_FAIL("OColumnTransferable::OColumnTransferable: could not collect essential data source attributes (part two) !");
137 : }
138 : }
139 :
140 0 : implConstruct(sDatasource, sURL,nCommandType, sCommand, _rFieldName);
141 :
142 0 : if ((m_nFormatFlags & CTF_COLUMN_DESCRIPTOR) == CTF_COLUMN_DESCRIPTOR)
143 : {
144 0 : if (_rxColumn.is())
145 0 : m_aDescriptor[daColumnObject] <<= _rxColumn;
146 0 : if (_rxConnection.is())
147 0 : m_aDescriptor[daConnection] <<= _rxConnection;
148 0 : }
149 0 : }
150 :
151 :
152 0 : sal_uInt32 OColumnTransferable::getDescriptorFormatId()
153 : {
154 : static sal_uInt32 s_nFormat = (sal_uInt32)-1;
155 0 : if ((sal_uInt32)-1 == s_nFormat)
156 : {
157 0 : s_nFormat = SotExchange::RegisterFormatName(OUString("application/x-openoffice;windows_formatname=\"dbaccess.ColumnDescriptorTransfer\""));
158 : OSL_ENSURE((sal_uInt32)-1 != s_nFormat, "OColumnTransferable::getDescriptorFormatId: bad exchange id!");
159 : }
160 0 : return s_nFormat;
161 : }
162 :
163 :
164 0 : void OColumnTransferable::implConstruct( const OUString& _rDatasource
165 : ,const OUString& _rConnectionResource
166 : ,const sal_Int32 _nCommandType
167 : ,const OUString& _rCommand
168 : , const OUString& _rFieldName)
169 : {
170 0 : const sal_Unicode cSeparator = sal_Unicode(11);
171 0 : const OUString sSeparator(&cSeparator, 1);
172 :
173 0 : m_sCompatibleFormat = OUString();
174 0 : m_sCompatibleFormat += _rDatasource;
175 0 : m_sCompatibleFormat += sSeparator;
176 0 : m_sCompatibleFormat += _rCommand;
177 0 : m_sCompatibleFormat += sSeparator;
178 :
179 : sal_Unicode cCommandType;
180 0 : switch (_nCommandType)
181 : {
182 : case CommandType::TABLE:
183 0 : cCommandType = '0';
184 0 : break;
185 : case CommandType::QUERY:
186 0 : cCommandType = '1';
187 0 : break;
188 : default:
189 0 : cCommandType = '2';
190 0 : break;
191 : }
192 0 : m_sCompatibleFormat += OUString(&cCommandType, 1);
193 0 : m_sCompatibleFormat += sSeparator;
194 0 : m_sCompatibleFormat += _rFieldName;
195 :
196 0 : m_aDescriptor.clear();
197 0 : if ((m_nFormatFlags & CTF_COLUMN_DESCRIPTOR) == CTF_COLUMN_DESCRIPTOR)
198 : {
199 0 : m_aDescriptor.setDataSource(_rDatasource);
200 0 : if ( !_rConnectionResource.isEmpty() )
201 0 : m_aDescriptor[daConnectionResource] <<= _rConnectionResource;
202 :
203 0 : m_aDescriptor[daCommand] <<= _rCommand;
204 0 : m_aDescriptor[daCommandType] <<= _nCommandType;
205 0 : m_aDescriptor[daColumnName] <<= _rFieldName;
206 0 : }
207 0 : }
208 :
209 :
210 0 : void OColumnTransferable::AddSupportedFormats()
211 : {
212 0 : if (CTF_CONTROL_EXCHANGE & m_nFormatFlags)
213 0 : AddFormat(SOT_FORMATSTR_ID_SBA_CTRLDATAEXCHANGE);
214 :
215 0 : if (CTF_FIELD_DESCRIPTOR & m_nFormatFlags)
216 0 : AddFormat(SOT_FORMATSTR_ID_SBA_FIELDDATAEXCHANGE);
217 :
218 0 : if (CTF_COLUMN_DESCRIPTOR & m_nFormatFlags)
219 0 : AddFormat(getDescriptorFormatId());
220 0 : }
221 :
222 :
223 0 : bool OColumnTransferable::GetData( const DataFlavor& _rFlavor, const OUString& /*rDestDoc*/ )
224 : {
225 0 : const sal_uInt32 nFormatId = SotExchange::GetFormat(_rFlavor);
226 0 : switch (nFormatId)
227 : {
228 : case SOT_FORMATSTR_ID_SBA_FIELDDATAEXCHANGE:
229 : case SOT_FORMATSTR_ID_SBA_CTRLDATAEXCHANGE:
230 0 : return SetString(m_sCompatibleFormat, _rFlavor);
231 : }
232 0 : if (nFormatId == getDescriptorFormatId())
233 0 : return SetAny( makeAny( m_aDescriptor.createPropertyValueSequence() ), _rFlavor );
234 :
235 0 : return false;
236 : }
237 :
238 :
239 0 : bool OColumnTransferable::canExtractColumnDescriptor(const DataFlavorExVector& _rFlavors, sal_Int32 _nFormats)
240 : {
241 0 : bool bFieldFormat = 0 != (_nFormats & CTF_FIELD_DESCRIPTOR);
242 0 : bool bControlFormat = 0 != (_nFormats & CTF_CONTROL_EXCHANGE);
243 0 : bool bDescriptorFormat = 0 != (_nFormats & CTF_COLUMN_DESCRIPTOR);
244 0 : for ( DataFlavorExVector::const_iterator aCheck = _rFlavors.begin();
245 0 : aCheck != _rFlavors.end();
246 : ++aCheck
247 : )
248 : {
249 0 : if (bFieldFormat && (SOT_FORMATSTR_ID_SBA_FIELDDATAEXCHANGE == aCheck->mnSotId))
250 0 : return true;
251 0 : if (bControlFormat && (SOT_FORMATSTR_ID_SBA_CTRLDATAEXCHANGE == aCheck->mnSotId))
252 0 : return true;
253 0 : if (bDescriptorFormat && (getDescriptorFormatId() == aCheck->mnSotId))
254 0 : return true;
255 : }
256 :
257 0 : return false;
258 : }
259 :
260 :
261 0 : ODataAccessDescriptor OColumnTransferable::extractColumnDescriptor(const TransferableDataHelper& _rData)
262 : {
263 0 : if (_rData.HasFormat(getDescriptorFormatId()))
264 : {
265 : // the object has a real descriptor object (not just the old compatible format)
266 :
267 : // extract the any from the transferable
268 0 : DataFlavor aFlavor;
269 : #if OSL_DEBUG_LEVEL > 0
270 : bool bSuccess =
271 : #endif
272 0 : SotExchange::GetFormatDataFlavor(getDescriptorFormatId(), aFlavor);
273 : OSL_ENSURE(bSuccess, "OColumnTransferable::extractColumnDescriptor: invalid data format (no flavor)!");
274 :
275 0 : Any aDescriptor = _rData.GetAny(aFlavor, OUString());
276 :
277 : // extract the property value sequence
278 0 : Sequence< PropertyValue > aDescriptorProps;
279 : #if OSL_DEBUG_LEVEL > 0
280 : bSuccess =
281 : #endif
282 0 : aDescriptor >>= aDescriptorProps;
283 : OSL_ENSURE(bSuccess, "OColumnTransferable::extractColumnDescriptor: invalid clipboard format!");
284 :
285 : // build the real descriptor
286 0 : return ODataAccessDescriptor(aDescriptorProps);
287 : }
288 :
289 : // only the old (compatible) format exists -> use the other extract method ...
290 0 : OUString sDatasource, sCommand, sFieldName,sDatabaseLocation,sConnectionResource;
291 0 : sal_Int32 nCommandType = CommandType::COMMAND;
292 :
293 0 : ODataAccessDescriptor aDescriptor;
294 0 : if (extractColumnDescriptor(_rData, sDatasource, sDatabaseLocation,sConnectionResource,nCommandType, sCommand, sFieldName))
295 : {
296 : // and build an own descriptor
297 0 : if ( !sDatasource.isEmpty() )
298 0 : aDescriptor[daDataSource] <<= sDatasource;
299 0 : if ( !sDatabaseLocation.isEmpty() )
300 0 : aDescriptor[daDatabaseLocation] <<= sDatabaseLocation;
301 0 : if ( !sConnectionResource.isEmpty() )
302 0 : aDescriptor[daConnectionResource] <<= sConnectionResource;
303 :
304 0 : aDescriptor[daCommand] <<= sCommand;
305 0 : aDescriptor[daCommandType] <<= nCommandType;
306 0 : aDescriptor[daColumnName] <<= sFieldName;
307 : }
308 0 : return aDescriptor;
309 : }
310 :
311 :
312 0 : bool OColumnTransferable::extractColumnDescriptor(const TransferableDataHelper& _rData
313 : ,OUString& _rDatasource
314 : ,OUString& _rDatabaseLocation
315 : ,OUString& _rConnectionResource
316 : ,sal_Int32& _nCommandType
317 : ,OUString& _rCommand
318 : ,OUString& _rFieldName)
319 : {
320 0 : if ( _rData.HasFormat(getDescriptorFormatId()) )
321 : {
322 0 : ODataAccessDescriptor aDescriptor = extractColumnDescriptor(_rData);
323 0 : if ( aDescriptor.has(daDataSource) )
324 0 : aDescriptor[daDataSource] >>= _rDatasource;
325 0 : if ( aDescriptor.has(daDatabaseLocation) )
326 0 : aDescriptor[daDatabaseLocation] >>= _rDatabaseLocation;
327 0 : if ( aDescriptor.has(daConnectionResource) )
328 0 : aDescriptor[daConnectionResource] >>= _rConnectionResource;
329 :
330 0 : aDescriptor[daCommand] >>= _rCommand;
331 0 : aDescriptor[daCommandType] >>= _nCommandType;
332 0 : aDescriptor[daColumnName] >>= _rFieldName;
333 0 : return true;
334 : }
335 :
336 : // check if we have a (string) format we can use ....
337 0 : SotFormatStringId nRecognizedFormat = 0;
338 0 : if (_rData.HasFormat(SOT_FORMATSTR_ID_SBA_FIELDDATAEXCHANGE))
339 0 : nRecognizedFormat = SOT_FORMATSTR_ID_SBA_FIELDDATAEXCHANGE;
340 0 : if (_rData.HasFormat(SOT_FORMATSTR_ID_SBA_CTRLDATAEXCHANGE))
341 0 : nRecognizedFormat = SOT_FORMATSTR_ID_SBA_CTRLDATAEXCHANGE;
342 0 : if (!nRecognizedFormat)
343 0 : return false;
344 :
345 0 : OUString sFieldDescription;
346 0 : (void)const_cast<TransferableDataHelper&>(_rData).GetString(nRecognizedFormat, sFieldDescription);
347 :
348 0 : const sal_Unicode cSeparator = sal_Unicode(11);
349 0 : _rDatasource = sFieldDescription.getToken(0, cSeparator);
350 0 : _rCommand = sFieldDescription.getToken(1, cSeparator);
351 0 : _nCommandType = sFieldDescription.getToken(2, cSeparator).toInt32();
352 0 : _rFieldName = sFieldDescription.getToken(3, cSeparator);
353 :
354 0 : return true;
355 : }
356 :
357 :
358 0 : void OColumnTransferable::addDataToContainer( TransferDataContainer* _pContainer )
359 : {
360 : OSL_ENSURE( _pContainer, "OColumnTransferable::addDataToContainer: invalid container!" );
361 0 : if ( _pContainer )
362 : {
363 0 : if ( m_nFormatFlags & CTF_FIELD_DESCRIPTOR )
364 0 : _pContainer->CopyAny( SOT_FORMATSTR_ID_SBA_FIELDDATAEXCHANGE, makeAny( m_sCompatibleFormat ) );
365 :
366 0 : if ( m_nFormatFlags & CTF_CONTROL_EXCHANGE )
367 0 : _pContainer->CopyAny( SOT_FORMATSTR_ID_SBA_CTRLDATAEXCHANGE, makeAny( m_sCompatibleFormat ) );
368 :
369 0 : if ( m_nFormatFlags & CTF_COLUMN_DESCRIPTOR )
370 : {
371 0 : Any aContent = makeAny( m_aDescriptor.createPropertyValueSequence() );
372 : _pContainer->CopyAny(
373 0 : sal::static_int_cast< sal_uInt16 >( getDescriptorFormatId() ),
374 0 : aContent );
375 : }
376 : }
377 0 : }
378 :
379 0 : ODataAccessObjectTransferable::ODataAccessObjectTransferable(
380 : const OUString& _rDatasource
381 : ,const OUString& _rConnectionResource
382 : ,const sal_Int32 _nCommandType
383 : ,const OUString& _rCommand
384 0 : )
385 : {
386 0 : construct(_rDatasource,_rConnectionResource,_nCommandType,_rCommand,NULL,(CommandType::COMMAND == _nCommandType),_rCommand);
387 0 : }
388 :
389 0 : ODataAccessObjectTransferable::ODataAccessObjectTransferable(
390 : const OUString& _rDatasource
391 : ,const OUString& _rConnectionResource
392 : ,const sal_Int32 _nCommandType
393 : ,const OUString& _rCommand
394 0 : ,const Reference< XConnection >& _rxConnection)
395 : {
396 : OSL_ENSURE(_rxConnection.is(),"Wrong ctor used.!");
397 0 : construct(_rDatasource,_rConnectionResource,_nCommandType,_rCommand,_rxConnection,(CommandType::COMMAND == _nCommandType),_rCommand);
398 0 : }
399 :
400 :
401 0 : ODataAccessObjectTransferable::ODataAccessObjectTransferable(const Reference< XPropertySet >& _rxLivingForm)
402 : {
403 : // collect some properties of the form
404 0 : OUString sDatasourceName,sConnectionResource;
405 0 : sal_Int32 nObjectType = CommandType::COMMAND;
406 0 : OUString sObjectName;
407 0 : Reference< XConnection > xConnection;
408 : try
409 : {
410 0 : _rxLivingForm->getPropertyValue(FM_PROP_COMMANDTYPE) >>= nObjectType;
411 0 : _rxLivingForm->getPropertyValue(FM_PROP_COMMAND) >>= sObjectName;
412 0 : _rxLivingForm->getPropertyValue(FM_PROP_DATASOURCE) >>= sDatasourceName;
413 0 : _rxLivingForm->getPropertyValue(FM_PROP_URL) >>= sConnectionResource;
414 0 : _rxLivingForm->getPropertyValue(FM_PROP_ACTIVE_CONNECTION) >>= xConnection;
415 : }
416 0 : catch(Exception&)
417 : {
418 : OSL_FAIL("ODataAccessObjectTransferable::ODataAccessObjectTransferable: could not collect essential form attributes !");
419 0 : return;
420 : }
421 :
422 : // check if the SQL-statement is modified
423 0 : OUString sCompleteStatement;
424 : try
425 : {
426 0 : _rxLivingForm->getPropertyValue(FM_PROP_ACTIVECOMMAND) >>= sCompleteStatement;
427 : }
428 0 : catch (const Exception&)
429 : {
430 : OSL_FAIL("ODataAccessObjectTransferable::ODataAccessObjectTransferable: could not collect essential form attributes (part two) !");
431 0 : return;
432 : }
433 :
434 : construct( sDatasourceName
435 : ,sConnectionResource
436 : ,nObjectType
437 : ,sObjectName,xConnection
438 : ,!((CommandType::QUERY == nObjectType))
439 0 : ,sCompleteStatement);
440 : }
441 :
442 :
443 0 : void ODataAccessObjectTransferable::AddSupportedFormats()
444 : {
445 0 : sal_Int32 nObjectType = CommandType::COMMAND;
446 0 : m_aDescriptor[daCommandType] >>= nObjectType;
447 0 : switch (nObjectType)
448 : {
449 : case CommandType::TABLE:
450 0 : AddFormat(SOT_FORMATSTR_ID_DBACCESS_TABLE);
451 0 : break;
452 : case CommandType::QUERY:
453 0 : AddFormat(SOT_FORMATSTR_ID_DBACCESS_QUERY);
454 0 : break;
455 : case CommandType::COMMAND:
456 0 : AddFormat(SOT_FORMATSTR_ID_DBACCESS_COMMAND);
457 0 : break;
458 : }
459 :
460 0 : if (!m_sCompatibleObjectDescription.isEmpty())
461 0 : AddFormat(SOT_FORMATSTR_ID_SBA_DATAEXCHANGE);
462 0 : }
463 :
464 :
465 0 : bool ODataAccessObjectTransferable::GetData( const DataFlavor& rFlavor, const OUString& /*rDestDoc*/ )
466 : {
467 0 : sal_uIntPtr nFormat = SotExchange::GetFormat(rFlavor);
468 0 : switch (nFormat)
469 : {
470 : case SOT_FORMATSTR_ID_DBACCESS_TABLE:
471 : case SOT_FORMATSTR_ID_DBACCESS_QUERY:
472 : case SOT_FORMATSTR_ID_DBACCESS_COMMAND:
473 0 : return SetAny( makeAny(m_aDescriptor.createPropertyValueSequence()), rFlavor );
474 :
475 : case SOT_FORMATSTR_ID_SBA_DATAEXCHANGE:
476 0 : return SetString(m_sCompatibleObjectDescription, rFlavor);
477 : }
478 0 : return false;
479 : }
480 :
481 :
482 0 : bool ODataAccessObjectTransferable::canExtractObjectDescriptor(const DataFlavorExVector& _rFlavors)
483 : {
484 0 : for ( DataFlavorExVector::const_iterator aCheck = _rFlavors.begin();
485 0 : aCheck != _rFlavors.end();
486 : ++aCheck
487 : )
488 : {
489 0 : if (SOT_FORMATSTR_ID_DBACCESS_TABLE == aCheck->mnSotId)
490 0 : return true;
491 0 : if (SOT_FORMATSTR_ID_DBACCESS_QUERY == aCheck->mnSotId)
492 0 : return true;
493 0 : if (SOT_FORMATSTR_ID_DBACCESS_COMMAND == aCheck->mnSotId)
494 0 : return true;
495 : }
496 0 : return false;
497 : }
498 :
499 :
500 0 : ODataAccessDescriptor ODataAccessObjectTransferable::extractObjectDescriptor(const TransferableDataHelper& _rData)
501 : {
502 0 : sal_Int32 nKnownFormatId = 0;
503 0 : if ( _rData.HasFormat( SOT_FORMATSTR_ID_DBACCESS_TABLE ) )
504 0 : nKnownFormatId = SOT_FORMATSTR_ID_DBACCESS_TABLE;
505 0 : if ( _rData.HasFormat( SOT_FORMATSTR_ID_DBACCESS_QUERY ) )
506 0 : nKnownFormatId = SOT_FORMATSTR_ID_DBACCESS_QUERY;
507 0 : if ( _rData.HasFormat( SOT_FORMATSTR_ID_DBACCESS_COMMAND ) )
508 0 : nKnownFormatId = SOT_FORMATSTR_ID_DBACCESS_COMMAND;
509 :
510 0 : if (0 != nKnownFormatId)
511 : {
512 : // extract the any from the transferable
513 0 : DataFlavor aFlavor;
514 : #if OSL_DEBUG_LEVEL > 0
515 : bool bSuccess =
516 : #endif
517 0 : SotExchange::GetFormatDataFlavor(nKnownFormatId, aFlavor);
518 : OSL_ENSURE(bSuccess, "OColumnTransferable::extractColumnDescriptor: invalid data format (no flavor)!");
519 :
520 0 : Any aDescriptor = _rData.GetAny(aFlavor, OUString());
521 :
522 : // extract the property value sequence
523 0 : Sequence< PropertyValue > aDescriptorProps;
524 : #if OSL_DEBUG_LEVEL > 0
525 : bSuccess =
526 : #endif
527 0 : aDescriptor >>= aDescriptorProps;
528 : OSL_ENSURE(bSuccess, "OColumnTransferable::extractColumnDescriptor: invalid clipboard format!");
529 :
530 : // build the real descriptor
531 0 : return ODataAccessDescriptor(aDescriptorProps);
532 : }
533 :
534 : OSL_FAIL( "OColumnTransferable::extractColumnDescriptor: unsupported formats only!" );
535 0 : return ODataAccessDescriptor();
536 : }
537 :
538 :
539 0 : void ODataAccessObjectTransferable::addCompatibleSelectionDescription( const Sequence< Any >& _rSelRows )
540 : {
541 0 : const sal_Unicode cSeparator(11);
542 0 : const OUString sSeparator(&cSeparator, 1);
543 :
544 0 : const Any* pSelRows = _rSelRows.getConstArray();
545 0 : const Any* pSelRowsEnd = pSelRows + _rSelRows.getLength();
546 0 : for ( ; pSelRows < pSelRowsEnd; ++pSelRows )
547 : {
548 0 : sal_Int32 nSelectedRow( 0 );
549 0 : OSL_VERIFY( *pSelRows >>= nSelectedRow );
550 :
551 0 : m_sCompatibleObjectDescription += OUString::number(nSelectedRow);
552 0 : m_sCompatibleObjectDescription += sSeparator;
553 0 : }
554 0 : }
555 :
556 :
557 0 : void ODataAccessObjectTransferable::ObjectReleased()
558 : {
559 0 : m_aDescriptor.clear();
560 0 : }
561 :
562 0 : void ODataAccessObjectTransferable::construct( const OUString& _rDatasource
563 : ,const OUString& _rConnectionResource
564 : ,const sal_Int32 _nCommandType
565 : ,const OUString& _rCommand
566 : ,const Reference< XConnection >& _rxConnection
567 : ,bool _bAddCommand
568 : ,const OUString& _sActiveCommand)
569 : {
570 0 : m_aDescriptor.setDataSource(_rDatasource);
571 : // build the descriptor (the property sequence)
572 0 : if ( !_rConnectionResource.isEmpty() )
573 0 : m_aDescriptor[daConnectionResource] <<= _rConnectionResource;
574 0 : if ( _rxConnection.is() )
575 0 : m_aDescriptor[daConnection] <<= _rxConnection;
576 0 : m_aDescriptor[daCommand] <<= _rCommand;
577 0 : m_aDescriptor[daCommandType] <<= _nCommandType;
578 :
579 : // extract the single values from the sequence
580 :
581 0 : OUString sObjectName;
582 0 : OUString sDatasourceName = _rDatasource;
583 0 : sObjectName = _rCommand;
584 :
585 : // for compatibility: create a string which can be used for the SOT_FORMATSTR_ID_SBA_DATAEXCHANGE format
586 :
587 0 : bool bTreatAsStatement = (CommandType::COMMAND == _nCommandType);
588 : // statements are - in this old and ugly format - described as queries
589 :
590 0 : const sal_Unicode cSeparator = sal_Unicode(11);
591 0 : const OUString sSeparator(&cSeparator, 1);
592 :
593 0 : const sal_Unicode cTableMark = '1';
594 0 : const sal_Unicode cQueryMark = '0';
595 :
596 : // build the descriptor string
597 0 : m_sCompatibleObjectDescription += sDatasourceName;
598 0 : m_sCompatibleObjectDescription += sSeparator;
599 0 : m_sCompatibleObjectDescription += bTreatAsStatement ? OUString() : sObjectName;
600 0 : m_sCompatibleObjectDescription += sSeparator;
601 0 : switch (_nCommandType)
602 : {
603 : case CommandType::TABLE:
604 0 : m_sCompatibleObjectDescription += OUString(&cTableMark, 1);
605 0 : break;
606 : case CommandType::QUERY:
607 0 : m_sCompatibleObjectDescription += OUString(&cQueryMark, 1);
608 0 : break;
609 : case CommandType::COMMAND:
610 0 : m_sCompatibleObjectDescription += OUString(&cQueryMark, 1);
611 : // think of it as a query
612 0 : break;
613 : }
614 0 : m_sCompatibleObjectDescription += sSeparator;
615 0 : m_sCompatibleObjectDescription += _bAddCommand ? _sActiveCommand : OUString();
616 0 : m_sCompatibleObjectDescription += sSeparator;
617 0 : }
618 :
619 :
620 0 : OMultiColumnTransferable::OMultiColumnTransferable(const Sequence< PropertyValue >& _aDescriptors) : m_aDescriptors(_aDescriptors)
621 : {
622 0 : }
623 :
624 0 : sal_uInt32 OMultiColumnTransferable::getDescriptorFormatId()
625 : {
626 : static sal_uInt32 s_nFormat = (sal_uInt32)-1;
627 0 : if ((sal_uInt32)-1 == s_nFormat)
628 : {
629 0 : s_nFormat = SotExchange::RegisterFormatName(OUString("application/x-openoffice;windows_formatname=\"dbaccess.MultipleColumnDescriptorTransfer\""));
630 : OSL_ENSURE((sal_uInt32)-1 != s_nFormat, "OColumnTransferable::getDescriptorFormatId: bad exchange id!");
631 : }
632 0 : return s_nFormat;
633 : }
634 :
635 0 : void OMultiColumnTransferable::AddSupportedFormats()
636 : {
637 0 : AddFormat(getDescriptorFormatId());
638 0 : }
639 :
640 0 : bool OMultiColumnTransferable::GetData( const DataFlavor& _rFlavor, const OUString& /*rDestDoc*/ )
641 : {
642 0 : const sal_uInt32 nFormatId = SotExchange::GetFormat(_rFlavor);
643 0 : if (nFormatId == getDescriptorFormatId())
644 : {
645 0 : return SetAny( makeAny( m_aDescriptors ), _rFlavor );
646 : }
647 :
648 0 : return false;
649 : }
650 :
651 :
652 0 : bool OMultiColumnTransferable::canExtractDescriptor(const DataFlavorExVector& _rFlavors)
653 : {
654 0 : DataFlavorExVector::const_iterator aCheck = _rFlavors.begin();
655 0 : for ( ;
656 0 : aCheck != _rFlavors.end() && getDescriptorFormatId() == aCheck->mnSotId;
657 : ++aCheck
658 : )
659 : ;
660 :
661 0 : return aCheck == _rFlavors.end();
662 : }
663 :
664 :
665 0 : Sequence< PropertyValue > OMultiColumnTransferable::extractDescriptor(const TransferableDataHelper& _rData)
666 : {
667 0 : Sequence< PropertyValue > aList;
668 0 : if (_rData.HasFormat(getDescriptorFormatId()))
669 : {
670 : // extract the any from the transferable
671 0 : DataFlavor aFlavor;
672 : #if OSL_DEBUG_LEVEL > 0
673 : bool bSuccess =
674 : #endif
675 0 : SotExchange::GetFormatDataFlavor(getDescriptorFormatId(), aFlavor);
676 : OSL_ENSURE(bSuccess, "OColumnTransferable::extractColumnDescriptor: invalid data format (no flavor)!");
677 :
678 0 : _rData.GetAny(aFlavor, OUString()) >>= aList;
679 : } // if (_rData.HasFormat(getDescriptorFormatId()))
680 0 : return aList;
681 : }
682 :
683 0 : void OMultiColumnTransferable::ObjectReleased()
684 : {
685 0 : m_aDescriptors.realloc(0);
686 0 : }
687 :
688 :
689 : } // namespace svx
690 :
691 :
692 :
693 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|