unit U_MakeEditGuideFile;

interface

uses
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, RzPanel, RzButton, StdCtrls, RzEdit, Mask,

  U_BaseCommon, U_BaseConstants, kbmMemTable, siComp, siLngLnk, pFIBDataSet,
  Buttons, U_ProtectionCommon;

type
  TF_MakeEditGuideFile = class(TForm)
    RzGroupBox1: TRzGroupBox;
    gbOkCancel: TRzGroupBox;
    btOk: TRzBitBtn;
    btCancel: TRzBitBtn;
    edName: TRzEdit;
    meDescription: TRzMemo;
    Label1: TLabel;
    Label2: TLabel;
    lng_Forms: TsiLangLinked;
    btLoadFile: TSpeedButton;
    btSaveFile: TSpeedButton;
    lbType: TLabel;
    edType: TRzEdit;
    procedure gbOkCancelResize(Sender: TObject);
    procedure btLoadFileClick(Sender: TObject);
    procedure btSaveFileClick(Sender: TObject);
    procedure btOkClick(Sender: TObject);
  private
    GForm: TForm;
    FObjectID: Integer;

    FFileContent: TMemoryStream;
    FFileContentPacked: Boolean;
    FFileExists: Boolean;

    FFileExt: String;
    FType: Integer;

    procedure SetParamsToDialog(ADialog: TOpenDialog; const AFileExt: String);
    procedure ShowFileInfo;
  public
    NewID: Integer;

    constructor Create(AOwner: TComponent; AForm: TForm);
    destructor  Destroy; override;

    function Execute(AMakeEdit: TMakeEdit; AFType: Integer): Boolean;
  end;

  function MakeEditGuideFileExecute(AMakeEdit: TMakeEdit; AFType: Integer): Boolean;

var
  F_MakeEditGuideFile: TF_MakeEditGuideFile;

implementation
Uses U_Main, Unit_DM_SCS, DB;
{$R *.dfm}

{ TF_MakeEditGuideFile }

constructor TF_MakeEditGuideFile.Create(AOwner: TComponent; AForm: TForm);
begin
  GForm := AForm;
  inherited Create(AOwner);
end;

destructor TF_MakeEditGuideFile.Destroy;
begin

  inherited;
end;

function TF_MakeEditGuideFile.Execute(AMakeEdit: TMakeEdit; AFType: Integer): Boolean;
var
  Producer: TProducer;
  ObjectID: Integer;
  DataSet: TpFIBDataSet;
begin
  Result := false;
  try
    Caption := '';
    NewID := 0;
    ObjectID := 0;

    FFileExt := '';
    FType := AFType;

    FFileContent := TMemoryStream.Create;
    FFileContentPacked := false;
    FFileExists := false;

    DataSet := nil;
    case AFType of
      gftCompSpecification:
        DataSet := TF_Main(GForm).DM.dsetCompSpecifications;
    end;

    case AMakeEdit of
      meMake:
        begin
          case AFType of
            gftCompSpecification:
              Caption := cMakeEditGuideFile_Msg1_1;
          end;
          edName.Text := '';
          meDescription.Lines.Clear;
        end;
      meEdit:
        begin
          case AFType of
            gftCompSpecification:
              Caption := cMakeEditGuideFile_Msg1_2;
          end;

          ObjectID := DataSet.FN(fnID).AsInteger;
          edName.Text := DataSet.FN(fnFileName).AsString;
          FFileExt := DataSet.FN(fnFileExt).AsString;
          BlobFieldToStrings(TBlobField(DataSet.FN(fnDescription)), meDescription.Lines);

          TBlobField(DataSet.FN(fnContent)).SaveToStream(FFileContent);
          FFileContent.Position := 0;
          FFileContentPacked := True;
          FFileExists := true;
        end;
    end;
    FObjectID := ObjectID;

    ShowFileInfo;

    gbOkCancelResize(gbOkCancel);
    if ShowModal = mrOk then
    begin
      Result := true;
      with TF_Main(GForm).DM do
      begin
        NewID := SaveGuideFile(ObjectID, AFType, edName.Text, FFileExt, meDescription.Lines, FFileContent, FFileContentPacked);
        if AMakeEdit = meEdit then
          DataSet.ReopenLocate(fnID);
      end;
    end;

    FFileContent.Free;
  except
    on E: Exception do AddExceptionToLog('TF_MakeEditGuideFile.Execute: '+E.Message);
  end;
