Browse Source

Bug 1396: Local custom commands for local files + Change: Remote custom commands cannot be executed with focus on local panel

https://winscp.net/tracker/1396

Source commit: 540d487ffe6db8a81dd9af3c552a8b5614a15197
Martin Prikryl 9 years ago
parent
commit
8e37e22329

+ 6 - 2
source/forms/CustomCommand.cpp

@@ -95,13 +95,15 @@ __fastcall TCustomCommandDialog::TCustomCommandDialog(TComponent* Owner,
     InitializeShortCutCombo(ShortCutCombo, *ShortCuts);
   }
 
-  EnableControl(RemoteCommandButton, FLAGCLEAR(Options, ccoDisableRemote));
+  FOptions = Options;
 
   UpdateControls();
 }
 //---------------------------------------------------------------------------
 void __fastcall TCustomCommandDialog::UpdateControls()
 {
+  EnableControl(RemoteCommandButton, FLAGCLEAR(FOptions, ccoDisableRemote));
+
   UnicodeString Command = CommandEdit->Text;
   EnableControl(OkButton, !Command.IsEmpty() && !DescriptionEdit->Text.IsEmpty());
 
@@ -135,7 +137,9 @@ void __fastcall TCustomCommandDialog::UpdateControls()
   EnableControl(ApplyToDirectoriesCheck, AllowApplyToDirectories);
   EnableControl(ShowResultsCheck, RemoteCommand);
   EnableControl(CopyResultsCheck, RemoteCommand);
-  EnableControl(RemoteFilesCheck, AllowRemoteFiles && (!RecursiveCheck->Enabled || !RecursiveCheck->Checked));
+  EnableControl(RemoteFilesCheck,
+    FLAGCLEAR(FOptions, ccoDisableRemoteFiles) && AllowRemoteFiles &&
+    (!RecursiveCheck->Enabled || !RecursiveCheck->Checked));
 }
 //---------------------------------------------------------------------------
 void __fastcall TCustomCommandDialog::SetParams(int value)

+ 1 - 0
source/forms/CustomCommand.h

@@ -46,6 +46,7 @@ private:
   UnicodeString FOrigDescription;
   const TCustomCommandList * FCustomCommandList;
   TCustomCommandValidate FOnValidate;
+  int FOptions;
 
   void __fastcall SetParams(int value);
   int __fastcall GetParams();

+ 79 - 6
source/forms/CustomScpExplorer.cpp

@@ -1627,7 +1627,7 @@ int __fastcall TCustomScpExplorerForm::CustomCommandState(
     else
     {
       // other local custom commands can be executed only on remote side
-      Result = (FCurrentSide == osRemote) && EnableSelectedOperation[osRemote];
+      Result = EnableSelectedOperation[FCurrentSide];
     }
   }
 
@@ -1700,21 +1700,22 @@ void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
 
     UnicodeString Command = InteractiveCustomCommand.Complete(ACommand.Command, false);
 
+    bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
+    bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
+
     Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
 
     if (!LocalCustomCommand.IsFileCommand(Command))
     {
       ExecuteShellAndWait(LocalCustomCommand.Complete(Command, true));
     }
-    else
+    // remote files?
+    else if ((FCurrentSide == osRemote) || LocalFileCommand)
     {
       TStrings * LocalFileList = NULL;
       TStrings * RemoteFileList = NULL;
       try
       {
-        bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
-        bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
-
         if (LocalFileCommand)
         {
           if (ALocalFileList == NULL)
@@ -1925,6 +1926,75 @@ void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
         }
       }
     }
+    // local files
+    else
+    {
+      std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
+
+      std::unique_ptr<TStrings> LocalFileList(new TStringList());
+
+      for (int Index = 0; Index < SelectedFileList->Count; Index++)
+      {
+        UnicodeString FileName = SelectedFileList->Strings[Index];
+        if (DirectoryExists(FileName))
+        {
+          if (FLAGSET(ACommand.Params, ccApplyToDirectories))
+          {
+            LocalFileList->Add(FileName);
+          }
+
+          if (FLAGSET(ACommand.Params, ccRecursive))
+          {
+            TMakeLocalFileListParams MakeFileListParam;
+            MakeFileListParam.FileList = LocalFileList.get();
+            MakeFileListParam.FileTimes = NULL;
+            MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
+            MakeFileListParam.Recursive = true;
+
+            ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
+          }
+        }
+        else
+        {
+          LocalFileList->Add(FileName);
+        }
+      }
+
+      TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
+
+      Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : LocalFileList->Count);
+      DebugAssert(FProgressForm != NULL);
+      FProgressForm->ReadOnly = true;
+
+      try
+      {
+        if (FileListCommand)
+        {
+          UnicodeString FileList = MakeFileList(LocalFileList.get());
+          TCustomCommandData Data(FTerminal);
+          TLocalCustomCommand CustomCommand(
+            Data, Terminal->CurrentDirectory,
+            L"", L"", FileList);
+          ExecuteShellAndWait(CustomCommand.Complete(Command, true));
+        }
+        else
+        {
+          for (int Index = 0; Index < LocalFileList->Count; Index++)
+          {
+            UnicodeString FileName = LocalFileList->Strings[Index];
+            TCustomCommandData Data(FTerminal);
+            TLocalCustomCommand CustomCommand(
+              Data, Terminal->CurrentDirectory,
+              FileName, L"", L"");
+            ExecuteShellAndWait(CustomCommand.Complete(Command, true));
+          }
+        }
+      }
+      __finally
+      {
+        Progress.Stop();
+      }
+    }
   }
 }
 //---------------------------------------------------------------------------
@@ -7732,7 +7802,10 @@ void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
   }
   Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
   FEditingFocusedAdHocCommand = OnFocused;
-  int Options = FLAGMASK(!RemoteAllowed, ccoDisableRemote);
+  bool LocalSide = (FCurrentSide == osLocal);
+  int Options =
+    FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
+    FLAGMASK(LocalSide, ccoDisableRemoteFiles);
   if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
        ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
   {

+ 1 - 0
source/windows/WinInterface.h

@@ -239,6 +239,7 @@ class TCustomCommandType;
 class TShortCuts;
 enum TCustomCommandsMode { ccmAdd, ccmEdit, ccmAdHoc };
 const ccoDisableRemote = 0x01;
+const ccoDisableRemoteFiles = 0x02;
 typedef void __fastcall (__closure *TCustomCommandValidate)
   (const TCustomCommandType & Command);
 bool __fastcall DoCustomCommandDialog(TCustomCommandType & Command,