This repository was archived by the owner on Dec 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.cpp
More file actions
834 lines (717 loc) · 27.2 KB
/
handler.cpp
File metadata and controls
834 lines (717 loc) · 27.2 KB
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
#pragma hdrstop
#include "handler.h"
#include "settings.h"
#include "main.h"
#include "input.h"
#include "addplugin.h"
#include "filetree.h"
#include "message.h"
#import "plugin.hpp" // optional import
//---------------------------------------------------------------------------
#pragma package(smart_init)
TMainForm* FForm;
__fastcall TGUIHandler::TGUIHandler()
{
FForm = MainForm;
FPluginManager = new TPluginManager();
// Misc
FScriptsTreeView = FForm->ScriptsTreeView;
FProfileComboBox = FForm->ProfileComboBox;
PluginsTable = FForm->PluginsTable;
FProfileComboBox->OnChange = this->OnProfileChanged;
PluginsTable->OnCellClick = this->OnCellClick;
FAniLoadingIndicator = FForm->AniLoadingIndicator;
PluginsTable->OnEditingDone = this->OnPluginsTableEditingDone;
ScriptsTreeView->OnDblClick = this->OnTreeViewDblClick;
ScriptsTreeView->OnMouseDown = this->OnTreeViewMouseDown;
FForm->OnKeyDown = this->FormKeyDown;
// Buttons
FAddProfileButton = FForm->AddProfileButton;
FDeleteProfileButton = FForm->DeleteProfileButton;
FSettingsButton = FForm->SettingsButton;
FSavePluginsListButton = FForm->SavePluginsListButton;
FExportToVdfButton = FForm->ExportToVdfButton;
FCloneProfileButton = FForm->CloneProfileButton;
FAddProfileButton->OnClick = this->OnAddProfileButton;
FEditProfileButton = FForm->EditProfileButton;
FEditProfileButton->OnClick = this->OnEditProfileButton;
FDeleteProfileButton->OnClick = this->OnDeleteProfile;
FSettingsButton->OnClick = this->OnShowSettingsForm;
FSavePluginsListButton->OnClick = this->OnSavePluginsListButton;
FExportToVdfButton->OnClick = this->OnExportToVdfButton;
FCloneProfileButton->OnClick = this->OnCloneProfileButton;
// Popup Menu
FPopupMenu = FForm->PopupMenu1;
FPopupMenu->OnPopup = this->OnPopupMenu;
FAddPluginButton = FForm->AddPluginButton;
FDeletePluginButton = FForm->DeletePluginButton;
FEnableDisablePluginsButton = FForm->EnableDisablePluginsButton;
FClearPluginsButton = FForm->ClearPluginsButton;
FAddPluginButton->OnClick = this->OnAddPluginButton;
FDeletePluginButton->OnClick = this->OnDeletePluginButton;
FEnableDisablePluginsButton->OnClick = this->OnEnableDisablePluginsButton;
LoadSettings();
LoadProfileList();
}
void __fastcall TGUIHandler::OnPluginsTableEditingDone(
TObject* Sender, const int ACol, const int ARow)
{
if (!FPluginManager)
return;
UnicodeString value = Sysutils::Trim(PluginsTable->Cells[ACol][ARow]);
TPlugin* plugin = FPluginManager->GetPluginByIndex(ARow);
if (!plugin)
return;
if ((ACol == 1 || ACol == 2) && value.IsEmpty()) {
if (ACol == 1)
PluginsTable->Cells[ACol][ARow] = plugin->Name;
if (ACol == 2)
PluginsTable->Cells[ACol][ARow] = plugin->Script;
return;
}
switch (ACol) {
case 1: // Name
{
for (int i = 0; i < FPluginManager->PluginCount; ++i) {
if (i == ARow)
continue;
TPlugin* other = FPluginManager->GetPluginByIndex(i);
if (other && Sysutils::SameText(other->Name, value)) {
ErrorMessage(
L"A plugin named '%s' already exists.", other->Name);
PluginsTable->Cells[ACol][ARow] = plugin->Name;
return;
}
}
plugin->Name = value;
break;
}
case 2: // Script
{
UnicodeString checkedPath = StringReplace(value, L"\\", L"/",
TReplaceFlags() << TReplaceFlag::rfReplaceAll);
if (checkedPath != value) {
PluginsTable->Cells[ACol][ARow] = checkedPath;
value = checkedPath;
}
plugin->Script = value;
break;
}
case 3: // Concommands
plugin->Concommands = value;
break;
case 4: // AdminLevel
{
TPopupColumn* comboColumn =
dynamic_cast<TPopupColumn*>(PluginsTable->Columns[4]);
if (comboColumn) {
int index = comboColumn->Items->IndexOf(value);
if (index >= 0)
plugin->AdminLevel = static_cast<TAdminLevel>(index);
}
break;
}
}
}
__fastcall TGUIHandler::~TGUIHandler()
{
if (TSettingsManager::GetInstance() != nullptr) {
try {
if (!SaveSettings())
ErrorMessage(L"Error while saving settings.");
} __finally
{
TSettingsManager::FreeInstance();
if (FPluginManager)
delete FPluginManager;
}
}
}
void __fastcall TGUIHandler::ShowPluginsList(bool loadFromFile)
{
if (loadFromFile)
ClearPluginsTable(); // Wipe Table and Plugins Manager
if (!FPluginManager || !ProfileComboBox->Selected)
return;
if (loadFromFile) {
PluginManagerResult result = LoadPluginsList();
if (result != PluginManagerResult::Success) {
if (result == PluginManagerResult::FileError)
ErrorMessage(L"Profile file is not found.");
else if (result == PluginManagerResult::ParseError)
ErrorMessage(L"Error parsing profile file.");
else
ErrorMessage(L"Unknown error loading profile.");
return;
}
}
PluginsTable->RowCount = FPluginManager->PluginCount;
PluginsTable->BeginUpdate();
for (int i = 0; i < FPluginManager->PluginCount; i++) {
TPlugin* plugin = FPluginManager->GetPluginByIndex(i);
if (!plugin)
continue;
PluginsTable->Cells[0][i] = BoolToStr(plugin->Enabled, true);
PluginsTable->Cells[1][i] = plugin->Name;
PluginsTable->Cells[2][i] = Sysutils::Trim(plugin->Script);
PluginsTable->Cells[3][i] = plugin->Concommands;
TPopupColumn* comboColumn =
dynamic_cast<TPopupColumn*>(FForm->PluginsTable->Columns[4]);
if (comboColumn) {
int adminLevelIndex = static_cast<int>(plugin->AdminLevel);
if (adminLevelIndex >= 0 &&
adminLevelIndex < comboColumn->Items->Count)
PluginsTable->Cells[4][i] =
comboColumn->Items->Strings[adminLevelIndex];
else
PluginsTable->Cells[4][i] = L"None";
} else {
PluginsTable->Cells[4][i] = L"N/A";
}
}
PluginsTable->EndUpdate();
}
void __fastcall TGUIHandler::ToggleDefaultProfile(bool AShowDefault)
{
//
}
void __fastcall TGUIHandler::LoadProfileList()
{
/*
At this point, it's easier to clear the profile list than to
insert or extract the default profile at various code locations.
*/
ProfileComboBox->Clear();
if (FSettings->GenerateDefaltProfile &&
FSettings->ValidatePath(FSettings->PluginsFilePath, false))
LoadDefaultProfile();
const UnicodeString rootFolder = FSettings->ProfilesFolderPath;
TStringDynArray profileFiles = TDirectory::GetFiles(rootFolder, L"*.json");
for (const auto &file : profileFiles) {
UnicodeString profileName =
Ioutils::TPath::GetFileNameWithoutExtension(file);
ProfileComboBox->Items->Add(profileName);
}
if (ProfileComboBox->Items->Count > 0)
ProfileComboBox->ItemIndex = 0;
else
ClearPluginsTable();
}
void __fastcall TGUIHandler::LoadDefaultProfile()
{
PluginManagerResult PMResult =
FPluginManager->ImportFromVdf(FSettings->PluginsFilePath);
switch (PMResult) {
case PluginManagerResult::FileError:
ErrorMessage(L"Can't open '%s'. Check that the path is correct.",
FSettings->PluginsFilePath);
break;
case PluginManagerResult::ParseError:
ErrorMessage(
L"Parsing error in '%s'. Check the syntax of the file. There may be comments in the file that are not currently supported",
FSettings->PluginsFilePath);
// Application->Terminate();
break;
case PluginManagerResult::Success: {
FForm->ProfileComboBox->BeginUpdate();
PluginsTable->RowCount = FPluginManager->PluginCount;
TListBoxItem* item = new TListBoxItem(FForm->ProfileComboBox);
item->Text = L"Default Plugins";
item->TagString = L"default";
item->StyledSettings =
item->StyledSettings >> TStyledSetting::Style;
item->Font->Style = item->Font->Style << TFontStyle::fsBold;
item->ImageIndex = 2;
FForm->ProfileComboBox->InsertObject(0, item);
FForm->ProfileComboBox->ItemIndex = 0;
FForm->ProfileComboBox->EndUpdate();
break;
}
case PluginManagerResult::InvalidIndex:
ErrorMessage(L"Invalid index error.");
break;
}
}
void __fastcall TGUIHandler::OnShowSettingsForm(TObject* Sender)
{
std::unique_ptr<TSettingsForm> SForm(new TSettingsForm(FForm));
SForm->DefaultProfileEdit->Text = FSettings->PluginsFilePath;
SForm->PluginsPathEdit->Text = FSettings->PluginsFolderPath;
SForm->ProfilesPathEdit->Text = FSettings->ProfilesFolderPath;
SForm->IsDefailtProfileEnabledCheckBox->IsChecked =
FSettings->GenerateDefaltProfile;
if (SForm->ShowModal() == mrOk) {
if (ConfirmMessage(
L"Unsaved data will be lost, do you want to continue?") == mrNo)
return;
FSettings->PluginsFilePath = SForm->DefaultProfileEdit->Text;
FSettings->PluginsFolderPath = SForm->PluginsPathEdit->Text;
FSettings->ProfilesFolderPath = SForm->ProfilesPathEdit->Text;
FSettings->GenerateDefaltProfile =
SForm->IsDefailtProfileEnabledCheckBox->IsChecked;
SaveSettings();
LoadProfileList();
GetFilesTree();
ScriptsTreeView->Repaint();
}
}
void __fastcall TGUIHandler::OnProfileChanged(TObject* Sender)
{
bool IsEditableProfile =
(ProfileComboBox->Selected != nullptr &&
ProfileComboBox->Selected->TagString != L"default");
FSavePluginsListButton->Enabled = IsEditableProfile;
FEditProfileButton->Enabled = IsEditableProfile;
FCloneProfileButton->Enabled = (ProfileComboBox->Selected != nullptr);
ShowPluginsList(true);
}
void __fastcall TGUIHandler::ClearPluginsTable()
{
PluginsTable->RowCount = 0;
FPluginManager->ClearPlugins();
}
TProfileInfo __fastcall TGUIHandler::GetCurrentProfileInfo()
{
TProfileInfo info;
TListBoxItem* item = ProfileComboBox->Selected;
if (item) {
info.Name = item->Text;
info.IsDefault = (item->TagString == L"default");
if (info.IsDefault)
info.ProfileFilePath = FSettings->PluginsFilePath;
else
info.ProfileFilePath = Ioutils::TPath::Combine(
FSettings->ProfilesFolderPath, info.Name + L".json");
} else {
info.Name = L"";
info.IsDefault = false;
info.ProfileFilePath = L"";
}
return info;
}
PluginManagerResult __fastcall TGUIHandler::LoadPluginsList()
{
TProfileInfo profileInfo = GetCurrentProfileInfo();
if (profileInfo.IsDefault && !profileInfo.ProfileFilePath.IsEmpty())
return FPluginManager->ImportFromVdf(profileInfo.ProfileFilePath);
else
return FPluginManager->LoadFromJson(profileInfo.ProfileFilePath);
}
PluginManagerResult __fastcall TGUIHandler::SavePluginsList()
{
TProfileInfo profileInfo = GetCurrentProfileInfo();
if (profileInfo.ProfileFilePath.IsEmpty() && !profileInfo.IsDefault) {
ErrorMessage(L"Profile is empty.");
return PluginManagerResult::FileError;
}
if (profileInfo.IsDefault)
return FPluginManager->ExportToVdf(profileInfo.ProfileFilePath);
else
return FPluginManager->SaveToJson(profileInfo.ProfileFilePath);
}
void __fastcall TGUIHandler::OnDeleteProfile(TObject* Sender)
{
if (ProfileComboBox->ItemIndex < 0)
return;
TListBoxItem* item = ProfileComboBox->Selected;
if (!item)
return;
if (item->TagString == L"default") {
ErrorMessage(L"Unable to delete the default profile.");
return;
}
UnicodeString profileName = item->Text;
UnicodeString profileFilePath = Ioutils::TPath::Combine(
FSettings->ProfilesFolderPath, profileName + L".json");
if (!TFile::Exists(profileFilePath)) {
ErrorMessage(L"Profile file not found.");
return;
}
if (ConfirmMessage(L"Are you sure you want to delete profile '%s'?",
profileName) != mrYes)
return;
try {
TFile::Delete(profileFilePath);
ProfileComboBox->Items->Delete(ProfileComboBox->ItemIndex);
if (ProfileComboBox->Count > 0)
ProfileComboBox->ItemIndex = 0; // ShowPluginsList(true);
else {
ClearPluginsTable();
FPluginManager->ClearPlugins();
}
} catch (const Exception &e) {
ErrorMessage(L"Error deleting profile. '%s'", e.Message);
}
}
bool TGUIHandler::LoadSettings()
{
bool Result = false;
FSettings = TSettingsManager::GetInstance();
FSettings->LoadSettings();
if (FSettings->IsSettingsLoaded() || FSettings->IsInitialized()) {
GetFilesTree();
FForm->Height = FSettings->FormHeight;
FForm->Width = FSettings->FormWidth;
FForm->LeftLayout->Width = FSettings->GetSeparatorSize();
Result = true;
}
return Result;
}
bool TGUIHandler::SaveSettings()
{
FSettings->FormWidth = FForm->Width;
FSettings->FormHeight = FForm->Height;
FSettings->SeparatorSize = FForm->LeftLayout->Width;
FSettings->SaveSettings();
return true;
}
void __fastcall TGUIHandler::OnAddProfileButton(TObject* Sender)
{
std::unique_ptr<TInputForm> IForm(new TInputForm(FForm));
if (IForm->ShowModal() == mrOk) {
UnicodeString profileName = Sysutils::Trim(IForm->NameEdit->Text);
if (!Ioutils::TPath::HasValidFileNameChars(profileName, false)) {
ErrorMessage(L"Profile name contains invalid characters.");
return;
}
UnicodeString profileFilePath = Ioutils::TPath::Combine(
FSettings->ProfilesFolderPath, profileName + L".json");
if (ProfileComboBox->Items->IndexOf(profileName) >= 0 ||
TFile::Exists(profileFilePath))
{
ErrorMessage(L"A profile named '%s' already exists.", profileName);
return;
}
try {
TFile::WriteAllText(
profileFilePath, L"{\"plugins\":[]}", TEncoding::UTF8);
} catch (const Exception &e) {
ErrorMessage(L"Error creating profile: '%s'", e.Message);
return;
}
ProfileComboBox->Items->Add(profileName);
ProfileComboBox->ItemIndex = ProfileComboBox->Items->Count - 1;
}
}
void __fastcall TGUIHandler::OnCellClick(TColumn* const Column, const int Row)
{
if (dynamic_cast<TCheckColumn*>(Column) != nullptr) {
UnicodeString cellText = PluginsTable->Cells[Column->Index][Row];
bool currentValue =
(cellText == "True") || (cellText == "true") || (cellText == "1");
bool newValue = !currentValue;
PluginsTable->Cells[Column->Index][Row] = newValue ? "True" : "False";
FPluginManager->GetPluginByIndex(Row)->Enabled =
newValue; // ? true : false;
}
}
void __fastcall TGUIHandler::GetFilesTree()
{
if (!FScriptsTreeView)
return;
const UnicodeString rootFolder = FSettings->PluginsFolderPath;
FAniLoadingIndicator->Visible = true;
FileTreeUtils::BuildFileTree(
FScriptsTreeView, rootFolder, L"*.as", [this]() {
FAniLoadingIndicator->Visible = false;
if (FScriptsTreeView->Count > 0)
FScriptsTreeView->ItemByIndex(0)->IsSelected = true;
});
}
void __fastcall TGUIHandler::OnAddPluginButton(TObject* Sender)
{
std::unique_ptr<TAddPluginForm> AddPluginForm(new TAddPluginForm(FForm));
TStringDynArray plugins;
try {
plugins = TDirectory::GetFiles(FSettings->PluginsFolderPath, L"*.as",
TSearchOption::soAllDirectories);
} catch (const Exception &e) {
ErrorMessage(L"Failed to get plugin scripts: '%s'", e.Message);
return;
}
AddPluginForm->SetFileList(plugins, FSettings->PluginsFolderPath);
if (AddPluginForm->ShowModal() != mrOk)
return; // ?
const String formPluginName =
Sysutils::Trim(AddPluginForm->GetPluginName());
const String formPluginPath =
Sysutils::Trim(AddPluginForm->GetPluginPath());
if (formPluginName.IsEmpty()) {
AlertMessage(L"Plugin name cannot be empty.");
return;
}
if (formPluginPath.IsEmpty()) {
AlertMessage(L"Plugin path cannot be empty.");
return;
}
if (IsPluginExists(formPluginName, formPluginPath))
return;
if (!AddPlugin(formPluginName, formPluginPath)) {
ErrorMessage(L"Failed to add plugin.");
return;
}
ShowPluginsList(false);
}
bool TGUIHandler::IsPluginExists(const String &name, const String &path)
{
if (FPluginManager->GetPluginByName(name) != nullptr) {
AlertMessage(L"A plugin named '%s' already exists.", name);
return true;
}
if (FPluginManager->GetPluginByScriptName(path) != nullptr) {
AlertMessage(L"A plugin script '%s' already exists.", path);
return true;
}
return false;
}
bool TGUIHandler::AddPlugin(const String &name, const String &script)
{
try {
FPluginManager->AddPlugin(name, script);
return true;
} catch (...) {
return false;
}
}
void __fastcall TGUIHandler::OnDeletePluginButton(TObject* Sender)
{
int selectedIndex = PluginsTable->Row;
if (selectedIndex < 0 || selectedIndex >= FPluginManager->PluginCount) {
AlertMessage(L"Please select a plugin to delete.");
return;
}
TPlugin* plugin = FPluginManager->GetPluginByIndex(selectedIndex);
String pluginName = plugin ? plugin->Name : L"<unknown plugin>";
if (ConfirmMessage(
L"Are you sure you want to delete '%s' from the current profile?",
pluginName) == mrNo)
return;
PluginManagerResult result =
FPluginManager->RemovePluginByIndex(selectedIndex);
if (result == PluginManagerResult::Success)
ShowPluginsList(false);
else if (result == PluginManagerResult::InvalidIndex)
ErrorMessage(L"Invalid index for deleting plugin '%s'.", pluginName);
else
ErrorMessage(L"Error occurred while deleting plugin '%s'.", pluginName);
}
void __fastcall TGUIHandler::OnEditProfileButton(TObject* Sender)
{
TListBoxItem* currentItem = ProfileComboBox->Selected;
if (!currentItem || currentItem->TagString == L"default") {
return;
}
UnicodeString oldName = currentItem->Text;
UnicodeString oldFilePath = Ioutils::TPath::Combine(
FSettings->ProfilesFolderPath, oldName + L".json");
std::unique_ptr<TInputForm> IForm(new TInputForm(FForm));
IForm->NameEdit->Text = oldName;
if (IForm->ShowModal() == mrOk) {
UnicodeString newName = Sysutils::Trim(IForm->NameEdit->Text);
if (newName.IsEmpty() || newName == oldName) {
return;
}
if (!Ioutils::TPath::HasValidFileNameChars(newName, false)) {
ErrorMessage(L"Profile name contains invalid characters.");
return;
}
if (ProfileComboBox->Items->IndexOf(newName) != -1) {
ErrorMessage(L"A profile named '%s' already exists.", newName);
return;
}
UnicodeString newFilePath = Ioutils::TPath::Combine(
FSettings->ProfilesFolderPath, newName + L".json");
if (TFile::Exists(newFilePath)) {
ErrorMessage(L"A profile named '%s' already exists.", newName);
return;
}
try {
TFile::Move(oldFilePath, newFilePath);
}
catch (Exception &e) {
ErrorMessage(L"Error renaming profile file: '%s'", e.Message);
return;
}
ProfileComboBox->BeginUpdate();
int CurIndex = currentItem->Index;
ProfileComboBox->Items->Delete(CurIndex);
ProfileComboBox->Items->Insert(CurIndex, newName);
ProfileComboBox->ItemIndex = CurIndex;
ProfileComboBox->EndUpdate();
}
}
void __fastcall TGUIHandler::OnEnableDisablePluginsButton(TObject * Sender)
{
if (FPluginManager == nullptr || FPluginManager->PluginCount == 0) {
AlertMessage(L"No plugins available.");
return;
}
bool anyEnabled = false;
for (int i = 0; i < FPluginManager->PluginCount; ++i) {
TPlugin* plugin = FPluginManager->GetPluginByIndex(i);
if (plugin && plugin->Enabled) {
anyEnabled = true;
break;
}
}
bool newEnabledState = !anyEnabled;
for (int i = 0; i < FPluginManager->PluginCount; ++i) {
TPlugin* plugin = FPluginManager->GetPluginByIndex(i);
if (plugin)
plugin->Enabled = newEnabledState;
}
ShowPluginsList(false);
}
void __fastcall TGUIHandler::OnSavePluginsListButton(TObject * Sender)
{
if (SavePluginsList() != PluginManagerResult::Success)
ErrorMessage(L"Failed to save plugins.");
else
Beep();
}
void __fastcall TGUIHandler::OnExportToVdfButton(TObject * Sender)
{
UnicodeString pluginsFilePath = FSettings->PluginsFilePath;
if (ProfileComboBox->Selected == nullptr || pluginsFilePath.IsEmpty()) {
ErrorMessage(
L"An error occured while exporting to the VDF file. Check the Settings.");
return;
}
if (SavePluginsList() != PluginManagerResult::Success)
ErrorMessage(
L"To avoid data loss during export, the current profile is saved automatically. However, an error occurred during the saving process the file might be corrupted or deleted.");
if (FPluginManager->ExportToVdf(FSettings->PluginsFilePath) ==
PluginManagerResult::Success)
{
InfoMessage(L"Profile exported to VDF successfully.");
int currentIndex = ProfileComboBox->ItemIndex;
ProfileComboBox->BeginUpdate();
LoadProfileList();
ShowPluginsList();
if (currentIndex > 0)
ProfileComboBox->ItemIndex = currentIndex;
ProfileComboBox->EndUpdate();
} else
ErrorMessage(L"Failed to export profile to VDF.");
}
void __fastcall TGUIHandler::OnTreeViewDblClick(TObject * Sender)
{
if (FForm->ProfileComboBox->Selected == nullptr) {
AlertMessage(L"Please select a profile first.");
return;
}
TTreeViewItem* item =
dynamic_cast<TTreeViewItem*>(ScriptsTreeView->Selected);
if (!item)
return;
if (item->ImageIndex == 0) {
item->IsExpanded = !item->IsExpanded;
return;
}
UnicodeString newText = Sysutils::Trim(item->Text);
UnicodeString newScript = Sysutils::Trim(item->TagString);
for (int row = 0; row < PluginsTable->RowCount; ++row) {
UnicodeString existingText =
Sysutils::Trim(PluginsTable->Cells[1][row]);
UnicodeString existingScript =
Sysutils::Trim(PluginsTable->Cells[2][row]);
if (Sysutils::SameText(existingText, newText)) {
AlertMessage(
L"Plugin named '%s' already exists.", existingText);
return;
}
if (Sysutils::SameText(existingScript, newScript)) {
AlertMessage(
L"Path named '%s' already exists.", existingScript);
return;
}
}
if (!AddPlugin(newText, item->TagString)) {
ErrorMessage(L"Failed to add plugin.");
return;
}
ShowPluginsList(false);
}
void __fastcall TGUIHandler::OnTreeViewMouseDown(TObject * Sender,
TMouseButton Button, TShiftState Shift, float X, float Y)
{
TTreeViewItem* item = ScriptsTreeView->ItemByPoint(X, Y);
if (!item)
ScriptsTreeView->Selected = nullptr;
}
void __fastcall TGUIHandler::OnPopupMenu(TObject * Sender)
{
TPointF screenPos;
IFMXMouseService* MouseService = nullptr;
if (TPlatformServices::Current->SupportsPlatformService(
__uuidof(IFMXMouseService),
reinterpret_cast<void**>(&MouseService)))
screenPos = MouseService->GetMousePos();
TPointF localPos = PluginsTable->ScreenToLocal(screenPos);
int cellCol = -1, cellRow = -1, rowCount = PluginsTable->RowCount;
bool cellFound =
PluginsTable->CellByPoint(localPos.X, localPos.Y, cellCol, cellRow);
FDeletePluginButton->Enabled = cellFound;
FEnableDisablePluginsButton->Enabled = (rowCount > 0);
FClearPluginsButton->Enabled = (bool)rowCount;
if (!cellFound)
PluginsTable->SelectCell(-1, -1);
else
PluginsTable->SelectCell(cellCol, cellRow);
}
void __fastcall TGUIHandler::FormKeyDown(TObject * Sender, WORD & Key,
System::WideChar & KeyChar, TShiftState Shift)
{
if (Key == 0x53 && Shift.Contains(ssCtrl)) {
if (ProfileComboBox->Selected != nullptr) {
if (ProfileComboBox->Selected->TagString == L"default") {
if (ConfirmMessage(
L"Do you want to export the default profile?\nSelect 'No' to cancel this action.") ==
mrNo)
return;
}
OnSavePluginsListButton(FForm);
}
} else if (Key == 0x45 && Shift.Contains(ssCtrl)) {
OnExportToVdfButton(FForm);
}
}
void __fastcall TGUIHandler::OnCloneProfileButton(TObject * Sender)
{
std::unique_ptr<TInputForm> IForm(new TInputForm(FForm));
UnicodeString defaultName = ProfileComboBox->Selected->Text + L" Copy";
int counter = 1;
UnicodeString profileName = defaultName;
while (ProfileComboBox->Items->IndexOf(profileName) >= 0 ||
TFile::Exists(Ioutils::TPath::Combine(
FSettings->ProfilesFolderPath, profileName + L".json")))
{
profileName = defaultName + L" (" + IntToStr(counter++) + L")";
}
IForm->NameEdit->Text = profileName;
if (IForm->ShowModal() == mrOk) {
profileName = Sysutils::Trim(IForm->NameEdit->Text);
if (!Ioutils::TPath::HasValidFileNameChars(profileName, false)) {
ErrorMessage(L"Profile name contains invalid characters.");
return;
}
UnicodeString profileFilePath = Ioutils::TPath::Combine(
FSettings->ProfilesFolderPath, profileName + L".json");
if (ProfileComboBox->Items->IndexOf(profileName) >= 0 ||
TFile::Exists(profileFilePath))
{
ErrorMessage(
L"A profile named '%s' already exists.", profileName);
return;
}
if (FPluginManager->SaveToJson(profileFilePath) !=
PluginManagerResult::Success)
{
ErrorMessage(L"Failed to save the cloned profile.");
return;
}
ProfileComboBox->Items->Add(profileName);
ProfileComboBox->ItemIndex = ProfileComboBox->Items->Count - 1;
ShowPluginsList();
}
}