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
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
|
/* -*- 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 "formoperations.hxx"
#include "frm_strings.hxx"
#include "frm_resource.hxx"
#include "frm_resource.hrc"
#include "frm_module.hxx"
#include <com/sun/star/ucb/AlreadyInitializedException.hpp>
#include <com/sun/star/util/XModifyBroadcaster.hpp>
#include <com/sun/star/form/runtime/FormFeature.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/DisposedException.hpp>
#include <com/sun/star/awt/XControl.hpp>
#include <com/sun/star/form/XGrid.hpp>
#include <com/sun/star/form/XBoundControl.hpp>
#include <com/sun/star/form/XBoundComponent.hpp>
#include <com/sun/star/sdbcx/XRowLocate.hpp>
#include <com/sun/star/form/XConfirmDeleteListener.hpp>
#include <com/sun/star/sdb/RowChangeEvent.hpp>
#include <com/sun/star/sdb/RowChangeAction.hpp>
#include <com/sun/star/sdb/OrderDialog.hpp>
#include <com/sun/star/sdb/FilterDialog.hpp>
#include <com/sun/star/sdbc/DataType.hpp>
#include <com/sun/star/form/XReset.hpp>
#include <com/sun/star/beans/XMultiPropertySet.hpp>
#include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
#include <com/sun/star/util/XRefreshable.hpp>
#include <connectivity/dbtools.hxx>
#include <connectivity/dbexception.hxx>
#include <vcl/svapp.hxx>
#include <vcl/stdtext.hxx>
#include <vcl/msgbox.hxx>
#include <vcl/waitobj.hxx>
#include <tools/diagnose_ex.h>
#include <comphelper/container.hxx>
#include <comphelper/property.hxx>
#include <comphelper/namedvaluecollection.hxx>
#include <comphelper/processfactory.hxx>
#include <cppuhelper/exc_hlp.hxx>
#include <osl/mutex.hxx>
#include <sal/macros.h>
//--------------------------------------------------------------------------
extern "C" void SAL_CALL createRegistryInfo_FormOperations()
{
static ::frm::OMultiInstanceAutoRegistration< ::frm::FormOperations > aAutoRegistration;
}
//........................................................................
namespace frm
{
//........................................................................
using ::dbtools::SQLExceptionInfo;
using ::com::sun::star::uno::Reference;
using ::com::sun::star::uno::XComponentContext;
using ::com::sun::star::uno::RuntimeException;
using ::com::sun::star::uno::Sequence;
using ::com::sun::star::uno::Exception;
using ::com::sun::star::uno::Any;
using ::com::sun::star::uno::XInterface;
using ::com::sun::star::sdbc::XRowSet;
using ::com::sun::star::sdbc::XResultSetUpdate;
using ::com::sun::star::form::runtime::XFormController;
using ::com::sun::star::form::runtime::XFormOperations;
using ::com::sun::star::form::runtime::XFeatureInvalidation;
using ::com::sun::star::form::runtime::FeatureState;
using ::com::sun::star::lang::IllegalArgumentException;
using ::com::sun::star::sdbc::SQLException;
using namespace ::com::sun::star::sdbc;
using ::com::sun::star::form::XForm;
using ::com::sun::star::ucb::AlreadyInitializedException;
using ::com::sun::star::util::XModifyBroadcaster;
using ::com::sun::star::uno::UNO_QUERY;
using ::com::sun::star::lang::EventObject;
using ::com::sun::star::beans::PropertyChangeEvent;
using ::com::sun::star::lang::XMultiServiceFactory;
using ::com::sun::star::lang::DisposedException;
using ::com::sun::star::beans::XPropertySet;
using ::com::sun::star::awt::XControl;
using ::com::sun::star::form::XGrid;
using ::com::sun::star::container::XIndexAccess;
using ::com::sun::star::uno::UNO_QUERY_THROW;
using ::com::sun::star::form::XBoundControl;
using ::com::sun::star::form::XBoundComponent;
using ::com::sun::star::sdbcx::XRowLocate;
using ::com::sun::star::form::XConfirmDeleteListener;
using ::com::sun::star::sdb::RowChangeEvent;
using namespace ::com::sun::star::sdb;
using ::com::sun::star::form::XReset;
using ::com::sun::star::beans::XMultiPropertySet;
using ::com::sun::star::uno::makeAny;
using ::com::sun::star::lang::WrappedTargetException;
using ::com::sun::star::beans::PropertyValue;
using ::com::sun::star::ui::dialogs::XExecutableDialog;
using ::com::sun::star::beans::NamedValue;
using ::com::sun::star::util::XRefreshable;
using ::com::sun::star::awt::XControlModel;
namespace FormFeature = ::com::sun::star::form::runtime::FormFeature;
namespace RowChangeAction = ::com::sun::star::sdb::RowChangeAction;
//====================================================================
//= FormOperations
//====================================================================
//--------------------------------------------------------------------
FormOperations::FormOperations( const Reference< XComponentContext >& _rxContext )
:FormOperations_Base( m_aMutex )
,m_xContext( _rxContext )
,m_bInitializedParser( false )
,m_bActiveControlModified( false )
,m_bConstructed( false )
#ifdef DBG_UTIL
,m_nMethodNestingLevel( false )
#endif
{
}
//--------------------------------------------------------------------
FormOperations::~FormOperations()
{
}
//--------------------------------------------------------------------
OUString FormOperations::getImplementationName_Static( ) throw(RuntimeException)
{
return OUString( "com.sun.star.comp.forms.FormOperations" );
}
//--------------------------------------------------------------------
Sequence< OUString > FormOperations::getSupportedServiceNames_Static( ) throw(RuntimeException)
{
Sequence< OUString > aNames(1);
aNames[0] = OUString( "com.sun.star.form.runtime.FormOperations" );
return aNames;
}
//--------------------------------------------------------------------
Reference< XInterface > SAL_CALL FormOperations::Create(const Reference< XMultiServiceFactory >& _rxFactory )
{
return *new FormOperations( comphelper::getComponentContext(_rxFactory) );
}
//--------------------------------------------------------------------
void SAL_CALL FormOperations::initialize( const Sequence< Any >& _arguments ) throw (Exception, RuntimeException)
{
if ( m_bConstructed )
throw AlreadyInitializedException();
if ( _arguments.getLength() == 1 )
{
Reference< XFormController > xController;
Reference< XForm > xForm;
if ( _arguments[0] >>= xController )
createWithFormController( xController );
else if ( _arguments[0] >>= xForm )
createWithForm( xForm );
else
throw IllegalArgumentException( OUString(), *this, 1 );
return;
}
throw IllegalArgumentException( OUString(), *this, 0 );
}
//--------------------------------------------------------------------
OUString SAL_CALL FormOperations::getImplementationName( ) throw (RuntimeException)
{
return getImplementationName_Static();
}
//--------------------------------------------------------------------
::sal_Bool SAL_CALL FormOperations::supportsService( const OUString& _ServiceName ) throw (RuntimeException)
{
Sequence< OUString > aSupportedServiceNames( getSupportedServiceNames() );
const OUString* pBegin = aSupportedServiceNames.getConstArray();
const OUString* pEnd = aSupportedServiceNames.getConstArray() + aSupportedServiceNames.getLength();
return ::std::find( pBegin, pEnd, _ServiceName ) != pEnd;
}
//--------------------------------------------------------------------
Sequence< OUString > SAL_CALL FormOperations::getSupportedServiceNames( ) throw (RuntimeException)
{
return getSupportedServiceNames_Static();
}
//--------------------------------------------------------------------
Reference< XRowSet > SAL_CALL FormOperations::getCursor() throw (RuntimeException)
{
MethodGuard aGuard( *this );
return m_xCursor;
}
//--------------------------------------------------------------------
Reference< XResultSetUpdate > SAL_CALL FormOperations::getUpdateCursor() throw (RuntimeException)
{
MethodGuard aGuard( *this );
return m_xUpdateCursor;
}
//--------------------------------------------------------------------
Reference< XFormController > SAL_CALL FormOperations::getController() throw (RuntimeException)
{
MethodGuard aGuard( *this );
return m_xController;
}
//--------------------------------------------------------------------
Reference< XFeatureInvalidation > SAL_CALL FormOperations::getFeatureInvalidation() throw (RuntimeException)
{
MethodGuard aGuard( *this );
return m_xFeatureInvalidation;
}
//--------------------------------------------------------------------
void SAL_CALL FormOperations::setFeatureInvalidation( const Reference< XFeatureInvalidation > & _rxFeatureInvalidation ) throw (RuntimeException)
{
MethodGuard aGuard( *this );
m_xFeatureInvalidation = _rxFeatureInvalidation;
}
//--------------------------------------------------------------------
FeatureState SAL_CALL FormOperations::getState( ::sal_Int16 _nFeature ) throw (RuntimeException)
{
MethodGuard aGuard( *this );
FeatureState aState;
aState.Enabled = sal_False;
try
{
// some checks for basic pre-requisites
if ( !m_xLoadableForm.is()
|| !m_xLoadableForm->isLoaded()
|| !m_xCursorProperties.is()
)
{
return aState;
}
switch ( _nFeature )
{
case FormFeature::MoveToFirst:
case FormFeature::MoveToPrevious:
aState.Enabled = impl_canMoveLeft_throw( );
break;
case FormFeature::MoveToNext:
aState.Enabled = impl_canMoveRight_throw();
break;
case FormFeature::MoveToLast:
aState.Enabled = impl_getRowCount_throw() && ( !m_xCursor->isLast() || impl_isInsertionRow_throw() );
break;
case FormFeature::DeleteRecord:
// already deleted ?
if ( m_xCursor->rowDeleted() )
aState.Enabled = sal_False;
else
{
// allowed to delete the row ?
aState.Enabled = !impl_isInsertionRow_throw() && ::dbtools::canDelete( m_xCursorProperties );
}
break;
case FormFeature::MoveToInsertRow:
// if we are inserting we can move to the next row if the current record or control is modified
aState.Enabled = impl_isInsertionRow_throw()
? impl_isModifiedRow_throw() || m_bActiveControlModified
: ::dbtools::canInsert( m_xCursorProperties );
break;
case FormFeature::ReloadForm:
{
// there must be an active connection
Reference< XRowSet > xCursorRowSet( m_xCursor, UNO_QUERY );
aState.Enabled = ::dbtools::getConnection( xCursorRowSet ).is();
// and an active command
OUString sActiveCommand;
m_xCursorProperties->getPropertyValue( PROPERTY_ACTIVECOMMAND ) >>= sActiveCommand;
aState.Enabled &= !sActiveCommand.isEmpty();
}
break;
case FormFeature::RefreshCurrentControl:
{
Reference< XRefreshable > xControlModelRefresh( impl_getCurrentControlModel_throw(), UNO_QUERY );
aState.Enabled = xControlModelRefresh.is();
}
break;
case FormFeature::SaveRecordChanges:
case FormFeature::UndoRecordChanges:
aState.Enabled = impl_isModifiedRow_throw() || m_bActiveControlModified;
break;
case FormFeature::RemoveFilterAndSort:
if ( impl_isParseable_throw() && impl_hasFilterOrOrder_throw() )
aState.Enabled = !impl_isInsertOnlyForm_throw();
break;
case FormFeature::SortAscending:
case FormFeature::SortDescending:
case FormFeature::AutoFilter:
if ( m_xController.is() && impl_isParseable_throw() )
{
sal_Bool bIsDeleted = m_xCursor->rowDeleted();
if ( !bIsDeleted && !impl_isInsertOnlyForm_throw() )
{
Reference< XPropertySet > xBoundField = impl_getCurrentBoundField_nothrow( );
if ( xBoundField.is() )
xBoundField->getPropertyValue( PROPERTY_SEARCHABLE ) >>= aState.Enabled;
}
}
break;
case FormFeature::InteractiveSort:
case FormFeature::InteractiveFilter:
if ( impl_isParseable_throw() )
aState.Enabled = !impl_isInsertOnlyForm_throw();
break;
case FormFeature::ToggleApplyFilter:
{
OUString sFilter;
m_xCursorProperties->getPropertyValue( PROPERTY_FILTER ) >>= sFilter;
if ( !sFilter.isEmpty() )
{
aState.State = m_xCursorProperties->getPropertyValue( PROPERTY_APPLYFILTER );
aState.Enabled = !impl_isInsertOnlyForm_throw();
}
else
aState.State <<= (sal_Bool)sal_False;
}
break;
case FormFeature::MoveAbsolute:
{
sal_Int32 nPosition = m_xCursor->getRow();
sal_Bool bIsNew = impl_isInsertionRow_throw();
sal_Int32 nCount = impl_getRowCount_throw();
sal_Bool bFinalCount = impl_isRowCountFinal_throw();
if ( ( nPosition >= 0 ) || bIsNew )
{
if ( bFinalCount )
{
// special case: there are no records at all, and we
// can't insert records -> disabled
if ( !nCount && !::dbtools::canInsert( m_xCursorProperties ) )
{
aState.Enabled = sal_False;
}
else
{
if ( bIsNew )
nPosition = ++nCount;
aState.State <<= (sal_Int32)nPosition;
aState.Enabled = sal_True;
}
}
else
{
aState.State <<= (sal_Int32)nPosition;
aState.Enabled = sal_True;
}
}
}
break;
case FormFeature::TotalRecords:
{
sal_Bool bIsNew = impl_isInsertionRow_throw();
sal_Int32 nCount = impl_getRowCount_throw();
sal_Bool bFinalCount = impl_isRowCountFinal_throw();
if ( bIsNew )
++nCount;
OUString sValue = OUString::valueOf( sal_Int32( nCount ) );
if ( !bFinalCount )
sValue += OUString(" *");
aState.State <<= sValue;
aState.Enabled = sal_True;
}
break;
default:
OSL_FAIL( "FormOperations::getState: unknown feature id!" );
break;
}
}
catch( const Exception& )
{
OSL_FAIL( "FormOperations::getState: caught an exception!" );
}
return aState;
}
//--------------------------------------------------------------------
::sal_Bool SAL_CALL FormOperations::isEnabled( ::sal_Int16 _nFeature ) throw (RuntimeException)
{
MethodGuard aGuard( *this );
FeatureState aState( getState( _nFeature ) );
return aState.Enabled;
}
//--------------------------------------------------------------------
namespace
{
static bool lcl_needConfirmCommit( sal_Int32 _nFeature )
{
return ( ( _nFeature == FormFeature::ReloadForm )
|| ( _nFeature == FormFeature::RemoveFilterAndSort )
|| ( _nFeature == FormFeature::ToggleApplyFilter )
|| ( _nFeature == FormFeature::SortAscending )
|| ( _nFeature == FormFeature::SortDescending )
|| ( _nFeature == FormFeature::AutoFilter )
|| ( _nFeature == FormFeature::InteractiveSort )
|| ( _nFeature == FormFeature::InteractiveFilter )
);
}
static bool lcl_requiresArguments( sal_Int32 _nFeature )
{
return ( _nFeature == FormFeature::MoveAbsolute );
}
static bool lcl_isExecutableFeature( sal_Int32 _nFeature )
{
return ( _nFeature != FormFeature::TotalRecords );
}
template < typename TYPE >
TYPE lcl_safeGetPropertyValue_throw( const Reference< XPropertySet >& _rxProperties, const OUString& _rPropertyName, TYPE _Default )
{
TYPE value( _Default );
OSL_PRECOND( _rxProperties.is(), "FormOperations::<foo>: no cursor (already disposed?)!" );
if ( _rxProperties.is() )
OSL_VERIFY( _rxProperties->getPropertyValue( _rPropertyName ) >>= value );
return value;
}
// returns false if parent should *abort* (user pressed cancel)
bool checkConfirmation(bool &needConfirmation, bool &shouldCommit)
{
if(needConfirmation)
{
// TODO: shouldn't this be done with an interaction handler?
QueryBox aQuery( NULL, WB_YES_NO_CANCEL | WB_DEF_YES, FRM_RES_STRING( RID_STR_QUERY_SAVE_MODIFIED_ROW ) );
switch ( aQuery.Execute() )
{
case RET_NO:
shouldCommit = false;
// no break on purpose: don't ask again!
case RET_YES:
needConfirmation = false;
return true;
case RET_CANCEL:
return false;
}
}
return true;
}
bool commit1Form(Reference< XFormController > xCntrl, bool &needConfirmation, bool &shouldCommit)
{
Reference< XFormOperations > xFrmOps(xCntrl->getFormOperations());
if (!xFrmOps->commitCurrentControl())
return false;
if(xFrmOps->isModifiedRow())
{
if(!checkConfirmation(needConfirmation, shouldCommit))
return false;
sal_Bool _;
if (shouldCommit && !xFrmOps->commitCurrentRecord(_))
return false;
}
return true;
}
bool commitFormAndSubforms(Reference< XFormController > xCntrl, bool needConfirmation)
{
bool shouldCommit(true);
assert(xCntrl.is());
Reference< XIndexAccess > xSubForms(xCntrl, UNO_QUERY);
assert(xSubForms.is());
if(xSubForms.is())
{
const sal_Int32 cnt = xSubForms->getCount();
for(int i=0; i < cnt; ++i)
{
Reference< XFormController > xSubForm(xSubForms->getByIndex(i), UNO_QUERY);
assert(xSubForm.is());
if (xSubForm.is())
{
if (!commit1Form(xSubForm, needConfirmation, shouldCommit))
return false;
}
}
}
if(!commit1Form(xCntrl, needConfirmation, shouldCommit))
return false;
return true;
}
bool commit1Form(Reference< XForm > xFrm, bool &needConfirmation, bool &shouldCommit)
{
Reference< XPropertySet > xProps(xFrm, UNO_QUERY_THROW);
// nothing to do if the record is not modified
if(!lcl_safeGetPropertyValue_throw( xProps, PROPERTY_ISMODIFIED, false ))
return true;
if(!checkConfirmation(needConfirmation, shouldCommit))
return false;
if(shouldCommit)
{
Reference< XResultSetUpdate > xUpd(xFrm, UNO_QUERY_THROW);
// insert respectively update the row
if ( lcl_safeGetPropertyValue_throw( xProps, PROPERTY_ISNEW, false ) )
xUpd->insertRow();
else
xUpd->updateRow();
}
return true;
}
bool commitFormAndSubforms(Reference< XForm > xFrm, bool needConfirmation)
{
// No control... do what we can with the models
bool shouldCommit(true);
Reference< XIndexAccess > xFormComps(xFrm, UNO_QUERY_THROW);
assert( xFormComps.is() );
const sal_Int32 cnt = xFormComps->getCount();
for(int i=0; i < cnt; ++i)
{
Reference< XForm > xSubForm(xFormComps->getByIndex(i), UNO_QUERY);
if(xSubForm.is())
{
if(!commit1Form(xSubForm, needConfirmation, shouldCommit))
return false;
}
}
if(!commit1Form(xFrm, needConfirmation, shouldCommit))
return false;
return true;
}
}
//--------------------------------------------------------------------
void SAL_CALL FormOperations::execute( ::sal_Int16 _nFeature ) throw (RuntimeException, IllegalArgumentException, SQLException, WrappedTargetException)
{
SolarMutexGuard aSolarGuard;
MethodGuard aGuard( *this );
if ( ( _nFeature != FormFeature::DeleteRecord ) && ( _nFeature != FormFeature::UndoRecordChanges ) )
{
if(m_xController.is())
{
if(!commitFormAndSubforms(m_xController, lcl_needConfirmCommit( _nFeature )))
return;
}
else if(m_xCursor.is())
{
Reference< XForm > xForm(m_xCursor, UNO_QUERY);
assert(xForm.is());
if(!commitFormAndSubforms(xForm, lcl_needConfirmCommit( _nFeature )))
return;
}
else
{
SAL_WARN( "forms.runtime", "No cursor, but trying to execute form operation " << _nFeature );
}
}
try
{
switch ( _nFeature )
{
case FormFeature::MoveToFirst:
m_xCursor->first();
break;
case FormFeature::MoveToNext:
impl_moveRight_throw( );
break;
case FormFeature::MoveToPrevious:
impl_moveLeft_throw( );
break;
case FormFeature::MoveToLast:
{
/*
// TODO: re-implement this .....
// run in an own thread if ...
// ... the data source is thread safe ...
sal_Bool bAllowOwnThread = sal_False;
if ( ::comphelper::hasProperty( PROPERTY_THREADSAFE, m_xCursorProperties ) )
m_xCursorProperties->getPropertyValue( PROPERTY_THREADSAFE ) >>= bAllowOwnThread;
// ... the record count is unknown
sal_Bool bNeedOwnThread sal_False;
if ( ::comphelper::hasProperty( PROPERTY_ROWCOUNTFINAL, m_xCursorProperties ) )
m_xCursorProperties->getPropertyValue( PROPERTY_ROWCOUNTFINAL ) >>= bNeedOwnThread;
if ( bNeedOwnThread && bAllowOwnThread )
;
else
*/
m_xCursor->last();
}
break;
case FormFeature::ReloadForm:
if ( m_xLoadableForm.is() )
{
WaitObject aWO( NULL );
m_xLoadableForm->reload();
// refresh all controls in the form (and sub forms) which can be refreshed
// #i90914#
::comphelper::IndexAccessIterator aIter( m_xLoadableForm );
Reference< XInterface > xElement( aIter.Next() );
while ( xElement.is() )
{
Reference< XRefreshable > xRefresh( xElement, UNO_QUERY );
if ( xRefresh.is() )
xRefresh->refresh();
xElement = aIter.Next();
}
}
break;
case FormFeature::RefreshCurrentControl:
{
Reference< XRefreshable > xControlModelRefresh( impl_getCurrentControlModel_throw(), UNO_QUERY );
OSL_ENSURE( xControlModelRefresh.is(), "FormOperations::execute: how did you reach this?" );
if ( xControlModelRefresh.is() )
xControlModelRefresh->refresh();
}
break;
case FormFeature::DeleteRecord:
{
sal_uInt32 nCount = impl_getRowCount_throw();
// next position
sal_Bool bLeft = m_xCursor->isLast() && ( nCount > 1 );
sal_Bool bRight= !m_xCursor->isLast();
sal_Bool bSuccess = sal_False;
try
{
// ask for confirmation
Reference< XConfirmDeleteListener > xConfirmDelete( m_xController, UNO_QUERY );
if ( xConfirmDelete.is() )
{
RowChangeEvent aEvent;
aEvent.Source = Reference< XInterface >( m_xCursor, UNO_QUERY );
aEvent.Action = RowChangeAction::DELETE;
aEvent.Rows = 1;
bSuccess = xConfirmDelete->confirmDelete( aEvent );
}
// delete it
if ( bSuccess )
m_xUpdateCursor->deleteRow();
}
catch( const Exception& )
{
bSuccess = sal_False;
}
if ( bSuccess )
{
if ( bLeft || bRight )
m_xCursor->relative( bRight ? 1 : -1 );
else
{
sal_Bool bCanInsert = ::dbtools::canInsert( m_xCursorProperties );
// is it possible to insert another record?
if ( bCanInsert )
m_xUpdateCursor->moveToInsertRow();
else
// move record to update stati
m_xCursor->first();
}
}
}
break;
case FormFeature::SaveRecordChanges:
case FormFeature::UndoRecordChanges:
{
sal_Bool bInserting = impl_isInsertionRow_throw();
if ( FormFeature::UndoRecordChanges == _nFeature )
{
if ( !bInserting )
m_xUpdateCursor->cancelRowUpdates();
// reset all controls for this form
impl_resetAllControls_nothrow( );
if ( bInserting ) // back to insertion mode for this form
m_xUpdateCursor->moveToInsertRow();
}
else
{
if ( bInserting )
{
m_xUpdateCursor->insertRow();
m_xCursor->last();
}
else
m_xUpdateCursor->updateRow();
}
}
break;
case FormFeature::MoveToInsertRow:
// move to the last row before moving to the insert row
m_xCursor->last();
m_xUpdateCursor->moveToInsertRow();
break;
case FormFeature::RemoveFilterAndSort:
{
// simultaneously reset Filter and Order property
Reference< XMultiPropertySet > xProperties( m_xCursorProperties, UNO_QUERY );
OSL_ENSURE( xProperties.is(), "FormOperations::execute: no multi property access!" );
if ( xProperties.is() )
{
Sequence< OUString > aNames( 2 );
aNames[0] = PROPERTY_FILTER;
aNames[1] = PROPERTY_SORT;
Sequence< Any> aValues( 2 );
aValues[0] <<= OUString();
aValues[1] <<= OUString();
WaitObject aWO( NULL );
xProperties->setPropertyValues( aNames, aValues );
if ( m_xLoadableForm.is() )
m_xLoadableForm->reload();
}
}
break;
case FormFeature::ToggleApplyFilter:
if ( impl_commitCurrentControl_throw() && impl_commitCurrentRecord_throw() )
{
// simply toggle the value
sal_Bool bApplied = sal_False;
m_xCursorProperties->getPropertyValue( PROPERTY_APPLYFILTER ) >>= bApplied;
m_xCursorProperties->setPropertyValue( PROPERTY_APPLYFILTER, makeAny( (sal_Bool)!bApplied ) );
// and reload
WaitObject aWO( NULL );
m_xLoadableForm->reload();
}
break;
case FormFeature::SortAscending:
impl_executeAutoSort_throw( true );
break;
case FormFeature::SortDescending:
impl_executeAutoSort_throw( false );
break;
case FormFeature::AutoFilter:
impl_executeAutoFilter_throw();
break;
case FormFeature::InteractiveSort:
impl_executeFilterOrSort_throw( false );
break;
case FormFeature::InteractiveFilter:
impl_executeFilterOrSort_throw( true );
break;
default:
{
sal_uInt16 nErrorResourceId = RID_STR_FEATURE_UNKNOWN;
if ( lcl_requiresArguments( _nFeature ) )
nErrorResourceId = RID_STR_FEATURE_REQUIRES_PARAMETERS;
else if ( !lcl_isExecutableFeature( _nFeature ) )
nErrorResourceId = RID_STR_FEATURE_NOT_EXECUTABLE;
throw IllegalArgumentException( FRM_RES_STRING( nErrorResourceId ), *this, 1 );
}
} // switch
}
catch( const RuntimeException& ) { throw; }
catch( const SQLException& ) { throw; }
catch( const Exception& )
{
throw WrappedTargetException( OUString(), *const_cast< FormOperations* >( this ), ::cppu::getCaughtException() );
}
impl_invalidateAllSupportedFeatures_nothrow( aGuard );
}
//--------------------------------------------------------------------
void SAL_CALL FormOperations::executeWithArguments( ::sal_Int16 _nFeature, const Sequence< NamedValue >& _rArguments ) throw (RuntimeException, IllegalArgumentException, SQLException, WrappedTargetException)
{
if ( !lcl_requiresArguments( _nFeature ) )
{
execute( _nFeature );
return;
}
SolarMutexGuard aSolarGuard;
MethodGuard aGuard( *this );
// at the moment we have only one feature which supports execution parameters
if ( !lcl_isExecutableFeature( _nFeature ) )
throw IllegalArgumentException( FRM_RES_STRING( RID_STR_FEATURE_NOT_EXECUTABLE ), *this, 1 );
switch ( _nFeature )
{
case FormFeature::MoveAbsolute:
{
sal_Int32 nPosition = -1;
::comphelper::NamedValueCollection aArguments( _rArguments );
aArguments.get_ensureType( "Position", nPosition );
if ( nPosition < 1 )
nPosition = 1;
try
{
// commit before doing anything else
if ( m_xController.is() && !impl_commitCurrentControl_throw() )
return;
if ( !impl_commitCurrentRecord_throw() )
return;
sal_Int32 nCount = impl_getRowCount_throw();
sal_Bool bFinalCount = impl_isRowCountFinal_throw();
if ( bFinalCount && ( (sal_Int32)nPosition > nCount ) )
nPosition = nCount;
m_xCursor->absolute( nPosition );
}
catch( const RuntimeException& ) { throw; }
catch( const SQLException& ) { throw; }
catch( const Exception& )
{
throw WrappedTargetException( OUString(), *this, ::cppu::getCaughtException() );
}
}
break;
default:
throw IllegalArgumentException( FRM_RES_STRING( RID_STR_FEATURE_UNKNOWN ), *this, 1 );
} // switch
}
//--------------------------------------------------------------------
::sal_Bool SAL_CALL FormOperations::commitCurrentRecord( ::sal_Bool& _out_rRecordInserted ) throw (RuntimeException, SQLException)
{
MethodGuard aGuard( *this );
_out_rRecordInserted = sal_False;
return impl_commitCurrentRecord_throw( &_out_rRecordInserted );
}
//--------------------------------------------------------------------
bool FormOperations::impl_commitCurrentRecord_throw( ::sal_Bool* _pRecordInserted ) const
{
DBG_ASSERT( m_nMethodNestingLevel, "FormOperations::impl_commitCurrentRecord_throw: to be called within a MethodGuard'ed section only!" );
if ( !impl_hasCursor_nothrow() )
return false;
// nothing to do if the record is not modified
sal_Bool bResult = !impl_isModifiedRow_throw();
if ( !bResult )
{
// insert respectively update the row
if ( impl_isInsertionRow_throw() )
{
m_xUpdateCursor->insertRow();
if ( _pRecordInserted )
*_pRecordInserted = sal_True;
}
else
m_xUpdateCursor->updateRow();
bResult = true;
}
return bResult;
}
//--------------------------------------------------------------------
::sal_Bool SAL_CALL FormOperations::commitCurrentControl() throw (RuntimeException, SQLException)
{
MethodGuard aGuard( *this );
return impl_commitCurrentControl_throw();
}
//--------------------------------------------------------------------
bool FormOperations::impl_commitCurrentControl_throw() const
{
DBG_ASSERT( m_nMethodNestingLevel, "FormOperations::impl_commitCurrentControl_throw: to be called within a MethodGuard'ed section only!" );
OSL_PRECOND( m_xController.is(), "FormOperations::commitCurrentControl: no controller!" );
if ( !m_xController.is() )
return false;
bool bSuccess = false;
try
{
Reference< XControl > xCurrentControl( m_xController->getCurrentControl() );
// check whether the control is locked
Reference< XBoundControl > xCheckLock( xCurrentControl, UNO_QUERY );
sal_Bool bControlIsLocked = xCheckLock.is() && xCheckLock->getLock();
// commit if necessary
bSuccess = true;
if ( xCurrentControl.is() && !bControlIsLocked )
{
// both the control and it's model can be committable, so try both
Reference< XBoundComponent > xBound( xCurrentControl, UNO_QUERY );
if ( !xBound.is() )
xBound = xBound.query( xCurrentControl->getModel() );
// and now really commit
if ( xBound.is() )
bSuccess = xBound->commit();
}
}
catch( const RuntimeException& ) { throw; }
catch( const SQLException& ) { throw; }
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION();
bSuccess = false;
}
return bSuccess;
}
//--------------------------------------------------------------------
::sal_Bool SAL_CALL FormOperations::isInsertionRow() throw (RuntimeException, WrappedTargetException)
{
sal_Bool bIs = sal_False;
try
{
bIs = impl_isInsertionRow_throw();
}
catch( const RuntimeException& ) { throw; }
catch( const Exception& )
{
throw WrappedTargetException( OUString(), *this, ::cppu::getCaughtException() );
}
return bIs;
}
//--------------------------------------------------------------------
::sal_Bool SAL_CALL FormOperations::isModifiedRow() throw (RuntimeException, WrappedTargetException)
{
sal_Bool bIs = sal_False;
try
{
bIs = impl_isModifiedRow_throw();
}
catch( const RuntimeException& ) { throw; }
catch( const Exception& )
{
throw WrappedTargetException( OUString(), *this, ::cppu::getCaughtException() );
}
return bIs;
}
//--------------------------------------------------------------------
void SAL_CALL FormOperations::cursorMoved( const EventObject& /*_Event*/ ) throw (RuntimeException)
{
MethodGuard aGuard( *this );
m_bActiveControlModified = sal_False;
impl_invalidateAllSupportedFeatures_nothrow( aGuard );
}
//--------------------------------------------------------------------
void SAL_CALL FormOperations::rowChanged( const EventObject& /*_Event*/ ) throw (RuntimeException)
{
// not interested in
}
//--------------------------------------------------------------------
void SAL_CALL FormOperations::rowSetChanged( const EventObject& /*_Event*/ ) throw (RuntimeException)
{
// not interested in
}
//--------------------------------------------------------------------
void SAL_CALL FormOperations::modified( const EventObject& /*_Source*/ ) throw( RuntimeException )
{
MethodGuard aGuard( *this );
OSL_ENSURE( m_xCursor.is(), "FormOperations::modified: already disposed!" );
if ( !m_bActiveControlModified )
{
m_bActiveControlModified = sal_True;
impl_invalidateModifyDependentFeatures_nothrow( aGuard );
}
}
//--------------------------------------------------------------------
void SAL_CALL FormOperations::propertyChange( const PropertyChangeEvent& _rEvent ) throw (RuntimeException)
{
MethodGuard aGuard( *this );
if ( m_xCursor.is() && ( m_xCursor == _rEvent.Source ) )
{
sal_Bool bIs = sal_False;
if ( ( _rEvent.PropertyName == PROPERTY_ISMODIFIED )
|| ( _rEvent.PropertyName == PROPERTY_ISNEW )
)
{
if ( ( _rEvent.NewValue >>= bIs ) && !bIs )
m_bActiveControlModified = sal_False;
}
impl_invalidateAllSupportedFeatures_nothrow( aGuard );
}
if ( m_xParser.is() && ( m_xCursor == _rEvent.Source ) )
{
try
{
OUString sNewValue;
_rEvent.NewValue >>= sNewValue;
if ( _rEvent.PropertyName == PROPERTY_ACTIVECOMMAND )
{
m_xParser->setElementaryQuery( sNewValue );
}
else if ( _rEvent.PropertyName == PROPERTY_FILTER )
{
if ( m_xParser->getFilter() != sNewValue )
m_xParser->setFilter( sNewValue );
}
else if ( _rEvent.PropertyName == PROPERTY_SORT )
{
_rEvent.NewValue >>= sNewValue;
if ( m_xParser->getOrder() != sNewValue )
m_xParser->setOrder( sNewValue );
}
}
catch( const Exception& )
{
OSL_FAIL( "FormOperations::propertyChange: caught an exception while updating the parser!" );
}
impl_invalidateAllSupportedFeatures_nothrow( aGuard );
}
}
//--------------------------------------------------------------------
void SAL_CALL FormOperations::disposing( const EventObject& /*_Source*/ ) throw (RuntimeException)
{
// TODO: should we react on this? Or is this the responsibility of our owner to dispose us?
}
//--------------------------------------------------------------------
void SAL_CALL FormOperations::disposing()
{
::osl::MutexGuard aGuard( m_aMutex );
impl_disposeParser_nothrow();
try
{
// revoke various listeners
if ( m_xCursor.is() )
m_xCursor->removeRowSetListener( this );
if ( m_xCursorProperties.is() )
{
m_xCursorProperties->removePropertyChangeListener( PROPERTY_ISMODIFIED,this );
m_xCursorProperties->removePropertyChangeListener( PROPERTY_ISNEW, this );
}
Reference< XModifyBroadcaster > xBroadcaster( m_xController, UNO_QUERY );
if ( xBroadcaster.is() )
xBroadcaster->removeModifyListener( this );
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION();
}
m_xController.clear();
m_xCursor.clear();
m_xUpdateCursor.clear();
m_xCursorProperties.clear();
m_xLoadableForm.clear();
m_xFeatureInvalidation.clear();
m_bActiveControlModified = true;
}
//--------------------------------------------------------------------
void FormOperations::impl_checkDisposed_throw() const
{
if ( impl_isDisposed_nothrow() )
throw DisposedException( OUString(), *const_cast< FormOperations* >( this ) );
}
//--------------------------------------------------------------------
void FormOperations::impl_initFromController_throw()
{
OSL_PRECOND( m_xController.is(), "FormOperations::impl_initFromController_throw: invalid controller!" );
m_xCursor = m_xCursor.query( m_xController->getModel() );
if ( !m_xCursor.is() )
throw IllegalArgumentException( OUString(), *this, 0 );
impl_initFromForm_throw();
Reference< XModifyBroadcaster > xBroadcaster( m_xController, UNO_QUERY );
if ( xBroadcaster.is() )
xBroadcaster->addModifyListener( this );
}
//--------------------------------------------------------------------
void FormOperations::impl_initFromForm_throw()
{
OSL_PRECOND( m_xCursor.is(), "FormOperations::impl_initFromForm_throw: invalid form!" );
m_xCursorProperties = m_xCursorProperties.query ( m_xCursor );
m_xUpdateCursor = m_xUpdateCursor.query ( m_xCursor );
m_xLoadableForm = m_xLoadableForm.query ( m_xCursor );
if ( !m_xCursor.is() || !m_xCursorProperties.is() || !m_xLoadableForm.is() )
throw IllegalArgumentException( OUString(), *this, 0 );
m_xCursor->addRowSetListener( this );
m_xCursorProperties->addPropertyChangeListener( PROPERTY_ISMODIFIED,this );
m_xCursorProperties->addPropertyChangeListener( PROPERTY_ISNEW, this );
}
//--------------------------------------------------------------------
void FormOperations::createWithFormController( const Reference< XFormController >& _rxController )
{
m_xController = _rxController;
if ( !m_xController.is() )
throw IllegalArgumentException( OUString(), *this, 0 );
impl_initFromController_throw();
m_bConstructed = true;
}
//--------------------------------------------------------------------
void FormOperations::createWithForm( const Reference< XForm >& _rxForm )
{
m_xCursor = m_xCursor.query( _rxForm );
if ( !m_xCursor.is() )
throw IllegalArgumentException( OUString(), *this, 0 );
impl_initFromForm_throw();
m_bConstructed = true;
}
//------------------------------------------------------------------------------
void FormOperations::impl_invalidateAllSupportedFeatures_nothrow( MethodGuard& _rClearForCallback ) const
{
if ( !m_xFeatureInvalidation.is() )
// nobody's interested in ...
return;
Reference< XFeatureInvalidation > xInvalidation = m_xFeatureInvalidation;
_rClearForCallback.clear();
xInvalidation->invalidateAllFeatures();
}
//------------------------------------------------------------------------------
void FormOperations::impl_invalidateModifyDependentFeatures_nothrow( MethodGuard& _rClearForCallback ) const
{
if ( !m_xFeatureInvalidation.is() )
// nobody's interested in ...
return;
static Sequence< sal_Int16 > s_aModifyDependentFeatures;
if ( s_aModifyDependentFeatures.getLength() == 0 )
{
sal_Int16 pModifyDependentFeatures[] =
{
FormFeature::MoveToNext,
FormFeature::MoveToInsertRow,
FormFeature::SaveRecordChanges,
FormFeature::UndoRecordChanges
};
size_t nFeatureCount = SAL_N_ELEMENTS( pModifyDependentFeatures );
s_aModifyDependentFeatures = Sequence< sal_Int16 >( pModifyDependentFeatures, nFeatureCount );
}
Reference< XFeatureInvalidation > xInvalidation = m_xFeatureInvalidation;
_rClearForCallback.clear();
xInvalidation->invalidateFeatures( s_aModifyDependentFeatures );
}
//--------------------------------------------------------------------
void FormOperations::impl_ensureInitializedParser_nothrow()
{
OSL_PRECOND( m_xCursorProperties.is(), "FormOperations::impl_ensureInitializedParser_nothrow: we're disposed!" );
if ( m_bInitializedParser )
return;
try
{
sal_Bool bUseEscapeProcessing = sal_False;
m_xCursorProperties->getPropertyValue( PROPERTY_ESCAPE_PROCESSING ) >>= bUseEscapeProcessing;
if ( bUseEscapeProcessing )
{
Reference< XMultiServiceFactory > xFactory( ::dbtools::getConnection( m_xCursor ), UNO_QUERY );
if ( xFactory.is() )
{
m_xParser.set( xFactory->createInstance( OUString( "com.sun.star.sdb.SingleSelectQueryComposer" ) ), UNO_QUERY );
OSL_ENSURE( m_xParser.is(), "FormOperations::impl_ensureInitializedParser_nothrow: factory did not create a parser for us!" );
}
}
if ( m_xParser.is() )
{
if ( m_xLoadableForm.is() && m_xLoadableForm->isLoaded() )
{
OUString sStatement;
OUString sFilter;
OUString sSort;
m_xCursorProperties->getPropertyValue( PROPERTY_ACTIVECOMMAND ) >>= sStatement;
m_xCursorProperties->getPropertyValue( PROPERTY_FILTER ) >>= sFilter;
m_xCursorProperties->getPropertyValue( PROPERTY_SORT ) >>= sSort;
m_xParser->setElementaryQuery( sStatement );
m_xParser->setFilter ( sFilter );
m_xParser->setOrder ( sSort );
}
// start listening at the order/sort properties at the form, so
// we can keep our parser in sync
m_xCursorProperties->addPropertyChangeListener( PROPERTY_ACTIVECOMMAND, this );
m_xCursorProperties->addPropertyChangeListener( PROPERTY_FILTER, this );
m_xCursorProperties->addPropertyChangeListener( PROPERTY_SORT, this );
}
}
catch( const Exception& )
{
OSL_FAIL( "FormOperations::impl_ensureInitializedParser_nothrow: caught an exception!" );
}
m_bInitializedParser = true;
}
//--------------------------------------------------------------------
void FormOperations::impl_disposeParser_nothrow()
{
try
{
// if we have a parser (and a cursor), then we're listening at the cursor's
// properties to keep the parser in sync with the cursor
if ( m_xParser.is() && m_xCursorProperties.is() )
{
m_xCursorProperties->removePropertyChangeListener( PROPERTY_FILTER, this );
m_xCursorProperties->removePropertyChangeListener( PROPERTY_ACTIVECOMMAND, this );
m_xCursorProperties->removePropertyChangeListener( PROPERTY_SORT, this );
}
Reference< XComponent > xParserComp( m_xParser, UNO_QUERY );
if ( xParserComp.is() )
xParserComp->dispose();
m_xParser.clear();
m_bInitializedParser = false;
}
catch( const Exception& )
{
OSL_FAIL( "FormOperations::impl_disposeParser_nothrow: caught an exception!" );
}
}
//--------------------------------------------------------------------
bool FormOperations::impl_canMoveLeft_throw( ) const
{
if ( !impl_hasCursor_nothrow() )
return false;
return impl_getRowCount_throw() && ( !m_xCursor->isFirst() || impl_isInsertionRow_throw() );
}
//--------------------------------------------------------------------
bool FormOperations::impl_canMoveRight_throw( ) const
{
if ( !impl_hasCursor_nothrow() )
return false;
bool bIsNew = impl_isInsertionRow_throw();
if ( impl_getRowCount_throw() && !m_xCursor->isLast() && !bIsNew )
return true;
if ( ::dbtools::canInsert( m_xCursorProperties ) )
if ( !bIsNew || impl_isModifiedRow_throw() )
return true;
if ( bIsNew && m_bActiveControlModified )
return true;
return false;
}
//--------------------------------------------------------------------
bool FormOperations::impl_isInsertionRow_throw() const
{
return lcl_safeGetPropertyValue_throw( m_xCursorProperties, PROPERTY_ISNEW, false );
}
//--------------------------------------------------------------------
sal_Int32 FormOperations::impl_getRowCount_throw() const
{
return lcl_safeGetPropertyValue_throw( m_xCursorProperties, PROPERTY_ROWCOUNT, (sal_Int32)0 );
}
//--------------------------------------------------------------------
bool FormOperations::impl_isRowCountFinal_throw() const
{
return lcl_safeGetPropertyValue_throw( m_xCursorProperties, PROPERTY_ROWCOUNTFINAL, false );
}
//--------------------------------------------------------------------
bool FormOperations::impl_isModifiedRow_throw() const
{
return lcl_safeGetPropertyValue_throw( m_xCursorProperties, PROPERTY_ISMODIFIED, false );
}
//--------------------------------------------------------------------
bool FormOperations::impl_isParseable_throw() const
{
const_cast< FormOperations* >( this )->impl_ensureInitializedParser_nothrow();
return m_xParser.is() && !m_xParser->getQuery().isEmpty();
}
//--------------------------------------------------------------------
bool FormOperations::impl_hasFilterOrOrder_throw() const
{
return impl_isParseable_throw() && ( !m_xParser->getFilter().isEmpty() || !m_xParser->getOrder().isEmpty() );
}
//--------------------------------------------------------------------
bool FormOperations::impl_isInsertOnlyForm_throw() const
{
return lcl_safeGetPropertyValue_throw( m_xCursorProperties, PROPERTY_INSERTONLY, true );
}
//------------------------------------------------------------------------------
Reference< XControlModel > FormOperations::impl_getCurrentControlModel_throw() const
{
Reference< XControl > xControl( m_xController->getCurrentControl() );
// special handling for grid controls
Reference< XGrid > xGrid( xControl, UNO_QUERY );
Reference< XControlModel > xControlModel;
if ( xGrid.is() )
{
Reference< XIndexAccess > xColumns( xControl->getModel(), UNO_QUERY_THROW );
sal_Int16 nCurrentPos = xGrid->getCurrentColumnPosition();
nCurrentPos = impl_gridView2ModelPos_nothrow( xColumns, nCurrentPos );
if ( nCurrentPos != (sal_Int16)-1 )
xColumns->getByIndex( nCurrentPos ) >>= xControlModel;
}
else if ( xControl.is() )
{
xControlModel = xControl->getModel();
}
return xControlModel;
}
//------------------------------------------------------------------------------
Reference< XPropertySet > FormOperations::impl_getCurrentBoundField_nothrow( ) const
{
OSL_PRECOND( m_xController.is(), "FormOperations::impl_getCurrentBoundField_nothrow: no controller -> no control!" );
if ( !m_xController.is() )
return NULL;
Reference< XPropertySet > xField;
try
{
Reference< XPropertySet > xControlModel( impl_getCurrentControlModel_throw(), UNO_QUERY );
if ( xControlModel.is() && ::comphelper::hasProperty( PROPERTY_BOUNDFIELD, xControlModel ) )
xControlModel->getPropertyValue( PROPERTY_BOUNDFIELD ) >>= xField;
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION();
}
return xField;
}
//------------------------------------------------------------------------------
sal_Int16 FormOperations::impl_gridView2ModelPos_nothrow( const Reference< XIndexAccess >& _rxColumns, sal_Int16 _nViewPos ) const
{
OSL_PRECOND( _rxColumns.is(), "FormOperations::impl_gridView2ModelPos_nothrow: invalid columns container!" );
try
{
// loop through all columns
sal_Int16 col = 0;
Reference< XPropertySet > xCol;
bool bHidden( false );
for ( col = 0; col < _rxColumns->getCount(); ++col )
{
_rxColumns->getByIndex( col ) >>= xCol;
OSL_VERIFY( xCol->getPropertyValue( PROPERTY_HIDDEN ) >>= bHidden );
if ( bHidden )
continue;
// for every visible col : if nViewPos is greater zero, decrement it, else we
// have found the model position
if ( !_nViewPos )
break;
else
--_nViewPos;
}
if ( col < _rxColumns->getCount() )
return col;
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION();
}
return (sal_Int16)-1;
}
//------------------------------------------------------------------------------
bool FormOperations::impl_moveLeft_throw( ) const
{
OSL_PRECOND( impl_hasCursor_nothrow(), "FormOperations::impl_moveLeft_throw: no cursor!" );
if ( !impl_hasCursor_nothrow() )
return false;
sal_Bool bRecordInserted = sal_False;
sal_Bool bSuccess = impl_commitCurrentRecord_throw( &bRecordInserted );
if ( !bSuccess )
return false;
if ( bRecordInserted )
{
// retrieve the bookmark of the new record and move to the record preceding this bookmark
Reference< XRowLocate > xLocate( m_xCursor, UNO_QUERY );
OSL_ENSURE( xLocate.is(), "FormOperations::impl_moveLeft_throw: no XRowLocate!" );
if ( xLocate.is() )
xLocate->moveRelativeToBookmark( xLocate->getBookmark(), -1 );
}
else
{
if ( impl_isInsertionRow_throw() )
{
// we assume that the inserted record is now the last record in the
// result set
m_xCursor->last();
}
else
m_xCursor->previous();
}
return true;
}
//--------------------------------------------------------------------
bool FormOperations::impl_moveRight_throw( ) const
{
OSL_PRECOND( impl_hasCursor_nothrow(), "FormOperations::impl_moveRight_throw: no cursor!" );
if ( !impl_hasCursor_nothrow() )
return false;
sal_Bool bRecordInserted = sal_False;
sal_Bool bSuccess = impl_commitCurrentRecord_throw( &bRecordInserted );
if ( !bSuccess )
return false;
if ( bRecordInserted )
{
// go to insert row
m_xUpdateCursor->moveToInsertRow();
}
else
{
if ( m_xCursor->isLast() )
m_xUpdateCursor->moveToInsertRow();
else
m_xCursor->next();
}
return true;
}
//--------------------------------------------------------------------
void FormOperations::impl_resetAllControls_nothrow() const
{
Reference< XIndexAccess > xContainer( m_xCursor, UNO_QUERY );
if ( !xContainer.is() )
return;
try
{
Reference< XReset > xReset;
sal_Int32 nCount( xContainer->getCount() );
for ( sal_Int32 i = 0; i < nCount; ++i )
{
if ( xContainer->getByIndex( i ) >>= xReset )
{
// no resets on sub forms
Reference< XForm > xAsForm( xReset, UNO_QUERY );
if ( !xAsForm.is() )
xReset->reset();
}
}
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION();
}
}
//------------------------------------------------------------------------------
void FormOperations::impl_executeAutoSort_throw( bool _bUp ) const
{
OSL_PRECOND( m_xController.is(), "FormOperations::impl_executeAutoSort_throw: need a controller for this!" );
OSL_PRECOND( impl_hasCursor_nothrow(), "FormOperations::impl_executeAutoSort_throw: need a cursor for this!" );
OSL_PRECOND( impl_isParseable_throw(), "FormOperations::impl_executeAutoSort_throw: need a parseable statement for this!" );
if ( !m_xController.is() || !impl_hasCursor_nothrow() || !impl_isParseable_throw() )
return;
try
{
Reference< XControl > xControl = m_xController->getCurrentControl();
if ( !xControl.is() || !impl_commitCurrentControl_throw() || !impl_commitCurrentRecord_throw() )
return;
Reference< XPropertySet > xBoundField( impl_getCurrentBoundField_nothrow() );
if ( !xBoundField.is() )
return;
OUString sOriginalSort;
m_xCursorProperties->getPropertyValue( PROPERTY_SORT ) >>= sOriginalSort;
// automatic sort by field is expected to always resets the previous sort order
m_xParser->setOrder( OUString() );
impl_appendOrderByColumn_throw aAction(this, xBoundField, _bUp);
impl_doActionInSQLContext_throw(aAction, RID_STR_COULD_NOT_SET_ORDER );
WaitObject aWO( NULL );
try
{
m_xCursorProperties->setPropertyValue( PROPERTY_SORT, makeAny( m_xParser->getOrder() ) );
m_xLoadableForm->reload();
}
catch( const Exception& )
{
OSL_FAIL( "FormOperations::impl_executeAutoSort_throw: caught an exception while setting the parser properties!" );
}
if ( !m_xLoadableForm->isLoaded() )
{ // something went wrong -> restore the original state
try
{
m_xParser->setOrder( sOriginalSort );
m_xCursorProperties->setPropertyValue( PROPERTY_SORT, makeAny( m_xParser->getOrder() ) );
m_xLoadableForm->reload();
}
catch( const Exception& )
{
OSL_FAIL( "FormOperations::impl_executeAutoSort_throw: could not reset the form to it's original state!" );
}
}
}
catch( const RuntimeException& ) { throw; }
catch( const SQLException& ) { throw; }
catch( const Exception& )
{
throw WrappedTargetException( OUString(), *const_cast< FormOperations* >( this ), ::cppu::getCaughtException() );
}
}
//------------------------------------------------------------------------------
void FormOperations::impl_executeAutoFilter_throw( ) const
{
OSL_PRECOND( m_xController.is(), "FormOperations::impl_executeAutoFilter_throw: need a controller for this!" );
OSL_PRECOND( impl_hasCursor_nothrow(), "FormOperations::impl_executeAutoFilter_throw: need a cursor for this!" );
OSL_PRECOND( impl_isParseable_throw(), "FormOperations::impl_executeAutoFilter_throw: need a parseable statement for this!" );
if ( !m_xController.is() || !impl_hasCursor_nothrow() || !impl_isParseable_throw() )
return;
try
{
Reference< XControl > xControl = m_xController->getCurrentControl();
if ( !xControl.is() || !impl_commitCurrentControl_throw() || !impl_commitCurrentRecord_throw() )
return;
Reference< XPropertySet > xBoundField( impl_getCurrentBoundField_nothrow() );
if ( !xBoundField.is() )
return;
OUString sOriginalFilter;
m_xCursorProperties->getPropertyValue( PROPERTY_FILTER ) >>= sOriginalFilter;
sal_Bool bApplied = sal_True;
m_xCursorProperties->getPropertyValue( PROPERTY_APPLYFILTER ) >>= bApplied;
// if we have a filter, but it's not applied, then we have to overwrite it, else append one
if ( !bApplied )
m_xParser->setFilter( OUString() );
impl_appendFilterByColumn_throw aAction(this, xBoundField);
impl_doActionInSQLContext_throw( aAction, RID_STR_COULD_NOT_SET_FILTER );
WaitObject aWO( NULL );
try
{
m_xCursorProperties->setPropertyValue( PROPERTY_FILTER, makeAny( m_xParser->getFilter() ) );
m_xCursorProperties->setPropertyValue( PROPERTY_APPLYFILTER, makeAny( (sal_Bool)sal_True ) );
m_xLoadableForm->reload();
}
catch( const Exception& )
{
OSL_FAIL( "FormOperations::impl_executeAutoFilter_throw: caught an exception while setting the parser properties!" );
}
if ( !m_xLoadableForm->isLoaded() )
{ // something went wrong -> restore the original state
try
{
m_xParser->setOrder( sOriginalFilter );
m_xCursorProperties->setPropertyValue( PROPERTY_APPLYFILTER, makeAny( (sal_Bool)bApplied ) );
m_xCursorProperties->setPropertyValue( PROPERTY_FILTER, makeAny( m_xParser->getFilter() ) );
m_xLoadableForm->reload();
}
catch( const Exception& )
{
OSL_FAIL( "FormOperations::impl_executeAutoFilter_throw: could not reset the form to it's original state!" );
}
}
}
catch( const RuntimeException& ) { throw; }
catch( const SQLException& ) { throw; }
catch( const Exception& )
{
throw WrappedTargetException( OUString(), *const_cast< FormOperations* >( this ), ::cppu::getCaughtException() );
}
}
//--------------------------------------------------------------------
void FormOperations::impl_executeFilterOrSort_throw( bool _bFilter ) const
{
OSL_PRECOND( m_xController.is(), "FormOperations::impl_executeFilterOrSort_throw: need a controller for this!" );
OSL_PRECOND( impl_hasCursor_nothrow(), "FormOperations::impl_executeFilterOrSort_throw: need a cursor for this!" );
OSL_PRECOND( impl_isParseable_throw(), "FormOperations::impl_executeFilterOrSort_throw: need a parseable statement for this!" );
if ( !m_xController.is() || !impl_hasCursor_nothrow() || !impl_isParseable_throw() )
return;
if ( !impl_commitCurrentControl_throw() || !impl_commitCurrentRecord_throw() )
return;
try
{
Reference< XExecutableDialog> xDialog;
if ( _bFilter )
{
xDialog = com::sun::star::sdb::FilterDialog::createWithQuery(m_xContext, m_xParser, m_xCursor,
Reference<com::sun::star::awt::XWindow>());
}
else
{
xDialog = com::sun::star::sdb::OrderDialog::createWithQuery(m_xContext, m_xParser, m_xCursorProperties);
}
if ( RET_OK == xDialog->execute() )
{
WaitObject aWO( NULL );
if ( _bFilter )
m_xCursorProperties->setPropertyValue( PROPERTY_FILTER, makeAny( m_xParser->getFilter() ) );
else
m_xCursorProperties->setPropertyValue( PROPERTY_SORT, makeAny( m_xParser->getOrder() ) );
m_xLoadableForm->reload();
}
}
catch( const RuntimeException& ) { throw; }
catch( const SQLException& ) { throw; }
catch( const Exception& )
{
throw WrappedTargetException( OUString(), *const_cast< FormOperations* >( this ), ::cppu::getCaughtException() );
}
}
//------------------------------------------------------------------------------
template < typename FunctObj >
void FormOperations::impl_doActionInSQLContext_throw( FunctObj f, sal_uInt16 _nErrorResourceId ) const
{
try
{
f();
}
catch( const SQLException& e )
{
(void)e;
if ( !_nErrorResourceId )
// no information to prepend
throw;
SQLExceptionInfo aInfo( ::cppu::getCaughtException() );
OUString sAdditionalError( FRM_RES_STRING( _nErrorResourceId ) );
aInfo.prepend( sAdditionalError );
aInfo.doThrow();
}
catch( const RuntimeException& ) { throw; }
catch( const Exception& )
{
OUString sAdditionalError( FRM_RES_STRING( _nErrorResourceId ) );
throw WrappedTargetException( sAdditionalError, *const_cast< FormOperations* >( this ), ::cppu::getCaughtException() );
}
}
//........................................................................
} // namespace frm
//........................................................................
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|