WinMain.cpp 13 KB

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