1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/config.h>
#include <config_features.h>
#include <config_fuzzers.h>
#include "Filter.hxx"
#include <strings.hrc>
#include <frm_resource.hxx>
#include <frm_strings.hxx>
#include <com/sun/star/awt/VclWindowPeerAttribute.hpp>
#include <com/sun/star/awt/XCheckBox.hpp>
#include <com/sun/star/awt/XComboBox.hpp>
#include <com/sun/star/awt/XListBox.hpp>
#include <com/sun/star/awt/XRadioButton.hpp>
#include <com/sun/star/awt/XVclWindowPeer.hpp>
#include <com/sun/star/beans/NamedValue.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/container/XChild.hpp>
#include <com/sun/star/container/XIndexAccess.hpp>
#include <com/sun/star/container/XNamed.hpp>
#include <com/sun/star/form/FormComponentType.hpp>
#include <com/sun/star/sdb/ErrorMessageDialog.hpp>
#include <com/sun/star/sdbc/XConnection.hpp>
#include <com/sun/star/sdbc/XRowSet.hpp>
#include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
#include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
#include <com/sun/star/util/NumberFormatter.hpp>
#include <com/sun/star/awt/XItemList.hpp>
#include <comphelper/property.hxx>
#include <comphelper/types.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <connectivity/dbtools.hxx>
#include <connectivity/formattedcolumnvalue.hxx>
#include <connectivity/predicateinput.hxx>
#include <o3tl/safeint.hxx>
#include <rtl/ustrbuf.hxx>
#include <comphelper/diagnose_ex.hxx>
#include <tools/gen.hxx>
namespace frm
{
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::awt;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::sdb;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::sdbcx;
using namespace ::com::sun::star::util;
using namespace ::com::sun::star::form;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::ui::dialogs;
using namespace ::connectivity;
OFilterControl::OFilterControl( const Reference< XComponentContext >& _rxORB )
:m_aTextListeners( *this )
,m_xContext( _rxORB )
,m_nControlClass( FormComponentType::TEXTFIELD )
,m_bFilterList( false )
,m_bMultiLine( false )
,m_bFilterListFilled( false )
{
}
bool OFilterControl::ensureInitialized( )
{
#if HAVE_FEATURE_DBCONNECTIVITY && !ENABLE_FUZZERS
if ( !m_xField.is() )
{
OSL_FAIL( "OFilterControl::ensureInitialized: improperly initialized: no field!" );
return false;
}
if ( !m_xConnection.is() )
{
OSL_FAIL( "OFilterControl::ensureInitialized: improperly initialized: no connection!" );
return false;
}
if ( !m_xFormatter.is() )
{
// we can create one from the connection, if it's an SDB connection
Reference< XNumberFormatsSupplier > xFormatSupplier = ::dbtools::getNumberFormats( m_xConnection, true, m_xContext );
if ( xFormatSupplier.is() )
{
m_xFormatter.set(NumberFormatter::create(m_xContext), UNO_QUERY_THROW );
m_xFormatter->attachNumberFormatsSupplier( xFormatSupplier );
}
}
if ( !m_xFormatter.is() )
{
OSL_FAIL( "OFilterControl::ensureInitialized: no number formatter!" );
// no fallback anymore
return false;
}
#endif
return true;
}
Any SAL_CALL OFilterControl::queryAggregation( const Type & rType )
{
Any aRet = UnoControl::queryAggregation( rType);
if(!aRet.hasValue())
aRet = OFilterControl_BASE::queryInterface(rType);
return aRet;
}
OUString OFilterControl::GetComponentServiceName() const
{
OUString aServiceName;
switch (m_nControlClass)
{
case FormComponentType::RADIOBUTTON:
aServiceName = "radiobutton";
break;
case FormComponentType::CHECKBOX:
aServiceName = "checkbox";
break;
case FormComponentType::COMBOBOX:
aServiceName = "combobox";
break;
case FormComponentType::LISTBOX:
aServiceName = "listbox";
break;
default:
if (m_bMultiLine)
aServiceName = "MultiLineEdit";
else
aServiceName = "Edit";
}
return aServiceName;
}
// XComponent
void OFilterControl::dispose()
{
EventObject aEvt(*this);
m_aTextListeners.disposeAndClear( aEvt );
UnoControl::dispose();
}
void OFilterControl::createPeer( const Reference< XToolkit > & rxToolkit, const Reference< XWindowPeer > & rParentPeer )
{
UnoControl::createPeer( rxToolkit, rParentPeer );
try
{
Reference< XVclWindowPeer > xVclWindow( getPeer(), UNO_QUERY_THROW );
switch ( m_nControlClass )
{
case FormComponentType::CHECKBOX:
{
// checkboxes always have a tristate-mode
xVclWindow->setProperty( PROPERTY_TRISTATE, Any( true ) );
xVclWindow->setProperty( PROPERTY_STATE, Any( sal_Int32( TRISTATE_INDET ) ) );
Reference< XCheckBox > xBox( getPeer(), UNO_QUERY_THROW );
xBox->addItemListener( this );
}
break;
case FormComponentType::RADIOBUTTON:
{
xVclWindow->setProperty( PROPERTY_STATE, Any( sal_Int32( TRISTATE_FALSE ) ) );
Reference< XRadioButton > xRadio( getPeer(), UNO_QUERY_THROW );
xRadio->addItemListener( this );
}
break;
case FormComponentType::LISTBOX:
{
Reference< XListBox > xListBox( getPeer(), UNO_QUERY_THROW );
xListBox->addItemListener( this );
[[fallthrough]];
}
case FormComponentType::COMBOBOX:
{
xVclWindow->setProperty(PROPERTY_AUTOCOMPLETE, Any( true ) );
[[fallthrough]];
}
default:
{
Reference< XWindow > xWindow( getPeer(), UNO_QUERY );
xWindow->addFocusListener( this );
Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
if (xText.is())
xText->setMaxTextLen(0);
}
break;
}
// filter controls are _never_ readonly
Reference< XPropertySet > xModel( getModel(), UNO_QUERY_THROW );
Reference< XPropertySetInfo > xModelPSI( xModel->getPropertySetInfo(), UNO_SET_THROW );
if ( xModelPSI->hasPropertyByName( PROPERTY_READONLY ) )
xVclWindow->setProperty( PROPERTY_READONLY, Any( false ) );
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("forms.component");
}
if (m_bFilterList)
m_bFilterListFilled = false;
}
void OFilterControl::PrepareWindowDescriptor( WindowDescriptor& rDescr )
{
if (m_bFilterList)
rDescr.WindowAttributes |= VclWindowPeerAttribute::DROPDOWN;
}
void OFilterControl::ImplSetPeerProperty( const OUString& rPropName, const Any& rVal )
{
// these properties are ignored
if (rPropName == PROPERTY_TEXT ||
rPropName == PROPERTY_STATE)
return;
UnoControl::ImplSetPeerProperty( rPropName, rVal );
}
// XEventListener
void SAL_CALL OFilterControl::disposing(const EventObject& Source)
{
UnoControl::disposing(Source);
}
// XItemListener
void SAL_CALL OFilterControl::itemStateChanged( const ItemEvent& rEvent )
{
#if !HAVE_FEATURE_DBCONNECTIVITY || ENABLE_FUZZERS
(void) rEvent;
#else
OUStringBuffer aText;
switch (m_nControlClass)
{
case FormComponentType::CHECKBOX:
{
if ( ( rEvent.Selected == TRISTATE_TRUE ) || ( rEvent.Selected == TRISTATE_FALSE ) )
{
sal_Int32 nBooleanComparisonMode = ::dbtools::DatabaseMetaData( m_xConnection ).getBooleanComparisonMode();
bool bSelected = ( rEvent.Selected == TRISTATE_TRUE );
OUString sExpressionMarker( "$expression$" );
::dbtools::getBooleanComparisonPredicate(
sExpressionMarker,
bSelected,
nBooleanComparisonMode,
aText
);
OUString sText( aText.makeStringAndClear() );
sal_Int32 nMarkerPos( sText.indexOf( sExpressionMarker ) );
OSL_ENSURE( nMarkerPos == 0, "OFilterControl::itemStateChanged: unsupported boolean comparison mode!" );
// If this assertion fails, then getBooleanComparisonPredicate created a predicate which
// does not start with the expression we gave it. The only known case is when
// the comparison mode is ACCESS_COMPAT, and the value is TRUE. In this case,
// the expression is rather complex.
// Well, so this is a known issue - the filter controls (and thus the form based filter)
// do not work with boolean MS Access fields.
// To fix this, we would probably have to revert here to always return "1" or "0" as normalized
// filter, and change our client code to properly translate this (which could be some effort).
if ( nMarkerPos == 0 )
aText.append( sText.subView(sExpressionMarker.getLength()) );
else
{
// fallback
aText.appendAscii( bSelected ? "1" : "0" );
}
}
}
break;
case FormComponentType::LISTBOX:
{
try
{
const Reference< XItemList > xItemList( getModel(), UNO_QUERY_THROW );
OUString sItemText( xItemList->getItemText( rEvent.Selected ) );
const MapString2String::const_iterator itemPos = m_aDisplayItemToValueItem.find( sItemText );
if ( itemPos != m_aDisplayItemToValueItem.end() )
{
sItemText = itemPos->second;
if ( !sItemText.isEmpty() )
{
::dbtools::OPredicateInputController aPredicateInput( m_xContext, m_xConnection, getParseContext() );
OUString sErrorMessage;
OSL_VERIFY( aPredicateInput.normalizePredicateString( sItemText, m_xField, &sErrorMessage ) );
}
}
aText.append( sItemText );
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("forms.component");
}
}
break;
case FormComponentType::RADIOBUTTON:
{
if ( rEvent.Selected == TRISTATE_TRUE )
aText.append( ::comphelper::getString( Reference< XPropertySet >( getModel(), UNO_QUERY_THROW )->getPropertyValue( PROPERTY_REFVALUE ) ) );
}
break;
}
OUString sText( aText.makeStringAndClear() );
if ( m_aText != sText )
{
m_aText = sText;
TextEvent aEvt;
aEvt.Source = *this;
m_aTextListeners.notifyEach(&css::awt::XTextListener::textChanged, aEvt);
}
#endif
}
void OFilterControl::implInitFilterList()
{
#if HAVE_FEATURE_DBCONNECTIVITY && !ENABLE_FUZZERS
if ( !ensureInitialized( ) )
// already asserted in ensureInitialized
return;
// ensure the cursor and the statement are disposed as soon as we leave
::utl::SharedUNOComponent< XResultSet > xListCursor;
::utl::SharedUNOComponent< XStatement > xStatement;
try
{
m_bFilterListFilled = true;
if ( !m_xField.is() )
return;
OUString sFieldName;
m_xField->getPropertyValue( PROPERTY_NAME ) >>= sFieldName;
// here we need a table to which the field belongs to
const Reference< XChild > xModelAsChild( getModel(), UNO_QUERY_THROW );
const Reference< XRowSet > xForm( xModelAsChild->getParent(), UNO_QUERY_THROW );
const Reference< XPropertySet > xFormProps( xForm, UNO_QUERY_THROW );
// create a query composer
Reference< XColumnsSupplier > xSuppColumns;
xFormProps->getPropertyValue("SingleSelectQueryComposer") >>= xSuppColumns;
const Reference< XConnection > xConnection( ::dbtools::getConnection( xForm ), UNO_SET_THROW );
const Reference< XNameAccess > xFieldNames( xSuppColumns->getColumns(), UNO_SET_THROW );
if ( !xFieldNames->hasByName( sFieldName ) )
return;
OUString sRealFieldName, sTableName;
const Reference< XPropertySet > xComposerFieldProps( xFieldNames->getByName( sFieldName ), UNO_QUERY_THROW );
xComposerFieldProps->getPropertyValue( PROPERTY_REALNAME ) >>= sRealFieldName;
xComposerFieldProps->getPropertyValue( PROPERTY_TABLENAME ) >>= sTableName;
// obtain the table of the field
const Reference< XTablesSupplier > xSuppTables( xSuppColumns, UNO_QUERY_THROW );
const Reference< XNameAccess > xTablesNames( xSuppTables->getTables(), UNO_SET_THROW );
const Reference< XNamed > xNamedTable( xTablesNames->getByName( sTableName ), UNO_QUERY_THROW );
sTableName = xNamedTable->getName();
// create a statement selecting all values for the given field
OUStringBuffer aStatement;
const Reference< XDatabaseMetaData > xMeta( xConnection->getMetaData(), UNO_SET_THROW );
const OUString sQuoteChar = xMeta->getIdentifierQuoteString();
aStatement.append( "SELECT DISTINCT " );
aStatement.append( sQuoteChar );
aStatement.append( sRealFieldName );
aStatement.append( sQuoteChar );
// if the field had an alias in our form's statement, give it this alias in the new statement, too
if ( !sFieldName.isEmpty() && ( sFieldName != sRealFieldName ) )
{
aStatement.append(" AS ");
aStatement.append( sQuoteChar );
aStatement.append( sFieldName );
aStatement.append( sQuoteChar );
}
aStatement.append( " FROM " );
OUString sCatalog, sSchema, sTable;
::dbtools::qualifiedNameComponents( xMeta, sTableName, sCatalog, sSchema, sTable, ::dbtools::EComposeRule::InDataManipulation );
aStatement.append( ::dbtools::composeTableNameForSelect( xConnection, sCatalog, sSchema, sTable ) );
// execute the statement
xStatement.reset( xConnection->createStatement() );
const OUString sSelectStatement( aStatement.makeStringAndClear( ) );
xListCursor.reset( xStatement->executeQuery( sSelectStatement ) );
// retrieve the one column which we take the values from
const Reference< XColumnsSupplier > xSupplyCols( xListCursor, UNO_QUERY_THROW );
const Reference< XIndexAccess > xFields( xSupplyCols->getColumns(), UNO_QUERY_THROW );
const Reference< XPropertySet > xDataField( xFields->getByIndex(0), UNO_QUERY_THROW );
// ensure the values will be formatted according to the field format
const ::dbtools::FormattedColumnValue aFormatter( m_xFormatter, xDataField );
::std::vector< OUString > aProposals;
aProposals.reserve(16);
while ( xListCursor->next() && ( aProposals.size() < o3tl::make_unsigned( SHRT_MAX ) ) )
{
const OUString sCurrentValue = aFormatter.getFormattedValue();
aProposals.push_back( sCurrentValue );
}
// fill the list items into our peer
Sequence< OUString> aStringSeq( comphelper::containerToSequence(aProposals) );
const Reference< XComboBox > xComboBox( getPeer(), UNO_QUERY_THROW );
xComboBox->addItems( aStringSeq, 0 );
// set the drop down line count to something reasonable
const sal_Int16 nLineCount = ::std::min( sal_Int16( 16 ), sal_Int16( aStringSeq.getLength() ) );
xComboBox->setDropDownLineCount( nLineCount );
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("forms.component");
}
#endif
}
// XFocusListener
void SAL_CALL OFilterControl::focusGained(const FocusEvent& /*e*/)
{
// should we fill the combobox?
if (m_bFilterList && !m_bFilterListFilled)
implInitFilterList();
}
void SAL_CALL OFilterControl::focusLost(const FocusEvent& /*e*/)
{
}
sal_Bool SAL_CALL OFilterControl::commit()
{
#if HAVE_FEATURE_DBCONNECTIVITY && !ENABLE_FUZZERS
if ( !ensureInitialized( ) )
// already asserted in ensureInitialized
return true;
OUString aText;
switch (m_nControlClass)
{
case FormComponentType::TEXTFIELD:
case FormComponentType::COMBOBOX:
{
Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
if (xText.is())
aText = xText->getText();
} break;
default:
return true;
}
if ( m_aText == aText )
return true;
// check the text with the SQL-Parser
OUString aNewText = aText.trim();
if ( !aNewText.isEmpty() )
{
::dbtools::OPredicateInputController aPredicateInput( m_xContext, m_xConnection, getParseContext() );
OUString sErrorMessage;
if ( !aPredicateInput.normalizePredicateString( aNewText, m_xField, &sErrorMessage ) )
{
// display the error and outta here
SQLContext aError;
aError.Message = ResourceManager::loadString(RID_STR_SYNTAXERROR);
aError.Details = sErrorMessage;
displayException( aError );
return false;
}
}
setText(aNewText);
TextEvent aEvt;
aEvt.Source = *this;
m_aTextListeners.notifyEach(&css::awt::XTextListener::textChanged, aEvt);
#endif
return true;
}
// XTextComponent
void SAL_CALL OFilterControl::addTextListener(const Reference< XTextListener > & l)
{
m_aTextListeners.addInterface( l );
}
void SAL_CALL OFilterControl::removeTextListener(const Reference< XTextListener > & l)
{
m_aTextListeners.removeInterface( l );
}
void SAL_CALL OFilterControl::setText( const OUString& aText )
{
if ( !ensureInitialized( ) )
// already asserted in ensureInitialized
return;
switch (m_nControlClass)
{
case FormComponentType::CHECKBOX:
{
Reference< XVclWindowPeer > xVclWindow( getPeer(), UNO_QUERY );
if (xVclWindow.is())
{
Any aValue;
if ( aText == "1"
|| aText.equalsIgnoreAsciiCase("TRUE")
|| aText.equalsIgnoreAsciiCase("IS TRUE")
)
{
aValue <<= sal_Int32(TRISTATE_TRUE);
}
else if ( aText == "0" || aText.equalsIgnoreAsciiCase("FALSE") )
{
aValue <<= sal_Int32(TRISTATE_FALSE);
}
else
aValue <<= sal_Int32(TRISTATE_INDET);
m_aText = aText;
xVclWindow->setProperty( PROPERTY_STATE, aValue );
}
} break;
case FormComponentType::RADIOBUTTON:
{
Reference< XVclWindowPeer > xVclWindow( getPeer(), UNO_QUERY );
if (xVclWindow.is())
{
OUString aRefText = ::comphelper::getString(css::uno::Reference< XPropertySet > (getModel(), UNO_QUERY_THROW)->getPropertyValue(PROPERTY_REFVALUE));
Any aValue;
if (aText == aRefText)
aValue <<= sal_Int32(TRISTATE_TRUE);
else
aValue <<= sal_Int32(TRISTATE_FALSE);
m_aText = aText;
xVclWindow->setProperty(PROPERTY_STATE, aValue);
}
} break;
case FormComponentType::LISTBOX:
{
Reference< XListBox > xListBox( getPeer(), UNO_QUERY );
if (xListBox.is())
{
m_aText = aText;
MapString2String::const_iterator itemPos = m_aDisplayItemToValueItem.find( m_aText );
if ( itemPos == m_aDisplayItemToValueItem.end() )
{
const bool isQuoted = ( m_aText.getLength() > 1 )
&& ( m_aText[0] == '\'' )
&& ( m_aText[ m_aText.getLength() - 1 ] == '\'' );
if ( isQuoted )
{
m_aText = m_aText.copy( 1, m_aText.getLength() - 2 );
itemPos = m_aDisplayItemToValueItem.find( m_aText );
}
}
OSL_ENSURE( ( itemPos != m_aDisplayItemToValueItem.end() ) || m_aText.isEmpty(),
"OFilterControl::setText: this text is not in my display list!" );
if ( itemPos == m_aDisplayItemToValueItem.end() )
m_aText.clear();
if ( m_aText.isEmpty() )
{
while ( xListBox->getSelectedItemPos() >= 0 )
{
xListBox->selectItemPos( xListBox->getSelectedItemPos(), false );
}
}
else
{
xListBox->selectItem( m_aText, true );
}
}
}
break;
default:
{
Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
if (xText.is())
{
m_aText = aText;
xText->setText(aText);
}
}
}
}
void SAL_CALL OFilterControl::insertText( const css::awt::Selection& rSel, const OUString& aText )
{
Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
if (xText.is())
{
xText->insertText(rSel, aText);
m_aText = xText->getText();
}
}
OUString SAL_CALL OFilterControl::getText()
{
return m_aText;
}
OUString SAL_CALL OFilterControl::getSelectedText()
{
OUString aSelected;
Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
if (xText.is())
aSelected = xText->getSelectedText();
return aSelected;
}
void SAL_CALL OFilterControl::setSelection( const css::awt::Selection& aSelection )
{
Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
if (xText.is())
xText->setSelection( aSelection );
}
css::awt::Selection SAL_CALL OFilterControl::getSelection()
{
css::awt::Selection aSel;
Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
if (xText.is())
aSel = xText->getSelection();
return aSel;
}
sal_Bool SAL_CALL OFilterControl::isEditable()
{
Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
return xText.is() && xText->isEditable();
}
void SAL_CALL OFilterControl::setEditable( sal_Bool bEditable )
{
Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
if (xText.is())
xText->setEditable(bEditable);
}
sal_Int16 SAL_CALL OFilterControl::getMaxTextLen()
{
Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
return xText.is() ? xText->getMaxTextLen() : 0;
}
void SAL_CALL OFilterControl::setMaxTextLen( sal_Int16 nLength )
{
Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
if (xText.is())
xText->setMaxTextLen(nLength);
}
void OFilterControl::displayException( const css::sdb::SQLContext& _rExcept )
{
try
{
Reference< XExecutableDialog > xErrorDialog = ErrorMessageDialog::create( m_xContext, "", m_xMessageParent, Any(_rExcept));
xErrorDialog->execute();
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("forms.component");
}
}
void SAL_CALL OFilterControl::initialize( const Sequence< Any >& aArguments )
{
const Any* pArguments = aArguments.getConstArray();
const Any* pArgumentsEnd = pArguments + aArguments.getLength();
PropertyValue aProp;
NamedValue aValue;
const OUString* pName = nullptr;
const Any* pValue = nullptr;
Reference< XPropertySet > xControlModel;
if (aArguments.getLength() == 3
&& (aArguments[0] >>= m_xMessageParent)
&& (aArguments[1] >>= m_xFormatter)
&& (aArguments[2] >>= xControlModel))
{
initControlModel(xControlModel);
}
else for ( ; pArguments != pArgumentsEnd; ++pArguments )
{
// we recognize PropertyValues and NamedValues
if ( *pArguments >>= aProp )
{
pName = &aProp.Name;
pValue = &aProp.Value;
}
else if ( *pArguments >>= aValue )
{
pName = &aValue.Name;
pValue = &aValue.Value;
}
else
{
OSL_FAIL( "OFilterControl::initialize: unrecognized argument!" );
continue;
}
if ( *pName == "MessageParent" )
{
// the message parent
*pValue >>= m_xMessageParent;
OSL_ENSURE( m_xMessageParent.is(), "OFilterControl::initialize: invalid MessageParent!" );
}
else if ( *pName == "NumberFormatter" )
{
// the number format. This argument is optional.
*pValue >>= m_xFormatter;
OSL_ENSURE( m_xFormatter.is(), "OFilterControl::initialize: invalid NumberFormatter!" );
}
else if ( *pName == "ControlModel" )
{
// the control model for which we act as filter control
if ( !(*pValue >>= xControlModel ) )
{
OSL_FAIL( "OFilterControl::initialize: invalid control model argument!" );
continue;
}
initControlModel(xControlModel);
}
}
}
void OFilterControl::initControlModel(Reference< XPropertySet > const & xControlModel)
{
#if !HAVE_FEATURE_DBCONNECTIVITY || ENABLE_FUZZERS
(void) xControlModel;
#else
if ( !xControlModel.is() )
{
OSL_FAIL( "OFilterControl::initialize: invalid control model argument!" );
return;
}
// some properties which are "derived" from the control model we're working for
// the field
m_xField.clear();
OSL_ENSURE( ::comphelper::hasProperty( PROPERTY_BOUNDFIELD, xControlModel ), "OFilterControl::initialize: control model needs a bound field property!" );
xControlModel->getPropertyValue( PROPERTY_BOUNDFIELD ) >>= m_xField;
// filter list and control class
m_bFilterList = ::comphelper::hasProperty( PROPERTY_FILTERPROPOSAL, xControlModel ) && ::comphelper::getBOOL( xControlModel->getPropertyValue( PROPERTY_FILTERPROPOSAL ) );
if ( m_bFilterList )
m_nControlClass = FormComponentType::COMBOBOX;
else
{
sal_Int16 nClassId = ::comphelper::getINT16( xControlModel->getPropertyValue( PROPERTY_CLASSID ) );
switch (nClassId)
{
case FormComponentType::CHECKBOX:
case FormComponentType::RADIOBUTTON:
case FormComponentType::LISTBOX:
case FormComponentType::COMBOBOX:
m_nControlClass = nClassId;
if ( FormComponentType::LISTBOX == nClassId )
{
Sequence< OUString > aDisplayItems;
OSL_VERIFY( xControlModel->getPropertyValue( PROPERTY_STRINGITEMLIST ) >>= aDisplayItems );
Sequence< OUString > aValueItems;
OSL_VERIFY( xControlModel->getPropertyValue( PROPERTY_VALUE_SEQ ) >>= aValueItems );
OSL_ENSURE( aDisplayItems.getLength() == aValueItems.getLength(), "OFilterControl::initialize: inconsistent item lists!" );
for ( sal_Int32 i=0; i < ::std::min( aDisplayItems.getLength(), aValueItems.getLength() ); ++i )
m_aDisplayItemToValueItem[ aDisplayItems[i] ] = aValueItems[i];
}
break;
default:
m_bMultiLine = ::comphelper::hasProperty( PROPERTY_MULTILINE, xControlModel ) && ::comphelper::getBOOL( xControlModel->getPropertyValue( PROPERTY_MULTILINE ) );
m_nControlClass = FormComponentType::TEXTFIELD;
break;
}
}
// the connection meta data for the form which we're working for
Reference< XChild > xModel( xControlModel, UNO_QUERY );
Reference< XRowSet > xForm;
if ( xModel.is() )
xForm.set(xModel->getParent(), css::uno::UNO_QUERY);
m_xConnection = ::dbtools::getConnection( xForm );
OSL_ENSURE( m_xConnection.is(), "OFilterControl::initialize: unable to determine the form's connection!" );
#endif
}
OUString SAL_CALL OFilterControl::getImplementationName( )
{
return "com.sun.star.comp.forms.OFilterControl";
}
sal_Bool SAL_CALL OFilterControl::supportsService( const OUString& ServiceName )
{
return cppu::supportsService(this, ServiceName);
}
Sequence< OUString > SAL_CALL OFilterControl::getSupportedServiceNames( )
{
return { "com.sun.star.form.control.FilterControl",
"com.sun.star.awt.UnoControl" };
}
} // namespace frm
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
com_sun_star_comp_forms_OFilterControl_get_implementation(css::uno::XComponentContext* context,
css::uno::Sequence<css::uno::Any> const &)
{
return cppu::acquire(new frm::OFilterControl(context));
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|