{%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
osCommerce Product Manager for Windows (oscpmwin).
Copyright ©2003-2006 by Mario A. Valdez-Ramirez.

You can contact Mario A. Valdez-Ramirez
by email at mario@mariovaldez.org or paper mail at
Olmos 809, San Nicolas, NL. 66495, Mexico.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%}
unit askupload;

interface

uses
  SysUtils, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls, Controls, Classes;

type
  Topm_Form_UploadImage = class(TForm)
    opm_BitBtn_UploadOk: TBitBtn;
    opm_BitBtn_UploadCancel: TBitBtn;
    opm_BitBtn_UploadSelectFile: TBitBtn;
    opm_Edit_UploadFileName: TEdit;
    opm_Edit_UploadNewName: TEdit;
    opm_label_UploadNewName: TLabel;
    opm_RadioGroup_UploadFormat: TRadioGroup;
    opm_ComboBox_UploadDir: TComboBox;
    opm_label_UploadDir: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure opm_BitBtn_UploadSelectFileClick(Sender: TObject);
    procedure opm_BitBtn_UploadOkClick(Sender: TObject);
    procedure opm_RadioGroup_UploadFormatClick(Sender: TObject);
  private
    FUNCTION PRopm_FixNewName (ImageName : STRING; JPEGWanted : BOOLEAN) : STRING;
  public
    { Public declarations }
  end;


VAR
  opm_Form_UploadImage : Topm_Form_UploadImage;
  opm_Selected_FileName : STRING;


implementation

{$R *.dfm}


Uses gnugettext, dataman, imageman, openpicdlg;


{%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%}
{%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%}
procedure Topm_Form_UploadImage.FormCreate(Sender: TObject);
begin
  opm_Selected_FileName := '';
  TranslateComponent (self);
end;

{%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%}
{%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%}
procedure Topm_Form_UploadImage.FormShow(Sender: TObject);
begin
  opm_Form_UploadImage.Caption := _('Upload product image');
  opm_RadioGroup_UploadFormat.ItemIndex := 0;
  opm_Edit_UploadFileName.Text := '';
  opm_Edit_UploadNewName.Text := '';
  opm_Selected_FileName := '';
end;

{%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%}
{%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%}
procedure Topm_Form_UploadImage.opm_BitBtn_UploadSelectFileClick (Sender: TObject);
VAR
  OpenDialog : opmT_OpenPictureDialog;
BEGIN
  TRY
    OpenDialog := opmT_OpenPictureDialog.Create (opm_Form_UploadImage);
    TRY
      OpenDialog.DefaultExt := opmC_Def_UploadExt;
      OpenDialog.FileName := opmC_Def_UploadFilename;
      OpenDialog.Filter := _('Image files') + '|' + opmC_Def_UploadFilter + '|' + _('All files (*.*)');
      OpenDialog.Title := _('Uploading image file...');
      IF (OpenDialog.Execute = TRUE) THEN
        BEGIN
          opm_Selected_FileName := OpenDialog.FileName;
          opm_Edit_UploadFileName.Text := opm_Selected_FileName;
          opm_Edit_UploadNewName.Text := PRopm_FixNewName (FNopm_CleanUploadName (ExtractFileName (opm_Selected_FileName)), (opm_RadioGroup_UploadFormat.ItemIndex = 0));
        END
      ELSE
        BEGIN
          opm_Selected_FileName := '';
          opm_Edit_UploadFileName.Text := '';
          opm_Edit_UploadNewName.Text := '';
        END;
    FINALLY
      OpenDialog.Free;
    END;
  EXCEPT
    opm_Selected_FileName := '';
    opm_Edit_UploadFileName.Text := '';
    opm_Edit_UploadNewName.Text := '';
  END;
END;


{%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%}
{%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%}
FUNCTION Topm_Form_UploadImage.PRopm_FixNewName (ImageName : STRING; JPEGWanted : BOOLEAN) : STRING;
BEGIN
  IF (JPEGWanted = TRUE) THEN
    BEGIN
      IF ((FNopm_ImageNameIsJPEG (ImageName) = FALSE) AND (ImageName <> '')) THEN
        BEGIN
          ImageName := COPY (ImageName, 1, LENGTH (ImageName) - LENGTH (ExtractFileExt (ImageName)));
          ImageName := ImageName + '.jpg';
        END;
    END
  ELSE
    BEGIN
      IF ((FNopm_ImageNameIsPNG (ImageName) = FALSE)  AND (ImageName <> '')) THEN
        BEGIN
          ImageName := COPY (ImageName, 1, LENGTH (ImageName) - LENGTH (ExtractFileExt (ImageName)));
          ImageName := ImageName + '.png';
        END;
    END;
  PRopm_FixNewName := ImageName;
END;


{%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%}
{%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%}
procedure Topm_Form_UploadImage.opm_BitBtn_UploadOkClick(Sender: TObject);
VAR
  TmpStr : STRING;
begin
  TmpStr := FNopm_CleanUploadName (ExtractFileName (opm_Edit_UploadNewName.Text));
  IF (TmpStr = '') THEN
    TmpStr := FNopm_CleanUploadName (ExtractFileName (opm_Selected_FileName));
    opm_Edit_UploadNewName.Text := PRopm_FixNewName (TmpStr, (opm_RadioGroup_UploadFormat.ItemIndex = 0))
end;

{%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%}
{%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%}
procedure Topm_Form_UploadImage.opm_RadioGroup_UploadFormatClick (Sender: TObject);
VAR
  TmpStr : STRING;
begin
  TmpStr := FNopm_CleanUploadName (ExtractFileName (opm_Edit_UploadNewName.Text));
  IF (TmpStr = '') THEN
    TmpStr := FNopm_CleanUploadName (ExtractFileName (opm_Selected_FileName));
    opm_Edit_UploadNewName.Text := PRopm_FixNewName (TmpStr, (opm_RadioGroup_UploadFormat.ItemIndex = 0))
end;





end.
