| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include <CoreMain.h>
- #include <Log.h>
- #include <TextsWin.h>
- #include <TextsCore.h>
- #include <HelpWin.h>
- #include "CustomScpExplorer.h"
- #include "TerminalManager.h"
- #include "NonVisual.h"
- #include "Glyphs.h"
- #include "ProgParams.h"
- #include "Setup.h"
- #include "WinConfiguration.h"
- #include "GUITools.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- //---------------------------------------------------------------------------
- TSessionData * GetLoginData(AnsiString SessionName, TOptions * Options,
- AnsiString & DownloadFile, bool & Url)
- {
- bool DefaultsOnly;
- TSessionData * Data;
- Data = StoredSessions->ParseUrl(SessionName, Options, DefaultsOnly,
- &DownloadFile, &Url);
- assert(Data != NULL);
- if (!Data->CanLogin || DefaultsOnly)
- {
- if (!DoLoginDialog(StoredSessions, Data, loStartup) || !Data->CanLogin)
- {
- delete Data;
- Data = NULL;
- }
- }
- return Data;
- }
- //---------------------------------------------------------------------------
- void __fastcall Upload(TTerminal * Terminal, TStrings * FileList, bool UseDefaults)
- {
- AnsiString TargetDirectory;
- TGUICopyParamType CopyParam = GUIConfiguration->DefaultCopyParam;
- TargetDirectory = UnixIncludeTrailingBackslash(Terminal->CurrentDirectory);
- int Options = coDisableQueue |
- (!Terminal->IsCapable[fcNewerOnlyUpload] ? coDisableNewerOnly : 0);
- int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
- if (UseDefaults ||
- DoCopyDialog(true, false, FileList, TargetDirectory, &CopyParam, Options,
- CopyParamAttrs, NULL))
- {
- int Params = (CopyParam.NewerOnly ? cpNewerOnly : 0);
- Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall Download(TTerminal * Terminal, const AnsiString FileName,
- bool UseDefaults)
- {
- AnsiString TargetDirectory;
- TGUICopyParamType CopyParam = GUIConfiguration->DefaultCopyParam;
- TStrings * FileList = NULL;
- try
- {
- FileList = new TStringList();
- TRemoteFile * File = Terminal->Files->FindFile(FileName);
- if (File == NULL)
- {
- throw Exception(FMTLOAD(FILE_NOT_EXISTS, (FileName)));
- }
- FileList->AddObject(FileName, File);
- AnsiString LocalDirectory = Terminal->SessionData->LocalDirectory;
- if (LocalDirectory.IsEmpty())
- {
- ::SpecialFolderLocation(CSIDL_PERSONAL, LocalDirectory);
- }
- TargetDirectory = IncludeTrailingBackslash(LocalDirectory);
- int Options = coDisableQueue;
- int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Download;
- if (UseDefaults ||
- DoCopyDialog(false, false, FileList, TargetDirectory, &CopyParam,
- Options, CopyParamAttrs, NULL))
- {
- Terminal->CopyToLocal(FileList, TargetDirectory, &CopyParam, 0);
- }
- }
- __finally
- {
- delete FileList;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall SynchronizeDirectories(TTerminal * Terminal,
- TStrings * CommandParams,
- AnsiString & LocalDirectory, AnsiString & RemoteDirectory)
- {
- if (CommandParams->Count >= 1)
- {
- LocalDirectory = CommandParams->Strings[0];
- }
- else if (!Terminal->SessionData->LocalDirectory.IsEmpty())
- {
- LocalDirectory = Terminal->SessionData->LocalDirectory;
- }
- else
- {
- LocalDirectory = WinConfiguration->ScpExplorer.LastLocalTargetDirectory;
- }
- if (CommandParams->Count >= 2)
- {
- RemoteDirectory = CommandParams->Strings[1];
- }
- else
- {
- RemoteDirectory = Terminal->CurrentDirectory;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall FullSynchronize(TTerminal * Terminal, TCustomScpExplorerForm * ScpExplorer,
- TStrings * CommandParams, bool UseDefaults)
- {
- AnsiString LocalDirectory;
- AnsiString RemoteDirectory;
- SynchronizeDirectories(Terminal, CommandParams, LocalDirectory, RemoteDirectory);
- bool SaveMode = true;
- // bit ugly
- TSynchronizeMode Mode = (TSynchronizeMode)GUIConfiguration->SynchronizeMode;
- if (ScpExplorer->DoFullSynchronizeDirectories(LocalDirectory,
- RemoteDirectory, Mode, SaveMode, UseDefaults))
- {
- if (SaveMode)
- {
- GUIConfiguration->SynchronizeMode = Mode;
- }
- Terminal->CloseOnCompletion();
- }
- else
- {
- Abort();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall Synchronize(TTerminal * Terminal, TCustomScpExplorerForm * ScpExplorer,
- TStrings * CommandParams, bool UseDefaults)
- {
- AnsiString LocalDirectory;
- AnsiString RemoteDirectory;
- SynchronizeDirectories(Terminal, CommandParams, LocalDirectory, RemoteDirectory);
- ScpExplorer->DoSynchronizeDirectories(LocalDirectory, RemoteDirectory, UseDefaults);
- Abort();
- }
- //---------------------------------------------------------------------------
- int __fastcall Execute()
- {
- assert(StoredSessions);
- TProgramParams * Params = TProgramParams::Instance();
- assert(Params);
- // do not flash message boxes on startup
- SetOnForeground(true);
- // let installer know, that some instance of application is running
- CreateMutex(NULL, False, AppName.c_str());
- bool OnlyInstance = (GetLastError() == 0);
- AnsiString KeyFile;
- if (Params->FindSwitch("PrivateKey", KeyFile))
- {
- WinConfiguration->DefaultKeyFile = KeyFile;
- }
- bool Help = Params->FindSwitch("help") || Params->FindSwitch("h") || Params->FindSwitch("?");
- if (Help || Params->FindSwitch("Console") || Params->FindSwitch("script") ||
- Params->FindSwitch("command"))
- {
- return Console(Help);
- }
- TTerminalManager * TerminalManager = NULL;
- GlyphsModule = NULL;
- NonVisualDataModule = NULL;
- TStrings * CommandParams = new TStringList;
- try
- {
- TerminalManager = TTerminalManager::Instance();
- HINSTANCE ResourceModule = GUIConfiguration->ChangeResourceModule(NULL);
- try
- {
- GlyphsModule = new TGlyphsModule(Application);
- }
- __finally
- {
- GUIConfiguration->ChangeResourceModule(ResourceModule);
- }
- NonVisualDataModule = new TNonVisualDataModule(Application);
- LogForm = NULL;
- Application->HintHidePause = 3000;
- AnsiString Value;
- AnsiString IniFileName = Params->SwitchValue("ini");
- if (!IniFileName.IsEmpty())
- {
- AnsiString IniFileNameExpanded = ExpandEnvironmentVariables(IniFileName);
- if (!FileExists(IniFileNameExpanded))
- {
- // this should be displayed rather at the very beginning.
- // however for simplicity (GUI-only), we do it only here.
- MessageDialog(FMTLOAD(FILE_NOT_EXISTS, (IniFileNameExpanded)), qtError, qaOK);
- }
- }
- if (Params->FindSwitch("UninstallCleanup"))
- {
- // The innosetup cannot skip UninstallCleanup run task for silent uninstalls,
- // workaround is that we create mutex in uninstaller, if it runs silent, and
- // ignore the UninstallCleanup, when the mutex exists.
- if ((OpenMutex(SYNCHRONIZE, false, "WinSCPSilentUninstall") == NULL) &&
- (MessageDialog(LoadStr(UNINSTALL_CLEANUP), qtConfirmation,
- qaYes | qaNo, HELP_UNINSTALL_CLEANUP) == qaYes))
- {
- DoCleanupDialog(StoredSessions, Configuration);
- }
- }
- else if (Params->FindSwitch("RegisterAsUrlHandler"))
- {
- RegisterAsUrlHandler();
- }
- else if (Params->FindSwitch("AddSearchPath"))
- {
- AddSearchPath(ExtractFilePath(Application->ExeName));
- }
- else if (Params->FindSwitch("RemoveSearchPath"))
- {
- try
- {
- RemoveSearchPath(ExtractFilePath(Application->ExeName));
- }
- catch(...)
- {
- // ignore errors
- // (RemoveSearchPath is called always on uninstallation,
- // even if AddSearchPath was not used, so we would get the error
- // always for non-priviledged user)
- }
- }
- else if (Params->FindSwitch("Update"))
- {
- CheckForUpdates(false);
- }
- else
- {
- TSessionData * Data;
- enum { pcNone, pcUpload, pcFullSynchronize, pcSynchronize } ParamCommand;
- ParamCommand = pcNone;
- AnsiString AutoStartSession;
- AnsiString DownloadFile;
- bool UseDefaults = false;
- // do not check for temp dirs for service tasks (like RegisterAsUrlHandler)
- if (OnlyInstance &&
- WinConfiguration->TemporaryDirectoryCleanup)
- {
- TemporaryDirectoryCleanup();
- }
- WinConfiguration->CheckDefaultTranslation();
- if (!Params->Empty)
- {
- if (Params->FindSwitch("Defaults"))
- {
- UseDefaults = true;
- }
- if (Params->FindSwitch("Upload", CommandParams))
- {
- ParamCommand = pcUpload;
- if (CommandParams->Count == 0)
- {
- throw Exception(NO_UPLOAD_LIST_ERROR);
- }
- }
- if (Params->FindSwitch("UploadIfAny", CommandParams))
- {
- if (CommandParams->Count > 0)
- {
- ParamCommand = pcUpload;
- }
- }
- else if (Params->FindSwitch("Synchronize", CommandParams, 2))
- {
- ParamCommand = pcFullSynchronize;
- }
- else if (Params->FindSwitch("KeepUpToDate", CommandParams, 2))
- {
- ParamCommand = pcSynchronize;
- }
- }
- if (Params->ParamCount > 0)
- {
- AutoStartSession = Params->Param[1];
- Params->ParamsProcessed(1, 1);
- }
- else if (WinConfiguration->EmbeddedSessions && StoredSessions->Count)
- {
- AutoStartSession = StoredSessions->Sessions[0]->Name;
- }
- else
- {
- AutoStartSession = WinConfiguration->AutoStartSession;
- }
- // from now flash message boxes on background
- SetOnForeground(false);
- bool Retry;
- do
- {
- Retry = false;
- bool Url = false;
- Data = GetLoginData(AutoStartSession, Params, DownloadFile, Url);
- if (Data)
- {
- if (Url || Params->FindSwitch("Unsafe"))
- {
- // prevent any automatic action when URL is provided on
- // command-line (the check is duplicated in Console())
- if (UseDefaults || Params->FindSwitch("Log"))
- {
- MessageDialog(LoadStr(UNSAFE_ACTIONS_DISABLED), qtWarning, qaOK);
- }
- UseDefaults = false;
- }
- else
- {
- AnsiString LogFile;
- if (Params->FindSwitch("Log", LogFile))
- {
- Configuration->TemporaryLogging(LogFile);
- }
- }
- if ((Data->FSProtocol == fsExternalSSH) ||
- (Data->FSProtocol == fsExternalSFTP))
- {
- OpenSessionInPutty(
- ((Data->FSProtocol == fsExternalSSH) ?
- GUIConfiguration->PuttyPath : GUIConfiguration->PSftpPath),
- Data, (GUIConfiguration->PuttyPassword ? Data->Password : AnsiString()));
- }
- else
- {
- try
- {
- assert(!TerminalManager->ActiveTerminal);
- TerminalManager->NewTerminal(Data);
- }
- __finally
- {
- delete Data;
- }
- try
- {
- if (!TerminalManager->ConnectActiveTerminal())
- {
- // do not prompt with login dialog, if connection of
- // auto-start session (typically from command line) failed
- if (AutoStartSession.IsEmpty())
- {
- Retry = true;
- }
- }
- else
- {
- TCustomScpExplorerForm * ScpExplorer = CreateScpExplorer();
- try
- {
- // moved inside try .. __finally, because it can fail as well
- TerminalManager->ScpExplorer = ScpExplorer;
- if (ParamCommand == pcUpload)
- {
- Upload(TerminalManager->ActiveTerminal, CommandParams, UseDefaults);
- }
- else if (ParamCommand == pcFullSynchronize)
- {
- FullSynchronize(TerminalManager->ActiveTerminal, ScpExplorer,
- CommandParams, UseDefaults);
- }
- else if (ParamCommand == pcSynchronize)
- {
- Synchronize(TerminalManager->ActiveTerminal, ScpExplorer,
- CommandParams, UseDefaults);
- }
- else if (!DownloadFile.IsEmpty())
- {
- Download(TerminalManager->ActiveTerminal, DownloadFile,
- UseDefaults);
- }
- Application->Run();
- }
- __finally
- {
- TerminalManager->ScpExplorer = NULL;
- SAFE_DESTROY(ScpExplorer);
- }
- }
- }
- catch (Exception &E)
- {
- ShowExtendedException(&E);
- }
- }
- }
- }
- while (Retry);
- }
- }
- __finally
- {
- delete NonVisualDataModule;
- NonVisualDataModule = NULL;
- delete GlyphsModule;
- GlyphsModule = NULL;
- TTerminalManager::DestroyInstance();
- delete CommandParams;
- }
- return 0;
- }
|