Browse Source

Bug 2079: Allow opening all sites in a folder in PuTTY (3rd)

https://winscp.net/tracker/2079

Source commit: 6957e121b27634e5fb97df365ac98071339d0401
Martin Prikryl 3 years ago
parent
commit
ccd498f150
3 changed files with 70 additions and 35 deletions
  1. 2 0
      source/WinSCP.cpp
  2. 67 35
      source/windows/GUITools.cpp
  3. 1 0
      source/windows/GUITools.h

+ 2 - 0
source/WinSCP.cpp

@@ -13,6 +13,7 @@ USEFORM("forms\ScpExplorer.cpp", ScpExplorerForm);
 #include <VCLCommon.h>
 #include <Setup.h>
 #include <PuttyTools.h>
+#include <GUITools.h>
 //---------------------------------------------------------------------------
 WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
 {
@@ -53,6 +54,7 @@ WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
     }
     __finally
     {
+      GUIFinalize();
       FinalizeSystemSettings();
       FinalizeWinHelp();
       CoreFinalize();

+ 67 - 35
source/windows/GUITools.cpp

@@ -153,6 +153,7 @@ class TPuttyCleanupThread : public TSimpleThread
 {
 public:
   static void Schedule();
+  static void Finalize();
 
 protected:
   virtual void __fastcall Execute();
@@ -166,8 +167,8 @@ private:
   static std::unique_ptr<TCriticalSection> FSection;
 };
 //---------------------------------------------------------------------------
-TPuttyCleanupThread * TPuttyCleanupThread::FInstance(NULL);
 std::unique_ptr<TCriticalSection> TPuttyCleanupThread::FSection(TraceInitPtr(new TCriticalSection()));
+TPuttyCleanupThread * TPuttyCleanupThread::FInstance;
 //---------------------------------------------------------------------------
 void TPuttyCleanupThread::Schedule()
 {
@@ -184,59 +185,85 @@ void TPuttyCleanupThread::Schedule()
   }
 }
 //---------------------------------------------------------------------------
-void __fastcall TPuttyCleanupThread::Execute()
+void TPuttyCleanupThread::Finalize()
 {
-  std::unique_ptr<TStrings> Sessions(new TStringList());
-
-  do
+  while (true)
   {
     {
       TGuard Guard(FSection.get());
-      std::unique_ptr<TRegistryStorage> Storage(new TRegistryStorage(Configuration->PuttySessionsKey));
-      Storage->AccessMode = smReadWrite;
-      Storage->ConfigureForPutty();
+      if (FInstance == NULL)
+      {
+        return;
+      }
+    }
+    Sleep(100);
+  }
+}
+//---------------------------------------------------------------------------
+void __fastcall TPuttyCleanupThread::Execute()
+{
+  try
+  {
+    std::unique_ptr<TStrings> Sessions(new TStringList());
+    bool Continue = true;
 
-      std::unique_ptr<TStringList> Sessions2(new TStringList());
-      if (Storage->OpenRootKey(true))
+    do
+    {
       {
-        std::unique_ptr<TStrings> SubKeys(new TStringList());
-        Storage->GetSubKeyNames(SubKeys.get());
-        for (int Index = 0; Index < SubKeys->Count; Index++)
+        TGuard Guard(FSection.get());
+        std::unique_ptr<TRegistryStorage> Storage(new TRegistryStorage(Configuration->PuttySessionsKey));
+        Storage->AccessMode = smReadWrite;
+        Storage->ConfigureForPutty();
+
+        std::unique_ptr<TStringList> Sessions2(new TStringList());
+        if (Storage->OpenRootKey(true))
         {
-          UnicodeString SessionName = SubKeys->Strings[Index];
-          if (StartsStr(GUIConfiguration->PuttySession, SessionName))
+          std::unique_ptr<TStrings> SubKeys(new TStringList());
+          Storage->GetSubKeyNames(SubKeys.get());
+          for (int Index = 0; Index < SubKeys->Count; Index++)
           {
-            Sessions2->Add(SessionName);
+            UnicodeString SessionName = SubKeys->Strings[Index];
+            if (StartsStr(GUIConfiguration->PuttySession, SessionName))
+            {
+              Sessions2->Add(SessionName);
+            }
+          }
+
+          Sessions2->Sort();
+          if (!Sessions->Equals(Sessions2.get()))
+          {
+            // Just in case new sessions from another WinSCP instance are added, delay the cleanup
+            // (to avoid having to implement some inter-process communication).
+            // Both instances will attempt to do the cleanup, but that not a problem
+            Sessions->Assign(Sessions2.get());
+            DoSchedule();
           }
         }
 
-        Sessions2->Sort();
-        if (!Sessions->Equals(Sessions2.get()))
+        if (FTimer < Now())
         {
-          // Just in case new sessions from another WinSCP instance are added, delay the cleanup
-          // (to avoid having to implement some inter-process communication).
-          // Both instances will attempt to do the cleanup, but that not a problem
-          Sessions->Assign(Sessions2.get());
-          DoSchedule();
+          for (int Index = 0; Index < Sessions->Count; Index++)
+          {
+            UnicodeString SessionName = Sessions->Strings[Index];
+            Storage->RecursiveDeleteSubKey(SessionName);
+          }
+
+          Continue = false;
         }
       }
 
-      if (FTimer < Now())
+      if (Continue)
       {
-        for (int Index = 0; Index < Sessions->Count; Index++)
-        {
-          UnicodeString SessionName = Sessions->Strings[Index];
-          Storage->RecursiveDeleteSubKey(SessionName);
-        }
-
-        FInstance = NULL;
-        return;
+        Sleep(1000);
       }
     }
-
-    Sleep(1000);
+    while (Continue);
+  }
+  __finally
+  {
+    TGuard Guard(FSection.get());
+    FInstance = NULL;
   }
-  while (true);
 }
 //---------------------------------------------------------------------------
 void __fastcall TPuttyCleanupThread::Terminate()
@@ -2269,3 +2296,8 @@ bool CanShowTimeEstimate(TDateTime StartTime)
 {
   return (SecondsBetween(StartTime, Now()) >= 3);
 }
+//---------------------------------------------------------------------------
+void GUIFinalize()
+{
+  TPuttyCleanupThread::Finalize();
+}

+ 1 - 0
source/windows/GUITools.h

@@ -10,6 +10,7 @@ class TSessionData;
 //---------------------------------------------------------------------------
 typedef void __fastcall (__closure* TProcessMessagesEvent)();
 //---------------------------------------------------------------------------
+void GUIFinalize();
 bool __fastcall FindFile(UnicodeString & Path);
 bool __fastcall FindTool(const UnicodeString & Name, UnicodeString & Path);
 void __fastcall ExecuteTool(const UnicodeString & Name);