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 | /* -*- 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 <core_resource.hxx>
#include <dbwizsetup.hxx>
#include <dsmeta.hxx>
#include "DBSetupConnectionPages.hxx"
#include <strings.hrc>
#include <strings.hxx>
#include <dsitems.hxx>
#include "dsnItem.hxx"
#include <unotools/pathoptions.hxx>
#include <svl/stritem.hxx>
#include "adminpages.hxx"
#include <sfx2/docfilt.hxx>
#include <unotools/ucbhelper.hxx>
#include "generalpage.hxx"
#include <unotools/confignode.hxx>
#include "DbAdminImpl.hxx"
#include <helpids.h>
#include "ConnectionPageSetup.hxx"
#include <UITools.hxx>
#include <dbaccess/AsynchronousLink.hxx>
#include <sfx2/filedlghelper.hxx>
#include <cppuhelper/exc_hlp.hxx>
#include <cppuhelper/implbase.hxx>
#include <com/sun/star/frame/TerminationVetoException.hpp>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/sdb/DatabaseContext.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/frame/FrameSearchFlag.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/ucb/SimpleFileAccess.hpp>
#include <com/sun/star/ucb/InteractiveIOException.hpp>
#include <com/sun/star/io/IOException.hpp>
#include <com/sun/star/frame/XTerminateListener.hpp>
#include <com/sun/star/document/MacroExecMode.hpp>
#include <com/sun/star/ucb/IOErrorCode.hpp>
#include <com/sun/star/task/InteractionHandler.hpp>
#include <com/sun/star/task/XInteractionHandler2.hpp>
#include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
#include <comphelper/interaction.hxx>
#include <comphelper/namedvaluecollection.hxx>
#include <tools/diagnose_ex.h>
#include <osl/diagnose.h>
#include <connectivity/DriversConfig.hxx>
namespace dbaui
{
using namespace dbtools;
using namespace vcl;
using namespace com::sun::star;
using namespace com::sun::star::uno;
using namespace com::sun::star::sdbc;
using namespace com::sun::star::sdbcx;
using namespace com::sun::star::task;
using namespace com::sun::star::lang;
using namespace com::sun::star::io;
using namespace com::sun::star::util;
using namespace com::sun::star::beans;
using namespace com::sun::star::container;
using namespace com::sun::star::frame;
using namespace com::sun::star::ucb;
using namespace ::com::sun::star::sdb;
using namespace ::com::sun::star::document;
using namespace ::comphelper;
using namespace ::cppu;
using vcl::RoadmapWizardTypes::WizardPath;
// ODbTypeWizDialogSetup
ODbTypeWizDialogSetup::ODbTypeWizDialogSetup(weld::Window* _pParent
,SfxItemSet const * _pItems
,const Reference< XComponentContext >& _rxORB
,const css::uno::Any& _aDataSourceName
)
: vcl::RoadmapWizardMachine( _pParent )
, m_bIsConnectable( false)
, m_sRM_IntroText( DBA_RES( STR_PAGETITLE_INTROPAGE ) )
, m_sRM_dBaseText( DBA_RES( STR_PAGETITLE_DBASE ) )
, m_sRM_TextText( DBA_RES( STR_PAGETITLE_TEXT ) )
, m_sRM_MSAccessText( DBA_RES( STR_PAGETITLE_MSACCESS ) )
, m_sRM_LDAPText( DBA_RES( STR_PAGETITLE_LDAP ) )
, m_sRM_ADOText( DBA_RES( STR_PAGETITLE_ADO ) )
, m_sRM_JDBCText( DBA_RES( STR_PAGETITLE_JDBC ) )
, m_sRM_MySQLNativePageTitle( DBA_RES( STR_PAGETITLE_MYSQL_NATIVE ) )
, m_sRM_OracleText( DBA_RES( STR_PAGETITLE_ORACLE ) )
, m_sRM_MySQLText( DBA_RES( STR_PAGETITLE_MYSQL ) )
, m_sRM_ODBCText( DBA_RES( STR_PAGETITLE_ODBC ) )
, m_sRM_DocumentOrSpreadSheetText( DBA_RES( STR_PAGETITLE_DOCUMENT_OR_SPREADSHEET ) )
, m_sRM_AuthentificationText( DBA_RES( STR_PAGETITLE_AUTHENTIFICATION ) )
, m_sRM_FinalText( DBA_RES( STR_PAGETITLE_FINAL ) )
, m_sWorkPath( SvtPathOptions().GetWorkPath() )
, m_pGeneralPage( nullptr )
, m_pMySQLIntroPage( nullptr )
, m_pFinalPage( nullptr )
, m_pCollection( nullptr )
{
// no local resources needed anymore
// extract the datasource type collection from the item set
const DbuTypeCollectionItem* pCollectionItem = dynamic_cast<const DbuTypeCollectionItem*>( _pItems->GetItem(DSID_TYPECOLLECTION) );
if (pCollectionItem)
m_pCollection = pCollectionItem->getCollection();
OSL_ENSURE(m_pCollection, "ODbTypeWizDialogSetup::ODbTypeWizDialogSetup : really need a DSN type collection !");
m_pImpl.reset(new ODbDataSourceAdministrationHelper(_rxORB, m_xAssistant.get(), _pParent, this));
m_pImpl->setDataSourceOrName(_aDataSourceName);
Reference< XPropertySet > xDatasource = m_pImpl->getCurrentDataSource();
m_pOutSet.reset( new SfxItemSet( *_pItems->GetPool(), _pItems->GetRanges() ) );
m_pImpl->translateProperties(xDatasource, *m_pOutSet);
defaultButton(WizardButtonFlags::NEXT);
enableButtons(WizardButtonFlags::FINISH, true);
enableAutomaticNextButtonState();
::dbaccess::ODsnTypeCollection::TypeIterator aIter = m_pCollection->begin();
::dbaccess::ODsnTypeCollection::TypeIterator aEnd = m_pCollection->end();
for(PathId i = 1;aIter != aEnd;++aIter,++i)
{
const OUString& sURLPrefix = aIter.getURLPrefix();
WizardPath aPath;
aPath.push_back(PAGE_DBSETUPWIZARD_INTRO);
m_pCollection->fillPageIds(sURLPrefix,aPath);
aPath.push_back(PAGE_DBSETUPWIZARD_AUTHENTIFICATION);
aPath.push_back(PAGE_DBSETUPWIZARD_FINAL);
declareAuthDepPath(sURLPrefix,i,aPath);
}
WizardPath aPath;
aPath.push_back(PAGE_DBSETUPWIZARD_INTRO);
declarePath( static_cast<PathId>(m_pCollection->size()+1), aPath);
m_xPrevPage->set_help_id(HID_DBWIZ_PREVIOUS);
m_xNextPage->set_help_id(HID_DBWIZ_NEXT);
m_xCancel->set_help_id(HID_DBWIZ_CANCEL);
m_xFinish->set_help_id(HID_DBWIZ_FINISH);
ActivatePage();
setTitleBase(DBA_RES(STR_DBWIZARDTITLE));
m_xAssistant->set_current_page(0);
}
void ODbTypeWizDialogSetup::declareAuthDepPath( const OUString& _sURL, PathId _nPathId, const WizardPath& _rPaths)
{
bool bHasAuthentication = DataSourceMetaData::getAuthentication( _sURL ) != AuthNone;
// collect the elements of the path
WizardPath aPath;
for (auto const& path : _rPaths)
{
if ( bHasAuthentication || ( path != PAGE_DBSETUPWIZARD_AUTHENTIFICATION ) )
aPath.push_back(path);
}
// call base method
::vcl::RoadmapWizardMachine::declarePath( _nPathId, aPath );
}
OUString ODbTypeWizDialogSetup::getStateDisplayName(WizardState _nState) const
{
OUString sRoadmapItem;
switch( _nState )
{
case PAGE_DBSETUPWIZARD_INTRO:
sRoadmapItem = m_sRM_IntroText;
break;
case PAGE_DBSETUPWIZARD_DBASE:
sRoadmapItem = m_sRM_dBaseText;
break;
case PAGE_DBSETUPWIZARD_ADO:
sRoadmapItem = m_sRM_ADOText;
break;
case PAGE_DBSETUPWIZARD_TEXT:
sRoadmapItem = m_sRM_TextText;
break;
case PAGE_DBSETUPWIZARD_MSACCESS:
sRoadmapItem = m_sRM_MSAccessText;
break;
case PAGE_DBSETUPWIZARD_LDAP:
sRoadmapItem = m_sRM_LDAPText;
break;
case PAGE_DBSETUPWIZARD_JDBC:
sRoadmapItem = m_sRM_JDBCText;
break;
case PAGE_DBSETUPWIZARD_ORACLE:
sRoadmapItem = m_sRM_OracleText;
break;
case PAGE_DBSETUPWIZARD_MYSQL_INTRO:
sRoadmapItem = m_sRM_MySQLText;
break;
case PAGE_DBSETUPWIZARD_MYSQL_JDBC:
sRoadmapItem = m_sRM_JDBCText;
break;
case PAGE_DBSETUPWIZARD_MYSQL_NATIVE:
sRoadmapItem = m_sRM_MySQLNativePageTitle;
break;
case PAGE_DBSETUPWIZARD_MYSQL_ODBC:
sRoadmapItem = m_sRM_ODBCText;
break;
case PAGE_DBSETUPWIZARD_ODBC:
sRoadmapItem = m_sRM_ODBCText;
break;
case PAGE_DBSETUPWIZARD_DOCUMENT_OR_SPREADSHEET:
sRoadmapItem = m_sRM_DocumentOrSpreadSheetText;
break;
case PAGE_DBSETUPWIZARD_AUTHENTIFICATION:
sRoadmapItem = m_sRM_AuthentificationText;
break;
case PAGE_DBSETUPWIZARD_USERDEFINED:
sRoadmapItem = DBA_RES(STR_PAGETITLE_CONNECTION);
break;
case PAGE_DBSETUPWIZARD_FINAL:
sRoadmapItem = m_sRM_FinalText;
break;
default:
break;
}
return sRoadmapItem;
}
ODbTypeWizDialogSetup::~ODbTypeWizDialogSetup()
{
}
IMPL_LINK_NOARG(ODbTypeWizDialogSetup, OnTypeSelected, OGeneralPage&, void)<--- syntax error
{
activateDatabasePath();
}
static void lcl_removeUnused(const ::comphelper::NamedValueCollection& _aOld,const ::comphelper::NamedValueCollection& _aNew,::comphelper::NamedValueCollection& _rDSInfo)
{
_rDSInfo.merge(_aNew,true);
uno::Sequence< beans::NamedValue > aOldValues = _aOld.getNamedValues();
const beans::NamedValue* pIter = aOldValues.getConstArray();
const beans::NamedValue* pEnd = pIter + aOldValues.getLength();
for(;pIter != pEnd;++pIter)
{
if ( !_aNew.has(pIter->Name) )
{
_rDSInfo.remove(pIter->Name);
}
}
}
void DataSourceInfoConverter::convert(const Reference<XComponentContext> & xContext, const ::dbaccess::ODsnTypeCollection* _pCollection,const OUString& _sOldURLPrefix,const OUString& _sNewURLPrefix,const css::uno::Reference< css::beans::XPropertySet >& _xDatasource)
{
if ( _pCollection->getPrefix(_sOldURLPrefix) == _pCollection->getPrefix(_sNewURLPrefix) )
return ;
uno::Sequence< beans::PropertyValue> aInfo;
_xDatasource->getPropertyValue(PROPERTY_INFO) >>= aInfo;
::comphelper::NamedValueCollection aDS(aInfo);
::connectivity::DriversConfig aDriverConfig(xContext);
const ::comphelper::NamedValueCollection& aOldProperties = aDriverConfig.getProperties(_sOldURLPrefix);
const ::comphelper::NamedValueCollection& aNewProperties = aDriverConfig.getProperties(_sNewURLPrefix);
lcl_removeUnused(aOldProperties,aNewProperties,aDS);
aDS >>= aInfo;
_xDatasource->setPropertyValue(PROPERTY_INFO,uno::makeAny(aInfo));
}
void ODbTypeWizDialogSetup::activateDatabasePath()
{
switch ( m_pGeneralPage->GetDatabaseCreationMode() )
{
case OGeneralPageWizard::eCreateNew:
{
sal_Int32 nCreateNewDBIndex = m_pCollection->getIndexOf( m_pGeneralPage->GetSelectedType() );
if ( nCreateNewDBIndex == -1 )
nCreateNewDBIndex = m_pCollection->getIndexOf( "sdbc:dbase:" );
OSL_ENSURE( nCreateNewDBIndex != -1, "ODbTypeWizDialogSetup::activateDatabasePath: the GeneralPage should have prevented this!" );
activatePath( static_cast< PathId >( nCreateNewDBIndex + 1 ), true );
enableState(PAGE_DBSETUPWIZARD_FINAL );
enableButtons( WizardButtonFlags::FINISH, true);
}
break;
case OGeneralPageWizard::eConnectExternal:
{
OUString sOld = m_sURL;
m_sURL = m_pGeneralPage->GetSelectedType();
DataSourceInfoConverter::convert(getORB(), m_pCollection,sOld,m_sURL,m_pImpl->getCurrentDataSource());
::dbaccess::DATASOURCE_TYPE eType = VerifyDataSourceType(m_pCollection->determineType(m_sURL));
if (eType == ::dbaccess::DST_UNKNOWN)
m_pCollection->determineType(m_sOldURL);
activatePath( static_cast<PathId>(m_pCollection->getIndexOf(m_sURL) + 1), true);
updateTypeDependentStates();
}
break;
case OGeneralPageWizard::eOpenExisting:
{
activatePath( static_cast<PathId>(m_pCollection->size() + 1), true );
enableButtons( WizardButtonFlags::FINISH, !m_pGeneralPage->GetSelectedDocumentURL().isEmpty() );
}
break;
default:
OSL_FAIL( "ODbTypeWizDialogSetup::activateDatabasePath: unknown creation mode!" );
}
enableButtons( WizardButtonFlags::NEXT, m_pGeneralPage->GetDatabaseCreationMode() != OGeneralPageWizard::eOpenExisting );
// TODO: this should go into the base class. Point is, we activate a path whose *last*
// step is also the current one. The base class should automatically disable
// the Next button in such a case. However, not for this patch ...
}
void ODbTypeWizDialogSetup::updateTypeDependentStates()
{
bool bDoEnable = false;
bool bIsConnectionRequired = m_pCollection->isConnectionUrlRequired(m_sURL);
if (!bIsConnectionRequired)
{
bDoEnable = true;
}
else if ( m_sURL == m_sOldURL )
{
bDoEnable = m_bIsConnectable;
}
enableState(PAGE_DBSETUPWIZARD_AUTHENTIFICATION, bDoEnable);
enableState(PAGE_DBSETUPWIZARD_FINAL, bDoEnable );
enableButtons( WizardButtonFlags::FINISH, bDoEnable);
}
void ODbTypeWizDialogSetup::resetPages(const Reference< XPropertySet >& _rxDatasource)
{
// remove all items which relate to indirect properties from the input set
// (without this, the following may happen: select an arbitrary data source where some indirect properties
// are set. Select another data source of the same type, where the indirect props are not set (yet). Then,
// the indirect property values of the first ds are shown in the second ds ...)
const ODbDataSourceAdministrationHelper::MapInt2String& rMap = m_pImpl->getIndirectProperties();
for (auto const& elem : rMap)
getWriteOutputSet()->ClearItem( static_cast<sal_uInt16>(elem.first) );
// extract all relevant data from the property set of the data source
m_pImpl->translateProperties(_rxDatasource, *getWriteOutputSet());
}
const SfxItemSet* ODbTypeWizDialogSetup::getOutputSet() const
{
return m_pOutSet.get();
}
SfxItemSet* ODbTypeWizDialogSetup::getWriteOutputSet()
{
return m_pOutSet.get();
}
std::pair< Reference<XConnection>,bool> ODbTypeWizDialogSetup::createConnection()
{
return m_pImpl->createConnection();
}
Reference< XComponentContext > ODbTypeWizDialogSetup::getORB() const
{
return m_pImpl->getORB();
}
Reference< XDriver > ODbTypeWizDialogSetup::getDriver()
{
return m_pImpl->getDriver();
}
::dbaccess::DATASOURCE_TYPE ODbTypeWizDialogSetup::VerifyDataSourceType(const ::dbaccess::DATASOURCE_TYPE DatabaseType) const
{
::dbaccess::DATASOURCE_TYPE LocDatabaseType = DatabaseType;
if ((LocDatabaseType == ::dbaccess::DST_MYSQL_JDBC) || (LocDatabaseType == ::dbaccess::DST_MYSQL_ODBC) || (LocDatabaseType == ::dbaccess::DST_MYSQL_NATIVE))
{
if (m_pMySQLIntroPage != nullptr)
{
switch( m_pMySQLIntroPage->getMySQLMode() )
{
case OMySQLIntroPageSetup::VIA_JDBC:
return ::dbaccess::DST_MYSQL_JDBC;
case OMySQLIntroPageSetup::VIA_NATIVE:
return ::dbaccess::DST_MYSQL_NATIVE;
case OMySQLIntroPageSetup::VIA_ODBC:
return ::dbaccess::DST_MYSQL_ODBC;
}
}
}
return LocDatabaseType;
}
OUString ODbTypeWizDialogSetup::getDatasourceType(const SfxItemSet& _rSet) const
{
OUString sRet = dbaui::ODbDataSourceAdministrationHelper::getDatasourceType(_rSet);
if (m_pMySQLIntroPage && m_pMySQLIntroPage->IsVisible())
{
switch( m_pMySQLIntroPage->getMySQLMode() )
{
case OMySQLIntroPageSetup::VIA_JDBC:
sRet = "sdbc:mysql:jdbc:";
break;
case OMySQLIntroPageSetup::VIA_NATIVE:
sRet = "sdbc:mysql:mysqlc:";
break;
case OMySQLIntroPageSetup::VIA_ODBC:
sRet = "sdbc:mysql:odbc:";
break;
}
}
return sRet;
}
void ODbTypeWizDialogSetup::clearPassword()
{
m_pImpl->clearPassword();
}
void ODbTypeWizDialogSetup::SetIntroPage(OMySQLIntroPageSetup* pPage)
{
m_pMySQLIntroPage = pPage;
m_pMySQLIntroPage->SetClickHdl(LINK( this, ODbTypeWizDialogSetup, ImplClickHdl ) );
}
void ODbTypeWizDialogSetup::SetGeneralPage(OGeneralPageWizard* pPage)
{
m_pGeneralPage = pPage;
m_pGeneralPage->SetTypeSelectHandler(LINK(this, ODbTypeWizDialogSetup, OnTypeSelected));
m_pGeneralPage->SetCreationModeHandler(LINK( this, ODbTypeWizDialogSetup, OnChangeCreationMode ) );
m_pGeneralPage->SetDocumentSelectionHandler(LINK( this, ODbTypeWizDialogSetup, OnRecentDocumentSelected ) );
m_pGeneralPage->SetChooseDocumentHandler(LINK( this, ODbTypeWizDialogSetup, OnSingleDocumentChosen ) );
}
void ODbTypeWizDialogSetup::SetFinalPage(OFinalDBPageSetup* pPage)
{
m_pFinalPage = pPage;
}
std::unique_ptr<BuilderPage> ODbTypeWizDialogSetup::createPage(WizardState _nState)
{
std::unique_ptr<OGenericAdministrationPage> xPage;
OString sIdent(OString::number(_nState));
weld::Container* pPageContainer = m_xAssistant->append_page(sIdent);
switch(_nState)
{
case PAGE_DBSETUPWIZARD_INTRO:
xPage = std::make_unique<OGeneralPageWizard>(pPageContainer,this,*m_pOutSet);
break;
case PAGE_DBSETUPWIZARD_DBASE:
xPage = OConnectionTabPageSetup::CreateDbaseTabPage(pPageContainer, this, *m_pOutSet);
break;
case PAGE_DBSETUPWIZARD_ADO:
xPage = OConnectionTabPageSetup::CreateADOTabPage(pPageContainer, this, *m_pOutSet);
break;
case PAGE_DBSETUPWIZARD_TEXT:
xPage = OTextConnectionPageSetup::CreateTextTabPage(pPageContainer, this, *m_pOutSet);
break;
case PAGE_DBSETUPWIZARD_ODBC:
xPage = OConnectionTabPageSetup::CreateODBCTabPage(pPageContainer, this, *m_pOutSet);
break;
case PAGE_DBSETUPWIZARD_JDBC:
xPage = OJDBCConnectionPageSetup::CreateJDBCTabPage(pPageContainer, this, *m_pOutSet);
break;
case PAGE_DBSETUPWIZARD_MYSQL_ODBC:
m_pOutSet->Put(SfxStringItem(DSID_CONNECTURL, m_pCollection->getPrefix("sdbc:mysql:odbc:")));
xPage = OConnectionTabPageSetup::CreateODBCTabPage(pPageContainer, this, *m_pOutSet);
break;
case PAGE_DBSETUPWIZARD_MYSQL_JDBC:
m_pOutSet->Put(SfxStringItem(DSID_CONNECTURL, m_pCollection->getPrefix("sdbc:mysql:jdbc:")));
xPage = OGeneralSpecialJDBCConnectionPageSetup::CreateMySQLJDBCTabPage(pPageContainer, this, *m_pOutSet);
break;
case PAGE_DBSETUPWIZARD_MYSQL_NATIVE:
m_pOutSet->Put(SfxStringItem(DSID_CONNECTURL, m_pCollection->getPrefix("sdbc:mysql:mysqlc:")));
xPage = MySQLNativeSetupPage::Create(pPageContainer, this, *m_pOutSet);
break;
case PAGE_DBSETUPWIZARD_ORACLE:
xPage = OGeneralSpecialJDBCConnectionPageSetup::CreateOracleJDBCTabPage(pPageContainer, this, *m_pOutSet);
break;
case PAGE_DBSETUPWIZARD_LDAP:
xPage = OLDAPConnectionPageSetup::CreateLDAPTabPage(pPageContainer, this, *m_pOutSet);
break;
case PAGE_DBSETUPWIZARD_DOCUMENT_OR_SPREADSHEET:
xPage = OSpreadSheetConnectionPageSetup::CreateDocumentOrSpreadSheetTabPage(pPageContainer, this, *m_pOutSet);
break;
case PAGE_DBSETUPWIZARD_MSACCESS:
xPage = OConnectionTabPageSetup::CreateMSAccessTabPage(pPageContainer, this, *m_pOutSet);
break;
case PAGE_DBSETUPWIZARD_MYSQL_INTRO:
xPage = OMySQLIntroPageSetup::CreateMySQLIntroTabPage(pPageContainer, this, *m_pOutSet);
break;
case PAGE_DBSETUPWIZARD_AUTHENTIFICATION:
xPage = OAuthentificationPageSetup::CreateAuthentificationTabPage(pPageContainer, this, *m_pOutSet);
break;
case PAGE_DBSETUPWIZARD_USERDEFINED:
xPage = OConnectionTabPageSetup::CreateUserDefinedTabPage(pPageContainer, this, *m_pOutSet);
break;
case PAGE_DBSETUPWIZARD_FINAL:
xPage = OFinalDBPageSetup::CreateFinalDBTabPageSetup(pPageContainer, this, *m_pOutSet);
break;
}
if ( xPage )
{
if ((_nState != PAGE_DBSETUPWIZARD_INTRO) && (_nState != PAGE_DBSETUPWIZARD_AUTHENTIFICATION))
{
xPage->SetModifiedHandler(LINK( this, ODbTypeWizDialogSetup, ImplModifiedHdl ) );
}
xPage->SetServiceFactory( m_pImpl->getORB() );
xPage->SetAdminDialog(this, this);
defaultButton( _nState == PAGE_DBSETUPWIZARD_FINAL ? WizardButtonFlags::FINISH : WizardButtonFlags::NEXT );
enableButtons( WizardButtonFlags::FINISH, _nState == PAGE_DBSETUPWIZARD_FINAL );
enableButtons( WizardButtonFlags::NEXT, _nState != PAGE_DBSETUPWIZARD_FINAL );
m_xAssistant->set_page_title(sIdent, getStateDisplayName(_nState));
}
return xPage;
}
IMPL_LINK(ODbTypeWizDialogSetup, ImplModifiedHdl, OGenericAdministrationPage const *, _pConnectionPageSetup, void)
{
m_bIsConnectable = _pConnectionPageSetup->GetRoadmapStateValue( );
enableState(PAGE_DBSETUPWIZARD_FINAL, m_bIsConnectable);
enableState(PAGE_DBSETUPWIZARD_AUTHENTIFICATION, m_bIsConnectable);
if (getCurrentState() == PAGE_DBSETUPWIZARD_FINAL)
enableButtons( WizardButtonFlags::FINISH, true);
else
enableButtons( WizardButtonFlags::FINISH, m_bIsConnectable);
enableButtons( WizardButtonFlags::NEXT, m_bIsConnectable && (getCurrentState() != PAGE_DBSETUPWIZARD_FINAL));
}
IMPL_LINK(ODbTypeWizDialogSetup, ImplClickHdl, OMySQLIntroPageSetup*, _pMySQLIntroPageSetup, void)
{
OUString sURLPrefix;
switch( _pMySQLIntroPageSetup->getMySQLMode() )
{
case OMySQLIntroPageSetup::VIA_ODBC:
sURLPrefix = "sdbc:mysql:odbc:";
break;
case OMySQLIntroPageSetup::VIA_JDBC:
sURLPrefix = "sdbc:mysql:jdbc:";
break;
case OMySQLIntroPageSetup::VIA_NATIVE:
sURLPrefix = "sdbc:mysql:mysqlc:";
break;
}
activatePath( static_cast<PathId>(m_pCollection->getIndexOf(sURLPrefix) + 1), true);
}
IMPL_LINK_NOARG(ODbTypeWizDialogSetup, OnChangeCreationMode, OGeneralPageWizard&, void)
{
activateDatabasePath();
}
IMPL_LINK_NOARG(ODbTypeWizDialogSetup, OnRecentDocumentSelected, OGeneralPageWizard&, void)
{
enableButtons( WizardButtonFlags::FINISH, !m_pGeneralPage->GetSelectedDocumentURL().isEmpty() );
}
IMPL_LINK_NOARG(ODbTypeWizDialogSetup, OnSingleDocumentChosen, OGeneralPageWizard&, void)
{
if (prepareLeaveCurrentState(WizardTypes::eFinish))
onFinish();
}
void ODbTypeWizDialogSetup::enterState(WizardState _nState)
{
m_sURL = dbaui::ODbDataSourceAdministrationHelper::getDatasourceType(*m_pOutSet);
RoadmapWizardMachine::enterState(_nState);
switch(_nState)
{
case PAGE_DBSETUPWIZARD_INTRO:
m_sOldURL = m_sURL;
break;
case PAGE_DBSETUPWIZARD_FINAL:
enableButtons( WizardButtonFlags::FINISH, true);
if ( m_pFinalPage )
m_pFinalPage->enableTableWizardCheckBox(m_pCollection->supportsTableCreation(m_sURL));
break;
}
}
void ODbTypeWizDialogSetup::saveDatasource()
{
SfxTabPage* pPage = static_cast<SfxTabPage*>(GetPage(getCurrentState()));
if ( pPage )
pPage->FillItemSet(m_pOutSet.get());
}
bool ODbTypeWizDialogSetup::leaveState(WizardState _nState)
{
if (_nState == PAGE_DBSETUPWIZARD_MYSQL_INTRO)
return true;
if ( _nState == PAGE_DBSETUPWIZARD_INTRO && m_sURL != m_sOldURL )
{
resetPages(m_pImpl->getCurrentDataSource());
}
SfxTabPage* pPage = static_cast<SfxTabPage*>(GetPage(_nState));
return pPage && pPage->DeactivatePage(m_pOutSet.get()) != DeactivateRC::KeepPage;
}
void ODbTypeWizDialogSetup::setTitle(const OUString& _sTitle)
{
m_xAssistant->set_title(_sTitle);
}
void ODbTypeWizDialogSetup::enableConfirmSettings( bool /*_bEnable*/ )
{
}
namespace
{
bool lcl_handle( const Reference< XInteractionHandler2 >& _rxHandler, const Any& _rRequest )
{
OInteractionRequest* pRequest = new OInteractionRequest( _rRequest );
Reference < XInteractionRequest > xRequest( pRequest );
OInteractionAbort* pAbort = new OInteractionAbort;
pRequest->addContinuation( pAbort );
return _rxHandler->handleInteractionRequest( xRequest );
}
}
bool ODbTypeWizDialogSetup::SaveDatabaseDocument()
{
Reference< XInteractionHandler2 > xHandler( InteractionHandler::createWithParent(getORB(), nullptr) );
try
{
if (callSaveAsDialog())
{
m_pImpl->saveChanges(*m_pOutSet);
Reference< XPropertySet > xDatasource = m_pImpl->getCurrentDataSource();
Reference< XModel > xModel( getDataSourceOrModel( xDatasource ), UNO_QUERY_THROW );
Reference< XStorable > xStore( xModel, UNO_QUERY_THROW );
if ( m_pGeneralPage->GetDatabaseCreationMode() == OGeneralPageWizard::eCreateNew )
CreateDatabase();
::comphelper::NamedValueCollection aArgs( xModel->getArgs() );
aArgs.put( "Overwrite", true );
aArgs.put( "InteractionHandler", xHandler );
aArgs.put( "MacroExecutionMode", MacroExecMode::USE_CONFIG );
aArgs.put( "IgnoreFirebirdMigration", true );
OUString sPath = ODbDataSourceAdministrationHelper::getDocumentUrl( *m_pOutSet );
xStore->storeAsURL( sPath, aArgs.getPropertyValues() );
if ( !m_pFinalPage || m_pFinalPage->IsDatabaseDocumentToBeRegistered() )
RegisterDataSourceByLocation( sPath );
return true;
}
}
catch ( const Exception& e )
{
Any aError = ::cppu::getCaughtException();
if ( xHandler.is() )
{
if ( !lcl_handle( xHandler, aError ) )
{
InteractiveIOException aRequest;
aRequest.Classification = InteractionClassification_ERROR;
if ( aError.isExtractableTo( ::cppu::UnoType< IOException >::get() ) )
// assume saving the document failed
aRequest.Code = IOErrorCode_CANT_WRITE;
else
aRequest.Code = IOErrorCode_GENERAL;
aRequest.Message = e.Message;
aRequest.Context = e.Context;
lcl_handle( xHandler, makeAny( aRequest ) );
}
}
}
return false;
}
bool ODbTypeWizDialogSetup::IsDatabaseDocumentToBeOpened() const
{
if ( m_pGeneralPage->GetDatabaseCreationMode() == OGeneralPageWizard::eOpenExisting )
return true;
if ( m_pFinalPage != nullptr )
return m_pFinalPage->IsDatabaseDocumentToBeOpened();
return true;
}
bool ODbTypeWizDialogSetup::IsTableWizardToBeStarted() const
{
if ( m_pGeneralPage->GetDatabaseCreationMode() == OGeneralPageWizard::eOpenExisting )
return false;
if ( m_pFinalPage != nullptr )
return m_pFinalPage->IsTableWizardToBeStarted();
return false;
}
void ODbTypeWizDialogSetup::CreateDatabase()
{
OUString sUrl;
const OUString eType = m_pGeneralPage->GetSelectedType();
if ( dbaccess::ODsnTypeCollection::isEmbeddedDatabase(eType) )
{
sUrl = eType;
Reference< XPropertySet > xDatasource = m_pImpl->getCurrentDataSource();
OSL_ENSURE(xDatasource.is(),"DataSource is null!");
if ( xDatasource.is() )
xDatasource->setPropertyValue( PROPERTY_INFO, makeAny( m_pCollection->getDefaultDBSettings( eType ) ) );
m_pImpl->translateProperties(xDatasource,*m_pOutSet);
}
else if ( m_pCollection->isFileSystemBased(eType) )
{
Reference< XSimpleFileAccess3 > xSimpleFileAccess(ucb::SimpleFileAccess::create(getORB()));
INetURLObject aDBPathURL(m_sWorkPath);
aDBPathURL.Append(m_aDocURL.getBase());
createUniqueFolderName(&aDBPathURL);
sUrl = aDBPathURL.GetMainURL( INetURLObject::DecodeMechanism::NONE);
xSimpleFileAccess->createFolder(sUrl);
sUrl = eType.concat(sUrl);
}
m_pOutSet->Put(SfxStringItem(DSID_CONNECTURL, sUrl));
m_pImpl->saveChanges(*m_pOutSet);
}
void ODbTypeWizDialogSetup::RegisterDataSourceByLocation(const OUString& _sPath)
{
Reference< XPropertySet > xDatasource = m_pImpl->getCurrentDataSource();
Reference< XDatabaseContext > xDatabaseContext( DatabaseContext::create(getORB()) );
INetURLObject aURL( _sPath );
OUString sFilename = aURL.getBase( INetURLObject::LAST_SEGMENT, true, INetURLObject::DecodeMechanism::WithCharset );
OUString sDatabaseName = ::dbtools::createUniqueName(xDatabaseContext, sFilename, false);
xDatabaseContext->registerObject(sDatabaseName, xDatasource);
}
bool ODbTypeWizDialogSetup::callSaveAsDialog()
{
bool bRet = false;
::sfx2::FileDialogHelper aFileDlg(
ui::dialogs::TemplateDescription::FILESAVE_AUTOEXTENSION,
FileDialogFlags::NONE, m_xAssistant.get());
std::shared_ptr<const SfxFilter> pFilter = getStandardDatabaseFilter();
if ( pFilter )
{
INetURLObject aWorkURL( m_sWorkPath );
aFileDlg.SetDisplayFolder( aWorkURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ));
OUString sDefaultName = DBA_RES( STR_DATABASEDEFAULTNAME );
OUString sExtension = pFilter->GetDefaultExtension();
sDefaultName += sExtension.replaceAt( 0, 1, OUString() );
aWorkURL.Append( sDefaultName );
sDefaultName = createUniqueFileName( aWorkURL );
aFileDlg.SetFileName( sDefaultName );
aFileDlg.AddFilter(pFilter->GetUIName(),pFilter->GetDefaultExtension());
aFileDlg.SetCurrentFilter(pFilter->GetUIName());
}
if ( aFileDlg.Execute() == ERRCODE_NONE )
{
m_aDocURL = INetURLObject(aFileDlg.GetPath());
if( m_aDocURL.GetProtocol() != INetProtocol::NotValid )
{
OUString sFileName = m_aDocURL.GetMainURL( INetURLObject::DecodeMechanism::NONE );
if ( ::utl::UCBContentHelper::IsDocument(sFileName) )
::utl::UCBContentHelper::Kill(sFileName);
m_pOutSet->Put(SfxStringItem(DSID_DOCUMENT_URL, sFileName));
bRet = true;
}
}
return bRet;
}
void ODbTypeWizDialogSetup::createUniqueFolderName(INetURLObject* pURL)
{
Reference< XSimpleFileAccess3 > xSimpleFileAccess(ucb::SimpleFileAccess::create(getORB()));
OUString sLastSegmentName = pURL->getName();
bool bFolderExists = true;
sal_Int32 i = 1;
while (bFolderExists)
{
bFolderExists = xSimpleFileAccess->isFolder(pURL->GetMainURL( INetURLObject::DecodeMechanism::NONE ));
if (bFolderExists)
{
i++;
pURL->setName(sLastSegmentName.concat(OUString::number(i)));
}
}
}
OUString ODbTypeWizDialogSetup::createUniqueFileName(const INetURLObject& _rURL)
{
Reference< XSimpleFileAccess3 > xSimpleFileAccess(ucb::SimpleFileAccess::create(getORB()));
OUString BaseName = _rURL.getBase();
bool bElementExists = true;
INetURLObject aExistenceCheck( _rURL );
for ( sal_Int32 i = 1; bElementExists; )
{
bElementExists = xSimpleFileAccess->exists( aExistenceCheck.GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
if ( bElementExists )
{
aExistenceCheck.setBase( BaseName.concat( OUString::number( i ) ) );
++i;
}
}
return aExistenceCheck.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DecodeMechanism::WithCharset );
}
vcl::IWizardPageController* ODbTypeWizDialogSetup::getPageController(BuilderPage* pCurrentPage) const
{
OGenericAdministrationPage* pPage = static_cast<OGenericAdministrationPage*>(pCurrentPage);
return pPage;
}
namespace
{
typedef ::cppu::WeakImplHelper< XTerminateListener
> AsyncLoader_Base;
class AsyncLoader : public AsyncLoader_Base
{
private:
Reference< XComponentLoader > m_xFrameLoader;
Reference< XDesktop2 > m_xDesktop;
Reference< XInteractionHandler2 > m_xInteractionHandler;
OUString m_sURL;
OAsynchronousLink m_aAsyncCaller;
public:
AsyncLoader( const Reference< XComponentContext >& _rxORB, const OUString& _rURL );
void doLoadAsync();
// XTerminateListener
virtual void SAL_CALL queryTermination( const css::lang::EventObject& Event ) override;
virtual void SAL_CALL notifyTermination( const css::lang::EventObject& Event ) override;
// XEventListener
virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
private:
DECL_LINK( OnOpenDocument, void*, void );
};
AsyncLoader::AsyncLoader( const Reference< XComponentContext >& _rxORB, const OUString& _rURL )
:m_sURL( _rURL )
,m_aAsyncCaller( LINK( this, AsyncLoader, OnOpenDocument ) )
{
try
{
m_xDesktop.set( Desktop::create(_rxORB) );
m_xFrameLoader.set( m_xDesktop, UNO_QUERY_THROW );
m_xInteractionHandler = InteractionHandler::createWithParent(_rxORB, nullptr);
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
}
void AsyncLoader::doLoadAsync()
{
OSL_ENSURE( !m_aAsyncCaller.IsRunning(), "AsyncLoader:doLoadAsync: already running!" );
acquire();
try
{
if ( m_xDesktop.is() )
m_xDesktop->addTerminateListener( this );
}
catch( const Exception& ) { DBG_UNHANDLED_EXCEPTION("dbaccess"); }
m_aAsyncCaller.Call();
}
IMPL_LINK_NOARG( AsyncLoader, OnOpenDocument, void*, void )
{
try
{
if ( m_xFrameLoader.is() )
{
::comphelper::NamedValueCollection aLoadArgs;
aLoadArgs.put( "InteractionHandler", m_xInteractionHandler );
aLoadArgs.put( "MacroExecutionMode", MacroExecMode::USE_CONFIG );
Sequence< PropertyValue > aLoadArgPV;
aLoadArgs >>= aLoadArgPV;
m_xFrameLoader->loadComponentFromURL( m_sURL,
"_default",
FrameSearchFlag::ALL,
aLoadArgPV
);
}
}
catch( const Exception& )
{
// do not assert.
// Such an exception happens for instance of the to-be-loaded document does not exist anymore.
}
try
{
if ( m_xDesktop.is() )
m_xDesktop->removeTerminateListener( this );
}
catch( const Exception& ) { DBG_UNHANDLED_EXCEPTION("dbaccess"); }
release();
}
void SAL_CALL AsyncLoader::queryTermination( const css::lang::EventObject& /*Event*/ )
{
throw TerminationVetoException();
}
void SAL_CALL AsyncLoader::notifyTermination( const css::lang::EventObject& /*Event*/ )
{
}
void SAL_CALL AsyncLoader::disposing( const css::lang::EventObject& /*Source*/ )
{
}
}
bool ODbTypeWizDialogSetup::onFinish()
{
if ( m_pGeneralPage->GetDatabaseCreationMode() == OGeneralPageWizard::eOpenExisting )
{
// we're not going to re-use the XModel we have - since the document the user
// wants us to load could be a non-database document. Instead, we asynchronously
// open the selected document. Thus, the wizard's return value is RET_CANCEL,
// which means to not continue loading the database document
if ( !WizardMachine::Finish() )
return false;
try
{
AsyncLoader* pAsyncLoader = new AsyncLoader( getORB(), m_pGeneralPage->GetSelectedDocumentURL() );
::rtl::Reference< AsyncLoader > xKeepAlive( pAsyncLoader );
pAsyncLoader->doLoadAsync();
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION("dbaccess");
}
return true;
}
if (getCurrentState() != PAGE_DBSETUPWIZARD_FINAL)
{
skipUntil(PAGE_DBSETUPWIZARD_FINAL);
}
if (getCurrentState() == PAGE_DBSETUPWIZARD_FINAL)
return SaveDatabaseDocument() && WizardMachine::onFinish();
else
{
enableButtons( WizardButtonFlags::FINISH, false );
return false;
}
}
} // namespace dbaui
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|