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;
    DriveWatcher: TTimer;
    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 DiscBusyImgPaint (Sender: TObject );
    procedure DiscFreeImgPaint (Sender: TObject );
    procedure DriveWatcherTimer (Sender: TObject );
    procedure EjectDiscBtnClick(Sender: TObject);
    procedure RefrashInfoBtnClick(Sender: TObject);
    procedure CreateFrm;
  private
   BusyColor, FreeColor: TRGBColor;
  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.DiscBusyImgPaint (Sender: TObject );
begin
DiscBusyImg.Canvas.Brush.Color:= RGB2Color(BusyColor);
DiscBusyImg.Canvas.FillRect(DiscBusyImg.Canvas.ClipRect);
end;
procedure Tcde_disc_frm.DiscFreeImgPaint (Sender: TObject );
begin
DiscFreeImg.Canvas.Brush.Color:= RGB2Color(FreeColor);
DiscFreeImg.Canvas.FillRect(DiscFreeImg.Canvas.ClipRect);
end;
procedure Tcde_disc_frm.DriveWatcherTimer (Sender: TObject );
begin
do_Refrash;
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;
begin
DriveInfoExistPanel.Hide;
DriveInfoNotExistPanel.Hide;
DiscBusyImg.Refresh;
DiscFreeImg.Refresh;
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);
CloseDiscBtn.Hint:= GetLocalizedString(cdeg_discinfo_discclose_hint);
DiscSizeImg.Hint:= GetLocalizedString(cdeg_discinfo_discsize_hint);
DiscBusyImg.Hint:= GetLocalizedString(cdeg_discinfo_discbuzy_hint);
DiscFreeImg.Hint:= GetLocalizedString(cdeg_discinfo_discfree_hint);
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);
 DiscSizeImg.Visible:= True;
 DiscSizeLbl.Caption:= FormatStrUDI(GetLocalizedString(cdeg_discinfo_discsize), DriveInfo);
 DiscBusyImg.Visible:= True;
 DiscBusyLbl.Caption:= FormatStrUDI(GetLocalizedString(cdeg_discinfo_discbuzy), DriveInfo);
 DiscFreeImg.Visible:= True;
 DiscFreeLbl.Caption:= FormatStrUDI(GetLocalizedString(cdeg_discinfo_discfree), DriveInfo);
 DiscSizePercentBar.Visible:= True;
 DiscSizePercentBar.Value:= 100 - round((DriveInfo.diDiscFree * 100) / DriveInfo.diDiscSize);
 end;
VolumeNameLbl.Hint:= FormatStr(VolumeNameLbl.Caption, ['&'], ['']);
NoDiscInfoLbl.Caption:= '';
end;
procedure Tcde_disc_frm.CreateFrm;
begin
//Colors
with BusyColor do
 begin
 R:= 0;
 G:= 0;
 B:= 0;
 end;
BusyColor:= JSReadRGBColor('/interface/ds_busycolor', BusyColor, GetCDEPath + 'configs\configs.json');
with FreeColor do
 begin
 R:= 192;
 G:= 192;
 B:= 192;
 end;
FreeColor:= JSReadRGBColor('/interface/ds_freecolor', FreeColor, GetCDEPath + 'configs\configs.json');
with DiscSizePercentBar do
 begin
 LineBkgColor:= RGB2Color(FreeColor);
 LineColor:= RGB2Color(BusyColor);
 end;
//Drive Watcher
with DriveWatcher do
 begin
 Enabled:= isDriveWatcherEnabled(Drive);
 Interval:= JSReadInteger('/drivewatcher/interval', 2000, GetCDEPath + 'configs\configs.json');
 end;
end;
end.
