unit cde_disc_frame;
{$mode delphi}
{$codepage UTF8}
interface
uses Classes, SysUtils, FileUtil, UTF8Process, Forms, Controls, ExtCtrls, StdCtrls, Buttons, ComCtrls, cde_types,
     cde_lang, ANBFormatString, ANBInputBox, cde_dir, SimplyJSON, BCRadialProgressBar, LazFileUtils, GraphicsEx;
type
  { Tcde_disc_frm }
  Tcde_disc_frm = class(TFrame)
    DiscBusyImg: TPaintBox;
    DiscFreeImg: TPaintBox;
    DiscFreeLbl: TLabel;
    DiscSizeLbl: TLabel;
    DiscSizeImg: TImage;
    DiscBusyLbl: TLabel;
    DiscSizePercentBar: TBCRadialProgressBar;
    DiscStateHint: TLabel;
    DriveInfoExistPanel: TPanel;
    DriveInfoNotExistPanel: TPanel;
    CloseDiscBtn: TSpeedButton;
    NoDiscInfoLbl: TLabel;
    NDiscLbl: TLabel;
    RefrashInfoBtn: TSpeedButton;
    InfoPanel: TPanel;
    Separator: TBevel;
    TitlePanel: TPanel;
    EjectDiscBtn: TSpeedButton;
    CreateLinkBtn: TSpeedButton;
    DiscImg: TImage;
    EDiscLbl: TLabel;
    FileSystemImg: TImage;
    DiscSerialImg: TImage;
    DiscSerialLbl: TLabel;
    AdditionalActionsPanel: TPanel;
    BrowseDiscBtn: TSpeedButton;
    VolumeNameLbl: TLabel;
    VolumeNameImg: TImage;
    FileSystemLbl: TLabel;
    procedure BrowseDiscBtnClick(Sender: TObject);
    procedure CloseDiscBtnClick (Sender: TObject );
    procedure CreateLinkBtnClick(Sender: TObject);
    procedure EjectDiscBtnClick(Sender: TObject);
    procedure RefrashInfoBtnClick(Sender: TObject);
  private
  public
   Drive: WideChar;
   DriveInfo: TDiscInfo;
   procedure UpdateDriveInfo;
   procedure do_EjectDisc;
   procedure do_CloseDisc;
   procedure do_Refrash;
   procedure do_CreateLink;
   procedure do_BrowseDisc;
  end;
implementation
uses kernel;
{$R *.lfm}
//frame actions procs
procedure Tcde_disc_frm.do_EjectDisc;
begin
EjectDrive(Drive);
UpdateDriveInfo;
end;
procedure Tcde_disc_frm.do_CloseDisc;
begin
CloseDrive(Drive);
UpdateDriveInfo;
end;
procedure Tcde_disc_frm.do_Refrash;
begin
UpdateDriveInfo;
end;
procedure Tcde_disc_frm.do_CreateLink;
var LinkName: String;
    isAccepted: Boolean;
begin
LinkName:= ShowInputBox(GetLocalizedString(cdeg_linkcreation_caption), GetLocalizedString(cdeg_linkcreation_prompt), FormatStrUDI(GetLocalizedString(cdeg_linkcreation_default), DriveInfo), [GetLocalizedString(cdeg_linkcreation_okbtn), GetLocalizedString(cdeg_linkcreation_cancelbtn)], isAccepted);
if isAccepted then
 CreateShortCut(ExpandPath(pth_desktop) + LinkName + '.lnk', '/eject="' + DriveInfo.diDrive + '"', GetCDEPath + 'bin\cdejecter.exe');
end;
procedure Tcde_disc_frm.do_BrowseDisc;
var AppName, AppParams: String;
    RunProcess: TProcessUTF8;
begin
AppName:= JSReadString('/browse/program', '', GetCDEPath + 'configs\configs.json');
AppName:= ExpandPath(AppName);
AppParams:= JSReadString('/browse/param', '', GetCDEPath + 'configs\configs.json');
AppParams:= FormatStrUDI(AppParams, DriveInfo);
if not FileExistsUTF8(AppName) then
 Abort;
RunProcess:= TProcessUTF8.Create(Nil);
with RunProcess do
 begin
 CommandLine:= AppName + ' ' + AppParams;
 Execute;
 Free;
 end;
