| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include <stdio.h>
- #include <Interface.h>
- #include <Common.h>
- #include <Configuration.h>
- #include <ScpMain.h>
- #include <Terminal.h>
- #include <Log.h>
- #include <TextsWin.h>
- #include "CustomScpExplorer.h"
- #include "TerminalManager.h"
- #include "NonVisual.h"
- #include "ProgParams.h"
- #include "Tools.h"
- #include "WinConfiguration.h"
- #include <NMHttp.hpp>
- #include <Psock.hpp>
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- //---------------------------------------------------------------------------
- TSessionData * GetLoginData(AnsiString SessionName)
- {
- bool ProtocolDefined = false;
- TFSProtocol Protocol;
- if (SessionName.SubString(1, 6).LowerCase() == "scp://")
- {
- Protocol = fsSCPonly;
- SessionName.Delete(1, 6);
- ProtocolDefined = true;
- }
- else if (SessionName.SubString(1, 7).LowerCase() == "sftp://")
- {
- Protocol = fsSFTPonly;
- SessionName.Delete(1, 7);
- ProtocolDefined = true;
- }
- bool DefaultsOnly = true;
- TSessionData *Data = new TSessionData("");
- if (!SessionName.IsEmpty())
- {
- TSessionData * AData = NULL;
- if (!ProtocolDefined)
- {
- AData = (TSessionData *)StoredSessions->FindByName(SessionName, False);
- }
-
- if (!AData)
- {
- Data->Assign(StoredSessions->DefaultSettings);
- if (Data->ParseUrl(SessionName, 0))
- {
- Data->Name = "";
- DefaultsOnly = false;
- }
- else
- {
- SimpleErrorDialog(FMTLOAD(SESSION_NOT_EXISTS_ERROR, (SessionName)));
- }
- }
- else
- {
- DefaultsOnly = false;
- Data->Assign(AData);
- if (StoredSessions->IsHidden(AData))
- {
- AData->Remove();
- StoredSessions->Remove(AData);
- StoredSessions->Save();
- }
- }
- }
- else
- {
- Data->Assign(StoredSessions->DefaultSettings);
- }
- if (ProtocolDefined)
- {
- Data->FSProtocol = Protocol;
- }
- if (!Data->CanLogin || DefaultsOnly)
- {
- if (!DoLoginDialog(StoredSessions, Data, loStartup) || !Data->CanLogin)
- {
- delete Data;
- Data = NULL;
- }
- }
- return Data;
- }
- //---------------------------------------------------------------------------
- void __fastcall Upload(TTerminal * Terminal, TProgramParams * Params,
- int ListFrom, int ListTo)
- {
- AnsiString TargetDirectory;
- TCopyParamType CopyParam = Configuration->CopyParam;
- TStrings * FileList = NULL;
- try
- {
- FileList = new TStringList();
- for (int Index = ListFrom; Index <= ListTo; Index++)
- {
- FileList->Add(Params->Param[Index]);
- }
- TargetDirectory = UnixIncludeTrailingBackslash(Terminal->CurrentDirectory);
- if (DoCopyDialog(true, false, false, FileList,
- Terminal->IsCapable[fcTextMode], TargetDirectory, &CopyParam, true))
- {
- int Params = 0;
- Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params);
- }
- }
- __finally
- {
- delete FileList;
- }
- }
- //---------------------------------------------------------------------------
- int __fastcall CalculateCompoundVersion(int MajorVer,
- int MinorVer, int Release, int Build)
- {
- int CompoundVer = Build + 1000 * (Release + 100 * (MinorVer +
- 100 * MajorVer));
- return CompoundVer;
- }
- //---------------------------------------------------------------------------
- void __fastcall RegisterAsUrlHandler()
- {
- try
- {
- bool Success;
- bool User = true;
- TRegistry * Registry = new TRegistry();
- try
- {
- do
- {
- Success = true;
- User = !User;
- try
- {
- assert(Configuration != NULL);
- AnsiString FileName = Application->ExeName;
- AnsiString BaseKey;
- Registry->Access = KEY_WRITE;
- if (User)
- {
- Registry->RootKey = HKEY_CURRENT_USER;
- BaseKey = "Software\\Classes\\";
- }
- else
- {
- Registry->RootKey = HKEY_CLASSES_ROOT;
- BaseKey = "";
- }
- AnsiString Protocol;
- for (int Index = 0; Index <= 1; Index++)
- {
- Protocol = (Index == 0) ? "SCP" : "SFTP";
- if (Registry->OpenKey(BaseKey + Protocol, true))
- {
- Registry->WriteString("", FMTLOAD(PROTOCOL_URL_DESC, (Protocol)));
- Registry->WriteString("URL Protocol", "");
- Registry->WriteInteger("EditFlags", 0x02);
- Registry->WriteInteger("BrowserFlags", 0x08);
- if (Registry->OpenKey("DefaultIcon", true))
- {
- Registry->WriteString("", FORMAT("\"%s\",0", (FileName)));
- Registry->CloseKey();
- }
- else
- {
- Abort();
- }
- }
- else
- {
- Abort();
- }
- if (Registry->OpenKey(BaseKey + Protocol, false) &&
- Registry->OpenKey("shell", true) &&
- Registry->OpenKey("open", true) &&
- Registry->OpenKey("command", true))
- {
- Registry->WriteString("", FORMAT("\"%s\" %%1", (FileName)));
- Registry->CloseKey();
- }
- else
- {
- Abort();
- }
- }
- }
- catch(...)
- {
- Success = false;
- }
- }
- while (!Success && !User);
- }
- __finally
- {
- delete Registry;
- }
- }
- catch(Exception & E)
- {
- throw ExtException(&E, LoadStr(REGISTER_URL_ERROR));
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall CheckForUpdates()
- {
- bool Found = false;
- TCustomForm * ActiveForm = Screen->ActiveCustomForm;
- Busy(true);
- try
- {
- if (ActiveForm)
- {
- assert(ActiveForm->Enabled);
- ActiveForm->Enabled = false;
- }
- try
- {
- AnsiString Response;
- TNMHTTP * CheckForUpdatesHTTP = new TNMHTTP(Application);
- try
- {
- CheckForUpdatesHTTP->Get(LoadStr(UPDATES_URL));
- Response = CheckForUpdatesHTTP->Body;
- }
- __finally
- {
- delete CheckForUpdatesHTTP;
- }
- while (!Response.IsEmpty() && !Found)
- {
- AnsiString Line = ::CutToChar(Response, '\n', false);
- AnsiString Name = ::CutToChar(Line, '=', false);
- if (AnsiSameText(Name, "Version"))
- {
- Found = true;
- int MajorVer = StrToInt(::CutToChar(Line, '.', false));
- int MinorVer = StrToInt(::CutToChar(Line, '.', false));
- int Release = StrToInt(::CutToChar(Line, '.', false));
- int Build = StrToInt(::CutToChar(Line, '.', false));
- int CompoundVer = CalculateCompoundVersion(MajorVer, MinorVer, Release, Build);
- AnsiString VersionStr =
- FORMAT("%d.%d", (MajorVer, MinorVer)) + (Release ? "."+IntToStr(Release) : AnsiString());
- TVSFixedFileInfo * FileInfo = Configuration->FixedApplicationInfo;
- int CurrentCompoundVer = CalculateCompoundVersion(
- HIWORD(FileInfo->dwFileVersionMS), LOWORD(FileInfo->dwFileVersionMS),
- HIWORD(FileInfo->dwFileVersionLS), LOWORD(FileInfo->dwFileVersionLS));
- if (CurrentCompoundVer < CompoundVer)
- {
- if (MessageDialog(FMTLOAD(NEW_VERSION, (VersionStr)), qtInformation,
- qaOK | qaCancel, 0) == qaOK)
- {
- NonVisualDataModule->OpenBrowser(LoadStr(DOWNLOAD_URL));
- }
- }
- else
- {
- MessageDialog(LoadStr(NO_NEW_VERSION), qtInformation, qaOK, 0);
- }
- }
- }
- }
- catch(Exception & E)
- {
- throw ExtException(&E, LoadStr(CHECK_FOR_UPDATES_ERROR));
- }
- }
- __finally
- {
- if (ActiveForm)
- {
- ActiveForm->Enabled = true;
- }
- Busy(false);
- }
- if (!Found)
- {
- throw Exception(LoadStr(CHECK_FOR_UPDATES_ERROR));
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall Execute(TProgramParams * Params)
- {
- assert(StoredSessions);
- assert(Params);
- TTerminalManager * TerminalManager = NULL;
- NonVisualDataModule = NULL;
- try
- {
- TerminalManager = TTerminalManager::Instance();
- NonVisualDataModule = new TNonVisualDataModule(Application);
- LogForm = NULL;
- Application->HintHidePause = 1000;
- if (Params->FindSwitch("UninstallCleanup"))
- {
- if (MessageDialog(LoadStr(UNINSTALL_CLEANUP), qtConfirmation,
- qaOK | qaCancel, 0) == qaOK)
- {
- DoCleanupDialog(StoredSessions, Configuration);
- }
- }
- else if (Params->FindSwitch("RegisterAsUrlHandler"))
- {
- RegisterAsUrlHandler();
- }
- else if (Params->FindSwitch("Update"))
- {
- CheckForUpdates();
- }
- else
- {
- TSessionData * Data;
- int UploadListStart = 0;
- AnsiString AutoStartSession;
- if (Params->ParamCount)
- {
- AnsiString DummyValue;
- if (Params->FindSwitch("Upload", DummyValue, UploadListStart))
- {
- UploadListStart++;
- if (UploadListStart >= 2)
- {
- AutoStartSession = Params->Param[1];
- }
- }
- else
- {
- AutoStartSession = Params->Param[1];
- }
- }
- else if (WinConfiguration->EmbeddedSessions && StoredSessions->Count)
- {
- AutoStartSession = StoredSessions->Sessions[0]->Name;
- }
- else
- {
- AutoStartSession = WinConfiguration->AutoStartSession;
- }
- Data = GetLoginData(AutoStartSession);
- if (Data)
- {
- try
- {
- assert(!TerminalManager->ActiveTerminal);
- TerminalManager->NewTerminal(Data);
- }
- __finally
- {
- delete Data;
- }
- try
- {
- if (TerminalManager->ConnectActiveTerminal())
- {
- TCustomScpExplorerForm * ScpExplorer = CreateScpExplorer();
- try
- {
- // moved inside try .. __finally, because it can fail as well
- TerminalManager->ScpExplorer = ScpExplorer;
- if (UploadListStart > 0)
- {
- if (UploadListStart <= Params->ParamCount)
- {
- Upload(TerminalManager->ActiveTerminal, Params,
- UploadListStart, Params->ParamCount);
- }
- else
- {
- throw Exception(NO_UPLOAD_LIST_ERROR);
- }
- }
- Application->Run();
- }
- __finally
- {
- TerminalManager->ScpExplorer = NULL;
- SAFE_DESTROY(ScpExplorer);
- }
- }
- }
- catch (Exception &E)
- {
- ShowExtendedExceptionEx(&E, Application, true);
- }
- }
- }
- }
- __finally
- {
- delete NonVisualDataModule;
- NonVisualDataModule = NULL;
- TTerminalManager::DestroyInstance();
- }
- }
|