unit cde_SettingsForm;
{$mode delphi}
{$codepage UTF8}
interface
uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls, StdCtrls, ExtCtrls, Buttons,
     EditBtn, Spin, cde_dir, cde_lang, VersionControl, cde_types, LazFileUtils, cde_kernel, ANBFormatString,
     windows, SimplyJSON, LazUTF8, DividerBevel, GraphicsEx, ANBInputBox;
type
  { Tcde_Settings }
  Tcde_Settings = class(TForm)
    Add2WhiteListBtn: TBitBtn;
    Add2BlackListBtn: TBitBtn;
    Add2DWDriveListBtn: TBitBtn;
    Delete4WhiteListBtn: TBitBtn;
    BrowseProgramEdt: TFileNameEdit;
    BrowseParamHlp: TSpeedButton;
    ColorDlg: TColorDialog;
    Delete4BlackListBtn: TBitBtn;
    Delete4DWDriveListBtn: TBitBtn;
    DFC_B: TSpinEdit;
    DFC_BLbl: TLabel;
    DFC_G: TSpinEdit;
    DFC_GLbl: TLabel;
    DBC_R: TSpinEdit;
    DBC_G: TSpinEdit;
    DBC_B: TSpinEdit;
    DFC_R: TSpinEdit;
    DBC_RLbl: TLabel;
    DBC_GLbl: TLabel;
    DBC_BLbl: TLabel;
    DFC_RLbl: TLabel;
    DBC_SelectBtn: TBitBtn;
    DFC_SelectBtn: TBitBtn;
    DiscPercentColorsBusyLbl: TLabel;
    DiscPercentColorsFreeLbl: TLabel;
    DiscPercentColorsSep: TDividerBevel;
    dwIntervalEdt: TFloatSpinEdit;
    dwIntervalLbl: TLabel;
    FilterTypeBox: TComboBox;
    DWTypeBox: TComboBox;
    FlagsList: TImageList;
    BrowseProgramLbl: TLabel;
    BrowseParamEdt: TLabeledEdit;
    LanguageBox: TListView;
    OkBtn: TBitBtn;
    CancelBtn: TBitBtn;
    ButtonsSep: TBevel;
    ButtonsPanel: TPanel;
    SettingsPages: TPageControl;
    LanguageSht: TTabSheet;
    BrowseSht: TTabSheet;
    BrowseProgramHlp: TSpeedButton;
    InterfaceSht: TTabSheet;
    ShowDiscM: TCheckBox;
    FilterSht: TTabSheet;
    DriveWatcherSht: TTabSheet;
    TabsImages: TImageList;
    SettingsTree: TTreeView;
    WhitelistBox: TListBox;
    BlacklistBox: TListBox;
    DWDriveListBox: TListBox;
    WhiteListSep: TDividerBevel;
    BlackListSep: TDividerBevel;
    DWDriveListSep: TDividerBevel;
    procedure Add2BlackListBtnClick (Sender: TObject );
    procedure Add2DWDriveListBtnClick (Sender: TObject );
    procedure Add2WhiteListBtnClick (Sender: TObject );
    procedure BrowseParamHlpClick(Sender: TObject);
    procedure BrowseProgramHlpClick(Sender: TObject);
    procedure Delete4BlackListBtnClick (Sender: TObject );
    procedure Delete4DWDriveListBtnClick (Sender: TObject );
    procedure Delete4WhiteListBtnClick (Sender: TObject );
    procedure DFC_SelectBtnClick (Sender: TObject );
    procedure DBC_SelectBtnClick (Sender: TObject );
    procedure FormCreate(Sender: TObject);
    procedure OkBtnClick(Sender: TObject);
    procedure SettingsTreeChange(Sender: TObject; Node: TTreeNode);
  private
    procedure FillLanguageList;
    procedure LoadSettings;
    procedure SaveSettings;
    procedure LocalizeIt;
  public
  end;
var
  cde_Settings: Tcde_Settings;
implementation
uses cde_MainForm;
{$R *.lfm}
{ Tcde_Settings }
procedure Tcde_Settings.FillLanguageList;
var SR: TSearchRec;
    LI: TListItem;
    Indx: Integer;
    FLanguage: String;
    LangInfo: TCDELanguageInfo;
    Image: TIcon;
