void __fastcall Download(TTerminal * Terminal, const UnicodeString FileName, int UseDefaults)
{
  TRemoteFile * File = NULL;

  try
  {
    Terminal->ExceptionOnFail = true;
    try
    {
      Terminal->ReadFile(FileName, File);
    }
    __finally
    {
      Terminal->ExceptionOnFail = false;
    }
    File->FullFileName = FileName;
    UnicodeString LocalDirectory = Terminal->SessionData->LocalDirectoryExpanded;
    if (LocalDirectory.IsEmpty())
    {
      LocalDirectory = GetPersonalFolder();
    }
    UnicodeString TargetDirectory = IncludeTrailingBackslash(LocalDirectory);

    TGUICopyParamType CopyParam = GUIConfiguration->DefaultCopyParam;
    UnicodeString DisplayName = File->FileName;

    bool CustomDisplayName =
      !File->DisplayName.IsEmpty() &&
      (File->DisplayName != DisplayName);
    if (CustomDisplayName)
    {
      DisplayName = File->DisplayName;
    }

    UnicodeString FriendyFileName = UnixIncludeTrailingBackslash(UnixExtractFilePath(FileName)) + DisplayName;
    std::unique_ptr<TStrings> FileListFriendly(new TStringList());
    FileListFriendly->AddObject(FriendyFileName, File);

    int Options = coDisableQueue;
    int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Download;
    if ((UseDefaults == 0) ||
        DoCopyDialog(false, false, FileListFriendly.get(), TargetDirectory, &CopyParam,
          Options, CopyParamAttrs, NULL, NULL, UseDefaults))
    {
      // Setting parameter overrides only now, otherwise the dialog would present the parametes as non-default

      if (CustomDisplayName)
      {
        // Set only now, so that it is not redundantly displayed on the copy dialog.
        // We should escape the * and ?'s.
        CopyParam.FileMask = DisplayName;
      }

      CopyParam.OnceDoneOperation = odoDisconnect;

      std::unique_ptr<TStrings> FileList(new TStringList());
      FileList->AddObject(FileName, File);
      Terminal->CopyToLocal(FileList.get(), TargetDirectory, &CopyParam, 0, NULL);
    }

    UnicodeString Directory = UnixExtractFilePath(FileName);
    Terminal->AutoReadDirectory = true;
    Terminal->ChangeDirectory(Directory);
  }
  __finally
  {
    delete File;
  }
}

void __fastcall Download(TTerminal * Terminal, const UnicodeString FileName, int UseDefaults)
{
  TRemoteFile * File = NULL;

  try
  {
    Terminal->ExceptionOnFail = true;
    try
    {
      Terminal->ReadFile(FileName, File);
    }
    __finally
    {
      Terminal->ExceptionOnFail = false;
    }
    File->FullFileName = FileName;
    UnicodeString LocalDirectory = Terminal->SessionData->LocalDirectoryExpanded;
    if (LocalDirectory.IsEmpty())
    {
      LocalDirectory = GetPersonalFolder();
    }
    UnicodeString TargetDirectory = IncludeTrailingBackslash(LocalDirectory);

    TGUICopyParamType CopyParam = GUIConfiguration->DefaultCopyParam;
    UnicodeString DisplayName = File->FileName;

    bool CustomDisplayName =
      !File->DisplayName.IsEmpty() &&
      (File->DisplayName != DisplayName);
    if (CustomDisplayName)
    {
      DisplayName = File->DisplayName;
    }

    UnicodeString FriendyFileName = UnixIncludeTrailingBackslash(UnixExtractFilePath(FileName)) + DisplayName;
    std::unique_ptr<TStrings> FileListFriendly(new TStringList());
    FileListFriendly->AddObject(FriendyFileName, File);

    int Options = coDisableQueue;
    int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Download;
    if ((UseDefaults == 0) ||
        DoCopyDialog(false, false, FileListFriendly.get(), TargetDirectory, &CopyParam,
          Options, CopyParamAttrs, NULL, NULL, UseDefaults))
    {
       

      if (CustomDisplayName)
      {
         
         
        CopyParam.FileMask = DisplayName;
      }

      CopyParam.OnceDoneOperation = odoDisconnect;

      std::unique_ptr<TStrings> FileList(new TStringList());
      FileList->AddObject(FileName, File);
      Terminal->CopyToLocal(FileList.get(), TargetDirectory, &CopyParam, 0, NULL);
    }

    UnicodeString Directory = UnixExtractFilePath(FileName);
    Terminal->AutoReadDirectory = true;
    Terminal->ChangeDirectory(Directory);
  }
  __finally
  {
    delete File;
  }
}
