Bläddra i källkod

Reusing TAutoBatch in trivial cases

Source commit: e5f2b9b75a50c7bbaf192b1d238355c4526d4eac
Martin Prikryl 7 år sedan
förälder
incheckning
6b01db6311
1 ändrade filer med 209 tillägg och 225 borttagningar
  1. 209 225
      source/forms/CustomScpExplorer.cpp

+ 209 - 225
source/forms/CustomScpExplorer.cpp

@@ -2476,280 +2476,272 @@ void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
 bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
   TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
 {
-  void * BatchStorage;
-  BatchStart(BatchStorage);
+  TAutoBatch AutoBatch(this);
   bool Result;
-  try
+  if ((Operation == foCopy) || (Operation == foMove))
   {
-    if ((Operation == foCopy) || (Operation == foMove))
+    TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
+    TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
+    UnicodeString TargetDirectory;
+    bool Temp = false;
+    bool DragDrop = false;
+    int Options = 0;
+    TAutoSwitch UseQueue = asAuto;
+    if (Param != NULL)
     {
-      TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
-      TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
-      UnicodeString TargetDirectory;
-      bool Temp = false;
-      bool DragDrop = false;
-      int Options = 0;
-      TAutoSwitch UseQueue = asAuto;
-      if (Param != NULL)
-      {
-        TTransferOperationParam& TParam =
-          *static_cast<TTransferOperationParam*>(Param);
-        TargetDirectory = TParam.TargetDirectory;
-        Temp = TParam.Temp;
-        DragDrop = TParam.DragDrop;
-        Options = TParam.Options;
-        UseQueue = TParam.Queue;
-      }
-      TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
-      switch (UseQueue)
-      {
-        case asOn:
-          CopyParam.Queue = true;
-          break;
+      TTransferOperationParam& TParam =
+        *static_cast<TTransferOperationParam*>(Param);
+      TargetDirectory = TParam.TargetDirectory;
+      Temp = TParam.Temp;
+      DragDrop = TParam.DragDrop;
+      Options = TParam.Options;
+      UseQueue = TParam.Queue;
+    }
+    TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
+    switch (UseQueue)
+    {
+      case asOn:
+        CopyParam.Queue = true;
+        break;
 
-        case asOff:
-          CopyParam.Queue = false;
-          break;
+      case asOff:
+        CopyParam.Queue = false;
+        break;
 
-        case asAuto:
-        default:
-          // keep default
-          break;
-      }
-      Result =
-        CopyParamDialog(Direction, Type, Temp, FileList, TargetDirectory,
-          CopyParam, !NoConfirmation, DragDrop, Options);
-      if (Result)
+      case asAuto:
+      default:
+        // keep default
+        break;
+    }
+    Result =
+      CopyParamDialog(Direction, Type, Temp, FileList, TargetDirectory,
+        CopyParam, !NoConfirmation, DragDrop, Options);
+    if (Result)
+    {
+      DebugAssert(Terminal);
+      bool SelectionRestored = false;
+      TCustomDirView * DView = NULL;
+      if (HasDirView[Side])
       {
-        DebugAssert(Terminal);
-        bool SelectionRestored = false;
-        TCustomDirView * DView = NULL;
-        if (HasDirView[Side])
-        {
-          DView = DirView(Side);
-          DView->SaveSelection();
-          DView->SaveSelectedNames();
-        }
+        DView = DirView(Side);
+        DView->SaveSelection();
+        DView->SaveSelectedNames();
+      }
+
+      UpdateCopyParamCounters(CopyParam);
 
-        UpdateCopyParamCounters(CopyParam);
+      std::unique_ptr<TStringList> TransferResumeList(new TStringList());
+      DebugAssert(FTransferResumeList == NULL);
+      FTransferResumeList =
+        Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
+      FMoveToQueue = false;
 
-        std::unique_ptr<TStringList> TransferResumeList(new TStringList());
-        DebugAssert(FTransferResumeList == NULL);
-        FTransferResumeList =
-          Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
-        FMoveToQueue = false;
+      int Params = FLAGMASK(Operation == foMove, cpDelete);
 
-        int Params = FLAGMASK(Operation == foMove, cpDelete);
+      try
+      {
+        TStrings * PermanentFileList;
+        std::unique_ptr<TStrings> PermanentFileListOwner;
 
         try
         {
-          TStrings * PermanentFileList;
-          std::unique_ptr<TStrings> PermanentFileListOwner;
+          if (Side == osLocal)
+          {
+            PermanentFileList = FileList;
 
-          try
+            Params |= FLAGMASK(Temp, cpTemporary);
+            Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params, NULL);
+            if (Operation == foMove)
+            {
+              ReloadLocalDirectory();
+              if (DView != NULL)
+              {
+                DView->RestoreSelection();
+              }
+              SelectionRestored = true;
+            }
+          }
+          else
           {
-            if (Side == osLocal)
+            // Clone the file list as it may refer to current directory files,
+            // which get destroyed, when the source directory is reloaded after foMove operation.
+            // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
+            // But for a hotfix, we are not going to do such a big change.
+            PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
+            PermanentFileList = PermanentFileListOwner.get();
+
+            try
+            {
+              Terminal->CopyToLocal(
+                FileList, TargetDirectory, &CopyParam, Params, NULL);
+            }
+            __finally
             {
-              PermanentFileList = FileList;
-
-              Params |= FLAGMASK(Temp, cpTemporary);
-              Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params, NULL);
               if (Operation == foMove)
               {
-                ReloadLocalDirectory();
                 if (DView != NULL)
                 {
                   DView->RestoreSelection();
                 }
                 SelectionRestored = true;
               }
-            }
-            else
-            {
-              // Clone the file list as it may refer to current directory files,
-              // which get destroyed, when the source directory is reloaded after foMove operation.
-              // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
-              // But for a hotfix, we are not going to do such a big change.
-              PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
-              PermanentFileList = PermanentFileListOwner.get();
-
-              try
-              {
-                Terminal->CopyToLocal(
-                  FileList, TargetDirectory, &CopyParam, Params, NULL);
-              }
-              __finally
-              {
-                if (Operation == foMove)
-                {
-                  if (DView != NULL)
-                  {
-                    DView->RestoreSelection();
-                  }
-                  SelectionRestored = true;
-                }
-                ReloadLocalDirectory(TargetDirectory);
-              }
+              ReloadLocalDirectory(TargetDirectory);
             }
           }
-          catch (EAbort &)
+        }
+        catch (EAbort &)
+        {
+          if (FMoveToQueue)
           {
-            if (FMoveToQueue)
-            {
-              Params |=
-                (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
-
-              DebugAssert(CopyParam.TransferSkipList == NULL);
-              DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
-              if (TransferResumeList->Count > 0)
-              {
-                CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
-                TransferResumeList->Delete(TransferResumeList->Count - 1);
-              }
+            Params |=
+              (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
 
-              CopyParam.TransferSkipList = TransferResumeList.release();
+            DebugAssert(CopyParam.TransferSkipList == NULL);
+            DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
+            if (TransferResumeList->Count > 0)
+            {
+              CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
+              TransferResumeList->Delete(TransferResumeList->Count - 1);
+            }
 
-              // not really needed, just to keep it consistent with TransferResumeList
-              FTransferResumeList = NULL;
-              FMoveToQueue = false;
+            CopyParam.TransferSkipList = TransferResumeList.release();
 
-              Configuration->Usage->Inc("MovesToBackground");
+            // not really needed, just to keep it consistent with TransferResumeList
+            FTransferResumeList = NULL;
+            FMoveToQueue = false;
 
-              AddQueueItem(Queue, Direction, PermanentFileList, TargetDirectory, CopyParam, Params);
-              ClearTransferSourceSelection(Direction);
-            }
+            Configuration->Usage->Inc("MovesToBackground");
 
-            throw;
+            AddQueueItem(Queue, Direction, PermanentFileList, TargetDirectory, CopyParam, Params);
+            ClearTransferSourceSelection(Direction);
           }
+
+          throw;
         }
-        __finally
+      }
+      __finally
+      {
+        if (!SelectionRestored && (DView != NULL))
         {
-          if (!SelectionRestored && (DView != NULL))
-          {
-            DView->DiscardSavedSelection();
-          }
-          FTransferResumeList = NULL;
+          DView->DiscardSavedSelection();
         }
+        FTransferResumeList = NULL;
       }
     }
-    else if (Operation == foRename)
+  }
+  else if (Operation == foRename)
+  {
+    DebugAssert(DirView(Side)->ItemFocused);
+    DirView(Side)->ItemFocused->EditCaption();
+    Result = true;
+  }
+  else if (Operation == foDelete)
+  {
+    DebugAssert(FileList->Count);
+    // We deliberately do not toggle alternative flag (Param), but use OR,
+    // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
+    // shortcut of CurrentDeleteAlternativeAction
+    bool Alternative =
+      bool(Param) || UseAlternativeFunction();
+    bool Recycle;
+    if (Side == osLocal)
     {
-      DebugAssert(DirView(Side)->ItemFocused);
-      DirView(Side)->ItemFocused->EditCaption();
-      Result = true;
+      Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
     }
-    else if (Operation == foDelete)
+    else
     {
-      DebugAssert(FileList->Count);
-      // We deliberately do not toggle alternative flag (Param), but use OR,
-      // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
-      // shortcut of CurrentDeleteAlternativeAction
-      bool Alternative =
-        bool(Param) || UseAlternativeFunction();
-      bool Recycle;
-      if (Side == osLocal)
-      {
-        Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
-      }
-      else
-      {
-        Recycle = (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
-          !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
-          !Terminal->IsRecycledFile(FileList->Strings[0]);
-      }
+      Recycle = (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
+        !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
+        !Terminal->IsRecycledFile(FileList->Strings[0]);
+    }
 
-      Result =
-        !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
-      if (!Result)
+    Result =
+      !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
+    if (!Result)
+    {
+      UnicodeString Query;
+      if (FileList->Count == 1)
       {
-        UnicodeString Query;
-        if (FileList->Count == 1)
+        if (Side == osLocal)
         {
-          if (Side == osLocal)
-          {
-            Query = ExtractFileName(FileList->Strings[0]);
-          }
-          else
-          {
-            Query = UnixExtractFileName(FileList->Strings[0]);
-          }
-          Query = FMTLOAD(
-            (Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
+          Query = ExtractFileName(FileList->Strings[0]);
         }
         else
         {
-          Query = FMTLOAD(
-            (Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
+          Query = UnixExtractFileName(FileList->Strings[0]);
         }
+        Query = FMTLOAD(
+          (Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
+      }
+      else
+      {
+        Query = FMTLOAD(
+          (Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
+      }
 
-        TMessageParams Params(mpNeverAskAgainCheck);
-        Params.ImageName = L"Delete file";
-        unsigned int Answer = MessageDialog(MainInstructions(Query), qtConfirmation,
-          qaOK | qaCancel, HELP_DELETE_FILE, &Params);
-        if (Answer == qaNeverAskAgain)
+      TMessageParams Params(mpNeverAskAgainCheck);
+      Params.ImageName = L"Delete file";
+      unsigned int Answer = MessageDialog(MainInstructions(Query), qtConfirmation,
+        qaOK | qaCancel, HELP_DELETE_FILE, &Params);
+      if (Answer == qaNeverAskAgain)
+      {
+        Result = true;
+        if (Recycle)
         {
-          Result = true;
-          if (Recycle)
-          {
-            WinConfiguration->ConfirmRecycling = false;
-          }
-          else
-          {
-            WinConfiguration->ConfirmDeleting = false;
-          }
+          WinConfiguration->ConfirmRecycling = false;
         }
         else
         {
-          Result = (Answer == qaOK);
+          WinConfiguration->ConfirmDeleting = false;
         }
       }
-
-      if (Result)
+      else
       {
-        DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
+        Result = (Answer == qaOK);
       }
     }
-    else if (Operation == foSetProperties)
-    {
-      RemoteDirView->SaveSelectedNames();
-      Result = SetProperties(Side, FileList);
-    }
-    else if (Operation == foCustomCommand)
-    {
-      DebugAssert(Param);
-      DebugAssert(Side == osRemote);
 
-      RemoteDirView->SaveSelectedNames();
-      const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
-      CustomCommand(FileList, *Command, NULL);
-      Result = true;
-    }
-    else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
-    {
-      DebugAssert(Side == osRemote);
-      Result = RemoteTransferFiles(FileList, NoConfirmation,
-        (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
-    }
-    else if (Operation == foLock)
-    {
-      DebugAssert(Side == osRemote);
-      LockFiles(FileList, true);
-      Result = true;
-    }
-    else if (Operation == foUnlock)
-    {
-      DebugAssert(Side == osRemote);
-      LockFiles(FileList, false);
-      Result = true;
-    }
-    else
+    if (Result)
     {
-      DebugFail();
+      DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
     }
   }
-  __finally
+  else if (Operation == foSetProperties)
   {
-    BatchEnd(BatchStorage);
+    RemoteDirView->SaveSelectedNames();
+    Result = SetProperties(Side, FileList);
+  }
+  else if (Operation == foCustomCommand)
+  {
+    DebugAssert(Param);
+    DebugAssert(Side == osRemote);
+
+    RemoteDirView->SaveSelectedNames();
+    const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
+    CustomCommand(FileList, *Command, NULL);
+    Result = true;
+  }
+  else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
+  {
+    DebugAssert(Side == osRemote);
+    Result = RemoteTransferFiles(FileList, NoConfirmation,
+      (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
+  }
+  else if (Operation == foLock)
+  {
+    DebugAssert(Side == osRemote);
+    LockFiles(FileList, true);
+    Result = true;
+  }
+  else if (Operation == foUnlock)
+  {
+    DebugAssert(Side == osRemote);
+    LockFiles(FileList, false);
+    Result = true;
+  }
+  else
+  {
+    DebugFail();
   }
   return Result;
 }
@@ -7418,20 +7410,12 @@ void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
 void __fastcall TCustomScpExplorerForm::DDDownload(
   TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
 {
-  void * BatchStorage;
-  BatchStart(BatchStorage);
-  try
-  {
-    UpdateCopyParamCounters(*CopyParam);
-    Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
-    if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
-    {
-      RemoteDriveView->UpdateDropSource();
-    }
-  }
-  __finally
+  TAutoBatch AutoBatch(this);
+  UpdateCopyParamCounters(*CopyParam);
+  Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
+  if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
   {
-    BatchEnd(BatchStorage);
+    RemoteDriveView->UpdateDropSource();
   }
 }
 //---------------------------------------------------------------------------