begin
Indx:= -1;
LanguageBox.Items.Clear;
FlagsList.Clear;
if FindFirstUTF8(IncludeTrailingBackslash(GetCDEPath) + 'languages\*.json', faAnyFile, SR) = 0 then
 repeat
  if (not ((SR.Name = '.') or (SR.Name = '..'))) and FileExistsUTF8(IncludeTrailingBackslash(GetCDEPath) + 'languages\' + SR.Name) then
   begin
   LI:= LanguageBox.Items.Add;
   FLanguage:= ExtractFileNameWithoutExt(SR.Name);
   LangInfo:= GetLanguageInfo(FLanguage);
   LI.Caption:= LangInfo.liName;
   LI.SubItems.Add(FLanguage);
   Li.SubItems.Add(LangInfo.liAuthor);
   LI.SubItems.Add(VersionInfoToStr(LangInfo.liVersion));
   LI.SubItems.Add(VersionInfoToStr(LangInfo.liCDEVersion));
   if (UTF8CompareStr(UTF8LowerCase(FLanguage), UTF8LowerCase(GetLanguage)) = 0) then
    Indx:= LI.Index;
   Image:= TIcon.Create;
   Image.LoadFromFile(IncludeTrailingBackslash(GetCDEPath) + 'languages\flags\' + FLanguage + '.ico');
   LI.ImageIndex:= FlagsList.AddIcon(Image);
   Image.Free;
   end;
  until FindNextUTF8(SR) <> 0;
FindCloseUTF8(SR);
LanguageBox.Items.Item[Indx].Selected:= True;
end;
procedure Tcde_Settings.LoadSettings;
var ConfigFile: String;
    RGBColor: TRGBColor;
begin
ConfigFile:= GetCDEPath + 'configs\configs.json';
FillLanguageList;
//Load interface
ShowDiscM.Checked:= JSReadBoolean('/interface/discmshow', True, ConfigFile);
//- Load busy color
with RGBColor do
 begin
 R:= 0;
 G:= 120;
 B:= 215;
 end;
RGBColor:= JSReadRGBColor('/interface/ds_busycolor', RGBColor, ConfigFile);
with RGBColor do
 begin
 DBC_R.Value:= R;
 DBC_G.Value:= G;
 DBC_B.Value:= B;
 end;
//- Load Free color
with RGBColor do
 begin
 R:= 240;
 G:= 240;
 B:= 240;
 end;
RGBColor:= JSReadRGBColor('/interface/ds_freecolor', RGBColor, ConfigFile);
with RGBColor do
 begin
 DFC_R.Value:= R;
 DFC_G.Value:= G;
 DFC_B.Value:= B;
 end;
//Load filtration
FilterTypeBox.ItemIndex:= JSReadInteger('/filter/type', 0, ConfigFile);
WhitelistBox.Items.Delimiter:= ';';
WhitelistBox.Items.DelimitedText:= JSReadString('/filter/whitelist', '', ConfigFile);
BlacklistBox.Items.Delimiter:= ';';
BlacklistBox.Items.DelimitedText:= JSReadString('/filter/blacklist', '', ConfigFile);
//Load browse
BrowseProgramEdt.Text:= JSReadString('/browse/program', '$windir$explorer.exe', ConfigFile);
BrowseParamEdt.Text:= JSReadString('/browse/param', '$letter$:\', ConfigFile);
//Load Drive Watcher
DWTypeBox.ItemIndex:= JSReadInteger('/drivewatcher/type', 1, ConfigFile);
dwIntervalEdt.Value:= JSReadInteger('/drivewatcher/interval', 2000, ConfigFile) / 1000;
DWDriveListBox.Items.Delimiter:= ';';
DWDriveListBox.Items.DelimitedText:= JSReadString('/drivewatcher/driveslist', '', ConfigFile);
end;
procedure Tcde_Settings.SaveSettings;
var ConfigFile: String;
    RGBColor: TRGBColor;
begin
ConfigFile:= GetCDEPath + 'configs\configs.json';
//Save language
JSWriteString('/interface/language', LanguageBox.Selected.SubItems[0], ConfigFile);
//Save interface
JSWriteBoolean('/interface/discmshow', ShowDiscM.Checked, ConfigFile);
//- save busy color
with RGBColor do
 begin
 R:= DBC_R.Value;
 G:= DBC_G.Value;
 B:= DBC_B.Value;
 end;
JSWriteRGBColor('/interface/ds_busycolor', RGBColor, ConfigFile);
//- save free color
with RGBColor do
 begin
 R:= DFC_R.Value;
 G:= DFC_G.Value;
 B:= DFC_B.Value;
 end;
JSWriteRGBColor('/interface/ds_freecolor', RGBColor, ConfigFile);
//Save filtration
JSWriteInteger('/filter/type', FilterTypeBox.ItemIndex, ConfigFile);
WhitelistBox.Items.Delimiter:= ';';
JSWriteString('/filter/whitelist', WhitelistBox.Items.DelimitedText, ConfigFile);
BlacklistBox.Items.Delimiter:= ';';
JSWriteString('/filter/blacklist', BlacklistBox.Items.DelimitedText, ConfigFile);
//Save browse
JSWriteString('/browse/program', BrowseProgramEdt.Text, ConfigFile);
JSWriteString('/browse/param', BrowseParamEdt.Text, ConfigFile);
//Save Drive Watcher
JSWriteInteger('/drivewatcher/type', DWTypeBox.ItemIndex, ConfigFile);
JSWriteInteger('/drivewatcher/interval', round(dwIntervalEdt.Value * 1000), ConfigFile);
JSWriteString('/drivewatcher/driveslist', DWDriveListBox.Items.DelimitedText, ConfigFile);
end;
procedure Tcde_Settings.LocalizeIt;
begin
Caption:= GetLocalizedString(cdeg_settings_caption);
with SettingsTree do
 begin
 Items.Item[0].Text:= GetLocalizedString(cdeg_settings_tabs_language);
 Items.Item[1].Text:= GetLocalizedString(cdeg_settings_tabs_interface);
 Items.Item[2].Text:= GetLocalizedString(cdeg_settings_tabs_filtration);
 Items.Item[3].Text:= GetLocalizedString(cdeg_settings_tabs_browse);
 Items.Item[4].Text:= GetLocalizedString(cdeg_settings_tabs_drivewatcher);
 end;
with LanguageBox do
 begin
 Column[0].Caption:= GetLocalizedString(cdeg_settings_languageboxcolumns_name);
 Column[2].Caption:= GetLocalizedString(cdeg_settings_languageboxcolumns_author);
 Column[3].Caption:= GetLocalizedString(cdeg_settings_languageboxcolumns_version);
 Column[4].Caption:= GetLocalizedString(cdeg_settings_languageboxcolumns_cdeversion);
 end;
ShowDiscM.Caption:= GetLocalizedString(cdeg_settings_showdiscm);
DiscPercentColorsSep.Caption:= GetLocalizedString(cdeg_settings_discpercentcolor);
DiscPercentColorsBusyLbl.Caption:= GetLocalizedString(cdeg_settings_discbusycolor);
DiscPercentColorsFreeLbl.Caption:= GetLocalizedString(cdeg_settings_discfreecolor);
DBC_SelectBtn.Caption:= GetLocalizedString(cdeg_settings_buttons_select);
DFC_SelectBtn.Caption:= GetLocalizedString(cdeg_settings_buttons_select);
with FilterTypeBox.Items do
 begin
 Strings[0]:= GetLocalizedString(cdeg_settings_filtertypes_disable);
 Strings[1]:= GetLocalizedString(cdeg_settings_filtertypes_onlywhitelist);
 Strings[2]:= GetLocalizedString(cdeg_settings_filtertypes_exceptblacklist);
 end;
WhiteListSep.Caption:= GetLocalizedString(cdeg_settings_whitelist);
Add2WhiteListBtn.Caption:= GetLocalizedString(cdeg_settings_buttons_add);
Delete4WhiteListBtn.Caption:= GetLocalizedString(cdeg_settings_buttons_remove);
BlackListSep.Caption:= GetLocalizedString(cdeg_settings_blacklist);
Add2BlackListBtn.Caption:= GetLocalizedString(cdeg_settings_buttons_add);
Delete4BlackListBtn.Caption:= GetLocalizedString(cdeg_settings_buttons_remove);
BrowseProgramLbl.Caption:= GetLocalizedString(cdeg_settings_browseprogram);
BrowseProgramEdt.Filter:= GetLocalizedString(cdeg_settings_browseprogramfilter) + ' (*.exe)|*.exe';
BrowseParamEdt.EditLabel.Caption:= GetLocalizedString(cdeg_settings_browseparam);
OkBtn.Caption:= GetLocalizedString(cdeg_settings_buttons_apply);
CancelBtn.Caption:= GetLocalizedString(cdeg_settings_buttons_cancel);
end;
procedure Tcde_Settings.SettingsTreeChange(Sender: TObject; Node: TTreeNode);
begin
SettingsPages.ActivePageIndex:= Node.StateIndex;
end;
procedure Tcde_Settings.FormCreate(Sender: TObject);
begin
LocalizeIt;
LoadSettings;
end;
procedure Tcde_Settings.OkBtnClick(Sender: TObject);
var NeedRestartMSG: String;
begin
NeedRestartMSG:= GetLocalizedString(cdeg_messages_needrestart);
SaveSettings;
ShowMessageBox(NeedRestartMSG, MB_ICONWARNING);
ModalResult:= mrOK;
end;
procedure Tcde_Settings.BrowseProgramHlpClick(Sender: TObject);
var HlpText: String;
begin
HlpText:= FormatStr(GetLocalizedString(cdeg_messages_settingsbrowseprogramhelp), ['$newline$'], [#13#10]);
ShowMessageBox(HlpText, MB_ICONASTERISK);
end;
procedure Tcde_Settings.Delete4BlackListBtnClick (Sender: TObject );
begin
if BlacklistBox.ItemIndex > - 1 then
 BlacklistBox.Items.Delete(BlacklistBox.ItemIndex);
end;
procedure Tcde_Settings.Delete4DWDriveListBtnClick (Sender: TObject );
begin
if DWDriveListBox.ItemIndex > - 1 then
 DWDriveListBox.Items.Delete(DWDriveListBox.ItemIndex);
end;
procedure Tcde_Settings.Delete4WhiteListBtnClick (Sender: TObject );
begin
if WhitelistBox.ItemIndex > - 1 then
 WhitelistBox.Items.Delete(WhitelistBox.ItemIndex);
end;
procedure Tcde_Settings.DFC_SelectBtnClick (Sender: TObject );
var RGBColor: TRGBColor;
begin
with RGBColor do
 begin
 R:= DFC_R.Value;
 G:= DFC_G.Value;
 B:= DFC_B.Value;
 end;
with ColorDlg do
 begin
 Color:= RGB2Color(RGBColor);
 Execute;
 RGBColor:= Color2RGB(Color);
 end;
with RGBColor do
 begin
 DFC_R.Value:= R;
 DFC_G.Value:= G;
 DFC_B.Value:= B;
 end;
end;
procedure Tcde_Settings.DBC_SelectBtnClick (Sender: TObject );
var RGBColor: TRGBColor;
begin
with RGBColor do
 begin
 R:= DBC_R.Value;
 G:= DBC_G.Value;
 B:= DBC_B.Value;
 end;
with ColorDlg do
 begin
 Color:= RGB2Color(RGBColor);
 Execute;
 RGBColor:= Color2RGB(Color);
 end;
with RGBColor do
 begin
 DBC_R.Value:= R;
 DBC_G.Value:= G;
 DBC_B.Value:= B;
 end;
end;
procedure Tcde_Settings.BrowseParamHlpClick(Sender: TObject);
var HlpText: String;
begin
HlpText:= FormatStr(GetLocalizedString(cdeg_messages_settingsbrowseparamhelp), ['$newline$'], [#13#10]);
ShowMessageBox(HlpText, MB_ICONASTERISK);
end;
procedure Tcde_Settings.Add2WhiteListBtnClick (Sender: TObject);
var isAccept: Boolean;
    DriveName: String;
begin
DriveName:= '';
isAccept:= False;
DriveName:= ShowInputBox(GetLocalizedString(cdeg_whitelistadd_caption), GetLocalizedString(cdeg_whitelistadd_prompt), 'a', [GetLocalizedString(cdeg_whitelistadd_okbtn), GetLocalizedString(cdeg_whitelistadd_cancelbtn)], isAccept);
if isAccept then
 if (WhitelistBox.Items.IndexOf((Trim(DriveName))[1]) = -1) then
 WhitelistBox.Items.Add((Trim(DriveName))[1]);
end;
procedure Tcde_Settings.Add2BlackListBtnClick (Sender: TObject );
var isAccept: Boolean;
    DriveName: String;
begin
DriveName:= '';
isAccept:= False;
DriveName:= ShowInputBox(GetLocalizedString(cdeg_blacklistadd_caption), GetLocalizedString(cdeg_blacklistadd_prompt), 'a', [GetLocalizedString(cdeg_blacklistadd_okbtn), GetLocalizedString(cdeg_blacklistadd_cancelbtn)], isAccept);
if isAccept then
 if (BlacklistBox.Items.IndexOf((Trim(DriveName))[1]) = -1) then
 BlacklistBox.Items.Add((Trim(DriveName))[1]);
end;
procedure Tcde_Settings.Add2DWDriveListBtnClick (Sender: TObject );
var isAccept: Boolean;
    DriveName: String;
begin
DriveName:= '';
isAccept:= False;
DriveName:= ShowInputBox(GetLocalizedString(cdeg_dwlistadd_caption), GetLocalizedString(cdeg_dwlistadd_prompt), 'a', [GetLocalizedString(cdeg_dwlistadd_okbtn), GetLocalizedString(cdeg_blacklistadd_cancelbtn)], isAccept);
if isAccept then
 if (DWDriveListBox.Items.IndexOf((Trim(DriveName))[1]) = -1) then
 DWDriveListBox.Items.Add((Trim(DriveName))[1]);
end;
end.