end;
procedure Tcde_disc_frm.RefrashInfoBtnClick(Sender: TObject);
begin
do_Refrash;
end;
procedure Tcde_disc_frm.EjectDiscBtnClick(Sender: TObject);
begin
do_EjectDisc;
end;
procedure Tcde_disc_frm.CreateLinkBtnClick(Sender: TObject);
begin
do_CreateLink;
end;
procedure Tcde_disc_frm.BrowseDiscBtnClick(Sender: TObject);
begin
do_BrowseDisc;
end;
procedure Tcde_disc_frm.CloseDiscBtnClick (Sender: TObject );
begin
do_CloseDisc;
end;
procedure Tcde_disc_frm.UpdateDriveInfo;
var BusyColor, FreeColor: TRGBColor;
begin
DriveInfoExistPanel.Hide;
DriveInfoNotExistPanel.Hide;
//Colors
with BusyColor do
 begin
 R:= 0;
 G:= 0;
 B:= 0;
 end;
BusyColor:= JSReadRGBColor('/interface/discsize/busycolor', BusyColor, GetCDEPath + 'configs\configs.json');
with FreeColor do
 begin
 R:= 192;
 G:= 192;
 B:= 192;
 end;
FreeColor:= JSReadRGBColor('/interface/discsize/freecolor', FreeColor, GetCDEPath + 'configs\configs.json');
with DiscSizePercentBar do
 begin
 LineBkgColor:= RGB2Color(FreeColor);
 LineColor:= RGB2Color(BusyColor);
 end;
DiscBusyImg.Canvas.Brush.Color:= RGB2Color(BusyColor);
DiscBusyImg.Canvas.FillRect(DiscBusyImg.Canvas.ClipRect);
DiscBusyImg.Refresh;
DiscFreeImg.Canvas.Brush.Color:= RGB2Color(FreeColor);
DiscFreeImg.Canvas.FillRect(DiscFreeImg.Canvas.ClipRect);
DriveInfo:= GetDiscInfo(Drive);
DiscImg.Picture.Clear;
if GetDiscState(Drive) <> dsNIL then
 begin
 DiscImg.Picture.LoadFromFile(GetCDEPath + 'images\in_drive.png');
 DiscStateHint.Caption:= GetLocalizedString(cdeg_discinfo_stateindrive);
 DriveInfoExistPanel.Show;
 end
 else
 begin
 DiscImg.Picture.LoadFromFile(GetCDEPath + 'images\not_in_drive.png');
 DiscStateHint.Caption:= GetLocalizedString(cdeg_discinfo_statenotindrive);
 DriveInfoNotExistPanel.Show;
 end;
EDiscLbl.Caption:= FormatStrUDI(GetLocalizedString(cdeg_discinfo_discletter), DriveInfo);
NDiscLbl.Caption:= EDiscLbl.Caption;
//Hints
VolumeNameImg.Hint:= GetLocalizedString(cdeg_discinfo_volumename_hint);
FileSystemImg.Hint:= GetLocalizedString(cdeg_discinfo_filesystem_hint);
DiscSerialImg.Hint:= GetLocalizedString(cdeg_discinfo_discserial_hint);
CreateLinkBtn.Hint:= GetLocalizedString(cdeg_discinfo_createlink_hint);
BrowseDiscBtn.Hint:= GetLocalizedString(cdeg_discinfo_discbrowse_hint);
EjectDiscBtn.Hint:= GetLocalizedString(cdeg_discinfo_disceject_hint);
RefrashInfoBtn.Hint:= GetLocalizedString(cdeg_discinfo_refrash_hint);
//CloseDisc
//DiscSize
//DiscBusy
//DiscFree
if (GetDiscState(Drive) = dsUnFormatted) then
 begin
 VolumeNameLbl.Caption:= GetLocalizedString(cdeg_discinfo_discunformatted);
 FileSystemLbl.Caption:= GetLocalizedString(cdeg_discinfo_discunformatted);
 DiscSerialImg.Visible:= False;
 DiscSerialLbl.Caption:= '';
 DiscSizeImg.Visible:= False;
 DiscSizeLbl.Caption:= '';
 DiscBusyImg.Visible:= False;
 DiscBusyLbl.Caption:= '';
 DiscFreeImg.Visible:= False;
 DiscFreeLbl.Caption:= '';
 DiscSizePercentBar.Visible:= False;
 end
 else
 begin
 VolumeNameLbl.Caption:= FormatStrUDI(GetLocalizedString(cdeg_discinfo_volumename), DriveInfo);
 FileSystemLbl.Caption:= FormatStrUDI(GetLocalizedString(cdeg_discinfo_filesystem), DriveInfo);
 DiscSerialImg.Visible:= True;
 DiscSerialLbl.Caption:= FormatStrUDI(GetLocalizedString(cdeg_discinfo_discserial), DriveInfo);
 end;
VolumeNameLbl.Hint:= FormatStr(VolumeNameLbl.Caption, ['&'], ['']);
NoDiscInfoLbl.Caption:= '';
end;
end.