end;

procedure TF_MakeEditGuideFile.gbOkCancelResize(Sender: TObject);
begin
  SetMiddleControlChilds(TControl(Sender), TControl(Self));
end;

procedure TF_MakeEditGuideFile.SetParamsToDialog(ADialog: TOpenDialog; const AFileExt: String);
begin
  case FType of
    gftCompSpecification:
      begin
        //ADialog.DefaultExt := '*.'+enDoc;
        //ADialog.Filter := GetDialogFilter(cexdDoc, enDoc);

        ADialog.DefaultExt := '*'+AFileExt;
        ADialog.Filter := GetDialogFilter(GetExtensionDescription(AFileExt), AFileExt);
      end;
  end;
  ADialog.InitialDir := '';
  ADialog.FileName := '';
  ADialog.Options := ADialog.Options - [ofNoChangeDir];
end;

procedure TF_MakeEditGuideFile.ShowFileInfo;
begin
  edType.Text := FFileExt;
end;

procedure TF_MakeEditGuideFile.btLoadFileClick(Sender: TObject);
var
  Dialog: TOpenDialog;
begin
  Dialog := TOpenDialog.Create(Self);
  Dialog.Title := cOpeningFile;
  SetParamsToDialog(Dialog, enDoc);
  Dialog.Options := Dialog.Options + [ofFileMustExist];

  if Dialog.Execute then
  begin
    Application.ProcessMessages;

    edName.Text := ExtractFileNameOnly(Dialog.FileName);
    FFileExt := ExtractFileExt(Dialog.FileName);

    FFileContent.Clear;
    FFileContent.LoadFromFile(Dialog.FileName);
    FFileContent.Position := 0;

    FFileContentPacked := false;
    FFileExists := true;

    ShowFileInfo;
  end;
  FreeAndNil(Dialog);
end;

procedure TF_MakeEditGuideFile.btSaveFileClick(Sender: TObject);
var
  Dialog: TSaveDialog;
  UnPackedStream: TMemoryStream;
begin
  if Not FFileExists then
    MessageInfo(cMakeEditGuideFile_Msg2)
  else
  begin
    Dialog := TSaveDialog.Create(Self);
    Dialog.Title := cSavingToFile;
    SetParamsToDialog(Dialog, FFileExt);
    Dialog.FileName := FileNameCorrect(edName.Text) + FFileExt;
    Dialog.Options := Dialog.Options + [ofOverwritePrompt];

    if Dialog.Execute then
    begin
      Application.ProcessMessages;
      try
        if FileExists(Dialog.FileName) then
          DeleteFile(Dialog.FileName);

        if FFileContentPacked then
        begin
          UnPackedStream := TMemoryStream.Create;
          UnPakStream(FFileContent, UnPackedStream);
          UnPackedStream.SaveToFile(Dialog.FileName);
          FreeAndNil(UnPackedStream);
        end
        else
        begin
          FFileContent.SaveToFile(Dialog.FileName);
        end;
      except
        on E: Exception do AddExceptionToLogExt(ClassName, MethodName(@TF_MakeEditGuideFile.btSaveFileClick), E.Message);
      end;
    end;
    FreeAndNil(Dialog);
  end;
end;

function MakeEditGuideFileExecute(AMakeEdit: TMakeEdit; AFType: Integer): Boolean;
begin
  if F_MakeEditGuideFile = nil then
    F_MakeEditGuideFile := TF_MakeEditGuideFile.Create(Application, F_NormBase);
  Result := F_MakeEditGuideFile.Execute(AMakeEdit, AFType);
end;

procedure TF_MakeEditGuideFile.btOkClick(Sender: TObject);
begin
  if Not FFileExists then
  begin
    MessageInfo(cMakeEditGuideFile_Msg2);
    ModalResult := mrNone;
  end;
end;

end.
