WinMain.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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, TOptions * Options,
  21. AnsiString & DownloadFile, bool & Url)
  22. {
  23. bool DefaultsOnly;
  24. TSessionData * Data;
  25. Data = StoredSessions->ParseUrl(SessionName, Options, DefaultsOnly,
  26. &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. int __fastcall Execute()
  153. {
  154. assert(StoredSessions);
  155. TProgramParams * Params = TProgramParams::Instance();
  156. assert(Params);
  157. // do not flash message boxes on startup
  158. SetOnForeground(true);
  159. // let installer know, that some instance of application is running
  160. CreateMutex(NULL, False, AppName.c_str());
  161. bool OnlyInstance = (GetLastError() == 0);
  162. AnsiString KeyFile;
  163. if (Params->FindSwitch("PrivateKey", KeyFile))
  164. {
  165. WinConfiguration->DefaultKeyFile = KeyFile;
  166. }
  167. bool Help = Params->FindSwitch("help") || Params->FindSwitch("h") || Params->FindSwitch("?");
  168. if (Help || Params->FindSwitch("Console") || Params->FindSwitch("script") ||
  169. Params->FindSwitch("command"))
  170. {
  171. return Console(Help);
  172. }
  173. TTerminalManager * TerminalManager = NULL;
  174. GlyphsModule = NULL;
  175. NonVisualDataModule = NULL;
  176. TStrings * CommandParams = new TStringList;
  177. try
  178. {
  179. TerminalManager = TTerminalManager::Instance();
  180. HINSTANCE ResourceModule = GUIConfiguration->ChangeResourceModule(NULL);
  181. try
  182. {
  183. GlyphsModule = new TGlyphsModule(Application);
  184. }
  185. __finally
  186. {
  187. GUIConfiguration->ChangeResourceModule(ResourceModule);
  188. }
  189. NonVisualDataModule = new TNonVisualDataModule(Application);
  190. LogForm = NULL;
  191. Application->HintHidePause = 3000;
  192. AnsiString Value;
  193. AnsiString IniFileName = Params->SwitchValue("ini");
  194. if (!IniFileName.IsEmpty())
  195. {
  196. AnsiString IniFileNameExpanded = ExpandEnvironmentVariables(IniFileName);
  197. if (!FileExists(IniFileNameExpanded))
  198. {
  199. // this should be displayed rather at the very beginning.
  200. // however for simplicity (GUI-only), we do it only here.
  201. MessageDialog(FMTLOAD(FILE_NOT_EXISTS, (IniFileNameExpanded)), qtError, qaOK);
  202. }
  203. }
  204. if (Params->FindSwitch("UninstallCleanup"))
  205. {
  206. // The innosetup cannot skip UninstallCleanup run task for silent uninstalls,
  207. // workaround is that we create mutex in uninstaller, if it runs silent, and
  208. // ignore the UninstallCleanup, when the mutex exists.
  209. if ((OpenMutex(SYNCHRONIZE, false, "WinSCPSilentUninstall") == NULL) &&
  210. (MessageDialog(LoadStr(UNINSTALL_CLEANUP), qtConfirmation,
  211. qaYes | qaNo, HELP_UNINSTALL_CLEANUP) == qaYes))
  212. {
  213. DoCleanupDialog(StoredSessions, Configuration);
  214. }
  215. }
  216. else if (Params->FindSwitch("RegisterAsUrlHandler"))
  217. {
  218. RegisterAsUrlHandler();
  219. }
  220. else if (Params->FindSwitch("AddSearchPath"))
  221. {
  222. AddSearchPath(ExtractFilePath(Application->ExeName));
  223. }
  224. else if (Params->FindSwitch("RemoveSearchPath"))
  225. {
  226. try
  227. {
  228. RemoveSearchPath(ExtractFilePath(Application->ExeName));
  229. }
  230. catch(...)
  231. {
  232. // ignore errors
  233. // (RemoveSearchPath is called always on uninstallation,
  234. // even if AddSearchPath was not used, so we would get the error
  235. // always for non-priviledged user)
  236. }
  237. }
  238. else if (Params->FindSwitch("Update"))
  239. {
  240. CheckForUpdates(false);
  241. }
  242. else
  243. {
  244. TSessionData * Data;
  245. enum { pcNone, pcUpload, pcFullSynchronize, pcSynchronize } ParamCommand;
  246. ParamCommand = pcNone;
  247. AnsiString AutoStartSession;
  248. AnsiString DownloadFile;
  249. bool UseDefaults = false;
  250. // do not check for temp dirs for service tasks (like RegisterAsUrlHandler)
  251. if (OnlyInstance &&
  252. WinConfiguration->TemporaryDirectoryCleanup)
  253. {
  254. TemporaryDirectoryCleanup();
  255. }
  256. WinConfiguration->CheckDefaultTranslation();
  257. if (!Params->Empty)
  258. {
  259. if (Params->FindSwitch("Defaults"))
  260. {
  261. UseDefaults = true;
  262. }
  263. if (Params->FindSwitch("Upload", CommandParams))
  264. {
  265. ParamCommand = pcUpload;
  266. if (CommandParams->Count == 0)
  267. {
  268. throw Exception(NO_UPLOAD_LIST_ERROR);
  269. }
  270. }
  271. if (Params->FindSwitch("UploadIfAny", CommandParams))
  272. {
  273. if (CommandParams->Count > 0)
  274. {
  275. ParamCommand = pcUpload;
  276. }
  277. }
  278. else if (Params->FindSwitch("Synchronize", CommandParams, 2))
  279. {
  280. ParamCommand = pcFullSynchronize;
  281. }
  282. else if (Params->FindSwitch("KeepUpToDate", CommandParams, 2))
  283. {
  284. ParamCommand = pcSynchronize;
  285. }
  286. }
  287. if (Params->ParamCount > 0)
  288. {
  289. AutoStartSession = Params->Param[1];
  290. Params->ParamsProcessed(1, 1);
  291. }
  292. else if (WinConfiguration->EmbeddedSessions && StoredSessions->Count)
  293. {
  294. AutoStartSession = StoredSessions->Sessions[0]->Name;
  295. }
  296. else
  297. {
  298. AutoStartSession = WinConfiguration->AutoStartSession;
  299. }
  300. // from now flash message boxes on background
  301. SetOnForeground(false);
  302. bool Retry;
  303. do
  304. {
  305. Retry = false;
  306. bool Url = false;
  307. Data = GetLoginData(AutoStartSession, Params, DownloadFile, Url);
  308. if (Data)
  309. {
  310. if (Url || Params->FindSwitch("Unsafe"))
  311. {
  312. // prevent any automatic action when URL is provided on
  313. // command-line (the check is duplicated in Console())
  314. if (UseDefaults || Params->FindSwitch("Log"))
  315. {
  316. MessageDialog(LoadStr(UNSAFE_ACTIONS_DISABLED), qtWarning, qaOK);
  317. }
  318. UseDefaults = false;
  319. }
  320. else
  321. {
  322. AnsiString LogFile;
  323. if (Params->FindSwitch("Log", LogFile))
  324. {
  325. Configuration->TemporaryLogging(LogFile);
  326. }
  327. }
  328. if ((Data->FSProtocol == fsExternalSSH) ||
  329. (Data->FSProtocol == fsExternalSFTP))
  330. {
  331. OpenSessionInPutty(
  332. ((Data->FSProtocol == fsExternalSSH) ?
  333. GUIConfiguration->PuttyPath : GUIConfiguration->PSftpPath),
  334. Data, (GUIConfiguration->PuttyPassword ? Data->Password : AnsiString()));
  335. }
  336. else
  337. {
  338. try
  339. {
  340. assert(!TerminalManager->ActiveTerminal);
  341. TerminalManager->NewTerminal(Data);
  342. }
  343. __finally
  344. {
  345. delete Data;
  346. }
  347. try
  348. {
  349. if (!TerminalManager->ConnectActiveTerminal())
  350. {
  351. // do not prompt with login dialog, if connection of
  352. // auto-start session (typically from command line) failed
  353. if (AutoStartSession.IsEmpty())
  354. {
  355. Retry = true;
  356. }
  357. }
  358. else
  359. {
  360. TCustomScpExplorerForm * ScpExplorer = CreateScpExplorer();
  361. try
  362. {
  363. // moved inside try .. __finally, because it can fail as well
  364. TerminalManager->ScpExplorer = ScpExplorer;
  365. if (ParamCommand == pcUpload)
  366. {
  367. Upload(TerminalManager->ActiveTerminal, CommandParams, UseDefaults);
  368. }
  369. else if (ParamCommand == pcFullSynchronize)
  370. {
  371. FullSynchronize(TerminalManager->ActiveTerminal, ScpExplorer,
  372. CommandParams, UseDefaults);
  373. }
  374. else if (ParamCommand == pcSynchronize)
  375. {
  376. Synchronize(TerminalManager->ActiveTerminal, ScpExplorer,
  377. CommandParams, UseDefaults);
  378. }
  379. else if (!DownloadFile.IsEmpty())
  380. {
  381. Download(TerminalManager->ActiveTerminal, DownloadFile,
  382. UseDefaults);
  383. }
  384. Application->Run();
  385. }
  386. __finally
  387. {
  388. TerminalManager->ScpExplorer = NULL;
  389. SAFE_DESTROY(ScpExplorer);
  390. }
  391. }
  392. }
  393. catch (Exception &E)
  394. {
  395. ShowExtendedException(&E);
  396. }
  397. }
  398. }
  399. }
  400. while (Retry);
  401. }
  402. }
  403. __finally
  404. {
  405. delete NonVisualDataModule;
  406. NonVisualDataModule = NULL;
  407. delete GlyphsModule;
  408. GlyphsModule = NULL;
  409. TTerminalManager::DestroyInstance();
  410. delete CommandParams;
  411. }
  412. return 0;
  413. }