WinMain.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <CoreMain.h>
  5. #include <Log.h>
  6. #include <TextsWin.h>
  7. #include <TextsCore.h>
  8. #include <HelpWin.h>
  9. #include "CustomScpExplorer.h"
  10. #include "TerminalManager.h"
  11. #include "NonVisual.h"
  12. #include "Glyphs.h"
  13. #include "ProgParams.h"
  14. #include "Setup.h"
  15. #include "WinConfiguration.h"
  16. #include "GUITools.h"
  17. //---------------------------------------------------------------------------
  18. #pragma package(smart_init)
  19. //---------------------------------------------------------------------------
  20. TSessionData * GetLoginData(AnsiString SessionName, AnsiString & DownloadFile,
  21. bool & Url)
  22. {
  23. bool DefaultsOnly;
  24. TSessionData * Data;
  25. Data = StoredSessions->ParseUrl(SessionName, DefaultsOnly,
  26. puExtractFileName | puDecodeUrlChars, &DownloadFile, &Url);
  27. assert(Data != NULL);
  28. if (!Data->CanLogin || DefaultsOnly)
  29. {
  30. if (!DoLoginDialog(StoredSessions, Data, loStartup) || !Data->CanLogin)
  31. {
  32. delete Data;
  33. Data = NULL;
  34. }
  35. }
  36. return Data;
  37. }
  38. //---------------------------------------------------------------------------
  39. void __fastcall Upload(TTerminal * Terminal, TStrings * FileList, bool UseDefaults)
  40. {
  41. AnsiString TargetDirectory;
  42. TGUICopyParamType CopyParam = GUIConfiguration->DefaultCopyParam;
  43. TargetDirectory = UnixIncludeTrailingBackslash(Terminal->CurrentDirectory);
  44. int Options = coDisableQueue |
  45. (!Terminal->IsCapable[fcNewerOnlyUpload] ? coDisableNewerOnly : 0);
  46. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  47. if (UseDefaults ||
  48. DoCopyDialog(true, false, FileList, TargetDirectory, &CopyParam, Options,
  49. CopyParamAttrs, NULL))
  50. {
  51. int Params = (CopyParam.NewerOnly ? cpNewerOnly : 0);
  52. Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params);
  53. }
  54. }
  55. //---------------------------------------------------------------------------
  56. void __fastcall Download(TTerminal * Terminal, const AnsiString FileName,
  57. bool UseDefaults)
  58. {
  59. AnsiString TargetDirectory;
  60. TGUICopyParamType CopyParam = GUIConfiguration->DefaultCopyParam;
  61. TStrings * FileList = NULL;
  62. try
  63. {
  64. FileList = new TStringList();
  65. TRemoteFile * File = Terminal->Files->FindFile(FileName);
  66. if (File == NULL)
  67. {
  68. throw Exception(FMTLOAD(FILE_NOT_EXISTS, (FileName)));
  69. }
  70. FileList->AddObject(FileName, File);
  71. AnsiString LocalDirectory = Terminal->SessionData->LocalDirectory;
  72. if (LocalDirectory.IsEmpty())
  73. {
  74. ::SpecialFolderLocation(CSIDL_PERSONAL, LocalDirectory);
  75. }
  76. TargetDirectory = IncludeTrailingBackslash(LocalDirectory);
  77. int Options = coDisableQueue;
  78. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Download;
  79. if (UseDefaults ||
  80. DoCopyDialog(false, false, FileList, TargetDirectory, &CopyParam,
  81. Options, CopyParamAttrs, NULL))
  82. {
  83. Terminal->CopyToLocal(FileList, TargetDirectory, &CopyParam, 0);
  84. }
  85. }
  86. __finally
  87. {
  88. delete FileList;
  89. }
  90. }
  91. //---------------------------------------------------------------------------
  92. void __fastcall SynchronizeDirectories(TTerminal * Terminal,
  93. TStrings * CommandParams,
  94. AnsiString & LocalDirectory, AnsiString & RemoteDirectory)
  95. {
  96. if (CommandParams->Count >= 1)
  97. {
  98. LocalDirectory = CommandParams->Strings[0];
  99. }
  100. else if (!Terminal->SessionData->LocalDirectory.IsEmpty())
  101. {
  102. LocalDirectory = Terminal->SessionData->LocalDirectory;
  103. }
  104. else
  105. {
  106. LocalDirectory = WinConfiguration->ScpExplorer.LastLocalTargetDirectory;
  107. }
  108. if (CommandParams->Count >= 2)
  109. {
  110. RemoteDirectory = CommandParams->Strings[1];
  111. }
  112. else
  113. {
  114. RemoteDirectory = Terminal->CurrentDirectory;
  115. }
  116. }
  117. //---------------------------------------------------------------------------
  118. void __fastcall FullSynchronize(TTerminal * Terminal, TCustomScpExplorerForm * ScpExplorer,
  119. TStrings * CommandParams, bool UseDefaults)
  120. {
  121. AnsiString LocalDirectory;
  122. AnsiString RemoteDirectory;
  123. SynchronizeDirectories(Terminal, CommandParams, LocalDirectory, RemoteDirectory);
  124. bool SaveMode = true;
  125. // bit ugly
  126. TSynchronizeMode Mode = (TSynchronizeMode)GUIConfiguration->SynchronizeMode;
  127. if (ScpExplorer->DoFullSynchronizeDirectories(LocalDirectory,
  128. RemoteDirectory, Mode, SaveMode, UseDefaults))
  129. {
  130. if (SaveMode)
  131. {
  132. GUIConfiguration->SynchronizeMode = Mode;
  133. }
  134. Terminal->CloseOnCompletion();
  135. }
  136. else
  137. {
  138. Abort();
  139. }
  140. }
  141. //---------------------------------------------------------------------------
  142. void __fastcall Synchronize(TTerminal * Terminal, TCustomScpExplorerForm * ScpExplorer,
  143. TStrings * CommandParams, bool UseDefaults)
  144. {
  145. AnsiString LocalDirectory;
  146. AnsiString RemoteDirectory;
  147. SynchronizeDirectories(Terminal, CommandParams, LocalDirectory, RemoteDirectory);
  148. ScpExplorer->DoSynchronizeDirectories(LocalDirectory, RemoteDirectory, UseDefaults);
  149. Abort();
  150. }
  151. //---------------------------------------------------------------------------
  152. void __fastcall InvalidDefaultTranslation()
  153. {
  154. if (WinConfiguration->ConfirmRemoveDefaultTranslation())
  155. {
  156. if (!DeleteFile(WinConfiguration->DefaultTranslationFile))
  157. {
  158. throw Exception(FMTLOAD(DELETE_LOCAL_FILE_ERROR,
  159. (WinConfiguration->DefaultTranslationFile)));
  160. }
  161. }
  162. }
  163. //---------------------------------------------------------------------------
  164. int __fastcall Execute()
  165. {
  166. assert(StoredSessions);
  167. TProgramParams * Params = TProgramParams::Instance();
  168. assert(Params);
  169. // do not flash message boxes on startup
  170. SetOnForeground(true);
  171. // let installer know, that some instance of application is running
  172. CreateMutex(NULL, False, AppName.c_str());
  173. bool OnlyInstance = (GetLastError() == 0);
  174. AnsiString KeyFile;
  175. if (Params->FindSwitch("PrivateKey", KeyFile))
  176. {
  177. WinConfiguration->DefaultKeyFile = KeyFile;
  178. }
  179. bool Help = Params->FindSwitch("help") || Params->FindSwitch("h") || Params->FindSwitch("?");
  180. if (Help || Params->FindSwitch("Console") || Params->FindSwitch("script") ||
  181. Params->FindSwitch("command"))
  182. {
  183. return Console(Help);
  184. }
  185. TTerminalManager * TerminalManager = NULL;
  186. GlyphsModule = NULL;
  187. NonVisualDataModule = NULL;
  188. TStrings * CommandParams = new TStringList;
  189. try
  190. {
  191. TerminalManager = TTerminalManager::Instance();
  192. HINSTANCE ResourceModule = GUIConfiguration->ChangeResourceModule(NULL);
  193. try
  194. {
  195. GlyphsModule = new TGlyphsModule(Application);
  196. }
  197. __finally
  198. {
  199. GUIConfiguration->ChangeResourceModule(ResourceModule);
  200. }
  201. NonVisualDataModule = new TNonVisualDataModule(Application);
  202. LogForm = NULL;
  203. Application->HintHidePause = 3000;
  204. AnsiString Value;
  205. AnsiString IniFileName = Params->SwitchValue("ini");
  206. if (!IniFileName.IsEmpty())
  207. {
  208. AnsiString IniFileNameExpanded = ExpandEnvironmentVariables(IniFileName);
  209. if (!FileExists(IniFileNameExpanded))
  210. {
  211. // this should be displayed rather at the very beginning.
  212. // however for simplicity (GUI-only), we do it only here.
  213. MessageDialog(FMTLOAD(FILE_NOT_EXISTS, (IniFileNameExpanded)), qtError, qaOK);
  214. }
  215. }
  216. if (Params->FindSwitch("UninstallCleanup"))
  217. {
  218. // The innosetup cannot skip UninstallCleanup run task for silent uninstalls,
  219. // workaround is that we create mutex in uninstaller, if it runs silent, and
  220. // ignore the UninstallCleanup, when the mutex exists.
  221. if ((OpenMutex(SYNCHRONIZE, false, "WinSCPSilentUninstall") == NULL) &&
  222. (MessageDialog(LoadStr(UNINSTALL_CLEANUP), qtConfirmation,
  223. qaYes | qaNo, HELP_UNINSTALL_CLEANUP) == qaYes))
  224. {
  225. DoCleanupDialog(StoredSessions, Configuration);
  226. }
  227. }
  228. else if (Params->FindSwitch("RegisterAsUrlHandler"))
  229. {
  230. RegisterAsUrlHandler();
  231. }
  232. else if (Params->FindSwitch("AddSearchPath"))
  233. {
  234. AddSearchPath(ExtractFilePath(Application->ExeName));
  235. }
  236. else if (Params->FindSwitch("RemoveSearchPath"))
  237. {
  238. RemoveSearchPath(ExtractFilePath(Application->ExeName));
  239. }
  240. else if (Params->FindSwitch("Update"))
  241. {
  242. CheckForUpdates(false);
  243. }
  244. else if (Params->FindSwitch("InvalidDefaultTranslation"))
  245. {
  246. InvalidDefaultTranslation();
  247. }
  248. else
  249. {
  250. TSessionData * Data;
  251. enum { pcNone, pcUpload, pcFullSynchronize, pcSynchronize } ParamCommand;
  252. ParamCommand = pcNone;
  253. AnsiString AutoStartSession;
  254. AnsiString DownloadFile;
  255. bool UseDefaults = false;
  256. // do not check for temp dirs for service tasks (like RegisterAsUrlHandler)
  257. if (OnlyInstance &&
  258. WinConfiguration->TemporaryDirectoryCleanup)
  259. {
  260. TemporaryDirectoryCleanup();
  261. }
  262. WinConfiguration->CheckDefaultTranslation();
  263. if (Params->Count > 0)
  264. {
  265. if (Params->FindSwitch("Defaults"))
  266. {
  267. UseDefaults = true;
  268. }
  269. if (Params->FindSwitch("Upload", CommandParams))
  270. {
  271. ParamCommand = pcUpload;
  272. if (CommandParams->Count == 0)
  273. {
  274. throw Exception(NO_UPLOAD_LIST_ERROR);
  275. }
  276. }
  277. if (Params->FindSwitch("UploadIfAny", CommandParams))
  278. {
  279. if (CommandParams->Count > 0)
  280. {
  281. ParamCommand = pcUpload;
  282. }
  283. }
  284. else if (Params->FindSwitch("Synchronize", CommandParams, 2))
  285. {
  286. ParamCommand = pcFullSynchronize;
  287. }
  288. else if (Params->FindSwitch("KeepUpToDate", CommandParams, 2))
  289. {
  290. ParamCommand = pcSynchronize;
  291. }
  292. }
  293. if (Params->ParamCount > 0)
  294. {
  295. AutoStartSession = Params->Param[1];
  296. Params->ParamsProcessed(1, 1);
  297. }
  298. else if (WinConfiguration->EmbeddedSessions && StoredSessions->Count)
  299. {
  300. AutoStartSession = StoredSessions->Sessions[0]->Name;
  301. }
  302. else
  303. {
  304. AutoStartSession = WinConfiguration->AutoStartSession;
  305. }
  306. // from now flash message boxes on background
  307. SetOnForeground(false);
  308. bool Retry;
  309. do
  310. {
  311. Retry = false;
  312. bool Url = false;
  313. Data = GetLoginData(AutoStartSession, DownloadFile, Url);
  314. if (Data)
  315. {
  316. if (Url)
  317. {
  318. // prevent any automatic action when URL is provided on
  319. // command-line
  320. UseDefaults = false;
  321. }
  322. else
  323. {
  324. AnsiString LogFile;
  325. if (Params->FindSwitch("Log", LogFile))
  326. {
  327. Configuration->TemporaryLogging(LogFile);
  328. }
  329. }
  330. if ((Data->FSProtocol == fsExternalSSH) ||
  331. (Data->FSProtocol == fsExternalSFTP))
  332. {
  333. OpenSessionInPutty(
  334. ((Data->FSProtocol == fsExternalSSH) ?
  335. GUIConfiguration->PuttyPath : GUIConfiguration->PSftpPath),
  336. Data, (GUIConfiguration->PuttyPassword ? Data->Password : AnsiString()));
  337. }
  338. else
  339. {
  340. try
  341. {
  342. assert(!TerminalManager->ActiveTerminal);
  343. TerminalManager->NewTerminal(Data);
  344. }
  345. __finally
  346. {
  347. delete Data;
  348. }
  349. try
  350. {
  351. if (!TerminalManager->ConnectActiveTerminal())
  352. {
  353. // do not prompt with login dialog, if connection of
  354. // auto-start session (typically from command line) failed
  355. if (AutoStartSession.IsEmpty())
  356. {
  357. Retry = true;
  358. }
  359. }
  360. else
  361. {
  362. TCustomScpExplorerForm * ScpExplorer = CreateScpExplorer();
  363. try
  364. {
  365. // moved inside try .. __finally, because it can fail as well
  366. TerminalManager->ScpExplorer = ScpExplorer;
  367. if (ParamCommand == pcUpload)
  368. {
  369. Upload(TerminalManager->ActiveTerminal, CommandParams, UseDefaults);
  370. }
  371. else if (ParamCommand == pcFullSynchronize)
  372. {
  373. FullSynchronize(TerminalManager->ActiveTerminal, ScpExplorer,
  374. CommandParams, UseDefaults);
  375. }
  376. else if (ParamCommand == pcSynchronize)
  377. {
  378. Synchronize(TerminalManager->ActiveTerminal, ScpExplorer,
  379. CommandParams, UseDefaults);
  380. }
  381. else if (!DownloadFile.IsEmpty())
  382. {
  383. Download(TerminalManager->ActiveTerminal, DownloadFile,
  384. UseDefaults);
  385. }
  386. Application->Run();
  387. }
  388. __finally
  389. {
  390. TerminalManager->ScpExplorer = NULL;
  391. SAFE_DESTROY(ScpExplorer);
  392. }
  393. }
  394. }
  395. catch (Exception &E)
  396. {
  397. ShowExtendedException(&E);
  398. }
  399. }
  400. }
  401. }
  402. while (Retry);
  403. }
  404. }
  405. __finally
  406. {
  407. delete NonVisualDataModule;
  408. NonVisualDataModule = NULL;
  409. delete GlyphsModule;
  410. GlyphsModule = NULL;
  411. TTerminalManager::DestroyInstance();
  412. delete CommandParams;
  413. }
  414. return 0;
  415. }