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
|
/* -*- 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 "moduldlg.hxx"
#include <basidesh.hxx>
#include <strings.hrc>
#include <bitmaps.hlst>
#include <iderdll.hxx>
#include "iderdll2.hxx"
#include <iderid.hxx>
#include <basobj.hxx>
#include <svx/passwd.hxx>
#include <ucbhelper/content.hxx>
#include <rtl/uri.hxx>
#include <sfx2/app.hxx>
#include <sfx2/dispatch.hxx>
#include <sfx2/filedlghelper.hxx>
#include <sfx2/request.hxx>
#include <sfx2/sfxsids.hrc>
#include <sfx2/viewfrm.hxx>
#include <svl/stritem.hxx>
#include <tools/debug.hxx>
#include <tools/urlobj.hxx>
#include <tools/diagnose_ex.h>
#include <vcl/svapp.hxx>
#include <vcl/weld.hxx>
#include <com/sun/star/io/Pipe.hpp>
#include <com/sun/star/ui/dialogs/XFilePicker3.hpp>
#include <com/sun/star/ui/dialogs/XFolderPicker2.hpp>
#include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
#include <com/sun/star/script/DocumentScriptLibraryContainer.hpp>
#include <com/sun/star/script/DocumentDialogLibraryContainer.hpp>
#include <com/sun/star/script/XLibraryContainerPassword.hpp>
#include <com/sun/star/script/XLibraryContainerExport.hpp>
#include <com/sun/star/task/InteractionHandler.hpp>
#include <com/sun/star/ucb/SimpleFileAccess.hpp>
#include <com/sun/star/ucb/XCommandEnvironment.hpp>
#include <com/sun/star/ucb/NameClash.hpp>
#include <com/sun/star/packages/manifest/ManifestWriter.hpp>
#include <unotools/pathoptions.hxx>
#include <com/sun/star/util/VetoException.hpp>
#include <com/sun/star/script/ModuleSizeExceededRequest.hpp>
#include <comphelper/processfactory.hxx>
#include <comphelper/propertysequence.hxx>
#include <cppuhelper/implbase.hxx>
#include <o3tl/string_view.hxx>
#include <cassert>
namespace basctl
{
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::ucb;
using namespace ::com::sun::star::ui::dialogs;
namespace
{
class DummyInteractionHandler : public ::cppu::WeakImplHelper< task::XInteractionHandler >
{
Reference< task::XInteractionHandler2 > m_xHandler;
public:
explicit DummyInteractionHandler(const Reference<task::XInteractionHandler2>& xHandler)
: m_xHandler(xHandler)
{
}
virtual void SAL_CALL handle( const Reference< task::XInteractionRequest >& rRequest ) override
{
if ( m_xHandler.is() )
{
script::ModuleSizeExceededRequest aModSizeException;
if ( rRequest->getRequest() >>= aModSizeException )
m_xHandler->handle( rRequest );
}
}
};
} // namespace
namespace
{
int FindEntry(const weld::TreeView& rBox, std::u16string_view rName)
{
int nCount = rBox.n_children();
for (int i = 0; i < nCount; ++i)
{
if (o3tl::equalsIgnoreAsciiCase(rName, rBox.get_text(i, 0)))
return i;
}
return -1;
}
}
// NewObjectDialog
IMPL_LINK_NOARG(NewObjectDialog, OkButtonHandler, weld::Button&, void)
{
if (!m_bCheckName || IsValidSbxName(m_xEdit->get_text()))
m_xDialog->response(RET_OK);
else
{
std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_xDialog.get(),
VclMessageType::Warning, VclButtonsType::Ok, IDEResId(RID_STR_BADSBXNAME)));
xErrorBox->run();
m_xEdit->grab_focus();
}
}
NewObjectDialog::NewObjectDialog(weld::Window * pParent, ObjectMode eMode, bool bCheckName)
: GenericDialogController(pParent, "modules/BasicIDE/ui/newlibdialog.ui", "NewLibDialog")
, m_xEdit(m_xBuilder->weld_entry("entry"))
, m_xOKButton(m_xBuilder->weld_button("ok"))
, m_bCheckName(bCheckName)
{
switch (eMode)
{
case ObjectMode::Library:
m_xDialog->set_title(IDEResId(RID_STR_NEWLIB));
break;
case ObjectMode::Module:
m_xDialog->set_title(IDEResId(RID_STR_NEWMOD));
break;
case ObjectMode::Dialog:
m_xDialog->set_title(IDEResId(RID_STR_NEWDLG));
break;
default:
assert(false);
}
m_xOKButton->connect_clicked(LINK(this, NewObjectDialog, OkButtonHandler));
}
// GotoLineDialog
GotoLineDialog::GotoLineDialog(weld::Window* pParent )
: GenericDialogController(pParent, "modules/BasicIDE/ui/gotolinedialog.ui", "GotoLineDialog")
, m_xEdit(m_xBuilder->weld_entry("entry"))
, m_xOKButton(m_xBuilder->weld_button("ok"))
{
m_xEdit->grab_focus();
m_xOKButton->connect_clicked(LINK(this, GotoLineDialog, OkButtonHandler));
}
GotoLineDialog::~GotoLineDialog()
{
}
sal_Int32 GotoLineDialog::GetLineNumber() const
{
return m_xEdit->get_text().toInt32();
}
IMPL_LINK_NOARG(GotoLineDialog, OkButtonHandler, weld::Button&, void)
{
if (GetLineNumber())
m_xDialog->response(RET_OK);
else
m_xEdit->select_region(0, -1);
}
// ExportDialog
IMPL_LINK_NOARG(ExportDialog, OkButtonHandler, weld::Button&, void)
{
m_bExportAsPackage = m_xExportAsPackageButton->get_active();
m_xDialog->response(RET_OK);
}
ExportDialog::ExportDialog(weld::Window * pParent)
: GenericDialogController(pParent, "modules/BasicIDE/ui/exportdialog.ui", "ExportDialog")
, m_bExportAsPackage(false)
, m_xExportAsPackageButton(m_xBuilder->weld_radio_button("extension"))
, m_xOKButton(m_xBuilder->weld_button("ok"))
{
m_xExportAsPackageButton->set_active(true);
m_xOKButton->connect_clicked(LINK(this, ExportDialog, OkButtonHandler));
}
ExportDialog::~ExportDialog()
{
}
// LibPage
LibPage::LibPage(weld::Container* pParent, OrganizeDialog* pDialog)
: OrganizePage(pParent, "modules/BasicIDE/ui/libpage.ui", "LibPage", pDialog)
, m_xBasicsBox(m_xBuilder->weld_combo_box("location"))
, m_xLibBox(m_xBuilder->weld_tree_view("library"))
, m_xEditButton(m_xBuilder->weld_button("edit"))
, m_xPasswordButton(m_xBuilder->weld_button("password"))
, m_xNewLibButton(m_xBuilder->weld_button("new"))
, m_xInsertLibButton(m_xBuilder->weld_button("import"))
, m_xExportButton(m_xBuilder->weld_button("export"))
, m_xDelButton(m_xBuilder->weld_button("delete"))
, m_aCurDocument(ScriptDocument::getApplicationScriptDocument())
, m_eCurLocation(LIBRARY_LOCATION_UNKNOWN)
{
Size aSize(m_xLibBox->get_approximate_digit_width() * 40,
m_xLibBox->get_height_rows(10));
m_xLibBox->set_size_request(aSize.Width(), aSize.Height());
// tdf#93476 The libraries should be listed alphabetically
m_xLibBox->make_sorted();
m_xEditButton->connect_clicked( LINK( this, LibPage, ButtonHdl ) );
m_xNewLibButton->connect_clicked( LINK( this, LibPage, ButtonHdl ) );
m_xPasswordButton->connect_clicked( LINK( this, LibPage, ButtonHdl ) );
m_xExportButton->connect_clicked( LINK( this, LibPage, ButtonHdl ) );
m_xInsertLibButton->connect_clicked( LINK( this, LibPage, ButtonHdl ) );
m_xDelButton->connect_clicked( LINK( this, LibPage, ButtonHdl ) );
m_xLibBox->connect_changed( LINK( this, LibPage, TreeListHighlightHdl ) );
m_xBasicsBox->connect_changed( LINK( this, LibPage, BasicSelectHdl ) );
m_xLibBox->connect_editing(LINK(this, LibPage, EditingEntryHdl),
LINK(this, LibPage, EditedEntryHdl));
FillListBox();
m_xBasicsBox->set_active(0);
SetCurLib();
CheckButtons();
}
IMPL_LINK(LibPage, EditingEntryHdl, const weld::TreeIter&, rIter, bool)
{
// check, if Standard library
OUString aLibName = m_xLibBox->get_text(rIter, 0);
if ( aLibName.equalsIgnoreAsciiCase( "Standard" ) )
{
std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_pDialog->getDialog(),
VclMessageType::Warning, VclButtonsType::Ok, IDEResId(RID_STR_CANNOTCHANGENAMESTDLIB)));
xErrorBox->run();
return false;
}
// check, if library is readonly
Reference< script::XLibraryContainer2 > xModLibContainer( m_aCurDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
Reference< script::XLibraryContainer2 > xDlgLibContainer( m_aCurDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
if ( ( xModLibContainer.is() && xModLibContainer->hasByName( aLibName ) && xModLibContainer->isLibraryReadOnly( aLibName ) && !xModLibContainer->isLibraryLink( aLibName ) ) ||
( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aLibName ) && xDlgLibContainer->isLibraryReadOnly( aLibName ) && !xDlgLibContainer->isLibraryLink( aLibName ) ) )
{
std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_pDialog->getDialog(),
VclMessageType::Warning, VclButtonsType::Ok, IDEResId(RID_STR_LIBISREADONLY)));
xErrorBox->run();
return false;
}
// i24094: Password verification necessary for renaming
if ( xModLibContainer.is() && xModLibContainer->hasByName( aLibName ) && !xModLibContainer->isLibraryLoaded( aLibName ) )
{
bool bOK = true;
// check password
Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aLibName ) && !xPasswd->isLibraryPasswordVerified( aLibName ) )
{
OUString aPassword;
bOK = QueryPassword(m_pDialog->getDialog(), xModLibContainer, aLibName, aPassword);
}
if ( !bOK )
return false;
}
// TODO: check if library is reference/link
return true;
}
IMPL_LINK(LibPage, EditedEntryHdl, const IterString&, rIterString, bool)
{
const weld::TreeIter& rIter = rIterString.first;
OUString sNewName = rIterString.second;
bool bValid = sNewName.getLength() <= 30 && IsValidSbxName(sNewName);
OUString aOldName(m_xLibBox->get_text(rIter, 0));
if (bValid && aOldName != sNewName)
{
try
{
Reference< script::XLibraryContainer2 > xModLibContainer( m_aCurDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
if ( xModLibContainer.is() )
xModLibContainer->renameLibrary( aOldName, sNewName );
Reference< script::XLibraryContainer2 > xDlgLibContainer( m_aCurDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
if ( xDlgLibContainer.is() )
xDlgLibContainer->renameLibrary( aOldName, sNewName );
MarkDocumentModified( m_aCurDocument );
if (SfxBindings* pBindings = GetBindingsPtr())
{
pBindings->Invalidate( SID_BASICIDE_LIBSELECTOR );
pBindings->Update( SID_BASICIDE_LIBSELECTOR );
}
}
catch (const container::ElementExistException& )
{
std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_pDialog->getDialog(),
VclMessageType::Warning, VclButtonsType::Ok, IDEResId(RID_STR_SBXNAMEALLREADYUSED)));
xErrorBox->run();
return false;
}
catch (const container::NoSuchElementException& )
{
DBG_UNHANDLED_EXCEPTION("basctl.basicide");
return false;
}
}
if ( !bValid )
{
OUString sWarning(sNewName.getLength() > 30 ? IDEResId(RID_STR_LIBNAMETOLONG) : IDEResId(RID_STR_BADSBXNAME));
std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_pDialog->getDialog(),
VclMessageType::Warning, VclButtonsType::Ok, sWarning));
xErrorBox->run();
}
return bValid;
}
LibPage::~LibPage()
{
if (m_xBasicsBox)
{
const sal_Int32 nCount = m_xBasicsBox->get_count();
for (sal_Int32 i = 0; i < nCount; ++i)
{
DocumentEntry* pEntry = weld::fromId<DocumentEntry*>(m_xBasicsBox->get_id(i));
delete pEntry;
}
}
}
void LibPage::CheckButtons()
{
std::unique_ptr<weld::TreeIter> xCur(m_xLibBox->make_iterator());
if (!m_xLibBox->get_cursor(xCur.get()))
return;
OUString aLibName = m_xLibBox->get_text(*xCur, 0);
Reference< script::XLibraryContainer2 > xModLibContainer( m_aCurDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
Reference< script::XLibraryContainer2 > xDlgLibContainer( m_aCurDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
if ( m_eCurLocation == LIBRARY_LOCATION_SHARE )
{
m_xPasswordButton->set_sensitive(false);
m_xNewLibButton->set_sensitive(false);
m_xInsertLibButton->set_sensitive(false);
m_xDelButton->set_sensitive(false);
}
else if ( aLibName.equalsIgnoreAsciiCase( "Standard" ) )
{
m_xPasswordButton->set_sensitive(false);
m_xNewLibButton->set_sensitive(true);
m_xInsertLibButton->set_sensitive(true);
m_xExportButton->set_sensitive(false);
m_xDelButton->set_sensitive(false);
}
else if ( ( xModLibContainer.is() && xModLibContainer->hasByName( aLibName ) && xModLibContainer->isLibraryReadOnly( aLibName ) ) ||
( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aLibName ) && xDlgLibContainer->isLibraryReadOnly( aLibName ) ) )
{
m_xPasswordButton->set_sensitive(false);
m_xNewLibButton->set_sensitive(true);
m_xInsertLibButton->set_sensitive(true);
if ( ( xModLibContainer.is() && xModLibContainer->hasByName( aLibName ) && xModLibContainer->isLibraryReadOnly( aLibName ) && !xModLibContainer->isLibraryLink( aLibName ) ) ||
( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aLibName ) && xDlgLibContainer->isLibraryReadOnly( aLibName ) && !xDlgLibContainer->isLibraryLink( aLibName ) ) )
m_xDelButton->set_sensitive(false);
else
m_xDelButton->set_sensitive(true);
}
else
{
if ( xModLibContainer.is() && !xModLibContainer->hasByName( aLibName ) )
m_xPasswordButton->set_sensitive(false);
else
m_xPasswordButton->set_sensitive(true);
m_xNewLibButton->set_sensitive(true);
m_xInsertLibButton->set_sensitive(true);
m_xExportButton->set_sensitive(true);
m_xDelButton->set_sensitive(true);
}
}
void LibPage::ActivatePage()
{
SetCurLib();
}
IMPL_LINK_NOARG(LibPage, TreeListHighlightHdl, weld::TreeView&, void)
{
CheckButtons();
}
IMPL_LINK_NOARG( LibPage, BasicSelectHdl, weld::ComboBox&, void )
{
SetCurLib();
CheckButtons();
}
IMPL_LINK( LibPage, ButtonHdl, weld::Button&, rButton, void )
{
if (&rButton == m_xEditButton.get())
{
SfxAllItemSet aArgs( SfxGetpApp()->GetPool() );
SfxRequest aRequest( SID_BASICIDE_APPEAR, SfxCallMode::SYNCHRON, aArgs );
SfxGetpApp()->ExecuteSlot( aRequest );
SfxUnoAnyItem aDocItem( SID_BASICIDE_ARG_DOCUMENT_MODEL, Any( m_aCurDocument.getDocumentOrNull() ) );
std::unique_ptr<weld::TreeIter> xCurEntry(m_xLibBox->make_iterator());
if (!m_xLibBox->get_cursor(xCurEntry.get()))
return;
OUString aLibName(m_xLibBox->get_text(*xCurEntry, 0));
SfxStringItem aLibNameItem( SID_BASICIDE_ARG_LIBNAME, aLibName );
if (SfxDispatcher* pDispatcher = GetDispatcher())
pDispatcher->ExecuteList( SID_BASICIDE_LIBSELECTED,
SfxCallMode::ASYNCHRON, { &aDocItem, &aLibNameItem });
EndTabDialog();
return;
}
else if (&rButton == m_xNewLibButton.get())
NewLib();
else if (&rButton == m_xInsertLibButton.get())
InsertLib();
else if (&rButton == m_xExportButton.get())
Export();
else if (&rButton == m_xDelButton.get())
DeleteCurrent();
else if (&rButton == m_xPasswordButton.get())
{
std::unique_ptr<weld::TreeIter> xCurEntry(m_xLibBox->make_iterator());
if (!m_xLibBox->get_cursor(xCurEntry.get()))
return;
OUString aLibName(m_xLibBox->get_text(*xCurEntry, 0));
// load module library (if not loaded)
Reference< script::XLibraryContainer > xModLibContainer = m_aCurDocument.getLibraryContainer( E_SCRIPTS );
if ( xModLibContainer.is() && xModLibContainer->hasByName( aLibName ) && !xModLibContainer->isLibraryLoaded( aLibName ) )
{
Shell* pShell = GetShell();
if (pShell)
pShell->GetViewFrame()->GetWindow().EnterWait();
xModLibContainer->loadLibrary( aLibName );
if (pShell)
pShell->GetViewFrame()->GetWindow().LeaveWait();
}
// load dialog library (if not loaded)
Reference< script::XLibraryContainer > xDlgLibContainer = m_aCurDocument.getLibraryContainer( E_DIALOGS );
if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aLibName ) && !xDlgLibContainer->isLibraryLoaded( aLibName ) )
{
Shell* pShell = GetShell();
if (pShell)
pShell->GetViewFrame()->GetWindow().EnterWait();
xDlgLibContainer->loadLibrary( aLibName );
if (pShell)
pShell->GetViewFrame()->GetWindow().LeaveWait();
}
// check, if library is password protected
if ( xModLibContainer.is() && xModLibContainer->hasByName( aLibName ) )
{
Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
if ( xPasswd.is() )
{
bool const bProtected = xPasswd->isLibraryPasswordProtected( aLibName );
// change password dialog
SvxPasswordDialog aDlg(m_pDialog->getDialog(), !bProtected);
aDlg.SetCheckPasswordHdl(LINK(this, LibPage, CheckPasswordHdl));
if (aDlg.run() == RET_OK)
{
bool const bNewProtected = xPasswd->isLibraryPasswordProtected( aLibName );
if ( bNewProtected != bProtected )
{
int nPos = m_xLibBox->get_iter_index_in_parent(*xCurEntry);
m_xLibBox->remove(*xCurEntry);
ImpInsertLibEntry(aLibName, nPos);
m_xLibBox->set_cursor(nPos);
}
MarkDocumentModified( m_aCurDocument );
}
}
}
}
CheckButtons();
}
IMPL_LINK( LibPage, CheckPasswordHdl, SvxPasswordDialog *, pDlg, bool )
{
bool bRet = false;
std::unique_ptr<weld::TreeIter> xCurEntry(m_xLibBox->make_iterator());
if (!m_xLibBox->get_cursor(xCurEntry.get()))
return bRet;
OUString aLibName(m_xLibBox->get_text(*xCurEntry, 0));
Reference< script::XLibraryContainerPassword > xPasswd( m_aCurDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
if ( xPasswd.is() )
{
try
{
OUString aOldPassword( pDlg->GetOldPassword() );
OUString aNewPassword( pDlg->GetNewPassword() );
xPasswd->changeLibraryPassword( aLibName, aOldPassword, aNewPassword );
bRet = true;
}
catch (...)
{
}
}
return bRet;
}
void LibPage::NewLib()
{
createLibImpl(m_pDialog->getDialog(), m_aCurDocument, m_xLibBox.get(), nullptr);
}
void LibPage::InsertLib()
{
Reference< uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
// file open dialog
sfx2::FileDialogHelper aDlg(ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE, FileDialogFlags::NONE, m_pDialog->getDialog());
aDlg.SetContext(sfx2::FileDialogHelper::BasicInsertLib);
const Reference <XFilePicker3>& xFP = aDlg.GetFilePicker();
xFP->setTitle(IDEResId(RID_STR_APPENDLIBS));
// filter
OUString aTitle(IDEResId(RID_STR_BASIC));
xFP->appendFilter( aTitle, "*.sbl;*.xlc;*.xlb" // library files
";*.sdw;*.sxw;*.odt" // text
";*.vor;*.stw;*.ott" // text template
";*.sgl;*.sxg;*.odm" // master document
";*.oth" // html document template
";*.sdc;*.sxc;*.ods" // spreadsheet
";*.stc;*.ots" // spreadsheet template
";*.sda;*.sxd;*.odg" // drawing
";*.std;*.otg" // drawing template
";*.sdd;*.sxi;*.odp" // presentation
";*.sti;*.otp" // presentation template
";*.sxm;*.odf" ); // formula
OUString aLastFilter(GetExtraData()->GetAddLibFilter());
if ( !aLastFilter.isEmpty() )
xFP->setCurrentFilter( aLastFilter );
else
xFP->setCurrentFilter( IDEResId(RID_STR_BASIC) );
if ( xFP->execute() != RET_OK )
return;
GetExtraData()->SetAddLibPath( xFP->getDisplayDirectory() );
GetExtraData()->SetAddLibFilter( xFP->getCurrentFilter() );
// library containers for import
Reference< script::XLibraryContainer2 > xModLibContImport;
Reference< script::XLibraryContainer2 > xDlgLibContImport;
// file URLs
Sequence< OUString > aFiles = xFP->getSelectedFiles();
INetURLObject aURLObj( aFiles[0] );
auto xModURLObj = std::make_shared<INetURLObject>(aURLObj);
auto xDlgURLObj = std::make_shared<INetURLObject>(aURLObj);
OUString aBase = aURLObj.getBase();
OUString aModBase( "script" );
OUString aDlgBase( "dialog" );
if ( aBase == aModBase || aBase == aDlgBase )
{
xModURLObj->setBase( aModBase );
xDlgURLObj->setBase( aDlgBase );
}
Reference< XSimpleFileAccess3 > xSFA( SimpleFileAccess::create(comphelper::getProcessComponentContext()) );
OUString aModURL( xModURLObj->GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
if ( xSFA->exists( aModURL ) )
{
xModLibContImport = script::DocumentScriptLibraryContainer::createWithURL(xContext, aModURL);
}
OUString aDlgURL( xDlgURLObj->GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
if ( xSFA->exists( aDlgURL ) )
{
xDlgLibContImport = script::DocumentDialogLibraryContainer::createWithURL(xContext, aDlgURL);
}
if ( !xModLibContImport.is() && !xDlgLibContImport.is() )
return;
std::shared_ptr<LibDialog> xLibDlg;
Sequence< OUString > aLibNames = GetMergedLibraryNames( xModLibContImport, xDlgLibContImport );
sal_Int32 nLibCount = aLibNames.getLength();
if (nLibCount)
{
// library import dialog
xLibDlg = std::make_shared<LibDialog>(m_pDialog->getDialog());
xLibDlg->SetStorageName(aURLObj.getName());
weld::TreeView& rView = xLibDlg->GetLibBox();
rView.make_unsorted();
rView.freeze();
const OUString* pLibNames = aLibNames.getConstArray();
for (sal_Int32 i = 0 ; i < nLibCount; ++i)
{
// libbox entries
OUString aLibName( pLibNames[ i ] );
if ( !( ( xModLibContImport.is() && xModLibContImport->hasByName( aLibName ) && xModLibContImport->isLibraryLink( aLibName ) ) ||
( xDlgLibContImport.is() && xDlgLibContImport->hasByName( aLibName ) && xDlgLibContImport->isLibraryLink( aLibName ) ) ) )
{
rView.append();
const int nRow = rView.n_children() - 1;
rView.set_toggle(nRow, TRISTATE_TRUE);
rView.set_text(nRow, aLibName, 0);
}
}
rView.thaw();
rView.make_sorted();
if (rView.n_children())
rView.set_cursor(0);
}
if (!xLibDlg)
{
std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_pDialog->getDialog(),
VclMessageType::Warning, VclButtonsType::Ok, IDEResId(RID_STR_NOLIBINSTORAGE)));
xErrorBox->run();
return;
}
OUString aExtension( aURLObj.getExtension() );
OUString aLibExtension( "xlb" );
OUString aContExtension( "xlc" );
// disable reference checkbox for documents and sbls
if ( aExtension != aLibExtension && aExtension != aContExtension )
xLibDlg->EnableReference(false);
weld::DialogController::runAsync(xLibDlg, [aContExtension, xDlgURLObj, aExtension, aLibExtension, xModURLObj, xLibDlg, xDlgLibContImport, xModLibContImport, this](sal_Int32 nResult)
{
if (!nResult )
return;
bool bChanges = false;
bool bRemove = false;
bool bReplace = xLibDlg->IsReplace();
bool bReference = xLibDlg->IsReference();
weld::TreeView& rView = xLibDlg->GetLibBox();
for (int nLib = 0, nChildren = rView.n_children(); nLib < nChildren; ++nLib)
{
if (rView.get_toggle(nLib) == TRISTATE_TRUE)
{
OUString aLibName(rView.get_text(nLib));
Reference< script::XLibraryContainer2 > xModLibContainer( m_aCurDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
Reference< script::XLibraryContainer2 > xDlgLibContainer( m_aCurDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
// check, if the library is already existing
if ( ( xModLibContainer.is() && xModLibContainer->hasByName( aLibName ) ) ||
( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aLibName ) ) )
{
if ( bReplace )
{
// check, if the library is the Standard library
if ( aLibName == "Standard" )
{
std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_pDialog->getDialog(),
VclMessageType::Warning, VclButtonsType::Ok, IDEResId(RID_STR_REPLACESTDLIB)));
xErrorBox->run();
continue;
}
// check, if the library is readonly and not a link
if ( ( xModLibContainer.is() && xModLibContainer->hasByName( aLibName ) && xModLibContainer->isLibraryReadOnly( aLibName ) && !xModLibContainer->isLibraryLink( aLibName ) ) ||
( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aLibName ) && xDlgLibContainer->isLibraryReadOnly( aLibName ) && !xDlgLibContainer->isLibraryLink( aLibName ) ) )
{
OUString aErrStr( IDEResId(RID_STR_REPLACELIB) );
aErrStr = aErrStr.replaceAll("XX", aLibName) + "\n" + IDEResId(RID_STR_LIBISREADONLY);
std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_pDialog->getDialog(),
VclMessageType::Warning, VclButtonsType::Ok, aErrStr));
xErrorBox->run();
continue;
}
// remove existing libraries
bRemove = true;
}
else
{
OUString aErrStr;
if ( bReference )
aErrStr = IDEResId(RID_STR_REFNOTPOSSIBLE);
else
aErrStr = IDEResId(RID_STR_IMPORTNOTPOSSIBLE);
aErrStr = aErrStr.replaceAll("XX", aLibName) + "\n" +IDEResId(RID_STR_SBXNAMEALLREADYUSED);
std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_pDialog->getDialog(),
VclMessageType::Warning, VclButtonsType::Ok, aErrStr));
xErrorBox->run();
continue;
}
}
// check, if the library is password protected
bool bOK = false;
OUString aPassword;
if ( xModLibContImport.is() && xModLibContImport->hasByName( aLibName ) )
{
Reference< script::XLibraryContainerPassword > xPasswd( xModLibContImport, UNO_QUERY );
if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aLibName ) && !xPasswd->isLibraryPasswordVerified( aLibName ) && !bReference )
{
bOK = QueryPassword(m_pDialog->getDialog(), xModLibContImport, aLibName, aPassword, true, true);
if ( !bOK )
{
OUString aErrStr( IDEResId(RID_STR_NOIMPORT) );
aErrStr = aErrStr.replaceAll("XX", aLibName);
std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_pDialog->getDialog(),
VclMessageType::Warning, VclButtonsType::Ok, aErrStr));
xErrorBox->run();
continue;
}
}
}
// remove existing libraries
if ( bRemove )
{
// remove listbox entry
int nEntry_ = FindEntry(*m_xLibBox, aLibName);
if (nEntry_ != -1)
m_xLibBox->remove(nEntry_);
// remove module library
if ( xModLibContainer.is() && xModLibContainer->hasByName( aLibName ) )
xModLibContainer->removeLibrary( aLibName );
// remove dialog library
if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aLibName ) )
xDlgLibContainer->removeLibrary( aLibName );
}
// copy module library
if ( xModLibContImport.is() && xModLibContImport->hasByName( aLibName ) && xModLibContainer.is() && !xModLibContainer->hasByName( aLibName ) )
{
Reference< container::XNameContainer > xModLib;
if ( bReference )
{
// storage URL
INetURLObject aModStorageURLObj(*xModURLObj);
if ( aExtension == aContExtension )
{
sal_Int32 nCount = aModStorageURLObj.getSegmentCount();
aModStorageURLObj.insertName( aLibName, false, nCount-1 );
aModStorageURLObj.setExtension( aLibExtension );
aModStorageURLObj.setFinalSlash();
}
OUString aModStorageURL( aModStorageURLObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
// create library link
xModLib.set( xModLibContainer->createLibraryLink( aLibName, aModStorageURL, true ), UNO_QUERY);
}
else
{
// create library
xModLib = xModLibContainer->createLibrary( aLibName );
if ( xModLib.is() )
{
// get import library
Reference< container::XNameContainer > xModLibImport;
Any aElement = xModLibContImport->getByName( aLibName );
aElement >>= xModLibImport;
if ( xModLibImport.is() )
{
// load library
if ( !xModLibContImport->isLibraryLoaded( aLibName ) )
xModLibContImport->loadLibrary( aLibName );
// copy all modules
Sequence< OUString > aModNames = xModLibImport->getElementNames();
sal_Int32 nModCount = aModNames.getLength();
const OUString* pModNames = aModNames.getConstArray();
for ( sal_Int32 i = 0 ; i < nModCount ; i++ )
{
OUString aModName( pModNames[ i ] );
Any aElement_ = xModLibImport->getByName( aModName );
xModLib->insertByName( aModName, aElement_ );
}
// set password
if ( bOK )
{
Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
if ( xPasswd.is() )
{
try
{
xPasswd->changeLibraryPassword( aLibName, OUString(), aPassword );
}
catch (...)
{
}
}
}
}
}
}
}
// copy dialog library
if ( xDlgLibContImport.is() && xDlgLibContImport->hasByName( aLibName ) && xDlgLibContainer.is() && !xDlgLibContainer->hasByName( aLibName ) )
{
Reference< container::XNameContainer > xDlgLib;
if ( bReference )
{
// storage URL
INetURLObject aDlgStorageURLObj( *xDlgURLObj );
if ( aExtension == aContExtension )
{
sal_Int32 nCount = aDlgStorageURLObj.getSegmentCount();
aDlgStorageURLObj.insertName( aLibName, false, nCount - 1 );
aDlgStorageURLObj.setExtension( aLibExtension );
aDlgStorageURLObj.setFinalSlash();
}
OUString aDlgStorageURL( aDlgStorageURLObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
// create library link
xDlgLib.set( xDlgLibContainer->createLibraryLink( aLibName, aDlgStorageURL, true ), UNO_QUERY);
}
else
{
// create library
xDlgLib = xDlgLibContainer->createLibrary( aLibName );
if ( xDlgLib.is() )
{
// get import library
Reference< container::XNameContainer > xDlgLibImport;
Any aElement = xDlgLibContImport->getByName( aLibName );
aElement >>= xDlgLibImport;
if ( xDlgLibImport.is() )
{
// load library
if ( !xDlgLibContImport->isLibraryLoaded( aLibName ) )
xDlgLibContImport->loadLibrary( aLibName );
// copy all dialogs
Sequence< OUString > aDlgNames = xDlgLibImport->getElementNames();
sal_Int32 nDlgCount = aDlgNames.getLength();
const OUString* pDlgNames = aDlgNames.getConstArray();
for ( sal_Int32 i = 0 ; i < nDlgCount ; i++ )
{
OUString aDlgName( pDlgNames[ i ] );
Any aElement_ = xDlgLibImport->getByName( aDlgName );
xDlgLib->insertByName( aDlgName, aElement_ );
}
}
}
}
}
// insert listbox entry
ImpInsertLibEntry( aLibName, m_xLibBox->n_children() );
m_xLibBox->set_cursor( m_xLibBox->find_text(aLibName) );
bChanges = true;
}
}
if ( bChanges )
MarkDocumentModified( m_aCurDocument );
});
}
void LibPage::Export()
{
std::unique_ptr<weld::TreeIter> xCurEntry(m_xLibBox->make_iterator());
if (!m_xLibBox->get_cursor(xCurEntry.get()))
return;
OUString aLibName(m_xLibBox->get_text(*xCurEntry, 0));
// Password verification
Reference< script::XLibraryContainer2 > xModLibContainer( m_aCurDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
if ( xModLibContainer.is() && xModLibContainer->hasByName( aLibName ) && !xModLibContainer->isLibraryLoaded( aLibName ) )
{
bool bOK = true;
// check password
Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aLibName ) && !xPasswd->isLibraryPasswordVerified( aLibName ) )
{
OUString aPassword;
bOK = QueryPassword(m_pDialog->getDialog(), xModLibContainer, aLibName, aPassword);
}
if ( !bOK )
return;
}
std::unique_ptr<ExportDialog> xNewDlg(new ExportDialog(m_pDialog->getDialog()));
if (xNewDlg->run() != RET_OK)
return;
try
{
bool bExportAsPackage = xNewDlg->isExportAsPackage();
//tdf#112063 ensure closing xNewDlg is not selected as
//parent of file dialog from ExportAs...
xNewDlg.reset();
if (bExportAsPackage)
ExportAsPackage( aLibName );
else
ExportAsBasic( aLibName );
}
catch(const util::VetoException& ) // user canceled operation
{
}
}
void LibPage::implExportLib( const OUString& aLibName, const OUString& aTargetURL,
const Reference< task::XInteractionHandler >& Handler )
{
Reference< script::XLibraryContainerExport > xModLibContainerExport
( m_aCurDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
Reference< script::XLibraryContainerExport > xDlgLibContainerExport
( m_aCurDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
if ( xModLibContainerExport.is() )
xModLibContainerExport->exportLibrary( aLibName, aTargetURL, Handler );
if (!xDlgLibContainerExport.is())
return;
Reference<container::XNameAccess> xNameAcc(xDlgLibContainerExport, UNO_QUERY);
if (!xNameAcc.is())
return;
if (!xNameAcc->hasByName(aLibName))
return;
xDlgLibContainerExport->exportLibrary(aLibName, aTargetURL, Handler);
}
// Implementation XCommandEnvironment
namespace {
class OLibCommandEnvironment : public cppu::WeakImplHelper< XCommandEnvironment >
{
Reference< task::XInteractionHandler > mxInteraction;
public:
explicit OLibCommandEnvironment(const Reference<task::XInteractionHandler>& xInteraction)
: mxInteraction( xInteraction )
{}
// Methods
virtual Reference< task::XInteractionHandler > SAL_CALL getInteractionHandler() override;
virtual Reference< XProgressHandler > SAL_CALL getProgressHandler() override;
};
}
Reference< task::XInteractionHandler > OLibCommandEnvironment::getInteractionHandler()
{
return mxInteraction;
}
Reference< XProgressHandler > OLibCommandEnvironment::getProgressHandler()
{
Reference< XProgressHandler > xRet;
return xRet;
}
void LibPage::ExportAsPackage( const OUString& aLibName )
{
// file open dialog
sfx2::FileDialogHelper aDlg(ui::dialogs::TemplateDescription::FILESAVE_SIMPLE, FileDialogFlags::NONE, m_pDialog->getDialog());
aDlg.SetContext(sfx2::FileDialogHelper::BasicExportPackage);
const Reference <XFilePicker3>& xFP = aDlg.GetFilePicker();
Reference< uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
Reference< task::XInteractionHandler2 > xHandler( task::InteractionHandler::createWithParent(xContext, nullptr) );
Reference< XSimpleFileAccess3 > xSFA = SimpleFileAccess::create(xContext);
xFP->setTitle(IDEResId(RID_STR_EXPORTPACKAGE));
// filter
OUString aTitle(IDEResId(RID_STR_PACKAGE_BUNDLE));
xFP->appendFilter( aTitle, "*.oxt" ); // library files
xFP->setCurrentFilter( aTitle );
if ( xFP->execute() != RET_OK )
return;
GetExtraData()->SetAddLibPath(xFP->getDisplayDirectory());
Sequence< OUString > aFiles = xFP->getSelectedFiles();
INetURLObject aURL( aFiles[0] );
if( aURL.getExtension().isEmpty() )
aURL.setExtension( u"oxt" );
OUString aPackageURL( aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
OUString aTmpPath = SvtPathOptions().GetTempPath();
INetURLObject aInetObj( aTmpPath );
aInetObj.insertName( aLibName, true, INetURLObject::LAST_SEGMENT, INetURLObject::EncodeMechanism::All );
OUString aSourcePath = aInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
if( xSFA->exists( aSourcePath ) )
xSFA->kill( aSourcePath );
Reference< task::XInteractionHandler > xDummyHandler( new DummyInteractionHandler( xHandler ) );
implExportLib( aLibName, aTmpPath, xDummyHandler );
Reference< XCommandEnvironment > xCmdEnv = new OLibCommandEnvironment(xHandler);
::ucbhelper::Content sourceContent( aSourcePath, xCmdEnv, comphelper::getProcessComponentContext() );
OUString destFolder = "vnd.sun.star.zip://" +
::rtl::Uri::encode( aPackageURL,
rtl_UriCharClassRegName,
rtl_UriEncodeIgnoreEscapes,
RTL_TEXTENCODING_UTF8 ) +
"/";
if( xSFA->exists( aPackageURL ) )
xSFA->kill( aPackageURL );
::ucbhelper::Content destFolderContent( destFolder, xCmdEnv, comphelper::getProcessComponentContext() );
destFolderContent.transferContent(
sourceContent, ::ucbhelper::InsertOperation::Copy,
OUString(), NameClash::OVERWRITE );
INetURLObject aMetaInfInetObj( aTmpPath );
aMetaInfInetObj.insertName( u"META-INF",
true, INetURLObject::LAST_SEGMENT, INetURLObject::EncodeMechanism::All );
OUString aMetaInfFolder = aMetaInfInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE );
if( xSFA->exists( aMetaInfFolder ) )
xSFA->kill( aMetaInfFolder );
xSFA->createFolder( aMetaInfFolder );
std::vector< Sequence<beans::PropertyValue> > manifest;
OUString fullPath = aLibName
+ "/" ;
auto attribs(::comphelper::InitPropertySequence({
{ "FullPath", Any(fullPath) },
{ "MediaType", Any(OUString("application/vnd.sun.star.basic-library")) }
}));
manifest.push_back( attribs );
// write into pipe:
Reference<packages::manifest::XManifestWriter> xManifestWriter = packages::manifest::ManifestWriter::create( xContext );
Reference<io::XOutputStream> xPipe( io::Pipe::create( xContext ), UNO_QUERY_THROW );
xManifestWriter->writeManifestSequence(
xPipe, Sequence< Sequence<beans::PropertyValue> >(
manifest.data(), manifest.size() ) );
aMetaInfInetObj.insertName( u"manifest.xml",
true, INetURLObject::LAST_SEGMENT, INetURLObject::EncodeMechanism::All );
// write buffered pipe data to content:
::ucbhelper::Content manifestContent( aMetaInfInetObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), xCmdEnv, comphelper::getProcessComponentContext() );
manifestContent.writeStream( Reference<io::XInputStream>( xPipe, UNO_QUERY_THROW ), true );
::ucbhelper::Content MetaInfContent( aMetaInfFolder, xCmdEnv, comphelper::getProcessComponentContext() );
destFolderContent.transferContent(
MetaInfContent, ::ucbhelper::InsertOperation::Copy,
OUString(), NameClash::OVERWRITE );
if( xSFA->exists( aSourcePath ) )
xSFA->kill( aSourcePath );
if( xSFA->exists( aMetaInfFolder ) )
xSFA->kill( aMetaInfFolder );
}
void LibPage::ExportAsBasic( const OUString& aLibName )
{
// Folder picker
Reference< uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
Reference< XFolderPicker2 > xFolderPicker = sfx2::createFolderPicker(xContext, m_pDialog->getDialog());
Reference< task::XInteractionHandler2 > xHandler( task::InteractionHandler::createWithParent(xContext, nullptr) );
xFolderPicker->setTitle(IDEResId(RID_STR_EXPORTBASIC));
// set display directory and filter
OUString aPath =GetExtraData()->GetAddLibPath();
if( aPath.isEmpty() )
aPath = SvtPathOptions().GetWorkPath();
// INetURLObject aURL(m_sSavePath, INetProtocol::File);
xFolderPicker->setDisplayDirectory( aPath );
short nRet = xFolderPicker->execute();
if( nRet == RET_OK )
{
OUString aTargetURL = xFolderPicker->getDirectory();
GetExtraData()->SetAddLibPath(aTargetURL);
Reference< task::XInteractionHandler > xDummyHandler( new DummyInteractionHandler( xHandler ) );
implExportLib( aLibName, aTargetURL, xDummyHandler );
}
}
void LibPage::DeleteCurrent()
{
std::unique_ptr<weld::TreeIter> xCurEntry(m_xLibBox->make_iterator());
if (!m_xLibBox->get_cursor(xCurEntry.get()))
return;
OUString aLibName(m_xLibBox->get_text(*xCurEntry, 0));
// check, if library is link
bool bIsLibraryLink = false;
Reference< script::XLibraryContainer2 > xModLibContainer( m_aCurDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
Reference< script::XLibraryContainer2 > xDlgLibContainer( m_aCurDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
if ( ( xModLibContainer.is() && xModLibContainer->hasByName( aLibName ) && xModLibContainer->isLibraryLink( aLibName ) ) ||
( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aLibName ) && xDlgLibContainer->isLibraryLink( aLibName ) ) )
{
bIsLibraryLink = true;
}
if (!QueryDelLib(aLibName, bIsLibraryLink, m_pDialog->getDialog()))
return;
// inform BasicIDE
SfxUnoAnyItem aDocItem( SID_BASICIDE_ARG_DOCUMENT_MODEL, Any( m_aCurDocument.getDocumentOrNull() ) );
SfxStringItem aLibNameItem( SID_BASICIDE_ARG_LIBNAME, aLibName );
if (SfxDispatcher* pDispatcher = GetDispatcher())
pDispatcher->ExecuteList(SID_BASICIDE_LIBREMOVED,
SfxCallMode::SYNCHRON, { &aDocItem, &aLibNameItem });
// remove library from module and dialog library containers
if ( xModLibContainer.is() && xModLibContainer->hasByName( aLibName ) )
xModLibContainer->removeLibrary( aLibName );
if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aLibName ) )
xDlgLibContainer->removeLibrary( aLibName );
m_xLibBox->remove(*xCurEntry);
MarkDocumentModified( m_aCurDocument );
}
void LibPage::EndTabDialog()
{
m_pDialog->response(RET_OK);
}
void LibPage::FillListBox()
{
InsertListBoxEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_USER );
InsertListBoxEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_SHARE );
ScriptDocuments aDocuments( ScriptDocument::getAllScriptDocuments( ScriptDocument::DocumentsSorted ) );
for (auto const& doc : aDocuments)
{
InsertListBoxEntry( doc, LIBRARY_LOCATION_DOCUMENT );
}
}
void LibPage::InsertListBoxEntry( const ScriptDocument& rDocument, LibraryLocation eLocation )
{
OUString aEntryText(rDocument.getTitle(eLocation));
OUString sId(weld::toId(new DocumentEntry(rDocument, eLocation)));
m_xBasicsBox->append(sId, aEntryText);
}
void LibPage::SetCurLib()
{
DocumentEntry* pEntry = weld::fromId<DocumentEntry*>(m_xBasicsBox->get_active_id());
if (!pEntry)
return;
const ScriptDocument& aDocument( pEntry->GetDocument() );
DBG_ASSERT( aDocument.isAlive(), "LibPage::SetCurLib: no document, or document is dead!" );
if ( !aDocument.isAlive() )
return;
LibraryLocation eLocation = pEntry->GetLocation();
if ( aDocument == m_aCurDocument && eLocation == m_eCurLocation )
return;
m_aCurDocument = aDocument;
m_eCurLocation = eLocation;
m_xLibBox->clear();
// get a sorted list of library names
Sequence< OUString > aLibNames = aDocument.getLibraryNames();
sal_Int32 nLibCount = aLibNames.getLength();
const OUString* pLibNames = aLibNames.getConstArray();
int nEntry = 0;
for (int i = 0 ; i < nLibCount; ++i)
{
OUString aLibName(pLibNames[i]);
if (eLocation == aDocument.getLibraryLocation(aLibName))
ImpInsertLibEntry(aLibName, nEntry++);
}
int nEntry_ = FindEntry(*m_xLibBox, u"Standard");
if (nEntry_ == -1 && m_xLibBox->n_children())
nEntry_ = 0;
m_xLibBox->set_cursor(nEntry_);
}
void LibPage::ImpInsertLibEntry( const OUString& rLibName, int nPos )
{
// check, if library is password protected
bool bProtected = false;
Reference< script::XLibraryContainer2 > xModLibContainer( m_aCurDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
if ( xModLibContainer.is() && xModLibContainer->hasByName( rLibName ) )
{
Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
if ( xPasswd.is() )
{
bProtected = xPasswd->isLibraryPasswordProtected( rLibName );
}
}
m_xLibBox->insert_text(nPos, rLibName);
if (bProtected)
m_xLibBox->set_image(nPos, RID_BMP_LOCKED);
// check, if library is link
if ( xModLibContainer.is() && xModLibContainer->hasByName( rLibName ) && xModLibContainer->isLibraryLink( rLibName ) )
{
OUString aLinkURL = xModLibContainer->getLibraryLinkURL( rLibName );
m_xLibBox->set_text(nPos, aLinkURL, 1);
}
}
// Helper function
void createLibImpl(weld::Window* pWin, const ScriptDocument& rDocument,
weld::TreeView* pLibBox, SbTreeListBox* pBasicBox)
{
OSL_ENSURE( rDocument.isAlive(), "createLibImpl: invalid document!" );
if ( !rDocument.isAlive() )
return;
// create library name
OUString aLibName;
bool bValid = false;
sal_Int32 i = 1;
while ( !bValid )
{
aLibName = "Library" + OUString::number( i );
if ( !rDocument.hasLibrary( E_SCRIPTS, aLibName ) && !rDocument.hasLibrary( E_DIALOGS, aLibName ) )
bValid = true;
i++;
}
NewObjectDialog aNewDlg(pWin, ObjectMode::Library);
aNewDlg.SetObjectName(aLibName);
if (!aNewDlg.run())
return;
if (!aNewDlg.GetObjectName().isEmpty())
aLibName = aNewDlg.GetObjectName();
if ( aLibName.getLength() > 30 )
{
std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(pWin,
VclMessageType::Warning, VclButtonsType::Ok, IDEResId(RID_STR_LIBNAMETOLONG)));
xErrorBox->run();
}
else if ( !IsValidSbxName( aLibName ) )
{
std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(pWin,
VclMessageType::Warning, VclButtonsType::Ok, IDEResId(RID_STR_BADSBXNAME)));
xErrorBox->run();
}
else if ( rDocument.hasLibrary( E_SCRIPTS, aLibName ) || rDocument.hasLibrary( E_DIALOGS, aLibName ) )
{
std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(pWin,
VclMessageType::Warning, VclButtonsType::Ok, IDEResId(RID_STR_SBXNAMEALLREADYUSED2)));
xErrorBox->run();
}
else
{
try
{
// create module and dialog library
rDocument.getOrCreateLibrary( E_SCRIPTS, aLibName );
rDocument.getOrCreateLibrary( E_DIALOGS, aLibName );
if( pLibBox )
{
pLibBox->append_text(aLibName);
pLibBox->set_cursor(pLibBox->find_text(aLibName));
}
// create a module
OUString aModName = rDocument.createObjectName( E_SCRIPTS, aLibName );
OUString sModuleCode;
if ( !rDocument.createModule( aLibName, aModName, true, sModuleCode ) )
throw Exception("could not create module " + aModName, nullptr);
SbxItem aSbxItem( SID_BASICIDE_ARG_SBX, rDocument, aLibName, aModName, TYPE_MODULE );
if (SfxDispatcher* pDispatcher = GetDispatcher())
pDispatcher->ExecuteList(SID_BASICIDE_SBXINSERTED,
SfxCallMode::SYNCHRON, { &aSbxItem });
if( pBasicBox )
{
std::unique_ptr<weld::TreeIter> xIter(pBasicBox->make_iterator(nullptr));
bool bValidIter = pBasicBox->get_cursor(xIter.get());
std::unique_ptr<weld::TreeIter> xRootEntry(pBasicBox->make_iterator(xIter.get()));
while (bValidIter)
{
pBasicBox->copy_iterator(*xIter, *xRootEntry);
bValidIter = pBasicBox->iter_parent(*xIter);
}
BrowseMode nMode = pBasicBox->GetMode();
bool bDlgMode = ( nMode & BrowseMode::Dialogs ) && !( nMode & BrowseMode::Modules );
const auto sId = bDlgMode ? OUString(RID_BMP_DLGLIB) : OUString(RID_BMP_MODLIB);
pBasicBox->AddEntry(aLibName, sId, xRootEntry.get(), false, std::make_unique<Entry>(OBJ_TYPE_LIBRARY));
pBasicBox->AddEntry(aModName, RID_BMP_MODULE, xRootEntry.get(), false, std::make_unique<Entry>(OBJ_TYPE_MODULE));
pBasicBox->set_cursor(*xRootEntry);
pBasicBox->select(*xRootEntry);
}
}
catch (const uno::Exception& )
{
DBG_UNHANDLED_EXCEPTION("basctl.basicide");
}
}
}
} // namespace basctl
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|