Skip to content

Commit

Permalink
Bug 2079: Allow opening all sites in a folder in PuTTY (3rd)
Browse files Browse the repository at this point in the history
https://winscp.net/tracker/2079

Source commit: 6957e121b27634e5fb97df365ac98071339d0401
  • Loading branch information
martinprikryl committed May 25, 2022
1 parent 49c63bf commit ccd498f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 35 deletions.
2 changes: 2 additions & 0 deletions source/WinSCP.cpp
Expand Up @@ -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)
{
Expand Down Expand Up @@ -53,6 +54,7 @@ WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
}
__finally
{
GUIFinalize();
FinalizeSystemSettings();
FinalizeWinHelp();
CoreFinalize();
Expand Down
102 changes: 67 additions & 35 deletions source/windows/GUITools.cpp
Expand Up @@ -153,6 +153,7 @@ class TPuttyCleanupThread : public TSimpleThread
{
public:
static void Schedule();
static void Finalize();

protected:
virtual void __fastcall Execute();
Expand All @@ -166,8 +167,8 @@ class TPuttyCleanupThread : public TSimpleThread
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()
{
Expand All @@ -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()
Expand Down Expand Up @@ -2269,3 +2296,8 @@ bool CanShowTimeEstimate(TDateTime StartTime)
{
return (SecondsBetween(StartTime, Now()) >= 3);
}
//---------------------------------------------------------------------------
void GUIFinalize()
{
TPuttyCleanupThread::Finalize();
}
1 change: 1 addition & 0 deletions source/windows/GUITools.h
Expand Up @@ -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);
Expand Down

0 comments on commit ccd498f

Please sign in to comment.